The best way to control Claude Code, Codex and more from any computer or phone

main
ts 289 lines 12.1 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Role } from "@alchemy.run/aws/IAM/Role";
3 import { Crl, Profile, TrustAnchor } from "@alchemy.run/aws/RolesAnywhere";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as rolesanywhere from "@distilled.cloud/aws/rolesanywhere";
7 import type * as Duration from "effect/Duration";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10 import { CA1_CERTIFICATE_PEM, CA2_CERTIFICATE_PEM, CRL1_PEM, CRL2_PEM } from "./fixtures/certs.ts";
11
12 const { test } = Test.make({ providers: AWS.providers() });
13
14 // Ungated typed-error probe: prove the distilled error union carries the
15 // not-found tag this provider's read/delete paths depend on.
16 test.provider(
17 "getTrustAnchor on a bogus UUID fails with ResourceNotFoundException",
18 () =>
19 Effect.gen(function* () {
20 const error = yield* Effect.flip(
21 rolesanywhere.getTrustAnchor({ trustAnchorId: "00000000-0000-0000-0000-000000000000" }),
22 );
23 expect(error._tag).toBe("ResourceNotFoundException");
24 }),
25 { tags: ["provider:aws", "provider:aws:rolesanywhere", "live"] },
26 );
27
28 const trustPolicy: AWS.IAM.PolicyDocument = {
29 Version: "2012-10-17",
30 Statement: [
31 {
32 Effect: "Allow" as const,
33 Principal: { Service: "rolesanywhere.amazonaws.com" },
34 Action: ["sts:AssumeRole", "sts:TagSession", "sts:SetSourceIdentity"],
35 },
36 ],
37 };
38
39 const sessionPolicy = JSON.stringify({
40 Version: "2012-10-17",
41 Statement: [{ Effect: "Deny", Action: "s3:DeleteObject", Resource: "*" }],
42 });
43
44 /** Assert a resource is gone, retrying briefly through eventual consistency. */
45 const untilGone = <A, E, R>(
46 get: Effect.Effect<A | undefined, E, R>,
47 ): Effect.Effect<void, E | Error, R> =>
48 Effect.gen(function* () {
49 const found = yield* get;
50 if (found !== undefined) {
51 return yield* Effect.fail(new Error("resource still exists"));
52 }
53 }).pipe(
54 Effect.retry({ schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(8)]) }),
55 );
56
57 test.provider(
58 "trust anchor + profile + crl full lifecycle (create, update, replace, destroy)",
59 (stack) =>
60 Effect.gen(function* () {
61 yield* stack.destroy();
62
63 const stackProgram = (props: {
64 anchorABundle: string;
65 anchorAEnabled: boolean;
66 anchorANotificationThreshold?: Duration.Input;
67 crlData: string;
68 crlEnabled: boolean;
69 crlOnAnchorB: boolean;
70 duration: Duration.Input;
71 sessionPolicy?: string;
72 subjectSpecifiers?: string[];
73 }) =>
74 Effect.gen(function* () {
75 const role = yield* Role("RolesAnywhereRole", { assumeRolePolicyDocument: trustPolicy });
76 const anchorA = yield* TrustAnchor("AnchorA", {
77 certificateBundle: props.anchorABundle,
78 enabled: props.anchorAEnabled,
79 notificationSettings:
80 props.anchorANotificationThreshold === undefined
81 ? undefined
82 : [
83 {
84 event: "CA_CERTIFICATE_EXPIRY",
85 threshold: props.anchorANotificationThreshold,
86 },
87 ],
88 tags: { fixture: "rolesanywhere" },
89 });
90 const anchorB = yield* TrustAnchor("AnchorB", { certificateBundle: CA2_CERTIFICATE_PEM });
91 const profile = yield* Profile("Profile", {
92 roleArns: [role.roleArn],
93 duration: props.duration,
94 sessionPolicy: props.sessionPolicy,
95 attributeMappings:
96 props.subjectSpecifiers === undefined
97 ? undefined
98 : [
99 {
100 certificateField: "x509Subject",
101 mappingRules: props.subjectSpecifiers.map((specifier) => ({ specifier })),
102 },
103 ],
104 tags: { fixture: "rolesanywhere" },
105 });
106 const crl = yield* Crl("Crl", {
107 crlData: props.crlData,
108 trustAnchorArn: props.crlOnAnchorB ? anchorB.trustAnchorArn : anchorA.trustAnchorArn,
109 enabled: props.crlEnabled,
110 tags: { fixture: "rolesanywhere" },
111 });
112 return { role, anchorA, anchorB, profile, crl };
113 });
114
115 // ── Step 1: greenfield create ─────────────────────────────────────
116 const first = yield* stack.deploy(
117 stackProgram({
118 anchorABundle: CA1_CERTIFICATE_PEM,
119 anchorAEnabled: true,
120 anchorANotificationThreshold: "40 days",
121 crlData: CRL1_PEM,
122 crlEnabled: true,
123 crlOnAnchorB: false,
124 duration: "1 hour",
125 subjectSpecifiers: ["CN"],
126 }),
127 );
128
129 expect(first.anchorA.trustAnchorArn).toContain(":trust-anchor/");
130 expect(first.anchorA.enabled).toBe(true);
131 expect(first.profile.profileArn).toContain(":profile/");
132 expect(first.profile.enabled).toBe(true);
133 expect(first.profile.roleArns).toEqual([first.role.roleArn]);
134 expect(first.crl.crlArn).toContain(":crl/");
135 expect(first.crl.trustAnchorArn).toBe(first.anchorA.trustAnchorArn);
136 expect(first.crl.enabled).toBe(true);
137
138 // Out-of-band verification via distilled.
139 const observedAnchor = yield* rolesanywhere.getTrustAnchor({
140 trustAnchorId: first.anchorA.trustAnchorId,
141 });
142 expect(observedAnchor.trustAnchor.source?.sourceType).toBe("CERTIFICATE_BUNDLE");
143 expect(
144 observedAnchor.trustAnchor.source?.sourceData !== undefined &&
145 "x509CertificateData" in observedAnchor.trustAnchor.source.sourceData
146 ? observedAnchor.trustAnchor.source.sourceData.x509CertificateData?.trim()
147 : undefined,
148 ).toBe(CA1_CERTIFICATE_PEM.trim());
149 const observedProfile = yield* rolesanywhere.getProfile({
150 profileId: first.profile.profileId,
151 });
152 expect(observedProfile.profile?.durationSeconds).toBe(3600);
153 expect(observedProfile.profile?.roleArns).toEqual([first.role.roleArn]);
154 // The custom attribute mapping landed (create-time put).
155 const firstSubjectMapping = observedProfile.profile?.attributeMappings?.find(
156 (mapping) => mapping.certificateField === "x509Subject",
157 );
158 expect(firstSubjectMapping?.mappingRules?.map((rule) => rule.specifier)).toEqual(["CN"]);
159 // The custom notification setting landed (create-time settings).
160 const firstNotification = observedAnchor.trustAnchor.notificationSettings?.find(
161 (setting) => setting.event === "CA_CERTIFICATE_EXPIRY",
162 );
163 expect(firstNotification?.threshold).toBe(40);
164
165 // ── Step 2: in-place updates on every mutable aspect ──────────────
166 const second = yield* stack.deploy(
167 stackProgram({
168 anchorABundle: CA2_CERTIFICATE_PEM, // updateTrustAnchor source
169 anchorAEnabled: false, // disableTrustAnchor
170 anchorANotificationThreshold: "30 days", // putNotificationSettings
171 crlData: CRL2_PEM, // updateCrl data
172 crlEnabled: false, // disableCrl
173 crlOnAnchorB: false,
174 duration: "2 hours", // updateProfile
175 sessionPolicy, // updateProfile
176 subjectSpecifiers: ["CN", "OU"], // putAttributeMapping
177 }),
178 );
179
180 // Stable identifiers survived the update.
181 expect(second.anchorA.trustAnchorId).toBe(first.anchorA.trustAnchorId);
182 expect(second.profile.profileId).toBe(first.profile.profileId);
183 expect(second.crl.crlId).toBe(first.crl.crlId);
184 expect(second.anchorA.enabled).toBe(false);
185 expect(second.crl.enabled).toBe(false);
186
187 const updatedAnchor = yield* rolesanywhere.getTrustAnchor({
188 trustAnchorId: second.anchorA.trustAnchorId,
189 });
190 expect(updatedAnchor.trustAnchor.enabled).toBe(false);
191 // putNotificationSettings converged the threshold.
192 const updatedNotification = updatedAnchor.trustAnchor.notificationSettings?.find(
193 (setting) => setting.event === "CA_CERTIFICATE_EXPIRY",
194 );
195 expect(updatedNotification?.threshold).toBe(30);
196 expect(
197 updatedAnchor.trustAnchor.source?.sourceData !== undefined &&
198 "x509CertificateData" in updatedAnchor.trustAnchor.source.sourceData
199 ? updatedAnchor.trustAnchor.source.sourceData.x509CertificateData?.trim()
200 : undefined,
201 ).toBe(CA2_CERTIFICATE_PEM.trim());
202 const updatedProfile = yield* rolesanywhere.getProfile({
203 profileId: second.profile.profileId,
204 });
205 expect(updatedProfile.profile?.durationSeconds).toBe(7200);
206 expect(updatedProfile.profile?.sessionPolicy).toBe(sessionPolicy);
207 // putAttributeMapping converged the mapping rules.
208 const updatedSubjectMapping = updatedProfile.profile?.attributeMappings?.find(
209 (mapping) => mapping.certificateField === "x509Subject",
210 );
211 expect(updatedSubjectMapping?.mappingRules?.map((rule) => rule.specifier).sort()).toEqual([
212 "CN",
213 "OU",
214 ]);
215 const updatedCrl = yield* rolesanywhere.getCrl({ crlId: second.crl.crlId });
216 expect(updatedCrl.crl.enabled).toBe(false);
217 expect(new TextDecoder().decode(updatedCrl.crl.crlData ?? new Uint8Array()).trim()).toBe(
218 CRL2_PEM.trim(),
219 );
220
221 // ── Step 3: moving the CRL to another trust anchor replaces it ────
222 // (both anchors stay deployed across the replacement step). Dropping
223 // the attribute mapping / notification setting exercises
224 // deleteAttributeMapping and resetNotificationSettings.
225 const third = yield* stack.deploy(
226 stackProgram({
227 anchorABundle: CA2_CERTIFICATE_PEM,
228 anchorAEnabled: false,
229 crlData: CRL2_PEM,
230 crlEnabled: false,
231 crlOnAnchorB: true,
232 duration: "2 hours",
233 sessionPolicy,
234 }),
235 );
236
237 expect(third.crl.crlId).not.toBe(second.crl.crlId);
238 expect(third.crl.trustAnchorArn).toBe(third.anchorB.trustAnchorArn);
239
240 // The previously managed attribute mapping was deleted.
241 const finalProfile = yield* rolesanywhere.getProfile({ profileId: third.profile.profileId });
242 const finalSubjectMapping = finalProfile.profile?.attributeMappings?.find(
243 (mapping) => mapping.certificateField === "x509Subject",
244 );
245 expect(finalSubjectMapping?.mappingRules?.map((rule) => rule.specifier) ?? []).not.toContain(
246 "OU",
247 );
248 // The previously managed notification setting was reset to defaults.
249 const finalAnchor = yield* rolesanywhere.getTrustAnchor({
250 trustAnchorId: third.anchorA.trustAnchorId,
251 });
252 const finalNotification = finalAnchor.trustAnchor.notificationSettings?.find(
253 (setting) => setting.event === "CA_CERTIFICATE_EXPIRY",
254 );
255 expect(finalNotification?.threshold).not.toBe(30);
256 // The replaced CRL was deleted.
257 yield* untilGone(
258 rolesanywhere.getCrl({ crlId: second.crl.crlId }).pipe(
259 Effect.map((r) => r.crl),
260 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined)),
261 ),
262 );
263
264 // ── Destroy and verify everything is gone ─────────────────────────
265 yield* stack.destroy();
266 yield* untilGone(
267 rolesanywhere.getTrustAnchor({ trustAnchorId: third.anchorA.trustAnchorId }).pipe(
268 Effect.map((r) => r.trustAnchor),
269 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined)),
270 ),
271 );
272 yield* untilGone(
273 rolesanywhere.getProfile({ profileId: third.profile.profileId }).pipe(
274 Effect.map((r) => r.profile),
275 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined)),
276 ),
277 );
278 yield* untilGone(
279 rolesanywhere.getCrl({ crlId: third.crl.crlId }).pipe(
280 Effect.map((r) => r.crl),
281 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined)),
282 ),
283 );
284 }),
285 {
286 tags: ["provider:aws", "provider:aws:iam", "provider:aws:rolesanywhere", "live"],
287 timeout: 180_000,
288 },
289 );