Custom Caddy with CrowdSec bouncer, AppSec and rate limiting
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
f9c44868a4
5 files changed
+266
No files matched your search
@@ -0,0 +1,41 @@
|
||||
# Fixture using every plugin directive in the shapes the real Caddyfile uses.
|
||||
{
|
||||
auto_https off
|
||||
order crowdsec first
|
||||
order appsec after crowdsec
|
||||
crowdsec {
|
||||
api_url http://crowdsec:8080
|
||||
api_key {$CROWDSEC_API_KEY}
|
||||
ticker_interval 15s
|
||||
appsec_url http://crowdsec:7422
|
||||
appsec_max_body_bytes 1048576
|
||||
appsec_fail_open
|
||||
}
|
||||
}
|
||||
|
||||
(protect) {
|
||||
crowdsec
|
||||
appsec
|
||||
}
|
||||
|
||||
(ratelimit_login) {
|
||||
rate_limit {
|
||||
zone {args[0]} {
|
||||
match {
|
||||
method POST
|
||||
path {args[1:]}
|
||||
not remote_ip private_ranges 100.64.0.0/10
|
||||
}
|
||||
key {client_ip}
|
||||
events 10
|
||||
window 1m
|
||||
}
|
||||
log_key
|
||||
}
|
||||
}
|
||||
|
||||
:8080 {
|
||||
import protect
|
||||
import ratelimit_login test_login /login /api/token/
|
||||
respond "ok"
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
"""Render phil/ansible's Caddyfile.j2 for `caddy validate`.
|
||||
|
||||
Usage: render_caddyfile.py <ansible checkout> (prints the Caddyfile)
|
||||
Only non-secret group_vars are loaded. Values that reference other variables
|
||||
are resolved by rendering until nothing changes; values that need the vault
|
||||
stay unresolved, and StrictUndefined makes the Caddyfile fail loudly if it
|
||||
ever uses one.
|
||||
"""
|
||||
import pathlib, sys
|
||||
import jinja2, yaml
|
||||
|
||||
root = pathlib.Path(sys.argv[1])
|
||||
ctx = {}
|
||||
for f in ("domains.yml", "infrastructure.yml"):
|
||||
ctx.update(yaml.safe_load((root / "group_vars/all" / f).read_text()) or {})
|
||||
ctx["caddy_custom"] = True
|
||||
|
||||
env = jinja2.Environment(undefined=jinja2.StrictUndefined, keep_trailing_newline=True)
|
||||
|
||||
def resolve(o):
|
||||
if isinstance(o, dict):
|
||||
return {k: resolve(v) for k, v in o.items()}
|
||||
if isinstance(o, list):
|
||||
return [resolve(v) for v in o]
|
||||
if isinstance(o, str) and "{{" in o:
|
||||
try:
|
||||
return env.from_string(o).render(ctx)
|
||||
except jinja2.UndefinedError:
|
||||
return o
|
||||
return o
|
||||
|
||||
for _ in range(10):
|
||||
new = resolve(ctx)
|
||||
if new == ctx:
|
||||
break
|
||||
ctx = new
|
||||
|
||||
tmpl = (root / "roles/docker/templates/Caddyfile.j2").read_text()
|
||||
sys.stdout.write(env.from_string(tmpl).render(ctx))
|
||||
Reference in new issue
Block a user