Skip to content

Commit

Permalink
updated error handeling in Rego validations
Browse files Browse the repository at this point in the history
Signed-off-by: santoshkal <ksantosh@intelops.dev>
  • Loading branch information
santoshkal committed Jun 11, 2024
1 parent d133ea5 commit aa0ea8f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
5 changes: 2 additions & 3 deletions cmd/regoval_dockerfileval.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ The required input Dockerfile and Rego policy files can be either be passed thr
such as those hosted on GitHub (e.g., https://github.com)
`,
Example: `
# Validate Dockerfil with Rego policies by providing the required args from local file system
# Validate Dockerfiles with Rego policies by providing the required args from local file system
./genval regoval dockerfileval --reqinput=input.json \
--policy=<'path/to/policy.rego file>
Expand Down Expand Up @@ -90,8 +90,7 @@ func runDockerfilevalCmd(cmd *cobra.Command, args []string) error {

err = validate.ValidateDockerfile(string(dockerfilefileContent), defaultRegoPolicies)
if err != nil {
log.Errorf("Dockerfile validation failed: %s\n", err)
return err
return fmt.Errorf("Dockerfile validation failed: %s\n", err)
}
} else {
err := validate.ValidateDockerfile(string(dockerfilefileContent), policy)
Expand Down
12 changes: 5 additions & 7 deletions pkg/validate/printresults.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package validate

import (
"encoding/json"
"errors"
"fmt"
"os"

"github.com/fatih/color"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/open-policy-agent/opa/rego"
"github.com/sirupsen/logrus"
)

// PrintResults prints the evaluation results along with the metadata
Expand All @@ -18,7 +18,6 @@ func PrintResults(result rego.ResultSet, metas []*regoMetadata) error {
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"Policy Name", "Status", "Description", "Severity", "Benchmark", "Category"})

var policyError error
var allResults []Results
var idCounter int

Expand All @@ -43,7 +42,7 @@ func PrintResults(result rego.ResultSet, metas []*regoMetadata) error {
} else {
saveStatus = "failed"
status = color.New(color.FgRed).Sprint("failed")
policyError = errors.New("policy evaluation failed: " + key)
logrus.Infof("\n" + color.New(color.FgRed).Sprintf("policy evaluation for %s failed", key))
}
} else {
// Handle other types of values (non-slice)
Expand All @@ -53,7 +52,7 @@ func PrintResults(result rego.ResultSet, metas []*regoMetadata) error {
} else {
saveStatus = "failed"
status = color.New(color.FgRed).Sprint("failed")
policyError = errors.New("policy evaluation failed: " + key)
logrus.Infof("\n" + color.New(color.FgRed).Sprintf("policy evaluation for %s failed", key))
}
}
t.AppendRow([]interface{}{key, status, meta.Description, meta.Severity, meta.Benchmark, meta.Category})
Expand All @@ -72,11 +71,10 @@ func PrintResults(result rego.ResultSet, metas []*regoMetadata) error {
}
} else {
fmt.Println("No policies passed")
policyError = errors.New("no policies passed")
logrus.Infof("\n" + color.New(color.FgRed).Sprintf("no policies passed"))
}
}

fmt.Println("Rendering table")
// Render the table after processing all results
t.Render()

Expand All @@ -87,7 +85,7 @@ func PrintResults(result rego.ResultSet, metas []*regoMetadata) error {
}
}

return policyError
return nil
}

type Results struct {
Expand Down

0 comments on commit aa0ea8f

Please sign in to comment.