blob: 677895b9b2f84a6bf681869ad17c090c7b629efb [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 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
95
96# 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 {
205 # 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
223# Clean up the CA files
224# cleanup_CA
225function cleanup_CA {
226 if is_fedora; then
227 sudo rm -f /usr/share/pki/ca-trust-source/anchors/devstack-chain.pem
228 sudo update-ca-trust
229 elif is_ubuntu; then
230 sudo rm -f /usr/local/share/ca-certificates/devstack-int.crt
231 sudo rm -f /usr/local/share/ca-certificates/devstack-root.crt
232 sudo update-ca-certificates
233 fi
Dean Troyerca802172013-01-09 19:08:02 -0600234}
Dean Troyerc83a7e12012-11-29 11:47:58 -0600235
Dean Troyerca802172013-01-09 19:08:02 -0600236# Create an initial server cert
237# init_cert
238function init_cert {
Dean Troyerc83a7e12012-11-29 11:47:58 -0600239 if [[ ! -r $DEVSTACK_CERT ]]; then
240 if [[ -n "$TLS_IP" ]]; then
241 # Lie to let incomplete match routines work
242 TLS_IP="DNS:$TLS_IP"
243 fi
244 make_cert $INT_CA_DIR $DEVSTACK_CERT_NAME $DEVSTACK_HOSTNAME "$TLS_IP"
245
246 # Create a cert bundle
247 cat $INT_CA_DIR/private/$DEVSTACK_CERT_NAME.key $INT_CA_DIR/$DEVSTACK_CERT_NAME.crt $INT_CA_DIR/cacert.pem >$DEVSTACK_CERT
248 fi
249}
250
251
252# 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
Stanislaw Pitucha2f69c6b2014-06-25 15:07:48 +0100260 # Only generate the certificate if it doesn't exist yet on the disk
261 if [ ! -r "$ca_dir/$cert_name.crt" ]; then
262 # Generate a signing request
263 $OPENSSL req \
264 -sha1 \
265 -newkey rsa \
266 -nodes \
267 -keyout $ca_dir/private/$cert_name.key \
268 -out $ca_dir/$cert_name.csr \
269 -subj "/O=${ORG_NAME}/OU=${ORG_UNIT_NAME} Servers/CN=${common_name}"
Dean Troyerc83a7e12012-11-29 11:47:58 -0600270
Stanislaw Pitucha2f69c6b2014-06-25 15:07:48 +0100271 if [[ -z "$alt_names" ]]; then
272 alt_names="DNS:${common_name}"
273 else
274 alt_names="DNS:${common_name},${alt_names}"
275 fi
276
277 # Sign the request valid for 1 year
278 SUBJECT_ALT_NAME="$alt_names" \
279 $OPENSSL ca -config $ca_dir/signing.conf \
280 -extensions req_extensions \
281 -days 365 \
282 -notext \
283 -in $ca_dir/$cert_name.csr \
284 -out $ca_dir/$cert_name.crt \
285 -subj "/O=${ORG_NAME}/OU=${ORG_UNIT_NAME} Servers/CN=${common_name}" \
286 -batch
Dean Troyerc83a7e12012-11-29 11:47:58 -0600287 fi
Dean Troyerc83a7e12012-11-29 11:47:58 -0600288}
289
290
291# Make an intermediate CA to sign everything else
292# make_int_CA ca-dir signing-ca-dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100293function make_int_CA {
Dean Troyerc83a7e12012-11-29 11:47:58 -0600294 local ca_dir=$1
295 local signing_ca_dir=$2
296
297 # Create the root CA
298 create_CA_base $ca_dir
299 create_CA_config $ca_dir 'Intermediate CA'
300 create_signing_config $ca_dir
301
Stanislaw Pitucha2f69c6b2014-06-25 15:07:48 +0100302 if [ ! -r "$ca_dir/cacert.pem" ]; then
303 # Create a signing certificate request
304 $OPENSSL req -config $ca_dir/ca.conf \
305 -sha1 \
306 -newkey rsa \
307 -nodes \
308 -keyout $ca_dir/private/cacert.key \
309 -out $ca_dir/cacert.csr \
310 -outform PEM
Dean Troyerc83a7e12012-11-29 11:47:58 -0600311
Stanislaw Pitucha2f69c6b2014-06-25 15:07:48 +0100312 # Sign the intermediate request valid for 1 year
313 $OPENSSL ca -config $signing_ca_dir/ca.conf \
314 -extensions ca_extensions \
315 -days 365 \
316 -notext \
317 -in $ca_dir/cacert.csr \
318 -out $ca_dir/cacert.pem \
319 -batch
320 fi
Dean Troyerc83a7e12012-11-29 11:47:58 -0600321}
322
323# Make a root CA to sign other CAs
324# make_root_CA ca-dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100325function make_root_CA {
Dean Troyerc83a7e12012-11-29 11:47:58 -0600326 local ca_dir=$1
327
328 # Create the root CA
329 create_CA_base $ca_dir
330 create_CA_config $ca_dir 'Root CA'
331
332 # Create a self-signed certificate valid for 5 years
333 $OPENSSL req -config $ca_dir/ca.conf \
334 -x509 \
335 -nodes \
336 -newkey rsa \
337 -days 21360 \
338 -keyout $ca_dir/private/cacert.key \
339 -out $ca_dir/cacert.pem \
340 -outform PEM
341}
342
343
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000344# Certificate Input Configuration
345# ===============================
346
347# check to see if the service(s) specified are to be SSL enabled.
348#
349# Multiple services specified as arguments are ``OR``'ed together; the test
350# is a short-circuit boolean, i.e it returns on the first match.
351#
352# Uses global ``SSL_ENABLED_SERVICES``
Ian Wienandaee18c72014-02-21 15:35:08 +1100353function is_ssl_enabled_service {
Sean Daguef0bd8db2014-07-23 15:14:07 -0400354 local services=$@
355 local service=""
Rob Crittenden18d47782014-03-19 17:47:42 -0400356 if [ "$USE_SSL" == "False" ]; then
357 return 1
358 fi
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000359 for service in ${services}; do
360 [[ ,${SSL_ENABLED_SERVICES}, =~ ,${service}, ]] && return 0
361 done
362 return 1
363}
364
365
366# Ensure that the certificates for a service are in place. This function does
367# not check that a service is SSL enabled, this should already have been
368# completed.
369#
370# The function expects to find a certificate, key and CA certificate in the
371# variables {service}_SSL_CERT, {service}_SSL_KEY and {service}_SSL_CA. For
372# example for keystone this would be KEYSTONE_SSL_CERT, KEYSTONE_SSL_KEY and
Rob Crittenden18d47782014-03-19 17:47:42 -0400373# KEYSTONE_SSL_CA.
374#
375# If it does not find these certificates then the devstack-issued server
376# certificate, key and CA certificate will be associated with the service.
377#
378# If only some of the variables are provided then the function will quit.
Ian Wienandaee18c72014-02-21 15:35:08 +1100379function ensure_certificates {
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000380 local service=$1
381
382 local cert_var="${service}_SSL_CERT"
383 local key_var="${service}_SSL_KEY"
384 local ca_var="${service}_SSL_CA"
385
386 local cert=${!cert_var}
387 local key=${!key_var}
388 local ca=${!ca_var}
389
Rob Crittenden18d47782014-03-19 17:47:42 -0400390 if [[ -z "$cert" && -z "$key" && -z "$ca" ]]; then
391 local cert="$INT_CA_DIR/$DEVSTACK_CERT_NAME.crt"
392 local key="$INT_CA_DIR/private/$DEVSTACK_CERT_NAME.key"
393 local ca="$INT_CA_DIR/ca-chain.pem"
394 eval ${service}_SSL_CERT=\$cert
395 eval ${service}_SSL_KEY=\$key
396 eval ${service}_SSL_CA=\$ca
397 return # the CA certificate is already in the bundle
398 elif [[ -z "$cert" || -z "$key" || -z "$ca" ]]; then
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000399 die $LINENO "Missing either the ${cert_var} ${key_var} or ${ca_var}" \
400 "variable to enable SSL for ${service}"
401 fi
402
403 cat $ca >> $SSL_BUNDLE_FILE
404}
405
Rob Crittenden18d47782014-03-19 17:47:42 -0400406# Enable the mod_ssl plugin in Apache
407function enable_mod_ssl {
408 echo "Enabling mod_ssl"
409
410 if is_ubuntu; then
411 sudo a2enmod ssl
412 elif is_fedora; then
413 # Fedora enables mod_ssl by default
414 :
415 fi
416 if ! sudo `which httpd || which apache2ctl` -M | grep -w -q ssl_module; then
417 die $LINENO "mod_ssl is not enabled in apache2/httpd, please check for it manually and run stack.sh again"
418 fi
419}
420
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000421
Dean Troyerc83a7e12012-11-29 11:47:58 -0600422# Proxy Functions
423# ===============
424
425# Starts the TLS proxy for the given IP/ports
426# start_tls_proxy front-host front-port back-host back-port
Ian Wienandaee18c72014-02-21 15:35:08 +1100427function start_tls_proxy {
Dean Troyerc83a7e12012-11-29 11:47:58 -0600428 local f_host=$1
429 local f_port=$2
430 local b_host=$3
431 local b_port=$4
432
433 stud $STUD_PROTO -f $f_host,$f_port -b $b_host,$b_port $DEVSTACK_CERT 2>/dev/null
434}
Sean Dague584d90e2013-03-29 14:34:53 -0400435
Dean Troyercc6b4432013-04-08 15:38:03 -0500436
Stanislaw Pituchabd5dae02014-06-25 15:29:43 +0100437# Cleanup Functions
Dean Troyer3324f192014-09-18 09:26:39 -0500438# =================
Stanislaw Pituchabd5dae02014-06-25 15:29:43 +0100439
440
441# Stops all stud processes. This should be done only after all services
442# using tls configuration are down.
443function stop_tls_proxy {
444 killall stud
445}
446
447
448# Remove CA along with configuration, as well as the local server certificate
449function cleanup_CA {
450 rm -rf "$DATA_DIR/CA" "$DEVSTACK_CERT"
451}
452
Adam Spiers6a5aa7c2013-10-24 11:27:02 +0100453# Tell emacs to use shell-script-mode
454## Local variables:
455## mode: shell-script
456## End: