Repository navigation
feat(dir): persist per-record policy-compliance verdicts - #2226
Conversation
…s on fakeSignatureDB fakeSignatureDB spells out types.DatabaseAPI by hand rather than embedding it, so extending the interface in the prior commit broke its compile-time satisfaction. The three new methods are no-ops, matching every other unused method on this fake. Fixes the Lint job failure on #2226. Signed-off-by: Luca Muscariello <[email protected]>
types.DatabaseAPI aggregates one embedded interface per capability domain by design; adding PolicyEvaluationDatabaseAPI took it from 10 to 11 members, over golangci-lint's default interfacebloat cap for a flat method list. Raises interface-max-methods to 15, matching the headroom already chosen for goconst in this file, so the next domain added doesn't hit the same wall. needsEvalCIDs always queried the same policy ID across every test in the file; only policy_version and the seeded data varied. Drops the parameter instead of threading a constant through every call site. Fixes the Lint job failure on #2226. Signed-off-by: Luca Muscariello <[email protected]>
interfacebloat's settings struct field is Max, mapstructure key "max" (pkg/config/linters_settings.go in golangci-lint v2.13.2, the version this repo pins) — not interface-max-methods, which I invented by analogy with the other linters' key names in this file and never verified against golangci-lint's own source. The unknown key was silently ignored, so the previous commit's cap never took effect and CI failed on the same finding. Fixes the Lint job failure on #2226. Signed-off-by: Luca Muscariello <[email protected]>
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
e7b38bd to
ac3b60b
Compare
| RecordCID string `gorm:"column:record_cid;primaryKey;not null"` | ||
| PolicyID string `gorm:"column:policy_id;primaryKey;not null"` | ||
| PolicyVersion string `gorm:"column:policy_version;not null;index"` |
There was a problem hiding this comment.
With the key on (record_cid, policy_id), the rollout #2235 suggests for a version bump — evaluating under the new version before it becomes the enforced one — can't work. The upsert (:68-77) overwrites the v1 row with the v2 verdict, and #2235's gate requires pe.policy_version = ? at the enforced version (policy_gate.go:29), so each record drops out of reads the moment it's pre-evaluated and comes back only at the switch. That moves the blackout rather than removing it.
Ran on the #2235 head (09a7d28b, which contains this PR's ac3b60b0) with a scratch test on the policy_gate_test.go helpers: two records compliant under opa:a v1, v1 enforced, GetRecordCIDs returns both. Upsert one under v2 with v1 still enforced: it returns one, and that record's only row is v2. With the change below it returns both, and the record holds a v1 and a v2 row.
#2206, which this closes, asked for one row per record × policy × version. That would be:
primaryKeyonpolicy_versionhere,policy_versionadded to theOnConflictcolumns and dropped fromDoUpdates, 3 lines.ReevaluationReplacesThePriorVerdictis then the only failing test indatabase/gorm; it becomes "same version replaces, a new version adds a row".- Delete superseded rows once the new version is enforced.
- Key feat(reconciler): evaluate records against content policies #2229's duplicate check (
task.go:55) on (ID, version), so v1 and v2 evaluators can run side by side and records published during the transition get verdicts under both. Its test rejects that pair because they "would overwrite each other's rows" (task_test.go:128-130), which is this key; that case flips. Read, not run.
The other way is a new policy ID per version, keeping this key, but that gives up "unchanged across versions" in evaluator.go:18-19. SQLite only, as the suite runs; not tried on Postgres.
There was a problem hiding this comment.
Thanks. We chose the other direction: the new version is enforced at once and records stay hidden until evaluated under it, since content already delivered can't be recalled. So the key stays one row per record and policy.
ac3b60b to
4d6040a
Compare
|
I would avoid using a composite primary key. A unique constraint + index would be sufficient here. I understand it was also used in scan reports, but this is not a good pattern |
4d6040a to
c14d0ec
Compare
|
I checked this and the related PRs - what's not clear to me is who is going to bump the version? Do we expect admins to bump the versions manually? Or is this something that will be automatic? I think it should be automatic, because it's not something admins would want to maintain themselves. We could store policy hashes instead of versions to automate that. We would need to store the policies in the database for that as well. I talked about this in the issue: #2135 |
|
@csirmazbendeguz agreed, a unique index does the job, and that goes for my earlier suggestion of putting |
| // (fail closed), so if it also stopped the record being retried, one | ||
| // transient evaluator error would exclude the record from every read until | ||
| // the policy version changed. | ||
| func (d *DB) GetRecordsNeedingPolicyEvaluation(policyID, policyVersion string) ([]coretypes.Record, error) { |
There was a problem hiding this comment.
This can fetch the entire records table without pagination
There was a problem hiding this comment.
Fixed. GetRecordsNeedingPolicyEvaluation takes the record_cid to start after and a limit, and returns records in order; the task evaluates batch_size (default 500) at a time.
|
@csirmazbendeguz Done: an On who bumps the version: nobody. A policy's version is derived from its content; the reconciler registers it and the server follows it. The config lists policy IDs only. |
…s on fakeSignatureDB fakeSignatureDB spells out types.DatabaseAPI by hand rather than embedding it, so extending the interface in the prior commit broke its compile-time satisfaction. The three new methods are no-ops, matching every other unused method on this fake. Fixes the Lint job failure on #2226. Signed-off-by: Luca Muscariello <[email protected]>
a55426b to
dfadb59
Compare
types.DatabaseAPI aggregates one embedded interface per capability domain by design; adding PolicyEvaluationDatabaseAPI took it from 10 to 11 members, over golangci-lint's default interfacebloat cap for a flat method list. Raises interface-max-methods to 15, matching the headroom already chosen for goconst in this file, so the next domain added doesn't hit the same wall. needsEvalCIDs always queried the same policy ID across every test in the file; only policy_version and the seeded data varied. Drops the parameter instead of threading a constant through every call site. Fixes the Lint job failure on #2226. Signed-off-by: Luca Muscariello <[email protected]>
interfacebloat's settings struct field is Max, mapstructure key "max" (pkg/config/linters_settings.go in golangci-lint v2.13.2, the version this repo pins) — not interface-max-methods, which I invented by analogy with the other linters' key names in this file and never verified against golangci-lint's own source. The unknown key was silently ignored, so the previous commit's cap never took effect and CI failed on the same finding. Fixes the Lint job failure on #2226. Signed-off-by: Luca Muscariello <[email protected]>
…s on fakeSignatureDB fakeSignatureDB spells out types.DatabaseAPI by hand rather than embedding it, so extending the interface in the prior commit broke its compile-time satisfaction. The three new methods are no-ops, matching every other unused method on this fake. Fixes the Lint job failure on #2226. Signed-off-by: Luca Muscariello <[email protected]>
types.DatabaseAPI aggregates one embedded interface per capability domain by design; adding PolicyEvaluationDatabaseAPI took it from 10 to 11 members, over golangci-lint's default interfacebloat cap for a flat method list. Raises interface-max-methods to 15, matching the headroom already chosen for goconst in this file, so the next domain added doesn't hit the same wall. needsEvalCIDs always queried the same policy ID across every test in the file; only policy_version and the seeded data varied. Drops the parameter instead of threading a constant through every call site. Fixes the Lint job failure on #2226. Signed-off-by: Luca Muscariello <[email protected]>
interfacebloat's settings struct field is Max, mapstructure key "max" (pkg/config/linters_settings.go in golangci-lint v2.13.2, the version this repo pins) — not interface-max-methods, which I invented by analogy with the other linters' key names in this file and never verified against golangci-lint's own source. The unknown key was silently ignored, so the previous commit's cap never took effect and CI failed on the same finding. Fixes the Lint job failure on #2226. Signed-off-by: Luca Muscariello <[email protected]>
dfadb59 to
4efb8e9
Compare
…s on fakeSignatureDB fakeSignatureDB spells out types.DatabaseAPI by hand rather than embedding it, so extending the interface in the prior commit broke its compile-time satisfaction. The three new methods are no-ops, matching every other unused method on this fake. Fixes the Lint job failure on #2226. Signed-off-by: Luca Muscariello <[email protected]>
types.DatabaseAPI aggregates one embedded interface per capability domain by design; adding PolicyEvaluationDatabaseAPI took it from 10 to 11 members, over golangci-lint's default interfacebloat cap for a flat method list. Raises interface-max-methods to 15, matching the headroom already chosen for goconst in this file, so the next domain added doesn't hit the same wall. needsEvalCIDs always queried the same policy ID across every test in the file; only policy_version and the seeded data varied. Drops the parameter instead of threading a constant through every call site. Fixes the Lint job failure on #2226. Signed-off-by: Luca Muscariello <[email protected]>
interfacebloat's settings struct field is Max, mapstructure key "max" (pkg/config/linters_settings.go in golangci-lint v2.13.2, the version this repo pins) — not interface-max-methods, which I invented by analogy with the other linters' key names in this file and never verified against golangci-lint's own source. The unknown key was silently ignored, so the previous commit's cap never took effect and CI failed on the same finding. Fixes the Lint job failure on #2226. Signed-off-by: Luca Muscariello <[email protected]>
4efb8e9 to
5e6b955
Compare
Adds a policy_evaluations table, one row per (record_cid, policy_id), versioned so a later policy change doesn't silently reinterpret an old verdict. Mirrors the existing scan_reports fail-closed convention: compliant is a placeholder (false) unless status is 'evaluated', the same way ScanReport.IsSafe is a placeholder on a failed scan. This is storage only. Nothing computes a verdict yet (#2207) and no read path filters on it yet (#2208). Refs #2206, #2205 Signed-off-by: Luca Muscariello <[email protected]>
…s on fakeSignatureDB fakeSignatureDB spells out types.DatabaseAPI by hand rather than embedding it, so extending the interface in the prior commit broke its compile-time satisfaction. The three new methods are no-ops, matching every other unused method on this fake. Fixes the Lint job failure on #2226. Signed-off-by: Luca Muscariello <[email protected]>
types.DatabaseAPI aggregates one embedded interface per capability domain by design; adding PolicyEvaluationDatabaseAPI took it from 10 to 11 members, over golangci-lint's default interfacebloat cap for a flat method list. Raises interface-max-methods to 15, matching the headroom already chosen for goconst in this file, so the next domain added doesn't hit the same wall. needsEvalCIDs always queried the same policy ID across every test in the file; only policy_version and the seeded data varied. Drops the parameter instead of threading a constant through every call site. Fixes the Lint job failure on #2226. Signed-off-by: Luca Muscariello <[email protected]>
interfacebloat's settings struct field is Max, mapstructure key "max" (pkg/config/linters_settings.go in golangci-lint v2.13.2, the version this repo pins) — not interface-max-methods, which I invented by analogy with the other linters' key names in this file and never verified against golangci-lint's own source. The unknown key was silently ignored, so the previous commit's cap never took effect and CI failed on the same finding. Fixes the Lint job failure on #2226. Signed-off-by: Luca Muscariello <[email protected]>
GetRecordsNeedingPolicyEvaluation treated any row at the current (policy_id, policy_version) as settled, failed rows included. A failed row reads as non-compliant, so one transient evaluator error excluded the record from reads until the policy version next changed. Only an evaluated row now suppresses re-selection; a failed one is retried on the next reconciler pass. A non-compliant verdict still suppresses, since re-evaluating it would recompute the same answer. Surfaced while designing the reconciler task in #2207. Signed-off-by: Luca Muscariello <[email protected]>
…ersions Review of the verdict storage raised three points. The table was keyed by (record_cid, policy_id) as a composite primary key. It now has an id primary key and a unique index on the same pair, which is cheaper to change later than a primary key. There is still one verdict per record and policy: a re-evaluation under another version replaces it. GetRecordsNeedingPolicyEvaluation returned every record needing a verdict in one query, which for a first run is the whole records table. It now takes the record_cid to start after and a limit, and returns records in record_cid order. Paging by the last record_cid seen costs the same however far in the caller is, and does not return a record whose evaluation failed again within one pass. Versions come from a policy's content, so nobody should have to state them. Evaluators register the version they run (policy_versions), and GetCurrentPolicyVersion returns the one seen most recently, which the server will follow. Refs #2206 Signed-off-by: Luca Muscariello <[email protected]>
5e6b955 to
03772f7
Compare
Closes #2206. Part of #2205.
What this adds
A
policy_evaluationstable, one row per(record_cid, policy_id), with anidprimary key and a unique index on that pair. Each row carries thepolicy_versionit was reached under, so a later policy change doesn't silently reinterpret an old verdict — a stale-version row is treated as needing re-evaluation, not as authoritative under the new policy.Storage only, mirroring the existing
scan_reportsfail-closed convention rather than inventing a new one:Compliantis a placeholder (false) unlessStatus == "evaluated", the same wayScanReport.IsSafeis a placeholder on a failed scan (seetypes.ScannedStatuses/ the newtypes.EvaluatedPolicyStatuses).UpsertPolicyEvaluationenforces this in code, not just by convention.GetRecordsNeedingPolicyEvaluation(policyID, policyVersion, afterCID, limit)mirrorsGetRecordsNeedingScan'sNOT EXISTSshape, for the reconciler backfill/re-evaluation job in Compute policy verdicts asynchronously and backfill existing records #2207. It returns records inrecord_cidorder afterafterCID, at mostlimitof them (zero returns all), so a first run doesn't load the wholerecordstable. Paging by the lastrecord_cidcosts the same however far in the caller is, and doesn't return a record whose evaluation failed again within one pass.policy_versionsregistry: evaluators register the version of each policy they run, andGetCurrentPolicyVersionreturns the one seen most recently. A policy's version comes from its content, so nobody states it; the server follows this registry (feat(dir): enforce content policies per kind of read, with a shadow mode #2248).Nothing computes a verdict yet (#2207) and no read path filters on it yet (#2208) — this task is deliberately just the schema those two build on.
Testing
New table-driven tests in
policy_evaluation_test.go: upsert transitions (including the fail-closed forcing case), re-evaluation replacing a prior verdict, multiple policies coexisting per record, every case ofGetRecordsNeedingPolicyEvaluation(never evaluated, current version suppresses, superseded version reselects, unrelated policy doesn't suppress, only the stale record is selected), paging in record order and a failed record not repeating within a pass.policy_version_test.gocovers the registry: a new version becoming current, returning to an earlier one, policies being independent, and database failures.go vetandgofmtclean on every touched file; fulldatabase/gormandtypespackage suites pass.