@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 226 lines 9.86 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import {
3 CertificateAuthority,
4 CertificateAuthorityPolicy,
5 Permission,
6 } from "@alchemy.run/aws/ACMPCA";
7 import { AWSEnvironment } from "@alchemy.run/aws/Environment";
8 import { expect } from "@alchemy.run/test";
9 import * as Test from "@alchemy.run/test/Alchemy";
10 import * as acmpca from "@distilled.cloud/aws/acm-pca";
11 import * as ram from "@distilled.cloud/aws/ram";
12 import * as Effect from "effect/Effect";
13 import * as Schedule from "effect/Schedule";
14
15 const { test } = Test.make({ providers: AWS.providers() });
16
17 // Ungated typed-error probes: prove the distilled union carries the tags the
18 // provider's read/reconcile/delete paths depend on. Run in every CI pass at
19 // near-zero cost, unlike the gated lifecycle below.
20 test.provider(
21 "describeCertificateAuthority on a nonexistent CA fails with ResourceNotFoundException",
22 () =>
23 Effect.gen(function* () {
24 const { accountId, region } = yield* AWSEnvironment.current;
25 const error = yield* Effect.flip(
26 acmpca.describeCertificateAuthority({
27 CertificateAuthorityArn: `arn:aws:acm-pca:${region}:${accountId}:certificate-authority/00000000-0000-0000-0000-000000000000`,
28 }),
29 );
30 expect(error._tag).toBe("ResourceNotFoundException");
31 }),
32 { tags: ["provider:aws", "provider:aws:acmpca", "live"] },
33 );
34
35 test.provider(
36 "getPolicy on a nonexistent CA fails with ResourceNotFoundException",
37 () =>
38 Effect.gen(function* () {
39 const { accountId, region } = yield* AWSEnvironment.current;
40 // Proves the typed tag the CertificateAuthorityPolicy provider's
41 // observe path treats as "no policy".
42 const error = yield* Effect.flip(
43 acmpca.getPolicy({
44 ResourceArn: `arn:aws:acm-pca:${region}:${accountId}:certificate-authority/00000000-0000-0000-0000-000000000000`,
45 }),
46 );
47 expect(error._tag).toBe("ResourceNotFoundException");
48 }),
49 { tags: ["provider:aws", "provider:aws:acmpca", "live"] },
50 );
51
52 test.provider(
53 "describeCertificateAuthority on a non-CA ARN fails with InvalidArnException",
54 () =>
55 Effect.gen(function* () {
56 const { accountId, region } = yield* AWSEnvironment.current;
57 // A well-formed ARN whose resource type is not certificate-authority.
58 // (A fully malformed string is rejected earlier as ValidationException.)
59 const error = yield* Effect.flip(
60 acmpca.describeCertificateAuthority({
61 CertificateAuthorityArn: `arn:aws:acm-pca:${region}:${accountId}:something/else`,
62 }),
63 );
64 expect(error._tag).toBe("InvalidArnException");
65 }),
66 { tags: ["provider:aws", "provider:aws:acmpca", "live"] },
67 );
68
69 // Deletion leaves the CA in the DELETED state for its 7-day restoration
70 // window; both DELETED and fully-gone count as deleted.
71 const assertCaDeleted = (arn: string) =>
72 acmpca.describeCertificateAuthority({ CertificateAuthorityArn: arn }).pipe(
73 Effect.map((r) => r.CertificateAuthority?.Status ?? "gone"),
74 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed("gone" as const)),
75 Effect.flatMap((status) =>
76 status === "gone" || status === "DELETED"
77 ? Effect.void
78 : Effect.fail(new Error(`CA '${arn}' still exists (status: ${status})`)),
79 ),
80 Effect.retry({ schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(10)]) }),
81 );
82
83 // A private CA bills a prorated monthly fee while it exists. The lifecycle
84 // keeps that to seconds: short-lived certificate mode (the cheaper tier), the
85 // CA is never activated (a PENDING_CERTIFICATE CA deletes without disabling),
86 // and the test always destroys what it created.
87 test.provider(
88 "create private CA + ACM permission, update, replace, destroy",
89 (stack) =>
90 Effect.gen(function* () {
91 yield* stack.destroy();
92 const { accountId } = yield* AWSEnvironment.current;
93
94 const caStack = (props: { commonName: string; tags: Record<string, string> }) =>
95 Effect.gen(function* () {
96 const ca = yield* CertificateAuthority("TestCA", {
97 subject: { commonName: props.commonName },
98 usageMode: "SHORT_LIVED_CERTIFICATE",
99 permanentDeletionTime: "7 days",
100 tags: props.tags,
101 });
102 const permission = yield* Permission("AcmRenewal", {
103 certificateAuthorityArn: ca.certificateAuthorityArn,
104 });
105 const policy = yield* CertificateAuthorityPolicy("SharePolicy", {
106 certificateAuthorityArn: ca.certificateAuthorityArn,
107 policy: {
108 Version: "2012-10-17",
109 Statement: [
110 {
111 Effect: "Allow",
112 Principal: { AWS: `arn:aws:iam::${accountId}:root` },
113 Action: [
114 "acm-pca:DescribeCertificateAuthority",
115 "acm-pca:GetCertificate",
116 "acm-pca:GetCertificateAuthorityCertificate",
117 "acm-pca:ListPermissions",
118 "acm-pca:IssueCertificate",
119 "acm-pca:RevokeCertificate",
120 ],
121 Resource: ca.certificateAuthorityArn,
122 },
123 ],
124 },
125 });
126 return { ca, permission, policy };
127 });
128 const deployCa = (props: { commonName: string; tags: Record<string, string> }) =>
129 stack.deploy(caStack(props));
130
131 // 1. CREATE — a fresh CA lands in PENDING_CERTIFICATE.
132 const first = yield* deployCa({
133 commonName: "alchemy-test.internal",
134 tags: { fixture: "acmpca" },
135 });
136 expect(first.ca.certificateAuthorityArn).toContain(":certificate-authority/");
137 expect(first.ca.status).toBe("PENDING_CERTIFICATE");
138 expect(first.permission.principal).toBe("acm.amazonaws.com");
139
140 // Out-of-band verification via distilled.
141 const described = yield* acmpca.describeCertificateAuthority({
142 CertificateAuthorityArn: first.ca.certificateAuthorityArn,
143 });
144 expect(described.CertificateAuthority?.Status).toBe("PENDING_CERTIFICATE");
145 expect(
146 described.CertificateAuthority?.CertificateAuthorityConfiguration?.Subject?.CommonName,
147 ).toBe("alchemy-test.internal");
148 expect(described.CertificateAuthority?.UsageMode).toBe("SHORT_LIVED_CERTIFICATE");
149
150 const perms = yield* acmpca.listPermissions({
151 CertificateAuthorityArn: first.ca.certificateAuthorityArn,
152 });
153 const acmPerm = perms.Permissions?.find((p) => p.Principal === "acm.amazonaws.com");
154 expect(acmPerm).toBeDefined();
155 expect([...(acmPerm?.Actions ?? [])].sort()).toEqual([
156 "GetCertificate",
157 "IssueCertificate",
158 "ListPermissions",
159 ]);
160
161 // The resource-based policy is attached (out-of-band via distilled).
162 expect(first.policy.certificateAuthorityArn).toBe(first.ca.certificateAuthorityArn);
163 const attachedPolicy = yield* acmpca.getPolicy({
164 ResourceArn: first.ca.certificateAuthorityArn,
165 });
166 expect(attachedPolicy.Policy).toContain("acm-pca:DescribeCertificateAuthority");
167
168 // 2. UPDATE — same CA (stable ARN); tags converge in place. (The
169 // permission's action set cannot shrink: AWS requires all three
170 // actions for the ACM principal — a subset fails with
171 // `ValidationException: Permissions must contain all three actions`.)
172 const second = yield* deployCa({
173 commonName: "alchemy-test.internal",
174 tags: { fixture: "acmpca", updated: "true" },
175 });
176 expect(second.ca.certificateAuthorityArn).toBe(first.ca.certificateAuthorityArn);
177 const tags = yield* acmpca.listTags({
178 CertificateAuthorityArn: second.ca.certificateAuthorityArn,
179 });
180 expect(tags.Tags?.find((t) => t.Key === "updated")?.Value).toBe("true");
181
182 // 3. REPLACE — changing the subject replaces the CA; the permission
183 // follows the new ARN and the old CA is deleted. The new ARN is an
184 // unresolved Output at plan time, and the policy must still plan as a
185 // replace (not an in-place update that leaves it on the old CA).
186 const replaced = {
187 commonName: "alchemy-test-replaced.internal",
188 tags: { fixture: "acmpca" },
189 };
190 const replacePlan = yield* stack.plan(caStack(replaced));
191 expect(replacePlan.resources["TestCA"]).toMatchObject({ action: "replace" });
192 expect(replacePlan.resources["SharePolicy"]).toMatchObject({ action: "replace" });
193 const third = yield* deployCa(replaced);
194 expect(third.ca.certificateAuthorityArn).not.toBe(first.ca.certificateAuthorityArn);
195 expect(third.permission.certificateAuthorityArn).toBe(third.ca.certificateAuthorityArn);
196 // The policy is replaced onto the new CA ...
197 expect(third.policy.certificateAuthorityArn).toBe(third.ca.certificateAuthorityArn);
198 const movedPolicy = yield* acmpca.getPolicy({
199 ResourceArn: third.ca.certificateAuthorityArn,
200 });
201 expect(movedPolicy.Policy).toContain(third.ca.certificateAuthorityArn);
202 yield* assertCaDeleted(first.ca.certificateAuthorityArn);
203 // ... and detached from the old one before it was deleted, so the RAM
204 // share the policy spawned does not outlive it in the restore window.
205 const oldShares = yield* ram
206 .getResourceShareAssociations({
207 associationType: "RESOURCE",
208 resourceArn: first.ca.certificateAuthorityArn,
209 associationStatus: "ASSOCIATED",
210 })
211 .pipe(
212 Effect.map((r) => r.resourceShareAssociations ?? []),
213 Effect.repeat({
214 schedule: Schedule.spaced("3 seconds"),
215 until: (associations) => associations.length === 0,
216 times: 10,
217 }),
218 );
219 expect(oldShares).toEqual([]);
220
221 // 4. DESTROY — CA enters its restoration window (DELETED).
222 yield* stack.destroy();
223 yield* assertCaDeleted(third.ca.certificateAuthorityArn);
224 }),
225 { tags: ["provider:aws", "provider:aws:acmpca", "live"], timeout: 300_000 },
226 );