2016-07-21 13:48:49 +00:00
|
|
|
#!/bin/bash
|
2018-01-13 14:47:09 +00:00
|
|
|
set -euo pipefail
|
2016-07-21 13:48:49 +00:00
|
|
|
|
2018-03-10 11:47:55 +00:00
|
|
|
function require_gotool() {
|
2018-01-13 14:47:09 +00:00
|
|
|
toolname=$(basename $1)
|
2018-03-10 11:47:55 +00:00
|
|
|
if ! (which ${toolname} >/dev/null 2>&1); then
|
|
|
|
go version || {
|
|
|
|
echo "${basename} not found and no usable go environment"
|
|
|
|
exit 1
|
|
|
|
}
|
2018-01-13 14:47:09 +00:00
|
|
|
go get -u $1
|
|
|
|
fi
|
2017-07-06 12:18:45 +00:00
|
|
|
}
|
|
|
|
|
2018-01-13 14:47:09 +00:00
|
|
|
require_gotool github.com/Luzifer/vault-user-token
|
|
|
|
|
|
|
|
source "${HOME}/.config/vault-user-token"
|
|
|
|
source "${HOME}/bin/script_framework.sh"
|
2017-05-26 19:15:07 +00:00
|
|
|
|
2018-01-13 14:47:09 +00:00
|
|
|
# Check whether a valid token is available
|
2018-03-10 11:47:55 +00:00
|
|
|
(vault token-lookup >/dev/null 2>&1) && exit 0
|
2017-05-26 19:15:07 +00:00
|
|
|
|
2018-01-13 14:47:09 +00:00
|
|
|
step "Vault is not authenticated, trying to authenticate... "
|
2017-05-26 19:15:07 +00:00
|
|
|
|
2018-01-13 14:47:09 +00:00
|
|
|
# Remove old, invalid token
|
|
|
|
rm -f "${HOME}/.vault-token"
|
2017-05-26 22:09:02 +00:00
|
|
|
|
2018-01-13 14:47:09 +00:00
|
|
|
VUT=$(pgrep -f vault-user-token || echo "" | xargs)
|
2018-03-10 11:47:55 +00:00
|
|
|
[ -n "$VUT" ] && {
|
|
|
|
step "Killing old vault-user-token processes..."
|
|
|
|
kill ${VUT}
|
|
|
|
}
|
2017-05-26 21:58:37 +00:00
|
|
|
|
2018-01-13 14:47:09 +00:00
|
|
|
# Start new vault-user-token daemon
|
|
|
|
vault-user-token --full-hostname=false >/dev/null 2>&1 &
|
|
|
|
VUT=$!
|
2017-05-26 19:15:07 +00:00
|
|
|
|
2018-01-13 14:47:09 +00:00
|
|
|
step "Waiting for token to become available..."
|
|
|
|
while ! [ -f "${HOME}/.vault-token" ]; do
|
|
|
|
# Give the program a moment to get a token
|
|
|
|
sleep 0.5
|
|
|
|
|
2018-03-10 11:47:55 +00:00
|
|
|
if ! (kill -0 $VUT); then
|
2018-01-13 14:47:09 +00:00
|
|
|
fail "vault-user-token exitted, giving up."
|
2017-05-26 19:15:07 +00:00
|
|
|
fi
|
2018-01-13 14:47:09 +00:00
|
|
|
done
|
|
|
|
|
2018-03-10 11:47:55 +00:00
|
|
|
(vault token-lookup >/dev/null 2>&1) || fail "Vault authentication failed finally"
|
2017-05-26 19:15:07 +00:00
|
|
|
|
2018-01-13 14:47:09 +00:00
|
|
|
success "Vault token became available and is valid"
|