Skip to content

bk: use OIDC to tear-down the cloud resources #2567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 5, 2025
Merged

Conversation

v1v
Copy link
Member

@v1v v1v commented May 5, 2025

What

  • Use OIDC to access the AWS cloud resources and run the tear-down.
  • This will run in parallel while we migrate.
  • cloud-reaper does not support OIDC, hence creating a service account that will be deleted afterwards

Why

Avoid using service accounts and use the Keyless/OIDC approach.

Test

See https://buildkite.com/elastic/elastic-package-cloud-cleanup/builds/459

Further details

Uses https://github.com/elastic/oblt-aws-auth-buildkite-plugin

Requires #2568, so i can test this PR in isolation before merging it.

This will run in parallel while we do the migration

Avoid using service accounts and use the Keyless/OIDC approach

Uses https://github.com/elastic/oblt-aws-auth-buildkite-plugin
v1v added 3 commits May 5, 2025 15:03
this should help with the lack of support for OIDC in cloud-reaper
@v1v v1v requested review from mrodm and a team May 5, 2025 13:23
@v1v v1v marked this pull request as ready for review May 5, 2025 13:23
@v1v v1v requested a review from a team as a code owner May 5, 2025 13:23
if [[ "$BUILDKITE_PIPELINE_SLUG" == "elastic-package-cloud-cleanup" && "$BUILDKITE_STEP_KEY" == "cloud-cleanup" ]]; then
AWS_SERVICE_ACCOUNT_SECRET_PATH=kv/ci-shared/platform-ingest/aws_ingest_ci
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's define the variable where it's used

@@ -0,0 +1,229 @@
#!/usr/bin/env bash
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied it from .buildkite/scripts/cloud-cleanup.sh and added the support for OIDC.

@@ -5,8 +5,8 @@ accounts:
- name: "${ACCOUNT_PROJECT}"
driver: "aws"
options:
key: '${ACCOUNT_KEY}'
secret: '${ACCOUNT_SECRET}'
key: '${AWS_ACCESS_KEY_ID}'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To help with using the existing env variables created by https://github.com/elastic/oblt-aws-auth-buildkite-plugin

Comment on lines 55 to 86
# As long as cloud reaper does not support OIDC authentication.
create_aws_ephemeral_user() {
# Generate a unique name for the ephemeral IAM user.
EPHEMERAL_USER="ephemeral-admin-$(date +%s)"
echo "Creating IAM user: ${EPHEMERAL_USER}"
aws iam create-user --user-name "${EPHEMERAL_USER}" \
--tags Key=ephemeral,Value=true Key=division,Value=engineering Key=org,Value=obs Key=environment,Value=ci Key=repo,Value=elastic-package Key=created_at,Value="$(date -Is)"

echo "Attaching AdministratorAccess policy to ${EPHEMERAL_USER}..."
aws iam attach-user-policy --user-name "${EPHEMERAL_USER}" --policy-arn arn:aws:iam::aws:policy/AdministratorAccess

echo "Creating access keys for ${EPHEMERAL_USER}..."
creds_json=$(aws iam create-access-key --user-name "${EPHEMERAL_USER}")
AWS_ACCESS_KEY_ID_EPHEMERAL=$(echo "$creds_json" | jq -r '.AccessKey.AccessKeyId')
AWS_SECRET_ACCESS_KEY_EPHEMERAL=$(echo "$creds_json" | jq -r '.AccessKey.SecretAccessKey')
export EPHEMERAL_USER AWS_ACCESS_KEY_ID_EPHEMERAL AWS_SECRET_ACCESS_KEY_EPHEMERAL
}

# Define cleanup function to delete the ephemeral IAM user regardless of script outcome.
cleanup_ephemeral_user() {
echo "Cleaning up ephemeral IAM user: ${EPHEMERAL_USER}"
aws iam detach-user-policy --user-name "${EPHEMERAL_USER}" --policy-arn arn:aws:iam::aws:policy/AdministratorAccess
key_id=$(echo "$creds_json" | jq -r '.AccessKey.AccessKeyId')
aws iam delete-access-key --user-name "${EPHEMERAL_USER}" --access-key-id "${key_id}"
aws iam delete-user --user-name "${EPHEMERAL_USER}"
echo "Ephemeral IAM user ${EPHEMERAL_USER} deleted."
}
trap cleanup_ephemeral_user EXIT

cloud_reaper_aws() {
echo "--- Configuring ephemeral user"
create_aws_ephemeral_user
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what I added to help with the OIDC when running cloud-reaper

Comment on lines +112 to +114
echo "--- Installing awscli"
with_aws_cli

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Run earlier, to help with using the aws cli to create the ephemeral service account

Comment on lines +68 to +70
AWS_ACCESS_KEY_ID_EPHEMERAL=$(echo "$creds_json" | jq -r '.AccessKey.AccessKeyId')
AWS_SECRET_ACCESS_KEY_EPHEMERAL=$(echo "$creds_json" | jq -r '.AccessKey.SecretAccessKey')
export EPHEMERAL_USER AWS_ACCESS_KEY_ID_EPHEMERAL AWS_SECRET_ACCESS_KEY_EPHEMERAL
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should AWS_ACCESS_KEY_ID_EPHEMERAL and AWS_SECRET_ACCESS_KEY_EPHEMERAL be renamed to follow the patterns so their contents are redacted ?

https://buildkite.com/docs/pipelines/configure/managing-log-output#redacted-environment-variables

Maybe something like AWS_ACCESS_KEY_ID_EPHEMERAL_SECRET and AWS_SECRET_ACCESS_KEY_EPHEMERAL_SECRET ? Or is it not needed in this scenario ?

Copy link
Member Author

@v1v v1v May 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not matter at all, those env variables are not masked, unless they are set in the pre-command.

When creating env variable on the fly, there is no way to redact values

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, got it!

Copy link
Contributor

@mrodm mrodm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@v1v
Copy link
Member Author

v1v commented May 5, 2025

I'm running this build to validate if the recent changes with dry-run: false work as expected

@v1v v1v enabled auto-merge (squash) May 5, 2025 16:09
@v1v v1v merged commit bddbc78 into main May 5, 2025
3 checks passed
@v1v v1v deleted the feature/cloud-reaper-oidc branch May 5, 2025 16:33
@elasticmachine
Copy link
Collaborator

💚 Build Succeeded

History

cc @v1v

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants