@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 254 lines 10.9 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { EnabledBaseline, EnabledControl } from "@alchemy.run/aws/ControlTower";
3 import { describe, expect } from "@alchemy.run/test";
4 import * as controltower from "@distilled.cloud/aws/controltower";
5 import * as Effect from "effect/Effect";
6 import * as Result from "effect/Result";
7 import * as Schedule from "effect/Schedule";
8 import * as Stream from "effect/Stream";
9 import * as OrganizationLease from "../Organizations/OrganizationLease.ts";
10
11 // Shared organization lease: the baseline lifecycle targets an OU, which the
12 // emulator fixture provides when AWS_TEST_CONTROLTOWER_OU is unset.
13 const { test } = OrganizationLease.make({ providers: AWS.providers() }, "shared");
14
15 // AWS Control Tower requires an AWS Organizations MANAGEMENT account with a
16 // landing zone deployed — neither is available on the shared testing
17 // account, so every lifecycle is hard-gated behind AWS_TEST_CONTROLTOWER=1.
18 // The ungated probes below prove the distilled wiring and typed error
19 // unions the providers' read/delete paths depend on, at near-zero cost.
20
21 describe(
22 "AWS.ControlTower",
23 { tags: ["provider:aws", "provider:aws:controltower", "live"] },
24 () => {
25 test.provider(
26 "getLandingZoneOperation on a bogus operation id yields a typed error",
27 () =>
28 Effect.gen(function* () {
29 const error = yield* Effect.flip(
30 controltower.getLandingZoneOperation({
31 operationIdentifier: "00000000-0000-0000-0000-000000000000",
32 }),
33 );
34 // A well-formed but absent operation id yields the typed
35 // ResourceNotFoundException the providers' waiters depend on —
36 // even on an account without a landing zone.
37 expect(error._tag).toBe("ResourceNotFoundException");
38 }),
39 { timeout: 60_000 },
40 );
41
42 test.provider(
43 "listLandingZones yields a typed result",
44 () =>
45 Effect.gen(function* () {
46 const result = yield* Effect.result(controltower.listLandingZones({}));
47 if (Result.isSuccess(result)) {
48 // No landing zone on the testing account — an empty (or singleton)
49 // list, never a crash.
50 expect(Array.isArray(result.success.landingZones)).toBe(true);
51 } else {
52 // Some org configurations reject the call outright — but always
53 // with a typed tag (UnauthorizedException is a distilled patch).
54 expect(["AccessDeniedException", "UnauthorizedException"]).toContain(
55 result.failure._tag,
56 );
57 }
58 }),
59 { timeout: 60_000 },
60 );
61
62 test.provider(
63 "getEnabledControl on a nonexistent identifier yields a typed error",
64 () =>
65 Effect.gen(function* () {
66 const error = yield* Effect.flip(
67 controltower.getEnabledControl({
68 enabledControlIdentifier:
69 "arn:aws:controltower:us-west-2:111111111111:enabledcontrol/AAAAAAAAAAAAAAAA",
70 }),
71 );
72 // Both a genuinely absent enabled control and a no-landing-zone
73 // account surface the typed ResourceNotFoundException the
74 // provider's observe/delete paths depend on.
75 expect(error._tag).toBe("ResourceNotFoundException");
76 }),
77 { timeout: 60_000 },
78 );
79
80 test.provider(
81 "getEnabledBaseline on a nonexistent identifier yields a typed error",
82 () =>
83 Effect.gen(function* () {
84 const error = yield* Effect.flip(
85 controltower.getEnabledBaseline({
86 enabledBaselineIdentifier:
87 "arn:aws:controltower:us-west-2:111111111111:enabledbaseline/AAAAAAAAAAAAAAAA",
88 }),
89 );
90 // The baseline API family rejects accounts without a landing zone
91 // with the wire error UnauthorizedException, which the Smithy model
92 // omits — surfaced as a typed tag via the distilled patch. An
93 // entitled account yields ResourceNotFoundException instead.
94 expect(["UnauthorizedException", "ResourceNotFoundException"]).toContain(error._tag);
95 }),
96 { timeout: 60_000 },
97 );
98
99 test.provider(
100 "listBaselines yields a typed result",
101 () =>
102 Effect.gen(function* () {
103 const result = yield* Effect.result(controltower.listBaselines({}));
104 if (Result.isSuccess(result)) {
105 // The baseline catalog is served even without a landing zone.
106 expect(result.success.baselines.length).toBeGreaterThan(0);
107 expect(result.success.baselines.some((b) => b.name === "AWSControlTowerBaseline")).toBe(
108 true,
109 );
110 } else {
111 expect(["AccessDeniedException", "UnauthorizedException"]).toContain(
112 result.failure._tag,
113 );
114 }
115 }),
116 { timeout: 60_000 },
117 );
118
119 // ---------------------------------------------------------------------
120 // Gated lifecycles — need an Organizations management account with a
121 // Control Tower landing zone. Provide:
122 // AWS_TEST_CONTROLTOWER=1
123 // AWS_TEST_CONTROLTOWER_OU target OU ARN (unregistered OU for
124 // the baseline test; provisioned by
125 // OrganizationLease on the emulator)
126 // AWS_TEST_CONTROLTOWER_CONTROL control ARN (defaults to the
127 // AWS-GR_ENCRYPTED_VOLUMES legacy ARN
128 // in us-west-2)
129 // AWS_TEST_CONTROLTOWER_BASELINE_VERSION (defaults to "4.0")
130 // ---------------------------------------------------------------------
131
132 const controlArn =
133 process.env.AWS_TEST_CONTROLTOWER_CONTROL ??
134 "arn:aws:controltower:us-west-2::control/AWS-GR_ENCRYPTED_VOLUMES";
135
136 test.provider.skipIf(!process.env.AWS_TEST_CONTROLTOWER)(
137 "enable a control on an OU, verify, then disable",
138 (stack) =>
139 Effect.gen(function* () {
140 yield* stack.destroy();
141 const ouArn = yield* OrganizationLease.controlTowerOuArn;
142
143 const { enabled } = yield* stack.deploy(
144 Effect.gen(function* () {
145 const enabled = yield* EnabledControl("Guardrail", {
146 controlIdentifier: controlArn,
147 targetIdentifier: ouArn,
148 tags: { fixture: "controltower-enabled-control" },
149 });
150 return { enabled };
151 }),
152 );
153
154 expect(enabled.enabledControlArn).toContain(":enabledcontrol/");
155 expect(enabled.controlIdentifier).toBe(controlArn);
156 expect(enabled.targetIdentifier).toBe(ouArn);
157
158 // Out-of-band verification via distilled.
159 const observed = yield* controltower.getEnabledControl({
160 enabledControlIdentifier: enabled.enabledControlArn,
161 });
162 expect(observed.enabledControlDetails.controlIdentifier).toBe(controlArn);
163 expect(observed.enabledControlDetails.statusSummary?.status).toBe("SUCCEEDED");
164
165 // Destroy and verify the enablement is gone (typed wait-until-gone).
166 yield* stack.destroy();
167 yield* controltower
168 .getEnabledControl({ enabledControlIdentifier: enabled.enabledControlArn })
169 .pipe(
170 Effect.flatMap(() => Effect.fail(new Error("enabled control still exists"))),
171 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
172 Effect.retry({
173 schedule: Schedule.max([Schedule.fixed("10 seconds"), Schedule.recurs(18)]),
174 }),
175 );
176 }),
177 // enable (~minutes) + disable (~minutes), one test.
178 { timeout: 1_500_000 },
179 );
180
181 test.provider.skipIf(!process.env.AWS_TEST_CONTROLTOWER)(
182 "enable the Control Tower baseline on an OU, verify, then disable",
183 (stack) =>
184 Effect.gen(function* () {
185 yield* stack.destroy();
186 const ouArn = yield* OrganizationLease.controlTowerOuArn;
187
188 // Discover the AWSControlTowerBaseline ARN dynamically.
189 const baselines = yield* controltower.listBaselines.pages({}).pipe(
190 Stream.runCollect,
191 Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.baselines)),
192 );
193 const controlTowerBaseline = baselines.find((b) => b.name === "AWSControlTowerBaseline");
194 expect(controlTowerBaseline).toBeDefined();
195 const baselineVersion = process.env.AWS_TEST_CONTROLTOWER_BASELINE_VERSION ?? "4.0";
196
197 const { enabled } = yield* stack.deploy(
198 Effect.gen(function* () {
199 const enabled = yield* EnabledBaseline("OuBaseline", {
200 baselineIdentifier: controlTowerBaseline!.arn,
201 baselineVersion,
202 targetIdentifier: ouArn,
203 tags: { fixture: "controltower-enabled-baseline" },
204 });
205 return { enabled };
206 }),
207 );
208
209 expect(enabled.enabledBaselineArn).toContain(":enabledbaseline/");
210 expect(enabled.baselineIdentifier).toBe(controlTowerBaseline!.arn);
211 expect(enabled.targetIdentifier).toBe(ouArn);
212
213 // Out-of-band verification via distilled.
214 const observed = yield* controltower.getEnabledBaseline({
215 enabledBaselineIdentifier: enabled.enabledBaselineArn,
216 });
217 expect(observed.enabledBaselineDetails?.statusSummary.status).toBe("SUCCEEDED");
218
219 // Destroy and verify the enablement is gone (typed wait-until-gone).
220 yield* stack.destroy();
221 yield* controltower
222 .getEnabledBaseline({ enabledBaselineIdentifier: enabled.enabledBaselineArn })
223 .pipe(
224 Effect.flatMap(() => Effect.fail(new Error("enabled baseline still exists"))),
225 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
226 Effect.retry({
227 schedule: Schedule.max([Schedule.fixed("10 seconds"), Schedule.recurs(18)]),
228 }),
229 );
230 }),
231 { timeout: 1_500_000 },
232 );
233
234 // The LandingZone resource's create/decommission paths are implemented
235 // but deliberately NOT exercised even when gated — decommissioning a
236 // live organization's landing zone is an irreversible ~1 hour operation.
237 // On an entitled account this asserts the observe/read path end-to-end.
238 test.provider.skipIf(!process.env.AWS_TEST_CONTROLTOWER)(
239 "landing zone read path resolves the deployed landing zone",
240 () =>
241 Effect.gen(function* () {
242 const { landingZones } = yield* controltower.listLandingZones({});
243 expect(landingZones.length).toBe(1);
244 const arn = landingZones[0]!.arn!;
245 const { landingZone } = yield* controltower.getLandingZone({
246 landingZoneIdentifier: arn,
247 });
248 expect(landingZone.version).toBeDefined();
249 expect(landingZone.status).toBeDefined();
250 }),
251 { timeout: 120_000 },
252 );
253 },
254 );