add redis database selection (#100)

*  add redis database selection

* 📝 update docs

* 📝 readme
This commit is contained in:
maxlerebourg
2023-05-25 17:20:14 +02:00
committed by GitHub
parent abae7ee028
commit 0c2668d578
10 changed files with 57 additions and 33 deletions
+5
View File
@@ -107,6 +107,10 @@ make run
- string - string
- default: "" - default: ""
- Password for the Redis service - Password for the Redis service
- RedisCacheDatabase
- string
- default: ""
- Database selection for the Redis service
- UpdateIntervalSeconds - UpdateIntervalSeconds
- int64 - int64
- default: 60 - default: 60
@@ -189,6 +193,7 @@ http:
redisCacheEnabled: false redisCacheEnabled: false
redisCacheHost: "redis:6379" redisCacheHost: "redis:6379"
redisCachePassword: password redisCachePassword: password
redisCacheDatabase: "5"
crowdsecLapiTLSCertificateAuthority: |- crowdsecLapiTLSCertificateAuthority: |-
-----BEGIN CERTIFICATE----- -----BEGIN CERTIFICATE-----
MIIEBzCCAu+gAwIBAgICEAAwDQYJKoZIhvcNAQELBQAwgZQxCzAJBgNVBAYTAlVT MIIEBzCCAu+gAwIBAgICEAAwDQYJKoZIhvcNAQELBQAwgZQxCzAJBgNVBAYTAlVT
+6 -1
View File
@@ -142,7 +142,12 @@ func New(ctx context.Context, next http.Handler, config *configuration.Config, n
cacheClient: &cache.Client{}, cacheClient: &cache.Client{},
} }
config.RedisCachePassword, _ = configuration.GetVariable(config, "RedisCachePassword") 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.StreamMode || config.CrowdsecMode == configuration.AloneMode) && ticker == nil {
if config.CrowdsecMode == configuration.AloneMode { if config.CrowdsecMode == configuration.AloneMode {
@@ -47,7 +47,7 @@ services:
- "traefik.http.middlewares.crowdsec-foo.plugin.bouncer.crowdseclapikey=40796d93c2958f9e58345514e67740e5" - "traefik.http.middlewares.crowdsec-foo.plugin.bouncer.crowdseclapikey=40796d93c2958f9e58345514e67740e5"
- "traefik.http.middlewares.crowdsec-foo.plugin.bouncer.rediscacheenabled=true" - "traefik.http.middlewares.crowdsec-foo.plugin.bouncer.rediscacheenabled=true"
# Contact redis-unsecure without a password # 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" - "traefik.http.middlewares.crowdsec-foo.plugin.bouncer.loglevel=DEBUG"
whoami-redis-secure: whoami-redis-secure:
@@ -67,9 +67,9 @@ services:
# crowdseclapikey must be uniq to the middleware attached to the service # 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.crowdseclapikey=44c36dac5c4140af9f06f397508e82c7"
- "traefik.http.middlewares.crowdsec-bar.plugin.bouncer.rediscacheenabled=true" - "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 # 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" - "traefik.http.middlewares.crowdsec-bar.plugin.bouncer.loglevel=DEBUG"
+1 -1
View File
@@ -4,5 +4,5 @@ go 1.19
require ( require (
github.com/leprosus/golang-ttl-map v1.1.7 github.com/leprosus/golang-ttl-map v1.1.7
github.com/maxlerebourg/simpleredis v1.0.6 github.com/maxlerebourg/simpleredis v1.0.7
) )
+2 -2
View File
@@ -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 h1:cF4AAFDDnJTFSV+/42sKLhmMluvLdRlCGS2UaifH6UM=
github.com/leprosus/golang-ttl-map v1.1.7/go.mod h1:4QWHJPeVBbrkhOhXdhCv9IEiyj/YzkO04/iexy4vSe0= 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.7 h1:d53p3GOIgQtxxWuqsWMGTJ0dZjP8UhiDX8L1q2QZ/vY=
github.com/maxlerebourg/simpleredis v1.0.6/go.mod h1:/DH8zOK6kDskSqoX/m5CJJdNGfkIQZd/ERBJgytDDSk= github.com/maxlerebourg/simpleredis v1.0.7/go.mod h1:/DH8zOK6kDskSqoX/m5CJJdNGfkIQZd/ERBJgytDDSk=
+2 -2
View File
@@ -82,9 +82,9 @@ type Client struct {
} }
// New Initialize cache client. // 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 { if isRedis {
redis.Init(host, pass) redis.Init(host, pass, database)
client.cache = &redisCache{} client.cache = &redisCache{}
} else { } else {
client.cache = &localCache{} client.cache = &localCache{}
+2
View File
@@ -57,6 +57,7 @@ type Config struct {
RedisCacheHost string `json:"redisCacheHost,omitempty"` RedisCacheHost string `json:"redisCacheHost,omitempty"`
RedisCachePassword string `json:"redisCachePassword,omitempty"` RedisCachePassword string `json:"redisCachePassword,omitempty"`
RedisCachePasswordFile string `json:"redisCachePasswordFile,omitempty"` RedisCachePasswordFile string `json:"redisCachePasswordFile,omitempty"`
RedisCacheDatabase string `json:"redisCacheDatabase,omitempty"`
} }
func contains(source []string, target string) bool { func contains(source []string, target string) bool {
@@ -86,6 +87,7 @@ func New() *Config {
RedisCacheEnabled: false, RedisCacheEnabled: false,
RedisCacheHost: "redis:6379", RedisCacheHost: "redis:6379",
RedisCachePassword: "", RedisCachePassword: "",
RedisCacheDatabase: "",
} }
} }
+1 -1
View File
@@ -9,7 +9,7 @@ import simpleredis "github.com/maxlerebourg/simpleredis"
var redis simpleredis.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 err := redis.Set("test", []bytes("whatever"), 60), // Set key "test" with "whatever" for 60 seconds
if err != nil { if err != nil {
+34 -22
View File
@@ -31,8 +31,9 @@ type redisCmd struct {
// A SimpleRedis is used to communicate with redis. // A SimpleRedis is used to communicate with redis.
type SimpleRedis struct { type SimpleRedis struct {
host string host string
pass string pass string
database string
} }
func genRedisArray(params ...[]byte) []byte { 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} dialer := net.Dialer{Timeout: 2 * time.Second}
conn, err := dialer.Dial("tcp", sr.host) conn, err := dialer.Dial("tcp", sr.host)
if err != nil { if err != nil {
@@ -70,21 +89,13 @@ func askRedis(sr *SimpleRedis, cmd redisCmd, channel chan redisCmd) {
if sr.pass != "" { if sr.pass != "" {
data := genRedisArray([]byte("AUTH"), []byte(sr.pass)) data := genRedisArray([]byte("AUTH"), []byte(sr.pass))
send(writer, "auth", data) send(writer, "auth", data)
for { sr.waitRedis(reader, channel)
select { }
case <-time.After(time.Second * 1):
channel <- redisCmd{Error: fmt.Errorf(RedisTimeout)} if sr.database != "" {
return data := genRedisArray([]byte("SELECT"), []byte(sr.database))
default: send(writer, "select", data)
read, _ := reader.ReadLineBytes() sr.waitRedis(reader, channel)
if string(read) != "+OK" {
channel <- redisCmd{Error: fmt.Errorf(RedisNoAuth)}
return
}
}
// breaks out of for
break
}
} }
switch cmd.Command { 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. // 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.host = host
sr.pass = pass sr.pass = pass
sr.database = database
} }
// Get fetches the value for key name in redis. // Get fetches the value for key name in redis.
@@ -133,7 +145,7 @@ func (sr *SimpleRedis) Get(name string) ([]byte, error) {
Name: name, Name: name,
} }
channel := make(chan redisCmd) channel := make(chan redisCmd)
go askRedis(sr, cmd, channel) go sr.askRedis(cmd, channel)
resp := <-channel resp := <-channel
if resp.Error != nil { if resp.Error != nil {
return nil, resp.Error return nil, resp.Error
@@ -149,7 +161,7 @@ func (sr *SimpleRedis) Set(name string, data []byte, duration int64) error {
Data: data, Data: data,
Duration: duration, Duration: duration,
} }
go askRedis(sr, cmd, nil) go sr.askRedis(cmd, nil)
return nil return nil
} }
@@ -159,6 +171,6 @@ func (sr *SimpleRedis) Del(name string) error {
Command: "DEL", Command: "DEL",
Name: name, Name: name,
} }
go askRedis(sr, cmd, nil) go sr.askRedis(cmd, nil)
return nil return nil
} }
+1 -1
View File
@@ -1,6 +1,6 @@
# github.com/leprosus/golang-ttl-map v1.1.7 # github.com/leprosus/golang-ttl-map v1.1.7
## explicit; go 1.15 ## explicit; go 1.15
github.com/leprosus/golang-ttl-map github.com/leprosus/golang-ttl-map
# github.com/maxlerebourg/simpleredis v1.0.6 # github.com/maxlerebourg/simpleredis v1.0.7
## explicit; go 1.19 ## explicit; go 1.19
github.com/maxlerebourg/simpleredis github.com/maxlerebourg/simpleredis