@pithy-sh/audit 0.1.1 → 0.1.3
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/dist/actions.d.ts +41 -0
- package/dist/actions.d.ts.map +1 -0
- package/dist/actions.js +55 -0
- package/dist/capability.d.ts +40 -0
- package/dist/capability.d.ts.map +1 -0
- package/dist/capability.js +86 -0
- package/dist/cli/emitFromCLI.d.ts +35 -0
- package/dist/cli/emitFromCLI.d.ts.map +1 -0
- package/dist/cli/emitFromCLI.js +37 -0
- package/dist/cli/resolveActor.d.ts +74 -0
- package/dist/cli/resolveActor.d.ts.map +1 -0
- package/dist/cli/resolveActor.js +83 -0
- package/dist/data/auditEvent.d.ts +57 -0
- package/dist/data/auditEvent.d.ts.map +1 -0
- package/dist/data/auditEvent.js +46 -0
- package/dist/data/tables.d.ts +52 -0
- package/dist/data/tables.d.ts.map +1 -0
- package/dist/data/tables.js +15 -0
- package/dist/error/errors.d.ts +41 -0
- package/dist/error/errors.d.ts.map +1 -0
- package/dist/error/errors.js +30 -0
- package/dist/http/guards.d.ts +68 -0
- package/dist/http/guards.d.ts.map +1 -0
- package/dist/http/guards.js +80 -0
- package/dist/http/responses.d.ts +158 -0
- package/dist/http/responses.d.ts.map +1 -0
- package/dist/http/responses.js +71 -0
- package/dist/http/routes.d.ts +59 -0
- package/dist/http/routes.d.ts.map +1 -0
- package/dist/http/routes.js +125 -0
- package/dist/http/schemas.d.ts +45 -0
- package/dist/http/schemas.d.ts.map +1 -0
- package/dist/http/schemas.js +68 -0
- package/dist/http/views.d.ts +53 -0
- package/dist/http/views.d.ts.map +1 -0
- package/dist/http/views.js +79 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10 -0
- package/dist/migrations/0001_init.d.ts +19 -0
- package/dist/migrations/0001_init.d.ts.map +1 -0
- package/dist/migrations/0001_init.js +45 -0
- package/dist/query.d.ts +103 -0
- package/dist/query.d.ts.map +1 -0
- package/dist/query.js +95 -0
- package/dist/recorder.d.ts +80 -0
- package/dist/recorder.d.ts.map +1 -0
- package/dist/recorder.js +64 -0
- package/dist/seeds/example.d.ts +16 -0
- package/dist/seeds/example.d.ts.map +1 -0
- package/dist/seeds/example.js +194 -0
- package/dist/version.generated.d.ts +5 -0
- package/dist/version.generated.d.ts.map +1 -0
- package/dist/version.generated.js +7 -0
- package/package.json +21 -10
- package/src/version.generated.ts +1 -1
package/dist/query.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { AuditEventRow } from "./data/auditEvent.js";
|
|
2
|
+
import { SQLiteDate } from "@pithy-sh/core/src/data/codecs";
|
|
3
|
+
import { decodeCursor, pageLimit, toPage } from "@pithy-sh/core/src/data/cursor";
|
|
4
|
+
//#region src/query.ts
|
|
5
|
+
/**
|
|
6
|
+
* The filtered, ordered query both readers run — one builder, so a filter can never mean two things.
|
|
7
|
+
*
|
|
8
|
+
* The order is `(occurredAt desc, id desc)` and it is not an incidental choice: it is what the keyset
|
|
9
|
+
* cursor names a position in. `id` is the tiebreak, and without it two events recorded in the same
|
|
10
|
+
* millisecond straddle a page boundary and one of them is skipped or returned twice — which on an
|
|
11
|
+
* audit trail is a missing record, not a cosmetic glitch.
|
|
12
|
+
*/
|
|
13
|
+
function auditEventQuery(db, filter) {
|
|
14
|
+
let query = db.selectFrom("pithyAuditEvents").selectAll();
|
|
15
|
+
if (filter.actorType !== void 0) query = query.where("actorType", "=", filter.actorType);
|
|
16
|
+
if (filter.actorId !== void 0) query = query.where("actorId", "=", filter.actorId);
|
|
17
|
+
if (filter.action !== void 0) query = query.where("action", "=", filter.action);
|
|
18
|
+
if (filter.outcome !== void 0) query = query.where("outcome", "=", filter.outcome);
|
|
19
|
+
if (filter.severity !== void 0) query = query.where("severity", "=", filter.severity);
|
|
20
|
+
if (filter.resourceType !== void 0) query = query.where("resourceType", "=", filter.resourceType);
|
|
21
|
+
if (filter.resourceId !== void 0) query = query.where("resourceId", "=", filter.resourceId);
|
|
22
|
+
if (filter.project !== void 0) query = query.where("project", "=", filter.project);
|
|
23
|
+
if (filter.environment !== void 0) query = query.where("environment", "=", filter.environment);
|
|
24
|
+
if (filter.worker !== void 0) query = query.where("worker", "=", filter.worker);
|
|
25
|
+
if (filter.tenant !== void 0) query = filter.tenant === null ? query.where("tenant", "is", null) : query.where("tenant", "=", filter.tenant);
|
|
26
|
+
if (filter.from !== void 0) query = query.where("occurredAt", ">=", SQLiteDate.encode(filter.from));
|
|
27
|
+
if (filter.to !== void 0) query = query.where("occurredAt", "<=", SQLiteDate.encode(filter.to));
|
|
28
|
+
return query.orderBy("occurredAt", "desc").orderBy("id", "desc");
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* A decoded cursor, narrowed to the position this table's ordering can actually use.
|
|
32
|
+
*
|
|
33
|
+
* `occurredAt` is a **ms-epoch number** here, not an ISO string — Pithy's own tables store dates as
|
|
34
|
+
* numbers, and only Better Auth's store them as TEXT. A cursor carrying a string would compare
|
|
35
|
+
* lexically against an integer column and silently return the wrong page, so anything that is not a
|
|
36
|
+
* pair of numbers is treated as no cursor at all. A bad cursor is a first page, which is the same
|
|
37
|
+
* answer `decodeCursor` gives to a truncated or stale one.
|
|
38
|
+
*/
|
|
39
|
+
function keysetPosition(cursor) {
|
|
40
|
+
if (!cursor) return void 0;
|
|
41
|
+
if (typeof cursor.sort !== "number" || typeof cursor.id !== "number") return void 0;
|
|
42
|
+
return {
|
|
43
|
+
sort: cursor.sort,
|
|
44
|
+
id: cursor.id
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Read audit events matching `filter`, newest first (`occurredAt` then `id`, both descending). Each
|
|
49
|
+
* row is decoded back through {@link AuditEventRow} — dates become `Date`s, `metadata` a parsed and
|
|
50
|
+
* validated object — so callers get the app shape, never raw SQLite columns.
|
|
51
|
+
*/
|
|
52
|
+
async function queryAuditEvents(db, filter = {}) {
|
|
53
|
+
let query = auditEventQuery(db, filter);
|
|
54
|
+
if (filter.limit !== void 0) query = query.limit(filter.limit);
|
|
55
|
+
return (await query.execute()).map((row) => AuditEventRow.parse(row));
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* One page of the trail, resumable — the read the control-plane route serves.
|
|
59
|
+
*
|
|
60
|
+
* **Keyset, never offset.** The audit table is appended to constantly while somebody is reading it, so
|
|
61
|
+
* with `OFFSET` every event recorded during a read pushes one row from the page a client already has
|
|
62
|
+
* onto the page it is about to fetch: it sees that row twice, and any row that fell off the far end it
|
|
63
|
+
* never sees at all. On a security trail, "you may silently miss a record while paging" is not a
|
|
64
|
+
* usability defect. The cursor names the last row's exact position instead, so the next page starts
|
|
65
|
+
* where the previous ended whatever was written in between.
|
|
66
|
+
*/
|
|
67
|
+
async function pageAuditEvents(db, filter = {}) {
|
|
68
|
+
const limit = pageLimit(filter.limit);
|
|
69
|
+
const position = keysetPosition(decodeCursor(filter.cursor));
|
|
70
|
+
let query = auditEventQuery(db, filter);
|
|
71
|
+
if (position) query = query.where((eb) => eb.or([eb("occurredAt", "<", position.sort), eb.and([eb("occurredAt", "=", position.sort), eb("id", "<", position.id)])]));
|
|
72
|
+
const decoded = (await query.limit(limit + 1).execute()).map((row) => AuditEventRow.parse(row));
|
|
73
|
+
const page = toPage(decoded, limit, (row) => ({
|
|
74
|
+
sort: row.occurredAt.getTime(),
|
|
75
|
+
id: row.id
|
|
76
|
+
}));
|
|
77
|
+
return {
|
|
78
|
+
events: page.items,
|
|
79
|
+
nextCursor: page.nextCursor
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Read one event by its `eventId`, or null when there is none.
|
|
84
|
+
*
|
|
85
|
+
* Addressed by `eventId` — the recorder's UUID idempotency key, uniquely indexed — and never by the
|
|
86
|
+
* autoincrement `id`. `id` is a surrogate that leaks how many events a project has recorded and is
|
|
87
|
+
* guessable by counting; `eventId` is neither, and it is the value the trail's own idempotency
|
|
88
|
+
* guarantee is written against.
|
|
89
|
+
*/
|
|
90
|
+
async function readAuditEvent(db, eventId) {
|
|
91
|
+
const row = await db.selectFrom("pithyAuditEvents").selectAll().where("eventId", "=", eventId).executeTakeFirst();
|
|
92
|
+
return row ? AuditEventRow.parse(row) : null;
|
|
93
|
+
}
|
|
94
|
+
//#endregion
|
|
95
|
+
export { pageAuditEvents, queryAuditEvents, readAuditEvent };
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { type AuditEventInput } from "@pithy-sh/core/src/audit/auditEvent";
|
|
2
|
+
import type { AuditEmit } from "@pithy-sh/core/src/audit/recorder";
|
|
3
|
+
import { type D1RetryOptions } from "@pithy-sh/core/src/data/withD1Retry";
|
|
4
|
+
import { PithyError } from "@pithy-sh/core/src/error/pithyError";
|
|
5
|
+
import type { AuditDatabase } from "./data/tables";
|
|
6
|
+
/**
|
|
7
|
+
* The D1-backed audit recorder. `recordAuditEvent` performs a **synchronous, non-fatal** insert: it
|
|
8
|
+
* awaits a direct `INSERT` into `pithy_audit_events` inside the handler, before the response — so the
|
|
9
|
+
* event is durably persisted by the time the caller gets a reply — and it **never throws**. A write
|
|
10
|
+
* failure (or an invalid event) is converted to a namespaced `PithyError` and handed to `onError`
|
|
11
|
+
* (logged by default), so an audit write can never break the action it records (CLAUDE.md §Security:
|
|
12
|
+
* audit is a non-blocking side effect).
|
|
13
|
+
*
|
|
14
|
+
* The insert is wrapped in {@link withD1Retry} to ride out transient `timeout`/`database-busy` faults
|
|
15
|
+
* before falling through to the non-fatal drop; its always-on idempotency guard means a retry after a
|
|
16
|
+
* transport hiccup never double-writes.
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* Where the recording happened — the origin columns, supplied to the **recorder**, never by an
|
|
20
|
+
* emitter.
|
|
21
|
+
*
|
|
22
|
+
* This is deliberately not a field on `AuditEvent`. Origin is a property of the *writer*, not of the
|
|
23
|
+
* action, and putting it on the event seam would make it caller-settable: any of the ~40 `c.var.emit`
|
|
24
|
+
* sites could then claim to be another Worker, another environment, or another project, and the column
|
|
25
|
+
* would be worth nothing precisely when it mattered. Binding it here means an emitter cannot set it,
|
|
26
|
+
* cannot omit it, and cannot get it wrong.
|
|
27
|
+
*
|
|
28
|
+
* Every field is nullable because every field is genuinely unknowable in some real case: a Worker
|
|
29
|
+
* scaffolded before the vars existed carries none of them, and `worker` is always null for a
|
|
30
|
+
* CLI-originated action, which came from no Worker at all.
|
|
31
|
+
*
|
|
32
|
+
* **`tenant` is deliberately not here.** It is the mirror image of these: whose action it was is a
|
|
33
|
+
* property of the *action*, not of the writer, and no `env` var can know it — so it lives on
|
|
34
|
+
* `AuditEvent`, comes from the emitter, and this recorder can neither default it nor check it. That is
|
|
35
|
+
* the honest trade. Putting it here would mean stamping every row in a multi-tenant Worker with one
|
|
36
|
+
* constant, which is precisely the gap the column exists to close.
|
|
37
|
+
*/
|
|
38
|
+
export interface AuditOrigin {
|
|
39
|
+
/** The owning project, from the Worker's `PROJECT` var or the CLI's resolved project name. */
|
|
40
|
+
project: string | null;
|
|
41
|
+
/** The environment served, from `ENVIRONMENT` or the command's `--env`. */
|
|
42
|
+
environment: string | null;
|
|
43
|
+
/** The recording Worker's `apps/<name>` directory name; null for a CLI action. */
|
|
44
|
+
worker: string | null;
|
|
45
|
+
/**
|
|
46
|
+
* The Cloudflare version id of the build recording the event, from `CF_VERSION_METADATA`; null for a
|
|
47
|
+
* CLI action and for a Worker that does not declare the binding.
|
|
48
|
+
*
|
|
49
|
+
* Required-but-nullable rather than optional, deliberately: making it optional would let a
|
|
50
|
+
* construction site forget it and silently record null, and there are three of them — this recorder,
|
|
51
|
+
* `emitFromCLI`, and the CLI's own `cliOrigin()`. A required field makes the compiler find all three.
|
|
52
|
+
*/
|
|
53
|
+
version: string | null;
|
|
54
|
+
}
|
|
55
|
+
/** Options for the recorder. All optional; the defaults route failures to a sink and retry transient D1 faults. */
|
|
56
|
+
export interface AuditRecorderOptions {
|
|
57
|
+
/**
|
|
58
|
+
* Where this recorder is writing from, stamped onto every event it records. Omitted, the three
|
|
59
|
+
* columns are `null` — the same shape as a row written before they existed, which is the truthful
|
|
60
|
+
* answer rather than a guess.
|
|
61
|
+
*/
|
|
62
|
+
origin?: AuditOrigin;
|
|
63
|
+
/**
|
|
64
|
+
* Called with the namespaced failure when an event can't be validated or persisted. The audit write
|
|
65
|
+
* is non-fatal, so this is the only signal a drop happened. The audit capability wires it to the core
|
|
66
|
+
* logger seam (`c.var.log.error(...)`); when omitted the failure is dropped silently — pass a sink.
|
|
67
|
+
*/
|
|
68
|
+
onError?: (error: PithyError) => void;
|
|
69
|
+
/** Retry tuning for the insert. Defaults to retrying `timeout` and `database-busy`. */
|
|
70
|
+
retry?: D1RetryOptions;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Record one audit event. Validates the event against the core {@link AuditEvent} seam, stamps
|
|
74
|
+
* `occurredAt` when the emitter did not, encodes every column through its codec, and inserts the row
|
|
75
|
+
* — all wrapped so no failure escapes. Returns when the event is persisted (or its failure logged).
|
|
76
|
+
*/
|
|
77
|
+
export declare function recordAuditEvent(db: AuditDatabase, input: AuditEventInput, options?: AuditRecorderOptions): Promise<void>;
|
|
78
|
+
/** Bind a recorder to one audit database — the `emit` seam `@pithy-sh/audit` installs on the request context. */
|
|
79
|
+
export declare function createAuditEmit(db: AuditDatabase, options?: AuditRecorderOptions): AuditEmit;
|
|
80
|
+
//# sourceMappingURL=recorder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"recorder.d.ts","sourceRoot":"","sources":["../src/recorder.ts"],"names":[],"mappings":"AAGA,OAAO,EAAc,KAAK,eAAe,EAAE,MAAM,qCAAqC,CAAC;AACvF,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,mCAAmC,CAAC;AAEnE,OAAO,EAAE,KAAK,cAAc,EAAe,MAAM,qCAAqC,CAAC;AACvF,OAAO,EAAa,UAAU,EAAE,MAAM,qCAAqC,CAAC;AAG5E,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAGnD;;;;;;;;;;;GAWG;AAEH;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,WAAW,WAAW;IAC1B,8FAA8F;IAC9F,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,2EAA2E;IAC3E,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,kFAAkF;IAClF,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB;;;;;;;OAOG;IACH,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;CACxB;AAKD,mHAAmH;AACnH,MAAM,WAAW,oBAAoB;IACnC;;;;OAIG;IACH,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB;;;;OAIG;IACH,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,UAAU,KAAK,IAAI,CAAC;IACtC,uFAAuF;IACvF,KAAK,CAAC,EAAE,cAAc,CAAC;CACxB;AAWD;;;;GAIG;AACH,wBAAsB,gBAAgB,CACpC,EAAE,EAAE,aAAa,EACjB,KAAK,EAAE,eAAe,EACtB,OAAO,CAAC,EAAE,oBAAoB,GAC7B,OAAO,CAAC,IAAI,CAAC,CA8Cf;AAED,iHAAiH;AACjH,wBAAgB,eAAe,CAAC,EAAE,EAAE,aAAa,EAAE,OAAO,CAAC,EAAE,oBAAoB,GAAG,SAAS,CAE5F"}
|
package/dist/recorder.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { AuditInvalidEventError, AuditWriteFailedError } from "./error/errors.js";
|
|
2
|
+
import { AuditMetadataColumn } from "./data/auditEvent.js";
|
|
3
|
+
import { AuditEvent } from "@pithy-sh/core/src/audit/auditEvent";
|
|
4
|
+
import { PithyError, messageOf } from "@pithy-sh/core/src/error/pithyError";
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
import { SQLiteDate } from "@pithy-sh/core/src/data/codecs";
|
|
7
|
+
import { withD1Retry } from "@pithy-sh/core/src/data/withD1Retry";
|
|
8
|
+
//#region src/recorder.ts
|
|
9
|
+
/** No origin established — what an unstamped Worker and an un-migrated caller both record. */
|
|
10
|
+
const NO_ORIGIN = {
|
|
11
|
+
project: null,
|
|
12
|
+
environment: null,
|
|
13
|
+
worker: null,
|
|
14
|
+
version: null
|
|
15
|
+
};
|
|
16
|
+
/** Map any caught failure to its namespaced audit `PithyError`. A bad event is `invalid_event`; anything else is `write_failed`. */
|
|
17
|
+
function toAuditError(error) {
|
|
18
|
+
if (error instanceof PithyError) return error;
|
|
19
|
+
if (error instanceof z.ZodError) return new AuditInvalidEventError({ detail: error.message }, { cause: error });
|
|
20
|
+
return new AuditWriteFailedError({ detail: messageOf(error) }, { cause: error });
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Record one audit event. Validates the event against the core {@link AuditEvent} seam, stamps
|
|
24
|
+
* `occurredAt` when the emitter did not, encodes every column through its codec, and inserts the row
|
|
25
|
+
* — all wrapped so no failure escapes. Returns when the event is persisted (or its failure logged).
|
|
26
|
+
*/
|
|
27
|
+
async function recordAuditEvent(db, input, options) {
|
|
28
|
+
const onError = options?.onError ?? (() => {});
|
|
29
|
+
const origin = options?.origin ?? NO_ORIGIN;
|
|
30
|
+
try {
|
|
31
|
+
const event = AuditEvent.parse(input);
|
|
32
|
+
const occurredAt = event.occurredAt ?? /* @__PURE__ */ new Date();
|
|
33
|
+
const eventId = crypto.randomUUID();
|
|
34
|
+
await withD1Retry(() => db.insertInto("pithyAuditEvents").values({
|
|
35
|
+
eventId,
|
|
36
|
+
occurredAt: SQLiteDate.encode(occurredAt),
|
|
37
|
+
action: event.action,
|
|
38
|
+
outcome: event.outcome,
|
|
39
|
+
severity: event.severity,
|
|
40
|
+
actorType: event.actorType,
|
|
41
|
+
actorId: event.actorId ?? null,
|
|
42
|
+
sessionId: event.sessionId ?? null,
|
|
43
|
+
resourceType: event.resourceType ?? null,
|
|
44
|
+
resourceId: event.resourceId ?? null,
|
|
45
|
+
ip: event.ip ?? null,
|
|
46
|
+
userAgent: event.userAgent ?? null,
|
|
47
|
+
requestId: event.requestId ?? null,
|
|
48
|
+
metadata: event.metadata == null ? null : AuditMetadataColumn.encode(event.metadata),
|
|
49
|
+
tenant: event.tenant ?? null,
|
|
50
|
+
project: origin.project,
|
|
51
|
+
environment: origin.environment,
|
|
52
|
+
worker: origin.worker,
|
|
53
|
+
version: origin.version
|
|
54
|
+
}).execute(), options?.retry);
|
|
55
|
+
} catch (error) {
|
|
56
|
+
onError(toAuditError(error));
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/** Bind a recorder to one audit database — the `emit` seam `@pithy-sh/audit` installs on the request context. */
|
|
60
|
+
function createAuditEmit(db, options) {
|
|
61
|
+
return (input) => recordAuditEvent(db, input, options);
|
|
62
|
+
}
|
|
63
|
+
//#endregion
|
|
64
|
+
export { createAuditEmit, recordAuditEvent };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type SeedSet } from "@pithy-sh/core/src/seed/seed";
|
|
2
|
+
/**
|
|
3
|
+
* A handful of example audit events — the durable, queryable trail the licensed dashboard reads. They
|
|
4
|
+
* are attributed to the shared cast from `@pithy-sh/core` ({@link EXAMPLE_ADA} et al.), so the audit
|
|
5
|
+
* trail lines up with the same users `auth` seeds and `leaderboard`/`ledger`/`multiplayer` reference:
|
|
6
|
+
* a fresh backend with `seed.includeExamples` on has a dashboard with something to show.
|
|
7
|
+
*
|
|
8
|
+
* The set is deliberately spread across the taxonomy the dashboard filters on — every `outcome`
|
|
9
|
+
* (`success`/`failure`/`denied`), a range of `severity` (`info`→`critical`), and both `user` and
|
|
10
|
+
* `system` actors — and across the domains that matter to a licensed product (`auth`, `entitlement`,
|
|
11
|
+
* `admin`, `ledger`): a successful sign-in, a denied one, an entitlement grant, an admin config change,
|
|
12
|
+
* a critical token-reuse alert, and a balance debit. Composed only when the project turns on
|
|
13
|
+
* `seed.includeExamples`, and only for `dev`/`staging` — an example fixture never targets production.
|
|
14
|
+
*/
|
|
15
|
+
export declare const auditExampleSeed: SeedSet;
|
|
16
|
+
//# sourceMappingURL=example.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"example.d.ts","sourceRoot":"","sources":["../../src/seeds/example.ts"],"names":[],"mappings":"AAIA,OAAO,EAA2B,KAAK,OAAO,EAAE,MAAM,8BAA8B,CAAC;AA0CrF;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,gBAAgB,EAAE,OAiI7B,CAAC"}
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { AuditEventRow } from "../data/auditEvent.js";
|
|
2
|
+
import { EXAMPLE_ADA, EXAMPLE_ALAN, EXAMPLE_GRACE } from "@pithy-sh/core/src/seed/exampleIdentities";
|
|
3
|
+
import { d1SeedGroup, defineSeed } from "@pithy-sh/core/src/seed/seed";
|
|
4
|
+
//#region src/seeds/example.ts
|
|
5
|
+
/**
|
|
6
|
+
* Where the audit example set sorts in the project's seed registry. It runs after `auth` (100), whose
|
|
7
|
+
* example seeds the users these events are attributed to, so every `actorId`/`resourceId` here points
|
|
8
|
+
* at a user that already exists.
|
|
9
|
+
*/
|
|
10
|
+
const AUDIT_EXAMPLE_SEED_ORDER = 150;
|
|
11
|
+
/**
|
|
12
|
+
* A short, realistic timeline ending "now", so the seeded events land across a dashboard's recency and
|
|
13
|
+
* time-range views instead of all sharing one timestamp.
|
|
14
|
+
*/
|
|
15
|
+
const minutesAgo = (minutes) => /* @__PURE__ */ new Date(Date.now() - minutes * 6e4);
|
|
16
|
+
/**
|
|
17
|
+
* The origin every example row carries — so a dev database looks like a real one, and the origin
|
|
18
|
+
* filters have something to return.
|
|
19
|
+
*
|
|
20
|
+
* `project` and `worker` are fixture constants in this file's own `example-` idiom: fictional, obviously
|
|
21
|
+
* so, and stable. **`environment` is null, and that asymmetry is deliberate.** A seed row is a static
|
|
22
|
+
* literal, but this set runs in both `dev` and `staging` (`environments` below) and nothing rewrites a
|
|
23
|
+
* row per run — so any environment written here would be a lie in half the runs it appears in. The other
|
|
24
|
+
* two are properties of the fiction; the environment is a property of the run, and the fixture does not
|
|
25
|
+
* know it. Null is what "not recorded" looks like everywhere else in this column, which is also the
|
|
26
|
+
* state of every row written before the origin migration.
|
|
27
|
+
*/
|
|
28
|
+
const EXAMPLE_ORIGIN = {
|
|
29
|
+
project: "example-app",
|
|
30
|
+
environment: null,
|
|
31
|
+
worker: "api",
|
|
32
|
+
version: null
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Two fictional tenants, so a dev database can show what the tenant filter is for.
|
|
36
|
+
*
|
|
37
|
+
* **Ada's events are split across both**, deliberately: one person administering two accounts is the
|
|
38
|
+
* case `actorId` cannot answer, and a fixture where every actor sat in exactly one tenant would make
|
|
39
|
+
* the column look redundant. One row is left with no tenant at all — null is a value, not a gap. It is
|
|
40
|
+
* the shape every row in a single-tenant app has, and it gives the "belongs to no tenant" filter
|
|
41
|
+
* something to return.
|
|
42
|
+
*/
|
|
43
|
+
const EXAMPLE_TENANT_ACME = "example-tenant-acme";
|
|
44
|
+
const EXAMPLE_TENANT_GLOBEX = "example-tenant-globex";
|
|
45
|
+
/**
|
|
46
|
+
* A handful of example audit events — the durable, queryable trail the licensed dashboard reads. They
|
|
47
|
+
* are attributed to the shared cast from `@pithy-sh/core` ({@link EXAMPLE_ADA} et al.), so the audit
|
|
48
|
+
* trail lines up with the same users `auth` seeds and `leaderboard`/`ledger`/`multiplayer` reference:
|
|
49
|
+
* a fresh backend with `seed.includeExamples` on has a dashboard with something to show.
|
|
50
|
+
*
|
|
51
|
+
* The set is deliberately spread across the taxonomy the dashboard filters on — every `outcome`
|
|
52
|
+
* (`success`/`failure`/`denied`), a range of `severity` (`info`→`critical`), and both `user` and
|
|
53
|
+
* `system` actors — and across the domains that matter to a licensed product (`auth`, `entitlement`,
|
|
54
|
+
* `admin`, `ledger`): a successful sign-in, a denied one, an entitlement grant, an admin config change,
|
|
55
|
+
* a critical token-reuse alert, and a balance debit. Composed only when the project turns on
|
|
56
|
+
* `seed.includeExamples`, and only for `dev`/`staging` — an example fixture never targets production.
|
|
57
|
+
*/
|
|
58
|
+
const auditExampleSeed = defineSeed({
|
|
59
|
+
name: "example",
|
|
60
|
+
order: AUDIT_EXAMPLE_SEED_ORDER,
|
|
61
|
+
environments: ["dev", "staging"],
|
|
62
|
+
example: true,
|
|
63
|
+
d1: [d1SeedGroup("app", "pithyAuditEvents", AuditEventRow, [
|
|
64
|
+
{
|
|
65
|
+
id: 1,
|
|
66
|
+
eventId: "example-audit-0001",
|
|
67
|
+
occurredAt: minutesAgo(90),
|
|
68
|
+
action: "auth/login",
|
|
69
|
+
outcome: "success",
|
|
70
|
+
severity: "info",
|
|
71
|
+
actorType: "user",
|
|
72
|
+
actorId: EXAMPLE_ADA.id,
|
|
73
|
+
sessionId: "example-session-ada",
|
|
74
|
+
resourceType: null,
|
|
75
|
+
resourceId: null,
|
|
76
|
+
ip: "203.0.113.10",
|
|
77
|
+
userAgent: "PithyDemo/1.0 (dev seed)",
|
|
78
|
+
requestId: "example-req-0001",
|
|
79
|
+
metadata: { method: "magic_link" },
|
|
80
|
+
tenant: EXAMPLE_TENANT_ACME,
|
|
81
|
+
...EXAMPLE_ORIGIN
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
id: 2,
|
|
85
|
+
eventId: "example-audit-0002",
|
|
86
|
+
occurredAt: minutesAgo(75),
|
|
87
|
+
action: "auth/login",
|
|
88
|
+
outcome: "denied",
|
|
89
|
+
severity: "warning",
|
|
90
|
+
actorType: "user",
|
|
91
|
+
actorId: EXAMPLE_GRACE.id,
|
|
92
|
+
sessionId: null,
|
|
93
|
+
resourceType: null,
|
|
94
|
+
resourceId: null,
|
|
95
|
+
ip: "203.0.113.22",
|
|
96
|
+
userAgent: "PithyDemo/1.0 (dev seed)",
|
|
97
|
+
requestId: "example-req-0002",
|
|
98
|
+
metadata: { reason: "expired_link" },
|
|
99
|
+
tenant: EXAMPLE_TENANT_GLOBEX,
|
|
100
|
+
...EXAMPLE_ORIGIN
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
id: 3,
|
|
104
|
+
eventId: "example-audit-0003",
|
|
105
|
+
occurredAt: minutesAgo(60),
|
|
106
|
+
action: "entitlement/granted",
|
|
107
|
+
outcome: "success",
|
|
108
|
+
severity: "info",
|
|
109
|
+
actorType: "system",
|
|
110
|
+
actorId: null,
|
|
111
|
+
sessionId: null,
|
|
112
|
+
resourceType: "user",
|
|
113
|
+
resourceId: EXAMPLE_ALAN.id,
|
|
114
|
+
ip: null,
|
|
115
|
+
userAgent: null,
|
|
116
|
+
requestId: "example-req-0003",
|
|
117
|
+
metadata: {
|
|
118
|
+
plan: "pro",
|
|
119
|
+
grantedBy: "system"
|
|
120
|
+
},
|
|
121
|
+
tenant: EXAMPLE_TENANT_ACME,
|
|
122
|
+
...EXAMPLE_ORIGIN
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
id: 4,
|
|
126
|
+
eventId: "example-audit-0004",
|
|
127
|
+
occurredAt: minutesAgo(40),
|
|
128
|
+
action: "admin/config_changed",
|
|
129
|
+
outcome: "success",
|
|
130
|
+
severity: "warning",
|
|
131
|
+
actorType: "user",
|
|
132
|
+
actorId: EXAMPLE_ADA.id,
|
|
133
|
+
sessionId: "example-session-ada",
|
|
134
|
+
resourceType: "config",
|
|
135
|
+
resourceId: "billing",
|
|
136
|
+
ip: "203.0.113.10",
|
|
137
|
+
userAgent: "PithyDemo/1.0 (dev seed)",
|
|
138
|
+
requestId: "example-req-0004",
|
|
139
|
+
metadata: {
|
|
140
|
+
field: "seatLimit",
|
|
141
|
+
from: 5,
|
|
142
|
+
to: 10
|
|
143
|
+
},
|
|
144
|
+
tenant: EXAMPLE_TENANT_GLOBEX,
|
|
145
|
+
...EXAMPLE_ORIGIN
|
|
146
|
+
},
|
|
147
|
+
{
|
|
148
|
+
id: 5,
|
|
149
|
+
eventId: "example-audit-0005",
|
|
150
|
+
occurredAt: minutesAgo(20),
|
|
151
|
+
action: "auth/token_reuse_detected",
|
|
152
|
+
outcome: "denied",
|
|
153
|
+
severity: "critical",
|
|
154
|
+
actorType: "user",
|
|
155
|
+
actorId: EXAMPLE_GRACE.id,
|
|
156
|
+
sessionId: null,
|
|
157
|
+
resourceType: "session",
|
|
158
|
+
resourceId: "example-family-grace",
|
|
159
|
+
ip: "198.51.100.7",
|
|
160
|
+
userAgent: "curl/8.4.0",
|
|
161
|
+
requestId: "example-req-0005",
|
|
162
|
+
metadata: {
|
|
163
|
+
familyId: "example-family-grace",
|
|
164
|
+
action: "family_revoked"
|
|
165
|
+
},
|
|
166
|
+
tenant: EXAMPLE_TENANT_GLOBEX,
|
|
167
|
+
...EXAMPLE_ORIGIN
|
|
168
|
+
},
|
|
169
|
+
{
|
|
170
|
+
id: 6,
|
|
171
|
+
eventId: "example-audit-0006",
|
|
172
|
+
occurredAt: minutesAgo(5),
|
|
173
|
+
action: "ledger/debit",
|
|
174
|
+
outcome: "success",
|
|
175
|
+
severity: "info",
|
|
176
|
+
actorType: "user",
|
|
177
|
+
actorId: EXAMPLE_ALAN.id,
|
|
178
|
+
sessionId: "example-session-alan",
|
|
179
|
+
resourceType: "ledger",
|
|
180
|
+
resourceId: "coins",
|
|
181
|
+
ip: "203.0.113.31",
|
|
182
|
+
userAgent: "PithyDemo/1.0 (dev seed)",
|
|
183
|
+
requestId: "example-req-0006",
|
|
184
|
+
metadata: {
|
|
185
|
+
amount: 50,
|
|
186
|
+
currency: "coins"
|
|
187
|
+
},
|
|
188
|
+
tenant: null,
|
|
189
|
+
...EXAMPLE_ORIGIN
|
|
190
|
+
}
|
|
191
|
+
])]
|
|
192
|
+
});
|
|
193
|
+
//#endregion
|
|
194
|
+
export { auditExampleSeed };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/** This package's npm name — the join key against a release feed. */
|
|
2
|
+
export declare const PACKAGE_NAME = "@pithy-sh/audit";
|
|
3
|
+
/** This package's version, stamped from its own package.json at generation time. */
|
|
4
|
+
export declare const PACKAGE_VERSION = "0.1.3";
|
|
5
|
+
//# sourceMappingURL=version.generated.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"version.generated.d.ts","sourceRoot":"","sources":["../src/version.generated.ts"],"names":[],"mappings":"AAWA,qEAAqE;AACrE,eAAO,MAAM,YAAY,oBAAoB,CAAC;AAE9C,oFAAoF;AACpF,eAAO,MAAM,eAAe,UAAU,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
//#region src/version.generated.ts
|
|
2
|
+
/** This package's npm name — the join key against a release feed. */
|
|
3
|
+
const PACKAGE_NAME = "@pithy-sh/audit";
|
|
4
|
+
/** This package's version, stamped from its own package.json at generation time. */
|
|
5
|
+
const PACKAGE_VERSION = "0.1.3";
|
|
6
|
+
//#endregion
|
|
7
|
+
export { PACKAGE_NAME, PACKAGE_VERSION };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pithy-sh/audit",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"license": "FSL-1.1-MIT",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -8,19 +8,24 @@
|
|
|
8
8
|
"directory": "packages/audit"
|
|
9
9
|
},
|
|
10
10
|
"files": [
|
|
11
|
+
"dist",
|
|
11
12
|
"src",
|
|
12
13
|
"pithy.manifest.json",
|
|
13
|
-
"!src/**/*.test.*"
|
|
14
|
+
"!src/**/*.test.*",
|
|
15
|
+
"!dist/**/*.test.*"
|
|
14
16
|
],
|
|
15
17
|
"type": "module",
|
|
16
18
|
"engines": {
|
|
17
19
|
"node": ">=22"
|
|
18
20
|
},
|
|
19
21
|
"exports": {
|
|
20
|
-
"./src/*":
|
|
22
|
+
"./src/*": {
|
|
23
|
+
"types": "./dist/*.d.ts",
|
|
24
|
+
"default": "./dist/*.js"
|
|
25
|
+
}
|
|
21
26
|
},
|
|
22
27
|
"scripts": {
|
|
23
|
-
"build": "tsc -p tsconfig.json
|
|
28
|
+
"build": "tsdown && tsc -p tsconfig.build.json",
|
|
24
29
|
"typecheck": "tsc -p tsconfig.json",
|
|
25
30
|
"test": "vitest run",
|
|
26
31
|
"test:node": "vitest run --project=node",
|
|
@@ -31,20 +36,26 @@
|
|
|
31
36
|
"dependencies": {
|
|
32
37
|
"@cloudflare/workers-types": "^5.20260729.1",
|
|
33
38
|
"@hono/zod-validator": "^0.9.0",
|
|
34
|
-
"@pithy-sh/cloudflare": "
|
|
35
|
-
"@pithy-sh/core": "
|
|
36
|
-
"hono": "^4.13.2",
|
|
37
|
-
"kysely": "^0.29.0",
|
|
38
|
-
"zod": "^4.0.0"
|
|
39
|
+
"@pithy-sh/cloudflare": "^0.1.3",
|
|
40
|
+
"@pithy-sh/core": "^0.1.3"
|
|
39
41
|
},
|
|
40
42
|
"devDependencies": {
|
|
41
43
|
"@cloudflare/vitest-plugin": "^1.0.0",
|
|
42
44
|
"@pithy-sh/tsconfig": "workspace:*",
|
|
43
45
|
"@types/node": "^22.15.0",
|
|
44
46
|
"@vitest/coverage-v8": "^4.1.0",
|
|
47
|
+
"hono": "^4.13.2",
|
|
48
|
+
"kysely": "^0.29.0",
|
|
45
49
|
"kysely-d1": "^0.4.0",
|
|
50
|
+
"tsdown": "^0.23.0",
|
|
46
51
|
"typescript": "^7.0.2",
|
|
47
52
|
"vitest": "^4.1.0",
|
|
48
|
-
"wrangler": "^4.115.0"
|
|
53
|
+
"wrangler": "^4.115.0",
|
|
54
|
+
"zod": "^4.4.0"
|
|
55
|
+
},
|
|
56
|
+
"peerDependencies": {
|
|
57
|
+
"hono": "^4.13.2",
|
|
58
|
+
"kysely": "^0.29.0",
|
|
59
|
+
"zod": "^4.4.0"
|
|
49
60
|
}
|
|
50
61
|
}
|
package/src/version.generated.ts
CHANGED