blob: fdb798ff307a9204e6fcca5c53e5ab2a7db5996a [file] [log] [blame]
Dean Troyerc83a7e12012-11-29 11:47:58 -06001# lib/tls
2# Functions to control the configuration and operation of the TLS proxy service
3
Dean Troyerc83a7e12012-11-29 11:47:58 -06004# !! source _before_ any services that use ``SERVICE_HOST``
Adam Spiers6a5aa7c2013-10-24 11:27:02 +01005#
6# Dependencies:
7#
8# - ``functions`` file
9# - ``DEST``, ``DATA_DIR`` must be defined
10# - ``HOST_IP``, ``SERVICE_HOST``
11# - ``KEYSTONE_TOKEN_FORMAT`` must be defined
Dean Troyerc83a7e12012-11-29 11:47:58 -060012
13# Entry points:
Adam Spiers6a5aa7c2013-10-24 11:27:02 +010014#
15# - configure_CA
16# - init_CA
Rob Crittenden18d47782014-03-19 17:47:42 -040017# - cleanup_CA
Dean Troyerc83a7e12012-11-29 11:47:58 -060018
Adam Spiers6a5aa7c2013-10-24 11:27:02 +010019# - configure_proxy
20# - start_tls_proxy
Dean Troyerc83a7e12012-11-29 11:47:58 -060021
Stanislaw Pituchabd5dae02014-06-25 15:29:43 +010022# - stop_tls_proxy
23# - cleanup_CA
24
Stanislaw Pitucha2e0f0542014-06-27 16:05:53 +010025# - make_root_CA
26# - make_int_CA
27# - make_cert ca-dir cert-name "common-name" ["alt-name" ...]
Adam Spiers6a5aa7c2013-10-24 11:27:02 +010028# - start_tls_proxy HOST_IP 5000 localhost 5000
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +100029# - ensure_certificates
30# - is_ssl_enabled_service
Rob Crittenden18d47782014-03-19 17:47:42 -040031# - enable_mod_ssl
Dean Troyerc83a7e12012-11-29 11:47:58 -060032
Dean Troyercc6b4432013-04-08 15:38:03 -050033# Defaults
34# --------
35
Dean Troyerc83a7e12012-11-29 11:47:58 -060036if is_service_enabled tls-proxy; then
37 # TODO(dtroyer): revisit this below after the search for HOST_IP has been done
38 TLS_IP=${TLS_IP:-$SERVICE_IP}
Dean Troyerc83a7e12012-11-29 11:47:58 -060039fi
40
Rob Crittenden18d47782014-03-19 17:47:42 -040041DEVSTACK_HOSTNAME=$(hostname -f)
Dean Troyerc83a7e12012-11-29 11:47:58 -060042DEVSTACK_CERT_NAME=devstack-cert
43DEVSTACK_CERT=$DATA_DIR/$DEVSTACK_CERT_NAME.pem
44
45# CA configuration
46ROOT_CA_DIR=${ROOT_CA_DIR:-$DATA_DIR/CA/root-ca}
47INT_CA_DIR=${INT_CA_DIR:-$DATA_DIR/CA/int-ca}
48
49ORG_NAME="OpenStack"
50ORG_UNIT_NAME="DevStack"
51
52# Stud configuration
53STUD_PROTO="--tls"
54STUD_CIPHERS='TLSv1+HIGH:!DES:!aNULL:!eNULL:@STRENGTH'
55
56
57# CA Functions
58# ============
59
60# There may be more than one, get specific
61OPENSSL=${OPENSSL:-/usr/bin/openssl}
62
63# Do primary CA configuration
Ian Wienandaee18c72014-02-21 15:35:08 +110064function configure_CA {
Dean Troyerc83a7e12012-11-29 11:47:58 -060065 # build common config file
66
67 # Verify ``TLS_IP`` is good
68 if [[ -n "$HOST_IP" && "$HOST_IP" != "$TLS_IP" ]]; then
69 # auto-discover has changed the IP
70 TLS_IP=$HOST_IP
71 fi
72}
73
74# Creates a new CA directory structure
75# create_CA_base ca-dir
Ian Wienandaee18c72014-02-21 15:35:08 +110076function create_CA_base {
Dean Troyerc83a7e12012-11-29 11:47:58 -060077 local ca_dir=$1
78
79 if [[ -d $ca_dir ]]; then
80 # Bail out it exists
81 return 0
82 fi
83
Dean Troyerb1e3d0f2014-07-25 14:57:54 -050084 local i
Dean Troyerc83a7e12012-11-29 11:47:58 -060085 for i in certs crl newcerts private; do
86 mkdir -p $ca_dir/$i
87 done
88 chmod 710 $ca_dir/private
89 echo "01" >$ca_dir/serial
90 cp /dev/null $ca_dir/index.txt
91}
92
93
94# Create a new CA configuration file
95# create_CA_config ca-dir common-name
Ian Wienandaee18c72014-02-21 15:35:08 +110096function create_CA_config {
Dean Troyerc83a7e12012-11-29 11:47:58 -060097 local ca_dir=$1
98 local common_name=$2
99
100 echo "
101[ ca ]
102default_ca = CA_default
103
104[ CA_default ]
105dir = $ca_dir
106policy = policy_match
107database = \$dir/index.txt
108serial = \$dir/serial
109certs = \$dir/certs
110crl_dir = \$dir/crl
111new_certs_dir = \$dir/newcerts
112certificate = \$dir/cacert.pem
113private_key = \$dir/private/cacert.key
114RANDFILE = \$dir/private/.rand
115default_md = default
116
117[ req ]
118default_bits = 1024
119default_md = sha1
120
121prompt = no
122distinguished_name = ca_distinguished_name
123
124x509_extensions = ca_extensions
125
126[ ca_distinguished_name ]
127organizationName = $ORG_NAME
128organizationalUnitName = $ORG_UNIT_NAME Certificate Authority
129commonName = $common_name
130
131[ policy_match ]
132countryName = optional
133stateOrProvinceName = optional
134organizationName = match
135organizationalUnitName = optional
136commonName = supplied
137
138[ ca_extensions ]
139basicConstraints = critical,CA:true
140subjectKeyIdentifier = hash
141authorityKeyIdentifier = keyid:always, issuer
142keyUsage = cRLSign, keyCertSign
143
144" >$ca_dir/ca.conf
145}
146
147# Create a new signing configuration file
148# create_signing_config ca-dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100149function create_signing_config {
Dean Troyerc83a7e12012-11-29 11:47:58 -0600150 local ca_dir=$1
151
152 echo "
153[ ca ]
154default_ca = CA_default
155
156[ CA_default ]
157dir = $ca_dir
158policy = policy_match
159database = \$dir/index.txt
160serial = \$dir/serial
161certs = \$dir/certs
162crl_dir = \$dir/crl
163new_certs_dir = \$dir/newcerts
164certificate = \$dir/cacert.pem
165private_key = \$dir/private/cacert.key
166RANDFILE = \$dir/private/.rand
167default_md = default
168
169[ req ]
170default_bits = 1024
171default_md = sha1
172
173prompt = no
174distinguished_name = req_distinguished_name
175
176x509_extensions = req_extensions
177
178[ req_distinguished_name ]
179organizationName = $ORG_NAME
180organizationalUnitName = $ORG_UNIT_NAME Server Farm
181
182[ policy_match ]
183countryName = optional
184stateOrProvinceName = optional
185organizationName = match
186organizationalUnitName = optional
187commonName = supplied
188
189[ req_extensions ]
190basicConstraints = CA:false
191subjectKeyIdentifier = hash
192authorityKeyIdentifier = keyid:always, issuer
193keyUsage = digitalSignature, keyEncipherment, keyAgreement
194extendedKeyUsage = serverAuth, clientAuth
195subjectAltName = \$ENV::SUBJECT_ALT_NAME
196
197" >$ca_dir/signing.conf
198}
199
Dean Troyerca802172013-01-09 19:08:02 -0600200# Create root and intermediate CAs
Dean Troyerc83a7e12012-11-29 11:47:58 -0600201# init_CA
202function init_CA {
203 # Ensure CAs are built
204 make_root_CA $ROOT_CA_DIR
205 make_int_CA $INT_CA_DIR $ROOT_CA_DIR
206
207 # Create the CA bundle
208 cat $ROOT_CA_DIR/cacert.pem $INT_CA_DIR/cacert.pem >>$INT_CA_DIR/ca-chain.pem
Rob Crittenden18d47782014-03-19 17:47:42 -0400209 cat $INT_CA_DIR/ca-chain.pem >> $SSL_BUNDLE_FILE
210
211 if is_fedora; then
212 sudo cp $INT_CA_DIR/ca-chain.pem /usr/share/pki/ca-trust-source/anchors/devstack-chain.pem
213 sudo update-ca-trust
214 elif is_ubuntu; then
215 sudo cp $INT_CA_DIR/ca-chain.pem /usr/local/share/ca-certificates/devstack-int.crt
216 sudo cp $ROOT_CA_DIR/cacert.pem /usr/local/share/ca-certificates/devstack-root.crt
217 sudo update-ca-certificates
218 fi
219}
220
221# Clean up the CA files
222# cleanup_CA
223function cleanup_CA {
224 if is_fedora; then
225 sudo rm -f /usr/share/pki/ca-trust-source/anchors/devstack-chain.pem
226 sudo update-ca-trust
227 elif is_ubuntu; then
228 sudo rm -f /usr/local/share/ca-certificates/devstack-int.crt
229 sudo rm -f /usr/local/share/ca-certificates/devstack-root.crt
230 sudo update-ca-certificates
231 fi
Dean Troyerca802172013-01-09 19:08:02 -0600232}
Dean Troyerc83a7e12012-11-29 11:47:58 -0600233
Dean Troyerca802172013-01-09 19:08:02 -0600234# Create an initial server cert
235# init_cert
236function init_cert {
Dean Troyerc83a7e12012-11-29 11:47:58 -0600237 if [[ ! -r $DEVSTACK_CERT ]]; then
238 if [[ -n "$TLS_IP" ]]; then
239 # Lie to let incomplete match routines work
240 TLS_IP="DNS:$TLS_IP"
241 fi
242 make_cert $INT_CA_DIR $DEVSTACK_CERT_NAME $DEVSTACK_HOSTNAME "$TLS_IP"
243
244 # Create a cert bundle
245 cat $INT_CA_DIR/private/$DEVSTACK_CERT_NAME.key $INT_CA_DIR/$DEVSTACK_CERT_NAME.crt $INT_CA_DIR/cacert.pem >$DEVSTACK_CERT
246 fi
247}
248
249
250# make_cert creates and signs a new certificate with the given commonName and CA
251# make_cert ca-dir cert-name "common-name" ["alt-name" ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100252function make_cert {
Dean Troyerc83a7e12012-11-29 11:47:58 -0600253 local ca_dir=$1
254 local cert_name=$2
255 local common_name=$3
256 local alt_names=$4
257
Stanislaw Pitucha2f69c6b2014-06-25 15:07:48 +0100258 # Only generate the certificate if it doesn't exist yet on the disk
259 if [ ! -r "$ca_dir/$cert_name.crt" ]; then
260 # Generate a signing request
261 $OPENSSL req \
262 -sha1 \
263 -newkey rsa \
264 -nodes \
265 -keyout $ca_dir/private/$cert_name.key \
266 -out $ca_dir/$cert_name.csr \
267 -subj "/O=${ORG_NAME}/OU=${ORG_UNIT_NAME} Servers/CN=${common_name}"
Dean Troyerc83a7e12012-11-29 11:47:58 -0600268
Stanislaw Pitucha2f69c6b2014-06-25 15:07:48 +0100269 if [[ -z "$alt_names" ]]; then
270 alt_names="DNS:${common_name}"
271 else
272 alt_names="DNS:${common_name},${alt_names}"
273 fi
274
275 # Sign the request valid for 1 year
276 SUBJECT_ALT_NAME="$alt_names" \
277 $OPENSSL ca -config $ca_dir/signing.conf \
278 -extensions req_extensions \
279 -days 365 \
280 -notext \
281 -in $ca_dir/$cert_name.csr \
282 -out $ca_dir/$cert_name.crt \
283 -subj "/O=${ORG_NAME}/OU=${ORG_UNIT_NAME} Servers/CN=${common_name}" \
284 -batch
Dean Troyerc83a7e12012-11-29 11:47:58 -0600285 fi
Dean Troyerc83a7e12012-11-29 11:47:58 -0600286}
287
288
289# Make an intermediate CA to sign everything else
290# make_int_CA ca-dir signing-ca-dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100291function make_int_CA {
Dean Troyerc83a7e12012-11-29 11:47:58 -0600292 local ca_dir=$1
293 local signing_ca_dir=$2
294
295 # Create the root CA
296 create_CA_base $ca_dir
297 create_CA_config $ca_dir 'Intermediate CA'
298 create_signing_config $ca_dir
299
Stanislaw Pitucha2f69c6b2014-06-25 15:07:48 +0100300 if [ ! -r "$ca_dir/cacert.pem" ]; then
301 # Create a signing certificate request
302 $OPENSSL req -config $ca_dir/ca.conf \
303 -sha1 \
304 -newkey rsa \
305 -nodes \
306 -keyout $ca_dir/private/cacert.key \
307 -out $ca_dir/cacert.csr \
308 -outform PEM
Dean Troyerc83a7e12012-11-29 11:47:58 -0600309
Stanislaw Pitucha2f69c6b2014-06-25 15:07:48 +0100310 # Sign the intermediate request valid for 1 year
311 $OPENSSL ca -config $signing_ca_dir/ca.conf \
312 -extensions ca_extensions \
313 -days 365 \
314 -notext \
315 -in $ca_dir/cacert.csr \
316 -out $ca_dir/cacert.pem \
317 -batch
318 fi
Dean Troyerc83a7e12012-11-29 11:47:58 -0600319}
320
321# Make a root CA to sign other CAs
322# make_root_CA ca-dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100323function make_root_CA {
Dean Troyerc83a7e12012-11-29 11:47:58 -0600324 local ca_dir=$1
325
326 # Create the root CA
327 create_CA_base $ca_dir
328 create_CA_config $ca_dir 'Root CA'
329
330 # Create a self-signed certificate valid for 5 years
331 $OPENSSL req -config $ca_dir/ca.conf \
332 -x509 \
333 -nodes \
334 -newkey rsa \
335 -days 21360 \
336 -keyout $ca_dir/private/cacert.key \
337 -out $ca_dir/cacert.pem \
338 -outform PEM
339}
340
341
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000342# Certificate Input Configuration
343# ===============================
344
345# check to see if the service(s) specified are to be SSL enabled.
346#
347# Multiple services specified as arguments are ``OR``'ed together; the test
348# is a short-circuit boolean, i.e it returns on the first match.
349#
350# Uses global ``SSL_ENABLED_SERVICES``
Ian Wienandaee18c72014-02-21 15:35:08 +1100351function is_ssl_enabled_service {
Sean Daguef0bd8db2014-07-23 15:14:07 -0400352 local services=$@
353 local service=""
Rob Crittenden18d47782014-03-19 17:47:42 -0400354 if [ "$USE_SSL" == "False" ]; then
355 return 1
356 fi
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000357 for service in ${services}; do
358 [[ ,${SSL_ENABLED_SERVICES}, =~ ,${service}, ]] && return 0
359 done
360 return 1
361}
362
363
364# Ensure that the certificates for a service are in place. This function does
365# not check that a service is SSL enabled, this should already have been
366# completed.
367#
368# The function expects to find a certificate, key and CA certificate in the
369# variables {service}_SSL_CERT, {service}_SSL_KEY and {service}_SSL_CA. For
370# example for keystone this would be KEYSTONE_SSL_CERT, KEYSTONE_SSL_KEY and
Rob Crittenden18d47782014-03-19 17:47:42 -0400371# KEYSTONE_SSL_CA.
372#
373# If it does not find these certificates then the devstack-issued server
374# certificate, key and CA certificate will be associated with the service.
375#
376# If only some of the variables are provided then the function will quit.
Ian Wienandaee18c72014-02-21 15:35:08 +1100377function ensure_certificates {
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000378 local service=$1
379
380 local cert_var="${service}_SSL_CERT"
381 local key_var="${service}_SSL_KEY"
382 local ca_var="${service}_SSL_CA"
383
384 local cert=${!cert_var}
385 local key=${!key_var}
386 local ca=${!ca_var}
387
Rob Crittenden18d47782014-03-19 17:47:42 -0400388 if [[ -z "$cert" && -z "$key" && -z "$ca" ]]; then
389 local cert="$INT_CA_DIR/$DEVSTACK_CERT_NAME.crt"
390 local key="$INT_CA_DIR/private/$DEVSTACK_CERT_NAME.key"
391 local ca="$INT_CA_DIR/ca-chain.pem"
392 eval ${service}_SSL_CERT=\$cert
393 eval ${service}_SSL_KEY=\$key
394 eval ${service}_SSL_CA=\$ca
395 return # the CA certificate is already in the bundle
396 elif [[ -z "$cert" || -z "$key" || -z "$ca" ]]; then
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000397 die $LINENO "Missing either the ${cert_var} ${key_var} or ${ca_var}" \
398 "variable to enable SSL for ${service}"
399 fi
400
401 cat $ca >> $SSL_BUNDLE_FILE
402}
403
Rob Crittenden18d47782014-03-19 17:47:42 -0400404# Enable the mod_ssl plugin in Apache
405function enable_mod_ssl {
406 echo "Enabling mod_ssl"
407
408 if is_ubuntu; then
409 sudo a2enmod ssl
410 elif is_fedora; then
411 # Fedora enables mod_ssl by default
412 :
413 fi
414 if ! sudo `which httpd || which apache2ctl` -M | grep -w -q ssl_module; then
415 die $LINENO "mod_ssl is not enabled in apache2/httpd, please check for it manually and run stack.sh again"
416 fi
417}
418
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000419
Dean Troyerc83a7e12012-11-29 11:47:58 -0600420# Proxy Functions
421# ===============
422
423# Starts the TLS proxy for the given IP/ports
424# start_tls_proxy front-host front-port back-host back-port
Ian Wienandaee18c72014-02-21 15:35:08 +1100425function start_tls_proxy {
Dean Troyerc83a7e12012-11-29 11:47:58 -0600426 local f_host=$1
427 local f_port=$2
428 local b_host=$3
429 local b_port=$4
430
431 stud $STUD_PROTO -f $f_host,$f_port -b $b_host,$b_port $DEVSTACK_CERT 2>/dev/null
432}
Sean Dague584d90e2013-03-29 14:34:53 -0400433
Dean Troyercc6b4432013-04-08 15:38:03 -0500434
Stanislaw Pituchabd5dae02014-06-25 15:29:43 +0100435# Cleanup Functions
Dean Troyer3324f192014-09-18 09:26:39 -0500436# =================
Stanislaw Pituchabd5dae02014-06-25 15:29:43 +0100437
438
439# Stops all stud processes. This should be done only after all services
440# using tls configuration are down.
441function stop_tls_proxy {
442 killall stud
443}
444
445
446# Remove CA along with configuration, as well as the local server certificate
447function cleanup_CA {
448 rm -rf "$DATA_DIR/CA" "$DEVSTACK_CERT"
449}
450
Adam Spiers6a5aa7c2013-10-24 11:27:02 +0100451# Tell emacs to use shell-script-mode
452## Local variables:
453## mode: shell-script
454## End: