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.

105 lines
3.3 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. if [ ! -e "$SSLDIR/ca-key.pem" ]; then
  64. # kube-apiserver key
  65. openssl genrsa -out apiserver-key.pem 2048 > /dev/null 2>&1
  66. openssl req -new -key apiserver-key.pem -out apiserver.csr -subj "/CN=kube-apiserver" -config ${CONFIG} > /dev/null 2>&1
  67. 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
  68. cat ca.pem >> apiserver.pem
  69. fi
  70. if [ -n "$MASTERS" ]; then
  71. for host in $MASTERS; do
  72. cn="${host%%.*}"
  73. # admin key
  74. openssl genrsa -out admin-${host}-key.pem 2048 > /dev/null 2>&1
  75. openssl req -new -key admin-${host}-key.pem -out admin-${host}.csr -subj "/CN=kube-admin-${cn}" > /dev/null 2>&1
  76. openssl x509 -req -in admin-${host}.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out admin-${host}.pem -days 365 > /dev/null 2>&1
  77. done
  78. fi
  79. # Nodes and Admin
  80. if [ -n "$HOSTS" ]; then
  81. for host in $HOSTS; do
  82. cn="${host%%.*}"
  83. # node key
  84. openssl genrsa -out node-${host}-key.pem 2048 > /dev/null 2>&1
  85. openssl req -new -key node-${host}-key.pem -out node-${host}.csr -subj "/CN=kube-node-${cn}" > /dev/null 2>&1
  86. openssl x509 -req -in node-${host}.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out node-${host}.pem -days 365 > /dev/null 2>&1
  87. done
  88. fi
  89. # Install certs
  90. mv *.pem ${SSLDIR}/