Skip to content

Commit

Permalink
Merge pull request #1139 from zregvart/pr/trace-print
Browse files Browse the repository at this point in the history
Print traces and debug messages from OPA
  • Loading branch information
zregvart authored Nov 3, 2023
2 parents fdc6682 + 8ac2d13 commit 3314714
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 9 deletions.
19 changes: 19 additions & 0 deletions acceptance/examples/trace_debug.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import future.keywords.contains
import future.keywords.if

# METADATA
# title: Debug
# description: This rule print to debug log
# custom:
# short_name: debuggy
# failure_msg: Prints and succeeds
deny contains result if {
print("here we are")
false
result := {
"code": "acceptance.debuggy",
"msg": "This should not happen",
}
}
32 changes: 32 additions & 0 deletions features/validate_image.feature
Original file line number Diff line number Diff line change
Expand Up @@ -896,3 +896,35 @@ Feature: evaluate enterprise contract
When ec command is run with "validate image --image ${REGISTRY}/acceptance/fetch-oci-blob --policy acceptance/ec-policy --public-key ${known_PUBLIC_KEY} --rekor-url ${REKOR} --show-successes"
Then the exit status should be 0
Then the output should match the snapshot

Scenario: tracing and debug logging
Given a key pair named "trace_debug"
And an image named "acceptance/trace-debug"
And a valid image signature of "acceptance/trace-debug" image signed by the "trace_debug" key
And a valid Rekor entry for image signature of "acceptance/trace-debug"
And a valid attestation of "acceptance/trace-debug" signed by the "trace_debug" key
And a valid Rekor entry for attestation of "acceptance/trace-debug"
And a git repository named "trace-debug" with
| main.rego | examples/trace_debug.rego |
And policy configuration named "ec-policy" with specification
"""
{
"sources": [
{
"policy": [
"git::https://${GITHOST}/git/trace-debug.git"
]
}
]
}
"""
When ec command is run with "validate image --image ${REGISTRY}/acceptance/trace-debug --policy acceptance/ec-policy --public-key ${trace_debug_PUBLIC_KEY} --rekor-url ${REKOR} --show-successes --trace"
Then the exit status should be 0
And the standard error should contain
"""
level=trace msg="\[data.main.deny\] Enter data.main.deny
"""
And the standard error should contain
"""
level=debug msg="\[data.main.deny\] .*/main.rego:13: here we are
"""
35 changes: 27 additions & 8 deletions internal/evaluator/conftest_evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,23 +163,42 @@ type conftestRunner struct {
}

func (r conftestRunner) Run(ctx context.Context, fileList []string) (result []Outcome, data Data, err error) {
if log.IsLevelEnabled(log.TraceLevel) {
r.Trace = true
}

var conftestResult []output.CheckResult
conftestResult, err = r.TestRunner.Run(ctx, fileList)
if err != nil {
return
}

for _, r := range conftestResult {
for _, res := range conftestResult {
if log.IsLevelEnabled(log.TraceLevel) {
for _, q := range res.Queries {
for _, t := range q.Traces {
log.Tracef("[%s] %s", q.Query, t)
}
}
}
if log.IsLevelEnabled(log.DebugLevel) {
for _, q := range res.Queries {
for _, o := range q.Outputs {
log.Debugf("[%s] %s", q.Query, o)
}
}
}

result = append(result, Outcome{
FileName: r.FileName,
Namespace: r.Namespace,
FileName: res.FileName,
Namespace: res.Namespace,
// Conftest doesn't give us a list of successes, just a count. Here we turn that count
// into a placeholder slice of that size to make processing easier later on.
Successes: make([]Result, r.Successes),
Skipped: toRules(r.Skipped),
Warnings: toRules(r.Warnings),
Failures: toRules(r.Failures),
Exceptions: toRules(r.Exceptions),
Successes: make([]Result, res.Successes),
Skipped: toRules(res.Skipped),
Warnings: toRules(res.Warnings),
Failures: toRules(res.Failures),
Exceptions: toRules(res.Exceptions),
})
}

Expand Down
2 changes: 1 addition & 1 deletion internal/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func InitLogging(verbose, quiet, debug, trace bool) {
var v string
switch {
case trace:
level = log.DebugLevel
level = log.TraceLevel
setupDebugMode()
v = "9"
case debug:
Expand Down

0 comments on commit 3314714

Please sign in to comment.