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

main
ts 136 lines 5.16 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Bucket } from "@alchemy.run/aws/S3";
3 import { AccessPoint, AccessPointPolicy } from "@alchemy.run/aws/S3Control";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as s3control from "@distilled.cloud/aws/s3-control";
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 ACCOUNT_ID = "391965393224";
15
16 const findPolicy = (name: string) =>
17 s3control.getAccessPointPolicy({ AccountId: ACCOUNT_ID, Name: name }).pipe(
18 Effect.map((r) => r.Policy),
19 Effect.catchTag(["NoSuchAccessPointPolicy", "NoSuchAccessPoint"], () =>
20 Effect.succeed(undefined),
21 ),
22 );
23
24 class AccessPointStillExists extends Data.TaggedError("AccessPointStillExists")<{
25 readonly name: string;
26 }> {}
27
28 // The policy's access point (and transitively its bucket) must be destroyed
29 // too — a destroy that only removes the policy would orphan them.
30 const assertAccessPointDeleted = (name: string) =>
31 s3control.getAccessPoint({ AccountId: ACCOUNT_ID, Name: name }).pipe(
32 Effect.flatMap(() => Effect.fail(new AccessPointStillExists({ name }))),
33 Effect.catchTag("NoSuchAccessPoint", () => Effect.void),
34 Effect.retry({
35 while: (e) => e._tag === "AccessPointStillExists",
36 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]),
37 }),
38 );
39
40 test.provider(
41 "create, update, delete access point policy",
42 (stack) =>
43 Effect.gen(function* () {
44 yield* stack.destroy();
45
46 const singleStatementStack = Effect.gen(function* () {
47 const bucket = yield* Bucket("PolicyBucket", { forceDestroy: true });
48 const accessPoint = yield* AccessPoint("PolicyAp", {
49 bucket: bucket.bucketName,
50 });
51 yield* AccessPointPolicy("PolicyApPolicy", {
52 accessPointName: accessPoint.accessPointName,
53 // typed PolicyDocument (not a raw JSON string)
54 policy: {
55 Version: "2012-10-17",
56 Statement: [
57 {
58 Effect: "Allow",
59 Principal: { AWS: `arn:aws:iam::${ACCOUNT_ID}:root` },
60 Action: ["s3:GetObject"],
61 Resource: Output.interpolate`${accessPoint.accessPointArn}/object/*`,
62 },
63 ],
64 },
65 });
66 return { accessPoint };
67 });
68
69 const deployed = yield* stack.deploy(singleStatementStack);
70
71 // out-of-band verification via distilled
72 const policy = yield* findPolicy(deployed.accessPoint.accessPointName);
73 expect(policy).toBeDefined();
74 const parsed = JSON.parse(policy!) as {
75 Statement: { Action: string | string[] }[];
76 };
77 expect(parsed.Statement).toHaveLength(1);
78 // AWS normalizes stored policies (single-element arrays collapse to
79 // plain strings), so compare the normalized form.
80 const actions = parsed.Statement[0].Action;
81 expect(Array.isArray(actions) ? actions : [actions]).toEqual(["s3:GetObject"]);
82
83 // re-deploy the identical PolicyDocument — must be a clean no-op:
84 // the stored policy is byte-for-byte unchanged afterwards.
85 yield* stack.deploy(singleStatementStack);
86 const redeployedPolicy = yield* findPolicy(deployed.accessPoint.accessPointName);
87 expect(redeployedPolicy).toBe(policy);
88
89 // update the policy document in place
90 yield* stack.deploy(
91 Effect.gen(function* () {
92 const bucket = yield* Bucket("PolicyBucket", { forceDestroy: true });
93 const accessPoint = yield* AccessPoint("PolicyAp", {
94 bucket: bucket.bucketName,
95 });
96 yield* AccessPointPolicy("PolicyApPolicy", {
97 accessPointName: accessPoint.accessPointName,
98 policy: {
99 Version: "2012-10-17",
100 Statement: [
101 {
102 Effect: "Allow",
103 Principal: { AWS: `arn:aws:iam::${ACCOUNT_ID}:root` },
104 Action: ["s3:GetObject"],
105 Resource: Output.interpolate`${accessPoint.accessPointArn}/object/*`,
106 },
107 {
108 Effect: "Allow",
109 Principal: { AWS: `arn:aws:iam::${ACCOUNT_ID}:root` },
110 Action: ["s3:ListBucket"],
111 Resource: accessPoint.accessPointArn,
112 },
113 ],
114 },
115 });
116 return { accessPoint };
117 }),
118 );
119
120 const updatedPolicy = yield* findPolicy(deployed.accessPoint.accessPointName);
121 const updatedParsed = JSON.parse(updatedPolicy!) as {
122 Statement: unknown[];
123 };
124 expect(updatedParsed.Statement).toHaveLength(2);
125
126 yield* stack.destroy();
127 // policy (and its access point) are gone after destroy
128 const afterDestroy = yield* findPolicy(deployed.accessPoint.accessPointName);
129 expect(afterDestroy).toBeUndefined();
130 yield* assertAccessPointDeleted(deployed.accessPoint.accessPointName);
131 }),
132 {
133 tags: ["provider:aws", "provider:aws:s3", "provider:aws:s3control", "live"],
134 timeout: 120_000,
135 },
136 );