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.

129 lines
4.5 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. # VAULT_SAVE_TOKEN - set to anything if you want to save the token
  10. # VAULT_RENEW_TOKEN - set to anything if you want to renew the token to default TTL before deploying
  11. # VAULT_KV_V2 - set to anything if you are using v2 of the kv engine
  12. #
  13. # additionally, you need to ensure that VAULT_TOKEN is avialable
  14. # to access the vault server
  15. #returns 0 means success, otherwise error.
  16. ######## Public functions #####################
  17. #domain keyfile certfile cafile fullchain
  18. vault_deploy() {
  19. _cdomain="$1"
  20. _ckey="$2"
  21. _ccert="$3"
  22. _cca="$4"
  23. _cfullchain="$5"
  24. _debug _cdomain "$_cdomain"
  25. _debug _ckey "$_ckey"
  26. _debug _ccert "$_ccert"
  27. _debug _cca "$_cca"
  28. _debug _cfullchain "$_cfullchain"
  29. # validate required env vars
  30. _getdeployconf VAULT_PREFIX
  31. if [ -z "$VAULT_PREFIX" ]; then
  32. _err "VAULT_PREFIX needs to be defined (contains prefix path in vault)"
  33. return 1
  34. fi
  35. _savedeployconf VAULT_PREFIX "$VAULT_PREFIX"
  36. _getdeployconf VAULT_ADDR
  37. if [ -z "$VAULT_ADDR" ]; then
  38. _err "VAULT_ADDR needs to be defined (contains vault connection address)"
  39. return 1
  40. fi
  41. _savedeployconf VAULT_ADDR "$VAULT_ADDR"
  42. _getdeployconf VAULT_SAVE_TOKEN
  43. _savedeployconf VAULT_SAVE_TOKEN "$VAULT_SAVE_TOKEN"
  44. _getdeployconf VAULT_RENEW_TOKEN
  45. _savedeployconf VAULT_RENEW_TOKEN "$VAULT_RENEW_TOKEN"
  46. _getdeployconf VAULT_KV_V2
  47. _savedeployconf VAULT_KV_V2 "$VAULT_KV_V2"
  48. _getdeployconf VAULT_TOKEN
  49. if [ -z "$VAULT_TOKEN" ]; then
  50. _err "VAULT_TOKEN needs to be defined"
  51. return 1
  52. fi
  53. if [ -n "$VAULT_SAVE_TOKEN" ]; then
  54. _savedeployconf VAULT_TOKEN "$VAULT_TOKEN"
  55. fi
  56. # JSON does not allow multiline strings.
  57. # So replacing new-lines with "\n" here
  58. _ckey=$(sed -z 's/\n/\\n/g' <"$2")
  59. _ccert=$(sed -z 's/\n/\\n/g' <"$3")
  60. _cca=$(sed -z 's/\n/\\n/g' <"$4")
  61. _cfullchain=$(sed -z 's/\n/\\n/g' <"$5")
  62. export _H1="X-Vault-Token: $VAULT_TOKEN"
  63. if [ -n "$VAULT_RENEW_TOKEN" ]; then
  64. URL="$VAULT_ADDR/v1/auth/token/renew-self"
  65. _info "Renew the token to default TTL"
  66. if ! _post "" "$URL" >/dev/null; then
  67. _err "Failed to renew the token"
  68. return 1
  69. fi
  70. fi
  71. URL="$VAULT_ADDR/v1/$VAULT_PREFIX/$_cdomain"
  72. if [ -n "$FABIO" ]; then
  73. _info "Writing certificate and key to $URL in Fabio mode"
  74. if [ -n "$VAULT_KV_V2" ]; then
  75. _post "{ \"data\": {\"cert\": \"$_cfullchain\", \"key\": \"$_ckey\"} }" "$URL" >/dev/null || return 1
  76. else
  77. _post "{\"cert\": \"$_cfullchain\", \"key\": \"$_ckey\"}" "$URL" >/dev/null || return 1
  78. fi
  79. else
  80. if [ -n "$VAULT_KV_V2" ]; then
  81. _info "Writing certificate to $URL/cert.pem"
  82. _post "{\"data\": {\"value\": \"$_ccert\"}}" "$URL/cert.pem" >/dev/null || return 1
  83. _info "Writing key to $URL/cert.key"
  84. _post "{\"data\": {\"value\": \"$_ckey\"}}" "$URL/cert.key" >/dev/null || return 1
  85. _info "Writing CA certificate to $URL/ca.pem"
  86. _post "{\"data\": {\"value\": \"$_cca\"}}" "$URL/ca.pem" >/dev/null || return 1
  87. _info "Writing full-chain certificate to $URL/fullchain.pem"
  88. _post "{\"data\": {\"value\": \"$_cfullchain\"}}" "$URL/fullchain.pem" >/dev/null || return 1
  89. else
  90. _info "Writing certificate to $URL/cert.pem"
  91. _post "{\"value\": \"$_ccert\"}" "$URL/cert.pem" >/dev/null || return 1
  92. _info "Writing key to $URL/cert.key"
  93. _post "{\"value\": \"$_ckey\"}" "$URL/cert.key" >/dev/null || return 1
  94. _info "Writing CA certificate to $URL/ca.pem"
  95. _post "{\"value\": \"$_cca\"}" "$URL/ca.pem" >/dev/null || return 1
  96. _info "Writing full-chain certificate to $URL/fullchain.pem"
  97. _post "{\"value\": \"$_cfullchain\"}" "$URL/fullchain.pem" >/dev/null || return 1
  98. fi
  99. # To make it compatible with the wrong ca path `chain.pem` which was used in former versions
  100. if _contains "$(_get "$URL/chain.pem")" "-----BEGIN CERTIFICATE-----"; then
  101. _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"
  102. _info "Updating CA certificate to $URL/chain.pem for backward compatibility"
  103. if [ -n "$VAULT_KV_V2" ]; then
  104. _post "{\"data\": {\"value\": \"$_cca\"}}" "$URL/chain.pem" >/dev/null || return 1
  105. else
  106. _post "{\"value\": \"$_cca\"}" "$URL/chain.pem" >/dev/null || return 1
  107. fi
  108. fi
  109. fi
  110. }