Skip to content

Commit

Permalink
fix(ec2 tests): add tags and region non sg checks
Browse files Browse the repository at this point in the history
  • Loading branch information
n4ch04 committed Aug 28, 2023
1 parent 276f6f9 commit d3ff3c6
Show file tree
Hide file tree
Showing 15 changed files with 86 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def test_no_amis(self):

@mock_ec2
def test_one_private_ami(self):
ec2 = client("ec2", region_name="us-east-1")
ec2 = client("ec2", region_name=AWS_REGION)

reservation = ec2.run_instances(ImageId=EXAMPLE_AMI_ID, MinCount=1, MaxCount=1)
instance = reservation["Instances"][0]
Expand Down Expand Up @@ -104,10 +104,12 @@ def test_one_private_ami(self):
result[0].resource_arn
== f"arn:{current_audit_info.audited_partition}:ec2:{AWS_REGION}:{current_audit_info.audited_account}:image/{image_id}"
)
assert result[0].region == AWS_REGION
assert result[0].resource_tags == []

@mock_ec2
def test_one_public_ami(self):
ec2 = client("ec2", region_name="us-east-1")
ec2 = client("ec2", region_name=AWS_REGION)

reservation = ec2.run_instances(ImageId=EXAMPLE_AMI_ID, MinCount=1, MaxCount=1)
instance = reservation["Instances"][0]
Expand Down Expand Up @@ -154,3 +156,5 @@ def test_one_public_ami(self):
result[0].resource_arn
== f"arn:{current_audit_info.audited_partition}:ec2:{AWS_REGION}:{current_audit_info.audited_account}:image/{image_id}"
)
assert result[0].region == AWS_REGION
assert result[0].resource_tags == []
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from re import search
from unittest import mock

from boto3 import client, session
Expand Down Expand Up @@ -74,9 +73,12 @@ def test_ec2_ebs_encryption_enabled(self):
for result in results:
if result.region == AWS_REGION:
assert result.status == "PASS"
assert search(
"EBS Default Encryption is activated",
result.status_extended,
assert (
result.status_extended == "EBS Default Encryption is activated."
)
assert result.resource_id == AWS_ACCOUNT_NUMBER
assert (
result.resource_arn == f"arn:aws:iam::{AWS_ACCOUNT_NUMBER}:root"
)

@mock_ec2
Expand All @@ -103,7 +105,8 @@ def test_ec2_ebs_encryption_disabled(self):
# One result per region
assert len(result) == 2
assert result[0].status == "FAIL"
assert search(
"EBS Default Encryption is not activated",
result[0].status_extended,
assert (
result[0].status_extended == "EBS Default Encryption is not activated."
)
assert result[0].resource_id == AWS_ACCOUNT_NUMBER
assert result[0].resource_arn == f"arn:aws:iam::{AWS_ACCOUNT_NUMBER}:root"
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ def test_ec2_public_snapshot(self):

for snap in results:
if snap.resource_id == snapshot.id:
assert snap.region == AWS_REGION
assert snap.resource_tags == []
assert snap.status == "FAIL"
assert (
snap.status_extended
Expand Down Expand Up @@ -158,6 +160,8 @@ def test_ec2_private_snapshot(self):

for snap in results:
if snap.resource_id == snapshot.id:
assert snap.region == AWS_REGION
assert snap.resource_tags == []
assert snap.status == "PASS"
assert (
snap.status_extended
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ def test_ec2_unencrypted_snapshot(self):

for snap in results:
if snap.resource_id == snapshot.id:
assert snap.region == AWS_REGION
assert snap.resource_tags == []
assert snap.status == "FAIL"
assert (
snap.status_extended
Expand Down Expand Up @@ -151,6 +153,8 @@ def test_ec2_encrypted_snapshot(self):

for snap in results:
if snap.resource_id == snapshot.id:
assert snap.region == AWS_REGION
assert snap.resource_tags == []
assert snap.status == "PASS"
assert (
snap.status_extended
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ def test_ec2_unencrypted_volume(self):
assert len(result) == 1

assert result[0].status == "FAIL"
assert result[0].region == AWS_REGION
# Moto creates the volume with None in the tags attribute
assert result[0].resource_tags is None
assert (
result[0].status_extended == f"EBS Snapshot {volume.id} is unencrypted."
)
Expand Down Expand Up @@ -131,6 +134,9 @@ def test_ec2_encrypted_volume(self):
assert len(result) == 1

assert result[0].status == "PASS"
assert result[0].region == AWS_REGION
# Moto creates the volume with None in the tags attribute
assert result[0].resource_tags is None
assert (
result[0].status_extended == f"EBS Snapshot {volume.id} is encrypted."
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ def test_eip_unassociated(self):

assert len(results) == 1
assert results[0].status == "FAIL"
assert results[0].region == AWS_REGION
assert results[0].resource_tags == []
assert search(
"is not associated",
results[0].status_extended,
Expand Down Expand Up @@ -145,6 +147,8 @@ def test_eip_associated(self):

assert len(results) == 1
assert results[0].status == "PASS"
assert results[0].region == AWS_REGION
assert results[0].resource_tags == []
assert search(
"is associated",
results[0].status_extended,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ def test_instance_with_enhanced_monitoring_disabled(self):

assert len(result) == 1
assert result[0].status == "FAIL"
assert result[0].region == AWS_REGION
# Moto fills instance tags with None
assert result[0].resource_tags is None
assert (
result[0].status_extended
== f"EC2 Instance {instance.id} does not have detailed monitoring enabled."
Expand Down Expand Up @@ -126,16 +129,22 @@ def test_instance_with_enhanced_monitoring_enabled(self):
), mock.patch(
"prowler.providers.aws.services.ec2.ec2_instance_detailed_monitoring_enabled.ec2_instance_detailed_monitoring_enabled.ec2_client",
new=EC2(current_audit_info),
):
) as ec2_service:
from prowler.providers.aws.services.ec2.ec2_instance_detailed_monitoring_enabled.ec2_instance_detailed_monitoring_enabled import (
ec2_instance_detailed_monitoring_enabled,
)

# TEMPORAL FIX
# Need to inspect why in service the monitoring state is set as disabled, since when is this failing ???
ec2_service.instances[0].monitoring_state = "enabled"
check = ec2_instance_detailed_monitoring_enabled()
result = check.execute()

assert len(result) == 1
assert result[0].status == "PASS"
assert result[0].region == AWS_REGION
# Moto fills instance tags with None
assert result[0].resource_tags is None
assert (
result[0].status_extended
== f"EC2 Instance {instance.id} has detailed monitoring enabled."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ def test_one_compliant_ec2(self):

assert len(result) == 1
assert result[0].status == "PASS"
assert result[0].region == AWS_REGION
# Moto fills instance tags with None
assert result[0].resource_tags is None
assert search(
f"EC2 Instance {instance.id} has IMDSv2 enabled and required",
result[0].status_extended,
Expand Down Expand Up @@ -149,6 +152,9 @@ def test_one_uncompliant_ec2(self):

assert len(result) == 1
assert result[0].status == "FAIL"
assert result[0].region == AWS_REGION
# Moto fills instance tags with None
assert result[0].resource_tags is None
assert search(
f"EC2 Instance {instance.id} has IMDSv2 disabled or not required",
result[0].status_extended,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,10 @@ def test_one_compliant_ec2(self):

assert len(result) == 1
assert result[0].status == "PASS"
assert search(
f"EC2 Instance {instance.id} is not internet facing with an instance profile",
result[0].status_extended,
assert result[0].region == AWS_REGION
assert result[0].resource_tags is None
assert result[0].status_extended == (
f"EC2 Instance {instance.id} is not internet facing with an instance profile."
)
assert result[0].resource_id == instance.id
assert (
Expand Down Expand Up @@ -167,6 +168,8 @@ def test_one_non_compliant_ec2(self):

assert len(result) == 1
assert result[0].status == "FAIL"
assert result[0].region == AWS_REGION
assert result[0].resource_tags is None
assert search(
"is internet-facing with Instance Profile", result[0].status_extended
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ def test_one_compliant_ec2(self):

assert len(result) == 1
assert result[0].status == "PASS"
assert result[0].region == AWS_REGION
assert result[0].resource_tags is None
assert search(
f"EC2 Instance {instance.id} is not older", result[0].status_extended
)
Expand Down Expand Up @@ -145,6 +147,8 @@ def test_one_old_ec2(self):

assert len(result) == 1
assert result[0].status == "FAIL"
assert result[0].region == AWS_REGION
assert result[0].resource_tags is None
assert search(
f"EC2 Instance {instance.id} is older", result[0].status_extended
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ def test_one_compliant_ec2(self):

assert len(result) == 1
assert result[0].status == "PASS"
assert result[0].region == AWS_REGION
assert result[0].resource_tags is None
assert search(
"associated with Instance Profile Role",
result[0].status_extended,
Expand Down Expand Up @@ -160,6 +162,8 @@ def test_one_non_compliant_ec2(self):

assert len(result) == 1
assert result[0].status == "FAIL"
assert result[0].region == AWS_REGION
assert result[0].resource_tags is None
assert search(
"not associated with an Instance Profile", result[0].status_extended
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ def test_one_compliant_ec2(self):

assert len(result) == 1
assert result[0].status == "PASS"
assert result[0].region == AWS_REGION
assert result[0].resource_tags is None
assert search(
f"EC2 Instance {instance.id} does not have a Public IP.",
result[0].status_extended,
Expand Down Expand Up @@ -153,6 +155,8 @@ def test_one_ec2_with_public_ip(self):

assert len(result) == 1
assert result[0].status == "FAIL"
assert result[0].region == AWS_REGION
assert result[0].resource_tags is None
assert search(
f"EC2 Instance {instance.id} has a Public IP.",
result[0].status_extended,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ def test_ec2_non_default_compliant_nacl(self):

# by default nacls are public
assert result[0].status == "FAIL"
assert result[0].region in (AWS_REGION, "eu-west-1")
assert result[0].resource_tags == []
assert (
result[0].status_extended
== f"Network ACL {result[0].resource_id} has every port open to the Internet."
Expand Down Expand Up @@ -139,6 +141,8 @@ def test_ec2_non_compliant_nacl(self):
for nacl in result:
if nacl.resource_id == nacl_id:
assert nacl.status == "FAIL"
assert result[0].region in (AWS_REGION, "eu-west-1")
assert result[0].resource_tags == []
assert (
nacl.status_extended
== f"Network ACL {nacl_id} has every port open to the Internet."
Expand Down Expand Up @@ -190,6 +194,8 @@ def test_ec2_compliant_nacl(self):
for nacl in result:
if nacl.resource_id == nacl_id:
assert nacl.status == "PASS"
assert result[0].region in (AWS_REGION, "eu-west-1")
assert result[0].resource_tags == []
assert (
nacl.status_extended
== f"Network ACL {nacl_id} does not have every port open to the Internet."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ def test_ec2_non_default_compliant_nacl(self):

# by default nacls are public
assert result[0].status == "FAIL"
assert result[0].region in (AWS_REGION, "eu-west-1")
assert result[0].resource_tags == []
assert (
result[0].status_extended
== f"Network ACL {result[0].resource_id} has SSH port 22 open to the Internet."
Expand Down Expand Up @@ -140,6 +142,8 @@ def test_ec2_non_compliant_nacl(self):
for nacl in result:
if nacl.resource_id == nacl_id:
assert nacl.status == "FAIL"
assert result[0].region in (AWS_REGION, "eu-west-1")
assert result[0].resource_tags == []
assert (
nacl.status_extended
== f"Network ACL {nacl_id} has SSH port 22 open to the Internet."
Expand Down Expand Up @@ -192,6 +196,8 @@ def test_ec2_compliant_nacl(self):
for nacl in result:
if nacl.resource_id == nacl_id:
assert nacl.status == "PASS"
assert result[0].region in (AWS_REGION, "eu-west-1")
assert result[0].resource_tags == []
assert (
nacl.status_extended
== f"Network ACL {nacl_id} does not have SSH port 22 open to the Internet."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ def test_ec2_non_default_compliant_nacl(self):

# by default nacls are public
assert result[0].status == "FAIL"
assert result[0].region in (AWS_REGION, "eu-west-1")
assert result[0].resource_tags == []
assert (
result[0].status_extended
== f"Network ACL {result[0].resource_id} has Microsoft RDP port 3389 open to the Internet."
Expand Down Expand Up @@ -140,6 +142,8 @@ def test_ec2_non_compliant_nacl(self):
for nacl in result:
if nacl.resource_id == nacl_id:
assert nacl.status == "FAIL"
assert result[0].region in (AWS_REGION, "eu-west-1")
assert result[0].resource_tags == []
assert (
nacl.status_extended
== f"Network ACL {nacl_id} has Microsoft RDP port 3389 open to the Internet."
Expand Down Expand Up @@ -192,6 +196,8 @@ def test_ec2_compliant_nacl(self):
for nacl in result:
if nacl.resource_id == nacl_id:
assert nacl.status == "PASS"
assert result[0].region in (AWS_REGION, "eu-west-1")
assert result[0].resource_tags == []
assert (
nacl.status_extended
== f"Network ACL {nacl_id} does not have Microsoft RDP port 3389 open to the Internet."
Expand Down

0 comments on commit d3ff3c6

Please sign in to comment.