@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 94 lines 3.76 KB
Raw
1 import * as Output from "alchemy/Output";
2 import type { Output as OutputType } from "alchemy/Output";
3 import * as Effect from "effect/Effect";
4 import * as AWSBindings from "../AWSBindings.ts";
5 import type { PolicyStatement } from "../IAM/Policy.ts";
6 import type { Parameter } from "./Parameter.ts";
7
8 /**
9 * Shared scaffolding for AWS SSM Parameter Store HTTP bindings.
10 *
11 * NOT exported from `index.ts` — every single-parameter `{Op}Http.ts` in this
12 * service is a thin `Layer.effect(Cap, makeParameterHttpBinding({ … }))` over
13 * this builder. Everything except the operation, the IAM action list, and the
14 * granted ARNs is boilerplate:
15 *
16 * - the deploy-time half registers `Allow(host, tag(parameter))` with the
17 * requested `actions` on the bound parameter's ARN (or the ARNs produced by
18 * `resources` — e.g. the `${arn}/*` subtree wildcard for
19 * `GetParametersByPath`), plus an optional second statement granting
20 * `kmsActions` on the parameter's encryption key so `SecureString`
21 * decryption (`kms:Decrypt`) or encryption (`kms:Encrypt`,
22 * `kms:GenerateDataKey`) works out of the box;
23 * - the runtime callable injects the resolved parameter name into the request
24 * under `requestKey` (`Name` by default, `Path` for the by-path read).
25 *
26 * Genuinely-different bindings stay bespoke: `GetParameters` (variadic
27 * multi-parameter request with order-preserving name resolution).
28 */
29 export const makeParameterHttpBinding = <I, A, E, R>(options: {
30 /** Fully-qualified binding tag, e.g. `AWS.SSM.GetParameterHistory`. */
31 tag: string;
32 /** The distilled operation the runtime callable invokes. */
33 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
34 /** IAM actions granted on the bound parameter. */
35 actions: readonly string[];
36 /**
37 * The request field the bound parameter's name is injected under.
38 * @default "Name"
39 */
40 requestKey?: "Name" | "Path";
41 /**
42 * KMS actions granted on the parameter's encryption key. For
43 * `String`/`StringList` parameters `keyArn` is undefined and the statement
44 * falls back to the parameter's own ARN, which matches no KMS key — the
45 * statement then grants nothing.
46 */
47 kmsActions?: readonly string[];
48 /** ARNs the SSM actions are granted on. @default the parameter ARN */
49 resources?: (parameter: Parameter) => (string | OutputType<string>)[];
50 }) =>
51 Effect.gen(function* () {
52 const bind = yield* AWSBindings.Bind;
53 const op = yield* AWSBindings.withCredentials(options.operation);
54 const requestKey = options.requestKey ?? "Name";
55
56 return Effect.fn(function* <P extends Parameter>(parameter: P) {
57 const Name = yield* parameter.parameterName;
58 if (!globalThis.__ALCHEMY_RUNTIME__) {
59 const statements: {
60 Effect: PolicyStatement["Effect"];
61 Action: string[];
62 Resource: (string | OutputType<string>)[];
63 }[] = [
64 {
65 Effect: "Allow",
66 Action: [...options.actions],
67 Resource: options.resources?.(parameter) ?? [parameter.parameterArn],
68 },
69 ];
70 if (options.kmsActions !== undefined) {
71 statements.push({
72 Effect: "Allow",
73 Action: [...options.kmsActions],
74 Resource: [
75 Output.all(parameter.parameterArn, parameter.keyArn).pipe(
76 Output.map(([parameterArn, keyArn]) => keyArn ?? parameterArn),
77 ),
78 ],
79 });
80 }
81 yield* bind`${options.tag}(${parameter})`({
82 policyStatements: statements,
83 });
84 }
85 return Effect.fn(`${options.tag}(${parameter.LogicalId})`)(function* (
86 request?: Omit<I, "Name" | "Path">,
87 ) {
88 return yield* op({
89 ...request,
90 [requestKey]: yield* Name,
91 } as I);
92 });
93 });
94 });