Repository navigation
Expand file tree
/
Copy pathhaproxy.cfg
More file actions
167 lines (156 loc) · 9.72 KB
/
Copy pathhaproxy.cfg
File metadata and controls
167 lines (156 loc) · 9.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# docker-socket-proxy's HAProxy config, for every proxy in front of an Alloy:
# the six compose stacks, which mount this directory, and the agent hosts,
# where scripts/deploy-agent.sh copies it into a volume.
#
# This is tecnativa/docker-socket-proxy v0.5.0's haproxy.cfg.template, byte for
# byte, with three differences:
#
# 1. The bind line is resolved. The image's entrypoint seds ${BIND_CONFIG}
# into ITS template only, and this file is passed to haproxy as-is, so it
# carries the value the entrypoint computes when DISABLE_IPV6 is unset.
#
# 2. CONTAINERS=1 allows three paths, not the whole /containers prefix. That
# is the first block marked "homelab" below, and the reason this file
# exists.
#
# 3. A log follow is held open however long its container is quiet. That is
# one line in the first block and the whole of the second (#1552).
#
# Why 2. Upstream's rule is a prefix match, so CONTAINERS=1 also allowed
# GET /containers/{id}/archive, /export, /top and /changes. POST=0 does not stop
# them, because they are GETs (Tecnativa/docker-socket-proxy#182; the fix,
# Tecnativa/docker-socket-proxy#183, is unmerged). Each one is served by the
# Docker daemon as full root. The result is that a client allowed only to list containers could
# read any file visible inside any container on the host. That includes
# Alloy's own /rootfs, the host's whole filesystem, which #188's dropped
# capabilities otherwise keep 0600 files out of. Measured on prometheus on
# 2026-10-10: all four answered 200 through the proxy.
#
# The three paths are what every client of these proxies sends, read from
# HAProxy's own request log on prometheus over three days: discovery.docker
# and cAdvisor list containers and inspect them, and loki.source.docker reads
# their logs. Anything else under /containers is now refused by the final
# `http-request deny`, the same 403 any other path gets.
#
# The path is url-decoded before matching, and the id class has no `/`, `%` or
# `?`. So an encoded slash, a double encoding or a `..` segment cannot dress up
# another endpoint as one of these three. The `$` anchors are what stop
# /containers/{id}/json/../archive. HAProxy's `path` does not include the query
# string, so ?stdout=1&follow=1 on logs still matches.
#
# Why 3. loki.source.docker follows /containers/{id}/logs?follow=1, and a
# quiet container sends nothing down that stream. The defaults' `timeout client
# 10m` and `timeout server 10m` therefore closed it after ten idle minutes.
# Alloy reconnected from its saved position, Docker returned the last line
# again, and Alloy shipped it again: a duplicate every ten minutes, until the
# line was seven days old and Loki rejected each copy as too old. That was
# 1363 dropped entries on fenrir by 2026-11-17, all one Zeek line of
# 2026-11-09. Busy containers never reach the timeout. Two lines lift it for
# that path only, and every other request keeps upstream's ten minutes:
#
# - `http-request set-timeout client 24d`, the client side. HAProxy's manual
# says the value "must be non-null", so it is not 0. 24 days is just under
# the 2^31 ms a HAProxy timer holds. It sits before the allow rule, because
# an allow ends rule evaluation.
# - `use_backend docker-events`, the server side. `set-timeout server` works
# only in a section with backend capabilities, and this frontend has none.
# Upstream's docker-events backend differs from dockerbackend only by
# `timeout server 0`, which is what a follow needs. That line is a second
# block at the end, because HAProxy warns when an http-request rule follows
# a use_backend, and the first block stands where upstream's rule stood.
#
# scripts/check_socket_proxy.sh boots the pinned image on this file and checks
# both halves: the three paths answer, and archive, export, top, changes,
# stats and the traversal and encoding variants get the proxy's own 403. It
# also proves that a log follow outlives the idle timeouts, on a copy with them
# cut to seconds, and that the same follow is closed without the two lines
# above. And it rebuilds upstream's template from this file and requires it to
# equal the one in the pinned image. A Dependabot bump that changes upstream's
# rules therefore fails CI, instead of being silently overridden by this copy.
#
# HAProxy reads this only at start. scripts/reload-config.sh restarts the
# container on every `make up`, before Alloy, so a config-only change still
# lands without a compose edit. A restart and not SIGUSR2, because a rejected
# reload keeps the old workers serving and says so only in the log; that
# script's restart_socket_proxy has the reasoning.
global
log stdout format raw daemon "${LOG_LEVEL}"
pidfile /run/haproxy.pid
maxconn 4000
# Turn on stats unix socket
server-state-file /var/lib/haproxy/server-state
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 10m
timeout server 10m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
# Allow seamless reloads
load-server-state-from-file global
# Use provided example error pages
errorfile 400 /usr/local/etc/haproxy/errors/400.http
errorfile 403 /usr/local/etc/haproxy/errors/403.http
errorfile 408 /usr/local/etc/haproxy/errors/408.http
errorfile 500 /usr/local/etc/haproxy/errors/500.http
errorfile 502 /usr/local/etc/haproxy/errors/502.http
errorfile 503 /usr/local/etc/haproxy/errors/503.http
errorfile 504 /usr/local/etc/haproxy/errors/504.http
backend dockerbackend
server dockersocket $SOCKET_PATH
backend docker-events
server dockersocket $SOCKET_PATH
timeout server 0
frontend dockerfrontend
bind [::]:2375 v4v6
http-request deny unless METH_GET || { env(POST) -m bool }
# Allowed endpoints
http-request allow if { path,url_dec -m reg -i ^(/v[\d\.]+)?/containers/[a-zA-Z0-9_.-]+/((stop)|(restart)|(kill)) } { env(ALLOW_RESTARTS) -m bool }
http-request allow if { path,url_dec -m reg -i ^(/v[\d\.]+)?/containers/[a-zA-Z0-9_.-]+/start } { env(ALLOW_START) -m bool }
http-request allow if { path,url_dec -m reg -i ^(/v[\d\.]+)?/containers/[a-zA-Z0-9_.-]+/stop } { env(ALLOW_STOP) -m bool }
http-request allow if { path,url_dec -m reg -i ^(/v[\d\.]+)?/containers/[a-zA-Z0-9_.-]+/pause } { env(ALLOW_PAUSE) -m bool }
http-request allow if { path,url_dec -m reg -i ^(/v[\d\.]+)?/containers/[a-zA-Z0-9_.-]+/unpause } { env(ALLOW_UNPAUSE) -m bool }
http-request allow if { path,url_dec -m reg -i ^(/v[\d\.]+)?/auth } { env(AUTH) -m bool }
http-request allow if { path,url_dec -m reg -i ^(/v[\d\.]+)?/build } { env(BUILD) -m bool }
http-request allow if { path,url_dec -m reg -i ^(/v[\d\.]+)?/commit } { env(COMMIT) -m bool }
http-request allow if { path,url_dec -m reg -i ^(/v[\d\.]+)?/configs } { env(CONFIGS) -m bool }
# BEGIN homelab: CONTAINERS=1 allows these three paths, not the /containers prefix (Tecnativa/docker-socket-proxy#182)
http-request allow if { path,url_dec -m reg -i ^(/v[\d\.]+)?/containers/json$ } { env(CONTAINERS) -m bool }
http-request allow if { path,url_dec -m reg -i ^(/v[\d\.]+)?/containers/[a-zA-Z0-9_.-]+/json$ } { env(CONTAINERS) -m bool }
# A log follow is idle while its container is quiet: no client timeout on it (#1552, the header's 3.)
http-request set-timeout client 24d if { path,url_dec -m reg -i ^(/v[\d\.]+)?/containers/[a-zA-Z0-9_.-]+/logs$ } { env(CONTAINERS) -m bool }
http-request allow if { path,url_dec -m reg -i ^(/v[\d\.]+)?/containers/[a-zA-Z0-9_.-]+/logs$ } { env(CONTAINERS) -m bool }
# END homelab
http-request allow if { path,url_dec -m reg -i ^(/v[\d\.]+)?/distribution } { env(DISTRIBUTION) -m bool }
http-request allow if { path,url_dec -m reg -i ^(/v[\d\.]+)?/events } { env(EVENTS) -m bool }
http-request allow if { path,url_dec -m reg -i ^(/v[\d\.]+)?/exec } { env(EXEC) -m bool }
http-request allow if { path,url_dec -m reg -i ^(/v[\d\.]+)?/grpc } { env(GRPC) -m bool }
http-request allow if { path,url_dec -m reg -i ^(/v[\d\.]+)?/images } { env(IMAGES) -m bool }
http-request allow if { path,url_dec -m reg -i ^(/v[\d\.]+)?/info } { env(INFO) -m bool }
http-request allow if { path,url_dec -m reg -i ^(/v[\d\.]+)?/networks } { env(NETWORKS) -m bool }
http-request allow if { path,url_dec -m reg -i ^(/v[\d\.]+)?/nodes } { env(NODES) -m bool }
http-request allow if { path,url_dec -m reg -i ^(/v[\d\.]+)?/_ping } { env(PING) -m bool }
http-request allow if { path,url_dec -m reg -i ^(/v[\d\.]+)?/plugins } { env(PLUGINS) -m bool }
http-request allow if { path,url_dec -m reg -i ^(/v[\d\.]+)?/secrets } { env(SECRETS) -m bool }
http-request allow if { path,url_dec -m reg -i ^(/v[\d\.]+)?/services } { env(SERVICES) -m bool }
http-request allow if { path,url_dec -m reg -i ^(/v[\d\.]+)?/session } { env(SESSION) -m bool }
http-request allow if { path,url_dec -m reg -i ^(/v[\d\.]+)?/swarm } { env(SWARM) -m bool }
http-request allow if { path,url_dec -m reg -i ^(/v[\d\.]+)?/system } { env(SYSTEM) -m bool }
http-request allow if { path,url_dec -m reg -i ^(/v[\d\.]+)?/tasks } { env(TASKS) -m bool }
http-request allow if { path,url_dec -m reg -i ^(/v[\d\.]+)?/version } { env(VERSION) -m bool }
http-request allow if { path,url_dec -m reg -i ^(/v[\d\.]+)?/volumes } { env(VOLUMES) -m bool }
http-request deny
default_backend dockerbackend
use_backend docker-events if { path,url_dec -m reg -i ^(/v[\d\.]+)?/events }
# BEGIN homelab: a log follow goes to the backend with no server timeout (#1552, the header's 3.)
use_backend docker-events if { path,url_dec -m reg -i ^(/v[\d\.]+)?/containers/[a-zA-Z0-9_.-]+/logs$ }
# END homelab