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.

102 lines
3.6 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. -c | --cadir : Directory where the existing CA is located
  26. -s | --service : Service for the ca
  27. ex :
  28. $(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. -c | --cadir) CADIR="${2}"; shift 2;;
  38. -s | --service) SERVICE="${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/calico/certs"
  52. fi
  53. tmpdir=$(mktemp -d /tmp/calico_${SERVICE}_certs.XXXXXX)
  54. trap 'rm -rf "${tmpdir}"' EXIT
  55. cd "${tmpdir}"
  56. mkdir -p ${SSLDIR} ${CADIR}
  57. # Root CA
  58. if [ -e "$CADIR/ca.key" ]; then
  59. # Reuse existing CA
  60. cp $CADIR/{ca.crt,ca.key} .
  61. else
  62. openssl genrsa -out ca.key {{certificates_key_size}} > /dev/null 2>&1
  63. openssl req -x509 -new -nodes -key ca.key -days {{certificates_duration}} -out ca.crt -subj "/CN=calico-${SERVICE}-ca" > /dev/null 2>&1
  64. fi
  65. if [ $SERVICE == "typha" ]; then
  66. # Typha server
  67. openssl genrsa -out typha-server.key {{certificates_key_size}} > /dev/null 2>&1
  68. openssl req -new -key typha-server.key -out typha-server.csr -subj "/CN=typha-server" -config ${CONFIG} > /dev/null 2>&1
  69. openssl x509 -req -in typha-server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out typha-server.crt -days {{certificates_duration}} -extensions ssl_client -extfile ${CONFIG} > /dev/null 2>&1
  70. # Typha client
  71. openssl genrsa -out typha-client.key {{certificates_key_size}} > /dev/null 2>&1
  72. openssl req -new -key typha-client.key -out typha-client.csr -subj "/CN=typha-client" -config ${CONFIG} > /dev/null 2>&1
  73. openssl x509 -req -in typha-client.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out typha-client.crt -days {{certificates_duration}} -extensions ssl_client -extfile ${CONFIG} > /dev/null 2>&1
  74. elif [ $SERVICE == "apiserver" ]; then
  75. # calico-apiserver
  76. openssl genrsa -out apiserver.key {{certificates_key_size}} > /dev/null 2>&1
  77. openssl req -new -key apiserver.key -out apiserver.csr -subj "/CN=calico-apiserver" -config ${CONFIG} > /dev/null 2>&1
  78. openssl x509 -req -in apiserver.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out apiserver.crt -days {{certificates_duration}} -extensions ssl_client -extfile ${CONFIG} > /dev/null 2>&1
  79. else
  80. echo "ERROR: the openssl configuration file is missing. option -s"
  81. exit 1
  82. fi
  83. # Install certs
  84. if [ -e "$CADIR/ca.key" ]; then
  85. # No pass existing CA
  86. rm -f ca.crt ca.key
  87. fi
  88. mv {*.crt,*.key} ${SSLDIR}/