Skip to content

Commit

Permalink
chore: revision
Browse files Browse the repository at this point in the history
  • Loading branch information
sergargar committed Oct 14, 2024
1 parent 84aa52c commit c08f1d8
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"Provider": "aws",
"CheckID": "autoscaling_group_launch_template",
"CheckID": "autoscaling_group_using_ec2_launch_template",
"CheckTitle": "Check if Amazon EC2 Auto Scaling groups use EC2 launch templates.",
"CheckType": [
"Software and Configuration Checks/AWS Security Best Practices"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
)


class autoscaling_group_launch_template(Check):
class autoscaling_group_using_ec2_launch_template(Check):
def execute(self):
findings = []
for group in autoscaling_client.groups:
Expand All @@ -15,16 +15,14 @@ def execute(self):
report.resource_tags = group.tags
report.status = "PASS"
report.status_extended = (
f"Autoscaling group {group.name} is using a launch template."
f"Autoscaling group {group.name} is using an EC2 launch template."
)
if (
not group.launch_template
and not group.mixed_instances_policy_launch_template
):
report.status = "FAIL"
report.status_extended = (
f"Autoscaling group {group.name} is not using a launch template."
)
report.status_extended = f"Autoscaling group {group.name} is not using an EC2 launch template."

findings.append(report)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ def _describe_auto_scaling_groups(self, regional_client):
launch_template=group.get("LaunchTemplate", {}),
mixed_instances_policy_launch_template=group.get(
"MixedInstancesPolicy", {}
).get("LaunchTemplate", {}),
)
.get("LaunchTemplate", {})
.get("LaunchTemplateSpecification", {}),
health_check_type=group.get("HealthCheckType", ""),
load_balancers=group.get("LoadBalancerNames", []),
target_groups=group.get("TargetGroupARNs", []),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from tests.providers.aws.utils import AWS_REGION_US_EAST_1, set_mocked_aws_provider


class Test_autoscaling_group_launch_template:
class Test_autoscaling_group_using_ec2_launch_template:
@mock_aws
def test_no_autoscaling(self):
autoscaling_client = client("autoscaling", region_name=AWS_REGION_US_EAST_1)
Expand All @@ -22,15 +22,15 @@ def test_no_autoscaling(self):
"prowler.providers.common.provider.Provider.get_global_provider",
return_value=aws_provider,
), mock.patch(
"prowler.providers.aws.services.autoscaling.autoscaling_group_launch_template.autoscaling_group_launch_template.autoscaling_client",
"prowler.providers.aws.services.autoscaling.autoscaling_group_using_ec2_launch_template.autoscaling_group_using_ec2_launch_template.autoscaling_client",
new=AutoScaling(aws_provider),
):
# Test Check
from prowler.providers.aws.services.autoscaling.autoscaling_group_launch_template.autoscaling_group_launch_template import (
autoscaling_group_launch_template,
from prowler.providers.aws.services.autoscaling.autoscaling_group_using_ec2_launch_template.autoscaling_group_using_ec2_launch_template import (
autoscaling_group_using_ec2_launch_template,
)

check = autoscaling_group_launch_template()
check = autoscaling_group_using_ec2_launch_template()
result = check.execute()

assert len(result) == 0
Expand Down Expand Up @@ -73,22 +73,22 @@ def test_groups_with_launch_template(self):
"prowler.providers.common.provider.Provider.get_global_provider",
return_value=aws_provider,
), mock.patch(
"prowler.providers.aws.services.autoscaling.autoscaling_group_launch_template.autoscaling_group_launch_template.autoscaling_client",
"prowler.providers.aws.services.autoscaling.autoscaling_group_using_ec2_launch_template.autoscaling_group_using_ec2_launch_template.autoscaling_client",
new=AutoScaling(aws_provider),
):
# Test Check
from prowler.providers.aws.services.autoscaling.autoscaling_group_launch_template.autoscaling_group_launch_template import (
autoscaling_group_launch_template,
from prowler.providers.aws.services.autoscaling.autoscaling_group_using_ec2_launch_template.autoscaling_group_using_ec2_launch_template import (
autoscaling_group_using_ec2_launch_template,
)

check = autoscaling_group_launch_template()
check = autoscaling_group_using_ec2_launch_template()
result = check.execute()

assert len(result) == 1
assert result[0].status == "PASS"
assert (
result[0].status_extended
== f"Autoscaling group {autoscaling_group_name} is using a launch template."
== f"Autoscaling group {autoscaling_group_name} is using an EC2 launch template."
)
assert result[0].resource_id == autoscaling_group_name
assert result[0].resource_arn == autoscaling_group_arn
Expand Down Expand Up @@ -146,22 +146,22 @@ def test_groups_with_mixed_policy_launch_template(self):
"prowler.providers.common.provider.Provider.get_global_provider",
return_value=aws_provider,
), mock.patch(
"prowler.providers.aws.services.autoscaling.autoscaling_group_launch_template.autoscaling_group_launch_template.autoscaling_client",
"prowler.providers.aws.services.autoscaling.autoscaling_group_using_ec2_launch_template.autoscaling_group_using_ec2_launch_template.autoscaling_client",
new=AutoScaling(aws_provider),
):
# Test Check
from prowler.providers.aws.services.autoscaling.autoscaling_group_launch_template.autoscaling_group_launch_template import (
autoscaling_group_launch_template,
from prowler.providers.aws.services.autoscaling.autoscaling_group_using_ec2_launch_template.autoscaling_group_using_ec2_launch_template import (
autoscaling_group_using_ec2_launch_template,
)

check = autoscaling_group_launch_template()
check = autoscaling_group_using_ec2_launch_template()
result = check.execute()

assert len(result) == 1
assert result[0].status == "PASS"
assert (
result[0].status_extended
== f"Autoscaling group {autoscaling_group_name} is using a launch template."
== f"Autoscaling group {autoscaling_group_name} is using an EC2 launch template."
)
assert result[0].resource_id == autoscaling_group_name
assert result[0].resource_tags == []
Expand Down Expand Up @@ -201,22 +201,22 @@ def test_groups_without_launch_templates(self):
"prowler.providers.common.provider.Provider.get_global_provider",
return_value=aws_provider,
), mock.patch(
"prowler.providers.aws.services.autoscaling.autoscaling_group_launch_template.autoscaling_group_launch_template.autoscaling_client",
"prowler.providers.aws.services.autoscaling.autoscaling_group_using_ec2_launch_template.autoscaling_group_using_ec2_launch_template.autoscaling_client",
new=AutoScaling(aws_provider),
):
# Test Check
from prowler.providers.aws.services.autoscaling.autoscaling_group_launch_template.autoscaling_group_launch_template import (
autoscaling_group_launch_template,
from prowler.providers.aws.services.autoscaling.autoscaling_group_using_ec2_launch_template.autoscaling_group_using_ec2_launch_template import (
autoscaling_group_using_ec2_launch_template,
)

check = autoscaling_group_launch_template()
check = autoscaling_group_using_ec2_launch_template()
result = check.execute()

assert len(result) == 1
assert result[0].status == "FAIL"
assert (
result[0].status_extended
== f"Autoscaling group {autoscaling_group_name} is not using a launch template."
== f"Autoscaling group {autoscaling_group_name} is not using an EC2 launch template."
)
assert result[0].resource_id == autoscaling_group_name
assert result[0].resource_tags == []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def test_describe_auto_scaling_groups(self):
# Generate AutoScaling Client
autoscaling_client = client("autoscaling", region_name=AWS_REGION_US_EAST_1)
ec2_client = client("ec2", region_name=AWS_REGION_US_EAST_1)
launch_template = ec2_client.create_launch_template(
ec2_client.create_launch_template(
LaunchTemplateName="test",
LaunchTemplateData={
"ImageId": "ami-12c6146b",
Expand Down Expand Up @@ -120,7 +120,7 @@ def test_describe_auto_scaling_groups(self):
Matcher={"HttpCode": "200"},
)

_ = autoscaling_client.create_auto_scaling_group(
autoscaling_client.create_auto_scaling_group(
AutoScalingGroupName="my-autoscaling-group",
LaunchTemplate={"LaunchTemplateName": "test", "Version": "$Latest"},
MinSize=0,
Expand Down Expand Up @@ -170,8 +170,12 @@ def test_describe_auto_scaling_groups(self):
aws_provider = set_mocked_aws_provider([AWS_REGION_US_EAST_1])
autoscaling = AutoScaling(aws_provider)
assert len(autoscaling.groups) == 2
# create_auto_scaling_group doesn't return the ARN, can't check it
# assert autoscaling.groups[0].arn ==
assert (
autoscaling.groups[0].arn
== autoscaling_client.describe_auto_scaling_groups(
AutoScalingGroupNames=["my-autoscaling-group"]
)["AutoScalingGroups"][0]["AutoScalingGroupARN"]
)
assert autoscaling.groups[0].name == "my-autoscaling-group"
assert autoscaling.groups[0].region == AWS_REGION_US_EAST_1
assert autoscaling.groups[0].availability_zones == ["us-east-1a", "us-east-1b"]
Expand All @@ -184,27 +188,16 @@ def test_describe_auto_scaling_groups(self):
"Value": "value_test",
}
]
assert autoscaling.groups[0].launch_template == {
"LaunchTemplateId": launch_template_id,
"LaunchTemplateName": "test",
"Version": "$Latest",
}
assert autoscaling.groups[1].mixed_instances_policy_launch_template == {
"LaunchTemplateSpecification": {
"LaunchTemplateId": launch_template_id,
"LaunchTemplateName": "test",
"Version": "$Latest",
},
"Overrides": [
{
"InstanceType": "t2.micro",
"WeightedCapacity": "1",
},
],
}
assert autoscaling.groups[0].health_check_type == "ELB"
assert autoscaling.groups[0].load_balancers == ["my-load-balancer"]
assert autoscaling.groups[0].target_groups == [
assert autoscaling.groups[0].launch_template["LaunchTemplateName"] == "test"
assert (
autoscaling.groups[1].mixed_instances_policy_launch_template[
"LaunchTemplateName"
]
== "test"
)
assert autoscaling.groups[1].health_check_type == "ELB"
assert autoscaling.groups[1].load_balancers == ["my-load-balancer"]
assert autoscaling.groups[1].target_groups == [
target_group["TargetGroups"][0]["TargetGroupArn"]
]

Expand Down

0 comments on commit c08f1d8

Please sign in to comment.