The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Output from "alchemy/Output"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as AWSBindings from "../AWSBindings.ts"; |
| 4 | import type { Server } from "./Server.ts"; |
| 5 | import type { User } from "./User.ts"; |
| 6 | |
| 7 | /** |
| 8 | * Shared scaffolding for the AWS Transfer Family runtime bindings. |
| 9 | * |
| 10 | * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a |
| 11 | * thin `Layer.effect(Cap, makeTransfer…HttpBinding({ … }))` over one of the |
| 12 | * three builders below. Everything except the operation, the IAM action |
| 13 | * list, and the injected identifier(s) is boilerplate. |
| 14 | */ |
| 15 | |
| 16 | /** |
| 17 | * Build the impl Effect for a server-scoped Transfer operation: the runtime |
| 18 | * callable injects the bound {@link Server}'s ID as `ServerId` and the |
| 19 | * deploy-time half grants `actions` on the server's ARN. |
| 20 | */ |
| 21 | export const makeTransferServerHttpBinding = <I extends { ServerId?: string }, A, E, R>(options: { |
| 22 | /** Fully-qualified binding tag, e.g. `AWS.Transfer.StartServer`. */ |
| 23 | tag: string; |
| 24 | /** The distilled operation; `ServerId` is injected from the server. */ |
| 25 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 26 | /** IAM actions granted on the server ARN. */ |
| 27 | actions: readonly string[]; |
| 28 | /** |
| 29 | * Override the IAM resource derived from the server. Most server-scoped |
| 30 | * operations authorize against the server ARN; a few operations use a |
| 31 | * related resource type instead (for example, TestIdentityProvider uses a |
| 32 | * user ARN even though ServerId is the injected request identifier). |
| 33 | */ |
| 34 | resource?: (server: Server) => string | Output.Output<string>; |
| 35 | }) => |
| 36 | Effect.gen(function* () { |
| 37 | const bind = yield* AWSBindings.Bind; |
| 38 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 39 | |
| 40 | return Effect.fn(function* (server: Server) { |
| 41 | // Outputs yield a DEFERRED effect — resolve again per invocation below. |
| 42 | const ServerId = yield* server.serverId; |
| 43 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 44 | yield* bind`${options.tag}(${server})`({ |
| 45 | policyStatements: [ |
| 46 | { |
| 47 | Effect: "Allow", |
| 48 | Action: [...options.actions], |
| 49 | Resource: [options.resource?.(server) ?? Output.interpolate`${server.arn}`], |
| 50 | }, |
| 51 | ], |
| 52 | }); |
| 53 | } |
| 54 | return Effect.fn(`${options.tag}(${server.LogicalId})`)(function* ( |
| 55 | request?: Omit<I, "ServerId">, |
| 56 | ) { |
| 57 | return yield* op({ ...request, ServerId: yield* ServerId } as I); |
| 58 | }); |
| 59 | }); |
| 60 | }); |
| 61 | |
| 62 | /** |
| 63 | * Build the impl Effect for a user-scoped Transfer operation: the runtime |
| 64 | * callable injects the bound {@link User}'s `ServerId` + `UserName` and the |
| 65 | * deploy-time half grants `actions` on the user's ARN |
| 66 | * (`arn:…:user/{serverId}/{userName}`). |
| 67 | */ |
| 68 | export const makeTransferUserHttpBinding = < |
| 69 | I extends { ServerId?: string; UserName?: string }, |
| 70 | A, |
| 71 | E, |
| 72 | R, |
| 73 | >(options: { |
| 74 | /** Fully-qualified binding tag, e.g. `AWS.Transfer.ImportSshPublicKey`. */ |
| 75 | tag: string; |
| 76 | /** The distilled operation; `ServerId` + `UserName` are injected. */ |
| 77 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 78 | /** IAM actions granted on the user ARN. */ |
| 79 | actions: readonly string[]; |
| 80 | }) => |
| 81 | Effect.gen(function* () { |
| 82 | const bind = yield* AWSBindings.Bind; |
| 83 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 84 | |
| 85 | return Effect.fn(function* (user: User) { |
| 86 | // Outputs yield a DEFERRED effect — resolve again per invocation below. |
| 87 | const ServerId = yield* user.serverId; |
| 88 | const UserName = yield* user.userName; |
| 89 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 90 | yield* bind`${options.tag}(${user})`({ |
| 91 | policyStatements: [ |
| 92 | { |
| 93 | Effect: "Allow", |
| 94 | Action: [...options.actions], |
| 95 | Resource: [Output.interpolate`${user.arn}`], |
| 96 | }, |
| 97 | ], |
| 98 | }); |
| 99 | } |
| 100 | return Effect.fn(`${options.tag}(${user.LogicalId})`)(function* ( |
| 101 | request?: Omit<I, "ServerId" | "UserName">, |
| 102 | ) { |
| 103 | return yield* op({ |
| 104 | ...request, |
| 105 | ServerId: yield* ServerId, |
| 106 | UserName: yield* UserName, |
| 107 | } as I); |
| 108 | }); |
| 109 | }); |
| 110 | }); |
| 111 | |
| 112 | /** |
| 113 | * Build the impl Effect for an account-level Transfer operation (no |
| 114 | * resource argument): the deploy-time half grants `actions` on `*`. Used |
| 115 | * for operations that authorize against ARNs unknowable at deploy time |
| 116 | * (e.g. `SendWorkflowStepState` authorizes on the workflow's own ARN, and |
| 117 | * the workflow/execution ids arrive at runtime inside the step event). |
| 118 | */ |
| 119 | export const makeTransferAccountHttpBinding = <I, A, E, R>(options: { |
| 120 | /** Fully-qualified binding tag, e.g. `AWS.Transfer.SendWorkflowStepState`. */ |
| 121 | tag: string; |
| 122 | /** The distilled operation, invoked with the caller's request as-is. */ |
| 123 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 124 | /** IAM actions granted on `*`. */ |
| 125 | actions: readonly string[]; |
| 126 | }) => |
| 127 | Effect.gen(function* () { |
| 128 | const bind = yield* AWSBindings.Bind; |
| 129 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 130 | |
| 131 | return Effect.fn(function* () { |
| 132 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 133 | yield* bind`${options.tag}()`({ |
| 134 | policyStatements: [ |
| 135 | { |
| 136 | Effect: "Allow", |
| 137 | Action: [...options.actions], |
| 138 | Resource: ["*"], |
| 139 | }, |
| 140 | ], |
| 141 | }); |
| 142 | } |
| 143 | return Effect.fn(options.tag)(function* (request: I) { |
| 144 | return yield* op(request); |
| 145 | }); |
| 146 | }); |
| 147 | }); |