4747 help = "A yaml file containing a list of policy names to exclude from the scan." ,
4848 type = click .Path (exists = True ),
4949 required = False ,
50- default = EXCLUSIONS_FILE ,
50+ default = str ( EXCLUSIONS_FILE ) ,
5151)
5252@click .option (
5353 "-o" ,
5454 "--output" ,
5555 required = False ,
5656 type = click .Path (exists = True ),
57- default = os .getcwd (),
57+ default = os .getcwd (), # noqa: PTH109
5858 help = "Output directory." ,
5959)
6060@click .option (
@@ -123,7 +123,7 @@ def scan(
123123
124124 if exclusions_file :
125125 # Get the exclusions configuration
126- with open (exclusions_file , encoding = "utf-8" ) as yaml_file :
126+ with Path (exclusions_file ). open ( encoding = "utf-8" ) as yaml_file :
127127 try :
128128 exclusions_cfg = yaml .safe_load (yaml_file )
129129 except yaml .YAMLError as exc :
@@ -139,9 +139,11 @@ def scan(
139139 flag_conditional_statements = False
140140 flag_resource_arn_statements = False
141141
142- if os .path .isfile (input_file ):
143- account_name = os .path .basename (input_file ).split ("." )[0 ]
144- account_authorization_details_cfg = json .loads (Path (input_file ).read_text (encoding = "utf-8" ))
142+ output = Path (output )
143+ input_file = Path (input_file )
144+ if input_file .is_file ():
145+ account_name = input_file .stem
146+ account_authorization_details_cfg = json .loads (input_file .read_text (encoding = "utf-8" ))
145147 rendered_html_report = scan_account_authorization_details (
146148 account_authorization_details_cfg ,
147149 exclusions ,
@@ -154,29 +156,29 @@ def scan(
154156 flag_trust_policies = flag_trust_policies ,
155157 severity = severity ,
156158 )
157- html_output_file = os . path . join ( output , f"iam-report-{ account_name } .html" )
159+ html_output_file = output / f"iam-report-{ account_name } .html"
158160 logger .info ("Saving the report to %s" , html_output_file )
159- if os . path . exists (html_output_file ):
160- os . remove ( html_output_file )
161+ if html_output_file . exists ():
162+ html_output_file . unlink ( )
161163
162- Path ( html_output_file ) .write_text (rendered_html_report , encoding = "utf-8" )
164+ html_output_file .write_text (rendered_html_report , encoding = "utf-8" )
163165
164166 print (f"Wrote HTML results to: { html_output_file } " )
165167
166168 # Open the report by default
167169 if not skip_open_report :
168170 print ("Opening the HTML report" )
169- url = f"file://{ os . path . abspath ( html_output_file )} "
171+ url = f"file://{ html_output_file . absolute ( )} "
170172 webbrowser .open (url , new = 2 )
171173
172- if os . path . isdir ( input_file ):
174+ if input_file . is_dir ( ):
173175 logger .info ("The path given is a directory. Scanning for account authorization files and generating report." )
174176 input_files = get_authorization_files_in_directory (input_file )
175177 for file in input_files :
176178 logger .info (f"Scanning file: { file } " )
177179 account_authorization_details_cfg = json .loads (Path (file ).read_text (encoding = "utf-8" ))
178180
179- account_name = os . path . basename ( input_file ). split ( "." )[ 0 ]
181+ account_name = input_file . parent . stem
180182 # Scan the Account Authorization Details config
181183 rendered_html_report = scan_account_authorization_details (
182184 account_authorization_details_cfg ,
@@ -187,19 +189,19 @@ def scan(
187189 minimize = minimize ,
188190 severity = severity ,
189191 )
190- html_output_file = os . path . join ( output , f"iam-report-{ account_name } .html" )
192+ html_output_file = output / f"iam-report-{ account_name } .html"
191193 logger .info ("Saving the report to %s" , html_output_file )
192- if os . path . exists (html_output_file ):
193- os . remove ( html_output_file )
194+ if html_output_file . exists ():
195+ html_output_file . unlink ( )
194196
195- Path ( html_output_file ) .write_text (rendered_html_report , encoding = "utf-8" )
197+ html_output_file .write_text (rendered_html_report , encoding = "utf-8" )
196198
197199 print (f"Wrote HTML results to: { html_output_file } " )
198200
199201 # Open the report by default
200202 if not skip_open_report :
201203 print ("Opening the HTML report" )
202- url = f"file://{ os . path . abspath ( html_output_file )} "
204+ url = f"file://{ html_output_file . absolute ( )} "
203205 webbrowser .open (url , new = 2 )
204206
205207
@@ -211,7 +213,7 @@ def scan_account_authorization_details(
211213 account_authorization_details_cfg : dict [str , Any ],
212214 exclusions : Exclusions ,
213215 account_name : str ,
214- output_directory : str ,
216+ output_directory : str | Path | None ,
215217 write_data_files : bool ,
216218 minimize : bool ,
217219 return_json_results : Literal [True ],
@@ -227,7 +229,7 @@ def scan_account_authorization_details(
227229 account_authorization_details_cfg : dict [str , Any ],
228230 exclusions : Exclusions ,
229231 account_name : str = ...,
230- output_directory : str = ...,
232+ output_directory : str | Path | None = ...,
231233 write_data_files : bool = ...,
232234 minimize : bool = ...,
233235 return_json_results : Literal [False ] = ...,
@@ -242,7 +244,7 @@ def scan_account_authorization_details(
242244 account_authorization_details_cfg : dict [str , Any ],
243245 exclusions : Exclusions ,
244246 account_name : str = "default" ,
245- output_directory : str = os . getcwd () ,
247+ output_directory : str | Path | None = None ,
246248 write_data_files : bool = False ,
247249 minimize : bool = False ,
248250 return_json_results : bool = False ,
@@ -285,14 +287,13 @@ def scan_account_authorization_details(
285287
286288 # Raw data file
287289 if write_data_files :
288- if output_directory is None :
289- output_directory = os .getcwd ()
290+ output_directory = Path (output_directory ) if output_directory else Path .cwd ()
290291
291- results_data_file = os . path . join ( output_directory , f"iam-results-{ account_name } .json" )
292+ results_data_file = output_directory / f"iam-results-{ account_name } .json"
292293 results_data_filepath = write_results_data_file (authorization_details .results , results_data_file )
293294 print (f"Results data saved: { results_data_filepath } " )
294295
295- findings_data_file = os . path . join ( output_directory , f"iam-findings-{ account_name } .json" )
296+ findings_data_file = output_directory / f"iam-findings-{ account_name } .json"
296297 findings_data_filepath = write_results_data_file (results , findings_data_file )
297298 print (f"Findings data file saved: { findings_data_filepath } " )
298299
@@ -302,15 +303,15 @@ def scan_account_authorization_details(
302303 "iam_findings" : results ,
303304 "rendered_report" : rendered_report ,
304305 }
305- else :
306- return rendered_report
306+
307+ return rendered_report
307308
308309
309310def get_authorization_files_in_directory (
310- directory : str ,
311+ directory : Path ,
311312) -> list [str ]: # pragma: no cover
312313 """Get a list of download-account-authorization-files in a directory"""
313- file_list_with_full_path = [file .absolute () for file in Path ( directory ) .glob ("*.json" )]
314+ file_list_with_full_path = [file .absolute () for file in directory .glob ("*.json" )]
314315
315316 new_file_list = []
316317 for file in file_list_with_full_path :
0 commit comments