@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 166 lines 5.74 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import type { PolicyDocument } from "@alchemy.run/aws/IAM/Policy";
3 import { EmailIdentity, EmailIdentityPolicy } from "@alchemy.run/aws/SES";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as sesv2 from "@distilled.cloud/aws/sesv2";
7 import * as Output from "alchemy/Output";
8 import * as Data from "effect/Data";
9 import * as Effect from "effect/Effect";
10 import * as Schedule from "effect/Schedule";
11
12 const { test } = Test.make({ providers: AWS.providers() });
13
14 const DOMAIN = "policy.alchemy-test.example.com";
15
16 // SES validates the policy document on write: Resource must be the identity's
17 // own ARN, not a wildcard ("BadRequestException: Invalid ARN: ARNs must start
18 // with 'arn:': *").
19 const allowPolicy = (sid: string, identityArn: string): PolicyDocument => ({
20 Version: "2012-10-17",
21 Statement: [
22 {
23 Sid: sid,
24 Effect: "Allow",
25 Principal: { AWS: "*" },
26 Action: ["ses:SendEmail"],
27 Resource: identityArn,
28 },
29 ],
30 });
31
32 class PolicyStillExists extends Data.TaggedError("PolicyStillExists")<{
33 readonly emailIdentity: string;
34 readonly policyName: string;
35 }> {}
36
37 const assertPolicyDeleted = (emailIdentity: string, policyName: string) =>
38 sesv2.getEmailIdentityPolicies({ EmailIdentity: emailIdentity }).pipe(
39 Effect.flatMap((res) =>
40 (res.Policies ?? {})[policyName] !== undefined
41 ? Effect.fail(new PolicyStillExists({ emailIdentity, policyName }))
42 : Effect.void,
43 ),
44 // A deleted identity takes its policies with it.
45 Effect.catchTag("NotFoundException", () => Effect.void),
46 Effect.retry({
47 while: (e) => e._tag === "PolicyStillExists",
48 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]),
49 }),
50 );
51
52 test.provider(
53 "identity policy lifecycle: create, update document, delete",
54 (stack) =>
55 Effect.gen(function* () {
56 yield* stack.destroy();
57
58 const { identity, policy } = yield* stack.deploy(
59 Effect.gen(function* () {
60 const identity = yield* EmailIdentity("PolicyIdentity", {
61 emailIdentity: DOMAIN,
62 });
63 const policy = yield* EmailIdentityPolicy("Authz", {
64 emailIdentity: identity.emailIdentity,
65 policy: Output.all(identity.identityArn).pipe(
66 Output.map(([arn]) => allowPolicy("AllowSend", arn)),
67 ),
68 });
69 return { identity, policy };
70 }),
71 );
72
73 expect(policy.emailIdentity).toBe(identity.emailIdentity);
74 expect(policy.policyName).toBeDefined();
75
76 // out-of-band verification via distilled
77 const observed = yield* sesv2.getEmailIdentityPolicies({
78 EmailIdentity: identity.emailIdentity,
79 });
80 expect(observed.Policies?.[policy.policyName]).toContain("AllowSend");
81
82 // update the policy document in place (identity stays deployed)
83 yield* stack.deploy(
84 Effect.gen(function* () {
85 const identity = yield* EmailIdentity("PolicyIdentity", {
86 emailIdentity: DOMAIN,
87 });
88 yield* EmailIdentityPolicy("Authz", {
89 emailIdentity: identity.emailIdentity,
90 policy: Output.all(identity.identityArn).pipe(
91 Output.map(([arn]) => allowPolicy("AllowSendUpdated", arn)),
92 ),
93 });
94 }),
95 );
96 const updated = yield* sesv2.getEmailIdentityPolicies({
97 EmailIdentity: identity.emailIdentity,
98 });
99 expect(updated.Policies?.[policy.policyName]).toContain("AllowSendUpdated");
100
101 yield* stack.destroy();
102 yield* assertPolicyDeleted(identity.emailIdentity, policy.policyName);
103 }),
104 {
105 tags: ["provider:aws", "provider:aws:iam", "provider:aws:ses", "live"],
106 timeout: 120_000,
107 },
108 );
109
110 test.provider(
111 "renaming the policy replaces it on the same identity",
112 (stack) =>
113 Effect.gen(function* () {
114 yield* stack.destroy();
115
116 // The identity stays deployed across the replacement so the engine never
117 // removes a dependency of the resource being replaced.
118 const first = yield* stack.deploy(
119 Effect.gen(function* () {
120 const identity = yield* EmailIdentity("RenamePolicyIdentity", {
121 emailIdentity: DOMAIN,
122 });
123 const policy = yield* EmailIdentityPolicy("RenamePolicy", {
124 emailIdentity: identity.emailIdentity,
125 policyName: "alchemy-test-policy-a",
126 policy: Output.all(identity.identityArn).pipe(
127 Output.map(([arn]) => allowPolicy("A", arn)),
128 ),
129 });
130 return {
131 emailIdentity: identity.emailIdentity,
132 policyName: policy.policyName,
133 };
134 }),
135 );
136 expect(first.policyName).toBe("alchemy-test-policy-a");
137
138 const second = yield* stack.deploy(
139 Effect.gen(function* () {
140 const identity = yield* EmailIdentity("RenamePolicyIdentity", {
141 emailIdentity: DOMAIN,
142 });
143 const policy = yield* EmailIdentityPolicy("RenamePolicy", {
144 emailIdentity: identity.emailIdentity,
145 policyName: "alchemy-test-policy-b",
146 policy: Output.all(identity.identityArn).pipe(
147 Output.map(([arn]) => allowPolicy("B", arn)),
148 ),
149 });
150 return {
151 emailIdentity: identity.emailIdentity,
152 policyName: policy.policyName,
153 };
154 }),
155 );
156 expect(second.policyName).toBe("alchemy-test-policy-b");
157 yield* assertPolicyDeleted(first.emailIdentity, "alchemy-test-policy-a");
158
159 yield* stack.destroy();
160 yield* assertPolicyDeleted(second.emailIdentity, "alchemy-test-policy-b");
161 }),
162 {
163 tags: ["provider:aws", "provider:aws:iam", "provider:aws:ses", "live"],
164 timeout: 120_000,
165 },
166 );