2
2
3
3
set -euo pipefail
4
4
5
+ # shellcheck source-path=SCRIPTDIR
6
+ source " $( dirname " ${BASH_SOURCE[0]} " ) /../lib/log.sh"
7
+
5
8
readonly default_image=" hashicorp/envconsul"
6
9
readonly default_tag=" latest"
7
10
readonly image=" ${BUILDKITE_PLUGIN_VAULT_ENV_IMAGE:- ${default_image} } :${BUILDKITE_PLUGIN_VAULT_ENV_TAG:- ${default_tag} } "
8
11
9
12
# Fail if there is no Vault token; gotta log in first
10
13
# #######################################################################
11
14
if [ -z " ${VAULT_TOKEN:- } " ]; then
12
- echo " --- :skull_and_crossbones: Could not find 'VAULT_TOKEN' in the environment!"
13
- exit 1
15
+ raise_error " Could not find 'VAULT_TOKEN' in the environment!"
14
16
fi
15
17
16
18
# Resolve Vault address
@@ -20,8 +22,7 @@ if [ -n "${BUILDKITE_PLUGIN_VAULT_ENV_ADDRESS:-}" ]; then
20
22
export VAULT_ADDR
21
23
fi
22
24
if [ -z " ${VAULT_ADDR:- } " ]; then
23
- echo " --- :skull_and_crossbones: Could not find 'VAULT_ADDR' in the environment, and 'BUILDKITE_PLUGIN_VAULT_ENV_ADDRESS' was not specified!"
24
- exit 1
25
+ raise_error " Could not find 'VAULT_ADDR' in the environment, and 'BUILDKITE_PLUGIN_VAULT_ENV_ADDRESS' was not specified!"
25
26
fi
26
27
27
28
# Resolve Vault namespace
@@ -31,8 +32,7 @@ if [ -n "${BUILDKITE_PLUGIN_VAULT_ENV_NAMESPACE:-}" ]; then
31
32
export VAULT_NAMESPACE
32
33
fi
33
34
if [ -z " ${VAULT_NAMESPACE:- } " ]; then
34
- echo " --- :skull_and_crossbones: Could not find 'VAULT_NAMESPACE' in the environment, and 'BUILDKITE_PLUGIN_VAULT_ENV_NAMESPACE' was not specified!"
35
- exit 1
35
+ raise_error " Could not find 'VAULT_NAMESPACE' in the environment, and 'BUILDKITE_PLUGIN_VAULT_ENV_NAMESPACE' was not specified!"
36
36
fi
37
37
38
38
# Resolve secret prefix
@@ -45,10 +45,9 @@ if [[ -n "${secret_prefix}" && ! "${secret_prefix}" =~ /$ ]]; then
45
45
fi
46
46
readonly secret_prefix
47
47
48
+ # Resolve secrets
48
49
# #######################################################################
49
50
50
- readonly container_name=" vault-env-plugin-${BUILDKITE_JOB_ID} "
51
-
52
51
# STOLEN FROM https://github.com/buildkite-plugins/docker-buildkite-plugin/blob/9f90d8ef742d9fa1eb3556720e16f2b842ff1cb2/hooks/command#L25-L47
53
52
#
54
53
# Reads a list from plugin config into a global result array
@@ -61,8 +60,7 @@ plugin_read_list_into_result() {
61
60
local parameter=" ${prefix} _${i} "
62
61
63
62
if [[ -n " ${! prefix:- } " ]]; then
64
- echo " :rotating_light: Plugin received a string for $prefix , expected an array" >&2
65
- exit 1
63
+ raise_error " Plugin received a string for $prefix , expected an array"
66
64
fi
67
65
68
66
while [[ -n " ${! parameter:- } " ]]; do
@@ -75,24 +73,32 @@ plugin_read_list_into_result() {
75
73
[[ ${# result[@]} -gt 0 ]] || return 1
76
74
}
77
75
78
- envconsul_env () {
79
- # This populates a `result` array for later use
80
- plugin_read_list_into_result BUILDKITE_PLUGIN_VAULT_ENV_SECRETS
76
+ secrets=()
77
+ if plugin_read_list_into_result BUILDKITE_PLUGIN_VAULT_ENV_SECRETS; then
78
+ secrets=(" ${result[@]} " )
79
+ else
80
+ raise_error " At least one secret must be specified!"
81
+ fi
81
82
82
- secrets=()
83
- for secret in " ${result[@]} " ; do
83
+ # #######################################################################
84
+
85
+ readonly container_name=" vault-env-plugin-${BUILDKITE_JOB_ID} "
86
+
87
+ envconsul_env () {
88
+ secret_args=()
89
+ for secret in " ${secrets[@]} " ; do
84
90
# secret_prefix is guaranteed to end with a / if it is non-empty
85
- secrets +=(" -secret=${secret_prefix}${secret} " )
91
+ secret_args +=(" -secret=${secret_prefix}${secret} " )
86
92
done
87
93
88
94
# Explicitly *not* using `--rm` so we can output the container
89
95
# logs in case of a failure.
90
- docker run \
96
+ log_and_run docker run \
91
97
--env VAULT_TOKEN \
92
98
--name=" ${container_name} " \
93
99
-- \
94
100
" ${image} " \
95
- " ${secrets [@]} " \
101
+ " ${secret_args [@]} " \
96
102
-once \
97
103
-upcase \
98
104
-pristine \
@@ -105,23 +111,23 @@ envconsul_env() {
105
111
}
106
112
107
113
cleanup () {
108
- docker container rm --force " ${container_name} " > /dev/null 2>&1
114
+ log_and_run docker container rm --force " ${container_name} " > /dev/null 2>&1
109
115
}
110
116
111
117
trap cleanup EXIT INT QUIT
112
118
113
- echo " --- :vault: Pulling secrets from Vault"
114
- echo " Using Docker image: ${image} "
115
- echo " VAULT_ADDR=${VAULT_ADDR} "
116
- echo " VAULT_NAMESPACE=${VAULT_NAMESPACE} "
119
+ log " --- :vault: Pulling secrets from Vault"
120
+ log " Using Docker image: ${image} "
121
+ log " VAULT_ADDR=${VAULT_ADDR} "
122
+ log " VAULT_NAMESPACE=${VAULT_NAMESPACE} "
117
123
118
124
if vault_env=$( envconsul_env) ; then
119
125
set -o allexport
120
126
eval " ${vault_env} "
121
127
set +o allexport
122
128
else
123
129
retval=$?
124
- echo " --- :skull_and_crossbones: Failed to retrieve secrets from Vault"
125
- docker container logs " ${container_name} "
130
+ log " --- :skull_and_crossbones: Failed to retrieve secrets from Vault"
131
+ log_and_run docker container logs " ${container_name} "
126
132
exit ${retval}
127
133
fi
0 commit comments