예제 #1
0
    def test_record_relative_path_with_abs_file(self):

        # test whether the record's repo_file_path is correct, relative to the CWD (with a / at the start).

        # this is just constructing the scan dir as normal
        current_dir = os.path.dirname(os.path.realpath(__file__))
        scan_file_path = os.path.join(current_dir, "resources",
                                      "serverless.yml")

        file_rel_path = os.path.relpath(scan_file_path)
        file_abs_path = os.path.abspath(scan_file_path)

        runner = Runner()
        checks_allowlist = ['CKV_AWS_49']
        report = runner.run(root_folder=None,
                            external_checks_dir=None,
                            files=[file_abs_path],
                            runner_filter=RunnerFilter(
                                framework='serverless',
                                checks=checks_allowlist))

        all_checks = report.failed_checks + report.passed_checks
        self.assertGreater(
            len(all_checks),
            0)  # ensure that the assertions below are going to do something
        for record in all_checks:
            # no need to join with a '/' because the CFN runner adds it to the start of the file path
            self.assertEqual(record.repo_file_path, f'/{file_rel_path}')
예제 #2
0
    def test_record_relative_path_with_relative_dir(self):

        # test whether the record's repo_file_path is correct, relative to the CWD (with a / at the start).

        # this is just constructing the scan dir as normal
        current_dir = os.path.dirname(os.path.realpath(__file__))
        scan_dir_path = os.path.join(current_dir, "resources")

        # this is the relative path to the directory to scan (what would actually get passed to the -d arg)
        dir_rel_path = os.path.relpath(scan_dir_path).replace('\\', '/')

        runner = Runner()
        checks_allowlist = ['CKV_AWS_49']
        report = runner.run(root_folder=dir_rel_path,
                            external_checks_dir=None,
                            runner_filter=RunnerFilter(
                                framework='serverless',
                                checks=checks_allowlist))

        all_checks = report.failed_checks + report.passed_checks
        self.assertGreater(
            len(all_checks),
            0)  # ensure that the assertions below are going to do something
        for record in all_checks:
            # no need to join with a '/' because the CFN runner adds it to the start of the file path
            self.assertEqual(record.repo_file_path,
                             f'/{dir_rel_path}{record.file_path}')
예제 #3
0
    def helper(self, check):
        runner = Runner()
        current_dir = os.path.dirname(os.path.realpath(__file__))

        test_files_dir = current_dir + "/example_CheckTypes"
        report = runner.run(root_folder=test_files_dir, runner_filter=RunnerFilter(checks=[check.id]))
        summary = report.get_summary()

        self.assertEqual(summary['passed'], 1)
        self.assertEqual(summary['failed'], 0)
        self.assertEqual(summary['skipped'], 0)
        self.assertEqual(summary['parsing_errors'], 0)
예제 #4
0
    def test_provider_function_att_type_mismatch(self):
        runner = Runner()
        current_dir = os.path.dirname(os.path.realpath(__file__))
        scan_file_path = os.path.join(current_dir, "resources",
                                      "serverless.yaml")
        file_abs_path = os.path.abspath(scan_file_path)

        report = runner.run(files=[file_abs_path],
                            runner_filter=RunnerFilter(framework='serverless'),
                            root_folder="")
        self.assertEqual(0, len(report.parsing_errors))
        self.assertLess(0, len(report.passed_checks + report.failed_checks))
예제 #5
0
    def test_summary(self):
        runner = Runner()
        current_dir = os.path.dirname(os.path.realpath(__file__))

        test_files_dir = current_dir + "/example_StarActionPolicyDocument"
        report = runner.run(root_folder=test_files_dir,
                            runner_filter=RunnerFilter(checks=[check.id]))
        summary = report.get_summary()

        self.assertEqual(summary['passed'], 1)
        self.assertEqual(summary['failed'], 2)
        self.assertEqual(summary['skipped'], 0)
        self.assertEqual(summary['parsing_errors'], 0)
예제 #6
0
    def test_summary(self):
        runner = Runner()
        current_dir = os.path.dirname(os.path.realpath(__file__))

        check = ServerlessCheck()

        test_files_dir = current_dir + "/example_WildcardEntities"
        report = runner.run(root_folder=test_files_dir, runner_filter=RunnerFilter(checks=[check.id]))
        summary = report.get_summary()

        registry.wildcard_checks['AWS::S3*'].remove(check)

        self.assertEqual(summary['passed'], 1)
        self.assertEqual(summary['failed'], 0)
        self.assertEqual(summary['skipped'], 0)
        self.assertEqual(summary['parsing_errors'], 0)
예제 #7
0
    def test_summary(self):
        runner = Runner()
        current_dir = os.path.dirname(os.path.realpath(__file__))

        # Used in
        os.environ["sneaky_var"] = "*"

        test_files_dir = current_dir + "/example_AdminPolicyDocument"
        report = runner.run(root_folder=test_files_dir,
                            runner_filter=RunnerFilter(checks=[check.id]))
        summary = report.get_summary()

        self.assertEqual(
            summary['passed'], 1,
            f"Passed checks: {[fc.file_path for fc in report.passed_checks]}")
        self.assertEqual(
            summary['failed'], 6,
            f"Failed checks: {[fc.file_path for fc in report.failed_checks]}")
        self.assertEqual(
            summary['skipped'], 0,
            f"Skipped checks: {[fc.file_path for fc in report.skipped_checks]}"
        )
        self.assertEqual(summary['parsing_errors'], 0)