-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpki-gen
executable file
·358 lines (311 loc) · 13.1 KB
/
pki-gen
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/bin/bash
# Quick and dirty bash script to generate PKI for Kubernetes using cfssl
############################################################
## Variables
ANSIBLE_DIR="../ansible"
ANSIBLE_INVENTORY="ansible-inventory -i production"
ANSIBLE_COMMON_K8S_DIR="${ANSIBLE_DIR}/roles/kubernetes_common/files/etc/kubernetes"
ANSIBLE_CONTROLLER_K8S_DIR="${ANSIBLE_DIR}/roles/kubernetes_controller/files/etc/kubernetes"
ANSIBLE_COMMON_PKI_DIR="${ANSIBLE_COMMON_K8S_DIR}/pki"
ANSIBLE_CONTROLLER_PKI_DIR="${ANSIBLE_CONTROLLER_K8S_DIR}/pki"
ANSIBLE_CONTROLLER_ETCD_DIR="${ANSIBLE_CONTROLLER_PKI_DIR}/etcd"
ANSIBLE_CONTROLLER_HOSTS=$(cd ${ANSIBLE_DIR} && ${ANSIBLE_INVENTORY} --graph k8s_controllers | grep '|' | cut -d '-' -f3-6)
ANSIBLE_WORKER_HOSTS=$(cd ${ANSIBLE_DIR} && ${ANSIBLE_INVENTORY} --graph k8s_workers | grep '|' | cut -d '-' -f3-6)
ANSIBLE_ALL_HOSTS=$(cd ${ANSIBLE_DIR} && ${ANSIBLE_INVENTORY} --graph k8s_all | grep '|' | cut -d '-' -f3-6 | grep -v '@')
K8S_CA_NAME="kubernetes-ca"
ETCD_CA_GENCERT="cfssl gencert -ca=etcd-ca.pem -ca-key=etcd-ca-key.pem --config=ca-config.json -profile=etcd"
K8S_CA_GENCERT="cfssl gencert -ca=${K8S_CA_NAME}.pem -ca-key=${K8S_CA_NAME}-key.pem --config=ca-config.json -profile=kubernetes"
PROXY_CA_GENCERT="cfssl gencert -ca=front-proxy-ca.pem -ca-key=front-proxy-ca-key.pem --config=ca-config.json -profile=proxy"
KUBERNETES_CONTROLLER_IPS=$(
for host in ${ANSIBLE_CONTROLLER_HOSTS}; do
ipv4=$(cd ${ANSIBLE_DIR} && ${ANSIBLE_INVENTORY} -y --host "${host}" | grep 'ansible_host'| head -n 1 | awk '{print $2}')
echo -n "$ipv4,"
done
)
DOMAIN=$(cd ${ANSIBLE_DIR} && ${ANSIBLE_INVENTORY} --graph k8s_controllers | grep '|' | cut -d '-' -f3-6 | head -n 1 | xargs "${ANSIBLE_INVENTORY}" -y --host | grep 'domain'| head -n 1 | awk '{print $2}')
CLUSTER_NAME=$(cd ${ANSIBLE_DIR} && ${ANSIBLE_INVENTORY} --graph k8s_controllers | grep '|' | cut -d '-' -f3-6 | head -n 1 | xargs "${ANSIBLE_INVENTORY}" -y --host | grep 'kubernetes_cluster_name'| head -n 1 | awk '{print $2}')
CLUSTER_IP=$(cd ${ANSIBLE_DIR} && ${ANSIBLE_INVENTORY} --graph k8s_controllers | grep '|' | cut -d '-' -f3-6 | head -n 1 | xargs "${ANSIBLE_INVENTORY}" -y --host | grep 'pacemaker_cluster_ip'| head -n 1 | awk '{print $2}')
CLUSTER_FQDN=$(cd ${ANSIBLE_DIR} && ${ANSIBLE_INVENTORY} --graph k8s_controllers | grep '|' | cut -d '-' -f3-6 | head -n 1 | xargs "${ANSIBLE_INVENTORY}" -y --host | grep 'kubernetes_cluster_fqdn'| head -n 1 | awk '{print $2}' | cut -d '.' -f1).${DOMAIN}
CALICO_POD_IP="10.96.0.1"
############################################################
## Validation
# Formatting shortcuts for readability
BOLD='\e[1m'
CLEAR='\e[0m'
# Check if requirements are met
commands=(cfssl cfssljson ansible-inventory sops)
for command in "${commands[@]}"; do
if [[ ! $(which "$command") ]]; then
echo -e "\n$command command not found!\n"
exit 1
fi
done
# Avaliable funtions
allowed_functions=(
"all"
"ssl_ca"
"ssl_crts"
"etcd"
"kubernetes"
"proxy"
"sealed_secrets"
"clean"
"clean_ca"
"clean_crts"
"clean_etcd"
"clean_kubernetes"
"clean_proxy"
"clean_sealed_secrets"
)
function help_text {
echo "PKI Generation script."
echo ""
echo "Creates PKI certificates for Kubernetes using cfssl."
echo "https://github.com/cloudflare/cfssl"
echo ""
echo "Requires the following things setup and working:"
for command in "${commands[@]}"; do
echo "- ${command}"
done
echo ""
echo -e "${BOLD}usage: $0 function${CLEAR}"
echo ""
echo "Avaliable functions:"
for function in "${allowed_functions[@]}"; do
echo "- ${function}"
done
echo ""
}
# Ensure exactly one permitted argument
if [[ $# -ne 1 ]] || [[ ! "${allowed_functions[*]}" =~ $1 ]]; then
help_text
exit 1
fi
############################################################
### Create Functions
function ssl_ca_kubernetes {
filename="${K8S_CA_NAME}"
cfssl gencert -initca "${filename}-csr.json" | cfssljson -bare "$filename"
sops -e "${filename}.pem" > "${ANSIBLE_COMMON_PKI_DIR}/ca.crt"
sops -e "${filename}.pem" > "${ANSIBLE_CONTROLLER_PKI_DIR}/ca.crt"
}
function ssl_ca_etcd {
filename=etcd-ca
cfssl gencert -initca "${filename}-csr.json" | cfssljson -bare "$filename"
sops -e "${filename}.pem" > "${ANSIBLE_CONTROLLER_ETCD_DIR}/ca.crt"
}
function ssl_ca_front_proxy {
filename=front-proxy-ca
cfssl gencert -initca ${filename}-csr.json | cfssljson -bare "$filename"
sops -e "${filename}.pem" > "${ANSIBLE_CONTROLLER_PKI_DIR}/${filename}.crt"
}
function ssl_crt_etcd_etcd_controller_hosts {
csr=$1
for host in ${ANSIBLE_CONTROLLER_HOSTS}; do
filename=${1}-${host}
ipv4=$(cd ${ANSIBLE_DIR} && ${ANSIBLE_INVENTORY} -y --host "${host}" | grep 'ansible_host'| head -n 1 | awk '{print $2}')
${ETCD_CA_GENCERT} --hostname="${host},${ipv4},localhost,127.0.0.1" --cn="${host}" "etcd-${csr}-csr.json" | cfssljson -bare "etcd-${filename}"
sops -e "etcd-${filename}.pem" > "${ANSIBLE_CONTROLLER_ETCD_DIR}/${filename}.crt"
sops -e "etcd-${filename}-key.pem" > "${ANSIBLE_CONTROLLER_ETCD_DIR}/${filename}.key"
done
}
function ssl_crt_etcd_etcd {
filename=$1
${ETCD_CA_GENCERT} "etcd-${filename}-csr.json" | cfssljson -bare "etcd-${filename}"
sops -e "etcd-${filename}.pem" > "${ANSIBLE_CONTROLLER_ETCD_DIR}/${filename}.crt"
sops -e "etcd-${filename}-key.pem" > "${ANSIBLE_CONTROLLER_ETCD_DIR}/${filename}.key"
}
function ssl_crt_etcd {
filename=$1
${ETCD_CA_GENCERT} "${filename}-csr.json" | cfssljson -bare "${filename}"
sops -e "${filename}.pem" > "${ANSIBLE_CONTROLLER_PKI_DIR}/${filename}.crt"
sops -e "${filename}-key.pem" > "${ANSIBLE_CONTROLLER_PKI_DIR}/${filename}.key"
}
function ssl_crt_kube_apiserver {
filename=apiserver
${K8S_CA_GENCERT} --hostname="${CLUSTER_FQDN},${KUBERNETES_HOSTNAMES},${CLUSTER_IP},${KUBERNETES_CONTROLLER_IPS::-1},${CALICO_POD_IP},127.0.0.1" "${filename}-csr.json" | cfssljson -bare "${filename}"
sops -e "${filename}.pem" > "${ANSIBLE_CONTROLLER_PKI_DIR}/${filename}.crt"
sops -e "${filename}-key.pem" > "${ANSIBLE_CONTROLLER_PKI_DIR}/${filename}.key"
}
function ssl_crt_k8s {
filename="$1"
${K8S_CA_GENCERT} "${filename}-csr.json" | cfssljson -bare "${filename}"
sops -e "${filename}.pem" > "${ANSIBLE_CONTROLLER_PKI_DIR}/${filename}.crt"
sops -e "${filename}-key.pem" > "${ANSIBLE_CONTROLLER_PKI_DIR}/${filename}.key"
}
function ssl_crt_kubelet_client {
for host in ${ANSIBLE_WORKER_HOSTS}; do
filename="apiserver-kubelet-client-${host}"
ipv4=$(cd ${ANSIBLE_DIR} && ${ANSIBLE_INVENTORY} -y --host "${host}" | grep 'ansible_host'| head -n 1 | awk '{print $2}')
${K8S_CA_GENCERT} --hostname="${host}","${ipv4}" --cn="system:node:${host}" kubelet-client-csr.json | cfssljson -bare "${filename}"
sops -e "${filename}.pem" > "${ANSIBLE_CONTROLLER_PKI_DIR}/${filename}.crt"
sops -e "${filename}-key.pem" > "${ANSIBLE_CONTROLLER_PKI_DIR}/${filename}.key"
done
}
function ssl_crt_proxy {
filename="$1"
${PROXY_CA_GENCERT} "${filename}-csr.json" | cfssljson -bare "${filename}"
sops -e "${filename}.pem" > "${ANSIBLE_CONTROLLER_PKI_DIR}/${filename}.crt"
sops -e "${filename}-key.pem" > "${ANSIBLE_CONTROLLER_PKI_DIR}/${filename}.key"
}
function ssl_crt_kubeconfig_admin {
for host in ${ANSIBLE_CONTROLLER_HOSTS}; do
filename="admin-${host}"
${K8S_CA_GENCERT} admin-csr.json | cfssljson -bare "${filename}"
kubectl config set-cluster "${CLUSTER_NAME}" --certificate-authority="${K8S_CA_NAME}.pem" --embed-certs=true --server="https://${CLUSTER_FQDN}:6443" --kubeconfig="${filename}.kubeconfig"
kubectl config set-credentials "${filename}" --client-certificate="${filename}.pem" --client-key="${filename}-key.pem" --embed-certs=true --kubeconfig="${filename}.kubeconfig"
kubectl config set-context default --cluster="${CLUSTER_NAME}" --user="${filename}" --kubeconfig="${filename}.kubeconfig"
kubectl config use-context default --kubeconfig="${filename}.kubeconfig"
sops -e "${filename}.kubeconfig" > "${ANSIBLE_CONTROLLER_K8S_DIR}/${filename}.conf"
done
}
function ssl_crt_kubeconfig_kublet {
for host in ${ANSIBLE_ALL_HOSTS}; do
filename="kubelet-${host}"
${K8S_CA_GENCERT} --hostname="${host}" --cn="system:node:${host}" kubelet-csr.json | cfssljson -bare "${filename}"
kubectl config set-cluster "${CLUSTER_NAME}" --certificate-authority="${K8S_CA_NAME}.pem" --embed-certs=true --server="https://${CLUSTER_FQDN}:6443" --kubeconfig="${filename}.kubeconfig"
kubectl config set-credentials "system:node:${host}" --client-certificate="${filename}.pem" --client-key="${filename}-key.pem" --embed-certs=true --kubeconfig="${filename}.kubeconfig"
kubectl config set-context "system:node:${host}" --cluster="${CLUSTER_NAME}" --user="system:node:${host}" --kubeconfig="${filename}.kubeconfig"
kubectl config use-context "system:node:${host}" --kubeconfig="${filename}.kubeconfig"
sops -e "${filename}.kubeconfig" > "${ANSIBLE_COMMON_K8S_DIR}/${filename}.conf"
done
}
function ssl_crt_kubeconfig_system {
csr=$1
for host in ${ANSIBLE_CONTROLLER_HOSTS}; do
filename="${1}-${host}"
ansible_filename=$(echo "${filename}"|cut -d '-' -f 2-10)
ipv4=$(cd ${ANSIBLE_DIR} && ${ANSIBLE_INVENTORY} -y --host "${host}" | grep 'ansible_host'| head -n 1 | awk '{print $2}')
${K8S_CA_GENCERT} "${csr}-csr.json" | cfssljson -bare "${filename}"
kubectl config set-cluster "${CLUSTER_NAME}" --certificate-authority="${K8S_CA_NAME}.pem" --embed-certs=true --server="https://${ipv4}:6443" --kubeconfig="${filename}.kubeconfig"
kubectl config set-credentials "system:${filename}" --client-certificate="${filename}.pem" --client-key="${filename}-key.pem" --embed-certs=true --kubeconfig="${filename}.kubeconfig"
kubectl config set-context default --cluster="${CLUSTER_NAME}" --user="system:${filename}" --kubeconfig="${filename}.kubeconfig"
kubectl config use-context default --kubeconfig="${filename}.kubeconfig"
sops -e "${filename}.kubeconfig" > "${ANSIBLE_CONTROLLER_K8S_DIR}/${ansible_filename}.conf"
done
}
function ssl_crt_sa {
filename="sa"
openssl genrsa -out "${filename}.key" 3072
openssl rsa -in "${filename}.key" -pubout -out "${filename}.pub"
sops -e "${filename}.pub" > "${ANSIBLE_CONTROLLER_PKI_DIR}/${filename}.pub"
sops -e "${filename}.key" > "${ANSIBLE_CONTROLLER_PKI_DIR}/${filename}.key"
}
function ssl_ca {
ssl_ca_kubernetes
ssl_ca_etcd
ssl_ca_front_proxy
}
function ssl_crts {
ssl_crt_etcd_etcd_controller_hosts server
ssl_crt_etcd_etcd_controller_hosts peer
ssl_crt_etcd_etcd healthcheck-client
ssl_crt_etcd apiserver-etcd-client
ssl_crt_kube_apiserver
ssl_crt_k8s apiserver-kubelet-client
ssl_crt_proxy front-proxy-client
ssl_crt_kubeconfig_admin
ssl_crt_kubeconfig_kublet
ssl_crt_kubeconfig_system kube-controller-manager
ssl_crt_kubeconfig_system kube-scheduler
ssl_crt_sa
}
function all {
ssl_ca
ssl_crts
}
function etcd {
ssl_ca_etcd
ssl_crt_etcd_etcd_controller_hosts server
ssl_crt_etcd_etcd_controller_hosts peer
ssl_crt_etcd_etcd healthcheck-client
ssl_crt_etcd apiserver-etcd-client
}
function kubernetes {
ssl_ca_kubernetes
ssl_crt_kube_apiserver
ssl_crt_k8s apiserver-kubelet-client
ssl_crt_kubeconfig_admin
ssl_crt_kubeconfig_kublet
ssl_crt_kubeconfig_system kube-controller-manager
ssl_crt_kubeconfig_system kube-scheduler
ssl_crt_sa
}
function proxy {
ssl_ca_front_proxy
ssl_crt_proxy front-proxy-client
}
function sealed_secrets {
filename="sealed-secrets"
openssl req -x509 -nodes -newkey rsa:4096 -keyout "${filename}.key" -out "${filename}.pem" -subj "/CN=sealed-secret/O=sealed-secret"
cp ${filename}.pem ${ANSIBLE_CONTROLLER_K8S_DIR}/secrets/${filename}.pem
kubectl create secret tls custom-sealed-secrets-key --cert="${filename}.pem" --key="${filename}.key" -n "kube-system" --dry-run=client -o yaml > ${filename}.yaml
sops -e ${filename}.yaml > ${ANSIBLE_CONTROLLER_K8S_DIR}/secrets/${filename}.yaml
}
############################################################
### Clean Functions
function clean_crt_hosts {
rm "${1}"-*{.pem,-key.pem,.csr}
clean_kubeconfig "${1}"
}
function clean_crt_single {
rm "${1}"{.pem,-key.pem,.csr}
clean_kubeconfig "${1}"
}
function clean_kubeconfig {
rm "${1}"*.kubeconfig
}
function clean_keypair {
rm "${1}"{.pub,.key}
}
function clean_ca {
clean_crt_single kubernetes-ca
clean_crt_single etcd-ca
clean_crt_single front-proxy-ca
}
function clean_crts {
clean_crt_hosts etcd-server
clean_crt_hosts etcd-peer
clean_crt_single etcd-healthcheck-client
clean_crt_single apiserver-etcd-client
clean_crt_single apiserver
clean_crt_single apiserver-kubelet-client
clean_crt_single front-proxy-client
clean_crt_hosts admin
clean_crt_hosts kubelet
clean_crt_hosts kube-controller-manager
clean_crt_hosts kube-scheduler
clean_keypair sa
}
function clean {
clean_ca
clean_crts
}
function clean_etcd {
clean_crt_single etcd-ca
clean_crt_hosts etcd-server
clean_crt_hosts etcd-peer
clean_crt_single etcd-healthcheck-client
clean_crt_single apiserver-etcd-client
}
function clean_kubernetes {
clean_crt_single kubernetes-ca
clean_crt_single apiserver
clean_crt_single apiserver-kubelet-client
clean_crt_single admin
clean_crt_hosts kubelet
clean_crt_hosts kube-controller-manager
clean_crt_hosts kube-scheduler
clean_keypair sa
}
function clean_front_proxy {
clean_crt_single front-proxy-ca
clean_crt_single front-proxy-client
}
function clean_sealed_secrets {
rm sealed-secrets{.pem,.key}
}
############################################################
# Execute function from argument
$1
exit 1