-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathservice.ts
More file actions
552 lines (494 loc) · 15.9 KB
/
Copy pathservice.ts
File metadata and controls
552 lines (494 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/**
* @module audit/service
* @description High-level audit logging service.
*
* Provides a clean API for application code to emit audit events without
* coupling directly to the store implementation. All sensitive state changes
* (contract lifecycle, payments, user management, auth events) must go through
* this service.
*
* Security notes:
* - Callers MUST sanitise metadata before passing it in — no raw PII.
* - Logging failures are caught and reported via console.error to avoid
* disrupting the primary request flow, but they are also re-thrown in
* strict mode so tests can assert on them.
*/
import type { AuditEntry, AuditQuery, AuditSeverity, CreateAuditEntryInput, IntegrityReport, AuditQueryResult } from './types';
import type { AuditAction } from './types';
import { decodeCursor } from './types';
import { createDefaultAuditRepository, type AuditLogRepository } from './repository';
import { auditExportService, AuditExportService, type AuditExportFilters, type AuditExportResult } from './exportService';
import { AuditCache, type AuditCacheOptions } from './auditCache';
export interface AuditServiceOptions {
/** Cache options for audit read responses. */
cache?: AuditCacheOptions;
}
export const VALID_ACTIONS = new Set<AuditAction>([
'CONTRACT_CREATED', 'CONTRACT_UPDATED', 'CONTRACT_CANCELLED', 'CONTRACT_COMPLETED',
'PAYMENT_INITIATED', 'PAYMENT_RELEASED', 'PAYMENT_DISPUTED',
'REPUTATION_UPDATED',
'REPUTATION_CORRECTED',
'USER_CREATED', 'USER_UPDATED', 'USER_DELETED',
'AUTH_LOGIN', 'AUTH_LOGOUT', 'AUTH_FAILED',
'AUTH_LOCKOUT_TRIGGERED', 'AUTH_LOCKOUT_RELEASED',
'ADMIN_ACTION',
'ENDPOINT_ACCESS', 'ENDPOINT_MUTATION',
]);
export const VALID_SEVERITIES = new Set<AuditSeverity>(['INFO', 'WARNING', 'CRITICAL']);
export function parseOptionalIsoDate(
value: string | undefined,
fieldName: 'from' | 'to',
): string | undefined {
if (value === undefined) {
return undefined;
}
const parsed = Date.parse(value);
if (Number.isNaN(parsed)) {
throw new Error(`Invalid ${fieldName} timestamp`);
}
return new Date(parsed).toISOString();
}
export function parseOffset(value: string | undefined): number {
if (value === undefined) {
return 0;
}
const parsed = Number.parseInt(value, 10);
if (!Number.isFinite(parsed) || parsed < 0) {
throw new Error('Invalid offset');
}
return parsed;
}
export function parseLimit(value: string | undefined, maxLimit: number, defaultLimit?: number): number | undefined {
if (value === undefined) {
return defaultLimit;
}
const parsed = Number.parseInt(value, 10);
if (!Number.isFinite(parsed) || parsed < 1) {
throw new Error('Invalid limit');
}
return Math.min(parsed, maxLimit);
}
export function parseAuditQuery(
reqQuery: Record<string, unknown>,
options: { defaultLimit?: number; maxLimit: number },
): { query: AuditQuery; limit?: number; offset: number } {
const action = reqQuery['action'] as string | undefined;
const severity = reqQuery['severity'] as string | undefined;
const actor = reqQuery['actor'] as string | undefined;
const resource = reqQuery['resource'] as string | undefined;
const resourceId = reqQuery['resourceId'] as string | undefined;
const cursor = reqQuery['cursor'] as string | undefined;
if (action && !VALID_ACTIONS.has(action as AuditAction)) {
throw new Error(`Invalid action: ${action}`);
}
if (severity && !VALID_SEVERITIES.has(severity as AuditSeverity)) {
throw new Error(`Invalid severity: ${severity}`);
}
const limit = parseLimit(reqQuery['limit'] as string | undefined, options.maxLimit, options.defaultLimit);
const offset = parseOffset(reqQuery['offset'] as string | undefined);
const from = parseOptionalIsoDate(reqQuery['from'] as string | undefined, 'from');
const to = parseOptionalIsoDate(reqQuery['to'] as string | undefined, 'to');
// Validate cursor format if provided
if (cursor) {
try {
decodeCursor(cursor);
} catch (_error) {
throw new Error('Invalid cursor format');
}
}
return {
query: {
...(action && { action: action as AuditAction }),
...(severity && { severity: severity as AuditSeverity }),
...(actor && { actor }),
...(resource && { resource }),
...(resourceId && { resourceId }),
...(from && { from }),
...(to && { to }),
...(limit !== undefined && { limit }),
offset,
...(cursor && { cursor }),
},
limit,
offset,
};
}
/**
* AuditService — application-level facade over AuditStore.
*
* @example
* ```ts
* import { auditService } from './audit/service';
*
* await auditService.log({
* action: 'CONTRACT_CREATED',
* severity: 'INFO',
* actor: req.user.id,
* resource: 'contract',
* resourceId: contract.id,
* metadata: { clientId: contract.clientId },
* ipAddress: req.ip,
* correlationId: req.headers['x-correlation-id'] as string,
* });
* ```
*/
export class AuditService {
private cache: AuditCache | null;
constructor(
private readonly repository: AuditLogRepository = createDefaultAuditRepository(),
private readonly options: AuditServiceOptions = {},
) {
this.cache = options.cache ? new AuditCache(options.cache) : null;
}
/**
* Records an audit event.
*
* @param input - Event details. metadata must be pre-sanitised.
* @returns The persisted, immutable AuditEntry.
* @throws Only when options.strict is true and the store throws.
*/
log(input: CreateAuditEntryInput): AuditEntry {
try {
const entry = this.repository.append(input);
// Invalidate cache on write operations
if (this.cache) {
this.cache.invalidateByResourceId(input.resourceId);
}
return entry;
} catch (err) {
console.error('[AuditService] Failed to persist audit entry:', err);
throw err;
}
}
/**
* Validates payload fields and creates an audit entry.
* Throws Error if any required field is missing.
*/
createEntry(input: CreateAuditEntryInput): AuditEntry {
if (!input.action || !input.severity || !input.actor || !input.resource || !input.resourceId) {
throw new Error('Missing required fields: action, severity, actor, resource, resourceId');
}
return this.log(input);
}
/**
* Validates raw query parameters and returns parsed AuditQuery.
*/
validateAndParseQuery(
reqQuery: Record<string, unknown>,
options: { defaultLimit?: number; maxLimit: number },
): { query: AuditQuery; limit?: number; offset: number } {
return parseAuditQuery(reqQuery, options);
}
/**
* Processes query filters and returns formatted paginated results.
*/
queryLogs(
queryParams: Record<string, unknown>,
options: { defaultLimit?: number; maxLimit: number } = { defaultLimit: 50, maxLimit: 100 },
):
| { entries: AuditEntry[]; count: number; limit?: number; nextCursor?: string }
| { entries: AuditEntry[]; count: number; limit: number; offset: number } {
const { query } = this.validateAndParseQuery(queryParams, options);
if (query.cursor) {
const result = this.queryWithCursor(query);
return {
entries: result.entries,
count: result.count,
limit: result.limit,
nextCursor: result.nextCursor,
};
}
const limit = query.limit ?? options.defaultLimit ?? 50;
const offset = query.offset ?? 0;
const entries = this.query(query);
return {
entries,
count: entries.length,
limit,
offset,
};
}
/**
* Orchestrates NDJSON compliance log exports and records an ADMIN_ACTION audit log.
*/
async exportAuditLogs(
queryParams: Record<string, unknown>,
context: { actor?: string; ipAddress?: string; correlationId?: string },
exportService: AuditExportService = auditExportService,
): Promise<AuditExportResult> {
const { query } = this.validateAndParseQuery(queryParams, { maxLimit: 50_000 });
const filters: AuditExportFilters = {
...(query.action && { action: query.action }),
...(query.severity && { severity: query.severity }),
...(query.actor && { actor: query.actor }),
...(query.resource && { resource: query.resource }),
...(query.resourceId && { resourceId: query.resourceId }),
...(query.from && { from: query.from }),
...(query.to && { to: query.to }),
...(query.limit !== undefined && { limit: query.limit }),
};
const exportResult = await exportService.createNdjsonExport(filters);
this.log({
action: 'ADMIN_ACTION',
severity: 'CRITICAL',
actor: context.actor ?? 'anonymous',
resource: 'audit-log',
resourceId: 'export',
metadata: {
operation: 'export',
format: 'ndjson',
filters: {
action: filters.action ?? null,
severity: filters.severity ?? null,
actor: filters.actor ?? null,
resource: filters.resource ?? null,
resourceId: filters.resourceId ?? null,
from: filters.from ?? null,
to: filters.to ?? null,
},
recordCount: exportResult.recordCount,
bytesWritten: exportResult.bytesWritten,
},
ipAddress: context.ipAddress,
correlationId: context.correlationId,
});
return exportResult;
}
/**
* Convenience wrapper for contract lifecycle events.
*/
logContractEvent(
action: Extract<AuditAction, `CONTRACT_${string}`>,
actor: string,
contractId: string,
metadata: Record<string, unknown> = {},
context: { ipAddress?: string; correlationId?: string } = {},
): AuditEntry {
return this.log({
action,
severity: 'INFO',
actor,
resource: 'contract',
resourceId: contractId,
metadata,
...context,
});
}
/**
* Convenience wrapper for milestone-mutation events on a contract.
*
* Milestones are a field on the Contract resource rather than a
* separately-persisted entity, so `metadata` is expected to carry a
* `{ before, after }` pair of bounded, redacted snapshots (see
* `modules/contracts/milestonesAudit.ts`) rather than a DB row diff.
*
* MILESTONES_DELETED is WARNING severity — losing milestone data (whether
* via an explicit clear or a contract deletion) is the change most likely
* to matter during an incident review, so it is flagged above the default
* INFO level used for created/updated.
*/
logMilestonesEvent(
action: Extract<AuditAction, `MILESTONES_${string}`>,
actor: string,
contractId: string,
metadata: Record<string, unknown> = {},
context: { ipAddress?: string; correlationId?: string } = {},
): AuditEntry {
const severity: AuditSeverity = action === 'MILESTONES_DELETED' ? 'WARNING' : 'INFO';
return this.log({
action,
severity,
actor,
resource: 'milestones',
resourceId: contractId,
metadata,
...context,
});
}
/**
* Convenience wrapper for payment events.
* Payment events are always CRITICAL severity.
*/
logPaymentEvent(
action: Extract<AuditAction, `PAYMENT_${string}`>,
actor: string,
paymentId: string,
metadata: Record<string, unknown> = {},
context: { ipAddress?: string; correlationId?: string } = {},
): AuditEntry {
return this.log({
action,
severity: 'CRITICAL',
actor,
resource: 'payment',
resourceId: paymentId,
metadata,
...context,
});
}
/**
* Convenience wrapper for authentication events.
* AUTH_FAILED is WARNING; others are INFO.
*/
logAuthEvent(
action: Extract<AuditAction, `AUTH_${string}`>,
actor: string,
metadata: Record<string, unknown> = {},
context: { ipAddress?: string; correlationId?: string } = {},
): AuditEntry {
const severity: AuditSeverity = action === 'AUTH_FAILED' ? 'WARNING' : 'INFO';
return this.log({
action,
severity,
actor,
resource: 'auth',
resourceId: actor,
metadata,
...context,
});
}
/**
* Convenience wrapper for user management events.
* USER_DELETED is WARNING; others are INFO.
*/
logUserEvent(
action: Extract<AuditAction, `USER_${string}`>,
actor: string,
targetUserId: string,
metadata: Record<string, unknown> = {},
context: { ipAddress?: string; correlationId?: string } = {},
): AuditEntry {
const severity: AuditSeverity = action === 'USER_DELETED' ? 'WARNING' : 'INFO';
return this.log({
action,
severity,
actor,
resource: 'user',
resourceId: targetUserId,
metadata,
...context,
});
}
/**
* Convenience wrapper for dispute lifecycle events.
* DISPUTE_UPDATED is WARNING; others are INFO.
*/
logDisputeEvent(
action: Extract<AuditAction, `DISPUTE_${string}`>,
actor: string,
disputeId: string,
metadata: Record<string, unknown> = {},
context: { ipAddress?: string; correlationId?: string } = {},
): AuditEntry {
const severity: AuditSeverity = action === 'DISPUTE_UPDATED' ? 'WARNING' : 'INFO';
return this.log({
action,
severity,
actor,
resource: 'dispute',
resourceId: disputeId,
metadata,
...context,
});
}
/**
* Queries the audit log with optional filters.
*
* @param query - Filter and pagination options.
* @returns Matching entries in insertion order.
*/
query(query: AuditQuery = {}): AuditEntry[] {
// Check cache first
if (this.cache) {
const cached = this.cache.get(query, 'query');
if (cached) {
return cached as AuditEntry[];
}
}
// Cache miss - fetch from repository
const entries = this.repository.query(query);
// Store in cache
if (this.cache) {
this.cache.set(query, entries, 'query');
}
return entries;
}
/**
* Queries the audit log with cursor-based pagination.
*
* @param query - Filter and pagination options including cursor.
* @returns Paginated result with entries and next cursor.
*/
queryWithCursor(query: AuditQuery = {}): AuditQueryResult {
// Check cache first
if (this.cache) {
const cached = this.cache.get(query, 'queryWithCursor');
if (cached) {
return cached as AuditQueryResult;
}
}
// Cache miss - fetch from repository
const result = this.repository.queryWithCursor(query);
// Store in cache
if (this.cache) {
this.cache.set(query, result, 'queryWithCursor');
}
return result;
}
/**
* Streams audit entries for export use cases without loading all rows.
*/
stream(query: AuditQuery = {}): IterableIterator<AuditEntry> {
return this.repository.stream(query);
}
/**
* Retrieves a single audit entry by ID.
*/
getById(id: string): AuditEntry | undefined {
// Check cache first
if (this.cache) {
const cached = this.cache.get({}, 'getById', id);
if (cached) {
return cached as AuditEntry;
}
}
// Cache miss - fetch from repository
const entry = this.repository.getById(id);
// Store in cache
if (this.cache && entry) {
this.cache.set({}, entry, 'getById', id);
}
return entry;
}
/**
* Retrieves a single entry by ID (alias method).
*/
getEntry(id: string): AuditEntry | undefined {
return this.getById(id);
}
/**
* Returns the total number of audit entries.
*/
count(): number {
return this.repository.count();
}
/**
* Verifies the integrity of the entire hash chain.
* Should be called by a scheduled monitoring job.
*
* @returns IntegrityReport — escalate immediately if valid === false.
*/
verifyIntegrity(): IntegrityReport {
return this.repository.verifyIntegrity();
}
/**
* Checks hash chain integrity and returns report with HTTP status code.
*/
checkIntegrity(): { report: IntegrityReport; status: number } {
const report = this.verifyIntegrity();
const status = report.valid ? 200 : 409;
return { report, status };
}
}
/** Singleton service instance. */
export const auditService = new AuditService();