blob: cff5c630a54cf8a207b0ac420453faab698c5f95 [file] [log] [blame]
Sean Daguee263c822014-12-05 14:25:28 -05001#!/bin/bash
2#
Dean Troyerc83a7e12012-11-29 11:47:58 -06003# lib/tls
4# Functions to control the configuration and operation of the TLS proxy service
5
Dean Troyerc83a7e12012-11-29 11:47:58 -06006# !! source _before_ any services that use ``SERVICE_HOST``
Adam Spiers6a5aa7c2013-10-24 11:27:02 +01007#
8# Dependencies:
9#
10# - ``functions`` file
11# - ``DEST``, ``DATA_DIR`` must be defined
12# - ``HOST_IP``, ``SERVICE_HOST``
13# - ``KEYSTONE_TOKEN_FORMAT`` must be defined
Dean Troyerc83a7e12012-11-29 11:47:58 -060014
15# Entry points:
Adam Spiers6a5aa7c2013-10-24 11:27:02 +010016#
17# - configure_CA
18# - init_CA
Dean Troyerc83a7e12012-11-29 11:47:58 -060019
Adam Spiers6a5aa7c2013-10-24 11:27:02 +010020# - configure_proxy
21# - start_tls_proxy
Dean Troyerc83a7e12012-11-29 11:47:58 -060022
Stanislaw Pituchabd5dae02014-06-25 15:29:43 +010023# - stop_tls_proxy
24# - cleanup_CA
25
Stanislaw Pitucha2e0f0542014-06-27 16:05:53 +010026# - make_root_CA
27# - make_int_CA
28# - make_cert ca-dir cert-name "common-name" ["alt-name" ...]
Adam Spiers6a5aa7c2013-10-24 11:27:02 +010029# - start_tls_proxy HOST_IP 5000 localhost 5000
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +100030# - ensure_certificates
31# - is_ssl_enabled_service
Rob Crittenden18d47782014-03-19 17:47:42 -040032# - enable_mod_ssl
Dean Troyerc83a7e12012-11-29 11:47:58 -060033
Dean Troyerdc97cb72015-03-28 08:20:50 -050034
Dean Troyercc6b4432013-04-08 15:38:03 -050035# Defaults
36# --------
37
Dean Troyerc83a7e12012-11-29 11:47:58 -060038if is_service_enabled tls-proxy; then
39 # TODO(dtroyer): revisit this below after the search for HOST_IP has been done
Jens Harbottdc7b4292017-09-19 10:52:32 +000040 TLS_IP=${TLS_IP:-$(ipv6_unquote $SERVICE_HOST)}
Dean Troyerc83a7e12012-11-29 11:47:58 -060041fi
42
Rob Crittenden18d47782014-03-19 17:47:42 -040043DEVSTACK_HOSTNAME=$(hostname -f)
Dean Troyerc83a7e12012-11-29 11:47:58 -060044DEVSTACK_CERT_NAME=devstack-cert
45DEVSTACK_CERT=$DATA_DIR/$DEVSTACK_CERT_NAME.pem
46
47# CA configuration
48ROOT_CA_DIR=${ROOT_CA_DIR:-$DATA_DIR/CA/root-ca}
49INT_CA_DIR=${INT_CA_DIR:-$DATA_DIR/CA/int-ca}
50
51ORG_NAME="OpenStack"
52ORG_UNIT_NAME="DevStack"
53
54# Stud configuration
55STUD_PROTO="--tls"
56STUD_CIPHERS='TLSv1+HIGH:!DES:!aNULL:!eNULL:@STRENGTH'
57
58
59# CA Functions
60# ============
61
62# There may be more than one, get specific
63OPENSSL=${OPENSSL:-/usr/bin/openssl}
64
65# Do primary CA configuration
Ian Wienandaee18c72014-02-21 15:35:08 +110066function configure_CA {
Dean Troyerc83a7e12012-11-29 11:47:58 -060067 # build common config file
68
69 # Verify ``TLS_IP`` is good
Jens Harbottdc7b4292017-09-19 10:52:32 +000070 if [[ -n "$SERVICE_HOST" && "$(ipv6_unquote $SERVICE_HOST)" != "$TLS_IP" ]]; then
Dean Troyerc83a7e12012-11-29 11:47:58 -060071 # auto-discover has changed the IP
Jens Harbottdc7b4292017-09-19 10:52:32 +000072 TLS_IP=$(ipv6_unquote $SERVICE_HOST)
Dean Troyerc83a7e12012-11-29 11:47:58 -060073 fi
74}
75
76# Creates a new CA directory structure
77# create_CA_base ca-dir
Ian Wienandaee18c72014-02-21 15:35:08 +110078function create_CA_base {
Dean Troyerc83a7e12012-11-29 11:47:58 -060079 local ca_dir=$1
80
81 if [[ -d $ca_dir ]]; then
82 # Bail out it exists
83 return 0
84 fi
85
Dean Troyerb1e3d0f2014-07-25 14:57:54 -050086 local i
Dean Troyerc83a7e12012-11-29 11:47:58 -060087 for i in certs crl newcerts private; do
88 mkdir -p $ca_dir/$i
89 done
90 chmod 710 $ca_dir/private
91 echo "01" >$ca_dir/serial
92 cp /dev/null $ca_dir/index.txt
93}
94
Dean Troyerc83a7e12012-11-29 11:47:58 -060095# Create a new CA configuration file
96# create_CA_config ca-dir common-name
Ian Wienandaee18c72014-02-21 15:35:08 +110097function create_CA_config {
Dean Troyerc83a7e12012-11-29 11:47:58 -060098 local ca_dir=$1
99 local common_name=$2
100
101 echo "
102[ ca ]
103default_ca = CA_default
104
105[ CA_default ]
106dir = $ca_dir
107policy = policy_match
108database = \$dir/index.txt
109serial = \$dir/serial
110certs = \$dir/certs
111crl_dir = \$dir/crl
112new_certs_dir = \$dir/newcerts
113certificate = \$dir/cacert.pem
114private_key = \$dir/private/cacert.key
115RANDFILE = \$dir/private/.rand
Clark Boylanfaffde12017-04-27 09:54:27 -0700116default_md = sha256
Dean Troyerc83a7e12012-11-29 11:47:58 -0600117
118[ req ]
Clark Boylanfaffde12017-04-27 09:54:27 -0700119default_bits = 2048
120default_md = sha256
Dean Troyerc83a7e12012-11-29 11:47:58 -0600121
122prompt = no
123distinguished_name = ca_distinguished_name
124
125x509_extensions = ca_extensions
126
127[ ca_distinguished_name ]
128organizationName = $ORG_NAME
129organizationalUnitName = $ORG_UNIT_NAME Certificate Authority
130commonName = $common_name
131
132[ policy_match ]
133countryName = optional
134stateOrProvinceName = optional
135organizationName = match
136organizationalUnitName = optional
137commonName = supplied
138
139[ ca_extensions ]
140basicConstraints = critical,CA:true
141subjectKeyIdentifier = hash
142authorityKeyIdentifier = keyid:always, issuer
143keyUsage = cRLSign, keyCertSign
144
145" >$ca_dir/ca.conf
146}
147
148# Create a new signing configuration file
149# create_signing_config ca-dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100150function create_signing_config {
Dean Troyerc83a7e12012-11-29 11:47:58 -0600151 local ca_dir=$1
152
153 echo "
154[ ca ]
155default_ca = CA_default
156
157[ CA_default ]
158dir = $ca_dir
159policy = policy_match
160database = \$dir/index.txt
161serial = \$dir/serial
162certs = \$dir/certs
163crl_dir = \$dir/crl
164new_certs_dir = \$dir/newcerts
165certificate = \$dir/cacert.pem
166private_key = \$dir/private/cacert.key
167RANDFILE = \$dir/private/.rand
168default_md = default
169
170[ req ]
171default_bits = 1024
Michael Johnson35bc6002022-02-28 18:42:34 +0000172default_md = sha256
Dean Troyerc83a7e12012-11-29 11:47:58 -0600173
174prompt = no
175distinguished_name = req_distinguished_name
176
177x509_extensions = req_extensions
178
179[ req_distinguished_name ]
180organizationName = $ORG_NAME
181organizationalUnitName = $ORG_UNIT_NAME Server Farm
182
183[ policy_match ]
184countryName = optional
185stateOrProvinceName = optional
186organizationName = match
187organizationalUnitName = optional
188commonName = supplied
189
190[ req_extensions ]
191basicConstraints = CA:false
192subjectKeyIdentifier = hash
193authorityKeyIdentifier = keyid:always, issuer
194keyUsage = digitalSignature, keyEncipherment, keyAgreement
195extendedKeyUsage = serverAuth, clientAuth
196subjectAltName = \$ENV::SUBJECT_ALT_NAME
197
198" >$ca_dir/signing.conf
199}
200
Dean Troyerca802172013-01-09 19:08:02 -0600201# Create root and intermediate CAs
Dean Troyerc83a7e12012-11-29 11:47:58 -0600202# init_CA
203function init_CA {
204 # Ensure CAs are built
205 make_root_CA $ROOT_CA_DIR
206 make_int_CA $INT_CA_DIR $ROOT_CA_DIR
207
208 # Create the CA bundle
209 cat $ROOT_CA_DIR/cacert.pem $INT_CA_DIR/cacert.pem >>$INT_CA_DIR/ca-chain.pem
Rob Crittenden18d47782014-03-19 17:47:42 -0400210 cat $INT_CA_DIR/ca-chain.pem >> $SSL_BUNDLE_FILE
211
212 if is_fedora; then
213 sudo cp $INT_CA_DIR/ca-chain.pem /usr/share/pki/ca-trust-source/anchors/devstack-chain.pem
214 sudo update-ca-trust
215 elif is_ubuntu; then
216 sudo cp $INT_CA_DIR/ca-chain.pem /usr/local/share/ca-certificates/devstack-int.crt
217 sudo cp $ROOT_CA_DIR/cacert.pem /usr/local/share/ca-certificates/devstack-root.crt
218 sudo update-ca-certificates
219 fi
220}
221
Dean Troyerca802172013-01-09 19:08:02 -0600222# Create an initial server cert
223# init_cert
224function init_cert {
Dean Troyerc83a7e12012-11-29 11:47:58 -0600225 if [[ ! -r $DEVSTACK_CERT ]]; then
226 if [[ -n "$TLS_IP" ]]; then
Jens Harbottd7a82f42020-06-23 10:21:09 +0200227 TLS_IP="IP:$TLS_IP"
Julia Kreger0fe25e32019-06-20 20:39:53 -0700228 if [[ -n "$HOST_IPV6" ]]; then
229 TLS_IP="$TLS_IP,IP:$HOST_IPV6"
230 fi
Dean Troyerc83a7e12012-11-29 11:47:58 -0600231 fi
232 make_cert $INT_CA_DIR $DEVSTACK_CERT_NAME $DEVSTACK_HOSTNAME "$TLS_IP"
233
234 # Create a cert bundle
235 cat $INT_CA_DIR/private/$DEVSTACK_CERT_NAME.key $INT_CA_DIR/$DEVSTACK_CERT_NAME.crt $INT_CA_DIR/cacert.pem >$DEVSTACK_CERT
236 fi
237}
238
Dean Troyerc83a7e12012-11-29 11:47:58 -0600239# make_cert creates and signs a new certificate with the given commonName and CA
240# make_cert ca-dir cert-name "common-name" ["alt-name" ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100241function make_cert {
Dean Troyerc83a7e12012-11-29 11:47:58 -0600242 local ca_dir=$1
243 local cert_name=$2
244 local common_name=$3
245 local alt_names=$4
246
Rob Crittendenbe00e952016-03-24 18:09:22 -0400247 if [ "$common_name" != "$SERVICE_HOST" ]; then
Ian Cordasco69e3c0a2016-09-26 12:53:14 -0500248 if is_ipv4_address "$SERVICE_HOST" ; then
Tim Burke01377032018-11-30 14:40:12 -0800249 if [[ -z "$alt_names" ]]; then
250 alt_names="IP:$SERVICE_HOST"
251 else
252 alt_names="$alt_names,IP:$SERVICE_HOST"
253 fi
Ian Cordasco69e3c0a2016-09-26 12:53:14 -0500254 fi
Rob Crittendenbe00e952016-03-24 18:09:22 -0400255 fi
256
Stanislaw Pitucha2f69c6b2014-06-25 15:07:48 +0100257 # Only generate the certificate if it doesn't exist yet on the disk
258 if [ ! -r "$ca_dir/$cert_name.crt" ]; then
259 # Generate a signing request
260 $OPENSSL req \
Michael Johnson35bc6002022-02-28 18:42:34 +0000261 -sha256 \
Stanislaw Pitucha2f69c6b2014-06-25 15:07:48 +0100262 -newkey rsa \
263 -nodes \
264 -keyout $ca_dir/private/$cert_name.key \
265 -out $ca_dir/$cert_name.csr \
266 -subj "/O=${ORG_NAME}/OU=${ORG_UNIT_NAME} Servers/CN=${common_name}"
Dean Troyerc83a7e12012-11-29 11:47:58 -0600267
Stanislaw Pitucha2f69c6b2014-06-25 15:07:48 +0100268 if [[ -z "$alt_names" ]]; then
269 alt_names="DNS:${common_name}"
270 else
271 alt_names="DNS:${common_name},${alt_names}"
272 fi
273
274 # Sign the request valid for 1 year
275 SUBJECT_ALT_NAME="$alt_names" \
276 $OPENSSL ca -config $ca_dir/signing.conf \
277 -extensions req_extensions \
278 -days 365 \
279 -notext \
280 -in $ca_dir/$cert_name.csr \
281 -out $ca_dir/$cert_name.crt \
282 -subj "/O=${ORG_NAME}/OU=${ORG_UNIT_NAME} Servers/CN=${common_name}" \
283 -batch
Dean Troyerc83a7e12012-11-29 11:47:58 -0600284 fi
Dean Troyerc83a7e12012-11-29 11:47:58 -0600285}
286
Dean Troyerc83a7e12012-11-29 11:47:58 -0600287# Make an intermediate CA to sign everything else
288# make_int_CA ca-dir signing-ca-dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100289function make_int_CA {
Dean Troyerc83a7e12012-11-29 11:47:58 -0600290 local ca_dir=$1
291 local signing_ca_dir=$2
292
293 # Create the root CA
294 create_CA_base $ca_dir
295 create_CA_config $ca_dir 'Intermediate CA'
296 create_signing_config $ca_dir
297
Stanislaw Pitucha2f69c6b2014-06-25 15:07:48 +0100298 if [ ! -r "$ca_dir/cacert.pem" ]; then
299 # Create a signing certificate request
300 $OPENSSL req -config $ca_dir/ca.conf \
Michael Johnson35bc6002022-02-28 18:42:34 +0000301 -sha256 \
Stanislaw Pitucha2f69c6b2014-06-25 15:07:48 +0100302 -newkey rsa \
303 -nodes \
304 -keyout $ca_dir/private/cacert.key \
305 -out $ca_dir/cacert.csr \
306 -outform PEM
Dean Troyerc83a7e12012-11-29 11:47:58 -0600307
Stanislaw Pitucha2f69c6b2014-06-25 15:07:48 +0100308 # Sign the intermediate request valid for 1 year
309 $OPENSSL ca -config $signing_ca_dir/ca.conf \
310 -extensions ca_extensions \
311 -days 365 \
312 -notext \
313 -in $ca_dir/cacert.csr \
314 -out $ca_dir/cacert.pem \
315 -batch
316 fi
Dean Troyerc83a7e12012-11-29 11:47:58 -0600317}
318
319# Make a root CA to sign other CAs
320# make_root_CA ca-dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100321function make_root_CA {
Dean Troyerc83a7e12012-11-29 11:47:58 -0600322 local ca_dir=$1
323
324 # Create the root CA
325 create_CA_base $ca_dir
326 create_CA_config $ca_dir 'Root CA'
327
Clark Boylan323b7262016-09-23 13:33:40 -0700328 if [ ! -r "$ca_dir/cacert.pem" ]; then
329 # Create a self-signed certificate valid for 5 years
330 $OPENSSL req -config $ca_dir/ca.conf \
331 -x509 \
332 -nodes \
333 -newkey rsa \
334 -days 21360 \
335 -keyout $ca_dir/private/cacert.key \
336 -out $ca_dir/cacert.pem \
337 -outform PEM
338 fi
Dean Troyerc83a7e12012-11-29 11:47:58 -0600339}
340
Daniel P. Berrangee9870eb2016-11-10 13:03:32 +0000341# Deploy the service cert & key to a service specific
342# location
343function deploy_int_cert {
344 local cert_target_file=$1
345 local key_target_file=$2
346
347 sudo cp "$INT_CA_DIR/$DEVSTACK_CERT_NAME.crt" "$cert_target_file"
348 sudo cp "$INT_CA_DIR/private/$DEVSTACK_CERT_NAME.key" "$key_target_file"
349}
350
351# Deploy the intermediate CA cert bundle file to a service
352# specific location
353function deploy_int_CA {
354 local ca_target_file=$1
355
356 sudo cp "$INT_CA_DIR/ca-chain.pem" "$ca_target_file"
357}
358
Rob Crittenden1987fcc2015-06-10 11:00:59 -0400359# If a non-system python-requests is installed then it will use the
360# built-in CA certificate store rather than the distro-specific
361# CA certificate store. Detect this and symlink to the correct
362# one. If the value for the CA is not rooted in /etc then we know
363# we need to change it.
364function fix_system_ca_bundle_path {
Sean Daguef3b2f4c2017-04-13 10:11:48 -0400365 if is_service_enabled tls-proxy; then
Ian Wienandada886d2015-10-07 14:06:26 +1100366 local capath
Clark Boylana40f9cb2018-04-04 14:02:30 -0700367 if [[ "$GLOBAL_VENV" == "True" ]] ; then
368 capath=$($DEVSTACK_VENV/bin/python3 -c $'try:\n from requests import certs\n print (certs.where())\nexcept ImportError: pass')
369 else
370 capath=$(python3 -c $'try:\n from requests import certs\n print (certs.where())\nexcept ImportError: pass')
371 fi
Rob Crittenden1987fcc2015-06-10 11:00:59 -0400372 if [[ ! $capath == "" && ! $capath =~ ^/etc/.* && ! -L $capath ]]; then
373 if is_fedora; then
374 sudo rm -f $capath
375 sudo ln -s /etc/pki/tls/certs/ca-bundle.crt $capath
376 elif is_ubuntu; then
377 sudo rm -f $capath
378 sudo ln -s /etc/ssl/certs/ca-certificates.crt $capath
379 else
380 echo "Don't know how to set the CA bundle, expect the install to fail."
381 fi
382 fi
383 fi
384}
385
Dean Troyerc83a7e12012-11-29 11:47:58 -0600386
Sean Daguef3b2f4c2017-04-13 10:11:48 -0400387# Only for compatibility, return if the tls-proxy is enabled
388function is_ssl_enabled_service {
389 return is_service_enabled tls-proxy
390}
391
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000392# Certificate Input Configuration
393# ===============================
394
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000395# Ensure that the certificates for a service are in place. This function does
396# not check that a service is SSL enabled, this should already have been
397# completed.
398#
399# The function expects to find a certificate, key and CA certificate in the
Dean Troyerdc97cb72015-03-28 08:20:50 -0500400# variables ``{service}_SSL_CERT``, ``{service}_SSL_KEY`` and ``{service}_SSL_CA``. For
401# example for keystone this would be ``KEYSTONE_SSL_CERT``, ``KEYSTONE_SSL_KEY`` and
402# ``KEYSTONE_SSL_CA``.
Rob Crittenden18d47782014-03-19 17:47:42 -0400403#
Dean Troyerdc97cb72015-03-28 08:20:50 -0500404# If it does not find these certificates then the DevStack-issued server
Rob Crittenden18d47782014-03-19 17:47:42 -0400405# certificate, key and CA certificate will be associated with the service.
406#
407# If only some of the variables are provided then the function will quit.
Ian Wienandaee18c72014-02-21 15:35:08 +1100408function ensure_certificates {
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000409 local service=$1
410
411 local cert_var="${service}_SSL_CERT"
412 local key_var="${service}_SSL_KEY"
413 local ca_var="${service}_SSL_CA"
414
415 local cert=${!cert_var}
416 local key=${!key_var}
417 local ca=${!ca_var}
418
Rob Crittenden18d47782014-03-19 17:47:42 -0400419 if [[ -z "$cert" && -z "$key" && -z "$ca" ]]; then
420 local cert="$INT_CA_DIR/$DEVSTACK_CERT_NAME.crt"
421 local key="$INT_CA_DIR/private/$DEVSTACK_CERT_NAME.key"
422 local ca="$INT_CA_DIR/ca-chain.pem"
423 eval ${service}_SSL_CERT=\$cert
424 eval ${service}_SSL_KEY=\$key
425 eval ${service}_SSL_CA=\$ca
426 return # the CA certificate is already in the bundle
427 elif [[ -z "$cert" || -z "$key" || -z "$ca" ]]; then
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000428 die $LINENO "Missing either the ${cert_var} ${key_var} or ${ca_var}" \
429 "variable to enable SSL for ${service}"
430 fi
431
432 cat $ca >> $SSL_BUNDLE_FILE
433}
434
Rob Crittenden18d47782014-03-19 17:47:42 -0400435# Enable the mod_ssl plugin in Apache
436function enable_mod_ssl {
437 echo "Enabling mod_ssl"
438
439 if is_ubuntu; then
440 sudo a2enmod ssl
441 elif is_fedora; then
442 # Fedora enables mod_ssl by default
443 :
444 fi
445 if ! sudo `which httpd || which apache2ctl` -M | grep -w -q ssl_module; then
446 die $LINENO "mod_ssl is not enabled in apache2/httpd, please check for it manually and run stack.sh again"
447 fi
448}
449
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000450
Dean Troyerc83a7e12012-11-29 11:47:58 -0600451# Proxy Functions
452# ===============
453
Clark Boylancfb9f052016-11-29 10:43:05 -0800454function tune_apache_connections {
Clark Boylanb9be9412025-03-26 10:09:38 -0700455 local should_restart=$1
Clark Boylancfb9f052016-11-29 10:43:05 -0800456 local tuning_file=$APACHE_SETTINGS_DIR/connection-tuning.conf
457 if ! [ -f $tuning_file ] ; then
458 sudo bash -c "cat > $tuning_file" << EOF
459# worker MPM
460# StartServers: initial number of server processes to start
461# MinSpareThreads: minimum number of worker threads which are kept spare
462# MaxSpareThreads: maximum number of worker threads which are kept spare
463# ThreadLimit: ThreadsPerChild can be changed to this maximum value during a
464# graceful restart. ThreadLimit can only be changed by stopping
465# and starting Apache.
466# ThreadsPerChild: constant number of worker threads in each server process
467# MaxClients: maximum number of simultaneous client connections
468# MaxRequestsPerChild: maximum number of requests a server process serves
469#
Clark Boylan8cf9acd2017-03-16 14:06:58 -0700470# We want to be memory thrifty so tune down apache to allow 256 total
471# connections. This should still be plenty for a dev env yet lighter than
472# apache defaults.
Clark Boylancfb9f052016-11-29 10:43:05 -0800473<IfModule mpm_worker_module>
474# Note that the next three conf values must be changed together.
475# MaxClients = ServerLimit * ThreadsPerChild
Clark Boylan8cf9acd2017-03-16 14:06:58 -0700476ServerLimit 8
Clark Boylancfb9f052016-11-29 10:43:05 -0800477ThreadsPerChild 32
Clark Boylan8cf9acd2017-03-16 14:06:58 -0700478MaxClients 256
479StartServers 2
480MinSpareThreads 32
481MaxSpareThreads 96
Clark Boylancfb9f052016-11-29 10:43:05 -0800482ThreadLimit 64
483MaxRequestsPerChild 0
484</IfModule>
485<IfModule mpm_event_module>
486# Note that the next three conf values must be changed together.
487# MaxClients = ServerLimit * ThreadsPerChild
Clark Boylan8cf9acd2017-03-16 14:06:58 -0700488ServerLimit 8
Clark Boylancfb9f052016-11-29 10:43:05 -0800489ThreadsPerChild 32
Clark Boylan8cf9acd2017-03-16 14:06:58 -0700490MaxClients 256
491StartServers 2
492MinSpareThreads 32
493MaxSpareThreads 96
Clark Boylancfb9f052016-11-29 10:43:05 -0800494ThreadLimit 64
495MaxRequestsPerChild 0
496</IfModule>
497EOF
Clark Boylanb9be9412025-03-26 10:09:38 -0700498 if [ "$should_restart" != "norestart" ] ; then
499 # Only restart the apache server if we know we really want to
500 # do so. Too many restarts in a short period of time is treated
501 # as an error by systemd.
502 restart_apache_server
503 fi
Clark Boylancfb9f052016-11-29 10:43:05 -0800504 fi
505}
506
Dean Troyerc83a7e12012-11-29 11:47:58 -0600507# Starts the TLS proxy for the given IP/ports
Jens Harbott46399842017-08-28 11:43:37 +0000508# start_tls_proxy service-name front-host front-port back-host back-port
Ian Wienandaee18c72014-02-21 15:35:08 +1100509function start_tls_proxy {
Gregory Haynes4b49e402016-08-31 18:19:51 -0700510 local b_service="$1-tls-proxy"
511 local f_host=$2
512 local f_port=$3
513 local b_host=$4
514 local b_port=$5
Clark Boylanf4dbd122017-05-31 13:17:22 -0700515 # 8190 is the default apache size.
516 local f_header_size=${6:-8190}
Dean Troyerc83a7e12012-11-29 11:47:58 -0600517
Clark Boylanb9be9412025-03-26 10:09:38 -0700518 # We don't restart apache here as we'll do it at the end of the function.
519 tune_apache_connections norestart
Clark Boylancfb9f052016-11-29 10:43:05 -0800520
Gregory Haynes4b49e402016-08-31 18:19:51 -0700521 local config_file
522 config_file=$(apache_site_config_for $b_service)
523 local listen_string
524 # Default apache configs on ubuntu and centos listen on 80 and 443
525 # newer apache seems fine with duplicate listen directive but older
526 # apache does not so special case 80 and 443.
527 if [[ "$f_port" == "80" ]] || [[ "$f_port" == "443" ]]; then
528 listen_string=""
529 elif [[ "$f_host" == '*' ]] ; then
530 listen_string="Listen $f_port"
531 else
532 listen_string="Listen $f_host:$f_port"
533 fi
534 sudo bash -c "cat >$config_file" << EOF
535$listen_string
536
537<VirtualHost $f_host:$f_port>
538 SSLEngine On
539 SSLCertificateFile $DEVSTACK_CERT
Dirk Muellerdc01a8a2019-07-14 22:33:13 +0200540 SSLProtocol -all +TLSv1.3 +TLSv1.2
Gregory Haynes4b49e402016-08-31 18:19:51 -0700541
Jordan Pittier43709252017-02-14 16:48:20 +0100542 # Disable KeepAlive to fix bug #1630664 a.k.a the
543 # ('Connection aborted.', BadStatusLine("''",)) error
544 KeepAlive Off
545
Clark Boylanf4dbd122017-05-31 13:17:22 -0700546 # This increase in allowed request header sizes is required
547 # for swift functional testing to work with tls enabled. It is 2 bytes
548 # larger than the apache default of 8190.
549 LimitRequestFieldSize $f_header_size
Jens Harbott411c34d2017-08-29 14:40:26 +0000550 RequestHeader set X-Forwarded-Proto "https"
Clark Boylanf4dbd122017-05-31 13:17:22 -0700551
Clark Boylane344c972018-12-07 14:49:15 -0800552 # Avoid races (at the cost of performance) to re-use a pooled connection
553 # where the connection is closed (bug 1807518).
Dan Smithc3b0b902023-08-04 06:41:30 -0700554 # Set acquire=1 to disable waiting for connection pool members so that
555 # we can determine when apache is overloaded (returns 503).
Clark Boylane344c972018-12-07 14:49:15 -0800556 SetEnv proxy-initial-not-pooled
Gregory Haynes4b49e402016-08-31 18:19:51 -0700557 <Location />
Dan Smithc3b0b902023-08-04 06:41:30 -0700558 ProxyPass http://$b_host:$b_port/ retry=0 nocanon acquire=1
Gregory Haynes4b49e402016-08-31 18:19:51 -0700559 ProxyPassReverse http://$b_host:$b_port/
560 </Location>
Clark Boylan66ce5c22016-10-05 12:11:05 -0700561 ErrorLog $APACHE_LOG_DIR/tls-proxy_error.log
Ian Wienand139837d2017-08-08 17:51:29 +1000562 ErrorLogFormat "%{cu}t [%-m:%l] [pid %P:tid %T] %7F: %E: [client\ %a] [frontend\ %A] %M% ,\ referer\ %{Referer}i"
Clark Boylan66ce5c22016-10-05 12:11:05 -0700563 LogLevel info
Dan Smith64d68672022-04-22 07:58:29 -0700564 CustomLog $APACHE_LOG_DIR/tls-proxy_access.log combined
Gregory Haynes4b49e402016-08-31 18:19:51 -0700565</VirtualHost>
566EOF
Jens Harbott411c34d2017-08-29 14:40:26 +0000567 for mod in headers ssl proxy proxy_http; do
Clark Boylanb9be9412025-03-26 10:09:38 -0700568 # We don't need to restart here as we will restart once at the end
569 # of the function.
570 enable_apache_mod $mod norestart
Gregory Haynes4b49e402016-08-31 18:19:51 -0700571 done
572 enable_apache_site $b_service
Ian Wienandf6a2d2c2017-04-26 10:50:29 +1000573 restart_apache_server
Dean Troyerc83a7e12012-11-29 11:47:58 -0600574}
Sean Dague584d90e2013-03-29 14:34:53 -0400575
Stanislaw Pituchabd5dae02014-06-25 15:29:43 +0100576# Cleanup Functions
Dean Troyer3324f192014-09-18 09:26:39 -0500577# =================
Stanislaw Pituchabd5dae02014-06-25 15:29:43 +0100578
Gregory Haynes4b49e402016-08-31 18:19:51 -0700579# Stops the apache service. This should be done only after all services
Stanislaw Pituchabd5dae02014-06-25 15:29:43 +0100580# using tls configuration are down.
581function stop_tls_proxy {
Gregory Haynes4b49e402016-08-31 18:19:51 -0700582 stop_apache_server
Jens Harbott1db9b5d2017-11-03 08:37:21 +0000583
584 # NOTE(jh): Removing all tls-proxy configs is a bit of a hack, but
585 # necessary so that we can restart after an unstack. A better
586 # solution would be to ensure that each service calling
587 # start_tls_proxy will call stop_tls_proxy with the same
588 # parameters on shutdown so we can use the disable_apache_site
589 # function and remove individual files there.
590 if is_ubuntu; then
591 sudo rm -f /etc/apache2/sites-enabled/*-tls-proxy.conf
592 else
593 for i in $APACHE_CONF_DIR/*-tls-proxy.conf; do
594 sudo mv $i $i.disabled
595 done
596 fi
Stanislaw Pituchabd5dae02014-06-25 15:29:43 +0100597}
598
Gregory Haynes4b49e402016-08-31 18:19:51 -0700599# Clean up the CA files
600# cleanup_CA
Stanislaw Pituchabd5dae02014-06-25 15:29:43 +0100601function cleanup_CA {
Gregory Haynes4b49e402016-08-31 18:19:51 -0700602 if is_fedora; then
603 sudo rm -f /usr/share/pki/ca-trust-source/anchors/devstack-chain.pem
604 sudo update-ca-trust
605 elif is_ubuntu; then
606 sudo rm -f /usr/local/share/ca-certificates/devstack-int.crt
607 sudo rm -f /usr/local/share/ca-certificates/devstack-root.crt
608 sudo update-ca-certificates
609 fi
610
Clark Boylan323b7262016-09-23 13:33:40 -0700611 rm -rf "$INT_CA_DIR" "$ROOT_CA_DIR" "$DEVSTACK_CERT"
Stanislaw Pituchabd5dae02014-06-25 15:29:43 +0100612}
613
Adam Spiers6a5aa7c2013-10-24 11:27:02 +0100614# Tell emacs to use shell-script-mode
615## Local variables:
616## mode: shell-script
617## End: