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.

89 lines
2.8 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. ex :
  27. $(basename $0) -f openssl.conf -d /srv/ssl
  28. EOF
  29. }
  30. # Options parsing
  31. while (($#)); do
  32. case "$1" in
  33. -h | --help) usage; exit 0;;
  34. -f | --config) CONFIG=${2}; shift 2;;
  35. -d | --ssldir) SSLDIR="${2}"; shift 2;;
  36. -c | --cadir) CADIR="${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/calico/certs"
  50. fi
  51. tmpdir=$(mktemp -d /tmp/calico_typha_certs.XXXXXX)
  52. trap 'rm -rf "${tmpdir}"' EXIT
  53. cd "${tmpdir}"
  54. mkdir -p ${SSLDIR} ${CADIR}
  55. # Root CA
  56. if [ -e "$CADIR/ca.key" ]; then
  57. # Reuse existing CA
  58. cp $CADIR/{ca.crt,ca.key} .
  59. else
  60. openssl genrsa -out ca.key 2048 > /dev/null 2>&1
  61. openssl req -x509 -new -nodes -key ca.key -days 3650 -out ca.crt -subj "/CN=calico-typha-ca" > /dev/null 2>&1
  62. fi
  63. # Typha server
  64. openssl genrsa -out typha-server.key 2048 > /dev/null 2>&1
  65. openssl req -new -key typha-server.key -out typha-server.csr -subj "/CN=typha-server" -config ${CONFIG} > /dev/null 2>&1
  66. openssl x509 -req -in typha-server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out typha-server.crt -days 3650 -extensions ssl_client -extfile ${CONFIG} > /dev/null 2>&1
  67. # Typha client
  68. openssl genrsa -out typha-client.key 2048 > /dev/null 2>&1
  69. openssl req -new -key typha-client.key -out typha-client.csr -subj "/CN=typha-client" -config ${CONFIG} > /dev/null 2>&1
  70. openssl x509 -req -in typha-client.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out typha-client.crt -days 3650 -extensions ssl_client -extfile ${CONFIG} > /dev/null 2>&1
  71. # Install certs
  72. if [ -e "$CADIR/ca.key" ]; then
  73. # No pass existing CA
  74. rm -f ca.crt ca.key
  75. fi
  76. mv {*.crt,*.key} ${SSLDIR}/