The best way to control Claude Code, Codex and more from any computer or phone

main
ts 109 lines 3.82 KB
Raw
1 import type * as Effect from "effect/Effect";
2 import type * as Stream from "effect/Stream";
3 import {
4 consumeBusEvents,
5 type EventRecord,
6 type EventRouteProps,
7 } from "../EventBridge/EventSource.ts";
8
9 /**
10 * The `detail` payload AWS KMS delivers to EventBridge. Rotation and
11 * deletion events carry the affected key's id; fields not shared by every
12 * event kind are optional (the schema grows over time).
13 */
14 export interface KeyEventDetail {
15 /** The id of the KMS key the event is about. */
16 "key-id"?: string;
17 /** Deletion events: when the key material was destroyed. */
18 "deletion-time"?: string;
19 /** Rotation events: when the key material was rotated. */
20 "rotation-time"?: string;
21 /** Imported key material expiration events: when the material expired. */
22 "expiration-time"?: string;
23 /** Additional event fields (the schema grows over time). */
24 [key: string]: unknown;
25 }
26
27 /** A KMS EventBridge event delivered to the handler. */
28 export type KeyEvent = EventRecord<KeyEventDetail>;
29
30 /** Which KMS notifications to subscribe to. */
31 export type KeyEventKind = "rotation" | "deletion" | "imported-key-material-expiration";
32
33 const DETAIL_TYPES: Record<KeyEventKind, string> = {
34 rotation: "KMS CMK Rotation",
35 deletion: "KMS CMK Deletion",
36 "imported-key-material-expiration": "KMS Imported Key Material Expiration",
37 };
38
39 export interface KeyEventSourceProps extends EventRouteProps {
40 /**
41 * Logical id for the backing EventBridge rule.
42 * @default "KMSKeyEvents"
43 */
44 id?: string;
45 /**
46 * Which notifications to subscribe to: automatic key-material rotations,
47 * completed key deletions (the end of the pending-deletion window), and/or
48 * imported key material expirations.
49 * @default all three kinds
50 */
51 kinds?: readonly KeyEventKind[];
52 /**
53 * Restrict to events about specific KMS keys (matched against the event's
54 * top-level `resources`, which contains the key's ARN).
55 */
56 keyArns?: readonly string[];
57 }
58
59 /**
60 * Event source connecting AWS KMS notifications to the hosting compute. KMS
61 * publishes key-material rotations, completed key deletions (the moment the
62 * pending-deletion window elapses and the material is destroyed), and
63 * imported key material expirations to the account's default EventBridge bus
64 * (source `aws.kms`); this subscribes the host Function to those events so
65 * it can audit rotations or react before/when a key becomes unusable.
66 *
67 * KMS publishes to EventBridge automatically — no additional resource is
68 * created besides the EventBridge rule targeting the host. Provide the
69 * host-specific implementation layer (e.g. `AWS.Lambda.EventSource`) on the
70 * Function effect.
71 *
72 * ### Consuming Key Events
73 * **Example:** Audit Rotations and Deletions
74 * ```typescript
75 * import * as AWS from "@alchemy.run/aws";
76 *
77 * export default AuditFunction.make(
78 * { main: import.meta.url },
79 * Effect.gen(function* () {
80 * yield* AWS.KMS.consumeKeyEvents(
81 * { kinds: ["rotation", "deletion"] },
82 * (events) =>
83 * Stream.runForEach(events, (event) =>
84 * Effect.logInfo(
85 * `${event["detail-type"]}: ${event.detail["key-id"]}`,
86 * ),
87 * ),
88 * );
89 * return {};
90 * }).pipe(Effect.provide(AWS.Lambda.EventSource)),
91 * );
92 * ```
93 */
94 export const consumeKeyEvents = <StreamReq = never, Req = never>(
95 props: KeyEventSourceProps,
96 process: (events: Stream.Stream<KeyEvent, never, StreamReq>) => Effect.Effect<void, never, Req>,
97 ) =>
98 consumeBusEvents(
99 props.id ?? "KMSKeyEvents",
100 {
101 source: ["aws.kms"],
102 "detail-type": (
103 props.kinds ?? (["rotation", "deletion", "imported-key-material-expiration"] as const)
104 ).map((kind) => DETAIL_TYPES[kind]),
105 ...(props.keyArns !== undefined ? { resources: [...props.keyArns] } : {}),
106 },
107 { description: props.description, state: props.state },
108 process,
109 );