Skip to content

Commit d4e2ee0

Browse files
authored
Merge pull request #269 from valkum/gcp_support
feat: add GCP workload identity federation compatibility
2 parents 51fce12 + 17b6fe3 commit d4e2ee0

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

README.md

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

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

280+
### `propagate-gcp-auth-tokens` (optional, boolean)
281+
282+
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.
283+
284+
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.
285+
280286
### `propagate-uid-gid` (optional, boolean)
281287

282288
Whether to match the user ID and group ID for the container user to the user ID and group ID for the host user. It is similar to specifying `user: 1000:1000`, except it avoids hardcoding a particular user/group ID.

commands/run.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,22 @@ if [[ "${BUILDKITE_PLUGIN_DOCKER_PROPAGATE_AWS_AUTH_TOKENS:-false}" =~ ^(true|on
316316
fi
317317
fi
318318

319+
# Propagate gcp auth environment variables into the container e.g. from workload identity federation plugins
320+
if [[ "${BUILDKITE_PLUGIN_DOCKER_PROPAGATE_GCP_AUTH_TOKENS:-false}" =~ ^(true|on|1)$ ]] ; then
321+
if [[ -n "${GOOGLE_APPLICATION_CREDENTIALS:-}" ]] ; then
322+
args+=( --env "GOOGLE_APPLICATION_CREDENTIALS" )
323+
fi
324+
if [[ -n "${CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE:-}" ]] ; then
325+
args+=( --env "CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE" )
326+
fi
327+
if [[ -n "${BUILDKITE_OIDC_TMPDIR:-}" ]] ; then
328+
args+=( --env "BUILDKITE_OIDC_TMPDIR" )
329+
# Add the OIDC temp dir as a volume
330+
args+=( --volume "${BUILDKITE_OIDC_TMPDIR}:${BUILDKITE_OIDC_TMPDIR}" )
331+
fi
332+
333+
fi
334+
319335
if [[ "${BUILDKITE_PLUGIN_DOCKER_EXPAND_IMAGE_VARS:-false}" =~ ^(true|on|1)$ ]] ; then
320336
image=$(eval echo "${BUILDKITE_PLUGIN_DOCKER_IMAGE}")
321337
else

plugin.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ configuration:
9393
type: boolean
9494
propagate-aws-auth-tokens:
9595
type: boolean
96+
propagate-gcp-auth-tokens:
97+
type: boolean
9698
propagate-uid-gid:
9799
type: boolean
98100
privileged:

tests/command.bats

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,25 @@ EOF
968968
unstub docker
969969
}
970970

971+
@test "Runs BUILDKITE_COMMAND with propagate gcp auth tokens" {
972+
export BUILDKITE_COMMAND="echo hello world"
973+
export BUILDKITE_PLUGIN_DOCKER_PROPAGATE_GCP_AUTH_TOKENS=true
974+
975+
export BUILDKITE_OIDC_TMPDIR="/tmp/.tmp.Xdasd23"
976+
export GOOGLE_APPLICATION_CREDENTIALS="${BUILDKITE_OIDC_TMPDIR}/credentials.json"
977+
export CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE="${GOOGLE_APPLICATION_CREDENTIALS}"
978+
979+
stub docker \
980+
"run -t -i --rm --init --volume $PWD:/workdir --workdir /workdir --env GOOGLE_APPLICATION_CREDENTIALS --env CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE --env BUILDKITE_OIDC_TMPDIR --volume \"/tmp/.tmp.Xdasd23:/tmp/.tmp.Xdasd23\" --label com.buildkite.job-id=1-2-3-4 image:tag /bin/sh -e -c 'echo hello world' : echo ran command in docker"
981+
982+
run "$PWD"/hooks/command
983+
984+
assert_success
985+
assert_output --partial "ran command in docker"
986+
987+
unstub docker
988+
}
989+
971990
@test "Runs BUILDKITE_COMMAND with memory options" {
972991
export BUILDKITE_PLUGIN_DOCKER_MEMORY=2g
973992
export BUILDKITE_COMMAND="echo hello world"

0 commit comments

Comments
 (0)