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.

102 lines
3.4 KiB

7 years ago
7 years ago
5 years ago
7 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. VAULT_CMD=$(command -v vault)
  56. if [ ! $? ]; then
  57. _err "cannot find vault binary!"
  58. return 1
  59. fi
  60. if [ -n "$VAULT_RENEW_TOKEN" ]; then
  61. _info "Renew the token to default TTL"
  62. if ! $VAULT_CMD token renew; then
  63. _err "Failed to renew the token"
  64. return 1
  65. fi
  66. fi
  67. if [ -n "$FABIO" ]; then
  68. _info "Writing certificate and key to $URL in Fabio mode"
  69. $VAULT_CMD kv put "${VAULT_PREFIX}/${_cdomain}" cert=@"$_cfullchain" key=@"$_ckey" || return 1
  70. else
  71. _info "Writing certificate to $URL/cert.pem"
  72. $VAULT_CMD kv put "${VAULT_PREFIX}/${_cdomain}/cert.pem" value=@"$_ccert" || return 1
  73. _info "Writing key to $URL/cert.key"
  74. $VAULT_CMD kv put "${VAULT_PREFIX}/${_cdomain}/cert.key" value=@"$_ckey" || return 1
  75. _info "Writing CA certificate to $URL/ca.pem"
  76. $VAULT_CMD kv put "${VAULT_PREFIX}/${_cdomain}/ca.pem" value=@"$_cca" || return 1
  77. _info "Writing full-chain certificate to $URL/fullchain.pem"
  78. $VAULT_CMD kv put "${VAULT_PREFIX}/${_cdomain}/fullchain.pem" value=@"$_cfullchain" || return 1
  79. # To make it compatible with the wrong ca path `chain.pem` which was used in former versions
  80. if $VAULT_CMD kv get "${VAULT_PREFIX}/${_cdomain}/chain.pem" >/dev/null; then
  81. _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"
  82. _info "Updating CA certificate to $URL/chain.pem for backward compatibility"
  83. $VAULT_CMD kv put "${VAULT_PREFIX}/${_cdomain}/chain.pem" value=@"$_cca" || return 1
  84. fi
  85. fi
  86. }