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.

65 lines
1.7 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. _getdeployconf VAULT_ADDR
  33. if [ -z "$VAULT_ADDR" ]; then
  34. _err "VAULT_ADDR needs to be defined (contains vault connection address)"
  35. return 1
  36. fi
  37. # JSON does not allow multiline strings.
  38. # So replacing new-lines with "\n" here
  39. _ckey=$(sed -z 's/\n/\\n/g' <"$2")
  40. _ccert=$(sed -z 's/\n/\\n/g' <"$3")
  41. _cca=$(sed -z 's/\n/\\n/g' <"$4")
  42. _cfullchain=$(sed -z 's/\n/\\n/g' <"$5")
  43. URL="$VAULT_ADDR/v1/$VAULT_PREFIX/$_cdomain"
  44. export _H1="X-Vault-Token: $VAULT_TOKEN"
  45. if [ -n "$FABIO" ]; then
  46. _post "{\"cert\": \"$_cfullchain\", \"key\": \"$_ckey\"}" "$URL"
  47. else
  48. _post "{\"value\": \"$_ccert\"}" "$URL/cert.pem"
  49. _post "{\"value\": \"$_ckey\"}" "$URL/cert.key"
  50. _post "{\"value\": \"$_cca\"}" "$URL/chain.pem"
  51. _post "{\"value\": \"$_cfullchain\"}" "$URL/fullchain.pem"
  52. fi
  53. }