diff --git a/config.template.yml b/config.template.yml index a3c81d888..30c2422e3 100644 --- a/config.template.yml +++ b/config.template.yml @@ -223,6 +223,8 @@ session: port: 6379 # This secret can also be set using the env variables AUTHELIA_SESSION_REDIS_PASSWORD password: authelia + # This is the Redis DB Index https://redis.io/commands/select (sometimes referred to as database number, DB, etc). + database_index: 0 # Configuration of the authentication regulation mechanism. # diff --git a/internal/configuration/schema/session.go b/internal/configuration/schema/session.go index 3e0cc2654..797cce82f 100644 --- a/internal/configuration/schema/session.go +++ b/internal/configuration/schema/session.go @@ -2,9 +2,10 @@ package schema // RedisSessionConfiguration represents the configuration related to redis session store. type RedisSessionConfiguration struct { - Host string `mapstructure:"host"` - Port int64 `mapstructure:"port"` - Password string `mapstructure:"password"` + Host string `mapstructure:"host"` + Port int64 `mapstructure:"port"` + Password string `mapstructure:"password"` + DatabaseIndex int `mapstructure:"database_index"` } // SessionConfiguration represents the configuration related to user sessions. diff --git a/internal/session/provider_config.go b/internal/session/provider_config.go index fea588df9..c8e078b85 100644 --- a/internal/session/provider_config.go +++ b/internal/session/provider_config.go @@ -43,9 +43,11 @@ func NewProviderConfig(configuration schema.SessionConfiguration) ProviderConfig if configuration.Redis != nil { providerName = "redis" providerConfig = &redis.Config{ - Host: configuration.Redis.Host, - Port: configuration.Redis.Port, - Password: configuration.Redis.Password, + Host: configuration.Redis.Host, + Port: configuration.Redis.Port, + Password: configuration.Redis.Password, + // DbNumber is the fasthttp/session property for the Redis DB Index + DbNumber: configuration.Redis.DatabaseIndex, PoolSize: 8, IdleTimeout: 300, KeyPrefix: "authelia-session", diff --git a/internal/session/provider_config_test.go b/internal/session/provider_config_test.go index 15718c886..fa61b11c1 100644 --- a/internal/session/provider_config_test.go +++ b/internal/session/provider_config_test.go @@ -54,4 +54,25 @@ func TestShouldCreateRedisSessionProvider(t *testing.T) { assert.Equal(t, "redis.example.com", pConfig.Host) assert.Equal(t, int64(6379), pConfig.Port) assert.Equal(t, "pass", pConfig.Password) + // DbNumber is the fasthttp/session property for the Redis DB Index + assert.Equal(t, 0, pConfig.DbNumber) +} + +func TestShouldSetDbNumber(t *testing.T) { + configuration := schema.SessionConfiguration{} + configuration.Domain = "example.com" + configuration.Name = "my_session" + configuration.Expiration = 40 + configuration.Redis = &schema.RedisSessionConfiguration{ + Host: "redis.example.com", + Port: 6379, + Password: "pass", + DatabaseIndex: 5, + } + providerConfig := NewProviderConfig(configuration) + assert.Equal(t, "redis", providerConfig.providerName) + assert.IsType(t, &redis.Config{}, providerConfig.providerConfig) + pConfig := providerConfig.providerConfig.(*redis.Config) + // DbNumber is the fasthttp/session property for the Redis DB Index + assert.Equal(t, 5, pConfig.DbNumber) }