blob: a9eb4129405083a4f99942d4138bde47cefa3ea9 [file] [log] [blame]
Luigi Santivettied568d42020-05-18 00:53:17 +01001#!/bin/bash
2#
3# apache templates
4#
5# Copyright 2019 Luigi Santivetti <luigi.santivetti@gmail.com>
6
7# Permission is hereby granted, free of charge, to any person obtaining a
8# copy of this software and associated documentation files (the "Software"),
9# to deal in the Software without restriction, including without limitation
10# the rights to use, copy, modify, merge, publish, distribute, sublicense,
11# and/or sell copies of the Software, and to permit persons to whom the
12# Software is furnished to do so, subject to the following conditions:
13
14# The above copyright notice and this permission notice (including the next
15# paragraph) shall be included in all copies or substantial portions of the
16# Software.
17
18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21# ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
22# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25declare -r vhost_http_t="\
26<VirtualHost *:${_APACHE_HTTP_PORT_}>
27
28 ServerName ${_APACHE_SERVER_NAME_}
29 ServerAlias ${_APACHE_SERVER_ALIAS_}
30
31 ErrorLog \"${_APACHE_VHOST_HTTP_LOG_F_}\"
32 RewriteEngine on
33
34 # NOTE: any POST or PUT send with http (:80) will be redirected
35 # with the side effect of dropping any data sent. We don't care
36 # nothing should transit over http anyway.
37
38 # Enforce https and www.
39 RewriteCond %{REQUEST_SCHEME} =http [NC]
40 RewriteCond %{SERVER_NAME} =${_APACHE_SERVER_NAME_} [NC]
41 RewriteRule ^ https://${_APACHE_SERVER_ALIAS_}%{REQUEST_URI} [NE,R=permanent,L]
42
43 # Enforce https
44 RewriteCond %{REQUEST_SCHEME} =http [NC]
45 RewriteCond %{SERVER_NAME} =${_APACHE_SERVER_ALIAS_} [NC]
46 RewriteRule ^ https://${_APACHE_SERVER_ALIAS_}%{REQUEST_URI} [NE,R=permanent,L]
47
48</VirtualHost>"
49
50declare -r vhost_https_t="\
51<IfModule mod_ssl.c>
52 <VirtualHost *:${_APACHE_HTTPS_PORT_}>
53
54 ServerName ${_APACHE_SERVER_NAME_}
55 ServerAlias ${_APACHE_SERVER_ALIAS_}
56
57 # DocumentRoot is the only publicly accessible data:
58 #
59 # - index.php, for dispatching requested URIs
60 # - theme/css, style
61 # - theme/js, style
62
63 DocumentRoot \"${_APACHE_DOCUMENT_ROOT_D_}\"
64 ErrorLog \"${_APACHE_SSL_LOG_F_}\"
65 LogLevel ${_APACHE_SSL_LOG_LEVEL_}
66
67 # Flags:
68 #
69 # NE, not escape, as keep chars such as & and ?
70 #
71 # R=status, redirect, if a valid URI is generated in the rewrite then
72 # issue a request to the browser. It always prepends the rewrite with
73 # [this protocol]://[thishost][:thisport]
74 #
75 # L, last, do not feed the rewrite result to the next rule
76 #
77 # NC, non case sensitive
78 #
79 # NOTE: This is to enforce 'www.' and *DO NOT* rediret! Skip POST and PUT
80 # because apache does an internal redirect 301 or 302 and drops the data
81 # attached to the request.
82
83 RewriteEngine on
84 RewriteCond %{REQUEST_METHOD} !^(POST|PUT) [NC]
85 RewriteCond %{SERVER_NAME} =${_APACHE_SERVER_NAME_} [NC]
86 RewriteRule ^ https://${_APACHE_SERVER_ALIAS_}%{REQUEST_URI} [NE]
87
88 # Proxy configuration
89 AllowEncodedSlashes on
90
91 # Route to vhost-gerrit.conf
92 #
93 # NOTE: 127.0.0.1 relative to container network
94
95 ProxyPassMatch /gerrit(/?)(.*) http://127.0.0.1:${_APACHE_GERRIT_PROXY_PORT_}/\$2 nocanon
96 ProxyPassReverse /gerrit(/?)(.*) http://127.0.0.1:${_APACHE_GERRIT_PROXY_PORT_}/\$2
97
98 # Within this context, it uses a file system path instead of URL path
99 #
100 # i.e.
101 # out %{REQUEST_URI} expands to https://servername.domain/whatever/
102 # in %{REQUEST_URI} expands to var/www/html/whatever/
103 #
104 # NOTE: %{REQUEST_URI} in directory context expands with a leading slash
105 # and a trailing slash
106
107 <Directory \"${_APACHE_DOCUMENT_ROOT_D_}\">
108
109 DirectorySlash Off
110 Require all granted
111
112 # This assumes to be calling a php handler, so:
113 #
114 # NOTE: Do not redirect or it will break the request.
115
116 RewriteCond %{REQUEST_FILENAME} !.*\.(css|js|mp4|jpg)$ [NC]
117 RewriteCond %{REQUEST_URI} !^/index.php [NC]
118 RewriteRule ^(.*)$ index.php/\$1 [NC,L]
119
120 </Directory>
121
122 # Lock out undesired auth requests
123
124 <Location \"/login/auth\">
125
126 Order Deny,Allow
127 Deny from all
128 Allow from ${_APACHE_MOD_AUTHNZ_LOCALHOST_}
129
130 </Location>
131
132 <LocationMatch \"^(/invite|/page/invite|/page/invite\\.html)$\">
133
134 AuthType Basic
135 AuthName \"Invite @ ${_APACHE_SERVER_NAME_}\"
136
137 # authnz_external
138 AuthBasicProvider external
139
140 # Call into php again
141 AuthExternal ${_APACHE_EXT_AUTH_KEYWORD_}
142
143 # mod_authz_core configuration
144 Require valid-user
145
146 </LocationMatch>
147
148 # NOTE: path relative to container rootfs
149 Include ${_APACHE_CON_SSL_CONF_F_}
150 SSLCertificateFile ${_APACHE_CON_SSL_CERT_F_}
151 SSLCertificateKeyFile ${_APACHE_CON_SSL_KEY_F_}
152
153 # Configure external authentication module
154 <IfModule mod_authnz_external.c>
155 DefineExternalAuth ${_APACHE_EXT_AUTH_KEYWORD_} pipe ${_APACHE_CON_EXT_AUTH_F_}
156 </IfModule>
157
158 # Configure set default environment
159 <IfModule mod_env.c>
160 SetEnv CI_ENV ${_APACHE_CODE_IGNITER_ENV_}
161 </IfModule>
162
163 </VirtualHost>
164</IfModule>"
165
166if [ "${_GERRIT_HAS_HTTPS_}" -eq 1 ]; then
167 declare -r vhost_gerrit_t_has_ssl="\
168 SSLProxyEngine on
169 SSLProxyCheckPeerCN off
170 SSLProxyCheckPeerName off"
171else
172 declare -r vhost_gerrit_t_has_ssl=""
173fi
174
175declare -r vhost_gerrit_t="\
176<VirtualHost 127.0.0.1:${_APACHE_GERRIT_PROXY_PORT_}>
177
178 # Restrict to only requests from 127.0.0.1
179
180 <LocationMatch \".*\">
181
182 Order Deny,Allow
183 Deny from all
184 Allow from 127.0.0.1
185
186 </LocationMatch>
187
188 ErrorLog \"${_APACHE_VHOST_GERRIT_LOG_F_}\"
189 LogLevel ${_APACHE_VHOST_GERRIT_LOG_LEVEL_}
190
191${vhost_gerrit_t_has_ssl}
192
193 ProxyVia off
194 ProxyRequests off
195 ProxyPreserveHost on
196 ProxyErrorOverride on
197
198 AllowEncodedSlashes on
199 RewriteEngine on
200 # Proxy incoming requests towards Gerrit Code Review
201 RewriteRule ^(/?)(.*)$ ${_GERRIT_PROXY_PROTOCOL_}://${_GERRIT_DKRC_FRONTEND_IP_}:${_GERRIT_PROXY_PORT_}/gerrit/\$2 [NE,P]
202
203 <LocationMatch \"(/gerrit/login(/?)|/login(/?))\">
204
205 AuthType Basic
206 AuthName \"Gerrit @ ${_APACHE_SERVER_NAME_}\"
207
208 # authnz_external
209 AuthBasicProvider external
210
211 # Call into php again
212 AuthExternal ${_APACHE_EXT_AUTH_KEYWORD_}
213
214 # mod_authz_core configuration
215 Require valid-user
216
217 </LocationMatch>
218
219 # Configure external authentication module
220 <IfModule mod_authnz_external.c>
221 DefineExternalAuth ${_APACHE_EXT_AUTH_KEYWORD_} pipe ${_APACHE_CON_EXT_AUTH_F_}
222 </IfModule>
223
224</VirtualHost>"
225
226declare -r ports_t="\
227# If you just change the port or add more ports here, you will likely also
228# have to change the VirtualHost statement in
229# /etc/apache2/sites-enabled/000-default.conf
230
231#
232# Port mapped to the host (only one)
233#
234Listen ${_APACHE_HTTP_PORT_}
235
236#
237# Gerrit, vhost, proxied
238#
239Listen ${_APACHE_GERRIT_PROXY_PORT_}
240
241<IfModule ssl_module>
242 Listen ${_APACHE_HTTPS_PORT_}
243</IfModule>
244
245<IfModule mod_gnutls.c>
246 Listen ${_APACHE_HTTPS_PORT_}
247</IfModule>"
248
249declare -r apache2_t="\
250# This is the main Apache server configuration file. It contains the
251# configuration directives that give the server its instructions.
252# See http://httpd.apache.org/docs/2.4/ for detailed information about
253# the directives and /usr/share/doc/apache2/README.Debian about Debian specific
254# hints.
255#
256#
257# Summary of how the Apache 2 configuration works in Debian:
258# The Apache 2 web server configuration in Debian is quite different to
259# upstream's suggested way to configure the web server. This is because Debian's
260# default Apache2 installation attempts to make adding and removing modules,
261# virtual hosts, and extra configuration directives as flexible as possible, in
262# order to make automating the changes and administering the server as easy as
263# possible.
264
265# It is split into several files forming the configuration hierarchy outlined
266# below, all located in the /etc/apache2/ directory:
267#
268# /etc/apache2/
269# |-- apache2.conf
270# | |-- ports.conf
271# |-- mods-enabled
272# | |-- *.load
273# | |-- *.conf
274# |-- conf-enabled
275# | |-- *.conf
276# +-- sites-enabled
277# |-- *.conf
278#
279#
280# * apache2.conf is the main configuration file (this file). It puts the pieces
281# together by including all remaining configuration files when starting up the
282# web server.
283#
284# * ports.conf is always included from the main configuration file. It is
285# supposed to determine listening ports for incoming connections which can be
286# customized anytime.
287#
288# * Configuration files in the mods-enabled/, conf-enabled/ and sites-enabled/
289# directories contain particular configuration snippets which manage modules,
290# global configuration fragments, or virtual host configurations,
291# respectively.
292#
293# They are activated by symlinking available configuration files from their
294# respective *-available/ counterparts. These should be managed by using our
295# helpers a2enmod/a2dismod, a2ensite/a2dissite and a2enconf/a2disconf. See
296# their respective man pages for detailed information.
297#
298# * The binary is called apache2. Due to the use of environment variables, in
299# the default configuration, apache2 needs to be started/stopped with
300# /etc/init.d/apache2 or apache2ctl. Calling /usr/bin/apache2 directly will not
301# work with the default configuration.
302
303#
304# Global configuration
305#
306
307ServerName ${_APACHE_SERVER_NAME_}
308
309#
310# ServerRoot: The top of the directory tree under which the server's
311# configuration, error, and log files are kept.
312#
313# NOTE! If you intend to place this on an NFS (or otherwise network)
314# mounted filesystem then please read the Mutex documentation (available
315# at <URL:http://httpd.apache.org/docs/2.4/mod/core.html#mutex>);
316# you will save yourself a lot of trouble.
317#
318# Do NOT add a slash at the end of the directory path.
319#
320#ServerRoot \"/etc/apache2\"
321
322#
323# The accept serialization lock file MUST BE STORED ON A LOCAL DISK.
324#
325#Mutex file:\${APACHE_LOCK_DIR} default
326
327#
328# The directory where shm and other runtime files will be stored.
329#
330
331DefaultRuntimeDir \${APACHE_RUN_DIR}
332
333#
334# PidFile: The file in which the server should record its process
335# identification number when it starts.
336# This needs to be set in /etc/apache2/envvars
337#
338PidFile \${APACHE_PID_FILE}
339
340#
341# Timeout: The number of seconds before receives and sends time out.
342#
343Timeout 300
344
345#
346# KeepAlive: Whether or not to allow persistent connections (more than
347# one request per connection). Set to \"Off\" to deactivate.
348#
349KeepAlive On
350
351#
352# MaxKeepAliveRequests: The maximum number of requests to allow
353# during a persistent connection. Set to 0 to allow an unlimited amount.
354# We recommend you leave this number high, for maximum performance.
355#
356MaxKeepAliveRequests 100
357
358#
359# KeepAliveTimeout: Number of seconds to wait for the next request from the
360# same client on the same connection.
361#
362KeepAliveTimeout 5
363
364
365# These need to be set in /etc/apache2/envvars
366User \${APACHE_RUN_USER}
367Group \${APACHE_RUN_GROUP}
368
369#
370# HostnameLookups: Log the names of clients or just their IP addresses
371# e.g., www.apache.org (on) or 204.62.129.132 (off).
372# The default is off because it'd be overall better for the net if people
373# had to knowingly turn this feature on, since enabling it means that
374# each client request will result in AT LEAST one lookup request to the
375# nameserver.
376#
377HostnameLookups Off
378
379# ErrorLog: The location of the error log file.
380# If you do not specify an ErrorLog directive within a <VirtualHost>
381# container, error messages relating to that virtual host will be
382# logged here. If you *do* define an error logfile for a <VirtualHost>
383# container, that host's errors will be logged there and not here.
384#
385ErrorLog \${APACHE_LOG_DIR}/error.log
386
387#
388# LogLevel: Control the severity of messages logged to the error_log.
389# Available values: trace8, ..., trace1, debug, info, notice, warn,
390# error, crit, alert, emerg.
391# It is also possible to configure the log level for particular modules, e.g.
392# \"LogLevel info ssl:warn\"
393#
394LogLevel trace1
395
396# Include module configuration:
397IncludeOptional mods-enabled/*.load
398IncludeOptional mods-enabled/*.conf
399
400# Include list of ports to listen on
401Include ports.conf
402
403
404# Sets the default security model of the Apache2 HTTPD server. It does
405# not allow access to the root filesystem outside of /usr/share and /var/www.
406# The former is used by web applications packaged in Debian,
407# the latter may be used for local directories served by the web server. If
408# your system is serving content from a sub-directory in /srv you must allow
409# access here, or in any related virtual host.
410<Directory />
411 Options FollowSymLinks
412 AllowOverride None
413 Require all denied
414</Directory>
415
416<Directory /usr/share>
417 AllowOverride None
418 Require all granted
419</Directory>
420
421<Directory /var/www/>
422 Options Indexes FollowSymLinks
423 AllowOverride None
424 Require all granted
425</Directory>
426
427#<Directory /srv/>
428# Options Indexes FollowSymLinks
429# AllowOverride None
430# Require all granted
431#</Directory>
432
433# AccessFileName: The name of the file to look for in each directory
434# for additional configuration directives. See also the AllowOverride
435# directive.
436#
437AccessFileName .htaccess
438
439#
440# The following lines prevent .htaccess and .htpasswd files from being
441# viewed by Web clients.
442#
443<FilesMatch \"^\.ht\">
444 Require all denied
445</FilesMatch>
446
447#
448# The following directives define some format nicknames for use with
449# a CustomLog directive.
450#
451# These deviate from the Common Log Format definitions in that they use %O
452# (the actual bytes sent including headers) instead of %b (the size of the
453# requested file), because the latter makes it impossible to detect partial
454# requests.
455#
456# Note that the use of %{X-Forwarded-For}i instead of %h is not recommended.
457# Use mod_remoteip instead.
458#
459LogFormat \"%v:%p %h %l %u %t \\\"%r\\\" %>s %O \\\"%{Referer}i\\\" \\\"%{User-Agent}i\\\"\" vhost_combined
460LogFormat \"%h %l %u %t \\\"%r\\\" %>s %O \\\"%{Referer}i\\\" \\\"%{User-Agent}i\\\"\" combined
461LogFormat \"%h %l %u %t \\\"%r\\\" %>s %O\" common
462LogFormat \"%{Referer}i -> %U\" referer
463LogFormat \"%{User-agent}i\" agent
464
465# Include of directories ignores editors' and dpkg's backup files,
466# see README.Debian for details.
467
468# Include generic snippets of statements
469IncludeOptional conf-enabled/*.conf
470
471# Include the virtual host configurations:
472IncludeOptional sites-enabled/*.conf
473
474# vim: syntax=apache ts=4 sw=4 sts=4 sr noet"
475
476if [ "${mod_mode}" = "${release}" ]; then
477 declare -rg dockerfile_debug_tools_t=""
478else
479 declare -rg dockerfile_debug_tools_t="\
480# debug only
481RUN apt-get -y install nmap
482RUN apt-get -y install net-tools"
483fi
484
485declare -rg dockerfile_t="\
486# Ref https://hub.docker.com/_/php/
487# php:7.3-apache-stretch
488# Apache/2.4.25 (Debian)
489# PHP 7.3 Cli
490ARG VERSION=7.3.9-apache-stretch
491FROM php:\$VERSION AS base
492
493RUN apt-get update
494RUN apt-get -y install sudo
495
496${dockerfile_debug_tools_t}
497
498# install apxs tools
499RUN apt-get -y install apache2-dev
500
501# mysqli driver
502RUN docker-php-ext-install mysqli
503
504# mysql dbd
505#RUN apt-get -y install libaprutil1-dbd-mysql
506
507# serivce and a2* need root access
508USER root
509
510# import mod_authnz_external source
511COPY ${_APACHE_MOD_AUTHNZ_EXTERNAL_D_} /${_APACHE_MOD_AUTHNZ_EXTERNAL_D_}
512
513# build and install mod_authnz_external
514WORKDIR /${_APACHE_MOD_AUTHNZ_EXTERNAL_D_}
515RUN apxs -c mod_authnz_external.c
516RUN sudo apxs -i -a mod_authnz_external.la
517
518# stop apache, it starts with docker-compose
519RUN [\"/bin/bash\", \"-c\", \"service apache2 stop\"]
520
521# disable default conf
522RUN [\"/bin/bash\", \"-c\", \"a2dissite 000-default\"]
523
524# enable proxy
525RUN [\"/bin/bash\", \"-c\", \"a2enmod proxy\"]
526RUN [\"/bin/bash\", \"-c\", \"a2enmod proxy_http\"]
527RUN [\"/bin/bash\", \"-c\", \"a2enmod proxy_ajp\"]
528RUN [\"/bin/bash\", \"-c\", \"a2enmod rewrite\"]
529RUN [\"/bin/bash\", \"-c\", \"a2enmod deflate\"]
530RUN [\"/bin/bash\", \"-c\", \"a2enmod headers\"]
531RUN [\"/bin/bash\", \"-c\", \"a2enmod proxy_balancer\"]
532RUN [\"/bin/bash\", \"-c\", \"a2enmod proxy_connect\"]
533RUN [\"/bin/bash\", \"-c\", \"a2enmod proxy_html\"]
534
535# enable mod_authnz_external
536RUN [\"/bin/bash\", \"-c\", \"a2enmod authnz_external\"]
537
538# enable dbd
539#RUN [\"/bin/bash\", \"-c\", \"a2enmod dbd\"]
540#RUN [\"/bin/bash\", \"-c\", \"a2enmod authn_dbd\"]
541#RUN [\"/bin/bash\", \"-c\", \"a2enmod authn_socache\"]
542
543# enable session
544#RUN [\"/bin/bash\", \"-c\", \"a2enmod session\"]
545#RUN [\"/bin/bash\", \"-c\", \"a2enmod session_dbd\"]
546
547# enable auth form
548#RUN [\"/bin/bash\", \"-c\", \"a2enmod request\"]
549#RUN [\"/bin/bash\", \"-c\", \"a2enmod auth_form\"]
550
551# enable SSL
552RUN [\"/bin/bash\", \"-c\", \"a2enmod ssl\"]"
553
554declare -r http_authentication_bang_t="\
555#!/bin/bash
556
557for key in username password; do
558 read -r value; eval \"declare -r \$key=\\\"\$value\\\"\"
559done
560
561declare -ir http_expected_status=\"${_APACHE_MOD_AUTHNZ_SUCCESS_CODE_}\"
562declare -r php_api=\"https://${_APACHE_MOD_AUTHNZ_LOCALHOST_}/login/auth\"
563
564declare curl_flags
565curl_flags+=\" -k -L --post301 --post302 --post303\"
566curl_flags+=\" -w %{http_code}\"
567curl_flags+=\" -o ${_APACHE_MOD_AUTHNZ_DEBUG_FLAG_}\"
568curl_flags+=\" -H \\\"${_APACHE_MOD_AUTHNZ_H_ACCEPT_}\\\"\"
569curl_flags+=\" -H \\\"${_APACHE_MOD_AUTHNZ_H_CONTENT_}\\\"\"
570curl_flags+=\" --data \\\"username=\$username&password=\$password\\\"\"
571
572declare -ir http_status=\"\$(eval \"curl \$curl_flags \$php_api\")\"
573[ \"\$http_status\" -eq \"\$http_expected_status\" ]
574declare -ir _CODE=\"\$?\"
575
576echo \"\$(date +'%d/%m/%Y - %H:%M:%S'): \${BASH_SOURCE[0]}: HTTP_STATUS: \$http_status\" >&2
577exit \$_CODE"