@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, 06:00 UTC

main
ts 178 lines 6.33 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 { LoadBalancer } from "./LoadBalancer.ts";
5 import type { TargetGroup } from "./TargetGroup.ts";
6 import type { TrustStore } from "./TrustStore.ts";
7
8 /**
9 * Shared scaffolding for ELBv2 HTTP bindings.
10 *
11 * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a
12 * thin `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the two
13 * builders below. Everything except the operation and the IAM action list is
14 * boilerplate: the runtime callable injects the bound resource's ARN
15 * (`TargetGroupArn` / `LoadBalancerArn`) into the request and the deploy-time
16 * half grants `actions` on that ARN — or on `*` for `Describe*` actions,
17 * which do not support resource-level permissions in ELBv2.
18 *
19 * Hosts: Lambda Functions plus self-registering compute (ECS Tasks and EC2
20 * Instances registering themselves into a target group at boot).
21 */
22
23 const BindHost = Effect.gen(function* () {
24 const bind = yield* AWSBindings.Bind;
25 return Effect.fn(function* (
26 tag: string,
27 actions: readonly string[],
28 resource: TargetGroup | LoadBalancer | TrustStore,
29 iamResource: Output.Output<string> | "*",
30 ) {
31 if (!globalThis.__ALCHEMY_RUNTIME__) {
32 yield* bind`${tag}(${resource})`({
33 policyStatements: [
34 {
35 Effect: "Allow",
36 Action: [...actions],
37 Resource: [iamResource === "*" ? "*" : iamResource],
38 },
39 ],
40 });
41 }
42 });
43 });
44
45 /**
46 * Build the impl Effect for a target-group-addressed operation: the runtime
47 * callable injects the bound {@link TargetGroup}'s ARN as `TargetGroupArn`;
48 * the deploy-time half grants `actions` on the target-group ARN
49 * (`"target-group"`, the default) or on `*` (`Describe*` actions do not
50 * support resource-level permissions).
51 */
52 export const makeTargetGroupHttpBinding = <
53 I extends { TargetGroupArn?: string },
54 A,
55 E,
56 R,
57 >(options: {
58 /** Fully-qualified binding tag, e.g. `AWS.ELBv2.RegisterTargets`. */
59 tag: string;
60 /** The distilled operation; `TargetGroupArn` is injected from the group. */
61 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
62 /** IAM actions granted by the binding. */
63 actions: readonly string[];
64 /**
65 * IAM resource scope. Write actions support resource-level permissions on
66 * the target-group ARN (`"target-group"`, the default); `Describe*`
67 * actions do not (`"*"`).
68 */
69 resource?: "target-group" | "*";
70 }) =>
71 Effect.gen(function* () {
72 const bindHost = yield* BindHost;
73 const op = yield* AWSBindings.withCredentials(options.operation);
74
75 return Effect.fn(function* (targetGroup: TargetGroup) {
76 const TargetGroupArn = yield* targetGroup.targetGroupArn;
77 yield* bindHost(
78 options.tag,
79 options.actions,
80 targetGroup,
81 options.resource === "*" ? "*" : Output.interpolate`${targetGroup.targetGroupArn}`,
82 );
83 return Effect.fn(`${options.tag}(${targetGroup.LogicalId})`)(function* (
84 request: Omit<I, "TargetGroupArn">,
85 ) {
86 return yield* op({
87 ...request,
88 TargetGroupArn: yield* TargetGroupArn,
89 } as I);
90 });
91 });
92 });
93
94 /**
95 * Build the impl Effect for a load-balancer-addressed operation: the runtime
96 * callable injects the bound {@link LoadBalancer}'s ARN as `LoadBalancerArn`;
97 * the deploy-time half grants `actions` on the load-balancer ARN
98 * (`"load-balancer"`, the default) or on `*` (`Describe*` actions do not
99 * support resource-level permissions).
100 */
101 export const makeLoadBalancerHttpBinding = <
102 I extends { LoadBalancerArn?: string },
103 A,
104 E,
105 R,
106 >(options: {
107 /** Fully-qualified binding tag, e.g. `AWS.ELBv2.ModifyCapacityReservation`. */
108 tag: string;
109 /** The distilled operation; `LoadBalancerArn` is injected from the LB. */
110 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
111 /** IAM actions granted by the binding. */
112 actions: readonly string[];
113 /**
114 * IAM resource scope. Write actions support resource-level permissions on
115 * the load-balancer ARN (`"load-balancer"`, the default); `Describe*`
116 * actions do not (`"*"`).
117 */
118 resource?: "load-balancer" | "*";
119 }) =>
120 Effect.gen(function* () {
121 const bindHost = yield* BindHost;
122 const op = yield* AWSBindings.withCredentials(options.operation);
123
124 return Effect.fn(function* (loadBalancer: LoadBalancer) {
125 const LoadBalancerArn = yield* loadBalancer.loadBalancerArn;
126 yield* bindHost(
127 options.tag,
128 options.actions,
129 loadBalancer,
130 options.resource === "*" ? "*" : Output.interpolate`${loadBalancer.loadBalancerArn}`,
131 );
132 return Effect.fn(`${options.tag}(${loadBalancer.LogicalId})`)(function* (
133 request?: Omit<I, "LoadBalancerArn">,
134 ) {
135 return yield* op({
136 ...request,
137 LoadBalancerArn: yield* LoadBalancerArn,
138 } as I);
139 });
140 });
141 });
142
143 /**
144 * Build the impl Effect for a trust-store-addressed operation: the runtime
145 * callable injects the bound {@link TrustStore}'s ARN as `TrustStoreArn`;
146 * the deploy-time half grants `actions` on the trust-store ARN (mTLS
147 * `GetTrustStore*` reads support resource-level permissions).
148 */
149 export const makeTrustStoreHttpBinding = <I extends { TrustStoreArn?: string }, A, E, R>(options: {
150 /** Fully-qualified binding tag, e.g. `AWS.ELBv2.GetTrustStoreCaCertificatesBundle`. */
151 tag: string;
152 /** The distilled operation; `TrustStoreArn` is injected from the store. */
153 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
154 /** IAM actions granted on the trust-store ARN. */
155 actions: readonly string[];
156 }) =>
157 Effect.gen(function* () {
158 const bindHost = yield* BindHost;
159 const op = yield* AWSBindings.withCredentials(options.operation);
160
161 return Effect.fn(function* (trustStore: TrustStore) {
162 const TrustStoreArn = yield* trustStore.trustStoreArn;
163 yield* bindHost(
164 options.tag,
165 options.actions,
166 trustStore,
167 Output.interpolate`${trustStore.trustStoreArn}`,
168 );
169 return Effect.fn(`${options.tag}(${trustStore.LogicalId})`)(function* (
170 request?: Omit<I, "TrustStoreArn">,
171 ) {
172 return yield* op({
173 ...request,
174 TrustStoreArn: yield* TrustStoreArn,
175 } as I);
176 });
177 });
178 });