3 # Here is a script to deploy cert to hashicorp vault using curl
4 # (https://www.vaultproject.io/)
6 # it requires following environment variables:
8 # VAULT_PREFIX - this contains the prefix path in vault
9 # VAULT_ADDR - vault requires this to find your vault server
10 # VAULT_SAVE_TOKEN - set to anything if you want to save the token
11 # VAULT_RENEW_TOKEN - set to anything if you want to renew the token to default TTL before deploying
12 # VAULT_KV_V2 - set to anything if you are using v2 of the kv engine
14 # additionally, you need to ensure that VAULT_TOKEN is avialable
15 # to access the vault server
17 #returns 0 means success, otherwise error.
19 ######## Public functions #####################
21 #domain keyfile certfile cafile fullchain
30 _debug _cdomain
"$_cdomain"
32 _debug _ccert
"$_ccert"
34 _debug _cfullchain
"$_cfullchain"
36 # validate required env vars
37 _getdeployconf VAULT_PREFIX
38 if [ -z "$VAULT_PREFIX" ]; then
39 _err
"VAULT_PREFIX needs to be defined (contains prefix path in vault)"
42 _savedeployconf VAULT_PREFIX
"$VAULT_PREFIX"
44 _getdeployconf VAULT_ADDR
45 if [ -z "$VAULT_ADDR" ]; then
46 _err
"VAULT_ADDR needs to be defined (contains vault connection address)"
49 _savedeployconf VAULT_ADDR
"$VAULT_ADDR"
51 _getdeployconf VAULT_SAVE_TOKEN
52 _savedeployconf VAULT_SAVE_TOKEN
"$VAULT_SAVE_TOKEN"
54 _getdeployconf VAULT_RENEW_TOKEN
55 _savedeployconf VAULT_RENEW_TOKEN
"$VAULT_RENEW_TOKEN"
57 _getdeployconf VAULT_KV_V2
58 _savedeployconf VAULT_KV_V2
"$VAULT_KV_V2"
60 _getdeployconf VAULT_TOKEN
61 if [ -z "$VAULT_TOKEN" ]; then
62 _err
"VAULT_TOKEN needs to be defined"
65 if [ -n "$VAULT_SAVE_TOKEN" ]; then
66 _savedeployconf VAULT_TOKEN
"$VAULT_TOKEN"
69 _migratedeployconf FABIO VAULT_FABIO_MODE
71 # JSON does not allow multiline strings.
72 # So replacing new-lines with "\n" here
73 _ckey
=$
(sed -z 's/\n/\\n/g' <"$2")
74 _ccert
=$
(sed -z 's/\n/\\n/g' <"$3")
75 _cca
=$
(sed -z 's/\n/\\n/g' <"$4")
76 _cfullchain
=$
(sed -z 's/\n/\\n/g' <"$5")
78 export _H1
="X-Vault-Token: $VAULT_TOKEN"
80 if [ -n "$VAULT_RENEW_TOKEN" ]; then
81 URL
="$VAULT_ADDR/v1/auth/token/renew-self"
82 _info
"Renew the Vault token to default TTL"
83 if ! _post
"" "$URL" >/dev
/null
; then
84 _err
"Failed to renew the Vault token"
89 URL
="$VAULT_ADDR/v1/$VAULT_PREFIX/$_cdomain"
91 if [ -n "$VAULT_FABIO_MODE" ]; then
92 _info
"Writing certificate and key to $URL in Fabio mode"
93 if [ -n "$VAULT_KV_V2" ]; then
94 _post
"{ \"data\": {\"cert\": \"$_cfullchain\", \"key\": \"$_ckey\"} }" "$URL" >/dev
/null ||
return 1
96 _post
"{\"cert\": \"$_cfullchain\", \"key\": \"$_ckey\"}" "$URL" >/dev
/null ||
return 1
99 if [ -n "$VAULT_KV_V2" ]; then
100 _info
"Writing certificate to $URL/cert.pem"
101 _post
"{\"data\": {\"value\": \"$_ccert\"}}" "$URL/cert.pem" >/dev
/null ||
return 1
102 _info
"Writing key to $URL/cert.key"
103 _post
"{\"data\": {\"value\": \"$_ckey\"}}" "$URL/cert.key" >/dev
/null ||
return 1
104 _info
"Writing CA certificate to $URL/ca.pem"
105 _post
"{\"data\": {\"value\": \"$_cca\"}}" "$URL/ca.pem" >/dev
/null ||
return 1
106 _info
"Writing full-chain certificate to $URL/fullchain.pem"
107 _post
"{\"data\": {\"value\": \"$_cfullchain\"}}" "$URL/fullchain.pem" >/dev
/null ||
return 1
109 _info
"Writing certificate to $URL/cert.pem"
110 _post
"{\"value\": \"$_ccert\"}" "$URL/cert.pem" >/dev
/null ||
return 1
111 _info
"Writing key to $URL/cert.key"
112 _post
"{\"value\": \"$_ckey\"}" "$URL/cert.key" >/dev
/null ||
return 1
113 _info
"Writing CA certificate to $URL/ca.pem"
114 _post
"{\"value\": \"$_cca\"}" "$URL/ca.pem" >/dev
/null ||
return 1
115 _info
"Writing full-chain certificate to $URL/fullchain.pem"
116 _post
"{\"value\": \"$_cfullchain\"}" "$URL/fullchain.pem" >/dev
/null ||
return 1
119 # To make it compatible with the wrong ca path `chain.pem` which was used in former versions
120 if _contains
"$(_get "$URL/chain.pem
")" "-----BEGIN CERTIFICATE-----"; then
121 _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"
122 _info
"Updating CA certificate to $URL/chain.pem for backward compatibility"
123 if [ -n "$VAULT_KV_V2" ]; then
124 _post
"{\"data\": {\"value\": \"$_cca\"}}" "$URL/chain.pem" >/dev
/null ||
return 1
126 _post
"{\"value\": \"$_cca\"}" "$URL/chain.pem" >/dev
/null ||
return 1