@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 140 lines 4.64 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Policy } from "@alchemy.run/aws/IAM";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as IAM from "@distilled.cloud/aws/iam";
6 import * as Provider from "alchemy/Provider";
7 import * as Effect from "effect/Effect";
8
9 const { test } = Test.make({ providers: AWS.providers() });
10
11 test.provider(
12 "create, update, and delete managed policy",
13 (stack) =>
14 Effect.gen(function* () {
15 yield* stack.destroy();
16
17 const policy = yield* stack.deploy(
18 Effect.gen(function* () {
19 return yield* Policy("IamPolicy", {
20 policyDocument: {
21 Version: "2012-10-17",
22 Statement: [{ Effect: "Allow", Action: ["s3:ListBucket"], Resource: ["*"] }],
23 },
24 tags: { env: "test" },
25 });
26 }),
27 );
28
29 const created = yield* IAM.getPolicy({ PolicyArn: policy.policyArn });
30 expect(created.Policy?.PolicyName).toBe(policy.policyName);
31
32 yield* stack.deploy(
33 Effect.gen(function* () {
34 return yield* Policy("IamPolicy", {
35 policyDocument: {
36 Version: "2012-10-17",
37 Statement: [{ Effect: "Allow", Action: ["s3:GetObject"], Resource: ["*"] }],
38 },
39 tags: { env: "prod" },
40 });
41 }),
42 );
43
44 const updatedTags = yield* IAM.listPolicyTags({ PolicyArn: policy.policyArn });
45 expect(
46 Object.fromEntries((updatedTags.Tags ?? []).map((tag) => [tag.Key, tag.Value])),
47 ).toMatchObject({ env: "prod" });
48
49 yield* stack.destroy();
50
51 const deleted = yield* IAM.getPolicy({ PolicyArn: policy.policyArn }).pipe(Effect.option);
52 expect(deleted._tag).toBe("None");
53 }),
54 { tags: ["provider:aws", "provider:aws:iam", "live"] },
55 );
56
57 test.provider(
58 "rotates policy versions past the 5-version cap",
59 (stack) =>
60 Effect.gen(function* () {
61 yield* stack.destroy();
62
63 const deployWith = (actions: [string, ...string[]]) =>
64 stack.deploy(
65 Effect.gen(function* () {
66 return yield* Policy("VersionCapPolicy", {
67 policyDocument: {
68 Version: "2012-10-17",
69 Statement: [{ Effect: "Allow", Action: actions, Resource: ["*"] }],
70 },
71 });
72 }),
73 );
74
75 // 1 create + 6 document updates = 7 versions requested; IAM caps
76 // managed policies at 5 versions, so the provider must prune the
77 // oldest non-default version before each rotation past the cap.
78 const documents: [string, ...string[]][] = [
79 ["s3:ListBucket"],
80 ["s3:GetObject"],
81 ["s3:PutObject"],
82 ["s3:DeleteObject"],
83 ["s3:GetObjectTagging"],
84 ["s3:PutObjectTagging"],
85 ["s3:GetBucketLocation"],
86 ];
87 let policy: { policyArn: string } | undefined;
88 for (const actions of documents) {
89 policy = yield* deployWith(actions);
90 }
91
92 const versions = yield* IAM.listPolicyVersions({ PolicyArn: policy!.policyArn });
93 expect(versions.Versions?.length ?? 0).toBeLessThanOrEqual(5);
94
95 // The default version must carry the last document.
96 const defaultVersion = versions.Versions?.find((version) => version.IsDefaultVersion);
97 expect(defaultVersion?.VersionId).toBeDefined();
98 const document = yield* IAM.getPolicyVersion({
99 PolicyArn: policy!.policyArn,
100 VersionId: defaultVersion!.VersionId!,
101 });
102 const decoded = JSON.parse(decodeURIComponent(document.PolicyVersion?.Document ?? "")) as {
103 Statement: [{ Action: string[] }];
104 };
105 expect(decoded.Statement[0].Action).toEqual(["s3:GetBucketLocation"]);
106
107 yield* stack.destroy();
108
109 const deleted = yield* IAM.getPolicy({ PolicyArn: policy!.policyArn }).pipe(Effect.option);
110 expect(deleted._tag).toBe("None");
111 }),
112 { tags: ["provider:aws", "provider:aws:iam", "live"], timeout: 120_000 },
113 );
114
115 test.provider(
116 "list enumerates the deployed policy",
117 (stack) =>
118 Effect.gen(function* () {
119 yield* stack.destroy();
120
121 const deployed = yield* stack.deploy(
122 Effect.gen(function* () {
123 return yield* Policy("ListPolicy", {
124 policyDocument: {
125 Version: "2012-10-17",
126 Statement: [{ Effect: "Allow", Action: ["s3:ListBucket"], Resource: ["*"] }],
127 },
128 });
129 }),
130 );
131
132 const provider = yield* Provider.findProvider(Policy);
133 const all = yield* provider.list();
134
135 expect(all.some((p) => p.policyArn === deployed.policyArn)).toBe(true);
136
137 yield* stack.destroy();
138 }),
139 { tags: ["provider:aws", "provider:aws:iam", "live"] },
140 );