The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as AWSBindings from "../AWSBindings.ts"; |
| 3 | import type { Secret } from "./Secret.ts"; |
| 4 | |
| 5 | /** |
| 6 | * Shared scaffolding for the AWS Secrets Manager HTTP bindings. |
| 7 | * |
| 8 | * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is |
| 9 | * a `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the two builders |
| 10 | * below. Everything except the operation and the IAM action list is |
| 11 | * boilerplate. |
| 12 | */ |
| 13 | |
| 14 | /** |
| 15 | * Build the impl Effect for a Secrets Manager operation scoped to a bound |
| 16 | * {@link Secret}: the deploy-time half grants `actions` on the secret's ARN, |
| 17 | * and the runtime half injects the secret ARN as `SecretId` into every |
| 18 | * request. |
| 19 | */ |
| 20 | export const makeSecretHttpBinding = <I extends { SecretId?: string }, A, E, R>(options: { |
| 21 | /** Fully-qualified binding tag, e.g. `AWS.SecretsManager.GetSecretValue`. */ |
| 22 | tag: string; |
| 23 | /** The distilled operation. */ |
| 24 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 25 | /** IAM actions granted on the bound secret. */ |
| 26 | actions: readonly `secretsmanager:${string}`[]; |
| 27 | }) => |
| 28 | Effect.gen(function* () { |
| 29 | const bind = yield* AWSBindings.Bind; |
| 30 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 31 | |
| 32 | return Effect.fn(function* (secret: Secret) { |
| 33 | const SecretId = yield* secret.secretArn; |
| 34 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 35 | yield* bind`${options.tag}(${secret})`({ |
| 36 | policyStatements: [ |
| 37 | { |
| 38 | Effect: "Allow", |
| 39 | Action: [...options.actions], |
| 40 | Resource: [secret.secretArn], |
| 41 | }, |
| 42 | ], |
| 43 | }); |
| 44 | } |
| 45 | return Effect.fn(`${options.tag}(${secret.LogicalId})`)(function* ( |
| 46 | request?: Omit<I, "SecretId">, |
| 47 | ) { |
| 48 | const secretId = yield* SecretId; |
| 49 | return yield* op({ ...request, SecretId: secretId } as I); |
| 50 | }); |
| 51 | }); |
| 52 | }); |
| 53 | |
| 54 | /** |
| 55 | * Build the impl Effect for an account-level Secrets Manager operation that |
| 56 | * is not scoped to a secret (e.g. `secretsmanager:GetRandomPassword`, |
| 57 | * `secretsmanager:ListSecrets`). The deploy-time half grants `actions` on |
| 58 | * `*`. |
| 59 | */ |
| 60 | export const makeSecretsManagerAccountHttpBinding = <I, A, E, R>(options: { |
| 61 | /** Fully-qualified binding tag, e.g. `AWS.SecretsManager.ListSecrets`. */ |
| 62 | tag: string; |
| 63 | /** The distilled operation, invoked with the caller's request as-is. */ |
| 64 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 65 | /** IAM actions granted on `*`. */ |
| 66 | actions: readonly `secretsmanager:${string}`[]; |
| 67 | }) => |
| 68 | Effect.gen(function* () { |
| 69 | const bind = yield* AWSBindings.Bind; |
| 70 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 71 | |
| 72 | return Effect.fn(function* () { |
| 73 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 74 | yield* bind`${options.tag}()`({ |
| 75 | policyStatements: [ |
| 76 | { |
| 77 | Effect: "Allow", |
| 78 | Action: [...options.actions], |
| 79 | Resource: ["*"], |
| 80 | }, |
| 81 | ], |
| 82 | }); |
| 83 | } |
| 84 | return Effect.fn(options.tag)(function* (request?: I) { |
| 85 | return yield* op((request ?? {}) as I); |
| 86 | }); |
| 87 | }); |
| 88 | }); |