The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as identitystore from "@distilled.cloud/aws/identitystore"; |
| 2 | import { isResolved } from "alchemy/Diff"; |
| 3 | import * as Provider from "alchemy/Provider"; |
| 4 | import { Resource } from "alchemy/Resource"; |
| 5 | import * as Data from "effect/Data"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import type { Providers } from "../Providers.ts"; |
| 8 | import { listGroups, resolveIdentityStoreId, retryIdentityCenter, unredact } from "./common.ts"; |
| 9 | |
| 10 | export class GroupUnexpectedResponse extends Data.TaggedError("GroupUnexpectedResponse")<{ |
| 11 | readonly message: string; |
| 12 | readonly cause?: unknown; |
| 13 | }> {} |
| 14 | |
| 15 | export interface GroupProps { |
| 16 | /** |
| 17 | * Explicit identity store ID. |
| 18 | * If omitted, Alchemy resolves it from the selected Identity Center instance. |
| 19 | */ |
| 20 | identityStoreId?: string; |
| 21 | /** |
| 22 | * Optional instance ARN used to discover the identity store ID. |
| 23 | */ |
| 24 | instanceArn?: string; |
| 25 | /** |
| 26 | * Group display name. |
| 27 | */ |
| 28 | displayName: string; |
| 29 | /** |
| 30 | * Optional group description. |
| 31 | */ |
| 32 | description?: string; |
| 33 | } |
| 34 | |
| 35 | export interface Group extends Resource< |
| 36 | "AWS.IdentityCenter.Group", |
| 37 | GroupProps, |
| 38 | { |
| 39 | /** The identity store containing the group. */ |
| 40 | identityStoreId: string; |
| 41 | /** The unique ID of the group. */ |
| 42 | groupId: string; |
| 43 | /** The display name of the group. */ |
| 44 | displayName: string | undefined; |
| 45 | /** The description of the group. */ |
| 46 | description: string | undefined; |
| 47 | /** When the group was created. */ |
| 48 | createdAt: Date | undefined; |
| 49 | /** When the group was last updated. */ |
| 50 | updatedAt: Date | undefined; |
| 51 | }, |
| 52 | never, |
| 53 | Providers |
| 54 | > {} |
| 55 | |
| 56 | /** |
| 57 | * A group in the IAM Identity Center identity store. |
| 58 | * ### Creating Groups |
| 59 | * **Example:** Platform Engineers |
| 60 | * ```typescript |
| 61 | * const engineers = yield* Group("PlatformEngineers", { |
| 62 | * displayName: "platform-engineers", |
| 63 | * description: "Platform engineering team", |
| 64 | * }); |
| 65 | * ``` |
| 66 | * |
| 67 | * @resource |
| 68 | */ |
| 69 | export const Group = Resource<Group>("AWS.IdentityCenter.Group"); |
| 70 | |
| 71 | export const GroupProvider = () => |
| 72 | Provider.effect( |
| 73 | Group, |
| 74 | Effect.gen(function* () { |
| 75 | return { |
| 76 | stables: ["identityStoreId", "groupId"], |
| 77 | list: () => |
| 78 | Effect.gen(function* () { |
| 79 | // Identity Center groups live in the instance's identity store. |
| 80 | // Resolve it from the (single) enabled SSO instance, then |
| 81 | // enumerate every group and hydrate each into the exact `read` |
| 82 | // shape via `describeGroup` (bounded concurrency, typed |
| 83 | // per-item not-found handled inside `readGroupById`). |
| 84 | const identityStoreId = yield* resolveIdentityStoreId({}); |
| 85 | const groups = yield* listGroups(identityStoreId); |
| 86 | const rows = yield* Effect.forEach( |
| 87 | groups, |
| 88 | (group) => |
| 89 | group.GroupId ? readGroupById(identityStoreId, group.GroupId) : Effect.undefined, |
| 90 | { concurrency: 10 }, |
| 91 | ); |
| 92 | return rows.filter((row): row is Group["Attributes"] => row !== undefined); |
| 93 | }), |
| 94 | diff: Effect.fn(function* ({ olds, news }) { |
| 95 | if (!isResolved(news)) return; |
| 96 | if (olds?.identityStoreId !== news.identityStoreId) { |
| 97 | return { action: "replace" } as const; |
| 98 | } |
| 99 | }), |
| 100 | read: Effect.fn(function* ({ olds, output }) { |
| 101 | if (output?.groupId && output.identityStoreId) { |
| 102 | return yield* readGroupById(output.identityStoreId, output.groupId); |
| 103 | } |
| 104 | |
| 105 | if (!olds) { |
| 106 | return undefined; |
| 107 | } |
| 108 | |
| 109 | return yield* readGroupByDisplayName(olds); |
| 110 | }), |
| 111 | reconcile: Effect.fn(function* ({ news, output, session }) { |
| 112 | const identityStoreId = output?.identityStoreId ?? (yield* resolveIdentityStoreId(news)); |
| 113 | |
| 114 | // Observe — find the group by id (if we already have one) or |
| 115 | // by display name. We never trust `output.groupId` blindly: a |
| 116 | // group deleted out of band shows up as missing here. |
| 117 | let existing = |
| 118 | (output?.groupId ? yield* readGroupById(identityStoreId, output.groupId) : undefined) ?? |
| 119 | (yield* readGroupByDisplayName({ |
| 120 | ...news, |
| 121 | identityStoreId, |
| 122 | })); |
| 123 | |
| 124 | // Ensure — create the group if missing. |
| 125 | if (!existing) { |
| 126 | const response = yield* retryIdentityCenter( |
| 127 | identitystore.createGroup({ |
| 128 | IdentityStoreId: identityStoreId, |
| 129 | DisplayName: news.displayName, |
| 130 | Description: news.description, |
| 131 | }), |
| 132 | ); |
| 133 | |
| 134 | existing = |
| 135 | (response.GroupId |
| 136 | ? yield* readGroupById(identityStoreId, response.GroupId) |
| 137 | : undefined) ?? |
| 138 | (yield* readGroupByDisplayName({ |
| 139 | ...news, |
| 140 | identityStoreId, |
| 141 | })); |
| 142 | |
| 143 | if (!existing) { |
| 144 | return yield* new GroupUnexpectedResponse({ |
| 145 | message: `group '${news.displayName}' not found after create`, |
| 146 | }); |
| 147 | } |
| 148 | |
| 149 | yield* session.note(existing.groupId); |
| 150 | return existing; |
| 151 | } |
| 152 | |
| 153 | // Sync mutable attributes — `updateGroup` overwrites |
| 154 | // displayName and description in one call. We diff against |
| 155 | // observed cloud state so adoption converges. |
| 156 | const operations: { |
| 157 | AttributePath: string; |
| 158 | AttributeValue: string; |
| 159 | }[] = []; |
| 160 | // Attribute paths are SCIM-style camelCase (`displayName`, |
| 161 | // `description`) per the UpdateGroup API. |
| 162 | if (existing.displayName !== news.displayName) { |
| 163 | operations.push({ |
| 164 | AttributePath: "displayName", |
| 165 | AttributeValue: news.displayName, |
| 166 | }); |
| 167 | } |
| 168 | const desiredDescription = news.description ?? ""; |
| 169 | if ((existing.description ?? "") !== desiredDescription) { |
| 170 | operations.push({ |
| 171 | AttributePath: "description", |
| 172 | AttributeValue: desiredDescription, |
| 173 | }); |
| 174 | } |
| 175 | |
| 176 | if (operations.length > 0) { |
| 177 | yield* retryIdentityCenter( |
| 178 | identitystore.updateGroup({ |
| 179 | IdentityStoreId: existing.identityStoreId, |
| 180 | GroupId: existing.groupId, |
| 181 | Operations: operations, |
| 182 | }), |
| 183 | ); |
| 184 | |
| 185 | const updated = yield* readGroupById(existing.identityStoreId, existing.groupId); |
| 186 | if (!updated) { |
| 187 | return yield* new GroupUnexpectedResponse({ |
| 188 | message: `group '${existing.groupId}' not found after update`, |
| 189 | }); |
| 190 | } |
| 191 | yield* session.note(updated.groupId); |
| 192 | return updated; |
| 193 | } |
| 194 | |
| 195 | yield* session.note(existing.groupId); |
| 196 | return existing; |
| 197 | }), |
| 198 | delete: Effect.fn(function* ({ output }) { |
| 199 | yield* retryIdentityCenter( |
| 200 | identitystore |
| 201 | .deleteGroup({ |
| 202 | IdentityStoreId: output.identityStoreId, |
| 203 | GroupId: output.groupId, |
| 204 | }) |
| 205 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)), |
| 206 | ); |
| 207 | }), |
| 208 | }; |
| 209 | }), |
| 210 | ); |
| 211 | |
| 212 | const readGroupById = Effect.fn(function* (identityStoreId: string, groupId: string) { |
| 213 | const response = yield* retryIdentityCenter( |
| 214 | identitystore |
| 215 | .describeGroup({ |
| 216 | IdentityStoreId: identityStoreId, |
| 217 | GroupId: groupId, |
| 218 | }) |
| 219 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined)), |
| 220 | ); |
| 221 | |
| 222 | if (!response?.GroupId || !response.IdentityStoreId) { |
| 223 | return undefined; |
| 224 | } |
| 225 | |
| 226 | return { |
| 227 | identityStoreId: response.IdentityStoreId, |
| 228 | groupId: response.GroupId, |
| 229 | // Sensitive-trait fields decode to Redacted at runtime — unwrap before |
| 230 | // persisting into Attributes / diffing against desired props. |
| 231 | displayName: unredact(response.DisplayName), |
| 232 | description: unredact(response.Description), |
| 233 | createdAt: response.CreatedAt, |
| 234 | updatedAt: response.UpdatedAt, |
| 235 | } satisfies Group["Attributes"]; |
| 236 | }); |
| 237 | |
| 238 | const readGroupByDisplayName = Effect.fn(function* ({ |
| 239 | identityStoreId, |
| 240 | instanceArn, |
| 241 | displayName, |
| 242 | }: Pick<GroupProps, "identityStoreId" | "instanceArn" | "displayName">) { |
| 243 | const resolvedIdentityStoreId = yield* resolveIdentityStoreId({ |
| 244 | identityStoreId, |
| 245 | instanceArn, |
| 246 | }); |
| 247 | const groups = yield* listGroups(resolvedIdentityStoreId); |
| 248 | const match = groups.find((group) => unredact(group.DisplayName) === displayName); |
| 249 | return match?.GroupId ? yield* readGroupById(resolvedIdentityStoreId, match.GroupId) : undefined; |
| 250 | }); |