@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 202 lines 7.7 KB
Raw
1 import { Region } from "@distilled.cloud/aws/Region";
2 import * as sts from "@distilled.cloud/aws/sts";
3 import * as Effect from "effect/Effect";
4 /**
5 * Shared scaffolding for the Amazon S3 Control HTTP bindings.
6 *
7 * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is
8 * a `Layer.effect(Cap, makeS3Control…HttpBinding({ … }))` over one of the
9 * builders below. Every S3 Control request carries the owning `AccountId`
10 * (an endpoint host label), so each builder's only real job is deciding
11 * where that account id comes from:
12 *
13 * - Access-point-scoped operations inject `AccountId` + `Name` from the
14 * bound {@link AccessPoint} and are granted on the access point ARN.
15 * - Multi-Region Access Point operations inject `AccountId` + `Mrap` from
16 * the bound {@link MultiRegionAccessPoint}, are granted on the MRAP ARN,
17 * and are pinned to `us-west-2` (the region the MRAP control plane and
18 * route APIs are served from).
19 * - Account-level operations (access point listing, S3 Batch Operations
20 * jobs) resolve the caller's account once via `sts:GetCallerIdentity`
21 * (cached per binding) and are granted on `*` — job ARNs are
22 * server-assigned at runtime and unknowable at deploy time.
23 */
24 import * as AWSBindings from "../AWSBindings.ts";
25 import type { PolicyStatement } from "../IAM/Policy.ts";
26 import type { AccessPoint } from "./AccessPoint.ts";
27 import type { MultiRegionAccessPoint } from "./MultiRegionAccessPoint.ts";
28
29 /**
30 * All Multi-Region Access Point control-plane requests are routed to the
31 * US West (Oregon) region regardless of the ambient region — mirrors the
32 * {@link MultiRegionAccessPoint} resource provider.
33 */
34 const inMrapRegion = <A, E, R>(
35 self: Effect.Effect<A, E, R>,
36 ): Effect.Effect<A, E, Exclude<R, Region>> =>
37 self.pipe(Effect.provideService(Region, Effect.succeed("us-west-2")));
38
39 /**
40 * Build the impl Effect for an S3 Control operation scoped to an
41 * {@link AccessPoint}: the deploy-time half grants `actions` on the bound
42 * access point's ARN, and the runtime half injects the access point's
43 * `AccountId` and `Name` into every request.
44 */
45 export const makeS3ControlAccessPointHttpBinding = <
46 I extends { AccountId: string; Name: string },
47 A,
48 E,
49 R,
50 >(options: {
51 /** Fully-qualified binding tag, e.g. `AWS.S3Control.GetAccessPoint`. */
52 tag: string;
53 /** The distilled operation; `AccountId`/`Name` are injected. */
54 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
55 /** IAM actions granted on the access point ARN. */
56 actions: readonly string[];
57 }) =>
58 Effect.gen(function* () {
59 const bind = yield* AWSBindings.Bind;
60 const op = yield* AWSBindings.withCredentials(options.operation);
61
62 return Effect.fn(function* (accessPoint: AccessPoint) {
63 const AccountId = yield* accessPoint.accountId;
64 const Name = yield* accessPoint.accessPointName;
65 if (!globalThis.__ALCHEMY_RUNTIME__) {
66 yield* bind`${options.tag}(${accessPoint})`({
67 policyStatements: [
68 {
69 Effect: "Allow",
70 Action: [...options.actions],
71 Resource: [accessPoint.accessPointArn],
72 },
73 ],
74 });
75 }
76 return Effect.fn(`${options.tag}(${accessPoint.LogicalId})`)(function* (
77 request?: Omit<I, "AccountId" | "Name">,
78 ) {
79 return yield* op({
80 ...request,
81 AccountId: yield* AccountId,
82 Name: yield* Name,
83 } as I);
84 });
85 });
86 });
87
88 /**
89 * Build the impl Effect for an S3 Control operation scoped to a
90 * {@link MultiRegionAccessPoint}: the deploy-time half grants `actions` on
91 * the bound MRAP's ARN, and the runtime half injects the MRAP's `AccountId`
92 * and alias-addressed ARN (`Mrap`) into every request, pinned to the
93 * `us-west-2` MRAP control-plane region.
94 */
95 export const makeS3ControlMrapHttpBinding = <
96 I extends { AccountId: string; Mrap: string },
97 A,
98 E,
99 R,
100 >(options: {
101 /** Fully-qualified binding tag, e.g. `AWS.S3Control.GetMultiRegionAccessPointRoutes`. */
102 tag: string;
103 /** The distilled operation; `AccountId`/`Mrap` are injected. */
104 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
105 /** IAM actions granted on the Multi-Region Access Point ARN. */
106 actions: readonly string[];
107 }) =>
108 Effect.gen(function* () {
109 const bind = yield* AWSBindings.Bind;
110 const op = yield* options.operation.pipe(inMrapRegion, AWSBindings.withCredentials);
111
112 return Effect.fn(function* (mrap: MultiRegionAccessPoint) {
113 const AccountId = yield* mrap.accountId;
114 const Mrap = yield* mrap.multiRegionAccessPointArn;
115 if (!globalThis.__ALCHEMY_RUNTIME__) {
116 yield* bind`${options.tag}(${mrap})`({
117 policyStatements: [
118 {
119 Effect: "Allow",
120 Action: [...options.actions],
121 Resource: [mrap.multiRegionAccessPointArn],
122 },
123 ],
124 });
125 }
126 return Effect.fn(`${options.tag}(${mrap.LogicalId})`)(function* (
127 request?: Omit<I, "AccountId" | "Mrap">,
128 ) {
129 return yield* op({
130 ...request,
131 AccountId: yield* AccountId,
132 Mrap: yield* Mrap,
133 } as I);
134 });
135 });
136 });
137
138 /**
139 * Build the impl Effect for an account-level S3 Control operation (access
140 * point listing, S3 Batch Operations jobs). The deploy-time half grants
141 * `actions` on `*` — job ARNs are server-assigned at runtime and unknowable
142 * at deploy time. The runtime half resolves the caller's account id once via
143 * `sts:GetCallerIdentity` (needs no extra IAM permission) and injects it as
144 * `AccountId`.
145 */
146 export const makeS3ControlAccountHttpBinding = <I extends { AccountId: string }, A, E, R>(options: {
147 /** Fully-qualified binding tag, e.g. `AWS.S3Control.ListJobs`. */
148 tag: string;
149 /** The distilled operation; `AccountId` is injected. */
150 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
151 /** IAM actions granted on `*`. */
152 actions: readonly string[];
153 /**
154 * Grant `iam:PassRole` so the function can hand S3 Batch Operations the
155 * execution role named in the job. Set on `CreateJob`. Matches the AWS
156 * Batch Operations permission model (an `iam:PassedToService` condition is
157 * NOT populated for `s3:CreateJob` — IAM denies a conditioned grant).
158 */
159 passRole?: boolean;
160 }) =>
161 Effect.gen(function* () {
162 const bind = yield* AWSBindings.Bind;
163 const op = yield* AWSBindings.withCredentials(options.operation);
164 const getCallerIdentity = yield* AWSBindings.withCredentials(sts.getCallerIdentity);
165
166 return Effect.fn(function* () {
167 if (!globalThis.__ALCHEMY_RUNTIME__) {
168 const policyStatements: PolicyStatement[] = [
169 {
170 Effect: "Allow",
171 Action: [...options.actions],
172 Resource: ["*"],
173 },
174 ];
175 if (options.passRole) {
176 // No `iam:PassedToService` condition — IAM does not populate the
177 // key for `s3:CreateJob`, so a conditioned grant is always
178 // denied. This matches the policy AWS documents for Batch
179 // Operations job creators.
180 policyStatements.push({
181 Effect: "Allow",
182 Action: ["iam:PassRole"],
183 Resource: ["*"],
184 });
185 }
186 yield* bind`${options.tag}()`({
187 policyStatements,
188 });
189 }
190 // Resolve the caller's account id lazily (first call inside the
191 // Lambda) and cache it for the life of the binding.
192 const accountId = yield* Effect.cached(
193 getCallerIdentity({}).pipe(Effect.map((r) => r.Account!)),
194 );
195 return Effect.fn(options.tag)(function* (request?: Omit<I, "AccountId">) {
196 return yield* op({
197 ...request,
198 AccountId: yield* accountId,
199 } as I);
200 });
201 });
202 });