mirror of
https://github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin.git
synced 2026-07-21 11:38:59 +02:00
🍱 lint
This commit is contained in:
@@ -142,6 +142,8 @@ This LAPI key must be set where is noted FIXME-LAPI-KEY in the docker-compose-te
|
|||||||
whoami:
|
whoami:
|
||||||
labels:
|
labels:
|
||||||
- "traefik.http.middlewares.crowdsec.plugin.bouncer.crowdseclapikey=FIXME-LAPI-KEY"
|
- "traefik.http.middlewares.crowdsec.plugin.bouncer.crowdseclapikey=FIXME-LAPI-KEY"
|
||||||
|
- "traefik.http.middlewares.crowdsec.plugin.bouncer.crowdseclapischeme=http"
|
||||||
|
- "traefik.http.middlewares.crowdsec.plugin.bouncer.crowdseclapihost=crowdsec:8080"
|
||||||
...
|
...
|
||||||
crowdsec:
|
crowdsec:
|
||||||
environment:
|
environment:
|
||||||
|
|||||||
+24
-20
@@ -19,14 +19,18 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
crowdsecLapiHeader = "X-Api-Key"
|
aloneMode = "alone"
|
||||||
crowdsecCapiHeader = "Authorization"
|
streamMode = "stream"
|
||||||
crowdsecLapiRoute = "v1/decisions"
|
liveMode = "live"
|
||||||
crowdsecLapiStreamRoute = "v1/decisions/stream"
|
noneMode = "none"
|
||||||
crowdsecCapiLogin = "v2/watchers/login"
|
crowdsecLapiHeader = "X-Api-Key"
|
||||||
crowdsecCapiDecisions = "v2/decisions/stream"
|
crowdsecCapiHeader = "Authorization"
|
||||||
cacheBannedValue = "t"
|
crowdsecLapiRoute = "v1/decisions"
|
||||||
cacheNoBannedValue = "f"
|
crowdsecLapiStreamRoute = "v1/decisions/stream"
|
||||||
|
crowdsecCapiLogin = "v2/watchers/login"
|
||||||
|
crowdsecCapiDecisions = "v2/decisions/stream"
|
||||||
|
cacheBannedValue = "t"
|
||||||
|
cacheNoBannedValue = "f"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config the plugin configuration.
|
// Config the plugin configuration.
|
||||||
@@ -47,7 +51,7 @@ type Config struct {
|
|||||||
func CreateConfig() *Config {
|
func CreateConfig() *Config {
|
||||||
return &Config{
|
return &Config{
|
||||||
Enabled: false,
|
Enabled: false,
|
||||||
CrowdsecMode: "alone",
|
CrowdsecMode: streamMode,
|
||||||
CrowdsecLapiScheme: "http",
|
CrowdsecLapiScheme: "http",
|
||||||
CrowdsecLapiHost: "crowdsec:8080",
|
CrowdsecLapiHost: "crowdsec:8080",
|
||||||
CrowdsecLapiKey: "",
|
CrowdsecLapiKey: "",
|
||||||
@@ -83,7 +87,7 @@ type Bouncer struct {
|
|||||||
// New creates the crowdsec bouncer plugin.
|
// New creates the crowdsec bouncer plugin.
|
||||||
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
|
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
|
||||||
var requiredStrings map[string]string
|
var requiredStrings map[string]string
|
||||||
if config.CrowdsecMode == "alone" {
|
if config.CrowdsecMode == aloneMode {
|
||||||
requiredStrings = map[string]string{
|
requiredStrings = map[string]string{
|
||||||
"CrowdsecCapiLogin": config.CrowdsecLapiScheme,
|
"CrowdsecCapiLogin": config.CrowdsecLapiScheme,
|
||||||
"CrowdsecCapiPwd": config.CrowdsecLapiHost,
|
"CrowdsecCapiPwd": config.CrowdsecLapiHost,
|
||||||
@@ -119,7 +123,7 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
|
|||||||
return nil, fmt.Errorf("%v: cannot be empty", key)
|
return nil, fmt.Errorf("%v: cannot be empty", key)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !contains([]string{"none", "live", "stream", "alone"}, config.CrowdsecMode) {
|
if !contains([]string{noneMode, liveMode, streamMode, aloneMode}, config.CrowdsecMode) {
|
||||||
return nil, fmt.Errorf("CrowdsecMode: must be one of 'none', 'live' or 'stream'")
|
return nil, fmt.Errorf("CrowdsecMode: must be one of 'none', 'live' or 'stream'")
|
||||||
}
|
}
|
||||||
if !contains([]string{"http", "https"}, config.CrowdsecLapiScheme) {
|
if !contains([]string{"http", "https"}, config.CrowdsecLapiScheme) {
|
||||||
@@ -160,9 +164,9 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
|
|||||||
cache: ttl_map.New(),
|
cache: ttl_map.New(),
|
||||||
}
|
}
|
||||||
// if we are on a stream mode, we fetch in a go routine every minute the new decisions.
|
// if we are on a stream mode, we fetch in a go routine every minute the new decisions.
|
||||||
if config.CrowdsecMode == "stream" {
|
if config.CrowdsecMode == streamMode {
|
||||||
go handleStreamCache(bouncer, true)
|
go handleStreamCache(bouncer, true)
|
||||||
} else if config.CrowdsecMode == "alone" {
|
} else if config.CrowdsecMode == aloneMode {
|
||||||
getToken(bouncer)
|
getToken(bouncer)
|
||||||
time.AfterFunc(10*time.Second, func() {
|
time.AfterFunc(10*time.Second, func() {
|
||||||
handleStreamCache(bouncer, false)
|
handleStreamCache(bouncer, false)
|
||||||
@@ -186,7 +190,7 @@ func (a *Bouncer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if a.crowdsecMode == "stream" || a.crowdsecMode == "live" {
|
if a.crowdsecMode == streamMode || a.crowdsecMode == liveMode {
|
||||||
isBanned, err := getDecision(a.cache, remoteHost)
|
isBanned, err := getDecision(a.cache, remoteHost)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if isBanned {
|
if isBanned {
|
||||||
@@ -199,7 +203,7 @@ func (a *Bouncer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Right here if we cannot join the stream we forbid the request to go on.
|
// Right here if we cannot join the stream we forbid the request to go on.
|
||||||
if a.crowdsecMode == "stream" {
|
if a.crowdsecMode == streamMode {
|
||||||
if a.crowdsecStreamHealthy {
|
if a.crowdsecStreamHealthy {
|
||||||
a.next.ServeHTTP(rw, req)
|
a.next.ServeHTTP(rw, req)
|
||||||
} else {
|
} else {
|
||||||
@@ -278,7 +282,7 @@ func handleNoStreamCache(a *Bouncer, rw http.ResponseWriter, req *http.Request,
|
|||||||
body := crowdsecQuery(a, routeURL.String(), false)
|
body := crowdsecQuery(a, routeURL.String(), false)
|
||||||
|
|
||||||
if bytes.Equal(body, []byte("null")) {
|
if bytes.Equal(body, []byte("null")) {
|
||||||
if a.crowdsecMode == "live" {
|
if a.crowdsecMode == liveMode {
|
||||||
setDecision(a.cache, remoteHost, false, a.defaultDecisionTimeout)
|
setDecision(a.cache, remoteHost, false, a.defaultDecisionTimeout)
|
||||||
}
|
}
|
||||||
a.next.ServeHTTP(rw, req)
|
a.next.ServeHTTP(rw, req)
|
||||||
@@ -293,7 +297,7 @@ func handleNoStreamCache(a *Bouncer, rw http.ResponseWriter, req *http.Request,
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if len(decisions) == 0 {
|
if len(decisions) == 0 {
|
||||||
if a.crowdsecMode == "live" {
|
if a.crowdsecMode == liveMode {
|
||||||
setDecision(a.cache, remoteHost, false, a.defaultDecisionTimeout)
|
setDecision(a.cache, remoteHost, false, a.defaultDecisionTimeout)
|
||||||
}
|
}
|
||||||
a.next.ServeHTTP(rw, req)
|
a.next.ServeHTTP(rw, req)
|
||||||
@@ -315,7 +319,7 @@ func handleStreamCache(a *Bouncer, initialized bool) {
|
|||||||
})
|
})
|
||||||
var rawQuery string
|
var rawQuery string
|
||||||
var path string
|
var path string
|
||||||
if a.crowdsecMode == "alone" {
|
if a.crowdsecMode == aloneMode {
|
||||||
rawQuery = ""
|
rawQuery = ""
|
||||||
path = crowdsecCapiDecisions
|
path = crowdsecCapiDecisions
|
||||||
} else {
|
} else {
|
||||||
@@ -380,7 +384,7 @@ func crowdsecQuery(a *Bouncer, stringURL string, isPost bool) []byte {
|
|||||||
} else {
|
} else {
|
||||||
req, _ = http.NewRequest(http.MethodGet, stringURL, nil)
|
req, _ = http.NewRequest(http.MethodGet, stringURL, nil)
|
||||||
}
|
}
|
||||||
if a.crowdsecMode == "alone" {
|
if a.crowdsecMode == aloneMode {
|
||||||
req.Header.Add(crowdsecCapiHeader, a.crowdsecKey)
|
req.Header.Add(crowdsecCapiHeader, a.crowdsecKey)
|
||||||
} else {
|
} else {
|
||||||
req.Header.Add(crowdsecLapiHeader, a.crowdsecKey)
|
req.Header.Add(crowdsecLapiHeader, a.crowdsecKey)
|
||||||
@@ -391,7 +395,7 @@ func crowdsecQuery(a *Bouncer, stringURL string, isPost bool) []byte {
|
|||||||
a.crowdsecStreamHealthy = false
|
a.crowdsecStreamHealthy = false
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if res.StatusCode == http.StatusUnauthorized && a.crowdsecMode == "alone" {
|
if res.StatusCode == http.StatusUnauthorized && a.crowdsecMode == aloneMode {
|
||||||
oldToken := a.crowdsecKey
|
oldToken := a.crowdsecKey
|
||||||
getToken(a)
|
getToken(a)
|
||||||
if oldToken == a.crowdsecKey {
|
if oldToken == a.crowdsecKey {
|
||||||
|
|||||||
Reference in New Issue
Block a user