Skip to content

Commit 3fb86d7

Browse files
fix(cloudwatch): handle None metric alarms (#7207)
Co-authored-by: Sergio Garcia <[email protected]>
1 parent 7874707 commit 3fb86d7

File tree

4 files changed

+139
-58
lines changed

4 files changed

+139
-58
lines changed

prowler/providers/aws/services/cloudwatch/cloudwatch_alarm_actions_alarm_state_configured/cloudwatch_alarm_actions_alarm_state_configured.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@
77
class cloudwatch_alarm_actions_alarm_state_configured(Check):
88
def execute(self):
99
findings = []
10-
for metric_alarm in cloudwatch_client.metric_alarms:
11-
report = Check_Report_AWS(metadata=self.metadata(), resource=metric_alarm)
12-
report.status = "PASS"
13-
report.status_extended = f"CloudWatch metric alarm {metric_alarm.name} has actions configured for the ALARM state."
14-
if not metric_alarm.alarm_actions:
15-
report.status = "FAIL"
16-
report.status_extended = f"CloudWatch metric alarm {metric_alarm.name} does not have actions configured for the ALARM state."
17-
findings.append(report)
10+
if cloudwatch_client.metric_alarms is not None:
11+
for metric_alarm in cloudwatch_client.metric_alarms:
12+
report = Check_Report_AWS(
13+
metadata=self.metadata(), resource=metric_alarm
14+
)
15+
report.status = "PASS"
16+
report.status_extended = f"CloudWatch metric alarm {metric_alarm.name} has actions configured for the ALARM state."
17+
if not metric_alarm.alarm_actions:
18+
report.status = "FAIL"
19+
report.status_extended = f"CloudWatch metric alarm {metric_alarm.name} does not have actions configured for the ALARM state."
20+
findings.append(report)
1821
return findings

prowler/providers/aws/services/cloudwatch/cloudwatch_alarm_actions_enabled/cloudwatch_alarm_actions_enabled.py

+13-10
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@
77
class cloudwatch_alarm_actions_enabled(Check):
88
def execute(self):
99
findings = []
10-
for metric_alarm in cloudwatch_client.metric_alarms:
11-
report = Check_Report_AWS(metadata=self.metadata(), resource=metric_alarm)
12-
report.status = "PASS"
13-
report.status_extended = (
14-
f"CloudWatch metric alarm {metric_alarm.name} has actions enabled."
15-
)
16-
if not metric_alarm.actions_enabled:
17-
report.status = "FAIL"
18-
report.status_extended = f"CloudWatch metric alarm {metric_alarm.name} does not have actions enabled."
19-
findings.append(report)
10+
if cloudwatch_client.metric_alarms is not None:
11+
for metric_alarm in cloudwatch_client.metric_alarms:
12+
report = Check_Report_AWS(
13+
metadata=self.metadata(), resource=metric_alarm
14+
)
15+
report.status = "PASS"
16+
report.status_extended = (
17+
f"CloudWatch metric alarm {metric_alarm.name} has actions enabled."
18+
)
19+
if not metric_alarm.actions_enabled:
20+
report.status = "FAIL"
21+
report.status_extended = f"CloudWatch metric alarm {metric_alarm.name} does not have actions enabled."
22+
findings.append(report)
2023
return findings

tests/providers/aws/services/cloudwatch/cloudwatch_alarm_actions_alarm_state_configured/cloudwatch_alarm_actions_alarm_state_configured_test.py

+58-20
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,52 @@ def test_no_cloudwatch_alarms(self):
1818

1919
aws_provider = set_mocked_aws_provider([AWS_REGION_US_EAST_1])
2020

21-
with mock.patch(
22-
"prowler.providers.common.provider.Provider.get_global_provider",
23-
return_value=aws_provider,
24-
), mock.patch(
25-
"prowler.providers.aws.services.cloudwatch.cloudwatch_alarm_actions_alarm_state_configured.cloudwatch_alarm_actions_alarm_state_configured.cloudwatch_client",
26-
new=CloudWatch(aws_provider),
21+
with (
22+
mock.patch(
23+
"prowler.providers.common.provider.Provider.get_global_provider",
24+
return_value=aws_provider,
25+
),
26+
mock.patch(
27+
"prowler.providers.aws.services.cloudwatch.cloudwatch_alarm_actions_alarm_state_configured.cloudwatch_alarm_actions_alarm_state_configured.cloudwatch_client",
28+
new=CloudWatch(aws_provider),
29+
),
2730
):
31+
from prowler.providers.aws.services.cloudwatch.cloudwatch_alarm_actions_alarm_state_configured.cloudwatch_alarm_actions_alarm_state_configured import (
32+
cloudwatch_alarm_actions_alarm_state_configured,
33+
)
34+
35+
check = cloudwatch_alarm_actions_alarm_state_configured()
36+
result = check.execute()
37+
38+
assert len(result) == 0
39+
40+
@mock_aws
41+
def test_none_cloudwatch_alarms(self):
42+
cloudwatch_client = client("cloudwatch", region_name=AWS_REGION_US_EAST_1)
43+
cloudwatch_client.metric_alarms = []
44+
45+
from prowler.providers.aws.services.cloudwatch.cloudwatch_service import (
46+
CloudWatch,
47+
)
2848

49+
aws_provider = set_mocked_aws_provider([AWS_REGION_US_EAST_1])
50+
51+
with (
52+
mock.patch(
53+
"prowler.providers.common.provider.Provider.get_global_provider",
54+
return_value=aws_provider,
55+
),
56+
mock.patch(
57+
"prowler.providers.aws.services.cloudwatch.cloudwatch_alarm_actions_alarm_state_configured.cloudwatch_alarm_actions_alarm_state_configured.cloudwatch_client",
58+
new=CloudWatch(aws_provider),
59+
) as cloudwatch_client_mock,
60+
):
2961
from prowler.providers.aws.services.cloudwatch.cloudwatch_alarm_actions_alarm_state_configured.cloudwatch_alarm_actions_alarm_state_configured import (
3062
cloudwatch_alarm_actions_alarm_state_configured,
3163
)
3264

65+
cloudwatch_client_mock.metric_alarms = None
66+
3367
check = cloudwatch_alarm_actions_alarm_state_configured()
3468
result = check.execute()
3569

@@ -53,14 +87,16 @@ def test_cloudwatch_alarms_actions_configured(self):
5387

5488
aws_provider = set_mocked_aws_provider([AWS_REGION_US_EAST_1])
5589

56-
with mock.patch(
57-
"prowler.providers.common.provider.Provider.get_global_provider",
58-
return_value=aws_provider,
59-
), mock.patch(
60-
"prowler.providers.aws.services.cloudwatch.cloudwatch_alarm_actions_alarm_state_configured.cloudwatch_alarm_actions_alarm_state_configured.cloudwatch_client",
61-
new=CloudWatch(aws_provider),
90+
with (
91+
mock.patch(
92+
"prowler.providers.common.provider.Provider.get_global_provider",
93+
return_value=aws_provider,
94+
),
95+
mock.patch(
96+
"prowler.providers.aws.services.cloudwatch.cloudwatch_alarm_actions_alarm_state_configured.cloudwatch_alarm_actions_alarm_state_configured.cloudwatch_client",
97+
new=CloudWatch(aws_provider),
98+
),
6299
):
63-
64100
from prowler.providers.aws.services.cloudwatch.cloudwatch_alarm_actions_alarm_state_configured.cloudwatch_alarm_actions_alarm_state_configured import (
65101
cloudwatch_alarm_actions_alarm_state_configured,
66102
)
@@ -100,14 +136,16 @@ def test_cloudwatch_alarms_actions_not_configured(self):
100136

101137
aws_provider = set_mocked_aws_provider([AWS_REGION_US_EAST_1])
102138

103-
with mock.patch(
104-
"prowler.providers.common.provider.Provider.get_global_provider",
105-
return_value=aws_provider,
106-
), mock.patch(
107-
"prowler.providers.aws.services.cloudwatch.cloudwatch_alarm_actions_alarm_state_configured.cloudwatch_alarm_actions_alarm_state_configured.cloudwatch_client",
108-
new=CloudWatch(aws_provider),
139+
with (
140+
mock.patch(
141+
"prowler.providers.common.provider.Provider.get_global_provider",
142+
return_value=aws_provider,
143+
),
144+
mock.patch(
145+
"prowler.providers.aws.services.cloudwatch.cloudwatch_alarm_actions_alarm_state_configured.cloudwatch_alarm_actions_alarm_state_configured.cloudwatch_client",
146+
new=CloudWatch(aws_provider),
147+
),
109148
):
110-
111149
from prowler.providers.aws.services.cloudwatch.cloudwatch_alarm_actions_alarm_state_configured.cloudwatch_alarm_actions_alarm_state_configured import (
112150
cloudwatch_alarm_actions_alarm_state_configured,
113151
)

tests/providers/aws/services/cloudwatch/cloudwatch_alarm_actions_enabled/cloudwatch_alarm_actions_enabled_test.py

+57-20
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,51 @@ def test_no_cloudwatch_alarms(self):
1818

1919
aws_provider = set_mocked_aws_provider([AWS_REGION_US_EAST_1])
2020

21-
with mock.patch(
22-
"prowler.providers.common.provider.Provider.get_global_provider",
23-
return_value=aws_provider,
24-
), mock.patch(
25-
"prowler.providers.aws.services.cloudwatch.cloudwatch_alarm_actions_enabled.cloudwatch_alarm_actions_enabled.cloudwatch_client",
26-
new=CloudWatch(aws_provider),
21+
with (
22+
mock.patch(
23+
"prowler.providers.common.provider.Provider.get_global_provider",
24+
return_value=aws_provider,
25+
),
26+
mock.patch(
27+
"prowler.providers.aws.services.cloudwatch.cloudwatch_alarm_actions_enabled.cloudwatch_alarm_actions_enabled.cloudwatch_client",
28+
new=CloudWatch(aws_provider),
29+
),
2730
):
31+
from prowler.providers.aws.services.cloudwatch.cloudwatch_alarm_actions_enabled.cloudwatch_alarm_actions_enabled import (
32+
cloudwatch_alarm_actions_enabled,
33+
)
34+
35+
check = cloudwatch_alarm_actions_enabled()
36+
result = check.execute()
37+
38+
assert len(result) == 0
39+
40+
def test_none_cloudwatch_alarms(self):
41+
cloudwatch_client = client("cloudwatch", region_name=AWS_REGION_US_EAST_1)
42+
cloudwatch_client.metric_alarms = []
2843

44+
from prowler.providers.aws.services.cloudwatch.cloudwatch_service import (
45+
CloudWatch,
46+
)
47+
48+
aws_provider = set_mocked_aws_provider([AWS_REGION_US_EAST_1])
49+
50+
with (
51+
mock.patch(
52+
"prowler.providers.common.provider.Provider.get_global_provider",
53+
return_value=aws_provider,
54+
),
55+
mock.patch(
56+
"prowler.providers.aws.services.cloudwatch.cloudwatch_alarm_actions_enabled.cloudwatch_alarm_actions_enabled.cloudwatch_client",
57+
new=CloudWatch(aws_provider),
58+
) as cloudwatch_client_mock,
59+
):
2960
from prowler.providers.aws.services.cloudwatch.cloudwatch_alarm_actions_enabled.cloudwatch_alarm_actions_enabled import (
3061
cloudwatch_alarm_actions_enabled,
3162
)
3263

64+
cloudwatch_client_mock.metric_alarms = None
65+
3366
check = cloudwatch_alarm_actions_enabled()
3467
result = check.execute()
3568

@@ -53,14 +86,16 @@ def test_cloudwatch_alarms_actions_enabled(self):
5386

5487
aws_provider = set_mocked_aws_provider([AWS_REGION_US_EAST_1])
5588

56-
with mock.patch(
57-
"prowler.providers.common.provider.Provider.get_global_provider",
58-
return_value=aws_provider,
59-
), mock.patch(
60-
"prowler.providers.aws.services.cloudwatch.cloudwatch_alarm_actions_enabled.cloudwatch_alarm_actions_enabled.cloudwatch_client",
61-
new=CloudWatch(aws_provider),
89+
with (
90+
mock.patch(
91+
"prowler.providers.common.provider.Provider.get_global_provider",
92+
return_value=aws_provider,
93+
),
94+
mock.patch(
95+
"prowler.providers.aws.services.cloudwatch.cloudwatch_alarm_actions_enabled.cloudwatch_alarm_actions_enabled.cloudwatch_client",
96+
new=CloudWatch(aws_provider),
97+
),
6298
):
63-
6499
from prowler.providers.aws.services.cloudwatch.cloudwatch_alarm_actions_enabled.cloudwatch_alarm_actions_enabled import (
65100
cloudwatch_alarm_actions_enabled,
66101
)
@@ -100,14 +135,16 @@ def test_cloudwatch_alarms_actions_disabled(self):
100135

101136
aws_provider = set_mocked_aws_provider([AWS_REGION_US_EAST_1])
102137

103-
with mock.patch(
104-
"prowler.providers.common.provider.Provider.get_global_provider",
105-
return_value=aws_provider,
106-
), mock.patch(
107-
"prowler.providers.aws.services.cloudwatch.cloudwatch_alarm_actions_enabled.cloudwatch_alarm_actions_enabled.cloudwatch_client",
108-
new=CloudWatch(aws_provider),
138+
with (
139+
mock.patch(
140+
"prowler.providers.common.provider.Provider.get_global_provider",
141+
return_value=aws_provider,
142+
),
143+
mock.patch(
144+
"prowler.providers.aws.services.cloudwatch.cloudwatch_alarm_actions_enabled.cloudwatch_alarm_actions_enabled.cloudwatch_client",
145+
new=CloudWatch(aws_provider),
146+
),
109147
):
110-
111148
from prowler.providers.aws.services.cloudwatch.cloudwatch_alarm_actions_enabled.cloudwatch_alarm_actions_enabled import (
112149
cloudwatch_alarm_actions_enabled,
113150
)

0 commit comments

Comments
 (0)