@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, 06:00 UTC

main
ts 133 lines 5.34 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import {
3 normalizePolicyDocument,
4 type ServiceControlPolicyDocument,
5 } from "@alchemy.run/aws/IAM/Policy";
6 import { Policy } from "@alchemy.run/aws/Organizations";
7 import { expect } from "@alchemy.run/test";
8 import * as organizations from "@distilled.cloud/aws/organizations";
9 import * as Provider from "alchemy/Provider";
10 import * as Effect from "effect/Effect";
11 import * as OrganizationLease from "./OrganizationLease.ts";
12
13 const { test } = OrganizationLease.make({ providers: AWS.providers() }, "shared");
14
15 // `listPolicies` requires a `Filter` (policy type), so `list()` fans out across
16 // every policy-type filter and hydrates each policy via `describePolicy` into the
17 // exact `read` shape. This runs read-only: when the account is an org management
18 // account, AWS-managed SCPs like `FullAWSAccess` appear; otherwise `list()`
19 // degrades to `[]` via the typed `AWSOrganizationsNotInUseException` /
20 // `AccessDeniedException` catches, so the assertions hold without deploying.
21 test.provider(
22 "list enumerates organization policies",
23 (stack) =>
24 Effect.gen(function* () {
25 const provider = yield* Provider.findProvider(Policy);
26 const all = yield* provider.list();
27
28 expect(Array.isArray(all)).toBe(true);
29
30 for (const policy of all) {
31 expect(typeof policy.policyId).toBe("string");
32 expect(policy.policyId.length).toBeGreaterThan(0);
33 expect(typeof policy.policyArn).toBe("string");
34 expect(policy.policyArn.startsWith("arn:aws")).toBe(true);
35 expect(typeof policy.name).toBe("string");
36 expect(policy.document).toBeDefined();
37 expect(policy.tags).toBeDefined();
38 }
39
40 yield* stack.destroy();
41 }),
42 { tags: ["provider:aws", "provider:aws:organizations", "live"] },
43 );
44
45 // PolicyDocument adoption: a typed `ServiceControlPolicyDocument` deploys, the
46 // stored SCP content round-trips (normalized comparison), and re-deploying the
47 // identical document is clean — reconcile diffs
48 // `normalizePolicyDocument(observed)` against
49 // `normalizePolicyDocument(desired)` and skips `updatePolicy` on equivalence.
50 //
51 // `createPolicy`/`deletePolicy` require an org MANAGEMENT account, so gate
52 // behind the same env var the other Organizations lifecycle tests use.
53 test.provider.skipIf(!process.env.AWS_ORG_MANAGEMENT_ACCOUNT)(
54 "typed SCP document deploys and re-deploys clean",
55 (stack) =>
56 Effect.gen(function* () {
57 const policyName = "alchemy-test-org-policy-typed-scp";
58
59 // A prior interrupted run may have left an (untagged, hence unowned)
60 // SCP with our deterministic name behind — the adoption probe would
61 // then fail the create with `OwnedBySomeoneElse`. Clean it up
62 // out-of-band before deploying.
63 const leftovers = yield* organizations
64 .listPolicies({ Filter: "SERVICE_CONTROL_POLICY" })
65 .pipe(Effect.map((page) => page.Policies ?? []));
66 for (const leftover of leftovers) {
67 if (leftover.Name !== policyName || leftover.Id == null) continue;
68 yield* organizations
69 .deletePolicy({ PolicyId: leftover.Id })
70 .pipe(Effect.catchTag("PolicyNotFoundException", () => Effect.void));
71 }
72
73 // Key order is deliberately non-alphabetical so the no-op re-deploy
74 // exercises the canonicalizing comparison, not string equality.
75 const document: ServiceControlPolicyDocument = {
76 Version: "2012-10-17",
77 Statement: [
78 {
79 Sid: "AllowAll",
80 Effect: "Allow",
81 Resource: "*",
82 Action: ["*"],
83 },
84 ],
85 };
86
87 const deployPolicy = stack.deploy(
88 Effect.gen(function* () {
89 return yield* Policy("TypedScp", {
90 name: policyName,
91 type: "SERVICE_CONTROL_POLICY",
92 document,
93 });
94 }),
95 );
96
97 const created = yield* deployPolicy;
98 expect(created.policyId).toBeTruthy();
99 expect(created.type).toBe("SERVICE_CONTROL_POLICY");
100 // The typed attribute round-trips the typed document.
101 expect(normalizePolicyDocument(created.document)).toBe(normalizePolicyDocument(document));
102
103 // Out-of-band verification via distilled: the stored content is
104 // equivalent to the typed document.
105 const described = yield* organizations.describePolicy({
106 PolicyId: created.policyId,
107 });
108 expect(described.Policy?.Content).toBeTruthy();
109 expect(normalizePolicyDocument(described.Policy?.Content ?? "")).toBe(
110 normalizePolicyDocument(document),
111 );
112
113 // Re-deploy the identical typed document — must be a clean no-op:
114 // same physical policy, equivalent content.
115 const redeployed = yield* deployPolicy;
116 expect(redeployed.policyId).toBe(created.policyId);
117 expect(redeployed.policyArn).toBe(created.policyArn);
118 expect(normalizePolicyDocument(redeployed.document)).toBe(normalizePolicyDocument(document));
119
120 yield* stack.destroy();
121
122 // Typed wait-until-gone: the policy is deleted after destroy.
123 const gone = yield* organizations.describePolicy({ PolicyId: created.policyId }).pipe(
124 Effect.map(() => false),
125 Effect.catchTag("PolicyNotFoundException", () => Effect.succeed(true)),
126 );
127 expect(gone).toBe(true);
128 }),
129 {
130 tags: ["provider:aws", "provider:aws:iam", "provider:aws:organizations", "live"],
131 timeout: 240_000,
132 },
133 );