Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scorecard - fixes for compatiblity with new config validator #586

Merged
merged 4 commits into from
Mar 2, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cli/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
github.com/posener/complete v1.2.1/go.mod h1:6gapUrK/U1TAN7ciCoNRIdVC5sbdBTUh1DKN0g6uH7E=
Expand Down Expand Up @@ -559,6 +560,7 @@ github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoH
github.com/stretchr/testify v0.0.0-20151208002404-e3a8ff8ce365/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/svanharmelen/jsonapi v0.0.0-20180618144545-0c0828c3f16d/go.mod h1:BSTlc8jOjh0niykqEGVXOLXdi9o0r0kR8tCYiMvjFgw=
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA=
Expand Down
21 changes: 20 additions & 1 deletion cli/scorecard/score.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"io"
"os"
"path/filepath"
"crypto/md5"

"github.com/forseti-security/config-validator/pkg/api/validator"
"github.com/forseti-security/config-validator/pkg/gcv"
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 = uniqueViolation(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,17 @@ func (inventory *InventoryConfig) Score(config *ScoringConfig, outputPath string

return nil
}

func uniqueViolation(violations []*validator.Violation) []*validator.Violation {
katze120 marked this conversation as resolved.
Show resolved Hide resolved
uniqueViolationMap := make(map[string]*validator.Violation)
for _, v := range violations {
b, _ := json.Marshal(v)
hash := md5.Sum(b)
uniqueViolationMap[string(hash[:])] = v
}
uniqueViolation := make([]*validator.Violation, 0, len(uniqueViolationMap))
for _, v := range uniqueViolationMap {
uniqueViolation = append(uniqueViolation, v)
}
return uniqueViolation
}
katze120 marked this conversation as resolved.
Show resolved Hide resolved
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