Skip to content

feat(dir): enforce content policies per kind of read, with a shadow mode - #2248

Merged
muscariello merged 5 commits into
feat/registry-credentials-peersfrom
feat/policy-enforcement-modes
Oct 2, 2026
Merged

muscariello merged 5 commits into
feat/registry-credentials-peersfrom
feat/policy-enforcement-modes

Conversation

@muscariello

@muscariello muscariello commented Sep 30, 2026 •

Copy link
Copy Markdown
Member

Part of #2209 (first PR: stages 1 and 3, and the metric). Part of #2205. Stacked on #2246: main ← #2226 ← #2229 ← #2235 ← #2237 ← #2238 ← #2241 ← #2246 ← this.

Why

The read gate from #2208 had no way to be turned on: nothing supplied the enforced policies. #2209 asks for a staged rollout, so turning it on starts with seeing what it would hide.

What this adds

  • policy.enforcement config. Lists the enforced policy IDs and gives each kind of read its own mode, off (default), shadow or enforce:
    • search: search, the catalog, filter values, publication by query or of every record.
    • fetch: pull, lookup, referrers, export, the peer RPC, publication of explicit CIDs, identity status lookups.
  • Versions are followed, not configured. A policy's version comes from its content, and the reconciler registers it (feat(reconciler): evaluate records against content policies #2229). The server rechecks every refresh_interval (default 30s) and enforces the current version strictly: a verdict under any other version does not count. After a policy is edited, a record stays hidden until it has been evaluated under the new rule, so nothing the new rule rejects is served meanwhile; content already delivered cannot be recalled. A policy no evaluator has registered is not enforced and is logged once.
  • Shadow mode checks each fetch but serves every record, even if the check itself fails. Searches in shadow mode are not filtered.
  • A served view of the database. The APIs read through a view that applies the enforcement. The database itself applies none, because ingestion and the reconciler read through it and must see every record. In daemon mode the reconciler shares the server's database, so without this it would have been filtered too.
  • Fail-safe startup. A database that can't enforce the policies stops the server from starting, rather than serving unchecked records. Enforcing with authz disabled logs a warning, because registry credentials then let any caller read excluded records (Restrict registry credentials to peer nodes #2245).
  • Metrics, when the metrics server is on, labelled outcome="would_exclude" in shadow mode and outcome="excluded" when enforcing:
    • dir_policy_gate_search_records_excluded: a gauge of indexed records searches exclude, counted at each scrape. It is left out, not failed, if the count errors.
    • dir_policy_gate_fetches_excluded_total: a counter of reads by CID the policies refused.
  • Settings exposed in Helm (both charts), the daemon config and the docker env. The policy IDs can also be set from the environment, comma-separated.

Rest of #2209

Testing

  • Search and fetch modes are independent: each of five mode combinations filters only its own kind of read.
  • The observer hears of each excluded fetch, under the mode that excluded it or would have.
  • A failing check serves the record in shadow mode and refuses it when enforcing.
  • The database a served view came from still returns every record.
  • CountRecordsExcluded counts non-compliant and unevaluated records.
  • The metrics are gathered through a pedantic registry: the gauge follows the search mode and is absent when off or when counting fails; fetches are counted by outcome from zero.
  • The server serves the database itself when every mode is off and a separate gated view otherwise; it refuses a database it can't gate and an invalid config.
  • Enforcement config validation (modes, missing and duplicate policy IDs, the refresh interval) and env loading of the modes, the IDs and the interval.
  • The resolver: the registered version of each policy is enforced, an unregistered policy is pending, an edit is enforced at once, a late registration is picked up, and rechecks happen in the background with failures keeping the last result.
  • The existing gate tests pass unchanged on the served view.
  • Mutation-checked, every mutant compiling: gating search whatever its mode, excluding in shadow mode, never telling the observer, excluding on a shadow check error, counting compliant instead of excluded records, labelling shadow as excluded, and serving the ungated database (two ways) each make a test fail.
  • server, reconciler, cli and tests vet clean. The affected server packages and cli/cmd/daemon pass with CGO_ENABLED=0, which avoids the local SDK link failure. The first commit builds and passes on its own.

@muscariello
muscariello requested a review from a team as a code owner September 30, 2026 20:29
@muscariello muscariello added 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 added this pull request to stack #2234 September 30, 2026 20:31
@github-actions github-actions Bot added size/L Denotes a PR that changes 1000-1999 lines and removed size/M Denotes a PR that changes 200-999 lines labels Sep 30, 2026
@codecov

codecov Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.66082% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
server/policy_enforcement.go 93.9% 2 Missing ⚠️
server/database/gorm/policy_gate.go 96.6% 1 Missing ⚠️
server/server.go 91.7% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

muscariello added a commit that referenced this pull request Oct 1, 2026
…ment-backfill

Brings in main's #2204, the policy_evaluation rename from #2229, and #2248's merge of both. The policy config keeps main's struct plus this branch's refresh interval and previous_version.

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

csirmazbendeguz commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

If I understand correctly the issue is that updating a policy may accidentally hide everything. So before updating a policy, admins can switch to shadow mode, update the policy, check the logs, and if everything is ok, switch back?

I wonder if there's a better way to add a safety rail or if a safety rail is really necessary here at all...

How about having a command for checking what records a policy would effect? That way users can check pre-emptively before applying any policies

@muscariello

Copy link
Copy Markdown
Member Author

@csirmazbendeguz Shadow mode works but lifts enforcement for the whole edit. Edits are enforced at once instead, so the safeguard is review and a trial on a node that doesn't serve users.

@muscariello
muscariello force-pushed the feat/policy-enforcement-modes branch from eb43adf to cc24c4e Compare October 1, 2026 15:52
@csirmazbendeguz

csirmazbendeguz commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

I'm not sure "shadow mode" is necessary to be honest

Personally I wouldn't use this feature as an admin just to test out some policy changes

If I was a node admin I would rather have some kind of way to check that the policy I'm about to deploy will affect anything - a dry run, to make sure it behaves correctly in advance

That was just my opinion as I was thinking about how I would use this. I'm not super against it though and so if you think it would be useful to you then we can merge it

@muscariello

Copy link
Copy Markdown
Member Author

@csirmazbendeguz Shadow mode is stage 1 of the rollout in #2209 (a metric telling "would exclude" from "excluded"), so I'd keep it. A dry run of a candidate policy before it's deployed is a good addition, and a better answer for edits. It needs an evaluator to run against the records, so I'd track it as a follow-up: #2264.

csirmazbendeguz
csirmazbendeguz previously approved these changes Oct 2, 2026
@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-enforcement-modes branch from cc8660f to 7831e98 Compare October 2, 2026 12:05
@muscariello
muscariello force-pushed the feat/policy-enforcement-modes branch from 7831e98 to bb29989 Compare October 2, 2026 12:33
Nothing supplied the enforced policies to the read gate, so it could not
be turned on. policy.enforcement now lists them, each at the version in
force, and gives each kind of read its own mode, so enforcement can be
rolled out one kind at a time:

- search: search, the catalog, filter values, publication by query;
- fetch: pull, lookup, referrers, export, the peer RPC, publication of
  explicit CIDs, verification lookups.

A mode is off, shadow or enforce. Shadow checks each fetch but serves
every record, even when the check itself fails; searches in shadow mode
are not filtered. Both modes default to off.

The server's APIs read through a view of the database that applies the
enforcement. The database itself applies none: ingestion and the
reconciler, which in daemon mode shares the server's database, read
through it and must see every record. A database that cannot enforce
the policies stops the server from starting rather than serving
unchecked records, and enforcing without authz logs a warning, since
registry credentials then let any caller read excluded records.

Refs #2209

Signed-off-by: Luca Muscariello <[email protected]>
Shadow mode is only useful if it tells what enforcing would hide. With a
metrics server, the gate now reports:

- dir_policy_gate_search_records_excluded, a gauge of the indexed records
  searches exclude, counted at each scrape;
- dir_policy_gate_fetches_excluded_total, a counter of reads by CID the
  policies refused.

Both carry outcome="would_exclude" in shadow mode and outcome="excluded"
when enforcing. The gauge is absent while searches check nothing, and
when it cannot be counted, rather than failing the whole scrape.

Refs #2209

Signed-off-by: Luca Muscariello <[email protected]>
Adding the served view pushed server.New's maintainability index to 19,
below maintidx's floor of 20. Opening the database and building the view
the APIs read through now happen in openDatabase, leaving New with one
branch where it had two.

Refs #2209

Signed-off-by: Luca Muscariello <[email protected]>
openDatabase is the path every deployed server takes: nothing hands it a
database, so it opens the configured one. The tests only ever handed one
in. They now open a configured SQLite database and check that the APIs
get a separate gated view, that a database handed in is used as is, and
that an unknown database type fails.

Refs #2209

Signed-off-by: Luca Muscariello <[email protected]>
Enforced policies were listed with a version, which an admin had to copy
from the evaluator into the server's config after every change to a
policy. A version derived from a policy's content cannot be kept up by
hand, and getting the order wrong hides records one by one as they are
re-evaluated.

policy.enforcement.policies now lists policy IDs only. The server follows
the version the reconciler registers for each, rechecking every
policy.enforcement.refresh_interval (default 30s), and enforces it
strictly: a verdict under any other version does not count. After a
policy is edited, a record stays hidden until it has been evaluated under
the new rule, so nothing the new rule rejects is served while that
happens. A policy no evaluator has registered is not enforced yet and is
logged once.

The IDs can be set from the environment as a comma-separated list.

Refs #2209

Signed-off-by: Luca Muscariello <[email protected]>
@muscariello
muscariello force-pushed the feat/policy-enforcement-modes branch from bb29989 to 1a1fcdf Compare October 2, 2026 12:59
@muscariello
muscariello added this pull request to the merge queue Oct 2, 2026
Merged via the queue into main with commit f7b164a Oct 2, 2026
34 checks passed
@muscariello
muscariello deleted the feat/policy-enforcement-modes 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/security Categorizes issue or PR as related to security. size/L Denotes a PR that changes 1000-1999 lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants