blob: ca57ed44e0b7e397197f136a5ca9cf0a728d4e5b [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
Rob Crittenden18d47782014-03-19 17:47:42 -040019# - cleanup_CA
Dean Troyerc83a7e12012-11-29 11:47:58 -060020
Adam Spiers6a5aa7c2013-10-24 11:27:02 +010021# - configure_proxy
22# - start_tls_proxy
Dean Troyerc83a7e12012-11-29 11:47:58 -060023
Stanislaw Pituchabd5dae02014-06-25 15:29:43 +010024# - stop_tls_proxy
25# - cleanup_CA
26
Stanislaw Pitucha2e0f0542014-06-27 16:05:53 +010027# - make_root_CA
28# - make_int_CA
29# - make_cert ca-dir cert-name "common-name" ["alt-name" ...]
Adam Spiers6a5aa7c2013-10-24 11:27:02 +010030# - start_tls_proxy HOST_IP 5000 localhost 5000
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +100031# - ensure_certificates
32# - is_ssl_enabled_service
Rob Crittenden18d47782014-03-19 17:47:42 -040033# - enable_mod_ssl
Dean Troyerc83a7e12012-11-29 11:47:58 -060034
Dean Troyerdc97cb72015-03-28 08:20:50 -050035
Dean Troyercc6b4432013-04-08 15:38:03 -050036# Defaults
37# --------
38
Dean Troyerc83a7e12012-11-29 11:47:58 -060039if is_service_enabled tls-proxy; then
40 # TODO(dtroyer): revisit this below after the search for HOST_IP has been done
41 TLS_IP=${TLS_IP:-$SERVICE_IP}
Dean Troyerc83a7e12012-11-29 11:47:58 -060042fi
43
Rob Crittenden18d47782014-03-19 17:47:42 -040044DEVSTACK_HOSTNAME=$(hostname -f)
Dean Troyerc83a7e12012-11-29 11:47:58 -060045DEVSTACK_CERT_NAME=devstack-cert
46DEVSTACK_CERT=$DATA_DIR/$DEVSTACK_CERT_NAME.pem
47
48# CA configuration
49ROOT_CA_DIR=${ROOT_CA_DIR:-$DATA_DIR/CA/root-ca}
50INT_CA_DIR=${INT_CA_DIR:-$DATA_DIR/CA/int-ca}
51
52ORG_NAME="OpenStack"
53ORG_UNIT_NAME="DevStack"
54
55# Stud configuration
56STUD_PROTO="--tls"
57STUD_CIPHERS='TLSv1+HIGH:!DES:!aNULL:!eNULL:@STRENGTH'
58
59
60# CA Functions
61# ============
62
63# There may be more than one, get specific
64OPENSSL=${OPENSSL:-/usr/bin/openssl}
65
66# Do primary CA configuration
Ian Wienandaee18c72014-02-21 15:35:08 +110067function configure_CA {
Dean Troyerc83a7e12012-11-29 11:47:58 -060068 # build common config file
69
70 # Verify ``TLS_IP`` is good
71 if [[ -n "$HOST_IP" && "$HOST_IP" != "$TLS_IP" ]]; then
72 # auto-discover has changed the IP
73 TLS_IP=$HOST_IP
74 fi
75}
76
77# Creates a new CA directory structure
78# create_CA_base ca-dir
Ian Wienandaee18c72014-02-21 15:35:08 +110079function create_CA_base {
Dean Troyerc83a7e12012-11-29 11:47:58 -060080 local ca_dir=$1
81
82 if [[ -d $ca_dir ]]; then
83 # Bail out it exists
84 return 0
85 fi
86
Dean Troyerb1e3d0f2014-07-25 14:57:54 -050087 local i
Dean Troyerc83a7e12012-11-29 11:47:58 -060088 for i in certs crl newcerts private; do
89 mkdir -p $ca_dir/$i
90 done
91 chmod 710 $ca_dir/private
92 echo "01" >$ca_dir/serial
93 cp /dev/null $ca_dir/index.txt
94}
95
Dean Troyerc83a7e12012-11-29 11:47:58 -060096# Create a new CA configuration file
97# create_CA_config ca-dir common-name
Ian Wienandaee18c72014-02-21 15:35:08 +110098function create_CA_config {
Dean Troyerc83a7e12012-11-29 11:47:58 -060099 local ca_dir=$1
100 local common_name=$2
101
102 echo "
103[ ca ]
104default_ca = CA_default
105
106[ CA_default ]
107dir = $ca_dir
108policy = policy_match
109database = \$dir/index.txt
110serial = \$dir/serial
111certs = \$dir/certs
112crl_dir = \$dir/crl
113new_certs_dir = \$dir/newcerts
114certificate = \$dir/cacert.pem
115private_key = \$dir/private/cacert.key
116RANDFILE = \$dir/private/.rand
117default_md = default
118
119[ req ]
120default_bits = 1024
121default_md = sha1
122
123prompt = no
124distinguished_name = ca_distinguished_name
125
126x509_extensions = ca_extensions
127
128[ ca_distinguished_name ]
129organizationName = $ORG_NAME
130organizationalUnitName = $ORG_UNIT_NAME Certificate Authority
131commonName = $common_name
132
133[ policy_match ]
134countryName = optional
135stateOrProvinceName = optional
136organizationName = match
137organizationalUnitName = optional
138commonName = supplied
139
140[ ca_extensions ]
141basicConstraints = critical,CA:true
142subjectKeyIdentifier = hash
143authorityKeyIdentifier = keyid:always, issuer
144keyUsage = cRLSign, keyCertSign
145
146" >$ca_dir/ca.conf
147}
148
149# Create a new signing configuration file
150# create_signing_config ca-dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100151function create_signing_config {
Dean Troyerc83a7e12012-11-29 11:47:58 -0600152 local ca_dir=$1
153
154 echo "
155[ ca ]
156default_ca = CA_default
157
158[ CA_default ]
159dir = $ca_dir
160policy = policy_match
161database = \$dir/index.txt
162serial = \$dir/serial
163certs = \$dir/certs
164crl_dir = \$dir/crl
165new_certs_dir = \$dir/newcerts
166certificate = \$dir/cacert.pem
167private_key = \$dir/private/cacert.key
168RANDFILE = \$dir/private/.rand
169default_md = default
170
171[ req ]
172default_bits = 1024
173default_md = sha1
174
175prompt = no
176distinguished_name = req_distinguished_name
177
178x509_extensions = req_extensions
179
180[ req_distinguished_name ]
181organizationName = $ORG_NAME
182organizationalUnitName = $ORG_UNIT_NAME Server Farm
183
184[ policy_match ]
185countryName = optional
186stateOrProvinceName = optional
187organizationName = match
188organizationalUnitName = optional
189commonName = supplied
190
191[ req_extensions ]
192basicConstraints = CA:false
193subjectKeyIdentifier = hash
194authorityKeyIdentifier = keyid:always, issuer
195keyUsage = digitalSignature, keyEncipherment, keyAgreement
196extendedKeyUsage = serverAuth, clientAuth
197subjectAltName = \$ENV::SUBJECT_ALT_NAME
198
199" >$ca_dir/signing.conf
200}
201
Dean Troyerca802172013-01-09 19:08:02 -0600202# Create root and intermediate CAs
Dean Troyerc83a7e12012-11-29 11:47:58 -0600203# init_CA
204function init_CA {
Rob Crittenden1987fcc2015-06-10 11:00:59 -0400205 fix_system_ca_bundle_path
Dean Troyerc83a7e12012-11-29 11:47:58 -0600206 # Ensure CAs are built
207 make_root_CA $ROOT_CA_DIR
208 make_int_CA $INT_CA_DIR $ROOT_CA_DIR
209
210 # Create the CA bundle
211 cat $ROOT_CA_DIR/cacert.pem $INT_CA_DIR/cacert.pem >>$INT_CA_DIR/ca-chain.pem
Rob Crittenden18d47782014-03-19 17:47:42 -0400212 cat $INT_CA_DIR/ca-chain.pem >> $SSL_BUNDLE_FILE
213
214 if is_fedora; then
215 sudo cp $INT_CA_DIR/ca-chain.pem /usr/share/pki/ca-trust-source/anchors/devstack-chain.pem
216 sudo update-ca-trust
217 elif is_ubuntu; then
218 sudo cp $INT_CA_DIR/ca-chain.pem /usr/local/share/ca-certificates/devstack-int.crt
219 sudo cp $ROOT_CA_DIR/cacert.pem /usr/local/share/ca-certificates/devstack-root.crt
220 sudo update-ca-certificates
221 fi
222}
223
224# Clean up the CA files
225# cleanup_CA
226function cleanup_CA {
227 if is_fedora; then
228 sudo rm -f /usr/share/pki/ca-trust-source/anchors/devstack-chain.pem
229 sudo update-ca-trust
230 elif is_ubuntu; then
231 sudo rm -f /usr/local/share/ca-certificates/devstack-int.crt
232 sudo rm -f /usr/local/share/ca-certificates/devstack-root.crt
233 sudo update-ca-certificates
234 fi
Dean Troyerca802172013-01-09 19:08:02 -0600235}
Dean Troyerc83a7e12012-11-29 11:47:58 -0600236
Dean Troyerca802172013-01-09 19:08:02 -0600237# Create an initial server cert
238# init_cert
239function init_cert {
Dean Troyerc83a7e12012-11-29 11:47:58 -0600240 if [[ ! -r $DEVSTACK_CERT ]]; then
241 if [[ -n "$TLS_IP" ]]; then
242 # Lie to let incomplete match routines work
243 TLS_IP="DNS:$TLS_IP"
244 fi
245 make_cert $INT_CA_DIR $DEVSTACK_CERT_NAME $DEVSTACK_HOSTNAME "$TLS_IP"
246
247 # Create a cert bundle
248 cat $INT_CA_DIR/private/$DEVSTACK_CERT_NAME.key $INT_CA_DIR/$DEVSTACK_CERT_NAME.crt $INT_CA_DIR/cacert.pem >$DEVSTACK_CERT
249 fi
250}
251
Dean Troyerc83a7e12012-11-29 11:47:58 -0600252# make_cert creates and signs a new certificate with the given commonName and CA
253# make_cert ca-dir cert-name "common-name" ["alt-name" ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100254function make_cert {
Dean Troyerc83a7e12012-11-29 11:47:58 -0600255 local ca_dir=$1
256 local cert_name=$2
257 local common_name=$3
258 local alt_names=$4
259
Rob Crittendenbe00e952016-03-24 18:09:22 -0400260 if [ "$common_name" != "$SERVICE_HOST" ]; then
261 if [[ -z "$alt_names" ]]; then
262 alt_names="DNS:$SERVICE_HOST"
263 else
264 alt_names="$alt_names,DNS:$SERVICE_HOST"
265 fi
266 fi
267
Stanislaw Pitucha2f69c6b2014-06-25 15:07:48 +0100268 # Only generate the certificate if it doesn't exist yet on the disk
269 if [ ! -r "$ca_dir/$cert_name.crt" ]; then
270 # Generate a signing request
271 $OPENSSL req \
272 -sha1 \
273 -newkey rsa \
274 -nodes \
275 -keyout $ca_dir/private/$cert_name.key \
276 -out $ca_dir/$cert_name.csr \
277 -subj "/O=${ORG_NAME}/OU=${ORG_UNIT_NAME} Servers/CN=${common_name}"
Dean Troyerc83a7e12012-11-29 11:47:58 -0600278
Stanislaw Pitucha2f69c6b2014-06-25 15:07:48 +0100279 if [[ -z "$alt_names" ]]; then
280 alt_names="DNS:${common_name}"
281 else
282 alt_names="DNS:${common_name},${alt_names}"
283 fi
284
285 # Sign the request valid for 1 year
286 SUBJECT_ALT_NAME="$alt_names" \
287 $OPENSSL ca -config $ca_dir/signing.conf \
288 -extensions req_extensions \
289 -days 365 \
290 -notext \
291 -in $ca_dir/$cert_name.csr \
292 -out $ca_dir/$cert_name.crt \
293 -subj "/O=${ORG_NAME}/OU=${ORG_UNIT_NAME} Servers/CN=${common_name}" \
294 -batch
Dean Troyerc83a7e12012-11-29 11:47:58 -0600295 fi
Dean Troyerc83a7e12012-11-29 11:47:58 -0600296}
297
Dean Troyerc83a7e12012-11-29 11:47:58 -0600298# Make an intermediate CA to sign everything else
299# make_int_CA ca-dir signing-ca-dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100300function make_int_CA {
Dean Troyerc83a7e12012-11-29 11:47:58 -0600301 local ca_dir=$1
302 local signing_ca_dir=$2
303
304 # Create the root CA
305 create_CA_base $ca_dir
306 create_CA_config $ca_dir 'Intermediate CA'
307 create_signing_config $ca_dir
308
Stanislaw Pitucha2f69c6b2014-06-25 15:07:48 +0100309 if [ ! -r "$ca_dir/cacert.pem" ]; then
310 # Create a signing certificate request
311 $OPENSSL req -config $ca_dir/ca.conf \
312 -sha1 \
313 -newkey rsa \
314 -nodes \
315 -keyout $ca_dir/private/cacert.key \
316 -out $ca_dir/cacert.csr \
317 -outform PEM
Dean Troyerc83a7e12012-11-29 11:47:58 -0600318
Stanislaw Pitucha2f69c6b2014-06-25 15:07:48 +0100319 # Sign the intermediate request valid for 1 year
320 $OPENSSL ca -config $signing_ca_dir/ca.conf \
321 -extensions ca_extensions \
322 -days 365 \
323 -notext \
324 -in $ca_dir/cacert.csr \
325 -out $ca_dir/cacert.pem \
326 -batch
327 fi
Dean Troyerc83a7e12012-11-29 11:47:58 -0600328}
329
330# Make a root CA to sign other CAs
331# make_root_CA ca-dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100332function make_root_CA {
Dean Troyerc83a7e12012-11-29 11:47:58 -0600333 local ca_dir=$1
334
335 # Create the root CA
336 create_CA_base $ca_dir
337 create_CA_config $ca_dir 'Root CA'
338
339 # Create a self-signed certificate valid for 5 years
340 $OPENSSL req -config $ca_dir/ca.conf \
341 -x509 \
342 -nodes \
343 -newkey rsa \
344 -days 21360 \
345 -keyout $ca_dir/private/cacert.key \
346 -out $ca_dir/cacert.pem \
347 -outform PEM
348}
349
Rob Crittenden1987fcc2015-06-10 11:00:59 -0400350# If a non-system python-requests is installed then it will use the
351# built-in CA certificate store rather than the distro-specific
352# CA certificate store. Detect this and symlink to the correct
353# one. If the value for the CA is not rooted in /etc then we know
354# we need to change it.
355function fix_system_ca_bundle_path {
356 if is_service_enabled tls-proxy || [ "$USE_SSL" == "True" ]; then
Ian Wienandada886d2015-10-07 14:06:26 +1100357 local capath
358 capath=$(python -c $'try:\n from requests import certs\n print certs.where()\nexcept ImportError: pass')
Rob Crittenden1987fcc2015-06-10 11:00:59 -0400359
360 if [[ ! $capath == "" && ! $capath =~ ^/etc/.* && ! -L $capath ]]; then
361 if is_fedora; then
362 sudo rm -f $capath
363 sudo ln -s /etc/pki/tls/certs/ca-bundle.crt $capath
364 elif is_ubuntu; then
365 sudo rm -f $capath
366 sudo ln -s /etc/ssl/certs/ca-certificates.crt $capath
367 else
368 echo "Don't know how to set the CA bundle, expect the install to fail."
369 fi
370 fi
371 fi
372}
373
Dean Troyerc83a7e12012-11-29 11:47:58 -0600374
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000375# Certificate Input Configuration
376# ===============================
377
378# check to see if the service(s) specified are to be SSL enabled.
379#
380# Multiple services specified as arguments are ``OR``'ed together; the test
381# is a short-circuit boolean, i.e it returns on the first match.
382#
383# Uses global ``SSL_ENABLED_SERVICES``
Ian Wienandaee18c72014-02-21 15:35:08 +1100384function is_ssl_enabled_service {
Sean Daguef0bd8db2014-07-23 15:14:07 -0400385 local services=$@
386 local service=""
Rob Crittenden18d47782014-03-19 17:47:42 -0400387 if [ "$USE_SSL" == "False" ]; then
388 return 1
389 fi
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000390 for service in ${services}; do
391 [[ ,${SSL_ENABLED_SERVICES}, =~ ,${service}, ]] && return 0
392 done
393 return 1
394}
395
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000396# Ensure that the certificates for a service are in place. This function does
397# not check that a service is SSL enabled, this should already have been
398# completed.
399#
400# The function expects to find a certificate, key and CA certificate in the
Dean Troyerdc97cb72015-03-28 08:20:50 -0500401# variables ``{service}_SSL_CERT``, ``{service}_SSL_KEY`` and ``{service}_SSL_CA``. For
402# example for keystone this would be ``KEYSTONE_SSL_CERT``, ``KEYSTONE_SSL_KEY`` and
403# ``KEYSTONE_SSL_CA``.
Rob Crittenden18d47782014-03-19 17:47:42 -0400404#
Dean Troyerdc97cb72015-03-28 08:20:50 -0500405# If it does not find these certificates then the DevStack-issued server
Rob Crittenden18d47782014-03-19 17:47:42 -0400406# certificate, key and CA certificate will be associated with the service.
407#
408# If only some of the variables are provided then the function will quit.
Ian Wienandaee18c72014-02-21 15:35:08 +1100409function ensure_certificates {
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000410 local service=$1
411
412 local cert_var="${service}_SSL_CERT"
413 local key_var="${service}_SSL_KEY"
414 local ca_var="${service}_SSL_CA"
415
416 local cert=${!cert_var}
417 local key=${!key_var}
418 local ca=${!ca_var}
419
Rob Crittenden18d47782014-03-19 17:47:42 -0400420 if [[ -z "$cert" && -z "$key" && -z "$ca" ]]; then
421 local cert="$INT_CA_DIR/$DEVSTACK_CERT_NAME.crt"
422 local key="$INT_CA_DIR/private/$DEVSTACK_CERT_NAME.key"
423 local ca="$INT_CA_DIR/ca-chain.pem"
424 eval ${service}_SSL_CERT=\$cert
425 eval ${service}_SSL_KEY=\$key
426 eval ${service}_SSL_CA=\$ca
427 return # the CA certificate is already in the bundle
428 elif [[ -z "$cert" || -z "$key" || -z "$ca" ]]; then
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000429 die $LINENO "Missing either the ${cert_var} ${key_var} or ${ca_var}" \
430 "variable to enable SSL for ${service}"
431 fi
432
433 cat $ca >> $SSL_BUNDLE_FILE
434}
435
Rob Crittenden18d47782014-03-19 17:47:42 -0400436# Enable the mod_ssl plugin in Apache
437function enable_mod_ssl {
438 echo "Enabling mod_ssl"
439
440 if is_ubuntu; then
441 sudo a2enmod ssl
442 elif is_fedora; then
443 # Fedora enables mod_ssl by default
444 :
445 fi
446 if ! sudo `which httpd || which apache2ctl` -M | grep -w -q ssl_module; then
447 die $LINENO "mod_ssl is not enabled in apache2/httpd, please check for it manually and run stack.sh again"
448 fi
449}
450
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000451
Dean Troyerc83a7e12012-11-29 11:47:58 -0600452# Proxy Functions
453# ===============
454
455# Starts the TLS proxy for the given IP/ports
456# start_tls_proxy front-host front-port back-host back-port
Ian Wienandaee18c72014-02-21 15:35:08 +1100457function start_tls_proxy {
Dean Troyerc83a7e12012-11-29 11:47:58 -0600458 local f_host=$1
459 local f_port=$2
460 local b_host=$3
461 local b_port=$4
462
463 stud $STUD_PROTO -f $f_host,$f_port -b $b_host,$b_port $DEVSTACK_CERT 2>/dev/null
464}
Sean Dague584d90e2013-03-29 14:34:53 -0400465
Dean Troyercc6b4432013-04-08 15:38:03 -0500466
Stanislaw Pituchabd5dae02014-06-25 15:29:43 +0100467# Cleanup Functions
Dean Troyer3324f192014-09-18 09:26:39 -0500468# =================
Stanislaw Pituchabd5dae02014-06-25 15:29:43 +0100469
Stanislaw Pituchabd5dae02014-06-25 15:29:43 +0100470# Stops all stud processes. This should be done only after all services
471# using tls configuration are down.
472function stop_tls_proxy {
473 killall stud
474}
475
Stanislaw Pituchabd5dae02014-06-25 15:29:43 +0100476# Remove CA along with configuration, as well as the local server certificate
477function cleanup_CA {
478 rm -rf "$DATA_DIR/CA" "$DEVSTACK_CERT"
479}
480
Adam Spiers6a5aa7c2013-10-24 11:27:02 +0100481# Tell emacs to use shell-script-mode
482## Local variables:
483## mode: shell-script
484## End: