@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 140 lines 5.01 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 { DataCatalog } from "./DataCatalog.ts";
6 import type { WorkGroup } from "./WorkGroup.ts";
7
8 /**
9 * Shared scaffolding for Athena HTTP bindings.
10 *
11 * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is
12 * a `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the two builders
13 * below. Everything except the operation, the IAM action list, and the
14 * injected identifier is boilerplate.
15 */
16
17 /**
18 * Build the impl Effect for a workgroup-scoped query operation. Athena
19 * authorizes query-execution actions against the workgroup the query ran in,
20 * so the deploy-time half grants `actions` on the bound {@link WorkGroup}'s
21 * ARN. When `injectWorkGroup` is set, the runtime callable also injects the
22 * workgroup's name as the request's `WorkGroup` field (for operations like
23 * `ListQueryExecutions` that scope by workgroup in the request itself).
24 */
25 export const makeWorkGroupScopedHttpBinding = <I, A, E, R>(options: {
26 /** Fully-qualified binding tag, e.g. `AWS.Athena.GetQueryExecution`. */
27 tag: string;
28 /** The distilled operation. */
29 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
30 /** IAM actions granted on the workgroup ARN. */
31 actions: readonly string[];
32 /** Inject the bound workgroup's name as the request `WorkGroup` field. */
33 injectWorkGroup?: boolean;
34 }) =>
35 Effect.gen(function* () {
36 const bind = yield* AWSBindings.Bind;
37 const op = yield* AWSBindings.withCredentials(options.operation);
38
39 return Effect.fn(function* (workGroup: WorkGroup) {
40 const WorkGroupName = yield* workGroup.workGroupName;
41 if (!globalThis.__ALCHEMY_RUNTIME__) {
42 yield* bind`${options.tag}(${workGroup})`({
43 policyStatements: [
44 {
45 Effect: "Allow",
46 Action: [...options.actions],
47 Resource: [workGroup.workGroupArn],
48 },
49 ],
50 });
51 }
52 return Effect.fn(`${options.tag}(${workGroup.LogicalId})`)(function* (
53 request: Omit<I, "WorkGroup">,
54 ) {
55 return yield* op(
56 (options.injectWorkGroup
57 ? { ...request, WorkGroup: yield* WorkGroupName }
58 : request) as I,
59 );
60 });
61 });
62 });
63
64 // The datacatalog ARN is `arn:aws:athena:{region}:{account}:datacatalog/{name}`
65 // — the Glue Data Catalog ARNs share that partition/region/account.
66 const glueArns = (dataCatalogArn: OutputType<string>) => {
67 const base = dataCatalogArn.pipe(
68 Output.map((arn) => {
69 const [, partition, , region, account] = arn.split(":");
70 return `arn:${partition}:glue:${region}:${account}`;
71 }),
72 );
73 return [
74 base.pipe(Output.map((b) => `${b}:catalog`)),
75 base.pipe(Output.map((b) => `${b}:database/*`)),
76 base.pipe(Output.map((b) => `${b}:table/*/*`)),
77 ];
78 };
79
80 /**
81 * Build the impl Effect for a catalog-metadata operation. The runtime callable
82 * injects the bound {@link DataCatalog}'s name as `CatalogName`; the
83 * deploy-time half grants `actions` on the datacatalog ARN plus the Glue
84 * Data Catalog reads Athena performs on the caller's behalf when the catalog
85 * resolves through Glue.
86 */
87 export const makeDataCatalogScopedHttpBinding = <
88 I extends { CatalogName: string },
89 A,
90 E,
91 R,
92 >(options: {
93 /** Fully-qualified binding tag, e.g. `AWS.Athena.GetDatabase`. */
94 tag: string;
95 /** The distilled operation; `CatalogName` is injected from the resource. */
96 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
97 /** IAM actions granted on the datacatalog ARN. */
98 actions: readonly string[];
99 }) =>
100 Effect.gen(function* () {
101 const bind = yield* AWSBindings.Bind;
102 const op = yield* AWSBindings.withCredentials(options.operation);
103
104 return Effect.fn(function* (catalog: DataCatalog) {
105 const CatalogName = yield* catalog.name;
106 if (!globalThis.__ALCHEMY_RUNTIME__) {
107 yield* bind`${options.tag}(${catalog})`({
108 policyStatements: [
109 {
110 Effect: "Allow",
111 Action: [...options.actions],
112 Resource: [catalog.dataCatalogArn],
113 },
114 {
115 // Metadata reads through a GLUE-type catalog federate to the
116 // Glue Data Catalog with the caller's credentials.
117 Effect: "Allow",
118 Action: [
119 "glue:GetDatabase",
120 "glue:GetDatabases",
121 "glue:GetTable",
122 "glue:GetTables",
123 "glue:GetPartition",
124 "glue:GetPartitions",
125 ],
126 Resource: glueArns(catalog.dataCatalogArn),
127 },
128 ],
129 });
130 }
131 return Effect.fn(`${options.tag}(${catalog.LogicalId})`)(function* (
132 request: Omit<I, "CatalogName">,
133 ) {
134 return yield* op({
135 ...request,
136 CatalogName: yield* CatalogName,
137 } as I);
138 });
139 });
140 });