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): Ensure EC2 launch templates do not assign public IPs #4852

Conversation

MarioRgzLpz
Copy link
Contributor

Context

This check verifies whether Amazon EC2 launch templates are configured to assign public IP addresses to network interfaces upon launch. The check fails if an EC2 launch template is set to assign a public IP address to network interfaces or if any network interface has a public IP address.

Description

Added ec2_launch_template_no_public_ip check with metadata and respective unit tests.

Checklist

License

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

@MarioRgzLpz MarioRgzLpz requested review from a team as code owners August 23, 2024 10:20
Comment on lines 24 to 25
f"EC2 Launch Template {template.name} in template versions: "
f"{', '.join(versions_with_public_ip)} is configured to assign a public IP address."
Copy link
Member

@MrCloudSec MrCloudSec Aug 23, 2024

Choose a reason for hiding this comment

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

Suggested change
f"EC2 Launch Template {template.name} in template versions: "
f"{', '.join(versions_with_public_ip)} is configured to assign a public IP address."
f"EC2 Launch Template {template.name} is configured to assign a public IP address to network interfaces upon launch in template versions: f"{', '.join(versions_with_public_ip)}."

)
else:
report.status = "PASS"
report.status_extended = f"No versions of EC2 Launch Template {template.name} are configured to assign a public IP address."
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
report.status_extended = f"No versions of EC2 Launch Template {template.name} are configured to assign a public IP address."
report.status_extended = f"EC2 Launch Template {template.name} is not configured to assign a public IP address to network interfaces upon launch."

@MrCloudSec
Copy link
Member

Can we also check the second part of the check?
"The control fails if an EC2 launch template is configured to assign a public IP address to network interfaces or if there is at least one network interface that has a public IP address."

MarioRgzLpz and others added 12 commits August 26, 2024 09:56
…-in-template-data-in-launch-template-version' into PRWLR-4304-add-new-ec-2-check-to-ensure-public-i-ps-are-not-assigned-to-network-interfaces
…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
…-in-template-data-in-launch-template-version' into PRWLR-4304-add-new-ec-2-check-to-ensure-public-i-ps-are-not-assigned-to-network-interfaces
…he-data-saved-in-template-data-in-launch-template-version
…-in-template-data-in-launch-template-version' into PRWLR-4304-add-new-ec-2-check-to-ensure-public-i-ps-are-not-assigned-to-network-interfaces
…-in-template-data-in-launch-template-version' into PRWLR-4304-add-new-ec-2-check-to-ensure-public-i-ps-are-not-assigned-to-network-interfaces
…check, change the default value for the publi_ip_autoassign and add tests
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
…blic-i-ps-are-not-assigned-to-network-interfaces
@github-actions github-actions bot added the provider/aws Issues/PRs related with the AWS provider label Sep 10, 2024
versions_with_network_interfaces_public_ip.append(
str(version.version_number)
)
break
Copy link
Member

Choose a reason for hiding this comment

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

Why a break here? Don't we want to add all the versions with this issue?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That break is only to stop looking in the network interfaces for public IPs, it will still add every version that is using a network interface with a public IP. That is only to optimize because a version can have a ton of network interfaces and it could take so long if not adding that break. My first idea was to add the network interface's id but that would require to iterate over all the network interfaces and that could cause the check to take a lot of time.

)
if version.template_data.network_interfaces:
for network_interface in version.template_data.network_interfaces:
if network_interface.public_ip_addresses != []:
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
if network_interface.public_ip_addresses != []:
if network_interface.public_ip_addresses:

Comment on lines 31 to 48
if (
versions_with_autoassign_public_ip
or versions_with_network_interfaces_public_ip
):
report.status = "FAIL"
extended_messages = []

if versions_with_autoassign_public_ip:
extended_messages.append(
f"EC2 Launch Template {template.name} is configured to assign a public IP address to network interfaces upon launch in template versions: {', '.join(versions_with_autoassign_public_ip)}."
)

if versions_with_network_interfaces_public_ip:
extended_messages.append(
f"EC2 Launch Template {template.name} is using a network interface with public IP addresses in template versions: {', '.join(versions_with_network_interfaces_public_ip)}."
)

report.status_extended = " ".join(extended_messages)
Copy link
Member

Choose a reason for hiding this comment

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

It is better to create 3 different status extended that using this logic to mix two of them.

@MrCloudSec MrCloudSec changed the title feat(ec2): Ensure public IPs are not assigned to network interfaces feat(ec2): Ensure EC2 launch templates do not assign public IPs Sep 10, 2024
Copy link

codecov bot commented Sep 16, 2024

Codecov Report

Attention: Patch coverage is 93.93939% with 2 lines in your changes missing coverage. Please review.

Project coverage is 88.97%. Comparing base (2c69441) to head (42f8354).
Report is 1253 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4852      +/-   ##
==========================================
+ Coverage   88.92%   88.97%   +0.05%     
==========================================
  Files         945      958      +13     
  Lines       29026    29372     +346     
==========================================
+ Hits        25810    26134     +324     
- Misses       3216     3238      +22     
Components Coverage Δ
prowler 88.97% <93.93%> (+0.05%) ⬆️
api ∅ <ø> (∅)

@MrCloudSec MrCloudSec self-requested a review September 16, 2024 16:52
@MrCloudSec MrCloudSec merged commit 91bf99c into master Sep 16, 2024
11 checks passed
@MrCloudSec MrCloudSec deleted the PRWLR-4304-add-new-ec-2-check-to-ensure-public-i-ps-are-not-assigned-to-network-interfaces branch September 16, 2024 16:52
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