generate-jks.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #! /bin/bash
  2. set -ex
  3. function usage() {
  4. echo Usage: `basename $0` cert_directory [logging_namespace] 1>&2
  5. }
  6. function generate_JKS_chain() {
  7. dir=${SCRATCH_DIR:-_output}
  8. ADD_OID=$1
  9. NODE_NAME=$2
  10. CERT_NAMES=${3:-$NODE_NAME}
  11. ks_pass=${KS_PASS:-kspass}
  12. ts_pass=${TS_PASS:-tspass}
  13. rm -rf $NODE_NAME
  14. extension_names=""
  15. for name in ${CERT_NAMES//,/ }; do
  16. extension_names="${extension_names},dns:${name}"
  17. done
  18. if [ "$ADD_OID" = true ]; then
  19. extension_names="${extension_names},oid:1.2.3.4.5.5"
  20. fi
  21. echo Generating keystore and certificate for node $NODE_NAME
  22. keytool -genkey \
  23. -alias $NODE_NAME \
  24. -keystore $dir/$NODE_NAME.jks \
  25. -keypass $ks_pass \
  26. -storepass $ks_pass \
  27. -keyalg RSA \
  28. -keysize 2048 \
  29. -validity 712 \
  30. -dname "CN=$NODE_NAME, OU=OpenShift, O=Logging" \
  31. -ext san=dns:localhost,ip:127.0.0.1"${extension_names}"
  32. echo Generating certificate signing request for node $NODE_NAME
  33. keytool -certreq \
  34. -alias $NODE_NAME \
  35. -keystore $dir/$NODE_NAME.jks \
  36. -storepass $ks_pass \
  37. -file $dir/$NODE_NAME.csr \
  38. -keyalg rsa \
  39. -dname "CN=$NODE_NAME, OU=OpenShift, O=Logging" \
  40. -ext san=dns:localhost,ip:127.0.0.1"${extension_names}"
  41. echo Sign certificate request with CA
  42. openssl ca \
  43. -in $dir/$NODE_NAME.csr \
  44. -notext \
  45. -out $dir/$NODE_NAME.crt \
  46. -config $dir/signing.conf \
  47. -extensions v3_req \
  48. -batch \
  49. -extensions server_ext
  50. echo "Import back to keystore (including CA chain)"
  51. keytool \
  52. -import \
  53. -file $dir/ca.crt \
  54. -keystore $dir/$NODE_NAME.jks \
  55. -storepass $ks_pass \
  56. -noprompt -alias sig-ca
  57. keytool \
  58. -import \
  59. -file $dir/$NODE_NAME.crt \
  60. -keystore $dir/$NODE_NAME.jks \
  61. -storepass $ks_pass \
  62. -noprompt \
  63. -alias $NODE_NAME
  64. echo All done for $NODE_NAME
  65. }
  66. function generate_JKS_client_cert() {
  67. NODE_NAME="$1"
  68. ks_pass=${KS_PASS:-kspass}
  69. ts_pass=${TS_PASS:-tspass}
  70. dir=${SCRATCH_DIR:-_output} # for writing files to bundle into secrets
  71. echo Generating keystore and certificate for node ${NODE_NAME}
  72. keytool -genkey \
  73. -alias $NODE_NAME \
  74. -keystore $dir/$NODE_NAME.jks \
  75. -keyalg RSA \
  76. -keysize 2048 \
  77. -validity 712 \
  78. -keypass $ks_pass \
  79. -storepass $ks_pass \
  80. -dname "CN=$NODE_NAME, OU=OpenShift, O=Logging"
  81. echo Generating certificate signing request for node $NODE_NAME
  82. keytool -certreq \
  83. -alias $NODE_NAME \
  84. -keystore $dir/$NODE_NAME.jks \
  85. -file $dir/$NODE_NAME.jks.csr \
  86. -keyalg rsa \
  87. -keypass $ks_pass \
  88. -storepass $ks_pass \
  89. -dname "CN=$NODE_NAME, OU=OpenShift, O=Logging"
  90. echo Sign certificate request with CA
  91. openssl ca \
  92. -in "$dir/$NODE_NAME.jks.csr" \
  93. -notext \
  94. -out "$dir/$NODE_NAME.jks.crt" \
  95. -config $dir/signing.conf \
  96. -extensions v3_req \
  97. -batch \
  98. -extensions server_ext
  99. echo "Import back to keystore (including CA chain)"
  100. keytool \
  101. -import \
  102. -file $dir/ca.crt \
  103. -keystore $dir/$NODE_NAME.jks \
  104. -storepass $ks_pass \
  105. -noprompt -alias sig-ca
  106. keytool \
  107. -import \
  108. -file $dir/$NODE_NAME.jks.crt \
  109. -keystore $dir/$NODE_NAME.jks \
  110. -storepass $ks_pass \
  111. -noprompt \
  112. -alias $NODE_NAME
  113. echo All done for $NODE_NAME
  114. }
  115. function join { local IFS="$1"; shift; echo "$*"; }
  116. function createTruststore() {
  117. echo "Import CA to truststore for validating client certs"
  118. keytool \
  119. -import \
  120. -file $dir/ca.crt \
  121. -keystore $dir/truststore.jks \
  122. -storepass $ts_pass \
  123. -noprompt -alias sig-ca
  124. }
  125. if [ $# -lt 1 ]; then
  126. usage
  127. exit 1
  128. fi
  129. dir=$1
  130. SCRATCH_DIR=$dir
  131. PROJECT=${2:-logging}
  132. if [[ ! -f $dir/system.admin.jks || -z "$(keytool -list -keystore $dir/system.admin.jks -storepass kspass | grep sig-ca)" ]]; then
  133. generate_JKS_client_cert "system.admin"
  134. fi
  135. if [[ ! -f $dir/elasticsearch.jks || -z "$(keytool -list -keystore $dir/elasticsearch.jks -storepass kspass | grep sig-ca)" ]]; then
  136. generate_JKS_chain true elasticsearch "$(join , logging-es{,-ops})"
  137. fi
  138. if [[ ! -f $dir/logging-es.jks || -z "$(keytool -list -keystore $dir/logging-es.jks -storepass kspass | grep sig-ca)" ]]; then
  139. generate_JKS_chain false logging-es "$(join , logging-es{,-ops}{,-cluster}{,.${PROJECT}.svc.cluster.local})"
  140. fi
  141. [ ! -f $dir/truststore.jks ] && createTruststore
  142. # necessary so that the job knows it completed successfully
  143. exit 0