Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
create ephemeral user
this should help with the lack of support for OIDC in cloud-reaper
  • Loading branch information
v1v committed May 5, 2025
commit 8042e94bbab4ddadb00d9367fa10df521fc8a862
43 changes: 36 additions & 7 deletions .buildkite/scripts/cloud-cleanup-oidc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,43 @@ any_resources_to_delete() {
return 0
}

# 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=creation-date,Value="$(date -u +"%Y-%m-%dT%H:%M:%SZ")"

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
Comment on lines +68 to +70
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!

}

# 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

echo "Validating configuration"
docker run --rm -v "$(pwd)/.buildkite/configs/cleanup.aws.yml":/etc/cloud-reaper/config.yml \
-e AWS_ACCESS_KEY_ID \
-e AWS_SECRET_ACCESS_KEY \
-e AWS_SESSION_TOKEN \
-e AWS_ACCESS_KEY_ID="$AWS_ACCESS_KEY_ID_EPHEMERAL" \
-e AWS_SECRET_ACCESS_KEY="$AWS_SECRET_ACCESS_KEY_EPHEMERAL" \
-e ACCOUNT_PROJECT="observability-ci" \
-e CREATION_DATE="${DELETE_RESOURCES_BEFORE_DATE}" \
"${CLOUD_REAPER_IMAGE}" \
Expand All @@ -68,14 +99,12 @@ cloud_reaper_aws() {

echo "Scanning resources"
docker run --rm -v "$(pwd)/.buildkite/configs/cleanup.aws.yml":/etc/cloud-reaper/config.yml \
-e AWS_ACCESS_KEY_ID \
-e AWS_SECRET_ACCESS_KEY \
-e AWS_SESSION_TOKEN \
-e AWS_ACCESS_KEY_ID="$AWS_ACCESS_KEY_ID_EPHEMERAL" \
-e AWS_SECRET_ACCESS_KEY="$AWS_SECRET_ACCESS_KEY_EPHEMERAL" \
-e ACCOUNT_PROJECT="observability-ci" \
-e CREATION_DATE="${DELETE_RESOURCES_BEFORE_DATE}" \
"${CLOUD_REAPER_IMAGE}" \
cloud-reaper \
--debug \
--config /etc/cloud-reaper/config.yml \
${COMMAND} | tee "${AWS_RESOURCES_FILE}"
}
Expand Down