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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

pubsys: Do not allow private AMIs to be published to SSM parameters in the public namespace #2680

Merged
merged 3 commits into from
Dec 21, 2022

Conversation

cbgbt
Copy link
Contributor

@cbgbt cbgbt commented Dec 21, 2022

Description of changes:
This change by default will disallow publishing SSM parameters to the public /aws/ SSM namespace if they refer to private AMIs.

Testing done:

  • Here's a sample run where I tried to set some of my own private AMIs to the /aws/service/doggierocket namespace. ( 馃惗 )
03:58:05 [INFO] Using AMI data from path: ../../build/images/aarch64-aws-k8s-1.21/bottlerocket-aws-k8s-1.21-aarch64-1.10.0-867fbbda-amis.json
03:58:05 [INFO] Parsing SSM parameter templates from policies/ssm/defaults.toml
03:58:05 [INFO] Ensuring that only public images are published to public parameters.
03:58:05 [ERROR] Attempted to set parameter '/aws/service/doggierocket/aws-k8s-1.21/arm64/1.10.0/image_version' in us-west-2 to '1.10.0', based on AMI ami-0bc2a2c72b2ce0a28. That AMI is not marked public!
03:58:05 [ERROR] Attempted to set parameter '/aws/service/doggierocket/aws-k8s-1.21/arm64/1.10.0/image_id' in us-west-2 to 'ami-0bc2a2c72b2ce0a28', based on AMI ami-0bc2a2c72b2ce0a28. That AMI is not marked public!
Failed to update SSM: Cowardly refusing to publish private image to public namespace without ALLOW_PRIVATE_IMAGES

[ec2-user@ip-10-0-0-27 pubsys]$ echo $?
1
  • Using --allow-private-images passes this check (but then fails because we are trying to update parameters that don't exist)

Originally I sought to use Traits and fakes to provide additional automated testing here, but it was pretty difficult to do it in a way that didn't require more significant refactoring.

Terms of contribution:

By submitting this pull request, I agree that this contribution is dual-licensed under the terms of both the Apache License, version 2.0, and the MIT license.

Copy link
Contributor

@stmcginnis stmcginnis left a comment

Choose a reason for hiding this comment

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

Looks good to me once the build failure is addressed. Great to have this!

@@ -156,8 +210,56 @@ pub(crate) async fn run(args: &Args, ssm_args: &SsmArgs) -> Result<()> {
Ok(())
}

// Determines the number of AMIs that can be checked for public permissions in parallel.
const CONCURRENT_AMI_CHECKS: usize = 16;
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it be better to set this at runtime based on the running host's number of cores?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since this is network-bound, I picked an arbitrarily "low" number of concurrent checks. I've found that you often hit throttle limits trying to hit AWS using all of your cores if your host is too beefy. This can be rectified with more careful throttling avoidance, but I opted for a simpler route here.

I should probably clarify why the number was chosen in a comment.

Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting, I was actually worried this number was too high. I was thinking something like Min(cpu_cores, 16), but even wondering if that should be more like 8 than 16.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

8 is probably a safer bet!

@cbgbt cbgbt force-pushed the ssm-cross-i-dot-t branch 2 times, most recently from d918b80 to 30b7f0d Compare December 21, 2022 16:40
@cbgbt
Copy link
Contributor Author

cbgbt commented Dec 21, 2022

  • Fix clippy warning on large size variation in Enum variants
  • Lower concurrent AMI query to a safer value to avoid throttling

@cbgbt cbgbt marked this pull request as ready for review December 21, 2022 16:41
Copy link
Contributor

@yeazelm yeazelm left a comment

Choose a reason for hiding this comment

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

I'm still learning Rust for concurrency things, so I didn't review those specifics, but otherwise it looks good to me.

Copy link
Contributor

@etungsten etungsten left a comment

Choose a reason for hiding this comment

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

Nice! Just have a few non-blocking questions and small suggestions.

tools/pubsys/src/aws/ami/public.rs Outdated Show resolved Hide resolved
tools/pubsys/src/aws/ssm/mod.rs Outdated Show resolved Hide resolved
tools/pubsys/src/aws/ssm/mod.rs Outdated Show resolved Hide resolved
tools/pubsys/src/aws/ssm/mod.rs Outdated Show resolved Hide resolved
tools/pubsys/src/aws/ssm/template.rs Show resolved Hide resolved
@cbgbt cbgbt force-pushed the ssm-cross-i-dot-t branch 3 times, most recently from 90912b1 to 9679767 Compare December 21, 2022 19:15
@cbgbt
Copy link
Contributor Author

cbgbt commented Dec 21, 2022

Addressed @etungsten's feedback

  • Cleaned up Option handling when calling EC2 DescribeImages
  • Added proper rate limiting to DescribeImages calls via governor crate
  • Pre-allocate memory for data structures with known sizes
  • linting fixes

etungsten
etungsten previously approved these changes Dec 21, 2022
@etungsten etungsten dismissed their stale review December 21, 2022 19:30

licensing issue with the governor crate

@cbgbt
Copy link
Contributor Author

cbgbt commented Dec 21, 2022

  • Re-added BSD-2-Clause to deny.toml. This license is allowed, it was just previously not used.

@cbgbt cbgbt merged commit 7eaaffb into bottlerocket-os:develop Dec 21, 2022
@cbgbt cbgbt deleted the ssm-cross-i-dot-t branch December 21, 2022 20:09
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.

None yet

4 participants