Compare commits

..
13 Commits
Author SHA1 Message Date
Max Lerebourg f876458bb2 fix logger 2022-10-01 20:00:42 +02:00
Max Lerebourg 6c6a80efb4 fix 2022-10-01 19:44:06 +02:00
MathieuHa cc0261aa86 Add clean message when stream polling start 2022-10-01 19:42:22 +02:00
MathieuHa 465851b536 Fix name of docker volumes 2022-10-01 19:36:18 +02:00
maxlerebourg 9e92aeb449 Merge pull request #13 from maxlerebourg/fixMax
 fix loop
2022-10-01 19:33:09 +02:00
MathieuHa 37c7648854 Clean docker-compose and aquis.yaml 2022-10-01 19:28:09 +02:00
Max Lerebourg dd704d4274 fix 2022-10-01 19:23:20 +02:00
Max Lerebourg 1e4508f091 fix log 2022-10-01 19:18:12 +02:00
Max Lerebourg 89e8b5631e fix loop 2022-10-01 19:12:48 +02:00
Max Lerebourg 68adb31065 fix loop 2022-10-01 19:09:22 +02:00
mathieuHa 1ebde35693 Update README.md 2022-09-30 21:54:51 +02:00
mathieuHa 26913c5f7e Update README.md 2022-09-30 21:54:21 +02:00
mathieuHa ec0ffabc21 Update Readme
Show modes in a table
Fix gramar
2022-09-30 21:53:54 +02:00
6 changed files with 89 additions and 77 deletions
+5 -1
View File
@@ -1,2 +1,6 @@
.idea/ .idea/
.DS_Store .DS_Store
config
db
logs
docker-compose.dev.yml
+9 -17
View File
@@ -17,21 +17,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. 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 4 operating modes (CrowdsecMode) for this plugin:
- 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. | Mode | Description |
Otherwise, request will continue as usual. |------|------|
The bouncer can leverage use of a local cache in order to reduce the number | 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 |
of requests made to the Crowdsec LAPI. It will keep in cache the status for | 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. |
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 | Streaming mode but the blacklisted IPs are fetched on the CAPI. Every 2 hours, the cache is updated with news from the Crowdsec CAPI.|
- 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 -> Streaming mode but the blacklisted IPs are fetched on the CAPI.
Every 2 hours, the cache is updated with news from the Crowdsec CAPI.
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. 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.
@@ -66,11 +58,11 @@ The recommanded mode for performance is the streaming mode, decisions are update
- UpdateIntervalSeconds - UpdateIntervalSeconds
- int64 - int64
- default: 60 - default: 60
- User only in `stream` mode, interval between fetching blacklisted IPs from LAPI - Used only in `stream` mode, interval between fetching blacklisted IPs from LAPI
- DefaultDecisionSeconds - DefaultDecisionSeconds
- int64 - int64
- default: 60 - default: 60
- User only in `live` mode, decision duration of accepted IPs - Used only in `live` mode, decision duration of accepted IPs
### Configuration ### Configuration
@@ -233,4 +225,4 @@ We initially run into this project: https://github.com/fbonalair/traefik-crowdse
It was using traefik and forward auth middleware to verify every requests. It was using traefik and forward auth middleware to verify every requests.
They had to go through a webserver which then contacts of another webservice (the crowdsec LAPI) to make a decision based on the source IP. They had to go through a webserver which then contacts of another webservice (the crowdsec LAPI) to make a decision based on the source IP.
We initially proposed some improvement by implementing a streaming mode and a local cache. We initially proposed some improvement by implementing a streaming mode and a local cache.
With the Traefik hackathon we deciced to implement our solution directly as a traefik plugin which could be found by every one on plugins.traefik.io and be more performant. With the Traefik hackathon we deciced to implement our solution directly as a traefik plugin which could be found by every one on plugins.traefik.io and be more performant.
+1 -1
View File
@@ -1,4 +1,4 @@
filenames: filenames:
- /var/log/traefik/*.log - /var/log/traefik/access.log
labels: labels:
type: traefik type: traefik
+46 -42
View File
@@ -51,15 +51,15 @@ type Config struct {
func CreateConfig() *Config { func CreateConfig() *Config {
return &Config{ return &Config{
Enabled: false, Enabled: false,
CrowdsecMode: streamMode, CrowdsecMode: aloneMode,
CrowdsecLapiScheme: "http", CrowdsecLapiScheme: "http",
CrowdsecLapiHost: "crowdsec:8080", CrowdsecLapiHost: "crowdsec:8080",
CrowdsecLapiKey: "", CrowdsecLapiKey: "",
CrowdsecCapiLogin: "", CrowdsecCapiLogin: "",
CrowdsecCapiPwd: "", CrowdsecCapiPwd: "",
CrowdsecCapiScenarios: []string{}, CrowdsecCapiScenarios: []string{},
UpdateIntervalSeconds: 60, UpdateIntervalSeconds: 10,
DefaultDecisionSeconds: 60, DefaultDecisionSeconds: 10,
} }
} }
@@ -116,14 +116,17 @@ 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 config.CrowdsecMode == streamMode || config.CrowdsecMode == aloneMode {
if config.CrowdsecMode == streamMode { if config.CrowdsecMode == aloneMode {
go handleStreamCache(bouncer, true) getToken(bouncer)
} else if config.CrowdsecMode == aloneMode { }
getToken(bouncer) ticker := time.NewTicker(time.Duration(config.UpdateIntervalSeconds) * time.Second)
time.AfterFunc(10*time.Second, func() { go func() {
handleStreamCache(bouncer, false) go handleStreamCache(bouncer)
}) for range ticker.C {
go handleStreamCache(bouncer)
}
}()
} }
return bouncer, nil return bouncer, nil
} }
@@ -138,13 +141,13 @@ func (a *Bouncer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
// TODO Make sur remote address does not include the port. // TODO Make sur remote address does not include the port.
remoteHost, _, err := net.SplitHostPort(req.RemoteAddr) remoteHost, _, err := net.SplitHostPort(req.RemoteAddr)
if err != nil { if err != nil {
log.Printf("failed to extract ip from remote address: %v", err) logger(fmt.Sprintf("failed to extract ip from remote address: %v", err))
a.next.ServeHTTP(rw, req) a.next.ServeHTTP(rw, req)
return return
} }
if a.crowdsecMode == streamMode || a.crowdsecMode == liveMode { if a.crowdsecMode == streamMode || a.crowdsecMode == aloneMode || a.crowdsecMode == liveMode {
isBanned, err := getDecision(a.cache, remoteHost) isBanned, err := getDecision(a, remoteHost)
if err == nil { if err == nil {
if isBanned { if isBanned {
rw.WriteHeader(http.StatusForbidden) rw.WriteHeader(http.StatusForbidden)
@@ -156,7 +159,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 == streamMode { if a.crowdsecMode == streamMode || a.crowdsecMode == aloneMode {
if a.crowdsecStreamHealthy { if a.crowdsecStreamHealthy {
a.next.ServeHTTP(rw, req) a.next.ServeHTTP(rw, req)
} else { } else {
@@ -195,6 +198,10 @@ type Login struct {
Expire string `json:"expire"` Expire string `json:"expire"`
} }
func logger(str string) {
log.Printf("Crowdsec Bouncer Traefik Plugin - %s", str)
}
func contains(source []string, target string) bool { func contains(source []string, target string) bool {
for _, a := range source { for _, a := range source {
if a == target { if a == target {
@@ -206,8 +213,8 @@ func contains(source []string, target string) bool {
// Get Decision check in the cache if the IP has the banned / not banned value. // 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. // Otherwise return with an error to add the IP in cache if we are on.
func getDecision(cache *ttl_map.Heap, clientIP string) (bool, error) { func getDecision(a *Bouncer, clientIP string) (bool, error) {
banned, isCached := cache.Get(clientIP) banned, isCached := a.cache.Get(clientIP)
bannedString, isValid := banned.(string) bannedString, isValid := banned.(string)
if isCached && isValid && len(bannedString) > 0 { if isCached && isValid && len(bannedString) > 0 {
return bannedString == cacheBannedValue, nil return bannedString == cacheBannedValue, nil
@@ -215,12 +222,15 @@ func getDecision(cache *ttl_map.Heap, clientIP string) (bool, error) {
return false, fmt.Errorf("no cache data") return false, fmt.Errorf("no cache data")
} }
func setDecision(cache *ttl_map.Heap, clientIP string, isBanned bool, duration int64) { func setDecision(a *Bouncer, clientIP string, isBanned bool, duration int64) {
if a.crowdsecMode == noneMode {
return
}
if isBanned { if isBanned {
log.Printf("%v banned", clientIP) logger(fmt.Sprintf("%v banned", clientIP))
cache.Set(clientIP, cacheBannedValue, duration) a.cache.Set(clientIP, cacheBannedValue, duration)
} else { } else {
cache.Set(clientIP, cacheNoBannedValue, duration) a.cache.Set(clientIP, cacheNoBannedValue, duration)
} }
} }
@@ -235,9 +245,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 == liveMode { setDecision(a, remoteHost, false, a.defaultDecisionTimeout)
setDecision(a.cache, remoteHost, false, a.defaultDecisionTimeout)
}
a.next.ServeHTTP(rw, req) a.next.ServeHTTP(rw, req)
return return
} }
@@ -245,38 +253,34 @@ func handleNoStreamCache(a *Bouncer, rw http.ResponseWriter, req *http.Request,
var decisions []Decision var decisions []Decision
err := json.Unmarshal(body, &decisions) err := json.Unmarshal(body, &decisions)
if err != nil { if err != nil {
log.Printf("failed to parse body: %s", err) logger(fmt.Sprintf("failed to parse body: %s", err))
rw.WriteHeader(http.StatusForbidden) rw.WriteHeader(http.StatusForbidden)
return return
} }
if len(decisions) == 0 { if len(decisions) == 0 {
if a.crowdsecMode == liveMode { setDecision(a, remoteHost, false, a.defaultDecisionTimeout)
setDecision(a.cache, remoteHost, false, a.defaultDecisionTimeout)
}
a.next.ServeHTTP(rw, req) a.next.ServeHTTP(rw, req)
return return
} }
rw.WriteHeader(http.StatusForbidden) rw.WriteHeader(http.StatusForbidden)
duration, err := time.ParseDuration(decisions[0].Duration) duration, err := time.ParseDuration(decisions[0].Duration)
if err != nil { if err != nil {
log.Printf("failed to parse duration: %s", err) logger(fmt.Sprintf("failed to parse duration: %s", err))
return return
} }
setDecision(a.cache, remoteHost, true, int64(duration.Seconds())) setDecision(a, remoteHost, true, int64(duration.Seconds()))
} }
func handleStreamCache(a *Bouncer, initialized bool) { func handleStreamCache(a *Bouncer) {
logger(fmt.Sprintf("Start handleStreamCache with health=%v", a.crowdsecStreamHealthy))
// TODO clean properly on exit. // TODO clean properly on exit.
time.AfterFunc(time.Duration(a.updateInterval)*time.Second, func() {
handleStreamCache(a, false)
})
var rawQuery string var rawQuery string
var path string var path string
if a.crowdsecMode == aloneMode { if a.crowdsecMode == aloneMode {
rawQuery = "" rawQuery = ""
path = crowdsecCapiDecisions path = crowdsecCapiDecisions
} else { } else {
rawQuery = fmt.Sprintf("startup=%t", initialized) rawQuery = fmt.Sprintf("startup=%t", !a.crowdsecStreamHealthy)
path = crowdsecLapiStreamRoute path = crowdsecLapiStreamRoute
} }
streamRouteURL := url.URL{ streamRouteURL := url.URL{
@@ -289,14 +293,14 @@ func handleStreamCache(a *Bouncer, initialized bool) {
var stream Stream var stream Stream
err := json.Unmarshal(body, &stream) err := json.Unmarshal(body, &stream)
if err != nil { if err != nil {
log.Printf("error while parsing body: %s", err) logger(fmt.Sprintf("error while parsing body: %s", err))
a.crowdsecStreamHealthy = false a.crowdsecStreamHealthy = false
return return
} }
for _, decision := range stream.New { for _, decision := range stream.New {
duration, err := time.ParseDuration(decision.Duration) duration, err := time.ParseDuration(decision.Duration)
if err == nil { if err == nil {
setDecision(a.cache, decision.Value, true, int64(duration.Seconds())) setDecision(a, decision.Value, true, int64(duration.Seconds()))
} }
} }
for _, decision := range stream.Deleted { for _, decision := range stream.Deleted {
@@ -315,7 +319,7 @@ func getToken(a *Bouncer) {
var login Login var login Login
err := json.Unmarshal(body, &login) err := json.Unmarshal(body, &login)
if err != nil { if err != nil {
log.Printf("error while parsing body: %s", err) logger(fmt.Sprintf("error while parsing body: %s", err))
a.crowdsecStreamHealthy = false a.crowdsecStreamHealthy = false
return return
} }
@@ -344,7 +348,7 @@ func crowdsecQuery(a *Bouncer, stringURL string, isPost bool) []byte {
} }
res, err := a.client.Do(req) res, err := a.client.Do(req)
if err != nil { if err != nil {
log.Printf("error while fetching %v: %s", stringURL, err) logger(fmt.Sprintf("error while fetching %v: %s", stringURL, err))
a.crowdsecStreamHealthy = false a.crowdsecStreamHealthy = false
return nil return nil
} }
@@ -358,19 +362,19 @@ func crowdsecQuery(a *Bouncer, stringURL string, isPost bool) []byte {
return crowdsecQuery(a, stringURL, false) return crowdsecQuery(a, stringURL, false)
} }
if res.StatusCode != http.StatusOK { if res.StatusCode != http.StatusOK {
log.Printf("error while fetching %v, status code: %d", stringURL, res.StatusCode) logger(fmt.Sprintf("error while fetching %v, status code: %d", stringURL, res.StatusCode))
a.crowdsecStreamHealthy = false a.crowdsecStreamHealthy = false
return nil return nil
} }
defer func(body io.ReadCloser) { defer func(body io.ReadCloser) {
err = body.Close() err = body.Close()
if err != nil { if err != nil {
log.Printf("failed to close body reader: %s", err) logger(fmt.Sprintf("failed to close body reader: %s", err))
} }
}(res.Body) }(res.Body)
body, err := ioutil.ReadAll(res.Body) body, err := ioutil.ReadAll(res.Body)
if err != nil { if err != nil {
log.Printf("error while reading body: %s", err) logger(fmt.Sprintf("error while reading body: %s", err))
a.crowdsecStreamHealthy = false a.crowdsecStreamHealthy = false
return nil return nil
} }
+27 -15
View File
@@ -7,7 +7,7 @@ services:
command: command:
# - "--log.level=DEBUG" # - "--log.level=DEBUG"
- "--accesslog" - "--accesslog"
- "--accesslog.filepath=/var/log/traefik/traefik.log" - "--accesslog.filepath=/var/log/traefik/access.log"
- "--api.insecure=true" - "--api.insecure=true"
- "--providers.docker=true" - "--providers.docker=true"
- "--providers.docker.exposedbydefault=false" - "--providers.docker.exposedbydefault=false"
@@ -15,28 +15,40 @@ services:
- "--experimental.localplugins.bouncer.modulename=github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin" - "--experimental.localplugins.bouncer.modulename=github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin"
volumes: volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro" - /var/run/docker.sock:/var/run/docker.sock:ro
- "logs:/var/log/traefik" - logs:/var/log/traefik
- ./:/plugins-local/src/github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin - ./:/plugins-local/src/github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin
ports:
- 8000:80
- 8080:8080
depends_on:
- 'crowdsec'
whoami:
image: traefik/whoami
container_name: "simple-service"
labels: labels:
- "traefik.enable=true" - "traefik.enable=true"
- "traefik.http.routers.whoami.rule=Host(`localhost`)"
- "traefik.http.routers.whoami.entrypoints=web"
- "traefik.http.routers.whoami.middlewares=crowdsec@docker"
- "traefik.http.middlewares.crowdsec.plugin.bouncer.enabled=true" - "traefik.http.middlewares.crowdsec.plugin.bouncer.enabled=true"
- "traefik.http.middlewares.crowdsec.plugin.bouncer.crowdseclapikey=40796d93c2958f9e58345514e67740e5" - "traefik.http.middlewares.crowdsec.plugin.bouncer.crowdseclapikey=40796d93c2958f9e58345514e67740e5"
- "traefik.http.middlewares.crowdsec.plugin.bouncer.crowdseccapilogin=3829a6c9870e4726a377d8951ebb64a1m8psGvMaq1ykJ3zX" - "traefik.http.middlewares.crowdsec.plugin.bouncer.crowdseccapilogin=3829a6c9870e4726a377d8951ebb64a1m8psGvMaq1ykJ3zX"
- "traefik.http.middlewares.crowdsec.plugin.bouncer.crowdseccapipwd=Q5pgN8bRNInHGdx6QCksdPOJVLeLQ7ipJntSeuP3r8088zXzRVs4G8liXAKfI1k6" - "traefik.http.middlewares.crowdsec.plugin.bouncer.crowdseccapipwd=Q5pgN8bRNInHGdx6QCksdPOJVLeLQ7ipJntSeuP3r8088zXzRVs4G8liXAKfI1k6"
- "traefik.http.middlewares.crowdsec.plugin.bouncer.crowdseccapiscenarios=crowdsecurity/http-backdoors-attempts,baudneo/zoneminder-bf" - "traefik.http.middlewares.crowdsec.plugin.bouncer.crowdseccapiscenarios=crowdsecurity/http-backdoors-attempts,baudneo/zoneminder-bf"
ports:
- 8000:80
- 8080:8080
depends_on:
- crowdsec
whoami1:
image: traefik/whoami
container_name: "simple-service1"
labels:
- "traefik.enable=true"
- "traefik.http.routers.whoami.rule=Host(`localhost`)"
- "traefik.http.routers.whoami.entrypoints=web"
- "traefik.http.routers.whoami.middlewares=crowdsec@docker"
- "traefik.http.services.whoami.loadbalancer.server.port=80"
whoami2:
image: traefik/whoami
container_name: "simple-service2"
labels:
- "traefik.enable=true"
- "traefik.http.routers.whoami.rule=Host(`localhost`)"
- "traefik.http.routers.whoami.entrypoints=web"
- "traefik.http.routers.whoami.middlewares=crowdsec@docker"
- "traefik.http.services.whoami.loadbalancer.server.port=80"
crowdsec: crowdsec:
image: crowdsecurity/crowdsec:v1.4.1 image: crowdsecurity/crowdsec:v1.4.1
+1 -1
View File
@@ -7,7 +7,7 @@ services:
command: command:
# - "--log.level=DEBUG" # - "--log.level=DEBUG"
- "--accesslog" - "--accesslog"
- "--accesslog.filepath=/var/log/traefik/traefik.log" - "--accesslog.filepath=/var/log/traefik/access.log"
- "--api.insecure=true" - "--api.insecure=true"
- "--providers.docker=true" - "--providers.docker=true"
- "--providers.docker.exposedbydefault=false" - "--providers.docker.exposedbydefault=false"