1
-
2
1
use std:: { collections:: HashMap , time:: Instant } ;
3
2
4
3
use mongodb:: bson:: uuid;
5
4
use serde_json:: json;
6
5
7
- use crate :: { utils:: { common:: { execute_command, post_json_data} , file_utils:: find_files_recursively} , scans:: tools:: sca_tool:: SUPPORTED_MANIFESTS } ;
8
-
6
+ use crate :: {
7
+ scans:: tools:: sca_tool:: SUPPORTED_MANIFESTS ,
8
+ utils:: { common:: execute_command, file_utils:: find_files_recursively} ,
9
+ } ;
9
10
10
11
pub struct LicenseTool ;
11
12
12
13
impl LicenseTool {
13
14
pub fn new ( ) -> Self {
14
15
LicenseTool
15
16
}
16
-
17
- pub async fn run_scan ( & self , _path : & str , _commit_id : Option < & str > , _branch : Option < & str > , verbose : bool ) {
17
+
18
+ pub async fn run_scan (
19
+ & self ,
20
+ _path : & str ,
21
+ _commit_id : Option < & str > ,
22
+ _branch : Option < & str > ,
23
+ verbose : bool ,
24
+ ) {
18
25
let start_time = Instant :: now ( ) ;
19
26
if verbose {
20
27
println ! ( "[+] Running License compliance scan on path: {}" , _path) ;
@@ -33,11 +40,11 @@ impl LicenseTool {
33
40
if let Some ( _branch) = _branch {
34
41
let clone_command = format ! ( "git clone -b {} {} /tmp/app" , _branch, _path) ;
35
42
execute_command ( & clone_command, false ) . await ;
36
- } else {
43
+ } else {
37
44
let clone_command = format ! ( "git clone {} /tmp/app" , _path) ;
38
45
execute_command ( & clone_command, false ) . await ;
39
46
}
40
- } else {
47
+ } else {
41
48
if verbose {
42
49
println ! ( "[+] Copying project to /tmp/app..." ) ;
43
50
}
@@ -58,20 +65,27 @@ impl LicenseTool {
58
65
// now run secret scan on /tmp/code folder
59
66
_path = format ! ( "/tmp/code" ) ;
60
67
}
61
- let manifests = find_files_recursively ( & _path, unsafe { SUPPORTED_MANIFESTS . to_vec ( ) } , ignore_dirs) . await ;
68
+ let manifests =
69
+ find_files_recursively ( & _path, unsafe { SUPPORTED_MANIFESTS . to_vec ( ) } , ignore_dirs)
70
+ . await ;
62
71
let mut manifest_license = HashMap :: new ( ) ;
63
72
for manifest in manifests. iter ( ) {
64
73
let file_name = manifest. split ( "/" ) . last ( ) . unwrap ( ) ;
65
74
let folder_path = manifest. replace ( file_name, "" ) ;
66
75
let random_file_name = format ! ( "{}.json" , uuid:: Uuid :: new( ) . to_string( ) ) ;
67
76
// if manifest ends with pom.xml then pass -t java otherwise nothing
68
- let mut license_command = format ! ( "cd {} && cdxgen -o {}" , folder_path, random_file_name) ;
77
+ let mut license_command =
78
+ format ! ( "cd {} && cdxgen -o {}" , folder_path, random_file_name) ;
69
79
if file_name. ends_with ( "pom.xml" ) {
70
- license_command = format ! ( "cd {} && cdxgen -o {} -t java" , folder_path, random_file_name) ;
80
+ license_command = format ! (
81
+ "cd {} && cdxgen -o {} -t java" ,
82
+ folder_path, random_file_name
83
+ ) ;
71
84
}
72
85
execute_command ( & license_command, false ) . await ;
73
86
// Read JSON file and parse data
74
- let license_json = std:: fs:: read_to_string ( format ! ( "{}/{}" , folder_path, random_file_name) ) . unwrap ( ) ;
87
+ let license_json =
88
+ std:: fs:: read_to_string ( format ! ( "{}/{}" , folder_path, random_file_name) ) . unwrap ( ) ;
75
89
let json_data = serde_json:: from_str :: < serde_json:: Value > ( & license_json) . unwrap ( ) ;
76
90
// extract license data from "components" key there will be list of components so grab licenses from there
77
91
let components = json_data[ "components" ] . as_array ( ) . unwrap ( ) ;
@@ -87,8 +101,14 @@ impl LicenseTool {
87
101
license_names. push ( license[ "id" ] . as_str ( ) . unwrap ( ) . to_string ( ) ) ;
88
102
}
89
103
}
90
- component_licenses. insert ( format ! ( "{}@{}" , component_name, component_version) , license_names) ;
91
- manifest_license. insert ( format ! ( "{}/{}" , folder_path, file_name) , component_licenses. clone ( ) ) ;
104
+ component_licenses. insert (
105
+ format ! ( "{}@{}" , component_name, component_version) ,
106
+ license_names,
107
+ ) ;
108
+ manifest_license. insert (
109
+ format ! ( "{}/{}" , folder_path, file_name) ,
110
+ component_licenses. clone ( ) ,
111
+ ) ;
92
112
}
93
113
}
94
114
// save data in output.json and before that get json data from output.json file if it exists and then append new data to it
@@ -99,10 +119,17 @@ impl LicenseTool {
99
119
output_json = serde_json:: from_str :: < serde_json:: Value > ( & output_json_data) . unwrap ( ) ;
100
120
}
101
121
output_json[ "license" ] = json ! ( manifest_license) ;
102
- std:: fs:: write ( "/tmp/output.json" , serde_json:: to_string_pretty ( & output_json) . unwrap ( ) ) . unwrap ( ) ;
122
+ std:: fs:: write (
123
+ "/tmp/output.json" ,
124
+ serde_json:: to_string_pretty ( & output_json) . unwrap ( ) ,
125
+ )
126
+ . unwrap ( ) ;
103
127
let end_time = Instant :: now ( ) ;
104
128
let elapsed_time = end_time - start_time;
105
129
let elapsed_seconds = elapsed_time. as_secs_f64 ( ) . round ( ) ;
106
- println ! ( "Execution time for License Compliance scan: {:?} seconds" , elapsed_seconds) ;
130
+ println ! (
131
+ "Execution time for License Compliance scan: {:?} seconds" ,
132
+ elapsed_seconds
133
+ ) ;
107
134
}
108
- }
135
+ }
0 commit comments