[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
pull/657/head
James Elliott 2020-02-28 10:14:44 +10:00 committed by GitHub
parent 829757d3bc
commit fc05b973ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 6 deletions

View File

@ -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.
#

View File

@ -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.

View File

@ -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",

View File

@ -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)
}