@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 168 lines 5.7 KB
Raw
1 import type * as securityhub from "@distilled.cloud/aws/securityhub";
2 import type * as Effect from "effect/Effect";
3 import type * as Stream from "effect/Stream";
4 import {
5 consumeBusEvents,
6 type EventRecord,
7 type EventRouteProps,
8 } from "../EventBridge/EventSource.ts";
9
10 /**
11 * The `detail` payload Security Hub delivers to EventBridge — a batch of
12 * findings in AWS Security Finding Format (ASFF).
13 */
14 export interface FindingEventDetail {
15 /** The findings the event carries (ASFF documents, up to 25 per event). */
16 findings?: securityhub.AwsSecurityFinding[];
17 /** Additional detail fields (the schema grows over time). */
18 [key: string]: unknown;
19 }
20
21 /** A Security Hub findings EventBridge event delivered to the handler. */
22 export type FindingEvent = EventRecord<FindingEventDetail>;
23
24 /**
25 * The `detail` payload of a `Security Hub Findings - Custom Action` event —
26 * the action that was invoked plus the findings it was invoked on.
27 */
28 export interface CustomActionEventDetail extends FindingEventDetail {
29 /** Name of the custom action that was invoked. */
30 actionName?: string;
31 /** Description of the custom action that was invoked. */
32 actionDescription?: string;
33 }
34
35 /** A Security Hub custom-action EventBridge event delivered to the handler. */
36 export type CustomActionEvent = EventRecord<CustomActionEventDetail>;
37
38 export interface FindingEventSourceProps extends EventRouteProps {
39 /**
40 * Logical id for the backing EventBridge rule.
41 * @default "SecurityHubFindings"
42 */
43 id?: string;
44 /**
45 * Restrict to findings with specific severity labels (matched against
46 * `detail.findings.Severity.Label`), e.g. `["HIGH", "CRITICAL"]`.
47 */
48 severityLabels?: readonly string[];
49 /**
50 * Restrict to findings with specific workflow statuses (matched against
51 * `detail.findings.Workflow.Status`), e.g. `["NEW"]`.
52 */
53 workflowStatuses?: readonly string[];
54 }
55
56 /**
57 * Event source connecting Security Hub findings to the hosting compute.
58 * Security Hub publishes every new finding and finding update to the
59 * account's default EventBridge bus (source `aws.securityhub`, detail-type
60 * `Security Hub Findings - Imported`); this subscribes the host Function to
61 * those events so it can triage, notify, or auto-remediate.
62 *
63 * Security Hub 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 * ### Consuming Findings
69 * **Example:** Alert on Critical Findings
70 * ```typescript
71 * import * as AWS from "@alchemy.run/aws";
72 *
73 * export default AlertFunction.make(
74 * { main: import.meta.url },
75 * Effect.gen(function* () {
76 * yield* AWS.SecurityHub.consumeFindings(
77 * { severityLabels: ["CRITICAL"] },
78 * (events) =>
79 * Stream.runForEach(events, (event) =>
80 * Effect.forEach(event.detail.findings ?? [], (finding) =>
81 * Effect.logError(`Security Hub: ${finding.Title}`),
82 * ),
83 * ),
84 * );
85 * return {};
86 * }).pipe(Effect.provide(AWS.Lambda.EventSource)),
87 * );
88 * ```
89 */
90 export const consumeFindings = <StreamReq = never, Req = never>(
91 props: FindingEventSourceProps,
92 process: (
93 events: Stream.Stream<FindingEvent, never, StreamReq>,
94 ) => Effect.Effect<void, never, Req>,
95 ) =>
96 consumeBusEvents(
97 props.id ?? "SecurityHubFindings",
98 {
99 source: ["aws.securityhub"],
100 "detail-type": ["Security Hub Findings - Imported"],
101 ...(props.severityLabels !== undefined || props.workflowStatuses !== undefined
102 ? {
103 detail: {
104 findings: {
105 ...(props.severityLabels !== undefined
106 ? { Severity: { Label: [...props.severityLabels] } }
107 : {}),
108 ...(props.workflowStatuses !== undefined
109 ? { Workflow: { Status: [...props.workflowStatuses] } }
110 : {}),
111 },
112 },
113 }
114 : {}),
115 },
116 { description: props.description, state: props.state },
117 process,
118 );
119
120 export interface CustomActionEventSourceProps extends EventRouteProps {
121 /**
122 * Logical id for the backing EventBridge rule.
123 * @default "SecurityHubCustomActions"
124 */
125 id?: string;
126 /**
127 * Restrict to specific custom actions (matched against the event's
128 * `resources`, which carry the action target ARN), e.g.
129 * `[actionTarget.actionTargetArn]`.
130 */
131 actionArns?: readonly string[];
132 }
133
134 /**
135 * Event source connecting Security Hub custom actions to the hosting
136 * compute. Selecting a custom action on findings or insights in the console
137 * publishes a `Security Hub Findings - Custom Action` event carrying the
138 * selected findings; this subscribes the host Function to those events.
139 * Define the action with {@link ActionTarget}.
140 *
141 * ### Consuming Custom Actions
142 * **Example:** Handle an Escalation Action
143 * ```typescript
144 * yield* AWS.SecurityHub.consumeCustomActions(
145 * { actionArns: [escalate.actionTargetArn] },
146 * (events) =>
147 * Stream.runForEach(events, (event) =>
148 * Effect.log(`${event.detail.actionName}:`, event.detail.findings),
149 * ),
150 * );
151 * ```
152 */
153 export const consumeCustomActions = <StreamReq = never, Req = never>(
154 props: CustomActionEventSourceProps,
155 process: (
156 events: Stream.Stream<CustomActionEvent, never, StreamReq>,
157 ) => Effect.Effect<void, never, Req>,
158 ) =>
159 consumeBusEvents(
160 props.id ?? "SecurityHubCustomActions",
161 {
162 source: ["aws.securityhub"],
163 "detail-type": ["Security Hub Findings - Custom Action"],
164 ...(props.actionArns !== undefined ? { resources: [...props.actionArns] } : {}),
165 },
166 { description: props.description, state: props.state },
167 process,
168 );