| 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 | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 19 |  | 
| Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 20 | # - configure_proxy | 
 | 21 | # - start_tls_proxy | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 22 |  | 
| Stanislaw Pitucha | bd5dae0 | 2014-06-25 15:29:43 +0100 | [diff] [blame] | 23 | # - stop_tls_proxy | 
 | 24 | # - cleanup_CA | 
 | 25 |  | 
| Stanislaw Pitucha | 2e0f054 | 2014-06-27 16:05:53 +0100 | [diff] [blame] | 26 | # - make_root_CA | 
 | 27 | # - make_int_CA | 
 | 28 | # - make_cert ca-dir cert-name "common-name" ["alt-name" ...] | 
| Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 29 | # - start_tls_proxy HOST_IP 5000 localhost 5000 | 
| Jamie Lennox | bd24a8d | 2013-09-20 16:26:42 +1000 | [diff] [blame] | 30 | # - ensure_certificates | 
 | 31 | # - is_ssl_enabled_service | 
| Rob Crittenden | 18d4778 | 2014-03-19 17:47:42 -0400 | [diff] [blame] | 32 | # - enable_mod_ssl | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 33 |  | 
| Dean Troyer | dc97cb7 | 2015-03-28 08:20:50 -0500 | [diff] [blame] | 34 |  | 
| Dean Troyer | cc6b443 | 2013-04-08 15:38:03 -0500 | [diff] [blame] | 35 | # Defaults | 
 | 36 | # -------- | 
 | 37 |  | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 38 | if 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 Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 41 | fi | 
 | 42 |  | 
| Rob Crittenden | 18d4778 | 2014-03-19 17:47:42 -0400 | [diff] [blame] | 43 | DEVSTACK_HOSTNAME=$(hostname -f) | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 44 | DEVSTACK_CERT_NAME=devstack-cert | 
 | 45 | DEVSTACK_CERT=$DATA_DIR/$DEVSTACK_CERT_NAME.pem | 
 | 46 |  | 
 | 47 | # CA configuration | 
 | 48 | ROOT_CA_DIR=${ROOT_CA_DIR:-$DATA_DIR/CA/root-ca} | 
 | 49 | INT_CA_DIR=${INT_CA_DIR:-$DATA_DIR/CA/int-ca} | 
 | 50 |  | 
 | 51 | ORG_NAME="OpenStack" | 
 | 52 | ORG_UNIT_NAME="DevStack" | 
 | 53 |  | 
 | 54 | # Stud configuration | 
 | 55 | STUD_PROTO="--tls" | 
 | 56 | STUD_CIPHERS='TLSv1+HIGH:!DES:!aNULL:!eNULL:@STRENGTH' | 
 | 57 |  | 
 | 58 |  | 
 | 59 | # CA Functions | 
 | 60 | # ============ | 
 | 61 |  | 
 | 62 | # There may be more than one, get specific | 
 | 63 | OPENSSL=${OPENSSL:-/usr/bin/openssl} | 
 | 64 |  | 
 | 65 | # Do primary CA configuration | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 66 | function configure_CA { | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 67 |     # 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 Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 78 | function create_CA_base { | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 79 |     local ca_dir=$1 | 
 | 80 |  | 
 | 81 |     if [[ -d $ca_dir ]]; then | 
 | 82 |         # Bail out it exists | 
 | 83 |         return 0 | 
 | 84 |     fi | 
 | 85 |  | 
| Dean Troyer | b1e3d0f | 2014-07-25 14:57:54 -0500 | [diff] [blame] | 86 |     local i | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 87 |     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 Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 95 | # Create a new CA configuration file | 
 | 96 | # create_CA_config ca-dir common-name | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 97 | function create_CA_config { | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 98 |     local ca_dir=$1 | 
 | 99 |     local common_name=$2 | 
 | 100 |  | 
 | 101 |     echo " | 
 | 102 | [ ca ] | 
 | 103 | default_ca = CA_default | 
 | 104 |  | 
 | 105 | [ CA_default ] | 
 | 106 | dir                     = $ca_dir | 
 | 107 | policy                  = policy_match | 
 | 108 | database                = \$dir/index.txt | 
 | 109 | serial                  = \$dir/serial | 
 | 110 | certs                   = \$dir/certs | 
 | 111 | crl_dir                 = \$dir/crl | 
 | 112 | new_certs_dir           = \$dir/newcerts | 
 | 113 | certificate             = \$dir/cacert.pem | 
 | 114 | private_key             = \$dir/private/cacert.key | 
 | 115 | RANDFILE                = \$dir/private/.rand | 
| Clark Boylan | faffde1 | 2017-04-27 09:54:27 -0700 | [diff] [blame] | 116 | default_md              = sha256 | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 117 |  | 
 | 118 | [ req ] | 
| Clark Boylan | faffde1 | 2017-04-27 09:54:27 -0700 | [diff] [blame] | 119 | default_bits            = 2048 | 
 | 120 | default_md              = sha256 | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 121 |  | 
 | 122 | prompt                  = no | 
 | 123 | distinguished_name      = ca_distinguished_name | 
 | 124 |  | 
 | 125 | x509_extensions         = ca_extensions | 
 | 126 |  | 
 | 127 | [ ca_distinguished_name ] | 
 | 128 | organizationName        = $ORG_NAME | 
 | 129 | organizationalUnitName  = $ORG_UNIT_NAME Certificate Authority | 
 | 130 | commonName              = $common_name | 
 | 131 |  | 
 | 132 | [ policy_match ] | 
 | 133 | countryName             = optional | 
 | 134 | stateOrProvinceName     = optional | 
 | 135 | organizationName        = match | 
 | 136 | organizationalUnitName  = optional | 
 | 137 | commonName              = supplied | 
 | 138 |  | 
 | 139 | [ ca_extensions ] | 
 | 140 | basicConstraints        = critical,CA:true | 
 | 141 | subjectKeyIdentifier    = hash | 
 | 142 | authorityKeyIdentifier  = keyid:always, issuer | 
 | 143 | keyUsage                = cRLSign, keyCertSign | 
 | 144 |  | 
 | 145 | " >$ca_dir/ca.conf | 
 | 146 | } | 
 | 147 |  | 
 | 148 | # Create a new signing configuration file | 
 | 149 | # create_signing_config ca-dir | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 150 | function create_signing_config { | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 151 |     local ca_dir=$1 | 
 | 152 |  | 
 | 153 |     echo " | 
 | 154 | [ ca ] | 
 | 155 | default_ca = CA_default | 
 | 156 |  | 
 | 157 | [ CA_default ] | 
 | 158 | dir                     = $ca_dir | 
 | 159 | policy                  = policy_match | 
 | 160 | database                = \$dir/index.txt | 
 | 161 | serial                  = \$dir/serial | 
 | 162 | certs                   = \$dir/certs | 
 | 163 | crl_dir                 = \$dir/crl | 
 | 164 | new_certs_dir           = \$dir/newcerts | 
 | 165 | certificate             = \$dir/cacert.pem | 
 | 166 | private_key             = \$dir/private/cacert.key | 
 | 167 | RANDFILE                = \$dir/private/.rand | 
 | 168 | default_md              = default | 
 | 169 |  | 
 | 170 | [ req ] | 
 | 171 | default_bits            = 1024 | 
 | 172 | default_md              = sha1 | 
 | 173 |  | 
 | 174 | prompt                  = no | 
 | 175 | distinguished_name      = req_distinguished_name | 
 | 176 |  | 
 | 177 | x509_extensions         = req_extensions | 
 | 178 |  | 
 | 179 | [ req_distinguished_name ] | 
 | 180 | organizationName        = $ORG_NAME | 
 | 181 | organizationalUnitName  = $ORG_UNIT_NAME Server Farm | 
 | 182 |  | 
 | 183 | [ policy_match ] | 
 | 184 | countryName             = optional | 
 | 185 | stateOrProvinceName     = optional | 
 | 186 | organizationName        = match | 
 | 187 | organizationalUnitName  = optional | 
 | 188 | commonName              = supplied | 
 | 189 |  | 
 | 190 | [ req_extensions ] | 
 | 191 | basicConstraints        = CA:false | 
 | 192 | subjectKeyIdentifier    = hash | 
 | 193 | authorityKeyIdentifier  = keyid:always, issuer | 
 | 194 | keyUsage                = digitalSignature, keyEncipherment, keyAgreement | 
 | 195 | extendedKeyUsage        = serverAuth, clientAuth | 
 | 196 | subjectAltName          = \$ENV::SUBJECT_ALT_NAME | 
 | 197 |  | 
 | 198 | " >$ca_dir/signing.conf | 
 | 199 | } | 
 | 200 |  | 
| Dean Troyer | ca80217 | 2013-01-09 19:08:02 -0600 | [diff] [blame] | 201 | # Create root and intermediate CAs | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 202 | # init_CA | 
 | 203 | function init_CA { | 
 | 204 |     # Ensure CAs are built | 
 | 205 |     make_root_CA $ROOT_CA_DIR | 
 | 206 |     make_int_CA $INT_CA_DIR $ROOT_CA_DIR | 
 | 207 |  | 
 | 208 |     # Create the CA bundle | 
 | 209 |     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] | 210 |     cat $INT_CA_DIR/ca-chain.pem >> $SSL_BUNDLE_FILE | 
 | 211 |  | 
 | 212 |     if is_fedora; then | 
 | 213 |         sudo cp $INT_CA_DIR/ca-chain.pem /usr/share/pki/ca-trust-source/anchors/devstack-chain.pem | 
 | 214 |         sudo update-ca-trust | 
| Clark Boylan | 35649ae | 2017-05-27 17:52:55 -0700 | [diff] [blame] | 215 |     elif is_suse; then | 
 | 216 |         sudo cp $INT_CA_DIR/ca-chain.pem /usr/share/pki/trust/anchors/devstack-chain.pem | 
 | 217 |         sudo update-ca-certificates | 
| Rob Crittenden | 18d4778 | 2014-03-19 17:47:42 -0400 | [diff] [blame] | 218 |     elif is_ubuntu; then | 
 | 219 |         sudo cp $INT_CA_DIR/ca-chain.pem /usr/local/share/ca-certificates/devstack-int.crt | 
 | 220 |         sudo cp $ROOT_CA_DIR/cacert.pem /usr/local/share/ca-certificates/devstack-root.crt | 
 | 221 |         sudo update-ca-certificates | 
 | 222 |     fi | 
 | 223 | } | 
 | 224 |  | 
| Dean Troyer | ca80217 | 2013-01-09 19:08:02 -0600 | [diff] [blame] | 225 | # Create an initial server cert | 
 | 226 | # init_cert | 
 | 227 | function init_cert { | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 228 |     if [[ ! -r $DEVSTACK_CERT ]]; then | 
 | 229 |         if [[ -n "$TLS_IP" ]]; then | 
 | 230 |             # Lie to let incomplete match routines work | 
| Ian Cordasco | 69e3c0a | 2016-09-26 12:53:14 -0500 | [diff] [blame] | 231 |             TLS_IP="DNS:$TLS_IP,IP:$TLS_IP" | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 232 |         fi | 
 | 233 |         make_cert $INT_CA_DIR $DEVSTACK_CERT_NAME $DEVSTACK_HOSTNAME "$TLS_IP" | 
 | 234 |  | 
 | 235 |         # Create a cert bundle | 
 | 236 |         cat $INT_CA_DIR/private/$DEVSTACK_CERT_NAME.key $INT_CA_DIR/$DEVSTACK_CERT_NAME.crt $INT_CA_DIR/cacert.pem >$DEVSTACK_CERT | 
 | 237 |     fi | 
 | 238 | } | 
 | 239 |  | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 240 | # make_cert creates and signs a new certificate with the given commonName and CA | 
 | 241 | # make_cert ca-dir cert-name "common-name" ["alt-name" ...] | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 242 | function make_cert { | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 243 |     local ca_dir=$1 | 
 | 244 |     local cert_name=$2 | 
 | 245 |     local common_name=$3 | 
 | 246 |     local alt_names=$4 | 
 | 247 |  | 
| Rob Crittenden | be00e95 | 2016-03-24 18:09:22 -0400 | [diff] [blame] | 248 |     if [ "$common_name" != "$SERVICE_HOST" ]; then | 
 | 249 |         if [[ -z "$alt_names" ]]; then | 
 | 250 |             alt_names="DNS:$SERVICE_HOST" | 
 | 251 |         else | 
 | 252 |             alt_names="$alt_names,DNS:$SERVICE_HOST" | 
 | 253 |         fi | 
| Ian Cordasco | 69e3c0a | 2016-09-26 12:53:14 -0500 | [diff] [blame] | 254 |         if is_ipv4_address "$SERVICE_HOST" ; then | 
 | 255 |             alt_names="$alt_names,IP:$SERVICE_HOST" | 
 | 256 |         fi | 
| Rob Crittenden | be00e95 | 2016-03-24 18:09:22 -0400 | [diff] [blame] | 257 |     fi | 
 | 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 |  | 
| Clark Boylan | 323b726 | 2016-09-23 13:33:40 -0700 | [diff] [blame] | 330 |     if [ ! -r "$ca_dir/cacert.pem" ]; then | 
 | 331 |         # Create a self-signed certificate valid for 5 years | 
 | 332 |         $OPENSSL req -config $ca_dir/ca.conf \ | 
 | 333 |             -x509 \ | 
 | 334 |             -nodes \ | 
 | 335 |             -newkey rsa \ | 
 | 336 |             -days 21360 \ | 
 | 337 |             -keyout $ca_dir/private/cacert.key \ | 
 | 338 |             -out $ca_dir/cacert.pem \ | 
 | 339 |             -outform PEM | 
 | 340 |     fi | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 341 | } | 
 | 342 |  | 
| Rob Crittenden | 1987fcc | 2015-06-10 11:00:59 -0400 | [diff] [blame] | 343 | # If a non-system python-requests is installed then it will use the | 
 | 344 | # built-in CA certificate store rather than the distro-specific | 
 | 345 | # CA certificate store. Detect this and symlink to the correct | 
 | 346 | # one. If the value for the CA is not rooted in /etc then we know | 
 | 347 | # we need to change it. | 
 | 348 | function fix_system_ca_bundle_path { | 
| Sean Dague | f3b2f4c | 2017-04-13 10:11:48 -0400 | [diff] [blame] | 349 |     if is_service_enabled tls-proxy; then | 
| Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 350 |         local capath | 
| Clark Boylan | 4baac65 | 2017-05-27 20:53:20 -0700 | [diff] [blame] | 351 |         local python_cmd=${1:-python} | 
 | 352 |         capath=$($python_cmd -c $'try:\n from requests import certs\n print (certs.where())\nexcept ImportError: pass') | 
| Rob Crittenden | 1987fcc | 2015-06-10 11:00:59 -0400 | [diff] [blame] | 353 |  | 
 | 354 |         if [[ ! $capath == "" && ! $capath =~ ^/etc/.* && ! -L $capath ]]; then | 
 | 355 |             if is_fedora; then | 
 | 356 |                 sudo rm -f $capath | 
 | 357 |                 sudo ln -s /etc/pki/tls/certs/ca-bundle.crt $capath | 
 | 358 |             elif is_ubuntu; then | 
 | 359 |                 sudo rm -f $capath | 
 | 360 |                 sudo ln -s /etc/ssl/certs/ca-certificates.crt $capath | 
| Clark Boylan | 35649ae | 2017-05-27 17:52:55 -0700 | [diff] [blame] | 361 |             elif is_suse; then | 
 | 362 |                 sudo rm -f $capath | 
 | 363 |                 sudo ln -s /etc/ssl/ca-bundle.pem $capath | 
| Rob Crittenden | 1987fcc | 2015-06-10 11:00:59 -0400 | [diff] [blame] | 364 |             else | 
 | 365 |                 echo "Don't know how to set the CA bundle, expect the install to fail." | 
 | 366 |             fi | 
 | 367 |         fi | 
 | 368 |     fi | 
 | 369 | } | 
 | 370 |  | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 371 |  | 
| Sean Dague | f3b2f4c | 2017-04-13 10:11:48 -0400 | [diff] [blame] | 372 | # Only for compatibility, return if the tls-proxy is enabled | 
 | 373 | function is_ssl_enabled_service { | 
 | 374 |     return is_service_enabled tls-proxy | 
 | 375 | } | 
 | 376 |  | 
| Jamie Lennox | bd24a8d | 2013-09-20 16:26:42 +1000 | [diff] [blame] | 377 | # Certificate Input Configuration | 
 | 378 | # =============================== | 
 | 379 |  | 
| Jamie Lennox | bd24a8d | 2013-09-20 16:26:42 +1000 | [diff] [blame] | 380 | # Ensure that the certificates for a service are in place. This function does | 
 | 381 | # not check that a service is SSL enabled, this should already have been | 
 | 382 | # completed. | 
 | 383 | # | 
 | 384 | # 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] | 385 | # variables ``{service}_SSL_CERT``, ``{service}_SSL_KEY`` and ``{service}_SSL_CA``. For | 
 | 386 | # example for keystone this would be ``KEYSTONE_SSL_CERT``, ``KEYSTONE_SSL_KEY`` and | 
 | 387 | # ``KEYSTONE_SSL_CA``. | 
| Rob Crittenden | 18d4778 | 2014-03-19 17:47:42 -0400 | [diff] [blame] | 388 | # | 
| Dean Troyer | dc97cb7 | 2015-03-28 08:20:50 -0500 | [diff] [blame] | 389 | # If it does not find these certificates then the DevStack-issued server | 
| Rob Crittenden | 18d4778 | 2014-03-19 17:47:42 -0400 | [diff] [blame] | 390 | # certificate, key and CA certificate will be associated with the service. | 
 | 391 | # | 
 | 392 | # 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] | 393 | function ensure_certificates { | 
| Jamie Lennox | bd24a8d | 2013-09-20 16:26:42 +1000 | [diff] [blame] | 394 |     local service=$1 | 
 | 395 |  | 
 | 396 |     local cert_var="${service}_SSL_CERT" | 
 | 397 |     local key_var="${service}_SSL_KEY" | 
 | 398 |     local ca_var="${service}_SSL_CA" | 
 | 399 |  | 
 | 400 |     local cert=${!cert_var} | 
 | 401 |     local key=${!key_var} | 
 | 402 |     local ca=${!ca_var} | 
 | 403 |  | 
| Rob Crittenden | 18d4778 | 2014-03-19 17:47:42 -0400 | [diff] [blame] | 404 |     if [[ -z "$cert" && -z "$key" && -z "$ca" ]]; then | 
 | 405 |         local cert="$INT_CA_DIR/$DEVSTACK_CERT_NAME.crt" | 
 | 406 |         local key="$INT_CA_DIR/private/$DEVSTACK_CERT_NAME.key" | 
 | 407 |         local ca="$INT_CA_DIR/ca-chain.pem" | 
 | 408 |         eval ${service}_SSL_CERT=\$cert | 
 | 409 |         eval ${service}_SSL_KEY=\$key | 
 | 410 |         eval ${service}_SSL_CA=\$ca | 
 | 411 |         return # the CA certificate is already in the bundle | 
 | 412 |     elif [[ -z "$cert" || -z "$key" || -z "$ca" ]]; then | 
| Jamie Lennox | bd24a8d | 2013-09-20 16:26:42 +1000 | [diff] [blame] | 413 |         die $LINENO "Missing either the ${cert_var} ${key_var} or ${ca_var}" \ | 
 | 414 |                     "variable to enable SSL for ${service}" | 
 | 415 |     fi | 
 | 416 |  | 
 | 417 |     cat $ca >> $SSL_BUNDLE_FILE | 
 | 418 | } | 
 | 419 |  | 
| Rob Crittenden | 18d4778 | 2014-03-19 17:47:42 -0400 | [diff] [blame] | 420 | # Enable the mod_ssl plugin in Apache | 
 | 421 | function enable_mod_ssl { | 
 | 422 |     echo "Enabling mod_ssl" | 
 | 423 |  | 
 | 424 |     if is_ubuntu; then | 
 | 425 |         sudo a2enmod ssl | 
| Clark Boylan | 35649ae | 2017-05-27 17:52:55 -0700 | [diff] [blame] | 426 |     elif is_suse; then | 
 | 427 |         sudo a2enmod ssl | 
 | 428 |         sudo a2enflag SSL | 
| Rob Crittenden | 18d4778 | 2014-03-19 17:47:42 -0400 | [diff] [blame] | 429 |     elif is_fedora; then | 
 | 430 |         # Fedora enables mod_ssl by default | 
 | 431 |         : | 
 | 432 |     fi | 
 | 433 |     if ! sudo `which httpd || which apache2ctl` -M | grep -w -q ssl_module; then | 
 | 434 |         die $LINENO "mod_ssl is not enabled in apache2/httpd, please check for it manually and run stack.sh again" | 
 | 435 |     fi | 
 | 436 | } | 
 | 437 |  | 
| Jamie Lennox | bd24a8d | 2013-09-20 16:26:42 +1000 | [diff] [blame] | 438 |  | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 439 | # Proxy Functions | 
 | 440 | # =============== | 
 | 441 |  | 
| Clark Boylan | cfb9f05 | 2016-11-29 10:43:05 -0800 | [diff] [blame] | 442 | function tune_apache_connections { | 
 | 443 |     local tuning_file=$APACHE_SETTINGS_DIR/connection-tuning.conf | 
 | 444 |     if ! [ -f $tuning_file ] ; then | 
 | 445 |         sudo bash -c "cat > $tuning_file" << EOF | 
 | 446 | # worker MPM | 
 | 447 | # StartServers: initial number of server processes to start | 
 | 448 | # MinSpareThreads: minimum number of worker threads which are kept spare | 
 | 449 | # MaxSpareThreads: maximum number of worker threads which are kept spare | 
 | 450 | # ThreadLimit: ThreadsPerChild can be changed to this maximum value during a | 
 | 451 | #              graceful restart. ThreadLimit can only be changed by stopping | 
 | 452 | #              and starting Apache. | 
 | 453 | # ThreadsPerChild: constant number of worker threads in each server process | 
 | 454 | # MaxClients: maximum number of simultaneous client connections | 
 | 455 | # MaxRequestsPerChild: maximum number of requests a server process serves | 
 | 456 | # | 
| Clark Boylan | 8cf9acd | 2017-03-16 14:06:58 -0700 | [diff] [blame] | 457 | # We want to be memory thrifty so tune down apache to allow 256 total | 
 | 458 | # connections. This should still be plenty for a dev env yet lighter than | 
 | 459 | # apache defaults. | 
| Clark Boylan | cfb9f05 | 2016-11-29 10:43:05 -0800 | [diff] [blame] | 460 | <IfModule mpm_worker_module> | 
 | 461 | # Note that the next three conf values must be changed together. | 
 | 462 | # MaxClients = ServerLimit * ThreadsPerChild | 
| Clark Boylan | 8cf9acd | 2017-03-16 14:06:58 -0700 | [diff] [blame] | 463 | ServerLimit           8 | 
| Clark Boylan | cfb9f05 | 2016-11-29 10:43:05 -0800 | [diff] [blame] | 464 | ThreadsPerChild      32 | 
| Clark Boylan | 8cf9acd | 2017-03-16 14:06:58 -0700 | [diff] [blame] | 465 | MaxClients          256 | 
 | 466 | StartServers          2 | 
 | 467 | MinSpareThreads      32 | 
 | 468 | MaxSpareThreads      96 | 
| Clark Boylan | cfb9f05 | 2016-11-29 10:43:05 -0800 | [diff] [blame] | 469 | ThreadLimit          64 | 
 | 470 | MaxRequestsPerChild   0 | 
 | 471 | </IfModule> | 
 | 472 | <IfModule mpm_event_module> | 
 | 473 | # Note that the next three conf values must be changed together. | 
 | 474 | # MaxClients = ServerLimit * ThreadsPerChild | 
| Clark Boylan | 8cf9acd | 2017-03-16 14:06:58 -0700 | [diff] [blame] | 475 | ServerLimit           8 | 
| Clark Boylan | cfb9f05 | 2016-11-29 10:43:05 -0800 | [diff] [blame] | 476 | ThreadsPerChild      32 | 
| Clark Boylan | 8cf9acd | 2017-03-16 14:06:58 -0700 | [diff] [blame] | 477 | MaxClients          256 | 
 | 478 | StartServers          2 | 
 | 479 | MinSpareThreads      32 | 
 | 480 | MaxSpareThreads      96 | 
| Clark Boylan | cfb9f05 | 2016-11-29 10:43:05 -0800 | [diff] [blame] | 481 | ThreadLimit          64 | 
 | 482 | MaxRequestsPerChild   0 | 
 | 483 | </IfModule> | 
 | 484 | EOF | 
 | 485 |         restart_apache_server | 
 | 486 |     fi | 
 | 487 | } | 
 | 488 |  | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 489 | # Starts the TLS proxy for the given IP/ports | 
 | 490 | # start_tls_proxy front-host front-port back-host back-port | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 491 | function start_tls_proxy { | 
| Gregory Haynes | 4b49e40 | 2016-08-31 18:19:51 -0700 | [diff] [blame] | 492 |     local b_service="$1-tls-proxy" | 
 | 493 |     local f_host=$2 | 
 | 494 |     local f_port=$3 | 
 | 495 |     local b_host=$4 | 
 | 496 |     local b_port=$5 | 
| Clark Boylan | f4dbd12 | 2017-05-31 13:17:22 -0700 | [diff] [blame] | 497 |     # 8190 is the default apache size. | 
 | 498 |     local f_header_size=${6:-8190} | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 499 |  | 
| Clark Boylan | cfb9f05 | 2016-11-29 10:43:05 -0800 | [diff] [blame] | 500 |     tune_apache_connections | 
 | 501 |  | 
| Gregory Haynes | 4b49e40 | 2016-08-31 18:19:51 -0700 | [diff] [blame] | 502 |     local config_file | 
 | 503 |     config_file=$(apache_site_config_for $b_service) | 
 | 504 |     local listen_string | 
 | 505 |     # Default apache configs on ubuntu and centos listen on 80 and 443 | 
 | 506 |     # newer apache seems fine with duplicate listen directive but older | 
 | 507 |     # apache does not so special case 80 and 443. | 
 | 508 |     if [[ "$f_port" == "80" ]] || [[ "$f_port" == "443" ]]; then | 
 | 509 |         listen_string="" | 
 | 510 |     elif [[ "$f_host" == '*' ]] ; then | 
 | 511 |         listen_string="Listen $f_port" | 
 | 512 |     else | 
 | 513 |         listen_string="Listen $f_host:$f_port" | 
 | 514 |     fi | 
 | 515 |     sudo bash -c "cat >$config_file" << EOF | 
 | 516 | $listen_string | 
 | 517 |  | 
 | 518 | <VirtualHost $f_host:$f_port> | 
 | 519 |     SSLEngine On | 
 | 520 |     SSLCertificateFile $DEVSTACK_CERT | 
 | 521 |  | 
| Jordan Pittier | 4370925 | 2017-02-14 16:48:20 +0100 | [diff] [blame] | 522 |     # Disable KeepAlive to fix bug #1630664 a.k.a the | 
 | 523 |     # ('Connection aborted.', BadStatusLine("''",)) error | 
 | 524 |     KeepAlive Off | 
 | 525 |  | 
| Clark Boylan | f4dbd12 | 2017-05-31 13:17:22 -0700 | [diff] [blame] | 526 |     # This increase in allowed request header sizes is required | 
 | 527 |     # for swift functional testing to work with tls enabled. It is 2 bytes | 
 | 528 |     # larger than the apache default of 8190. | 
 | 529 |     LimitRequestFieldSize $f_header_size | 
 | 530 |  | 
| Gregory Haynes | 4b49e40 | 2016-08-31 18:19:51 -0700 | [diff] [blame] | 531 |     <Location /> | 
| Sean Dague | a1446b9 | 2017-04-17 14:31:21 -0400 | [diff] [blame] | 532 |         ProxyPass http://$b_host:$b_port/ retry=0 nocanon | 
| Gregory Haynes | 4b49e40 | 2016-08-31 18:19:51 -0700 | [diff] [blame] | 533 |         ProxyPassReverse http://$b_host:$b_port/ | 
 | 534 |     </Location> | 
| Clark Boylan | 66ce5c2 | 2016-10-05 12:11:05 -0700 | [diff] [blame] | 535 |     ErrorLog $APACHE_LOG_DIR/tls-proxy_error.log | 
| Ian Wienand | 139837d | 2017-08-08 17:51:29 +1000 | [diff] [blame] | 536 |     ErrorLogFormat "%{cu}t [%-m:%l] [pid %P:tid %T] %7F: %E: [client\ %a] [frontend\ %A] %M% ,\ referer\ %{Referer}i" | 
| Clark Boylan | 66ce5c2 | 2016-10-05 12:11:05 -0700 | [diff] [blame] | 537 |     LogLevel info | 
| Ian Wienand | 139837d | 2017-08-08 17:51:29 +1000 | [diff] [blame] | 538 |     CustomLog $APACHE_LOG_DIR/tls-proxy_access.log "%{%Y-%m-%d}t %{%T}t.%{msec_frac}t [%l] %a \"%r\" %>s %b" | 
| Gregory Haynes | 4b49e40 | 2016-08-31 18:19:51 -0700 | [diff] [blame] | 539 | </VirtualHost> | 
 | 540 | EOF | 
| Clark Boylan | 35649ae | 2017-05-27 17:52:55 -0700 | [diff] [blame] | 541 |     if is_suse ; then | 
 | 542 |         sudo a2enflag SSL | 
 | 543 |     fi | 
| Gregory Haynes | 4b49e40 | 2016-08-31 18:19:51 -0700 | [diff] [blame] | 544 |     for mod in ssl proxy proxy_http; do | 
 | 545 |         enable_apache_mod $mod | 
 | 546 |     done | 
 | 547 |     enable_apache_site $b_service | 
| Ian Wienand | f6a2d2c | 2017-04-26 10:50:29 +1000 | [diff] [blame] | 548 |     restart_apache_server | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 549 | } | 
| Sean Dague | 584d90e | 2013-03-29 14:34:53 -0400 | [diff] [blame] | 550 |  | 
| Sean Dague | f06455e | 2016-10-07 06:57:03 -0400 | [diff] [blame] | 551 | # Follow TLS proxy | 
 | 552 | function follow_tls_proxy { | 
 | 553 |     sudo touch /var/log/$APACHE_NAME/tls-proxy_error.log | 
 | 554 |     tail_log tls-error /var/log/$APACHE_NAME/tls-proxy_error.log | 
 | 555 |     sudo touch /var/log/$APACHE_NAME/tls-proxy_access.log | 
 | 556 |     tail_log tls-proxy /var/log/$APACHE_NAME/tls-proxy_access.log | 
 | 557 | } | 
| Dean Troyer | cc6b443 | 2013-04-08 15:38:03 -0500 | [diff] [blame] | 558 |  | 
| Stanislaw Pitucha | bd5dae0 | 2014-06-25 15:29:43 +0100 | [diff] [blame] | 559 | # Cleanup Functions | 
| Dean Troyer | 3324f19 | 2014-09-18 09:26:39 -0500 | [diff] [blame] | 560 | # ================= | 
| Stanislaw Pitucha | bd5dae0 | 2014-06-25 15:29:43 +0100 | [diff] [blame] | 561 |  | 
| Gregory Haynes | 4b49e40 | 2016-08-31 18:19:51 -0700 | [diff] [blame] | 562 | # Stops the apache service. This should be done only after all services | 
| Stanislaw Pitucha | bd5dae0 | 2014-06-25 15:29:43 +0100 | [diff] [blame] | 563 | # using tls configuration are down. | 
 | 564 | function stop_tls_proxy { | 
| Gregory Haynes | 4b49e40 | 2016-08-31 18:19:51 -0700 | [diff] [blame] | 565 |     stop_apache_server | 
| Stanislaw Pitucha | bd5dae0 | 2014-06-25 15:29:43 +0100 | [diff] [blame] | 566 | } | 
 | 567 |  | 
| Gregory Haynes | 4b49e40 | 2016-08-31 18:19:51 -0700 | [diff] [blame] | 568 | # Clean up the CA files | 
 | 569 | # cleanup_CA | 
| Stanislaw Pitucha | bd5dae0 | 2014-06-25 15:29:43 +0100 | [diff] [blame] | 570 | function cleanup_CA { | 
| Gregory Haynes | 4b49e40 | 2016-08-31 18:19:51 -0700 | [diff] [blame] | 571 |     if is_fedora; then | 
 | 572 |         sudo rm -f /usr/share/pki/ca-trust-source/anchors/devstack-chain.pem | 
 | 573 |         sudo update-ca-trust | 
 | 574 |     elif is_ubuntu; then | 
 | 575 |         sudo rm -f /usr/local/share/ca-certificates/devstack-int.crt | 
 | 576 |         sudo rm -f /usr/local/share/ca-certificates/devstack-root.crt | 
 | 577 |         sudo update-ca-certificates | 
 | 578 |     fi | 
 | 579 |  | 
| Clark Boylan | 323b726 | 2016-09-23 13:33:40 -0700 | [diff] [blame] | 580 |     rm -rf "$INT_CA_DIR" "$ROOT_CA_DIR" "$DEVSTACK_CERT" | 
| Stanislaw Pitucha | bd5dae0 | 2014-06-25 15:29:43 +0100 | [diff] [blame] | 581 | } | 
 | 582 |  | 
| Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 583 | # Tell emacs to use shell-script-mode | 
 | 584 | ## Local variables: | 
 | 585 | ## mode: shell-script | 
 | 586 | ## End: |