The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Lambda from "@alchemy.run/aws/Lambda"; |
| 2 | import * as RAM from "@alchemy.run/aws/RAM"; |
| 3 | import * as Duration from "effect/Duration"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 6 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 7 | import * as Layer from "effect/Layer"; |
| 8 | import path from "pathe"; |
| 9 | |
| 10 | const main = path.resolve(import.meta.dirname, "handler.ts"); |
| 11 | |
| 12 | /** |
| 13 | * Fabricate a well-formed same-account invitation ARN that cannot exist — |
| 14 | * drives the typed not-found path for the invitation bindings, proving the |
| 15 | * grant reaches the API (an IAM gap would surface AccessDeniedException |
| 16 | * instead). |
| 17 | */ |
| 18 | const fakeInvitationArn = (shareArn: string) => { |
| 19 | const [, , , region, accountId] = shareArn.split(":"); |
| 20 | return `arn:aws:ram:${region}:${accountId}:resource-share-invitation/00000000-0000-4000-8000-000000000000`; |
| 21 | }; |
| 22 | |
| 23 | /** A well-formed same-account subnet ARN that cannot exist. */ |
| 24 | const fakeSubnetArn = (shareArn: string) => { |
| 25 | const [, , , region, accountId] = shareArn.split(":"); |
| 26 | return `arn:aws:ec2:${region}:${accountId}:subnet/subnet-00000000000000000`; |
| 27 | }; |
| 28 | |
| 29 | export class RAMTestFunction extends Lambda.Function<Lambda.Function>()("RAMTestFunction") {} |
| 30 | |
| 31 | export default RAMTestFunction.make( |
| 32 | { |
| 33 | main, |
| 34 | functionUrl: true, |
| 35 | timeout: Duration.seconds(30), |
| 36 | }, |
| 37 | Effect.gen(function* () { |
| 38 | // A resource share owned by the fixture — guarantees that |
| 39 | // GetResourceShares(SELF) is non-empty and provides the region/account |
| 40 | // for the fabricated not-found ARNs. |
| 41 | yield* RAM.ResourceShare("BindingsShare", { |
| 42 | allowExternalPrincipals: true, |
| 43 | }); |
| 44 | |
| 45 | const acceptInvitation = yield* RAM.AcceptResourceShareInvitation(); |
| 46 | const rejectInvitation = yield* RAM.RejectResourceShareInvitation(); |
| 47 | const getInvitations = yield* RAM.GetResourceShareInvitations(); |
| 48 | const listPendingResources = yield* RAM.ListPendingInvitationResources(); |
| 49 | const getResourceShares = yield* RAM.GetResourceShares(); |
| 50 | const getAssociations = yield* RAM.GetResourceShareAssociations(); |
| 51 | const listResources = yield* RAM.ListResources(); |
| 52 | const listPrincipals = yield* RAM.ListPrincipals(); |
| 53 | const getResourcePolicies = yield* RAM.GetResourcePolicies(); |
| 54 | const getPermission = yield* RAM.GetPermission(); |
| 55 | const listPermissions = yield* RAM.ListPermissions(); |
| 56 | |
| 57 | const bound = { |
| 58 | acceptInvitation, |
| 59 | rejectInvitation, |
| 60 | getInvitations, |
| 61 | listPendingResources, |
| 62 | getResourceShares, |
| 63 | getAssociations, |
| 64 | listResources, |
| 65 | listPrincipals, |
| 66 | getResourcePolicies, |
| 67 | getPermission, |
| 68 | listPermissions, |
| 69 | }; |
| 70 | |
| 71 | // The fixture's own share doubles as the region/account source for |
| 72 | // fabricated ARNs. |
| 73 | const firstShareArn = getResourceShares({ resourceOwner: "SELF" }).pipe( |
| 74 | Effect.map((r) => r.resourceShares?.[0]?.resourceShareArn), |
| 75 | ); |
| 76 | |
| 77 | return { |
| 78 | fetch: Effect.gen(function* () { |
| 79 | const request = yield* HttpServerRequest; |
| 80 | const url = new URL(request.originalUrl); |
| 81 | const pathname = url.pathname; |
| 82 | |
| 83 | if (request.method === "GET" && pathname === "/bindings") { |
| 84 | return yield* HttpServerResponse.json({ |
| 85 | bound: Object.keys(bound), |
| 86 | }); |
| 87 | } |
| 88 | |
| 89 | if (request.method === "GET" && pathname === "/shares") { |
| 90 | const result = yield* getResourceShares({ resourceOwner: "SELF" }); |
| 91 | return yield* HttpServerResponse.json({ |
| 92 | count: (result.resourceShares ?? []).length, |
| 93 | }); |
| 94 | } |
| 95 | |
| 96 | if (request.method === "GET" && pathname === "/associations") { |
| 97 | const shareArn = yield* firstShareArn; |
| 98 | if (shareArn === undefined) { |
| 99 | return yield* HttpServerResponse.json({ tag: "NoShares" }); |
| 100 | } |
| 101 | const result = yield* getAssociations({ |
| 102 | associationType: "PRINCIPAL", |
| 103 | resourceShareArns: [shareArn], |
| 104 | }); |
| 105 | return yield* HttpServerResponse.json({ |
| 106 | tag: "Ok", |
| 107 | count: (result.resourceShareAssociations ?? []).length, |
| 108 | }); |
| 109 | } |
| 110 | |
| 111 | if (request.method === "GET" && pathname === "/invitations") { |
| 112 | const result = yield* getInvitations(); |
| 113 | return yield* HttpServerResponse.json({ |
| 114 | count: (result.resourceShareInvitations ?? []).length, |
| 115 | }); |
| 116 | } |
| 117 | |
| 118 | if (request.method === "GET" && pathname === "/accept-nonexistent") { |
| 119 | const shareArn = yield* firstShareArn; |
| 120 | if (shareArn === undefined) { |
| 121 | return yield* HttpServerResponse.json({ tag: "NoShares" }); |
| 122 | } |
| 123 | // Typed not-found path — proves the AcceptResourceShareInvitation |
| 124 | // grant reaches the API. |
| 125 | const result = yield* acceptInvitation({ |
| 126 | resourceShareInvitationArn: fakeInvitationArn(shareArn), |
| 127 | }).pipe( |
| 128 | Effect.map(() => "Ok"), |
| 129 | Effect.catchTag( |
| 130 | ["ResourceShareInvitationArnNotFoundException", "MalformedArnException"], |
| 131 | (e) => Effect.succeed(e._tag), |
| 132 | ), |
| 133 | ); |
| 134 | return yield* HttpServerResponse.json({ tag: result }); |
| 135 | } |
| 136 | |
| 137 | if (request.method === "GET" && pathname === "/reject-nonexistent") { |
| 138 | const shareArn = yield* firstShareArn; |
| 139 | if (shareArn === undefined) { |
| 140 | return yield* HttpServerResponse.json({ tag: "NoShares" }); |
| 141 | } |
| 142 | const result = yield* rejectInvitation({ |
| 143 | resourceShareInvitationArn: fakeInvitationArn(shareArn), |
| 144 | }).pipe( |
| 145 | Effect.map(() => "Ok"), |
| 146 | Effect.catchTag( |
| 147 | ["ResourceShareInvitationArnNotFoundException", "MalformedArnException"], |
| 148 | (e) => Effect.succeed(e._tag), |
| 149 | ), |
| 150 | ); |
| 151 | return yield* HttpServerResponse.json({ tag: result }); |
| 152 | } |
| 153 | |
| 154 | if (request.method === "GET" && pathname === "/pending-resources-nonexistent") { |
| 155 | const shareArn = yield* firstShareArn; |
| 156 | if (shareArn === undefined) { |
| 157 | return yield* HttpServerResponse.json({ tag: "NoShares" }); |
| 158 | } |
| 159 | const result = yield* listPendingResources({ |
| 160 | resourceShareInvitationArn: fakeInvitationArn(shareArn), |
| 161 | }).pipe( |
| 162 | Effect.map(() => "Ok"), |
| 163 | Effect.catchTag( |
| 164 | [ |
| 165 | "ResourceShareInvitationArnNotFoundException", |
| 166 | "MalformedArnException", |
| 167 | "InvalidParameterException", |
| 168 | ], |
| 169 | (e) => Effect.succeed(e._tag), |
| 170 | ), |
| 171 | ); |
| 172 | return yield* HttpServerResponse.json({ tag: result }); |
| 173 | } |
| 174 | |
| 175 | if (request.method === "GET" && pathname === "/resources") { |
| 176 | const result = yield* listResources({ resourceOwner: "SELF" }); |
| 177 | return yield* HttpServerResponse.json({ |
| 178 | count: (result.resources ?? []).length, |
| 179 | }); |
| 180 | } |
| 181 | |
| 182 | if (request.method === "GET" && pathname === "/principals") { |
| 183 | const result = yield* listPrincipals({ resourceOwner: "SELF" }); |
| 184 | return yield* HttpServerResponse.json({ |
| 185 | count: (result.principals ?? []).length, |
| 186 | }); |
| 187 | } |
| 188 | |
| 189 | if (request.method === "GET" && pathname === "/resource-policies-nonexistent") { |
| 190 | const shareArn = yield* firstShareArn; |
| 191 | if (shareArn === undefined) { |
| 192 | return yield* HttpServerResponse.json({ tag: "NoShares" }); |
| 193 | } |
| 194 | // A fabricated subnet ARN — either the typed not-found or an empty |
| 195 | // policy list proves the grant. |
| 196 | const result = yield* getResourcePolicies({ |
| 197 | resourceArns: [fakeSubnetArn(shareArn)], |
| 198 | }).pipe( |
| 199 | Effect.map((r) => ({ |
| 200 | tag: "Ok" as string, |
| 201 | count: (r.policies ?? []).length, |
| 202 | })), |
| 203 | Effect.catchTag(["ResourceArnNotFoundException", "MalformedArnException"], (e) => |
| 204 | Effect.succeed({ tag: e._tag as string, count: 0 }), |
| 205 | ), |
| 206 | ); |
| 207 | return yield* HttpServerResponse.json(result); |
| 208 | } |
| 209 | |
| 210 | if (request.method === "GET" && pathname === "/permissions") { |
| 211 | const result = yield* listPermissions(); |
| 212 | return yield* HttpServerResponse.json({ |
| 213 | count: (result.permissions ?? []).length, |
| 214 | }); |
| 215 | } |
| 216 | |
| 217 | if (request.method === "GET" && pathname === "/permission") { |
| 218 | const listed = yield* listPermissions({ maxResults: 5 }); |
| 219 | const first = (listed.permissions ?? [])[0]; |
| 220 | if (first?.arn === undefined) { |
| 221 | return yield* HttpServerResponse.json({ tag: "NoPermissions" }); |
| 222 | } |
| 223 | const result = yield* getPermission({ permissionArn: first.arn }); |
| 224 | return yield* HttpServerResponse.json({ |
| 225 | tag: "Ok", |
| 226 | name: result.permission?.name, |
| 227 | }); |
| 228 | } |
| 229 | |
| 230 | return yield* HttpServerResponse.json( |
| 231 | { error: "Not found", method: request.method, pathname }, |
| 232 | { status: 404 }, |
| 233 | ); |
| 234 | }).pipe(Effect.orDie), |
| 235 | }; |
| 236 | }).pipe( |
| 237 | Effect.provide( |
| 238 | Layer.mergeAll( |
| 239 | Lambda.EventSource, |
| 240 | RAM.AcceptResourceShareInvitationHttp, |
| 241 | RAM.RejectResourceShareInvitationHttp, |
| 242 | RAM.GetResourceShareInvitationsHttp, |
| 243 | RAM.ListPendingInvitationResourcesHttp, |
| 244 | RAM.GetResourceSharesHttp, |
| 245 | RAM.GetResourceShareAssociationsHttp, |
| 246 | RAM.ListResourcesHttp, |
| 247 | RAM.ListPrincipalsHttp, |
| 248 | RAM.GetResourcePoliciesHttp, |
| 249 | RAM.GetPermissionHttp, |
| 250 | RAM.ListPermissionsHttp, |
| 251 | ), |
| 252 | ), |
| 253 | ), |
| 254 | ); |