From 92cc2c8e6914ba58c037892df4d821cd35bf0aac Mon Sep 17 00:00:00 2001 From: Sergio Garcia <38561120+sergargar@users.noreply.github.com> Date: Tue, 10 Jan 2023 17:06:14 +0100 Subject: [PATCH] fix(config): path error in Windows environment (#1684) Co-authored-by: sergargar --- prowler/config/config.py | 11 ++++++----- prowler/providers/aws/aws_provider.py | 8 +++++--- tests/lib/check/check_test.py | 12 ++++++------ tests/lib/outputs/outputs_test.py | 6 +++--- 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/prowler/config/config.py b/prowler/config/config.py index b8744642..79f7b1cb 100644 --- a/prowler/config/config.py +++ b/prowler/config/config.py @@ -1,4 +1,4 @@ -import os +import pathlib from datetime import datetime, timezone from os import getcwd @@ -30,7 +30,8 @@ csv_file_suffix = ".csv" json_file_suffix = ".json" json_asff_file_suffix = ".asff.json" html_file_suffix = ".html" -config_yaml = f"{os.path.dirname(os.path.realpath(__file__))}/config.yaml" +config_yaml = f"{pathlib.Path().absolute()}/prowler/config/config.yaml" +print(config_yaml) def change_config_var(variable, value): @@ -59,10 +60,10 @@ def get_config_var(variable): def get_aws_available_regions(): try: - actual_directory = ("/").join( - os.path.dirname(os.path.realpath(__file__)).split("/")[:-1] + actual_directory = pathlib.Path().absolute() + f = open_file( + f"{actual_directory}/prowler/providers/aws/{aws_services_json_file}" ) - f = open_file(f"{actual_directory}/providers/aws/{aws_services_json_file}") data = parse_json_file(f) regions = set() diff --git a/prowler/providers/aws/aws_provider.py b/prowler/providers/aws/aws_provider.py index 9bab46dc..195d5689 100644 --- a/prowler/providers/aws/aws_provider.py +++ b/prowler/providers/aws/aws_provider.py @@ -1,4 +1,4 @@ -import os +import pathlib import sys from boto3 import session @@ -108,8 +108,10 @@ def generate_regional_clients( try: regional_clients = {} # Get json locally - actual_directory = os.path.dirname(os.path.realpath(__file__)) - f = open_file(f"{actual_directory}/{aws_services_json_file}") + actual_directory = pathlib.Path().absolute() + f = open_file( + f"{actual_directory}/prowler/providers/aws/{aws_services_json_file}" + ) data = parse_json_file(f) # Check if it is a subservice json_regions = data["services"][service]["regions"][ diff --git a/tests/lib/check/check_test.py b/tests/lib/check/check_test.py index 6d37871e..8be79dec 100644 --- a/tests/lib/check/check_test.py +++ b/tests/lib/check/check_test.py @@ -1,4 +1,4 @@ -import os +import pathlib from importlib.machinery import FileFinder from pkgutil import ModuleInfo @@ -107,7 +107,7 @@ class Test_Check: test_cases = [ { "input": { - "metadata_path": f"{os.path.dirname(os.path.realpath(__file__))}/fixtures/metadata.json", + "metadata_path": f"{pathlib.Path().absolute()}/tests/lib/check/fixtures/metadata.json", }, "expected": { "CheckID": "iam_disable_30_days_credentials", @@ -129,7 +129,7 @@ class Test_Check: test_cases = [ { "input": { - "path": f"{os.path.dirname(os.path.realpath(__file__))}/fixtures/checklistA.json", + "path": f"{pathlib.Path().absolute()}/tests/lib/check/fixtures/checklistA.json", "provider": "aws", }, "expected": {"check11", "check12", "check7777"}, @@ -263,7 +263,7 @@ class Test_Check: # } # with mock.patch( # "prowler.lib.check.check.compliance_specification_dir_path", - # new=f"{os.path.dirname(os.path.realpath(__file__))}/fixtures", + # new=f"{pathlib.Path().absolute()}/fixtures", # ): # provider = "aws" # bulk_compliance_frameworks = bulk_load_compliance_frameworks(provider) @@ -286,7 +286,7 @@ class Test_Check: # } # with mock.patch( # "prowler.lib.check.check.compliance_specification_dir", - # new=f"{os.path.dirname(os.path.realpath(__file__))}/fixtures", + # new=f"{pathlib.Path().absolute()}/fixtures", # ): # provider = "aws" # bulk_compliance_frameworks = bulk_load_compliance_frameworks(provider) @@ -305,7 +305,7 @@ class Test_Check: # } # with mock.patch( # "prowler.lib.check.check.compliance_specification_dir", - # new=f"{os.path.dirname(os.path.realpath(__file__))}/fixtures", + # new=f"{pathlib.Path().absolute()}/fixtures", # ): # provider = "aws" # bulk_compliance_frameworks = bulk_load_compliance_frameworks(provider) diff --git a/tests/lib/outputs/outputs_test.py b/tests/lib/outputs/outputs_test.py index f00f2f6c..77480ed0 100644 --- a/tests/lib/outputs/outputs_test.py +++ b/tests/lib/outputs/outputs_test.py @@ -1,4 +1,4 @@ -import os +import pathlib from os import path, remove from unittest import mock @@ -66,7 +66,7 @@ def mock_make_api_call(self, operation_name, kwarg): class Test_Outputs: def test_fill_file_descriptors(self): audited_account = AWS_ACCOUNT_ID - output_directory = f"{os.path.dirname(os.path.realpath(__file__))}" + output_directory = f"{pathlib.Path().absolute()}" audit_info = AWS_Audit_Info( original_session=None, audit_session=None, @@ -334,7 +334,7 @@ class Test_Outputs: # Create mock csv output file fixtures_dir = "fixtures" output_directory = ( - f"{os.path.dirname(os.path.realpath(__file__))}/{fixtures_dir}" + f"{pathlib.Path().absolute()}/tests/lib/outputs/{fixtures_dir}" ) output_mode = "csv" filename = f"prowler-output-{input_audit_info.audited_account}"