@dads-framework/audit 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +148 -19
- package/dist/audit.tsbuildinfo +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/lib/export/create-audit-log-exporter.d.ts +19 -0
- package/dist/lib/export/create-audit-log-exporter.d.ts.map +1 -0
- package/dist/lib/export/create-audit-log-exporter.js +59 -0
- package/dist/lib/export/create-audit-log-exporter.js.map +1 -0
- package/package.json +11 -11
package/README.md
CHANGED
|
@@ -1,36 +1,122 @@
|
|
|
1
1
|
# @dads-framework/audit
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Find who changed an audited row, what changed, and which request caused it.
|
|
4
4
|
The kit also records requests and client diagnostics. `composeAudit` wires
|
|
5
5
|
their retention jobs and diagnostic route to the application's services.
|
|
6
6
|
|
|
7
|
-
##
|
|
7
|
+
## Try it
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
In the generated starter, [`migrations/20261120080000_starter.migration.ts`](../../../apps/kit-workbench/migrations/20261120080000_starter.migration.ts)
|
|
10
|
+
installs the audit schema and attaches row triggers. That migration knows the
|
|
11
|
+
application's table keys and which columns define tenant scope.
|
|
12
|
+
|
|
13
|
+
Use Node.js 22 or later and a disposable Postgres database. Set
|
|
14
|
+
`DATABASE_URL` to the migrated database. Apply the audit schema through
|
|
15
|
+
`installAuditSchemaV1(pgm)` in your migration runner, create the example
|
|
16
|
+
table, and attach the trigger as shown under [Use it](#use-it). The
|
|
17
|
+
application must authorize access before invoking the audit reader.
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
npm install @dads-framework/audit @dads-framework/audit-actor @dads-framework/postgres
|
|
21
|
+
npm install -D tsx
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Save as `record-audit.mts` and run `npx tsx record-audit.mts`.
|
|
25
|
+
|
|
26
|
+
<!-- dads:check-example -->
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
import { randomUUID } from "node:crypto";
|
|
30
|
+
import { DrizzleAuditLogReader } from "@dads-framework/audit";
|
|
31
|
+
import {
|
|
32
|
+
currentAuditActor,
|
|
33
|
+
withSystemActor,
|
|
34
|
+
} from "@dads-framework/audit-actor";
|
|
35
|
+
import { createPostgresConnection } from "@dads-framework/postgres";
|
|
36
|
+
|
|
37
|
+
const connectionString = process.env.DATABASE_URL;
|
|
38
|
+
if (!connectionString) throw new Error("Set DATABASE_URL.");
|
|
39
|
+
|
|
40
|
+
const connection = createPostgresConnection(
|
|
41
|
+
{
|
|
42
|
+
connectionString,
|
|
43
|
+
schema: {},
|
|
44
|
+
auditActorSource: currentAuditActor,
|
|
45
|
+
},
|
|
46
|
+
console,
|
|
47
|
+
);
|
|
48
|
+
try {
|
|
49
|
+
const id = randomUUID();
|
|
50
|
+
const workspace = "audit-example";
|
|
51
|
+
await withSystemActor("example-import", () =>
|
|
52
|
+
connection.pool.query(
|
|
53
|
+
"INSERT INTO documents (id, workspace_key, project_key) VALUES ($1, $2, $3)",
|
|
54
|
+
[id, workspace, "example-project"],
|
|
55
|
+
),
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
const reader = new DrizzleAuditLogReader(connection.drizzle);
|
|
59
|
+
const filter = { record: { tableName: "documents", rowKey: { id } } };
|
|
60
|
+
const rows = await reader.listRows(
|
|
61
|
+
{ kind: "application", values: { workspace } },
|
|
62
|
+
filter,
|
|
63
|
+
null,
|
|
64
|
+
10,
|
|
65
|
+
);
|
|
66
|
+
console.log(rows.map(({ operation, actorId }) => [operation, actorId]));
|
|
67
|
+
const outside = await reader.listRows(
|
|
68
|
+
{ kind: "application", values: { workspace: "unrelated" } },
|
|
69
|
+
filter,
|
|
70
|
+
null,
|
|
71
|
+
10,
|
|
72
|
+
);
|
|
73
|
+
console.log(outside.length);
|
|
74
|
+
} finally {
|
|
75
|
+
await connection.close();
|
|
76
|
+
}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
The result is one row with operation `insert` and actor ID `example-import`,
|
|
80
|
+
then `0` for the unrelated workspace.
|
|
81
|
+
|
|
82
|
+
## Use it
|
|
83
|
+
|
|
84
|
+
Register a migration after the audit schema migration. For the example, add
|
|
85
|
+
the table before attaching the trigger.
|
|
13
86
|
|
|
14
87
|
```ts
|
|
15
88
|
import { configureAuditTriggers } from "@dads-framework/audit/schema";
|
|
89
|
+
import type { MigrationBuilder } from "node-pg-migrate";
|
|
16
90
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
documents
|
|
20
|
-
|
|
21
|
-
|
|
91
|
+
export function up(pgm: MigrationBuilder) {
|
|
92
|
+
pgm.sql(`
|
|
93
|
+
CREATE TABLE public.documents (
|
|
94
|
+
id uuid PRIMARY KEY,
|
|
95
|
+
workspace_key text NOT NULL,
|
|
96
|
+
project_key text NOT NULL
|
|
97
|
+
)
|
|
98
|
+
`);
|
|
99
|
+
configureAuditTriggers(pgm, {
|
|
100
|
+
tables: {
|
|
101
|
+
documents: {
|
|
102
|
+
keyColumns: ["id"],
|
|
103
|
+
scopeColumns: { workspace: "workspace_key", project: "project_key" },
|
|
104
|
+
},
|
|
22
105
|
},
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
});
|
|
106
|
+
});
|
|
107
|
+
}
|
|
26
108
|
```
|
|
27
109
|
|
|
110
|
+
In an application migration, declare each audited table's identity columns and
|
|
111
|
+
map any scope dimensions. Use `excludeTables` to explicitly remove triggers
|
|
112
|
+
from existing tables such as temporary credential storage.
|
|
113
|
+
|
|
28
114
|
The migration reconciles each named table's trigger in the public schema.
|
|
29
115
|
Name new tables in their own application migration. Audit-owned tables are
|
|
30
116
|
always excluded from this configuration. Configuration changes affect future
|
|
31
117
|
writes. The kit keeps previously recorded audit entries intact.
|
|
32
118
|
|
|
33
|
-
|
|
119
|
+
### Read the audit trail
|
|
34
120
|
|
|
35
121
|
Capture dimensions from the affected row at write time. Read them with
|
|
36
122
|
`{ kind: "application", values: { workspace: "example" } }`. All supplied
|
|
@@ -43,15 +129,58 @@ from an application request or job. Applications can supply their own actor
|
|
|
43
129
|
kinds and request attribution. An audited write without an actor stamp
|
|
44
130
|
fails before it commits.
|
|
45
131
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
132
|
+
## Export audit metadata for a tenant
|
|
133
|
+
|
|
134
|
+
Build the exporter with the audit reader, the tenant dimension configured on
|
|
135
|
+
your audited tables, and an application permission check. This handler fragment
|
|
136
|
+
assumes `auditReader` is your `AuditLogReader` and `canExportAuditLog` checks
|
|
137
|
+
the authenticated caller's export permission for the supplied tenant.
|
|
138
|
+
|
|
139
|
+
```ts
|
|
140
|
+
import { createAuditLogExporter } from "@dads-framework/audit";
|
|
141
|
+
|
|
142
|
+
const exportPage = createAuditLogExporter({
|
|
143
|
+
reader: auditReader,
|
|
144
|
+
tenantDimension: "workspace",
|
|
145
|
+
authorize: (tenantId) => canExportAuditLog(caller, tenantId),
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
const first = await exportPage({ tenantId: "example", pageSize: 100 });
|
|
149
|
+
if (first.kind === "page") {
|
|
150
|
+
// Write first.body as first.contentType and return first.nextCursor.
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
The exporter checks the permission on every page before reading the audit log.
|
|
155
|
+
A denied request returns `{ kind: "denied" }`. Malformed input throws a Zod
|
|
156
|
+
validation error. The permission check must cover all audit metadata in the
|
|
157
|
+
requested tenant. Serve the page through an authenticated handler and prevent
|
|
158
|
+
shared caching of its contents.
|
|
159
|
+
|
|
160
|
+
Each successful page contains zero or more newline-terminated JSON objects as
|
|
161
|
+
`application/x-ndjson`. Each object includes the position, exact UTC timestamp,
|
|
162
|
+
actor kind and ID, user ID, request ID, table, operation, and changed column
|
|
163
|
+
names. Row keys, row images, actor reasons, and client fields stay in storage.
|
|
164
|
+
|
|
165
|
+
`pageSize` defaults to 100 and accepts integers from 1 to 500. Pages run newest
|
|
166
|
+
first by timestamp and position. Resume with the same tenant and pass
|
|
167
|
+
`nextCursor` as `olderThan`. The next page starts strictly before that cursor.
|
|
168
|
+
A `null` cursor means the query found no further rows, and an empty page has an
|
|
169
|
+
empty body. Pages are independent reads of the live log. Writes or deletes
|
|
170
|
+
between requests can change the remaining results.
|
|
49
171
|
|
|
50
172
|
## What it refuses to do
|
|
51
173
|
|
|
52
174
|
Audit records describe supplied mutations and actors. Application policies decide which records each caller may see.
|
|
53
175
|
|
|
54
|
-
##
|
|
176
|
+
## Contract
|
|
177
|
+
|
|
178
|
+
The trigger records identity columns and before and after row images as JSON
|
|
179
|
+
objects. Request and diagnostic context follows the client context contract.
|
|
180
|
+
Captured client hint headers are stored as string values. The reader returns
|
|
181
|
+
the newest rows first. Pass the last row's cursor to continue paging.
|
|
182
|
+
|
|
183
|
+
### Install in an API
|
|
55
184
|
|
|
56
185
|
`createAuditPlugin` declares the audit migrations, exclusions, and scope columns.
|
|
57
186
|
Provide a database, clock, and logger through `auditDepsService`. Include
|