2019-02-12 14:12:03 +00:00
|
|
|
#!/bin/bash
|
2019-02-13 17:57:18 +00:00
|
|
|
|
|
|
|
for i in "$@"
|
|
|
|
do
|
|
|
|
case $i in
|
|
|
|
--service=*)
|
|
|
|
service="${i#*=}"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
--namespace=*)
|
|
|
|
namespace="${i#*=}"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
--serverIp=*)
|
|
|
|
serverIp="${i#*=}"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2019-02-13 13:28:16 +00:00
|
|
|
echo "service is $service"
|
|
|
|
echo "namespace is $namespace"
|
|
|
|
echo "serverIp is $serverIp"
|
|
|
|
|
2019-02-12 14:12:03 +00:00
|
|
|
destdir="certs"
|
|
|
|
if [ ! -d "$destdir" ]; then
|
2019-02-13 13:28:16 +00:00
|
|
|
mkdir ${destdir} || exit 1
|
2019-02-12 14:12:03 +00:00
|
|
|
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
|
|
|
|
|
2019-02-13 13:28:16 +00:00
|
|
|
openssl genrsa -out ${outKeyFile} 2048 || exit 2
|
2019-02-19 16:01:47 +00:00
|
|
|
|
2019-02-13 17:57:18 +00:00
|
|
|
if [ ! -z "${service}" ]; then
|
2019-02-19 16:01:47 +00:00
|
|
|
if [ ! -z "${namespace}" ]; then
|
|
|
|
subjectCN="${service}.${namespace}.svc"
|
|
|
|
else
|
|
|
|
subjectCN="${service}"
|
|
|
|
fi
|
2019-02-13 17:57:18 +00:00
|
|
|
else
|
|
|
|
subjectCN=${serverIp}
|
|
|
|
fi
|
2019-02-19 16:01:47 +00:00
|
|
|
echo "Generating certificate for CN=${subjectCN}"
|
2019-02-21 16:13:21 +00:00
|
|
|
openssl req -new -key ${destdir}/server-key.pem -subj "/CN=${subjectCN}" -out ${tmpdir}/server.csr -config ${tmpdir}/csr.conf || exit 3
|
2019-02-12 14:12:03 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2019-02-13 13:28:16 +00:00
|
|
|
kubectl certificate approve ${CSR_NAME} || exit 4
|
|
|
|
kubectl get csr ${CSR_NAME} -o jsonpath='{.status.certificate}' | base64 --decode > ${outCertFile} || exit 5
|
2019-02-12 14:12:03 +00:00
|
|
|
|
|
|
|
echo "Generated:"
|
|
|
|
echo ${outKeyFile}
|
|
|
|
echo ${outCertFile}
|