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

main
ts 171 lines 6.31 KB
Raw
1 import * as securityhub from "@distilled.cloud/aws/securityhub";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import * as Provider from "alchemy/Provider";
4 import { Resource } from "alchemy/Resource";
5 import * as Effect from "effect/Effect";
6 import type { Providers } from "../Providers.ts";
7
8 /**
9 * How linked Regions are selected for cross-Region finding aggregation.
10 */
11 export type RegionLinkingMode =
12 | "ALL_REGIONS"
13 | "ALL_REGIONS_EXCEPT_SPECIFIED"
14 | "SPECIFIED_REGIONS"
15 | "NO_REGIONS";
16
17 export interface FindingAggregatorProps {
18 /**
19 * How linked Regions are selected: `ALL_REGIONS` aggregates from every
20 * Region (including future ones), `ALL_REGIONS_EXCEPT_SPECIFIED` excludes
21 * `regions`, `SPECIFIED_REGIONS` includes only `regions`, and `NO_REGIONS`
22 * disables aggregation. Updatable in place.
23 */
24 regionLinkingMode: RegionLinkingMode;
25
26 /**
27 * The Regions excluded from (`ALL_REGIONS_EXCEPT_SPECIFIED`) or included
28 * in (`SPECIFIED_REGIONS`) aggregation. Updatable in place.
29 */
30 regions?: string[];
31 }
32
33 /** @resource */
34 export interface FindingAggregator extends Resource<
35 "AWS.SecurityHub.FindingAggregator",
36 FindingAggregatorProps,
37 {
38 /** ARN of the finding aggregator (its identity). */
39 findingAggregatorArn: string;
40 /** The home Region findings are aggregated into. */
41 findingAggregationRegion: string | undefined;
42 /** The active Region-linking mode. */
43 regionLinkingMode: string | undefined;
44 /** The Regions included in or excluded from aggregation. */
45 regions: string[] | undefined;
46 },
47 never,
48 Providers
49 > {}
50
51 /**
52 * The Security Hub cross-Region finding aggregator — replicates findings
53 * from linked Regions into the home Region. Only one aggregator can exist
54 * per account, so this is a singleton: adopting a pre-existing aggregator
55 * that Alchemy did not create requires `--adopt`.
56 *
57 * ### Aggregating Findings Across Regions
58 * **Example:** Aggregate from All Regions
59 * ```typescript
60 * const aggregator = yield* AWS.SecurityHub.FindingAggregator("Aggregator", {
61 * regionLinkingMode: "ALL_REGIONS",
62 * });
63 * ```
64 *
65 * **Example:** Aggregate from Specific Regions
66 * ```typescript
67 * const aggregator = yield* AWS.SecurityHub.FindingAggregator("Aggregator", {
68 * regionLinkingMode: "SPECIFIED_REGIONS",
69 * regions: ["us-east-1", "eu-west-1"],
70 * });
71 * ```
72 */
73 const FindingAggregatorResource = Resource<FindingAggregator>("AWS.SecurityHub.FindingAggregator");
74
75 export { FindingAggregatorResource as FindingAggregator };
76
77 export const FindingAggregatorProvider = () =>
78 Provider.effect(
79 FindingAggregatorResource,
80 Effect.gen(function* () {
81 const getAggregator = (arn: string) =>
82 securityhub.getFindingAggregator({ FindingAggregatorArn: arn }).pipe(
83 Effect.map((r) => r as securityhub.GetFindingAggregatorResponse | undefined),
84 Effect.catchTags({
85 ResourceNotFoundException: () => Effect.undefined,
86 InvalidAccessException: () => Effect.undefined,
87 }),
88 );
89
90 // At most one aggregator exists per account — a single page suffices.
91 const findAggregator = securityhub.listFindingAggregators({}).pipe(
92 Effect.map((r) => r.FindingAggregators?.[0]?.FindingAggregatorArn),
93 Effect.catchTag("InvalidAccessException", () => Effect.undefined),
94 );
95
96 const buildAttrs = (r: securityhub.GetFindingAggregatorResponse) => ({
97 findingAggregatorArn: r.FindingAggregatorArn!,
98 findingAggregationRegion: r.FindingAggregationRegion,
99 regionLinkingMode: r.RegionLinkingMode,
100 regions: r.Regions,
101 });
102
103 return {
104 stables: ["findingAggregatorArn", "findingAggregationRegion"],
105 read: Effect.fn(function* ({ output }) {
106 if (output?.findingAggregatorArn) {
107 const live = yield* getAggregator(output.findingAggregatorArn);
108 return live ? buildAttrs(live) : undefined;
109 }
110 // Aggregators cannot be tagged; an existing one we have no state
111 // for is foreign until adopted.
112 const arn = yield* findAggregator;
113 if (!arn) return undefined;
114 const live = yield* getAggregator(arn);
115 return live ? Unowned(buildAttrs(live)) : undefined;
116 }),
117 list: () =>
118 Effect.gen(function* () {
119 const arn = yield* findAggregator;
120 if (!arn) return [];
121 const live = yield* getAggregator(arn);
122 return live ? [buildAttrs(live)] : [];
123 }),
124 reconcile: Effect.fn(function* ({ news, output, session }) {
125 // 1. OBSERVE — the account's single aggregator is authoritative.
126 const arn = output?.findingAggregatorArn ?? (yield* findAggregator);
127 const live = arn ? yield* getAggregator(arn) : undefined;
128
129 let final: securityhub.GetFindingAggregatorResponse;
130 if (!live) {
131 // 2. ENSURE.
132 final = yield* securityhub.createFindingAggregator({
133 RegionLinkingMode: news.regionLinkingMode,
134 Regions: news.regions,
135 });
136 } else if (
137 live.RegionLinkingMode !== news.regionLinkingMode ||
138 // The API returns Regions in normalized order — compare as sets.
139 JSON.stringify([...(live.Regions ?? [])].sort()) !==
140 JSON.stringify([...(news.regions ?? [])].sort())
141 ) {
142 // 3. SYNC — observed ↔ desired.
143 final = yield* securityhub.updateFindingAggregator({
144 FindingAggregatorArn: live.FindingAggregatorArn!,
145 RegionLinkingMode: news.regionLinkingMode,
146 Regions: news.regions,
147 });
148 } else {
149 final = live;
150 }
151
152 // 4. RETURN fresh attributes.
153 yield* session.note(final.FindingAggregatorArn!);
154 return buildAttrs(final);
155 }),
156 delete: Effect.fn(function* ({ output }) {
157 // Idempotent — the aggregator (or the whole hub) may already be gone.
158 yield* securityhub
159 .deleteFindingAggregator({
160 FindingAggregatorArn: output.findingAggregatorArn,
161 })
162 .pipe(
163 Effect.catchTags({
164 ResourceNotFoundException: () => Effect.void,
165 InvalidAccessException: () => Effect.void,
166 }),
167 );
168 }),
169 };
170 }),
171 );