-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathvault.sh
45 lines (40 loc) · 1.14 KB
/
vault.sh
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
#!/bin/bash
if ! command -v jq &> /dev/null
then
echo "jq could not be found. Please install for correct work"
exit 1
fi
function vault_agent_cmd {
COMMAND=$1
if [[ ${VAULT_RUN_MODE:=legacy} == "docker" ]]; then
local VAULT_DOCKER_ARGS
VAULT_DOCKER_ARGS=(
--cap-add IPC_LOCK
-e VAULT_TOKEN="${VAULT_TOKEN}"
-e VAULT_ADDR="${VAULT_ADDR}"
-e VAULT_LOG_LEVEL="${VAULT_LOG_LEVEL:=info}"
)
docker run --rm -t \
"${VAULT_DOCKER_ARGS[@]}" \
${VAULT_DOCKER_IMAGE:=vault:latest} \
vault ${COMMAND}
else
vault ${COMMAND}
fi
}
function vault_get_keys_by_secret_path {
SECRET_PATH=$1
vault_agent_cmd "kv get -format json -field=data ${SECRET_PATH}"
}
function vault_load_variables_by_secret_path {
SECRET_PATH=$1
IS_EXPORT=${2:-false}
local values=$(vault_agent_cmd "kv get -format json -field=data ${SECRET_PATH}")
while IFS="=" read key value; do
if [[ ${IS_EXPORT} == true ]]; then
export ${key}="${value}"
else
eval ${key}="${value}"
fi
done < <( echo $values | jq --raw-output 'to_entries|map("\(.key|ascii_upcase)=\"\(.value|tostring)\"")|.[]')
}