Debian Codegraph @

doc/sources/admin/configcaddy.rst

raw
1 Deploy Caddy configuration
2 ==========================
3
4 Caddy is a modern web server written in Go with automatic HTTPS capabilities.
5 It supports integration with authentication services via its ``forward_auth``
6 directive, which works similarly to Traefik's ``forwardAuth``.
7
8 .. versionadded:: 2.19.0
9
10 Since Caddy's ``forward_auth`` behavior is identical to Traefik's ``forwardAuth``,
11 LemonLDAP::NG uses the same handler for both servers: ``SOURCE_SERVER=traefik``.
12
13 PSGI server
14 -----------
15
16 Caddy does not support FastCGI for authentication (it doesn't implement
17 ``FCGI_ROLE=AUTHORIZER``), so it must work with a PSGI server exposing an
18 HTTP socket. See :doc:`Advanced PSGI usage<psgi>`.
19
20 For example, to use the handler with uWSGI, exposing an HTTP socket
21 binding on 127.0.0.1:8183:
22
23 ::
24
25 cd /usr/share/lemonldap-ng/llng-server && SOURCE_SERVER=traefik /sbin/uwsgi \
26 --plugin psgi \
27 --psgi llng-server.psgi \
28 --master \
29 --workers 2 \
30 --max-worker-lifetime 86400 \
31 --max-requests 10000 \
32 --disable-logging \
33 --harakiri 30 \
34 --buffer-size 65535 \
35 --limit-post 0 \
36 --die-on-term \
37 --http-socket 127.0.0.1:8183
38
39 .. note::
40 Use ``SOURCE_SERVER=traefik`` - this handler works for both Traefik and Caddy
41 since they have identical ``forward_auth`` / ``forwardAuth`` behavior.
42
43 Handler behavior
44 ----------------
45
46 The Traefik handler (used for both Traefik and Caddy):
47
48 - Reads ``X-Forwarded-*`` headers sent by Caddy
49 - Returns 302/303 redirects directly (unlike Nginx which requires
50 conversion to 401)
51 - Passes headers through directly using Caddy's ``copy_headers`` directive
52
53 Caddy configuration
54 -------------------
55
56 Basic Caddyfile
57 ~~~~~~~~~~~~~~~
58
59 Here is a basic configuration example:
60
61 ::
62
63 # Portal LemonLDAP::NG
64 auth.example.com {
65 reverse_proxy llng-portal:9090
66 }
67
68 # Manager LemonLDAP::NG
69 manager.example.com {
70 reverse_proxy llng-manager:9090
71 }
72
73 # Protected application
74 app.example.com {
75 # Security: remove headers that could be forged by malicious users
76 request_header -Auth-User
77 request_header -Auth-Mail
78 request_header -Auth-Groups
79 request_header -Lm-Remote-User
80
81 forward_auth llng-handler:8184 {
82 uri /
83 copy_headers {
84 Auth-User
85 Auth-Mail
86 Auth-Groups
87 Lm-Remote-User
88 }
89 }
90
91 reverse_proxy backend:8080
92 }
93
94 The ``forward_auth`` directive:
95
96 - Sends the original request information via ``X-Forwarded-*`` headers
97 - On 2xx response: copies specified headers to the original request
98 - On non-2xx response: returns the response to the client (including redirects
99 to the login portal)
100
101 Headers configuration
102 ~~~~~~~~~~~~~~~~~~~~~
103
104 Use ``copy_headers`` to specify which headers should be copied from the
105 authentication response to the original request:
106
107 ::
108
109 forward_auth llng-handler:8184 {
110 uri /
111 copy_headers {
112 Auth-User
113 Auth-Mail
114 Auth-Groups
115 Lm-Remote-User
116 Lm-Remote-Custom
117 }
118 }
119
120 .. warning::
121
122 It is **highly recommended** to prefix your exported headers with ``Auth-``
123 to make it easier to prevent header injection attacks. Always remove these
124 headers before the ``forward_auth`` directive using ``request_header -HeaderName``.
125
126 Passing headers to backend
127 ~~~~~~~~~~~~~~~~~~~~~~~~~~
128
129 After authentication, you can pass headers to the backend using
130 ``header_up`` in the ``reverse_proxy`` directive:
131
132 ::
133
134 reverse_proxy backend:8080 {
135 header_up X-Remote-User {http.request.header.Auth-User}
136 header_up X-Remote-Mail {http.request.header.Auth-Mail}
137 }
138
139 Docker/Kubernetes example
140 ~~~~~~~~~~~~~~~~~~~~~~~~~
141
142 In a containerized environment, your Caddyfile might look like:
143
144 ::
145
146 :80 {
147 handle /health {
148 respond "OK" 200
149 }
150
151 handle {
152 request_header -Auth-User
153 request_header -Auth-Mail
154
155 forward_auth llng-handler:8184 {
156 uri /
157 copy_headers Auth-User Auth-Mail Lm-Remote-User
158 }
159
160 reverse_proxy {$BACKEND_URL}
161 }
162 }
163
164 Security considerations
165 -----------------------
166
167 Header injection prevention
168 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
169
170 Always remove headers that could be forged by malicious users before
171 authentication:
172
173 ::
174
175 # Remove all headers that LLNG might set
176 request_header -Auth-*
177 request_header -Lm-Remote-User
178 request_header -Lm-Remote-Custom
179
180 forward_auth llng-handler:8184 {
181 # ...
182 }
183
184 See also
185 --------
186
187 - :doc:`Deploy Traefik configuration<configtraefik>`
188 - :doc:`Deploy Nginx configuration<confignginx>`
189 - :doc:`Advanced PSGI usage<psgi>`
190 - :doc:`Handler architecture<handlerarch>`