From fc05b973adc06808dfccfd8aed7d14675bdeb6bf Mon Sep 17 00:00:00 2001 From: James Elliott Date: Fri, 28 Feb 2020 10:14:44 +1000 Subject: [PATCH] [FEATURE] Redis DB Index Selection (#653) * [FEATURE] Redis DB Number Selection - Allow users to specify the DB number - This is so users who use their redis for multiple purposes can have clear demarcation between their data * revert: import order * Add default/example to config template with docs * Set DB Index property name to be more clear --- config.template.yml | 2 ++ internal/configuration/schema/session.go | 7 ++++--- internal/session/provider_config.go | 8 +++++--- internal/session/provider_config_test.go | 21 +++++++++++++++++++++ 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/config.template.yml b/config.template.yml index a3c81d88..30c2422e 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 3e0cc265..797cce82 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 fea588df..c8e078b8 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 15718c88..fa61b11c 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) }