@@ -159,97 +159,106 @@ def main(argv: List[str] | None = None) -> int:
159159
160160 output_format = "json" if getattr (args , "json" , False ) or getattr (args , "format" , "table" ) == "json" else "table"
161161
162- if args .command == "scan" :
163- findings = scan_config (config_path , args .server )
164-
165- if args .severity :
166- findings = [f for f in findings if f .severity == args .severity or f .severity == "critical" ]
162+ try :
163+ if args .command == "scan" :
164+ findings = scan_config (config_path , args .server )
165+
166+ if args .severity :
167+ findings = [f for f in findings if f .severity == args .severity or f .severity == "critical" ]
167168
168- if output_format == "json" :
169- print (json .dumps ([f .to_dict () for f in findings ], indent = 2 ))
170- elif output_format == "table" :
171- table = Table (title = f"Security Scan: { args .config } " )
172- table .add_column ("Server" , style = "cyan" )
173- table .add_column ("Severity" , style = "bold" )
174- table .add_column ("Category" , style = "dim" )
175- table .add_column ("Finding" )
176-
177- for f in findings :
178- sev_color = "red" if f .severity == "critical" else "yellow"
179- table .add_row (f .server , f"[{ sev_color } ]{ f .severity .upper ()} [/{ sev_color } ]" , f .category , f .message )
180-
181- console .print (table )
182-
183- return 1 if any (f .severity == "critical" for f in findings ) else 0
169+ if output_format == "json" :
170+ print (json .dumps ([f .to_dict () for f in findings ], indent = 2 ))
171+ elif output_format == "table" :
172+ table = Table (title = f"Security Scan: { args .config } " )
173+ table .add_column ("Server" , style = "cyan" )
174+ table .add_column ("Severity" , style = "bold" )
175+ table .add_column ("Category" , style = "dim" )
176+ table .add_column ("Finding" )
177+
178+ for f in findings :
179+ sev_color = "red" if f .severity == "critical" else "yellow"
180+ table .add_row (f .server , f"[{ sev_color } ]{ f .severity .upper ()} [/{ sev_color } ]" , f .category , f .message )
181+
182+ console .print (table )
183+
184+ return 1 if any (f .severity == "critical" for f in findings ) else 0
184185
185- elif args .command == "fingerprint" :
186- fingerprints = get_fingerprints (config_path )
187-
188- if args .compare :
189- with open (args .compare ) as f :
190- saved = json .load (f )
186+ elif args .command == "fingerprint" :
187+ fingerprints = get_fingerprints (config_path )
191188
192- diffs = {}
193- for name , h in fingerprints .items ():
194- if name not in saved :
195- diffs [name ] = "new"
196- elif saved [name ] != h :
197- diffs [name ] = "changed"
189+ if args .compare :
190+ with open (args .compare ) as f :
191+ saved = json .load (f )
192+
193+ diffs = {}
194+ for name , h in fingerprints .items ():
195+ if name not in saved :
196+ diffs [name ] = "new"
197+ elif saved [name ] != h :
198+ diffs [name ] = "changed"
199+
200+ if output_format == "json" :
201+ print (json .dumps ({"current" : fingerprints , "diffs" : diffs }, indent = 2 ))
202+ else :
203+ print (f"Comparison results for { args .config } :" )
204+ for name , status in diffs .items ():
205+ print (f" { name } : { status } " )
206+ if not diffs :
207+ print (" Identical fingerprints." )
208+
209+ elif args .output :
210+ with open (args .output , "w" ) as f :
211+ json .dump (fingerprints , f , indent = 2 )
212+ if output_format != "json" :
213+ print (f"Fingerprints saved to { args .output } " )
214+ else :
215+ print (json .dumps ({"status" : "success" , "file" : args .output }, indent = 2 ))
198216
199- if output_format == "json" :
200- print (json .dumps ({"current" : fingerprints , "diffs" : diffs }, indent = 2 ))
201- else :
202- print (f"Comparison results for { args .config } :" )
203- for name , status in diffs .items ():
204- print (f" { name } : { status } " )
205- if not diffs :
206- print (" Identical fingerprints." )
207-
208- elif args .output :
209- with open (args .output , "w" ) as f :
210- json .dump (fingerprints , f , indent = 2 )
211- if output_format != "json" :
212- print (f"Fingerprints saved to { args .output } " )
213217 else :
214- print (json .dumps ({"status" : "success" , "file" : args .output }, indent = 2 ))
215-
216- else :
217- if output_format == "json" :
218- print (json .dumps (fingerprints , indent = 2 ))
218+ if output_format == "json" :
219+ print (json .dumps (fingerprints , indent = 2 ))
220+ else :
221+ for name , h in fingerprints .items ():
222+ print (f"{ name :20} { h } " )
223+
224+ elif args .command == "report" :
225+ findings = scan_config (config_path )
226+ fingerprints = get_fingerprints (config_path )
227+
228+ report = {
229+ "config" : str (config_path ),
230+ "summary" : {
231+ "total_servers" : len (fingerprints ),
232+ "total_findings" : len (findings ),
233+ "critical" : len ([f for f in findings if f .severity == "critical" ]),
234+ "warning" : len ([f for f in findings if f .severity == "warning" ])
235+ },
236+ "findings" : [f .to_dict () for f in findings ],
237+ "fingerprints" : fingerprints
238+ }
239+
240+ if output_format == "json" or getattr (args , "format" , "markdown" ) == "json" :
241+ print (json .dumps (report , indent = 2 ))
219242 else :
220- for name , h in fingerprints .items ():
221- print (f"{ name :20} { h } " )
243+ # Simple markdown report
244+ print (f"# Security Report: { args .config } " )
245+ print ()
246+ print (f"- Total Servers: { report ['summary' ]['total_servers' ]} " )
247+ print (f"- Total Findings: { report ['summary' ]['total_findings' ]} " )
248+ print ()
249+ print ("## Findings" )
250+ for f in findings :
251+ print (f"- **{ f .server } ** ({ f .severity .upper ()} ): { f .message } " )
222252
223- elif args .command == "report" :
224- findings = scan_config (config_path )
225- fingerprints = get_fingerprints (config_path )
226-
227- report = {
228- "config" : str (config_path ),
229- "summary" : {
230- "total_servers" : len (fingerprints ),
231- "total_findings" : len (findings ),
232- "critical" : len ([f for f in findings if f .severity == "critical" ]),
233- "warning" : len ([f for f in findings if f .severity == "warning" ])
234- },
235- "findings" : [f .to_dict () for f in findings ],
236- "fingerprints" : fingerprints
237- }
238-
239- if output_format == "json" or getattr (args , "format" , "markdown" ) == "json" :
240- print (json .dumps (report , indent = 2 ))
253+ return 0
254+ except Exception as e :
255+ is_known = isinstance (e , (FileNotFoundError , ValueError , yaml .YAMLError ))
256+ msg = str (e ) if is_known else "An error occurred during scanning"
257+ if output_format == "json" :
258+ print (json .dumps ({"status" : "error" , "message" : msg , "type" : e .__class__ .__name__ if is_known else "InternalError" }, indent = 2 ))
241259 else :
242- # Simple markdown report
243- print (f"# Security Report: { args .config } " )
244- print ()
245- print (f"- Total Servers: { report ['summary' ]['total_servers' ]} " )
246- print (f"- Total Findings: { report ['summary' ]['total_findings' ]} " )
247- print ()
248- print ("## Findings" )
249- for f in findings :
250- print (f"- **{ f .server } ** ({ f .severity .upper ()} ): { f .message } " )
251-
252- return 0
260+ print (f"Error: { msg } " )
261+ return 1
253262
254263
255264if __name__ == "__main__" :
0 commit comments