This repository has been archived by the owner on Sep 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathssl.sh
executable file
·82 lines (72 loc) · 1.82 KB
/
ssl.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
#! /bin/sh
set -eo pipefail
export APP="${1:-host-mutator}"
export NAMESPACE="${2:-default}"
export CSR_NAME="${APP}.${NAMESPACE}.svc"
rm -rf ssl && mkdir ssl && cd ssl
echo "Creating cert.key"
openssl genrsa -out cert.key 2048
echo "Creating csr.conf"
cat > csr.conf <<EOF
[req]
req_extensions = v3_req
distinguished_name = req_distinguished_name
[req_distinguished_name]
[ v3_req ]
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names
[alt_names]
DNS.1 = ${APP}
DNS.2 = ${APP}.${NAMESPACE}
DNS.3 = ${CSR_NAME}
DNS.4 = ${CSR_NAME}.cluster.local
EOF
openssl req -new -key cert.key -subj "/CN=${CSR_NAME}" -out cert.csr -config csr.conf
echo "Deleting existing csr, if any"
kubectl delete csr "$CSR_NAME" || true
echo "Creating kubernetes CSR object"
echo "kubectl create -f -"
kubectl create -f - <<EOF
apiVersion: certificates.k8s.io/v1beta1
kind: CertificateSigningRequest
metadata:
name: ${CSR_NAME}
spec:
groups:
- system:authenticated
request: $(base64 -i cert.csr | tr -d '\n')
usages:
- digital signature
- key encipherment
- server auth
EOF
SECONDS=0
while true; do
echo "Waiting for csr to be present in kubernetes"
if kubectl get csr "$CSR_NAME" > /dev/null 2>&1; then
break
fi
if [[ $SECONDS -ge 60 ]]; then
echo "Timed out waiting for csr"
exit 1
fi
sleep 2
done
kubectl certificate approve "$CSR_NAME"
SECONDS=0
while true; do
echo "Waiting for serverCert to be present in kubernetes"
serverCert=$(kubectl get csr "$CSR_NAME" -o jsonpath='{.status.certificate}')
if [[ $serverCert != "" ]]; then
break
fi
if [[ $SECONDS -ge 60 ]]; then
echo "Timed out waiting for serverCert"
exit 1
fi
sleep 2
done
echo "Creating cert.pem"
echo "$serverCert" | openssl base64 -d -A -out cert.pem