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 { PolicyStatement } from "../IAM/Policy.ts"; |
| 5 | import type { SigningProfile } from "./SigningProfile.ts"; |
| 6 | |
| 7 | /** |
| 8 | * Shared scaffolding for the AWS Signer HTTP runtime bindings. |
| 9 | * |
| 10 | * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a |
| 11 | * thin `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the two |
| 12 | * builders below. Everything except the operation and the IAM action list is |
| 13 | * boilerplate. |
| 14 | * |
| 15 | * Signer authorizes profile-addressed actions against the *signing profile* |
| 16 | * ARN (`arn:…:/signing-profiles/name`) and its version-qualified variant |
| 17 | * (`arn:…:/signing-profiles/name/version`), so the profile-scoped builder |
| 18 | * grants on both. Job-addressed actions (describe/list/revoke a signing job) |
| 19 | * target job ids chosen per request at runtime, so those bindings are |
| 20 | * account-level and grant on `Resource: ["*"]`. |
| 21 | */ |
| 22 | const profilePolicyStatement = (profile: SigningProfile, actions: readonly string[]) => ({ |
| 23 | Effect: "Allow" as const, |
| 24 | Action: [...actions], |
| 25 | Resource: [ |
| 26 | Output.interpolate`${profile.arn}`, |
| 27 | // The version-qualified profile ARN (`…/signing-profiles/name/version`). |
| 28 | Output.map(profile.arn, (arn) => `${arn}/*`), |
| 29 | ], |
| 30 | }); |
| 31 | |
| 32 | /** |
| 33 | * Build the impl Effect for an operation whose input carries a `profileName` |
| 34 | * field: the runtime callable injects the bound {@link SigningProfile}'s name |
| 35 | * and the deploy-time half grants `actions` on the profile ARN (and its |
| 36 | * version-qualified pattern). |
| 37 | */ |
| 38 | export const makeSignerProfileHttpBinding = <I extends { profileName?: string }, A, E, R>(options: { |
| 39 | /** Fully-qualified binding tag, e.g. `AWS.Signer.StartSigningJob`. */ |
| 40 | tag: string; |
| 41 | /** The distilled operation; `profileName` is injected from the profile. */ |
| 42 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 43 | /** IAM actions granted on the profile ARN + version pattern. */ |
| 44 | actions: readonly string[]; |
| 45 | }) => |
| 46 | Effect.gen(function* () { |
| 47 | const bind = yield* AWSBindings.Bind; |
| 48 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 49 | |
| 50 | return Effect.fn(function* (profile: SigningProfile) { |
| 51 | // Outputs yield a DEFERRED effect — resolve again per invocation below. |
| 52 | const ProfileName = yield* profile.profileName; |
| 53 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 54 | yield* bind`${options.tag}(${profile})`({ |
| 55 | policyStatements: [profilePolicyStatement(profile, options.actions)], |
| 56 | }); |
| 57 | } |
| 58 | return Effect.fn(`${options.tag}(${profile.LogicalId})`)(function* ( |
| 59 | request: Omit<I, "profileName">, |
| 60 | ) { |
| 61 | return yield* op({ |
| 62 | ...request, |
| 63 | profileName: yield* ProfileName, |
| 64 | } as I); |
| 65 | }); |
| 66 | }); |
| 67 | }); |
| 68 | |
| 69 | /** |
| 70 | * Build the impl Effect for an account-level Signer operation (signing jobs |
| 71 | * are addressed by runtime-chosen job ids; platforms are AWS-managed read-only |
| 72 | * catalog entries): the binding takes no resource argument and the deploy-time |
| 73 | * half grants `actions` on `Resource: ["*"]`. |
| 74 | */ |
| 75 | export const makeSignerHttpBinding = <I, A, E, R>(options: { |
| 76 | /** Fully-qualified binding tag, e.g. `AWS.Signer.DescribeSigningJob`. */ |
| 77 | tag: string; |
| 78 | /** The distilled operation, invoked with the caller's request as-is. */ |
| 79 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 80 | /** |
| 81 | * IAM actions granted on `Resource: ["*"]` (the target jobs/platforms are |
| 82 | * chosen per request at runtime and unknowable at deploy time). |
| 83 | */ |
| 84 | actions: readonly string[]; |
| 85 | }) => |
| 86 | Effect.gen(function* () { |
| 87 | const bind = yield* AWSBindings.Bind; |
| 88 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 89 | |
| 90 | return Effect.fn(function* () { |
| 91 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 92 | const policyStatements: PolicyStatement[] = [ |
| 93 | { |
| 94 | Effect: "Allow", |
| 95 | Action: [...options.actions], |
| 96 | Resource: ["*"], |
| 97 | }, |
| 98 | ]; |
| 99 | yield* bind`${options.tag}()`({ |
| 100 | policyStatements, |
| 101 | }); |
| 102 | } |
| 103 | return Effect.fn(options.tag)(function* (request?: I) { |
| 104 | return yield* op((request ?? {}) as I); |
| 105 | }); |
| 106 | }); |
| 107 | }); |