From 0c2668d5786e57fdb47d21dcf2bacbab2650cfdc Mon Sep 17 00:00:00 2001 From: maxlerebourg Date: Thu, 25 May 2023 17:20:14 +0200 Subject: [PATCH] :sparkles: add redis database selection (#100) * :sparkles: add redis database selection * :memo: update docs * :memo: readme --- README.md | 5 ++ bouncer.go | 7 ++- exemples/redis-cache/docker-compose.redis.yml | 6 +- go.mod | 2 +- go.sum | 4 +- pkg/cache/cache.go | 4 +- pkg/configuration/configuration.go | 2 + .../maxlerebourg/simpleredis/README.md | 2 +- .../maxlerebourg/simpleredis/simpleredis.go | 56 +++++++++++-------- vendor/modules.txt | 2 +- 10 files changed, 57 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 752efdf..0e1c0ec 100644 --- a/README.md +++ b/README.md @@ -107,6 +107,10 @@ make run - string - default: "" - Password for the Redis service +- RedisCacheDatabase + - string + - default: "" + - Database selection for the Redis service - UpdateIntervalSeconds - int64 - default: 60 @@ -189,6 +193,7 @@ http: redisCacheEnabled: false redisCacheHost: "redis:6379" redisCachePassword: password + redisCacheDatabase: "5" crowdsecLapiTLSCertificateAuthority: |- -----BEGIN CERTIFICATE----- MIIEBzCCAu+gAwIBAgICEAAwDQYJKoZIhvcNAQELBQAwgZQxCzAJBgNVBAYTAlVT diff --git a/bouncer.go b/bouncer.go index bd14b39..ac86a9c 100644 --- a/bouncer.go +++ b/bouncer.go @@ -142,7 +142,12 @@ func New(ctx context.Context, next http.Handler, config *configuration.Config, n cacheClient: &cache.Client{}, } config.RedisCachePassword, _ = configuration.GetVariable(config, "RedisCachePassword") - bouncer.cacheClient.New(config.RedisCacheEnabled, config.RedisCacheHost, config.RedisCachePassword) + bouncer.cacheClient.New( + config.RedisCacheEnabled, + config.RedisCacheHost, + config.RedisCachePassword, + config.RedisCacheDatabase, + ) if (config.CrowdsecMode == configuration.StreamMode || config.CrowdsecMode == configuration.AloneMode) && ticker == nil { if config.CrowdsecMode == configuration.AloneMode { diff --git a/exemples/redis-cache/docker-compose.redis.yml b/exemples/redis-cache/docker-compose.redis.yml index 7c3ffa6..dde2030 100644 --- a/exemples/redis-cache/docker-compose.redis.yml +++ b/exemples/redis-cache/docker-compose.redis.yml @@ -47,7 +47,7 @@ services: - "traefik.http.middlewares.crowdsec-foo.plugin.bouncer.crowdseclapikey=40796d93c2958f9e58345514e67740e5" - "traefik.http.middlewares.crowdsec-foo.plugin.bouncer.rediscacheenabled=true" # Contact redis-unsecure without a password - - "traefik.http.middlewares.crowdsec-foo.plugin.bouncer.redisCacheHost=redis-insecure:6379" + - "traefik.http.middlewares.crowdsec-foo.plugin.bouncer.rediscachehost=redis-insecure:6379" - "traefik.http.middlewares.crowdsec-foo.plugin.bouncer.loglevel=DEBUG" whoami-redis-secure: @@ -67,9 +67,9 @@ services: # crowdseclapikey must be uniq to the middleware attached to the service - "traefik.http.middlewares.crowdsec-bar.plugin.bouncer.crowdseclapikey=44c36dac5c4140af9f06f397508e82c7" - "traefik.http.middlewares.crowdsec-bar.plugin.bouncer.rediscacheenabled=true" - - "traefik.http.middlewares.crowdsec-bar.plugin.bouncer.redisCachePassword=FIXME" + - "traefik.http.middlewares.crowdsec-bar.plugin.bouncer.rediscachepassword=FIXME" # Contact redis-secure with password - - "traefik.http.middlewares.crowdsec-bar.plugin.bouncer.redisCacheHost=redis-secure:6379" + - "traefik.http.middlewares.crowdsec-bar.plugin.bouncer.rediscachehost=redis-secure:6379" - "traefik.http.middlewares.crowdsec-bar.plugin.bouncer.loglevel=DEBUG" diff --git a/go.mod b/go.mod index cb79c13..3cce6fb 100644 --- a/go.mod +++ b/go.mod @@ -4,5 +4,5 @@ go 1.19 require ( github.com/leprosus/golang-ttl-map v1.1.7 - github.com/maxlerebourg/simpleredis v1.0.6 + github.com/maxlerebourg/simpleredis v1.0.7 ) diff --git a/go.sum b/go.sum index 18ebd7d..23c6f4e 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,4 @@ github.com/leprosus/golang-ttl-map v1.1.7 h1:cF4AAFDDnJTFSV+/42sKLhmMluvLdRlCGS2UaifH6UM= github.com/leprosus/golang-ttl-map v1.1.7/go.mod h1:4QWHJPeVBbrkhOhXdhCv9IEiyj/YzkO04/iexy4vSe0= -github.com/maxlerebourg/simpleredis v1.0.6 h1:dKd0hgKk7uGKjujWUMuPTVOAONYdlCys5Iqh6w3dOU4= -github.com/maxlerebourg/simpleredis v1.0.6/go.mod h1:/DH8zOK6kDskSqoX/m5CJJdNGfkIQZd/ERBJgytDDSk= +github.com/maxlerebourg/simpleredis v1.0.7 h1:d53p3GOIgQtxxWuqsWMGTJ0dZjP8UhiDX8L1q2QZ/vY= +github.com/maxlerebourg/simpleredis v1.0.7/go.mod h1:/DH8zOK6kDskSqoX/m5CJJdNGfkIQZd/ERBJgytDDSk= diff --git a/pkg/cache/cache.go b/pkg/cache/cache.go index a2c0b3f..d14a82f 100644 --- a/pkg/cache/cache.go +++ b/pkg/cache/cache.go @@ -82,9 +82,9 @@ type Client struct { } // New Initialize cache client. -func (client *Client) New(isRedis bool, host string, pass string) { +func (client *Client) New(isRedis bool, host, pass, database string) { if isRedis { - redis.Init(host, pass) + redis.Init(host, pass, database) client.cache = &redisCache{} } else { client.cache = &localCache{} diff --git a/pkg/configuration/configuration.go b/pkg/configuration/configuration.go index 713b0e5..a5bb102 100644 --- a/pkg/configuration/configuration.go +++ b/pkg/configuration/configuration.go @@ -57,6 +57,7 @@ type Config struct { RedisCacheHost string `json:"redisCacheHost,omitempty"` RedisCachePassword string `json:"redisCachePassword,omitempty"` RedisCachePasswordFile string `json:"redisCachePasswordFile,omitempty"` + RedisCacheDatabase string `json:"redisCacheDatabase,omitempty"` } func contains(source []string, target string) bool { @@ -86,6 +87,7 @@ func New() *Config { RedisCacheEnabled: false, RedisCacheHost: "redis:6379", RedisCachePassword: "", + RedisCacheDatabase: "", } } diff --git a/vendor/github.com/maxlerebourg/simpleredis/README.md b/vendor/github.com/maxlerebourg/simpleredis/README.md index 284dade..0bb98fe 100644 --- a/vendor/github.com/maxlerebourg/simpleredis/README.md +++ b/vendor/github.com/maxlerebourg/simpleredis/README.md @@ -9,7 +9,7 @@ import simpleredis "github.com/maxlerebourg/simpleredis" var redis simpleredis.SimpleRedis -redis.Init("redis:6379", "") // redisHost, redisPass +redis.Init("redis:6379", "", "") // redisHost, redisPass, redisDatabase err := redis.Set("test", []bytes("whatever"), 60), // Set key "test" with "whatever" for 60 seconds if err != nil { diff --git a/vendor/github.com/maxlerebourg/simpleredis/simpleredis.go b/vendor/github.com/maxlerebourg/simpleredis/simpleredis.go index dff464c..dc36f26 100644 --- a/vendor/github.com/maxlerebourg/simpleredis/simpleredis.go +++ b/vendor/github.com/maxlerebourg/simpleredis/simpleredis.go @@ -31,8 +31,9 @@ type redisCmd struct { // A SimpleRedis is used to communicate with redis. type SimpleRedis struct { - host string - pass string + host string + pass string + database string } func genRedisArray(params ...[]byte) []byte { @@ -51,7 +52,25 @@ func send(wr *textproto.Writer, method string, data []byte) { } } -func askRedis(sr *SimpleRedis, cmd redisCmd, channel chan redisCmd) { +func (sr *SimpleRedis) waitRedis(reader *textproto.Reader, channel chan redisCmd) { + for { + select { + case <-time.After(time.Second * 1): + channel <- redisCmd{Error: fmt.Errorf(RedisTimeout)} + return + default: + read, _ := reader.ReadLineBytes() + if string(read) != "+OK" { + channel <- redisCmd{Error: fmt.Errorf(RedisNoAuth)} + return + } + } + // breaks out of for + break + } +} + +func (sr *SimpleRedis) askRedis(cmd redisCmd, channel chan redisCmd) { dialer := net.Dialer{Timeout: 2 * time.Second} conn, err := dialer.Dial("tcp", sr.host) if err != nil { @@ -70,21 +89,13 @@ func askRedis(sr *SimpleRedis, cmd redisCmd, channel chan redisCmd) { if sr.pass != "" { data := genRedisArray([]byte("AUTH"), []byte(sr.pass)) send(writer, "auth", data) - for { - select { - case <-time.After(time.Second * 1): - channel <- redisCmd{Error: fmt.Errorf(RedisTimeout)} - return - default: - read, _ := reader.ReadLineBytes() - if string(read) != "+OK" { - channel <- redisCmd{Error: fmt.Errorf(RedisNoAuth)} - return - } - } - // breaks out of for - break - } + sr.waitRedis(reader, channel) + } + + if sr.database != "" { + data := genRedisArray([]byte("SELECT"), []byte(sr.database)) + send(writer, "select", data) + sr.waitRedis(reader, channel) } switch cmd.Command { @@ -121,9 +132,10 @@ func askRedis(sr *SimpleRedis, cmd redisCmd, channel chan redisCmd) { } // Init sets the redisHost used to connect to redis. -func (sr *SimpleRedis) Init(host string, pass string) { +func (sr *SimpleRedis) Init(host, pass, database string) { sr.host = host sr.pass = pass + sr.database = database } // Get fetches the value for key name in redis. @@ -133,7 +145,7 @@ func (sr *SimpleRedis) Get(name string) ([]byte, error) { Name: name, } channel := make(chan redisCmd) - go askRedis(sr, cmd, channel) + go sr.askRedis(cmd, channel) resp := <-channel if resp.Error != nil { return nil, resp.Error @@ -149,7 +161,7 @@ func (sr *SimpleRedis) Set(name string, data []byte, duration int64) error { Data: data, Duration: duration, } - go askRedis(sr, cmd, nil) + go sr.askRedis(cmd, nil) return nil } @@ -159,6 +171,6 @@ func (sr *SimpleRedis) Del(name string) error { Command: "DEL", Name: name, } - go askRedis(sr, cmd, nil) + go sr.askRedis(cmd, nil) return nil } diff --git a/vendor/modules.txt b/vendor/modules.txt index dccd597..36b8c06 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1,6 +1,6 @@ # github.com/leprosus/golang-ttl-map v1.1.7 ## explicit; go 1.15 github.com/leprosus/golang-ttl-map -# github.com/maxlerebourg/simpleredis v1.0.6 +# github.com/maxlerebourg/simpleredis v1.0.7 ## explicit; go 1.19 github.com/maxlerebourg/simpleredis