@@ -3,7 +3,7 @@ use serde_json::{json, Value};
3
3
use std:: { collections:: HashMap , process:: exit} ;
4
4
5
5
use crate :: utils:: common:: {
6
- bulk_check_hash_exists, insert_job_info, slack_alert, upload_to_defect_dojo,
6
+ bulk_check_hash_exists, get_commit_of_file , insert_job_info, slack_alert, upload_to_defect_dojo,
7
7
} ;
8
8
9
9
use super :: common:: { self , execute_command, print_error, redact_github_token} ;
@@ -194,7 +194,6 @@ pub async fn pipeline_failure(
194
194
195
195
let hashes: Vec < String > = message_to_hash. keys ( ) . cloned ( ) . collect ( ) ;
196
196
let existing_hashes_result = bulk_check_hash_exists ( & hashes, & mongo_uri) . await ;
197
-
198
197
// Handle the Result properly
199
198
let existing_hashes = match existing_hashes_result {
200
199
Ok ( hashes) => hashes,
@@ -446,35 +445,29 @@ pub async fn pipeline_failure(
446
445
let mut secret_result = HashMap :: new ( ) ;
447
446
secret_result. insert (
448
447
"file" ,
449
- result[ "SourceMetadata" ] [ "Data" ] [ "Filesystem" ] [ "file" ] . to_string ( ) ,
448
+ result[ "SourceMetadata" ] [ "Data" ] [ "Filesystem" ] [ "file" ]
449
+ . as_str ( )
450
+ . unwrap_or ( "" )
451
+ . to_string ( ) ,
450
452
) ;
451
453
secret_result. insert ( "line" , number_string) ;
452
- secret_result. insert ( "raw" , result[ "Raw" ] . to_string ( ) ) ;
454
+ secret_result. insert ( "raw" , result[ "Raw" ] . as_str ( ) . unwrap_or ( "" ) . to_string ( ) ) ;
453
455
secret_result. insert (
454
456
"detector_name" ,
455
- result[ "DetectorName" ] . to_string ( ) . to_uppercase ( ) ,
457
+ result[ "DetectorName" ] . as_str ( ) . unwrap_or ( "" ) . to_uppercase ( ) ,
458
+ ) ;
459
+ secret_result. insert (
460
+ "decoder_name" ,
461
+ result[ "DecoderName" ] . as_str ( ) . unwrap_or ( "" ) . to_string ( ) ,
456
462
) ;
457
- secret_result. insert ( "decoder_name" , result[ "DecoderName" ] . to_string ( ) ) ;
458
463
secret_result
459
464
} ;
460
465
secret_results. push ( secret_result) ;
461
- if !detected_detectors. contains (
462
- & result[ "DetectorName" ]
463
- . as_str ( )
464
- . unwrap ( )
465
- . to_string ( )
466
- . to_uppercase ( ) ,
467
- ) {
468
- detected_detectors. push (
469
- result[ "DetectorName" ]
470
- . as_str ( )
471
- . unwrap ( )
472
- . to_string ( )
473
- . to_uppercase ( ) ,
474
- ) ;
466
+ let detector_name = result[ "DetectorName" ] . as_str ( ) . unwrap_or ( "" ) . to_uppercase ( ) ;
467
+ if !detected_detectors. contains ( & detector_name) {
468
+ detected_detectors. push ( detector_name) ;
475
469
}
476
470
}
477
-
478
471
detected_detectors = detected_detectors
479
472
. iter ( )
480
473
. map ( |x| x. to_string ( ) )
@@ -493,24 +486,30 @@ pub async fn pipeline_failure(
493
486
}
494
487
495
488
let mut secret_count = 0 ;
496
- let mut message_to_hash: HashMap < String , ( String , String , String , String ) > = HashMap :: new ( ) ;
489
+ let mut message_to_hash: HashMap < String , ( String , String , String , String , String ) > =
490
+ HashMap :: new ( ) ;
497
491
498
492
// Collect all secret records and their hashes
499
493
for value in secret_results. clone ( ) {
494
+ // Append to slack alert message, remove first 2 values after split with "/"
495
+ let file_commit = get_commit_of_file ( & value[ "file" ] ) ;
496
+ let commit_base_link = commit_path. split ( "/commit" ) . collect :: < Vec < & str > > ( ) [ 0 ] ;
497
+ let commit_link = format ! ( "{}/commit/{}" , commit_base_link, file_commit. unwrap( ) ) ;
500
498
let vuln_record = format ! (
501
- "\n \n File: {}\n Line: {}\n Raw: {}\n Detector Name: {}" ,
502
- value[ "file" ] , value[ "line" ] , value[ "raw" ] , value[ "detector_name" ]
499
+ "\n \n File: {}\n Line: {}\n Raw: {}\n Detector Name: {}\n Commit: {} " ,
500
+ value[ "file" ] , value[ "line" ] , value[ "raw" ] , value[ "detector_name" ] , commit_link
503
501
) ;
504
502
let hashed_message = common:: hash_text ( & vuln_record) ;
505
503
506
504
// Collect messages and their hashes along with other details
507
505
message_to_hash. insert (
508
506
hashed_message,
509
507
(
510
- value[ "file" ] . replace ( " \" " , "" ) ,
508
+ value[ "file" ] . clone ( ) ,
511
509
value[ "line" ] . clone ( ) ,
512
- value[ "raw" ] . replace ( "\" " , "" ) ,
513
- value[ "detector_name" ] . replace ( "\" " , "" ) ,
510
+ value[ "raw" ] . clone ( ) ,
511
+ value[ "detector_name" ] . clone ( ) ,
512
+ commit_link,
514
513
) ,
515
514
) ;
516
515
}
@@ -529,10 +528,8 @@ pub async fn pipeline_failure(
529
528
} ;
530
529
531
530
let mut secret_count = 0 ;
532
- let mut found_secret_issues = false ;
533
-
534
531
// Process each message to check for existence and add to the table
535
- for ( hashed_message, ( file, line, raw, detector_name) ) in message_to_hash {
532
+ for ( hashed_message, ( file, line, raw, detector_name, commit_link ) ) in message_to_hash {
536
533
if !existing_hashes. contains ( & hashed_message) {
537
534
found_secret_issues = true ;
538
535
secret_count += 1 ;
@@ -545,10 +542,9 @@ pub async fn pipeline_failure(
545
542
// Add row to table
546
543
table. add_row ( row ! [ secret_count, file, line, raw_truncated, detector_name] ) ;
547
544
548
- // Append to slack alert message
549
545
slack_alert_msg. push_str ( & format ! (
550
- "\n \n File: {}\n Line: {}\n Raw: {}\n Detector Name: {}" ,
551
- file, line, raw, detector_name
546
+ "\n \n File: {}\n Line: {}\n Raw: {}\n Detector Name: {}\n Commit: {} " ,
547
+ file, line, raw, detector_name, commit_link
552
548
) ) ;
553
549
554
550
// Register the missing hash
@@ -617,7 +613,6 @@ pub async fn pipeline_failure(
617
613
. collect :: < Vec < String > > ( ) ;
618
614
pipeline_secret_license_data. insert ( "licenses" , licenses_list) ;
619
615
}
620
-
621
616
if found_sast_issues == false
622
617
&& found_sca_issues == false
623
618
&& found_secret_issues == false
@@ -1101,7 +1096,7 @@ pub async fn pipeline_failure(
1101
1096
println ! ( "\t \t Job ID: {}" , job_id) ;
1102
1097
if !mongo_uri. is_empty ( ) {
1103
1098
println ! ( "\t \t Inserting job info into MongoDB" ) ;
1104
- insert_job_info (
1099
+ let _ = insert_job_info (
1105
1100
& mongo_uri,
1106
1101
& job_id,
1107
1102
& pipeline_failure_reason,
@@ -1130,7 +1125,7 @@ pub async fn pipeline_failure(
1130
1125
println ! ( "\t \t Job ID: {}" , job_id) ;
1131
1126
if !mongo_uri. is_empty ( ) {
1132
1127
println ! ( "\t \t Inserting job info into MongoDB" ) ;
1133
- insert_job_info (
1128
+ let _ = insert_job_info (
1134
1129
& mongo_uri,
1135
1130
& job_id,
1136
1131
& pipeline_failure_reason,
@@ -1165,7 +1160,7 @@ pub async fn pipeline_failure(
1165
1160
println ! ( "[+] No issues found in scan results, so slack alert is not sent" ) ;
1166
1161
}
1167
1162
}
1168
- insert_job_info (
1163
+ let _ = insert_job_info (
1169
1164
& mongo_uri,
1170
1165
& job_id,
1171
1166
"No policy file provided, skipping policy check" ,
@@ -1236,6 +1231,7 @@ pub async fn pipeline_failure(
1236
1231
println ! ( "[+] Could not upload SARIF report to Defect Dojo because of missing configuration - defectdojo-token, defectdojo-url, product-name, engagement-name" ) ;
1237
1232
}
1238
1233
}
1234
+
1239
1235
pub async fn get_commit_info (
1240
1236
start_line : u64 ,
1241
1237
end_line : u64 ,
@@ -1302,6 +1298,7 @@ pub async fn get_commit_info(
1302
1298
} )
1303
1299
}
1304
1300
// Function to fetch commit information from GitHub API
1301
+ // Function to fetch commit information from GitHub API
1305
1302
async fn get_commit_info_from_github ( path : & str , repo_url_with_pat : & str ) -> Option < Value > {
1306
1303
// Parse the repository URL with PAT
1307
1304
println ! ( "Fetching commit info from GitHub API for {}" , path) ;
0 commit comments