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

main
ts 137 lines 4.17 KB
Raw
1 import * as identitystore from "@distilled.cloud/aws/identitystore";
2 import * as ssoAdmin from "@distilled.cloud/aws/sso-admin";
3 import * as Data from "effect/Data";
4 import * as Effect from "effect/Effect";
5 import * as Redacted from "effect/Redacted";
6 import * as Schedule from "effect/Schedule";
7 import * as Stream from "effect/Stream";
8
9 export class IdentityCenterCommonUnexpectedResponse extends Data.TaggedError(
10 "IdentityCenterCommonUnexpectedResponse",
11 )<{
12 readonly message: string;
13 readonly cause?: unknown;
14 }> {}
15
16 export class IdentityCenterCommonInvalidProps extends Data.TaggedError(
17 "IdentityCenterCommonInvalidProps",
18 )<{
19 readonly message: string;
20 readonly cause?: unknown;
21 }> {}
22
23 /**
24 * Unwrap an identity-store sensitive field. Distilled decodes
25 * `smithy.api#sensitive` strings (`DisplayName`, `Description`, `UserName`,
26 * …) into `Redacted.Redacted<string>` at runtime, so any resource-level
27 * comparison or Attributes projection must unwrap first — comparing the
28 * Redacted wrapper against a plain string is always `false` and would make
29 * reconcile loop or persist `<redacted>` wrappers into state.
30 */
31 export const unredact = (
32 value: string | Redacted.Redacted<string> | undefined,
33 ): string | undefined =>
34 value === undefined ? undefined : Redacted.isRedacted(value) ? Redacted.value(value) : value;
35
36 export const retryIdentityCenter = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
37 effect.pipe(
38 Effect.retry({
39 while: (error: any) =>
40 error?._tag === "ConflictException" ||
41 error?._tag === "ThrottlingException" ||
42 error?._tag === "InternalServerException",
43 schedule: Schedule.max([Schedule.exponential(200), Schedule.recurs(8)]),
44 }),
45 );
46
47 export const listInstances = Effect.fn(function* () {
48 return yield* ssoAdmin.listInstances
49 .items({
50 MaxResults: 100,
51 })
52 .pipe(
53 Stream.runCollect,
54 Effect.map((instances) => Array.from(instances) as ssoAdmin.InstanceMetadata[]),
55 );
56 });
57
58 export const resolveInstance = Effect.fn(function* (instanceArn?: string) {
59 const instances = yield* listInstances();
60
61 if (instanceArn) {
62 const selected = instances.find((instance) => instance.InstanceArn === instanceArn);
63 if (!selected?.InstanceArn || !selected.IdentityStoreId) {
64 return yield* new IdentityCenterCommonUnexpectedResponse({
65 message: `Identity Center instance '${instanceArn}' was not found`,
66 });
67 }
68 return selected;
69 }
70
71 if (instances.length === 1) {
72 return instances[0]!;
73 }
74
75 const active = instances.filter((instance) => instance.Status === "ACTIVE");
76 if (active.length === 1) {
77 return active[0]!;
78 }
79
80 return yield* new IdentityCenterCommonInvalidProps({
81 message:
82 "Unable to resolve a single visible Identity Center instance; pass instanceArn explicitly",
83 });
84 });
85
86 export const resolveIdentityStoreId = Effect.fn(function* ({
87 identityStoreId,
88 instanceArn,
89 }: {
90 identityStoreId?: string;
91 instanceArn?: string;
92 }) {
93 if (identityStoreId) {
94 return identityStoreId;
95 }
96
97 const instance = yield* resolveInstance(instanceArn);
98 if (!instance.IdentityStoreId) {
99 return yield* new IdentityCenterCommonUnexpectedResponse({
100 message: `Identity Center instance '${instance.InstanceArn}' is missing an identity store ID`,
101 });
102 }
103
104 return instance.IdentityStoreId;
105 });
106
107 export const toInstanceAttributes = (
108 instance: ssoAdmin.InstanceMetadata,
109 ): {
110 instanceArn: string;
111 identityStoreId: string;
112 ownerAccountId: string | undefined;
113 name: string | undefined;
114 status: string | undefined;
115 statusReason: string | undefined;
116 createdDate: Date | undefined;
117 } => ({
118 instanceArn: instance.InstanceArn ?? "",
119 identityStoreId: instance.IdentityStoreId ?? "",
120 ownerAccountId: instance.OwnerAccountId,
121 name: instance.Name,
122 status: instance.Status,
123 statusReason: instance.StatusReason,
124 createdDate: instance.CreatedDate,
125 });
126
127 export const listGroups = Effect.fn(function* (identityStoreId: string) {
128 return yield* identitystore.listGroups
129 .items({
130 IdentityStoreId: identityStoreId,
131 MaxResults: 100,
132 })
133 .pipe(
134 Stream.runCollect,
135 Effect.map((groups) => Array.from(groups) as identitystore.Group[]),
136 );
137 });