Skip to content

Commit

Permalink
Merge pull request #46 from j-fuentes/fix-unmarshalling-from-rego
Browse files Browse the repository at this point in the history
Change how results are unmarshaled from rego
  • Loading branch information
jetstack-bot authored Jan 14, 2020
2 parents 7171fdd + 3e463b0 commit 526ec43
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
15 changes: 12 additions & 3 deletions pkg/results/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,19 +129,28 @@ func NewResultCollectionFromRegoResultSet(rs *rego.ResultSet) (*ResultCollection
for _, k := range keys {
var violations []string
boolValid, boolOk := values[k].(bool)
strings, stringsOk := values[k].([]string)
sliceValid, sliceOk := values[k].([]interface{})
if boolOk {
log.Printf("Using a boolean for `Value` is deprecated, found for key: (%s)", k)
if boolValid {
violations = []string{}
} else {
violations = []string{"missing violation context in rule definition"}
}
} else if stringsOk {
violations = strings
} else if sliceOk {
violations = make([]string, len(sliceValid))
for idx, e := range sliceValid {
violation, ok := e.(string)
if !ok {
// worst case scenario, use string representation of the variable
violation = fmt.Sprintf("%+v", e)
}
violations[idx] = violation
}
} else {
return nil, fmt.Errorf("format error, cannot unmarshall value '%+v' to bool or []string", values[k])
}

rc = append(rc, &Result{
ID: k,
Value: violations,
Expand Down
14 changes: 8 additions & 6 deletions pkg/results/results_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,13 @@ func TestNewResultCollectionFromRegoResultSet(t *testing.T) {
regoResultSet := &rego.ResultSet{
rego.Result{Expressions: []*rego.ExpressionValue{&rego.ExpressionValue{
Value: map[string]interface{}{
"_1_4_3": []string{},
"_1_4_4": []string{"violation"},
"_1_4_5": []string{},
"_1_4_6": []string{},
"node_pools_with_legacy_endpoints_enabled": []string{},
"node_pools_without_cloud_platform_scope": []string{"violation"},
"_1_4_3": []interface{}{},
"_1_4_4": []interface{}{"violation"},
"_1_4_5": []interface{}{},
"_1_4_6": []interface{}{},
"node_pools_with_legacy_endpoints_enabled": []interface{}{},
"node_pools_without_cloud_platform_scope": []interface{}{"violation"},
"something_returning_a_map": []interface{}{map[string]string{"bar": "foo"}},
},
Text: "data.package.name",
}}},
Expand All @@ -154,6 +155,7 @@ func TestNewResultCollectionFromRegoResultSet(t *testing.T) {
&Result{ID: "_1_4_6", Value: []string{}, Violations: []string{}, Package: "package.name"},
&Result{ID: "node_pools_with_legacy_endpoints_enabled", Value: []string{}, Violations: []string{}, Package: "package.name"},
&Result{ID: "node_pools_without_cloud_platform_scope", Value: []string{"violation"}, Violations: []string{"violation"}, Package: "package.name"},
&Result{ID: "something_returning_a_map", Value: []string{fmt.Sprintf("%+v", map[string]string{"bar": "foo"})}, Violations: []string{fmt.Sprintf("%+v", map[string]string{"bar": "foo"})}, Package: "package.name"},
}

rc, err := NewResultCollectionFromRegoResultSet(regoResultSet)
Expand Down

0 comments on commit 526ec43

Please sign in to comment.