mirror of
https://github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin.git
synced 2026-09-02 20:28:50 +02:00
Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dd322a966a | ||
|
|
f0bb140596 | ||
|
|
46e581eca2 | ||
|
|
50690d1ac7 | ||
|
|
b079073ff6 |
@@ -103,6 +103,10 @@ make run
|
||||
- string
|
||||
- default: "redis:6379"
|
||||
- hostname and port for the Redis service
|
||||
- RedisCachePassword
|
||||
- string
|
||||
- default: ""
|
||||
- Password for the Redis service
|
||||
- UpdateIntervalSeconds
|
||||
- int64
|
||||
- default: 60
|
||||
@@ -183,6 +187,7 @@ http:
|
||||
forwardedHeadersCustomName: X-Custom-Header
|
||||
redisCacheEnabled: false
|
||||
redisCacheHost: "redis:6379"
|
||||
redisCachePassword: password
|
||||
crowdsecLapiTLSCertificateAuthority: |-
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIIEBzCCAu+gAwIBAgICEAAwDQYJKoZIhvcNAQELBQAwgZQxCzAJBgNVBAYTAlVT
|
||||
|
||||
+24
-16
@@ -141,21 +141,29 @@ func New(ctx context.Context, next http.Handler, config *configuration.Config, n
|
||||
},
|
||||
cacheClient: &cache.Client{},
|
||||
}
|
||||
bouncer.cacheClient.New(config.RedisCacheEnabled, config.RedisCacheHost)
|
||||
config.RedisCachePassword, _ = configuration.GetVariable(config, "RedisCachePassword")
|
||||
bouncer.cacheClient.New(config.RedisCacheEnabled, config.RedisCacheHost, config.RedisCachePassword)
|
||||
|
||||
//nolint:nestif
|
||||
if (config.CrowdsecMode == configuration.StreamMode || config.CrowdsecMode == configuration.AloneMode) && ticker == nil {
|
||||
if config.CrowdsecMode == configuration.AloneMode {
|
||||
err = getToken(bouncer)
|
||||
if err != nil {
|
||||
if err := getToken(bouncer); err != nil {
|
||||
logger.Error(fmt.Sprintf("New:getToken %s", err.Error()))
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
ticker = startTicker(config, func() {
|
||||
handleStreamCache(bouncer)
|
||||
})
|
||||
handleStreamCache(bouncer)
|
||||
if err := handleStreamCache(bouncer); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
isStartup = false
|
||||
ticker = startTicker(config, func() {
|
||||
if err := handleStreamCache(bouncer); err != nil {
|
||||
isCrowdsecStreamHealthy = false
|
||||
logger.Error(err.Error())
|
||||
} else {
|
||||
isCrowdsecStreamHealthy = true
|
||||
}
|
||||
})
|
||||
}
|
||||
logger.Debug(fmt.Sprintf("New initialized mode:%s", config.CrowdsecMode))
|
||||
|
||||
@@ -218,7 +226,7 @@ func (bouncer *Bouncer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||
if isCrowdsecStreamHealthy {
|
||||
bouncer.next.ServeHTTP(rw, req)
|
||||
} else {
|
||||
logger.Error(fmt.Sprintf("ServeHTTP isCrowdsecStreamHealthy:false ip:%s", remoteIP))
|
||||
logger.Debug(fmt.Sprintf("ServeHTTP isCrowdsecStreamHealthy:false ip:%s", remoteIP))
|
||||
rw.WriteHeader(http.StatusForbidden)
|
||||
}
|
||||
} else {
|
||||
@@ -348,7 +356,7 @@ func getToken(bouncer *Bouncer) error {
|
||||
return fmt.Errorf("getToken statusCode:%d", login.Code)
|
||||
}
|
||||
|
||||
func handleStreamCache(bouncer *Bouncer) {
|
||||
func handleStreamCache(bouncer *Bouncer) error {
|
||||
// TODO clean properly on exit.
|
||||
// Instead of blocking the goroutine interval for all the secondary node,
|
||||
// if the master service is shut down, other goroutine can take the lead
|
||||
@@ -356,7 +364,10 @@ func handleStreamCache(bouncer *Bouncer) {
|
||||
_, err := bouncer.cacheClient.GetDecision(cacheTimeoutKey)
|
||||
if err == nil {
|
||||
logger.Debug("handleStreamCache:alreadyUpdated")
|
||||
return
|
||||
return nil
|
||||
}
|
||||
if err.Error() != cache.CacheMiss {
|
||||
return err
|
||||
}
|
||||
bouncer.cacheClient.SetDecision(cacheTimeoutKey, false, bouncer.updateInterval-1)
|
||||
streamRouteURL := url.URL{
|
||||
@@ -367,16 +378,12 @@ func handleStreamCache(bouncer *Bouncer) {
|
||||
}
|
||||
body, err := crowdsecQuery(bouncer, streamRouteURL.String(), false)
|
||||
if err != nil {
|
||||
logger.Error(err.Error())
|
||||
isCrowdsecStreamHealthy = false
|
||||
return
|
||||
return err
|
||||
}
|
||||
var stream Stream
|
||||
err = json.Unmarshal(body, &stream)
|
||||
if err != nil {
|
||||
logger.Error(fmt.Sprintf("handleStreamCache:parsingBody %s", err.Error()))
|
||||
isCrowdsecStreamHealthy = false
|
||||
return
|
||||
return fmt.Errorf("handleStreamCache:parsingBody %w", err)
|
||||
}
|
||||
for _, decision := range stream.New {
|
||||
duration, err := time.ParseDuration(decision.Duration)
|
||||
@@ -389,6 +396,7 @@ func handleStreamCache(bouncer *Bouncer) {
|
||||
}
|
||||
logger.Debug("handleStreamCache:updated")
|
||||
isCrowdsecStreamHealthy = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func crowdsecQuery(bouncer *Bouncer, stringURL string, isPost bool) ([]byte, error) {
|
||||
|
||||
+8
-3
@@ -142,14 +142,19 @@ func Test_handleStreamCache(t *testing.T) {
|
||||
bouncer *Bouncer
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
name string
|
||||
args args
|
||||
wantErr bool
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
handleStreamCache(tt.args.bouncer)
|
||||
err := handleStreamCache(tt.args.bouncer)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("handleStreamCache() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,5 +4,5 @@ go 1.19
|
||||
|
||||
require (
|
||||
github.com/leprosus/golang-ttl-map v1.1.7
|
||||
github.com/maxlerebourg/simpleredis v1.0.3
|
||||
github.com/maxlerebourg/simpleredis v1.0.5
|
||||
)
|
||||
|
||||
@@ -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.3 h1:VhXq9bVytWDqD/TS/GjHKayvQb/VUeEql5F+yUbdOiI=
|
||||
github.com/maxlerebourg/simpleredis v1.0.3/go.mod h1:/DH8zOK6kDskSqoX/m5CJJdNGfkIQZd/ERBJgytDDSk=
|
||||
github.com/maxlerebourg/simpleredis v1.0.5 h1:1ubyIpTgIb+dadpILivAxuhZTHOOapEHRhcMakveuYY=
|
||||
github.com/maxlerebourg/simpleredis v1.0.5/go.mod h1:/DH8zOK6kDskSqoX/m5CJJdNGfkIQZd/ERBJgytDDSk=
|
||||
|
||||
Vendored
+2
-2
@@ -82,9 +82,9 @@ type Client struct {
|
||||
}
|
||||
|
||||
// New Initialize cache client.
|
||||
func (client *Client) New(isRedis bool, host string) {
|
||||
func (client *Client) New(isRedis bool, host string, pass string) {
|
||||
if isRedis {
|
||||
redis.Init(host)
|
||||
redis.Init(host, pass)
|
||||
client.cache = &redisCache{}
|
||||
} else {
|
||||
client.cache = &localCache{}
|
||||
|
||||
@@ -55,6 +55,8 @@ type Config struct {
|
||||
ClientTrustedIPs []string `json:"clientTrustedIps,omitempty"`
|
||||
RedisCacheEnabled bool `json:"redisCacheEnabled,omitempty"`
|
||||
RedisCacheHost string `json:"redisCacheHost,omitempty"`
|
||||
RedisCachePassword string `json:"redisCachePassword,omitempty"`
|
||||
RedisCachePasswordFile string `json:"redisCachePasswordFile,omitempty"`
|
||||
}
|
||||
|
||||
func contains(source []string, target string) bool {
|
||||
@@ -83,6 +85,7 @@ func New() *Config {
|
||||
ClientTrustedIPs: []string{},
|
||||
RedisCacheEnabled: false,
|
||||
RedisCacheHost: "redis:6379",
|
||||
RedisCachePassword: "",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,7 +118,7 @@ func GetVariable(config *Config, key string) (string, error) {
|
||||
|
||||
// ValidateParams validate all the param gave by user.
|
||||
//
|
||||
//nolint:gocyclo
|
||||
//nolint:gocyclo,gocognit
|
||||
func ValidateParams(config *Config) error {
|
||||
if err := validateParamsRequired(config); err != nil {
|
||||
return err
|
||||
@@ -128,6 +131,10 @@ func ValidateParams(config *Config) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := GetVariable(config, "RedisCachePassword"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if config.CrowdsecMode == AloneMode {
|
||||
if _, err := GetVariable(config, "CrowdsecCapiMachineID"); err != nil {
|
||||
return err
|
||||
|
||||
+28
-1
@@ -1,2 +1,29 @@
|
||||
# simpleredis
|
||||
Minimal go redis with only get, set and delete operation
|
||||
Minimal go redis with only `get`, `set` and `delete` operation.
|
||||
With **NO** extern dependencies.
|
||||
|
||||
## Example
|
||||
```go
|
||||
import simpleredis "github.com/maxlerebourg/simpleredis"
|
||||
|
||||
var redis simpleredis.SimpleRedis
|
||||
|
||||
redis.Init("redis:6379", "") // redisHost, redisPass
|
||||
|
||||
err := redis.Set("test", []bytes("whatever"), 60), // Set key "test" with "whatever" for 60 seconds
|
||||
if err != nil {
|
||||
...
|
||||
}
|
||||
val, err := redis.Get("test") // get key test
|
||||
if err != nil {
|
||||
// err could be only redis:unreachable, redis:miss or redis:timeout available in simpleredis.RedisUnreachable
|
||||
...
|
||||
}
|
||||
err = redis.Del("test")
|
||||
if err != nil {
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
## Author
|
||||
Max Lerebourg @ [Primadviz.com](https://primadviz.com)
|
||||
+45
-19
@@ -17,10 +17,11 @@ const (
|
||||
RedisUnreachable = "redis:unreachable"
|
||||
RedisMiss = "redis:miss"
|
||||
RedisTimeout = "redis:timeout"
|
||||
RedisNoAuth = "redis:noauth"
|
||||
)
|
||||
|
||||
// A RedisCmd is used to communicate with redis at low level using commands.
|
||||
type RedisCmd struct {
|
||||
// A redisCmd is used to communicate with redis at low level using commands.
|
||||
type redisCmd struct {
|
||||
Command string
|
||||
Name string
|
||||
Data []byte
|
||||
@@ -30,7 +31,8 @@ type RedisCmd struct {
|
||||
|
||||
// A SimpleRedis is used to communicate with redis.
|
||||
type SimpleRedis struct {
|
||||
redisHost string
|
||||
host string
|
||||
pass string
|
||||
}
|
||||
|
||||
func genRedisArray(params ...[]byte) []byte {
|
||||
@@ -49,11 +51,11 @@ func send(wr *textproto.Writer, method string, data []byte) {
|
||||
}
|
||||
}
|
||||
|
||||
func askRedis(hostnamePort string, cmd RedisCmd, channel chan RedisCmd) {
|
||||
func askRedis(sr *SimpleRedis, cmd redisCmd, channel chan redisCmd) {
|
||||
dialer := net.Dialer{Timeout: 2 * time.Second}
|
||||
conn, err := dialer.Dial("tcp", hostnamePort)
|
||||
conn, err := dialer.Dial("tcp", sr.host)
|
||||
if err != nil {
|
||||
channel <- RedisCmd{Error: fmt.Errorf(RedisUnreachable)}
|
||||
channel <- redisCmd{Error: fmt.Errorf(RedisUnreachable)}
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
@@ -65,6 +67,25 @@ func askRedis(hostnamePort string, cmd RedisCmd, channel chan RedisCmd) {
|
||||
writer := textproto.NewWriter(bufio.NewWriter(conn))
|
||||
reader := textproto.NewReader(bufio.NewReader(conn))
|
||||
|
||||
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
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
switch cmd.Command {
|
||||
case "SET":
|
||||
data := genRedisArray([]byte("SET"), []byte(cmd.Name), cmd.Data, []byte("EX"), []byte(fmt.Sprintf("%d", cmd.Duration)))
|
||||
@@ -78,16 +99,20 @@ func askRedis(hostnamePort string, cmd RedisCmd, channel chan RedisCmd) {
|
||||
for {
|
||||
select {
|
||||
case <-time.After(time.Second * 1):
|
||||
channel <- RedisCmd{Error: fmt.Errorf(RedisTimeout)}
|
||||
channel <- redisCmd{Error: fmt.Errorf(RedisTimeout)}
|
||||
return
|
||||
default:
|
||||
read, _ := reader.ReadLineBytes()
|
||||
if string(read) != "$1" {
|
||||
channel <- RedisCmd{Error: fmt.Errorf(RedisMiss)}
|
||||
str := string(read)
|
||||
if strings.Contains(str, "-NOAUTH") {
|
||||
channel <- redisCmd{Error: fmt.Errorf(RedisNoAuth)}
|
||||
return
|
||||
} else if str != "$1" {
|
||||
channel <- redisCmd{Error: fmt.Errorf(RedisMiss)}
|
||||
return
|
||||
}
|
||||
read, _ = reader.ReadLineBytes()
|
||||
channel <- RedisCmd{Data: read}
|
||||
channel <- redisCmd{Data: read}
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -95,18 +120,19 @@ func askRedis(hostnamePort string, cmd RedisCmd, channel chan RedisCmd) {
|
||||
}
|
||||
|
||||
// Init sets the redisHost used to connect to redis.
|
||||
func (sr *SimpleRedis) Init(redisHost string) {
|
||||
sr.redisHost = redisHost
|
||||
func (sr *SimpleRedis) Init(host string, pass string) {
|
||||
sr.host = host
|
||||
sr.pass = pass
|
||||
}
|
||||
|
||||
// Get fetches the value for key name in redis.
|
||||
func (sr *SimpleRedis) Get(name string) ([]byte, error) {
|
||||
redisCmd := RedisCmd{
|
||||
cmd := redisCmd{
|
||||
Command: "GET",
|
||||
Name: name,
|
||||
}
|
||||
channel := make(chan RedisCmd)
|
||||
go askRedis(sr.redisHost, redisCmd, channel)
|
||||
channel := make(chan redisCmd)
|
||||
go askRedis(sr, cmd, channel)
|
||||
resp := <-channel
|
||||
if resp.Error != nil {
|
||||
return nil, resp.Error
|
||||
@@ -116,22 +142,22 @@ func (sr *SimpleRedis) Get(name string) ([]byte, error) {
|
||||
|
||||
// Set updates the value for key name in redis with value data for duration.
|
||||
func (sr *SimpleRedis) Set(name string, data []byte, duration int64) error {
|
||||
redisCmd := RedisCmd{
|
||||
cmd := redisCmd{
|
||||
Command: "SET",
|
||||
Name: name,
|
||||
Data: data,
|
||||
Duration: duration,
|
||||
}
|
||||
go askRedis(sr.redisHost, redisCmd, nil)
|
||||
go askRedis(sr, cmd, nil)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Del removes the key name in redis.
|
||||
func (sr *SimpleRedis) Del(name string) error {
|
||||
redisCmd := RedisCmd{
|
||||
cmd := redisCmd{
|
||||
Command: "DEL",
|
||||
Name: name,
|
||||
}
|
||||
go askRedis(sr.redisHost, redisCmd, nil)
|
||||
go askRedis(sr, cmd, nil)
|
||||
return nil
|
||||
}
|
||||
|
||||
Vendored
+1
-1
@@ -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.3
|
||||
# github.com/maxlerebourg/simpleredis v1.0.5
|
||||
## explicit; go 1.19
|
||||
github.com/maxlerebourg/simpleredis
|
||||
|
||||
Reference in New Issue
Block a user