Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 1 | # lib/tls |
| 2 | # Functions to control the configuration and operation of the TLS proxy service |
| 3 | |
Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 4 | # !! source _before_ any services that use ``SERVICE_HOST`` |
Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 5 | # |
| 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 Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 12 | |
| 13 | # Entry points: |
Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 14 | # |
| 15 | # - configure_CA |
| 16 | # - init_CA |
Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 17 | |
Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 18 | # - configure_proxy |
| 19 | # - start_tls_proxy |
Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 20 | |
Stanislaw Pitucha | bd5dae0 | 2014-06-25 15:29:43 +0100 | [diff] [blame] | 21 | # - stop_tls_proxy |
| 22 | # - cleanup_CA |
| 23 | |
Stanislaw Pitucha | 2e0f054 | 2014-06-27 16:05:53 +0100 | [diff] [blame] | 24 | # - make_root_CA |
| 25 | # - make_int_CA |
| 26 | # - make_cert ca-dir cert-name "common-name" ["alt-name" ...] |
Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 27 | # - start_tls_proxy HOST_IP 5000 localhost 5000 |
Jamie Lennox | bd24a8d | 2013-09-20 16:26:42 +1000 | [diff] [blame] | 28 | # - ensure_certificates |
| 29 | # - is_ssl_enabled_service |
Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 30 | |
Dean Troyer | cc6b443 | 2013-04-08 15:38:03 -0500 | [diff] [blame] | 31 | # Defaults |
| 32 | # -------- |
| 33 | |
Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 34 | if is_service_enabled tls-proxy; then |
| 35 | # TODO(dtroyer): revisit this below after the search for HOST_IP has been done |
| 36 | TLS_IP=${TLS_IP:-$SERVICE_IP} |
| 37 | |
| 38 | # Set the default ``SERVICE_PROTOCOL`` for TLS |
| 39 | SERVICE_PROTOCOL=https |
| 40 | fi |
| 41 | |
| 42 | # Make up a hostname for cert purposes |
| 43 | # will be added to /etc/hosts? |
| 44 | DEVSTACK_HOSTNAME=secure.devstack.org |
| 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 | |
| 96 | |
| 97 | # Create a new CA configuration file |
| 98 | # create_CA_config ca-dir common-name |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 99 | function create_CA_config { |
Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 100 | local ca_dir=$1 |
| 101 | local common_name=$2 |
| 102 | |
| 103 | echo " |
| 104 | [ ca ] |
| 105 | default_ca = CA_default |
| 106 | |
| 107 | [ CA_default ] |
| 108 | dir = $ca_dir |
| 109 | policy = policy_match |
| 110 | database = \$dir/index.txt |
| 111 | serial = \$dir/serial |
| 112 | certs = \$dir/certs |
| 113 | crl_dir = \$dir/crl |
| 114 | new_certs_dir = \$dir/newcerts |
| 115 | certificate = \$dir/cacert.pem |
| 116 | private_key = \$dir/private/cacert.key |
| 117 | RANDFILE = \$dir/private/.rand |
| 118 | default_md = default |
| 119 | |
| 120 | [ req ] |
| 121 | default_bits = 1024 |
| 122 | default_md = sha1 |
| 123 | |
| 124 | prompt = no |
| 125 | distinguished_name = ca_distinguished_name |
| 126 | |
| 127 | x509_extensions = ca_extensions |
| 128 | |
| 129 | [ ca_distinguished_name ] |
| 130 | organizationName = $ORG_NAME |
| 131 | organizationalUnitName = $ORG_UNIT_NAME Certificate Authority |
| 132 | commonName = $common_name |
| 133 | |
| 134 | [ policy_match ] |
| 135 | countryName = optional |
| 136 | stateOrProvinceName = optional |
| 137 | organizationName = match |
| 138 | organizationalUnitName = optional |
| 139 | commonName = supplied |
| 140 | |
| 141 | [ ca_extensions ] |
| 142 | basicConstraints = critical,CA:true |
| 143 | subjectKeyIdentifier = hash |
| 144 | authorityKeyIdentifier = keyid:always, issuer |
| 145 | keyUsage = cRLSign, keyCertSign |
| 146 | |
| 147 | " >$ca_dir/ca.conf |
| 148 | } |
| 149 | |
| 150 | # Create a new signing configuration file |
| 151 | # create_signing_config ca-dir |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 152 | function create_signing_config { |
Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 153 | local ca_dir=$1 |
| 154 | |
| 155 | echo " |
| 156 | [ ca ] |
| 157 | default_ca = CA_default |
| 158 | |
| 159 | [ CA_default ] |
| 160 | dir = $ca_dir |
| 161 | policy = policy_match |
| 162 | database = \$dir/index.txt |
| 163 | serial = \$dir/serial |
| 164 | certs = \$dir/certs |
| 165 | crl_dir = \$dir/crl |
| 166 | new_certs_dir = \$dir/newcerts |
| 167 | certificate = \$dir/cacert.pem |
| 168 | private_key = \$dir/private/cacert.key |
| 169 | RANDFILE = \$dir/private/.rand |
| 170 | default_md = default |
| 171 | |
| 172 | [ req ] |
| 173 | default_bits = 1024 |
| 174 | default_md = sha1 |
| 175 | |
| 176 | prompt = no |
| 177 | distinguished_name = req_distinguished_name |
| 178 | |
| 179 | x509_extensions = req_extensions |
| 180 | |
| 181 | [ req_distinguished_name ] |
| 182 | organizationName = $ORG_NAME |
| 183 | organizationalUnitName = $ORG_UNIT_NAME Server Farm |
| 184 | |
| 185 | [ policy_match ] |
| 186 | countryName = optional |
| 187 | stateOrProvinceName = optional |
| 188 | organizationName = match |
| 189 | organizationalUnitName = optional |
| 190 | commonName = supplied |
| 191 | |
| 192 | [ req_extensions ] |
| 193 | basicConstraints = CA:false |
| 194 | subjectKeyIdentifier = hash |
| 195 | authorityKeyIdentifier = keyid:always, issuer |
| 196 | keyUsage = digitalSignature, keyEncipherment, keyAgreement |
| 197 | extendedKeyUsage = serverAuth, clientAuth |
| 198 | subjectAltName = \$ENV::SUBJECT_ALT_NAME |
| 199 | |
| 200 | " >$ca_dir/signing.conf |
| 201 | } |
| 202 | |
Dean Troyer | ca80217 | 2013-01-09 19:08:02 -0600 | [diff] [blame] | 203 | # Create root and intermediate CAs |
Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 204 | # init_CA |
| 205 | function init_CA { |
| 206 | # Ensure CAs are built |
| 207 | make_root_CA $ROOT_CA_DIR |
| 208 | make_int_CA $INT_CA_DIR $ROOT_CA_DIR |
| 209 | |
| 210 | # Create the CA bundle |
| 211 | cat $ROOT_CA_DIR/cacert.pem $INT_CA_DIR/cacert.pem >>$INT_CA_DIR/ca-chain.pem |
Dean Troyer | ca80217 | 2013-01-09 19:08:02 -0600 | [diff] [blame] | 212 | } |
Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 213 | |
Dean Troyer | ca80217 | 2013-01-09 19:08:02 -0600 | [diff] [blame] | 214 | # Create an initial server cert |
| 215 | # init_cert |
| 216 | function init_cert { |
Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 217 | if [[ ! -r $DEVSTACK_CERT ]]; then |
| 218 | if [[ -n "$TLS_IP" ]]; then |
| 219 | # Lie to let incomplete match routines work |
| 220 | TLS_IP="DNS:$TLS_IP" |
| 221 | fi |
| 222 | make_cert $INT_CA_DIR $DEVSTACK_CERT_NAME $DEVSTACK_HOSTNAME "$TLS_IP" |
| 223 | |
| 224 | # Create a cert bundle |
| 225 | cat $INT_CA_DIR/private/$DEVSTACK_CERT_NAME.key $INT_CA_DIR/$DEVSTACK_CERT_NAME.crt $INT_CA_DIR/cacert.pem >$DEVSTACK_CERT |
| 226 | fi |
| 227 | } |
| 228 | |
| 229 | |
| 230 | # make_cert creates and signs a new certificate with the given commonName and CA |
| 231 | # make_cert ca-dir cert-name "common-name" ["alt-name" ...] |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 232 | function make_cert { |
Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 233 | local ca_dir=$1 |
| 234 | local cert_name=$2 |
| 235 | local common_name=$3 |
| 236 | local alt_names=$4 |
| 237 | |
| 238 | # Generate a signing request |
| 239 | $OPENSSL req \ |
| 240 | -sha1 \ |
| 241 | -newkey rsa \ |
| 242 | -nodes \ |
| 243 | -keyout $ca_dir/private/$cert_name.key \ |
| 244 | -out $ca_dir/$cert_name.csr \ |
| 245 | -subj "/O=${ORG_NAME}/OU=${ORG_UNIT_NAME} Servers/CN=${common_name}" |
| 246 | |
| 247 | if [[ -z "$alt_names" ]]; then |
| 248 | alt_names="DNS:${common_name}" |
| 249 | else |
| 250 | alt_names="DNS:${common_name},${alt_names}" |
| 251 | fi |
| 252 | |
| 253 | # Sign the request valid for 1 year |
| 254 | SUBJECT_ALT_NAME="$alt_names" \ |
| 255 | $OPENSSL ca -config $ca_dir/signing.conf \ |
| 256 | -extensions req_extensions \ |
| 257 | -days 365 \ |
| 258 | -notext \ |
| 259 | -in $ca_dir/$cert_name.csr \ |
| 260 | -out $ca_dir/$cert_name.crt \ |
| 261 | -subj "/O=${ORG_NAME}/OU=${ORG_UNIT_NAME} Servers/CN=${common_name}" \ |
| 262 | -batch |
| 263 | } |
| 264 | |
| 265 | |
| 266 | # Make an intermediate CA to sign everything else |
| 267 | # make_int_CA ca-dir signing-ca-dir |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 268 | function make_int_CA { |
Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 269 | local ca_dir=$1 |
| 270 | local signing_ca_dir=$2 |
| 271 | |
| 272 | # Create the root CA |
| 273 | create_CA_base $ca_dir |
| 274 | create_CA_config $ca_dir 'Intermediate CA' |
| 275 | create_signing_config $ca_dir |
| 276 | |
| 277 | # Create a signing certificate request |
| 278 | $OPENSSL req -config $ca_dir/ca.conf \ |
| 279 | -sha1 \ |
| 280 | -newkey rsa \ |
| 281 | -nodes \ |
| 282 | -keyout $ca_dir/private/cacert.key \ |
| 283 | -out $ca_dir/cacert.csr \ |
| 284 | -outform PEM |
| 285 | |
| 286 | # Sign the intermediate request valid for 1 year |
| 287 | $OPENSSL ca -config $signing_ca_dir/ca.conf \ |
| 288 | -extensions ca_extensions \ |
| 289 | -days 365 \ |
| 290 | -notext \ |
| 291 | -in $ca_dir/cacert.csr \ |
| 292 | -out $ca_dir/cacert.pem \ |
| 293 | -batch |
| 294 | } |
| 295 | |
| 296 | # Make a root CA to sign other CAs |
| 297 | # make_root_CA ca-dir |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 298 | function make_root_CA { |
Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 299 | local ca_dir=$1 |
| 300 | |
| 301 | # Create the root CA |
| 302 | create_CA_base $ca_dir |
| 303 | create_CA_config $ca_dir 'Root CA' |
| 304 | |
| 305 | # Create a self-signed certificate valid for 5 years |
| 306 | $OPENSSL req -config $ca_dir/ca.conf \ |
| 307 | -x509 \ |
| 308 | -nodes \ |
| 309 | -newkey rsa \ |
| 310 | -days 21360 \ |
| 311 | -keyout $ca_dir/private/cacert.key \ |
| 312 | -out $ca_dir/cacert.pem \ |
| 313 | -outform PEM |
| 314 | } |
| 315 | |
| 316 | |
Jamie Lennox | bd24a8d | 2013-09-20 16:26:42 +1000 | [diff] [blame] | 317 | # Certificate Input Configuration |
| 318 | # =============================== |
| 319 | |
| 320 | # check to see if the service(s) specified are to be SSL enabled. |
| 321 | # |
| 322 | # Multiple services specified as arguments are ``OR``'ed together; the test |
| 323 | # is a short-circuit boolean, i.e it returns on the first match. |
| 324 | # |
| 325 | # Uses global ``SSL_ENABLED_SERVICES`` |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 326 | function is_ssl_enabled_service { |
Sean Dague | f0bd8db | 2014-07-23 15:14:07 -0400 | [diff] [blame] | 327 | local services=$@ |
| 328 | local service="" |
Jamie Lennox | bd24a8d | 2013-09-20 16:26:42 +1000 | [diff] [blame] | 329 | for service in ${services}; do |
| 330 | [[ ,${SSL_ENABLED_SERVICES}, =~ ,${service}, ]] && return 0 |
| 331 | done |
| 332 | return 1 |
| 333 | } |
| 334 | |
| 335 | |
| 336 | # Ensure that the certificates for a service are in place. This function does |
| 337 | # not check that a service is SSL enabled, this should already have been |
| 338 | # completed. |
| 339 | # |
| 340 | # The function expects to find a certificate, key and CA certificate in the |
| 341 | # variables {service}_SSL_CERT, {service}_SSL_KEY and {service}_SSL_CA. For |
| 342 | # example for keystone this would be KEYSTONE_SSL_CERT, KEYSTONE_SSL_KEY and |
| 343 | # KEYSTONE_SSL_CA. If it does not find these certificates the program will |
| 344 | # quit. |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 345 | function ensure_certificates { |
Jamie Lennox | bd24a8d | 2013-09-20 16:26:42 +1000 | [diff] [blame] | 346 | local service=$1 |
| 347 | |
| 348 | local cert_var="${service}_SSL_CERT" |
| 349 | local key_var="${service}_SSL_KEY" |
| 350 | local ca_var="${service}_SSL_CA" |
| 351 | |
| 352 | local cert=${!cert_var} |
| 353 | local key=${!key_var} |
| 354 | local ca=${!ca_var} |
| 355 | |
Solly Ross | 66115e5 | 2014-03-18 15:12:05 -0400 | [diff] [blame] | 356 | if [[ -z "$cert" || -z "$key" || -z "$ca" ]]; then |
Jamie Lennox | bd24a8d | 2013-09-20 16:26:42 +1000 | [diff] [blame] | 357 | die $LINENO "Missing either the ${cert_var} ${key_var} or ${ca_var}" \ |
| 358 | "variable to enable SSL for ${service}" |
| 359 | fi |
| 360 | |
| 361 | cat $ca >> $SSL_BUNDLE_FILE |
| 362 | } |
| 363 | |
| 364 | |
Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 365 | # Proxy Functions |
| 366 | # =============== |
| 367 | |
| 368 | # Starts the TLS proxy for the given IP/ports |
| 369 | # start_tls_proxy front-host front-port back-host back-port |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 370 | function start_tls_proxy { |
Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 371 | local f_host=$1 |
| 372 | local f_port=$2 |
| 373 | local b_host=$3 |
| 374 | local b_port=$4 |
| 375 | |
| 376 | stud $STUD_PROTO -f $f_host,$f_port -b $b_host,$b_port $DEVSTACK_CERT 2>/dev/null |
| 377 | } |
Sean Dague | 584d90e | 2013-03-29 14:34:53 -0400 | [diff] [blame] | 378 | |
Dean Troyer | cc6b443 | 2013-04-08 15:38:03 -0500 | [diff] [blame] | 379 | |
Stanislaw Pitucha | bd5dae0 | 2014-06-25 15:29:43 +0100 | [diff] [blame] | 380 | # Cleanup Functions |
| 381 | # =============== |
| 382 | |
| 383 | |
| 384 | # Stops all stud processes. This should be done only after all services |
| 385 | # using tls configuration are down. |
| 386 | function stop_tls_proxy { |
| 387 | killall stud |
| 388 | } |
| 389 | |
| 390 | |
| 391 | # Remove CA along with configuration, as well as the local server certificate |
| 392 | function cleanup_CA { |
| 393 | rm -rf "$DATA_DIR/CA" "$DEVSTACK_CERT" |
| 394 | } |
| 395 | |
Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 396 | # Tell emacs to use shell-script-mode |
| 397 | ## Local variables: |
| 398 | ## mode: shell-script |
| 399 | ## End: |