@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 128 lines 5.12 KB
Raw
1 import * as Output from "alchemy/Output";
2 import * as Effect from "effect/Effect";
3 import * as AWSBindings from "../AWSBindings.ts";
4 import type { Environment } from "./Environment.ts";
5
6 /**
7 * Shared scaffolding for Amazon MWAA HTTP bindings.
8 *
9 * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a
10 * thin `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the builders
11 * below. Everything except the operation and the IAM action list is
12 * boilerplate: every MWAA data-plane operation is scoped to one Airflow
13 * {@link Environment}, whose name is injected as `Name`.
14 *
15 * MWAA distinguishes two IAM resource shapes:
16 *
17 * - Environment-scoped actions (`airflow:CreateCliToken`,
18 * `airflow:GetEnvironment`) are granted on the environment ARN
19 * `arn:{partition}:airflow:{region}:{account}:environment/{name}`.
20 * - Airflow-RBAC-role-scoped actions (`airflow:CreateWebLoginToken`,
21 * `airflow:InvokeRestApi`) are granted on the Airflow role ARN
22 * `arn:{partition}:airflow:{region}:{account}:role/{name}/{airflow-role}` —
23 * MWAA maps the calling IAM identity to that Apache Airflow RBAC role.
24 */
25
26 /**
27 * Options for Airflow-RBAC-role-scoped bindings ({@link Environment} web
28 * login tokens and Airflow REST API invocation).
29 */
30 export interface AirflowRoleOptions {
31 /**
32 * The Apache Airflow RBAC role the IAM grant is scoped to — MWAA maps the
33 * function's identity to this role when it calls the webserver. One of the
34 * default Airflow roles (`Admin`, `Op`, `User`, `Viewer`, `Public`) or a
35 * custom role defined in the environment.
36 * @default "Admin"
37 */
38 airflowRole?: string;
39 }
40
41 /**
42 * Build the impl Effect for an environment-scoped MWAA operation: the runtime
43 * callable injects the bound {@link Environment}'s name as `Name` and the
44 * deploy-time half grants `actions` on the environment ARN.
45 */
46 export const makeMWAAEnvironmentHttpBinding = <I extends { Name: string }, A, E, R>(options: {
47 /** Fully-qualified binding tag, e.g. `AWS.MWAA.CreateCliToken`. */
48 tag: string;
49 /** The distilled operation; `Name` is injected from the environment. */
50 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
51 /** IAM actions granted on the environment ARN. */
52 actions: readonly string[];
53 }) =>
54 Effect.gen(function* () {
55 const bind = yield* AWSBindings.Bind;
56 const op = yield* AWSBindings.withCredentials(options.operation);
57
58 return Effect.fn(function* (environment: Environment) {
59 const Name = yield* environment.environmentName;
60 if (!globalThis.__ALCHEMY_RUNTIME__) {
61 yield* bind`${options.tag}(${environment})`({
62 policyStatements: [
63 {
64 Effect: "Allow",
65 Action: [...options.actions],
66 Resource: [Output.interpolate`${environment.arn}`],
67 },
68 ],
69 });
70 }
71 return Effect.fn(`${options.tag}(${environment.LogicalId})`)(function* (
72 request?: Omit<I, "Name">,
73 ) {
74 return yield* op({ ...request, Name: yield* Name } as I);
75 });
76 });
77 });
78
79 /**
80 * Build the impl Effect for an Airflow-RBAC-role-scoped MWAA operation
81 * (`CreateWebLoginToken`, `InvokeRestApi`): the runtime callable injects the
82 * bound {@link Environment}'s name as `Name` and the deploy-time half grants
83 * `actions` on the Airflow role ARN
84 * `arn:{partition}:airflow:{region}:{account}:role/{name}/{airflowRole}` —
85 * MWAA maps the function's IAM identity to that Apache Airflow RBAC role
86 * (default `Admin`).
87 */
88 export const makeMWAAAirflowRoleHttpBinding = <I extends { Name: string }, A, E, R>(options: {
89 /** Fully-qualified binding tag, e.g. `AWS.MWAA.InvokeRestApi`. */
90 tag: string;
91 /** The distilled operation; `Name` is injected from the environment. */
92 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
93 /** IAM actions granted on the Airflow role ARN. */
94 actions: readonly string[];
95 }) =>
96 Effect.gen(function* () {
97 const bind = yield* AWSBindings.Bind;
98 const op = yield* AWSBindings.withCredentials(options.operation);
99
100 return Effect.fn(function* (environment: Environment, roleOptions?: AirflowRoleOptions) {
101 const Name = yield* environment.environmentName;
102 const airflowRole = roleOptions?.airflowRole ?? "Admin";
103 if (!globalThis.__ALCHEMY_RUNTIME__) {
104 yield* bind`${options.tag}(${environment}, ${airflowRole})`({
105 policyStatements: [
106 {
107 Effect: "Allow",
108 Action: [...options.actions],
109 // The environment ARN is
110 // `arn:{partition}:airflow:{region}:{account}:environment/{name}`;
111 // the Airflow RBAC role ARN swaps the resource type and
112 // appends the role.
113 Resource: [
114 environment.arn.pipe(
115 Output.map((arn) => `${arn.replace(":environment/", ":role/")}/${airflowRole}`),
116 ),
117 ],
118 },
119 ],
120 });
121 }
122 return Effect.fn(`${options.tag}(${environment.LogicalId})`)(function* (
123 request?: Omit<I, "Name">,
124 ) {
125 return yield* op({ ...request, Name: yield* Name } as I);
126 });
127 });
128 });