forked from kyverno/kyverno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-server-cert.sh
executable file
·88 lines (77 loc) · 1.87 KB
/
generate-server-cert.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
#!/bin/bash
for i in "$@"
do
case $i in
--service=*)
service="${i#*=}"
shift
;;
--namespace=*)
namespace="${i#*=}"
shift
;;
--serverIp=*)
serverIp="${i#*=}"
shift
;;
esac
done
echo "service is $service"
echo "namespace is $namespace"
echo "serverIp is $serverIp"
destdir="certs"
if [ ! -d "$destdir" ]; then
mkdir ${destdir} || exit 1
fi
tmpdir=$(mktemp -d)
cat <<EOF >> ${tmpdir}/csr.conf
[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 = ${service}
DNS.2 = ${service}.${namespace}
DNS.3 = ${service}.${namespace}.svc
DNS.4 = ${serverIp}
EOF
outKeyFile=${destdir}/server-key.pem
outCertFile=${destdir}/server.crt
openssl genrsa -out ${outKeyFile} 2048 || exit 2
if [ ! -z "${service}" ]; then
if [ ! -z "${namespace}" ]; then
subjectCN="${service}.${namespace}.svc"
else
subjectCN="${service}"
fi
else
subjectCN=${serverIp}
fi
echo "Generating certificate for CN=${subjectCN}"
openssl req -new -key ${destdir}/server-key.pem -subj "/CN=${subjectCN}" -out ${tmpdir}/server.csr -config ${tmpdir}/csr.conf || exit 3
CSR_NAME=${service}.cert-request
kubectl delete csr ${CSR_NAME} 2>/dev/null
cat <<EOF | kubectl create -f -
apiVersion: certificates.k8s.io/v1beta1
kind: CertificateSigningRequest
metadata:
name: ${CSR_NAME}
spec:
groups:
- system:authenticated
request: $(cat ${tmpdir}/server.csr | base64 | tr -d '\n')
usages:
- digital signature
- key encipherment
- server auth
EOF
kubectl certificate approve ${CSR_NAME} || exit 4
kubectl get csr ${CSR_NAME} -o jsonpath='{.status.certificate}' | base64 --decode > ${outCertFile} || exit 5
echo "Generated:"
echo ${outKeyFile}
echo ${outCertFile}