@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 80 lines 2.98 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as AWSBindings from "../AWSBindings.ts";
3 import { keyLabel, keyPolicyStatement, type KeyLike } from "./KeyBinding.ts";
4
5 /**
6 * Shared scaffolding for the AWS KMS HTTP bindings.
7 *
8 * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is
9 * a `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the two builders
10 * below. Everything except the operation and the IAM action list is
11 * boilerplate.
12 */
13
14 /**
15 * Build the impl Effect for a KMS cryptographic operation scoped to a
16 * {@link KeyLike} target (a `Key` resource or the `alias/...` name of a
17 * pre-existing key): the deploy-time half grants `actions` on the bound key
18 * (exact key ARN, or `Resource: "*"` + `kms:RequestAlias` for an alias), and
19 * the runtime half injects the key identifier into every request.
20 */
21 export const makeKmsKeyHttpBinding = <I extends { KeyId?: string }, A, E, R>(options: {
22 /** Fully-qualified binding tag, e.g. `AWS.KMS.Sign`. */
23 tag: string;
24 /** The distilled operation. */
25 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
26 /** IAM actions granted on the bound key. */
27 actions: readonly `kms:${string}`[];
28 }) =>
29 Effect.gen(function* () {
30 const bind = yield* AWSBindings.Bind;
31 const op = yield* AWSBindings.withCredentials(options.operation);
32
33 return Effect.fn(function* (key: KeyLike) {
34 const KeyId = typeof key === "string" ? Effect.succeed(key) : yield* key.keyId;
35 if (!globalThis.__ALCHEMY_RUNTIME__) {
36 yield* bind`${options.tag}(${key})`({
37 policyStatements: [keyPolicyStatement(options.actions, key)],
38 });
39 }
40 return Effect.fn(`${options.tag}(${keyLabel(key)})`)(function* (request?: Omit<I, "KeyId">) {
41 const keyId = yield* KeyId;
42 return yield* op({ ...request, KeyId: keyId } as I);
43 });
44 });
45 });
46
47 /**
48 * Build the impl Effect for an account-level KMS operation that is not
49 * scoped to a key (e.g. `kms:GenerateRandom`). The deploy-time half grants
50 * `actions` on `*`.
51 */
52 export const makeKmsAccountHttpBinding = <I, A, E, R>(options: {
53 /** Fully-qualified binding tag, e.g. `AWS.KMS.GenerateRandom`. */
54 tag: string;
55 /** The distilled operation, invoked with the caller's request as-is. */
56 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
57 /** IAM actions granted on `*`. */
58 actions: readonly `kms:${string}`[];
59 }) =>
60 Effect.gen(function* () {
61 const bind = yield* AWSBindings.Bind;
62 const op = yield* AWSBindings.withCredentials(options.operation);
63
64 return Effect.fn(function* () {
65 if (!globalThis.__ALCHEMY_RUNTIME__) {
66 yield* bind`${options.tag}()`({
67 policyStatements: [
68 {
69 Effect: "Allow",
70 Action: [...options.actions],
71 Resource: ["*"],
72 },
73 ],
74 });
75 }
76 return Effect.fn(options.tag)(function* (request?: I) {
77 return yield* op((request ?? {}) as I);
78 });
79 });
80 });