gencerts.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #! /usr/bin/env bash
  2. #
  3. # Licensed to the Apache Software Foundation (ASF) under one
  4. # or more contributor license agreements. See the NOTICE file
  5. # distributed with this work for additional information
  6. # regarding copyright ownership. The ASF licenses this file
  7. # to you under the Apache License, Version 2.0 (the
  8. # "License"); you may not use this file except in compliance
  9. # with the License. You may obtain a copy of the License at
  10. #
  11. # https://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing,
  14. # software distributed under the License is distributed on an
  15. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. # KIND, either express or implied. See the License for the
  17. # specific language governing permissions and limitations
  18. # under the License.
  19. #
  20. #
  21. # This script cleans up old transaction logs and snapshots
  22. #
  23. #
  24. # If this scripted is run out of /usr/bin or some other system bin directory
  25. # it should be linked to and not copied. Things like java jar files are found
  26. # relative to the canonical path of this script.
  27. #
  28. # determining the domain name in the certificates:
  29. # - use the first commandline argument, if present
  30. # - if not, then use the fully qualified domain name
  31. # - if `hostname` command fails, fall back to zookeeper.apache.org
  32. FQDN=$(hostname -f)
  33. FQDN=${1:-$FQDN}
  34. FQDN=${FQDN:-"zookeeper.apache.org"}
  35. # Generate the root key
  36. openssl genrsa -out rootkey.pem 2048
  37. #Generate the root Cert
  38. openssl req -x509 -new -key rootkey.pem -out root.crt -config <(
  39. cat <<-EOF
  40. [ req ]
  41. default_bits = 2048
  42. prompt = no
  43. default_md = sha256
  44. distinguished_name = dn
  45. [ dn ]
  46. C = US
  47. ST = California
  48. L = San Francisco
  49. O = ZooKeeper
  50. emailAddress = dev@$FQDN
  51. CN = $FQDN
  52. EOF
  53. )
  54. #Generate Client Key
  55. openssl genrsa -out clientkey.pem 2048
  56. #Generate Client Cert
  57. openssl req -new -key clientkey.pem -out client.csr -config <(
  58. cat <<-EOF
  59. [ req ]
  60. default_bits = 2048
  61. prompt = no
  62. default_md = sha256
  63. distinguished_name = dn
  64. [ dn ]
  65. C = US
  66. ST = California
  67. L = San Francisco
  68. O = ZooKeeper
  69. emailAddress = dev@$FQDN
  70. CN = $FQDN
  71. EOF
  72. )
  73. openssl x509 -req -in client.csr -CA root.crt -CAkey rootkey.pem -CAcreateserial -days 3650 -out client.crt
  74. #Export in pkcs12 format
  75. openssl pkcs12 -export -in client.crt -inkey clientkey.pem -out client.pkcs12 -password pass:password
  76. # Import Keystore in JKS
  77. keytool -importkeystore -srckeystore client.pkcs12 -destkeystore client.jks -srcstoretype pkcs12 -srcstorepass password -deststorepass password
  78. ############################################################
  79. #Generate Server key
  80. openssl genrsa -out serverkey.pem 2048
  81. #Generate Server Cert
  82. openssl req -new -key serverkey.pem -out server.csr -config <(
  83. cat <<-EOF
  84. [ req ]
  85. default_bits = 2048
  86. prompt = no
  87. default_md = sha256
  88. distinguished_name = dn
  89. [ dn ]
  90. C = US
  91. ST = California
  92. L = San Francisco
  93. O = ZooKeeper
  94. emailAddress = dev@$FQDN
  95. CN = $FQDN
  96. EOF
  97. )
  98. openssl x509 -req -in server.csr -CA root.crt -CAkey rootkey.pem -CAcreateserial -days 3650 -out server.crt
  99. #Export in pkcs12 format
  100. openssl pkcs12 -export -in server.crt -inkey serverkey.pem -out server.pkcs12 -password pass:password
  101. # Import Keystore in JKS
  102. keytool -importkeystore -srckeystore server.pkcs12 -destkeystore server.jks -srcstoretype pkcs12 -srcstorepass password -deststorepass password
  103. keytool -importcert -keystore server.jks -file root.crt -storepass password -noprompt
  104. keytool -importcert -alias ca -file root.crt -keystore clienttrust.jks -storepass password -noprompt
  105. keytool -importcert -alias clientcert -file client.crt -keystore clienttrust.jks -storepass password -noprompt
  106. keytool -importcert -alias ca -file root.crt -keystore servertrust.jks -storepass password -noprompt
  107. keytool -importcert -alias servercert -file server.crt -keystore servertrust.jks -storepass password -noprompt