Debian Codegraph @

contrib/drivers/k8s_opensuse_v1/templates/fragments/make-cert.sh

raw symbols SCIP
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 if [[ -z "${KUBE_NODE_PUBLIC_IP}" ]]; then
28 KUBE_NODE_PUBLIC_IP=$(curl -s http://169.254.169.254/latest/meta-data/public-ipv4)
29 fi
30 if [[ -z "${KUBE_NODE_IP}" ]]; then
31 KUBE_NODE_IP=$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4)
32 fi
33
34 sans="IP:${KUBE_NODE_PUBLIC_IP},IP:${KUBE_NODE_IP}"
35 if [ "${KUBE_NODE_PUBLIC_IP}" != "${KUBE_API_PUBLIC_ADDRESS}" ] \
36 && [ -n "${KUBE_API_PUBLIC_ADDRESS}" ]; then
37 sans="${sans},IP:${KUBE_API_PUBLIC_ADDRESS}"
38 fi
39 if [ "${KUBE_NODE_IP}" != "${KUBE_API_PRIVATE_ADDRESS}" ] \
40 && [ -n "${KUBE_API_PRIVATE_ADDRESS}" ]; then
41 sans="${sans},IP:${KUBE_API_PRIVATE_ADDRESS}"
42 fi
43 MASTER_HOSTNAME=${MASTER_HOSTNAME:-}
44 if [[ -n "${MASTER_HOSTNAME}" ]]; then
45 sans="${sans},DNS:${MASTER_HOSTNAME}"
46 fi
47 sans="${sans},IP:127.0.0.1"
48
49 cert_dir=/etc/kubernetes/ssl
50
51 mkdir -p "$cert_dir"
52
53 CA_CERT=$cert_dir/ca.crt
54 SERVER_CERT=$cert_dir/server.crt
55 SERVER_CSR=$cert_dir/server.csr
56 SERVER_KEY=$cert_dir/server.key
57
58 #Get a token by user credentials and trust
59 auth_json=$(cat << EOF
60 {
61 "auth": {
62 "identity": {
63 "methods": [
64 "password"
65 ],
66 "password": {
67 "user": {
68 "id": "$TRUSTEE_USER_ID",
69 "password": "$TRUSTEE_PASSWORD"
70 }
71 }
72 },
73 "scope": {
74 "OS-TRUST:trust": {
75 "id": "$TRUST_ID"
76 }
77 }
78 }
79 }
80 EOF
81 )
82
83 #trust is introduced in Keystone v3 version
84 AUTH_URL=${AUTH_URL/v2.0/v3}
85 content_type='Content-Type: application/json'
86 url="$AUTH_URL/auth/tokens"
87 USER_TOKEN=`curl -s -i -X POST -H "$content_type" -d "$auth_json" $url \
88 | grep X-Subject-Token | awk '{print $2}' | tr -d '[[:space:]]'`
89
90 # Get CA certificate for this cluster
91 curl -X GET \
92 -H "X-Auth-Token: $USER_TOKEN" \
93 $MAGNUM_URL/certificates/$CLUSTER_UUID | python -c 'import sys, json; print json.load(sys.stdin)["pem"]' > ${CA_CERT}
94
95 # Create config for server's csr
96 cat > ${cert_dir}/server.conf <<EOF
97 [req]
98 distinguished_name = req_distinguished_name
99 req_extensions = req_ext
100 prompt = no
101 [req_distinguished_name]
102 CN = kubernetes.invalid
103 [req_ext]
104 subjectAltName = ${sans}
105 extendedKeyUsage = clientAuth,serverAuth
106 EOF
107
108 # Generate server's private key and csr
109 openssl genrsa -out "${SERVER_KEY}" 4096
110 chmod 600 ${SERVER_KEY}
111 openssl req -new -days 1000 \
112 -key "${SERVER_KEY}" \
113 -out "${SERVER_CSR}" \
114 -reqexts req_ext \
115 -config "${cert_dir}/server.conf"
116
117 # Send csr to Magnum to have it signed
118 csr_req=$(python -c "import json; fp = open('${SERVER_CSR}'); print json.dumps({'cluster_uuid': '$CLUSTER_UUID', 'csr': fp.read()}); fp.close()")
119 curl -X POST \
120 -H "X-Auth-Token: $USER_TOKEN" \
121 -H "Content-Type: application/json" \
122 -d "$csr_req" \
123 $MAGNUM_URL/certificates | python -c 'import sys, json; print json.load(sys.stdin)["pem"]' > ${SERVER_CERT}
124
125 chmod 700 ${cert_dir}
126 chmod 600 ${cert_dir}/*
127 chown -R kube:kube ${cert_dir}