kosli evaluate policy command lets you evaluate Kosli trails against custom policies written in . This is useful for enforcing rules like “every artifact must have an approved pull request” or “all security scans must pass”, and for gating deployments in CI/CD pipelines based on those rules. Kosli evaluates the policy on the server, against the trails as Kosli recorded them, and the CLI prints the result.
kosli evaluate trail and kosli evaluate trails are deprecated. Use kosli evaluate policy --context trail=<flow>/<trail> instead. The older commands evaluate the policy in the CLI and pass it a different input. See Local evaluation for the differences.1
Prerequisites
To follow this tutorial, you need to:
- Install Kosli CLI.
- Get a Kosli API token.
- Have a flow with a few trails that carry a pull request attestation, and a trail with a Snyk container scan attestation for the later steps.
-
Set the
KOSLI_API_TOKENandKOSLI_ORGenvironment variables:
Kosli runs the policy with a built-in Rego evaluator, so you only need to write a
.rego policy file. You need OPA installed only to test a policy on your machine with opa eval, as in the step on passing parameters.2
Write a policy
Create a file called This policy applies three design rules that every evaluate policy should follow.Rule 1: If the
pr-approved.rego with the following content:pr-approved.rego
default allow := false (fail safe)Trails are denied unless the policy explicitly allows them. Anything the policy cannot positively verify is treated as non-compliant. This matches Kosli’s compliance direction: a false non-compliant blocks a good trail (recoverable); a false compliant passes a bad one (not recoverable).The alias pr_attestation_name := data.params.pr_attestation_name reads the attestation name from a params file rather than hardcoding it. Different orgs and flows use different names for their pull-request attestation (for example "pull-request" or "pr"). If the param is absent, pr_attestation_name is undefined, the lookup into attestations_statuses fails, trail_is_approved does not fire, and allow stays false. That is the correct fail-safe.Rule 2: Drive allow via a positive assertion, not the absence of violationsallow fires through trail_is_approved, which makes a positive claim: every PR has at least one approver. It is never driven by count(violations) == 0.The following pattern looks equivalent but is not safe:violations rule body references a field that does not exist (a typo in pull_requests, an unexpected schema change, a missing key), the rule body silently produces no messages. The violations set is empty, count(violations) == 0 is true, and allow fires even though no PRs were actually checked. The trail receives a false-positive compliant result.With the safe pattern, if pull_requests is undefined, every pr in ... fails to evaluate, trail_is_approved does not fire, and allow stays false.Rule 3: Violations provide diagnostics onlyviolations explains why a policy was denied. It does not decide whether it was denied. When a violations rule body encounters an undefined reference, it silently produces no message. This is the safe failure mode: you lose a diagnostic, not a compliance check.The # METADATA and # entrypoint: true annotation on violations makes Kosli return the rule in the evaluation result, so the CLI can print the messages when the policy denies.See the Rego Policy reference for the full policy contract, input data shape, and exit code behavior.
3
Evaluate multiple trails
Evaluate several trails of one flow against the policy. Name each trail with its own If any pull request has no approvers, the policy denies and the CLI prints the violations. The CLI also prints the policy that ran, by its digest, which is left out of the outputs in this tutorial:When every pull request has at least one approver, the policy allows:
--context trail=<flow>/<trail>. Kosli evaluates them all at the same instant and passes them to the policy as input.trails. Replace the attestation name in --params with the name your flow uses for its pull request attestation, such as "pull-request" or "pr":4
Evaluate a single trail
When the context names exactly one trail, Kosli also passes it to the policy as The trail has zero high-severity vulnerabilities, so the policy allows it.
input.trail. Use this to check facts within a single trail. For example, you might check that a Snyk container scan found no high-severity vulnerabilities.Save this as snyk-no-high-vulns.rego:snyk-no-high-vulns.rego
allow fires only when artifact_scan_is_clean succeeds for every artifact. If snyk-container-scan is absent or processed_snyk_results is undefined, artifact_scan_is_clean fails to fire and allow stays false. Replace snyk-container-scan with the name of the Snyk attestation in your flow.Evaluate one trail:The full input data shape, including
input.trail and input.trails, is documented in the Rego Policy reference.5
Pass parameters to a policy
Policies often need thresholds that vary by environment, stricter in production than in staging. Use the Why the aliases at the top matterIf Save the thresholds for each environment as params files:Evaluate with permissive staging thresholds. Apply stricter production thresholds:Now verify the fail-safe: omit
--params flag to pass these values as data.params rather than hardcoding them in the policy.Save this as snyk-severity-threshold.rego:snyk-severity-threshold.rego
max_high := data.params.max_high is not just shorthand. In the compliance path, result.high_count <= max_high is a positive bound check. If max_high is absent from the params file, this condition is undefined, artifact_within_threshold fails to fire, and allow stays false. That is the correct fail-safe behavior.Compare this to a policy that drives allow through the absence of violations:data.params.max_high is absent, the violations rule body fails silently, the set stays empty, and allow fires. A misconfigured params file grants compliance rather than denying it.Testing locally with opa evalYou can test a policy on your machine without a real trail using opa eval, which needs OPA installed. Create a minimal input file:--data params:params-staging.json loads the file under data.params, where Kosli puts the params of an evaluation:max_high from params entirely:allow is false even though no violation message was produced. The missing param causes the compliance check to fail, not to vacuously pass. Always verify this explicitly when writing a policy that relies on params.Pass the same params files to kosli evaluate policy with the @ prefix. Parameters are always available as data.params in the policy:6
Explore the policy input
When writing policies, it helps to see exactly what data is available. Every evaluation has an ID. Run the evaluation with Then use From there, explore
--output json to print it as Kosli returns it, including its id:kosli get evaluation with --show-input to read the input the policy received. Pipe it through jq to explore the structure:.input.trail.compliance_status to find the attestation fields your policy can read.See Replaying an evaluation to write the input, params and policy of an evaluation to a directory and replay it with OPA.
7
Use in CI/CD
By default, This pattern lets you enforce custom compliance rules as part of your delivery pipeline, using the same trail data that Kosli already collects.
kosli evaluate policy prints the result and exits with 0 whether the policy allows or denies. Add --assert to exit with 1 when the policy denies, which makes it a pipeline gate. An evaluation that fails also exits with 1. See the Rego Policy reference for details.8
Record the evaluation
After evaluating a trail, you can record the result in Kosli as an audit record.If the policy decides a control, publish it with This creates a generic attestation on the trail with:
kosli publish rego-policy and evaluate it with --identifier and --control. Kosli records the outcome as a decision against that control, in the --flow and --trail you name. Only a published policy can record a decision. See Working with controls for the full workflow.Otherwise, record the result as a generic attestation. This step requires write access to your Kosli org. The examples below use variables you’d set in your CI/CD pipeline, and my-policy.rego as a placeholder for your own policy file:--compliantset based on whether the policy allowed or denied, read from the JSON report--attachmentscontaining the Rego policy (for reproducibility) and the JSON evaluation report, which includes the evaluation ID so you can read its input later withkosli get evaluation--user-datacontaining the violations, which appear in the Kosli UI as structured metadata on the attestation
What you’ve accomplished
You have written OPA/Rego policies using the three design rules that prevent false-positive compliance results: fail-safe default, compliance via positive assertion, and violations as diagnostics only. You’ve evaluated Kosli trails against those policies withkosli evaluate policy, tested safety properties locally with opa eval, and recorded evaluation results as attestations.
From here you can:
- Explore evaluated trails in the Kosli app
- Gate deployments in CI/CD pipelines using
kosli evaluate policy --assert - Use environment-specific params files to enforce different thresholds per environment
- Publish your policies and record decisions against controls
- Extend your policies to check other attestation types. See
kosli evaluate policyfor the full flag reference