@@ -35,20 +35,20 @@ extern crate chrono;
3535extern crate encoding;
3636
3737use clap:: ArgMatches ;
38- use log:: { self , debug, info , error } ;
38+ use log:: { self , debug, error , info } ;
3939use serde:: { Deserialize , Serialize } ;
4040use simple_error:: SimpleError ;
4141use std:: fs:: File ;
4242use std:: io:: { Cursor , Read } ;
4343use std:: path:: { Path , PathBuf } ;
4444use std:: { io, str} ;
45- use walkdir:: { WalkDir } ;
45+ use walkdir:: WalkDir ;
4646
4747use encoding:: all:: ASCII ;
4848use encoding:: { DecoderTrap , Encoding } ;
49+ use path_clean:: PathClean ;
4950use rusty_hogs:: { SecretScanner , SecretScannerBuilder } ;
5051use std:: collections:: HashSet ;
51- use path_clean:: PathClean ;
5252
5353#[ derive( Serialize , Deserialize , Debug , PartialEq , Eq , Hash , Clone , Default ) ]
5454/// `serde_json` object that represents a single found secret - finding
@@ -87,7 +87,7 @@ fn main() {
8787 . get_matches ( ) ;
8888 match run ( & matches) {
8989 Ok ( ( ) ) => { }
90- Err ( e) => error ! ( "Error running command: {}" , e)
90+ Err ( e) => error ! ( "Error running command: {}" , e) ,
9191 }
9292}
9393
@@ -116,7 +116,13 @@ fn run(arg_matches: &ArgMatches) -> Result<(), SimpleError> {
116116 let mut output: HashSet < FileFinding > = HashSet :: new ( ) ;
117117
118118 if Path :: is_dir ( fspath) {
119- output. extend ( scan_dir ( fspath, output_file, & secret_scanner, recursive, unzip) ) ;
119+ output. extend ( scan_dir (
120+ fspath,
121+ output_file,
122+ & secret_scanner,
123+ recursive,
124+ unzip,
125+ ) ) ;
120126 } else {
121127 let f = File :: open ( fspath) . unwrap ( ) ;
122128 output. extend ( scan_file ( fspath, & secret_scanner, f, "" , unzip) ) ;
@@ -125,7 +131,10 @@ fn run(arg_matches: &ArgMatches) -> Result<(), SimpleError> {
125131 info ! ( "Found {} secrets" , output. len( ) ) ;
126132 match secret_scanner. output_findings ( & output) {
127133 Ok ( _) => Ok ( ( ) ) ,
128- Err ( err) => Err ( SimpleError :: with ( "failed to output findings" , SimpleError :: new ( err. to_string ( ) ) ) )
134+ Err ( err) => Err ( SimpleError :: with (
135+ "failed to output findings" ,
136+ SimpleError :: new ( err. to_string ( ) ) ,
137+ ) ) ,
129138 }
130139}
131140
@@ -155,24 +164,36 @@ fn scan_dir(
155164 output
156165}
157166
158- fn recursive_dir_scan < C > ( fspath : & Path , output_file : & Path , mut closure : C ) where C : FnMut ( & Path ) {
167+ fn recursive_dir_scan < C > ( fspath : & Path , output_file : & Path , mut closure : C )
168+ where
169+ C : FnMut ( & Path ) ,
170+ {
159171 for entry in WalkDir :: new ( fspath) . into_iter ( ) . filter_map ( |e| e. ok ( ) ) {
160172 if entry. file_type ( ) . is_file ( ) && & PathBuf :: from ( entry. path ( ) ) . clean ( ) != output_file {
161173 closure ( & entry. path ( ) ) ;
162174 }
163175 }
164176}
165177
166- fn flat_dir_scan < C > ( fspath : & Path , output_file : & Path , mut closure : C ) where C : FnMut ( & Path ) {
178+ fn flat_dir_scan < C > ( fspath : & Path , output_file : & Path , mut closure : C )
179+ where
180+ C : FnMut ( & Path ) ,
181+ {
167182 let dir_contents: Vec < PathBuf > = fspath
168- . read_dir ( )
169- . expect ( "read_dir call failed" )
170- . filter_map ( |e| e. ok ( ) )
171- . filter ( |e| e. file_type ( ) . unwrap ( ) . is_file ( ) )
172- . map ( |e| e. path ( ) )
173- . inspect ( |e| debug ! ( "clean path: {:?}, output_file: {:?}" , & e. clean( ) , output_file) )
174- . filter ( |e| e. clean ( ) != output_file)
175- . collect ( ) ;
183+ . read_dir ( )
184+ . expect ( "read_dir call failed" )
185+ . filter_map ( |e| e. ok ( ) )
186+ . filter ( |e| e. file_type ( ) . unwrap ( ) . is_file ( ) )
187+ . map ( |e| e. path ( ) )
188+ . inspect ( |e| {
189+ debug ! (
190+ "clean path: {:?}, output_file: {:?}" ,
191+ & e. clean( ) ,
192+ output_file
193+ )
194+ } )
195+ . filter ( |e| e. clean ( ) != output_file)
196+ . collect ( ) ;
176197 debug ! ( "dir_contents: {:?}" , dir_contents) ;
177198
178199 for file_path in dir_contents {
@@ -204,7 +225,10 @@ fn scan_file<R: Read + io::Seek>(
204225 // and moving it (inefficient) *but* that means we can recursively decompress
205226 let mut innerdata: Vec < u8 > = Vec :: new ( ) ;
206227 let read_result = innerfile. read_to_end ( & mut innerdata) ;
207- if read_result. is_err ( ) { info ! ( "read error within ZIP file" ) ; continue ; }
228+ if read_result. is_err ( ) {
229+ info ! ( "read error within ZIP file" ) ;
230+ continue ;
231+ }
208232 let new_reader = Cursor :: new ( innerdata) ;
209233 let mut inner_findings = scan_file (
210234 innerfile. sanitized_name ( ) . as_path ( ) ,
@@ -226,7 +250,10 @@ fn scan_file<R: Read + io::Seek>(
226250 let mut inner_entry = entry_result. unwrap ( ) ;
227251 let mut innerdata: Vec < u8 > = Vec :: new ( ) ;
228252 let read_result = inner_entry. read_to_end ( & mut innerdata) ;
229- if read_result. is_err ( ) { info ! ( "read error within TAR file" ) ; continue ; }
253+ if read_result. is_err ( ) {
254+ info ! ( "read error within TAR file" ) ;
255+ continue ;
256+ }
230257 let new_reader = Cursor :: new ( innerdata) ;
231258 let mut inner_findings = scan_file (
232259 inner_entry. path ( ) . unwrap ( ) . as_ref ( ) ,
@@ -245,21 +272,18 @@ fn scan_file<R: Read + io::Seek>(
245272 let mut decompressor = flate2:: read:: GzDecoder :: new ( reader) ;
246273 let mut innerdata: Vec < u8 > = Vec :: new ( ) ;
247274 let read_result = decompressor. read_to_end ( & mut innerdata) ;
248- if read_result. is_err ( ) { info ! ( "read error within ZIP file" ) ; return findings; }
275+ if read_result. is_err ( ) {
276+ info ! ( "read error within ZIP file" ) ;
277+ return findings;
278+ }
249279 let new_reader = Cursor :: new ( innerdata) ;
250280 let mut tempstring = String :: from ( file_path. file_stem ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) ) ;
251281 if ext. to_ascii_lowercase ( ) == "tgz" {
252282 tempstring. push_str ( ".tar" ) ;
253283 }
254284 let inner_path: & Path = Path :: new ( & tempstring) ;
255285 info ! ( "gunzip inner path: {:?}" , inner_path) ;
256- let mut inner_findings = scan_file (
257- inner_path,
258- ss,
259- new_reader,
260- & path_string,
261- unzip,
262- ) ;
286+ let mut inner_findings = scan_file ( inner_path, ss, new_reader, & path_string, unzip) ;
263287 for d in inner_findings. drain ( ) {
264288 info ! ( "FileFinding: {:?}" , d) ;
265289 findings. insert ( d) ;
@@ -268,7 +292,9 @@ fn scan_file<R: Read + io::Seek>(
268292 } else {
269293 let mut data = Vec :: new ( ) ;
270294 let read_result = reader. read_to_end ( & mut data) ;
271- if read_result. is_err ( ) { info ! ( "read error for file {}" , path_string) ; }
295+ if read_result. is_err ( ) {
296+ info ! ( "read error for file {}" , path_string) ;
297+ }
272298 scan_bytes ( data, ss, path_string)
273299 }
274300}
@@ -309,23 +335,17 @@ fn scan_bytes(input: Vec<u8>, ss: &SecretScanner, path: String) -> HashSet<FileF
309335#[ cfg( test) ]
310336mod tests {
311337 use super :: * ;
338+ use escargot:: CargoBuild ;
312339 use std:: io:: Result ;
313- use std:: process:: Output ;
314340 use std:: io:: Write ;
341+ use std:: process:: Output ;
315342 use tempfile:: { NamedTempFile , TempDir } ;
316- use escargot:: CargoBuild ;
317343
318344 fn run_command_in_dir ( dir : & TempDir , command : & str , args : & [ & str ] ) -> Result < Output > {
319345 let dir_path = dir. path ( ) . to_str ( ) . unwrap ( ) ;
320- let binary = CargoBuild :: new ( )
321- . bin ( command)
322- . run ( )
323- . unwrap ( ) ;
324-
325- binary. command ( )
326- . current_dir ( dir_path)
327- . args ( args)
328- . output ( )
346+ let binary = CargoBuild :: new ( ) . bin ( command) . run ( ) . unwrap ( ) ;
347+
348+ binary. command ( ) . current_dir ( dir_path) . args ( args) . output ( )
329349 }
330350
331351 fn write_temp_file ( dir : & TempDir , filename : & str , contents : & str ) {
@@ -346,7 +366,11 @@ mod tests {
346366 fn does_not_scan_output_file ( ) {
347367 let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
348368
349- write_temp_file ( & temp_dir, "insecure-file.txt" , "My email is username@mail.com" ) ;
369+ write_temp_file (
370+ & temp_dir,
371+ "insecure-file.txt" ,
372+ "My email is username@mail.com" ,
373+ ) ;
350374
351375 let cmd_args = [ "-o" , "output_file.txt" , "." ] ;
352376
@@ -374,13 +398,20 @@ mod tests {
374398 }
375399 "# ;
376400 write ! ( allowlist_temp_file, "{}" , json) . unwrap ( ) ;
377- write_temp_file ( & temp_dir, "insecure-file.txt" , "My email is username@mail.com" ) ;
401+ write_temp_file (
402+ & temp_dir,
403+ "insecure-file.txt" ,
404+ "My email is username@mail.com" ,
405+ ) ;
378406
379- let cmd_args = [ "--allowlist" , allowlist_temp_file. path ( ) . to_str ( ) . unwrap ( ) , "." ] ;
407+ let cmd_args = [
408+ "--allowlist" ,
409+ allowlist_temp_file. path ( ) . to_str ( ) . unwrap ( ) ,
410+ "." ,
411+ ] ;
380412
381413 let output = run_command_in_dir ( & temp_dir, "duroc_hog" , & cmd_args) . unwrap ( ) ;
382414
383415 assert_eq ! ( "[]\n " , str :: from_utf8( & output. stdout) . unwrap( ) ) ;
384-
385416 }
386- }
417+ }
0 commit comments