Skip to content

feat(dir): serve only records that comply with every enforced policy - #2235

Merged
muscariello merged 1 commit into
feat/policy-evaluation-taskfrom
feat/policy-read-gate
Oct 2, 2026
Merged

muscariello merged 1 commit into
feat/policy-evaluation-taskfrom
feat/policy-read-gate

Conversation

@muscariello

@muscariello muscariello commented Sep 30, 2026 •

Copy link
Copy Markdown
Member

Part of #2208 (first of two PRs), part of #2205. Stacked on #2229, which is stacked on #2226: main ← #2226 ← #2229 ← this. Based on feat/policy-evaluation-task; each PR retargets to main as the one below it merges.

What this adds

A policy gate in the database read path. For each enforced policy, a record needs an evaluated, compliant verdict at the version in force to be returned.

  • Fails closed. It requires a positive verdict rather than excluding negative ones, so a record never evaluated, whose evaluation failed, or compliant only under a superseded version is excluded too. It checks both status and compliant, as types.EvaluatedPolicyStatuses requires of every read-path filter, rather than relying on UpsertPolicyEvaluation clearing compliant on failed rows.
  • Unconditional. Applied in handleFilterOptions, which every search, catalog and resolve query passes through (GetRecords, GetRecordCIDs, CountRecords, GetCatalogEntries, CountCatalogEntries), so no caller can forget it and no caller-supplied filter can lift it — asking for an excluded record by CID or by name returns nothing.
  • Covers the listings too. ListFilterValues and ListCatalogTags read the skill, domain, module and author values directly; they're gated as well, or they'd list values that only excluded records carry.
  • Doesn't hide records from evaluation. The reconciler's GetRecordsNeedingPolicyEvaluation has its own query and stays ungated. If it were gated, a record with no verdict would never be evaluated, and so never served.

The enforced set is supplied at construction with WithEnforcedPolicies(func() []types.EnforcedPolicy) and read per query. Nothing supplies it yet, so behaviour is unchanged until a policy engine does — the same source should build both the reconciler's evaluators (#2229) and this set, so the two can't disagree about which policies exist.

Deviation from the issue text

#2208 suggests a NOT EXISTS against the verdict table, shaped like applyExcludedScanSeverities. That excludes records with a non-compliant row and lets records with no row through — fail-open, contradicting #2205's rule that "no verdict yet" must mean excluded. This uses EXISTS of a compliant row per enforced policy instead. The mutation check below confirms the NOT EXISTS shape fails the fail-closed tests.

Consequences worth knowing

  • A policy version bump excludes every record until it's re-evaluated under the new version — a blackout as long as the backfill takes. That follows from the guarantee (a verdict under v1 says nothing about v2), but Stage the read-path enforcement rollout: shadow mode, fail-closed, audited bypass #2209's rollout should handle it, e.g. by evaluating under the new version before it becomes the enforced one.
  • Publication follows the gate. The publication worker selects records through the same query, so a record that's out of policy or not yet evaluated isn't announced to the network: nothing is advertised that wouldn't be served.
  • The skill publisher's existence check goes through the same query; a gated lookup only means an idempotent re-push.

Not in this PR

Testing

8 tests in policy_gate_test.go: nothing enforced changes nothing (no gate, and an empty set); only a current compliant verdict is served (non-compliant, no verdict, failed-but-marked-compliant, and stale-version records excluded); every enforced policy must pass; verdicts of unenforced policies don't count; no filter can lift the gate; the enforced set is read per query; the reconciler's selector still sees unevaluated records; and every read — records, count, catalog entries, catalog count, filter values (skills and authors), catalog tags — is gated, with an ungated pass first so the gated counts are provably not vacuous.

Mutation-checked, each mutant compiling: the issue's NOT EXISTS shape, a vacuous status check, a vacuous version check, and removing the gate from handleFilterOptions, distinctColumn or distinctAuthors each make at least one test fail. Full server suite passes; go vet and gofmt clean.

@muscariello
muscariello requested a review from a team as a code owner September 30, 2026 07:46
@muscariello muscariello added kind/feature Categorizes issue or PR as related to a new feature. area/dir/server kind/security Categorizes issue or PR as related to security. labels Sep 30, 2026
@github-actions github-actions Bot added the size/M Denotes a PR that changes 200-999 lines label Sep 30, 2026
@muscariello
muscariello changed the base branch from feat/policy-evaluation-schema to feat/policy-evaluation-task September 30, 2026 07:53
@muscariello
muscariello added this pull request to stack #2234 September 30, 2026 08:05
@codecov

codecov Bot commented Sep 30, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.46154% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
server/database/gorm/filter_values.go 70.0% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

@csirmazbendeguz

csirmazbendeguz commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Unconditional. Applied in handleFilterOptions, which every search, catalog and resolve query passes through (GetRecords, GetRecordCIDs, CountRecords, GetCatalogEntries, CountCatalogEntries), so no caller can forget it and no caller-supplied filter can lift it

It's a good default, but admins should be able to see which records are "quarantined" somehow. Same way you can also view quarantined files in Windows Defender. I was thinking of a search flag, but we may want to have a separate command for this instead e.g. dirctl policies (I wrote about this in #2128)

A policy version bump excludes every record until it's re-evaluated under the new version — a blackout as long as the backfill takes.

I'm not sure this is acceptable, this is quite disruptive. We could ignore the policy version here, check if the record has a positive verdict (latest policy version or not). If a policy is updated, it would take a couple minutes for it to apply but that's expected. As an admin / user, that's how I would expect it to work

@muscariello
muscariello force-pushed the feat/policy-read-gate branch from db58072 to 6b8f23d Compare October 1, 2026 15:12
@muscariello

Copy link
Copy Markdown
Member Author

@csirmazbendeguz Quarantined records: the audit API in #2253 is the view; a dirctl command over it is a follow-up. The blackout is intentional, since a verdict under another version doesn't count. It's narrower now: versions are followed automatically, a new policy isn't enforced until every record has a verdict, and dir_policy_gate_records_unevaluated shows what's hidden.

csirmazbendeguz
csirmazbendeguz previously approved these changes Oct 2, 2026

@csirmazbendeguz csirmazbendeguz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ok, lgtm

@muscariello
muscariello dismissed csirmazbendeguz’s stale review October 2, 2026 12:05

The merge-base changed after approval.

@muscariello
muscariello force-pushed the feat/policy-read-gate branch from 0af7426 to 482e206 Compare October 2, 2026 12:05
@muscariello
muscariello force-pushed the feat/policy-read-gate branch from 482e206 to ddf40ac Compare October 2, 2026 12:33
A policy gate in the shared record query builder: for each enforced
policy, a record needs an evaluated, compliant verdict at the version
in force. It fails closed, requiring a positive verdict rather than
excluding negative ones, so a record never evaluated, whose evaluation
failed, or compliant only under a superseded version is excluded too.

Applied in handleFilterOptions, which every search, catalog and
resolve query passes through, so no caller-supplied filter can lift
it; and in the distinct-value listings behind ListFilterValues and
ListCatalogTags, which would otherwise list skills, domains and
authors that only excluded records carry. The reconciler's selector
of records to evaluate has its own query and stays ungated.

The enforced set is supplied at construction (WithEnforcedPolicies)
and read per query. Nothing supplies it yet, so behaviour is
unchanged until a policy engine does.

Refs #2208, #2205

Signed-off-by: Luca Muscariello <[email protected]>
@muscariello
muscariello added this pull request to the merge queue Oct 2, 2026
Merged via the queue into main with commit e86eca7 Oct 2, 2026
55 of 61 checks passed
@muscariello
muscariello deleted the feat/policy-read-gate branch October 2, 2026 15:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/dir/server kind/feature Categorizes issue or PR as related to a new feature. kind/security Categorizes issue or PR as related to security. size/M Denotes a PR that changes 200-999 lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants