From 130b2fc97b505b8bdc44ffc5f28dc2d6f0757821 Mon Sep 17 00:00:00 2001 From: ipeleg Date: Tue, 17 Oct 2023 12:11:51 +0300 Subject: [PATCH 1/4] giving file path on relative the the current dir for cases there is no either specifirv root_folder and the is no repo scan dir --- checkov/sca_package_2/runner.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/checkov/sca_package_2/runner.py b/checkov/sca_package_2/runner.py index d68525f2800..06caaa53a44 100644 --- a/checkov/sca_package_2/runner.py +++ b/checkov/sca_package_2/runner.py @@ -140,6 +140,13 @@ def run( return report + def _persist_file_if_required(self, package_files_to_persist: List[FileToPersist], + file_path: Path, root_path: Path | None) -> None: + if file_path.name in SCANNABLE_PACKAGE_FILES or file_path.suffix in SCANNABLE_PACKAGE_FILES_EXTENSIONS: + file_path_str = str(file_path) + # in case of root_path is None, we will get the path in related to the current work dir + package_files_to_persist.append(FileToPersist(file_path_str, os.path.relpath(file_path_str, root_path))) + def upload_package_files( self, root_path: Path | None, @@ -154,21 +161,18 @@ def upload_package_files( try: if root_path: for file_path in root_path.glob("**/*"): - if (file_path.name in SCANNABLE_PACKAGE_FILES or file_path.suffix in SCANNABLE_PACKAGE_FILES_EXTENSIONS) and not any( - p in file_path.parts for p in excluded_paths) and file_path.name not in excluded_file_names: - file_path_str = str(file_path) - package_files_to_persist.append( - FileToPersist(file_path_str, os.path.relpath(file_path_str, root_path))) + if any(p in file_path.parts for p in excluded_paths) or file_path.name in excluded_file_names: + logging.debug(f"[sca_package:runner](upload_package_files) - File {file_path} was excluded") + continue + self._persist_file_if_required(package_files_to_persist, file_path, root_path) if files: - root_folder = os.path.split(os.path.commonprefix(files))[0] for file in files: file_path = Path(file) if not file_path.exists(): - logging.warning(f"File {file_path} doesn't exist") + logging.warning(f"[sca_package:runner](upload_package_files) - File {file_path} doesn't exist") continue - if file_path.name in SCANNABLE_PACKAGE_FILES or file_path.suffix in SCANNABLE_PACKAGE_FILES_EXTENSIONS: - package_files_to_persist.append(FileToPersist(file, os.path.relpath(file, root_folder))) + self._persist_file_if_required(package_files_to_persist, file_path, root_path) logging.info(f"{len(package_files_to_persist)} sca package files found.") bc_integration.persist_files(package_files_to_persist) From a4a4b2255c6207af15bf9d90a333ef89fa3d357c Mon Sep 17 00:00:00 2001 From: ipeleg Date: Tue, 17 Oct 2023 12:40:39 +0300 Subject: [PATCH 2/4] adjusting the tests --- tests/sca_package_2/test_runner.py | 73 +++++++++++++++++------------- 1 file changed, 41 insertions(+), 32 deletions(-) diff --git a/tests/sca_package_2/test_runner.py b/tests/sca_package_2/test_runner.py index 8e5fc4b61c8..abad65b06b5 100644 --- a/tests/sca_package_2/test_runner.py +++ b/tests/sca_package_2/test_runner.py @@ -1,3 +1,4 @@ +import os from pathlib import Path from mock.mock import MagicMock @@ -74,40 +75,48 @@ def test_upload_scannable_files_exclude_go_and_requirements(): def test_upload_scannable_files_file_config(): - # when - input_output_paths = Runner().upload_package_files( - root_path=None, - files=[ - str(EXAMPLES_DIR / 'requirements.txt'), - str(EXAMPLES_DIR / 'go.sum'), - str(EXAMPLES_DIR / 'package-lock.json'), - str(EXAMPLES_DIR / 'package.json'), - str(EXAMPLES_DIR / 'go.mod'), - str(EXAMPLES_DIR / 'Microsoft.NET.Sdk.csproj') - ], - excluded_paths=set(), - excluded_file_names=set() - ) - # expected - expected_output = { - FileToPersist(full_file_path=str(EXAMPLES_DIR / 'requirements.txt'), - s3_file_key='requirements.txt'), - FileToPersist(full_file_path=str(EXAMPLES_DIR / 'go.sum'), - s3_file_key='go.sum'), - FileToPersist(full_file_path=str(EXAMPLES_DIR / 'package-lock.json'), - s3_file_key='package-lock.json'), - FileToPersist(full_file_path=str(EXAMPLES_DIR / 'package.json'), - s3_file_key='package.json'), - FileToPersist(full_file_path=str(EXAMPLES_DIR / 'go.mod'), - s3_file_key='go.mod'), - FileToPersist(full_file_path=str(EXAMPLES_DIR / 'Microsoft.NET.Sdk.csproj'), - s3_file_key='Microsoft.NET.Sdk.csproj') - } + origin_cwd = os.getcwd() + try: + # setup + os.chdir(str(Path(__file__).parent)) + + # when + input_output_paths = Runner().upload_package_files( + root_path=None, + files=[ + str(EXAMPLES_DIR / 'requirements.txt'), + str(EXAMPLES_DIR / 'go.sum'), + str(EXAMPLES_DIR / 'package-lock.json'), + str(EXAMPLES_DIR / 'package.json'), + str(EXAMPLES_DIR / 'go.mod'), + str(EXAMPLES_DIR / 'Microsoft.NET.Sdk.csproj') + ], + excluded_paths=set(), + excluded_file_names=set() + ) + # expected (paths are in related to the test-working-dir) + expected_output = { + FileToPersist(full_file_path=str(EXAMPLES_DIR / 'requirements.txt'), + s3_file_key='examples/requirements.txt'), + FileToPersist(full_file_path=str(EXAMPLES_DIR / 'go.sum'), + s3_file_key='examples/go.sum'), + FileToPersist(full_file_path=str(EXAMPLES_DIR / 'package-lock.json'), + s3_file_key='examples/package-lock.json'), + FileToPersist(full_file_path=str(EXAMPLES_DIR / 'package.json'), + s3_file_key='examples/package.json'), + FileToPersist(full_file_path=str(EXAMPLES_DIR / 'go.mod'), + s3_file_key='examples/go.mod'), + FileToPersist(full_file_path=str(EXAMPLES_DIR / 'Microsoft.NET.Sdk.csproj'), + s3_file_key='examples/Microsoft.NET.Sdk.csproj') + } - # then - assert len(input_output_paths) == 6 + # then + assert len(input_output_paths) == 6 - assert set(input_output_paths) == expected_output + assert set(input_output_paths) == expected_output + finally: + # teardown + os.chdir(origin_cwd) def test_run(sca_package_2_report): From 2a91f4a1893d678e912137c215ff02d61bb75e41 Mon Sep 17 00:00:00 2001 From: ipeleg Date: Tue, 17 Oct 2023 15:35:48 +0300 Subject: [PATCH 3/4] commit --- checkov/sca_package_2/runner.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/checkov/sca_package_2/runner.py b/checkov/sca_package_2/runner.py index 06caaa53a44..c0bacaad8e7 100644 --- a/checkov/sca_package_2/runner.py +++ b/checkov/sca_package_2/runner.py @@ -181,4 +181,4 @@ def upload_package_files( logging.debug("Unexpected failure happened during uploading files for package scanning.\n" "the scanning is terminating. details are below.\n" "please try again. if it is repeated, please report.", exc_info=True) - return None + return None \ No newline at end of file From 3a5d1f3b0aa5e8ce90d79742bfebf6c0f6eb4e8e Mon Sep 17 00:00:00 2001 From: ipeleg Date: Tue, 17 Oct 2023 15:46:45 +0300 Subject: [PATCH 4/4] commit --- checkov/sca_package_2/runner.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/checkov/sca_package_2/runner.py b/checkov/sca_package_2/runner.py index c0bacaad8e7..06caaa53a44 100644 --- a/checkov/sca_package_2/runner.py +++ b/checkov/sca_package_2/runner.py @@ -181,4 +181,4 @@ def upload_package_files( logging.debug("Unexpected failure happened during uploading files for package scanning.\n" "the scanning is terminating. details are below.\n" "please try again. if it is repeated, please report.", exc_info=True) - return None \ No newline at end of file + return None