mirror of
https://github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin.git
synced 2026-09-02 20:28:50 +02:00
Compare commits
7
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6dde683a0a | ||
|
|
be0306eb49 | ||
|
|
b69bd77409 | ||
|
|
64a117d7b0 | ||
|
|
d0ba71c0c8 | ||
|
|
2e780b304d | ||
|
|
4d7615dd19 |
@@ -16,14 +16,13 @@ The crowdsec utility will provide the community blocklist which contains highly
|
||||
|
||||
When used with crowdsec it will leverage the local API which will analyze traefik logs and take decisions on the requests made by users/bots. Malicious actors will be banned based on patterns against your website.
|
||||
|
||||
There are 4 operating modes (CrowdsecMode) for this plugin:
|
||||
There are 3 operating modes (CrowdsecMode) for this plugin:
|
||||
|
||||
| Mode | Description |
|
||||
|------|------|
|
||||
| none | If the client IP is on ban list, it will get a http code 403 response. Otherwise, request will continue as usual. All request call the Crowdsec LAPI |
|
||||
| live | If the client IP is on ban list, it will get a http code 403 response. Otherwise, request will continue as usual. The bouncer can leverage use of a local cache in order to reduce the number of requests made to the Crowdsec LAPI. It will keep in cache the status for each IP that makes queries. |
|
||||
| stream | Stream Streaming mode allows you to keep in the local cache only the Banned IPs, every requests that does not hit the cache is authorized. Every minute, the cache is updated with news from the Crowdsec LAPI. |
|
||||
| alone | Standalone mode, similar to the streaming mode but the blacklisted IPs are fetched on the CAPI. Every 2 hours, the cache is updated with news from the Crowdsec CAPI. It does not include any localy banned IP, but can work without a crowdsec service|
|
||||
|
||||
The recommanded mode for performance is the streaming mode, decisions are updated every 60 sec by default and that's the only communication between traefik and crowdsec. Every requests that happens hits the cache for quick decisions.
|
||||
|
||||
@@ -52,7 +51,7 @@ At each start of synchronisation, the middleware will wait a random number of se
|
||||
- enable the plugin
|
||||
- CrowdsecMode
|
||||
- string
|
||||
- default: `stream`, expected value are: `none`, `live`, `stream`, `alone`
|
||||
- default: `live`, expected value are: `none`, `live`, `stream`
|
||||
- CrowdsecLapiScheme
|
||||
- string
|
||||
- default: `http`, expected value are: `http`, `https`
|
||||
@@ -63,15 +62,6 @@ At each start of synchronisation, the middleware will wait a random number of se
|
||||
- CrowdsecLapiKey
|
||||
- string
|
||||
- Crowdsec LAPI generated key for the bouncer : **must be unique by service**.
|
||||
- CrowdsecCapiLogin
|
||||
- string
|
||||
- Used only in `alone` mode, login for Crowdsec CAPI
|
||||
- CrowdsecCapiPwd
|
||||
- string
|
||||
- Used only in `alone` mode, password for Crowdsec CAPI
|
||||
- CrowdsecCapiScenarios
|
||||
- []string
|
||||
- Used only in `alone` mode, scenarios for Crowdsec CAPI
|
||||
- UpdateIntervalSeconds
|
||||
- int64
|
||||
- default: 60
|
||||
@@ -126,17 +116,10 @@ http:
|
||||
crowdsecLapiKey: privateKey
|
||||
crowdsecLapiHost: crowdsec:8080
|
||||
crowdsecLapiScheme: http
|
||||
crowdsecCapiLogin: login
|
||||
crowdsecCapiPwd: password
|
||||
crowdsecCapiScenarios:
|
||||
- scenario1
|
||||
- scenario2
|
||||
...
|
||||
|
||||
```
|
||||
Except for the crowdsecLapiKey, crowdsecCapiLogin, crowdsecCapiPwd, crowdsecCapiScenarios, these are the default value of the plugin.
|
||||
These are the default values of the plugin except for LapiKey.
|
||||
|
||||
#### Generate LAPI KEY (exept for `alone` mode)
|
||||
#### Generate LAPI KEY
|
||||
You need to generate a crowdsec API key for the LAPI.
|
||||
You can follow the documentation here: https://docs.crowdsec.net/docs/user_guides/lapi_mgmt/
|
||||
|
||||
@@ -165,36 +148,6 @@ You can then run all the containers:
|
||||
docker-compose up -d
|
||||
```
|
||||
|
||||
#### Generate CAPI credentials (only for `alone` mode)
|
||||
You need to create a crowdsec API credentials for the CAPI.
|
||||
You can follow the documentation here: https://docs.crowdsec.net/docs/central_api/intro
|
||||
|
||||
```bash
|
||||
curl -X POST "https://api.crowdsec.net/v2/watchers" -H "accept: application/json" -H "Content-Type: application/json" -d "{ \"password\": \"PASSWORD\", \"machine_id\": \"LOGIN\"}"
|
||||
```
|
||||
|
||||
These CAPI credentials must be set in your docker-compose.yml or in your config files
|
||||
```yaml
|
||||
...
|
||||
traefik:
|
||||
command:
|
||||
...
|
||||
- "--experimental.plugins.bouncer.modulename=github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin"
|
||||
- "--experimental.plugins.bouncer.version=v1.0.0"
|
||||
...
|
||||
whoami:
|
||||
labels:
|
||||
- "traefik.http.middlewares.crowdsec.plugin.bouncer.crowdseccapilogin=LOGIN"
|
||||
- "traefik.http.middlewares.crowdsec.plugin.bouncer.crowdseccapipwd=PASSWORD"
|
||||
- "traefik.http.middlewares.crowdsec.plugin.bouncer.crowdseccapiscenarios=scenario1, scenario2, ..."
|
||||
- "traefik.http.middlewares.crowdsec.plugin.bouncer.enabled=true"
|
||||
```
|
||||
|
||||
You can then run all the containers:
|
||||
```bash
|
||||
docker-compose up -d
|
||||
```
|
||||
|
||||
#### Add manually an IP to the blocklist (testing purpose)
|
||||
|
||||
```bash
|
||||
|
||||
+73
-170
@@ -12,7 +12,6 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
@@ -20,7 +19,6 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
aloneMode = "alone"
|
||||
streamMode = "stream"
|
||||
liveMode = "live"
|
||||
noneMode = "none"
|
||||
@@ -28,37 +26,29 @@ const (
|
||||
crowdsecCapiHeader = "Authorization"
|
||||
crowdsecLapiRoute = "v1/decisions"
|
||||
crowdsecLapiStreamRoute = "v1/decisions/stream"
|
||||
crowdsecCapiLogin = "v2/watchers/login"
|
||||
crowdsecCapiDecisions = "v2/decisions/stream"
|
||||
cacheBannedValue = "t"
|
||||
cacheNoBannedValue = "f"
|
||||
)
|
||||
|
||||
// Config the plugin configuration.
|
||||
type Config struct {
|
||||
Enabled bool `json:"enabled,omitempty"`
|
||||
CrowdsecMode string `json:"crowdsecMode,omitempty"`
|
||||
CrowdsecLapiScheme string `json:"crowdsecLapiScheme,omitempty"`
|
||||
CrowdsecLapiHost string `json:"crowdsecLapiHost,omitempty"`
|
||||
CrowdsecLapiKey string `json:"crowdsecLapiKey,omitempty"`
|
||||
CrowdsecCapiLogin string `json:"crowdsecCapiLogin,omitempty"`
|
||||
CrowdsecCapiPwd string `json:"crowdsecCapiPwd,omitempty"`
|
||||
CrowdsecCapiScenarios []string `json:"crowdsecCapiScenarios,omitempty"`
|
||||
UpdateIntervalSeconds int64 `json:"updateIntervalSeconds,omitempty"`
|
||||
DefaultDecisionSeconds int64 `json:"defaultDecisionSeconds,omitempty"`
|
||||
Enabled bool `json:"enabled,omitempty"`
|
||||
CrowdsecMode string `json:"crowdsecMode,omitempty"`
|
||||
CrowdsecLapiScheme string `json:"crowdsecLapiScheme,omitempty"`
|
||||
CrowdsecLapiHost string `json:"crowdsecLapiHost,omitempty"`
|
||||
CrowdsecLapiKey string `json:"crowdsecLapiKey,omitempty"`
|
||||
UpdateIntervalSeconds int64 `json:"updateIntervalSeconds,omitempty"`
|
||||
DefaultDecisionSeconds int64 `json:"defaultDecisionSeconds,omitempty"`
|
||||
}
|
||||
|
||||
// CreateConfig creates the default plugin configuration.
|
||||
func CreateConfig() *Config {
|
||||
return &Config{
|
||||
Enabled: false,
|
||||
CrowdsecMode: streamMode,
|
||||
CrowdsecMode: liveMode,
|
||||
CrowdsecLapiScheme: "http",
|
||||
CrowdsecLapiHost: "crowdsec:8080",
|
||||
CrowdsecLapiKey: "",
|
||||
CrowdsecCapiLogin: "",
|
||||
CrowdsecCapiPwd: "",
|
||||
CrowdsecCapiScenarios: []string{},
|
||||
UpdateIntervalSeconds: 60,
|
||||
DefaultDecisionSeconds: 60,
|
||||
}
|
||||
@@ -78,9 +68,6 @@ type Bouncer struct {
|
||||
crowdsecMode string
|
||||
updateInterval int64
|
||||
defaultDecisionTimeout int64
|
||||
crowdsecLogin string
|
||||
crowdsecPwd string
|
||||
crowdsecScenarios []string
|
||||
client *http.Client
|
||||
cache *ttl_map.Heap
|
||||
}
|
||||
@@ -103,9 +90,6 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
|
||||
crowdsecScheme: config.CrowdsecLapiScheme,
|
||||
crowdsecHost: config.CrowdsecLapiHost,
|
||||
crowdsecKey: config.CrowdsecLapiKey,
|
||||
crowdsecLogin: config.CrowdsecCapiLogin,
|
||||
crowdsecPwd: config.CrowdsecCapiPwd,
|
||||
crowdsecScenarios: config.CrowdsecCapiScenarios,
|
||||
updateInterval: config.UpdateIntervalSeconds,
|
||||
defaultDecisionTimeout: config.DefaultDecisionSeconds,
|
||||
client: &http.Client{
|
||||
@@ -117,14 +101,14 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
|
||||
},
|
||||
cache: ttl_map.New(),
|
||||
}
|
||||
if config.CrowdsecMode == streamMode || config.CrowdsecMode == aloneMode {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
if config.CrowdsecMode == aloneMode {
|
||||
getToken(bouncer)
|
||||
}
|
||||
ticker := time.NewTicker(time.Duration(config.UpdateIntervalSeconds) * time.Second)
|
||||
if config.CrowdsecMode == streamMode {
|
||||
go func() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
timeout := rand.Int63n(30)
|
||||
logger(fmt.Sprintf("Wait: %v", timeout))
|
||||
time.Sleep(time.Duration(timeout) * time.Second)
|
||||
go handleStreamCache(bouncer)
|
||||
ticker := time.NewTicker(time.Duration(config.UpdateIntervalSeconds) * time.Second)
|
||||
for range ticker.C {
|
||||
go handleStreamCache(bouncer)
|
||||
}
|
||||
@@ -134,9 +118,9 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
|
||||
}
|
||||
|
||||
// ServeHTTP principal function of plugin.
|
||||
func (a *Bouncer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||
if !a.enabled {
|
||||
a.next.ServeHTTP(rw, req)
|
||||
func (bouncer *Bouncer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||
if !bouncer.enabled {
|
||||
bouncer.next.ServeHTTP(rw, req)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -144,31 +128,31 @@ func (a *Bouncer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||
remoteHost, _, err := net.SplitHostPort(req.RemoteAddr)
|
||||
if err != nil {
|
||||
logger(fmt.Sprintf("failed to extract ip from remote address: %v", err))
|
||||
a.next.ServeHTTP(rw, req)
|
||||
bouncer.next.ServeHTTP(rw, req)
|
||||
return
|
||||
}
|
||||
|
||||
if a.crowdsecMode == streamMode || a.crowdsecMode == aloneMode || a.crowdsecMode == liveMode {
|
||||
isBanned, err := getDecision(a, remoteHost)
|
||||
if bouncer.crowdsecMode == streamMode || bouncer.crowdsecMode == liveMode {
|
||||
isBanned, err := getDecision(bouncer, remoteHost)
|
||||
if err == nil {
|
||||
if isBanned {
|
||||
rw.WriteHeader(http.StatusForbidden)
|
||||
} else {
|
||||
a.next.ServeHTTP(rw, req)
|
||||
bouncer.next.ServeHTTP(rw, req)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Right here if we cannot join the stream we forbid the request to go on.
|
||||
if a.crowdsecMode == streamMode || a.crowdsecMode == aloneMode {
|
||||
if a.crowdsecStreamHealthy {
|
||||
a.next.ServeHTTP(rw, req)
|
||||
if bouncer.crowdsecMode == streamMode {
|
||||
if bouncer.crowdsecStreamHealthy {
|
||||
bouncer.next.ServeHTTP(rw, req)
|
||||
} else {
|
||||
rw.WriteHeader(http.StatusForbidden)
|
||||
}
|
||||
} else {
|
||||
handleNoStreamCache(a, rw, req, remoteHost)
|
||||
handleNoStreamCache(bouncer, rw, req, remoteHost)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -205,8 +189,8 @@ func logger(str string) {
|
||||
}
|
||||
|
||||
func contains(source []string, target string) bool {
|
||||
for _, a := range source {
|
||||
if a == target {
|
||||
for _, item := range source {
|
||||
if item == target {
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -215,8 +199,8 @@ func contains(source []string, target string) bool {
|
||||
|
||||
// Get Decision check in the cache if the IP has the banned / not banned value.
|
||||
// Otherwise return with an error to add the IP in cache if we are on.
|
||||
func getDecision(a *Bouncer, clientIP string) (bool, error) {
|
||||
banned, isCached := a.cache.Get(clientIP)
|
||||
func getDecision(bouncer *Bouncer, clientIP string) (bool, error) {
|
||||
banned, isCached := bouncer.cache.Get(clientIP)
|
||||
bannedString, isValid := banned.(string)
|
||||
if isCached && isValid && len(bannedString) > 0 {
|
||||
return bannedString == cacheBannedValue, nil
|
||||
@@ -224,31 +208,31 @@ func getDecision(a *Bouncer, clientIP string) (bool, error) {
|
||||
return false, fmt.Errorf("no cache data")
|
||||
}
|
||||
|
||||
func setDecision(a *Bouncer, clientIP string, isBanned bool, duration int64) {
|
||||
if a.crowdsecMode == noneMode {
|
||||
func setDecision(bouncer *Bouncer, clientIP string, isBanned bool, duration int64) {
|
||||
if bouncer.crowdsecMode == noneMode {
|
||||
return
|
||||
}
|
||||
if isBanned {
|
||||
logger(fmt.Sprintf("%v banned", clientIP))
|
||||
a.cache.Set(clientIP, cacheBannedValue, duration)
|
||||
bouncer.cache.Set(clientIP, cacheBannedValue, duration)
|
||||
} else {
|
||||
a.cache.Set(clientIP, cacheNoBannedValue, duration)
|
||||
bouncer.cache.Set(clientIP, cacheNoBannedValue, duration)
|
||||
}
|
||||
}
|
||||
|
||||
func handleNoStreamCache(a *Bouncer, rw http.ResponseWriter, req *http.Request, remoteHost string) {
|
||||
func handleNoStreamCache(bouncer *Bouncer, rw http.ResponseWriter, req *http.Request, remoteHost string) {
|
||||
// We are now in none or live mode.
|
||||
routeURL := url.URL{
|
||||
Scheme: a.crowdsecScheme,
|
||||
Host: a.crowdsecHost,
|
||||
Scheme: bouncer.crowdsecScheme,
|
||||
Host: bouncer.crowdsecHost,
|
||||
Path: crowdsecLapiRoute,
|
||||
RawQuery: fmt.Sprintf("ip=%v&banned=true", remoteHost),
|
||||
}
|
||||
body := crowdsecQuery(a, routeURL.String(), false)
|
||||
body := crowdsecQuery(bouncer, routeURL.String())
|
||||
|
||||
if bytes.Equal(body, []byte("null")) {
|
||||
setDecision(a, remoteHost, false, a.defaultDecisionTimeout)
|
||||
a.next.ServeHTTP(rw, req)
|
||||
setDecision(bouncer, remoteHost, false, bouncer.defaultDecisionTimeout)
|
||||
bouncer.next.ServeHTTP(rw, req)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -260,8 +244,8 @@ func handleNoStreamCache(a *Bouncer, rw http.ResponseWriter, req *http.Request,
|
||||
return
|
||||
}
|
||||
if len(decisions) == 0 {
|
||||
setDecision(a, remoteHost, false, a.defaultDecisionTimeout)
|
||||
a.next.ServeHTTP(rw, req)
|
||||
setDecision(bouncer, remoteHost, false, bouncer.defaultDecisionTimeout)
|
||||
bouncer.next.ServeHTTP(rw, req)
|
||||
return
|
||||
}
|
||||
rw.WriteHeader(http.StatusForbidden)
|
||||
@@ -270,114 +254,50 @@ func handleNoStreamCache(a *Bouncer, rw http.ResponseWriter, req *http.Request,
|
||||
logger(fmt.Sprintf("failed to parse duration: %s", err))
|
||||
return
|
||||
}
|
||||
setDecision(a, remoteHost, true, int64(duration.Seconds()))
|
||||
setDecision(bouncer, remoteHost, true, int64(duration.Seconds()))
|
||||
}
|
||||
|
||||
func waitStream() {
|
||||
logger("Start Wait Stream")
|
||||
sec := rand.Int63n(30)
|
||||
logger(fmt.Sprintf("Sec=%v", sec))
|
||||
time.Sleep(time.Duration(sec) * time.Second)
|
||||
logger("End Wait Stream")
|
||||
}
|
||||
|
||||
func handleStreamCache(a *Bouncer) {
|
||||
logger(fmt.Sprintf("Start handleStreamCache with health=%v", a.crowdsecStreamHealthy))
|
||||
func handleStreamCache(bouncer *Bouncer) {
|
||||
// TODO clean properly on exit.
|
||||
|
||||
waitStream()
|
||||
// wait random number of sec
|
||||
var rawQuery string
|
||||
var path string
|
||||
if a.crowdsecMode == aloneMode {
|
||||
rawQuery = ""
|
||||
path = crowdsecCapiDecisions
|
||||
} else {
|
||||
rawQuery = fmt.Sprintf("startup=%t", !a.crowdsecStreamHealthy)
|
||||
path = crowdsecLapiStreamRoute
|
||||
}
|
||||
streamRouteURL := url.URL{
|
||||
Scheme: a.crowdsecScheme,
|
||||
Host: a.crowdsecHost,
|
||||
Path: path,
|
||||
RawQuery: rawQuery,
|
||||
Scheme: bouncer.crowdsecScheme,
|
||||
Host: bouncer.crowdsecHost,
|
||||
Path: crowdsecLapiStreamRoute,
|
||||
RawQuery: fmt.Sprintf("startup=%t", !bouncer.crowdsecStreamHealthy),
|
||||
}
|
||||
body := crowdsecQuery(a, streamRouteURL.String(), false)
|
||||
body := crowdsecQuery(bouncer, streamRouteURL.String())
|
||||
var stream Stream
|
||||
err := json.Unmarshal(body, &stream)
|
||||
if err != nil {
|
||||
logger(fmt.Sprintf("error while parsing body: %s", err))
|
||||
a.crowdsecStreamHealthy = false
|
||||
bouncer.crowdsecStreamHealthy = false
|
||||
return
|
||||
}
|
||||
for _, decision := range stream.New {
|
||||
duration, err := time.ParseDuration(decision.Duration)
|
||||
if err == nil {
|
||||
setDecision(a, decision.Value, true, int64(duration.Seconds()))
|
||||
setDecision(bouncer, decision.Value, true, int64(duration.Seconds()))
|
||||
}
|
||||
}
|
||||
for _, decision := range stream.Deleted {
|
||||
a.cache.Del(decision.Value)
|
||||
bouncer.cache.Del(decision.Value)
|
||||
}
|
||||
a.crowdsecStreamHealthy = true
|
||||
logger(fmt.Sprintf("End handleStreamCache with health=%v", a.crowdsecStreamHealthy))
|
||||
bouncer.crowdsecStreamHealthy = true
|
||||
}
|
||||
|
||||
func getToken(a *Bouncer) {
|
||||
loginURL := url.URL{
|
||||
Scheme: a.crowdsecScheme,
|
||||
Host: a.crowdsecHost,
|
||||
Path: crowdsecCapiLogin,
|
||||
}
|
||||
body := crowdsecQuery(a, loginURL.String(), true)
|
||||
var login Login
|
||||
err := json.Unmarshal(body, &login)
|
||||
if err != nil {
|
||||
logger(fmt.Sprintf("error while parsing body: %s", err))
|
||||
a.crowdsecStreamHealthy = false
|
||||
return
|
||||
}
|
||||
if login.Code == 200 && len(login.Token) > 0 {
|
||||
a.crowdsecKey = login.Token
|
||||
}
|
||||
}
|
||||
|
||||
func crowdsecQuery(a *Bouncer, stringURL string, isPost bool) []byte {
|
||||
func crowdsecQuery(bouncer *Bouncer, stringURL string) []byte {
|
||||
var req *http.Request
|
||||
if isPost {
|
||||
data := []byte(fmt.Sprintf(
|
||||
`{"machine_id": "%v","password": "%v","scenarios": ["%v"]}`,
|
||||
a.crowdsecLogin,
|
||||
a.crowdsecPwd,
|
||||
strings.Join(a.crowdsecScenarios, `","`),
|
||||
))
|
||||
req, _ = http.NewRequest(http.MethodPost, stringURL, bytes.NewBuffer(data))
|
||||
} else {
|
||||
req, _ = http.NewRequest(http.MethodGet, stringURL, nil)
|
||||
}
|
||||
if a.crowdsecMode == aloneMode {
|
||||
req.Header.Add(crowdsecCapiHeader, a.crowdsecKey)
|
||||
} else {
|
||||
req.Header.Add(crowdsecLapiHeader, a.crowdsecKey)
|
||||
}
|
||||
res, err := a.client.Do(req)
|
||||
req, _ = http.NewRequest(http.MethodGet, stringURL, nil)
|
||||
req.Header.Add(crowdsecLapiHeader, bouncer.crowdsecKey)
|
||||
res, err := bouncer.client.Do(req)
|
||||
if err != nil {
|
||||
logger(fmt.Sprintf("error while fetching %v: %s", stringURL, err))
|
||||
a.crowdsecStreamHealthy = false
|
||||
bouncer.crowdsecStreamHealthy = false
|
||||
return nil
|
||||
}
|
||||
if res.StatusCode == http.StatusUnauthorized && a.crowdsecMode == aloneMode {
|
||||
oldToken := a.crowdsecKey
|
||||
getToken(a)
|
||||
if oldToken == a.crowdsecKey {
|
||||
a.crowdsecStreamHealthy = false
|
||||
return nil
|
||||
}
|
||||
return crowdsecQuery(a, stringURL, false)
|
||||
}
|
||||
if res.StatusCode != http.StatusOK {
|
||||
logger(fmt.Sprintf("error while fetching %v, status code: %d", stringURL, res.StatusCode))
|
||||
a.crowdsecStreamHealthy = false
|
||||
bouncer.crowdsecStreamHealthy = false
|
||||
return nil
|
||||
}
|
||||
defer func(body io.ReadCloser) {
|
||||
@@ -389,43 +309,26 @@ func crowdsecQuery(a *Bouncer, stringURL string, isPost bool) []byte {
|
||||
body, err := ioutil.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
logger(fmt.Sprintf("error while reading body: %s", err))
|
||||
a.crowdsecStreamHealthy = false
|
||||
bouncer.crowdsecStreamHealthy = false
|
||||
return nil
|
||||
}
|
||||
return body
|
||||
}
|
||||
|
||||
func validateParams(config *Config) error {
|
||||
var requiredStrings map[string]string
|
||||
if config.CrowdsecMode == aloneMode {
|
||||
requiredStrings = map[string]string{
|
||||
"CrowdsecCapiLogin": config.CrowdsecLapiScheme,
|
||||
"CrowdsecCapiPwd": config.CrowdsecLapiHost,
|
||||
}
|
||||
for _, val := range config.CrowdsecCapiScenarios {
|
||||
if len(val) == 0 {
|
||||
return fmt.Errorf("CrowdsecCapiScenarios: one or more scenario are empty")
|
||||
}
|
||||
}
|
||||
config.UpdateIntervalSeconds = 7200
|
||||
config.CrowdsecLapiKey = ""
|
||||
config.CrowdsecLapiScheme = "https"
|
||||
config.CrowdsecLapiHost = "api.crowdsec.net"
|
||||
} else {
|
||||
requiredStrings = map[string]string{
|
||||
"CrowdsecLapiScheme": config.CrowdsecLapiScheme,
|
||||
"CrowdsecLapiHost": config.CrowdsecLapiHost,
|
||||
"CrowdsecLapiKey": config.CrowdsecLapiKey,
|
||||
"CrowdsecMode": config.CrowdsecMode,
|
||||
}
|
||||
requiredInt := map[string]int64{
|
||||
"UpdateIntervalSeconds": config.UpdateIntervalSeconds,
|
||||
"DefaultDecisionSeconds": config.DefaultDecisionSeconds,
|
||||
}
|
||||
for key, val := range requiredInt {
|
||||
if val < 1 {
|
||||
return fmt.Errorf("%v: cannot be less than 1", key)
|
||||
}
|
||||
requiredStrings := map[string]string{
|
||||
"CrowdsecLapiScheme": config.CrowdsecLapiScheme,
|
||||
"CrowdsecLapiHost": config.CrowdsecLapiHost,
|
||||
"CrowdsecLapiKey": config.CrowdsecLapiKey,
|
||||
"CrowdsecMode": config.CrowdsecMode,
|
||||
}
|
||||
requiredInt := map[string]int64{
|
||||
"UpdateIntervalSeconds": config.UpdateIntervalSeconds,
|
||||
"DefaultDecisionSeconds": config.DefaultDecisionSeconds,
|
||||
}
|
||||
for key, val := range requiredInt {
|
||||
if val < 1 {
|
||||
return fmt.Errorf("%v: cannot be less than 1", key)
|
||||
}
|
||||
}
|
||||
for key, val := range requiredStrings {
|
||||
@@ -433,7 +336,7 @@ func validateParams(config *Config) error {
|
||||
return fmt.Errorf("%v: cannot be empty", key)
|
||||
}
|
||||
}
|
||||
if !contains([]string{noneMode, liveMode, streamMode, aloneMode}, config.CrowdsecMode) {
|
||||
if !contains([]string{noneMode, liveMode, streamMode}, config.CrowdsecMode) {
|
||||
return fmt.Errorf("CrowdsecMode: must be one of 'none', 'live' or 'stream'")
|
||||
}
|
||||
if !contains([]string{"http", "https"}, config.CrowdsecLapiScheme) {
|
||||
|
||||
+4
-6
@@ -1,22 +1,20 @@
|
||||
package crowdsec_bouncer_traefik_plugin_test
|
||||
package crowdsec_bouncer_traefik_plugin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
crowdsec_bouncer_traefik_plugin "github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin"
|
||||
)
|
||||
|
||||
func TestCrowdSec(t *testing.T) {
|
||||
cfg := crowdsec_bouncer_traefik_plugin.CreateConfig()
|
||||
cfg.CrowdsecLapiKey = "caca"
|
||||
cfg := CreateConfig()
|
||||
cfg.CrowdsecLapiKey = "test"
|
||||
|
||||
ctx := context.Background()
|
||||
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {})
|
||||
|
||||
handler, err := crowdsec_bouncer_traefik_plugin.New(ctx, next, cfg, "demo-plugin")
|
||||
handler, err := New(ctx, next, cfg, "demo-plugin")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user