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.

80 lines
2.4 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. ex :
  26. $(basename $0) -f openssl.conf -d /srv/ssl
  27. EOF
  28. }
  29. # Options parsing
  30. while (($#)); do
  31. case "$1" in
  32. -h | --help) usage; exit 0;;
  33. -f | --config) CONFIG=${2}; shift 2;;
  34. -d | --ssldir) SSLDIR="${2}"; shift 2;;
  35. *)
  36. usage
  37. echo "ERROR : Unknown option"
  38. exit 3
  39. ;;
  40. esac
  41. done
  42. if [ -z ${CONFIG} ]; then
  43. echo "ERROR: the openssl configuration file is missing. option -f"
  44. exit 1
  45. fi
  46. if [ -z ${SSLDIR} ]; then
  47. SSLDIR="/etc/kubernetes/certs"
  48. fi
  49. tmpdir=$(mktemp -d /tmp/kubernetes_cacert.XXXXXX)
  50. trap 'rm -rf "${tmpdir}"' EXIT
  51. cd "${tmpdir}"
  52. mkdir -p "${SSLDIR}"
  53. # Root CA
  54. openssl genrsa -out ca-key.pem 2048 > /dev/null 2>&1
  55. openssl req -x509 -new -nodes -key ca-key.pem -days 10000 -out ca.pem -subj "/CN=kube-ca" > /dev/null 2>&1
  56. # Apiserver
  57. openssl genrsa -out apiserver-key.pem 2048 > /dev/null 2>&1
  58. openssl req -new -key apiserver-key.pem -out apiserver.csr -subj "/CN=kube-apiserver" -config ${CONFIG} > /dev/null 2>&1
  59. 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
  60. # Nodes and Admin
  61. for i in node admin; do
  62. openssl genrsa -out ${i}-key.pem 2048 > /dev/null 2>&1
  63. openssl req -new -key ${i}-key.pem -out ${i}.csr -subj "/CN=kube-${i}" > /dev/null 2>&1
  64. openssl x509 -req -in ${i}.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out ${i}.pem -days 365 > /dev/null 2>&1
  65. done
  66. # Install certs
  67. mv *.pem ${SSLDIR}/