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.

78 lines
2.3 KiB

  1. #!/usr/bin/env sh
  2. # Here is a script to deploy cert to hashicorp vault using curl
  3. # (https://www.vaultproject.io/)
  4. #
  5. # it requires following environment variables:
  6. #
  7. # VAULT_PREFIX - this contains the prefix path in vault
  8. # VAULT_ADDR - vault requires this to find your vault server
  9. #
  10. # additionally, you need to ensure that VAULT_TOKEN is avialable
  11. # to access the vault server
  12. #returns 0 means success, otherwise error.
  13. ######## Public functions #####################
  14. #domain keyfile certfile cafile fullchain
  15. vault_deploy() {
  16. _cdomain="$1"
  17. _ckey="$2"
  18. _ccert="$3"
  19. _cca="$4"
  20. _cfullchain="$5"
  21. _debug _cdomain "$_cdomain"
  22. _debug _ckey "$_ckey"
  23. _debug _ccert "$_ccert"
  24. _debug _cca "$_cca"
  25. _debug _cfullchain "$_cfullchain"
  26. # validate required env vars
  27. _getdeployconf VAULT_PREFIX
  28. if [ -z "$VAULT_PREFIX" ]; then
  29. _err "VAULT_PREFIX needs to be defined (contains prefix path in vault)"
  30. return 1
  31. fi
  32. _savedeployconf VAULT_PREFIX "$VAULT_PREFIX"
  33. _getdeployconf VAULT_ADDR
  34. if [ -z "$VAULT_ADDR" ]; then
  35. _err "VAULT_ADDR needs to be defined (contains vault connection address)"
  36. return 1
  37. fi
  38. _savedeployconf VAULT_ADDR "$VAULT_ADDR"
  39. # JSON does not allow multiline strings.
  40. # So replacing new-lines with "\n" here
  41. _ckey=$(sed -z 's/\n/\\n/g' <"$2")
  42. _ccert=$(sed -z 's/\n/\\n/g' <"$3")
  43. _cca=$(sed -z 's/\n/\\n/g' <"$4")
  44. _cfullchain=$(sed -z 's/\n/\\n/g' <"$5")
  45. URL="$VAULT_ADDR/v1/$VAULT_PREFIX/$_cdomain"
  46. export _H1="X-Vault-Token: $VAULT_TOKEN"
  47. if [ -n "$FABIO" ]; then
  48. if [ -n "$VAULT_KV_V2" ]; then
  49. _post "{ \"data\": {\"cert\": \"$_cfullchain\", \"key\": \"$_ckey\"} }" "$URL"
  50. else
  51. _post "{\"cert\": \"$_cfullchain\", \"key\": \"$_ckey\"}" "$URL"
  52. fi
  53. else
  54. if [ -n "$VAULT_KV_V2" ]; then
  55. _post "{\"data\": {\"value\": \"$_ccert\"}}" "$URL/cert.pem"
  56. _post "{\"data\": {\"value\": \"$_ckey\"}}" "$URL/cert.key"
  57. _post "{\"data\": {\"value\": \"$_cca\"}}" "$URL/chain.pem"
  58. _post "{\"data\": {\"value\": \"$_cfullchain\"}}" "$URL/fullchain.pem"
  59. else
  60. _post "{\"value\": \"$_ccert\"}" "$URL/cert.pem"
  61. _post "{\"value\": \"$_ckey\"}" "$URL/cert.key"
  62. _post "{\"value\": \"$_cca\"}" "$URL/chain.pem"
  63. _post "{\"value\": \"$_cfullchain\"}" "$URL/fullchain.pem"
  64. fi
  65. fi
  66. }