@flipboxlabs/aws-audit-sdk 1.1.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/LICENSE +21 -0
- package/README.md +30 -0
- package/dist/audits.d.ts +188 -0
- package/dist/audits.d.ts.map +1 -0
- package/dist/audits.js +225 -0
- package/dist/batch/batch-processor.d.ts +81 -0
- package/dist/batch/batch-processor.d.ts.map +1 -0
- package/dist/batch/batch-processor.js +102 -0
- package/dist/batch/index.d.ts +2 -0
- package/dist/batch/index.d.ts.map +1 -0
- package/dist/batch/index.js +1 -0
- package/dist/config.d.ts +65 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +44 -0
- package/dist/constants.d.ts +147 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +191 -0
- package/dist/events/batch-handler.d.ts +62 -0
- package/dist/events/batch-handler.d.ts.map +1 -0
- package/dist/events/batch-handler.js +92 -0
- package/dist/events/bus.d.ts +79 -0
- package/dist/events/bus.d.ts.map +1 -0
- package/dist/events/bus.js +86 -0
- package/dist/events/index.d.ts +3 -0
- package/dist/events/index.d.ts.map +1 -0
- package/dist/events/index.js +2 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/middleware/middy.d.ts +66 -0
- package/dist/middleware/middy.d.ts.map +1 -0
- package/dist/middleware/middy.js +105 -0
- package/dist/repository.d.ts +421 -0
- package/dist/repository.d.ts.map +1 -0
- package/dist/repository.js +486 -0
- package/dist/repository.utils.d.ts +37 -0
- package/dist/repository.utils.d.ts.map +1 -0
- package/dist/repository.utils.js +70 -0
- package/dist/schema/audit.d.ts +202 -0
- package/dist/schema/audit.d.ts.map +1 -0
- package/dist/schema/audit.js +48 -0
- package/dist/schema/common.d.ts +128 -0
- package/dist/schema/common.d.ts.map +1 -0
- package/dist/schema/common.js +148 -0
- package/dist/schema/index.d.ts +27 -0
- package/dist/schema/index.d.ts.map +1 -0
- package/dist/schema/index.js +26 -0
- package/dist/schema/log.d.ts +211 -0
- package/dist/schema/log.d.ts.map +1 -0
- package/dist/schema/log.js +179 -0
- package/dist/schema/model.d.ts +110 -0
- package/dist/schema/model.d.ts.map +1 -0
- package/dist/schema/model.js +86 -0
- package/dist/schema/service.d.ts +189 -0
- package/dist/schema/service.d.ts.map +1 -0
- package/dist/schema/service.js +89 -0
- package/dist/schema/storage.d.ts +105 -0
- package/dist/schema/storage.d.ts.map +1 -0
- package/dist/schema/storage.js +63 -0
- package/dist/service.d.ts +285 -0
- package/dist/service.d.ts.map +1 -0
- package/dist/service.js +237 -0
- package/dist/test-config.d.ts +56 -0
- package/dist/test-config.d.ts.map +1 -0
- package/dist/test-config.js +24 -0
- package/dist/types.d.ts +40 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/dist/utils.d.ts +231 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +313 -0
- package/package.json +85 -0
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { Logger } from "@aws-lambda-powertools/logger";
|
|
2
|
+
import { type PutEventsRequestEntry, type PutEventsResultEntry } from "@aws-sdk/client-eventbridge";
|
|
3
|
+
import type { UpsertAudit } from "../schema/service.js";
|
|
4
|
+
import { BatchHandler } from "./batch-handler.js";
|
|
5
|
+
/**
|
|
6
|
+
* Event detail payload structure for audit events.
|
|
7
|
+
*
|
|
8
|
+
* Wraps the audit data in a consistent structure for EventBridge events.
|
|
9
|
+
*/
|
|
10
|
+
export interface AuditEventDetail {
|
|
11
|
+
/** The audit record being published */
|
|
12
|
+
audit: UpsertAudit;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Publishes audit events to AWS EventBridge.
|
|
16
|
+
*
|
|
17
|
+
* Provides a high-level interface for publishing audit-related events
|
|
18
|
+
* to EventBridge, with automatic event formatting and batching.
|
|
19
|
+
*
|
|
20
|
+
* Events are published to the configured audit event bus with the
|
|
21
|
+
* appropriate source and detail type for downstream consumers.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* const eventBus = new AuditEventBus(logger);
|
|
26
|
+
*
|
|
27
|
+
* // Publish audit upsert events
|
|
28
|
+
* await eventBus.upserted([
|
|
29
|
+
* { id: 'audit-1', operation: 'createUser', ... },
|
|
30
|
+
* { id: 'audit-2', operation: 'updateUser', ... },
|
|
31
|
+
* ]);
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export declare class AuditEventBus {
|
|
35
|
+
protected readonly handler: BatchHandler;
|
|
36
|
+
/**
|
|
37
|
+
* Creates a new AuditEventBus instance.
|
|
38
|
+
*
|
|
39
|
+
* @param logger - Logger instance for the underlying BatchHandler
|
|
40
|
+
* @param handler - BatchHandler for EventBridge operations (defaults to new instance with EventBridgeClient)
|
|
41
|
+
*/
|
|
42
|
+
constructor(logger: Logger, handler?: BatchHandler);
|
|
43
|
+
/**
|
|
44
|
+
* Serializes an audit record into an EventBridge detail string.
|
|
45
|
+
*
|
|
46
|
+
* Wraps the audit in an AuditEventDetail structure and converts to JSON.
|
|
47
|
+
*
|
|
48
|
+
* @param audit - The audit record to serialize
|
|
49
|
+
* @returns JSON string containing the event detail
|
|
50
|
+
*
|
|
51
|
+
* @internal
|
|
52
|
+
*/
|
|
53
|
+
protected createDetail(audit: UpsertAudit): string;
|
|
54
|
+
/**
|
|
55
|
+
* Publishes audit "upserted" events to EventBridge.
|
|
56
|
+
*
|
|
57
|
+
* Creates EventBridge events for each audit item with:
|
|
58
|
+
* - EventBusName: Configured audit event bus
|
|
59
|
+
* - DetailType: "Upserted" event type
|
|
60
|
+
* - Source: Configured audit source
|
|
61
|
+
* - Detail: JSON-serialized audit data
|
|
62
|
+
*
|
|
63
|
+
* Additional events can be included via the extraEvents parameter.
|
|
64
|
+
*
|
|
65
|
+
* @param items - Array of audit records to publish as upserted events
|
|
66
|
+
* @param extraEvents - Optional additional EventBridge events to include in the batch
|
|
67
|
+
* @returns Array of result entries from EventBridge
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```typescript
|
|
71
|
+
* // Publish audit events with additional custom events
|
|
72
|
+
* const results = await eventBus.upserted(audits, [
|
|
73
|
+
* { DetailType: 'CustomEvent', Detail: '{}', Source: 'custom' },
|
|
74
|
+
* ]);
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
upserted(items: Array<UpsertAudit>, extraEvents?: PutEventsRequestEntry[]): Promise<PutEventsResultEntry[]>;
|
|
78
|
+
}
|
|
79
|
+
//# sourceMappingURL=bus.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bus.d.ts","sourceRoot":"","sources":["../../src/events/bus.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,+BAA+B,CAAC;AAC5D,OAAO,EAEN,KAAK,qBAAqB,EAC1B,KAAK,oBAAoB,EACzB,MAAM,6BAA6B,CAAC;AAErC,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACxD,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAElD;;;;GAIG;AACH,MAAM,WAAW,gBAAgB;IAChC,uCAAuC;IACvC,KAAK,EAAE,WAAW,CAAC;CACnB;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAAa,aAAa;IASxB,SAAS,CAAC,QAAQ,CAAC,OAAO,EAAE,YAAY;IARzC;;;;;OAKG;gBAEF,MAAM,EAAE,MAAM,EACK,OAAO,GAAE,YAM3B;IAGF;;;;;;;;;OASG;IACH,SAAS,CAAC,YAAY,CAAC,KAAK,EAAE,WAAW,GAAG,MAAM;IAQlD;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACU,QAAQ,CACpB,KAAK,EAAE,KAAK,CAAC,WAAW,CAAC,EACzB,WAAW,CAAC,EAAE,qBAAqB,EAAE,GACnC,OAAO,CAAC,oBAAoB,EAAE,CAAC;CAUlC"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { EventBridgeClient, } from "@aws-sdk/client-eventbridge";
|
|
2
|
+
import { EventBridge } from "../constants.js";
|
|
3
|
+
import { BatchHandler } from "./batch-handler.js";
|
|
4
|
+
/**
|
|
5
|
+
* Publishes audit events to AWS EventBridge.
|
|
6
|
+
*
|
|
7
|
+
* Provides a high-level interface for publishing audit-related events
|
|
8
|
+
* to EventBridge, with automatic event formatting and batching.
|
|
9
|
+
*
|
|
10
|
+
* Events are published to the configured audit event bus with the
|
|
11
|
+
* appropriate source and detail type for downstream consumers.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* const eventBus = new AuditEventBus(logger);
|
|
16
|
+
*
|
|
17
|
+
* // Publish audit upsert events
|
|
18
|
+
* await eventBus.upserted([
|
|
19
|
+
* { id: 'audit-1', operation: 'createUser', ... },
|
|
20
|
+
* { id: 'audit-2', operation: 'updateUser', ... },
|
|
21
|
+
* ]);
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export class AuditEventBus {
|
|
25
|
+
handler;
|
|
26
|
+
/**
|
|
27
|
+
* Creates a new AuditEventBus instance.
|
|
28
|
+
*
|
|
29
|
+
* @param logger - Logger instance for the underlying BatchHandler
|
|
30
|
+
* @param handler - BatchHandler for EventBridge operations (defaults to new instance with EventBridgeClient)
|
|
31
|
+
*/
|
|
32
|
+
constructor(logger, handler = new BatchHandler(logger, new EventBridgeClient({
|
|
33
|
+
region: process.env.AWS_REGION,
|
|
34
|
+
logger,
|
|
35
|
+
}))) {
|
|
36
|
+
this.handler = handler;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Serializes an audit record into an EventBridge detail string.
|
|
40
|
+
*
|
|
41
|
+
* Wraps the audit in an AuditEventDetail structure and converts to JSON.
|
|
42
|
+
*
|
|
43
|
+
* @param audit - The audit record to serialize
|
|
44
|
+
* @returns JSON string containing the event detail
|
|
45
|
+
*
|
|
46
|
+
* @internal
|
|
47
|
+
*/
|
|
48
|
+
createDetail(audit) {
|
|
49
|
+
const props = {
|
|
50
|
+
audit,
|
|
51
|
+
};
|
|
52
|
+
return JSON.stringify(props);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Publishes audit "upserted" events to EventBridge.
|
|
56
|
+
*
|
|
57
|
+
* Creates EventBridge events for each audit item with:
|
|
58
|
+
* - EventBusName: Configured audit event bus
|
|
59
|
+
* - DetailType: "Upserted" event type
|
|
60
|
+
* - Source: Configured audit source
|
|
61
|
+
* - Detail: JSON-serialized audit data
|
|
62
|
+
*
|
|
63
|
+
* Additional events can be included via the extraEvents parameter.
|
|
64
|
+
*
|
|
65
|
+
* @param items - Array of audit records to publish as upserted events
|
|
66
|
+
* @param extraEvents - Optional additional EventBridge events to include in the batch
|
|
67
|
+
* @returns Array of result entries from EventBridge
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```typescript
|
|
71
|
+
* // Publish audit events with additional custom events
|
|
72
|
+
* const results = await eventBus.upserted(audits, [
|
|
73
|
+
* { DetailType: 'CustomEvent', Detail: '{}', Source: 'custom' },
|
|
74
|
+
* ]);
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
async upserted(items, extraEvents) {
|
|
78
|
+
const events = items.map((audit) => ({
|
|
79
|
+
EventBusName: EventBridge.Bus.Name(),
|
|
80
|
+
Detail: this.createDetail(audit),
|
|
81
|
+
DetailType: EventBridge.DetailType.UPSERTED,
|
|
82
|
+
Source: EventBridge.Source,
|
|
83
|
+
}));
|
|
84
|
+
return await this.handler.putEvents([...events, ...(extraEvents || [])]);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/events/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,UAAU,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from "./audits.js";
|
|
2
|
+
export * from "./config.js";
|
|
3
|
+
export { type AnyEventBridgeDetailType, AUDIT_LOG_IDENTIFIER, DynamoDB, EventBridge, SDK_VERSION, } from "./constants.js";
|
|
4
|
+
export * from "./events/index.js";
|
|
5
|
+
export * from "./schema/index.js";
|
|
6
|
+
export * from "./service.js";
|
|
7
|
+
export * from "./types.js";
|
|
8
|
+
export { buildAudit, buildAuditFromSQSRecord, getReceiveCount, getRecordId, isRetry, normalizeEventBridgetInput, } from "./utils.js";
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,OAAO,EACN,KAAK,wBAAwB,EAC7B,oBAAoB,EACpB,QAAQ,EACR,WAAW,EACX,WAAW,GACX,MAAM,gBAAgB,CAAC;AACxB,cAAc,mBAAmB,CAAC;AAClC,cAAc,mBAAmB,CAAC;AAClC,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,OAAO,EACN,UAAU,EACV,uBAAuB,EACvB,eAAe,EACf,WAAW,EACX,OAAO,EACP,0BAA0B,GAC1B,MAAM,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from "./audits.js";
|
|
2
|
+
export * from "./config.js";
|
|
3
|
+
export { AUDIT_LOG_IDENTIFIER, DynamoDB, EventBridge, SDK_VERSION, } from "./constants.js";
|
|
4
|
+
export * from "./events/index.js";
|
|
5
|
+
export * from "./schema/index.js";
|
|
6
|
+
export * from "./service.js";
|
|
7
|
+
export * from "./types.js";
|
|
8
|
+
export { buildAudit, buildAuditFromSQSRecord, getReceiveCount, getRecordId, isRetry, normalizeEventBridgetInput, } from "./utils.js";
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { MiddlewareLikeObj } from "@aws-lambda-powertools/commons/types";
|
|
2
|
+
import type { Audits } from "../audits.js";
|
|
3
|
+
import type { AuditConfig } from "../config.js";
|
|
4
|
+
/**
|
|
5
|
+
* Type alias for any Audits instance regardless of config type.
|
|
6
|
+
*/
|
|
7
|
+
type AnyAudits = Audits<AuditConfig>;
|
|
8
|
+
/**
|
|
9
|
+
* Creates a Middy.js middleware for automatic audit log management.
|
|
10
|
+
*
|
|
11
|
+
* This middleware integrates with the AWS Lambda Powertools pattern and is
|
|
12
|
+
* compatible with `@middy/core@3.x` and above. It automates the lifecycle
|
|
13
|
+
* of audit log collection and emission.
|
|
14
|
+
*
|
|
15
|
+
* **Behavior:**
|
|
16
|
+
* - **Before hook**: Registers a cleanup function on `request.internal` that
|
|
17
|
+
* can be invoked by other middlewares for early termination scenarios
|
|
18
|
+
* - **After hook**: Logs the Lambda event and publishes all buffered audits
|
|
19
|
+
* - **OnError hook**: Same as after hook - ensures audits are published even
|
|
20
|
+
* when the handler throws an error
|
|
21
|
+
*
|
|
22
|
+
* The cleanup function is stored at `audits` in `request.internal`,
|
|
23
|
+
* following the Powertools convention for middleware coordination.
|
|
24
|
+
*
|
|
25
|
+
* @param handler - The Audits instance to use for audit management
|
|
26
|
+
* @returns A Middy-compatible middleware object with before, after, and onError hooks
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* import middy from '@middy/core';
|
|
31
|
+
* import { Audits, defineAuditConfig } from '@flipboxlabs/aws-audit-sdk';
|
|
32
|
+
* import { logAudits } from '@flipboxlabs/aws-audit-sdk/middleware';
|
|
33
|
+
*
|
|
34
|
+
* const config = defineAuditConfig({
|
|
35
|
+
* apps: ['Orders'] as const,
|
|
36
|
+
* resourceTypes: ['Order'] as const,
|
|
37
|
+
* });
|
|
38
|
+
*
|
|
39
|
+
* const audits = new Audits({ config });
|
|
40
|
+
*
|
|
41
|
+
* const handler = middy(async (event, context) => {
|
|
42
|
+
* audits.addAudit({
|
|
43
|
+
* operation: 'processOrder',
|
|
44
|
+
* target: { app: 'Orders', type: 'Order', id: event.orderId },
|
|
45
|
+
* status: 'success',
|
|
46
|
+
* });
|
|
47
|
+
*
|
|
48
|
+
* return { statusCode: 200 };
|
|
49
|
+
* }).use(logAudits(audits));
|
|
50
|
+
*
|
|
51
|
+
* export { handler };
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```typescript
|
|
56
|
+
* // With multiple middlewares - cleanup function ensures audits
|
|
57
|
+
* // are published even if another middleware returns early
|
|
58
|
+
* const handler = middy(lambdaHandler)
|
|
59
|
+
* .use(logAudits(audits))
|
|
60
|
+
* .use(httpErrorHandler())
|
|
61
|
+
* .use(cors());
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
declare const logAudits: (handler: AnyAudits) => MiddlewareLikeObj;
|
|
65
|
+
export { logAudits };
|
|
66
|
+
//# sourceMappingURL=middy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middy.d.ts","sourceRoot":"","sources":["../../src/middleware/middy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,iBAAiB,EAEjB,MAAM,sCAAsC,CAAC;AAC9C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAEhD;;GAEG;AACH,KAAK,SAAS,GAAG,MAAM,CAAC,WAAW,CAAC,CAAC;AAErC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuDG;AACH,QAAA,MAAM,SAAS,GAAI,SAAS,SAAS,KAAG,iBAoDvC,CAAC;AAEF,OAAO,EAAE,SAAS,EAAE,CAAC"}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a Middy.js middleware for automatic audit log management.
|
|
3
|
+
*
|
|
4
|
+
* This middleware integrates with the AWS Lambda Powertools pattern and is
|
|
5
|
+
* compatible with `@middy/core@3.x` and above. It automates the lifecycle
|
|
6
|
+
* of audit log collection and emission.
|
|
7
|
+
*
|
|
8
|
+
* **Behavior:**
|
|
9
|
+
* - **Before hook**: Registers a cleanup function on `request.internal` that
|
|
10
|
+
* can be invoked by other middlewares for early termination scenarios
|
|
11
|
+
* - **After hook**: Logs the Lambda event and publishes all buffered audits
|
|
12
|
+
* - **OnError hook**: Same as after hook - ensures audits are published even
|
|
13
|
+
* when the handler throws an error
|
|
14
|
+
*
|
|
15
|
+
* The cleanup function is stored at `audits` in `request.internal`,
|
|
16
|
+
* following the Powertools convention for middleware coordination.
|
|
17
|
+
*
|
|
18
|
+
* @param handler - The Audits instance to use for audit management
|
|
19
|
+
* @returns A Middy-compatible middleware object with before, after, and onError hooks
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* import middy from '@middy/core';
|
|
24
|
+
* import { Audits, defineAuditConfig } from '@flipboxlabs/aws-audit-sdk';
|
|
25
|
+
* import { logAudits } from '@flipboxlabs/aws-audit-sdk/middleware';
|
|
26
|
+
*
|
|
27
|
+
* const config = defineAuditConfig({
|
|
28
|
+
* apps: ['Orders'] as const,
|
|
29
|
+
* resourceTypes: ['Order'] as const,
|
|
30
|
+
* });
|
|
31
|
+
*
|
|
32
|
+
* const audits = new Audits({ config });
|
|
33
|
+
*
|
|
34
|
+
* const handler = middy(async (event, context) => {
|
|
35
|
+
* audits.addAudit({
|
|
36
|
+
* operation: 'processOrder',
|
|
37
|
+
* target: { app: 'Orders', type: 'Order', id: event.orderId },
|
|
38
|
+
* status: 'success',
|
|
39
|
+
* });
|
|
40
|
+
*
|
|
41
|
+
* return { statusCode: 200 };
|
|
42
|
+
* }).use(logAudits(audits));
|
|
43
|
+
*
|
|
44
|
+
* export { handler };
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```typescript
|
|
49
|
+
* // With multiple middlewares - cleanup function ensures audits
|
|
50
|
+
* // are published even if another middleware returns early
|
|
51
|
+
* const handler = middy(lambdaHandler)
|
|
52
|
+
* .use(logAudits(audits))
|
|
53
|
+
* .use(httpErrorHandler())
|
|
54
|
+
* .use(cors());
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
const logAudits = (handler) => {
|
|
58
|
+
const instance = handler;
|
|
59
|
+
/**
|
|
60
|
+
* Registers the cleanup function on the request's internal state.
|
|
61
|
+
*
|
|
62
|
+
* This allows other middlewares to invoke the audit flush if they
|
|
63
|
+
* need to return early, following the Powertools middleware pattern.
|
|
64
|
+
*
|
|
65
|
+
* @param request - The Middy request object
|
|
66
|
+
* @internal
|
|
67
|
+
*/
|
|
68
|
+
const setCleanupFunction = (request) => {
|
|
69
|
+
request.internal = {
|
|
70
|
+
...request.internal,
|
|
71
|
+
["audits"]: afterOrError,
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* Before hook - runs before the Lambda handler.
|
|
76
|
+
*
|
|
77
|
+
* Registers the cleanup function for middleware coordination.
|
|
78
|
+
*
|
|
79
|
+
* @param request - The Middy request object
|
|
80
|
+
* @internal
|
|
81
|
+
*/
|
|
82
|
+
const before = async (request) => {
|
|
83
|
+
setCleanupFunction(request);
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* After/Error hook - runs after the Lambda handler completes or throws.
|
|
87
|
+
*
|
|
88
|
+
* Logs the original event for debugging and publishes all buffered
|
|
89
|
+
* audit entries. This ensures audits are always emitted regardless
|
|
90
|
+
* of whether the handler succeeded or failed.
|
|
91
|
+
*
|
|
92
|
+
* @param request - The Middy request object containing the event
|
|
93
|
+
* @internal
|
|
94
|
+
*/
|
|
95
|
+
const afterOrError = async (request) => {
|
|
96
|
+
instance.logger.info(request.event);
|
|
97
|
+
instance.publishStoredAudits();
|
|
98
|
+
};
|
|
99
|
+
return {
|
|
100
|
+
before,
|
|
101
|
+
after: afterOrError,
|
|
102
|
+
onError: afterOrError,
|
|
103
|
+
};
|
|
104
|
+
};
|
|
105
|
+
export { logAudits };
|