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

main
ts 267 lines 11.1 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Role } from "@alchemy.run/aws/IAM/Role";
3 import {
4 AwsLogSource,
5 CustomLogSource,
6 DataLake,
7 ExceptionSubscription,
8 Subscriber,
9 SubscriberNotification,
10 } from "@alchemy.run/aws/SecurityLake";
11 import { expect } from "@alchemy.run/test";
12 import * as Test from "@alchemy.run/test/Alchemy";
13 import * as securitylake from "@distilled.cloud/aws/securitylake";
14 import * as Effect from "effect/Effect";
15 import * as Result from "effect/Result";
16 import * as Schedule from "effect/Schedule";
17
18 const { test } = Test.make({ providers: AWS.providers() });
19
20 // ---------------------------------------------------------------------------
21 // Ungated typed-error probes: prove the distilled error union carries the tags
22 // the providers' read/delete paths depend on, at near-zero cost. Security Lake
23 // lifecycle itself is account-wide and heavy, so it is gated below.
24 // ---------------------------------------------------------------------------
25
26 test.provider(
27 "getSubscriber on a nonexistent subscriber fails with a typed tag",
28 () =>
29 Effect.gen(function* () {
30 const error = yield* Effect.flip(
31 securitylake.getSubscriber({
32 // valid UUID shape, never allocated
33 subscriberId: "00000000-0000-4000-8000-000000000000",
34 }),
35 );
36 // Onboarded accounts return ResourceNotFoundException; accounts that
37 // never enabled Security Lake reject every subscriber API with the
38 // (patched-in) UnauthorizedException wire error.
39 expect(["ResourceNotFoundException", "UnauthorizedException"]).toContain(error._tag);
40 }),
41 { tags: ["provider:aws", "provider:aws:securitylake", "live"] },
42 );
43
44 test.provider(
45 "listDataLakes returns data lakes or a typed not-onboarded rejection",
46 () =>
47 Effect.gen(function* () {
48 const result = yield* Effect.result(securitylake.listDataLakes({}));
49 if (Result.isSuccess(result)) {
50 expect(Array.isArray(result.success.dataLakes ?? [])).toBe(true);
51 } else {
52 // Accounts that never onboarded Security Lake reject listDataLakes
53 // with AccessDeniedException.
54 expect(result.failure._tag).toBe("AccessDeniedException");
55 }
56 }),
57 { tags: ["provider:aws", "provider:aws:securitylake", "live"] },
58 );
59
60 test.provider(
61 "getDataLakeExceptionSubscription returns the subscription or a typed rejection",
62 () =>
63 Effect.gen(function* () {
64 const result = yield* Effect.result(securitylake.getDataLakeExceptionSubscription({}));
65 if (Result.isSuccess(result)) {
66 // No subscription configured — every field is absent.
67 expect(typeof (result.success.notificationEndpoint ?? "")).toBe("string");
68 } else {
69 // Accounts that never onboarded Security Lake (or have no
70 // subscription) reject with one of the typed tags the
71 // ExceptionSubscription provider's read/delete paths depend on.
72 expect([
73 "AccessDeniedException",
74 "ResourceNotFoundException",
75 "UnauthorizedException",
76 ]).toContain(result.failure._tag);
77 }
78 }),
79 { tags: ["provider:aws", "provider:aws:securitylake", "live"] },
80 );
81
82 test.provider(
83 "deleteSubscriberNotification on a nonexistent subscriber fails with a typed tag",
84 () =>
85 Effect.gen(function* () {
86 const error = yield* Effect.flip(
87 securitylake.deleteSubscriberNotification({
88 subscriberId: "00000000-0000-4000-8000-000000000000",
89 }),
90 );
91 expect([
92 "AccessDeniedException",
93 "ResourceNotFoundException",
94 "UnauthorizedException",
95 ]).toContain(error._tag);
96 }),
97 { tags: ["provider:aws", "provider:aws:securitylake", "live"] },
98 );
99
100 // ---------------------------------------------------------------------------
101 // Gated live lifecycle. Enabling Security Lake onboards the whole account in
102 // the Region: it creates S3 buckets, registers them with Lake Formation, and
103 // configures the Glue metastore. The buckets are retained after delete (AWS
104 // behavior). Run with AWS_TEST_SECURITYLAKE=1 on an account you are willing to
105 // onboard/offboard.
106 // ---------------------------------------------------------------------------
107
108 const assertDataLakeGone = securitylake.listDataLakes({}).pipe(
109 Effect.flatMap((response) =>
110 (response.dataLakes ?? []).length === 0
111 ? Effect.void
112 : Effect.fail(new Error("data lake still present")),
113 ),
114 Effect.retry({ schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(12)]) }),
115 );
116
117 test.provider.skipIf(!process.env.AWS_TEST_SECURITYLAKE)(
118 "lifecycle: enable data lake, add ROUTE53 source + subscriber, update, destroy",
119 (stack) =>
120 Effect.gen(function* () {
121 const { accountId, region } = yield* AWS.AWSEnvironment.current;
122
123 // Never stomp an existing Security Lake deployment.
124 const preexisting = yield* securitylake.listDataLakes({});
125 if ((preexisting.dataLakes ?? []).length > 0) {
126 yield* Effect.logInfo(
127 "Security Lake already enabled in this account — skipping destructive lifecycle test",
128 );
129 return;
130 }
131
132 yield* stack.destroy();
133
134 const deployOnce = (subscriberDescription: string, withExtras: boolean) =>
135 stack.deploy(
136 Effect.gen(function* () {
137 const metastoreRole = yield* Role("MetastoreRole", {
138 assumeRolePolicyDocument: {
139 Version: "2012-10-17",
140 Statement: [
141 {
142 Effect: "Allow",
143 Principal: { Service: "lambda.amazonaws.com" },
144 Action: ["sts:AssumeRole"],
145 },
146 ],
147 },
148 managedPolicyArns: [
149 "arn:aws:iam::aws:policy/service-role/AmazonSecurityLakeMetastoreManager",
150 ],
151 });
152 const lake = yield* DataLake("Lake", {
153 configurations: [
154 { region, lifecycleConfiguration: { expiration: { days: "30 days" } } },
155 ],
156 metaStoreManagerRoleArn: metastoreRole.roleArn,
157 tags: { fixture: "securitylake" },
158 });
159 const source = yield* AwsLogSource("Route53Source", {
160 sourceName: "ROUTE53",
161 regions: lake.regions,
162 });
163 const subscriber = yield* Subscriber("Consumer", {
164 subscriberIdentity: { principal: accountId, externalId: "alchemy-securitylake-test" },
165 subscriberDescription,
166 // Reference the log source's output so destroy ordering tears
167 // the subscriber down before the source and data lake.
168 sources: [{ awsLogSource: { sourceName: source.sourceName } }],
169 accessTypes: ["S3"],
170 tags: { fixture: "securitylake" },
171 });
172 if (!withExtras) {
173 return { lake, source, subscriber, extras: undefined };
174 }
175
176 // Second wave (the data lake already exists by now): the
177 // exception subscription singleton, an SQS subscriber
178 // notification, and a custom log source with its crawler role.
179 const exceptions = yield* ExceptionSubscription("Exceptions", {
180 subscriptionProtocol: "email",
181 notificationEndpoint: "securitylake-test@example.com",
182 exceptionTimeToLive: "30 days",
183 });
184 const crawlerRole = yield* Role("CrawlerRole", {
185 assumeRolePolicyDocument: {
186 Version: "2012-10-17",
187 Statement: [
188 {
189 Effect: "Allow",
190 Principal: { Service: "glue.amazonaws.com" },
191 Action: ["sts:AssumeRole"],
192 },
193 ],
194 },
195 managedPolicyArns: ["arn:aws:iam::aws:policy/service-role/AWSGlueServiceRole"],
196 });
197 const custom = yield* CustomLogSource("CustomSource", {
198 sourceName: "alchemy-securitylake-test-custom",
199 eventClasses: ["FILE_ACTIVITY"],
200 crawlerConfiguration: { roleArn: crawlerRole.roleArn },
201 providerIdentity: { principal: accountId, externalId: "alchemy-securitylake-custom" },
202 });
203 const notification = yield* SubscriberNotification("Notify", {
204 subscriberId: subscriber.subscriberId,
205 sqs: true,
206 });
207 return { lake, source, subscriber, extras: { exceptions, custom, notification } };
208 }),
209 );
210
211 // Create.
212 const { lake, source, subscriber } = yield* deployOnce("v1", false);
213 expect(lake.dataLakeArn).toContain(":securitylake:");
214 expect(lake.regions).toContain(region);
215 expect(source.sourceName).toBe("ROUTE53");
216 expect(source.regions).toContain(region);
217 expect(subscriber.subscriberId).toBeDefined();
218 expect(subscriber.s3BucketArn).toBeDefined();
219
220 // Out-of-band verification via distilled.
221 const lakes = yield* securitylake.listDataLakes({});
222 expect(lakes.dataLakes?.some((l) => l.region === region)).toBe(true);
223 const observedSubscriber = yield* securitylake.getSubscriber({
224 subscriberId: subscriber.subscriberId,
225 });
226 expect(observedSubscriber.subscriber?.subscriberArn).toBe(subscriber.subscriberArn);
227
228 // Update in place (subscriber description) + add the second wave of
229 // resources (exception subscription, custom source, notification).
230 const second = yield* deployOnce("v2", true);
231 expect(second.subscriber.subscriberId).toBe(subscriber.subscriberId);
232 const updated = yield* securitylake.getSubscriber({ subscriberId: subscriber.subscriberId });
233 expect(updated.subscriber?.subscriberDescription).toBe("v2");
234
235 const extras = second.extras!;
236 expect(extras.exceptions.subscriptionProtocol).toBe("email");
237 expect(extras.exceptions.exceptionTimeToLive).toBe(30);
238 expect(extras.custom.sourceName).toBe("alchemy-securitylake-test-custom");
239 expect(extras.notification.subscriberEndpoint).toBeDefined();
240
241 // Out-of-band verification via distilled.
242 const exceptionSubscription = yield* securitylake.getDataLakeExceptionSubscription({});
243 expect(exceptionSubscription.notificationEndpoint).toBe("securitylake-test@example.com");
244 const logSources = yield* securitylake.listLogSources({});
245 expect(
246 (logSources.sources ?? []).some((entry) =>
247 (entry.sources ?? []).some(
248 (source) => source.customLogSource?.sourceName === "alchemy-securitylake-test-custom",
249 ),
250 ),
251 ).toBe(true);
252 const notified = yield* securitylake.getSubscriber({ subscriberId: subscriber.subscriberId });
253 expect(notified.subscriber?.subscriberEndpoint).toBeDefined();
254
255 // Destroy — offboards the account; buckets are retained by AWS design.
256 yield* stack.destroy();
257 const goneSubscriber = yield* Effect.flip(
258 securitylake.getSubscriber({ subscriberId: subscriber.subscriberId }),
259 );
260 expect(goneSubscriber._tag).toBe("ResourceNotFoundException");
261 yield* assertDataLakeGone;
262 }),
263 {
264 tags: ["provider:aws", "provider:aws:iam", "provider:aws:securitylake", "live"],
265 timeout: 1_200_000,
266 },
267 );