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.

62 lines
2.0 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. if [ -z "$VAULT_PREFIX" ]; then
  28. _err "VAULT_PREFIX needs to be defined (contains prefix path in vault)"
  29. return 1
  30. fi
  31. if [ -z "$VAULT_ADDR" ]; then
  32. _err "VAULT_ADDR needs to be defined (contains vault connection address)"
  33. return 1
  34. fi
  35. # JSON does not allow multiline strings.
  36. # So replacing new-lines with "\n" here
  37. _ckey=$(sed -z 's/\n/\\n/g' <"$2")
  38. _ccert=$(sed -z 's/\n/\\n/g' <"$3")
  39. _cca=$(sed -z 's/\n/\\n/g' <"$4")
  40. _cfullchain=$(sed -z 's/\n/\\n/g' <"$5")
  41. URL="$VAULT_ADDR/v1/$VAULT_PREFIX/$_cdomain"
  42. if [ -n "$FABIO" ]; then
  43. curl --silent -H "X-Vault-Token: $VAULT_TOKEN" --request POST --data "{\"cert\": \"$_cfullchain\", \"key\": \"$_ckey\"}" "$URL" || return 1
  44. else
  45. curl --silent -H "X-Vault-Token: $VAULT_TOKEN" --request POST --data "{\"value\": \"$_ccert\"}" "$URL/cert.pem" || return 1
  46. curl --silent -H "X-Vault-Token: $VAULT_TOKEN" --request POST --data "{\"value\": \"$_ckey\"}" "$URL/cert.key" || return 1
  47. curl --silent -H "X-Vault-Token: $VAULT_TOKEN" --request POST --data "{\"value\": \"$_cca\"}" "$URL/chain.pem" || return 1
  48. curl --silent -H "X-Vault-Token: $VAULT_TOKEN" --request POST --data "{\"value\": \"$_cfullchain\"}" "$URL/fullchain.pem" || return 1
  49. fi
  50. }