#!/bin/bash # Copyright 2020 The Kubernetes Authors. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # http://www.apache.org/licenses/LICENSE-2.0 # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # File originally from https://github.com/istio/istio/blob/release-0.7/install/kubernetes/webhook-create-signed-cert.sh set -e if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then cat <> "${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 EOF openssl genrsa -out "${tmpdir}"/server-key.pem 2048 openssl req -new -key "${tmpdir}"/server-key.pem -subj "/O=C=US/ST=CA/L=Palo Alto/O=VMware/OU=CNS" -out "${tmpdir}"/server.csr -config "${tmpdir}"/csr.conf # clean-up any previously created CSR for our service. Ignore errors if not present. kubectl delete csr ${csrName} 2>/dev/null || true # create server cert/key CSR and send to k8s API cat <&2 exit 1 fi echo "${serverCert}" | openssl base64 -d -A -out "${tmpdir}"/server-cert.pem cat <"${tmpdir}"/webhook.config [WebHookConfig] port = "8443" cert-file = "/etc/webhook/cert.pem" key-file = "/etc/webhook/key.pem" eof # create the secret with CA cert and server cert/key kubectl create secret generic "${secret}" \ --from-file=key.pem="${tmpdir}"/server-key.pem \ --from-file=cert.pem="${tmpdir}"/server-cert.pem \ --from-file=webhook.config="${tmpdir}"/webhook.config \ --dry-run=client -o yaml | kubectl -n "${namespace}" apply -f -