Skip to content

Commit 86f1a0a

Browse files
committed
feat: add support for GCP token propagation
1 parent ddec64a commit 86f1a0a

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ Will propagate `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `AWS_SESSION_TOKEN`
9393

9494
When the `AWS_WEB_IDENTITY_TOKEN_FILE` is specified, it will also mount it automatically for you and make it usable within the container.
9595

96+
#### `propagate-gcp-auth-tokens` (run only, boolean)
97+
98+
Whether or not to automatically propagate gcp auth credentials into the docker container. Avoiding the need to be specified with `environment`. This is useful if you are using a workload identity federation to impersonate a service account and you want to pass it to the docker container. This is compatible with the `gcp-workload-identity-federation` plugin.
99+
100+
Will propagate `GOOGLE_APPLICATION_CREDENTIALS`, `CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE` and `BUILDKITE_OIDC_TMPDIR` and also mount the dir specified by `BUILDKITE_OIDC_TMPDIR` into the container.
101+
96102
#### `command` (run only, array)
97103

98104
Sets the command for the Docker image, and defaults the `shell` option to `false`. Useful if the Docker image has an entrypoint, or doesn't contain a shell.

commands/run.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,23 @@ if [[ "$(plugin_read_config PROPAGATE_AWS_AUTH_TOKENS "false")" =~ ^(true|on|1)$
164164
fi
165165
fi
166166

167+
# Propagate gcp auth environment variables into the container e.g. from workload identity federation plugins
168+
if [[ "$(plugin_read_config PROPAGATE_GCP_AUTH_TOKENS "false")" =~ ^(true|on|1)$ ]] ; then
169+
if [[ -n "${GOOGLE_APPLICATION_CREDENTIALS:-}" ]] ; then
170+
run_params+=( --env "GOOGLE_APPLICATION_CREDENTIALS" )
171+
fi
172+
if [[ -n "${CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE:-}" ]] ; then
173+
run_params+=( --env "CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE" )
174+
fi
175+
if [[ -n "${BUILDKITE_OIDC_TMPDIR:-}" ]] ; then
176+
run_params+=( --env "BUILDKITE_OIDC_TMPDIR" )
177+
# Add the OIDC temp dir as a volume
178+
run_params+=( --volume "${BUILDKITE_OIDC_TMPDIR}:${BUILDKITE_OIDC_TMPDIR}" )
179+
fi
180+
fi
181+
182+
183+
167184
# If requested, propagate a set of env vars as listed in a given env var to the
168185
# container.
169186
if [[ -n "$(plugin_read_config ENV_PROPAGATION_LIST)" ]]; then

plugin.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ configuration:
115115
progress:
116116
type: string
117117
enum: [ "auto", "tty", "plain", "json", "quiet" ]
118+
propagate-gcp-auth-tokens:
119+
type: boolean
118120
propagate-environment:
119121
type: boolean
120122
propagate-uid-gid:

tests/run.bats

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,3 +1340,29 @@ cmd3"
13401340

13411341
unstub docker
13421342
}
1343+
1344+
@test "Run with propagate gcp auth tokens" {
1345+
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_RUN=myservice
1346+
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CHECK_LINKED_CONTAINERS=false
1347+
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CLEANUP=false
1348+
export BUILDKITE_COMMAND="echo hello world"
1349+
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_PROPAGATE_GCP_AUTH_TOKENS=true
1350+
1351+
export BUILDKITE_OIDC_TMPDIR="/tmp/.tmp.Xdasd23"
1352+
export GOOGLE_APPLICATION_CREDENTIALS="${BUILDKITE_OIDC_TMPDIR}/credentials.json"
1353+
export CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE="${GOOGLE_APPLICATION_CREDENTIALS}"
1354+
1355+
stub docker \
1356+
"compose -f docker-compose.yml -p buildkite1111 up -d --scale myservice=0 myservice : echo ran myservice dependencies" \
1357+
"compose -f docker-compose.yml -p buildkite1111 run --name buildkite1111_myservice_build_1 --env GOOGLE_APPLICATION_CREDENTIALS --env CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE --env BUILDKITE_OIDC_TMPDIR --volume \"/tmp/.tmp.Xdasd23:/tmp/.tmp.Xdasd23\" -T --rm myservice /bin/sh -e -c 'echo hello world' : echo ran myservice"
1358+
1359+
stub buildkite-agent \
1360+
"meta-data exists docker-compose-plugin-built-image-tag-myservice : exit 1"
1361+
1362+
run "$PWD"/hooks/command
1363+
1364+
assert_success
1365+
assert_output --partial "ran myservice"
1366+
unstub docker
1367+
unstub buildkite-agent
1368+
}

0 commit comments

Comments
 (0)