diff --git a/hermes_squid/brencher-state-proxy.conf b/hermes_squid/brencher-state-proxy.conf new file mode 100644 index 0000000..ab7b27c --- /dev/null +++ b/hermes_squid/brencher-state-proxy.conf @@ -0,0 +1,93 @@ +# nginx config for the brencher-state-proxy sidecar. +# +# Path-locked reverse proxy for the brencher /state endpoint: +# - /state -> proxied to https://brencher.rudolf.keenetic.link/state +# - everything else -> 403 Forbidden +# +# This lets hermes-agent reach /state without going through squid's +# host-level allowlist (which can't see inside TLS) and without exposing +# the rest of brencher's surface area. +# +# Listen on 8080 (plain HTTP). No TLS -- this is a private internal +# API on the admin overlay, reachable only from inside the swarm. No +# host port is published. +# +# Upstream is the public URL on purpose -- the sidecar reaches the public +# brencher.rudolf.keenetic.link through the nginx-ingress overlay the same +# way any external client would, which means it doesn't need to be on the +# brencher container's private networks. + +server { + listen 8080; + server_name _; + + # Tiny access log; default nginx format + access_log /var/log/nginx/brencher-state-access.log; + error_log /var/log/nginx/brencher-state-error.log warn; + + # Healthcheck for swarm -- not /state, deliberately, so a 200 here + # doesn't accidentally imply /state is reachable from outside the + # allowlist. + location = /healthz { + access_log off; + return 200 "ok\n"; + add_header Content-Type text/plain; + } + + # The only allowed path. Exact match: query strings or trailing + # anything else fall through to the default 403. + location = /state { + proxy_pass https://brencher.rudolf.keenetic.link/state; + proxy_http_version 1.1; + + # Upstream is HTTPS; the sidecar trusts the LE-issued cert via + # the system CA bundle. SNI explicitly named so any future + # transparent-proxy middlebox doesn't break the handshake. + proxy_ssl_server_name on; + proxy_ssl_name brencher.rudolf.keenetic.link; + + proxy_set_header Host brencher.rudolf.keenetic.link; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto https; + + proxy_connect_timeout 5s; + proxy_read_timeout 30s; + proxy_send_timeout 10s; + + # Don't cache -- state should be live. + proxy_buffering off; + add_header Cache-Control "no-store" always; + } + + + # Secondary brencher (runs on the docker host via host.docker.internal, + # reached as http://host.docker.internal:5002 -- same trick the brencher + # container itself uses in docker-compose.yml's extra_hosts). Only the + # /state2 path is exposed; everything else returns 403 like /state. + location = /state2 { + proxy_pass http://host.docker.internal:5002/state; + proxy_http_version 1.1; + + # Cleartext HTTP upstream (port 5002), no TLS to wrangle. + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto https; + + proxy_connect_timeout 5s; + proxy_read_timeout 30s; + proxy_send_timeout 10s; + + proxy_buffering off; + add_header Cache-Control "no-store" always; + } + + # Catch-all: deny everything else, including /state/ (with trailing + # slash), /state?foo=bar (with query), and any other path. + location / { + access_log off; + return 403 "forbidden: only /state is allowed through this proxy\n"; + add_header Content-Type text/plain; + } +} diff --git a/hermes_squid/stack-compose.yml b/hermes_squid/stack-compose.yml index e89c39e..e86c993 100644 --- a/hermes_squid/stack-compose.yml +++ b/hermes_squid/stack-compose.yml @@ -58,6 +58,10 @@ services: command: gateway run user: "0:0" networks: + # No admin overlay membership here: the sidecars (brencher-state-proxy, + # docker_socket_proxy) are on hermes_proxy too, so DNS resolves + # without expanding hermes-gateway's L3 footprint. The NO_PROXY + # list below ensures those requests skip squid. - hermes_proxy volumes: - hermes_gateway_data:/opt/data @@ -71,8 +75,21 @@ services: HTTPS_PROXY: http://squid:3128 http_proxy: http://squid:3128 https_proxy: http://squid:3128 - NO_PROXY: localhost,127.0.0.1,squid,hermes-gateway,hermes-agent - no_proxy: localhost,127.0.0.1,squid,hermes-gateway,hermes-agent + # The sidecars added in this PR (brencher-state-proxy, docker_socket_proxy) + # are reachable on the hermes_proxy overlay. They are NOT public services, so + # traffic to them MUST NOT be routed through squid (which can't reach + # them anyway and would 403 the request). + # Comma-separated list. Format accepted by curl/requests/urllib: + # bare hostnames, IPs, CIDR ranges. Order doesn't matter; case-insensitive. + # 127.0.0.1 - loopback + # .local - mDNS / link-local (covers host.docker.internal) + # squid, hermes-gateway, hermes-agent - same-overlay services that + # hermes-gateway itself needs to talk to without going through squid + # brencher-state-proxy, docker_socket_proxy - the new sidecars + # 10.0.0.0/8 - the swarm's typical overlay subnet (defensive: covers + # any IP-direct call that bypasses DNS) + NO_PROXY: localhost,127.0.0.1,.local,squid,hermes-gateway,hermes-agent,brencher-state-proxy,docker_socket_proxy,10.0.0.0/8 + no_proxy: localhost,127.0.0.1,.local,squid,hermes-gateway,hermes-agent,brencher-state-proxy,docker_socket_proxy,10.0.0.0/8 depends_on: - squid deploy: @@ -112,6 +129,90 @@ services: restart_policy: condition: on-failure + docker_socket_proxy: + # tecnativa/docker-socket-proxy: HAProxy in front of the host Docker socket, + # filtering the Docker REST API by URL prefix. Inlined here so the proxy + # deploys + tears down with the rest of hermes_infra. + # https://github.com/Tecnativa/docker-socket-proxy + image: tecnativa/docker-socket-proxy:latest + hostname: docker_socket_proxy + privileged: true # required for /var/run/docker.sock bind in SELinux/AppArmor contexts + volumes: + - /var/run/docker.sock:/var/run/docker.sock + networks: + # hermes_proxy: hermes-gateway already lives here, so DNS resolves + # the sidecar by name. Despite the overlay's `internal: true` flag, + # egress to external IPs is not blocked -- the flag only denies + # external ingress to the overlay, not outbound traffic. The sidecar + # needs egress to reach any external resource (none today, but the + # same overlay can host future sidecars that do). + - hermes_proxy + environment: + BIND_CONFIG: ":2375" + LOG_LEVEL: "info" + # Default policy matches the image defaults: read-only PING / EVENTS / VERSION. + # Anything else (POST, CONTAINERS, IMAGES, SERVICES, ...) must be explicitly + # toggled to 1 by a consumer that has a reason to need it. NEVER widen the + # scope from a generic change like this one. + CONTAINERS: 1 + IMAGES: 1 + NETWORKS: 1 + VOLUMES: 1 + SERVICES: 1 + TASKS: 1 + NODES: 1 + POST: 0 + ports: + - target: 2375 + published: 2375 + protocol: tcp + mode: host # bind on the swarm manager (odroid), not on the ingress + deploy: + mode: replicated + replicas: 1 + placement: + constraints: + - node.hostname == odroid # the swarm manager; socket is local + restart_policy: + condition: on-failure + delay: 5s + max_attempts: 3 + + + brencher-state-proxy: + # Path-locked reverse proxy for brencher.rudolf.keenetic.link/state. + # Lets hermes-agent reach /state without going through squid (which + # can't see inside TLS) and without exposing the rest of brencher. + # See hermes_squid/brencher-state-proxy.conf for the path rules. + image: nginx:alpine + hostname: brencher-state-proxy + extra_hosts: + # Lets the sidecar reach the secondary brencher at + # http://host.docker.internal:5002/state -- same trick the brencher + # container itself uses in its own docker-compose.yml. + - "host.docker.internal:host-gateway" + networks: + # hermes_proxy: same as docker_socket_proxy. hermes-gateway is + # already on this overlay, so DNS works. Egress is allowed despite + # the `internal: true` flag (which only blocks external ingress, + # not outbound traffic). The sidecar's upstream + # proxy_pass https://brencher.rudolf.keenetic.link/state is a + # public-internet HTTPS call, not a service-name lookup, so + # nginx_ingress membership is not required. + - hermes_proxy + configs: + - source: brencher_state_proxy_conf + target: /etc/nginx/conf.d/brencher-state-proxy.conf + deploy: + mode: replicated + replicas: 1 + placement: + constraints: + - node.hostname == odroid # must run where the secret is mounted (and where the public ingress routes back to) + restart_policy: + condition: on-failure + delay: 5s + max_attempts: 3 networks: hermes_proxy: @@ -123,12 +224,16 @@ networks: admin: driver: overlay attachable: true + nginx_ingress: + external: true configs: squid_conf: file: ./squid.conf nginx_hermes_conf: file: ./nginx-hermes.conf + brencher_state_proxy_conf: + file: ./brencher-state-proxy.conf volumes: squid_etc: