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

fix(regions): show all for empty regions #5143

Merged
merged 4 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions prowler/providers/aws/aws_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ def print_credentials(self):
regions = (
", ".join(self._identity.audited_regions)
if self._identity.audited_regions is not None
and self._identity.audited_regions != set()
else "all"
)
# Beautify audited profile, set "default" if there is no profile set
Expand Down
113 changes: 113 additions & 0 deletions tests/providers/aws/aws_provider_test.py
Copy link
Member

Choose a reason for hiding this comment

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

Could you add a test for all regions, please?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
from json import dumps
from os import rmdir
from re import search
from unittest import mock

import botocore
from boto3 import client, resource, session
from colorama import Fore, Style
from freezegun import freeze_time
from mock import patch
from moto import mock_aws
Expand Down Expand Up @@ -1685,3 +1687,114 @@ def test_refresh_credentials_after_expiration(self):

assert len(session_token) == 356
assert search(r"^FQoGZXIvYXdzE.*$", session_token)


def mock_print_boxes(report_lines, report_title):
return report_lines, report_title


class TestPrintCredentials:
@mock.patch("prowler.providers.aws.aws_provider.print_boxes")
def test_print_credentials(self, mock_print_boxes):
from prowler.providers.aws.aws_provider import AwsProvider

mock_self = AwsProvider.__new__(AwsProvider)

mock_self._identity = mock.MagicMock()
mock_self._identity.audited_regions = ["us-east-1", "us-west-2"]
mock_self._identity.profile = "my-profile"
mock_self._identity.account = "123456789012"
mock_self._identity.user_id = "AID1234567890"
mock_self._identity.identity_arn = "arn:aws:iam::123456789012:user/my-user"

mock_self._assumed_role = mock.MagicMock()
mock_self._assumed_role.info.role_arn.arn = (
"arn:aws:sts::123456789012:assumed-role/my-role"
)

mock_self.print_credentials()

expected_lines = [
f"AWS-CLI Profile: {Fore.YELLOW}my-profile{Style.RESET_ALL}",
f"AWS Regions: {Fore.YELLOW}us-east-1, us-west-2{Style.RESET_ALL}",
f"AWS Account: {Fore.YELLOW}123456789012{Style.RESET_ALL}",
f"User Id: {Fore.YELLOW}AID1234567890{Style.RESET_ALL}",
f"Caller Identity ARN: {Fore.YELLOW}arn:aws:iam::123456789012:user/my-user{Style.RESET_ALL}",
f"Assumed Role ARN: {Fore.YELLOW}[arn:aws:sts::123456789012:assumed-role/my-role]{Style.RESET_ALL}",
]

expected_title = (
f"{Style.BRIGHT}Using the AWS credentials below:{Style.RESET_ALL}"
)

mock_print_boxes.assert_called_once_with(expected_lines, expected_title)

@mock.patch("prowler.providers.aws.aws_provider.print_boxes")
def test_print_credentials_no_regions_None(self, mock_print_boxes):
from prowler.providers.aws.aws_provider import AwsProvider

mock_self = AwsProvider.__new__(AwsProvider)

mock_self._identity = mock.MagicMock()
mock_self._identity.audited_regions = None
mock_self._identity.profile = "my-profile"
mock_self._identity.account = "123456789012"
mock_self._identity.user_id = "AID1234567890"
mock_self._identity.identity_arn = "arn:aws:iam::123456789012:user/my-user"

mock_self._assumed_role = mock.MagicMock()
mock_self._assumed_role.info.role_arn.arn = (
"arn:aws:sts::123456789012:assumed-role/my-role"
)

mock_self.print_credentials()

expected_lines = [
f"AWS-CLI Profile: {Fore.YELLOW}my-profile{Style.RESET_ALL}",
f"AWS Regions: {Fore.YELLOW}all{Style.RESET_ALL}",
f"AWS Account: {Fore.YELLOW}123456789012{Style.RESET_ALL}",
f"User Id: {Fore.YELLOW}AID1234567890{Style.RESET_ALL}",
f"Caller Identity ARN: {Fore.YELLOW}arn:aws:iam::123456789012:user/my-user{Style.RESET_ALL}",
f"Assumed Role ARN: {Fore.YELLOW}[arn:aws:sts::123456789012:assumed-role/my-role]{Style.RESET_ALL}",
]

expected_title = (
f"{Style.BRIGHT}Using the AWS credentials below:{Style.RESET_ALL}"
)

mock_print_boxes.assert_called_once_with(expected_lines, expected_title)

@mock.patch("prowler.providers.aws.aws_provider.print_boxes")
def test_print_credentials_no_regions_empty_set(self, mock_print_boxes):
from prowler.providers.aws.aws_provider import AwsProvider

mock_self = AwsProvider.__new__(AwsProvider)

mock_self._identity = mock.MagicMock()
mock_self._identity.audited_regions = set()
mock_self._identity.profile = "my-profile"
mock_self._identity.account = "123456789012"
mock_self._identity.user_id = "AID1234567890"
mock_self._identity.identity_arn = "arn:aws:iam::123456789012:user/my-user"

mock_self._assumed_role = mock.MagicMock()
mock_self._assumed_role.info.role_arn.arn = (
"arn:aws:sts::123456789012:assumed-role/my-role"
)

mock_self.print_credentials()

expected_lines = [
f"AWS-CLI Profile: {Fore.YELLOW}my-profile{Style.RESET_ALL}",
f"AWS Regions: {Fore.YELLOW}all{Style.RESET_ALL}",
f"AWS Account: {Fore.YELLOW}123456789012{Style.RESET_ALL}",
f"User Id: {Fore.YELLOW}AID1234567890{Style.RESET_ALL}",
f"Caller Identity ARN: {Fore.YELLOW}arn:aws:iam::123456789012:user/my-user{Style.RESET_ALL}",
f"Assumed Role ARN: {Fore.YELLOW}[arn:aws:sts::123456789012:assumed-role/my-role]{Style.RESET_ALL}",
]

expected_title = (
f"{Style.BRIGHT}Using the AWS credentials below:{Style.RESET_ALL}"
)

mock_print_boxes.assert_called_once_with(expected_lines, expected_title)