entrypoint.sh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/bin/bash
  2. set -eo pipefail
  3. # Paraphrased from:
  4. # https://github.com/influxdata/influxdata-docker/blob/0d341f18067c4652dfa8df7dcb24d69bf707363d/influxdb/2.0/entrypoint.sh
  5. # (a repo with no LICENSE.md)
  6. export STEPPATH=$(step path)
  7. # List of env vars required for step ca init
  8. declare -ra REQUIRED_INIT_VARS=(DOCKER_STEPCA_INIT_NAME DOCKER_STEPCA_INIT_DNS_NAMES)
  9. # Ensure all env vars required to run step ca init are set.
  10. function init_if_possible () {
  11. local missing_vars=0
  12. for var in "${REQUIRED_INIT_VARS[@]}"; do
  13. if [ -z "${!var}" ]; then
  14. missing_vars=1
  15. fi
  16. done
  17. if [ ${missing_vars} = 1 ]; then
  18. >&2 echo "there is no ca.json config file; please run step ca init, or provide config parameters via DOCKER_STEPCA_INIT_ vars"
  19. else
  20. step_ca_init "${@}"
  21. fi
  22. }
  23. function generate_password () {
  24. set +o pipefail
  25. < /dev/urandom tr -dc A-Za-z0-9 | head -c40
  26. echo
  27. set -o pipefail
  28. }
  29. # Initialize a CA if not already initialized
  30. function step_ca_init () {
  31. local -a setup_args=(
  32. --name "${DOCKER_STEPCA_INIT_NAME}"
  33. --dns "${DOCKER_STEPCA_INIT_DNS_NAMES}"
  34. --provisioner "${DOCKER_STEPCA_INIT_PROVISIONER_NAME:-admin}"
  35. --password-file "${STEPPATH}/password"
  36. --provisioner-password-file "${STEPPATH}/provisioner_password"
  37. --address ":9000"
  38. )
  39. if [ -n "${DOCKER_STEPCA_INIT_PASSWORD}" ]; then
  40. echo "${DOCKER_STEPCA_INIT_PASSWORD}" > "${STEPPATH}/password"
  41. echo "${DOCKER_STEPCA_INIT_PASSWORD}" > "${STEPPATH}/provisioner_password"
  42. else
  43. generate_password > "${STEPPATH}/password"
  44. generate_password > "${STEPPATH}/provisioner_password"
  45. fi
  46. if [ -n "${DOCKER_STEPCA_INIT_SSH}" ]; then
  47. setup_args=("${setup_args[@]}" --ssh)
  48. fi
  49. if [ -n "${DOCKER_STEPCA_INIT_ACME}" ]; then
  50. setup_args=("${setup_args[@]}" --acme)
  51. fi
  52. if [ -n "${DOCKER_STEPCA_INIT_REMOTE_MANAGEMENT}" ]; then
  53. setup_args=("${setup_args[@]}" --remote-management)
  54. fi
  55. step ca init "${setup_args[@]}"
  56. mv $STEPPATH/password $PWDPATH
  57. mv $STEPPATH/provisioner_password $PROVISIONER_PWDPATH
  58. }
  59. if [ -f /usr/sbin/pcscd ]; then
  60. /usr/sbin/pcscd
  61. fi
  62. if [ ! -f "${STEPPATH}/config/ca.json" ]; then
  63. init_if_possible
  64. fi
  65. if [ ! -f "${PROVISIONER_PWDPATH}" ]; then
  66. # For backward compatibility,
  67. # if the --provisioner-password-file doesn't exist,
  68. # use the same password as the CA.
  69. cp ${PWDPATH} ${PROVISIONER_PWDPATH}
  70. fi
  71. exec "${@}"