@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 374 lines 13.5 KB
Raw
1 import * as securitylake from "@distilled.cloud/aws/securitylake";
2 import { isResolved } from "alchemy/Diff";
3 import * as Provider from "alchemy/Provider";
4 import { Resource } from "alchemy/Resource";
5 import * as Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import * as Stream from "effect/Stream";
8 import type { Providers } from "../Providers.ts";
9
10 /**
11 * A natively supported AWS log source that Security Lake can collect.
12 */
13 export type AwsLogSourceName = securitylake.AwsLogSourceName;
14
15 export interface AwsLogSourceProps {
16 /**
17 * The AWS service to collect logs from (e.g. `ROUTE53`, `VPC_FLOW`,
18 * `CLOUD_TRAIL_MGMT`, `SH_FINDINGS`, `LAMBDA_EXECUTION`, `S3_DATA`,
19 * `EKS_AUDIT`, `WAF`). Changing this replaces the log source.
20 */
21 sourceName: AwsLogSourceName;
22
23 /**
24 * The version of the source schema to collect. Changing this replaces the
25 * log source.
26 * @default - the latest version supported by Security Lake
27 */
28 sourceVersion?: string;
29
30 /**
31 * The Regions to collect this source in. Security Lake must already be
32 * enabled (via `SecurityLake.DataLake`) in each Region.
33 */
34 regions: string[];
35
36 /**
37 * The AWS account IDs to collect this source from.
38 * @default - the current account
39 */
40 accounts?: string[];
41 }
42
43 /** @resource */
44 export interface AwsLogSource extends Resource<
45 "AWS.SecurityLake.AwsLogSource",
46 AwsLogSourceProps,
47 {
48 /** The AWS service the source collects logs from. */
49 sourceName: string;
50 /** The resolved source schema version. */
51 sourceVersion: string | undefined;
52 /** The Regions the source is enabled in. */
53 regions: string[];
54 /** The account IDs the source is collected from (undefined = current). */
55 accounts: string[] | undefined;
56 },
57 never,
58 Providers
59 > {}
60
61 /**
62 * A natively supported AWS log source (Route 53, VPC Flow Logs, CloudTrail,
63 * Security Hub findings, ...) enabled for collection into the Security Lake
64 * data lake. Requires `SecurityLake.DataLake` to already be enabled in every
65 * configured Region.
66 *
67 * ### Collecting AWS logs
68 * **Example:** Route 53 resolver query logs
69 * ```typescript
70 * const lake = yield* SecurityLake.DataLake("Lake", {
71 * configurations: [{ region: "us-west-2" }],
72 * metaStoreManagerRoleArn: metastoreRole.roleArn,
73 * });
74 * const route53 = yield* SecurityLake.AwsLogSource("Route53Logs", {
75 * sourceName: "ROUTE53",
76 * regions: lake.regions,
77 * });
78 * ```
79 *
80 * **Example:** VPC Flow Logs from specific accounts
81 * ```typescript
82 * const vpcFlow = yield* SecurityLake.AwsLogSource("VpcFlow", {
83 * sourceName: "VPC_FLOW",
84 * sourceVersion: "2.0",
85 * regions: ["us-west-2", "us-east-1"],
86 * accounts: ["123456789012"],
87 * });
88 * ```
89 */
90 const AwsLogSourceResource = Resource<AwsLogSource>("AWS.SecurityLake.AwsLogSource");
91
92 export { AwsLogSourceResource as AwsLogSource };
93
94 /**
95 * Security Lake reported per-account failures when enabling or disabling an
96 * AWS log source.
97 */
98 export class AwsLogSourceOperationFailed extends Data.TaggedError("AwsLogSourceOperationFailed")<{
99 readonly operation: "create" | "delete";
100 readonly sourceName: string;
101 readonly failedAccounts: string[];
102 }> {}
103
104 const failIfPartialFailure = (
105 operation: "create" | "delete",
106 sourceName: string,
107 failed: string[] | undefined,
108 ) =>
109 failed !== undefined && failed.length > 0
110 ? Effect.fail(
111 new AwsLogSourceOperationFailed({
112 operation,
113 sourceName,
114 failedAccounts: [...failed],
115 }),
116 )
117 : Effect.void;
118
119 export const AwsLogSourceProvider = () =>
120 Provider.effect(
121 AwsLogSourceResource,
122 Effect.gen(function* () {
123 // Observed state: the set of Regions in which this source (matched by
124 // name, and version when pinned) is currently collecting, plus the
125 // resolved versions AWS reports.
126 const observeSource = Effect.fn(function* (
127 sourceName: string,
128 sourceVersion: string | undefined,
129 ) {
130 const entries = yield* securitylake.listLogSources
131 .items({
132 sources: [
133 {
134 awsLogSource: {
135 sourceName,
136 ...(sourceVersion !== undefined ? { sourceVersion } : {}),
137 },
138 },
139 ],
140 })
141 .pipe(Stream.runCollect);
142 const regions = new Set<string>();
143 let resolvedVersion: string | undefined;
144 for (const entry of entries) {
145 const matched = (entry.sources ?? []).find(
146 (source) =>
147 source.awsLogSource?.sourceName === sourceName &&
148 (sourceVersion === undefined || source.awsLogSource.sourceVersion === sourceVersion),
149 );
150 if (matched && entry.region !== undefined) {
151 regions.add(entry.region);
152 resolvedVersion ??= matched.awsLogSource?.sourceVersion;
153 }
154 }
155 return { regions, resolvedVersion };
156 });
157
158 const sourceConfig = (
159 sourceName: string,
160 sourceVersion: string | undefined,
161 regions: string[],
162 accounts: string[] | undefined,
163 ): securitylake.AwsLogSourceConfiguration => ({
164 sourceName,
165 regions,
166 ...(sourceVersion !== undefined ? { sourceVersion } : {}),
167 ...(accounts !== undefined ? { accounts } : {}),
168 });
169
170 return {
171 read: Effect.fn(function* ({ olds, output }) {
172 const sourceName = output?.sourceName ?? olds?.sourceName;
173 if (sourceName === undefined) return undefined;
174 const sourceVersion = output?.sourceVersion ?? olds?.sourceVersion;
175 // An account that never onboarded Security Lake rejects
176 // listLogSources — that means "no log source", not a failure.
177 const observed = yield* observeSource(sourceName, sourceVersion).pipe(
178 Effect.catchTag(
179 ["AccessDeniedException", "ResourceNotFoundException", "UnauthorizedException"],
180 () =>
181 Effect.succeed({
182 regions: new Set<string>(),
183 resolvedVersion: undefined as string | undefined,
184 }),
185 ),
186 );
187 if (observed.regions.size === 0) return undefined;
188 // Log sources carry no tags, so ownership can't be distinguished —
189 // read reports observed state directly.
190 return {
191 sourceName,
192 sourceVersion: observed.resolvedVersion ?? sourceVersion,
193 regions: [...observed.regions].sort(),
194 accounts: output?.accounts ?? olds?.accounts,
195 };
196 }),
197
198 // Enumerate every enabled AWS log source, aggregated by
199 // (sourceName, sourceVersion) across Regions.
200 list: () =>
201 Effect.gen(function* () {
202 const entries = yield* securitylake.listLogSources.items({}).pipe(
203 Stream.runCollect,
204 // An account that never onboarded Security Lake has no data
205 // lake to list sources for.
206 Effect.catchTag(
207 ["AccessDeniedException", "ResourceNotFoundException", "UnauthorizedException"],
208 () => Effect.succeed([] as securitylake.LogSource[]),
209 ),
210 );
211 const grouped = new Map<
212 string,
213 {
214 sourceName: string;
215 sourceVersion: string | undefined;
216 regions: Set<string>;
217 }
218 >();
219 for (const entry of entries) {
220 for (const source of entry.sources ?? []) {
221 const aws = source.awsLogSource;
222 if (aws?.sourceName === undefined || entry.region === undefined) continue;
223 const key = `${aws.sourceName}@${aws.sourceVersion ?? ""}`;
224 const group = grouped.get(key) ?? {
225 sourceName: aws.sourceName,
226 sourceVersion: aws.sourceVersion,
227 regions: new Set<string>(),
228 };
229 group.regions.add(entry.region);
230 grouped.set(key, group);
231 }
232 }
233 return [...grouped.values()].map((group) => ({
234 sourceName: group.sourceName,
235 sourceVersion: group.sourceVersion,
236 regions: [...group.regions].sort(),
237 accounts: undefined,
238 }));
239 }),
240
241 // The source identity (name + pinned version) is create-only — a
242 // change tears down collection for the old source and enables the new.
243 diff: Effect.fn(function* ({ news, olds }) {
244 if (!isResolved(news)) return undefined;
245 if (news.sourceName !== olds.sourceName || news.sourceVersion !== olds.sourceVersion) {
246 return { action: "replace" } as const;
247 }
248 }),
249
250 reconcile: Effect.fn(function* ({ news, output, session }) {
251 const desiredRegions = news.regions;
252 const previousAccounts = output?.accounts;
253
254 // 1. OBSERVE — which Regions already collect this source.
255 const observed = yield* observeSource(news.sourceName, news.sourceVersion);
256
257 // 2. ENSURE — enable collection in Regions where it is missing.
258 const missingRegions = desiredRegions.filter((region) => !observed.regions.has(region));
259 if (missingRegions.length > 0) {
260 const response = yield* securitylake.createAwsLogSource({
261 sources: [
262 sourceConfig(news.sourceName, news.sourceVersion, missingRegions, news.accounts),
263 ],
264 });
265 yield* failIfPartialFailure(
266 "create",
267 news.sourceName,
268 response.failed ? [...response.failed] : undefined,
269 );
270 }
271
272 // 3. SYNC accounts — enable for newly added accounts everywhere,
273 // disable for accounts no longer desired.
274 const addedAccounts = (news.accounts ?? []).filter(
275 (account) => !(previousAccounts ?? []).includes(account),
276 );
277 const alreadyEnabledRegions = desiredRegions.filter((region) =>
278 observed.regions.has(region),
279 );
280 if (
281 addedAccounts.length > 0 &&
282 previousAccounts !== undefined &&
283 alreadyEnabledRegions.length > 0
284 ) {
285 const response = yield* securitylake.createAwsLogSource({
286 sources: [
287 sourceConfig(
288 news.sourceName,
289 news.sourceVersion,
290 alreadyEnabledRegions,
291 addedAccounts,
292 ),
293 ],
294 });
295 yield* failIfPartialFailure(
296 "create",
297 news.sourceName,
298 response.failed ? [...response.failed] : undefined,
299 );
300 }
301 const removedAccounts = (previousAccounts ?? []).filter(
302 (account) => news.accounts !== undefined && !news.accounts.includes(account),
303 );
304 if (removedAccounts.length > 0 && desiredRegions.length > 0) {
305 const response = yield* securitylake.deleteAwsLogSource({
306 sources: [
307 sourceConfig(news.sourceName, news.sourceVersion, desiredRegions, removedAccounts),
308 ],
309 });
310 yield* failIfPartialFailure(
311 "delete",
312 news.sourceName,
313 response.failed ? [...response.failed] : undefined,
314 );
315 }
316
317 // 3b. SYNC Region removals — only Regions this resource previously
318 // managed (recorded in output) are disabled.
319 const staleRegions = (output?.regions ?? []).filter(
320 (managed) => !desiredRegions.includes(managed) && observed.regions.has(managed),
321 );
322 if (staleRegions.length > 0) {
323 yield* securitylake.deleteAwsLogSource({
324 sources: [
325 sourceConfig(
326 news.sourceName,
327 news.sourceVersion,
328 staleRegions,
329 previousAccounts ?? news.accounts,
330 ),
331 ],
332 });
333 }
334
335 // 4. RETURN fresh attributes.
336 const final = yield* observeSource(news.sourceName, news.sourceVersion);
337 yield* session.note(news.sourceName);
338 return {
339 sourceName: news.sourceName,
340 sourceVersion: final.resolvedVersion ?? news.sourceVersion,
341 regions:
342 final.regions.size > 0 ? [...final.regions].sort() : [...desiredRegions].sort(),
343 accounts: news.accounts,
344 };
345 }),
346
347 delete: Effect.fn(function* ({ output }) {
348 if (output.regions.length === 0) return;
349 yield* securitylake
350 .deleteAwsLogSource({
351 sources: [
352 {
353 sourceName: output.sourceName,
354 regions: output.regions,
355 ...(output.sourceVersion !== undefined
356 ? { sourceVersion: output.sourceVersion }
357 : {}),
358 ...(output.accounts !== undefined ? { accounts: output.accounts } : {}),
359 },
360 ],
361 })
362 .pipe(
363 // Gone already, or the data lake itself was offboarded first
364 // (which stops all collection and makes log-source APIs reject
365 // with UnauthorizedException/AccessDeniedException).
366 Effect.catchTag(
367 ["AccessDeniedException", "ResourceNotFoundException", "UnauthorizedException"],
368 () => Effect.void,
369 ),
370 );
371 }),
372 };
373 }),
374 );