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

feat(ec2): Amazon EC2 Instances Should Not Use Multiple ENIs #4935

Merged

Conversation

MarioRgzLpz
Copy link
Contributor

Context

Using multiple ENIs can lead to increased network security complexity and unintended network paths, particularly in instances with multiple subnets. I added a new check that helps ensure that instances are not overly complex or exposed to additional network risks.

Checks whether an Amazon EC2 instance uses multiple Elastic Network Interfaces (ENIs) or Elastic Fabric Adapters (EFAs). The control passes if the instance is using a single network adapter.

Description

Change ec2_service to add a new attribute network interfaces to Instance model saving ENIs ids to check type. Added the check logic in ec2_instance_multiple_eni. Added tests for service and check.

Checklist

License

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

MarioRgzLpz and others added 11 commits August 23, 2024 10:41
… chose what we save for each launch template version, adjust the service test to the changes
…bute public_ip_addresses with ipaddress IPs to the NetworkInterface model with tests, added tags to LaunchTemplate model and list with network interfaces to TemplateData model
…erfaces to adjust to the change from list to dict and add new attribute public_ip_addresses to one ec2 test
…he-data-saved-in-template-data-in-launch-template-version
…l, network_interfaces, and adjust service tests
@MarioRgzLpz MarioRgzLpz requested review from a team as code owners September 5, 2024 10:24
Base automatically changed from PRWLR-4617-change-ec-2-service-to-adjust-the-data-saved-in-template-data-in-launch-template-version to master September 9, 2024 16:32
@github-actions github-actions bot added the provider/aws Issues/PRs related with the AWS provider label Sep 10, 2024
@@ -72,6 +72,11 @@ def _describe_instances(self, regional_client):
if not self.audit_resources or (
is_resource_filtered(arn, self.audit_resources)
):
enis = []
for eni in instance.get("NetworkInterfaces"):
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
for eni in instance.get("NetworkInterfaces"):
for eni in instance.get("NetworkInterfaces, []"):

)

message_status_extended = ""
if len(eni_types["efa"]) + len(eni_types["interface"]) > 1:
Copy link
Member

Choose a reason for hiding this comment

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

Why not trunk here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

SecurityHub recommendation is to check only for multiple EFAs or interfaces. Thats because a trunk ENI is used to increase the number of ENIs you can attach to a instance. For more information look at the first paragraph here https://docs.aws.amazon.com/AmazonECS/latest/developerguide/container-instance-eni.html and at the AWSVPC trunking section here https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-account-settings.html. I don't know if there should be a limit to trunk ENIs maybe we have to discuss that.

Copy link
Member

Choose a reason for hiding this comment

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

Can we then ignore trunk ENIs in the check? So the user do not get confused when seeing the status extended since he is not going to be able to delete those trunk ENIs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why aren't you able to delete trunk ENIs? Looked for it in the docs but didn't find it. Still I think is a good idea to ignore trunk ENIs since they only add noise and no real value to the provided information.

Copy link
Member

Choose a reason for hiding this comment

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

We saw that trunks are normal interfaces with a specific option so I would count it as an ENI too.

@@ -0,0 +1,34 @@
{
"Provider": "aws",
"CheckID": "ec2_instance_multiple_eni",
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"CheckID": "ec2_instance_multiple_eni",
"CheckID": "ec2_instance_uses_single_eni",

assert result[0].resource_id == "i-0123456789abcdef0"
assert (
result[0].status_extended
== "EC2 Instance i-0123456789abcdef0 uses multiple ENIs: "
Copy link
Member

Choose a reason for hiding this comment

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

Can you prettify this message please?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What do you mean by prettify? Should I add global attributes for the instance id and enis ids?

Copy link
Member

Choose a reason for hiding this comment

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

Something like:

"EC2 Instance i-0123456789abcdef0 uses only one ENI: (EFAs: ['eni-2'], Trunks: ['eni-1', 'eni-3'].)"

Copy link

codecov bot commented Sep 10, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 88.98%. Comparing base (c0c5996) to head (b6fce74).
Report is 1206 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4935      +/-   ##
==========================================
+ Coverage   88.94%   88.98%   +0.04%     
==========================================
  Files         951      955       +4     
  Lines       29182    29295     +113     
==========================================
+ Hits        25956    26069     +113     
  Misses       3226     3226              
Components Coverage Δ
prowler 88.98% <100.00%> (+0.04%) ⬆️
api ∅ <ø> (∅)

@MrCloudSec MrCloudSec self-requested a review September 12, 2024 17:36
@MrCloudSec MrCloudSec merged commit 92b6e72 into master Sep 12, 2024
11 checks passed
@MrCloudSec MrCloudSec deleted the PRWLR-4489-amazon-ec-2-instances-should-not-use-multiple-en-is branch September 12, 2024 18:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
provider/aws Issues/PRs related with the AWS provider
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants