You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.4 KiB

  1. #!/bin/bash
  2. # Author: skahlouc@skahlouc-laptop
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. set -o errexit
  16. set -o pipefail
  17. usage()
  18. {
  19. cat << EOF
  20. Create self signed certificates
  21. Usage : $(basename $0) -f <config> [-c <cloud_provider>] [-d <ssldir>] [-g <ssl_group>]
  22. -h | --help : Show this message
  23. -f | --config : Openssl configuration file
  24. -c | --cloud : Cloud provider (GCE, AWS or AZURE)
  25. -d | --ssldir : Directory where the certificates will be installed
  26. -g | --sslgrp : Group of the certificates
  27. ex :
  28. $(basename $0) -f openssl.conf -c GCE -d /srv/ssl -g kube
  29. EOF
  30. }
  31. # Options parsing
  32. while (($#)); do
  33. case "$1" in
  34. -h | --help) usage; exit 0;;
  35. -f | --config) CONFIG=${2}; shift 2;;
  36. -c | --cloud) CLOUD=${2}; shift 2;;
  37. -d | --ssldir) SSLDIR="${2}"; shift 2;;
  38. -g | --group) SSLGRP="${2}"; shift 2;;
  39. *)
  40. usage
  41. echo "ERROR : Unknown option"
  42. exit 3
  43. ;;
  44. esac
  45. done
  46. if [ -z ${CONFIG} ]; then
  47. echo "ERROR: the openssl configuration file is missing. option -f"
  48. exit 1
  49. fi
  50. if [ -z ${SSLDIR} ]; then
  51. SSLDIR="/etc/kubernetes/certs"
  52. fi
  53. if [ -z ${SSLGRP} ]; then
  54. SSLGRP="kube-cert"
  55. fi
  56. #echo "config=$CONFIG, cloud=$CLOUD, certdir=$SSLDIR, certgroup=$SSLGRP"
  57. SUPPORTED_CLOUDS="GCE AWS AZURE"
  58. # TODO: Add support for discovery on other providers?
  59. if [ "${CLOUD}" == "GCE" ]; then
  60. CLOUD_IP=$(curl -s -H Metadata-Flavor:Google http://metadata.google.internal./computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ip)
  61. fi
  62. if [ "${CLOUD}" == "AWS" ]; then
  63. CLOUD_IP=$(curl -s http://169.254.169.254/latest/meta-data/public-ipv4)
  64. fi
  65. if [ "${CLOUD}" == "AZURE" ]; then
  66. CLOUD_IP=$(uname -n | awk -F. '{ print $2 }').cloudapp.net
  67. fi
  68. tmpdir=$(mktemp -d --tmpdir kubernetes_cacert.XXXXXX)
  69. trap 'rm -rf "${tmpdir}"' EXIT
  70. cd "${tmpdir}"
  71. mkdir -p "${SSLDIR}"
  72. # Root CA
  73. openssl genrsa -out ca-key.pem 2048 > /dev/null 2>&1
  74. openssl req -x509 -new -nodes -key ca-key.pem -days 10000 -out ca.pem -subj "/CN=kube-ca" > /dev/null 2>&1
  75. # Apiserver
  76. openssl genrsa -out apiserver-key.pem 2048 > /dev/null 2>&1
  77. openssl req -new -key apiserver-key.pem -out apiserver.csr -subj "/CN=kube-apiserver" -config ${CONFIG} > /dev/null 2>&1
  78. openssl x509 -req -in apiserver.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out apiserver.pem -days 365 -extensions v3_req -extfile ${CONFIG} > /dev/null 2>&1
  79. # Nodes and Admin
  80. for i in node admin; do
  81. openssl genrsa -out ${i}-key.pem 2048 > /dev/null 2>&1
  82. openssl req -new -key ${i}-key.pem -out ${i}.csr -subj "/CN=kube-${i}" > /dev/null 2>&1
  83. openssl x509 -req -in ${i}.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out ${i}.pem -days 365 > /dev/null 2>&1
  84. done
  85. # Install certs
  86. mv *.pem ${SSLDIR}/
  87. chgrp ${SSLGRP} ${SSLDIR}/*
  88. chmod 600 ${SSLDIR}/*-key.pem
  89. chown root:root ${SSLDIR}/*-key.pem