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

main
ts 109 lines 4.08 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 Private CA delivers to EventBridge. Success
11 * events (certificate issued, certificate revoked, CRL generated, audit
12 * report created) carry no detail — the CA ARN and the certificate/report
13 * ARN ride in the event's top-level `resources` array. Failure events
14 * (e.g. a CRL that could not be written to S3) describe the failure here.
15 */
16 export interface CertificateAuthorityEventDetail {
17 /** Failure events: `"failure"`. Absent on success events. */
18 result?: string;
19 /** Additional event fields (the schema grows over time). */
20 [key: string]: unknown;
21 }
22
23 /** An AWS Private CA EventBridge event delivered to the handler. */
24 export type CertificateAuthorityEvent = EventRecord<CertificateAuthorityEventDetail>;
25
26 /** Which AWS Private CA notifications to subscribe to. */
27 export type CertificateAuthorityEventKind = "issuance" | "revocation" | "crl" | "auditReport";
28
29 const DETAIL_TYPES: Record<CertificateAuthorityEventKind, string> = {
30 issuance: "ACM Private CA Certificate Issuance",
31 revocation: "ACM Private CA Certificate Revocation",
32 crl: "ACM Private CA CRL Generation",
33 auditReport: "ACM Private CA Audit Report Generation",
34 };
35
36 export interface CertificateAuthorityEventSourceProps extends EventRouteProps {
37 /**
38 * Logical id for the backing EventBridge rule.
39 * @default "ACMPCAEvents"
40 */
41 id?: string;
42 /**
43 * Which notifications to subscribe to: certificate issuance, certificate
44 * revocation, CRL generation, audit report generation, or any subset.
45 * @default ["issuance"]
46 */
47 kinds?: readonly CertificateAuthorityEventKind[];
48 /**
49 * Restrict to events about specific CAs (matched against the event's
50 * top-level `resources`, which always includes the CA's ARN).
51 */
52 certificateAuthorityArns?: readonly string[];
53 }
54
55 /**
56 * Event source connecting AWS Private CA notifications to the hosting
57 * compute. AWS Private CA publishes certificate issuance/revocation, CRL
58 * generation, and audit report completion to the account's default
59 * EventBridge bus (source `aws.acm-pca`); this subscribes the host Function
60 * to those events so it can distribute freshly issued certificates, alert
61 * on revocations, or track CRL/audit failures.
62 *
63 * AWS Private CA publishes to EventBridge automatically — no additional
64 * resource is created besides the EventBridge rule targeting the host.
65 * Provide the host-specific implementation layer (e.g.
66 * `AWS.Lambda.EventSource`) on the Function effect.
67 *
68 * Success events carry the CA ARN and the certificate (or audit report)
69 * ARN in the event's top-level `resources` array; `detail` is empty.
70 * Failure events set `detail.result` to `"failure"`.
71 *
72 * ### Consuming Certificate Authority Events
73 * **Example:** React To Issued Certificates
74 * ```typescript
75 * import * as AWS from "@alchemy.run/aws";
76 *
77 * export default CertFunction.make(
78 * { main: import.meta.url },
79 * Effect.gen(function* () {
80 * yield* AWS.ACMPCA.consumeCertificateAuthorityEvents(
81 * { kinds: ["issuance", "revocation"] },
82 * (events) =>
83 * Stream.runForEach(events, (event) =>
84 * Effect.log(`${event["detail-type"]}: ${event.resources[1]}`),
85 * ),
86 * );
87 * return {};
88 * }).pipe(Effect.provide(AWS.Lambda.EventSource)),
89 * );
90 * ```
91 */
92 export const consumeCertificateAuthorityEvents = <StreamReq = never, Req = never>(
93 props: CertificateAuthorityEventSourceProps,
94 process: (
95 events: Stream.Stream<CertificateAuthorityEvent, never, StreamReq>,
96 ) => Effect.Effect<void, never, Req>,
97 ) =>
98 consumeBusEvents(
99 props.id ?? "ACMPCAEvents",
100 {
101 source: ["aws.acm-pca"],
102 "detail-type": (props.kinds ?? (["issuance"] as const)).map((kind) => DETAIL_TYPES[kind]),
103 ...(props.certificateAuthorityArns !== undefined
104 ? { resources: [...props.certificateAuthorityArns] }
105 : {}),
106 },
107 { description: props.description, state: props.state },
108 process,
109 );