Skip to content

feat(dir): try a candidate content policy before deploying it - #2275

Merged
muscariello merged 13 commits into
mainfrom
feat/policy-dry-run
Oct 6, 2026
Merged

muscariello merged 13 commits into
mainfrom
feat/policy-dry-run

Conversation

@muscariello

Copy link
Copy Markdown
Member

Closes #2264. Part of #2205.

What this adds

Under strict enforcement, editing a policy hides every record until it is evaluated under the new rule, and a policy that wrongly rejects records hides them until it is fixed. A dry run tries a candidate policy on the node's real records before it is deployed and reports how many it would exclude, a sample of them, and why.

$ dirctl daemon policy dry-run --candidate ./candidate.yaml
Policy cel:has-description, version 87ac6c3e1834af20
  Evaluated:      1204 records
  Would pass:     1190
  Would exclude:  14 (12 rejected by the policy, 2 it could not evaluate)

  Records it would exclude (first 10 of 14; --samples lists more):
    baeareiaik2d74qrz...  ERROR: CEL expression evaluated to false: record.description != ""
    ...

Nothing was stored: no verdict was written and no policy version registered,
so what this node serves is unchanged.
  • Reads only. policy.DryRun reads through DryRunDB, an interface with no method that writes: it cannot store a verdict or register a version, so it cannot change what a read returns. It selects records under an ID no verdict is stored under, so every record is evaluated, including those the deployed policy of the same ID already has a verdict for.
  • The same evaluator and limits as the task. It reuses ValidatorEvaluator (feat(reconciler): evaluate records against OPA and CEL policies #2266) and the policy task's batch size and per-record timeout, so a record that takes too long fails here as it would there. A record the evaluator cannot judge, or that cannot be read, counts as excluded and is marked as such, since it would stay hidden.
  • Candidate file. A validators list as in the configuration, entries with op: ["evaluate"], so a good policy can be pasted into the configuration unchanged. It is read and checked before the node is touched.
  • Two entry points, one runner (reconciler/dryrun): dirctl daemon policy dry-run, and reconciler dry-run in the reconciler's own image, which has the database and the registry a Helm deployment keeps (the candidate can come on standard input with kubectl exec -i). Flags: --candidate, --policy-dir (default: the node's), --policy (one policy of the file), --samples, -o human|json.
  • The node has to be running. The records are read from its database and its registry. The command refuses to start, and says so, when nothing answers, rather than reporting every record as one the policy could not judge; the report also says that unreadable records can look like policy failures.
  • Docs: a "Trying a Policy Before You Deploy It" section in the runbook, and the command in the CLI reference.

Testing

  • policy.DryRun: counts and the sample (in CID order, with reasons); a failure counts as excluded; nothing is stored or registered; records are selected under the dry-run ID; paging; the sample size; an empty node; the per-record timeout; interruption returns the report so far without counting the record in hand; selection errors.
  • Runner, against a real SQLite database and real CEL and OPA policies: the report in both formats, only the samples asked for, one policy of a file, a file with no policy, an invalid or duplicated one, no extension (standard input), an unreachable store, bad options; no verdict is stored and no version registered after a run.
  • Command: it is under daemon policy, --candidate is required, an unreachable registry is refused with where it looked, a bad file is refused without touching the node.
  • End to end, in the policy suite against a live daemon: a candidate that rejects what the node serves reports that record and not the withheld one; a dry run of the deployed policy reports the record the node withholds and not the one it serves; and afterwards the served record is still served, the withheld one still withheld, and search still finds the first. Running it caught a bug the unit tests could not: opening the daemon's store directory as an OCI layout, instead of reading through its embedded registry, made every record unreadable.
  • Mutation-checked, every mutant compiling: selecting under the deployed policy's own ID, not excluding records the policy could not judge, counting failures as compliant, an unbounded sample, ignoring the record timeout (dies on the test timeout), counting an interrupted record, paging that does not advance (dies on the test timeout), ignoring the batch size or the policy filter, keeping non-evaluate entries, accepting an empty candidate, dropping would_exclude from the JSON, and dropping the "nothing was stored" line each make a test fail.
  • reconciler, cli, tests and server vet clean; reconciler and cli tests pass.

Not covered

The Kubernetes path (kubectl exec -i … /reconciler dry-run) is documented but was not run on a cluster; it shares the runner with the daemon command, which is tested end to end.

Under strict enforcement a policy that rejects too much hides those
records until it is fixed. DryRun evaluates an Evaluator against every
indexed record and reports how many the policy would exclude and, for
the first of them in CID order, why.

It reads through DryRunDB, which has no method that writes, so a dry run
cannot store a verdict or register a version, and so cannot change what
a read returns. It selects the records under an ID no verdict is ever
stored under, so every record is evaluated, including those the deployed
policy of the same ID already has a verdict for. It evaluates with the
policy task's batch size and per-record timeout, so a record that takes
too long fails here as it would there. A record the evaluator cannot
judge counts as excluded, since it would stay hidden, and is told apart
in the report. When the context ends the report so far is returned with
the error, and the record in hand is not counted.

Refs #2264, #2205

Signed-off-by: Luca Muscariello <[email protected]>
The dry run needs a way to be run. The candidate is a file with a
validators list, as in the configuration, whose entries have op
evaluate, so a policy that proves good can be pasted into the
configuration unchanged. It is read and checked before the node is
touched, and the policies are built as the reconciler builds them.

dirctl daemon policy dry-run runs it against the daemon, and
reconciler dry-run in the reconciler's own image, which has the database
and the registry a Helm deployment keeps. Both read the node's records
from its database and its registry, so the node has to be running; they
refuse to start, and say so, when nothing answers, since a record that
cannot be read is not a record the policy rejects. The report lists the
count, a sample and the reasons, as text or JSON (-o human|json, as the
other dirctl commands), and says that nothing was stored.

The runbook and the CLI reference describe it, including how to run it in
a pod with the candidate on standard input.

Refs #2264, #2205

Signed-off-by: Luca Muscariello <[email protected]>
…nd changes nothing

Runs dirctl daemon policy dry-run against the policy suite's daemon. A
candidate that rejects what the node serves reports that record, and not
the one the node withholds; the policy the node already enforces reports
the record the node withholds and not the one it serves, so the report
can be checked against what a read does; and after both, the served
record is still served, the withheld one still withheld, and search
still finds the first.

Running it caught that a dry run must read through the daemon's registry
and not open the store directory itself, which the embedded registry owns.

Closes #2264. Refs #2205

Signed-off-by: Luca Muscariello <[email protected]>
@muscariello
muscariello requested a review from a team as a code owner October 2, 2026 16:50
@github-actions github-actions Bot added the size/L Denotes a PR that changes 1000-1999 lines label Oct 2, 2026
…in the dry-run tests

wsl_v5 wants a blank line between a declaration and the loop that fills it,
and modernize's embedlit objects to naming the embedded AuthConfig type in
a struct literal, so the field is set after the literal instead.

Fixes the Lint job failure on #2275.

Signed-off-by: Luca Muscariello <[email protected]>
…literal

modernize's embedlit objects both to naming the embedded AuthConfig type
in a struct literal and to assigning its field after the literal: with Go
1.27 the promoted field is set in the literal itself.

Fixes the Lint job failure on #2275.

Signed-off-by: Luca Muscariello <[email protected]>
tagalign wants the mapstructure tags of consecutive fields in one column.

Fixes the Lint job failure on #2275.

Signed-off-by: Luca Muscariello <[email protected]>
Error assertions use require, and a cleanup that restores a variable is
separated from the declaration by a blank line.

Fixes the Lint job failure on #2275.

Signed-off-by: Luca Muscariello <[email protected]>
…eads

The dry-run suite waited until the node served the record the policy passes
and took that to mean both records had their verdicts. Until the policy is
enforced the node serves every record, so when this suite ran before the
enforcement suite the wait returned at once and the read of the rejected
record succeeded. Wait for the rejected record to be refused first.

Signed-off-by: Luca Muscariello <[email protected]>
@codecov

codecov Bot commented Oct 2, 2026 •

Copy link
Copy Markdown

The reconciler binary's dry-run entry point had no unit test: only the
dirctl wrapper was exercised, by the E2E suite. Cover the bad-input paths,
which must fail before the node is opened, the unreachable store, and a
report on an empty node.

Signed-off-by: Luca Muscariello <[email protected]>
@csirmazbendeguz

Copy link
Copy Markdown
Contributor

Do we have to nest this under dirctl daemon ? What if I want to test a Kubernetes deployment? dirctl policy would make more sense to me

@muscariello

Copy link
Copy Markdown
Member Author

Kubernetes has its own command, so you don't need dirctl daemon there. Run this against your cluster:

kubectl exec -i deploy/<reconciler> -- /reconciler dry-run --candidate /dev/stdin < candidate.yaml

It's the same dry run, started inside the reconciler pod, which already has the database and the registry. dirctl daemon policy dry-run is the version for a local daemon. Both are described in the PR description and in the runbook section "Trying a Policy Before You Deploy It".

I haven't run the Kubernetes command on a real cluster yet. It uses the same code as the daemon command, which is tested end to end.

@csirmazbendeguz

Copy link
Copy Markdown
Contributor

It would be better to have a single command, we can add a dry-run RPC for this, that would be best IMO

@muscariello

Copy link
Copy Markdown
Member Author

Agreed, a single command through a dry-run RPC is the better end state.

That RPC is a new service with its own authorization and limits, so I'd rather do it in a separate PR. This PR already gives operators a working dry run now, on a local daemon and on Kubernetes.

I'll open a follow-up issue for the RPC and link it here. OK to merge this one first?

@muscariello

Copy link
Copy Markdown
Member Author

Follow-up filed: #2290, the dry-run RPC and a single dirctl policy dry-run command.

@muscariello

Copy link
Copy Markdown
Member Author

@csirmazbendeguz I think merging the commands is a bad idea. If we do that we have to create an RPC in the server which brings only bad performance to the overall system. Offloading to clients it's the right approach as the load becomes pull based.

@csirmazbendeguz

Copy link
Copy Markdown
Contributor

I see. We can proceed with this for now, and do some refactoring later when we can evaluate policies efficiently...

The real issue is that evaluating policies is slow. We'll need to address that later. OPA policies can be translated to SQL, which would make evaluation as simple as a SQL query. There are also SQL translators for CEL.

@muscariello
muscariello added this pull request to the merge queue Oct 6, 2026
Merged via the queue into main with commit 50d0749 Oct 6, 2026
37 checks passed
@muscariello
muscariello deleted the feat/policy-dry-run branch October 6, 2026 11:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/L Denotes a PR that changes 1000-1999 lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dry-run a policy against a node's records before deploying it

2 participants