import_jks_certs.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #!/bin/bash
  2. #
  3. # Copyright 2014-2015 Red Hat, Inc. and/or its affiliates
  4. # and other contributors as indicated by the @author tags.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. set -ex
  19. function import_certs() {
  20. dir=$CERT_DIR
  21. hawkular_metrics_keystore_password=$(echo $METRICS_KEYSTORE_PASSWD | base64 -d)
  22. hawkular_cassandra_keystore_password=$(echo $CASSANDRA_KEYSTORE_PASSWD | base64 -d)
  23. hawkular_metrics_truststore_password=$(echo $METRICS_TRUSTSTORE_PASSWD | base64 -d)
  24. hawkular_cassandra_truststore_password=$(echo $CASSANDRA_TRUSTSTORE_PASSWD | base64 -d)
  25. hawkular_jgroups_password=$(echo $JGROUPS_PASSWD | base64 -d)
  26. cassandra_alias=`keytool -noprompt -list -keystore $dir/hawkular-cassandra.truststore -storepass ${hawkular_cassandra_truststore_password} | sed -n '7~2s/,.*$//p'`
  27. hawkular_alias=`keytool -noprompt -list -keystore $dir/hawkular-metrics.truststore -storepass ${hawkular_metrics_truststore_password} | sed -n '7~2s/,.*$//p'`
  28. if [ ! -f $dir/hawkular-metrics.keystore ]; then
  29. echo "Creating the Hawkular Metrics keystore from the PEM file"
  30. keytool -importkeystore -v \
  31. -srckeystore $dir/hawkular-metrics.pkcs12 \
  32. -destkeystore $dir/hawkular-metrics.keystore \
  33. -srcstoretype PKCS12 \
  34. -deststoretype JKS \
  35. -srcstorepass $hawkular_metrics_keystore_password \
  36. -deststorepass $hawkular_metrics_keystore_password
  37. fi
  38. if [ ! -f $dir/hawkular-cassandra.keystore ]; then
  39. echo "Creating the Hawkular Cassandra keystore from the PEM file"
  40. keytool -importkeystore -v \
  41. -srckeystore $dir/hawkular-cassandra.pkcs12 \
  42. -destkeystore $dir/hawkular-cassandra.keystore \
  43. -srcstoretype PKCS12 \
  44. -deststoretype JKS \
  45. -srcstorepass $hawkular_cassandra_keystore_password \
  46. -deststorepass $hawkular_cassandra_keystore_password
  47. fi
  48. if [[ ! ${cassandra_alias[*]} =~ hawkular-metrics ]]; then
  49. echo "Importing the Hawkular Certificate into the Cassandra Truststore"
  50. keytool -noprompt -import -v -trustcacerts -alias hawkular-metrics \
  51. -file $dir/hawkular-metrics.crt \
  52. -keystore $dir/hawkular-cassandra.truststore \
  53. -trustcacerts \
  54. -storepass $hawkular_cassandra_truststore_password
  55. fi
  56. if [[ ! ${hawkular_alias[*]} =~ hawkular-cassandra ]]; then
  57. echo "Importing the Cassandra Certificate into the Hawkular Truststore"
  58. keytool -noprompt -import -v -trustcacerts -alias hawkular-cassandra \
  59. -file $dir/hawkular-cassandra.crt \
  60. -keystore $dir/hawkular-metrics.truststore \
  61. -trustcacerts \
  62. -storepass $hawkular_metrics_truststore_password
  63. fi
  64. if [[ ! ${cassandra_alias[*]} =~ hawkular-cassandra ]]; then
  65. echo "Importing the Hawkular Cassandra Certificate into the Cassandra Truststore"
  66. keytool -noprompt -import -v -trustcacerts -alias hawkular-cassandra \
  67. -file $dir/hawkular-cassandra.crt \
  68. -keystore $dir/hawkular-cassandra.truststore \
  69. -trustcacerts \
  70. -storepass $hawkular_cassandra_truststore_password
  71. fi
  72. cert_alias_names=(ca metricca cassandraca)
  73. for cert_alias in ${cert_alias_names[*]}; do
  74. if [[ ! ${cassandra_alias[*]} =~ "$cert_alias" ]]; then
  75. echo "Importing the CA Certificate with alias $cert_alias into the Cassandra Truststore"
  76. keytool -noprompt -import -v -trustcacerts -alias $cert_alias \
  77. -file ${dir}/ca.crt \
  78. -keystore $dir/hawkular-cassandra.truststore \
  79. -trustcacerts \
  80. -storepass $hawkular_cassandra_truststore_password
  81. fi
  82. done
  83. for cert_alias in ${cert_alias_names[*]}; do
  84. if [[ ! ${hawkular_alias[*]} =~ "$cert_alias" ]]; then
  85. echo "Importing the CA Certificate with alias $cert_alias into the Hawkular Metrics Truststore"
  86. keytool -noprompt -import -v -trustcacerts -alias $cert_alias \
  87. -file ${dir}/ca.crt \
  88. -keystore $dir/hawkular-metrics.truststore \
  89. -trustcacerts \
  90. -storepass $hawkular_metrics_truststore_password
  91. fi
  92. done
  93. if [ ! -f $dir/hawkular-jgroups.keystore ]; then
  94. echo "Generating the jgroups keystore"
  95. keytool -genseckey -alias hawkular -keypass ${hawkular_jgroups_password} \
  96. -storepass ${hawkular_jgroups_password} \
  97. -keyalg Blowfish \
  98. -keysize 56 \
  99. -keystore $dir/hawkular-jgroups.keystore \
  100. -storetype JCEKS
  101. fi
  102. }
  103. import_certs
  104. exit 0