1+ """
2+ Tool Comparison Script
3+ Compares TerraSecure vs Checkov vs Trivy
4+ """
5+
6+ import json
7+ import sys
8+ from pathlib import Path
9+ from collections import defaultdict
10+
11+ def load_terrasecure (filepath ):
12+ """Load TerraSecure results"""
13+ try :
14+ with open (filepath , 'r' , encoding = 'utf-8' ) as f :
15+ data = json .load (f )
16+
17+ issues = data .get ('issues' , [])
18+ stats = {
19+ 'total' : len (issues ),
20+ 'critical' : sum (1 for i in issues if i .get ('severity' , '' ).upper () == 'CRITICAL' ),
21+ 'high' : sum (1 for i in issues if i .get ('severity' , '' ).upper () == 'HIGH' ),
22+ 'medium' : sum (1 for i in issues if i .get ('severity' , '' ).upper () == 'MEDIUM' ),
23+ 'low' : sum (1 for i in issues if i .get ('severity' , '' ).upper () == 'LOW' ),
24+ }
25+
26+ return {
27+ 'tool' : 'TerraSecure' ,
28+ 'total_issues' : stats ['total' ],
29+ 'critical' : stats ['critical' ],
30+ 'high' : stats ['high' ],
31+ 'medium' : stats ['medium' ],
32+ 'low' : stats ['low' ],
33+ 'ml_enabled' : True ,
34+ 'ai_enabled' : True ,
35+ 'issues' : issues
36+ }
37+ except Exception as e :
38+ print (f"Error loading TerraSecure results: { e } " )
39+ return None
40+
41+ def load_checkov (filepath ):
42+ """Load Checkov results"""
43+ try :
44+ with open (filepath , 'r' , encoding = 'utf-8' ) as f :
45+ data = json .load (f )
46+
47+ results = data .get ('results' , {})
48+ failed_checks = results .get ('failed_checks' , [])
49+
50+ severity_map = defaultdict (int )
51+ for check in failed_checks :
52+ severity = check .get ('check_result' , {}).get ('result' , {}).get ('severity' , 'MEDIUM' )
53+ severity_map [severity .upper ()] += 1
54+
55+ return {
56+ 'tool' : 'Checkov' ,
57+ 'total_issues' : len (failed_checks ),
58+ 'critical' : severity_map .get ('CRITICAL' , 0 ),
59+ 'high' : severity_map .get ('HIGH' , 0 ),
60+ 'medium' : severity_map .get ('MEDIUM' , 0 ),
61+ 'low' : severity_map .get ('LOW' , 0 ),
62+ 'ml_enabled' : False ,
63+ 'ai_enabled' : False ,
64+ 'issues' : failed_checks
65+ }
66+ except Exception as e :
67+ print (f"Error loading Checkov results: { e } " )
68+ return None
69+
70+ def load_trivy (filepath ):
71+ """Load Trivy results"""
72+ try :
73+ with open (filepath , 'r' , encoding = 'utf-8' ) as f :
74+ data = json .load (f )
75+
76+ results = data .get ('Results' , [])
77+ all_misconfigs = []
78+
79+ for result in results :
80+ misconfigs = result .get ('Misconfigurations' , [])
81+ all_misconfigs .extend (misconfigs )
82+
83+ severity_map = defaultdict (int )
84+ for m in all_misconfigs :
85+ severity = m .get ('Severity' , 'MEDIUM' )
86+ severity_map [severity .upper ()] += 1
87+
88+ return {
89+ 'tool' : 'Trivy' ,
90+ 'total_issues' : len (all_misconfigs ),
91+ 'critical' : severity_map .get ('CRITICAL' , 0 ),
92+ 'high' : severity_map .get ('HIGH' , 0 ),
93+ 'medium' : severity_map .get ('MEDIUM' , 0 ),
94+ 'low' : severity_map .get ('LOW' , 0 ),
95+ 'ml_enabled' : False ,
96+ 'ai_enabled' : False ,
97+ 'issues' : all_misconfigs
98+ }
99+ except Exception as e :
100+ print (f"Error loading Trivy results: { e } " )
101+ return None
102+
103+ def print_comparison (results ):
104+ """Print comparison table"""
105+
106+ print ("\n " + "=" * 80 )
107+ print ("TOOL COMPARISON RESULTS" .center (80 ))
108+ print ("=" * 80 + "\n " )
109+
110+ # Summary table
111+ print (f"{ 'Metric' :<20} { 'TerraSecure' :<15} { 'Checkov' :<15} { 'Trivy' :<15} " )
112+ print ("-" * 80 )
113+
114+ terrasecure = next ((r for r in results if r ['tool' ] == 'TerraSecure' ), None )
115+ checkov = next ((r for r in results if r ['tool' ] == 'Checkov' ), None )
116+ trivy = next ((r for r in results if r ['tool' ] == 'Trivy' ), None )
117+
118+ if terrasecure :
119+ print (f"{ 'Total Issues' :<20} { terrasecure ['total_issues' ]:<15} { checkov ['total_issues' ] if checkov else 'N/A' :<15} { trivy ['total_issues' ] if trivy else 'N/A' :<15} " )
120+ print (f"{ 'Critical' :<20} { terrasecure ['critical' ]:<15} { checkov ['critical' ] if checkov else 'N/A' :<15} { trivy ['critical' ] if trivy else 'N/A' :<15} " )
121+ print (f"{ 'High' :<20} { terrasecure ['high' ]:<15} { checkov ['high' ] if checkov else 'N/A' :<15} { trivy ['high' ] if trivy else 'N/A' :<15} " )
122+ print (f"{ 'Medium' :<20} { terrasecure ['medium' ]:<15} { checkov ['medium' ] if checkov else 'N/A' :<15} { trivy ['medium' ] if trivy else 'N/A' :<15} " )
123+ print (f"{ 'Low' :<20} { terrasecure ['low' ]:<15} { checkov ['low' ] if checkov else 'N/A' :<15} { trivy ['low' ] if trivy else 'N/A' :<15} " )
124+ print ("-" * 80 )
125+ print (f"{ 'ML Powered' :<20} { '✓ YES' :<15} { '✗ NO' :<15} { '✗ NO' :<15} " )
126+ print (f"{ 'AI Explanations' :<20} { '✓ YES' :<15} { '✗ NO' :<15} { '✗ NO' :<15} " )
127+
128+ print ("=" * 80 + "\n " )
129+
130+ # Calculate overlap
131+ if terrasecure and checkov :
132+ print ("\n OVERLAP ANALYSIS:" )
133+ print ("-" * 80 )
134+ print (f"TerraSecure unique findings: { terrasecure ['total_issues' ] - checkov ['total_issues' ]} " )
135+ print (f"Checkov unique findings: { checkov ['total_issues' ] - terrasecure ['total_issues' ]} " )
136+ print (f"Estimated overlap: ~{ min (terrasecure ['total_issues' ], checkov ['total_issues' ])} issues" )
137+
138+ print ("\n " + "=" * 80 + "\n " )
139+
140+ def main ():
141+ if len (sys .argv ) < 2 :
142+ print ("Usage: python compare_tools.py <terrasecure.json> [checkov.json] [trivy.json]" )
143+ sys .exit (1 )
144+
145+ results = []
146+
147+ # Load TerraSecure
148+ if len (sys .argv ) >= 2 :
149+ ts_result = load_terrasecure (sys .argv [1 ])
150+ if ts_result :
151+ results .append (ts_result )
152+
153+ # Load Checkov
154+ if len (sys .argv ) >= 3 :
155+ ck_result = load_checkov (sys .argv [2 ])
156+ if ck_result :
157+ results .append (ck_result )
158+
159+ # Load Trivy
160+ if len (sys .argv ) >= 4 :
161+ tv_result = load_trivy (sys .argv [3 ])
162+ if tv_result :
163+ results .append (tv_result )
164+
165+ if results :
166+ print_comparison (results )
167+ else :
168+ print ("No results to compare!" )
169+ sys .exit (1 )
170+
171+ if __name__ == "__main__" :
172+ main ()
0 commit comments