| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as Binding from "../../Binding.ts"; |
| 3 | import { isBindingHost } from "../Lambda/Function.ts"; |
| 4 | import type { Graph } from "./Graph.ts"; |
| 5 | |
| 6 | /** |
| 7 | * Shared scaffolding for Amazon Detective HTTP bindings. |
| 8 | * |
| 9 | * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is |
| 10 | * a `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the two builders |
| 11 | * below. Everything except the operation and the IAM action list is |
| 12 | * boilerplate. |
| 13 | */ |
| 14 | |
| 15 | /** |
| 16 | * Build the impl Effect for a Detective operation scoped to a behavior |
| 17 | * {@link Graph}: the deploy-time half grants `actions` on the bound graph's |
| 18 | * ARN, and the runtime half injects the graph's `GraphArn` into every |
| 19 | * request. |
| 20 | */ |
| 21 | export const makeDetectiveGraphHttpBinding = < |
| 22 | I extends { GraphArn: string }, |
| 23 | A, |
| 24 | E, |
| 25 | R, |
| 26 | >(options: { |
| 27 | /** Fully-qualified binding tag, e.g. `AWS.Detective.ListMembers`. */ |
| 28 | tag: string; |
| 29 | /** The distilled operation. */ |
| 30 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 31 | /** IAM actions granted on the behavior graph ARN. */ |
| 32 | actions: readonly string[]; |
| 33 | }) => |
| 34 | Effect.gen(function* () { |
| 35 | const op = yield* options.operation; |
| 36 | |
| 37 | return Effect.fn(function* (graph: Graph) { |
| 38 | const GraphArn = yield* graph.graphArn; |
| 39 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 40 | const host = yield* Binding.Host; |
| 41 | if (isBindingHost(host)) { |
| 42 | yield* host.bind`Allow(${host}, ${options.tag}(${graph}))`({ |
| 43 | policyStatements: [ |
| 44 | { |
| 45 | Effect: "Allow", |
| 46 | Action: [...options.actions], |
| 47 | Resource: [graph.graphArn], |
| 48 | }, |
| 49 | ], |
| 50 | }); |
| 51 | } |
| 52 | } |
| 53 | return Effect.fn(`${options.tag}(${graph.LogicalId})`)(function* ( |
| 54 | request?: Omit<I, "GraphArn">, |
| 55 | ) { |
| 56 | const graphArn = yield* GraphArn; |
| 57 | return yield* op({ ...request, GraphArn: graphArn } as I); |
| 58 | }); |
| 59 | }); |
| 60 | }); |
| 61 | |
| 62 | /** |
| 63 | * Build the impl Effect for an account-level Detective operation — the |
| 64 | * member-account invitation flow (`AcceptInvitation`, `RejectInvitation`, |
| 65 | * `DisassociateMembership`, `ListInvitations`, |
| 66 | * `BatchGetMembershipDatasources`) and the organization-admin actions. The |
| 67 | * deploy-time half grants `actions` on `*`: these operations either take no |
| 68 | * resource at all or target a behavior graph owned by a *different* (admin) |
| 69 | * account whose ARN is only known at runtime. |
| 70 | */ |
| 71 | export const makeDetectiveAccountHttpBinding = <I, A, E, R>(options: { |
| 72 | /** Fully-qualified binding tag, e.g. `AWS.Detective.ListInvitations`. */ |
| 73 | tag: string; |
| 74 | /** The distilled operation, invoked with the caller's request as-is. */ |
| 75 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 76 | /** IAM actions granted on `*`. */ |
| 77 | actions: readonly string[]; |
| 78 | }) => |
| 79 | Effect.gen(function* () { |
| 80 | const op = yield* options.operation; |
| 81 | |
| 82 | return Effect.fn(function* () { |
| 83 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 84 | const host = yield* Binding.Host; |
| 85 | if (isBindingHost(host)) { |
| 86 | yield* host.bind`Allow(${host}, ${options.tag}())`({ |
| 87 | policyStatements: [ |
| 88 | { |
| 89 | Effect: "Allow", |
| 90 | Action: [...options.actions], |
| 91 | Resource: ["*"], |
| 92 | }, |
| 93 | ], |
| 94 | }); |
| 95 | } |
| 96 | } |
| 97 | return Effect.fn(options.tag)(function* (request?: I) { |
| 98 | return yield* op((request ?? {}) as I); |
| 99 | }); |
| 100 | }); |
| 101 | }); |