def test_load_file_search(self): '''Check that the recursive file detection is working as expected''' scan_result_proto = engine_pb2.LaunchToolResponse() cons = ExampleConsumer(self.config) collected_results = cons.load_files(scan_result_proto, self.tmp_root_dir) self.assertEqual(len(collected_results), 2)
def setUp(self): self.config = Config() scan_start_time = Timestamp() scan_start_time.FromJsonString("1991-01-01T00:00:00Z") scan_info = engine_pb2.ScanInfo( scan_start_time=scan_start_time, scan_uuid='dd1794f2-544d-456b-a45a-a2bec53633b1' ) scan_results = engine_pb2.LaunchToolResponse( scan_info=scan_info ) scan_results.tool_name = 'unit_tests' issue = issue_pb2.Issue() issue.target = 'target.py:0' issue.type = "test" issue.title = "test title" issue.cvss = 2.0 issue.description = "test.description" issue.severity = issue_pb2.Severity.SEVERITY_LOW issue.confidence = issue_pb2.Confidence.CONFIDENCE_LOW scan_results.issues.extend([issue]) first_seen = Timestamp() first_seen.FromJsonString("1992-02-02T00:00:00Z") enriched_issue = issue_pb2.EnrichedIssue(first_seen=first_seen) enriched_issue.raw_issue.CopyFrom(issue) enriched_issue.count = 2 enriched_issue.false_positive = True enriched_scan_results = engine_pb2.EnrichedLaunchToolResponse( original_results=scan_results, ) enriched_scan_results.issues.extend([enriched_issue]) self.enriched_dtemp = tempfile.mkdtemp( prefix="enriched_", dir=self.config.pvc_location) self.enriched, _ = tempfile.mkstemp( prefix="enriched_", dir=self.enriched_dtemp, suffix=".pb") self.raw_dtemp = tempfile.mkdtemp( prefix="raw_", dir=self.config.pvc_location) self.raw, _ = tempfile.mkstemp( prefix="raw_", dir=self.raw_dtemp, suffix=".pb") f = open(self.enriched, "wb") scan_proto_string = enriched_scan_results.SerializeToString() f.write(scan_proto_string) f.close() f = open(self.raw, "wb") scan_proto_string = scan_results.SerializeToString() f.write(scan_proto_string) f.close()
def test_load_file_proto_read(self): '''Test we can load protos and read from them correctly Also ensures we handled malformed protobufs gracefully ''' scan_result_proto = engine_pb2.LaunchToolResponse() cons = ExampleConsumer(self.config) collected_results = cons.load_files(scan_result_proto, self.tmp_root_dir) result = collected_results.pop() self.assertEqual(result.scan_info.scan_uuid, 'dd1794f2-544d-456b-a45a-a2bec53633b1')
def _load_plain_results(self): scan_results = engine_pb2.LaunchToolResponse() return self.load_files(scan_results, self.pvc_location)
def setUp(self): self.dojo_url = 'http://dojo.local/' self.dojo_api_key = '' self.config = ConsumerMockConfig() self.config.dojo_url = self.dojo_url self.config.api_key = self.dojo_api_key self.config.dojo_user = '******' self.config.dojo_user_id = '1' self.config.dojo_product = 1 self.config.dojo_engagement = 1 self.config.raw = False scan_start_time = Timestamp() scan_start_time.FromJsonString("1991-01-01T00:00:00Z") scan_info = engine_pb2.ScanInfo( scan_start_time=scan_start_time, scan_uuid='dd1794f2-544d-456b-a45a-a2bec53633b1' ) scan_results = engine_pb2.LaunchToolResponse( scan_info=scan_info ) scan_results.tool_name = 'unit_tests' # Raw results issue = issue_pb2.Issue() issue.target = 'target.py:0' issue.type = "test" issue.title = "test title" issue.cvss = 2.0 issue.description = "test.description" issue.severity = issue_pb2.Severity.SEVERITY_LOW issue.confidence = issue_pb2.Confidence.CONFIDENCE_LOW scan_results.issues.extend([issue]) # Enriched, duplicate and False Positive results first_seen = Timestamp() first_seen.FromJsonString("1992-02-02T00:00:00Z") enriched_issue = issue_pb2.EnrichedIssue(first_seen=first_seen) enriched_issue.raw_issue.CopyFrom(issue) enriched_issue.count = 2 enriched_issue.false_positive = True enriched_scan_results = engine_pb2.EnrichedLaunchToolResponse( original_results=scan_results, ) enriched_scan_results.issues.extend([enriched_issue]) # Enriched, unique, false positive result enriched_issue.count = 0 enriched_issue.false_positive = True issue.target = 'target0.py:0' issue.type = "test0" issue.title = "test0 title0" enriched_issue.raw_issue.CopyFrom(issue) enriched_scan_results.issues.extend([enriched_issue]) # Enriched, unique, true positive result enriched_scan_results.issues.extend([enriched_issue]) enriched_issue.count = 0 enriched_issue.false_positive = False issue.target = 'target1.py:0' issue.type = "test1" issue.title = "test1 title1" enriched_issue.raw_issue.CopyFrom(issue) enriched_scan_results.issues.extend([enriched_issue]) self.enriched_dtemp = tempfile.mkdtemp( prefix="enriched_", dir=self.config.pvc_location) self.enriched, _ = tempfile.mkstemp( prefix="enriched_", dir=self.enriched_dtemp, suffix=".pb") self.raw_dtemp = tempfile.mkdtemp( prefix="raw_", dir=self.config.pvc_location) self.raw, _ = tempfile.mkstemp( prefix="raw_", dir=self.raw_dtemp, suffix=".pb") f = open(self.enriched, "wb") scan_proto_string = enriched_scan_results.SerializeToString() f.write(scan_proto_string) f.close() f = open(self.raw, "wb") scan_proto_string = scan_results.SerializeToString() f.write(scan_proto_string) f.close()
def setUp(self): self.config = { 'dry_run': True, 'es_index': 'dracon', 'es_url': 'https://some_test.url.somewhere.io:443', 'pvc_location': './' } # Create an scan results object and serialize it to a file ts = Timestamp() ts.FromJsonString("1991-01-01T00:00:00Z") scan_results = engine_pb2.LaunchToolResponse( scan_info=engine_pb2.ScanInfo( scan_uuid='dd1794f2-544d-456b-a45a-a2bec53633b1', scan_start_time=ts, ), tool_name='bandit', ) issue = issue_pb2.Issue() issue.target = 'target.py:0' scan_results.issues.extend([issue]) enriched_scan_results = engine_pb2.EnrichedLaunchToolResponse( original_results=scan_results, ) f = open(self.config['pvc_location'] + "example_response.pb", "wb") scan_proto_string = enriched_scan_results.SerializeToString() f.write(scan_proto_string) f.close() scan_results = engine_pb2.LaunchToolResponse( scan_info=engine_pb2.ScanInfo( scan_uuid='dd1794f2-544d-456b-a45a-a2bec53633b1', ), tool_name='bandit', ) self.tmp_root_dir = tempfile.mkdtemp() _, self.tmpfile = tempfile.mkstemp(suffix=".pb", prefix="example_response_", dir=self.tmp_root_dir) with open(self.tmpfile, "wb") as f: serialized_proto = scan_results.SerializeToString() f.write(serialized_proto) # Duplicate the serialized protobuf into a subfolder to check recursion self.tmp_subdir = tempfile.mkdtemp(dir=self.tmp_root_dir) _, self.tmpfile2 = tempfile.mkstemp(suffix=".pb", prefix="example_response_copy_", dir=self.tmp_subdir) with open(self.tmpfile2, "wb") as f: serialized_proto = scan_results.SerializeToString() f.write(serialized_proto) # Create a malformed protobuf to check we handle it gracefully malformed_proto = serialized_proto[10:] _, self.malformed = tempfile.mkstemp(suffix=".pb", prefix="malformed_", dir=self.tmp_root_dir) with open(self.malformed, "wb") as f: f.write(malformed_proto) print(self.tmp_root_dir, self.tmp_subdir, self.tmpfile, self.tmpfile2, self.malformed)