2018-02-15 20:34:47 +00:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
|
|
|
|
# Here is a script to deploy cert to hashicorp vault
|
|
|
|
# (https://www.vaultproject.io/)
|
2019-10-05 13:06:58 +00:00
|
|
|
#
|
2018-02-15 20:34:47 +00:00
|
|
|
# it requires the vault binary to be available in PATH, and the following
|
|
|
|
# environment variables:
|
2019-10-05 13:06:58 +00:00
|
|
|
#
|
2018-02-15 20:34:47 +00:00
|
|
|
# VAULT_PREFIX - this contains the prefix path in vault
|
|
|
|
# VAULT_ADDR - vault requires this to find your vault server
|
|
|
|
#
|
|
|
|
# additionally, you need to ensure that VAULT_TOKEN is avialable or
|
|
|
|
# `vault auth` has applied the appropriate authorization for the vault binary
|
|
|
|
# to access the vault server
|
|
|
|
|
|
|
|
#returns 0 means success, otherwise error.
|
|
|
|
|
|
|
|
######## Public functions #####################
|
|
|
|
|
|
|
|
#domain keyfile certfile cafile fullchain
|
2018-02-20 14:11:45 +00:00
|
|
|
vault_cli_deploy() {
|
2018-02-15 20:34:47 +00:00
|
|
|
|
|
|
|
_cdomain="$1"
|
|
|
|
_ckey="$2"
|
|
|
|
_ccert="$3"
|
|
|
|
_cca="$4"
|
|
|
|
_cfullchain="$5"
|
|
|
|
|
|
|
|
_debug _cdomain "$_cdomain"
|
|
|
|
_debug _ckey "$_ckey"
|
|
|
|
_debug _ccert "$_ccert"
|
|
|
|
_debug _cca "$_cca"
|
|
|
|
_debug _cfullchain "$_cfullchain"
|
|
|
|
|
|
|
|
# validate required env vars
|
2018-02-16 14:01:26 +00:00
|
|
|
if [ -z "$VAULT_PREFIX" ]; then
|
2018-02-15 20:34:47 +00:00
|
|
|
_err "VAULT_PREFIX needs to be defined (contains prefix path in vault)"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2018-02-16 14:01:26 +00:00
|
|
|
if [ -z "$VAULT_ADDR" ]; then
|
2018-02-15 20:34:47 +00:00
|
|
|
_err "VAULT_ADDR needs to be defined (contains vault connection address)"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
VAULT_CMD=$(which vault)
|
2018-02-16 14:19:47 +00:00
|
|
|
if [ ! $? ]; then
|
2018-02-15 20:34:47 +00:00
|
|
|
_err "cannot find vault binary!"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2018-12-10 13:55:21 +00:00
|
|
|
if [ -n "$FABIO" ]; then
|
|
|
|
$VAULT_CMD write "${VAULT_PREFIX}/${_cdomain}" cert=@"$_cfullchain" key=@"$_ckey" || return 1
|
|
|
|
else
|
|
|
|
$VAULT_CMD write "${VAULT_PREFIX}/${_cdomain}/cert.pem" value=@"$_ccert" || return 1
|
|
|
|
$VAULT_CMD write "${VAULT_PREFIX}/${_cdomain}/cert.key" value=@"$_ckey" || return 1
|
|
|
|
$VAULT_CMD write "${VAULT_PREFIX}/${_cdomain}/chain.pem" value=@"$_cca" || return 1
|
|
|
|
$VAULT_CMD write "${VAULT_PREFIX}/${_cdomain}/fullchain.pem" value=@"$_cfullchain" || return 1
|
|
|
|
fi
|
2018-02-15 20:34:47 +00:00
|
|
|
|
|
|
|
}
|