Skip to content

Commit

Permalink
Adds support for passing external-id to aws sts.
Browse files Browse the repository at this point in the history
  • Loading branch information
peterschutt committed Dec 27, 2020
1 parent 28c5cec commit 3177eca
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .buildkite/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ steps:
- label: ":shell: Lint"
plugins:
plugin-linter#v2.0.0:
id: cultureamp/aws-assume-role
id: topsport-com-au/aws-assume-role

- label: ":shell: Tests"
plugins:
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ The duration (in seconds) to assume the role for. Defaults to 3600 (1 hour).

Exports `AWS_REGION` and `AWS_DEFAULT_REGION` with the value you set. If not set the values of AWS_REGION and AWS_DEFAULT_REGION will not be changed.

### `external_id` (optional)

Unique identifier possibly required for assuming a role in another account.

Development
-----------

Expand Down
21 changes: 16 additions & 5 deletions hooks/pre-command
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ main() {
local build="${BUILDKITE_BUILD_NUMBER:-}"
local duration="${BUILDKITE_PLUGIN_AWS_ASSUME_ROLE_DURATION:-3600}"
local region="${BUILDKITE_PLUGIN_AWS_ASSUME_ROLE_REGION:-""}"
local external_id="${BUILDKITE_PLUGIN_AWS_ASSUME_ROLE_EXTERNAL_ID:-""}"

if [[ -n $role && -n $build ]]; then
echo "~~~ Assuming IAM role $role ..."
local exports; exports="$(assume_role_credentials "$role" "$build" "$duration" | credentials_json_to_shell_exports)"
local exports; exports="$(assume_role_credentials "$role" "$build" "$duration" "$external_id" | credentials_json_to_shell_exports)"
eval "$exports"

echo "Exported session credentials:"
Expand Down Expand Up @@ -43,11 +44,21 @@ assume_role_credentials() {
local role="$1"
local build="$2"
local duration="$3"
aws sts assume-role \
--role-arn "$role" \
--role-session-name "aws-assume-role-buildkite-plugin-${build}" \
--duration-seconds "$duration" \
local external_id="$4"

params=(
--role-arn "$role"
--role-session-name "aws-assume-role-buildkite-plugin-${build}"
--duration-seconds "$duration"
--query Credentials
)

if [[ -n $external_id ]]; then
params+=(--external-id "${external_id}")
fi

aws sts assume-role ${params[@]}

}

# Convert credentials JSON to shell export statements using standard CLI tools
Expand Down
2 changes: 2 additions & 0 deletions plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ configuration:
type: string
region:
type: string
external_id:
type: string
required:
- role
additionalProperties: false
18 changes: 18 additions & 0 deletions tests/pre-command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ EOF
unstub aws
}

@test "calls aws sts with an external_id" {
export BUILDKITE_BUILD_NUMBER="42"
export BUILDKITE_PLUGIN_AWS_ASSUME_ROLE_ROLE="role123"
export BUILDKITE_PLUGIN_AWS_ASSUME_ROLE_EXTERNAL_ID="some-id"

stub aws "sts assume-role --role-arn role123 --role-session-name aws-assume-role-buildkite-plugin-42 --duration-seconds 3600 --query Credentials --external-id some-id : cat tests/sts.json"

run $PWD/hooks/pre-command
assert_output --partial "~~~ Assuming IAM role role123 ..."
assert_output --partial "Exported session credentials"
assert_output --partial "AWS_ACCESS_KEY_ID=baz"
assert_output --partial "AWS_SECRET_ACCESS_KEY=(3 chars)"
assert_output --partial "AWS_SESSION_TOKEN=(3 chars)"

assert_success
unstub aws
}

@test "passes in a custom region" {
export BUILDKITE_BUILD_NUMBER="42"
export BUILDKITE_PLUGIN_AWS_ASSUME_ROLE_ROLE="role123"
Expand Down

0 comments on commit 3177eca

Please sign in to comment.