cert-setup.sh.jinja 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. {% macro cert_setup_script(values, random_cert_pass) -%}
  2. {%- set p12 = "/tmp/certs/ix.p12" %}
  3. {%- set key = "%s/%s"|format(values.consts.temp_certs_path, values.consts.key_name) %}
  4. {%- set cert = "%s/%s"|format(values.consts.temp_certs_path, values.consts.crt_name) %}
  5. {%- set keystore = "%s/%s"|format(values.consts.keystore_path, values.consts.keystore_name) %}
  6. #!/bin/sh
  7. mkdir -p /tmp/certs || { echo "Failed to create temp p12 certs path"; exit 1; }
  8. mkdir -p {{ values.consts.temp_certs_path }} || { echo "Failed to create temp certs path"; exit 1; }
  9. mkdir -p {{ values.consts.keystore_path }} || { echo "Failed to create keystore path"; exit 1; }
  10. if [ -f "{{ p12 }}" ]; then
  11. echo "Cleaning up old p12 certificate"
  12. rm -f "{{ p12 }}" || { echo "Failed to clean up old p12 certificate"; exit 1; }
  13. fi
  14. echo "Generating new p12 from key and cert"
  15. if [ -f "{{ key }}" ] && [ -f "{{ cert }}" ]; then
  16. echo "Found key and cert, generating p12 certificate"
  17. openssl pkcs12 -inkey "{{ key }}" -in "{{ cert }}" \
  18. -export -out "{{ p12 }}" \
  19. -password pass:{{ random_cert_pass }} || { echo "Failed to generate p12 certificate"; exit 1; }
  20. echo "Successfully generated p12 certificate"
  21. if [ -f "{{ keystore }}" ]; then
  22. echo "Cleaning up old keystore"; rm -f "{{ keystore }}"
  23. fi
  24. echo "Importing certificate into a new java keystore"
  25. keytool -importkeystore -srckeystore "{{ p12 }}" -srcstoretype pkcs12 \
  26. -destkeystore "{{ keystore }}" -deststoretype JKS \
  27. -srcstorepass "{{ random_cert_pass }}" \
  28. -deststorepass "{{ random_cert_pass }}" || { echo "Failed to import certificate"; exit 1; }
  29. echo "Certificate imported into keystore"
  30. else
  31. echo "Failed to find key and cert, skipping certificate import"; exit 1
  32. fi
  33. {%- endmacro %}