Skip to content

Commit

Permalink
Merge pull request #269 from valkum/gcp_support
Browse files Browse the repository at this point in the history
feat: add GCP workload identity federation compatibility
  • Loading branch information
pzeballos committed Apr 12, 2024
2 parents 51fce12 + 17b6fe3 commit d4e2ee0
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,12 @@ Will propagate `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `AWS_SESSION_TOKEN`

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

### `propagate-gcp-auth-tokens` (optional, boolean)

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.

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.

### `propagate-uid-gid` (optional, boolean)

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.
Expand Down
16 changes: 16 additions & 0 deletions commands/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,22 @@ if [[ "${BUILDKITE_PLUGIN_DOCKER_PROPAGATE_AWS_AUTH_TOKENS:-false}" =~ ^(true|on
fi
fi

# Propagate gcp auth environment variables into the container e.g. from workload identity federation plugins
if [[ "${BUILDKITE_PLUGIN_DOCKER_PROPAGATE_GCP_AUTH_TOKENS:-false}" =~ ^(true|on|1)$ ]] ; then
if [[ -n "${GOOGLE_APPLICATION_CREDENTIALS:-}" ]] ; then
args+=( --env "GOOGLE_APPLICATION_CREDENTIALS" )
fi
if [[ -n "${CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE:-}" ]] ; then
args+=( --env "CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE" )
fi
if [[ -n "${BUILDKITE_OIDC_TMPDIR:-}" ]] ; then
args+=( --env "BUILDKITE_OIDC_TMPDIR" )
# Add the OIDC temp dir as a volume
args+=( --volume "${BUILDKITE_OIDC_TMPDIR}:${BUILDKITE_OIDC_TMPDIR}" )
fi

fi

if [[ "${BUILDKITE_PLUGIN_DOCKER_EXPAND_IMAGE_VARS:-false}" =~ ^(true|on|1)$ ]] ; then
image=$(eval echo "${BUILDKITE_PLUGIN_DOCKER_IMAGE}")
else
Expand Down
2 changes: 2 additions & 0 deletions plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ configuration:
type: boolean
propagate-aws-auth-tokens:
type: boolean
propagate-gcp-auth-tokens:
type: boolean
propagate-uid-gid:
type: boolean
privileged:
Expand Down
19 changes: 19 additions & 0 deletions tests/command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,25 @@ EOF
unstub docker
}

@test "Runs BUILDKITE_COMMAND with propagate gcp auth tokens" {
export BUILDKITE_COMMAND="echo hello world"
export BUILDKITE_PLUGIN_DOCKER_PROPAGATE_GCP_AUTH_TOKENS=true

export BUILDKITE_OIDC_TMPDIR="/tmp/.tmp.Xdasd23"
export GOOGLE_APPLICATION_CREDENTIALS="${BUILDKITE_OIDC_TMPDIR}/credentials.json"
export CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE="${GOOGLE_APPLICATION_CREDENTIALS}"

stub docker \
"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"

run "$PWD"/hooks/command

assert_success
assert_output --partial "ran command in docker"

unstub docker
}

@test "Runs BUILDKITE_COMMAND with memory options" {
export BUILDKITE_PLUGIN_DOCKER_MEMORY=2g
export BUILDKITE_COMMAND="echo hello world"
Expand Down

0 comments on commit d4e2ee0

Please sign in to comment.