Skip to content
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

Add Confused Deputy Prevention #1503

Merged
merged 5 commits into from
Feb 3, 2025
Merged

Add Confused Deputy Prevention #1503

merged 5 commits into from
Feb 3, 2025

Conversation

dricross
Copy link
Contributor

@dricross dricross commented Jan 15, 2025

Description of the issue

To support confused deputy prevention, the CloudWatch Agent needs to be able to pass confused deputy context keys in the headers of STS AssumeRole calls so that dependent service teams can allow their customers to use confused deputy context keys in their role policies.

For background on the confused deputy problem, see: https://docs.aws.amazon.com/IAM/latest/UserGuide/confused-deputy.html

Description of changes

Enable the CloudWatch Agent to resource confused deputy context keys from environment variables and include the key values in the STS AssumeRole request headers.

I noticed while testing my new integration test that the destroy step was always failing because terraform wasn't initialized. Example: https://github.com/aws/amazon-cloudwatch-agent/actions/runs/13013756921/job/36303755191. This wound up leaving a ton of resources behind that didn't get cleaned up automatically. Added a terraform init step to the terraform destroy step to fix this

License

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Tests

New assume_role integration test created in test repo. Example run: https://github.com/aws/amazon-cloudwatch-agent/actions/runs/13020191778. See assume_role test, e.g. https://github.com/aws/amazon-cloudwatch-agent/actions/runs/13065824342/job/36483181515. See PR for amazon-cloudwatch-agent-test for more details: aws/amazon-cloudwatch-agent-test#449

Requirements

Before commit the code, please do the following steps.

  1. Run make fmt and make fmt-sh
  2. Run make lint

@dricross dricross requested a review from a team as a code owner January 15, 2025 20:13
cfg/aws/credentials_test.go Outdated Show resolved Hide resolved
cfg/aws/credentials.go Outdated Show resolved Hide resolved
cfg/aws/credentials.go Outdated Show resolved Hide resolved
cfg/aws/credentials.go Outdated Show resolved Hide resolved
@dricross dricross force-pushed the dricross/confused-deputy branch 6 times, most recently from 818dbd6 to a45a647 Compare January 28, 2025 22:50
@dricross dricross force-pushed the dricross/confused-deputy branch 2 times, most recently from 82ece88 to 5e816a4 Compare January 29, 2025 19:28
sky333999
sky333999 previously approved these changes Jan 29, 2025
Paramadon
Paramadon previously approved these changes Jan 30, 2025
Copy link
Contributor

@Paramadon Paramadon left a comment

Choose a reason for hiding this comment

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

LGTM!

jefchien
jefchien previously approved these changes Jan 30, 2025
Copy link
Contributor

Choose a reason for hiding this comment

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

These are all formatting changes right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Everything in terraform apply is formatting. The changes in the Terraform destroy step are actually important. The step would fail with an error that effectively says "you need to run terraform init first". So whenever my integ test run would fail, it would leave behind a bunch of resources

Copy link
Contributor

Choose a reason for hiding this comment

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

We shouldn't need to call terraform init before the destroy. The working directory should already be initialized before we call apply.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like that's because it was already destroyed by the terraform apply.

Copy link
Contributor Author

@dricross dricross Feb 2, 2025

Choose a reason for hiding this comment

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

Hm... so maybe it's failing because the destroy step always cds into ${{inputs.test_dir}} whereas the apply step cds into ${{ matrix.arrays.terraform_dir }} if its defined, so ${{inputs.test_dir}} may not be init'd. And the real fix should be to cd into ${{ matrix.arrays.terraform_dir }} if it's defined and we shouldn't need the extra terraform init

Copy link
Contributor

@musa-asad musa-asad Feb 3, 2025

Choose a reason for hiding this comment

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

Can you bring the same if statement to the destroy?

if [ "${{ matrix.arrays.terraform_dir }}" != "" ]; then
  cd "${{ matrix.arrays.terraform_dir }}"
else
  cd ${{inputs.test_dir}}
fi

We need to make sure to destroy the correct environment.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I think that's right.

@dricross dricross dismissed stale reviews from jefchien, Paramadon, and sky333999 via 1605eb3 January 31, 2025 02:59
@dricross dricross force-pushed the dricross/confused-deputy branch 2 times, most recently from 866274c to 20257af Compare January 31, 2025 14:45
Paramadon
Paramadon previously approved these changes Feb 3, 2025
jefchien
jefchien previously approved these changes Feb 3, 2025
@dricross dricross dismissed stale reviews from jefchien and Paramadon via a03eed1 February 3, 2025 17:02
@sky333999 sky333999 merged commit b55abe4 into main Feb 3, 2025
7 checks passed
@sky333999 sky333999 deleted the dricross/confused-deputy branch February 3, 2025 23:05
musa-asad pushed a commit that referenced this pull request Feb 5, 2025
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.

5 participants