Skip to content

Commit 2b0c93d

Browse files
fix(iam): update logic of Root Hardware MFA check (#4774)
Co-authored-by: Sergio <[email protected]>
1 parent 7b29326 commit 2b0c93d

File tree

2 files changed

+30
-34
lines changed

2 files changed

+30
-34
lines changed

prowler/providers/aws/services/iam/iam_root_hardware_mfa_enabled/iam_root_hardware_mfa_enabled.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ def execute(self) -> Check_Report_AWS:
1515
report.resource_arn = iam_client.mfa_arn_template
1616

1717
if iam_client.account_summary["SummaryMap"]["AccountMFAEnabled"] > 0:
18-
virtual_mfas = iam_client.virtual_mfa_devices
19-
for mfa in virtual_mfas:
20-
if "root" in mfa["SerialNumber"]:
18+
for mfa in iam_client.virtual_mfa_devices:
19+
# If the ARN of the associated IAM user of the Virtual MFA device is "arn:aws:iam::[aws-account-id]:root", your AWS root account is not using a hardware-based MFA device for MFA protection.
20+
if "root" in mfa.get("User", {}).get("Arn", ""):
2121
virtual_mfa = True
2222
report.status = "FAIL"
2323
report.status_extended = "Root account has a virtual MFA instead of a hardware MFA device enabled."
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
from re import search
22
from unittest import mock
33

4-
from boto3 import client
5-
from moto import mock_aws
6-
74
from tests.providers.aws.audit_info_utils import (
85
AWS_ACCOUNT_NUMBER,
96
AWS_REGION_US_EAST_1,
@@ -19,13 +16,20 @@ class Test_iam_root_hardware_mfa_enabled_test:
1916
set_mocked_aws_audit_info,
2017
)
2118

22-
@mock_aws
23-
def test_root_hardware_virtual_mfa_enabled(self):
24-
iam = client("iam")
25-
mfa_device_name = "mfa-test"
26-
iam.create_virtual_mfa_device(VirtualMFADeviceName=mfa_device_name)
27-
28-
from prowler.providers.aws.services.iam.iam_service import IAM
19+
def test_root_virtual_mfa_enabled(self):
20+
iam_client = mock.MagicMock
21+
iam_client.account_summary = {
22+
"SummaryMap": {"AccountMFAEnabled": 1},
23+
}
24+
iam_client.virtual_mfa_devices = [
25+
{
26+
"SerialNumber": f"arn:aws:iam::{AWS_ACCOUNT_NUMBER}:mfa/mfa",
27+
"User": {"Arn": f"arn:aws:iam::{AWS_ACCOUNT_NUMBER}:root"},
28+
}
29+
]
30+
iam_client.audited_partition = "aws"
31+
iam_client.region = AWS_REGION_US_EAST_1
32+
iam_client.mfa_arn_template = f"arn:aws:iam::{AWS_ACCOUNT_NUMBER}:mfa"
2933

3034
current_audit_info = set_mocked_aws_audit_info([AWS_REGION_US_EAST_1])
3135

@@ -34,15 +38,12 @@ def test_root_hardware_virtual_mfa_enabled(self):
3438
new=current_audit_info,
3539
), mock.patch(
3640
"prowler.providers.aws.services.iam.iam_root_hardware_mfa_enabled.iam_root_hardware_mfa_enabled.iam_client",
37-
new=IAM(current_audit_info),
38-
) as service_client:
41+
new=iam_client,
42+
):
3943
from prowler.providers.aws.services.iam.iam_root_hardware_mfa_enabled.iam_root_hardware_mfa_enabled import (
4044
iam_root_hardware_mfa_enabled,
4145
)
4246

43-
service_client.account_summary["SummaryMap"]["AccountMFAEnabled"] = 1
44-
service_client.virtual_mfa_devices[0]["SerialNumber"] = "sddfaf-root-sfsfds"
45-
4647
check = iam_root_hardware_mfa_enabled()
4748
result = check.execute()
4849
assert result[0].status == "FAIL"
@@ -52,13 +53,15 @@ def test_root_hardware_virtual_mfa_enabled(self):
5253
)
5354
assert result[0].resource_id == "<root_account>"
5455

55-
@mock_aws
56-
def test_root_hardware_virtual_hardware_mfa_enabled(self):
57-
iam = client("iam")
58-
mfa_device_name = "mfa-test"
59-
iam.create_virtual_mfa_device(VirtualMFADeviceName=mfa_device_name)
60-
61-
from prowler.providers.aws.services.iam.iam_service import IAM
56+
def test_root_hardware_mfa_enabled(self):
57+
iam_client = mock.MagicMock
58+
iam_client.account_summary = {
59+
"SummaryMap": {"AccountMFAEnabled": 1},
60+
}
61+
iam_client.virtual_mfa_devices = []
62+
iam_client.audited_partition = "aws"
63+
iam_client.region = AWS_REGION_US_EAST_1
64+
iam_client.mfa_arn_template = f"arn:aws:iam::{AWS_ACCOUNT_NUMBER}:mfa"
6265

6366
current_audit_info = set_mocked_aws_audit_info([AWS_REGION_US_EAST_1])
6467

@@ -67,15 +70,12 @@ def test_root_hardware_virtual_hardware_mfa_enabled(self):
6770
new=current_audit_info,
6871
), mock.patch(
6972
"prowler.providers.aws.services.iam.iam_root_hardware_mfa_enabled.iam_root_hardware_mfa_enabled.iam_client",
70-
new=IAM(current_audit_info),
71-
) as service_client:
73+
new=iam_client,
74+
):
7275
from prowler.providers.aws.services.iam.iam_root_hardware_mfa_enabled.iam_root_hardware_mfa_enabled import (
7376
iam_root_hardware_mfa_enabled,
7477
)
7578

76-
service_client.account_summary["SummaryMap"]["AccountMFAEnabled"] = 1
77-
service_client.virtual_mfa_devices[0]["SerialNumber"] = ""
78-
7979
check = iam_root_hardware_mfa_enabled()
8080
result = check.execute()
8181
assert result[0].status == "PASS"
@@ -84,7 +84,3 @@ def test_root_hardware_virtual_hardware_mfa_enabled(self):
8484
result[0].status_extended,
8585
)
8686
assert result[0].resource_id == "<root_account>"
87-
assert (
88-
result[0].resource_arn
89-
== f"arn:aws:iam:{AWS_REGION_US_EAST_1}:{AWS_ACCOUNT_NUMBER}:mfa"
90-
)

0 commit comments

Comments
 (0)