The best way to control Claude Code, Codex and more from any computer or phone

main
ts 133 lines 4.77 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 { groupArnGlob, topicArnGlob, transactionalIdArnGlob } from "./BindingHttp.ts";
5 import { connectEnvPrefix } from "./Connect.ts";
6 import type { ServerlessCluster } from "./ServerlessCluster.ts";
7
8 /**
9 * Shared scaffolding for the MSK connect bindings.
10 *
11 * NOT exported from `index.ts` — `ConnectReadHttp.ts` / `ConnectWriteHttp.ts`
12 * / `ConnectReadWriteHttp.ts` are thin
13 * `Layer.effect(Cap, makeKafkaConnectHttpBinding({ … }))` calls over the
14 * builder below. Only the IAM action sets differ per access level.
15 */
16
17 /** Cluster-level actions every MSK IAM-auth client needs. */
18 export const KAFKA_CONNECT_ACTIONS = [
19 "kafka-cluster:Connect",
20 "kafka-cluster:DescribeCluster",
21 "kafka-cluster:DescribeClusterDynamicConfiguration",
22 ] as const;
23
24 /** Topic-level consumer actions. */
25 export const KAFKA_READ_TOPIC_ACTIONS = [
26 "kafka-cluster:DescribeTopic",
27 "kafka-cluster:ReadData",
28 ] as const;
29
30 /** Consumer-group actions (join a group, commit offsets). */
31 export const KAFKA_READ_GROUP_ACTIONS = [
32 "kafka-cluster:DescribeGroup",
33 "kafka-cluster:AlterGroup",
34 ] as const;
35
36 /** Topic-level producer actions. */
37 export const KAFKA_WRITE_TOPIC_ACTIONS = [
38 "kafka-cluster:DescribeTopic",
39 "kafka-cluster:WriteData",
40 ] as const;
41
42 /** Cluster-level producer actions (idempotent producers). */
43 export const KAFKA_WRITE_CLUSTER_ACTIONS = ["kafka-cluster:WriteDataIdempotently"] as const;
44
45 /** Transactional-id actions (transactional producers). */
46 export const KAFKA_WRITE_TRANSACTION_ACTIONS = [
47 "kafka-cluster:DescribeTransactionalId",
48 "kafka-cluster:AlterTransactionalId",
49 ] as const;
50
51 /**
52 * Build the impl Effect for a connect binding. At deploy time it grants the
53 * given action sets on the cluster / topic / group / transactional-id ARN
54 * namespaces and publishes the SASL/IAM bootstrap endpoint as
55 * `MSK_{LOGICAL_ID}_{BROKERS,ARN}` environment variables on the host
56 * Function; at runtime it resolves the same values into a typed connection
57 * descriptor.
58 */
59 export const makeKafkaConnectHttpBinding = (options: {
60 /** Fully-qualified binding tag, e.g. `AWS.Kafka.ConnectReadWrite`. */
61 tag: string;
62 /** IAM actions granted on the cluster ARN. */
63 clusterActions: readonly string[];
64 /** IAM actions granted on the cluster's `topic/…/*` ARNs. */
65 topicActions: readonly string[];
66 /** IAM actions granted on the cluster's `group/…/*` ARNs. */
67 groupActions?: readonly string[];
68 /** IAM actions granted on the cluster's `transactional-id/…/*` ARNs. */
69 transactionActions?: readonly string[];
70 }) =>
71 Effect.gen(function* () {
72 const bind = yield* AWSBindings.Bind;
73 return Effect.fn(function* (cluster: ServerlessCluster) {
74 const BootstrapServers = yield* cluster.bootstrapBrokerStringSaslIam;
75 const ClusterArn = yield* cluster.clusterArn;
76
77 if (!globalThis.__ALCHEMY_RUNTIME__) {
78 const prefix = connectEnvPrefix(cluster.LogicalId);
79 yield* bind`${options.tag}(${cluster})`({
80 env: {
81 [`${prefix}_BROKERS`]: cluster.bootstrapBrokerStringSaslIam,
82 [`${prefix}_ARN`]: cluster.clusterArn,
83 },
84 policyStatements: [
85 {
86 Effect: "Allow",
87 Action: [...options.clusterActions],
88 Resource: [Output.interpolate`${cluster.clusterArn}`],
89 },
90 {
91 Effect: "Allow",
92 Action: [...options.topicActions],
93 Resource: [cluster.clusterArn.pipe(Output.map(topicArnGlob))],
94 },
95 ...(options.groupActions?.length
96 ? [
97 {
98 Effect: "Allow" as const,
99 Action: [...options.groupActions],
100 Resource: [cluster.clusterArn.pipe(Output.map(groupArnGlob))],
101 },
102 ]
103 : []),
104 ...(options.transactionActions?.length
105 ? [
106 {
107 Effect: "Allow" as const,
108 Action: [...options.transactionActions],
109 Resource: [cluster.clusterArn.pipe(Output.map(transactionalIdArnGlob))],
110 },
111 ]
112 : []),
113 ],
114 });
115 }
116
117 return Effect.gen(function* () {
118 const bootstrapServers = yield* BootstrapServers;
119 const clusterArn = yield* ClusterArn;
120 if (!bootstrapServers) {
121 return yield* Effect.die(
122 `MSK SASL/IAM bootstrap brokers for '${cluster.LogicalId}' are not available yet`,
123 );
124 }
125 return {
126 bootstrapServers,
127 brokers: bootstrapServers.split(","),
128 clusterArn,
129 authentication: "iam" as const,
130 };
131 });
132 });
133 });