gencerts.sh 3.7 KB

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