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 cloudidentity from "@distilled.cloud/gcp/cloudidentity_v1"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import * as Provider from "alchemy/Provider"; |
| 5 | import { Resource } from "alchemy/Resource"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import type { Providers } from "../Providers.ts"; |
| 9 | import { |
| 10 | expandGroup, |
| 11 | findSsoAssignment, |
| 12 | getSsoAssignment, |
| 13 | lastSegment, |
| 14 | listOwnedGroups, |
| 15 | listSsoAssignments, |
| 16 | normalizeCustomer, |
| 17 | replaceOnIdentity, |
| 18 | sameJson, |
| 19 | sameText, |
| 20 | updateMaskOf, |
| 21 | } from "./internal.ts"; |
| 22 | import { resourceNameFromOperation, waitForOperation, waitUntilPresent } from "./operations.ts"; |
| 23 | |
| 24 | export type InboundSsoAssignmentSamlSsoInfo = { |
| 25 | /** `inboundSamlSsoProfiles/{id}` to use when `ssoMode` is `SAML_SSO`. */ |
| 26 | inboundSamlSsoProfile?: string; |
| 27 | }; |
| 28 | |
| 29 | export type InboundSsoAssignmentOidcSsoInfo = { |
| 30 | /** `inboundOidcSsoProfiles/{id}` to use when `ssoMode` is `OIDC_SSO`. */ |
| 31 | inboundOidcSsoProfile?: string; |
| 32 | }; |
| 33 | |
| 34 | export type InboundSsoAssignmentSignInBehavior = { |
| 35 | /** When to redirect sign-ins to the IdP. */ |
| 36 | redirectCondition?: cloudidentity.SignInBehaviorRedirectConditionEnum | (string & {}); |
| 37 | }; |
| 38 | |
| 39 | export type InboundSsoAssignmentProps = { |
| 40 | /** |
| 41 | * Customer (`customers/C0123abc` or `customers/my_customer`). |
| 42 | * Immutable — changing it replaces the assignment. |
| 43 | * @default "customers/my_customer" |
| 44 | */ |
| 45 | customer?: string; |
| 46 | /** |
| 47 | * Target group (`groups/{group}`). Immutable — changing it |
| 48 | * replaces the assignment. Rank must be ≥ 1 when set. |
| 49 | */ |
| 50 | targetGroup?: string; |
| 51 | /** |
| 52 | * Target org unit (`orgUnits/{org_unit}`). Immutable — changing |
| 53 | * it replaces the assignment. Rank must be 0 (the default). |
| 54 | */ |
| 55 | targetOrgUnit?: string; |
| 56 | /** |
| 57 | * Inbound SSO behavior. |
| 58 | */ |
| 59 | ssoMode?: cloudidentity.InboundSsoAssignmentSsoModeEnum | (string & {}); |
| 60 | /** |
| 61 | * Priority among group-targeted assignments. Zero for org units; |
| 62 | * ≥ 1 for groups. |
| 63 | */ |
| 64 | rank?: number; |
| 65 | /** |
| 66 | * SAML profile, required when `ssoMode` is `SAML_SSO`. |
| 67 | */ |
| 68 | samlSsoInfo?: InboundSsoAssignmentSamlSsoInfo; |
| 69 | /** |
| 70 | * OIDC profile, required when `ssoMode` is `OIDC_SSO`. |
| 71 | */ |
| 72 | oidcSsoInfo?: InboundSsoAssignmentOidcSsoInfo; |
| 73 | /** |
| 74 | * Sign-in redirect behavior. |
| 75 | */ |
| 76 | signInBehavior?: InboundSsoAssignmentSignInBehavior; |
| 77 | }; |
| 78 | |
| 79 | export type InboundSsoAssignment = Resource< |
| 80 | "GCP.CloudIdentity.InboundSsoAssignment", |
| 81 | InboundSsoAssignmentProps, |
| 82 | { |
| 83 | /** Resource name `inboundSsoAssignments/{assignment}`. */ |
| 84 | name: string; |
| 85 | /** Assignment id (last path segment). */ |
| 86 | assignmentId: string; |
| 87 | /** Customer. */ |
| 88 | customer: string | undefined; |
| 89 | /** Target group, if any. */ |
| 90 | targetGroup: string | undefined; |
| 91 | /** Target org unit, if any. */ |
| 92 | targetOrgUnit: string | undefined; |
| 93 | /** SSO mode. */ |
| 94 | ssoMode: string | undefined; |
| 95 | /** Rank. */ |
| 96 | rank: number | undefined; |
| 97 | /** SAML profile, if any. */ |
| 98 | samlSsoInfo: InboundSsoAssignmentSamlSsoInfo | undefined; |
| 99 | /** OIDC profile, if any. */ |
| 100 | oidcSsoInfo: InboundSsoAssignmentOidcSsoInfo | undefined; |
| 101 | /** Sign-in behavior. */ |
| 102 | signInBehavior: InboundSsoAssignmentSignInBehavior | undefined; |
| 103 | }, |
| 104 | never, |
| 105 | Providers |
| 106 | >; |
| 107 | |
| 108 | /** |
| 109 | * An inbound SSO assignment for a Cloud Identity group or org unit. |
| 110 | * |
| 111 | * Assignments have no labels or description, so Alchemy lists |
| 112 | * assignments whose `targetGroup` is an alchemy-owned group for |
| 113 | * `list` / nuke. Customer, target group, and target org unit are |
| 114 | * identity; mode, rank, and IdP details update in place. |
| 115 | * |
| 116 | * ### Creating an Assignment |
| 117 | * **Example:** SAML SSO for a group |
| 118 | * ```typescript |
| 119 | * const assignment = yield* GCP.CloudIdentity.InboundSsoAssignment( |
| 120 | * "EngSso", |
| 121 | * { |
| 122 | * targetGroup: group.name, |
| 123 | * ssoMode: "SAML_SSO", |
| 124 | * rank: 1, |
| 125 | * samlSsoInfo: { inboundSamlSsoProfile: profile.name }, |
| 126 | * }, |
| 127 | * ); |
| 128 | * ``` |
| 129 | * |
| 130 | * @resource |
| 131 | * @category CloudIdentity |
| 132 | */ |
| 133 | export const InboundSsoAssignment = Resource<InboundSsoAssignment>( |
| 134 | "GCP.CloudIdentity.InboundSsoAssignment", |
| 135 | ); |
| 136 | |
| 137 | export class InboundSsoAssignmentNotResolved extends Data.TaggedError( |
| 138 | "GCP.CloudIdentity.InboundSsoAssignmentNotResolved", |
| 139 | )<{ |
| 140 | resourceName: string; |
| 141 | }> {} |
| 142 | |
| 143 | const toAttrs = (assignment: cloudidentity.InboundSsoAssignment) => { |
| 144 | const name = assignment.name ?? ""; |
| 145 | return { |
| 146 | name, |
| 147 | assignmentId: lastSegment(name), |
| 148 | customer: assignment.customer, |
| 149 | targetGroup: assignment.targetGroup, |
| 150 | targetOrgUnit: assignment.targetOrgUnit, |
| 151 | ssoMode: assignment.ssoMode, |
| 152 | rank: assignment.rank, |
| 153 | samlSsoInfo: assignment.samlSsoInfo |
| 154 | ? { inboundSamlSsoProfile: assignment.samlSsoInfo.inboundSamlSsoProfile } |
| 155 | : undefined, |
| 156 | oidcSsoInfo: assignment.oidcSsoInfo |
| 157 | ? { inboundOidcSsoProfile: assignment.oidcSsoInfo.inboundOidcSsoProfile } |
| 158 | : undefined, |
| 159 | signInBehavior: assignment.signInBehavior |
| 160 | ? { redirectCondition: assignment.signInBehavior.redirectCondition } |
| 161 | : undefined, |
| 162 | }; |
| 163 | }; |
| 164 | |
| 165 | const expandTargetGroup = (value: string | undefined) => |
| 166 | value !== undefined && value.length > 0 ? expandGroup(value) : undefined; |
| 167 | |
| 168 | const observeAssignment = (input: { |
| 169 | name?: string; |
| 170 | targetGroup?: string; |
| 171 | targetOrgUnit?: string; |
| 172 | ssoMode?: string; |
| 173 | }) => |
| 174 | findSsoAssignment({ |
| 175 | name: input.name, |
| 176 | targetGroup: expandTargetGroup(input.targetGroup), |
| 177 | targetOrgUnit: input.targetOrgUnit, |
| 178 | ssoMode: input.ssoMode, |
| 179 | }); |
| 180 | |
| 181 | export const InboundSsoAssignmentProvider = () => |
| 182 | Provider.succeed(InboundSsoAssignment, { |
| 183 | stables: ["name", "assignmentId", "customer", "targetGroup", "targetOrgUnit"], |
| 184 | |
| 185 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 186 | if (!isResolved(news)) return undefined; |
| 187 | const previousCustomer = olds?.customer ?? output?.customer; |
| 188 | const nextCustomer = |
| 189 | news.customer !== undefined ? normalizeCustomer(news.customer) : previousCustomer; |
| 190 | const previousGroup = olds?.targetGroup ?? output?.targetGroup; |
| 191 | const nextGroup = expandTargetGroup(news.targetGroup); |
| 192 | const previousOu = olds?.targetOrgUnit ?? output?.targetOrgUnit; |
| 193 | return replaceOnIdentity({ |
| 194 | previousId: previousGroup, |
| 195 | nextId: nextGroup, |
| 196 | previousParent: previousCustomer, |
| 197 | nextParent: nextCustomer, |
| 198 | extra: |
| 199 | news.targetOrgUnit !== undefined && |
| 200 | previousOu !== undefined && |
| 201 | news.targetOrgUnit !== previousOu, |
| 202 | }); |
| 203 | }), |
| 204 | |
| 205 | read: Effect.fn(function* ({ olds, output }) { |
| 206 | const existing = yield* observeAssignment({ |
| 207 | name: output?.name, |
| 208 | targetGroup: olds?.targetGroup ?? output?.targetGroup, |
| 209 | targetOrgUnit: olds?.targetOrgUnit ?? output?.targetOrgUnit, |
| 210 | ssoMode: olds?.ssoMode ?? output?.ssoMode, |
| 211 | }); |
| 212 | if (existing === undefined) return undefined; |
| 213 | const attrs = toAttrs(existing); |
| 214 | return output !== undefined ? attrs : Unowned(attrs); |
| 215 | }), |
| 216 | |
| 217 | list: () => |
| 218 | Effect.gen(function* () { |
| 219 | const groups = yield* listOwnedGroups(); |
| 220 | const names = new Set( |
| 221 | groups.map((group) => group.name).filter((name): name is string => name !== undefined), |
| 222 | ); |
| 223 | const assignments = yield* listSsoAssignments(); |
| 224 | return assignments |
| 225 | .filter( |
| 226 | (assignment) => |
| 227 | assignment.targetGroup !== undefined && names.has(assignment.targetGroup), |
| 228 | ) |
| 229 | .map(toAttrs); |
| 230 | }), |
| 231 | |
| 232 | reconcile: Effect.fn(function* ({ news, output }) { |
| 233 | const customer = normalizeCustomer(news.customer ?? output?.customer); |
| 234 | const targetGroup = expandTargetGroup(news.targetGroup); |
| 235 | const desired: cloudidentity.InboundSsoAssignment = { |
| 236 | customer, |
| 237 | targetGroup, |
| 238 | targetOrgUnit: news.targetOrgUnit, |
| 239 | ssoMode: news.ssoMode, |
| 240 | rank: news.rank, |
| 241 | samlSsoInfo: news.samlSsoInfo, |
| 242 | oidcSsoInfo: news.oidcSsoInfo, |
| 243 | signInBehavior: news.signInBehavior, |
| 244 | }; |
| 245 | |
| 246 | let current = yield* observeAssignment({ |
| 247 | name: output?.name, |
| 248 | targetGroup, |
| 249 | targetOrgUnit: news.targetOrgUnit ?? output?.targetOrgUnit, |
| 250 | ssoMode: news.ssoMode ?? output?.ssoMode, |
| 251 | }); |
| 252 | |
| 253 | if (current === undefined) { |
| 254 | const created = yield* cloudidentity |
| 255 | .createInboundSsoAssignments({ body: desired }) |
| 256 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 257 | if (created !== undefined) { |
| 258 | yield* waitForOperation(created).pipe( |
| 259 | Effect.catchTag("GCP.CloudIdentity.OperationPending", () => Effect.void), |
| 260 | ); |
| 261 | const createdName = resourceNameFromOperation(created); |
| 262 | if (createdName !== undefined) { |
| 263 | current = yield* getSsoAssignment(createdName); |
| 264 | } |
| 265 | } |
| 266 | if (current === undefined) { |
| 267 | current = yield* waitUntilPresent( |
| 268 | observeAssignment({ |
| 269 | name: output?.name, |
| 270 | targetGroup, |
| 271 | targetOrgUnit: news.targetOrgUnit, |
| 272 | ssoMode: news.ssoMode, |
| 273 | }), |
| 274 | targetGroup ?? news.targetOrgUnit ?? "", |
| 275 | ).pipe( |
| 276 | Effect.catchTag("GCP.CloudIdentity.OperationPending", () => |
| 277 | observeAssignment({ |
| 278 | targetGroup, |
| 279 | targetOrgUnit: news.targetOrgUnit, |
| 280 | ssoMode: news.ssoMode, |
| 281 | }), |
| 282 | ), |
| 283 | ); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | if (current === undefined) { |
| 288 | return yield* new InboundSsoAssignmentNotResolved({ |
| 289 | resourceName: output?.name ?? targetGroup ?? news.targetOrgUnit ?? "", |
| 290 | }); |
| 291 | } |
| 292 | |
| 293 | const name = current.name ?? output?.name ?? ""; |
| 294 | const modeChanged = news.ssoMode !== undefined && !sameText(current.ssoMode, news.ssoMode); |
| 295 | const rankChanged = news.rank !== undefined && (current.rank ?? 0) !== news.rank; |
| 296 | const samlChanged = |
| 297 | news.samlSsoInfo !== undefined && !sameJson(current.samlSsoInfo, news.samlSsoInfo); |
| 298 | const oidcChanged = |
| 299 | news.oidcSsoInfo !== undefined && !sameJson(current.oidcSsoInfo, news.oidcSsoInfo); |
| 300 | const signInChanged = |
| 301 | news.signInBehavior !== undefined && !sameJson(current.signInBehavior, news.signInBehavior); |
| 302 | const updateMask = updateMaskOf( |
| 303 | modeChanged ? "sso_mode" : undefined, |
| 304 | rankChanged ? "rank" : undefined, |
| 305 | samlChanged ? "saml_sso_info" : undefined, |
| 306 | oidcChanged ? "oidc_sso_info" : undefined, |
| 307 | signInChanged ? "sign_in_behavior" : undefined, |
| 308 | ); |
| 309 | |
| 310 | if (updateMask.length > 0 && name.length > 0) { |
| 311 | const patched = yield* cloudidentity.patchInboundSsoAssignments({ |
| 312 | name, |
| 313 | updateMask, |
| 314 | body: { |
| 315 | ssoMode: news.ssoMode, |
| 316 | rank: news.rank, |
| 317 | samlSsoInfo: news.samlSsoInfo, |
| 318 | oidcSsoInfo: news.oidcSsoInfo, |
| 319 | signInBehavior: news.signInBehavior, |
| 320 | }, |
| 321 | }); |
| 322 | yield* waitForOperation(patched).pipe( |
| 323 | Effect.catchTag("GCP.CloudIdentity.OperationPending", () => Effect.void), |
| 324 | ); |
| 325 | current = (yield* getSsoAssignment(name)) ?? current; |
| 326 | } |
| 327 | |
| 328 | return toAttrs(current); |
| 329 | }), |
| 330 | |
| 331 | delete: Effect.fn(function* ({ output }) { |
| 332 | if (output.name.length === 0) return; |
| 333 | const deleted = yield* cloudidentity |
| 334 | .deleteInboundSsoAssignments({ name: output.name }) |
| 335 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 336 | if (deleted !== undefined) { |
| 337 | yield* waitForOperation(deleted, { notFoundOk: true }); |
| 338 | } |
| 339 | }), |
| 340 | }); |