📝 add a Nomad example (#425)

A single job runs whoami, Traefik and CrowdSec. CrowdSec is a prestart
sidecar in Traefik's group, so it reads Traefik's access log from the
allocation directory, and a prestart task waits for the LAPI before
Traefik starts. Traefik discovers whoami with the Nomad provider through
the task API socket, using the job's workload identity, which also
reads the bouncer key from a Nomad variable. This works with ACLs on.

The acquisition sets force_inotify: CrowdSec can load it before Traefik
has created its log file, and would then never follow it.

Renovate now also bumps the plugin pin and the image tags in the Nomad
example.

Closes #50

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
mathieuHaandClaude Opus 5.5 authored and GitHub committed 2026-11-07 12:48:59 +01:00
1 parent 0004a05cc7
commit 0f7ee2e42b
4 files changed
+267 -2

No files matched your search

+2
View File
@@ -858,6 +858,8 @@ docker exec crowdsec cscli decisions remove --ip 10.0.0.10 -t captcha
#### 12. Using Traefik with AppSec bot detection enabled [examples/bot-detection/README.md](https://github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin/blob/main/examples/bot-detection/README.md)
#### 13. Using Crowdsec and Traefik in Nomad [examples/nomad/README.md](https://github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin/blob/main/examples/nomad/README.md)
### Local Mode
Traefik also offers a developer mode that can be used for temporary testing of plugins not hosted on GitHub.
+58
View File
@@ -0,0 +1,58 @@
# Example
## Using Crowdsec and Traefik in Nomad
[traefik-crowdsec.nomad.hcl](traefik-crowdsec.nomad.hcl) is a single Nomad job with three tasks:
- `whoami`, the backend, registered as a Nomad service;
- `traefik`, which discovers it with the Nomad provider and runs the bouncer on its `web` entrypoint (port 8000);
- `crowdsec`, which serves the LAPI and AppSec, and reads Traefik's access logs.
CrowdSec runs as a sidecar of Traefik, in the same group, so that it can read Traefik's logs from the
allocation directory (`/alloc/logs/traefik.stdout.*`). A prestart task waits for the LAPI before Traefik starts.
The bouncer key is stored in a Nomad variable that only this job's workload identity can read. CrowdSec
registers the bouncer with it (`BOUNCER_KEY_traefik`), and the plugin reads it with `crowdsecLapiKeyFile`.
### Requirements
- Nomad 1.5 or later, with the Docker driver (tested with Nomad 2.1). `nomad agent -dev` is enough to try it.
- ACLs can be on or off. Traefik reaches the Nomad API through the task API socket with the job's workload
identity, which can read the job's own variables and the Nomad services in its namespace.
### Run it
Create the bouncer key, then the job:
```bash
nomad var put nomad/jobs/traefik-crowdsec bouncer_key=$(openssl rand -hex 32)
nomad job run examples/nomad/traefik-crowdsec.nomad.hcl
```
We can query the whoami service normally:
```bash
curl http://localhost:8000/foo
```
A malicious request is blocked by AppSec:
```bash
curl http://localhost:8000/.env
```
You should get a 403.
Then ban your IP (`127.0.0.1` from the node itself). The plugin runs in stream mode, so the ban applies
within a minute:
```bash
nomad job allocs traefik-crowdsec
nomad alloc exec -task crowdsec <allocation of the traefik group> cscli decisions add --ip 127.0.0.1
curl http://localhost:8000/foo
```
The Traefik dashboard listens on `127.0.0.1:8080` on the node.
### Notes
- `force_inotify: true` makes CrowdSec watch `/alloc/logs` from the start. Without it, CrowdSec only follows
Traefik's log if the file already exists when CrowdSec loads its acquisition, which races with Traefik's
start: the prestart task only waits for the LAPI.
- The sticky `ephemeral_disk` keeps the CrowdSec database (`/alloc/data/crowdsec`) when the allocation is
replaced on the same node. Use a host volume or a CSI volume to keep it anywhere else.
- In a namespace other than `default`, add `--providers.nomad.namespaces=<namespace>` to Traefik.
+193
View File
@@ -0,0 +1,193 @@
job "traefik-crowdsec" {
group "traefik" {
network {
port "web" {
static = 8000
}
port "lapi" {
to = 8080
}
port "appsec" {
to = 7422
}
}
ephemeral_disk {
sticky = true
migrate = true
}
task "crowdsec" {
driver = "docker"
lifecycle {
hook = "prestart"
sidecar = true
}
config {
image = "crowdsecurity/crowdsec:v1.8.2"
ports = ["lapi", "appsec"]
volumes = [
"local/acquis.d:/etc/crowdsec/acquis.d",
"../alloc/data/crowdsec:/var/lib/crowdsec/data",
]
}
env {
COLLECTIONS = "crowdsecurity/traefik crowdsecurity/appsec-virtual-patching crowdsecurity/appsec-generic-rules"
}
template {
data = <<-EOF
BOUNCER_KEY_traefik={{ with nomadVar "nomad/jobs/traefik-crowdsec" }}{{ .bouncer_key }}{{ end }}
EOF
destination = "secrets/crowdsec.env"
env = true
}
template {
data = <<-EOF
filenames:
- /alloc/logs/traefik.stdout.*
force_inotify: true
labels:
type: traefik
EOF
destination = "local/acquis.d/traefik.yaml"
}
template {
data = <<-EOF
source: appsec
listen_addr: 0.0.0.0:7422
appsec_config: crowdsecurity/appsec-default
labels:
type: appsec
EOF
destination = "local/acquis.d/appsec.yaml"
}
resources {
cpu = 500
memory = 512
}
}
task "wait-for-crowdsec" {
driver = "docker"
lifecycle {
hook = "prestart"
}
config {
image = "busybox:1.37"
network_mode = "host"
command = "sh"
args = ["-c", "until wget -q -O /dev/null http://${NOMAD_HOST_ADDR_lapi}/health; do sleep 2; done"]
}
resources {
cpu = 50
memory = 32
}
}
task "traefik" {
driver = "docker"
identity {
env = true
}
config {
image = "traefik:v3.7.17"
network_mode = "host"
args = [
"--entrypoints.web.address=:${NOMAD_PORT_web}",
"--entrypoints.web.http.middlewares=crowdsec@file",
"--entrypoints.traefik.address=127.0.0.1:8080",
"--api.insecure=true",
"--providers.nomad.exposedByDefault=false",
"--providers.file.filename=/local/dynamic.yml",
"--accesslog=true",
"--accesslog.format=json",
"--accesslog.fields.headers.defaultmode=drop",
"--accesslog.fields.headers.names.User-Agent=keep",
"--experimental.plugins.bouncer.modulename=github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin",
"--experimental.plugins.bouncer.version=v1.8.1",
]
}
env {
NOMAD_ADDR = "unix:///secrets/api.sock"
}
template {
data = <<-EOF
{{ with nomadVar "nomad/jobs/traefik-crowdsec" }}{{ .bouncer_key }}{{ end }}
EOF
destination = "secrets/bouncer_key"
}
template {
data = <<-EOF
http:
middlewares:
crowdsec:
plugin:
bouncer:
enabled: true
crowdsecMode: stream
crowdsecLapiScheme: http
crowdsecLapiHost: {{ env "NOMAD_HOST_ADDR_lapi" }}
crowdsecLapiKeyFile: /secrets/bouncer_key
crowdsecAppsecEnabled: true
crowdsecAppsecHost: {{ env "NOMAD_HOST_ADDR_appsec" }}
EOF
destination = "local/dynamic.yml"
}
resources {
cpu = 500
memory = 256
}
}
}
group "whoami" {
shutdown_delay = "5s"
network {
port "http" {
to = 80
}
}
service {
name = "whoami"
provider = "nomad"
port = "http"
tags = [
"traefik.enable=true",
"traefik.http.routers.whoami.rule=PathPrefix(`/`)",
"traefik.http.routers.whoami.entrypoints=web",
]
}
task "whoami" {
driver = "docker"
config {
image = "traefik/whoami"
ports = ["http"]
}
resources {
cpu = 50
memory = 32
}
}
}
}
+14 -2
View File
@@ -26,9 +26,12 @@
],
"customManagers": [
{
"description": "Plugin self-pin in docker-compose CLI args (--experimental.plugins.bouncer.version=vX)",
"description": "Plugin self-pin in docker-compose and Nomad CLI args (--experimental.plugins.bouncer.version=vX)",
"customType": "regex",
"managerFilePatterns": ["/(^|/)docker-compose[^/]*\\.ya?ml$/"],
"managerFilePatterns": [
"/(^|/)docker-compose[^/]*\\.ya?ml$/",
"/^examples/nomad/.+\\.hcl$/"
],
"matchStrings": [
"experimental\\.plugins\\.bouncer\\.version=(?<currentValue>v[0-9]+\\.[0-9]+\\.[0-9]+)"
],
@@ -76,6 +79,15 @@
],
"depNameTemplate": "traefik/traefik",
"datasourceTemplate": "github-releases"
},
{
"description": "Image tags in the Nomad example (docker driver image = \"name:tag\")",
"customType": "regex",
"managerFilePatterns": ["/^examples/nomad/.+\\.hcl$/"],
"matchStrings": [
"image\\s*=\\s*\"(?<depName>[^\":]+):(?<currentValue>[^\"]+)\""
],
"datasourceTemplate": "docker"
}
]
}