mirror of
https://github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin.git
synced 2026-09-02 20:28:50 +02:00
Compare commits
109
Commits
v0.1.4-alpha
...
v1.1.3
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2d0bea8eec | ||
|
|
bb714b5dfd | ||
|
|
bba5620187 | ||
|
|
73374ccefe | ||
|
|
6b7f8655ac | ||
|
|
c9a3f1f8a8 | ||
|
|
490e5e934f | ||
|
|
5b0d4b533f | ||
|
|
400de0d552 | ||
|
|
3742b6b540 | ||
|
|
d2f0a9416d | ||
|
|
c1f2131bc2 | ||
|
|
3f70e2b256 | ||
|
|
bf76ca9ef5 | ||
|
|
1baa5d7667 | ||
|
|
18f68de196 | ||
|
|
4a5f1eca6a | ||
|
|
cdda369fb8 | ||
|
|
f0f28fecef | ||
|
|
23620207f7 | ||
|
|
4a674da9f9 | ||
|
|
0dfd18f18e | ||
|
|
d8ee0a34eb | ||
|
|
b50074dca4 | ||
|
|
5044004ec2 | ||
|
|
41a46c0584 | ||
|
|
0a186cf9a9 | ||
|
|
ffcf4356fc | ||
|
|
f94e48aa03 | ||
|
|
a197194591 | ||
|
|
44e329cd57 | ||
|
|
81ffeabcec | ||
|
|
e14d179612 | ||
|
|
4058836678 | ||
|
|
87ed9e9c4e | ||
|
|
395c80dccf | ||
|
|
59268ee33d | ||
|
|
781a83465e | ||
|
|
8696501f61 | ||
|
|
f22fc2cd09 | ||
|
|
8eec1c5656 | ||
|
|
552b30a9ef | ||
|
|
6dde683a0a | ||
|
|
be0306eb49 | ||
|
|
b69bd77409 | ||
|
|
64a117d7b0 | ||
|
|
d0ba71c0c8 | ||
|
|
2e780b304d | ||
|
|
4d7615dd19 | ||
|
|
952fcd844f | ||
|
|
9cf4827768 | ||
|
|
6d8e811a03 | ||
|
|
18e4d42e8e | ||
|
|
54183fbc97 | ||
|
|
c99a0ddc46 | ||
|
|
f876458bb2 | ||
|
|
6c6a80efb4 | ||
|
|
cc0261aa86 | ||
|
|
465851b536 | ||
|
|
9e92aeb449 | ||
|
|
37c7648854 | ||
|
|
dd704d4274 | ||
|
|
1e4508f091 | ||
|
|
89e8b5631e | ||
|
|
68adb31065 | ||
|
|
1ebde35693 | ||
|
|
26913c5f7e | ||
|
|
ec0ffabc21 | ||
|
|
2f2cb60f74 | ||
|
|
cb16130ca0 | ||
|
|
d4fce42fd7 | ||
|
|
253caac5a4 | ||
|
|
e122ce488c | ||
|
|
d9d92ed92d | ||
|
|
3452c62860 | ||
|
|
b545eac5a8 | ||
|
|
2569f26805 | ||
|
|
773f001239 | ||
|
|
6b46c63287 | ||
|
|
c8f59c2233 | ||
|
|
2110c70bbd | ||
|
|
00bbcfdb94 | ||
|
|
a6012015e6 | ||
|
|
feb40d9b5f | ||
|
|
6ab8ccb9db | ||
|
|
dd36df5147 | ||
|
|
ae959e4e0d | ||
|
|
7f5f69d134 | ||
|
|
1d7c5948f2 | ||
|
|
2559c71930 | ||
|
|
593681fd53 | ||
|
|
c7714cb793 | ||
|
|
75267a1746 | ||
|
|
45eca85f3a | ||
|
|
1d9bd3fab0 | ||
|
|
0aff6e1789 | ||
|
|
0abf1b7390 | ||
|
|
cb65e48a37 | ||
|
|
cba7c1231f | ||
|
|
da47ef320d | ||
|
|
bbbb5acac5 | ||
|
|
6cf89bfc15 | ||
|
|
f39317263f | ||
|
|
6dbd498212 | ||
|
|
7c2ac9830d | ||
|
|
b985f2d748 | ||
|
|
a8758eb2e0 | ||
|
|
aad92013d1 | ||
|
|
4843b86b45 |
@@ -3,7 +3,7 @@ name: Main
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
- main
|
||||
pull_request:
|
||||
|
||||
jobs:
|
||||
|
||||
@@ -1,2 +1,6 @@
|
||||
.idea/
|
||||
.DS_Store
|
||||
config
|
||||
db
|
||||
logs
|
||||
docker-compose.dev.yml
|
||||
+3
-4
@@ -4,10 +4,9 @@ iconPath: .assets/icon.png
|
||||
|
||||
import: github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin
|
||||
|
||||
summary: 'Crowdsec Bouncer Traefik Plugin'
|
||||
summary: Middleware plugin which forwards the request IP to local Crowdsec agent, which can be used to allow/deny the request
|
||||
|
||||
testData:
|
||||
bouncer:
|
||||
enabled: true
|
||||
crowdsecLapiKey: 40796d93c2958f9e58345514e67740e5
|
||||
Enabled: false
|
||||
CrowdsecLapiKey: 40796d93c2958f9e58345514e67740e5
|
||||
|
||||
|
||||
@@ -18,3 +18,59 @@ vendor:
|
||||
|
||||
clean:
|
||||
rm -rf ./vendor
|
||||
|
||||
run_dev:
|
||||
docker-compose -f docker-compose.dev.yml up -d --remove-orphans
|
||||
|
||||
run_local:
|
||||
docker-compose -f docker-compose.local.yml up -d --remove-orphans
|
||||
|
||||
run_behindproxy:
|
||||
docker-compose -f exemples/behind-proxy/docker-compose.cloudflare.yml up -d --remove-orphans
|
||||
|
||||
run_cacheredis:
|
||||
docker-compose -f exemples/redis-cache/docker-compose.redis.yml up -d --remove-orphans
|
||||
|
||||
run_trustedips:
|
||||
docker-compose -f exemples/trusted-ips/docker-compose.trusted.yml up -d --remove-orphans
|
||||
|
||||
run:
|
||||
docker-compose -f docker-compose.yml up -d --remove-orphans
|
||||
|
||||
restart_dev:
|
||||
docker-compose -f docker-compose.dev.yml restart
|
||||
|
||||
restart_local:
|
||||
docker-compose -f docker-compose.local.yml restart
|
||||
|
||||
restart:
|
||||
docker-compose -f docker-compose.yml restart
|
||||
|
||||
restart_behindproxy:
|
||||
docker-compose -f exemples/behind-proxy/docker-compose.cloudflare.yml restart
|
||||
|
||||
restart_cacheredis:
|
||||
docker-compose -f exemples/redis-cache/docker-compose.redis.yml restart
|
||||
|
||||
restart_trustedips:
|
||||
docker-compose -f exemples/trusted-ips/docker-compose.trusted.yml restart
|
||||
|
||||
show_logs:
|
||||
docker-compose -f docker-compose.yml restart
|
||||
|
||||
show_local_logs:
|
||||
docker-compose -f docker-compose.local.yml logs -f
|
||||
|
||||
show_dev_logs:
|
||||
docker-compose -f docker-compose.dev.yml logs -f
|
||||
|
||||
clean_all_docker:
|
||||
docker-compose -f exemples/behind-proxy/docker-compose.cloudflare.yml down --remove-orphans
|
||||
docker-compose -f exemples/redis-cache/docker-compose.redis.yml down --remove-orphans
|
||||
docker-compose -f exemples/trusted-ips/docker-compose.trusted.yml down --remove-orphans
|
||||
docker-compose -f docker-compose.local.yml down --remove-orphans
|
||||
docker-compose -f docker-compose.yml down --remove-orphans
|
||||
|
||||
show_metrics:
|
||||
docker exec crowdsec cscli metrics
|
||||
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
[](https://github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin/actions)
|
||||

|
||||

|
||||

|
||||
[](https://github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin/actions)
|
||||
[](https://goreportcard.com/badge/github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin)
|
||||
|
||||
# Crowdsec Bouncer Traefik plugin
|
||||
|
||||
@@ -6,31 +10,89 @@ This plugins aims to implement a Crowdsec Bouncer into a traefik plugin.
|
||||
> [CrowdSec](https://www.crowdsec.net/) is an open-source and collaborative IPS (Intrusion Prevention System) and a security suite.
|
||||
> We leverage local behavior analysis and crowd power to build the largest CTI network in the world.
|
||||
|
||||
The purpose is to enable treafik to authorize and block requests from IP based and their reputation and behavior.
|
||||
The purpose is to enable treafik to authorize or block requests from IP based and their reputation and behavior.
|
||||
|
||||
The crowdsec utility will provide the community blocklist which contains highly reported and validated IP banned from the crowdsec network.
|
||||
|
||||
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 is 3 operating mode 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
|
||||
There are 3 operating modes (CrowdsecMode) for this plugin:
|
||||
|
||||
- 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.
|
||||
| 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. |
|
||||
|
||||
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 cache can be local to the Traefik instance using the filesystem or use of a separated redis instance.
|
||||
The redis instance is currently in beta and support Redis 7.0.X version
|
||||
|
||||
## Usage
|
||||
|
||||
To get started, use the `docker-compose.yml` file.
|
||||
|
||||
You can run it with:
|
||||
```bash
|
||||
make run
|
||||
```
|
||||
|
||||
### Note
|
||||
|
||||
**/!\ Since Release 1.10, cache is no longer duplicated but shared by all services**
|
||||
*This lowers the overhead of the cache in memory and the numbers of cache to fetch it from crowdsec in situation with many services*
|
||||
|
||||
|
||||
### Variables
|
||||
- Enabled
|
||||
- bool
|
||||
- enable the plugin
|
||||
- default: false
|
||||
- LogLevel
|
||||
- string
|
||||
- default: `INFO`, expected value are: `INFO`, `DEBUG`
|
||||
- CrowdsecMode
|
||||
- string
|
||||
- default: `live`, expected value are: `none`, `live`, `stream`
|
||||
- CrowdsecLapiScheme
|
||||
- string
|
||||
- default: `http`, expected value are: `http`, `https`
|
||||
- CrowdsecLapiHost
|
||||
- string
|
||||
- default: "crowdsec:8080"
|
||||
- Crowdsec LAPI available on which host and port.
|
||||
- CrowdsecLapiKey
|
||||
- string
|
||||
- Crowdsec LAPI generated key for the bouncer : **must be unique by service**.
|
||||
- UpdateIntervalSeconds
|
||||
- int64
|
||||
- default: 60
|
||||
- Used only in `stream` mode, interval between fetching blacklisted IPs from LAPI
|
||||
- DefaultDecisionSeconds
|
||||
- int64
|
||||
- default: 60
|
||||
- Used only in `live` mode, decision duration of accepted IPs
|
||||
- ForwardedHeadersTrustedIPs
|
||||
- []string
|
||||
- default: []
|
||||
- List of IPs of trusted Proxies that are in front of traefik (ex: Cloudflare)
|
||||
- ForwardedHeadersCustomName
|
||||
- string
|
||||
- default: "X-Forwarded-For"
|
||||
- Name of the header where the real IP of the client should be retrieved
|
||||
- RedisCacheEnabled
|
||||
- bool
|
||||
- default: false
|
||||
- enable redis cache instead of filesystem cache
|
||||
- RedisCacheHost
|
||||
- string
|
||||
- default: "redis:6379"
|
||||
- hostname and port for the redis service
|
||||
- ClientTrustedIPs
|
||||
- string
|
||||
- default: []
|
||||
- List of client IPs we trust, they will bypass any more check to the bouncer or cache (usefull for LAN or VPN IP)
|
||||
|
||||
### Configuration
|
||||
|
||||
@@ -42,7 +104,7 @@ The following declaration (given here in YAML) defines a plugin:
|
||||
# Static configuration
|
||||
|
||||
experimental:
|
||||
localPlugins:
|
||||
plugins:
|
||||
bouncer:
|
||||
moduleName: github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin
|
||||
```
|
||||
@@ -58,7 +120,7 @@ http:
|
||||
entryPoints:
|
||||
- web
|
||||
middlewares:
|
||||
- my-plugin
|
||||
- crowdsec
|
||||
|
||||
services:
|
||||
service-foo:
|
||||
@@ -70,13 +132,59 @@ http:
|
||||
crowdsec:
|
||||
plugin:
|
||||
bouncer:
|
||||
enabled: true
|
||||
crowdseclapikey: 40796d93c2958f9e58345514e67740e5
|
||||
enabled: false
|
||||
updateIntervalSeconds: 60
|
||||
defaultDecisionSeconds: 60
|
||||
crowdsecLapiHost:
|
||||
crowdsecLapiScheme:
|
||||
crowdsecMode: stream
|
||||
crowdsecMode: live
|
||||
crowdsecLapiKey: privateKey
|
||||
crowdsecLapiHost: crowdsec:8080
|
||||
crowdsecLapiScheme: http
|
||||
forwardedHeadersTrustedIPs:
|
||||
- 10.0.10.23/32
|
||||
- 10.0.20.0/24
|
||||
clientTrustedIPs:
|
||||
- 192.168.1.0/24
|
||||
forwardedHeadersCustomName: X-Custom-Header
|
||||
redisCacheEnabled: false
|
||||
redisCacheHost: "redis:6379"
|
||||
```
|
||||
These are the default values of the plugin except for LapiKey.
|
||||
|
||||
#### 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/
|
||||
|
||||
```bash
|
||||
docker-compose -f docker-compose-local.yml up -d crowdsec
|
||||
docker exec crowdsec cscli bouncers add crowdsecBouncer
|
||||
```
|
||||
|
||||
This LAPI key must be set where is noted FIXME-LAPI-KEY in the docker-compose-test.yml
|
||||
```yaml
|
||||
...
|
||||
whoami:
|
||||
labels:
|
||||
- "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:
|
||||
environment:
|
||||
BOUNCER_KEY_TRAEFIK: FIXME-LAPI-KEY
|
||||
...
|
||||
```
|
||||
|
||||
You can then run all the containers:
|
||||
```bash
|
||||
docker-compose up -d
|
||||
```
|
||||
|
||||
#### Add manually an IP to the blocklist (testing purpose)
|
||||
|
||||
```bash
|
||||
docker-compose up -d crowdsec
|
||||
docker exec crowdsec cscli decisions add --ip 10.0.0.10 -d 10m # this will be effective 10min
|
||||
docker exec crowdsec cscli decisions remove --ip 10.0.0.10
|
||||
```
|
||||
|
||||
### Local Mode
|
||||
@@ -103,53 +211,98 @@ The source code of the plugin should be organized as follows:
|
||||
└── vendor/*
|
||||
```
|
||||
|
||||
For local developpement a docker-compose-local.yml is provided and reproduce the directory layout needed by traefik. This works once you have generated and filled your LAPI-KEY, if not look below for informations
|
||||
For local developpement a docker-compose.local.yml is provided and reproduce the directory layout needed by traefik. This works once you have generated and filled your LAPI-KEY (crowdsecLapiKey), if not look below for informations
|
||||
|
||||
```bash
|
||||
docker-compose -f docker-compose-local.yml up -d
|
||||
docker-compose -f docker-compose.local.yml up -d
|
||||
```
|
||||
|
||||
#### 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/
|
||||
|
||||
Equivalent to
|
||||
```bash
|
||||
docker-compose -f docker-compose-local.yml up -d crowdsec
|
||||
docker exec crowdsec cscli bouncers add crowdsecBouncer
|
||||
make run_local
|
||||
```
|
||||
|
||||
This LApi key must be set where is noted FIXME-LAPI-KEY in the docker-compose-test.yml
|
||||
### Exemples
|
||||
|
||||
1. Behind another proxy service (ex: clouflare)
|
||||
|
||||
You need to configure your Traefik to trust Forwarded headers by your front proxy
|
||||
In the exemple we use another instance of traefik with the container named cloudflare to simulate a front proxy
|
||||
|
||||
The "internal" Traefik instance is configured to trust the cloudflare forward headers
|
||||
This helps Traefik choose the right IP of the client: see https://doc.traefik.io/traefik/routing/entrypoints/#forwarded-headers
|
||||
```yaml
|
||||
...
|
||||
whoami:
|
||||
labels:
|
||||
- "traefik.http.middlewares.crowdsec.plugin.bouncer.crowdseclapikey=FIXME-LAPI-KEY"
|
||||
...
|
||||
crowdsec:
|
||||
environment:
|
||||
BOUNCER_KEY_TRAEFIK: FIXME-LAPI-KEY
|
||||
...
|
||||
- "--entrypoints.web.forwardedheaders.trustedips=172.21.0.5"
|
||||
```
|
||||
|
||||
You can then run all the containers:
|
||||
```bash
|
||||
docker-compose -f docker-compose-local.yml up -d
|
||||
We configure the middleware to trust as well the IP:
|
||||
```yaml
|
||||
- "traefik.http.middlewares.crowdsec1.plugin.bouncer.forwardedheaderstrustedips=172.21.0.5"
|
||||
```
|
||||
|
||||
#### Add manually an IP to the blocklist
|
||||
|
||||
To play the demo environnement run:
|
||||
```bash
|
||||
docker-compose -f docker-compose-local.yml up -d crowdsec
|
||||
docker exec crowdsec cscli decisions add --ip 10.0.0.10
|
||||
make run_behindproxy
|
||||
```
|
||||
|
||||
2. With Redis as an external shared cache
|
||||
|
||||
The plugin must be configured to connect to a redis instance
|
||||
```yaml
|
||||
redisCacheHost: "redis:6379"
|
||||
```
|
||||
Here **redis** is the hostname of a container located in the same network as Traefik and **6379** the default port of redis
|
||||
|
||||
To play the demo environnement run:
|
||||
```bash
|
||||
make run_cacheredis
|
||||
```
|
||||
|
||||
3. Using Trusted IP (ex: LAN OR VPN) that won't get filtered by crowdsec
|
||||
|
||||
You need to configure your Traefik to trust Forwarded headers by your front proxy
|
||||
In the exemple we use a whoami container protected by crowdsec, and we ban or IP before allowing using TrustedIPs
|
||||
|
||||
If you are using another proxy in front, you need to add it's IP in the trusted IP for the forwarded headers.
|
||||
This helps Traefik choose the right IP of the client: see https://doc.traefik.io/traefik/routing/entrypoints/#forwarded-headers
|
||||
The "internal" Traefik instance is configured to trust the forward headers
|
||||
```yaml
|
||||
- "--entrypoints.web.forwardedheaders.trustedips=172.21.0.5"
|
||||
```
|
||||
|
||||
We configure the middleware to trust as well the IP of the intermediate proxy if needed:
|
||||
```yaml
|
||||
- "traefik.http.middlewares.crowdsec.plugin.bouncer.forwardedheaderstrustedips=172.21.0.5"
|
||||
```
|
||||
|
||||
Add your IP to the ban list
|
||||
```bash
|
||||
docker exec crowdsec cscli decisions add --ip 10.0.10.30 -d 10m
|
||||
```
|
||||
You should get a 403 on http://localhost/foo
|
||||
|
||||
> Replace *10.0.10.30* by your IP
|
||||
|
||||
Add the IPs that will not be filtered by the plugin
|
||||
```yaml
|
||||
- "traefik.http.middlewares.crowdsec.plugin.bouncer.clientTrustedips=10.0.10.30/32"
|
||||
```
|
||||
|
||||
> Replace *10.0.10.30/32* by your IP or IP range, so it's not getting checked against ban cache of crowdsec
|
||||
|
||||
You should get a 200 on http://localhost/foo even if you are on the ban cache
|
||||
|
||||
To play the demo environnement run:
|
||||
```bash
|
||||
make run_trustedips
|
||||
```
|
||||
|
||||
### About
|
||||
|
||||
[maxlerebourg](https://github.com/maxlerebourg) and [I](https://github.com/mhanotaux) have been using traefik since 2020.
|
||||
We come from developper and security engineer background and wanted to add the power of a very promesing technologie (Crowdsec) into the edge router we love.
|
||||
Me and [mathieuHa](https://github.com/mathieuHa) have been using traefik since 2020 at [Primadviz](https://primadviz.com).
|
||||
We come from web developper and security engineer background and wanted to add the power of a very promesing technology (Crowdsec) into the edge router we love.
|
||||
|
||||
We initially run into this project: https://github.com/fbonalair/traefik-crowdsec-bouncer
|
||||
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.
|
||||
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.
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
filenames:
|
||||
- /var/log/traefik/*.log
|
||||
- /var/log/traefik/access.log
|
||||
labels:
|
||||
type: traefik
|
||||
+269
-191
@@ -5,126 +5,120 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
ttl_map "github.com/leprosus/golang-ttl-map"
|
||||
cache "github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin/pkg/cache"
|
||||
ip "github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin/pkg/ip"
|
||||
logger "github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin/pkg/logger"
|
||||
simpleredis "github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin/pkg/redis"
|
||||
)
|
||||
|
||||
const (
|
||||
crowdsecAuthHeader = "X-Api-Key"
|
||||
crowdsecRoute = "v1/decisions"
|
||||
crowdsecStreamRoute = "v1/decisions/stream"
|
||||
cacheBannedValue = "t"
|
||||
cacheNoBannedValue = "f"
|
||||
streamMode = "stream"
|
||||
liveMode = "live"
|
||||
noneMode = "none"
|
||||
crowdsecLapiHeader = "X-Api-Key"
|
||||
crowdsecLapiRoute = "v1/decisions"
|
||||
crowdsecLapiStreamRoute = "v1/decisions/stream"
|
||||
cacheTimeoutKey = "updated"
|
||||
)
|
||||
|
||||
var cache = ttl_map.New()
|
||||
var (
|
||||
crowdsecStreamHealthy = false
|
||||
ticker chan bool
|
||||
)
|
||||
|
||||
// Config the plugin configuration.
|
||||
type Config struct {
|
||||
Enabled bool `json:"enabled,omitempty"`
|
||||
LogLevel string `json:"logLevel,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"`
|
||||
ForwardedHeadersCustomName string `json:"forwardedheaderscustomheader,omitempty"`
|
||||
ForwardedHeadersTrustedIPs []string `json:"forwardedHeadersTrustedIps,omitempty"`
|
||||
ClientTrustedIPs []string `json:"clientTrustedIps,omitempty"`
|
||||
RedisCacheEnabled bool `json:"redisCacheEnabled,omitempty"`
|
||||
RedisCacheHost string `json:"redisCacheHost,omitempty"`
|
||||
}
|
||||
|
||||
// CreateConfig creates the default plugin configuration.
|
||||
func CreateConfig() *Config {
|
||||
return &Config{
|
||||
Enabled: false,
|
||||
CrowdsecMode: "stream",
|
||||
LogLevel: "INFO",
|
||||
CrowdsecMode: liveMode,
|
||||
CrowdsecLapiScheme: "http",
|
||||
CrowdsecLapiHost: "crowdsec:8080",
|
||||
CrowdsecLapiKey: "",
|
||||
UpdateIntervalSeconds: 60,
|
||||
DefaultDecisionSeconds: 60,
|
||||
ForwardedHeadersTrustedIPs: []string{},
|
||||
ClientTrustedIPs: []string{},
|
||||
ForwardedHeadersCustomName: "X-Forwarded-For",
|
||||
RedisCacheEnabled: false,
|
||||
RedisCacheHost: "redis:6379",
|
||||
}
|
||||
}
|
||||
|
||||
// Bouncer a Bouncer struct.
|
||||
type Bouncer struct {
|
||||
next http.Handler
|
||||
name string
|
||||
template *template.Template
|
||||
|
||||
enabled bool
|
||||
crowdsecStreamHealthy bool
|
||||
crowdsecScheme string
|
||||
crowdsecHost string
|
||||
crowdsecKey string
|
||||
crowdsecMode string
|
||||
updateInterval int64
|
||||
defaultDecisionTimeout int64
|
||||
customHeader string
|
||||
serverPoolStrategy *ip.PoolStrategy
|
||||
clientPoolStrategy *ip.PoolStrategy
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
// New creates the crowdsec bouncer plugin.
|
||||
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
|
||||
requiredStrings := map[string]string{
|
||||
"CrowdsecLapiScheme": config.CrowdsecLapiScheme,
|
||||
"CrowdsecLapiHost": config.CrowdsecLapiHost,
|
||||
"CrowdsecLapiKey": config.CrowdsecLapiKey,
|
||||
"CrowdsecMode": config.CrowdsecMode,
|
||||
}
|
||||
for key, val := range requiredStrings {
|
||||
if len(val) == 0 {
|
||||
return nil, fmt.Errorf("%v cannot be empty", key)
|
||||
}
|
||||
}
|
||||
requiredInt := map[string]int64{
|
||||
"UpdateIntervalSeconds": config.UpdateIntervalSeconds,
|
||||
"DefaultDecisionSeconds": config.DefaultDecisionSeconds,
|
||||
}
|
||||
for key, val := range requiredInt {
|
||||
if val < 1 {
|
||||
return nil, fmt.Errorf("%v cannot be less than 1", key)
|
||||
}
|
||||
}
|
||||
// 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.
|
||||
if !contains([]string{"none", "live", "stream"}, config.CrowdsecMode) {
|
||||
return nil, fmt.Errorf("CrowdsecMode must be one of: none, live or stream")
|
||||
}
|
||||
if !contains([]string{"http", "https"}, config.CrowdsecLapiScheme) {
|
||||
return nil, fmt.Errorf("CrowdsecLapiScheme must be one of: http, https")
|
||||
}
|
||||
testUrl := url.URL{
|
||||
Scheme: config.CrowdsecLapiScheme,
|
||||
Host: config.CrowdsecLapiHost,
|
||||
Path: crowdsecRoute,
|
||||
}
|
||||
_, err := http.NewRequest(http.MethodGet, testUrl.String(), nil)
|
||||
logger.Init(config.LogLevel)
|
||||
err := validateParams(config)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("CrowdsecLapiScheme://CrowdsecLapiHost: '%v://%v' must be an URL", config.CrowdsecLapiScheme, config.CrowdsecLapiHost)
|
||||
logger.Info(err.Error())
|
||||
return nil, err
|
||||
}
|
||||
|
||||
serverChecker, _ := ip.NewChecker(config.ForwardedHeadersTrustedIPs)
|
||||
clientChecker, _ := ip.NewChecker(config.ClientTrustedIPs)
|
||||
|
||||
bouncer := &Bouncer{
|
||||
next: next,
|
||||
name: name,
|
||||
template: template.New("CrowdsecBouncer").Delims("[[", "]]"),
|
||||
|
||||
enabled: config.Enabled,
|
||||
crowdsecStreamHealthy: false,
|
||||
crowdsecMode: config.CrowdsecMode,
|
||||
crowdsecScheme: config.CrowdsecLapiScheme,
|
||||
crowdsecHost: config.CrowdsecLapiHost,
|
||||
crowdsecKey: config.CrowdsecLapiKey,
|
||||
updateInterval: config.UpdateIntervalSeconds,
|
||||
customHeader: config.ForwardedHeadersCustomName,
|
||||
defaultDecisionTimeout: config.DefaultDecisionSeconds,
|
||||
serverPoolStrategy: &ip.PoolStrategy{
|
||||
Checker: serverChecker,
|
||||
},
|
||||
clientPoolStrategy: &ip.PoolStrategy{
|
||||
Checker: clientChecker,
|
||||
},
|
||||
client: &http.Client{
|
||||
Transport: &http.Transport{
|
||||
MaxIdleConns: 10,
|
||||
@@ -133,113 +127,82 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
|
||||
Timeout: 5 * time.Second,
|
||||
},
|
||||
}
|
||||
// if we are on a stream mode, we fetch in a go routine every minute the new decisions
|
||||
if config.CrowdsecMode == "stream" {
|
||||
go handleStreamCache(bouncer, true)
|
||||
if config.RedisCacheEnabled {
|
||||
cache.InitRedisClient(config.RedisCacheHost)
|
||||
}
|
||||
if config.CrowdsecMode == streamMode && ticker == nil {
|
||||
ticker = startTicker(config, func() {
|
||||
handleStreamCache(bouncer)
|
||||
})
|
||||
go handleStreamCache(bouncer)
|
||||
}
|
||||
|
||||
return bouncer, nil
|
||||
}
|
||||
|
||||
// TODO the serve HTTP should be split as it's too long
|
||||
func (a *Bouncer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||
if !a.enabled {
|
||||
log.Printf("Crowdsec Bouncer not enabled")
|
||||
a.next.ServeHTTP(rw, req)
|
||||
// ServeHTTP principal function of plugin.
|
||||
func (bouncer *Bouncer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||
if !bouncer.enabled {
|
||||
bouncer.next.ServeHTTP(rw, req)
|
||||
return
|
||||
}
|
||||
|
||||
// TODO Make sur remote address does not include the port
|
||||
remoteHost, _, err := net.SplitHostPort(req.RemoteAddr)
|
||||
// Here we check for the trusted IPs in the customHeader
|
||||
remoteHost, err := ip.GetRemoteIP(req, bouncer.serverPoolStrategy, bouncer.customHeader)
|
||||
if err != nil {
|
||||
log.Printf("failed to extract ip from remote address: %v", err)
|
||||
a.next.ServeHTTP(rw, req)
|
||||
logger.Info(err.Error())
|
||||
bouncer.next.ServeHTTP(rw, req)
|
||||
return
|
||||
}
|
||||
|
||||
if a.crowdsecMode == "stream" || a.crowdsecMode == "live" {
|
||||
isBanned, err := getDecision(remoteHost)
|
||||
if err == nil {
|
||||
trusted, err := bouncer.clientPoolStrategy.Checker.Contains(remoteHost)
|
||||
if err != nil {
|
||||
logger.Info(err.Error())
|
||||
return
|
||||
}
|
||||
// if our IP is in the trusted list we bypass the next checks
|
||||
logger.Debug(fmt.Sprintf("ServeHTTP ip:%v isTrusted:%v", remoteHost, trusted))
|
||||
if trusted {
|
||||
bouncer.next.ServeHTTP(rw, req)
|
||||
return
|
||||
}
|
||||
|
||||
healthy := crowdsecStreamHealthy
|
||||
if bouncer.crowdsecMode != noneMode {
|
||||
isBanned, err := cache.GetDecision(remoteHost)
|
||||
if err != nil {
|
||||
logger.Debug(err.Error())
|
||||
if err.Error() == simpleredis.RedisUnreachable {
|
||||
healthy = false
|
||||
}
|
||||
} else {
|
||||
logger.Debug(fmt.Sprintf("ServeHTTP cache:hit isBanned:%v", isBanned))
|
||||
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 == "stream" {
|
||||
if a.crowdsecStreamHealthy {
|
||||
a.next.ServeHTTP(rw, req)
|
||||
// Right here if we cannot join the stream we forbid the request to go on.
|
||||
if bouncer.crowdsecMode == streamMode {
|
||||
if healthy {
|
||||
bouncer.next.ServeHTTP(rw, req)
|
||||
} else {
|
||||
rw.WriteHeader(http.StatusForbidden)
|
||||
}
|
||||
return
|
||||
} else {
|
||||
handleNoStreamCache(bouncer, rw, req, remoteHost)
|
||||
}
|
||||
|
||||
// We are now in none or live mode
|
||||
noneUrl := url.URL{
|
||||
Scheme: a.crowdsecScheme,
|
||||
Host: a.crowdsecHost,
|
||||
Path: crowdsecRoute,
|
||||
RawQuery: fmt.Sprintf("ip=%v&banned=true", remoteHost),
|
||||
}
|
||||
request, _ := http.NewRequest(http.MethodGet, noneUrl.String(), nil)
|
||||
request.Header.Add(crowdsecAuthHeader, a.crowdsecKey)
|
||||
res, err := a.client.Do(request)
|
||||
if err != nil {
|
||||
log.Printf("failed to get decision: %s", err)
|
||||
rw.WriteHeader(http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
defer res.Body.Close()
|
||||
if res.StatusCode != 200 {
|
||||
log.Printf("failed to get decision, status code: %d", res.StatusCode)
|
||||
rw.WriteHeader(http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
body, err := ioutil.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
log.Printf("failed to read body: %s", err)
|
||||
rw.WriteHeader(http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
if !bytes.Equal(body, []byte("null")) {
|
||||
var decisions []Decision
|
||||
err = json.Unmarshal(body, &decisions)
|
||||
if err != nil {
|
||||
log.Printf("failed to parse body: %s", err)
|
||||
rw.WriteHeader(http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
if len(decisions) == 0 {
|
||||
if a.crowdsecMode == "live" {
|
||||
setDecision(remoteHost, false, a.defaultDecisionTimeout)
|
||||
}
|
||||
a.next.ServeHTTP(rw, req)
|
||||
return
|
||||
}
|
||||
duration, err := time.ParseDuration(decisions[0].Duration)
|
||||
if err != nil {
|
||||
log.Printf("failed to parse duration: %s", err)
|
||||
rw.WriteHeader(http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
rw.WriteHeader(http.StatusForbidden)
|
||||
setDecision(remoteHost, true, int64(duration.Seconds()))
|
||||
return
|
||||
}
|
||||
if a.crowdsecMode == "live" {
|
||||
setDecision(remoteHost, false, a.defaultDecisionTimeout)
|
||||
}
|
||||
a.next.ServeHTTP(rw, req)
|
||||
}
|
||||
|
||||
// CUSTOM CODE
|
||||
// TODO place in another file
|
||||
// CUSTOM CODE.
|
||||
// TODO place in another file.
|
||||
|
||||
// Decision Body returned from Crowdsec LAPI.
|
||||
type Decision struct {
|
||||
Id int `json:"id"`
|
||||
ID int `json:"id"`
|
||||
Origin string `json:"origin"`
|
||||
Type string `json:"type"`
|
||||
Scope string `json:"scope"`
|
||||
@@ -249,88 +212,203 @@ type Decision struct {
|
||||
Simulated bool `json:"simulated"`
|
||||
}
|
||||
|
||||
// Stream Body returned from Crowdsec Stream LAPI.
|
||||
type Stream struct {
|
||||
Deleted []Decision `json:"deleted"`
|
||||
New []Decision `json:"new"`
|
||||
}
|
||||
|
||||
func contains(source []string, target string) bool {
|
||||
for _, a := range source {
|
||||
if a == target {
|
||||
for _, item := range source {
|
||||
if item == target {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// 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(clientIP string) (bool, error) {
|
||||
isBanned, ok := cache.Get(clientIP)
|
||||
if ok && len(isBanned.(string)) > 0 {
|
||||
if isBanned == cacheNoBannedValue {
|
||||
return false, nil
|
||||
} else {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
return false, fmt.Errorf("no data")
|
||||
}
|
||||
|
||||
func setDecision(clientIP string, isBanned bool, duration int64) {
|
||||
if isBanned {
|
||||
cache.Set(clientIP, cacheBannedValue, duration)
|
||||
} else {
|
||||
cache.Set(clientIP, cacheNoBannedValue, duration)
|
||||
}
|
||||
}
|
||||
|
||||
func handleStreamCache(a *Bouncer, initialized bool) {
|
||||
// TODO clean properly on exit
|
||||
time.AfterFunc(time.Duration(a.updateInterval)*time.Second, func() {
|
||||
handleStreamCache(a, false)
|
||||
})
|
||||
streamUrl := url.URL{
|
||||
Scheme: a.crowdsecScheme,
|
||||
Host: a.crowdsecHost,
|
||||
Path: crowdsecStreamRoute,
|
||||
RawQuery: fmt.Sprintf("startup=%t", initialized),
|
||||
}
|
||||
req, _ := http.NewRequest(http.MethodGet, streamUrl.String(), nil)
|
||||
req.Header.Add(crowdsecAuthHeader, a.crowdsecKey)
|
||||
res, err := a.client.Do(req)
|
||||
if err != nil || res.StatusCode == http.StatusForbidden {
|
||||
log.Printf("error while fetching decisions: %s", err)
|
||||
a.crowdsecStreamHealthy = false
|
||||
func startTicker(config *Config, work func()) chan bool {
|
||||
ticker := time.NewTicker(time.Duration(config.UpdateIntervalSeconds) * time.Second)
|
||||
stop := make(chan bool, 1)
|
||||
go func() {
|
||||
defer logger.Debug("ticker:stopped")
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
go work()
|
||||
case <-stop:
|
||||
return
|
||||
}
|
||||
if res.StatusCode == http.StatusForbidden {
|
||||
log.Printf("error while fetching decisions, status code: %d", res.StatusCode)
|
||||
a.crowdsecStreamHealthy = false
|
||||
return
|
||||
}
|
||||
defer res.Body.Close()
|
||||
body, err := ioutil.ReadAll(res.Body)
|
||||
}()
|
||||
return stop
|
||||
}
|
||||
|
||||
// We are now in none or live mode.
|
||||
func handleNoStreamCache(bouncer *Bouncer, rw http.ResponseWriter, req *http.Request, remoteHost string) {
|
||||
routeURL := url.URL{
|
||||
Scheme: bouncer.crowdsecScheme,
|
||||
Host: bouncer.crowdsecHost,
|
||||
Path: crowdsecLapiRoute,
|
||||
RawQuery: fmt.Sprintf("ip=%v&banned=true", remoteHost),
|
||||
}
|
||||
body, err := crowdsecQuery(bouncer, routeURL.String())
|
||||
if err != nil {
|
||||
log.Printf("error while reading body: %s", err)
|
||||
a.crowdsecStreamHealthy = false
|
||||
logger.Info(err.Error())
|
||||
rw.WriteHeader(http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
if bytes.Equal(body, []byte("null")) {
|
||||
if bouncer.crowdsecMode == liveMode {
|
||||
cache.SetDecision(remoteHost, false, bouncer.defaultDecisionTimeout)
|
||||
}
|
||||
bouncer.next.ServeHTTP(rw, req)
|
||||
return
|
||||
}
|
||||
|
||||
var decisions []Decision
|
||||
err = json.Unmarshal(body, &decisions)
|
||||
if err != nil {
|
||||
logger.Info(fmt.Sprintf("failed to parse body: %s", err))
|
||||
rw.WriteHeader(http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
if len(decisions) == 0 {
|
||||
if bouncer.crowdsecMode == liveMode {
|
||||
cache.SetDecision(remoteHost, false, bouncer.defaultDecisionTimeout)
|
||||
}
|
||||
bouncer.next.ServeHTTP(rw, req)
|
||||
return
|
||||
}
|
||||
rw.WriteHeader(http.StatusForbidden)
|
||||
duration, err := time.ParseDuration(decisions[0].Duration)
|
||||
if err != nil {
|
||||
logger.Info(fmt.Sprintf("failed to parse duration: %s", err))
|
||||
return
|
||||
}
|
||||
if bouncer.crowdsecMode == liveMode {
|
||||
cache.SetDecision(remoteHost, true, int64(duration.Seconds()))
|
||||
}
|
||||
}
|
||||
|
||||
func handleStreamCache(bouncer *Bouncer) {
|
||||
// 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
|
||||
// because updated routine information is in the cache
|
||||
logger.Debug("handleStreamCache")
|
||||
_, err := cache.GetDecision(cacheTimeoutKey)
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
cache.SetDecision(cacheTimeoutKey, false, bouncer.updateInterval-1)
|
||||
streamRouteURL := url.URL{
|
||||
Scheme: bouncer.crowdsecScheme,
|
||||
Host: bouncer.crowdsecHost,
|
||||
Path: crowdsecLapiStreamRoute,
|
||||
RawQuery: fmt.Sprintf("startup=%t", !crowdsecStreamHealthy),
|
||||
}
|
||||
body, err := crowdsecQuery(bouncer, streamRouteURL.String())
|
||||
if err != nil {
|
||||
logger.Info(err.Error())
|
||||
crowdsecStreamHealthy = false
|
||||
return
|
||||
}
|
||||
var stream Stream
|
||||
err = json.Unmarshal(body, &stream)
|
||||
if err != nil {
|
||||
log.Printf("error while parsing body: %s", err)
|
||||
a.crowdsecStreamHealthy = false
|
||||
logger.Info(fmt.Sprintf("error while parsing body: %s", err))
|
||||
crowdsecStreamHealthy = false
|
||||
return
|
||||
}
|
||||
for _, decision := range stream.New {
|
||||
duration, err := time.ParseDuration(decision.Duration)
|
||||
if err == nil {
|
||||
setDecision(decision.Value, true, int64(duration.Seconds()))
|
||||
cache.SetDecision(decision.Value, true, int64(duration.Seconds()))
|
||||
}
|
||||
}
|
||||
for _, decision := range stream.Deleted {
|
||||
cache.Del(decision.Value)
|
||||
cache.DeleteDecision(decision.Value)
|
||||
}
|
||||
a.crowdsecStreamHealthy = true
|
||||
crowdsecStreamHealthy = true
|
||||
}
|
||||
|
||||
func crowdsecQuery(bouncer *Bouncer, stringURL string) ([]byte, error) {
|
||||
var req *http.Request
|
||||
req, _ = http.NewRequest(http.MethodGet, stringURL, nil)
|
||||
req.Header.Add(crowdsecLapiHeader, bouncer.crowdsecKey)
|
||||
res, err := bouncer.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error while fetching %v: %s", stringURL, err)
|
||||
}
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("error while fetching %v, status code: %d", stringURL, res.StatusCode)
|
||||
}
|
||||
defer func(body io.ReadCloser) {
|
||||
err = body.Close()
|
||||
if err != nil {
|
||||
logger.Info(fmt.Sprintf("failed to close body reader: %s", err))
|
||||
}
|
||||
}(res.Body)
|
||||
body, err := ioutil.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error while reading body: %s", err)
|
||||
}
|
||||
return body, nil
|
||||
}
|
||||
|
||||
func validateParams(config *Config) error {
|
||||
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 {
|
||||
if len(val) == 0 {
|
||||
return fmt.Errorf("%v: cannot be empty", key)
|
||||
}
|
||||
}
|
||||
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) {
|
||||
return fmt.Errorf("CrowdsecLapiScheme: must be one of 'http' or 'https'")
|
||||
}
|
||||
testURL := url.URL{
|
||||
Scheme: config.CrowdsecLapiScheme,
|
||||
Host: config.CrowdsecLapiHost,
|
||||
}
|
||||
_, err := http.NewRequest(http.MethodGet, testURL.String(), nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("CrowdsecLapiScheme://CrowdsecLapiHost: '%v://%v' must be an URL", config.CrowdsecLapiScheme, config.CrowdsecLapiHost)
|
||||
}
|
||||
if len(config.ForwardedHeadersTrustedIPs) > 0 {
|
||||
_, err = ip.NewChecker(config.ForwardedHeadersTrustedIPs)
|
||||
if err != nil {
|
||||
return fmt.Errorf("ForwardedHeadersTrustedIPs must be a list of IP/CIDR :%w", err)
|
||||
}
|
||||
} else {
|
||||
logger.Debug("No IP provided for ForwardedHeadersTrustedIPs")
|
||||
}
|
||||
if len(config.ClientTrustedIPs) > 0 {
|
||||
_, err = ip.NewChecker(config.ClientTrustedIPs)
|
||||
if err != nil {
|
||||
return fmt.Errorf("TrustedIPs must be a list of IP/CIDR :%w", err)
|
||||
}
|
||||
} else {
|
||||
logger.Debug("No IP provided for TrustedIPs")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
+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)
|
||||
}
|
||||
|
||||
@@ -1,54 +0,0 @@
|
||||
version: "3.8"
|
||||
|
||||
services:
|
||||
traefik:
|
||||
image: "traefik:v2.8.7"
|
||||
container_name: "traefik"
|
||||
command:
|
||||
# - "--log.level=DEBUG"
|
||||
- "--accesslog"
|
||||
- "--accesslog.filepath=/var/log/traefik/traefik.log"
|
||||
- "--api.insecure=true"
|
||||
- "--providers.docker=true"
|
||||
- "--providers.docker.exposedbydefault=false"
|
||||
- "--entrypoints.web.address=:80"
|
||||
- "--experimental.localplugins.bouncer.modulename=github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin"
|
||||
volumes:
|
||||
- "/var/run/docker.sock:/var/run/docker.sock:ro"
|
||||
- "logs:/var/log/traefik"
|
||||
- ./:/plugins-local/src/github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin
|
||||
ports:
|
||||
- 8000:80
|
||||
- 8080:8080
|
||||
|
||||
whoami:
|
||||
image: traefik/whoami
|
||||
container_name: "simple-service"
|
||||
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.middlewares.crowdsec.plugin.bouncer.enabled=true"
|
||||
- "traefik.http.middlewares.crowdsec.plugin.bouncer.crowdseclapikey=FIXME-LAPI-KEY"
|
||||
|
||||
crowdsec:
|
||||
image: crowdsecurity/crowdsec:v1.4.1
|
||||
container_name: "crowdsec"
|
||||
command: rm -rf /etc/crowdsec/acquis.yaml
|
||||
environment:
|
||||
COLLECTIONS: crowdsecurity/traefik
|
||||
CUSTOM_HOSTNAME: crowdsec
|
||||
BOUNCER_KEY_TRAEFIK: FIXME-LAPI-KEY
|
||||
depends_on:
|
||||
- 'traefik'
|
||||
volumes:
|
||||
- ./acquis.yaml:/etc/crowdsec/acquis.yaml:ro
|
||||
- logs:/var/log/traefik:ro
|
||||
- crowdsec-db:/var/lib/crowdsec/data/
|
||||
- crowdsec-config:/etc/crowdsec/
|
||||
|
||||
volumes:
|
||||
logs:
|
||||
crowdsec-db:
|
||||
crowdsec-config:
|
||||
@@ -0,0 +1,73 @@
|
||||
version: "3.8"
|
||||
|
||||
services:
|
||||
traefik:
|
||||
image: "traefik:v2.9.4"
|
||||
container_name: "traefik"
|
||||
restart: unless-stopped
|
||||
command:
|
||||
- "--log.level=DEBUG"
|
||||
- "--accesslog"
|
||||
- "--accesslog.filepath=/var/log/traefik/access.log"
|
||||
- "--api.insecure=true"
|
||||
- "--providers.docker=true"
|
||||
- "--providers.docker.exposedbydefault=false"
|
||||
- "--entrypoints.web.address=:80"
|
||||
|
||||
- "--experimental.localplugins.bouncer.modulename=github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin"
|
||||
volumes:
|
||||
- /var/run/docker.sock:/var/run/docker.sock:ro
|
||||
- logs-local:/var/log/traefik
|
||||
- ./:/plugins-local/src/github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin
|
||||
ports:
|
||||
- 80:80
|
||||
- 8080:8080
|
||||
depends_on:
|
||||
- crowdsec
|
||||
|
||||
whoami-foo:
|
||||
image: traefik/whoami
|
||||
container_name: "simple-service-foo"
|
||||
restart: unless-stopped
|
||||
labels:
|
||||
- "traefik.enable=true"
|
||||
- "traefik.http.routers.router-foo.rule=Path(`/foo`)"
|
||||
- "traefik.http.routers.router-foo.entrypoints=web"
|
||||
- "traefik.http.routers.router-foo.middlewares=crowdsec-foo@docker"
|
||||
- "traefik.http.services.service-foo.loadbalancer.server.port=80"
|
||||
- "traefik.http.middlewares.crowdsec-foo.plugin.bouncer.enabled=true"
|
||||
- "traefik.http.middlewares.crowdsec-foo.plugin.bouncer.crowdseclapikey=40796d93c2958f9e58345514e67740e5"
|
||||
|
||||
whoami2:
|
||||
image: traefik/whoami
|
||||
container_name: "simple-service-bar"
|
||||
restart: unless-stopped
|
||||
labels:
|
||||
- "traefik.enable=true"
|
||||
- "traefik.http.routers.router-bar.rule=Path(`/bar`)"
|
||||
- "traefik.http.routers.router-bar.entrypoints=web"
|
||||
- "traefik.http.routers.router-bar.middlewares=crowdsec-bar@docker"
|
||||
- "traefik.http.services.service-bar.loadbalancer.server.port=80"
|
||||
- "traefik.http.middlewares.crowdsec-bar.plugin.bouncer.enabled=true"
|
||||
- "traefik.http.middlewares.crowdsec-bar.plugin.bouncer.crowdseclapikey=44c36dac5c4140af9f06f397508e82c7"
|
||||
|
||||
crowdsec:
|
||||
image: crowdsecurity/crowdsec:v1.4.1
|
||||
container_name: "crowdsec"
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
COLLECTIONS: crowdsecurity/traefik
|
||||
CUSTOM_HOSTNAME: crowdsec
|
||||
BOUNCER_KEY_TRAEFIK_1: 40796d93c2958f9e58345514e67740e5
|
||||
BOUNCER_KEY_TRAEFIK_2: 44c36dac5c4140af9f06f397508e82c7
|
||||
volumes:
|
||||
- ./acquis.yaml:/etc/crowdsec/acquis.yaml:ro
|
||||
- logs-local:/var/log/traefik:ro
|
||||
- crowdsec-db-local:/var/lib/crowdsec/data/
|
||||
- crowdsec-config-local:/etc/crowdsec/
|
||||
labels:
|
||||
- "traefik.enable=false"
|
||||
volumes:
|
||||
logs-local:
|
||||
crowdsec-db-local:
|
||||
crowdsec-config-local:
|
||||
+48
-15
@@ -2,50 +2,83 @@ version: "3.8"
|
||||
|
||||
services:
|
||||
traefik:
|
||||
image: "traefik:v2.8.7"
|
||||
image: "traefik:v2.9.4"
|
||||
container_name: "traefik"
|
||||
restart: unless-stopped
|
||||
command:
|
||||
# - "--log.level=DEBUG"
|
||||
- "--accesslog"
|
||||
- "--accesslog.filepath=/var/log/traefik/traefik.log"
|
||||
- "--accesslog.filepath=/var/log/traefik/access.log"
|
||||
- "--api.insecure=true"
|
||||
- "--providers.docker=true"
|
||||
- "--providers.docker.exposedbydefault=false"
|
||||
- "--entrypoints.web.address=:80"
|
||||
- "--experimental.localplugins.bouncer.modulename=github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin"
|
||||
|
||||
- "--experimental.plugins.bouncer.modulename=github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin"
|
||||
- "--experimental.plugins.bouncer.version=v1.1.2"
|
||||
volumes:
|
||||
- "/var/run/docker.sock:/var/run/docker.sock:ro"
|
||||
- "logs:/var/log/traefik"
|
||||
ports:
|
||||
- 8000:80
|
||||
- 8080:8080
|
||||
depends_on:
|
||||
- 'crowdsec'
|
||||
|
||||
whoami:
|
||||
whoami1:
|
||||
image: traefik/whoami
|
||||
container_name: "simple-service"
|
||||
container_name: "simple-service-foo"
|
||||
restart: unless-stopped
|
||||
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.middlewares.crowdsec.plugin.bouncer.enabled=true"
|
||||
- "traefik.http.middlewares.crowdsec.plugin.bouncer.crowdseclapikey=FIXME-LAPI-KEY"
|
||||
# Definition of the router
|
||||
- "traefik.http.routers.router-foo.rule=Path(`/foo`)"
|
||||
- "traefik.http.routers.router-foo.entrypoints=web"
|
||||
- "traefik.http.routers.router-foo.middlewares=crowdsec-foo@docker"
|
||||
# Definition of the service
|
||||
- "traefik.http.services.service-foo.loadbalancer.server.port=80"
|
||||
# Definition of the middleware
|
||||
- "traefik.http.middlewares.crowdsec-foo.plugin.bouncer.enabled=true"
|
||||
# crowdseclapikey must be unique to the middleware attached to the service
|
||||
- "traefik.http.middlewares.crowdsec-foo.plugin.bouncer.crowdseclapikey=FIXME-LAPI-KEY-1"
|
||||
# forwardedheaderstrustedips should be the IP of the proxy that is in front of traefik (if any)
|
||||
- "traefik.http.middlewares.crowdsec-foo.plugin.bouncer.forwardedheaderstrustedips=172.21.0.5"
|
||||
|
||||
whoami2:
|
||||
image: traefik/whoami
|
||||
container_name: "simple-service-bar"
|
||||
restart: unless-stopped
|
||||
labels:
|
||||
- "traefik.enable=true"
|
||||
# Definition of the router
|
||||
- "traefik.http.routers.router-bar.rule=Path(`/bar`)"
|
||||
- "traefik.http.routers.router-bar.entrypoints=web"
|
||||
- "traefik.http.routers.router-bar.middlewares=crowdsec-bar@docker"
|
||||
# Definition of the service
|
||||
- "traefik.http.services.service-bar.loadbalancer.server.port=80"
|
||||
# Definitin of the middleware
|
||||
- "traefik.http.middlewares.crowdsec-bar.plugin.bouncer.enabled=true"
|
||||
# crowdseclapikey must be unique to the middleware attached to the service
|
||||
- "traefik.http.middlewares.crowdsec-bar.plugin.bouncer.crowdseclapikey=FIXME-LAPI-KEY-2"
|
||||
# forwardedheaderstrustedips should be the IP of the proxy that is in front of traefik (if any)
|
||||
- "traefik.http.middlewares.crowdsec-bar.plugin.bouncer.forwardedheaderstrustedips=172.21.0.5"
|
||||
|
||||
crowdsec:
|
||||
image: crowdsecurity/crowdsec:v1.4.1
|
||||
container_name: "crowdsec"
|
||||
command: rm -rf /etc/crowdsec/acquis.yaml
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
COLLECTIONS: crowdsecurity/traefik
|
||||
CUSTOM_HOSTNAME: crowdsec
|
||||
BOUNCER_KEY_TRAEFIK: FIXME-LAPI-KEY
|
||||
depends_on:
|
||||
- 'traefik'
|
||||
# We need to register one api key per service we will use
|
||||
BOUNCER_KEY_TRAEFIK_1: FIXME-LAPI-KEY-1
|
||||
BOUNCER_KEY_TRAEFIK_2: FIXME-LAPI-KEY-2
|
||||
volumes:
|
||||
- ./acquis.yaml:/etc/crowdsec/acquis.yaml:ro
|
||||
- logs:/var/log/traefik:ro
|
||||
- crowdsec-db:/var/lib/crowdsec/data/
|
||||
- crowdsec-config:/etc/crowdsec/
|
||||
labels:
|
||||
- "traefik.enable=false"
|
||||
|
||||
volumes:
|
||||
logs:
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
filenames:
|
||||
- /var/log/traefik/access.log
|
||||
labels:
|
||||
type: traefik
|
||||
@@ -0,0 +1,15 @@
|
||||
http:
|
||||
# Add the router
|
||||
routers:
|
||||
router0:
|
||||
entryPoints:
|
||||
- web
|
||||
service: service-foo
|
||||
rule: Path(`/foo`)
|
||||
|
||||
# Add the service
|
||||
services:
|
||||
service-foo:
|
||||
loadBalancer:
|
||||
servers:
|
||||
- url: http://traefik/foo:80
|
||||
@@ -0,0 +1,111 @@
|
||||
version: "3.8"
|
||||
|
||||
services:
|
||||
cloudflare:
|
||||
image: "traefik:v2.9.4"
|
||||
container_name: "cloudflare"
|
||||
restart: unless-stopped
|
||||
command:
|
||||
# - "--log.level=DEBUG"
|
||||
- "--accesslog"
|
||||
- "--accesslog.filepath=/var/log/traefik/access.log"
|
||||
- "--api.insecure=true"
|
||||
- "--entrypoints.web.address=:80"
|
||||
- "--providers.file.filename=/cloud.yaml"
|
||||
- "--experimental.localplugins.bouncer.modulename=github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin"
|
||||
volumes:
|
||||
- /var/run/docker.sock:/var/run/docker.sock:ro
|
||||
- ./cloudflare-exemple.yaml:/cloud.yaml:ro
|
||||
- logs-cloudflare:/var/log/traefik
|
||||
ports:
|
||||
- 80:80
|
||||
- 8080:8080
|
||||
|
||||
traefik:
|
||||
image: "traefik:v2.9.4"
|
||||
container_name: "traefik"
|
||||
restart: unless-stopped
|
||||
command:
|
||||
# - "--log.level=DEBUG"
|
||||
- "--accesslog"
|
||||
- "--accesslog.filepath=/var/log/traefik/access.log"
|
||||
- "--api.insecure=true"
|
||||
- "--providers.docker=true"
|
||||
- "--providers.docker.exposedbydefault=false"
|
||||
- "--entrypoints.web.address=:80"
|
||||
- "--entrypoints.web.forwardedheaders.trustedips=172.21.0.5"
|
||||
|
||||
- "--experimental.plugins.bouncer.modulename=github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin"
|
||||
- "--experimental.plugins.bouncer.version=v1.1.3"
|
||||
volumes:
|
||||
- /var/run/docker.sock:/var/run/docker.sock:ro
|
||||
- logs-traefik:/var/log/traefik
|
||||
ports:
|
||||
- 90:80
|
||||
- 9080:8080
|
||||
depends_on:
|
||||
- crowdsec
|
||||
|
||||
whoami1:
|
||||
image: traefik/whoami
|
||||
container_name: "simple-service-foo"
|
||||
restart: unless-stopped
|
||||
labels:
|
||||
- "traefik.enable=true"
|
||||
# Definition of the router
|
||||
- "traefik.http.routers.router-foo.rule=Path(`/foo`)"
|
||||
- "traefik.http.routers.router-foo.entrypoints=web"
|
||||
- "traefik.http.routers.router-foo.middlewares=crowdsec-foo@docker"
|
||||
# Definition of the service
|
||||
- "traefik.http.services.service-foo.loadbalancer.server.port=80"
|
||||
# Definitin of the middleware
|
||||
- "traefik.http.middlewares.crowdsec-foo.plugin.bouncer.enabled=true"
|
||||
# crowdseclapikey must be uniq to the middleware attached to the service
|
||||
- "traefik.http.middlewares.crowdsec-foo.plugin.bouncer.crowdseclapikey=40796d93c2958f9e58345514e67740e5"
|
||||
- "traefik.http.middlewares.crowdsec-foo.plugin.bouncer.crowdsecmode=live"
|
||||
- "traefik.http.middlewares.crowdsec-foo.plugin.bouncer.forwardedheaderstrustedips=172.21.0.5"
|
||||
- "traefik.http.middlewares.crowdsec-foo.plugin.bouncer.loglevel=DEBUG"
|
||||
|
||||
whoami2:
|
||||
image: traefik/whoami
|
||||
container_name: "simple-service-bar"
|
||||
restart: unless-stopped
|
||||
labels:
|
||||
- "traefik.enable=true"
|
||||
# Definition of the router
|
||||
- "traefik.http.routers.router-bar.rule=Path(`/bar`)"
|
||||
- "traefik.http.routers.router-bar.entrypoints=web"
|
||||
- "traefik.http.routers.router-bar.middlewares=crowdsec-bar@docker"
|
||||
# Definition of the service
|
||||
- "traefik.http.services.service-bar.loadbalancer.server.port=80"
|
||||
# Definitin of the middleware
|
||||
- "traefik.http.middlewares.crowdsec-bar.plugin.bouncer.enabled=true"
|
||||
# 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.crowdsecmode=live"
|
||||
- "traefik.http.middlewares.crowdsec-bar.plugin.bouncer.forwardedheaderstrustedips=172.21.0.5"
|
||||
- "traefik.http.middlewares.crowdsec-bar.plugin.bouncer.loglevel=DEBUG"
|
||||
|
||||
|
||||
crowdsec:
|
||||
image: crowdsecurity/crowdsec:v1.4.1
|
||||
container_name: "crowdsec"
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
COLLECTIONS: crowdsecurity/traefik
|
||||
CUSTOM_HOSTNAME: crowdsec
|
||||
BOUNCER_KEY_TRAEFIK_DEV_1: 40796d93c2958f9e58345514e67740e5
|
||||
BOUNCER_KEY_TRAEFIK_DEV_2: 44c36dac5c4140af9f06f397508e82c7
|
||||
volumes:
|
||||
- ./acquis.yaml:/etc/crowdsec/acquis.yaml:ro
|
||||
- logs-cloudflare:/var/log/traefik:ro
|
||||
- crowdsec-db-cloudflare:/var/lib/crowdsec/data/
|
||||
- crowdsec-config-cloudflare:/etc/crowdsec/
|
||||
labels:
|
||||
- "traefik.enable=false"
|
||||
|
||||
volumes:
|
||||
logs-traefik:
|
||||
logs-cloudflare:
|
||||
crowdsec-db-cloudflare:
|
||||
crowdsec-config-cloudflare:
|
||||
@@ -0,0 +1,4 @@
|
||||
filenames:
|
||||
- /var/log/traefik/access.log
|
||||
labels:
|
||||
type: traefik
|
||||
@@ -0,0 +1,101 @@
|
||||
version: "3.8"
|
||||
|
||||
services:
|
||||
traefik:
|
||||
image: "traefik:v2.9.4"
|
||||
container_name: "traefik"
|
||||
restart: unless-stopped
|
||||
command:
|
||||
# - "--log.level=DEBUG"
|
||||
- "--accesslog"
|
||||
- "--accesslog.filepath=/var/log/traefik/access.log"
|
||||
- "--api.insecure=true"
|
||||
- "--providers.docker=true"
|
||||
- "--providers.docker.exposedbydefault=false"
|
||||
- "--entrypoints.web.address=:80"
|
||||
|
||||
- "--experimental.plugins.bouncer.modulename=github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin"
|
||||
- "--experimental.plugins.bouncer.version=v1.1.3"
|
||||
# - "--experimental.localplugins.bouncer.modulename=github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin"
|
||||
volumes:
|
||||
- /var/run/docker.sock:/var/run/docker.sock:ro
|
||||
- logs-redis:/var/log/traefik
|
||||
- ./../../:/plugins-local/src/github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin
|
||||
ports:
|
||||
- 80:80
|
||||
- 8080:8080
|
||||
depends_on:
|
||||
- crowdsec
|
||||
- redis
|
||||
|
||||
whoami-foo:
|
||||
image: traefik/whoami
|
||||
container_name: "simple-service-foo"
|
||||
restart: unless-stopped
|
||||
labels:
|
||||
- "traefik.enable=true"
|
||||
# Definition of the router
|
||||
- "traefik.http.routers.router-foo.rule=Path(`/foo`)"
|
||||
- "traefik.http.routers.router-foo.entrypoints=web"
|
||||
- "traefik.http.routers.router-foo.middlewares=crowdsec-foo@docker"
|
||||
# Definition of the service
|
||||
- "traefik.http.services.service-foo.loadbalancer.server.port=80"
|
||||
# Definition of the middleware
|
||||
- "traefik.http.middlewares.crowdsec-foo.plugin.bouncer.enabled=true"
|
||||
# crowdseclapikey must be uniq to the middleware attached to the service
|
||||
- "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.loglevel=DEBUG"
|
||||
|
||||
whoami-bar:
|
||||
image: traefik/whoami
|
||||
container_name: "simple-service-bar"
|
||||
restart: unless-stopped
|
||||
labels:
|
||||
- "traefik.enable=true"
|
||||
# Definition of the router
|
||||
- "traefik.http.routers.router-bar.rule=Path(`/bar`)"
|
||||
- "traefik.http.routers.router-bar.entrypoints=web"
|
||||
- "traefik.http.routers.router-bar.middlewares=crowdsec-bar@docker"
|
||||
# Definition of the service
|
||||
- "traefik.http.services.service-bar.loadbalancer.server.port=80"
|
||||
# Definition of the middleware
|
||||
- "traefik.http.middlewares.crowdsec-bar.plugin.bouncer.enabled=true"
|
||||
# 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.rediscacheenabled=true"
|
||||
- "traefik.http.middlewares.crowdsec-bar.plugin.bouncer.loglevel=DEBUG"
|
||||
|
||||
|
||||
crowdsec:
|
||||
image: crowdsecurity/crowdsec:v1.4.1
|
||||
container_name: "crowdsec"
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
COLLECTIONS: crowdsecurity/traefik
|
||||
CUSTOM_HOSTNAME: crowdsec
|
||||
BOUNCER_KEY_TRAEFIK_DEV_1: 40796d93c2958f9e58345514e67740e5
|
||||
BOUNCER_KEY_TRAEFIK_DEV_2: 44c36dac5c4140af9f06f397508e82c7
|
||||
volumes:
|
||||
- ./acquis.yaml:/etc/crowdsec/acquis.yaml:ro
|
||||
- logs-redis:/var/log/traefik:ro
|
||||
- crowdsec-db-redis:/var/lib/crowdsec/data/
|
||||
- crowdsec-config-redis:/etc/crowdsec/
|
||||
labels:
|
||||
- "traefik.enable=false"
|
||||
|
||||
redis:
|
||||
image: "redis:7.0.5-alpine"
|
||||
container_name: "redis"
|
||||
restart: unless-stopped
|
||||
command: "redis-server --save 60 1"
|
||||
volumes:
|
||||
- redis-data:/data
|
||||
ports:
|
||||
- 6379:6379
|
||||
|
||||
volumes:
|
||||
logs-redis:
|
||||
crowdsec-db-redis:
|
||||
crowdsec-config-redis:
|
||||
redis-data:
|
||||
@@ -0,0 +1,4 @@
|
||||
filenames:
|
||||
- /var/log/traefik/access.log
|
||||
labels:
|
||||
type: traefik
|
||||
@@ -0,0 +1,91 @@
|
||||
version: "3.8"
|
||||
|
||||
services:
|
||||
traefik:
|
||||
image: "traefik:v2.9.4"
|
||||
container_name: "traefik"
|
||||
restart: unless-stopped
|
||||
command:
|
||||
# - "--log.level=DEBUG"
|
||||
- "--accesslog"
|
||||
- "--accesslog.filepath=/var/log/traefik/access.log"
|
||||
- "--api.insecure=true"
|
||||
- "--providers.docker=true"
|
||||
- "--providers.docker.exposedbydefault=false"
|
||||
- "--entrypoints.web.address=:80"
|
||||
|
||||
- "--experimental.plugins.bouncer.modulename=github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin"
|
||||
- "--experimental.plugins.bouncer.version=v1.1.3"
|
||||
# - "--experimental.localplugins.bouncer.modulename=github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin"
|
||||
volumes:
|
||||
- /var/run/docker.sock:/var/run/docker.sock:ro
|
||||
- logs-trustedips:/var/log/traefik
|
||||
- ./../../:/plugins-local/src/github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin
|
||||
ports:
|
||||
- 80:80
|
||||
- 8080:8080
|
||||
depends_on:
|
||||
- crowdsec
|
||||
|
||||
whoami1:
|
||||
image: traefik/whoami
|
||||
container_name: "simple-service-foo"
|
||||
restart: unless-stopped
|
||||
labels:
|
||||
- "traefik.enable=true"
|
||||
# Definition of the router
|
||||
- "traefik.http.routers.router-foo.rule=Path(`/foo`)"
|
||||
- "traefik.http.routers.router-foo.entrypoints=web"
|
||||
- "traefik.http.routers.router-foo.middlewares=crowdsec-foo@docker"
|
||||
# Definition of the service
|
||||
- "traefik.http.services.service-foo.loadbalancer.server.port=80"
|
||||
# Definition of the middleware
|
||||
- "traefik.http.middlewares.crowdsec-foo.plugin.bouncer.enabled=true"
|
||||
# crowdseclapikey must be uniq to the middleware attached to the service
|
||||
- "traefik.http.middlewares.crowdsec-foo.plugin.bouncer.crowdseclapikey=40796d93c2958f9e58345514e67740e5"
|
||||
# Replace 10.0.10.30/32 by your IP range which is "trusted"
|
||||
- "traefik.http.middlewares.crowdsec-foo.plugin.bouncer.clienttrustedips=10.0.10.30/32"
|
||||
- "traefik.http.middlewares.crowdsec-foo.plugin.bouncer.loglevel=DEBUG"
|
||||
|
||||
whoami2:
|
||||
image: traefik/whoami
|
||||
container_name: "simple-service-bar"
|
||||
restart: unless-stopped
|
||||
labels:
|
||||
- "traefik.enable=true"
|
||||
# Definition of the router
|
||||
- "traefik.http.routers.router-bar.rule=Path(`/bar`)"
|
||||
- "traefik.http.routers.router-bar.entrypoints=web"
|
||||
- "traefik.http.routers.router-bar.middlewares=crowdsec-bar@docker"
|
||||
# Definition of the service
|
||||
- "traefik.http.services.service-bar.loadbalancer.server.port=80"
|
||||
# Definition of the middleware
|
||||
- "traefik.http.middlewares.crowdsec-bar.plugin.bouncer.enabled=true"
|
||||
# crowdseclapikey must be uniq to the middleware attached to the service
|
||||
- "traefik.http.middlewares.crowdsec-bar.plugin.bouncer.crowdseclapikey=44c36dac5c4140af9f06f397508e82c7"
|
||||
# Replace 10.0.10.30/32 by your IP range which is "trusted"
|
||||
- "traefik.http.middlewares.crowdsec-bar.plugin.bouncer.clienttrustedips=10.0.10.30/32"
|
||||
- "traefik.http.middlewares.crowdsec-bar.plugin.bouncer.loglevel=DEBUG"
|
||||
|
||||
|
||||
crowdsec:
|
||||
image: crowdsecurity/crowdsec:v1.4.1
|
||||
container_name: "crowdsec"
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
COLLECTIONS: crowdsecurity/traefik
|
||||
CUSTOM_HOSTNAME: crowdsec
|
||||
BOUNCER_KEY_TRAEFIK_DEV_1: 40796d93c2958f9e58345514e67740e5
|
||||
BOUNCER_KEY_TRAEFIK_DEV_2: 44c36dac5c4140af9f06f397508e82c7
|
||||
volumes:
|
||||
- ./acquis.yaml:/etc/crowdsec/acquis.yaml:ro
|
||||
- logs-trustedips:/var/log/traefik:ro
|
||||
- crowdsec-db-trustedips:/var/lib/crowdsec/data/
|
||||
- crowdsec-config-trustedips:/etc/crowdsec/
|
||||
labels:
|
||||
- "traefik.enable=false"
|
||||
|
||||
volumes:
|
||||
logs-trustedips:
|
||||
crowdsec-db-trustedips:
|
||||
crowdsec-config-trustedips:
|
||||
@@ -1,5 +1,5 @@
|
||||
module github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin
|
||||
|
||||
go 1.17
|
||||
go 1.18
|
||||
|
||||
require github.com/leprosus/golang-ttl-map v1.1.7
|
||||
|
||||
Vendored
+98
@@ -0,0 +1,98 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
ttl_map "github.com/leprosus/golang-ttl-map"
|
||||
|
||||
logger "github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin/pkg/logger"
|
||||
simpleredis "github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin/pkg/redis"
|
||||
)
|
||||
|
||||
const (
|
||||
cacheBannedValue = "t"
|
||||
cacheNoBannedValue = "f"
|
||||
)
|
||||
|
||||
var cache = ttl_map.New()
|
||||
var redis simpleredis.SimpleRedis
|
||||
|
||||
var redisEnabled = false
|
||||
|
||||
// CLASSIC
|
||||
|
||||
func getDecisionLocalCache(clientIP string) (bool, error) {
|
||||
banned, isCached := cache.Get(clientIP)
|
||||
bannedString, isValid := banned.(string)
|
||||
if isCached && isValid && len(bannedString) > 0 {
|
||||
return bannedString == cacheBannedValue, nil
|
||||
}
|
||||
return false, fmt.Errorf("cache:miss")
|
||||
}
|
||||
|
||||
func setDecisionLocalCache(clientIP string, value string, duration int64) {
|
||||
cache.Set(clientIP, value, duration)
|
||||
}
|
||||
|
||||
func deleteDecisionLocalCache(clientIP string) {
|
||||
cache.Del(clientIP)
|
||||
}
|
||||
|
||||
// REDIS
|
||||
|
||||
func getDecisionRedisCache(clientIP string) (bool, error) {
|
||||
banned, err := redis.Get(clientIP)
|
||||
bannedString := string(banned)
|
||||
if err == nil && len(bannedString) > 0 {
|
||||
return bannedString == cacheBannedValue, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
|
||||
func setDecisionRedisCache(clientIP string, value string, duration int64) {
|
||||
redis.Set(clientIP, []byte(value), duration)
|
||||
}
|
||||
|
||||
func deleteDecisionRedisCache(clientIP string) {
|
||||
redis.Del(clientIP)
|
||||
}
|
||||
|
||||
// DeleteDecision delete decision in cache
|
||||
func DeleteDecision(clientIP string) {
|
||||
if redisEnabled {
|
||||
deleteDecisionRedisCache(clientIP)
|
||||
} else {
|
||||
deleteDecisionLocalCache(clientIP)
|
||||
}
|
||||
}
|
||||
|
||||
// GetDecision 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(clientIP string) (bool, error) {
|
||||
if redisEnabled {
|
||||
return getDecisionRedisCache(clientIP)
|
||||
} else {
|
||||
return getDecisionLocalCache(clientIP)
|
||||
}
|
||||
}
|
||||
|
||||
func SetDecision(clientIP string, isBanned bool, duration int64) {
|
||||
var value string
|
||||
if isBanned {
|
||||
logger.Debug(fmt.Sprintf("%v banned", clientIP))
|
||||
value = cacheBannedValue
|
||||
} else {
|
||||
value = cacheNoBannedValue
|
||||
}
|
||||
if redisEnabled {
|
||||
setDecisionRedisCache(clientIP, value, duration)
|
||||
} else {
|
||||
setDecisionLocalCache(clientIP, value, duration)
|
||||
}
|
||||
}
|
||||
|
||||
func InitRedisClient(host string) {
|
||||
redisEnabled = true
|
||||
redis.Init(host)
|
||||
logger.Debug("Redis initialized")
|
||||
}
|
||||
+127
@@ -0,0 +1,127 @@
|
||||
package ip
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin/pkg/logger"
|
||||
)
|
||||
|
||||
// CHECKER
|
||||
|
||||
// Checker allows to check that addresses are in a trusted IPs.
|
||||
type Checker struct {
|
||||
authorizedIPs []*net.IP
|
||||
authorizedIPsNet []*net.IPNet
|
||||
}
|
||||
|
||||
// NewChecker builds a new Checker given a list of CIDR-Strings to trusted IPs.
|
||||
func NewChecker(trustedIPs []string) (*Checker, error) {
|
||||
checker := &Checker{}
|
||||
|
||||
for _, ipMask := range trustedIPs {
|
||||
if ipAddr := net.ParseIP(ipMask); ipAddr != nil {
|
||||
checker.authorizedIPs = append(checker.authorizedIPs, &ipAddr)
|
||||
logger.Debug(fmt.Sprintf("IP %v is trusted", ipAddr))
|
||||
continue
|
||||
}
|
||||
|
||||
_, ipAddr, err := net.ParseCIDR(ipMask)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parsing CIDR trusted IPs %s: %w", ipAddr, err)
|
||||
}
|
||||
checker.authorizedIPsNet = append(checker.authorizedIPsNet, ipAddr)
|
||||
logger.Debug(fmt.Sprintf("IP network %v is trusted", ipAddr))
|
||||
}
|
||||
|
||||
return checker, nil
|
||||
}
|
||||
|
||||
// Contains checks if provided address is in the trusted IPs.
|
||||
func (ip *Checker) Contains(addr string) (bool, error) {
|
||||
if len(addr) == 0 {
|
||||
return false, errors.New("empty IP address")
|
||||
}
|
||||
|
||||
ipAddr, err := parseIP(addr)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("unable to parse address: %s: %w", addr, err)
|
||||
}
|
||||
|
||||
return ip.ContainsIP(ipAddr), nil
|
||||
}
|
||||
|
||||
// ContainsIP checks if provided address is in the trusted IPs.
|
||||
func (ip *Checker) ContainsIP(addr net.IP) bool {
|
||||
for _, authorizedIP := range ip.authorizedIPs {
|
||||
if authorizedIP.Equal(addr) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
for _, authorizedNet := range ip.authorizedIPsNet {
|
||||
if authorizedNet.Contains(addr) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func parseIP(addr string) (net.IP, error) {
|
||||
userIP := net.ParseIP(addr)
|
||||
if userIP == nil {
|
||||
return nil, fmt.Errorf("can't parse IP from address %s", addr)
|
||||
}
|
||||
|
||||
return userIP, nil
|
||||
}
|
||||
|
||||
// STRATEGY
|
||||
|
||||
// PoolStrategy is a strategy based on an IP Checker.
|
||||
// It allows to check whether addresses are in a given pool of IPs.
|
||||
type PoolStrategy struct {
|
||||
Checker *Checker
|
||||
}
|
||||
|
||||
// GetIP checks the list of Forwarded IPs (most recent first) against the
|
||||
// Checker pool of IPs. It returns the first IP that is not in the pool, or the
|
||||
// empty string otherwise.
|
||||
func (s *PoolStrategy) getIP(req *http.Request, customHeader string) string {
|
||||
if s.Checker == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
xff := req.Header.Get(customHeader)
|
||||
|
||||
xffs := strings.Split(xff, ",")
|
||||
|
||||
for i := len(xffs) - 1; i >= 0; i-- {
|
||||
xffTrimmed := strings.TrimSpace(xffs[i])
|
||||
if len(xffTrimmed) == 0 {
|
||||
continue
|
||||
}
|
||||
if contain, _ := s.Checker.Contains(xffTrimmed); !contain {
|
||||
return xffTrimmed
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
// GetRemoteIP It returns the first IP that is not in the pool, or the empty string otherwise.
|
||||
func GetRemoteIP(req *http.Request, strategy *PoolStrategy, customHeader string) (string, error) {
|
||||
remoteIP := strategy.getIP(req, customHeader)
|
||||
if len(remoteIP) != 0 {
|
||||
return remoteIP, nil
|
||||
}
|
||||
remoteIP, _, err := net.SplitHostPort(req.RemoteAddr)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to extract ip from remote address: %w", err)
|
||||
}
|
||||
return remoteIP, nil
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
var (
|
||||
loggerInfo = log.New(io.Discard, "INFO: CrowdsecBouncerTraefikPlugin: ", log.Ldate|log.Ltime)
|
||||
loggerDebug = log.New(io.Discard, "DEBUG: CrowdsecBouncerTraefikPlugin: ", log.Ldate|log.Ltime)
|
||||
)
|
||||
|
||||
// Init Set Default log level to info in case log level to defined
|
||||
func Init(logLevel string) {
|
||||
switch logLevel {
|
||||
case "INFO":
|
||||
loggerInfo.SetOutput(os.Stdout)
|
||||
case "DEBUG":
|
||||
loggerInfo.SetOutput(os.Stdout)
|
||||
loggerDebug.SetOutput(os.Stdout)
|
||||
default:
|
||||
loggerInfo.SetOutput(os.Stdout)
|
||||
}
|
||||
}
|
||||
|
||||
// Info Log info
|
||||
func Info(str string) {
|
||||
loggerInfo.Printf(str)
|
||||
}
|
||||
|
||||
// Info Log debug
|
||||
func Debug(str string) {
|
||||
loggerDebug.Printf(str)
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
package simpleredis
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/textproto"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
logger "github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin/pkg/logger"
|
||||
)
|
||||
|
||||
const (
|
||||
RedisUnreachable = "redis:unreachable"
|
||||
RedisMiss = "redis:miss"
|
||||
RedisTimeout = "redis:timeout"
|
||||
)
|
||||
|
||||
type RedisCmd struct {
|
||||
Command string
|
||||
Name string
|
||||
Data []byte
|
||||
Duration int64
|
||||
Error error
|
||||
}
|
||||
|
||||
type SimpleRedis struct {
|
||||
redisHost string
|
||||
}
|
||||
|
||||
func genRedisArray(params ...[]byte) []byte {
|
||||
MSG := ""
|
||||
for cntr := 0; cntr < len(params); cntr++ {
|
||||
MSG = strings.Join([]string{MSG, string(params[cntr])}, " ")
|
||||
}
|
||||
MSG = strings.Trim(MSG, " ")
|
||||
MSG = strings.Join([]string{MSG, "\r\n"}, "")
|
||||
return []byte(MSG)
|
||||
}
|
||||
|
||||
func askRedis(hostnamePort string, cmd RedisCmd, channel chan RedisCmd) {
|
||||
dialer := net.Dialer{Timeout: 2 * time.Second}
|
||||
conn, err := dialer.Dial("tcp", hostnamePort)
|
||||
if err != nil {
|
||||
channel <- RedisCmd{Error: fmt.Errorf(RedisUnreachable)}
|
||||
return
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
writer := textproto.NewWriter(bufio.NewWriter(conn))
|
||||
reader := textproto.NewReader(bufio.NewReader(conn))
|
||||
|
||||
switch cmd.Command {
|
||||
case "SET":
|
||||
data := genRedisArray([]byte("SET"), []byte(cmd.Name), []byte(cmd.Data), []byte("EX"), []byte(fmt.Sprintf("%v", cmd.Duration)))
|
||||
writer.PrintfLine(string(data))
|
||||
logger.Debug("redis:set")
|
||||
case "DEL":
|
||||
data := genRedisArray([]byte("DEL"), []byte(cmd.Name))
|
||||
writer.PrintfLine(string(data))
|
||||
logger.Debug("redis:del")
|
||||
case "GET":
|
||||
data := genRedisArray([]byte("GET"), []byte(cmd.Name))
|
||||
writer.PrintfLine(string(data))
|
||||
logger.Debug("redis:get")
|
||||
for {
|
||||
select {
|
||||
case <-time.After(time.Second * 1):
|
||||
channel <- RedisCmd{Error: fmt.Errorf(RedisTimeout)}
|
||||
return
|
||||
default:
|
||||
read, _ := reader.ReadLineBytes()
|
||||
if string(read) != "$1" {
|
||||
channel <- RedisCmd{Error: fmt.Errorf(RedisMiss)}
|
||||
return
|
||||
}
|
||||
read, _ = reader.ReadLineBytes()
|
||||
channel <- RedisCmd{Data: read}
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (sr *SimpleRedis) Init(redisHost string) {
|
||||
sr.redisHost = redisHost
|
||||
}
|
||||
|
||||
func (sr *SimpleRedis) Get(name string) ([]byte, error) {
|
||||
redisCmd := RedisCmd{
|
||||
Command: "GET",
|
||||
Name: name,
|
||||
}
|
||||
channel := make(chan RedisCmd)
|
||||
go askRedis(sr.redisHost, redisCmd, channel)
|
||||
resp := <-channel
|
||||
if resp.Error != nil {
|
||||
return nil, resp.Error
|
||||
}
|
||||
return resp.Data, nil
|
||||
}
|
||||
|
||||
func (sr *SimpleRedis) Set(name string, data []byte, duration int64) error {
|
||||
redisCmd := RedisCmd{
|
||||
Command: "SET",
|
||||
Name: name,
|
||||
Data: data,
|
||||
Duration: duration,
|
||||
}
|
||||
go askRedis(sr.redisHost, redisCmd, nil)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (sr *SimpleRedis) Del(name string) error {
|
||||
redisCmd := RedisCmd{
|
||||
Command: "DEL",
|
||||
Name: name,
|
||||
}
|
||||
go askRedis(sr.redisHost, redisCmd, nil)
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user