forked from sjenning/hosted-cluster-poc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make-pki.sh
executable file
·179 lines (140 loc) · 5.36 KB
/
make-pki.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#/bin/bash
set -e
source config-defaults.sh
mkdir -p pki
cd pki
function generate_client_key_cert() {
ca=$1
file=$2
user=$3
org=$4
hostname="$5"
if [ -f "${file}.pem" ]; then return 0; fi
cat > ${file}-csr.json <<EOF
{
"CN": "${user}",
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"O": "${org}"
}
]
}
EOF
cfssl gencert \
-ca=${ca}.pem \
-ca-key=${ca}-key.pem \
-config=ca-config.json \
-profile=kubernetes \
-hostname="${hostname}" \
${file}-csr.json | cfssljson -bare ${file}
rm ${file}-csr.json ${file}.csr
}
function generate_client_kubeconfig() {
ca=$1
file=$2
name=$3
server="kube-apiserver:6443"
if [ ! -z "${6}" ]; then
server="${6}"
fi
if [ -f "${file}.kubeconfig" ]; then return 0; fi
generate_client_key_cert "${1}" "${2}" "${3}" "${4}" "${5}"
kubectl config set-cluster default \
--certificate-authority=root-ca.pem \
--embed-certs=true \
--server=https://${server} \
--kubeconfig=${file}.kubeconfig
kubectl config set-credentials ${file} \
--client-certificate=${file}.pem \
--client-key=${file}-key.pem \
--embed-certs=true \
--kubeconfig=${file}.kubeconfig
kubectl config set-context default \
--cluster=default \
--user=${file} \
--kubeconfig=${file}.kubeconfig
kubectl config use-context default --kubeconfig=${file}.kubeconfig
rm ${file}.pem ${file}-key.pem
}
function generate_ca() {
name="$1"
if [ -f "${name}.pem" ]; then return 0; fi
cat <<EOF | cfssl gencert -initca - | cfssljson -bare ${name}
{
"CN": "${name}",
"key": {
"algo": "rsa",
"size": 2048
}
}
EOF
}
function generate_secret() {
file=$2
if [ -f "${file}-tls.yaml" ]; then return 0; fi
generate_client_key_cert "${1}" "${2}" "${3}" "${4}" "${5}"
export SECRET_NAME=${file}
export SECRET_CERT=$(cat ${file}.pem | base64 | tr -d '\n')
export SECRET_KEY=$(cat ${file}-key.pem | base64 | tr -d '\n')
envsubst < ../templates/secret-tls-template.yaml > ${file}-tls.yaml
}
# generate ca-config
cat > ca-config.json <<EOF
{
"signing": {
"default": {
"expiry": "8760h"
},
"profiles": {
"kubernetes": {
"usages": ["signing", "key encipherment", "server auth", "client auth"],
"expiry": "8760h"
}
}
}
}
EOF
# generate CAs
generate_ca "root-ca"
generate_ca "cluster-signer"
# admin kubeconfig
generate_client_kubeconfig "root-ca" "admin" "system:admin" "system:masters" "" "${EXTERNAL_API_DNS_NAME}:${EXTERNAL_API_PORT}"
# kubelet bootstrapper kubeconfig
generate_client_kubeconfig "cluster-signer" "kubelet-bootstrap" "system:bootstrapper" "system:bootstrappers" "" "${EXTERNAL_API_DNS_NAME}:${EXTERNAL_API_PORT}"
# service client admin kubeconfig
generate_client_kubeconfig "root-ca" "service-admin" "system:admin" "system:masters" "kube-apiserver"
# kube-controller-manager
generate_client_kubeconfig "root-ca" "kube-controller-manager" "system:admin" "system:masters" "kube-apiserver"
if [ ! -e "service-account-key.pem" ]; then
openssl genrsa -out service-account-key.pem 2048
openssl rsa -in service-account-key.pem -pubout > service-account.pem
fi
# kube-scheduler
generate_client_kubeconfig "root-ca" "kube-scheduler" "system:admin" "system:masters"
# kube-apiserver
generate_client_key_cert "root-ca" "kube-apiserver-server" "kubernetes" "kubernetes" "${EXTERNAL_API_DNS_NAME},${SERVICE_NETWORK_PREFIX}.0.1,${EXTERNAL_API_IP_ADDRESS},kubernetes,kubernetes.default.svc,kubernetes.default.svc.cluster.local,kube-apiserver,kube-apiserver.${NAMESPACE}.svc,kube-apiserver.${NAMESPACE}.svc.cluster.local"
generate_client_key_cert "root-ca" "kube-apiserver-kubelet" "system:kube-apiserver" "kubernetes"
generate_client_key_cert "root-ca" "kube-apiserver-aggregator-proxy-client" "system:openshift-aggregator" "kubernetes"
# etcd
generate_client_key_cert "root-ca" "etcd-client" "etcd-client" "kubernetes"
generate_client_key_cert "root-ca" "etcd-server" "etcd-server" "kubernetes" "*.etcd.${NAMESPACE}.svc,etcd-client.${NAMESPACE}.svc,etcd,etcd-client,localhost"
generate_client_key_cert "root-ca" "etcd-peer" "etcd-peer" "kubernetes" "*.etcd.${NAMESPACE}.svc,*.etcd.${NAMESPACE}.svc.cluster.local"
# openshift-apiserver
generate_client_key_cert "root-ca" "openshift-apiserver-server" "openshift-apiserver" "openshift" "openshift-apiserver,openshift-apiserver.${NAMESPACE}.svc,openshift-controller-manager.${NAMESPACE}.svc.cluster.local,openshift-apiserver.default.svc,openshift-apiserver.default.svc.cluster.local"
# openshift-controller-manager
generate_client_key_cert "root-ca" "openshift-controller-manager-server" "openshift-controller-manager" "openshift" "openshift-controller-manager,openshift-controller-manager.${NAMESPACE}.svc,openshift-controller-manager.${NAMESPACE}.svc.cluster.local"
cat root-ca.pem cluster-signer.pem > combined-ca.pem
rm -f *.csr
# openvpn assets
generate_ca "openvpn-ca"
generate_client_key_cert "openvpn-ca" "openvpn-server" "server" "kubernetes" "openvpn-server,openvpn-server.${NAMESPACE}.svc,${EXTERNAL_API_DNS_NAME}:${OPENVPN_NODEPORT}"
generate_client_key_cert "openvpn-ca" "openvpn-kube-apiserver-client" "kube-apiserver" "kubernetes"
generate_client_key_cert "openvpn-ca" "openvpn-worker-client" "worker" "kubernetes"
if [ ! -e "openvpn-dh.pem" ]; then
# this might be slow, lots of entropy required
openssl dhparam -out openvpn-dh.pem 2048
fi