Skip to content

Commit 98ac2d8

Browse files
authored
- Added exclusion information to the analysis report (#99)
1 parent ba68853 commit 98ac2d8

File tree

2 files changed

+47
-27
lines changed

2 files changed

+47
-27
lines changed

pkg/analysis/analysis.go

+38-27
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,8 @@ func (a *analyzer) initialize() error {
258258
return nil
259259
}
260260

261-
func (a *analyzer) shouldExclude(subject map[string]interface{}, exclusions []*exclusion) (bool, error) {
262-
for _, exclusion := range exclusions {
261+
func (a *analyzer) shouldExclude(subject map[string]interface{}, exclusions []*exclusion) (bool, int, error) {
262+
for i, exclusion := range exclusions {
263263
if exclusion.exclusion.Disabled {
264264
klog.V(7).Infof("Exclusion '%v' is disabled - skipping", exclusion.exclusion.Comment)
265265
continue
@@ -275,20 +275,20 @@ func (a *analyzer) shouldExclude(subject map[string]interface{}, exclusions []*e
275275
})
276276

277277
if err != nil {
278-
return false, err
278+
return false, i, err
279279
}
280280

281281
exclude, ok := recommendationOutput.Value().(bool)
282282
if !ok {
283-
return false, fmt.Errorf("Failed to cast exclusion result '%v'", exclusion.exclusion.Comment)
283+
return false, i, fmt.Errorf("Failed to cast exclusion result '%v'", exclusion.exclusion.Comment)
284284
}
285285

286286
if exclude {
287-
return true, nil
287+
return true, i, nil
288288
}
289289
}
290290

291-
return false, nil
291+
return false, 0, nil
292292
}
293293

294294
func (a *analyzer) Analyze() (*AnalysisReport, error) {
@@ -301,8 +301,9 @@ func (a *analyzer) Analyze() (*AnalysisReport, error) {
301301
Description: a.config.Description,
302302
Uuid: a.config.Uuid,
303303
},
304-
CreatedOn: time.Now().Format(time.RFC3339),
305-
Findings: []AnalysisReportFinding{},
304+
CreatedOn: time.Now().Format(time.RFC3339),
305+
Findings: []AnalysisReportFinding{},
306+
ExclusionsInfo: []ExclusionInfo{},
306307
}
307308

308309
errs := []error{}
@@ -339,20 +340,39 @@ func (a *analyzer) Analyze() (*AnalysisReport, error) {
339340
for _, subject := range subjects {
340341
sub := subject.(map[string]interface{})
341342

342-
exclude, err := a.shouldExclude(sub, rule.exclusions)
343+
s := v1.Subject{}
344+
if kind, exist := sub["kind"]; exist {
345+
s.Kind = kind.(string)
346+
}
347+
if apiGroup, exist := sub["apiGroup"]; exist {
348+
s.APIGroup = apiGroup.(string)
349+
}
350+
if name, exist := sub["name"]; exist {
351+
s.Name = name.(string)
352+
}
353+
if namespace, exist := sub["namespace"]; exist {
354+
s.Namespace = namespace.(string)
355+
}
356+
357+
exclude, index, err := a.shouldExclude(sub, rule.exclusions)
343358
if err != nil {
344-
klog.Errorf("Failed to check exclusion for rule '%v' and subject %v - %v", rule.rule.Name, sub, err)
359+
klog.Errorf("Failed to check exclusion for rule '%v' and subject %v - %v (exclusion #%v)", rule.rule.Name, sub, err, index+1)
345360
errs = append(errs, err)
346361
//Continue on error - assume malformed exception expression
347362
}
348363

349364
if exclude {
350365
analysisStats.ExclusionCount++
351-
klog.V(5).Infof("Skipping subject '%v' from rule exclusion - %v", sub, rule.rule.Name)
366+
klog.V(5).Infof("Skipping subject '%v' from rule exclusion - %v (exclusion #%v)", sub, rule.rule.Name, index+1)
367+
ei := ExclusionInfo{
368+
Subject: &s,
369+
Message: fmt.Sprintf("For rule: \"%v\", subject excluded by the rule-level (#%v) - \"%v\" ", rule.rule.Name, index+1, rule.rule.Exclusions[index].Comment),
370+
}
371+
report.ExclusionsInfo = append(report.ExclusionsInfo, ei)
352372
continue
353373
}
354374

355-
exclude, err = a.shouldExclude(sub, a.globalExclusions)
375+
exclude, index, err = a.shouldExclude(sub, a.globalExclusions)
356376
if err != nil {
357377
klog.Errorf("Failed to check global exclusion for rule '%v' and subject %v - %v", rule.rule.Name, sub, err)
358378
errs = append(errs, err)
@@ -361,7 +381,12 @@ func (a *analyzer) Analyze() (*AnalysisReport, error) {
361381

362382
if exclude {
363383
analysisStats.ExclusionCount++
364-
klog.V(5).Infof("Skipping subject '%v' from rule exclusion - %v", sub, rule.rule.Name)
384+
klog.V(5).Infof("Skipping subject '%v' from global exclusion - %v", s, index+1)
385+
ei := ExclusionInfo{
386+
Subject: &s,
387+
Message: fmt.Sprintf("For rule: \"%v\", subject excluded by a global exclusion (#%v) - \"%v\" ", rule.rule.Name, index+1, a.globalExclusions[index].exclusion.Comment),
388+
}
389+
report.ExclusionsInfo = append(report.ExclusionsInfo, ei)
365390
continue
366391
}
367392

@@ -389,20 +414,6 @@ func (a *analyzer) Analyze() (*AnalysisReport, error) {
389414
References: rule.rule.References,
390415
}
391416

392-
s := v1.Subject{}
393-
if kind, exist := sub["kind"]; exist {
394-
s.Kind = kind.(string)
395-
}
396-
if apiGroup, exist := sub["apiGroup"]; exist {
397-
s.APIGroup = apiGroup.(string)
398-
}
399-
if name, exist := sub["name"]; exist {
400-
s.Name = name.(string)
401-
}
402-
if namespace, exist := sub["namespace"]; exist {
403-
s.Namespace = namespace.(string)
404-
}
405-
406417
finding := AnalysisReportFinding{
407418
Subject: &s,
408419
Finding: info,

pkg/analysis/report.go

+9
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ type AnalysisReport struct {
1212
CreatedOn string
1313

1414
Findings []AnalysisReportFinding
15+
16+
ExclusionsInfo []ExclusionInfo
1517
}
1618

1719
type AnalysisStats struct {
@@ -45,3 +47,10 @@ type AnalysisFinding struct {
4547
//Documetation & additional reading references
4648
References []string
4749
}
50+
51+
type ExclusionInfo struct {
52+
Subject *v1.Subject
53+
54+
//Exclusion Message
55+
Message string
56+
}

0 commit comments

Comments
 (0)