| Sean Dague | e263c82 | 2014-12-05 14:25:28 -0500 | [diff] [blame] | 1 | #!/bin/bash | 
 | 2 | # | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 3 | # lib/tls | 
 | 4 | # Functions to control the configuration and operation of the TLS proxy service | 
 | 5 |  | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 6 | # !! source _before_ any services that use ``SERVICE_HOST`` | 
| Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 7 | # | 
 | 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 Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 14 |  | 
 | 15 | # Entry points: | 
| Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 16 | # | 
 | 17 | # - configure_CA | 
 | 18 | # - init_CA | 
| Rob Crittenden | 18d4778 | 2014-03-19 17:47:42 -0400 | [diff] [blame] | 19 | # - cleanup_CA | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 20 |  | 
| Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 21 | # - configure_proxy | 
 | 22 | # - start_tls_proxy | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 23 |  | 
| Stanislaw Pitucha | bd5dae0 | 2014-06-25 15:29:43 +0100 | [diff] [blame] | 24 | # - stop_tls_proxy | 
 | 25 | # - cleanup_CA | 
 | 26 |  | 
| Stanislaw Pitucha | 2e0f054 | 2014-06-27 16:05:53 +0100 | [diff] [blame] | 27 | # - make_root_CA | 
 | 28 | # - make_int_CA | 
 | 29 | # - make_cert ca-dir cert-name "common-name" ["alt-name" ...] | 
| Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 30 | # - start_tls_proxy HOST_IP 5000 localhost 5000 | 
| Jamie Lennox | bd24a8d | 2013-09-20 16:26:42 +1000 | [diff] [blame] | 31 | # - ensure_certificates | 
 | 32 | # - is_ssl_enabled_service | 
| Rob Crittenden | 18d4778 | 2014-03-19 17:47:42 -0400 | [diff] [blame] | 33 | # - enable_mod_ssl | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 34 |  | 
| Dean Troyer | dc97cb7 | 2015-03-28 08:20:50 -0500 | [diff] [blame] | 35 |  | 
| Dean Troyer | cc6b443 | 2013-04-08 15:38:03 -0500 | [diff] [blame] | 36 | # Defaults | 
 | 37 | # -------- | 
 | 38 |  | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 39 | if 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 Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 42 | fi | 
 | 43 |  | 
| Rob Crittenden | 18d4778 | 2014-03-19 17:47:42 -0400 | [diff] [blame] | 44 | DEVSTACK_HOSTNAME=$(hostname -f) | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 45 | DEVSTACK_CERT_NAME=devstack-cert | 
 | 46 | DEVSTACK_CERT=$DATA_DIR/$DEVSTACK_CERT_NAME.pem | 
 | 47 |  | 
 | 48 | # CA configuration | 
 | 49 | ROOT_CA_DIR=${ROOT_CA_DIR:-$DATA_DIR/CA/root-ca} | 
 | 50 | INT_CA_DIR=${INT_CA_DIR:-$DATA_DIR/CA/int-ca} | 
 | 51 |  | 
 | 52 | ORG_NAME="OpenStack" | 
 | 53 | ORG_UNIT_NAME="DevStack" | 
 | 54 |  | 
 | 55 | # Stud configuration | 
 | 56 | STUD_PROTO="--tls" | 
 | 57 | STUD_CIPHERS='TLSv1+HIGH:!DES:!aNULL:!eNULL:@STRENGTH' | 
 | 58 |  | 
 | 59 |  | 
 | 60 | # CA Functions | 
 | 61 | # ============ | 
 | 62 |  | 
 | 63 | # There may be more than one, get specific | 
 | 64 | OPENSSL=${OPENSSL:-/usr/bin/openssl} | 
 | 65 |  | 
 | 66 | # Do primary CA configuration | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 67 | function configure_CA { | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 68 |     # 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 Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 79 | function create_CA_base { | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 80 |     local ca_dir=$1 | 
 | 81 |  | 
 | 82 |     if [[ -d $ca_dir ]]; then | 
 | 83 |         # Bail out it exists | 
 | 84 |         return 0 | 
 | 85 |     fi | 
 | 86 |  | 
| Dean Troyer | b1e3d0f | 2014-07-25 14:57:54 -0500 | [diff] [blame] | 87 |     local i | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 88 |     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 Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 96 | # Create a new CA configuration file | 
 | 97 | # create_CA_config ca-dir common-name | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 98 | function create_CA_config { | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 99 |     local ca_dir=$1 | 
 | 100 |     local common_name=$2 | 
 | 101 |  | 
 | 102 |     echo " | 
 | 103 | [ ca ] | 
 | 104 | default_ca = CA_default | 
 | 105 |  | 
 | 106 | [ CA_default ] | 
 | 107 | dir                     = $ca_dir | 
 | 108 | policy                  = policy_match | 
 | 109 | database                = \$dir/index.txt | 
 | 110 | serial                  = \$dir/serial | 
 | 111 | certs                   = \$dir/certs | 
 | 112 | crl_dir                 = \$dir/crl | 
 | 113 | new_certs_dir           = \$dir/newcerts | 
 | 114 | certificate             = \$dir/cacert.pem | 
 | 115 | private_key             = \$dir/private/cacert.key | 
 | 116 | RANDFILE                = \$dir/private/.rand | 
 | 117 | default_md              = default | 
 | 118 |  | 
 | 119 | [ req ] | 
 | 120 | default_bits            = 1024 | 
 | 121 | default_md              = sha1 | 
 | 122 |  | 
 | 123 | prompt                  = no | 
 | 124 | distinguished_name      = ca_distinguished_name | 
 | 125 |  | 
 | 126 | x509_extensions         = ca_extensions | 
 | 127 |  | 
 | 128 | [ ca_distinguished_name ] | 
 | 129 | organizationName        = $ORG_NAME | 
 | 130 | organizationalUnitName  = $ORG_UNIT_NAME Certificate Authority | 
 | 131 | commonName              = $common_name | 
 | 132 |  | 
 | 133 | [ policy_match ] | 
 | 134 | countryName             = optional | 
 | 135 | stateOrProvinceName     = optional | 
 | 136 | organizationName        = match | 
 | 137 | organizationalUnitName  = optional | 
 | 138 | commonName              = supplied | 
 | 139 |  | 
 | 140 | [ ca_extensions ] | 
 | 141 | basicConstraints        = critical,CA:true | 
 | 142 | subjectKeyIdentifier    = hash | 
 | 143 | authorityKeyIdentifier  = keyid:always, issuer | 
 | 144 | keyUsage                = cRLSign, keyCertSign | 
 | 145 |  | 
 | 146 | " >$ca_dir/ca.conf | 
 | 147 | } | 
 | 148 |  | 
 | 149 | # Create a new signing configuration file | 
 | 150 | # create_signing_config ca-dir | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 151 | function create_signing_config { | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 152 |     local ca_dir=$1 | 
 | 153 |  | 
 | 154 |     echo " | 
 | 155 | [ ca ] | 
 | 156 | default_ca = CA_default | 
 | 157 |  | 
 | 158 | [ CA_default ] | 
 | 159 | dir                     = $ca_dir | 
 | 160 | policy                  = policy_match | 
 | 161 | database                = \$dir/index.txt | 
 | 162 | serial                  = \$dir/serial | 
 | 163 | certs                   = \$dir/certs | 
 | 164 | crl_dir                 = \$dir/crl | 
 | 165 | new_certs_dir           = \$dir/newcerts | 
 | 166 | certificate             = \$dir/cacert.pem | 
 | 167 | private_key             = \$dir/private/cacert.key | 
 | 168 | RANDFILE                = \$dir/private/.rand | 
 | 169 | default_md              = default | 
 | 170 |  | 
 | 171 | [ req ] | 
 | 172 | default_bits            = 1024 | 
 | 173 | default_md              = sha1 | 
 | 174 |  | 
 | 175 | prompt                  = no | 
 | 176 | distinguished_name      = req_distinguished_name | 
 | 177 |  | 
 | 178 | x509_extensions         = req_extensions | 
 | 179 |  | 
 | 180 | [ req_distinguished_name ] | 
 | 181 | organizationName        = $ORG_NAME | 
 | 182 | organizationalUnitName  = $ORG_UNIT_NAME Server Farm | 
 | 183 |  | 
 | 184 | [ policy_match ] | 
 | 185 | countryName             = optional | 
 | 186 | stateOrProvinceName     = optional | 
 | 187 | organizationName        = match | 
 | 188 | organizationalUnitName  = optional | 
 | 189 | commonName              = supplied | 
 | 190 |  | 
 | 191 | [ req_extensions ] | 
 | 192 | basicConstraints        = CA:false | 
 | 193 | subjectKeyIdentifier    = hash | 
 | 194 | authorityKeyIdentifier  = keyid:always, issuer | 
 | 195 | keyUsage                = digitalSignature, keyEncipherment, keyAgreement | 
 | 196 | extendedKeyUsage        = serverAuth, clientAuth | 
 | 197 | subjectAltName          = \$ENV::SUBJECT_ALT_NAME | 
 | 198 |  | 
 | 199 | " >$ca_dir/signing.conf | 
 | 200 | } | 
 | 201 |  | 
| Dean Troyer | ca80217 | 2013-01-09 19:08:02 -0600 | [diff] [blame] | 202 | # Create root and intermediate CAs | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 203 | # init_CA | 
 | 204 | function 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 Crittenden | 18d4778 | 2014-03-19 17:47:42 -0400 | [diff] [blame] | 211 |     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 | 
 | 225 | function 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 Troyer | ca80217 | 2013-01-09 19:08:02 -0600 | [diff] [blame] | 234 | } | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 235 |  | 
| Dean Troyer | ca80217 | 2013-01-09 19:08:02 -0600 | [diff] [blame] | 236 | # Create an initial server cert | 
 | 237 | # init_cert | 
 | 238 | function init_cert { | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 239 |     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 |  | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 251 | # make_cert creates and signs a new certificate with the given commonName and CA | 
 | 252 | # make_cert ca-dir cert-name "common-name" ["alt-name" ...] | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 253 | function make_cert { | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 254 |     local ca_dir=$1 | 
 | 255 |     local cert_name=$2 | 
 | 256 |     local common_name=$3 | 
 | 257 |     local alt_names=$4 | 
 | 258 |  | 
| Stanislaw Pitucha | 2f69c6b | 2014-06-25 15:07:48 +0100 | [diff] [blame] | 259 |     # Only generate the certificate if it doesn't exist yet on the disk | 
 | 260 |     if [ ! -r "$ca_dir/$cert_name.crt" ]; then | 
 | 261 |         # Generate a signing request | 
 | 262 |         $OPENSSL req \ | 
 | 263 |             -sha1 \ | 
 | 264 |             -newkey rsa \ | 
 | 265 |             -nodes \ | 
 | 266 |             -keyout $ca_dir/private/$cert_name.key \ | 
 | 267 |             -out $ca_dir/$cert_name.csr \ | 
 | 268 |             -subj "/O=${ORG_NAME}/OU=${ORG_UNIT_NAME} Servers/CN=${common_name}" | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 269 |  | 
| Stanislaw Pitucha | 2f69c6b | 2014-06-25 15:07:48 +0100 | [diff] [blame] | 270 |         if [[ -z "$alt_names" ]]; then | 
 | 271 |             alt_names="DNS:${common_name}" | 
 | 272 |         else | 
 | 273 |             alt_names="DNS:${common_name},${alt_names}" | 
 | 274 |         fi | 
 | 275 |  | 
 | 276 |         # Sign the request valid for 1 year | 
 | 277 |         SUBJECT_ALT_NAME="$alt_names" \ | 
 | 278 |         $OPENSSL ca -config $ca_dir/signing.conf \ | 
 | 279 |             -extensions req_extensions \ | 
 | 280 |             -days 365 \ | 
 | 281 |             -notext \ | 
 | 282 |             -in $ca_dir/$cert_name.csr \ | 
 | 283 |             -out $ca_dir/$cert_name.crt \ | 
 | 284 |             -subj "/O=${ORG_NAME}/OU=${ORG_UNIT_NAME} Servers/CN=${common_name}" \ | 
 | 285 |             -batch | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 286 |     fi | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 287 | } | 
 | 288 |  | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 289 | # Make an intermediate CA to sign everything else | 
 | 290 | # make_int_CA ca-dir signing-ca-dir | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 291 | function make_int_CA { | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 292 |     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 Pitucha | 2f69c6b | 2014-06-25 15:07:48 +0100 | [diff] [blame] | 300 |     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 Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 309 |  | 
| Stanislaw Pitucha | 2f69c6b | 2014-06-25 15:07:48 +0100 | [diff] [blame] | 310 |         # 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 Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 319 | } | 
 | 320 |  | 
 | 321 | # Make a root CA to sign other CAs | 
 | 322 | # make_root_CA ca-dir | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 323 | function make_root_CA { | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 324 |     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 Lennox | bd24a8d | 2013-09-20 16:26:42 +1000 | [diff] [blame] | 342 | # 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 Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 351 | function is_ssl_enabled_service { | 
| Sean Dague | f0bd8db | 2014-07-23 15:14:07 -0400 | [diff] [blame] | 352 |     local services=$@ | 
 | 353 |     local service="" | 
| Rob Crittenden | 18d4778 | 2014-03-19 17:47:42 -0400 | [diff] [blame] | 354 |     if [ "$USE_SSL" == "False" ]; then | 
 | 355 |         return 1 | 
 | 356 |     fi | 
| Jamie Lennox | bd24a8d | 2013-09-20 16:26:42 +1000 | [diff] [blame] | 357 |     for service in ${services}; do | 
 | 358 |         [[ ,${SSL_ENABLED_SERVICES}, =~ ,${service}, ]] && return 0 | 
 | 359 |     done | 
 | 360 |     return 1 | 
 | 361 | } | 
 | 362 |  | 
| Jamie Lennox | bd24a8d | 2013-09-20 16:26:42 +1000 | [diff] [blame] | 363 | # Ensure that the certificates for a service are in place. This function does | 
 | 364 | # not check that a service is SSL enabled, this should already have been | 
 | 365 | # completed. | 
 | 366 | # | 
 | 367 | # The function expects to find a certificate, key and CA certificate in the | 
| Dean Troyer | dc97cb7 | 2015-03-28 08:20:50 -0500 | [diff] [blame] | 368 | # variables ``{service}_SSL_CERT``, ``{service}_SSL_KEY`` and ``{service}_SSL_CA``. For | 
 | 369 | # example for keystone this would be ``KEYSTONE_SSL_CERT``, ``KEYSTONE_SSL_KEY`` and | 
 | 370 | # ``KEYSTONE_SSL_CA``. | 
| Rob Crittenden | 18d4778 | 2014-03-19 17:47:42 -0400 | [diff] [blame] | 371 | # | 
| Dean Troyer | dc97cb7 | 2015-03-28 08:20:50 -0500 | [diff] [blame] | 372 | # If it does not find these certificates then the DevStack-issued server | 
| Rob Crittenden | 18d4778 | 2014-03-19 17:47:42 -0400 | [diff] [blame] | 373 | # certificate, key and CA certificate will be associated with the service. | 
 | 374 | # | 
 | 375 | # If only some of the variables are provided then the function will quit. | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 376 | function ensure_certificates { | 
| Jamie Lennox | bd24a8d | 2013-09-20 16:26:42 +1000 | [diff] [blame] | 377 |     local service=$1 | 
 | 378 |  | 
 | 379 |     local cert_var="${service}_SSL_CERT" | 
 | 380 |     local key_var="${service}_SSL_KEY" | 
 | 381 |     local ca_var="${service}_SSL_CA" | 
 | 382 |  | 
 | 383 |     local cert=${!cert_var} | 
 | 384 |     local key=${!key_var} | 
 | 385 |     local ca=${!ca_var} | 
 | 386 |  | 
| Rob Crittenden | 18d4778 | 2014-03-19 17:47:42 -0400 | [diff] [blame] | 387 |     if [[ -z "$cert" && -z "$key" && -z "$ca" ]]; then | 
 | 388 |         local cert="$INT_CA_DIR/$DEVSTACK_CERT_NAME.crt" | 
 | 389 |         local key="$INT_CA_DIR/private/$DEVSTACK_CERT_NAME.key" | 
 | 390 |         local ca="$INT_CA_DIR/ca-chain.pem" | 
 | 391 |         eval ${service}_SSL_CERT=\$cert | 
 | 392 |         eval ${service}_SSL_KEY=\$key | 
 | 393 |         eval ${service}_SSL_CA=\$ca | 
 | 394 |         return # the CA certificate is already in the bundle | 
 | 395 |     elif [[ -z "$cert" || -z "$key" || -z "$ca" ]]; then | 
| Jamie Lennox | bd24a8d | 2013-09-20 16:26:42 +1000 | [diff] [blame] | 396 |         die $LINENO "Missing either the ${cert_var} ${key_var} or ${ca_var}" \ | 
 | 397 |                     "variable to enable SSL for ${service}" | 
 | 398 |     fi | 
 | 399 |  | 
 | 400 |     cat $ca >> $SSL_BUNDLE_FILE | 
 | 401 | } | 
 | 402 |  | 
| Rob Crittenden | 18d4778 | 2014-03-19 17:47:42 -0400 | [diff] [blame] | 403 | # Enable the mod_ssl plugin in Apache | 
 | 404 | function enable_mod_ssl { | 
 | 405 |     echo "Enabling mod_ssl" | 
 | 406 |  | 
 | 407 |     if is_ubuntu; then | 
 | 408 |         sudo a2enmod ssl | 
 | 409 |     elif is_fedora; then | 
 | 410 |         # Fedora enables mod_ssl by default | 
 | 411 |         : | 
 | 412 |     fi | 
 | 413 |     if ! sudo `which httpd || which apache2ctl` -M | grep -w -q ssl_module; then | 
 | 414 |         die $LINENO "mod_ssl is not enabled in apache2/httpd, please check for it manually and run stack.sh again" | 
 | 415 |     fi | 
 | 416 | } | 
 | 417 |  | 
| Jamie Lennox | bd24a8d | 2013-09-20 16:26:42 +1000 | [diff] [blame] | 418 |  | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 419 | # Proxy Functions | 
 | 420 | # =============== | 
 | 421 |  | 
 | 422 | # Starts the TLS proxy for the given IP/ports | 
 | 423 | # start_tls_proxy front-host front-port back-host back-port | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 424 | function start_tls_proxy { | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 425 |     local f_host=$1 | 
 | 426 |     local f_port=$2 | 
 | 427 |     local b_host=$3 | 
 | 428 |     local b_port=$4 | 
 | 429 |  | 
 | 430 |     stud $STUD_PROTO -f $f_host,$f_port -b $b_host,$b_port $DEVSTACK_CERT 2>/dev/null | 
 | 431 | } | 
| Sean Dague | 584d90e | 2013-03-29 14:34:53 -0400 | [diff] [blame] | 432 |  | 
| Dean Troyer | cc6b443 | 2013-04-08 15:38:03 -0500 | [diff] [blame] | 433 |  | 
| Stanislaw Pitucha | bd5dae0 | 2014-06-25 15:29:43 +0100 | [diff] [blame] | 434 | # Cleanup Functions | 
| Dean Troyer | 3324f19 | 2014-09-18 09:26:39 -0500 | [diff] [blame] | 435 | # ================= | 
| Stanislaw Pitucha | bd5dae0 | 2014-06-25 15:29:43 +0100 | [diff] [blame] | 436 |  | 
| Stanislaw Pitucha | bd5dae0 | 2014-06-25 15:29:43 +0100 | [diff] [blame] | 437 | # Stops all stud processes. This should be done only after all services | 
 | 438 | # using tls configuration are down. | 
 | 439 | function stop_tls_proxy { | 
 | 440 |     killall stud | 
 | 441 | } | 
 | 442 |  | 
| Stanislaw Pitucha | bd5dae0 | 2014-06-25 15:29:43 +0100 | [diff] [blame] | 443 | # Remove CA along with configuration, as well as the local server certificate | 
 | 444 | function cleanup_CA { | 
 | 445 |     rm -rf "$DATA_DIR/CA" "$DEVSTACK_CERT" | 
 | 446 | } | 
 | 447 |  | 
| Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 448 | # Tell emacs to use shell-script-mode | 
 | 449 | ## Local variables: | 
 | 450 | ## mode: shell-script | 
 | 451 | ## End: |