-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvault-auth
More file actions
executable file
·91 lines (75 loc) · 1.85 KB
/
vault-auth
File metadata and controls
executable file
·91 lines (75 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/bin/bash
# Simple script to authenticate to Vault server
# Set values
pkg=${0##*/}
VAULT_AUTH=${VAULT_AUTH:-"ping"}
# set colors
red=$(tput setaf 1)
green=$(tput setaf 2)
yellow=$(tput setaf 3)
blue=$(tput setaf 4)
purple=$(tput setaf 5)
cyan=$(tput setaf 6)
white=$(tput setaf 7)
reset=$(tput sgr0)
log() {
# Write messages to screen
echo "$(date +"%F %T") [${pkg}] $1"
}
die() {
log "${red}[FAIL] $1${reset}" >&2 && return 1
}
usage() {
cat <<EOM
${pkg}
SSH into remote host using Vault authentication.
Usage: ${pkg} [options]
Options:
-h, --help
Output help (this message)
-a=, --auth=[github | ping]
Provider to use for authentication. Defaults to "ping". Note that "github" is deprecated.
EOM
}
vault-auth() {
# Authenticate to Vault, getting access tokens. Store the role for future usage.
[[ -f ~/.vault-token ]] && rm ~/.vault-token > /dev/null
log "Authenticating to Vault for ${USER} using ${VAULT_AUTH} method."
if [[ ${VAULT_AUTH} == "ping" ]]; then
vault login -method=userpass -path=${VAULT_AUTH} username=${USER}
else
if [[ ! -z ${VAULT_AUTH_GITHUB_TOKEN} ]]; then
vault login -method=${VAULT_AUTH} token=${VAULT_AUTH_GITHUB_TOKEN}
else
die "Missing value for environment variable VAULT_AUTH_GITHUB_TOKEN"
fi
fi
}
# Process command line
for arg in "$@"; do
if test -n "$prev_arg"; then
eval "$prev_arg=\$arg"
prev_arg=
fi
case "$arg" in
-*=*) optarg=`echo "$arg" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
*) optarg= ;;
esac
case $arg in
-h | --help)
usage && exit 0
;;
-a=* | --auth=*)
VAULT_AUTH="$optarg"
;;
-*)
echo "${red}Unknown option ${arg}, exiting...${reset}" && exit 1
;;
*)
echo "${red}Unknown option or missing argument for ${arg}, exiting.${reset}"
usage
exit 1
;;
esac
done
vault-auth