@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 59 lines 2.24 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as AWSBindings from "../AWSBindings.ts";
3 import type { PolicyStatement } from "./Policy.ts";
4
5 /**
6 * Shared HTTP scaffolding for the IAM audit/insight runtime bindings.
7 *
8 * IAM's runtime surface (policy simulation, credential reports, access
9 * advisor, account audit) targets *arbitrary* IAM entities discovered at
10 * runtime — e.g. a compliance Lambda iterating every role in the account —
11 * so every binding is account-level and grants its action(s) on
12 * `Resource: ["*"]` (where AWS scopes the action to an entity, the entity is
13 * chosen per request and unknowable at deploy time). The only variation
14 * between bindings is the distilled operation and the IAM action.
15 *
16 * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is
17 * a `Layer.effect(Cap, makeIamHttpBinding({ … }))`. Genuinely-different
18 * bindings (e.g. `GetAccessKeyLastUsed`, which injects the identifier of a
19 * bound canonical `AccessKey`) stay bespoke.
20 */
21 export const makeIamHttpBinding = <I, A, E, R>(options: {
22 /**
23 * Short capability name used in the binding sid and runtime span, e.g.
24 * `"SimulateCustomPolicy"`.
25 */
26 capability: string;
27 /**
28 * IAM actions granted on `Resource: ["*"]` (the target entities are chosen
29 * per request at runtime and unknowable at deploy time).
30 */
31 iamActions: readonly string[];
32 /**
33 * The distilled operation implementing the capability.
34 */
35 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
36 }) =>
37 Effect.gen(function* () {
38 const bind = yield* AWSBindings.Bind;
39 const op = yield* AWSBindings.withCredentials(options.operation);
40
41 return Effect.fn(function* () {
42 if (!globalThis.__ALCHEMY_RUNTIME__) {
43 const policyStatements: PolicyStatement[] = [
44 {
45 Effect: "Allow",
46 Action: [...options.iamActions],
47 // The target IAM entities are chosen per request at runtime.
48 Resource: ["*"],
49 },
50 ];
51 yield* bind`AWS.IAM.${options.capability}()`({
52 policyStatements,
53 });
54 }
55 return Effect.fn(`AWS.IAM.${options.capability}`)(function* (request?: I) {
56 return yield* op((request ?? {}) as I);
57 });
58 });
59 });