vault.sh 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. _migratedeployconf FABIO VAULT_FABIO_MODE
  57. # JSON does not allow multiline strings.
  58. # So replacing new-lines with "\n" here
  59. _ckey=$(sed -e ':a' -e N -e '$ ! ba' -e 's/\n/\\n/g' <"$2")
  60. _ccert=$(sed -e ':a' -e N -e '$ ! ba' -e 's/\n/\\n/g' <"$3")
  61. _cca=$(sed -e ':a' -e N -e '$ ! ba' -e 's/\n/\\n/g' <"$4")
  62. _cfullchain=$(sed -e ':a' -e N -e '$ ! ba' -e 's/\n/\\n/g' <"$5")
  63. export _H1="X-Vault-Token: $VAULT_TOKEN"
  64. if [ -n "$VAULT_RENEW_TOKEN" ]; then
  65. URL="$VAULT_ADDR/v1/auth/token/renew-self"
  66. _info "Renew the Vault token to default TTL"
  67. _response=$(_post "" "$URL")
  68. if [ "$?" != "0" ]; then
  69. _err "Failed to renew the Vault token"
  70. return 1
  71. fi
  72. if echo "$_response" | grep -q '"errors":\['; then
  73. _err "Failed to renew the Vault token: $_response"
  74. return 1
  75. fi
  76. fi
  77. URL="$VAULT_ADDR/v1/$VAULT_PREFIX/$_cdomain"
  78. if [ -n "$VAULT_FABIO_MODE" ]; then
  79. _info "Writing certificate and key to $URL in Fabio mode"
  80. if [ -n "$VAULT_KV_V2" ]; then
  81. _response=$(_post "{ \"data\": {\"cert\": \"$_cfullchain\", \"key\": \"$_ckey\"} }" "$URL")
  82. if [ "$?" != "0" ]; then return 1; fi
  83. if echo "$_response" | grep -q '"errors":\['; then
  84. _err "Vault error: $_response"
  85. return 1
  86. fi
  87. else
  88. _response=$(_post "{\"cert\": \"$_cfullchain\", \"key\": \"$_ckey\"}" "$URL")
  89. if [ "$?" != "0" ]; then return 1; fi
  90. if echo "$_response" | grep -q '"errors":\['; then
  91. _err "Vault error: $_response"
  92. return 1
  93. fi
  94. fi
  95. else
  96. if [ -n "$VAULT_KV_V2" ]; then
  97. _info "Writing certificate to $URL/cert.pem"
  98. _response=$(_post "{\"data\": {\"value\": \"$_ccert\"}}" "$URL/cert.pem")
  99. if [ "$?" != "0" ]; then return 1; fi
  100. if echo "$_response" | grep -q '"errors":\['; then
  101. _err "Vault error writing cert.pem: $_response"
  102. return 1
  103. fi
  104. _info "Writing key to $URL/cert.key"
  105. _response=$(_post "{\"data\": {\"value\": \"$_ckey\"}}" "$URL/cert.key")
  106. if [ "$?" != "0" ]; then return 1; fi
  107. if echo "$_response" | grep -q '"errors":\['; then
  108. _err "Vault error writing cert.key: $_response"
  109. return 1
  110. fi
  111. _info "Writing CA certificate to $URL/ca.pem"
  112. _response=$(_post "{\"data\": {\"value\": \"$_cca\"}}" "$URL/ca.pem")
  113. if [ "$?" != "0" ]; then return 1; fi
  114. if echo "$_response" | grep -q '"errors":\['; then
  115. _err "Vault error writing ca.pem: $_response"
  116. return 1
  117. fi
  118. _info "Writing full-chain certificate to $URL/fullchain.pem"
  119. _response=$(_post "{\"data\": {\"value\": \"$_cfullchain\"}}" "$URL/fullchain.pem")
  120. if [ "$?" != "0" ]; then return 1; fi
  121. if echo "$_response" | grep -q '"errors":\['; then
  122. _err "Vault error writing fullchain.pem: $_response"
  123. return 1
  124. fi
  125. else
  126. _info "Writing certificate to $URL/cert.pem"
  127. _response=$(_post "{\"value\": \"$_ccert\"}" "$URL/cert.pem")
  128. if [ "$?" != "0" ]; then return 1; fi
  129. if echo "$_response" | grep -q '"errors":\['; then
  130. _err "Vault error writing cert.pem: $_response"
  131. return 1
  132. fi
  133. _info "Writing key to $URL/cert.key"
  134. _response=$(_post "{\"value\": \"$_ckey\"}" "$URL/cert.key")
  135. if [ "$?" != "0" ]; then return 1; fi
  136. if echo "$_response" | grep -q '"errors":\['; then
  137. _err "Vault error writing cert.key: $_response"
  138. return 1
  139. fi
  140. _info "Writing CA certificate to $URL/ca.pem"
  141. _response=$(_post "{\"value\": \"$_cca\"}" "$URL/ca.pem")
  142. if [ "$?" != "0" ]; then return 1; fi
  143. if echo "$_response" | grep -q '"errors":\['; then
  144. _err "Vault error writing ca.pem: $_response"
  145. return 1
  146. fi
  147. _info "Writing full-chain certificate to $URL/fullchain.pem"
  148. _response=$(_post "{\"value\": \"$_cfullchain\"}" "$URL/fullchain.pem")
  149. if [ "$?" != "0" ]; then return 1; fi
  150. if echo "$_response" | grep -q '"errors":\['; then
  151. _err "Vault error writing fullchain.pem: $_response"
  152. return 1
  153. fi
  154. fi
  155. # To make it compatible with the wrong ca path `chain.pem` which was used in former versions
  156. if _contains "$(_get "$URL/chain.pem")" "-----BEGIN CERTIFICATE-----"; then
  157. _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"
  158. _info "Updating CA certificate to $URL/chain.pem for backward compatibility"
  159. if [ -n "$VAULT_KV_V2" ]; then
  160. _response=$(_post "{\"data\": {\"value\": \"$_cca\"}}" "$URL/chain.pem")
  161. if [ "$?" != "0" ]; then return 1; fi
  162. if echo "$_response" | grep -q '"errors":\['; then
  163. _err "Vault error writing chain.pem: $_response"
  164. return 1
  165. fi
  166. else
  167. _response=$(_post "{\"value\": \"$_cca\"}" "$URL/chain.pem")
  168. if [ "$?" != "0" ]; then return 1; fi
  169. if echo "$_response" | grep -q '"errors":\['; then
  170. _err "Vault error writing chain.pem: $_response"
  171. return 1
  172. fi
  173. fi
  174. fi
  175. fi
  176. }