import_jks_certs.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. cassandra_alias=`keytool -noprompt -list -keystore $dir/hawkular-cassandra.truststore -storepass ${hawkular_cassandra_truststore_password} | sed -n '7~2s/,.*$//p'`
  26. hawkular_alias=`keytool -noprompt -list -keystore $dir/hawkular-metrics.truststore -storepass ${hawkular_metrics_truststore_password} | sed -n '7~2s/,.*$//p'`
  27. if [ ! -f $dir/hawkular-metrics.keystore ]; then
  28. echo "Creating the Hawkular Metrics keystore from the PEM file"
  29. keytool -importkeystore -v \
  30. -srckeystore $dir/hawkular-metrics.pkcs12 \
  31. -destkeystore $dir/hawkular-metrics.keystore \
  32. -srcstoretype PKCS12 \
  33. -deststoretype JKS \
  34. -srcstorepass $hawkular_metrics_keystore_password \
  35. -deststorepass $hawkular_metrics_keystore_password
  36. fi
  37. if [ ! -f $dir/hawkular-cassandra.keystore ]; then
  38. echo "Creating the Hawkular Cassandra keystore from the PEM file"
  39. keytool -importkeystore -v \
  40. -srckeystore $dir/hawkular-cassandra.pkcs12 \
  41. -destkeystore $dir/hawkular-cassandra.keystore \
  42. -srcstoretype PKCS12 \
  43. -deststoretype JKS \
  44. -srcstorepass $hawkular_cassandra_keystore_password \
  45. -deststorepass $hawkular_cassandra_keystore_password
  46. fi
  47. if [[ ! ${cassandra_alias[*]} =~ hawkular-metrics ]]; then
  48. echo "Importing the Hawkular Certificate into the Cassandra Truststore"
  49. keytool -noprompt -import -v -trustcacerts -alias hawkular-metrics \
  50. -file $dir/hawkular-metrics.crt \
  51. -keystore $dir/hawkular-cassandra.truststore \
  52. -trustcacerts \
  53. -storepass $hawkular_cassandra_truststore_password
  54. fi
  55. if [[ ! ${hawkular_alias[*]} =~ hawkular-cassandra ]]; then
  56. echo "Importing the Cassandra Certificate into the Hawkular Truststore"
  57. keytool -noprompt -import -v -trustcacerts -alias hawkular-cassandra \
  58. -file $dir/hawkular-cassandra.crt \
  59. -keystore $dir/hawkular-metrics.truststore \
  60. -trustcacerts \
  61. -storepass $hawkular_metrics_truststore_password
  62. fi
  63. if [[ ! ${cassandra_alias[*]} =~ hawkular-cassandra ]]; then
  64. echo "Importing the Hawkular Cassandra Certificate into the Cassandra Truststore"
  65. keytool -noprompt -import -v -trustcacerts -alias hawkular-cassandra \
  66. -file $dir/hawkular-cassandra.crt \
  67. -keystore $dir/hawkular-cassandra.truststore \
  68. -trustcacerts \
  69. -storepass $hawkular_cassandra_truststore_password
  70. fi
  71. cert_alias_names=(ca metricca cassandraca)
  72. for cert_alias in ${cert_alias_names[*]}; do
  73. if [[ ! ${cassandra_alias[*]} =~ "$cert_alias" ]]; then
  74. echo "Importing the CA Certificate with alias $cert_alias into the Cassandra Truststore"
  75. keytool -noprompt -import -v -trustcacerts -alias $cert_alias \
  76. -file ${dir}/ca.crt \
  77. -keystore $dir/hawkular-cassandra.truststore \
  78. -trustcacerts \
  79. -storepass $hawkular_cassandra_truststore_password
  80. fi
  81. done
  82. for cert_alias in ${cert_alias_names[*]}; do
  83. if [[ ! ${hawkular_alias[*]} =~ "$cert_alias" ]]; then
  84. echo "Importing the CA Certificate with alias $cert_alias into the Hawkular Metrics Truststore"
  85. keytool -noprompt -import -v -trustcacerts -alias $cert_alias \
  86. -file ${dir}/ca.crt \
  87. -keystore $dir/hawkular-metrics.truststore \
  88. -trustcacerts \
  89. -storepass $hawkular_metrics_truststore_password
  90. fi
  91. done
  92. }
  93. import_certs