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

main
ts 177 lines 6.72 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Bucket } from "@alchemy.run/aws/S3";
3 import { AccessPoint, ObjectLambdaAccessPoint } 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 Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10 import {
11 ObjectTransformFunction,
12 ObjectTransformFunctionLive,
13 } from "./fixtures/transform-handler.ts";
14
15 const { test } = Test.make({ providers: AWS.providers() });
16
17 const ACCOUNT_ID = "391965393224";
18
19 const findObjectLambdaAccessPoint = (name: string) =>
20 s3control
21 .getAccessPointForObjectLambda({ AccountId: ACCOUNT_ID, Name: name })
22 .pipe(Effect.catchTag("NoSuchAccessPoint", () => Effect.succeed(undefined)));
23
24 class ObjectLambdaAccessPointStillExists extends Data.TaggedError(
25 "ObjectLambdaAccessPointStillExists",
26 )<{ readonly name: string }> {}
27
28 const assertObjectLambdaAccessPointDeleted = (name: string) =>
29 findObjectLambdaAccessPoint(name).pipe(
30 Effect.flatMap((olap) =>
31 olap === undefined
32 ? Effect.void
33 : Effect.fail(new ObjectLambdaAccessPointStillExists({ name })),
34 ),
35 Effect.retry({
36 while: (e) => e._tag === "ObjectLambdaAccessPointStillExists",
37 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]),
38 }),
39 );
40
41 test.provider(
42 "typed NoSuchAccessPoint tag on a nonexistent object lambda access point",
43 () =>
44 Effect.gen(function* () {
45 const result = yield* s3control
46 .getAccessPointForObjectLambda({
47 AccountId: ACCOUNT_ID,
48 Name: "alchemy-does-not-exist-xyz",
49 })
50 .pipe(
51 Effect.map(() => "found" as const),
52 Effect.catchTag("NoSuchAccessPoint", () => Effect.succeed("missing" as const)),
53 );
54 expect(result).toBe("missing");
55 }),
56 { tags: ["provider:aws", "provider:aws:s3control", "live"], timeout: 60_000 },
57 );
58
59 // S3 Object Lambda is entitlement-gated: "Amazon S3 Object Lambda is
60 // available only to existing customers that are currently using the service
61 // as well as to select AWS Partner Network (APN) partners." (AccessDenied on
62 // CreateAccessPointForObjectLambda, surfaced as the typed
63 // ObjectLambdaNotAvailable tag). This probe pins the typed tag; an entitled
64 // account instead fails with NoSuchAccessPoint for the bogus supporting AP.
65 test.provider(
66 "typed ObjectLambdaNotAvailable tag on a non-entitled account",
67 () =>
68 Effect.gen(function* () {
69 const result = yield* s3control
70 .createAccessPointForObjectLambda({
71 AccountId: ACCOUNT_ID,
72 Name: "alchemy-olap-entitlement-probe",
73 Configuration: {
74 SupportingAccessPoint: `arn:aws:s3:us-west-2:${ACCOUNT_ID}:accesspoint/alchemy-does-not-exist-xyz`,
75 TransformationConfigurations: [
76 {
77 Actions: ["GetObject"],
78 ContentTransformation: {
79 AwsLambda: {
80 FunctionArn: `arn:aws:lambda:us-west-2:${ACCOUNT_ID}:function:does-not-exist`,
81 },
82 },
83 },
84 ],
85 },
86 })
87 .pipe(
88 Effect.map(() => "created" as const),
89 Effect.catchTag("ObjectLambdaNotAvailable", () => Effect.succeed("gated" as const)),
90 Effect.catchTag("NoSuchAccessPoint", () =>
91 Effect.succeed("missing-supporting-ap" as const),
92 ),
93 );
94 // this account is not entitled; an entitled account would reach the
95 // supporting-access-point validation instead
96 expect(["gated", "missing-supporting-ap"]).toContain(result);
97 // never actually created
98 expect(result).not.toBe("created");
99 }),
100 { tags: ["provider:aws", "provider:aws:s3control", "live"], timeout: 60_000 },
101 );
102
103 // Full lifecycle requires an account entitled to S3 Object Lambda (the
104 // service is closed to new customers) — gate it so entitled accounts can
105 // still run it unchanged: AWS_TEST_OBJECT_LAMBDA=1.
106 test.provider.skipIf(!process.env.AWS_TEST_OBJECT_LAMBDA)(
107 "create, update configuration, delete object lambda access point",
108 (stack) =>
109 Effect.gen(function* () {
110 yield* stack.destroy();
111
112 const makeStack = (cloudWatchMetricsEnabled: boolean) =>
113 Effect.gen(function* () {
114 const bucket = yield* Bucket("OlapBucket", { forceDestroy: true });
115 const accessPoint = yield* AccessPoint("OlapSupportingAp", {
116 bucket: bucket.bucketName,
117 });
118 const transformer = yield* ObjectTransformFunction.pipe(
119 Effect.provide(ObjectTransformFunctionLive),
120 );
121 const olap = yield* ObjectLambdaAccessPoint("TestOlap", {
122 supportingAccessPoint: accessPoint.accessPointArn,
123 cloudWatchMetricsEnabled,
124 transformationConfigurations: [
125 {
126 Actions: ["GetObject"],
127 ContentTransformation: {
128 AwsLambda: { FunctionArn: transformer.functionArn },
129 },
130 },
131 ],
132 });
133 return { accessPoint, olap };
134 });
135
136 const deployed = yield* stack.deploy(makeStack(false));
137
138 expect(deployed.olap.objectLambdaAccessPointName).toBeDefined();
139 expect(deployed.olap.objectLambdaAccessPointArn).toContain(":s3-object-lambda:");
140
141 // out-of-band verification via distilled
142 const live = yield* findObjectLambdaAccessPoint(deployed.olap.objectLambdaAccessPointName);
143 expect(live).toBeDefined();
144
145 const config = yield* s3control.getAccessPointConfigurationForObjectLambda({
146 AccountId: ACCOUNT_ID,
147 Name: deployed.olap.objectLambdaAccessPointName,
148 });
149 expect(config.Configuration?.SupportingAccessPoint).toBe(deployed.accessPoint.accessPointArn);
150 expect(config.Configuration?.CloudWatchMetricsEnabled ?? false).toBe(false);
151
152 // update the mutable configuration in place — same access point
153 const updated = yield* stack.deploy(makeStack(true));
154 expect(updated.olap.objectLambdaAccessPointName).toBe(
155 deployed.olap.objectLambdaAccessPointName,
156 );
157
158 const updatedConfig = yield* s3control.getAccessPointConfigurationForObjectLambda({
159 AccountId: ACCOUNT_ID,
160 Name: deployed.olap.objectLambdaAccessPointName,
161 });
162 expect(updatedConfig.Configuration?.CloudWatchMetricsEnabled).toBe(true);
163
164 yield* stack.destroy();
165 yield* assertObjectLambdaAccessPointDeleted(deployed.olap.objectLambdaAccessPointName);
166 }),
167 {
168 tags: [
169 "provider:aws",
170 "provider:aws:lambda",
171 "provider:aws:s3",
172 "provider:aws:s3control",
173 "live",
174 ],
175 timeout: 240_000,
176 },
177 );