Skip to content

feat(policy-devel): extend eval command to show raw evaluation of messages#2357

Merged
Piskoo merged 15 commits intochainloop-dev:mainfrom
Piskoo:feat-policy-extend-eval-to-show-raw-evaluation
Aug 20, 2025
Merged

feat(policy-devel): extend eval command to show raw evaluation of messages#2357
Piskoo merged 15 commits intochainloop-dev:mainfrom
Piskoo:feat-policy-extend-eval-to-show-raw-evaluation

Conversation

@Piskoo
Copy link
Collaborator

@Piskoo Piskoo commented Aug 13, 2025

This PR changes the format of policy devel eval and adds additional feature to --debug flag.

  1. policy devel eval output is now an object instead of array of objects:

Before

[
   {
      "violations": [
         "test"
      ],
      "skip_reasons": [],
      "skipped": false
   }
]

After

{
   "result": {
      "skipped": false,
      "skipReasons": [],
      "violations": [
         "test"
      ]
   }
}
  1. --debug flag adds additional information to output in a debug_info field. It contains inputs field that holds json input material enriched with passed input args and chainloop metadata. Also raw_results field that contains full output out of opa eval which includes all rules.
{
   "result": {
      "skipped": false,
      "skipReasons": [],
      "violations": [
         "test"
      ]
   },
   "debug_info": {
      "inputs": [
         {
            "args": {
               "licenses": "AGPL"
            },
            "bomFormat": "CycloneDX",
            "chainloop_metadata": {
               "annotations": {
                  "chainloop.material.cas.inline": true,
                  "chainloop.material.name": "material",
                  "chainloop.material.type": "EVIDENCE"
               },
               "content": 
               (...) material content
         }
      ],
      "raw_results": [
         {
            "data.pck": {
              (...) #results
         },
         {
            "data.main": {
            (...) #results
            }
         }
      ]
   }
}

Closes #2353

Piskoo added 10 commits August 13, 2025 15:45
Signed-off-by: Sylwester Piskozub <sylwesterpiskozub@gmail.com>
Signed-off-by: Sylwester Piskozub <sylwesterpiskozub@gmail.com>
Signed-off-by: Sylwester Piskozub <sylwesterpiskozub@gmail.com>
Signed-off-by: Sylwester Piskozub <sylwesterpiskozub@gmail.com>
Signed-off-by: Sylwester Piskozub <sylwesterpiskozub@gmail.com>
Signed-off-by: Sylwester Piskozub <sylwesterpiskozub@gmail.com>
Signed-off-by: Sylwester Piskozub <sylwesterpiskozub@gmail.com>
Signed-off-by: Sylwester Piskozub <sylwesterpiskozub@gmail.com>
Signed-off-by: Sylwester Piskozub <sylwesterpiskozub@gmail.com>
Signed-off-by: Sylwester Piskozub <sylwesterpiskozub@gmail.com>
@Piskoo Piskoo marked this pull request as ready for review August 18, 2025 08:08
@Piskoo Piskoo requested review from javirln and migmartri August 18, 2025 08:08
@Piskoo
Copy link
Collaborator Author

Piskoo commented Aug 18, 2025

raw_results is missing requested script and result due to limitations of naming rego scripts. data.<package_name> returned by opa eval is used for verification, otherwise the output scripts could be labeled by their filename for non embedded type, but that causes problem for embedded scripts since there's no name attached to them.

Copy link
Member

@migmartri migmartri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Piskoo

cmd.Flags().StringVarP(&policyPath, "policy", "p", "policy.yaml", "Path to custom policy file")
cmd.Flags().StringSliceVar(&inputs, "input", []string{}, "Key-value pairs of policy inputs (key=value)")
cmd.Flags().StringSliceVar(&allowedHostnames, "allowed-hostnames", []string{}, "Additional hostnames allowed for http.send requests in policies")
cmd.Flags().BoolVarP(&debug, "debug", "", false, "Enable debug/verbose output and logging mode")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could probably be more explicit here, currently, the description seems like a generic debug mode, when in reality it does more than that.

SkipReasons []string `json:"skip_reasons"`
Skipped bool `json:"skipped"`
Ignored bool `json:"ignored,omitempty"`
Violations []string `json:"violations"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in your example in the description it seems that the json keys where capitalized, aren't they using these annotations?


// Try the main rule first
if err := executeQuery(mainRule, r.operatingMode == EnvironmentModeRestrictive); err != nil {
if err := executeQuery(fmt.Sprintf("%v.%s\n", parsedModule.Package.Path, mainRule), r.operatingMode == EnvironmentModeRestrictive); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fmt.Sprintf("%v.%s\n", parsedModule.Package.Path, mainRule looks likke it could be in a function

}

return parseViolationsRule(res, policy)
return parseViolationsRule(res, policy, rawData)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we make this optional? Meaning that you can initialize the engine to return raw data too but optionally?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense due to only policy devel eval making use of it, thanks

Piskoo added 3 commits August 19, 2025 13:35
… desc

Signed-off-by: Sylwester Piskozub <sylwesterpiskozub@gmail.com>
Signed-off-by: Sylwester Piskozub <sylwesterpiskozub@gmail.com>
Signed-off-by: Sylwester Piskozub <sylwesterpiskozub@gmail.com>
for _, policyEv := range policyEvs {
result := &EvalResult{
// Only one evaluation expected for a single policy attachment
policyEv := policyEvs[0]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you sure about this? Can't an evaluation contain info from multiple paths?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Results from a single policy attachment, evaluated against a single material, are merged into one evaluation result. So results from each path in the policy will be merged in the end.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick. ok, should we protect the code?

Copy link
Member

@migmartri migmartri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

double check my comment about number of evaluations. Thanks!

Copy link
Member

@migmartri migmartri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome

for _, policyEv := range policyEvs {
result := &EvalResult{
// Only one evaluation expected for a single policy attachment
policyEv := policyEvs[0]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick. ok, should we protect the code?

string org_name = 4;
}

repeated google.protobuf.Struct raw_results = 19;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a comment explainign what this is please

RawData *RawData
}
type RawData struct {
Input interface{}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check json rawmessage

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated types for RawData in the engine and also in proto file

Piskoo added 2 commits August 20, 2025 11:28
Signed-off-by: Sylwester Piskozub <sylwesterpiskozub@gmail.com>
Signed-off-by: Sylwester Piskozub <sylwesterpiskozub@gmail.com>
@Piskoo Piskoo requested a review from migmartri August 20, 2025 11:01
@Piskoo Piskoo merged commit 2cc25f3 into chainloop-dev:main Aug 20, 2025
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

extend eval command to show raw evaluation of messages

2 participants