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 |
| 116 | default_md = default |
| 117 | |
| 118 | [ req ] |
| 119 | default_bits = 1024 |
| 120 | default_md = sha1 |
| 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 { |
Rob Crittenden | 1987fcc | 2015-06-10 11:00:59 -0400 | [diff] [blame] | 204 | fix_system_ca_bundle_path |
Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 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 | |
Dean Troyer | ca80217 | 2013-01-09 19:08:02 -0600 | [diff] [blame] | 223 | # Create an initial server cert |
| 224 | # init_cert |
| 225 | function init_cert { |
Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 226 | if [[ ! -r $DEVSTACK_CERT ]]; then |
| 227 | if [[ -n "$TLS_IP" ]]; then |
| 228 | # Lie to let incomplete match routines work |
Ian Cordasco | 69e3c0a | 2016-09-26 12:53:14 -0500 | [diff] [blame^] | 229 | TLS_IP="DNS:$TLS_IP,IP:$TLS_IP" |
Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 230 | fi |
| 231 | make_cert $INT_CA_DIR $DEVSTACK_CERT_NAME $DEVSTACK_HOSTNAME "$TLS_IP" |
| 232 | |
| 233 | # Create a cert bundle |
| 234 | cat $INT_CA_DIR/private/$DEVSTACK_CERT_NAME.key $INT_CA_DIR/$DEVSTACK_CERT_NAME.crt $INT_CA_DIR/cacert.pem >$DEVSTACK_CERT |
| 235 | fi |
| 236 | } |
| 237 | |
Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 238 | # make_cert creates and signs a new certificate with the given commonName and CA |
| 239 | # make_cert ca-dir cert-name "common-name" ["alt-name" ...] |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 240 | function make_cert { |
Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 241 | local ca_dir=$1 |
| 242 | local cert_name=$2 |
| 243 | local common_name=$3 |
| 244 | local alt_names=$4 |
| 245 | |
Rob Crittenden | be00e95 | 2016-03-24 18:09:22 -0400 | [diff] [blame] | 246 | if [ "$common_name" != "$SERVICE_HOST" ]; then |
| 247 | if [[ -z "$alt_names" ]]; then |
| 248 | alt_names="DNS:$SERVICE_HOST" |
| 249 | else |
| 250 | alt_names="$alt_names,DNS:$SERVICE_HOST" |
| 251 | fi |
Ian Cordasco | 69e3c0a | 2016-09-26 12:53:14 -0500 | [diff] [blame^] | 252 | if is_ipv4_address "$SERVICE_HOST" ; then |
| 253 | alt_names="$alt_names,IP:$SERVICE_HOST" |
| 254 | fi |
Rob Crittenden | be00e95 | 2016-03-24 18:09:22 -0400 | [diff] [blame] | 255 | fi |
| 256 | |
Stanislaw Pitucha | 2f69c6b | 2014-06-25 15:07:48 +0100 | [diff] [blame] | 257 | # Only generate the certificate if it doesn't exist yet on the disk |
| 258 | if [ ! -r "$ca_dir/$cert_name.crt" ]; then |
| 259 | # Generate a signing request |
| 260 | $OPENSSL req \ |
| 261 | -sha1 \ |
| 262 | -newkey rsa \ |
| 263 | -nodes \ |
| 264 | -keyout $ca_dir/private/$cert_name.key \ |
| 265 | -out $ca_dir/$cert_name.csr \ |
| 266 | -subj "/O=${ORG_NAME}/OU=${ORG_UNIT_NAME} Servers/CN=${common_name}" |
Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 267 | |
Stanislaw Pitucha | 2f69c6b | 2014-06-25 15:07:48 +0100 | [diff] [blame] | 268 | if [[ -z "$alt_names" ]]; then |
| 269 | alt_names="DNS:${common_name}" |
| 270 | else |
| 271 | alt_names="DNS:${common_name},${alt_names}" |
| 272 | fi |
| 273 | |
| 274 | # Sign the request valid for 1 year |
| 275 | SUBJECT_ALT_NAME="$alt_names" \ |
| 276 | $OPENSSL ca -config $ca_dir/signing.conf \ |
| 277 | -extensions req_extensions \ |
| 278 | -days 365 \ |
| 279 | -notext \ |
| 280 | -in $ca_dir/$cert_name.csr \ |
| 281 | -out $ca_dir/$cert_name.crt \ |
| 282 | -subj "/O=${ORG_NAME}/OU=${ORG_UNIT_NAME} Servers/CN=${common_name}" \ |
| 283 | -batch |
Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 284 | fi |
Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 285 | } |
| 286 | |
Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 287 | # Make an intermediate CA to sign everything else |
| 288 | # make_int_CA ca-dir signing-ca-dir |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 289 | function make_int_CA { |
Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 290 | local ca_dir=$1 |
| 291 | local signing_ca_dir=$2 |
| 292 | |
| 293 | # Create the root CA |
| 294 | create_CA_base $ca_dir |
| 295 | create_CA_config $ca_dir 'Intermediate CA' |
| 296 | create_signing_config $ca_dir |
| 297 | |
Stanislaw Pitucha | 2f69c6b | 2014-06-25 15:07:48 +0100 | [diff] [blame] | 298 | if [ ! -r "$ca_dir/cacert.pem" ]; then |
| 299 | # Create a signing certificate request |
| 300 | $OPENSSL req -config $ca_dir/ca.conf \ |
| 301 | -sha1 \ |
| 302 | -newkey rsa \ |
| 303 | -nodes \ |
| 304 | -keyout $ca_dir/private/cacert.key \ |
| 305 | -out $ca_dir/cacert.csr \ |
| 306 | -outform PEM |
Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 307 | |
Stanislaw Pitucha | 2f69c6b | 2014-06-25 15:07:48 +0100 | [diff] [blame] | 308 | # Sign the intermediate request valid for 1 year |
| 309 | $OPENSSL ca -config $signing_ca_dir/ca.conf \ |
| 310 | -extensions ca_extensions \ |
| 311 | -days 365 \ |
| 312 | -notext \ |
| 313 | -in $ca_dir/cacert.csr \ |
| 314 | -out $ca_dir/cacert.pem \ |
| 315 | -batch |
| 316 | fi |
Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | # Make a root CA to sign other CAs |
| 320 | # make_root_CA ca-dir |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 321 | function make_root_CA { |
Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 322 | local ca_dir=$1 |
| 323 | |
| 324 | # Create the root CA |
| 325 | create_CA_base $ca_dir |
| 326 | create_CA_config $ca_dir 'Root CA' |
| 327 | |
| 328 | # Create a self-signed certificate valid for 5 years |
| 329 | $OPENSSL req -config $ca_dir/ca.conf \ |
| 330 | -x509 \ |
| 331 | -nodes \ |
| 332 | -newkey rsa \ |
| 333 | -days 21360 \ |
| 334 | -keyout $ca_dir/private/cacert.key \ |
| 335 | -out $ca_dir/cacert.pem \ |
| 336 | -outform PEM |
| 337 | } |
| 338 | |
Rob Crittenden | 1987fcc | 2015-06-10 11:00:59 -0400 | [diff] [blame] | 339 | # If a non-system python-requests is installed then it will use the |
| 340 | # built-in CA certificate store rather than the distro-specific |
| 341 | # CA certificate store. Detect this and symlink to the correct |
| 342 | # one. If the value for the CA is not rooted in /etc then we know |
| 343 | # we need to change it. |
| 344 | function fix_system_ca_bundle_path { |
| 345 | if is_service_enabled tls-proxy || [ "$USE_SSL" == "True" ]; then |
Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 346 | local capath |
| 347 | capath=$(python -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] | 348 | |
| 349 | if [[ ! $capath == "" && ! $capath =~ ^/etc/.* && ! -L $capath ]]; then |
| 350 | if is_fedora; then |
| 351 | sudo rm -f $capath |
| 352 | sudo ln -s /etc/pki/tls/certs/ca-bundle.crt $capath |
| 353 | elif is_ubuntu; then |
| 354 | sudo rm -f $capath |
| 355 | sudo ln -s /etc/ssl/certs/ca-certificates.crt $capath |
| 356 | else |
| 357 | echo "Don't know how to set the CA bundle, expect the install to fail." |
| 358 | fi |
| 359 | fi |
| 360 | fi |
| 361 | } |
| 362 | |
Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 363 | |
Jamie Lennox | bd24a8d | 2013-09-20 16:26:42 +1000 | [diff] [blame] | 364 | # Certificate Input Configuration |
| 365 | # =============================== |
| 366 | |
| 367 | # check to see if the service(s) specified are to be SSL enabled. |
| 368 | # |
| 369 | # Multiple services specified as arguments are ``OR``'ed together; the test |
| 370 | # is a short-circuit boolean, i.e it returns on the first match. |
| 371 | # |
| 372 | # Uses global ``SSL_ENABLED_SERVICES`` |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 373 | function is_ssl_enabled_service { |
Sean Dague | f0bd8db | 2014-07-23 15:14:07 -0400 | [diff] [blame] | 374 | local services=$@ |
| 375 | local service="" |
Rob Crittenden | 18d4778 | 2014-03-19 17:47:42 -0400 | [diff] [blame] | 376 | if [ "$USE_SSL" == "False" ]; then |
| 377 | return 1 |
| 378 | fi |
Jamie Lennox | bd24a8d | 2013-09-20 16:26:42 +1000 | [diff] [blame] | 379 | for service in ${services}; do |
| 380 | [[ ,${SSL_ENABLED_SERVICES}, =~ ,${service}, ]] && return 0 |
| 381 | done |
| 382 | return 1 |
| 383 | } |
| 384 | |
Jamie Lennox | bd24a8d | 2013-09-20 16:26:42 +1000 | [diff] [blame] | 385 | # Ensure that the certificates for a service are in place. This function does |
| 386 | # not check that a service is SSL enabled, this should already have been |
| 387 | # completed. |
| 388 | # |
| 389 | # 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] | 390 | # variables ``{service}_SSL_CERT``, ``{service}_SSL_KEY`` and ``{service}_SSL_CA``. For |
| 391 | # example for keystone this would be ``KEYSTONE_SSL_CERT``, ``KEYSTONE_SSL_KEY`` and |
| 392 | # ``KEYSTONE_SSL_CA``. |
Rob Crittenden | 18d4778 | 2014-03-19 17:47:42 -0400 | [diff] [blame] | 393 | # |
Dean Troyer | dc97cb7 | 2015-03-28 08:20:50 -0500 | [diff] [blame] | 394 | # If it does not find these certificates then the DevStack-issued server |
Rob Crittenden | 18d4778 | 2014-03-19 17:47:42 -0400 | [diff] [blame] | 395 | # certificate, key and CA certificate will be associated with the service. |
| 396 | # |
| 397 | # 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] | 398 | function ensure_certificates { |
Jamie Lennox | bd24a8d | 2013-09-20 16:26:42 +1000 | [diff] [blame] | 399 | local service=$1 |
| 400 | |
| 401 | local cert_var="${service}_SSL_CERT" |
| 402 | local key_var="${service}_SSL_KEY" |
| 403 | local ca_var="${service}_SSL_CA" |
| 404 | |
| 405 | local cert=${!cert_var} |
| 406 | local key=${!key_var} |
| 407 | local ca=${!ca_var} |
| 408 | |
Rob Crittenden | 18d4778 | 2014-03-19 17:47:42 -0400 | [diff] [blame] | 409 | if [[ -z "$cert" && -z "$key" && -z "$ca" ]]; then |
| 410 | local cert="$INT_CA_DIR/$DEVSTACK_CERT_NAME.crt" |
| 411 | local key="$INT_CA_DIR/private/$DEVSTACK_CERT_NAME.key" |
| 412 | local ca="$INT_CA_DIR/ca-chain.pem" |
| 413 | eval ${service}_SSL_CERT=\$cert |
| 414 | eval ${service}_SSL_KEY=\$key |
| 415 | eval ${service}_SSL_CA=\$ca |
| 416 | return # the CA certificate is already in the bundle |
| 417 | elif [[ -z "$cert" || -z "$key" || -z "$ca" ]]; then |
Jamie Lennox | bd24a8d | 2013-09-20 16:26:42 +1000 | [diff] [blame] | 418 | die $LINENO "Missing either the ${cert_var} ${key_var} or ${ca_var}" \ |
| 419 | "variable to enable SSL for ${service}" |
| 420 | fi |
| 421 | |
| 422 | cat $ca >> $SSL_BUNDLE_FILE |
| 423 | } |
| 424 | |
Rob Crittenden | 18d4778 | 2014-03-19 17:47:42 -0400 | [diff] [blame] | 425 | # Enable the mod_ssl plugin in Apache |
| 426 | function enable_mod_ssl { |
| 427 | echo "Enabling mod_ssl" |
| 428 | |
| 429 | if is_ubuntu; then |
| 430 | sudo a2enmod ssl |
| 431 | elif is_fedora; then |
| 432 | # Fedora enables mod_ssl by default |
| 433 | : |
| 434 | fi |
| 435 | if ! sudo `which httpd || which apache2ctl` -M | grep -w -q ssl_module; then |
| 436 | die $LINENO "mod_ssl is not enabled in apache2/httpd, please check for it manually and run stack.sh again" |
| 437 | fi |
| 438 | } |
| 439 | |
Jamie Lennox | bd24a8d | 2013-09-20 16:26:42 +1000 | [diff] [blame] | 440 | |
Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 441 | # Proxy Functions |
| 442 | # =============== |
| 443 | |
| 444 | # Starts the TLS proxy for the given IP/ports |
| 445 | # start_tls_proxy front-host front-port back-host back-port |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 446 | function start_tls_proxy { |
Gregory Haynes | 4b49e40 | 2016-08-31 18:19:51 -0700 | [diff] [blame] | 447 | local b_service="$1-tls-proxy" |
| 448 | local f_host=$2 |
| 449 | local f_port=$3 |
| 450 | local b_host=$4 |
| 451 | local b_port=$5 |
Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 452 | |
Gregory Haynes | 4b49e40 | 2016-08-31 18:19:51 -0700 | [diff] [blame] | 453 | local config_file |
| 454 | config_file=$(apache_site_config_for $b_service) |
| 455 | local listen_string |
| 456 | # Default apache configs on ubuntu and centos listen on 80 and 443 |
| 457 | # newer apache seems fine with duplicate listen directive but older |
| 458 | # apache does not so special case 80 and 443. |
| 459 | if [[ "$f_port" == "80" ]] || [[ "$f_port" == "443" ]]; then |
| 460 | listen_string="" |
| 461 | elif [[ "$f_host" == '*' ]] ; then |
| 462 | listen_string="Listen $f_port" |
| 463 | else |
| 464 | listen_string="Listen $f_host:$f_port" |
| 465 | fi |
| 466 | sudo bash -c "cat >$config_file" << EOF |
| 467 | $listen_string |
| 468 | |
| 469 | <VirtualHost $f_host:$f_port> |
| 470 | SSLEngine On |
| 471 | SSLCertificateFile $DEVSTACK_CERT |
| 472 | |
| 473 | <Location /> |
| 474 | ProxyPass http://$b_host:$b_port/ retry=5 nocanon |
| 475 | ProxyPassReverse http://$b_host:$b_port/ |
| 476 | </Location> |
| 477 | </VirtualHost> |
| 478 | EOF |
| 479 | for mod in ssl proxy proxy_http; do |
| 480 | enable_apache_mod $mod |
| 481 | done |
| 482 | enable_apache_site $b_service |
| 483 | # Only a reload is required to pull in new vhosts |
| 484 | # Note that a restart reliably fails on centos7 and trusty |
| 485 | # because apache can't open port 80 because the old apache |
| 486 | # still has it open. Using reload fixes trusty but centos7 |
| 487 | # still doesn't work. |
| 488 | reload_apache_server |
Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 489 | } |
Sean Dague | 584d90e | 2013-03-29 14:34:53 -0400 | [diff] [blame] | 490 | |
Dean Troyer | cc6b443 | 2013-04-08 15:38:03 -0500 | [diff] [blame] | 491 | |
Stanislaw Pitucha | bd5dae0 | 2014-06-25 15:29:43 +0100 | [diff] [blame] | 492 | # Cleanup Functions |
Dean Troyer | 3324f19 | 2014-09-18 09:26:39 -0500 | [diff] [blame] | 493 | # ================= |
Stanislaw Pitucha | bd5dae0 | 2014-06-25 15:29:43 +0100 | [diff] [blame] | 494 | |
Gregory Haynes | 4b49e40 | 2016-08-31 18:19:51 -0700 | [diff] [blame] | 495 | # Stops the apache service. This should be done only after all services |
Stanislaw Pitucha | bd5dae0 | 2014-06-25 15:29:43 +0100 | [diff] [blame] | 496 | # using tls configuration are down. |
| 497 | function stop_tls_proxy { |
Gregory Haynes | 4b49e40 | 2016-08-31 18:19:51 -0700 | [diff] [blame] | 498 | stop_apache_server |
Stanislaw Pitucha | bd5dae0 | 2014-06-25 15:29:43 +0100 | [diff] [blame] | 499 | } |
| 500 | |
Gregory Haynes | 4b49e40 | 2016-08-31 18:19:51 -0700 | [diff] [blame] | 501 | # Clean up the CA files |
| 502 | # cleanup_CA |
Stanislaw Pitucha | bd5dae0 | 2014-06-25 15:29:43 +0100 | [diff] [blame] | 503 | function cleanup_CA { |
Gregory Haynes | 4b49e40 | 2016-08-31 18:19:51 -0700 | [diff] [blame] | 504 | if is_fedora; then |
| 505 | sudo rm -f /usr/share/pki/ca-trust-source/anchors/devstack-chain.pem |
| 506 | sudo update-ca-trust |
| 507 | elif is_ubuntu; then |
| 508 | sudo rm -f /usr/local/share/ca-certificates/devstack-int.crt |
| 509 | sudo rm -f /usr/local/share/ca-certificates/devstack-root.crt |
| 510 | sudo update-ca-certificates |
| 511 | fi |
| 512 | |
Stanislaw Pitucha | bd5dae0 | 2014-06-25 15:29:43 +0100 | [diff] [blame] | 513 | rm -rf "$DATA_DIR/CA" "$DEVSTACK_CERT" |
| 514 | } |
| 515 | |
Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 516 | # Tell emacs to use shell-script-mode |
| 517 | ## Local variables: |
| 518 | ## mode: shell-script |
| 519 | ## End: |