Skip to content

Commit cff439e

Browse files
authored
Merge pull request #468 from valkum/gcp-token
feat: add support for GCP token propagation
2 parents ddec64a + f12a262 commit cff439e

File tree

5 files changed

+80
-26
lines changed

5 files changed

+80
-26
lines changed

README.md

+6
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

+17
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

docs/examples.md

+27-26
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ The following pipeline will run `test.sh` inside a `app` service container using
1010
steps:
1111
- command: test.sh
1212
plugins:
13-
- docker-compose#v5.5.0:
13+
- docker-compose#v5.7.0:
1414
run: app
1515
```
1616
@@ -19,7 +19,7 @@ steps:
1919
```yml
2020
steps:
2121
- plugins:
22-
- docker-compose#v5.5.0:
22+
- docker-compose#v5.7.0:
2323
run: app
2424
command: ["custom", "command", "values"]
2525
```
@@ -30,7 +30,7 @@ The plugin will honor the value of the `COMPOSE_FILE` environment variable if on
3030
steps:
3131
- command: test.sh
3232
plugins:
33-
- docker-compose#v5.5.0:
33+
- docker-compose#v5.7.0:
3434
run: app
3535
config: docker-compose.tests.yml
3636
env:
@@ -46,15 +46,15 @@ steps:
4646
- plugins:
4747
- docker-login#v2.0.1:
4848
username: xyz
49-
- docker-compose#v5.5.0:
49+
- docker-compose#v5.7.0:
5050
build: app
5151
push: app:index.docker.io/myorg/myrepo:tag
5252
- wait
5353
- command: test.sh
5454
plugins:
5555
- docker-login#v2.0.1:
5656
username: xyz
57-
- docker-compose#v5.5.0:
57+
- docker-compose#v5.7.0:
5858
run: app
5959
```
6060

@@ -71,7 +71,7 @@ steps:
7171
- command: generate-dist.sh
7272
artifact_paths: "dist/*"
7373
plugins:
74-
- docker-compose#v5.5.0:
74+
- docker-compose#v5.7.0:
7575
run: app
7676
volumes:
7777
- "./dist:/folder/dist"
@@ -95,7 +95,7 @@ this plugin offers a `environment` block of its own:
9595
steps:
9696
- command: generate-dist.sh
9797
plugins:
98-
- docker-compose#v5.5.0:
98+
- docker-compose#v5.7.0:
9999
run: app
100100
env:
101101
- BUILDKITE_BUILD_NUMBER
@@ -113,7 +113,7 @@ Alternatively, you can have the plugin add all environment variables defined for
113113
steps:
114114
- command: use-vars.sh
115115
plugins:
116-
- docker-compose#v5.5.0:
116+
- docker-compose#v5.7.0:
117117
run: app
118118
propagate-environment: true
119119
```
@@ -129,7 +129,7 @@ steps:
129129
env:
130130
COMPOSE_PROFILES: "frontend,debug"
131131
plugins:
132-
- docker-compose#v5.5.0:
132+
- docker-compose#v5.7.0:
133133
run: app
134134
```
135135

@@ -138,6 +138,7 @@ It is important to understand that, as documented in the official documentation,
138138
### Container Labels
139139

140140
When running a command, the plugin will automatically add the following Docker labels to the container specified in the `run` option:
141+
141142
- `com.buildkite.pipeline_name=${BUILDKITE_PIPELINE_NAME}`
142143
- `com.buildkite.pipeline_slug=${BUILDKITE_PIPELINE_SLUG}`
143144
- `com.buildkite.build_number=${BUILDKITE_BUILD_NUMBER}`
@@ -165,7 +166,7 @@ Alternatively, if you want to set build arguments when pre-building an image, th
165166
steps:
166167
- command: generate-dist.sh
167168
plugins:
168-
- docker-compose#v5.5.0:
169+
- docker-compose#v5.7.0:
169170
build: app
170171
args:
171172
- MY_CUSTOM_ARG=panda
@@ -182,7 +183,7 @@ If you have multiple steps that use the same service/image (such as steps that r
182183
steps:
183184
- label: ":docker: Build"
184185
plugins:
185-
- docker-compose#v5.5.0:
186+
- docker-compose#v5.7.0:
186187
build: app
187188
push: app
188189
@@ -192,7 +193,7 @@ steps:
192193
command: test.sh
193194
parallelism: 25
194195
plugins:
195-
- docker-compose#v5.5.0:
196+
- docker-compose#v5.7.0:
196197
run: app
197198
require-prebuild: true
198199
```
@@ -211,7 +212,7 @@ steps:
211212
agents:
212213
queue: docker-builder
213214
plugins:
214-
- docker-compose#v5.5.0:
215+
- docker-compose#v5.7.0:
215216
build:
216217
- app
217218
- tests
@@ -225,7 +226,7 @@ steps:
225226
command: test.sh
226227
parallelism: 25
227228
plugins:
228-
- docker-compose#v5.5.0:
229+
- docker-compose#v5.7.0:
229230
run: tests
230231
```
231232

@@ -237,7 +238,7 @@ If you want to push your Docker images ready for deployment, you can use the `pu
237238
steps:
238239
- label: ":docker: Push"
239240
plugins:
240-
- docker-compose#v5.5.0:
241+
- docker-compose#v5.7.0:
241242
push: app
242243
```
243244

@@ -247,7 +248,7 @@ To push multiple images, you can use a list:
247248
steps:
248249
- label: ":docker: Push"
249250
plugins:
250-
- docker-compose#v5.5.0:
251+
- docker-compose#v5.7.0:
251252
push:
252253
- first-service
253254
- second-service
@@ -259,7 +260,7 @@ If you want to push to a specific location (that's not defined as the `image` in
259260
steps:
260261
- label: ":docker: Push"
261262
plugins:
262-
- docker-compose#v5.5.0:
263+
- docker-compose#v5.7.0:
263264
push:
264265
- app:index.docker.io/myorg/myrepo/myapp
265266
- app:index.docker.io/myorg/myrepo/myapp:latest
@@ -273,7 +274,7 @@ A newly spawned agent won't contain any of the docker caches for the first run w
273274
steps:
274275
- label: ":docker: Build an image"
275276
plugins:
276-
- docker-compose#v5.5.0:
277+
- docker-compose#v5.7.0:
277278
build: app
278279
push: app:index.docker.io/myorg/myrepo:my-branch
279280
cache-from:
@@ -284,7 +285,7 @@ steps:
284285
285286
- label: ":docker: Push to final repository"
286287
plugins:
287-
- docker-compose#v5.5.0:
288+
- docker-compose#v5.7.0:
288289
push:
289290
- app:myregistry:port/myrepo/myapp:latest
290291
```
@@ -297,7 +298,7 @@ The values you add in the `cache-from` will be mapped to the corresponding servi
297298
steps:
298299
- label: ":docker: Build an image"
299300
plugins:
300-
- docker-compose#v5.5.0:
301+
- docker-compose#v5.7.0:
301302
build: app
302303
push: app:index.docker.io/myorg/myrepo:my-branch
303304
cache-from:
@@ -308,7 +309,7 @@ steps:
308309
309310
- label: ":docker: Push to final repository"
310311
plugins:
311-
- docker-compose#v5.5.0:
312+
- docker-compose#v5.7.0:
312313
push:
313314
- app:myregistry:port/myrepo/myapp:latest
314315
```
@@ -325,7 +326,7 @@ The `docker` driver can handle most situations but for advance features with the
325326
steps:
326327
- label: ":docker: Build an image"
327328
plugins:
328-
- docker-compose#v5.5.0:
329+
- docker-compose#v5.7.0:
329330
build: app
330331
push: app:index.docker.io/myorg/myrepo:my-branch
331332
cache-from:
@@ -347,7 +348,7 @@ By default, Builder Instances specified by `name` or that are created with `crea
347348
steps:
348349
- label: ":docker: Build an image"
349350
plugins:
350-
- docker-compose#v5.5.0:
351+
- docker-compose#v5.7.0:
351352
build: app
352353
push: app:index.docker.io/myorg/myrepo:my-branch
353354
cache-from:
@@ -366,7 +367,7 @@ By default, Builder Instances specified by `name` or that are created with `crea
366367
steps:
367368
- label: ":docker: Build an image"
368369
plugins:
369-
- docker-compose#v5.5.0:
370+
- docker-compose#v5.7.0:
370371
build: app
371372
push: app:index.docker.io/myorg/myrepo:my-branch
372373
cache-from:
@@ -392,7 +393,7 @@ A newly spawned agent won't contain any of the docker caches for the first run w
392393
steps:
393394
- label: ":docker: Build an image and push cache"
394395
plugins:
395-
- docker-compose#v5.5.0:
396+
- docker-compose#v5.7.0:
396397
build: app
397398
push: app:${DOCKER_REGISTRY}/${IMAGE_REPO}:cache
398399
cache-from:
@@ -409,7 +410,7 @@ steps:
409410
410411
- label: ":docker: Build an image using remote cache"
411412
plugins:
412-
- docker-compose#v5.5.0:
413+
- docker-compose#v5.7.0:
413414
build: app
414415
cache-from:
415416
- "app:type=registry,ref=${DOCKER_REGISTRY}/${IMAGE_REPO}:cache"

plugin.yml

+4
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ configuration:
115115
progress:
116116
type: string
117117
enum: [ "auto", "tty", "plain", "json", "quiet" ]
118+
propagate-aws-auth-tokens:
119+
type: boolean
120+
propagate-gcp-auth-tokens:
121+
type: boolean
118122
propagate-environment:
119123
type: boolean
120124
propagate-uid-gid:

tests/run.bats

+26
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)