pull/860/head
Ilan Erenstein 2016-12-15 11:31:30 -08:00
parent 3aa54b6b20
commit 8669576493
1 changed files with 29 additions and 14 deletions

View File

@ -1,34 +1,49 @@
# Credit https://github.com/iljaweis/vault-bash-completion/ # ---------------------------------------------------------------------------
# vault-bash-completion
#
# This adds bash completions for [HashiCorp Vault](https://www.vaultproject.io/)
#
# see https://github.com/iljaweis/vault-bash-completion
# ---------------------------------------------------------------------------
function _vault_mounts() {
(
set -euo pipefail
if ! vault mounts 2> /dev/null | awk 'NR > 1 {print $1}'; then
echo "secret"
fi
)
}
function _vault() { function _vault() {
local VAULT_COMMANDS='delete path-help read renew revoke server status write audit-disable audit-enable audit-list auth auth-disable auth-enable capabilities generate-root init key-status list mount mount-tune mounts policies policy-delete policy-write rekey remount rotate seal ssh step-down token-create token-lookup token-renew token-revoke unmount unseal version' local VAULT_COMMANDS='delete path-help read renew revoke server status write audit-disable audit-enable audit-list auth auth-disable auth-enable capabilities generate-root init key-status list mount mount-tune mounts policies policy-delete policy-write rekey remount rotate seal ssh step-down token-create token-lookup token-renew token-revoke unmount unseal version'
# get root paths local cur
vault mounts >/dev/null 2>&1 local prev
if [ $? != 0 ]; then
# we do not have access to list mounts if [ $COMP_CWORD -gt 0 ]; then
local VAULT_ROOTPATH="secret" cur=${COMP_WORDS[COMP_CWORD]}
else prev=${COMP_WORDS[COMP_CWORD-1]}
local VAULT_ROOTPATH=$(vault mounts | tail -n +2 | awk '{print $1}' | paste -s -d ' ' -)
fi fi
local cur=${COMP_WORDS[COMP_CWORD]}
local line=${COMP_LINE} local line=${COMP_LINE}
if [ "$(echo $line | wc -w)" -le 2 ]; then if [[ $prev =~ ^(policies|policy-write|policy-delete) ]]; then
local policies=$(vault policies 2> /dev/null)
COMPREPLY=($(compgen -W "$policies" -- $cur))
elif [ "$(echo $line | wc -w)" -le 2 ]; then
if [[ "$line" =~ ^vault\ (read|write|delete|list)\ $ ]]; then if [[ "$line" =~ ^vault\ (read|write|delete|list)\ $ ]]; then
COMPREPLY=($(compgen -W "$VAULT_ROOTPATH" -- '')) COMPREPLY=($(compgen -W "$(_vault_mounts)" -- ''))
else else
COMPREPLY=($(compgen -W "$VAULT_COMMANDS" -- $cur)) COMPREPLY=($(compgen -W "$VAULT_COMMANDS" -- $cur))
fi fi
elif [[ "$line" =~ ^vault\ (read|write|delete|list)\ (.*)$ ]]; then elif [[ "$line" =~ ^vault\ (read|write|delete|list)\ (.*)$ ]]; then
path=${BASH_REMATCH[2]} path=${BASH_REMATCH[2]}
if [[ "$path" =~ ^([^ ]+)/([^ /]*)$ ]]; then if [[ "$path" =~ ^([^ ]+)/([^ /]*)$ ]]; then
list=$(vault list ${BASH_REMATCH[1]} | tail -n +2) list=$(vault list -format=yaml ${BASH_REMATCH[1]} 2> /dev/null | awk '{ print $2 }')
COMPREPLY=($(compgen -W "$list" -P "${BASH_REMATCH[1]}/" -- ${BASH_REMATCH[2]})) COMPREPLY=($(compgen -W "$list" -P "${BASH_REMATCH[1]}/" -- ${BASH_REMATCH[2]}))
else else
COMPREPLY=($(compgen -W "$VAULT_ROOTPATH" -- $path)) COMPREPLY=($(compgen -W "$(_vault_mounts)" -- $path))
fi fi
fi fi
} }