@contractspec/module.audit-trail 1.56.1 → 1.58.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/dist/audit-trail.capability.d.ts +1 -6
- package/dist/audit-trail.capability.d.ts.map +1 -1
- package/dist/audit-trail.capability.js +17 -20
- package/dist/audit-trail.feature.d.ts +1 -7
- package/dist/audit-trail.feature.d.ts.map +1 -1
- package/dist/audit-trail.feature.js +31 -64
- package/dist/browser/audit-trail.capability.js +16 -0
- package/dist/browser/audit-trail.feature.js +32 -0
- package/dist/browser/contracts/index.js +241 -0
- package/dist/browser/entities/index.js +103 -0
- package/dist/browser/index.js +474 -0
- package/dist/browser/storage/index.js +101 -0
- package/dist/contracts/index.d.ts +549 -555
- package/dist/contracts/index.d.ts.map +1 -1
- package/dist/contracts/index.js +227 -355
- package/dist/entities/index.d.ts +70 -75
- package/dist/entities/index.d.ts.map +1 -1
- package/dist/entities/index.js +100 -122
- package/dist/index.d.ts +6 -6
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +474 -5
- package/dist/node/audit-trail.capability.js +16 -0
- package/dist/node/audit-trail.feature.js +32 -0
- package/dist/node/contracts/index.js +241 -0
- package/dist/node/entities/index.js +103 -0
- package/dist/node/index.js +474 -0
- package/dist/node/storage/index.js +101 -0
- package/dist/storage/index.d.ts +59 -64
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/storage/index.js +100 -110
- package/package.json +86 -25
- package/dist/audit-trail.capability.js.map +0 -1
- package/dist/audit-trail.feature.js.map +0 -1
- package/dist/contracts/index.js.map +0 -1
- package/dist/entities/index.js.map +0 -1
- package/dist/storage/index.js.map +0 -1
|
@@ -0,0 +1,474 @@
|
|
|
1
|
+
// src/audit-trail.feature.ts
|
|
2
|
+
import { defineFeature } from "@contractspec/lib.contracts";
|
|
3
|
+
var AuditTrailFeature = defineFeature({
|
|
4
|
+
meta: {
|
|
5
|
+
key: "audit-trail",
|
|
6
|
+
title: "Audit Trail",
|
|
7
|
+
description: "Audit logging, querying, export, and compliance reporting",
|
|
8
|
+
domain: "platform",
|
|
9
|
+
version: "1.0.0",
|
|
10
|
+
owners: ["@platform.audit-trail"],
|
|
11
|
+
tags: ["audit", "compliance", "logging", "security"],
|
|
12
|
+
stability: "stable"
|
|
13
|
+
},
|
|
14
|
+
operations: [
|
|
15
|
+
{ key: "audit.logs.export", version: "1.0.0" },
|
|
16
|
+
{ key: "audit.logs.query", version: "1.0.0" },
|
|
17
|
+
{ key: "audit.logs.get", version: "1.0.0" },
|
|
18
|
+
{ key: "audit.trace.get", version: "1.0.0" },
|
|
19
|
+
{ key: "audit.stats", version: "1.0.0" }
|
|
20
|
+
],
|
|
21
|
+
events: [],
|
|
22
|
+
presentations: [],
|
|
23
|
+
opToPresentation: [],
|
|
24
|
+
presentationsTargets: [],
|
|
25
|
+
capabilities: {
|
|
26
|
+
provides: [{ key: "audit-trail", version: "1.0.0" }],
|
|
27
|
+
requires: []
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
// src/contracts/index.ts
|
|
32
|
+
import {
|
|
33
|
+
defineCommand,
|
|
34
|
+
defineQuery,
|
|
35
|
+
defineSchemaModel
|
|
36
|
+
} from "@contractspec/lib.contracts";
|
|
37
|
+
import { ScalarTypeEnum, defineEnum } from "@contractspec/lib.schema";
|
|
38
|
+
var OWNERS = ["platform.audit-trail"];
|
|
39
|
+
var AuditLogModel = defineSchemaModel({
|
|
40
|
+
name: "AuditLog",
|
|
41
|
+
description: "Detailed audit log entry",
|
|
42
|
+
fields: {
|
|
43
|
+
id: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
44
|
+
eventName: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
45
|
+
eventVersion: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
46
|
+
payload: { type: ScalarTypeEnum.JSONObject(), isOptional: false },
|
|
47
|
+
actorId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
|
|
48
|
+
actorType: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
|
|
49
|
+
actorEmail: { type: ScalarTypeEnum.EmailAddress(), isOptional: true },
|
|
50
|
+
targetId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
|
|
51
|
+
targetType: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
|
|
52
|
+
orgId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
|
|
53
|
+
traceId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
|
|
54
|
+
clientIp: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
|
|
55
|
+
occurredAt: { type: ScalarTypeEnum.DateTime(), isOptional: false },
|
|
56
|
+
recordedAt: { type: ScalarTypeEnum.DateTime(), isOptional: false }
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
var AuditQueryInputModel = defineSchemaModel({
|
|
60
|
+
name: "AuditQueryInput",
|
|
61
|
+
description: "Input for querying audit logs",
|
|
62
|
+
fields: {
|
|
63
|
+
eventName: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
|
|
64
|
+
actorId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
|
|
65
|
+
targetId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
|
|
66
|
+
targetType: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
|
|
67
|
+
orgId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
|
|
68
|
+
traceId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
|
|
69
|
+
from: { type: ScalarTypeEnum.DateTime(), isOptional: true },
|
|
70
|
+
to: { type: ScalarTypeEnum.DateTime(), isOptional: true },
|
|
71
|
+
limit: {
|
|
72
|
+
type: ScalarTypeEnum.Int_unsecure(),
|
|
73
|
+
isOptional: true,
|
|
74
|
+
defaultValue: 100
|
|
75
|
+
},
|
|
76
|
+
offset: {
|
|
77
|
+
type: ScalarTypeEnum.Int_unsecure(),
|
|
78
|
+
isOptional: true,
|
|
79
|
+
defaultValue: 0
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
var AuditQueryOutputModel = defineSchemaModel({
|
|
84
|
+
name: "AuditQueryOutput",
|
|
85
|
+
description: "Output from querying audit logs",
|
|
86
|
+
fields: {
|
|
87
|
+
logs: { type: AuditLogModel, isArray: true, isOptional: false },
|
|
88
|
+
total: { type: ScalarTypeEnum.Int_unsecure(), isOptional: false },
|
|
89
|
+
hasMore: { type: ScalarTypeEnum.Boolean(), isOptional: false }
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
var ExportFormatEnum = defineEnum("ExportFormat", [
|
|
93
|
+
"json",
|
|
94
|
+
"csv",
|
|
95
|
+
"parquet"
|
|
96
|
+
]);
|
|
97
|
+
var AuditExportInputModel = defineSchemaModel({
|
|
98
|
+
name: "AuditExportInput",
|
|
99
|
+
description: "Input for exporting audit logs",
|
|
100
|
+
fields: {
|
|
101
|
+
orgId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
102
|
+
from: { type: ScalarTypeEnum.DateTime(), isOptional: false },
|
|
103
|
+
to: { type: ScalarTypeEnum.DateTime(), isOptional: false },
|
|
104
|
+
format: { type: ExportFormatEnum, isOptional: true, defaultValue: "json" },
|
|
105
|
+
eventNames: {
|
|
106
|
+
type: ScalarTypeEnum.String_unsecure(),
|
|
107
|
+
isArray: true,
|
|
108
|
+
isOptional: true
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
var ExportStatusEnum = defineEnum("ExportStatus", [
|
|
113
|
+
"pending",
|
|
114
|
+
"processing",
|
|
115
|
+
"completed",
|
|
116
|
+
"failed"
|
|
117
|
+
]);
|
|
118
|
+
var AuditExportOutputModel = defineSchemaModel({
|
|
119
|
+
name: "AuditExportOutput",
|
|
120
|
+
description: "Output from initiating an audit export",
|
|
121
|
+
fields: {
|
|
122
|
+
exportId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
123
|
+
status: { type: ExportStatusEnum, isOptional: false },
|
|
124
|
+
downloadUrl: { type: ScalarTypeEnum.URL(), isOptional: true }
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
var AuditStatsInputModel = defineSchemaModel({
|
|
128
|
+
name: "AuditStatsInput",
|
|
129
|
+
description: "Input for getting audit statistics",
|
|
130
|
+
fields: {
|
|
131
|
+
orgId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
|
|
132
|
+
from: { type: ScalarTypeEnum.DateTime(), isOptional: true },
|
|
133
|
+
to: { type: ScalarTypeEnum.DateTime(), isOptional: true }
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
var AuditStatsOutputModel = defineSchemaModel({
|
|
137
|
+
name: "AuditStatsOutput",
|
|
138
|
+
description: "Audit log statistics",
|
|
139
|
+
fields: {
|
|
140
|
+
totalLogs: { type: ScalarTypeEnum.Int_unsecure(), isOptional: false },
|
|
141
|
+
uniqueActors: { type: ScalarTypeEnum.Int_unsecure(), isOptional: false },
|
|
142
|
+
uniqueTargets: { type: ScalarTypeEnum.Int_unsecure(), isOptional: false },
|
|
143
|
+
eventCounts: { type: ScalarTypeEnum.JSONObject(), isOptional: false }
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
var QueryAuditLogsContract = defineQuery({
|
|
147
|
+
meta: {
|
|
148
|
+
key: "audit.logs.query",
|
|
149
|
+
version: "1.0.0",
|
|
150
|
+
stability: "stable",
|
|
151
|
+
owners: [...OWNERS],
|
|
152
|
+
tags: ["audit", "logs", "query"],
|
|
153
|
+
description: "Query audit logs with filters.",
|
|
154
|
+
goal: "Enable searching and filtering of audit history.",
|
|
155
|
+
context: "Admin dashboard, compliance reporting, debugging."
|
|
156
|
+
},
|
|
157
|
+
io: {
|
|
158
|
+
input: AuditQueryInputModel,
|
|
159
|
+
output: AuditQueryOutputModel
|
|
160
|
+
},
|
|
161
|
+
policy: {
|
|
162
|
+
auth: "admin"
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
var GetAuditLogContract = defineQuery({
|
|
166
|
+
meta: {
|
|
167
|
+
key: "audit.logs.get",
|
|
168
|
+
version: "1.0.0",
|
|
169
|
+
stability: "stable",
|
|
170
|
+
owners: [...OWNERS],
|
|
171
|
+
tags: ["audit", "logs", "get"],
|
|
172
|
+
description: "Get a specific audit log by ID.",
|
|
173
|
+
goal: "View detailed audit log entry.",
|
|
174
|
+
context: "Log detail view."
|
|
175
|
+
},
|
|
176
|
+
io: {
|
|
177
|
+
input: defineSchemaModel({
|
|
178
|
+
name: "GetAuditLogInput",
|
|
179
|
+
fields: {
|
|
180
|
+
logId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false }
|
|
181
|
+
}
|
|
182
|
+
}),
|
|
183
|
+
output: AuditLogModel
|
|
184
|
+
},
|
|
185
|
+
policy: {
|
|
186
|
+
auth: "admin"
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
var GetAuditTraceContract = defineQuery({
|
|
190
|
+
meta: {
|
|
191
|
+
key: "audit.trace.get",
|
|
192
|
+
version: "1.0.0",
|
|
193
|
+
stability: "stable",
|
|
194
|
+
owners: [...OWNERS],
|
|
195
|
+
tags: ["audit", "trace", "get"],
|
|
196
|
+
description: "Get all audit logs for a trace.",
|
|
197
|
+
goal: "View complete request trace for debugging.",
|
|
198
|
+
context: "Request tracing, debugging."
|
|
199
|
+
},
|
|
200
|
+
io: {
|
|
201
|
+
input: defineSchemaModel({
|
|
202
|
+
name: "GetAuditTraceInput",
|
|
203
|
+
fields: {
|
|
204
|
+
traceId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false }
|
|
205
|
+
}
|
|
206
|
+
}),
|
|
207
|
+
output: defineSchemaModel({
|
|
208
|
+
name: "GetAuditTraceOutput",
|
|
209
|
+
fields: {
|
|
210
|
+
logs: { type: AuditLogModel, isArray: true, isOptional: false }
|
|
211
|
+
}
|
|
212
|
+
})
|
|
213
|
+
},
|
|
214
|
+
policy: {
|
|
215
|
+
auth: "admin"
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
var ExportAuditLogsContract = defineCommand({
|
|
219
|
+
meta: {
|
|
220
|
+
key: "audit.logs.export",
|
|
221
|
+
version: "1.0.0",
|
|
222
|
+
stability: "stable",
|
|
223
|
+
owners: [...OWNERS],
|
|
224
|
+
tags: ["audit", "logs", "export"],
|
|
225
|
+
description: "Export audit logs for compliance reporting.",
|
|
226
|
+
goal: "Generate audit reports for compliance.",
|
|
227
|
+
context: "Compliance reporting, external audits."
|
|
228
|
+
},
|
|
229
|
+
io: {
|
|
230
|
+
input: AuditExportInputModel,
|
|
231
|
+
output: AuditExportOutputModel
|
|
232
|
+
},
|
|
233
|
+
policy: {
|
|
234
|
+
auth: "admin"
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
var GetAuditStatsContract = defineQuery({
|
|
238
|
+
meta: {
|
|
239
|
+
key: "audit.stats",
|
|
240
|
+
version: "1.0.0",
|
|
241
|
+
stability: "stable",
|
|
242
|
+
owners: [...OWNERS],
|
|
243
|
+
tags: ["audit", "stats"],
|
|
244
|
+
description: "Get audit log statistics.",
|
|
245
|
+
goal: "Monitor audit activity levels.",
|
|
246
|
+
context: "Admin dashboard, monitoring."
|
|
247
|
+
},
|
|
248
|
+
io: {
|
|
249
|
+
input: AuditStatsInputModel,
|
|
250
|
+
output: AuditStatsOutputModel
|
|
251
|
+
},
|
|
252
|
+
policy: {
|
|
253
|
+
auth: "admin"
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
// src/entities/index.ts
|
|
258
|
+
import { defineEntity, field, index } from "@contractspec/lib.schema";
|
|
259
|
+
var AuditLogEntity = defineEntity({
|
|
260
|
+
name: "AuditLog",
|
|
261
|
+
description: "Audit log entry for tracking system events.",
|
|
262
|
+
schema: "lssm_audit",
|
|
263
|
+
map: "audit_log",
|
|
264
|
+
fields: {
|
|
265
|
+
id: field.id({ description: "Unique audit log ID" }),
|
|
266
|
+
eventName: field.string({ description: "Event name/type" }),
|
|
267
|
+
eventVersion: field.int({ description: "Event version" }),
|
|
268
|
+
payload: field.json({ description: "Event payload (may be redacted)" }),
|
|
269
|
+
actorId: field.string({
|
|
270
|
+
isOptional: true,
|
|
271
|
+
description: "User/service that triggered the event"
|
|
272
|
+
}),
|
|
273
|
+
actorType: field.string({
|
|
274
|
+
isOptional: true,
|
|
275
|
+
description: "Actor type (user, system, service)"
|
|
276
|
+
}),
|
|
277
|
+
actorEmail: field.string({
|
|
278
|
+
isOptional: true,
|
|
279
|
+
description: "Actor email (for searchability)"
|
|
280
|
+
}),
|
|
281
|
+
targetId: field.string({
|
|
282
|
+
isOptional: true,
|
|
283
|
+
description: "Resource affected by the event"
|
|
284
|
+
}),
|
|
285
|
+
targetType: field.string({
|
|
286
|
+
isOptional: true,
|
|
287
|
+
description: "Resource type"
|
|
288
|
+
}),
|
|
289
|
+
orgId: field.string({
|
|
290
|
+
isOptional: true,
|
|
291
|
+
description: "Organization context"
|
|
292
|
+
}),
|
|
293
|
+
tenantId: field.string({ isOptional: true, description: "Tenant context" }),
|
|
294
|
+
traceId: field.string({
|
|
295
|
+
isOptional: true,
|
|
296
|
+
description: "Distributed trace ID"
|
|
297
|
+
}),
|
|
298
|
+
spanId: field.string({ isOptional: true, description: "Span ID" }),
|
|
299
|
+
requestId: field.string({ isOptional: true, description: "Request ID" }),
|
|
300
|
+
sessionId: field.string({ isOptional: true, description: "Session ID" }),
|
|
301
|
+
clientIp: field.string({
|
|
302
|
+
isOptional: true,
|
|
303
|
+
description: "Client IP address"
|
|
304
|
+
}),
|
|
305
|
+
userAgent: field.string({
|
|
306
|
+
isOptional: true,
|
|
307
|
+
description: "User agent string"
|
|
308
|
+
}),
|
|
309
|
+
tags: field.json({
|
|
310
|
+
isOptional: true,
|
|
311
|
+
description: "Custom tags for filtering"
|
|
312
|
+
}),
|
|
313
|
+
metadata: field.json({
|
|
314
|
+
isOptional: true,
|
|
315
|
+
description: "Additional metadata"
|
|
316
|
+
}),
|
|
317
|
+
occurredAt: field.dateTime({ description: "When the event occurred" }),
|
|
318
|
+
recordedAt: field.createdAt({ description: "When the log was recorded" })
|
|
319
|
+
},
|
|
320
|
+
indexes: [
|
|
321
|
+
index.on(["actorId", "occurredAt"]),
|
|
322
|
+
index.on(["targetId", "occurredAt"]),
|
|
323
|
+
index.on(["orgId", "occurredAt"]),
|
|
324
|
+
index.on(["eventName", "occurredAt"]),
|
|
325
|
+
index.on(["traceId"]),
|
|
326
|
+
index.on(["occurredAt"])
|
|
327
|
+
]
|
|
328
|
+
});
|
|
329
|
+
var AuditLogArchiveEntity = defineEntity({
|
|
330
|
+
name: "AuditLogArchive",
|
|
331
|
+
description: "Archived audit logs for long-term retention.",
|
|
332
|
+
schema: "lssm_audit",
|
|
333
|
+
map: "audit_log_archive",
|
|
334
|
+
fields: {
|
|
335
|
+
id: field.id(),
|
|
336
|
+
batchId: field.string({ description: "Archive batch ID" }),
|
|
337
|
+
logCount: field.int({ description: "Number of logs in batch" }),
|
|
338
|
+
fromDate: field.dateTime({ description: "Earliest log in batch" }),
|
|
339
|
+
toDate: field.dateTime({ description: "Latest log in batch" }),
|
|
340
|
+
storagePath: field.string({ description: "Path to archived data" }),
|
|
341
|
+
storageType: field.string({ description: "Storage type (s3, gcs, file)" }),
|
|
342
|
+
compressedSize: field.int({ description: "Compressed size in bytes" }),
|
|
343
|
+
checksum: field.string({ description: "SHA-256 checksum" }),
|
|
344
|
+
retainUntil: field.dateTime({ description: "When archive can be deleted" }),
|
|
345
|
+
createdAt: field.createdAt()
|
|
346
|
+
},
|
|
347
|
+
indexes: [index.on(["fromDate", "toDate"]), index.on(["retainUntil"])]
|
|
348
|
+
});
|
|
349
|
+
var auditTrailEntities = [AuditLogEntity, AuditLogArchiveEntity];
|
|
350
|
+
var auditTrailSchemaContribution = {
|
|
351
|
+
moduleId: "@contractspec/module.audit-trail",
|
|
352
|
+
entities: auditTrailEntities
|
|
353
|
+
};
|
|
354
|
+
|
|
355
|
+
// src/storage/index.ts
|
|
356
|
+
class InMemoryAuditStorage {
|
|
357
|
+
records = [];
|
|
358
|
+
async store(record) {
|
|
359
|
+
this.records.push(record);
|
|
360
|
+
}
|
|
361
|
+
async query(options) {
|
|
362
|
+
let results = [...this.records];
|
|
363
|
+
if (options.eventKey) {
|
|
364
|
+
const pattern = options.eventKey.replace(/\*/g, ".*");
|
|
365
|
+
const regex = new RegExp(`^${pattern}$`);
|
|
366
|
+
results = results.filter((r) => regex.test(r.eventKey));
|
|
367
|
+
}
|
|
368
|
+
if (options.actorId) {
|
|
369
|
+
results = results.filter((r) => r.metadata?.actorId === options.actorId);
|
|
370
|
+
}
|
|
371
|
+
if (options.targetId) {
|
|
372
|
+
results = results.filter((r) => r.metadata?.targetId === options.targetId);
|
|
373
|
+
}
|
|
374
|
+
if (options.targetType) {
|
|
375
|
+
results = results.filter((r) => r.metadata?.targetType === options.targetType);
|
|
376
|
+
}
|
|
377
|
+
if (options.orgId) {
|
|
378
|
+
results = results.filter((r) => r.metadata?.orgId === options.orgId);
|
|
379
|
+
}
|
|
380
|
+
if (options.traceId) {
|
|
381
|
+
results = results.filter((r) => r.traceId === options.traceId);
|
|
382
|
+
}
|
|
383
|
+
if (options.from) {
|
|
384
|
+
const fromDate = options.from;
|
|
385
|
+
results = results.filter((r) => new Date(r.occurredAt) >= fromDate);
|
|
386
|
+
}
|
|
387
|
+
if (options.to) {
|
|
388
|
+
const toDate = options.to;
|
|
389
|
+
results = results.filter((r) => new Date(r.occurredAt) <= toDate);
|
|
390
|
+
}
|
|
391
|
+
const orderBy = options.orderBy ?? "occurredAt";
|
|
392
|
+
const orderDir = options.orderDir ?? "desc";
|
|
393
|
+
results.sort((a, b) => {
|
|
394
|
+
const aTime = orderBy === "occurredAt" ? new Date(a.occurredAt).getTime() : a.recordedAt.getTime();
|
|
395
|
+
const bTime = orderBy === "occurredAt" ? new Date(b.occurredAt).getTime() : b.recordedAt.getTime();
|
|
396
|
+
return orderDir === "desc" ? bTime - aTime : aTime - bTime;
|
|
397
|
+
});
|
|
398
|
+
const offset = options.offset ?? 0;
|
|
399
|
+
const limit = options.limit ?? 100;
|
|
400
|
+
return results.slice(offset, offset + limit);
|
|
401
|
+
}
|
|
402
|
+
async count(options) {
|
|
403
|
+
const results = await this.query({ ...options, limit: Infinity });
|
|
404
|
+
return results.length;
|
|
405
|
+
}
|
|
406
|
+
async getById(id) {
|
|
407
|
+
return this.records.find((r) => r.id === id) ?? null;
|
|
408
|
+
}
|
|
409
|
+
async getByTraceId(traceId) {
|
|
410
|
+
return this.query({ traceId });
|
|
411
|
+
}
|
|
412
|
+
async deleteOlderThan(date) {
|
|
413
|
+
const before = this.records.length;
|
|
414
|
+
this.records = this.records.filter((r) => new Date(r.occurredAt) >= date);
|
|
415
|
+
return before - this.records.length;
|
|
416
|
+
}
|
|
417
|
+
getAll() {
|
|
418
|
+
return [...this.records];
|
|
419
|
+
}
|
|
420
|
+
clear() {
|
|
421
|
+
this.records = [];
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
class RetentionPolicy {
|
|
426
|
+
config;
|
|
427
|
+
constructor(config) {
|
|
428
|
+
this.config = config;
|
|
429
|
+
}
|
|
430
|
+
getHotCutoff() {
|
|
431
|
+
const cutoff = new Date;
|
|
432
|
+
cutoff.setDate(cutoff.getDate() - this.config.hotRetentionDays);
|
|
433
|
+
return cutoff;
|
|
434
|
+
}
|
|
435
|
+
getArchiveCutoff() {
|
|
436
|
+
if (!this.config.archiveRetentionDays)
|
|
437
|
+
return null;
|
|
438
|
+
const cutoff = new Date;
|
|
439
|
+
cutoff.setDate(cutoff.getDate() - this.config.archiveRetentionDays);
|
|
440
|
+
return cutoff;
|
|
441
|
+
}
|
|
442
|
+
async apply(storage) {
|
|
443
|
+
const cutoff = this.getHotCutoff();
|
|
444
|
+
const deletedCount = await storage.deleteOlderThan(cutoff);
|
|
445
|
+
return { deletedCount };
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
function createInMemoryAuditStorage() {
|
|
449
|
+
return new InMemoryAuditStorage;
|
|
450
|
+
}
|
|
451
|
+
export {
|
|
452
|
+
createInMemoryAuditStorage,
|
|
453
|
+
auditTrailSchemaContribution,
|
|
454
|
+
auditTrailEntities,
|
|
455
|
+
RetentionPolicy,
|
|
456
|
+
QueryAuditLogsContract,
|
|
457
|
+
InMemoryAuditStorage,
|
|
458
|
+
GetAuditTraceContract,
|
|
459
|
+
GetAuditStatsContract,
|
|
460
|
+
GetAuditLogContract,
|
|
461
|
+
ExportStatusEnum,
|
|
462
|
+
ExportFormatEnum,
|
|
463
|
+
ExportAuditLogsContract,
|
|
464
|
+
AuditTrailFeature,
|
|
465
|
+
AuditStatsOutputModel,
|
|
466
|
+
AuditStatsInputModel,
|
|
467
|
+
AuditQueryOutputModel,
|
|
468
|
+
AuditQueryInputModel,
|
|
469
|
+
AuditLogModel,
|
|
470
|
+
AuditLogEntity,
|
|
471
|
+
AuditLogArchiveEntity,
|
|
472
|
+
AuditExportOutputModel,
|
|
473
|
+
AuditExportInputModel
|
|
474
|
+
};
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// src/storage/index.ts
|
|
2
|
+
class InMemoryAuditStorage {
|
|
3
|
+
records = [];
|
|
4
|
+
async store(record) {
|
|
5
|
+
this.records.push(record);
|
|
6
|
+
}
|
|
7
|
+
async query(options) {
|
|
8
|
+
let results = [...this.records];
|
|
9
|
+
if (options.eventKey) {
|
|
10
|
+
const pattern = options.eventKey.replace(/\*/g, ".*");
|
|
11
|
+
const regex = new RegExp(`^${pattern}$`);
|
|
12
|
+
results = results.filter((r) => regex.test(r.eventKey));
|
|
13
|
+
}
|
|
14
|
+
if (options.actorId) {
|
|
15
|
+
results = results.filter((r) => r.metadata?.actorId === options.actorId);
|
|
16
|
+
}
|
|
17
|
+
if (options.targetId) {
|
|
18
|
+
results = results.filter((r) => r.metadata?.targetId === options.targetId);
|
|
19
|
+
}
|
|
20
|
+
if (options.targetType) {
|
|
21
|
+
results = results.filter((r) => r.metadata?.targetType === options.targetType);
|
|
22
|
+
}
|
|
23
|
+
if (options.orgId) {
|
|
24
|
+
results = results.filter((r) => r.metadata?.orgId === options.orgId);
|
|
25
|
+
}
|
|
26
|
+
if (options.traceId) {
|
|
27
|
+
results = results.filter((r) => r.traceId === options.traceId);
|
|
28
|
+
}
|
|
29
|
+
if (options.from) {
|
|
30
|
+
const fromDate = options.from;
|
|
31
|
+
results = results.filter((r) => new Date(r.occurredAt) >= fromDate);
|
|
32
|
+
}
|
|
33
|
+
if (options.to) {
|
|
34
|
+
const toDate = options.to;
|
|
35
|
+
results = results.filter((r) => new Date(r.occurredAt) <= toDate);
|
|
36
|
+
}
|
|
37
|
+
const orderBy = options.orderBy ?? "occurredAt";
|
|
38
|
+
const orderDir = options.orderDir ?? "desc";
|
|
39
|
+
results.sort((a, b) => {
|
|
40
|
+
const aTime = orderBy === "occurredAt" ? new Date(a.occurredAt).getTime() : a.recordedAt.getTime();
|
|
41
|
+
const bTime = orderBy === "occurredAt" ? new Date(b.occurredAt).getTime() : b.recordedAt.getTime();
|
|
42
|
+
return orderDir === "desc" ? bTime - aTime : aTime - bTime;
|
|
43
|
+
});
|
|
44
|
+
const offset = options.offset ?? 0;
|
|
45
|
+
const limit = options.limit ?? 100;
|
|
46
|
+
return results.slice(offset, offset + limit);
|
|
47
|
+
}
|
|
48
|
+
async count(options) {
|
|
49
|
+
const results = await this.query({ ...options, limit: Infinity });
|
|
50
|
+
return results.length;
|
|
51
|
+
}
|
|
52
|
+
async getById(id) {
|
|
53
|
+
return this.records.find((r) => r.id === id) ?? null;
|
|
54
|
+
}
|
|
55
|
+
async getByTraceId(traceId) {
|
|
56
|
+
return this.query({ traceId });
|
|
57
|
+
}
|
|
58
|
+
async deleteOlderThan(date) {
|
|
59
|
+
const before = this.records.length;
|
|
60
|
+
this.records = this.records.filter((r) => new Date(r.occurredAt) >= date);
|
|
61
|
+
return before - this.records.length;
|
|
62
|
+
}
|
|
63
|
+
getAll() {
|
|
64
|
+
return [...this.records];
|
|
65
|
+
}
|
|
66
|
+
clear() {
|
|
67
|
+
this.records = [];
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
class RetentionPolicy {
|
|
72
|
+
config;
|
|
73
|
+
constructor(config) {
|
|
74
|
+
this.config = config;
|
|
75
|
+
}
|
|
76
|
+
getHotCutoff() {
|
|
77
|
+
const cutoff = new Date;
|
|
78
|
+
cutoff.setDate(cutoff.getDate() - this.config.hotRetentionDays);
|
|
79
|
+
return cutoff;
|
|
80
|
+
}
|
|
81
|
+
getArchiveCutoff() {
|
|
82
|
+
if (!this.config.archiveRetentionDays)
|
|
83
|
+
return null;
|
|
84
|
+
const cutoff = new Date;
|
|
85
|
+
cutoff.setDate(cutoff.getDate() - this.config.archiveRetentionDays);
|
|
86
|
+
return cutoff;
|
|
87
|
+
}
|
|
88
|
+
async apply(storage) {
|
|
89
|
+
const cutoff = this.getHotCutoff();
|
|
90
|
+
const deletedCount = await storage.deleteOlderThan(cutoff);
|
|
91
|
+
return { deletedCount };
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
function createInMemoryAuditStorage() {
|
|
95
|
+
return new InMemoryAuditStorage;
|
|
96
|
+
}
|
|
97
|
+
export {
|
|
98
|
+
createInMemoryAuditStorage,
|
|
99
|
+
RetentionPolicy,
|
|
100
|
+
InMemoryAuditStorage
|
|
101
|
+
};
|