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.

121 lines
3.5 KiB

  1. #!/bin/bash
  2. # Author: Smana smainklh@gmail.com
  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> [-d <ssldir>]
  22. -h | --help : Show this message
  23. -f | --config : Openssl configuration file
  24. -d | --ssldir : Directory where the certificates will be installed
  25. Environmental variables MASTERS and HOSTS should be set to generate keys
  26. for each host.
  27. ex :
  28. MASTERS=node1 HOSTS="node1 node2" $(basename $0) -f openssl.conf -d /srv/ssl
  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. -d | --ssldir) SSLDIR="${2}"; shift 2;;
  37. *)
  38. usage
  39. echo "ERROR : Unknown option"
  40. exit 3
  41. ;;
  42. esac
  43. done
  44. if [ -z ${CONFIG} ]; then
  45. echo "ERROR: the openssl configuration file is missing. option -f"
  46. exit 1
  47. fi
  48. if [ -z ${SSLDIR} ]; then
  49. SSLDIR="/etc/kubernetes/certs"
  50. fi
  51. tmpdir=$(mktemp -d /tmp/kubernetes_cacert.XXXXXX)
  52. trap 'rm -rf "${tmpdir}"' EXIT
  53. cd "${tmpdir}"
  54. mkdir -p "${SSLDIR}"
  55. # Root CA
  56. if [ -e "$SSLDIR/ca-key.pem" ]; then
  57. # Reuse existing CA
  58. cp $SSLDIR/{ca.pem,ca-key.pem} .
  59. else
  60. openssl genrsa -out ca-key.pem 2048 > /dev/null 2>&1
  61. openssl req -x509 -new -nodes -key ca-key.pem -days 10000 -out ca.pem -subj "/CN=kube-ca" > /dev/null 2>&1
  62. fi
  63. gen_key_and_cert() {
  64. local name=$1
  65. local subject=$2
  66. openssl genrsa -out ${name}-key.pem 2048 > /dev/null 2>&1
  67. openssl req -new -key ${name}-key.pem -out ${name}.csr -subj "${subject}" -config ${CONFIG} > /dev/null 2>&1
  68. openssl x509 -req -in ${name}.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out ${name}.pem -days 3650 -extensions v3_req -extfile ${CONFIG} > /dev/null 2>&1
  69. }
  70. # Admins
  71. if [ -n "$MASTERS" ]; then
  72. # kube-apiserver
  73. # Generate only if we don't have existing ca and apiserver certs
  74. if ! [ -e "$SSLDIR/ca-key.pem" ] || ! [ -e "$SSLDIR/apiserver-key.pem" ]; then
  75. gen_key_and_cert "apiserver" "/CN=kube-apiserver"
  76. cat ca.pem >> apiserver.pem
  77. fi
  78. # If any host requires new certs, just regenerate scheduler and controller-manager master certs
  79. # kube-scheduler
  80. gen_key_and_cert "kube-scheduler" "/CN=system:kube-scheduler"
  81. # kube-controller-manager
  82. gen_key_and_cert "kube-controller-manager" "/CN=system:kube-controller-manager"
  83. for host in $MASTERS; do
  84. cn="${host%%.*}"
  85. # admin
  86. gen_key_and_cert "admin-${host}" "/CN=kube-admin-${cn}/O=system:masters"
  87. done
  88. fi
  89. # Nodes
  90. if [ -n "$HOSTS" ]; then
  91. for host in $HOSTS; do
  92. cn="${host%%.*}"
  93. gen_key_and_cert "node-${host}" "/CN=system:node:${cn,,}/O=system:nodes"
  94. done
  95. fi
  96. # system:node-proxier
  97. if [ -n "$HOSTS" ]; then
  98. for host in $HOSTS; do
  99. # kube-proxy
  100. gen_key_and_cert "kube-proxy-${host}" "/CN=system:kube-proxy/O=system:node-proxier"
  101. done
  102. fi
  103. # Install certs
  104. mv *.pem ${SSLDIR}/