Skip to content

Commit

Permalink
fix(cli): Update scorecard for compatibility with new config validator (
Browse files Browse the repository at this point in the history
#586)

* remove duplicate violations

* get constraint name from alternate location

* style fixes

* style fixes
  • Loading branch information
katze120 authored Mar 2, 2020
1 parent c434f1f commit fa316de
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
22 changes: 21 additions & 1 deletion cli/scorecard/score.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package scorecard

import (
"context"
"crypto/md5"
"encoding/csv"
"encoding/json"
"flag"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

1 change: 1 addition & 0 deletions cli/scorecard/violations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit fa316de

Please sign in to comment.