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

main
ts 309 lines 12.4 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { CisScanConfiguration } from "@alchemy.run/aws/Inspector2/CisScanConfiguration";
3 import { Enabler } from "@alchemy.run/aws/Inspector2/Enabler";
4 import { describe, expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as inspector2 from "@distilled.cloud/aws/inspector2";
7 import * as Provider from "alchemy/Provider";
8 import * as Data from "effect/Data";
9 import * as Effect from "effect/Effect";
10 import * as Result from "effect/Result";
11 import * as Schedule from "effect/Schedule";
12
13 const { test } = Test.make({ providers: AWS.providers() });
14
15 const accountStatus = inspector2.batchGetAccountStatus({}).pipe(Effect.map((r) => r.accounts?.[0]));
16
17 const typeStatus = (account: inspector2.AccountState | undefined, key: "ec2" | "ecr" | "lambda") =>
18 account?.resourceState?.[key]?.status;
19
20 class InspectorNotSettled extends Data.TaggedError("InspectorNotSettled")<{
21 readonly expected: string;
22 readonly status: string | undefined;
23 readonly ec2: string | undefined;
24 }> {}
25
26 class InspectorRequestRejected extends Data.TaggedError("InspectorRequestRejected")<{
27 readonly operation: "enable" | "disable";
28 readonly failedAccounts: readonly inspector2.FailedAccount[];
29 }> {}
30
31 // Enablement transitions are asynchronous (ENABLING/DISABLING). Poll with a
32 // bounded budget (~50s) and check the terminal value — Effect.repeat returns
33 // its last success when the budget runs out even if `until` never held.
34 const waitForAccount = (expected: string, done: (account: inspector2.AccountState) => boolean) =>
35 accountStatus.pipe(
36 Effect.repeat({
37 schedule: Schedule.spaced("5 seconds"),
38 until: (account) => account !== undefined && done(account),
39 times: 10,
40 }),
41 Effect.flatMap((account) =>
42 account !== undefined && done(account)
43 ? Effect.succeed(account)
44 : Effect.fail(
45 new InspectorNotSettled({
46 expected,
47 status: account?.state?.status,
48 ec2: typeStatus(account, "ec2"),
49 }),
50 ),
51 ),
52 );
53
54 // The scan type borrowed to satisfy the CIS APIs' "account is enabled" gate.
55 // CIS scans target EC2 instances, and any one enabled type flips the account
56 // status to ENABLED.
57 const CIS_SCAN_TYPE = "EC2";
58
59 // Disable exactly the scan type `withInspectorEnabled` turned on.
60 const restoreDisabled = (accountId: string) =>
61 Effect.gen(function* () {
62 // Disable is rejected with ENABLE_IN_PROGRESS until the enable settles.
63 yield* waitForAccount(
64 `${CIS_SCAN_TYPE} not ENABLING`,
65 (account) => typeStatus(account, "ec2") !== "ENABLING",
66 );
67 const response = yield* inspector2.disable({
68 accountIds: [accountId],
69 resourceTypes: [CIS_SCAN_TYPE],
70 });
71 if (response.failedAccounts && response.failedAccounts.length > 0) {
72 return yield* Effect.fail(
73 new InspectorRequestRejected({
74 operation: "disable",
75 failedAccounts: response.failedAccounts,
76 }),
77 );
78 }
79 // Live disable can stay DISABLING for minutes. An accepted disable that is
80 // still converging is not a leak; anything else is.
81 yield* waitForAccount("DISABLED", (account) => account.state?.status === "DISABLED").pipe(
82 Effect.catchTag("InspectorNotSettled", (e) =>
83 e.ec2 === "DISABLING"
84 ? Effect.logWarning(
85 `Inspector ${CIS_SCAN_TYPE} disable accepted but still DISABLING after the wait budget — converging asynchronously`,
86 )
87 : Effect.fail(e),
88 ),
89 );
90 });
91
92 // Capture-and-restore of the account/region enablement singleton for the
93 // enclosing scope. An account that is already ENABLED is left untouched; a
94 // DISABLED one gets CIS_SCAN_TYPE enabled, and only that type is disabled
95 // again when the scope closes.
96 const withInspectorEnabled = Effect.gen(function* () {
97 const initial = yield* waitForAccount(
98 "ENABLED or DISABLED",
99 (account) => account.state?.status === "ENABLED" || account.state?.status === "DISABLED",
100 );
101 if (initial.state?.status === "ENABLED") {
102 yield* Effect.logInfo(
103 "Inspector already enabled — CIS lifecycle leaves the enablement untouched",
104 );
105 return;
106 }
107
108 const accountId = initial.accountId;
109 yield* Effect.acquireRelease(
110 inspector2.enable({ accountIds: [accountId], resourceTypes: [CIS_SCAN_TYPE] }).pipe(
111 Effect.flatMap((response) =>
112 response.failedAccounts && response.failedAccounts.length > 0
113 ? Effect.fail(
114 new InspectorRequestRejected({
115 operation: "enable",
116 failedAccounts: response.failedAccounts,
117 }),
118 )
119 : Effect.void,
120 ),
121 ),
122 () => restoreDisabled(accountId).pipe(Effect.orDie),
123 );
124 yield* waitForAccount(
125 "ENABLED",
126 (account) => account.state?.status === "ENABLED" && typeStatus(account, "ec2") === "ENABLED",
127 );
128 });
129
130 test.provider(
131 "account scan status is observable",
132 () =>
133 Effect.gen(function* () {
134 const account = yield* accountStatus;
135 expect(account?.accountId).toBeTruthy();
136 expect(
137 ["ENABLED", "ENABLING", "DISABLED", "DISABLING"].includes(typeStatus(account, "ec2") ?? ""),
138 ).toBe(true);
139 expect(
140 ["ENABLED", "ENABLING", "DISABLED", "DISABLING"].includes(typeStatus(account, "ecr") ?? ""),
141 ).toBe(true);
142 }),
143 { tags: ["provider:aws", "provider:aws:inspector2", "live"] },
144 );
145
146 // Inspector enablement is an account/region singleton. Every test that
147 // toggles it — or asserts behavior that depends on it — lives in this one
148 // sequential block so none of them observes another's enable window. Order:
149 // the disabled-account probe first (before any toggling), then the CIS
150 // lifecycle (which waits for its own disable to settle), then the Enabler
151 // lifecycle (which requires a fully DISABLED account).
152 describe.sequential("Inspector2 account enablement", () => {
153 // The CIS scan APIs are hard-gated on Inspector enablement — a disabled
154 // account gets a typed AccessDeniedException ("Invoking account is not
155 // enabled."). This ungated probe pins that behavior.
156 test.provider(
157 "CIS scan APIs reject a non-enabled account (typed)",
158 () =>
159 Effect.gen(function* () {
160 const account = (yield* inspector2.batchGetAccountStatus({})).accounts?.[0];
161 if (account?.state?.status === "ENABLED") {
162 yield* Effect.logInfo(
163 "Inspector is enabled in this account — CIS APIs are accessible, probe not applicable",
164 );
165 return;
166 }
167 const result = yield* Effect.result(inspector2.listCisScanConfigurations({}));
168 expect(Result.isFailure(result)).toBe(true);
169 if (Result.isFailure(result)) {
170 expect(result.failure._tag).toBe("AccessDeniedException");
171 }
172 }),
173 { tags: ["provider:aws", "provider:aws:inspector2", "live"] },
174 );
175
176 // Full CIS scan configuration lifecycle. Self-sufficient: borrows Inspector
177 // enablement for the duration of the test when the account is disabled.
178 test.provider.skipIf(!process.env.INSPECTOR2_TEST_CIS)(
179 "lifecycle: CIS scan configuration create, update, destroy",
180 (stack) =>
181 Effect.scoped(
182 Effect.gen(function* () {
183 yield* withInspectorEnabled;
184 // Finalizers run in reverse order, so this teardown runs while
185 // Inspector is still enabled — the harness's own trailing destroy
186 // runs after the scope disabled it, when the CIS delete API rejects.
187 yield* Effect.addFinalizer(() => stack.destroy().pipe(Effect.ignore));
188
189 yield* stack.destroy();
190
191 const deploy = (props: { securityLevel: "LEVEL_1" | "LEVEL_2"; timeOfDay: string }) =>
192 stack.deploy(
193 Effect.gen(function* () {
194 const cis = yield* CisScanConfiguration("NightlyCis", {
195 securityLevel: props.securityLevel,
196 schedule: {
197 daily: { startTime: { timeOfDay: props.timeOfDay, timezone: "UTC" } },
198 },
199 targets: {
200 accountIds: ["SELF"],
201 targetResourceTags: { AlchemyCisTest: ["true"] },
202 },
203 tags: { env: "test" },
204 });
205 return {
206 scanConfigurationArn: cis.scanConfigurationArn,
207 scanName: cis.scanName,
208 securityLevel: cis.securityLevel,
209 };
210 }),
211 );
212
213 const created = yield* deploy({ securityLevel: "LEVEL_1", timeOfDay: "02:00" });
214 // arn:aws:inspector2:<region>:<account>:owner/<owner>/cis-configuration/<id>
215 expect(created.scanConfigurationArn).toContain("cis-configuration");
216 expect(created.securityLevel).toBe("LEVEL_1");
217
218 const byArn = () =>
219 inspector2
220 .listCisScanConfigurations({
221 filterCriteria: {
222 scanConfigurationArnFilters: [
223 { comparison: "EQUALS", value: created.scanConfigurationArn },
224 ],
225 },
226 })
227 .pipe(Effect.map((r) => r.scanConfigurations?.[0]));
228
229 const live = yield* byArn();
230 expect(live?.securityLevel).toBe("LEVEL_1");
231 expect(live?.schedule?.daily?.startTime.timeOfDay).toBe("02:00");
232
233 // Canonical list() coverage.
234 const provider = yield* Provider.findProvider(CisScanConfiguration);
235 const all = yield* provider.list();
236 expect(all.some((c) => c.scanConfigurationArn === created.scanConfigurationArn)).toBe(
237 true,
238 );
239
240 // Update in place — the ARN is stable.
241 const updated = yield* deploy({ securityLevel: "LEVEL_2", timeOfDay: "03:30" });
242 expect(updated.scanConfigurationArn).toBe(created.scanConfigurationArn);
243 expect(updated.securityLevel).toBe("LEVEL_2");
244 const liveUpdated = yield* byArn();
245 expect(liveUpdated?.schedule?.daily?.startTime.timeOfDay).toBe("03:30");
246
247 // Destroy — the configuration is gone.
248 yield* stack.destroy();
249 expect(yield* byArn()).toBeUndefined();
250 }),
251 ),
252 { tags: ["provider:aws", "provider:aws:inspector2", "live"], timeout: 240_000 },
253 );
254
255 // This test only runs when Inspector is fully disabled — it must never
256 // disable scan types the user already enabled (capture-and-restore safety).
257 test.provider.skipIf(!process.env.INSPECTOR2_TEST_ENABLER)(
258 "lifecycle: enable EC2/ECR, add LAMBDA, disable",
259 (stack) =>
260 Effect.gen(function* () {
261 const preexisting = yield* accountStatus;
262 if (preexisting && preexisting.state?.status !== "DISABLED") {
263 yield* Effect.logInfo(
264 `Inspector already enabled (${preexisting.state?.status}) — skipping destructive lifecycle test`,
265 );
266 return;
267 }
268
269 yield* stack.destroy();
270
271 // Create — enable EC2 + ECR scanning.
272 const created = yield* stack.deploy(
273 Effect.gen(function* () {
274 return yield* Enabler("Inspector", { resourceTypes: ["EC2", "ECR"] });
275 }),
276 );
277 expect(created.accountId).toBeTruthy();
278 expect(created.resourceTypes.sort()).toEqual(["EC2", "ECR"]);
279
280 // Out-of-band verification.
281 const live = yield* accountStatus;
282 expect(typeStatus(live, "ec2")).toBe("ENABLED");
283 expect(typeStatus(live, "ecr")).toBe("ENABLED");
284
285 // Canonical list() coverage.
286 const provider = yield* Provider.findProvider(Enabler);
287 const all = yield* provider.list();
288 expect(all.some((e) => e.accountId === created.accountId)).toBe(true);
289
290 // Update — add LAMBDA scanning.
291 const updated = yield* stack.deploy(
292 Effect.gen(function* () {
293 return yield* Enabler("Inspector", { resourceTypes: ["EC2", "ECR", "LAMBDA"] });
294 }),
295 );
296 expect(updated.resourceTypes.sort()).toEqual(["EC2", "ECR", "LAMBDA"]);
297 const afterUpdate = yield* accountStatus;
298 expect(typeStatus(afterUpdate, "lambda")).toBe("ENABLED");
299
300 // Destroy — Inspector scanning is disabled again.
301 yield* stack.destroy();
302 const after = yield* accountStatus;
303 expect(typeStatus(after, "ec2")).not.toBe("ENABLED");
304 expect(typeStatus(after, "ecr")).not.toBe("ENABLED");
305 expect(typeStatus(after, "lambda")).not.toBe("ENABLED");
306 }),
307 { tags: ["provider:aws", "provider:aws:inspector2", "live"], timeout: 240_000 },
308 );
309 });