Skip to content

Commit 3987929

Browse files
authored
Merge pull request #426 from christophe-scalepad/master
feat: Add option to automatically mount AWS auth tokens when using docker compose run command
2 parents 2bf3948 + 7c8295f commit 3987929

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ Whether or not to automatically propagate all pipeline environment variables int
7878

7979
**Important**: only pipeline environment variables will be propagated (what you see in the BuildKite UI, those listed in `$BUILDKITE_ENV_FILE`). This does not include variables exported in preceeding `environment` hooks. If you wish for those to be propagated you will need to list them specifically or use `env-propagation-list`.
8080

81+
### `propagate-aws-auth-tokens` (run only, boolean)
82+
83+
Whether or not to automatically propagate aws authentication environment variables into the docker container. Avoiding the need to be specified with `environment`. This is useful for example if you are using an assume role plugin or you want to pass the role of an agent running in ECS or EKS to the docker container.
84+
85+
Will propagate `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `AWS_SESSION_TOKEN`, `AWS_REGION`, `AWS_DEFAULT_REGION`, `AWS_STS_REGIONAL_ENDPOINTS`, `AWS_WEB_IDENTITY_TOKEN_FILE`, `AWS_ROLE_ARN`, `AWS_CONTAINER_CREDENTIALS_FULL_URI`, `AWS_CONTAINER_CREDENTIALS_RELATIVE_URI`, and `AWS_CONTAINER_AUTHORIZATION_TOKEN`, only if they are set already.
86+
87+
When the `AWS_WEB_IDENTITY_TOKEN_FILE` is specified, it will also mount it automatically for you and make it usable within the container.
88+
8189
#### `command` (run only, array)
8290

8391
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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,49 @@ if [[ "$(plugin_read_config PROPAGATE_ENVIRONMENT "false")" =~ ^(true|on|1)$ ]]
112112
fi
113113
fi
114114

115+
# Propagate AWS credentials if requested
116+
if [[ "$(plugin_read_config PROPAGATE_AWS_AUTH_TOKENS "false")" =~ ^(true|on|1)$ ]] ; then
117+
if [[ -n "${AWS_ACCESS_KEY_ID:-}" ]] ; then
118+
run_params+=( --env "AWS_ACCESS_KEY_ID" )
119+
fi
120+
if [[ -n "${AWS_SECRET_ACCESS_KEY:-}" ]] ; then
121+
run_params+=( --env "AWS_SECRET_ACCESS_KEY" )
122+
fi
123+
if [[ -n "${AWS_SESSION_TOKEN:-}" ]] ; then
124+
run_params+=( --env "AWS_SESSION_TOKEN" )
125+
fi
126+
if [[ -n "${AWS_REGION:-}" ]] ; then
127+
run_params+=( --env "AWS_REGION" )
128+
fi
129+
if [[ -n "${AWS_DEFAULT_REGION:-}" ]] ; then
130+
run_params+=( --env "AWS_DEFAULT_REGION" )
131+
fi
132+
if [[ -n "${AWS_ROLE_ARN:-}" ]] ; then
133+
run_params+=( --env "AWS_ROLE_ARN" )
134+
fi
135+
if [[ -n "${AWS_STS_REGIONAL_ENDPOINTS:-}" ]] ; then
136+
run_params+=( --env "AWS_STS_REGIONAL_ENDPOINTS" )
137+
fi
138+
# Pass ECS variables when the agent is running in ECS
139+
# https://docs.aws.amazon.com/sdkref/latest/guide/feature-container-credentials.html
140+
if [[ -n "${AWS_CONTAINER_CREDENTIALS_FULL_URI:-}" ]] ; then
141+
run_params+=( --env "AWS_CONTAINER_CREDENTIALS_FULL_URI" )
142+
fi
143+
if [[ -n "${AWS_CONTAINER_CREDENTIALS_RELATIVE_URI:-}" ]] ; then
144+
run_params+=( --env "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI" )
145+
fi
146+
if [[ -n "${AWS_CONTAINER_AUTHORIZATION_TOKEN:-}" ]] ; then
147+
run_params+=( --env "AWS_CONTAINER_AUTHORIZATION_TOKEN" )
148+
fi
149+
# Pass EKS variables when the agent is running in EKS
150+
# https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts-minimum-sdk.html
151+
if [[ -n "${AWS_WEB_IDENTITY_TOKEN_FILE:-}" ]] ; then
152+
run_params+=( --env "AWS_WEB_IDENTITY_TOKEN_FILE" )
153+
# Add the token file as a volume
154+
run_params+=( --volume "${AWS_WEB_IDENTITY_TOKEN_FILE}:${AWS_WEB_IDENTITY_TOKEN_FILE}" )
155+
fi
156+
fi
157+
115158
# If requested, propagate a set of env vars as listed in a given env var to the
116159
# container.
117160
if [[ -n "$(plugin_read_config ENV_PROPAGATION_LIST)" ]]; then

0 commit comments

Comments
 (0)