diff --git a/CLAUDE.md b/CLAUDE.md index db72c0f..d6d4577 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -132,6 +132,17 @@ user (`vault_aws_dns`). It also deletes the records listed under - `ufw-docker` makes UFW govern container ports. Only Caddy (80/443) and Gitea SSH (host port 222) are allowed in from the internet. - Admin UIs (Dockge, Syncthing, Cronmaster, Apprise, CouchDB) bind to the Tailscale IP only. +- Caddy writes a JSON access log to `/data/logs/access.log` (volume + `caddy_caddy_data`). CrowdSec (infrastructure, tag `crowdsec`) reads it. +- Custom Caddy: `caddy_custom` in `infrastructure.yml` switches between stock + `caddy:2-alpine` and the image from repo `phil/caddy-custom` (CrowdSec + bouncer, AppSec, rate limiting; rebuilt daily, rolled out by dockcheck). + `caddy_crowdsec_enforce`, `caddy_appsec` and `caddy_ratelimit` stage the + features; `caddy_custom: false` rolls all of it back in one deploy. +- Unban an IP: `docker exec crowdsec cscli decisions delete --ip ` (SSH + over Tailscale never goes through Caddy). +- Tinyauth (`auth.`) puts a Pocket ID login (group `admins`) in front of + Baikal's `/admin` and the Changedetection UI via the `(sso)` snippet. ### SSO (Pocket ID) Pocket ID (`id.thesatelliteoflove.com`) replaced Authentik on 2026-10-31. diff --git a/group_vars/all/infrastructure.yml b/group_vars/all/infrastructure.yml index 47de1fb..a01a62c 100644 --- a/group_vars/all/infrastructure.yml +++ b/group_vars/all/infrastructure.yml @@ -33,3 +33,23 @@ notifications: appriseapi_endpoint: "http://apprise:8000/notify/apprise" # Pocket ID group allowed through Tinyauth to admin pages. tinyauth_admin_group: admins + +# Custom Caddy (repo phil/caddy-custom: CrowdSec bouncer + AppSec + rate +# limiting, rebuilt daily, rolled out by dockcheck). +# caddy_custom: false = stock caddy:2-alpine and no plugin directives in the +# Caddyfile; set it back to false to roll everything back in one deploy. +# The other flags stage the rollout and only matter when caddy_custom is on. +caddy_custom: false +caddy_custom_image: "{{ subdomains.git }}/phil/caddy" +caddy_custom_tag: "2" # pin to "-" to hold a build +caddy_image: "{{ (caddy_custom_image ~ ':' ~ caddy_custom_tag) if caddy_custom else 'caddy:2-alpine' }}" +caddy_crowdsec_enforce: false # block IPs CrowdSec has banned +caddy_appsec: false # AppSec virtual patching on each request +caddy_ratelimit: false # per-IP limits on login POSTs + +# Never banned by CrowdSec, never rate limited. +crowdsec_allowlist: + - "{{ docker.subnet }}" # containers hairpinning through Caddy + - "100.64.0.0/10" # Tailscale IPv4 + - "fd7a:115c:a1e0::/48" # Tailscale IPv6 + - "{{ network.netcup_ip }}" # the server itself diff --git a/roles/docker/handlers/main.yml b/roles/docker/handlers/main.yml index 2e1360e..5f420f8 100644 --- a/roles/docker/handlers/main.yml +++ b/roles/docker/handlers/main.yml @@ -6,6 +6,13 @@ - compose.yml state: restarted +- name: restart crowdsec + community.docker.docker_compose_v2: + project_src: /opt/stacks/crowdsec + files: + - compose.yml + state: restarted + - name: restart obsidian-livesync community.docker.docker_compose_v2: project_src: /opt/stacks/obsidian-livesync diff --git a/roles/docker/tasks/infrastructure/caddy.yml b/roles/docker/tasks/infrastructure/caddy.yml index da98b93..bc8701d 100644 --- a/roles/docker/tasks/infrastructure/caddy.yml +++ b/roles/docker/tasks/infrastructure/caddy.yml @@ -14,12 +14,24 @@ group: docker notify: restart caddy +- name: template caddy .env (CrowdSec bouncer key) + ansible.builtin.template: + src: caddy-env.j2 + dest: /opt/stacks/caddy/.env + owner: root + mode: '0640' + group: docker + diff: false + when: caddy_custom + register: caddy_env + - name: template caddy compose file ansible.builtin.template: src: caddy-compose.yml.j2 dest: /opt/stacks/caddy/compose.yml owner: root mode: '0644' + register: caddy_compose - name: deploy caddy stack community.docker.docker_compose_v2: @@ -28,6 +40,16 @@ files: - compose.yml +# recreate: never (above) keeps the running container, so a new image or env +# (caddy_custom on/off, a pinned tag) only applies through this task. +- name: recreate caddy when its compose file or .env changed + community.docker.docker_compose_v2: + project_src: /opt/stacks/caddy + recreate: always + files: + - compose.yml + when: caddy_compose.changed or (caddy_env.changed | default(false)) + # Lets public traffic through to Caddy. Re-adds the rule if Caddy's IP changed. - name: allow public traffic to caddy ansible.builtin.command: /usr/local/bin/ufw-docker.sh allow caddy-caddy-1 {{ item }} diff --git a/roles/docker/tasks/infrastructure/crowdsec.yml b/roles/docker/tasks/infrastructure/crowdsec.yml new file mode 100644 index 0000000..6ae1992 --- /dev/null +++ b/roles/docker/tasks/infrastructure/crowdsec.yml @@ -0,0 +1,68 @@ +# CrowdSec next to Caddy. Unban: docker exec crowdsec cscli decisions delete --ip +- name: make crowdsec directories + ansible.builtin.file: + path: "{{ item }}" + state: directory + loop: + - /opt/stacks/crowdsec + - /opt/stacks/crowdsec/acquis.d + +- name: Template out the crowdsec compose file + ansible.builtin.template: + src: crowdsec-compose.yml.j2 + dest: /opt/stacks/crowdsec/compose.yml + owner: root + mode: '0644' + +- name: Template out the crowdsec .env file + ansible.builtin.template: + src: crowdsec-env.j2 + dest: /opt/stacks/crowdsec/.env + owner: root + mode: '0640' + group: docker + diff: false # contains the bouncer key + +- name: Template out the crowdsec acquisition files + ansible.builtin.template: + src: "crowdsec-acquis-{{ item }}.yaml.j2" + dest: "/opt/stacks/crowdsec/acquis.d/{{ item }}.yaml" + owner: root + mode: '0644' + loop: [caddy, appsec] + notify: restart crowdsec + +- name: deploy crowdsec stack + community.docker.docker_compose_v2: + project_src: /opt/stacks/crowdsec + files: + - compose.yml + +- name: wait for the crowdsec local API + ansible.builtin.command: docker exec crowdsec cscli lapi status + register: crowdsec_lapi + retries: 20 + delay: 3 + until: crowdsec_lapi.rc == 0 + changed_when: false + when: not ansible_check_mode + +# Our own networks are never banned (centralized allowlist, CrowdSec >= 1.6.8) +- name: read the crowdsec allowlist + ansible.builtin.command: docker exec crowdsec cscli allowlists inspect trusted -o json + register: crowdsec_allowlist_current + failed_when: false + changed_when: false + when: not ansible_check_mode + +- name: create the crowdsec allowlist + ansible.builtin.command: docker exec crowdsec cscli allowlists create trusted -d "own networks (ansible)" + when: not ansible_check_mode and crowdsec_allowlist_current.rc != 0 + +- name: add missing entries to the crowdsec allowlist + ansible.builtin.command: >- + docker exec crowdsec cscli allowlists add trusted {{ item }} -d ansible + loop: "{{ crowdsec_allowlist }}" + when: >- + not ansible_check_mode and + (crowdsec_allowlist_current.rc != 0 or item not in crowdsec_allowlist_current.stdout) diff --git a/roles/docker/tasks/infrastructure/main.yml b/roles/docker/tasks/infrastructure/main.yml index 8c19e35..6fe5348 100644 --- a/roles/docker/tasks/infrastructure/main.yml +++ b/roles/docker/tasks/infrastructure/main.yml @@ -9,6 +9,10 @@ import_tasks: pocketid.yml tags: pocketid +- name: Install crowdsec + import_tasks: crowdsec.yml + tags: crowdsec + - name: Install tinyauth import_tasks: tinyauth.yml tags: tinyauth diff --git a/roles/docker/templates/Caddyfile.j2 b/roles/docker/templates/Caddyfile.j2 index 5869a64..f9a5f6b 100644 --- a/roles/docker/templates/Caddyfile.j2 +++ b/roles/docker/templates/Caddyfile.j2 @@ -1,3 +1,21 @@ +{% if caddy_custom %} +# Plugins come from the custom image (repo phil/caddy-custom). +{ + 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 + # Inspect at most 1 MiB of a body (uploads, registry pushes). + appsec_max_body_bytes 1048576 + # CrowdSec being down must not take every site down. + appsec_fail_open + } +} + +{% endif %} # Tell browsers to always use HTTPS for each site for a year. No # includeSubDomains, so hosts not served here are unaffected. # defer: replace any HSTS header an app sends itself, so there's only one. @@ -56,6 +74,7 @@ import logging import hsts import security_headers + import protect header { Content-Security-Policy "base-uri 'self'; object-src 'none'; frame-ancestors 'self'; form-action 'self'" X-Frame-Options SAMEORIGIN @@ -73,28 +92,65 @@ } } +# CrowdSec, imported by every site (via site/static_site). Empty unless the +# custom image is on and the stage is enabled (caddy_crowdsec_enforce, +# caddy_appsec). AppSec skips bulk binary traffic: registry and git pushes. +(protect) { +{% if caddy_custom and caddy_crowdsec_enforce %} + crowdsec +{% endif %} +{% if caddy_custom and caddy_appsec %} + @appsec not path /v2/* /api/packages/* */git-upload-pack */git-receive-pack */info/refs + appsec @appsec +{% endif %} +} + +# Per-IP limit on login/token POSTs: import ratelimit_login +# Zone names must be unique per site. Our own networks are exempt. +(ratelimit_login) { +{% if caddy_custom and caddy_ratelimit %} + rate_limit { + zone {args[0]} { + match { + method POST + path {args[1:]} + not remote_ip {{ crowdsec_allowlist | join(' ') }} + } + key {client_ip} + events 10 + window 1m + } + log_key + } +{% endif %} +} + # Every proxied site: log, HSTS and the shared headers. (site) { import logging import hsts import security_headers + import protect } {{ subdomains.pocketid }} { import site import no_framing + import ratelimit_login pocketid_login /api/webauthn/login/* /api/one-time-access-token* /api/one-time-access-email* reverse_proxy pocket-id:1411 } {{ subdomains.tinyauth }} { import site import no_framing + import ratelimit_login tinyauth_login /api/oauth/* reverse_proxy tinyauth:3000 } paper.thesatelliteoflove.com { import site import no_framing + import ratelimit_login paperless_login /accounts/login/ /api/token/ reverse_proxy paperlessngx-webserver-1:8000 } @@ -112,24 +168,28 @@ cal.thesatelliteoflove.com { {{ subdomains.books }} { import site import no_framing + import ratelimit_login kavita_login /api/Account/login /api/Account/forgot-password /api/Plugin/authenticate reverse_proxy kavita:5000 } audio.thesatelliteoflove.com { import site import no_framing + import ratelimit_login abs_login /login /auth/refresh reverse_proxy audiobookshelf-audiobookshelf-1:80 } loclog.thesatelliteoflove.com { import site import no_framing + import ratelimit_login dawarich_login /users/sign_in reverse_proxy dawarich_app:3000 } watcher.thesatelliteoflove.com { import site import no_framing + import ratelimit_login cd_login /login # UI needs Pocket ID; the API (own key) stays reachable for scripts. @ui not path /api/* import sso @ui @@ -139,12 +199,14 @@ watcher.thesatelliteoflove.com { snippets.thesatelliteoflove.com { import site import no_framing + import ratelimit_login bytestash_login /api/auth/login /api/auth/register reverse_proxy bytestash:5000 } git.thesatelliteoflove.com { import site import no_framing + import ratelimit_login gitea_login /user/login /user/sign_up /user/forgot_password /user/two_factor* /login/oauth/access_token reverse_proxy gitea:3000 } @@ -171,18 +233,21 @@ thesatelliteoflove.com { bookmarks.thesatelliteoflove.com { import site import no_framing + import ratelimit_login karakeep_login /api/auth/callback/credentials /api/auth/signin/* reverse_proxy hoarder-web-1:3000 } social.thesatelliteoflove.com { import site import no_framing + import ratelimit_login gts_login /auth/sign_in /oauth/token /auth/2fa* reverse_proxy gotosocial:8080 } models.thesatelliteoflove.com { import site import no_framing + import ratelimit_login manyfold_login /users/sign_in reverse_proxy manyfold-app-1:3214 } @@ -190,6 +255,7 @@ models.thesatelliteoflove.com { gotify.thesatelliteoflove.com { import site import no_framing + import ratelimit_login gotify_login /client /user reverse_proxy gotify:80 } @@ -217,6 +283,7 @@ pdg.thesatelliteoflove.com { starpunk.thesatelliteoflove.com { import site import no_framing + import ratelimit_login starpunk_login /auth/login /auth/authorize /auth/token # MCP endpoint and its OAuth protected resource metadata (StarPunk v2.15.0) @mcp path /mcp /mcp/* /.well-known/oauth-protected-resource /.well-known/oauth-protected-resource/* handle @mcp { @@ -247,6 +314,7 @@ forms.nerder.land { listmonk.nerder.land { import site import no_framing + import ratelimit_login listmonk_login /admin/login /subscription/form /api/public/subscription reverse_proxy listmonk:9000 } @@ -258,6 +326,7 @@ repaircafe.nerder.land { gokapi.thesatelliteoflove.com { import site import no_framing + import ratelimit_login gokapi_login /login reverse_proxy gokapi:53842 } diff --git a/roles/docker/templates/caddy-compose.yml.j2 b/roles/docker/templates/caddy-compose.yml.j2 index f9a7673..eeca07f 100644 --- a/roles/docker/templates/caddy-compose.yml.j2 +++ b/roles/docker/templates/caddy-compose.yml.j2 @@ -1,11 +1,14 @@ services: caddy: - image: caddy:2-alpine + image: {{ caddy_image }} restart: unless-stopped ports: - "80:80" - "443:443" - "443:443/udp" +{% if caddy_custom %} + env_file: .env # CROWDSEC_API_KEY +{% endif %} volumes: - ./Caddyfile:/etc/caddy/Caddyfile - ./site:/srv diff --git a/roles/docker/templates/caddy-env.j2 b/roles/docker/templates/caddy-env.j2 new file mode 100644 index 0000000..0285c4d --- /dev/null +++ b/roles/docker/templates/caddy-env.j2 @@ -0,0 +1 @@ +CROWDSEC_API_KEY={{ vault_crowdsec.bouncer_key }} diff --git a/roles/docker/templates/crowdsec-acquis-appsec.yaml.j2 b/roles/docker/templates/crowdsec-acquis-appsec.yaml.j2 new file mode 100644 index 0000000..f6f8c1d --- /dev/null +++ b/roles/docker/templates/crowdsec-acquis-appsec.yaml.j2 @@ -0,0 +1,8 @@ +# AppSec endpoint for the Caddy bouncer; reachable only on lava. +source: appsec +name: caddy-appsec +listen_addr: 0.0.0.0:7422 +appsec_configs: + - crowdsecurity/appsec-default +labels: + type: appsec diff --git a/roles/docker/templates/crowdsec-acquis-caddy.yaml.j2 b/roles/docker/templates/crowdsec-acquis-caddy.yaml.j2 new file mode 100644 index 0000000..88c1ee4 --- /dev/null +++ b/roles/docker/templates/crowdsec-acquis-caddy.yaml.j2 @@ -0,0 +1,5 @@ +# Caddy's JSON access log (exact name, so rolled access-*.log files aren't re-read) +filenames: + - /var/log/caddy/access.log +labels: + type: caddy diff --git a/roles/docker/templates/crowdsec-compose.yml.j2 b/roles/docker/templates/crowdsec-compose.yml.j2 new file mode 100644 index 0000000..7c45dad --- /dev/null +++ b/roles/docker/templates/crowdsec-compose.yml.j2 @@ -0,0 +1,41 @@ +# CrowdSec: reads Caddy's JSON access log, decides which IPs to ban, and +# serves those decisions (LAPI :8080) and AppSec checks (:7422) to Caddy over +# lava. Nothing is published to the host. +services: + crowdsec: + image: crowdsecurity/crowdsec:{{ crowdsec_version | default('latest') }} + container_name: crowdsec + restart: unless-stopped + env_file: .env # BOUNCER_KEY_caddy + environment: + COLLECTIONS: >- + crowdsecurity/caddy crowdsecurity/http-cve crowdsecurity/base-http-scenarios + crowdsecurity/appsec-virtual-patching + TZ: Etc/UTC + volumes: + - crowdsec_config:/etc/crowdsec + - crowdsec_data:/var/lib/crowdsec/data + - ./acquis.d:/etc/crowdsec/acquis.d:ro + # Only the logs/ folder of Caddy's data volume; its TLS keys stay out. + - type: volume + source: caddy_data + target: /var/log/caddy + read_only: true + volume: + subpath: logs + security_opt: + - no-new-privileges:true + labels: + mag37.dockcheck.update: true + +volumes: + crowdsec_config: + crowdsec_data: + caddy_data: + external: true + name: caddy_caddy_data + +networks: + default: + external: true + name: {{ docker.network_name }} diff --git a/roles/docker/templates/crowdsec-env.j2 b/roles/docker/templates/crowdsec-env.j2 new file mode 100644 index 0000000..bfd7b1c --- /dev/null +++ b/roles/docker/templates/crowdsec-env.j2 @@ -0,0 +1 @@ +BOUNCER_KEY_caddy={{ vault_crowdsec.bouncer_key }}