Skip to content

Commit

Permalink
fix: violation not visible in json output (#314)
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Choudhary <[email protected]>
  • Loading branch information
vishal-chdhry authored Feb 23, 2024
1 parent acadce6 commit 7a0399b
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 15 deletions.
3 changes: 1 addition & 2 deletions pkg/commands/scan/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/kyverno/kyverno-json/pkg/policy"
"github.com/kyverno/kyverno/ext/output/pluralize"
"github.com/spf13/cobra"
"go.uber.org/multierr"
"k8s.io/apimachinery/pkg/labels"
)

Expand Down Expand Up @@ -89,7 +88,7 @@ func (c *options) run(cmd *cobra.Command, _ []string) error {
if rule.Error != nil {
out.println("-", policy.Policy.Name, "/", rule.Rule.Name, "/", rule.Identifier, "ERROR:", rule.Error.Error())
} else if len(rule.Violations) != 0 {
out.println("-", policy.Policy.Name, "/", rule.Rule.Name, "/", rule.Identifier, "FAILED:", multierr.Combine(rule.Violations...).Error())
out.println("-", policy.Policy.Name, "/", rule.Rule.Name, "/", rule.Identifier, "FAILED:", strings.Join(rule.Violations, "; "))
} else {
// TODO: handle skip, warn
out.println("-", policy.Policy.Name, "/", rule.Rule.Name, "/", rule.Identifier, "PASSED")
Expand Down
2 changes: 1 addition & 1 deletion pkg/json-engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type RuleResponse struct {
Rule v1alpha1.ValidatingRule
Identifier string
Error error
Violations []error
Violations []string
}

// PolicyResult specifies state of a policy result
Expand Down
15 changes: 7 additions & 8 deletions pkg/matching/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package matching

import (
"context"
"errors"

"github.com/jmespath-community/go-jmespath/pkg/binding"
"github.com/kyverno/kyverno-json/pkg/apis/policy/v1alpha1"
Expand All @@ -11,12 +10,12 @@ import (
"k8s.io/apimachinery/pkg/util/validation/field"
)

func MatchAssert(ctx context.Context, path *field.Path, match *v1alpha1.Assert, actual any, bindings binding.Bindings, opts ...template.Option) ([]error, error) {
func MatchAssert(ctx context.Context, path *field.Path, match *v1alpha1.Assert, actual any, bindings binding.Bindings, opts ...template.Option) ([]string, error) {
if match == nil || (len(match.Any) == 0 && len(match.All) == 0) {
return nil, field.Invalid(path, match, "an empty assert is not valid")
} else {
if len(match.Any) != 0 {
var fails []error
var fails []string
path := path.Child("any")
for i, assertion := range match.Any {
checkFails, err := assert.Assert(ctx, path.Index(i).Child("check"), assert.Parse(ctx, assertion.Check.Value), actual, bindings, opts...)
Expand All @@ -30,17 +29,17 @@ func MatchAssert(ctx context.Context, path *field.Path, match *v1alpha1.Assert,
if assertion.Message != "" {
msg := template.String(ctx, assertion.Message, actual, bindings, opts...)
msg += ": " + checkFails.ToAggregate().Error()
fails = append(fails, errors.New(msg))
fails = append(fails, msg)
} else {
fails = append(fails, checkFails.ToAggregate())
fails = append(fails, checkFails.ToAggregate().Error())
}
}
if fails != nil {
return fails, nil
}
}
if len(match.All) != 0 {
var fails []error
var fails []string
path := path.Child("all")
for i, assertion := range match.All {
checkFails, err := assert.Assert(ctx, path.Index(i).Child("check"), assert.Parse(ctx, assertion.Check.Value), actual, bindings, opts...)
Expand All @@ -51,9 +50,9 @@ func MatchAssert(ctx context.Context, path *field.Path, match *v1alpha1.Assert,
if assertion.Message != "" {
msg := template.String(ctx, assertion.Message, actual, bindings, opts...)
msg += ": " + checkFails.ToAggregate().Error()
fails = append(fails, errors.New(msg))
fails = append(fails, msg)
} else {
fails = append(fails, checkFails.ToAggregate())
fails = append(fails, checkFails.ToAggregate().Error())
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/server/model/response.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package model

import (
"strings"

jsonengine "github.com/kyverno/kyverno-json/pkg/json-engine"
"go.uber.org/multierr"
)

type Response struct {
Expand Down Expand Up @@ -51,7 +52,7 @@ func makeMessage(rule jsonengine.RuleResponse) string {
return rule.Error.Error()
}
if len(rule.Violations) != 0 {
return multierr.Combine(rule.Violations...).Error()
return strings.Join(rule.Violations, "; ")
}
return ""
}
4 changes: 2 additions & 2 deletions test/api/go/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"context"
"encoding/json"
"log"
"strings"

jsonengine "github.com/kyverno/kyverno-json/pkg/json-engine"
"github.com/kyverno/kyverno-json/pkg/policy"
"go.uber.org/multierr"
)

const policyYAML = `
Expand Down Expand Up @@ -70,7 +70,7 @@ func main() {
if rule.Error != nil {
logger.Printf("error: %s/%s -> %s: %s", policy.Policy.Name, rule.Rule.Name, rule.Identifier, rule.Error)
} else if len(rule.Violations) != 0 {
logger.Printf("fail: %s/%s -> %s: %s", policy.Policy.Name, rule.Rule.Name, rule.Identifier, multierr.Combine(rule.Violations...))
logger.Printf("fail: %s/%s -> %s: %s", policy.Policy.Name, rule.Rule.Name, rule.Identifier, strings.Join(rule.Violations, "; "))
} else {
logger.Printf("pass: %s/%s -> %s", policy.Policy.Name, rule.Rule.Name, rule.Identifier)
}
Expand Down

0 comments on commit 7a0399b

Please sign in to comment.