Repository navigation
Expand file tree
/
Copy pathcell-api.cfg
More file actions
77 lines (67 loc) · 3.58 KB
/
Copy pathcell-api.cfg
File metadata and controls
77 lines (67 loc) · 3.58 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
# HAProxy 3.2+. Run one private API pool per cell behind trusted HTTPS ingress.
global
log stdout format raw local0
maxconn "${TOKENCRAFT_LB_MAX_CONNECTIONS}"
defaults
mode http
log global
log-format "status=%ST backend=%b replica=%s queue_ms=%Tw connect_ms=%Tc headers_ms=%Tr total_ms=%Ta termination=%tsc"
timeout http-request 10s
timeout http-keep-alive 15s
timeout connect 2s
timeout queue 4s
timeout check 2s
timeout client 120s
timeout server 120s
retries 0
resolvers cell_dns
parse-resolv-conf
timeout resolve 1s
timeout retry 1s
hold valid 5s
hold obsolete 5s
frontend cell_ingress
bind "${TOKENCRAFT_LB_BIND}"
# The TLS ingress overwrites client-supplied forwarding headers.
acl trusted_ingress src -f "${TOKENCRAFT_LB_TRUSTED_INGRESS_FILE}"
http-request return status 403 unless trusted_ingress
acl balancer_probe path /_tokencraft/ingress-ready
acl probe_method method GET HEAD
http-request return status 405 hdr Allow "GET, HEAD" if balancer_probe !probe_method
http-request return status 503 if balancer_probe { nbsrv(cell_api) lt 1 }
http-request return status 200 if balancer_probe
# Enable only on a listener reached exclusively by trusted cloudflared connectors.
acl cloudflare_tunnel env(TOKENCRAFT_LB_CLOUDFLARE_TUNNEL) -m str true
acl single_edge_ip req.hdr_cnt(CF-Connecting-IP) eq 1
acl parsed_edge_ip req.hdr_ip(CF-Connecting-IP) -m found
http-request return status 400 if cloudflare_tunnel !single_edge_ip
http-request return status 400 if cloudflare_tunnel !parsed_edge_ip
http-request set-header X-Forwarded-For %[req.hdr_ip(CF-Connecting-IP)] if cloudflare_tunnel
http-request del-header CF-Connecting-IP
acl single_client_ip req.hdr_cnt(X-Forwarded-For) eq 1
acl parsed_client_ip req.hdr_ip(X-Forwarded-For) -m found
http-request return status 400 unless single_client_ip parsed_client_ip
http-request set-header X-Forwarded-For %[req.hdr_ip(X-Forwarded-For)]
http-request set-header X-Forwarded-Proto https
http-request del-header Forwarded
# Optional per-proxy burst protection keyed by the verified client address.
stick-table type string len 45 size 100k expire 30s store http_req_rate(10s)
acl ip_rate_enabled env(TOKENCRAFT_LB_ENABLE_IP_RATE_LIMIT) -m str true
http-request track-sc0 req.hdr_ip(X-Forwarded-For) if ip_rate_enabled
http-request return status 429 hdr Retry-After 10 content-type application/json string '{"error":{"code":"ingress_rate_limit","message":"client address request rate exceeded"}}' if ip_rate_enabled { sc_http_req_rate(0) gt 1000 }
http-request deny if { path -i /internal/capacity }
default_backend cell_api
# The proxy can generate its own 503 while every API replica is withdrawn.
acl unavailable status 503
acl no_ready_replicas nbsrv(cell_api) lt 1
acl has_retry_hint res.hdr(Retry-After) -m found
http-after-response set-header Retry-After 5 if unavailable no_ready_replicas !has_retry_hint
http-after-response set-header Cache-Control no-store if unavailable no_ready_replicas
backend cell_api
# Concurrent streams can stay open far longer than ordinary API reads.
balance leastconn
option httpchk
http-check send meth GET uri /ready ver HTTP/1.1 hdr Host tokencraft-api
http-check expect status 200
default-server init-state fully-down inter 2s downinter 2s fall 3 rise 2 maxconn "${TOKENCRAFT_LB_CONNECTIONS_PER_API}"
server-template api "${TOKENCRAFT_LB_API_SLOTS}" "${TOKENCRAFT_LB_API_ADDRESS}" check resolvers cell_dns init-addr none