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

main
ts 168 lines 6.8 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { AllowList } from "@alchemy.run/aws/Macie2/AllowList";
3 import { CustomDataIdentifier } from "@alchemy.run/aws/Macie2/CustomDataIdentifier";
4 import { FindingsFilter } from "@alchemy.run/aws/Macie2/FindingsFilter";
5 import { Session } from "@alchemy.run/aws/Macie2/Session";
6 import { expect } from "@alchemy.run/test";
7 import * as Test from "@alchemy.run/test/Alchemy";
8 import * as macie2 from "@distilled.cloud/aws/macie2";
9 import * as Effect from "effect/Effect";
10 import { makeMacie2TestLease } from "./TestLease.ts";
11
12 const { test, beforeAll, afterAll } = Test.make({ providers: AWS.providers() });
13 const testLease = makeMacie2TestLease();
14
15 // Lease acquisition may queue behind the complete lifecycle of every other
16 // Macie file. This does not widen any cloud-operation polling budget.
17 beforeAll(testLease.acquire, { timeout: 3_600_000 });
18 afterAll(testLease.release);
19
20 // `getMacieSession` throws `AccessDeniedException` ("Macie is not enabled")
21 // when the account has no session.
22 const getSession = macie2.getMacieSession({}).pipe(
23 Effect.map((s) => s as macie2.GetMacieSessionResponse | undefined),
24 Effect.catchTag("AccessDeniedException", () => Effect.succeed(undefined)),
25 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined)),
26 );
27
28 // AllowList, CustomDataIdentifier, and FindingsFilter all require Macie to be
29 // enabled. To avoid touching a Macie session the user already operates
30 // (capture-and-restore safety), the test only runs when Macie is not already
31 // enabled — it enables Macie itself and disables it again on destroy.
32 test.provider(
33 "lifecycle: allow list + custom data identifier + findings filter",
34 (stack) =>
35 Effect.gen(function* () {
36 const preexisting = yield* getSession;
37 if (preexisting) {
38 yield* Effect.logInfo("Macie already enabled — skipping Macie2 resources lifecycle test");
39 return;
40 }
41
42 yield* stack.destroy();
43
44 // Phase 1 — enable Macie and create all three resources.
45 const created = yield* stack.deploy(
46 Effect.gen(function* () {
47 yield* Session("Macie", { status: "ENABLED" });
48 const allowList = yield* AllowList("Ignore", {
49 description: "internal ticket ids",
50 criteria: { regex: "TICKET-[0-9]{6}" },
51 tags: { env: "test" },
52 });
53 const identifier = yield* CustomDataIdentifier("EmployeeId", {
54 regex: "EMP-[0-9]{8}",
55 description: "internal employee id",
56 tags: { env: "test" },
57 });
58 const filter = yield* FindingsFilter("LowSeverity", {
59 action: "ARCHIVE",
60 position: 1,
61 findingCriteria: {
62 criterion: { "severity.description": { eq: ["Low"] } },
63 },
64 tags: { env: "test" },
65 });
66 return {
67 allowListId: allowList.id,
68 allowListArn: allowList.arn,
69 identifierId: identifier.id,
70 identifierArn: identifier.arn,
71 filterId: filter.id,
72 filterAction: filter.action,
73 };
74 }),
75 );
76 expect(created.allowListId).toBeTruthy();
77 expect(created.allowListArn).toContain(":allow-list/");
78 expect(created.identifierArn).toContain(":custom-data-identifier/");
79 expect(created.filterAction).toBe("ARCHIVE");
80
81 // Out-of-band verification (including Alchemy ownership tags).
82 const liveAllow = yield* macie2.getAllowList({
83 id: created.allowListId,
84 });
85 expect(liveAllow.criteria?.regex).toBe("TICKET-[0-9]{6}");
86 expect(liveAllow.tags?.["env"]).toBe("test");
87 expect(liveAllow.tags?.["alchemy::id"]).toBe("Ignore");
88
89 const liveIdentifier = yield* macie2.getCustomDataIdentifier({
90 id: created.identifierId,
91 });
92 expect(liveIdentifier.regex).toBe("EMP-[0-9]{8}");
93 expect(liveIdentifier.tags?.["alchemy::id"]).toBe("EmployeeId");
94
95 const liveFilter = yield* macie2.getFindingsFilter({
96 id: created.filterId,
97 });
98 expect(liveFilter.action).toBe("ARCHIVE");
99 expect(liveFilter.findingCriteria?.criterion?.["severity.description"]?.eq).toEqual(["Low"]);
100
101 // Phase 2 — in-place updates (allow list criteria + filter action/tags)
102 // and a replacement (custom data identifiers are immutable, so a regex
103 // change swaps the physical identifier).
104 const updated = yield* stack.deploy(
105 Effect.gen(function* () {
106 yield* Session("Macie", { status: "ENABLED" });
107 const allowList = yield* AllowList("Ignore", {
108 description: "internal ticket ids v2",
109 criteria: { regex: "TICKET-[0-9]{7}" },
110 tags: { env: "test" },
111 });
112 const identifier = yield* CustomDataIdentifier("EmployeeId", {
113 regex: "EMP-[0-9]{9}",
114 description: "internal employee id",
115 tags: { env: "test" },
116 });
117 const filter = yield* FindingsFilter("LowSeverity", {
118 action: "NOOP",
119 position: 1,
120 findingCriteria: {
121 criterion: { "severity.description": { eq: ["Low"] } },
122 },
123 tags: { env: "test", phase: "two" },
124 });
125 return {
126 allowListId: allowList.id,
127 identifierId: identifier.id,
128 filterId: filter.id,
129 };
130 }),
131 );
132
133 // Allow list + filter update in place; the identifier is replaced.
134 expect(updated.allowListId).toBe(created.allowListId);
135 expect(updated.filterId).toBe(created.filterId);
136 expect(updated.identifierId).not.toBe(created.identifierId);
137
138 const updatedAllow = yield* macie2.getAllowList({
139 id: updated.allowListId,
140 });
141 expect(updatedAllow.criteria?.regex).toBe("TICKET-[0-9]{7}");
142 expect(updatedAllow.description).toBe("internal ticket ids v2");
143
144 const updatedFilter = yield* macie2.getFindingsFilter({
145 id: updated.filterId,
146 });
147 expect(updatedFilter.action).toBe("NOOP");
148 expect(updatedFilter.tags?.["phase"]).toBe("two");
149
150 const updatedIdentifier = yield* macie2.getCustomDataIdentifier({
151 id: updated.identifierId,
152 });
153 expect(updatedIdentifier.regex).toBe("EMP-[0-9]{9}");
154 // The replaced identifier is soft-deleted.
155 const replaced = yield* macie2.getCustomDataIdentifier({
156 id: created.identifierId,
157 });
158 expect(replaced.deleted).toBe(true);
159
160 // Destroy — every resource is removed and Macie is disabled (which
161 // also proves delete-idempotence via the AccessDenied catch: the
162 // session delete may land before the sub-resource deletes re-check).
163 yield* stack.destroy();
164 const after = yield* getSession;
165 expect(after).toBeUndefined();
166 }),
167 { tags: ["provider:aws", "provider:aws:macie2", "live"], timeout: 300_000 },
168 );