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 { Assessment } from "./Assessment.ts"; |
| 5 | |
| 6 | /** |
| 7 | * Shared scaffolding for Audit Manager 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 assessment-scoped operations) the injected `assessmentId` is |
| 13 | * boilerplate. |
| 14 | */ |
| 15 | |
| 16 | /** |
| 17 | * Build the impl Effect for an assessment-scoped operation: the runtime |
| 18 | * callable injects the bound {@link Assessment}'s id as `assessmentId` and |
| 19 | * the deploy-time half grants `actions` on the assessment ARN. |
| 20 | */ |
| 21 | export const makeAssessmentScopedHttpBinding = < |
| 22 | I extends { assessmentId: string }, |
| 23 | A, |
| 24 | E, |
| 25 | R, |
| 26 | >(options: { |
| 27 | /** Fully-qualified binding tag, e.g. `AWS.AuditManager.GetEvidence`. */ |
| 28 | tag: string; |
| 29 | /** The distilled operation; `assessmentId` is injected from the resource. */ |
| 30 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 31 | /** IAM actions granted on the assessment ARN. */ |
| 32 | actions: readonly string[]; |
| 33 | }) => |
| 34 | Effect.gen(function* () { |
| 35 | const bind = yield* AWSBindings.Bind; |
| 36 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 37 | |
| 38 | return Effect.fn(function* (assessment: Assessment) { |
| 39 | const AssessmentId = yield* assessment.assessmentId; |
| 40 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 41 | yield* bind`${options.tag}(${assessment})`({ |
| 42 | policyStatements: [ |
| 43 | { |
| 44 | Effect: "Allow", |
| 45 | Action: [...options.actions], |
| 46 | Resource: [Output.interpolate`${assessment.arn}`], |
| 47 | }, |
| 48 | ], |
| 49 | }); |
| 50 | } |
| 51 | return Effect.fn(`${options.tag}(${assessment.LogicalId})`)(function* ( |
| 52 | request?: Omit<I, "assessmentId">, |
| 53 | ) { |
| 54 | return yield* op({ |
| 55 | ...request, |
| 56 | assessmentId: yield* AssessmentId, |
| 57 | } as I); |
| 58 | }); |
| 59 | }); |
| 60 | }); |
| 61 | |
| 62 | /** |
| 63 | * Build the impl Effect for an account-level operation (no target assessment |
| 64 | * — registration status, account-wide insights, report listings, presigned |
| 65 | * evidence-upload URLs). The deploy-time half grants `actions` on `*` |
| 66 | * because these IAM actions do not support resource-level scoping. |
| 67 | */ |
| 68 | export const makeAuditManagerAccountHttpBinding = <I, A, E, R>(options: { |
| 69 | /** Fully-qualified binding tag, e.g. `AWS.AuditManager.GetInsights`. */ |
| 70 | tag: string; |
| 71 | /** The distilled operation, invoked with the caller's request as-is. */ |
| 72 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 73 | /** IAM actions granted on `*`. */ |
| 74 | actions: readonly string[]; |
| 75 | }) => |
| 76 | Effect.gen(function* () { |
| 77 | const bind = yield* AWSBindings.Bind; |
| 78 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 79 | |
| 80 | return Effect.fn(function* () { |
| 81 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 82 | yield* bind`${options.tag}()`({ |
| 83 | policyStatements: [ |
| 84 | { |
| 85 | Effect: "Allow", |
| 86 | Action: [...options.actions], |
| 87 | Resource: ["*"], |
| 88 | }, |
| 89 | ], |
| 90 | }); |
| 91 | } |
| 92 | return Effect.fn(options.tag)(function* (request?: I) { |
| 93 | return yield* op((request ?? {}) as I); |
| 94 | }); |
| 95 | }); |
| 96 | }); |