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 { BackupVault } from "./BackupVault.ts"; |
| 5 | |
| 6 | /** |
| 7 | * Shared scaffolding for AWS Backup HTTP bindings. |
| 8 | * |
| 9 | * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a |
| 10 | * thin `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the two |
| 11 | * builders below. Everything except the operation, the IAM action list, and |
| 12 | * (for vault-scoped operations) the injected `BackupVaultName` is |
| 13 | * boilerplate. The three `Start*Job` bindings inject an IAM role and a |
| 14 | * PassRole grant, so they stay bespoke. |
| 15 | */ |
| 16 | |
| 17 | /** |
| 18 | * Build the impl Effect for an account-level operation (job monitoring, |
| 19 | * protected-resource discovery, restore orchestration). The deploy-time half |
| 20 | * grants `actions` on `*` — backup job / restore job / copy job IAM actions |
| 21 | * do not support resource-level scoping. |
| 22 | */ |
| 23 | export const makeBackupAccountHttpBinding = <I, A, E, R>(options: { |
| 24 | /** Fully-qualified binding tag, e.g. `AWS.Backup.ListBackupJobs`. */ |
| 25 | tag: string; |
| 26 | /** The distilled operation, invoked with the caller's request as-is. */ |
| 27 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 28 | /** IAM actions granted on `*`. */ |
| 29 | actions: readonly string[]; |
| 30 | }) => |
| 31 | Effect.gen(function* () { |
| 32 | const bind = yield* AWSBindings.Bind; |
| 33 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 34 | |
| 35 | return Effect.fn(function* () { |
| 36 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 37 | yield* bind`${options.tag}()`({ |
| 38 | policyStatements: [ |
| 39 | { |
| 40 | Effect: "Allow", |
| 41 | Action: [...options.actions], |
| 42 | Resource: ["*"], |
| 43 | }, |
| 44 | ], |
| 45 | }); |
| 46 | } |
| 47 | return Effect.fn(options.tag)(function* (request?: I) { |
| 48 | return yield* op((request ?? {}) as I); |
| 49 | }); |
| 50 | }); |
| 51 | }); |
| 52 | |
| 53 | /** |
| 54 | * Build the impl Effect for a vault-scoped operation: the runtime callable |
| 55 | * injects the bound {@link BackupVault}'s name as `BackupVaultName` and the |
| 56 | * deploy-time half grants `actions` on the vault ARN (or `*` for |
| 57 | * recovery-point actions, which authorize on the recovery point's underlying |
| 58 | * resource ARN — an EBS/RDS snapshot ARN — and cannot be scoped to the |
| 59 | * vault). |
| 60 | */ |
| 61 | export const makeBackupVaultHttpBinding = < |
| 62 | I extends { BackupVaultName: string }, |
| 63 | A, |
| 64 | E, |
| 65 | R, |
| 66 | >(options: { |
| 67 | /** Fully-qualified binding tag, e.g. `AWS.Backup.DescribeRecoveryPoint`. */ |
| 68 | tag: string; |
| 69 | /** The distilled operation; `BackupVaultName` is injected from the vault. */ |
| 70 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 71 | /** IAM actions granted on the vault ARN (or `*` — see `wildcardIam`). */ |
| 72 | actions: readonly string[]; |
| 73 | /** |
| 74 | * Grant on `*` instead of the vault ARN. Recovery-point actions authorize |
| 75 | * on the recovery point ARN (the underlying resource's snapshot ARN), |
| 76 | * which is unknowable at deploy time. |
| 77 | */ |
| 78 | wildcardIam?: boolean; |
| 79 | }) => |
| 80 | Effect.gen(function* () { |
| 81 | const bind = yield* AWSBindings.Bind; |
| 82 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 83 | |
| 84 | return Effect.fn(function* (vault: BackupVault) { |
| 85 | const BackupVaultName = yield* vault.backupVaultName; |
| 86 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 87 | yield* bind`${options.tag}(${vault})`({ |
| 88 | policyStatements: [ |
| 89 | { |
| 90 | Effect: "Allow", |
| 91 | Action: [...options.actions], |
| 92 | Resource: options.wildcardIam ? ["*"] : [Output.interpolate`${vault.backupVaultArn}`], |
| 93 | }, |
| 94 | ], |
| 95 | }); |
| 96 | } |
| 97 | return Effect.fn(`${options.tag}(${vault.LogicalId})`)(function* ( |
| 98 | request?: Omit<I, "BackupVaultName">, |
| 99 | ) { |
| 100 | return yield* op({ |
| 101 | ...request, |
| 102 | BackupVaultName: yield* BackupVaultName, |
| 103 | } as I); |
| 104 | }); |
| 105 | }); |
| 106 | }); |