L'idée est d'utiliser une authentification "htpasswd", surveillée par Fail2ban, avec toutes vos applis docker gérées par traefik.
Je vous renvoie sur le tuto de @ex_rat pour l'installation de fail2ban.
https://mondedie.fr/d/5318-Tuto-Securisation-Logs-V-3-nginx
Exemple pour créer un id/pass avec htpasswd
htpasswd -c /etc/apache2/.htpasswd toto
Cela vous donne
toto:$apr1$HnXLUBvW$g.AzDXNiu7iXYv4qMwEhl/
que vous récupérez dans /etc/apache2/.htpasswd<br />
Création de la jail pour traefik
nano /etc/fail2ban/jail.d/traefik.conf
Vous copiez le contenu ci dessous
[DEFAULT]
ignoreip = 127.0.0.1 <ip_domicile>
findtime = 3600
bantime = 86400
maxretry = 3
[traefik-auth]
enabled = true
logpath = /var/log/traefik/access.log
port = http,https
banaction = docker-action
maxretry = 2
Ensuite le regex
nano /etc/fail2ban/filter.d/traefik-auth.conf
Vous copiez le contenu ci dessous
[Definition]
failregex = ^<HOST> \- \S+ \[\] \"(GET|POST|HEAD) .+\" 401 .+$
ignoreregex =
Fail2ban action
nano /etc/fail2ban/action.d/docker-action.conf
Coller le contenu ci dessous
[Definition]
actionstart = iptables -N f2b-traefik-auth
iptables -A f2b-traefik-auth -j RETURN
iptables -I FORWARD -p tcp -m multiport --dports 443 -j f2b-traefik-auth
actionstop = iptables -D FORWARD -p tcp -m multiport --dports 443 -j f2b-traefik-auth
iptables -F f2b-traefik-auth
iptables -X f2b-traefik-auth
actioncheck = iptables -n -L FORWARD | grep -q 'f2b-traefik-auth[ \t]'
actionban = iptables -I f2b-traefik-auth -s <ip> -j DROP
actionunban = iptables -D f2b-traefik-auth -s <ip> -j DROP
Configuration traefik
nano /home/<user>/traefik/traefik.toml
Copier le contenu ci dessous
defaultEntryPoints = ["https","http"]
InsecureSkipVerify = true
[api]
entryPoint = "traefik"
dashboard = true
[entryPoints]
[entryPoints.http]
address = ":80"
[entryPoints.http.redirect]
entryPoint = "https"
[entryPoints.https]
address = ":443"
[entryPoints.http.auth.basic]
users = ["toto:$apr1$HnXLUBvW$g.AzDXNiu7iXYv4qMwEhl/"]
[entryPoints.https.tls]
minVersion = "VersionTLS12"
cipherSuites = [
"TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305",
"TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
"TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
"TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256",
"TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA",
"TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA"
]
[entryPoints.traefik]
address = ":8080"
[acme]
email = "admin@example.com"
storage = "/etc/traefik/acme/acme.json"
entryPoint = "https"
onHostRule = true
onDemand = false
[acme.httpChallenge]
entryPoint = "http"
[docker]
endpoint = "unix:///var/run/docker.sock"
domain = "example.com"
watch = true
exposedbydefault = false
Vous modifiez le domaine, adresse mail et vous insérer le "passwd" créé plus haut à la place de l'exemple dans le fichier.
Et enfin le docker-compose de traefik
version: '3'
services:
traefik:
image: traefik
container_name: traefik
restart: unless-stopped
hostname: traefik
labels:
- traefik.enable=true
- traefik.frontend.rule=Host:traefik.example.com
- traefik.port=8080
- traefik.docker.network=traefik_proxy
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
- /home/<user>/traefik/traefik.toml:/traefik.toml:ro
- /home/<user>/letsencrypt/certs:/etc/traefik/acme:rw
- /var/log/traefik:/var/log
ports:
- "80:80"
- "443:443"
networks:
- proxy
command:
- --web
- --accessLog.filePath=/var/log/access.log
- --accessLog.filters.statusCodes=400-499
networks:
proxy:
external:
name: traefik_proxy
on relance le tout
docker-compose rm -fs traefik && docker-compose up -d traefik
systemctl restart fail2ban
fail2ban-client reload
On peut également poursuivre plus loin
On peut aussi utiliser une image docker fail2ban ca marche aussi bien
https://stackoverflow.com/questions/52123355/how-to-implement-fail2ban-with-traefik
Source : https://www.the-lazy-dev.com/fr/installez-fail2ban-avec-docker/