@devries-cameron20 / t3code Mirror · read-only
Fork 0

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, 04:45 UTC

main
ts 666 lines 22.1 KB
Raw
1 import * as sqladmin from "@distilled.cloud/gcp/sqladmin_v1";
2 import { isResolved } from "alchemy/Diff";
3 import { createPhysicalName } from "alchemy/PhysicalName";
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 * as Redacted from "effect/Redacted";
9 import * as Schedule from "effect/Schedule";
10 import * as Stream from "effect/Stream";
11 import { GcpEnvironment } from "../Environment.ts";
12 import { ALCHEMY_LABEL_PREFIX } from "../Labels.ts";
13 import type { Providers } from "../Providers.ts";
14 import { recoverIfInstanceMissing, waitForSqlOperation } from "./operations.ts";
15
16 const MAX_NAME_LENGTH = 32;
17
18 const SYSTEM_USERS = new Set([
19 "root",
20 "postgres",
21 "sqlserver",
22 "cloudsqladmin",
23 "cloudsqlsuperuser",
24 "cloudsqlagent",
25 "cloudsqliamuser",
26 "cloudsqliamserviceaccount",
27 "cloudsqliamgroup",
28 "cloudsqlreplica",
29 "cloudsqlreadonly",
30 "cloudsqlapplier",
31 "mysql.sys",
32 "mysql.session",
33 "mysql.infoschema",
34 ]);
35
36 const AUTO_USER_TYPES = new Set(["CLOUD_IAM_GROUP_USER", "CLOUD_IAM_GROUP_SERVICE_ACCOUNT"]);
37
38 export type UserDeletionPolicy = "DELETE" | "ABANDON";
39
40 export type UserType = sqladmin.UserTypeEnum | (string & {});
41
42 export type DualPasswordType = sqladmin.UserDualPasswordTypeEnum | (string & {});
43
44 export type SqlServerUserDetails = {
45 /**
46 * Disable this SQL Server login.
47 */
48 disabled?: boolean;
49 /**
50 * SQL Server server-level roles (`sysadmin`, `dbcreator`, …).
51 */
52 serverRoles?: string[];
53 };
54
55 export type UserPasswordPolicy = {
56 /**
57 * Failed login attempts allowed before the user is locked.
58 */
59 allowedFailedAttempts?: number;
60 /**
61 * How long a password stays valid after it is set (duration string).
62 */
63 passwordExpirationDuration?: string;
64 /**
65 * Enable the failed-attempts lockout check.
66 */
67 enableFailedAttemptsCheck?: boolean;
68 /**
69 * Require the current password when changing it. MySQL only.
70 */
71 enablePasswordVerification?: boolean;
72 };
73
74 export type UserPasswordPolicyStatus = {
75 /** Whether the user is locked out of login. */
76 locked?: boolean;
77 /** RFC3339 expiration of the current password. */
78 passwordExpirationTime?: string;
79 };
80
81 export type UserProps = {
82 /**
83 * Cloud SQL instance id (the `{instance}` segment of
84 * `projects/{project}/instances/{instance}`). Full resource names are
85 * accepted and reduced to the last path segment. Immutable — changing
86 * it replaces the user.
87 */
88 instance: string;
89 /**
90 * Database user name. If omitted, a unique name is generated from the
91 * stack, stage, and logical id. MySQL limits names to 32 characters.
92 * Immutable — changing it replaces the user.
93 */
94 userName?: string;
95 /**
96 * Host the user can connect from. MySQL `BUILT_IN` users only; omit
97 * for PostgreSQL and SQL Server. Immutable — changing it replaces the
98 * user. `"%"` allows any host.
99 */
100 host?: string;
101 /**
102 * Password for `BUILT_IN` users. Write-only — never returned by the
103 * API. Required for PostgreSQL built-in users. Omit for IAM user
104 * types. Updating this value rotates the password. Accepts a
105 * `Redacted` value (e.g. `Alchemy.Random`'s `text`).
106 */
107 password?: string | Redacted.Redacted<string>;
108 /**
109 * Authentication type (`BUILT_IN`, `CLOUD_IAM_USER`,
110 * `CLOUD_IAM_SERVICE_ACCOUNT`, `CLOUD_IAM_GROUP`, …). Immutable —
111 * changing it replaces the user.
112 * @default "BUILT_IN"
113 */
114 type?: UserType;
115 /**
116 * PostgreSQL / MySQL database roles to grant (`cloudsqlsuperuser`, …).
117 */
118 databaseRoles?: string[];
119 /**
120 * User-level password validation policy (MySQL).
121 */
122 passwordPolicy?: UserPasswordPolicy;
123 /**
124 * Dual password status (`NO_DUAL_PASSWORD`, `DUAL_PASSWORD`, …).
125 */
126 dualPasswordType?: DualPasswordType;
127 /**
128 * SQL Server–only login details. Ignored on MySQL and PostgreSQL.
129 */
130 sqlserverUserDetails?: SqlServerUserDetails;
131 /**
132 * Full email for an IAM user. MySQL IAM users only.
133 */
134 iamEmail?: string;
135 /**
136 * What to do on destroy. `ABANDON` removes the user from state without
137 * calling the Cloud SQL API — useful when the parent instance is being
138 * deleted, or when a PostgreSQL user still holds roles.
139 * @default "DELETE"
140 */
141 deletionPolicy?: UserDeletionPolicy;
142 };
143
144 export type User = Resource<
145 "GCP.SQL.User",
146 UserProps,
147 {
148 /** Database user name. */
149 userName: string;
150 /** Host the user can connect from, if set. */
151 host: string | undefined;
152 /** Cloud SQL instance id. */
153 instance: string;
154 /** Project id. */
155 project: string;
156 /** Authentication type. */
157 type: string | undefined;
158 /** IAM email, if this is an IAM user. */
159 iamEmail: string | undefined;
160 /** Granted database roles. */
161 databaseRoles: string[] | undefined;
162 /** Password policy currently on the user. */
163 passwordPolicy: (UserPasswordPolicy & { status?: UserPasswordPolicyStatus }) | undefined;
164 /** Dual password status. */
165 dualPasswordType: string | undefined;
166 /** SQL Server details, if this is a SQL Server login. */
167 sqlserverUserDetails: SqlServerUserDetails | undefined;
168 /** IAM group availability (`ACTIVE`, `INACTIVE`, …). */
169 iamStatus: string | undefined;
170 /** HTTP etag. */
171 etag: string | undefined;
172 },
173 never,
174 Providers
175 >;
176
177 /**
178 * A database user on a Cloud SQL instance.
179 *
180 * Cloud SQL users have no labels field. `list` enumerates non-system
181 * users on alchemy-labeled instances so `pnpm nuke:gcp` can find leaked
182 * rows without dropping `root` / `postgres` / `cloudsqladmin`.
183 *
184 * Changing `instance`, `userName`, `host`, or `type` replaces the user.
185 * `password`, `databaseRoles`, and `passwordPolicy` update in place.
186 *
187 * ### Creating a User
188 * **Example:** Generated name on a Cloud SQL instance
189 * ```typescript
190 * const instance = yield* GCP.SQL.Instance("AppDb", {
191 * tier: "db-f1-micro",
192 * backup: { enabled: false },
193 * });
194 * const appUser = yield* GCP.SQL.User("AppUser", {
195 * instance: instance.instanceName,
196 * password: "change-me",
197 * });
198 * ```
199 *
200 * **Example:** Explicit MySQL user and host
201 * ```typescript
202 * const appUser = yield* GCP.SQL.User("AppUser", {
203 * instance: instance.instanceName,
204 * userName: "app",
205 * host: "%",
206 * password: "change-me",
207 * type: "BUILT_IN",
208 * });
209 * ```
210 *
211 * ### IAM Users
212 * **Example:** Cloud IAM database user
213 * ```typescript
214 * const iamUser = yield* GCP.SQL.User("IamUser", {
215 * instance: instance.instanceName,
216 * userName: "alice@example.com",
217 * type: "CLOUD_IAM_USER",
218 * });
219 * ```
220 *
221 * @resource
222 * @category SQL
223 */
224 export const User = Resource<User>("GCP.SQL.User");
225
226 export class UserNotResolved extends Data.TaggedError("GCP.SQL.UserNotResolved")<{
227 instance: string;
228 userName: string;
229 host: string | undefined;
230 }> {}
231
232 export class UserStillExists extends Data.TaggedError("GCP.SQL.UserStillExists")<{
233 instance: string;
234 userName: string;
235 host: string | undefined;
236 }> {}
237
238 const lastSegment = (value: string) => {
239 const trimmed = value.replace(/\/+$/, "");
240 const parts = trimmed.split("/");
241 return parts[parts.length - 1] || trimmed;
242 };
243
244 const instanceIdOf = (value: string) => lastSegment(value);
245
246 const normalizeHost = (host: string | undefined) => host ?? "";
247
248 const typeOf = (type: string | undefined) => (type ?? "BUILT_IN").toUpperCase();
249
250 const isIamType = (type: string | undefined) => {
251 const value = typeOf(type);
252 return value.startsWith("CLOUD_IAM") || value === "ENTRAID_USER";
253 };
254
255 const hasAlchemyInstanceLabels = (labels: Record<string, string | undefined> | null | undefined) =>
256 Object.keys(labels ?? {}).some((key) => key.startsWith(ALCHEMY_LABEL_PREFIX));
257
258 const isManagedUser = (user: sqladmin.User) => {
259 const name = (user.name ?? "").toLowerCase();
260 if (name.length === 0) return false;
261 if (SYSTEM_USERS.has(name)) return false;
262 if (name.startsWith("cloudsql")) return false;
263 if (name.startsWith("mysql.")) return false;
264 if (user.type !== undefined && AUTO_USER_TYPES.has(user.type)) return false;
265 return true;
266 };
267
268 const toSqlIdentifier = (name: string) => {
269 let next = name
270 .toLowerCase()
271 .replace(/[^a-z0-9_]/g, "_")
272 .replace(/_+/g, "_")
273 .replace(/^_+|_+$/g, "");
274 if (!/^[a-z]/.test(next)) next = `u${next}`;
275 next = next.slice(0, MAX_NAME_LENGTH).replace(/_+$/g, "");
276 return next.length > 0 ? next : "dbuser";
277 };
278
279 const toUserName = (id: string, userName: string | undefined, existing?: string) =>
280 Effect.gen(function* () {
281 if (userName !== undefined) return userName;
282 if (existing !== undefined) return existing;
283 return toSqlIdentifier(
284 yield* createPhysicalName({
285 id,
286 maxLength: MAX_NAME_LENGTH,
287 lowercase: true,
288 delimiter: "_",
289 }),
290 );
291 });
292
293 const rolesKey = (roles: string[] | undefined) =>
294 JSON.stringify([...(roles ?? [])].map((role) => role.toLowerCase()).sort());
295
296 const policyKey = (
297 policy: UserPasswordPolicy | sqladmin.UserPasswordValidationPolicy | undefined,
298 ) =>
299 JSON.stringify({
300 allowedFailedAttempts: policy?.allowedFailedAttempts ?? null,
301 passwordExpirationDuration: policy?.passwordExpirationDuration ?? "",
302 enableFailedAttemptsCheck: policy?.enableFailedAttemptsCheck === true,
303 enablePasswordVerification: policy?.enablePasswordVerification === true,
304 });
305
306 const sqlServerKey = (details: SqlServerUserDetails | undefined) =>
307 JSON.stringify({
308 disabled: details?.disabled === true,
309 serverRoles: [...(details?.serverRoles ?? [])].map((role) => role.toLowerCase()).sort(),
310 });
311
312 const dualPasswordOf = (value: string | undefined) =>
313 (value ?? "DUAL_PASSWORD_TYPE_UNSPECIFIED").toUpperCase();
314
315 const toPasswordPolicy = (
316 policy: UserPasswordPolicy | sqladmin.UserPasswordValidationPolicy | undefined,
317 ): sqladmin.UserPasswordValidationPolicy | undefined => {
318 if (policy === undefined) return undefined;
319 return {
320 allowedFailedAttempts: policy.allowedFailedAttempts,
321 passwordExpirationDuration: policy.passwordExpirationDuration,
322 enableFailedAttemptsCheck: policy.enableFailedAttemptsCheck,
323 enablePasswordVerification: policy.enablePasswordVerification,
324 };
325 };
326
327 const toAttrs = (live: sqladmin.User, project: string, instance: string) => ({
328 userName: live.name ?? "",
329 host: live.host,
330 instance: live.instance ?? instance,
331 project: live.project ?? project,
332 type: live.type,
333 iamEmail: live.iamEmail,
334 databaseRoles: live.databaseRoles,
335 passwordPolicy: live.passwordPolicy,
336 dualPasswordType: live.dualPasswordType,
337 sqlserverUserDetails: live.sqlserverUserDetails,
338 iamStatus: live.iamStatus,
339 etag: live.etag,
340 });
341
342 const matchesUser = (live: sqladmin.User, userName: string, host: string | undefined) =>
343 (live.name ?? "") === userName &&
344 (host === undefined || normalizeHost(live.host) === normalizeHost(host));
345
346 const getByName = (project: string, instance: string, userName: string, host: string | undefined) =>
347 sqladmin
348 .getUsers({
349 project,
350 instance,
351 name: userName,
352 ...(normalizeHost(host) ? { host: normalizeHost(host) } : {}),
353 })
354 .pipe(
355 Effect.catchTag("NotFound", () => Effect.undefined),
356 recoverIfInstanceMissing(project, instance, () => undefined),
357 Effect.flatMap((live) => {
358 if (live !== undefined && matchesUser(live, userName, host)) {
359 return Effect.succeed(live);
360 }
361 return sqladmin.listUsers({ project, instance }).pipe(
362 Effect.map((page) =>
363 (page.items ?? []).find((item) => matchesUser(item, userName, host)),
364 ),
365 Effect.catchTag("NotFound", () => Effect.undefined),
366 recoverIfInstanceMissing(project, instance, () => undefined),
367 );
368 }),
369 );
370
371 const waitForOperation = (
372 project: string,
373 operation: sqladmin.Operation,
374 options?: { notFoundOk?: boolean },
375 ) =>
376 waitForSqlOperation(project, operation, {
377 budget: "10 minutes",
378 notFoundOk: options?.notFoundOk,
379 });
380
381 const waitUntilExists = (
382 project: string,
383 instance: string,
384 userName: string,
385 host: string | undefined,
386 ) =>
387 getByName(project, instance, userName, host).pipe(
388 Effect.flatMap((user) =>
389 user ? Effect.succeed(user) : Effect.fail(new UserNotResolved({ instance, userName, host })),
390 ),
391 Effect.retry({
392 while: (error) => error._tag === "GCP.SQL.UserNotResolved",
393 times: 8,
394 schedule: Schedule.spaced("1 second"),
395 }),
396 );
397
398 const waitUntilGone = (
399 project: string,
400 instance: string,
401 userName: string,
402 host: string | undefined,
403 ) =>
404 getByName(project, instance, userName, host).pipe(
405 Effect.flatMap((user) =>
406 user === undefined
407 ? Effect.void
408 : Effect.fail(new UserStillExists({ instance, userName, host })),
409 ),
410 Effect.retry({
411 while: (error) => error._tag === "GCP.SQL.UserStillExists",
412 times: 10,
413 schedule: Schedule.spaced("1 second"),
414 }),
415 );
416
417 const passwordValue = (password: string | Redacted.Redacted<string>) =>
418 Redacted.isRedacted(password) ? Redacted.value(password) : password;
419
420 const toBody = (
421 news: UserProps,
422 userName: string,
423 instance: string,
424 project: string,
425 current?: sqladmin.User,
426 options?: { includePassword?: boolean },
427 ): sqladmin.User => {
428 const type = news.type ?? current?.type;
429 const includePassword =
430 options?.includePassword !== false && news.password !== undefined && !isIamType(type);
431 return {
432 name: userName,
433 instance,
434 project,
435 host: news.host ?? current?.host,
436 type,
437 password:
438 includePassword && news.password !== undefined ? passwordValue(news.password) : undefined,
439 // Echoing the observed roles back is rejected ("Invalid request to
440 // update database roles"); send roles only when they are declared.
441 databaseRoles: news.databaseRoles,
442 passwordPolicy: toPasswordPolicy(news.passwordPolicy ?? current?.passwordPolicy),
443 dualPasswordType: news.dualPasswordType ?? current?.dualPasswordType,
444 sqlserverUserDetails: news.sqlserverUserDetails ?? current?.sqlserverUserDetails,
445 iamEmail: news.iamEmail ?? current?.iamEmail,
446 };
447 };
448
449 const recoverIfPresent = <E>(
450 project: string,
451 instance: string,
452 userName: string,
453 host: string | undefined,
454 error: E,
455 ) =>
456 getByName(project, instance, userName, host).pipe(
457 Effect.flatMap((existing) => (existing ? Effect.void : Effect.fail(error))),
458 );
459
460 export const UserProvider = () =>
461 Provider.succeed(User, {
462 stables: ["userName", "host", "instance", "project"],
463
464 diff: Effect.fn(function* ({ news, olds, output }) {
465 if (!isResolved(news)) return undefined;
466 const previousInstance = olds?.instance ?? output?.instance;
467 const nextInstance = news.instance;
468 const previousName = olds?.userName ?? output?.userName;
469 const nextName = news.userName ?? previousName;
470 const previousHost = olds?.host ?? output?.host;
471 const nextHost = news.host ?? previousHost;
472 const previousType = olds?.type ?? output?.type;
473 const nextType = news.type ?? previousType;
474 const instanceChanged =
475 previousInstance !== undefined &&
476 instanceIdOf(previousInstance) !== instanceIdOf(nextInstance);
477 const nameChanged =
478 previousName !== undefined && nextName !== undefined && previousName !== nextName;
479 const hostChanged =
480 previousHost !== undefined &&
481 nextHost !== undefined &&
482 normalizeHost(previousHost) !== normalizeHost(nextHost);
483 const typeChanged =
484 previousType !== undefined &&
485 nextType !== undefined &&
486 typeOf(previousType) !== typeOf(nextType);
487 if (!instanceChanged && !nameChanged && !hostChanged && !typeChanged) {
488 return undefined;
489 }
490 const identityChanged = instanceChanged || nameChanged || hostChanged;
491 return {
492 action: "replace" as const,
493 deleteFirst: typeChanged && !identityChanged,
494 };
495 }),
496
497 read: Effect.fn(function* ({ id, olds, output }) {
498 const env = yield* GcpEnvironment.current;
499 const instance = instanceIdOf(olds?.instance ?? output?.instance ?? "");
500 if (instance.length === 0) return undefined;
501 const userName = yield* toUserName(id, olds?.userName, output?.userName);
502 const host = olds?.host ?? output?.host;
503 const existing = yield* getByName(env.project, instance, userName, host);
504 if (existing === undefined) return undefined;
505 return toAttrs(existing, env.project, instance);
506 }),
507
508 list: () =>
509 Effect.gen(function* () {
510 const env = yield* GcpEnvironment.current;
511 const instances = yield* sqladmin.listInstances
512 .items({
513 project: env.project,
514 maxResults: 1000,
515 filter: "instanceType:CLOUD_SQL_INSTANCE",
516 })
517 .pipe(
518 Stream.filter((instance) => hasAlchemyInstanceLabels(instance.settings?.userLabels)),
519 Stream.runCollect,
520 Effect.map((chunk) => Array.from(chunk)),
521 Effect.catchTag("NotFound", () => Effect.succeed([] as sqladmin.DatabaseInstance[])),
522 );
523 const pages = yield* Effect.forEach(
524 instances,
525 (instance) => {
526 const instanceName = instance.name;
527 if (instanceName === undefined || instanceName.length === 0) {
528 return Effect.succeed([] as User["Attributes"][]);
529 }
530 return sqladmin
531 .listUsers({
532 project: env.project,
533 instance: instanceName,
534 })
535 .pipe(
536 Effect.map((page) =>
537 (page.items ?? [])
538 .filter(isManagedUser)
539 .map((user) => toAttrs(user, env.project, instanceName)),
540 ),
541 // The instance may be deleted mid-listing.
542 Effect.catchTag("NotFound", () => Effect.succeed([] as User["Attributes"][])),
543 recoverIfInstanceMissing(
544 env.project,
545 instanceName,
546 () => [] as User["Attributes"][],
547 ),
548 );
549 },
550 { concurrency: 4 },
551 );
552 return pages.flat();
553 }),
554
555 reconcile: Effect.fn(function* ({ id, news, output, olds }) {
556 const env = yield* GcpEnvironment.current;
557 const instance = instanceIdOf(news.instance);
558 const userName = yield* toUserName(id, news.userName, output?.userName);
559 const host = news.host ?? output?.host;
560
561 let current = yield* getByName(env.project, instance, userName, host);
562 let inserted = false;
563
564 if (current === undefined) {
565 const created = yield* sqladmin
566 .insertUsers({
567 project: env.project,
568 instance,
569 body: toBody(news, userName, instance, env.project),
570 })
571 .pipe(
572 Effect.flatMap((operation) =>
573 waitForOperation(env.project, operation).pipe(Effect.as(true)),
574 ),
575 Effect.catchTags({
576 Conflict: (error) =>
577 recoverIfPresent(env.project, instance, userName, host, error).pipe(
578 Effect.as(false),
579 ),
580 BadRequest: (error) =>
581 recoverIfPresent(env.project, instance, userName, host, error).pipe(
582 Effect.as(false),
583 ),
584 }),
585 Effect.retry({
586 while: (error) => error._tag === "Conflict",
587 times: 8,
588 schedule: Schedule.spaced("5 seconds"),
589 }),
590 );
591 current = yield* waitUntilExists(env.project, instance, userName, host);
592 inserted = created;
593 }
594
595 const passwordChanged =
596 news.password !== undefined &&
597 !inserted &&
598 !isIamType(news.type ?? current.type) &&
599 (olds?.password === undefined ||
600 passwordValue(olds.password) !== passwordValue(news.password));
601 const rolesChanged =
602 news.databaseRoles !== undefined &&
603 rolesKey(current.databaseRoles) !== rolesKey(news.databaseRoles);
604 const policyChanged =
605 news.passwordPolicy !== undefined &&
606 policyKey(current.passwordPolicy) !== policyKey(news.passwordPolicy);
607 const dualPasswordChanged =
608 news.dualPasswordType !== undefined &&
609 dualPasswordOf(current.dualPasswordType) !== dualPasswordOf(news.dualPasswordType);
610 const sqlServerChanged =
611 news.sqlserverUserDetails !== undefined &&
612 sqlServerKey(current.sqlserverUserDetails) !== sqlServerKey(news.sqlserverUserDetails);
613 const iamEmailChanged =
614 news.iamEmail !== undefined && (current.iamEmail ?? "") !== news.iamEmail;
615
616 if (
617 passwordChanged ||
618 rolesChanged ||
619 policyChanged ||
620 dualPasswordChanged ||
621 sqlServerChanged ||
622 iamEmailChanged
623 ) {
624 const patched = yield* sqladmin.updateUsers({
625 project: env.project,
626 instance,
627 name: userName,
628 ...(normalizeHost(host) ? { host: normalizeHost(host) } : {}),
629 revokeExistingRoles: rolesChanged ? true : undefined,
630 body: toBody(news, userName, instance, env.project, current, {
631 includePassword: passwordChanged,
632 }),
633 });
634 yield* waitForOperation(env.project, patched);
635 current = yield* waitUntilExists(env.project, instance, userName, host);
636 }
637
638 return toAttrs(current, env.project, instance);
639 }),
640
641 delete: Effect.fn(function* ({ olds, output }) {
642 if (olds.deletionPolicy === "ABANDON") return;
643 const project = output.project;
644 const instance = instanceIdOf(output.instance);
645 const userName = output.userName;
646 const host = output.host;
647 yield* sqladmin
648 .deleteUsers({
649 project,
650 instance,
651 name: userName,
652 ...(normalizeHost(host) ? { host: normalizeHost(host) } : {}),
653 })
654 .pipe(
655 Effect.flatMap((operation) => waitForOperation(project, operation, { notFoundOk: true })),
656 Effect.catchTag("NotFound", () => Effect.void),
657 recoverIfInstanceMissing(project, instance, () => undefined),
658 Effect.retry({
659 while: (error) => error._tag === "Conflict",
660 times: 8,
661 schedule: Schedule.spaced("5 seconds"),
662 }),
663 );
664 yield* waitUntilGone(project, instance, userName, host);
665 }),
666 });