@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 6.03 KB
Raw
1 import * as securitylake from "@distilled.cloud/aws/securitylake";
2 import * as Provider from "alchemy/Provider";
3 import { Resource } from "alchemy/Resource";
4 import { toWireDays } from "alchemy/Util/Duration";
5 import type * as Duration from "effect/Duration";
6 import * as Effect from "effect/Effect";
7 import type { Providers } from "../Providers.ts";
8 import { retryWhileConflict } from "./internal.ts";
9
10 export interface ExceptionSubscriptionProps {
11 /**
12 * The protocol used to notify the subscription endpoint, e.g. `email`,
13 * `sqs`, or `https`.
14 */
15 subscriptionProtocol: string;
16
17 /**
18 * The endpoint Security Lake delivers exception notifications to (an email
19 * address, SQS queue ARN, or HTTPS endpoint, matching the protocol).
20 */
21 notificationEndpoint: string;
22
23 /**
24 * How long unresolved exceptions are retained before expiring. Accepts any
25 * `Duration.Input` (e.g. `"30 days"`, `Duration.days(30)`; a bare number is
26 * milliseconds); the wire unit is whole days.
27 * @default - exceptions do not expire
28 */
29 exceptionTimeToLive?: Duration.Input;
30 }
31
32 /** @resource */
33 export interface ExceptionSubscription extends Resource<
34 "AWS.SecurityLake.ExceptionSubscription",
35 ExceptionSubscriptionProps,
36 {
37 /** The protocol used to notify the subscription endpoint. */
38 subscriptionProtocol: string;
39 /** The endpoint that receives exception notifications. */
40 notificationEndpoint: string;
41 /** Retention of unresolved exceptions, in whole days. */
42 exceptionTimeToLive: number | undefined;
43 },
44 never,
45 Providers
46 > {}
47
48 /**
49 * The Amazon Security Lake exception notification subscription — an
50 * account-Region singleton that delivers notifications (via SNS protocols
51 * like email, SQS, or HTTPS) whenever Security Lake hits an exception it
52 * cannot resolve automatically.
53 *
54 * ### Subscribing to exceptions
55 * **Example:** Email notifications
56 * ```typescript
57 * const exceptions = yield* SecurityLake.ExceptionSubscription("Exceptions", {
58 * subscriptionProtocol: "email",
59 * notificationEndpoint: "security-team@example.com",
60 * });
61 * ```
62 *
63 * **Example:** SQS notifications with a 30-day exception TTL
64 * ```typescript
65 * const exceptions = yield* SecurityLake.ExceptionSubscription("Exceptions", {
66 * subscriptionProtocol: "sqs",
67 * notificationEndpoint: queue.queueArn,
68 * exceptionTimeToLive: "30 days",
69 * });
70 * ```
71 */
72 const ExceptionSubscriptionResource = Resource<ExceptionSubscription>(
73 "AWS.SecurityLake.ExceptionSubscription",
74 );
75
76 export { ExceptionSubscriptionResource as ExceptionSubscription };
77
78 // Observation for read/list: an account that never onboarded Security Lake
79 // (or has no subscription) yields "missing", not a failure.
80 const observeSubscription = securitylake.getDataLakeExceptionSubscription({}).pipe(
81 Effect.map((response) =>
82 response.notificationEndpoint !== undefined && response.subscriptionProtocol !== undefined
83 ? {
84 subscriptionProtocol: response.subscriptionProtocol,
85 notificationEndpoint: response.notificationEndpoint,
86 exceptionTimeToLive: response.exceptionTimeToLive,
87 }
88 : undefined,
89 ),
90 Effect.catchTag(
91 ["AccessDeniedException", "ResourceNotFoundException", "UnauthorizedException"],
92 () => Effect.undefined,
93 ),
94 );
95
96 export const ExceptionSubscriptionProvider = () =>
97 Provider.effect(
98 ExceptionSubscriptionResource,
99 Effect.gen(function* () {
100 return {
101 // The subscription carries no tags and is an account-Region
102 // singleton — read reports observed state directly.
103 read: Effect.fn(function* () {
104 return yield* observeSubscription;
105 }),
106
107 list: () =>
108 Effect.gen(function* () {
109 const observed = yield* observeSubscription;
110 return observed === undefined ? [] : [observed];
111 }),
112
113 reconcile: Effect.fn(function* ({ news, session }) {
114 const desired = {
115 subscriptionProtocol: news.subscriptionProtocol,
116 notificationEndpoint: news.notificationEndpoint,
117 exceptionTimeToLive: toWireDays(news.exceptionTimeToLive),
118 };
119
120 // 1. OBSERVE — the singleton's current state.
121 const observed = yield* observeSubscription;
122
123 // 2. ENSURE — create when missing; a ConflictException means a
124 // concurrent create won the race, so converge via update below.
125 if (observed === undefined) {
126 yield* securitylake
127 .createDataLakeExceptionSubscription(desired)
128 .pipe(
129 Effect.catchTag("ConflictException", () =>
130 securitylake
131 .updateDataLakeExceptionSubscription(desired)
132 .pipe(retryWhileConflict),
133 ),
134 );
135 } else if (
136 observed.subscriptionProtocol !== desired.subscriptionProtocol ||
137 observed.notificationEndpoint !== desired.notificationEndpoint ||
138 observed.exceptionTimeToLive !== desired.exceptionTimeToLive
139 ) {
140 // 3. SYNC — apply only when observed differs from desired.
141 yield* securitylake
142 .updateDataLakeExceptionSubscription(desired)
143 .pipe(retryWhileConflict);
144 }
145
146 // 4. RETURN fresh attributes.
147 const final = (yield* observeSubscription) ?? {
148 subscriptionProtocol: desired.subscriptionProtocol,
149 notificationEndpoint: desired.notificationEndpoint,
150 exceptionTimeToLive: desired.exceptionTimeToLive,
151 };
152 yield* session.note(`${final.subscriptionProtocol}:${final.notificationEndpoint}`);
153 return final;
154 }),
155
156 delete: Effect.fn(function* () {
157 yield* securitylake.deleteDataLakeExceptionSubscription({}).pipe(
158 retryWhileConflict,
159 // Gone already, or the account was never onboarded.
160 Effect.catchTag(
161 ["AccessDeniedException", "ResourceNotFoundException", "UnauthorizedException"],
162 () => Effect.void,
163 ),
164 );
165 }),
166 };
167 }),
168 );