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.

98 lines
3.2 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/ssl/etcd"
  48. fi
  49. tmpdir=$(mktemp -d /tmp/etcd_cacert.XXXXXX)
  50. trap 'rm -rf "${tmpdir}"' EXIT
  51. cd "${tmpdir}"
  52. mkdir -p "${SSLDIR}"
  53. # Root CA
  54. if [ -e "$SSLDIR/ca-key.pem" ]; then
  55. # Reuse existing CA
  56. cp $SSLDIR/{ca.pem,ca-key.pem} .
  57. else
  58. openssl genrsa -out ca-key.pem 2048 > /dev/null 2>&1
  59. openssl req -x509 -new -nodes -key ca-key.pem -days 10000 -out ca.pem -subj "/CN=etcd-ca" > /dev/null 2>&1
  60. fi
  61. # ETCD member
  62. if [ -n "$MASTERS" ]; then
  63. for host in $MASTERS; do
  64. cn="${host%%.*}"
  65. # Member key
  66. openssl genrsa -out member-${host}-key.pem 2048 > /dev/null 2>&1
  67. openssl req -new -key member-${host}-key.pem -out member-${host}.csr -subj "/CN=etcd-member-${cn}" -config ${CONFIG} > /dev/null 2>&1
  68. openssl x509 -req -in member-${host}.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out member-${host}.pem -days 365 -extensions ssl_client -extfile ${CONFIG} > /dev/null 2>&1
  69. # Admin key
  70. openssl genrsa -out admin-${host}-key.pem 2048 > /dev/null 2>&1
  71. openssl req -new -key admin-${host}-key.pem -out admin-${host}.csr -subj "/CN=etcd-admin-${cn}" > /dev/null 2>&1
  72. openssl x509 -req -in admin-${host}.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out admin-${host}.pem -days 365 -extensions ssl_client -extfile ${CONFIG} > /dev/null 2>&1
  73. done
  74. fi
  75. # Node keys
  76. if [ -n "$HOSTS" ]; then
  77. for host in $HOSTS; do
  78. cn="${host%%.*}"
  79. openssl genrsa -out node-${host}-key.pem 2048 > /dev/null 2>&1
  80. openssl req -new -key node-${host}-key.pem -out node-${host}.csr -subj "/CN=etcd-node-${cn}" > /dev/null 2>&1
  81. openssl x509 -req -in node-${host}.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out node-${host}.pem -days 365 -extensions ssl_client -extfile ${CONFIG} > /dev/null 2>&1
  82. done
  83. fi
  84. # Install certs
  85. mv *.pem ${SSLDIR}/