2019-05-29 21:12:09 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2020-07-05 02:35:31 +00:00
|
|
|
|
|
|
|
for i in "$@"
|
|
|
|
do
|
|
|
|
case $i in
|
|
|
|
--service=*)
|
|
|
|
service="${i#*=}"
|
|
|
|
shift
|
|
|
|
;;
|
2022-10-03 12:51:46 +00:00
|
|
|
--namespace=*)
|
|
|
|
namespace="${i#*=}"
|
|
|
|
shift
|
|
|
|
;;
|
2020-07-05 02:35:31 +00:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ "$service" == "" ]; then
|
|
|
|
service="kyverno-svc"
|
|
|
|
fi
|
|
|
|
|
2022-10-03 12:51:46 +00:00
|
|
|
|
|
|
|
if [ "$namespace" == "" ]; then
|
|
|
|
namespace="kyverno"
|
|
|
|
fi
|
|
|
|
|
2020-07-05 02:35:31 +00:00
|
|
|
echo "service is $service"
|
2022-10-03 12:51:46 +00:00
|
|
|
echo "namespace is $namespace"
|
2020-07-05 02:35:31 +00:00
|
|
|
|
2019-05-29 21:12:09 +00:00
|
|
|
echo "Generating self-signed certificate"
|
|
|
|
# generate priv key for root CA
|
2022-10-03 12:51:46 +00:00
|
|
|
openssl genrsa -out rootCA.key 4096
|
2019-05-29 21:12:09 +00:00
|
|
|
# generate root CA
|
2022-10-03 12:51:46 +00:00
|
|
|
openssl req -x509 -new -nodes -key rootCA.key -sha256 -days 1024 -out rootCA.crt -subj "/C=US/ST=test/L=test /O=test /OU=PIB/CN=*.${namespace}.svc/emailAddress=test@test.com"
|
2019-05-29 21:12:09 +00:00
|
|
|
# generate priv key
|
|
|
|
openssl genrsa -out webhook.key 4096
|
|
|
|
# generate certificate
|
2022-10-03 12:51:46 +00:00
|
|
|
openssl req -new -key webhook.key -out webhook.csr -subj "/C=US/ST=test /L=test /O=test /OU=PIB/CN=${service}.${namespace}.svc/emailAddress=test@test.com"
|
2021-01-20 23:25:27 +00:00
|
|
|
|
|
|
|
# generate SANs
|
2022-10-03 12:51:46 +00:00
|
|
|
echo "subjectAltName = DNS:kyverno-svc,DNS:${service}.${namespace},DNS:${service}.${namespace}.svc" >> webhook.ext
|
2021-01-20 23:25:27 +00:00
|
|
|
|
2019-05-29 21:12:09 +00:00
|
|
|
# sign the certificate using the root CA
|
|
|
|
openssl x509 -req -in webhook.csr -CA rootCA.crt -CAkey rootCA.key -CAcreateserial -out webhook.crt -days 1024 -sha256
|
|
|
|
|
|
|
|
echo "Generating corresponding kubernetes secrets for TLS pair and root CA"
|
|
|
|
# create project namespace
|
2022-10-03 12:51:46 +00:00
|
|
|
kubectl create ns ${namespace}
|
2019-05-29 21:12:09 +00:00
|
|
|
# create tls pair secret
|
2022-10-03 12:51:46 +00:00
|
|
|
kubectl -n ${namespace} create secret tls ${service}.${namespace}.svc.kyverno-tls-pair --cert=webhook.crt --key=webhook.key
|
2019-05-29 21:12:09 +00:00
|
|
|
# annotate tls pair secret to specify use of self-signed certificates and check if root CA is created as secret
|
2022-10-03 12:51:46 +00:00
|
|
|
kubectl annotate secret ${service}.${namespace}.svc.kyverno-tls-pair -n ${namespace} self-signed-cert=true
|
2019-05-29 21:12:09 +00:00
|
|
|
# create root CA secret
|
2022-10-03 12:51:46 +00:00
|
|
|
kubectl -n ${namespace} create secret generic ${service}.${namespace}.svc.kyverno-tls-ca --from-file=rootCA.crt
|