@@ -24,21 +24,227 @@ import (
2424
2525func TestViolationFailBuild (t * testing.T ) {
2626 components := map [string ]services.Component {"gav://antparent:ant:1.6.5" : {}}
27+
2728 tests := []struct {
28- violations []services.Violation
29- expectedError bool
29+ name string
30+ auditResults * SecurityCommandResults
31+ expectedResult bool
3032 }{
31- {[]services.Violation {{Components : components , FailBuild : false }, {Components : components , FailBuild : false }, {Components : components , FailBuild : false }}, false },
32- {[]services.Violation {{Components : components , FailBuild : false }, {Components : components , FailBuild : true }, {Components : components , FailBuild : false }}, true },
33- {[]services.Violation {{Components : components , FailBuild : true }, {Components : components , FailBuild : true }, {Components : components , FailBuild : true }}, true },
33+ {
34+ name : "non-applicable violations with FailBuild & no skip-non-applicable in ScaResults.Violations - build should fail" ,
35+ auditResults : createSecurityCommandResultsForFailBuildTest (true , true , utils .NewBoolPtr (false )),
36+ expectedResult : true ,
37+ },
38+ {
39+ name : "non-applicable violations with FailBuild & skip-non-applicable in ScaResults.Violations - build should not fail" ,
40+ auditResults : createSecurityCommandResultsForFailBuildTest (true , true , utils .NewBoolPtr (true )),
41+ expectedResult : false ,
42+ },
43+ {
44+ name : "non-applicable violations with FailBuild & no skip-non-applicable in DeprecatedXrayResults - build should fail" ,
45+ auditResults : createSecurityCommandResultsForFailBuildTest (false , true , utils .NewBoolPtr (false )),
46+ expectedResult : true ,
47+ },
48+ {
49+ name : "non-applicable violations with FailBuild & skip-non-applicable in DeprecatedXrayResults - build should not fail" ,
50+ auditResults : createSecurityCommandResultsForFailBuildTest (false , true , utils .NewBoolPtr (true )),
51+ expectedResult : false ,
52+ },
53+ {
54+ name : "no applicability results, violations with FailBuild in DeprecatedXrayResults - build should fail" ,
55+ auditResults : createSecurityCommandResultsForFailBuildTest (false , false , nil ),
56+ expectedResult : true ,
57+ },
58+ {
59+ name : "no applicability results, violations with FailBuild in ScaResults.Violations - build should fail" ,
60+ auditResults : createSecurityCommandResultsForFailBuildTest (true , false , nil ),
61+ expectedResult : true ,
62+ },
63+ {
64+ name : "multiple targets - first target should not fail, second target should fail" ,
65+ auditResults : & SecurityCommandResults {
66+ EntitledForJas : true ,
67+ Targets : []* TargetResults {
68+ {
69+ // First target - should not fail
70+ ScanTarget : ScanTarget {Target : "test-target-1" },
71+ ScaResults : & ScaScanResults {
72+ Violations : []services.Violation {
73+ {
74+ // Violation 1: FailBuild & FailPr set to false - should not fail
75+ Components : components ,
76+ ViolationType : utils .ViolationTypeSecurity .String (),
77+ FailBuild : false ,
78+ FailPr : false ,
79+ Cves : []services.Cve {{Id : "CVE-2024-1111" }},
80+ Severity : "High" ,
81+ },
82+ {
83+ // Violation 2: FailBuild=true, notApplicable, skip-not-applicable - should not fail
84+ Components : components ,
85+ ViolationType : utils .ViolationTypeSecurity .String (),
86+ FailBuild : true ,
87+ Policies : []services.Policy {{SkipNotApplicable : true }},
88+ Cves : []services.Cve {{Id : "CVE-2024-2222" }},
89+ Severity : "High" ,
90+ },
91+ },
92+ },
93+ JasResults : & JasScansResults {
94+ ApplicabilityScanResults : []ScanResult [[]* sarif.Run ]{
95+ {
96+ Scan : []* sarif.Run {
97+ {
98+ Tool : & sarif.Tool {
99+ Driver : & sarif.ToolComponent {
100+ Rules : []* sarif.ReportingDescriptor {
101+ {
102+ ID : utils .NewStringPtr (jasutils .CveToApplicabilityRuleId ("CVE-2024-2222" )),
103+ Properties : & sarif.PropertyBag {
104+ Properties : map [string ]interface {}{
105+ jasutils .ApplicabilitySarifPropertyKey : "not_applicable" ,
106+ },
107+ },
108+ },
109+ },
110+ },
111+ },
112+ },
113+ },
114+ },
115+ },
116+ },
117+ },
118+ {
119+ // Second target - should fail
120+ ScanTarget : ScanTarget {Target : "test-target-2" },
121+ ScaResults : & ScaScanResults {
122+ Violations : []services.Violation {
123+ {
124+ // Violation 1: FailBuild=true, notApplicable, NOT skip-not-applicable - should fail
125+ Components : components ,
126+ ViolationType : utils .ViolationTypeSecurity .String (),
127+ FailBuild : true ,
128+ Policies : []services.Policy {{SkipNotApplicable : false }},
129+ Cves : []services.Cve {{Id : "CVE-2024-3333" }},
130+ Severity : "High" ,
131+ },
132+ {
133+ // Violation 2: FailBuild & FailPr set to false - should not fail
134+ Components : components ,
135+ ViolationType : utils .ViolationTypeSecurity .String (),
136+ FailBuild : false ,
137+ FailPr : false ,
138+ Cves : []services.Cve {{Id : "CVE-2024-4444" }},
139+ Severity : "High" ,
140+ },
141+ },
142+ },
143+ JasResults : & JasScansResults {
144+ ApplicabilityScanResults : []ScanResult [[]* sarif.Run ]{
145+ {
146+ Scan : []* sarif.Run {
147+ {
148+ Tool : & sarif.Tool {
149+ Driver : & sarif.ToolComponent {
150+ Rules : []* sarif.ReportingDescriptor {
151+ {
152+ ID : utils .NewStringPtr (jasutils .CveToApplicabilityRuleId ("CVE-2024-3333" )),
153+ Properties : & sarif.PropertyBag {
154+ Properties : map [string ]interface {}{
155+ jasutils .ApplicabilitySarifPropertyKey : "not_applicable" ,
156+ },
157+ },
158+ },
159+ },
160+ },
161+ },
162+ },
163+ },
164+ },
165+ },
166+ },
167+ },
168+ },
169+ },
170+ expectedResult : true , // Should fail because second target has a violation that should fail
171+ },
34172 }
35173
36174 for _ , test := range tests {
37- var err error
38- if CheckIfFailBuild ([]services.ScanResponse {{Violations : test .violations }}) {
39- err = NewFailBuildError ()
175+ t .Run (test .name , func (t * testing.T ) {
176+ shouldFailBuild , err := CheckIfFailBuild (test .auditResults )
177+ assert .NoError (t , err )
178+ assert .Equal (t , test .expectedResult , shouldFailBuild )
179+ })
180+ }
181+ }
182+
183+ func createSecurityCommandResultsForFailBuildTest (useNewViolations bool , hasJasResults bool , skipNotApplicable * bool ) * SecurityCommandResults {
184+ components := map [string ]services.Component {"gav://antparent:ant:1.6.5" : {}}
185+ cveId := "CVE-2024-1234"
186+
187+ target := & TargetResults {
188+ ScanTarget : ScanTarget {Target : "test-target" },
189+ ScaResults : & ScaScanResults {},
190+ }
191+
192+ violation := services.Violation {
193+ Components : components ,
194+ ViolationType : utils .ViolationTypeSecurity .String (),
195+ FailBuild : true ,
196+ Cves : []services.Cve {{Id : cveId }},
197+ Severity : "High" ,
198+ }
199+
200+ if skipNotApplicable != nil {
201+ violation .Policies = []services.Policy {{SkipNotApplicable : * skipNotApplicable }}
202+ }
203+
204+ if useNewViolations {
205+ target .ScaResults .Violations = []services.Violation {violation }
206+ } else {
207+ target .ScaResults .DeprecatedXrayResults = []ScanResult [services.ScanResponse ]{
208+ {
209+ Scan : services.ScanResponse {
210+ Violations : []services.Violation {violation },
211+ },
212+ },
213+ }
214+ }
215+
216+ if hasJasResults {
217+ target .JasResults = & JasScansResults {
218+ ApplicabilityScanResults : []ScanResult [[]* sarif.Run ]{
219+ {
220+ Scan : []* sarif.Run {
221+ {
222+ Tool : & sarif.Tool {
223+ Driver : & sarif.ToolComponent {
224+ Rules : []* sarif.ReportingDescriptor {
225+ {
226+ ID : utils .NewStringPtr (jasutils .CveToApplicabilityRuleId (cveId )),
227+ Properties : & sarif.PropertyBag {
228+ Properties : map [string ]interface {}{
229+ jasutils .ApplicabilitySarifPropertyKey : "not_applicable" ,
230+ },
231+ },
232+ },
233+ },
234+ },
235+ },
236+ },
237+ },
238+ },
239+ },
40240 }
41- assert .Equal (t , test .expectedError , err != nil )
241+ } else {
242+ target .JasResults = nil
243+ }
244+
245+ return & SecurityCommandResults {
246+ EntitledForJas : true ,
247+ Targets : []* TargetResults {target },
42248 }
43249}
44250
0 commit comments