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
| 1 | import * as ssoAdmin from "@distilled.cloud/aws/sso-admin"; |
| 2 | import { isResolved } from "alchemy/Diff"; |
| 3 | import * as Provider from "alchemy/Provider"; |
| 4 | import { Resource } from "alchemy/Resource"; |
| 5 | import { normalizeDurationInput } from "alchemy/Util/Duration"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Duration from "effect/Duration"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Stream from "effect/Stream"; |
| 10 | import type { Providers } from "../Providers.ts"; |
| 11 | import { resolveInstance, retryIdentityCenter } from "./common.ts"; |
| 12 | |
| 13 | export class PermissionSetUnexpectedResponse extends Data.TaggedError( |
| 14 | "PermissionSetUnexpectedResponse", |
| 15 | )<{ |
| 16 | readonly message: string; |
| 17 | readonly cause?: unknown; |
| 18 | }> {} |
| 19 | |
| 20 | export interface PermissionSetProps { |
| 21 | /** |
| 22 | * Explicit IAM Identity Center instance ARN. |
| 23 | * If omitted, Alchemy adopts the only visible instance. |
| 24 | */ |
| 25 | instanceArn?: string; |
| 26 | /** |
| 27 | * Permission set name. |
| 28 | */ |
| 29 | name: string; |
| 30 | /** |
| 31 | * Optional human-readable description. |
| 32 | */ |
| 33 | description?: string; |
| 34 | /** |
| 35 | * Optional session duration, e.g. `"8 hours"` or `Duration.hours(8)`. |
| 36 | * Sent to Identity Center as an ISO-8601 string such as `PT8H` (a bare |
| 37 | * number is milliseconds). |
| 38 | */ |
| 39 | sessionDuration?: Duration.Input; |
| 40 | /** |
| 41 | * Optional relay state passed to supported applications. |
| 42 | */ |
| 43 | relayState?: string; |
| 44 | } |
| 45 | |
| 46 | export interface PermissionSet extends Resource< |
| 47 | "AWS.IdentityCenter.PermissionSet", |
| 48 | PermissionSetProps, |
| 49 | { |
| 50 | /** The Identity Center instance the permission set lives in. */ |
| 51 | instanceArn: string; |
| 52 | /** The ARN of the permission set. */ |
| 53 | permissionSetArn: string; |
| 54 | /** The name of the permission set. */ |
| 55 | name: string; |
| 56 | /** The description of the permission set. */ |
| 57 | description: string | undefined; |
| 58 | /** The session duration in ISO-8601 format (e.g. `PT8H`). */ |
| 59 | sessionDuration: string | undefined; |
| 60 | /** The relay state URL users land on after federating, if set. */ |
| 61 | relayState: string | undefined; |
| 62 | /** When the permission set was created. */ |
| 63 | createdDate: Date | undefined; |
| 64 | }, |
| 65 | never, |
| 66 | Providers |
| 67 | > {} |
| 68 | |
| 69 | /** |
| 70 | * An IAM Identity Center permission set. |
| 71 | * ### Creating Permission Sets |
| 72 | * **Example:** Administrator Access |
| 73 | * ```typescript |
| 74 | * const admin = yield* PermissionSet("AdministratorAccess", { |
| 75 | * name: "AdministratorAccess", |
| 76 | * description: "Administrator access for platform engineers", |
| 77 | * sessionDuration: "8 hours", |
| 78 | * }); |
| 79 | * ``` |
| 80 | * |
| 81 | * @resource |
| 82 | */ |
| 83 | export const PermissionSet = Resource<PermissionSet>("AWS.IdentityCenter.PermissionSet"); |
| 84 | |
| 85 | export const PermissionSetProvider = () => |
| 86 | Provider.effect( |
| 87 | PermissionSet, |
| 88 | Effect.gen(function* () { |
| 89 | return { |
| 90 | stables: ["permissionSetArn", "instanceArn"], |
| 91 | list: () => |
| 92 | Effect.gen(function* () { |
| 93 | // Permission sets live on an Identity Center instance. Resolve |
| 94 | // the single enabled SSO instance, enumerate every permission |
| 95 | // set ARN via `listPermissionSets` (exhaustively paginated by |
| 96 | // the distilled `.items` stream), then hydrate each into the |
| 97 | // exact `read` shape via `describePermissionSet` (bounded |
| 98 | // concurrency, typed per-item not-found handled inside |
| 99 | // `readPermissionSetByArn`). |
| 100 | const instance = yield* resolveInstance(); |
| 101 | const arns = yield* ssoAdmin.listPermissionSets |
| 102 | .items({ |
| 103 | InstanceArn: instance.InstanceArn!, |
| 104 | MaxResults: 100, |
| 105 | }) |
| 106 | .pipe(Stream.runCollect); |
| 107 | const rows = yield* Effect.forEach( |
| 108 | arns, |
| 109 | (permissionSetArn) => |
| 110 | readPermissionSetByArn({ |
| 111 | instanceArn: instance.InstanceArn!, |
| 112 | permissionSetArn, |
| 113 | }), |
| 114 | { concurrency: 10 }, |
| 115 | ); |
| 116 | return rows.filter((row): row is PermissionSet["Attributes"] => row !== undefined); |
| 117 | }), |
| 118 | diff: Effect.fn(function* ({ olds, news }) { |
| 119 | if (!isResolved(news)) return; |
| 120 | if (olds?.instanceArn !== news.instanceArn || olds?.name !== news.name) { |
| 121 | return { action: "replace" } as const; |
| 122 | } |
| 123 | }), |
| 124 | read: Effect.fn(function* ({ olds, output }) { |
| 125 | if (output?.permissionSetArn && output.instanceArn) { |
| 126 | return yield* readPermissionSetByArn({ |
| 127 | instanceArn: output.instanceArn, |
| 128 | permissionSetArn: output.permissionSetArn, |
| 129 | }); |
| 130 | } |
| 131 | |
| 132 | if (!olds) { |
| 133 | return undefined; |
| 134 | } |
| 135 | |
| 136 | return yield* readPermissionSetByName(olds); |
| 137 | }), |
| 138 | reconcile: Effect.fn(function* ({ news, output, session }) { |
| 139 | const instance = yield* resolveInstance(output?.instanceArn ?? news.instanceArn); |
| 140 | const desiredSessionDuration = |
| 141 | news.sessionDuration !== undefined |
| 142 | ? toIsoSessionDuration(news.sessionDuration) |
| 143 | : undefined; |
| 144 | |
| 145 | // Observe — find the permission set by ARN (when we already |
| 146 | // have one) or by name on the resolved instance. |
| 147 | let existing = |
| 148 | (output?.permissionSetArn |
| 149 | ? yield* readPermissionSetByArn({ |
| 150 | instanceArn: instance.InstanceArn!, |
| 151 | permissionSetArn: output.permissionSetArn, |
| 152 | }) |
| 153 | : undefined) ?? |
| 154 | (yield* readPermissionSetByName({ |
| 155 | ...news, |
| 156 | instanceArn: instance.InstanceArn, |
| 157 | })); |
| 158 | |
| 159 | // Ensure — create the permission set if missing. |
| 160 | if (!existing) { |
| 161 | const response = yield* retryIdentityCenter( |
| 162 | ssoAdmin.createPermissionSet({ |
| 163 | InstanceArn: instance.InstanceArn!, |
| 164 | Name: news.name, |
| 165 | Description: news.description, |
| 166 | SessionDuration: desiredSessionDuration, |
| 167 | RelayState: news.relayState, |
| 168 | }), |
| 169 | ); |
| 170 | |
| 171 | const createdArn = response.PermissionSet?.PermissionSetArn; |
| 172 | existing = |
| 173 | (createdArn |
| 174 | ? yield* readPermissionSetByArn({ |
| 175 | instanceArn: instance.InstanceArn!, |
| 176 | permissionSetArn: createdArn, |
| 177 | }) |
| 178 | : undefined) ?? |
| 179 | (yield* readPermissionSetByName({ |
| 180 | ...news, |
| 181 | instanceArn: instance.InstanceArn, |
| 182 | })); |
| 183 | |
| 184 | if (!existing) { |
| 185 | return yield* new PermissionSetUnexpectedResponse({ |
| 186 | message: `permission set '${news.name}' not found after create`, |
| 187 | }); |
| 188 | } |
| 189 | |
| 190 | yield* session.note(existing.permissionSetArn); |
| 191 | return existing; |
| 192 | } |
| 193 | |
| 194 | // Sync mutable attributes — `updatePermissionSet` overwrites |
| 195 | // description, sessionDuration, relayState. Diff against |
| 196 | // observed cloud state so adoption converges; only call when |
| 197 | // there's a real delta. |
| 198 | if ( |
| 199 | (existing.description ?? undefined) !== news.description || |
| 200 | (existing.sessionDuration ?? undefined) !== desiredSessionDuration || |
| 201 | (existing.relayState ?? undefined) !== news.relayState |
| 202 | ) { |
| 203 | yield* retryIdentityCenter( |
| 204 | ssoAdmin.updatePermissionSet({ |
| 205 | InstanceArn: existing.instanceArn, |
| 206 | PermissionSetArn: existing.permissionSetArn, |
| 207 | Description: news.description, |
| 208 | SessionDuration: desiredSessionDuration, |
| 209 | RelayState: news.relayState, |
| 210 | }), |
| 211 | ); |
| 212 | |
| 213 | const updated = yield* readPermissionSetByArn({ |
| 214 | instanceArn: existing.instanceArn, |
| 215 | permissionSetArn: existing.permissionSetArn, |
| 216 | }); |
| 217 | if (!updated) { |
| 218 | return yield* new PermissionSetUnexpectedResponse({ |
| 219 | message: `permission set '${existing.permissionSetArn}' not found after update`, |
| 220 | }); |
| 221 | } |
| 222 | yield* session.note(updated.permissionSetArn); |
| 223 | return updated; |
| 224 | } |
| 225 | |
| 226 | yield* session.note(existing.permissionSetArn); |
| 227 | return existing; |
| 228 | }), |
| 229 | delete: Effect.fn(function* ({ output }) { |
| 230 | yield* retryIdentityCenter( |
| 231 | ssoAdmin |
| 232 | .deletePermissionSet({ |
| 233 | InstanceArn: output.instanceArn, |
| 234 | PermissionSetArn: output.permissionSetArn, |
| 235 | }) |
| 236 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)), |
| 237 | ); |
| 238 | }), |
| 239 | }; |
| 240 | }), |
| 241 | ); |
| 242 | |
| 243 | /** |
| 244 | * Format a {@link Duration.Input} as the canonical ISO-8601 duration string |
| 245 | * (`PT8H`, `PT1H30M`, …) the `SessionDuration` wire field expects. ISO-8601 |
| 246 | * is semantically part of the AWS field, so only the normalization (state |
| 247 | * round-trip re-hydration) comes from the central Duration util. |
| 248 | */ |
| 249 | const toIsoSessionDuration = (input: Duration.Input): string => { |
| 250 | const totalSeconds = Math.round(Duration.toSeconds(normalizeDurationInput(input))); |
| 251 | const hours = Math.floor(totalSeconds / 3600); |
| 252 | const minutes = Math.floor((totalSeconds % 3600) / 60); |
| 253 | const seconds = totalSeconds % 60; |
| 254 | const parts = [ |
| 255 | hours > 0 ? `${hours}H` : "", |
| 256 | minutes > 0 ? `${minutes}M` : "", |
| 257 | seconds > 0 || totalSeconds === 0 ? `${seconds}S` : "", |
| 258 | ].join(""); |
| 259 | return `PT${parts}`; |
| 260 | }; |
| 261 | |
| 262 | const readPermissionSetByArn = Effect.fn(function* ({ |
| 263 | instanceArn, |
| 264 | permissionSetArn, |
| 265 | }: { |
| 266 | instanceArn: string; |
| 267 | permissionSetArn: string; |
| 268 | }) { |
| 269 | const response = yield* retryIdentityCenter( |
| 270 | ssoAdmin |
| 271 | .describePermissionSet({ |
| 272 | InstanceArn: instanceArn, |
| 273 | PermissionSetArn: permissionSetArn, |
| 274 | }) |
| 275 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined)), |
| 276 | ); |
| 277 | |
| 278 | const permissionSet = response?.PermissionSet; |
| 279 | if (!permissionSet?.PermissionSetArn || !permissionSet.Name) { |
| 280 | return undefined; |
| 281 | } |
| 282 | |
| 283 | return { |
| 284 | instanceArn, |
| 285 | permissionSetArn: permissionSet.PermissionSetArn, |
| 286 | name: permissionSet.Name, |
| 287 | description: permissionSet.Description, |
| 288 | sessionDuration: permissionSet.SessionDuration, |
| 289 | relayState: permissionSet.RelayState, |
| 290 | createdDate: permissionSet.CreatedDate, |
| 291 | } satisfies PermissionSet["Attributes"]; |
| 292 | }); |
| 293 | |
| 294 | const readPermissionSetByName = Effect.fn(function* ({ |
| 295 | instanceArn, |
| 296 | name, |
| 297 | }: Pick<PermissionSetProps, "instanceArn" | "name">) { |
| 298 | const instance = yield* resolveInstance(instanceArn); |
| 299 | const arns = yield* ssoAdmin.listPermissionSets |
| 300 | .items({ |
| 301 | InstanceArn: instance.InstanceArn!, |
| 302 | MaxResults: 100, |
| 303 | }) |
| 304 | .pipe(Stream.runCollect); |
| 305 | |
| 306 | for (const permissionSetArn of arns) { |
| 307 | const permissionSet = yield* readPermissionSetByArn({ |
| 308 | instanceArn: instance.InstanceArn!, |
| 309 | permissionSetArn, |
| 310 | }); |
| 311 | if (permissionSet?.name === name) { |
| 312 | return permissionSet; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | return undefined; |
| 317 | }); |