| 1 | #!/bin/sh |
| 2 | |
| 3 | # Copyright 2014 The Kubernetes Authors All rights reserved. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | . /etc/sysconfig/heat-params |
| 18 | |
| 19 | set -o errexit |
| 20 | set -o nounset |
| 21 | set -o pipefail |
| 22 | |
| 23 | if [ "$TLS_DISABLED" == "True" ]; then |
| 24 | exit 0 |
| 25 | fi |
| 26 | |
| 27 | cert_dir=/etc/kubernetes/ssl |
| 28 | |
| 29 | mkdir -p "$cert_dir" |
| 30 | |
| 31 | CA_CERT=$cert_dir/ca.crt |
| 32 | CLIENT_CERT=$cert_dir/client.crt |
| 33 | CLIENT_CSR=$cert_dir/client.csr |
| 34 | CLIENT_KEY=$cert_dir/client.key |
| 35 | KUBE_MASTER_URI="https://$KUBE_MASTER_IP:$KUBE_API_PORT" |
| 36 | |
| 37 | #Get a token by user credentials and trust |
| 38 | auth_json=$(cat << EOF |
| 39 | { |
| 40 | "auth": { |
| 41 | "identity": { |
| 42 | "methods": [ |
| 43 | "password" |
| 44 | ], |
| 45 | "password": { |
| 46 | "user": { |
| 47 | "id": "$TRUSTEE_USER_ID", |
| 48 | "password": "$TRUSTEE_PASSWORD" |
| 49 | } |
| 50 | } |
| 51 | }, |
| 52 | "scope": { |
| 53 | "OS-TRUST:trust": { |
| 54 | "id": "$TRUST_ID" |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | EOF |
| 60 | ) |
| 61 | |
| 62 | #trust is introduced in Keystone v3 version |
| 63 | AUTH_URL=${AUTH_URL/v2.0/v3} |
| 64 | content_type='Content-Type: application/json' |
| 65 | url="$AUTH_URL/auth/tokens" |
| 66 | USER_TOKEN=`curl -s -i -X POST -H "$content_type" -d "$auth_json" $url \ |
| 67 | | grep X-Subject-Token | awk '{print $2}' | tr -d '[[:space:]]'` |
| 68 | |
| 69 | # Get CA certificate for this cluster |
| 70 | curl -X GET \ |
| 71 | -H "X-Auth-Token: $USER_TOKEN" \ |
| 72 | $MAGNUM_URL/certificates/$CLUSTER_UUID | python -c 'import sys, json; print json.load(sys.stdin)["pem"]' > $CA_CERT |
| 73 | |
| 74 | # Create config for client's csr |
| 75 | cat > ${cert_dir}/client.conf <<EOF |
| 76 | [req] |
| 77 | distinguished_name = req_distinguished_name |
| 78 | req_extensions = req_ext |
| 79 | prompt = no |
| 80 | [req_distinguished_name] |
| 81 | CN = kubernetes.invalid |
| 82 | [req_ext] |
| 83 | keyUsage=critical,digitalSignature,keyEncipherment |
| 84 | extendedKeyUsage=clientAuth |
| 85 | subjectAltName=dirName:kubelet,dirName:kubeproxy |
| 86 | [kubelet] |
| 87 | CN=kubelet |
| 88 | [kubeproxy] |
| 89 | CN=kube-proxy |
| 90 | EOF |
| 91 | |
| 92 | # Generate client's private key and csr |
| 93 | openssl genrsa -out "${CLIENT_KEY}" 4096 |
| 94 | chmod 600 ${CLIENT_KEY} |
| 95 | openssl req -new -days 1000 \ |
| 96 | -key "${CLIENT_KEY}" \ |
| 97 | -out "${CLIENT_CSR}" \ |
| 98 | -reqexts req_ext \ |
| 99 | -config "${cert_dir}/client.conf" |
| 100 | |
| 101 | # Send csr to Magnum to have it signed |
| 102 | csr_req=$(python -c "import json; fp = open('${CLIENT_CSR}'); print json.dumps({'cluster_uuid': '$CLUSTER_UUID', 'csr': fp.read()}); fp.close()") |
| 103 | curl -X POST \ |
| 104 | -H "X-Auth-Token: $USER_TOKEN" \ |
| 105 | -H "Content-Type: application/json" \ |
| 106 | -d "$csr_req" \ |
| 107 | $MAGNUM_URL/certificates | python -c 'import sys, json; print json.load(sys.stdin)["pem"]' > ${CLIENT_CERT} |
| 108 | |
| 109 | chmod 700 ${cert_dir} |
| 110 | chmod 600 ${cert_dir}/* |
| 111 | chown -R kube:kube ${cert_dir} |
| 112 | |
| 113 | sed -i ' |
| 114 | s|CA_CERT|'"$CA_CERT"'| |
| 115 | s|CLIENT_CERT|'"$CLIENT_CERT"'| |
| 116 | s|CLIENT_KEY|'"$CLIENT_KEY"'| |
| 117 | s|KUBE_MASTER_URI|'"$KUBE_MASTER_URI"'| |
| 118 | ' /etc/kubernetes/kubeconfig.yaml |