-
Notifications
You must be signed in to change notification settings - Fork 0
fix(server): shorten default audit retention #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
packages/server/src/__tests__/audit-retention-migration.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import assert from "node:assert/strict"; | ||
| import { readFile } from "node:fs/promises"; | ||
| import { DatabaseSync } from "node:sqlite"; | ||
| import test from "node:test"; | ||
|
|
||
| import { | ||
| createNodeSqliteRunner, | ||
| runMigrations, | ||
| sha256, | ||
| type MigrationSource, | ||
| } from "@relayauth/migrate"; | ||
|
|
||
| const MIGRATION_ID = "0006_audit_retention_default"; | ||
| const MIGRATION_URL = new URL( | ||
| `../db/migrations/${MIGRATION_ID}.sql`, | ||
| import.meta.url, | ||
| ); | ||
|
|
||
| test("audit retention default migration preserves explicit overrides", async (t) => { | ||
| const db = new DatabaseSync(":memory:"); | ||
| t.after(() => db.close()); | ||
|
|
||
| db.exec(` | ||
| CREATE TABLE audit_retention_config ( | ||
| org_id TEXT PRIMARY KEY, | ||
| retention_days INTEGER NOT NULL DEFAULT 90 | ||
| ); | ||
| INSERT INTO audit_retention_config (org_id, retention_days) | ||
| VALUES ('org_override', 30); | ||
| `); | ||
|
|
||
| const sql = await readFile(MIGRATION_URL, "utf8"); | ||
| const source: MigrationSource = { | ||
| async list() { | ||
| return [{ id: MIGRATION_ID, sql, checksum: sha256(sql) }]; | ||
| }, | ||
| }; | ||
| const result = await runMigrations(createNodeSqliteRunner(db), source); | ||
|
|
||
| assert.deepEqual(result, { applied: [MIGRATION_ID], skipped: [] }); | ||
| assert.deepEqual( | ||
| db | ||
| .prepare( | ||
| "SELECT org_id, retention_days FROM audit_retention_config ORDER BY org_id", | ||
| ) | ||
| .all() | ||
| .map((row) => ({ | ||
| org_id: row.org_id, | ||
| retention_days: row.retention_days, | ||
| })), | ||
| [{ org_id: "org_override", retention_days: 30 }], | ||
| ); | ||
|
|
||
| db.prepare("INSERT INTO audit_retention_config (org_id) VALUES (?)").run( | ||
| "org_default", | ||
| ); | ||
| assert.deepEqual( | ||
| db | ||
| .prepare( | ||
| "SELECT org_id, retention_days FROM audit_retention_config ORDER BY org_id", | ||
| ) | ||
| .all() | ||
| .map((row) => ({ | ||
| org_id: row.org_id, | ||
| retention_days: row.retention_days, | ||
| })), | ||
| [ | ||
| { org_id: "org_default", retention_days: 2 }, | ||
| { org_id: "org_override", retention_days: 30 }, | ||
| ], | ||
| ); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
packages/server/src/db/migrations/0006_audit_retention_default.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| -- Keep audit history intentionally short unless an organization explicitly | ||
| -- opts into a longer window. SQLite cannot alter a column default in place, | ||
| -- so rebuild this small configuration table while preserving every override. | ||
| CREATE TABLE audit_retention_config_v2 ( | ||
| org_id TEXT PRIMARY KEY, | ||
| retention_days INTEGER NOT NULL DEFAULT 2 | ||
| ); | ||
|
|
||
| INSERT INTO audit_retention_config_v2 (org_id, retention_days) | ||
| SELECT org_id, retention_days | ||
| FROM audit_retention_config; | ||
|
|
||
| DROP TABLE audit_retention_config; | ||
|
|
||
| ALTER TABLE audit_retention_config_v2 RENAME TO audit_retention_config; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P3: The repository now exposes conflicting retention contracts: this design document says 2/1, but
specs/audit.mdand the retention workflow still require 90/7. Updating the canonical spec and generated workflow (or clearly marking them obsolete) would prevent future implementations and verification from restoring the old behavior.Prompt for AI agents