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

main
ts 139 lines 5.43 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 { AWSEnvironment } from "../Environment.ts";
5 import type { Instance } from "./Instance.ts";
6
7 /**
8 * Shared scaffolding for the IAM Identity Center runtime bindings.
9 *
10 * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is
11 * a `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the two builders
12 * below. Everything except the operation and the IAM action list is
13 * boilerplate:
14 *
15 * - {@link makeIdentityStoreHttpBinding} — Identity Store data-plane
16 * operations (`identitystore:*` actions: user CRUD, group lookups,
17 * membership management). The runtime callable injects the bound
18 * {@link Instance}'s `IdentityStoreId`; the deploy-time half grants
19 * `actions` on the identity store ARN plus the region-less
20 * `user/*`/`group/*`/`membership/*` sub-resource ARNs the actions require.
21 * - {@link makeIdentityCenterInstanceHttpBinding} — `sso:*` admin reads
22 * scoped to the bound {@link Instance} (account-assignment audit,
23 * permission set reads). The runtime callable injects the instance's
24 * `InstanceArn`; the deploy-time half grants `actions` on the instance ARN
25 * plus the region-less `permissionSet/*` and `account/*` ARNs.
26 */
27
28 /**
29 * Build the impl Effect for an Identity Store data-plane operation scoped to
30 * one {@link Instance}: the deploy-time half grants `actions` on the
31 * identity store ARN (`arn:aws:identitystore::{account}:identitystore/{id}`)
32 * and the `user/*`/`group/*`/`membership/*` sub-resources, and the runtime
33 * half injects the instance's `IdentityStoreId` into every request.
34 */
35 export const makeIdentityStoreHttpBinding = <
36 I extends { IdentityStoreId?: string },
37 A,
38 E,
39 R,
40 >(options: {
41 /** Fully-qualified binding tag, e.g. `AWS.IdentityCenter.DescribeUser`. */
42 tag: string;
43 /** The distilled operation; `IdentityStoreId` is injected from the instance. */
44 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
45 /** IAM actions granted on the identity store + sub-resource ARNs. */
46 actions: readonly string[];
47 }) =>
48 Effect.gen(function* () {
49 const bind = yield* AWSBindings.Bind;
50 const op = yield* AWSBindings.withCredentials(options.operation);
51
52 return Effect.fn(function* (instance: Instance) {
53 const IdentityStoreId = yield* instance.identityStoreId;
54 if (!globalThis.__ALCHEMY_RUNTIME__) {
55 const { accountId } = yield* AWSEnvironment.current as unknown as Effect.Effect<{
56 accountId: string;
57 }>;
58 yield* bind`${options.tag}(${instance})`({
59 policyStatements: [
60 {
61 Effect: "Allow",
62 Action: [...options.actions],
63 Resource: [
64 Output.interpolate`arn:aws:identitystore::${accountId}:identitystore/${instance.identityStoreId}`,
65 // Identity Store sub-resources are region-less and
66 // account-less; the concrete user/group/membership ids
67 // are chosen per request at runtime.
68 "arn:aws:identitystore:::user/*",
69 "arn:aws:identitystore:::group/*",
70 "arn:aws:identitystore:::membership/*",
71 ],
72 },
73 ],
74 });
75 }
76 return Effect.fn(`${options.tag}(${instance.LogicalId})`)(function* (
77 request?: Omit<I, "IdentityStoreId">,
78 ) {
79 return yield* op({
80 ...request,
81 IdentityStoreId: yield* IdentityStoreId,
82 } as I);
83 });
84 });
85 });
86
87 /**
88 * Build the impl Effect for an `sso:*` admin operation scoped to one
89 * {@link Instance}: the deploy-time half grants `actions` on the instance
90 * ARN plus the region-less `permissionSet/*` and `account/*` ARNs, and the
91 * runtime half injects the instance's `InstanceArn` into every request.
92 */
93 export const makeIdentityCenterInstanceHttpBinding = <
94 I extends { InstanceArn?: string },
95 A,
96 E,
97 R,
98 >(options: {
99 /** Fully-qualified binding tag, e.g. `AWS.IdentityCenter.ListPermissionSets`. */
100 tag: string;
101 /** The distilled operation; `InstanceArn` is injected from the instance. */
102 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
103 /** IAM actions granted on the instance / permission set / account ARNs. */
104 actions: readonly string[];
105 }) =>
106 Effect.gen(function* () {
107 const bind = yield* AWSBindings.Bind;
108 const op = yield* AWSBindings.withCredentials(options.operation);
109
110 return Effect.fn(function* (instance: Instance) {
111 const InstanceArn = yield* instance.instanceArn;
112 if (!globalThis.__ALCHEMY_RUNTIME__) {
113 yield* bind`${options.tag}(${instance})`({
114 policyStatements: [
115 {
116 Effect: "Allow",
117 Action: [...options.actions],
118 Resource: [
119 Output.interpolate`${instance.instanceArn}`,
120 // Permission sets and target accounts are chosen per
121 // request at runtime; their ARNs are region-less and
122 // account-less.
123 "arn:aws:sso:::permissionSet/*",
124 "arn:aws:sso:::account/*",
125 ],
126 },
127 ],
128 });
129 }
130 return Effect.fn(`${options.tag}(${instance.LogicalId})`)(function* (
131 request?: Omit<I, "InstanceArn">,
132 ) {
133 return yield* op({
134 ...request,
135 InstanceArn: yield* InstanceArn,
136 } as I);
137 });
138 });
139 });