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

main
ts 201 lines 7.04 KB
Raw
1 import { Bucket } from "@alchemy.run/neon/Bucket";
2 import { Credential } from "@alchemy.run/neon/Credential";
3 import { Project } from "@alchemy.run/neon/Project";
4 import { providers } from "@alchemy.run/neon/Providers";
5 import { storageLayer } from "@alchemy.run/neon/Storage";
6 import { expect } from "@alchemy.run/test";
7 import * as Test from "@alchemy.run/test/Alchemy";
8 import * as S3 from "@distilled.cloud/aws/s3";
9 import * as SDK from "@distilled.cloud/neon";
10 import * as Effect from "effect/Effect";
11 import * as HttpClient from "effect/http/HttpClient";
12 import * as Redacted from "effect/Redacted";
13 import * as Result from "effect/Result";
14 import * as Schedule from "effect/Schedule";
15 import { signStorageRead } from "./fixtures/StorageNative.ts";
16
17 const { test } = Test.make({ providers: providers() });
18
19 test.provider(
20 "read scopes authorize S3 and write-only rejection is typed",
21 (stack) =>
22 Effect.gen(function* () {
23 yield* stack.destroy();
24 const { read, write, both } = yield* stack.deploy(
25 Effect.gen(function* () {
26 const project = yield* Project("StorageCredentialProject", {
27 region: "aws-us-east-2",
28 });
29 const read = yield* Credential("Read", {
30 project,
31 scopes: ["storage:read"],
32 });
33 const write = yield* Credential("Write", {
34 project,
35 scopes: ["storage:write"],
36 });
37 const both = yield* Credential("Both", {
38 project,
39 scopes: ["storage:read", "storage:write"],
40 });
41 return { read, write, both };
42 }),
43 );
44 const storage = yield* SDK.getProjectBranchStorage({
45 project_id: read.projectId,
46 branch_id: read.branchId,
47 });
48 for (const credential of [read, write, both]) {
49 const response = yield* S3.listBuckets({}).pipe(
50 Effect.provide(
51 storageLayer({
52 endpoint: storage.s3_endpoint,
53 region: storage.region,
54 accessKeyId: credential.tokenId,
55 secretAccessKey: credential.s3SecretAccessKey,
56 }),
57 ),
58 Effect.retry({
59 while: (error) => error._tag === "AccessDeniedException",
60 schedule: Schedule.spaced("500 millis"),
61 times: 8,
62 }),
63 Effect.result,
64 );
65 const signed = yield* Effect.tryPromise(() =>
66 signStorageRead({
67 endpoint: storage.s3_endpoint,
68 region: storage.region,
69 accessKeyId: credential.tokenId,
70 secretAccessKey: Redacted.value(credential.s3SecretAccessKey),
71 }),
72 );
73 const native = yield* (yield* HttpClient.HttpClient).get(signed.url, {
74 headers: signed.headers,
75 });
76 yield* Effect.log(
77 `Neon scope ${credential.scopes.join(",")}: distilled=${Result.isSuccess(response) ? "ok" : response.failure._tag}; native=${native.status}`,
78 );
79 if (credential === write) {
80 expect(native.status).toBe(403);
81 expect(Result.isFailure(response)).toBe(true);
82 if (Result.isFailure(response))
83 expect(response.failure._tag).toBe("AccessDeniedException");
84 } else {
85 expect(native.status).toBe(200);
86 expect(Result.isSuccess(response)).toBe(true);
87 }
88 }
89 yield* stack.destroy();
90 yield* stack.destroy();
91 }),
92 {
93 tags: [
94 "provider:aws",
95 "provider:aws:s3",
96 "provider:neon",
97 "provider:neon:credential",
98 "provider:neon:project",
99 "live",
100 ],
101 timeout: 120_000,
102 },
103 );
104
105 // Neon's docs state storage:write includes all read operations, but the data
106 // plane enforces write-only strictly: Put/Delete succeed while GetObject,
107 // HeadObject, ListObjectsV2, and ListBuckets all return AccessDenied
108 // (measured 2026-09-20; reported upstream). Managed write clients therefore
109 // request storage:read alongside storage:write. This probe pins the observed
110 // contract — it fails if Neon ships the documented implied read, at which
111 // point write clients can drop the extra read scope.
112 test.provider(
113 "storage:write-only is enforced strictly write-only (documented implied read is absent)",
114 (stack) =>
115 Effect.gen(function* () {
116 yield* stack.destroy();
117 const { bucket, credential } = yield* stack.deploy(
118 Effect.gen(function* () {
119 const project = yield* Project("WriteImpliesReadProject", {
120 region: "aws-us-east-2",
121 });
122 return {
123 bucket: yield* Bucket("Probe", {
124 project,
125 name: "write-only-probe",
126 }),
127 credential: yield* Credential("Writer", {
128 project,
129 scopes: ["storage:write"],
130 }),
131 };
132 }),
133 );
134 const storage = yield* SDK.getProjectBranchStorage({
135 project_id: credential.projectId,
136 branch_id: credential.branchId,
137 });
138 const layer = storageLayer({
139 endpoint: storage.s3_endpoint,
140 region: storage.region,
141 accessKeyId: credential.tokenId,
142 secretAccessKey: credential.s3SecretAccessKey,
143 });
144 const outcome = <A, E>(op: Effect.Effect<A, E, any>) =>
145 op.pipe(
146 Effect.provide(layer),
147 Effect.result,
148 Effect.map((result) =>
149 Result.isSuccess(result)
150 ? "ok"
151 : ((result.failure as { _tag: string })._tag ?? "unknown"),
152 ),
153 );
154 const matrix = {
155 PutObject: yield* outcome(
156 S3.putObject({
157 Bucket: bucket.bucketName,
158 Key: "probe.txt",
159 Body: new TextEncoder().encode("probe"),
160 }),
161 ),
162 GetObject: yield* outcome(S3.getObject({ Bucket: bucket.bucketName, Key: "probe.txt" })),
163 HeadObject: yield* outcome(S3.headObject({ Bucket: bucket.bucketName, Key: "probe.txt" })),
164 ListObjectsV2: yield* outcome(S3.listObjectsV2({ Bucket: bucket.bucketName })),
165 ListBuckets: yield* outcome(S3.listBuckets({})),
166 DeleteObject: yield* outcome(
167 S3.deleteObject({ Bucket: bucket.bucketName, Key: "probe.txt" }),
168 ),
169 };
170 yield* Effect.log(`Neon storage:write-only matrix: ${JSON.stringify(matrix)}`);
171 yield* stack.destroy();
172 expect(
173 yield* SDK.getProject({ project_id: credential.projectId }).pipe(
174 Effect.as(false),
175 Effect.catchTag("NotFound", () => Effect.succeed(true)),
176 ),
177 ).toBe(true);
178 yield* Effect.log("Write-only storage fixture project is absent");
179 yield* stack.destroy();
180 expect(matrix).toEqual({
181 PutObject: "ok",
182 GetObject: "AccessDeniedException",
183 HeadObject: "AccessDeniedException",
184 ListObjectsV2: "AccessDeniedException",
185 ListBuckets: "AccessDeniedException",
186 DeleteObject: "ok",
187 });
188 }),
189 {
190 tags: [
191 "provider:aws",
192 "provider:aws:s3",
193 "provider:neon",
194 "provider:neon:bucket",
195 "provider:neon:credential",
196 "provider:neon:project",
197 "live",
198 ],
199 timeout: 120_000,
200 },
201 );