You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.6 KiB

7 years ago
7 years ago
4 years ago
7 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. #!/usr/bin/env sh
  2. # Here is a script to deploy cert to hashicorp vault
  3. # (https://www.vaultproject.io/)
  4. #
  5. # it requires the vault binary to be available in PATH, and the following
  6. # environment variables:
  7. #
  8. # VAULT_PREFIX - this contains the prefix path in vault
  9. # VAULT_ADDR - vault requires this to find your vault server
  10. # VAULT_SAVE_TOKEN - set to anything if you want to save the token
  11. # VAULT_RENEW_TOKEN - set to anything if you want to renew the token to default TTL before deploying
  12. #
  13. # additionally, you need to ensure that VAULT_TOKEN is avialable or
  14. # `vault auth` has applied the appropriate authorization for the vault binary
  15. # to access the vault server
  16. #returns 0 means success, otherwise error.
  17. ######## Public functions #####################
  18. #domain keyfile certfile cafile fullchain
  19. vault_cli_deploy() {
  20. _cdomain="$1"
  21. _ckey="$2"
  22. _ccert="$3"
  23. _cca="$4"
  24. _cfullchain="$5"
  25. _debug _cdomain "$_cdomain"
  26. _debug _ckey "$_ckey"
  27. _debug _ccert "$_ccert"
  28. _debug _cca "$_cca"
  29. _debug _cfullchain "$_cfullchain"
  30. # validate required env vars
  31. _getdeployconf VAULT_PREFIX
  32. if [ -z "$VAULT_PREFIX" ]; then
  33. _err "VAULT_PREFIX needs to be defined (contains prefix path in vault)"
  34. return 1
  35. fi
  36. _savedeployconf VAULT_PREFIX "$VAULT_PREFIX"
  37. _getdeployconf VAULT_ADDR
  38. if [ -z "$VAULT_ADDR" ]; then
  39. _err "VAULT_ADDR needs to be defined (contains vault connection address)"
  40. return 1
  41. fi
  42. _savedeployconf VAULT_ADDR "$VAULT_ADDR"
  43. _getdeployconf VAULT_SAVE_TOKEN
  44. _savedeployconf VAULT_SAVE_TOKEN "$VAULT_SAVE_TOKEN"
  45. _getdeployconf VAULT_RENEW_TOKEN
  46. _savedeployconf VAULT_RENEW_TOKEN "$VAULT_RENEW_TOKEN"
  47. _getdeployconf VAULT_TOKEN
  48. if [ -z "$VAULT_TOKEN" ]; then
  49. _err "VAULT_TOKEN needs to be defined"
  50. return 1
  51. fi
  52. if [ -n "$VAULT_SAVE_TOKEN" ]; then
  53. _savedeployconf VAULT_TOKEN "$VAULT_TOKEN"
  54. fi
  55. _migratedeployconf FABIO VAULT_FABIO_MODE
  56. VAULT_CMD=$(command -v vault)
  57. if [ ! $? ]; then
  58. _err "cannot find vault binary!"
  59. return 1
  60. fi
  61. if [ -n "$VAULT_RENEW_TOKEN" ]; then
  62. _info "Renew the Vault token to default TTL"
  63. if ! $VAULT_CMD token renew; then
  64. _err "Failed to renew the Vault token"
  65. return 1
  66. fi
  67. fi
  68. if [ -n "$VAULT_FABIO_MODE" ]; then
  69. _info "Writing certificate and key to ${VAULT_PREFIX}/${_cdomain} in Fabio mode"
  70. $VAULT_CMD kv put "${VAULT_PREFIX}/${_cdomain}" cert=@"$_cfullchain" key=@"$_ckey" || return 1
  71. else
  72. _info "Writing certificate to ${VAULT_PREFIX}/${_cdomain}/cert.pem"
  73. $VAULT_CMD kv put "${VAULT_PREFIX}/${_cdomain}/cert.pem" value=@"$_ccert" || return 1
  74. _info "Writing key to ${VAULT_PREFIX}/${_cdomain}/cert.key"
  75. $VAULT_CMD kv put "${VAULT_PREFIX}/${_cdomain}/cert.key" value=@"$_ckey" || return 1
  76. _info "Writing CA certificate to ${VAULT_PREFIX}/${_cdomain}/ca.pem"
  77. $VAULT_CMD kv put "${VAULT_PREFIX}/${_cdomain}/ca.pem" value=@"$_cca" || return 1
  78. _info "Writing full-chain certificate to ${VAULT_PREFIX}/${_cdomain}/fullchain.pem"
  79. $VAULT_CMD kv put "${VAULT_PREFIX}/${_cdomain}/fullchain.pem" value=@"$_cfullchain" || return 1
  80. # To make it compatible with the wrong ca path `chain.pem` which was used in former versions
  81. if $VAULT_CMD kv get "${VAULT_PREFIX}/${_cdomain}/chain.pem" >/dev/null; then
  82. _err "The CA certificate has moved from chain.pem to ca.pem, if you don't depend on chain.pem anymore, you can delete it to avoid this warning"
  83. _info "Updating CA certificate to ${VAULT_PREFIX}/${_cdomain}/chain.pem for backward compatibility"
  84. $VAULT_CMD kv put "${VAULT_PREFIX}/${_cdomain}/chain.pem" value=@"$_cca" || return 1
  85. fi
  86. fi
  87. }