Debian Codegraph @

doc/sources/admin/docker.rst

raw
1 LemonLDAP::NG in Docker
2 =======================
3
4 |image0|
5
6 Presentation
7 ------------
8
9 `Docker <https://www.docker.com/>`__ allows do run application into
10 containers.
11
12 You can find a Docker image for LemonLDAP::NG in this repository:
13 `<https://hub.docker.com/r/lemonldapng/lemonldap-ng>`_
14
15 See also this github project:
16 https://github.com/LemonLDAPNG/lemonldap-ng-docker
17
18 .. note::
19
20 The docker images are compatible with podman. The images can also be built with podman if needed.
21
22 Usage
23 -----
24
25 Prerequisites:
26
27 - Add the URLs below to /etc/hosts on your host server where the docker container will be deployed
28
29 ::
30
31 echo "127.0.0.1 auth.example.com manager.example.com reload.example.com test1.example.com test2.example.com" | sudo tee -a /etc/hosts
32
33 Simple Deployment
34 -----------------
35
36 - With sudo the container can be deployed directly to the host port 80 (option -p)
37
38 ::
39
40 sudo docker run -d -p 80:80 --name lemonldap-ng lemonldapng/lemonldap-ng
41
42 - With a rootless docker instance mapping the container to the host port 8080 on the host is neccessary
43
44 ::
45
46 docker run -d -p 8080:8080 -e PORT=8080 --name lemonldap-ng lemonldapng/lemonldap-ng
47
48 .. attention::
49
50 With more complex deployments such as with SELinux the internal container port can be modified with the environment variable specified above with PORTS
51
52 Then connect to http://auth.example.com with your browser and log in
53 with dwho/dwho.
54
55 Advanced configuration
56 ----------------------
57
58 For first time deployments you will need access to the lemonldap static (lemonldap-ng.ini) and dynamic configuration (lmConf-X.json) we will need to configure the volume mounts.
59
60 The following folders will container the theme components such as logos, templates and more:
61
62 - /usr/share/lemonldap-ng/portal/htdocs/static/CustomTheme
63 - /usr/share/lemonldap-ng/portal/htdocs/static/CustomTheme/css
64 - /usr/share/lemonldap-ng/portal/htdocs/static/CustomTheme/js
65 - /usr/share/lemonldap-ng/portal/htdocs/static/CustomTheme/images
66 - /usr/share/lemonldap-ng/portal/templates/CustomTheme
67
68 .. note::
69
70 Loading the custom theme elements requires the configuration of portalSkin variable. In docker this variable must also be configured with the name of the folder being used.
71 In our example case we would configure the variable with "CustomTheme" Keeping the folder name consistant between "templates" and "htdocs/static" is important.
72
73 The plugins will be stored in these folders:
74
75 - /usr/share/perl5/Lemonldap/NG/Portal/Plugins/CustomPlugin
76 - /usr/share/perl5/Lemonldap/NG/Portal/Register/CustomRegister
77 - /usr/share/perl5/Lemonldap/NG/Portal/UserDB/CustomUserdb
78 - /usr/share/perl5/Lemonldap/NG/Portal/Auth/CustomAuth
79 - /usr/share/perl5/Lemonldap/NG/Portal/Captcha/CustomCaptcha
80 - /usr/share/perl5/Lemonldap/NG/Portal/MenuTab/CustomMenuTab
81
82 .. note::
83
84 The plugins will be stored in a foler appended with "Custom" therefore all plugins much include the full path to the plugin in the header of the perl file.
85 Example: For plugins stored in /usr/share/perl5/Lemonldap/NG/Portal/Plugins/CustomPlugin/LDAPSearch.pm the header would be package Lemonldap::NG::Portal::Plugin::CustomPlugin::LDAPSeach
86
87 The following folders for the configuration and session data can also be mounted and exposed:
88
89 - /etc/lemonldap-ng
90 - /var/lib/lemonldap-ng/conf
91 - /var/lib/lemonldap-ng/sessions
92 - /var/lib/lemonldap-ng/psessions
93 - /etc/nginx/sites-enabled
94
95 Finally a cron job to purge sessions must also be configured with `crontab`:
96
97 ::
98
99 # Lemonldap::NG::Handler Session Purge
100 1 * * * * docker exec -it lemonldap bash -c "[ -x /usr/share/lemonldap-ng/bin/purgeLocalCache ] && if [ ! -d /run/systemd/system ]; then /usr/share/lemonldap-ng/bin/purgeLocalCache; fi"
101 # Lemonldap::NG::Portal Session Purge
102 7 * * * * docker exec -it lemonldap bash -c "[ -x /usr/share/lemonldap-ng/bin/purgeCentralCache ] && if [ ! -d /run/systemd/system ]; then /usr/share/lemonldap-ng/bin/purgeCentralCache; fi"
103
104 .. note::
105
106 If the container is deployed with rootless docker make sure to execute the crontab without sude.
107 Running sudo crontab will execute the cronjobs on the sudo docker instance deployment.
108
109 We can use the docker run command as follows which include all the possible customisations feasible with the image:
110
111 ::
112
113 docker run -d \
114 --name lemonldap-ng
115 -e SSODOMAIN=example.com \
116 -e PORTAL_HOSTNAME=auth.example.com \
117 -e MANAGER_HOSTNAME=manager.example.com \
118 -e HANDLER_HOSTNAME=reload.example.com \
119 -e TEST1_HOSTNAME=test1.example.com \
120 -e TEST2_HOSTNAME=test2.example.com \
121 -e PRESERVEFILES=/etc/lemonldap-ng /var/lib/lemonldap-ng/conf /var/lib/lemonldap-ng/sessions /var/lib/lemonldap-ng/psessions \
122 -e LOGLEVEL=debug \
123 -e FASTCGI_LISTEN_PORT=9000 \
124 -e PORT=8080 \
125 -e IPV4_ONLY=true \
126 -p 8080:8080 \
127 -p 9000:9000 \
128 -v ./llng/etc:/etc/lemonldap-ng \
129 -v ./llng/var-conf:/var/lib/lemonldap-ng/conf \
130 -v ./llng/var-sessions:/var/lib/lemonldap-ng/sessions \
131 -v ./llng/var-psessions:/var/lib/lemonldap-ng/psessions \
132 -v ./llng/theme:/usr/share/lemonldap-ng/portal/htdocs/static/CustomTheme \
133 -v ./llng/template:/usr/share/lemonldap-ng/portal/templates/CustomTheme \
134 -v ./llng/plugins:/usr/share/perl5/Lemonldap/NG/Portal/Plugins/CustomPlugin \
135 -v ./llng/register:/usr/share/perl5/Lemonldap/NG/Portal/Register/CustomRegister \
136 -v ./llng/userdb:/usr/share/perl5/Lemonldap/NG/Portal/UserDB/CustomUserdb \
137 -v ./llng/auth:/usr/share/perl5/Lemonldap/NG/Portal/Auth/CustomAuth \
138 -v ./llng/captcha:/usr/share/perl5/Lemonldap/NG/Portal/Captcha/CustomCaptcha \
139 -v ./llng/menutab:/usr/share/perl5/Lemonldap/NG/Portal/MenuTab/CustomMenuTab \
140 -v ./llng/nginx:/etc/nginx/sites-enabled \
141 lemonldapng/lemonldap-ng:latest
142
143 Adding the `:Z` is required for SELinux:
144
145 ::
146
147 docker run -d \
148 --name lemonldap-ng
149 -e SSODOMAIN=example.com \
150 -e PORTAL_HOSTNAME=auth.example.com \
151 -e MANAGER_HOSTNAME=manager.example.com \
152 -e HANDLER_HOSTNAME=reload.example.com \
153 -e TEST1_HOSTNAME=test1.example.com \
154 -e TEST2_HOSTNAME=test2.example.com \
155 -e PRESERVEFILES=/etc/lemonldap-ng /var/lib/lemonldap-ng/conf /var/lib/lemonldap-ng/sessions /var/lib/lemonldap-ng/psessions \
156 -e LOGLEVEL=debug \
157 -e FASTCGI_LISTEN_PORT=9000 \
158 -e PORT=8080 \
159 -e IPV4_ONLY=true \
160 -p 8080:8080 \
161 -p 9000:9000 \
162 -v ./llng/etc:/etc/lemonldap-ng:Z \
163 -v ./llng/var-conf:/var/lib/lemonldap-ng/conf:Z \
164 -v ./llng/var-sessions:/var/lib/lemonldap-ng/sessions:Z \
165 -v ./llng/var-psessions:/var/lib/lemonldap-ng/psessions:Z \
166 -v ./llng/theme:/usr/share/lemonldap-ng/portal/htdocs/static/CustomTheme:Z \
167 -v ./llng/template:/usr/share/lemonldap-ng/portal/templates/CustomTheme:Z \
168 -v ./llng/plugins:/usr/share/perl5/Lemonldap/NG/Portal/Plugins/CustomPlugin:Z \
169 -v ./llng/register:/usr/share/perl5/Lemonldap/NG/Portal/Register/CustomRegister:Z \
170 -v ./llng/userdb:/usr/share/perl5/Lemonldap/NG/Portal/UserDB/CustomUserdb:Z \
171 -v ./llng/auth:/usr/share/perl5/Lemonldap/NG/Portal/Auth/CustomAuth:Z \
172 -v ./llng/captcha:/usr/share/perl5/Lemonldap/NG/Portal/Captcha/CustomCaptcha:Z \
173 -v ./llng/menutab:/usr/share/perl5/Lemonldap/NG/Portal/MenuTab/CustomMenuTab:Z \
174 -v ./llng/nginx:/etc/nginx/sites-enabled:Z \
175 lemonldapng/lemonldap-ng:latest
176
177 The above command is a rootless docker deployment, using sudo, removing the PORT environment variable and binding to port 80 should be sufficient to directly expose the container.
178
179 In addition, the FASTCGI_LISTEN_PORT executes the fastcgi-server exposing port 9000 in the container. Removing this variable from the container deployment causes fastcgi-server to deploy with socket file inside the container. Regardless of the fastcgi-server deployment mechanism nginx is able to connect to fastcgi-server.
180
181 We provide docker-compose.yml example files in the github repository.
182
183 - `https://github.com/LemonLDAPNG/lemonldap-ng-docker/blob/master/docker-compose-selinux.yaml`
184 - `https://github.com/LemonLDAPNG/lemonldap-ng-docker/blob/master/docker-compose.yaml`
185
186 ::
187
188 docker compose -f docker-compose-selinux.yaml up -d
189 docker compose up -d
190
191 Utilities
192 ---------
193
194 The docker image provides the same utilities that is provided in a native installation of lemonldap.
195
196 ::
197
198 docker exec -it lemonldap-ng /usr/share/lemonldap-ng/bin/lemonldap-ng-cli
199 docker exec -it lemonldap-ng /usr/share/lemonldap-ng/bin/lmConfigEditor
200
201 Import and export configuration
202 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
203
204 Restoring existing configuration can be done by either placing the lmConf-X.json in the folder mounted to /var/lib/lemonldap-ng/ or executing the following commands:
205
206 ::
207
208 docker cp lmConf-X.json lemonldap-ng:/var/lib/lemonldap-ng/lmConfig.json
209 docker exec -it lemonldap-ng bash
210 /usr/share/lemonldap-ng/bin/lemonldap-ng-cli restore - < /var/lib/lemonldap-ng/lmConfig.json
211 rm /var/lib/lemonldap-ng/lmConfig.json
212
213 Exporting configuration is simple when copied directly from the mounted folder /var/lib/lemonldap-ng/ or following these commands:
214
215 ::
216
217 docker exec -it lemonldap-ng bash
218 /usr/share/lemonldap-ng/bin/lemonldap-ng-cli save > /var/lib/lemonldap-ng/lmConfig.json
219 docker cp lemonldap-ng:/var/lib/lemonldap-ng/lmConfig.json lmConfig.json
220
221 Reverse Proxy
222 -------------
223
224 Apache2 or NGinx can both be configured to bind to port 80 and proxy pass to another port binding of lemonldap.
225
226 HTTP
227 ^^^^
228 ::
229
230 <VirtualHost *:80>
231 ProxyPreserveHost On
232 ProxyRequests Off
233 CustomLog /var/log/httpd/llng.log combined
234 ServerName example.com
235 ServerAlias auth.example.com manager.example.com reload.example.com test1.example.com test2.example.com
236 ProxyPass / http://127.0.0.1:8080/
237 ProxyPassReverse / http://127.0.0.1:8080/
238 </VirtualHost>
239
240 HTTPS
241 ^^^^^
242
243 ::
244
245 <VirtualHost *:80>
246 ServerName example.com
247 ServerAlias auth.example.com manager.example.com reload.example.com test1.example.com test2.example.com
248
249 RewriteEngine On
250 RewriteCond %{HTTPS} off
251 RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
252 </VirtualHost>
253
254 <VirtualHost *:443>
255 ServerName example.com
256 CustomLog /var/log/httpd/llng.log combined
257
258 SSLEngine on
259 SSLCertificateChainFile "/path/to/example.com.cacert.cert"
260 SSLCertificateFile "/path/to/example.com.cert"
261 SSLCertificateKeyFile "/path/to/example.com.key"
262
263 ProxyPreserveHost On
264 ProxyPass / http://127.0.0.1:8080/
265 ProxyPassReverse / http://127.0.0.1:8080/
266 </VirtualHost>
267
268 Protecting applications
269 ^^^^^^^^^^^^^^^^^^^^^^^
270
271 Lemonldap::NG provides capabilities to [protect applications](https://lemonldap-ng.org/documentation/latest/configvhost.html?highlight=nginx%20reverse#reverse-proxy-1) with the handler. However the nginx configuration needs to be modified slightly to work in a containerised deployment. The creation of a [virtual host](https://lemonldap-ng.org/documentation/latest/configvhost.html?highlight=virtual%20host#lemonldap-ng-configuration) in Lemonldap::NG manager is also required for it to accepted by lemonldap for authentication and redirection of the protected application.
272
273 The following nginx configuration can be added to the `./llng/nginx` folder mounted as a volume to docker container. All configurations will be picked up by nginx after restarting the container as the folder is mounted to `/etc/nginx/sites-enabled/`.
274
275 ::
276
277 server {
278 listen 8080;
279 server_name application.example.com;
280
281 # Internal authentication request
282 location = /lmauth {
283 internal;
284 include /etc/nginx/fastcgi_params;
285 fastcgi_pass /path/to/llng-fastcgi-server.sock;
286 # Drop post data
287 fastcgi_pass_request_body off;
288 fastcgi_param CONTENT_LENGTH "";
289 # Prevent attacks on older Nginx versions (<1.29.5)
290 fastcgi_param HTTP_HOST $host;
291 # Keep original request (LLNG server will receive /lmauth)
292 fastcgi_param X_ORIGINAL_URI $original_uri;
293 }
294
295 # Client requests
296 location / {
297 auth_request /lmauth;
298 set $original_uri $uri$is_args$args;
299 auth_request_set $lmremote_user $upstream_http_lm_remote_user;
300 auth_request_set $lmlocation $upstream_http_location;
301 error_page 401 $lmlocation;
302
303 proxy_pass http://application-proxy.example.com/;
304 include /etc/nginx/proxy_params;
305
306 ##################################
307 # PASSING HEADERS TO APPLICATION #
308 ##################################
309 # IF LUA IS SUPPORTED
310 #include /path/to/nginx-lua-headers.conf;
311
312 # ELSE
313 # Set manually your headers
314 #auth_request_set $authuser $upstream_http_auth_user;
315 #proxy_set_header HTTP_AUTH_USER $authuser;
316 }
317 }
318
319 If the application being protected by Lemonldap::NG and also requires AUTH-USER or other HTTP headers sent to the application during proxy_pass directive make sure use `proxy_set_header` in the nginx configuration above.
320
321 When the docker deployment directly exploses Lemonldap::NG with nginx the following configuration is required:
322
323 ::
324
325 include /etc/nginx/proxy_params;
326
327 When a reverse proxy is used to redirect to the containerised instance of Lemonldap:NG additional configuration is required.
328
329 The following Nginx configurations needs to added and modified:
330
331 ::
332
333 #include /etc/nginx/proxy_params
334 proxy_set_header X-Real-IP $remote_addr;
335 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
336 proxy_set_header X-Forwarded-Proto $scheme;
337 proxy_pass_request_headers on;
338
339 Meanwhile the following Apache2 (httpd) configurations need to be created:
340
341 ::
342
343 <VirtualHost *:80>
344 ServerName application.example.com
345
346 RewriteEngine On
347 RewriteCond %{HTTPS} off
348 RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
349 </VirtualHost>
350
351 <VirtualHost *:443>
352 ServerName application.example.com
353 CustomLog /var/log/httpd/application.log combined
354
355 SSLEngine on
356 SSLCertificateChainFile "/path/to/application.example.com.cacert.cert"
357 SSLCertificateFile "/path/to/application.example.com.cert"
358 SSLCertificateKeyFile "/path/to/application.example.com.key"
359
360 ProxyPreserveHost On
361 ProxyPass / http://localhost:8080/
362 ProxyPassReverse / http://application.example.com:8080/
363 </VirtualHost>
364
365 .. note::
366
367 Use of directory volumes mounts or docker volumes ensure safe storage of persistent configurations and important files.
368
369 .. |image0| image:: /documentation/lemonldap-ng-docker.png
370 :class: align-center
371