2015-12-11 10:32:13 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2016-02-11 22:08:16 +00:00
|
|
|
# Author: Smana smainklh@gmail.com
|
2015-12-11 10:32:13 +00:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
set -o errexit
|
|
|
|
set -o pipefail
|
|
|
|
|
|
|
|
usage()
|
|
|
|
{
|
|
|
|
cat << EOF
|
|
|
|
Create self signed certificates
|
|
|
|
|
2016-02-11 22:08:16 +00:00
|
|
|
Usage : $(basename $0) -f <config> [-d <ssldir>]
|
2015-12-11 10:32:13 +00:00
|
|
|
-h | --help : Show this message
|
|
|
|
-f | --config : Openssl configuration file
|
|
|
|
-d | --ssldir : Directory where the certificates will be installed
|
2016-09-28 11:05:08 +00:00
|
|
|
|
2016-12-27 11:02:45 +00:00
|
|
|
Environmental variables MASTERS and HOSTS should be set to generate keys
|
|
|
|
for each host.
|
|
|
|
|
|
|
|
ex :
|
|
|
|
MASTERS=node1 HOSTS="node1 node2" $(basename $0) -f openssl.conf -d /srv/ssl
|
2015-12-11 10:32:13 +00:00
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
# Options parsing
|
|
|
|
while (($#)); do
|
|
|
|
case "$1" in
|
|
|
|
-h | --help) usage; exit 0;;
|
|
|
|
-f | --config) CONFIG=${2}; shift 2;;
|
2016-09-28 11:05:08 +00:00
|
|
|
-d | --ssldir) SSLDIR="${2}"; shift 2;;
|
2015-12-11 10:32:13 +00:00
|
|
|
*)
|
|
|
|
usage
|
|
|
|
echo "ERROR : Unknown option"
|
|
|
|
exit 3
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ -z ${CONFIG} ]; then
|
|
|
|
echo "ERROR: the openssl configuration file is missing. option -f"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
if [ -z ${SSLDIR} ]; then
|
|
|
|
SSLDIR="/etc/kubernetes/certs"
|
|
|
|
fi
|
|
|
|
|
2016-02-18 20:56:10 +00:00
|
|
|
tmpdir=$(mktemp -d /tmp/kubernetes_cacert.XXXXXX)
|
2015-12-11 10:32:13 +00:00
|
|
|
trap 'rm -rf "${tmpdir}"' EXIT
|
|
|
|
cd "${tmpdir}"
|
|
|
|
|
|
|
|
mkdir -p "${SSLDIR}"
|
|
|
|
|
|
|
|
# Root CA
|
2016-12-27 11:02:45 +00:00
|
|
|
if [ -e "$SSLDIR/ca-key.pem" ]; then
|
|
|
|
# Reuse existing CA
|
|
|
|
cp $SSLDIR/{ca.pem,ca-key.pem} .
|
|
|
|
else
|
|
|
|
openssl genrsa -out ca-key.pem 2048 > /dev/null 2>&1
|
|
|
|
openssl req -x509 -new -nodes -key ca-key.pem -days 10000 -out ca.pem -subj "/CN=kube-ca" > /dev/null 2>&1
|
|
|
|
fi
|
|
|
|
|
2017-01-13 11:03:20 +00:00
|
|
|
if [ ! -e "$SSLDIR/ca-key.pem" ]; then
|
|
|
|
# kube-apiserver key
|
|
|
|
openssl genrsa -out apiserver-key.pem 2048 > /dev/null 2>&1
|
|
|
|
openssl req -new -key apiserver-key.pem -out apiserver.csr -subj "/CN=kube-apiserver" -config ${CONFIG} > /dev/null 2>&1
|
2017-02-24 14:38:13 +00:00
|
|
|
openssl x509 -req -in apiserver.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out apiserver.pem -days 3650 -extensions v3_req -extfile ${CONFIG} > /dev/null 2>&1
|
2017-01-13 11:03:20 +00:00
|
|
|
cat ca.pem >> apiserver.pem
|
|
|
|
fi
|
|
|
|
|
2017-06-15 10:15:52 +00:00
|
|
|
# Admins
|
2016-12-27 11:02:45 +00:00
|
|
|
if [ -n "$MASTERS" ]; then
|
|
|
|
for host in $MASTERS; do
|
2017-01-13 16:02:23 +00:00
|
|
|
cn="${host%%.*}"
|
2016-12-27 11:02:45 +00:00
|
|
|
# admin key
|
|
|
|
openssl genrsa -out admin-${host}-key.pem 2048 > /dev/null 2>&1
|
2017-03-08 14:42:25 +00:00
|
|
|
openssl req -new -key admin-${host}-key.pem -out admin-${host}.csr -subj "/CN=kube-admin-${cn}/O=system:masters" > /dev/null 2>&1
|
2017-02-24 14:38:13 +00:00
|
|
|
openssl x509 -req -in admin-${host}.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out admin-${host}.pem -days 3650 > /dev/null 2>&1
|
2016-12-27 11:02:45 +00:00
|
|
|
done
|
|
|
|
fi
|
2015-12-11 10:32:13 +00:00
|
|
|
|
2017-06-15 10:15:52 +00:00
|
|
|
# Nodes
|
2016-12-27 11:02:45 +00:00
|
|
|
if [ -n "$HOSTS" ]; then
|
|
|
|
for host in $HOSTS; do
|
2017-01-13 16:02:23 +00:00
|
|
|
cn="${host%%.*}"
|
2017-01-13 11:03:20 +00:00
|
|
|
# node key
|
2016-12-27 11:02:45 +00:00
|
|
|
openssl genrsa -out node-${host}-key.pem 2048 > /dev/null 2>&1
|
2017-06-15 10:15:52 +00:00
|
|
|
openssl req -new -key node-${host}-key.pem -out node-${host}.csr -subj "/CN=kube-node-${cn}/O=system:nodes" > /dev/null 2>&1
|
2017-02-24 14:38:13 +00:00
|
|
|
openssl x509 -req -in node-${host}.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out node-${host}.pem -days 3650 > /dev/null 2>&1
|
2016-12-27 11:02:45 +00:00
|
|
|
done
|
|
|
|
fi
|
2015-12-11 10:32:13 +00:00
|
|
|
|
2017-06-15 10:15:52 +00:00
|
|
|
# system:kube-proxy
|
2017-06-15 11:20:58 +00:00
|
|
|
if [ -n "$HOSTS" ]; then
|
|
|
|
for host in $HOSTS; do
|
|
|
|
cn="${host%%.*}"
|
|
|
|
# kube-proxy key
|
|
|
|
openssl genrsa -out kube-proxy-${host}-key.pem 2048 > /dev/null 2>&1
|
|
|
|
openssl req -new -key kube-proxy-${host}-key.pem -out kube-proxy-${host}.csr -subj "/CN=system:kube-proxy" > /dev/null 2>&1
|
|
|
|
openssl x509 -req -in kube-proxy-${host}.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out kube-proxy-${host}.pem -days 3650 > /dev/null 2>&1
|
|
|
|
done
|
|
|
|
fi
|
2017-06-15 10:15:52 +00:00
|
|
|
|
|
|
|
|
2015-12-11 10:32:13 +00:00
|
|
|
# Install certs
|
|
|
|
mv *.pem ${SSLDIR}/
|