Skip to content
This repository was archived by the owner on Dec 26, 2022. It is now read-only.

Commit 2f92b19

Browse files
Merge pull request #25 from grapl-security/cm/always-pull
Add an `always-pull` option
2 parents 953b528 + e4ee474 commit 2f92b19

File tree

7 files changed

+103
-18
lines changed

7 files changed

+103
-18
lines changed

.buildkite/pipeline.verify.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,12 @@ steps:
4040
- "grapl-security/vault-login#${BUILDKITE_COMMIT}":
4141
- improbable-eng/metahook#v0.4.1:
4242
pre-exit: .buildkite/scripts/pre-exit-hook-validation.sh
43+
44+
- label: ":buildkite::vault: Validate Plugin Behavior with pull"
45+
command:
46+
- .buildkite/scripts/environment-hook-validation.sh
47+
plugins:
48+
- "grapl-security/vault-login#${BUILDKITE_COMMIT}":
49+
always-pull: true
50+
- improbable-eng/metahook#v0.4.1:
51+
pre-exit: .buildkite/scripts/pre-exit-hook-validation.sh

README.md

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -82,38 +82,29 @@ Setting `attempt_count` to `1` effectively disables the retry logic.
8282

8383
## Configuration
8484

85-
### `address` (optional, string)
85+
### `vault` Flags
86+
87+
#### `address` (optional, string)
8688

8789
The address of the Vault server to access. If not set, falls back to
8890
`VAULT_ADDR` in the environment. If `VAULT_ADDR` is not set either,
8991
the plugin fails with an error.
9092

91-
### `auth_role` (optional, string)
93+
#### `auth_role` (optional, string)
9294

9395
The name of the Vault AWS role to authenticate as. If not specified,
9496
uses (Grapl-specific) logic to generate the role name from the
9597
Buildkite agent queue name.
9698

97-
### `image` (optional, string)
98-
99-
The container image with the `vault` binary that the plugin uses. Any
100-
container used should have the `vault` binary as its entrypoint.
101-
102-
Defaults to `hashicorp/vault`.
103-
104-
### `namespace` (optional, string)
99+
#### `namespace` (optional, string)
105100

106101
The Vault namespace to access. If not set, falls back to
107102
`VAULT_NAMESPACE` in the environment. If `VAULT_NAMESPACE` is not set
108103
either, the plugin fails with an error.
109104

110-
### `tag` (optional, string)
105+
### Retry Configuration
111106

112-
The container image tag the plugin uses.
113-
114-
Defaults to `latest`.
115-
116-
### `attempt_count` (optional, integer)
107+
#### `attempt_count` (optional, integer)
117108

118109
The number of times to attempt to login to Vault before giving
119110
up.
@@ -122,12 +113,35 @@ Defaults to `3`.
122113

123114
You can disable retries by setting this to `1`.
124115

125-
### `attempt_wait_seconds` (optional, integer)
116+
#### `attempt_wait_seconds` (optional, integer)
126117

127118
The number of seconds to wait between each retry attempt.
128119

129120
Defaults to `5`.
130121

122+
### Container Image Configuration
123+
124+
#### `image` (optional, string)
125+
126+
The container image with the `vault` binary that the plugin uses. Any
127+
container used should have the `vault` binary as its entrypoint.
128+
129+
Defaults to `hashicorp/vault`.
130+
131+
#### `tag` (optional, string)
132+
133+
The container image tag the plugin uses.
134+
135+
Defaults to `latest`.
136+
137+
#### `always_pull` (optional, boolean)
138+
139+
Whether or not to perform an explicit `docker pull` of the configured
140+
image before running. Useful when using the `latest` tag to ensure you
141+
are always using the _actual_ latest image.
142+
143+
Defaults to `false`.
144+
131145
## Building
132146

133147
Requires `make`, `docker`, and Docker Compose v2.

hooks/environment

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ if (("${attempt_wait_seconds}" < 1)); then
6565
raise_error "Must provide a positive value for attempt_wait_seconds!"
6666
fi
6767

68+
maybe_pull_image
69+
6870
echo "--- :vault: Login to ${VAULT_ADDR}"
6971
echo "Using Docker image: ${image}"
7072
echo "VAULT_ADDR=${VAULT_ADDR}"

lib/vault.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,26 @@
22

33
set -euo pipefail
44

5+
# shellcheck source-path=SCRIPTDIR
6+
source "$(dirname "${BASH_SOURCE[0]}")/log.sh"
7+
58
readonly default_image="hashicorp/vault"
69
readonly default_tag="latest"
710
# TODO: add a "debug" mode where we spit out the specific image and
811
# commands being used
912
readonly image="${BUILDKITE_PLUGIN_VAULT_LOGIN_IMAGE:-${default_image}}:${BUILDKITE_PLUGIN_VAULT_LOGIN_TAG:-${default_tag}}"
1013

14+
maybe_pull_image() {
15+
# We match against the value true / on / 1, rather than simply
16+
# checking whether it is set or not, to make it possible to set a
17+
# value globally via the environment, but still allow for
18+
# job-specific overrides.
19+
if [[ "${BUILDKITE_PLUGIN_VAULT_LOGIN_ALWAYS_PULL:-false}" =~ ^(true|on|1)$ ]]; then
20+
echo ":docker: Explicitly pulling '${image}' image"
21+
log_and_run docker pull "${image}"
22+
fi
23+
}
24+
1125
# Wrap up the invocation of a Vault container image to alleviate the
1226
# need to have a Vault binary installed on the Buildkite agent machine
1327
# already. Scripts can just source this file and then call `vault`

lib/vault_test.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,25 @@ test_vault_stdout_contains_no_ANSI_codes() {
3636
"${output}" \
3737
"${expanded_output}"
3838
}
39+
40+
test_explicit_pull() {
41+
output="$(BUILDKITE_PLUGIN_VAULT_LOGIN_ALWAYS_PULL=1 maybe_pull_image)"
42+
assertContains "${output}" "latest: Pulling from hashicorp/vault"
43+
}
44+
45+
test_no_explicit_pull() {
46+
output="$(
47+
unset BUILDKITE_PLUGIN_VAULT_LOGIN_ALWAYS_PULL
48+
maybe_pull_image
49+
)"
50+
assertEquals "" "${output}"
51+
}
52+
53+
test_synonyms_for_always_pull_activation() {
54+
for value in "true" "on" "1"; do
55+
output="$(BUILDKITE_PLUGIN_VAULT_LOGIN_ALWAYS_PULL=${value} maybe_pull_image)"
56+
assertContains "Expected '${value}' to enable 'always-pull' option" \
57+
"${output}" \
58+
"latest: Pulling from hashicorp/vault"
59+
done
60+
}

plugin.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
name: Vault Login
33
description: Log into a Hashicorp Vault server
44
author: https://github.com/grapl-security
5-
requirements: ["docker"]
5+
requirements:
6+
- "docker"
67
configuration:
78
properties:
89
auth_role:
@@ -19,6 +20,15 @@ configuration:
1920
description: |
2021
The `vault` image tag to use; defaults to `latest`.
2122
type: string
23+
always-pull:
24+
description: |
25+
Explicitly pull the image before running. Useful if using the
26+
`latest` tag. Defaults to `false`.
27+
28+
Note that "true", "on", and "1" are all acceptable values to
29+
enable this option. Any other value is considered synonymous
30+
with `false`.
31+
type: boolean
2232
address:
2333
description: |
2434
The address of the Vault server to interact with. Should

tests/environment.bats

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ setup() {
2020
teardown() {
2121
unset BUILDKITE_AGENT_META_DATA_QUEUE
2222
unset BUILDKITE_PLUGIN_VAULT_LOGIN_ADDRESS
23+
unset BUILDKITE_PLUGIN_VAULT_LOGIN_ALWAYS_PULL
2324
unset BUILDKITE_PLUGIN_VAULT_LOGIN_AUTH_ROLE
2425
unset BUILDKITE_PLUGIN_VAULT_LOGIN_IMAGE
2526
unset BUILDKITE_PLUGIN_VAULT_LOGIN_NAMESPACE
@@ -245,3 +246,16 @@ teardown() {
245246

246247
assert_output --partial "Must provide a positive value for attempt_wait_seconds!"
247248
}
249+
250+
@test "always-pull will pull an image before running" {
251+
export BUILDKITE_PLUGIN_VAULT_LOGIN_ALWAYS_PULL=1
252+
253+
stub docker \
254+
"pull ${DEFAULT_IMAGE}:${DEFAULT_TAG} : echo 'pulling image'" \
255+
"run --init --rm --env=SKIP_SETCAP=true --env=VAULT_ADDR=${VAULT_ADDR} --env=VAULT_NAMESPACE=${VAULT_NAMESPACE} -- ${DEFAULT_IMAGE}:${DEFAULT_TAG} login -method=aws -token-only role=default : echo 'THIS_IS_YOUR_VAULT_TOKEN'"
256+
257+
run "${PWD}/hooks/environment"
258+
assert_success
259+
260+
unstub docker
261+
}

0 commit comments

Comments
 (0)