---
title: "Gate a pipeline on a policy"
description: "Write a policy, ask TRUSTIVAN for a verdict on a scan, and make a build stop when the verdict says it should."
---

By the end of this tutorial your pipeline will scan an image, ask TRUSTIVAN
whether that scan satisfied your organisation's policy, and exit non-zero when
it did not, while still telling you the difference between "the build should
stop" and "TRUSTIVAN was unreachable".

It takes about twenty minutes. You need an account on TRUSTIVAN and an API
key; the [Quickstart](/getting-started/quickstart) gets you both.

## The division of labour, before you start

Two things happen in this tutorial, and they are done by two different
principals on purpose.

**A human writes the policy.** `policies:manage` is not grantable to an API
key. A compromised CI credential that could both request a verdict and rewrite
the policy it is judged by is a build grading its own homework, and the entire
control collapses to nothing.

**The pipeline only requests verdicts.** Its key holds `scans:create`,
`scans:read` and `verdicts:request`, and nothing else. It cannot author a
policy, and it cannot suppress the findings that failed it.

So steps 1 to 3 are done from a browser session: the dashboard, or `curl` with
the session cookies from the Quickstart. Steps 4 onward are what your CI job
runs.

There is **no TRUSTIVAN CLI**. The CI story is `curl` against a scoped key,
which is why everything below is a shell command you can paste.

## 1. Create a policy

A policy starts disabled and with no rules. A policy that began enforcing the
moment it was created would break a pipeline before its author had read it
back.

```bash Terminal
curl -s -X POST localhost:8080/api/v1/policies \
  -H 'Content-Type: application/json' \
  -b cookies.txt \
  -H "X-CSRF-Token: $(grep trustivan_csrf cookies.txt | awk '{print $7}')" \
  -d '{
        "name": "Release gate",
        "description": "What this organisation refuses to ship"
      }'
```

Keep the `id` from the response:

```bash Terminal
export POLICY_ID='...'
```

The policy's scope is `organization`. That is the only scope implemented today,
and it is the one a team's first policy needs: "what do we refuse to ship,
anywhere".

## 2. Add a version with rules

Rules are declarative structs, not code. There is no expression language and
no embedded interpreter: a rule is a set of conditions, **all** of which must
hold for a finding to match, plus an effect.

```bash Terminal
curl -s -X POST localhost:8080/api/v1/policies/$POLICY_ID/versions \
  -H 'Content-Type: application/json' \
  -b cookies.txt \
  -H "X-CSRF-Token: $(grep trustivan_csrf cookies.txt | awk '{print $7}')" \
  -d '{
        "note": "initial gate",
        "rules": [
          {
            "id": "no-criticals",
            "description": "No critical vulnerabilities",
            "effect": "fail",
            "min_severity": "critical"
          },
          {
            "id": "high-water-mark",
            "description": "At most five high vulnerabilities",
            "effect": "warn",
            "min_severity": "high",
            "min_count": 5
          }
        ]
      }'
```

Two rules, because OR is expressed by writing a second rule rather than by an
operator. That keeps each rule independently explainable in the verdict, which
is what a developer reads when the build stops.

Every rule was checked against the same inputs, and every rule is reported
back, including the ones that did not fire. Every populated condition on a
rule is ANDed: `min_severity`, `kinds`, `statuses`, `fixabilities`, `identifiers`,
`older_than_days` and `min_count`. A rule with **no** conditions is rejected at
authoring time, because it would match every finding, which is never what
anyone meant. A policy may hold at most 50 rules, and a version with no rules
at all is rejected too.

This version is now **immutable**. A database trigger rejects every `UPDATE`
and `DELETE` on it, unconditionally. Changing the policy later means adding
version 2; version 1 stays readable forever, so a verdict recorded today can
still be explained next year.

## 3. Enable it

```bash Terminal
curl -s -X PATCH localhost:8080/api/v1/policies/$POLICY_ID \
  -H 'Content-Type: application/json' \
  -b cookies.txt \
  -H "X-CSRF-Token: $(grep trustivan_csrf cookies.txt | awk '{print $7}')" \
  -d '{"enabled": true}'
```

At most one policy may be enabled per scope, enforced by a partial unique
index. Without that, "did this scan pass?" would have as many answers as there
are enabled policies.

## 4. Issue the pipeline's key

Back to a machine credential, with the smallest set of scopes that does the
job.

```bash Terminal
curl -s -X POST localhost:8080/api/v1/api-keys \
  -H 'Content-Type: application/json' \
  -b cookies.txt \
  -H "X-CSRF-Token: $(grep trustivan_csrf cookies.txt | awk '{print $7}')" \
  -d '{
        "name": "release-gate",
        "scopes": ["scans:create", "scans:read", "verdicts:request"],
        "expires_at": "2026-11-27T00:00:00Z"
      }'
```

`expires_at` is optional (omitted, it defaults to 90 days), but expiry itself
is not: there is no value meaning never. The secret is in the response and
nowhere else, ever. Put it straight into your CI provider's secret store.

Note what is absent. There is no `findings:suppress`, so this credential cannot
silence the findings that fail it. There is no `policies:manage`, because no
credential may hold it.

## 5. Request a scan

```bash Terminal
export TRUSTIVAN_KEY='twk_...'
export IMAGE='alpine:3.19'

curl -s -X POST localhost:8080/api/v1/scans \
  -H "Authorization: Bearer $TRUSTIVAN_KEY" \
  -H 'Content-Type: application/json' \
  -H "Idempotency-Key: build-1001" \
  -d "{\"image\": \"$IMAGE\"}"
```

`202` means the work was accepted, not that it is done. A `200` instead means
an existing scan was returned, so a retry that was deduplicated is
distinguishable from a fresh request. Use your build identifier as the
idempotency key: it is reserved before any work begins, so two concurrent
retries cannot both queue a scan.

## 6. Poll until the scan is terminal

```bash Terminal
export SCAN_ID='...'

for _ in $(seq 1 300); do
  scan=$(curl -sf "localhost:8080/api/v1/scans/$SCAN_ID" \
           -H "Authorization: Bearer $TRUSTIVAN_KEY") || exit 2
  status=$(printf '%s' "$scan" | jq -r '.scan.status')
  case "$status" in
    succeeded) break ;;
    failed)    echo "scan failed: $(printf '%s' "$scan" | jq -r '.scan.error')"; exit 2 ;;
  esac
  sleep 2
done
```

Give the loop a real ceiling. Pulling and unpacking a large image takes
minutes, and a poll loop with no bound is a build that hangs rather than one
that reports a problem.

The scan body also carries `database_state`, which reports how fresh the
advisory data behind the result was. A scan run against stale data reports "no
vulnerabilities" with exactly the same confidence as a correct one, which is
why TRUSTIVAN surfaces the age rather than hiding it.

## 7. Ask for the verdict

This is the only request the gate itself needs. It never has to look at a
single finding.

```bash Terminal
verdict=$(curl -s -o /tmp/verdict.json -w '%{http_code}' \
  -X POST "localhost:8080/api/v1/scans/$SCAN_ID/verdict" \
  -H "Authorization: Bearer $TRUSTIVAN_KEY")
```

`201` when a decision is recorded, `200` when a retry replays the one already
recorded. Idempotency here is a unique constraint on `(scan, policy version)`,
so a CI job that retries because its pod was preempted gets the same answer
rather than a second decision.

## 8. Branch on the body, not the status line

This is the most important step in the tutorial.

```bash Terminal
if [ "$verdict" -lt 200 ] || [ "$verdict" -ge 300 ]; then
  # TRUSTIVAN did not answer. This is an infrastructure problem, not a
  # security decision — do not treat it as a policy failure, and do not
  # treat it as a pass.
  echo "TRUSTIVAN is unreachable (HTTP $verdict)" >&2
  exit 2
fi

result=$(jq -r '.result'   /tmp/verdict.json)
summary=$(jq -r '.summary' /tmp/verdict.json)
blocking=$(jq -r '.blocking' /tmp/verdict.json)

echo "TRUSTIVAN: $result — $summary"

if [ "$blocking" = "true" ]; then
  exit 1     # the build should stop
fi
exit 0       # pass or warn
```

A **failing verdict is a successful request**. The HTTP status describes
whether TRUSTIVAN answered; the body describes what the answer was. A pipeline
that treated a policy failure as an HTTP error could not distinguish "the build
should stop" from "TRUSTIVAN is down", and those two demand opposite reactions.
The first fails the build. The second is retried, or escalated to a human, but
it is never quietly interpreted as a pass.

`result` is one of `pass`, `warn` or `fail`. Only `fail` blocks, and `blocking`
is precomputed in the body so your gate does not reimplement TRUSTIVAN's opinion
about which results stop a deployment. A warning that blocks is a failure with
a friendlier name.

## 9. Read why

When the gate stops a build, the reason is already in the response you have.

```bash Terminal
jq '.rules[] | {rule_id, effect, fired, matched, threshold}' /tmp/verdict.json
```

Every rule is reported, fired or not, with how many findings matched, the
threshold it was compared against, and a bounded sample of the findings
responsible. A passing verdict explains itself too: "what was checked?" is a
real question, especially the first time a policy is turned on.

## What you have now

A pipeline that stops on a policy failure, keeps running on a warning, and
fails loudly rather than silently when TRUSTIVAN cannot answer. The decision it
acted on is recorded, attributed to the key that requested it, and will still
be explainable after the policy changes.

From here:

- [Policies and verdicts](/concepts/policies-and-verdicts): the rule
  language in full, including what suppression does to a verdict.
- [CI pipelines](/integrations/ci-pipelines): the same recipe as GitHub
  Actions and GitLab CI jobs.
- [Triage a first backlog](/tutorials/triage-a-backlog): what to do
  about the findings the gate reported.
