From 5020d67913fe961992225c60a2031943b3dc00ab Mon Sep 17 00:00:00 2001 From: pedrooot Date: Mon, 23 Sep 2024 11:02:46 +0200 Subject: [PATCH 1/3] fix(regions): show all for empty regions --- prowler/providers/aws/aws_provider.py | 1 + 1 file changed, 1 insertion(+) diff --git a/prowler/providers/aws/aws_provider.py b/prowler/providers/aws/aws_provider.py index 80ca0ac6c64..84a2ac548f5 100644 --- a/prowler/providers/aws/aws_provider.py +++ b/prowler/providers/aws/aws_provider.py @@ -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 From a83232eef2d2c128957019051059f40df792f7ed Mon Sep 17 00:00:00 2001 From: pedrooot Date: Mon, 23 Sep 2024 12:20:30 +0200 Subject: [PATCH 2/3] fix(cli): add credentials --- tests/providers/aws/aws_provider_test.py | 43 ++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/tests/providers/aws/aws_provider_test.py b/tests/providers/aws/aws_provider_test.py index cbec76b7a57..f195ac441ae 100644 --- a/tests/providers/aws/aws_provider_test.py +++ b/tests/providers/aws/aws_provider_test.py @@ -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 @@ -1685,3 +1687,44 @@ 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) From 651f179bd700985ea52bdc61e7fa88e2ac562878 Mon Sep 17 00:00:00 2001 From: pedrooot Date: Mon, 23 Sep 2024 14:45:31 +0200 Subject: [PATCH 3/3] fix(regions): resolve comments --- tests/providers/aws/aws_provider_test.py | 70 ++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/tests/providers/aws/aws_provider_test.py b/tests/providers/aws/aws_provider_test.py index f195ac441ae..a55ade130fd 100644 --- a/tests/providers/aws/aws_provider_test.py +++ b/tests/providers/aws/aws_provider_test.py @@ -1728,3 +1728,73 @@ def test_print_credentials(self, mock_print_boxes): ) 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)