blob: 2c4e18d388f2176ac7ec3aa6016cbd319272e59a [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
40 TLS_IP=${TLS_IP:-$SERVICE_IP}
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
70 if [[ -n "$HOST_IP" && "$HOST_IP" != "$TLS_IP" ]]; then
71 # auto-discover has changed the IP
72 TLS_IP=$HOST_IP
73 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
116default_md = default
117
118[ req ]
119default_bits = 1024
120default_md = sha1
121
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
172default_md = sha1
173
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 {
Rob Crittenden1987fcc2015-06-10 11:00:59 -0400204 fix_system_ca_bundle_path
Dean Troyerc83a7e12012-11-29 11:47:58 -0600205 # Ensure CAs are built
206 make_root_CA $ROOT_CA_DIR
207 make_int_CA $INT_CA_DIR $ROOT_CA_DIR
208
209 # Create the CA bundle
210 cat $ROOT_CA_DIR/cacert.pem $INT_CA_DIR/cacert.pem >>$INT_CA_DIR/ca-chain.pem
Rob Crittenden18d47782014-03-19 17:47:42 -0400211 cat $INT_CA_DIR/ca-chain.pem >> $SSL_BUNDLE_FILE
212
213 if is_fedora; then
214 sudo cp $INT_CA_DIR/ca-chain.pem /usr/share/pki/ca-trust-source/anchors/devstack-chain.pem
215 sudo update-ca-trust
216 elif is_ubuntu; then
217 sudo cp $INT_CA_DIR/ca-chain.pem /usr/local/share/ca-certificates/devstack-int.crt
218 sudo cp $ROOT_CA_DIR/cacert.pem /usr/local/share/ca-certificates/devstack-root.crt
219 sudo update-ca-certificates
220 fi
221}
222
Dean Troyerca802172013-01-09 19:08:02 -0600223# Create an initial server cert
224# init_cert
225function init_cert {
Dean Troyerc83a7e12012-11-29 11:47:58 -0600226 if [[ ! -r $DEVSTACK_CERT ]]; then
227 if [[ -n "$TLS_IP" ]]; then
228 # Lie to let incomplete match routines work
229 TLS_IP="DNS:$TLS_IP"
230 fi
231 make_cert $INT_CA_DIR $DEVSTACK_CERT_NAME $DEVSTACK_HOSTNAME "$TLS_IP"
232
233 # Create a cert bundle
234 cat $INT_CA_DIR/private/$DEVSTACK_CERT_NAME.key $INT_CA_DIR/$DEVSTACK_CERT_NAME.crt $INT_CA_DIR/cacert.pem >$DEVSTACK_CERT
235 fi
236}
237
Dean Troyerc83a7e12012-11-29 11:47:58 -0600238# make_cert creates and signs a new certificate with the given commonName and CA
239# make_cert ca-dir cert-name "common-name" ["alt-name" ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100240function make_cert {
Dean Troyerc83a7e12012-11-29 11:47:58 -0600241 local ca_dir=$1
242 local cert_name=$2
243 local common_name=$3
244 local alt_names=$4
245
Rob Crittendenbe00e952016-03-24 18:09:22 -0400246 if [ "$common_name" != "$SERVICE_HOST" ]; then
247 if [[ -z "$alt_names" ]]; then
248 alt_names="DNS:$SERVICE_HOST"
249 else
250 alt_names="$alt_names,DNS:$SERVICE_HOST"
251 fi
252 fi
253
Stanislaw Pitucha2f69c6b2014-06-25 15:07:48 +0100254 # Only generate the certificate if it doesn't exist yet on the disk
255 if [ ! -r "$ca_dir/$cert_name.crt" ]; then
256 # Generate a signing request
257 $OPENSSL req \
258 -sha1 \
259 -newkey rsa \
260 -nodes \
261 -keyout $ca_dir/private/$cert_name.key \
262 -out $ca_dir/$cert_name.csr \
263 -subj "/O=${ORG_NAME}/OU=${ORG_UNIT_NAME} Servers/CN=${common_name}"
Dean Troyerc83a7e12012-11-29 11:47:58 -0600264
Stanislaw Pitucha2f69c6b2014-06-25 15:07:48 +0100265 if [[ -z "$alt_names" ]]; then
266 alt_names="DNS:${common_name}"
267 else
268 alt_names="DNS:${common_name},${alt_names}"
269 fi
270
271 # Sign the request valid for 1 year
272 SUBJECT_ALT_NAME="$alt_names" \
273 $OPENSSL ca -config $ca_dir/signing.conf \
274 -extensions req_extensions \
275 -days 365 \
276 -notext \
277 -in $ca_dir/$cert_name.csr \
278 -out $ca_dir/$cert_name.crt \
279 -subj "/O=${ORG_NAME}/OU=${ORG_UNIT_NAME} Servers/CN=${common_name}" \
280 -batch
Dean Troyerc83a7e12012-11-29 11:47:58 -0600281 fi
Dean Troyerc83a7e12012-11-29 11:47:58 -0600282}
283
Dean Troyerc83a7e12012-11-29 11:47:58 -0600284# Make an intermediate CA to sign everything else
285# make_int_CA ca-dir signing-ca-dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100286function make_int_CA {
Dean Troyerc83a7e12012-11-29 11:47:58 -0600287 local ca_dir=$1
288 local signing_ca_dir=$2
289
290 # Create the root CA
291 create_CA_base $ca_dir
292 create_CA_config $ca_dir 'Intermediate CA'
293 create_signing_config $ca_dir
294
Stanislaw Pitucha2f69c6b2014-06-25 15:07:48 +0100295 if [ ! -r "$ca_dir/cacert.pem" ]; then
296 # Create a signing certificate request
297 $OPENSSL req -config $ca_dir/ca.conf \
298 -sha1 \
299 -newkey rsa \
300 -nodes \
301 -keyout $ca_dir/private/cacert.key \
302 -out $ca_dir/cacert.csr \
303 -outform PEM
Dean Troyerc83a7e12012-11-29 11:47:58 -0600304
Stanislaw Pitucha2f69c6b2014-06-25 15:07:48 +0100305 # Sign the intermediate request valid for 1 year
306 $OPENSSL ca -config $signing_ca_dir/ca.conf \
307 -extensions ca_extensions \
308 -days 365 \
309 -notext \
310 -in $ca_dir/cacert.csr \
311 -out $ca_dir/cacert.pem \
312 -batch
313 fi
Dean Troyerc83a7e12012-11-29 11:47:58 -0600314}
315
316# Make a root CA to sign other CAs
317# make_root_CA ca-dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100318function make_root_CA {
Dean Troyerc83a7e12012-11-29 11:47:58 -0600319 local ca_dir=$1
320
321 # Create the root CA
322 create_CA_base $ca_dir
323 create_CA_config $ca_dir 'Root CA'
324
325 # Create a self-signed certificate valid for 5 years
326 $OPENSSL req -config $ca_dir/ca.conf \
327 -x509 \
328 -nodes \
329 -newkey rsa \
330 -days 21360 \
331 -keyout $ca_dir/private/cacert.key \
332 -out $ca_dir/cacert.pem \
333 -outform PEM
334}
335
Rob Crittenden1987fcc2015-06-10 11:00:59 -0400336# If a non-system python-requests is installed then it will use the
337# built-in CA certificate store rather than the distro-specific
338# CA certificate store. Detect this and symlink to the correct
339# one. If the value for the CA is not rooted in /etc then we know
340# we need to change it.
341function fix_system_ca_bundle_path {
342 if is_service_enabled tls-proxy || [ "$USE_SSL" == "True" ]; then
Ian Wienandada886d2015-10-07 14:06:26 +1100343 local capath
344 capath=$(python -c $'try:\n from requests import certs\n print certs.where()\nexcept ImportError: pass')
Rob Crittenden1987fcc2015-06-10 11:00:59 -0400345
346 if [[ ! $capath == "" && ! $capath =~ ^/etc/.* && ! -L $capath ]]; then
347 if is_fedora; then
348 sudo rm -f $capath
349 sudo ln -s /etc/pki/tls/certs/ca-bundle.crt $capath
350 elif is_ubuntu; then
351 sudo rm -f $capath
352 sudo ln -s /etc/ssl/certs/ca-certificates.crt $capath
353 else
354 echo "Don't know how to set the CA bundle, expect the install to fail."
355 fi
356 fi
357 fi
358}
359
Dean Troyerc83a7e12012-11-29 11:47:58 -0600360
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000361# Certificate Input Configuration
362# ===============================
363
364# check to see if the service(s) specified are to be SSL enabled.
365#
366# Multiple services specified as arguments are ``OR``'ed together; the test
367# is a short-circuit boolean, i.e it returns on the first match.
368#
369# Uses global ``SSL_ENABLED_SERVICES``
Ian Wienandaee18c72014-02-21 15:35:08 +1100370function is_ssl_enabled_service {
Sean Daguef0bd8db2014-07-23 15:14:07 -0400371 local services=$@
372 local service=""
Rob Crittenden18d47782014-03-19 17:47:42 -0400373 if [ "$USE_SSL" == "False" ]; then
374 return 1
375 fi
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000376 for service in ${services}; do
377 [[ ,${SSL_ENABLED_SERVICES}, =~ ,${service}, ]] && return 0
378 done
379 return 1
380}
381
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000382# Ensure that the certificates for a service are in place. This function does
383# not check that a service is SSL enabled, this should already have been
384# completed.
385#
386# The function expects to find a certificate, key and CA certificate in the
Dean Troyerdc97cb72015-03-28 08:20:50 -0500387# variables ``{service}_SSL_CERT``, ``{service}_SSL_KEY`` and ``{service}_SSL_CA``. For
388# example for keystone this would be ``KEYSTONE_SSL_CERT``, ``KEYSTONE_SSL_KEY`` and
389# ``KEYSTONE_SSL_CA``.
Rob Crittenden18d47782014-03-19 17:47:42 -0400390#
Dean Troyerdc97cb72015-03-28 08:20:50 -0500391# If it does not find these certificates then the DevStack-issued server
Rob Crittenden18d47782014-03-19 17:47:42 -0400392# certificate, key and CA certificate will be associated with the service.
393#
394# If only some of the variables are provided then the function will quit.
Ian Wienandaee18c72014-02-21 15:35:08 +1100395function ensure_certificates {
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000396 local service=$1
397
398 local cert_var="${service}_SSL_CERT"
399 local key_var="${service}_SSL_KEY"
400 local ca_var="${service}_SSL_CA"
401
402 local cert=${!cert_var}
403 local key=${!key_var}
404 local ca=${!ca_var}
405
Rob Crittenden18d47782014-03-19 17:47:42 -0400406 if [[ -z "$cert" && -z "$key" && -z "$ca" ]]; then
407 local cert="$INT_CA_DIR/$DEVSTACK_CERT_NAME.crt"
408 local key="$INT_CA_DIR/private/$DEVSTACK_CERT_NAME.key"
409 local ca="$INT_CA_DIR/ca-chain.pem"
410 eval ${service}_SSL_CERT=\$cert
411 eval ${service}_SSL_KEY=\$key
412 eval ${service}_SSL_CA=\$ca
413 return # the CA certificate is already in the bundle
414 elif [[ -z "$cert" || -z "$key" || -z "$ca" ]]; then
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000415 die $LINENO "Missing either the ${cert_var} ${key_var} or ${ca_var}" \
416 "variable to enable SSL for ${service}"
417 fi
418
419 cat $ca >> $SSL_BUNDLE_FILE
420}
421
Rob Crittenden18d47782014-03-19 17:47:42 -0400422# Enable the mod_ssl plugin in Apache
423function enable_mod_ssl {
424 echo "Enabling mod_ssl"
425
426 if is_ubuntu; then
427 sudo a2enmod ssl
428 elif is_fedora; then
429 # Fedora enables mod_ssl by default
430 :
431 fi
432 if ! sudo `which httpd || which apache2ctl` -M | grep -w -q ssl_module; then
433 die $LINENO "mod_ssl is not enabled in apache2/httpd, please check for it manually and run stack.sh again"
434 fi
435}
436
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000437
Dean Troyerc83a7e12012-11-29 11:47:58 -0600438# Proxy Functions
439# ===============
440
441# Starts the TLS proxy for the given IP/ports
442# start_tls_proxy front-host front-port back-host back-port
Ian Wienandaee18c72014-02-21 15:35:08 +1100443function start_tls_proxy {
Gregory Haynes4b49e402016-08-31 18:19:51 -0700444 local b_service="$1-tls-proxy"
445 local f_host=$2
446 local f_port=$3
447 local b_host=$4
448 local b_port=$5
Dean Troyerc83a7e12012-11-29 11:47:58 -0600449
Gregory Haynes4b49e402016-08-31 18:19:51 -0700450 local config_file
451 config_file=$(apache_site_config_for $b_service)
452 local listen_string
453 # Default apache configs on ubuntu and centos listen on 80 and 443
454 # newer apache seems fine with duplicate listen directive but older
455 # apache does not so special case 80 and 443.
456 if [[ "$f_port" == "80" ]] || [[ "$f_port" == "443" ]]; then
457 listen_string=""
458 elif [[ "$f_host" == '*' ]] ; then
459 listen_string="Listen $f_port"
460 else
461 listen_string="Listen $f_host:$f_port"
462 fi
463 sudo bash -c "cat >$config_file" << EOF
464$listen_string
465
466<VirtualHost $f_host:$f_port>
467 SSLEngine On
468 SSLCertificateFile $DEVSTACK_CERT
469
470 <Location />
471 ProxyPass http://$b_host:$b_port/ retry=5 nocanon
472 ProxyPassReverse http://$b_host:$b_port/
473 </Location>
474</VirtualHost>
475EOF
476 for mod in ssl proxy proxy_http; do
477 enable_apache_mod $mod
478 done
479 enable_apache_site $b_service
480 # Only a reload is required to pull in new vhosts
481 # Note that a restart reliably fails on centos7 and trusty
482 # because apache can't open port 80 because the old apache
483 # still has it open. Using reload fixes trusty but centos7
484 # still doesn't work.
485 reload_apache_server
Dean Troyerc83a7e12012-11-29 11:47:58 -0600486}
Sean Dague584d90e2013-03-29 14:34:53 -0400487
Dean Troyercc6b4432013-04-08 15:38:03 -0500488
Stanislaw Pituchabd5dae02014-06-25 15:29:43 +0100489# Cleanup Functions
Dean Troyer3324f192014-09-18 09:26:39 -0500490# =================
Stanislaw Pituchabd5dae02014-06-25 15:29:43 +0100491
Gregory Haynes4b49e402016-08-31 18:19:51 -0700492# Stops the apache service. This should be done only after all services
Stanislaw Pituchabd5dae02014-06-25 15:29:43 +0100493# using tls configuration are down.
494function stop_tls_proxy {
Gregory Haynes4b49e402016-08-31 18:19:51 -0700495 stop_apache_server
Stanislaw Pituchabd5dae02014-06-25 15:29:43 +0100496}
497
Gregory Haynes4b49e402016-08-31 18:19:51 -0700498# Clean up the CA files
499# cleanup_CA
Stanislaw Pituchabd5dae02014-06-25 15:29:43 +0100500function cleanup_CA {
Gregory Haynes4b49e402016-08-31 18:19:51 -0700501 if is_fedora; then
502 sudo rm -f /usr/share/pki/ca-trust-source/anchors/devstack-chain.pem
503 sudo update-ca-trust
504 elif is_ubuntu; then
505 sudo rm -f /usr/local/share/ca-certificates/devstack-int.crt
506 sudo rm -f /usr/local/share/ca-certificates/devstack-root.crt
507 sudo update-ca-certificates
508 fi
509
Stanislaw Pituchabd5dae02014-06-25 15:29:43 +0100510 rm -rf "$DATA_DIR/CA" "$DEVSTACK_CERT"
511}
512
Adam Spiers6a5aa7c2013-10-24 11:27:02 +0100513# Tell emacs to use shell-script-mode
514## Local variables:
515## mode: shell-script
516## End: