gencerts.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. # use local fully qualified domain name in the certificates, or fall back
  25. # to zookeeper.apache.org if no domain name is set or the `hostname` command fails
  26. FQDN=`hostname -f`
  27. FQDN=${FQDN:-"zookeeper.apache.org"}
  28. # Generate the root key
  29. openssl genrsa -out rootkey.pem 2048
  30. #Generate the root Cert
  31. openssl req -x509 -new -key rootkey.pem -out root.crt -config <(
  32. cat <<-EOF
  33. [ req ]
  34. default_bits = 2048
  35. prompt = no
  36. default_md = sha256
  37. distinguished_name = dn
  38. [ dn ]
  39. C = US
  40. ST = California
  41. L = San Francisco
  42. O = ZooKeeper
  43. emailAddress = dev@$FQDN
  44. CN = $FQDN
  45. EOF
  46. )
  47. #Generate Client Key
  48. openssl genrsa -out clientkey.pem 2048
  49. #Generate Client Cert
  50. openssl req -new -key clientkey.pem -out client.csr -config <(
  51. cat <<-EOF
  52. [ req ]
  53. default_bits = 2048
  54. prompt = no
  55. default_md = sha256
  56. distinguished_name = dn
  57. [ dn ]
  58. C = US
  59. ST = California
  60. L = San Francisco
  61. O = ZooKeeper
  62. emailAddress = dev@$FQDN
  63. CN = $FQDN
  64. EOF
  65. )
  66. openssl x509 -req -in client.csr -CA root.crt -CAkey rootkey.pem -CAcreateserial -days 3650 -out client.crt
  67. #Export in pkcs12 format
  68. openssl pkcs12 -export -in client.crt -inkey clientkey.pem -out client.pkcs12 -password pass:password
  69. # Import Keystore in JKS
  70. keytool -importkeystore -srckeystore client.pkcs12 -destkeystore client.jks -srcstoretype pkcs12 -srcstorepass password -deststorepass password
  71. ############################################################
  72. #Generate Server key
  73. openssl genrsa -out serverkey.pem 2048
  74. #Generate Server Cert
  75. openssl req -new -key serverkey.pem -out server.csr -config <(
  76. cat <<-EOF
  77. [ req ]
  78. default_bits = 2048
  79. prompt = no
  80. default_md = sha256
  81. distinguished_name = dn
  82. [ dn ]
  83. C = US
  84. ST = California
  85. L = San Francisco
  86. O = ZooKeeper
  87. emailAddress = dev@$FQDN
  88. CN = $FQDN
  89. EOF
  90. )
  91. openssl x509 -req -in server.csr -CA root.crt -CAkey rootkey.pem -CAcreateserial -days 3650 -out server.crt
  92. #Export in pkcs12 format
  93. openssl pkcs12 -export -in server.crt -inkey serverkey.pem -out server.pkcs12 -password pass:password
  94. # Import Keystore in JKS
  95. keytool -importkeystore -srckeystore server.pkcs12 -destkeystore server.jks -srcstoretype pkcs12 -srcstorepass password -deststorepass password
  96. keytool -importcert -keystore server.jks -file root.crt -storepass password -noprompt
  97. keytool -importcert -alias ca -file root.crt -keystore clienttrust.jks -storepass password -noprompt
  98. keytool -importcert -alias clientcert -file client.crt -keystore clienttrust.jks -storepass password -noprompt
  99. keytool -importcert -alias ca -file root.crt -keystore servertrust.jks -storepass password -noprompt
  100. keytool -importcert -alias servercert -file server.crt -keystore servertrust.jks -storepass password -noprompt