From fa316de21614a72c4b157e21c2dff2091cf7fdef Mon Sep 17 00:00:00 2001 From: Keshi Tan Date: Mon, 2 Mar 2020 14:39:07 +0800 Subject: [PATCH] fix(cli): Update scorecard for compatibility with new config validator (#586) * remove duplicate violations * get constraint name from alternate location * style fixes * style fixes --- cli/scorecard/score.go | 22 +++++++++++++++++++++- cli/scorecard/violations.go | 1 + 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/cli/scorecard/score.go b/cli/scorecard/score.go index 0abf9ea6ea9..c0606c877d1 100644 --- a/cli/scorecard/score.go +++ b/cli/scorecard/score.go @@ -16,6 +16,7 @@ package scorecard import ( "context" + "crypto/md5" "encoding/csv" "encoding/json" "flag" @@ -85,7 +86,8 @@ func (cv constraintViolations) Count() int { } func (cv constraintViolations) GetName() string { - return cv.constraint.GetMetadata().GetStructValue().GetFields()["name"].GetStringValue() + return cv.Violations[0].Constraint + // return cv.constraint.GetMetadata().GetStructValue().GetFields()["name"].GetStringValue() } // RichViolation holds a violation with its category @@ -171,6 +173,9 @@ func (inventory *InventoryConfig) Score(config *ScoringConfig, outputPath string if err != nil { return err } + Log.Debug("AuditResult from Config Validator", "# of Violations", len(auditResult.Violations)) + auditResult.Violations = uniqueViolations(auditResult.Violations) + Log.Debug("AuditResult from Config Validator", "# of Unique Violations", len(auditResult.Violations)) err = config.attachViolations(auditResult) if err != nil { @@ -269,3 +274,18 @@ func (inventory *InventoryConfig) Score(config *ScoringConfig, outputPath string return nil } + +func uniqueViolations(violations []*validator.Violation) []*validator.Violation { + uniqueViolationMap := make(map[string]*validator.Violation) + for _, v := range violations { + b, _ := json.Marshal(v) + hash := md5.Sum(b) + uniqueViolationMap[string(hash[:])] = v + } + uniqueViolations := make([]*validator.Violation, 0, len(uniqueViolationMap)) + for _, v := range uniqueViolationMap { + uniqueViolations = append(uniqueViolations, v) + } + return uniqueViolations +} + diff --git a/cli/scorecard/violations.go b/cli/scorecard/violations.go index 7ac93849118..eb9a045c8c3 100644 --- a/cli/scorecard/violations.go +++ b/cli/scorecard/violations.go @@ -115,6 +115,7 @@ func getViolations(inventory *InventoryConfig, config *ScoringConfig) (*validato auditResult := &validator.AuditResponse{} for _, asset := range pbAssets { violations, err := config.validator.ReviewAsset(context.Background(), asset) + if err != nil { return nil, errors.Wrapf(err, "reviewing asset %s", asset) }