@wtfalch/foundry 0.45.1 → 0.47.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/dist/build-json.d.ts +28 -0
  2. package/dist/build-json.js +95 -0
  3. package/dist/index.d.ts +10 -4
  4. package/dist/index.js +6 -2
  5. package/dist/migrations/0058_app_requests.sql +32 -0
  6. package/dist/org.d.ts +68 -5
  7. package/dist/org.js +287 -135
  8. package/dist/plan/agora.d.ts +14 -0
  9. package/dist/plan/agora.js +21 -0
  10. package/dist/plan/backup-gate.d.ts +15 -0
  11. package/dist/plan/backup-gate.js +38 -0
  12. package/dist/plan/boule-owners-http.d.ts +12 -0
  13. package/dist/plan/boule-owners-http.js +59 -0
  14. package/dist/plan/boule.d.ts +38 -2
  15. package/dist/plan/boule.js +72 -1
  16. package/dist/plan/company-backups.d.ts +7 -1
  17. package/dist/plan/company-backups.js +42 -2
  18. package/dist/plan/cookie-domain.d.ts +41 -0
  19. package/dist/plan/cookie-domain.js +71 -0
  20. package/dist/plan/desktop-auth.d.ts +58 -0
  21. package/dist/plan/desktop-auth.js +144 -0
  22. package/dist/plan/desktop-callback.d.ts +26 -0
  23. package/dist/plan/desktop-callback.js +59 -0
  24. package/dist/plan/diff.d.ts +13 -4
  25. package/dist/plan/diff.js +277 -88
  26. package/dist/plan/executor.d.ts +136 -6
  27. package/dist/plan/executor.js +732 -105
  28. package/dist/plan/health-gaps.js +5 -0
  29. package/dist/plan/health-targets.js +1 -2
  30. package/dist/plan/hostnames.d.ts +2 -2
  31. package/dist/plan/hostnames.js +6 -6
  32. package/dist/plan/index.d.ts +4 -1
  33. package/dist/plan/index.js +3 -1
  34. package/dist/plan/job-app.d.ts +23 -0
  35. package/dist/plan/job-app.js +41 -0
  36. package/dist/plan/live.d.ts +6 -0
  37. package/dist/plan/live.js +16 -0
  38. package/dist/plan/login-host.d.ts +28 -0
  39. package/dist/plan/login-host.js +36 -0
  40. package/dist/plan/org-names.js +9 -1
  41. package/dist/plan/org-postgres-job.d.ts +22 -0
  42. package/dist/plan/org-postgres-job.js +91 -0
  43. package/dist/plan/org-postgres-migrate.d.ts +115 -0
  44. package/dist/plan/org-postgres-migrate.js +466 -0
  45. package/dist/plan/org-postgres.d.ts +90 -0
  46. package/dist/plan/org-postgres.js +293 -0
  47. package/dist/plan/plan-apply.d.ts +60 -3
  48. package/dist/plan/plan-apply.js +174 -8
  49. package/dist/plan/platform-diff.d.ts +45 -0
  50. package/dist/plan/platform-diff.js +159 -4
  51. package/dist/plan/platform-org.d.ts +2 -0
  52. package/dist/plan/platform-org.js +4 -0
  53. package/dist/plan/platform-providers.d.ts +12 -2
  54. package/dist/plan/platform-providers.js +282 -9
  55. package/dist/plan/post-logout-uris.d.ts +22 -0
  56. package/dist/plan/post-logout-uris.js +43 -0
  57. package/dist/plan/providers-readers.d.ts +10 -1
  58. package/dist/plan/providers-readers.js +56 -5
  59. package/dist/plan/sign-in-bindings.d.ts +4 -0
  60. package/dist/plan/sign-in-bindings.js +10 -0
  61. package/dist/plan/sign-in-org.d.ts +19 -6
  62. package/dist/plan/sign-in-org.js +35 -10
  63. package/dist/plan/sign-in.d.ts +12 -5
  64. package/dist/plan/sign-in.js +12 -5
  65. package/dist/plan/signin-db-move.js +10 -34
  66. package/dist/plan/worker-app.js +3 -1
  67. package/dist/refuse-org.d.ts +7 -0
  68. package/dist/refuse-org.js +10 -0
  69. package/dist/requested-changes.d.ts +50 -4
  70. package/dist/requested-changes.js +9 -1
  71. package/dist/store/postgres.d.ts +5 -1
  72. package/dist/store/postgres.js +67 -8
  73. package/dist/store/schema.d.ts +72 -0
  74. package/dist/store/schema.js +4 -0
  75. package/dist/store.d.ts +24 -1
  76. package/dist/worker-app-config.d.ts +8 -2
  77. package/dist/worker-app-config.js +20 -6
  78. package/package.json +5 -5
@@ -0,0 +1,144 @@
1
+ import { createHash } from 'node:crypto';
2
+ import { ORG_SERVICE_SCHEMAS, orgServiceDatabase } from '../org.js';
3
+ import { AGORA_CLIENT_ID_SCOPE_KEY, DESKTOP_AUTH_APP_SCOPE_KEY, DESKTOP_AUTH_DEPLOYED_SCOPE_KEY, DESKTOP_AUTH_PORT, ZITADEL_ORG_SCOPE_KEY, agoraHostnameFor, desktopAuthInternalName, desktopAuthUpstream, withRecordedMessagingProject, } from './agora.js';
4
+ import { ZITADEL_FILES_PROJECT_SCOPE_KEY } from './core-files.js';
5
+ export { DESKTOP_AUTH_APP_SCOPE_KEY, DESKTOP_AUTH_DEPLOYED_SCOPE_KEY, DESKTOP_AUTH_PORT, desktopAuthInternalName, desktopAuthUpstream, };
6
+ /**
7
+ * #440: Agora's desktop sign-in backend (`apps/desktop-auth` of the agora repo), one internal app per
8
+ * company on its Postgres server's `desktop_auth` home. Agora proxies `/auth/desktop/*` to it. Its own
9
+ * secrets (cookie, handoff key) live in its env only and are generated once.
10
+ */
11
+ export const DESKTOP_AUTH_DOCKERFILE = '/apps/desktop-auth/Dockerfile';
12
+ export const DESKTOP_AUTH_HOME = 'desktop_auth';
13
+ /** The digest of the non-secret env last written; a changed hostname, issuer or id plans the step again. */
14
+ export const DESKTOP_AUTH_REVISION_SCOPE_KEY = 'coolify_desktop_auth_revision';
15
+ /** The Agora origin the app was last written for (CLAUDE.md rule 3: the applied hostname is a scope id). */
16
+ export const DESKTOP_AUTH_ORIGIN_SCOPE_KEY = 'coolify_desktop_auth_origin';
17
+ /** The callback URI last put on Agora's client, or `unsupported:<uri>` when the providers client cannot. */
18
+ export const DESKTOP_AUTH_REDIRECT_SCOPE_KEY = 'coolify_desktop_auth_redirect';
19
+ /** Recorded once the readiness probe through Agora answered. */
20
+ export const DESKTOP_AUTH_READY_SCOPE_KEY = 'coolify_desktop_auth_ready';
21
+ /** `https://<agora host>/auth/desktop/callback`. */
22
+ export function desktopAuthCallbackUri(agoraOrigin) {
23
+ return `${agoraOrigin}/auth/desktop/callback`;
24
+ }
25
+ export const desktopAuthOrigin = (org) => `https://${agoraHostnameFor(org)}`;
26
+ /** The route desktop-auth answers 200 on without a session; foundry probes it through Agora. */
27
+ export const DESKTOP_AUTH_READY_PATH = '/auth/desktop/error';
28
+ export const desktopAuthRedirectUnsupported = (uri) => `unsupported:${uri}`;
29
+ /** The inputs from the host values and recorded ids, and the names of those still missing. */
30
+ export function desktopAuthInputs(org, recorded, rawHost) {
31
+ const host = withRecordedMessagingProject(rawHost, recorded);
32
+ const clientId = recorded[AGORA_CLIENT_ID_SCOPE_KEY];
33
+ const organisationId = recorded[ZITADEL_ORG_SCOPE_KEY];
34
+ const missing = [];
35
+ if (!host.issuer)
36
+ missing.push('issuer');
37
+ if (!clientId)
38
+ missing.push('clientId');
39
+ if (!organisationId)
40
+ missing.push('zitadelOrgId');
41
+ if (!host.messagingProjectId)
42
+ missing.push('messagingProjectId');
43
+ if (!host.mailProjectId)
44
+ missing.push('mailProjectId');
45
+ if (missing.length > 0 || !host.issuer || !clientId || !organisationId) {
46
+ return { inputs: null, missing };
47
+ }
48
+ return {
49
+ inputs: {
50
+ origin: desktopAuthOrigin(org),
51
+ issuer: host.issuer,
52
+ clientId,
53
+ organisationId,
54
+ projectId: host.messagingProjectId,
55
+ mailProjectId: host.mailProjectId,
56
+ filesProjectId: recorded[ZITADEL_FILES_PROJECT_SCOPE_KEY] ?? null,
57
+ },
58
+ missing,
59
+ };
60
+ }
61
+ /** The env that is not a secret and not a connection URL. `APP_URL` is the origin with its slash: the app refuses any other form. */
62
+ export function desktopAuthEnv(inputs, runtimeRole) {
63
+ return {
64
+ APP_URL: `${inputs.origin}/`,
65
+ AUTH_ISSUER: inputs.issuer,
66
+ AUTH_CLIENT_ID: inputs.clientId,
67
+ AUTH_ORGANIZATION_ID: inputs.organisationId,
68
+ AUTH_PROJECT_ID: inputs.projectId,
69
+ MAIL_PROJECT_ID: inputs.mailProjectId,
70
+ ...(inputs.filesProjectId ? { FILES_PROJECT_ID: inputs.filesProjectId } : {}),
71
+ DESKTOP_AUTH_RUNTIME_ROLE: runtimeRole,
72
+ PORT: DESKTOP_AUTH_PORT,
73
+ };
74
+ }
75
+ export function desktopAuthRevision(env) {
76
+ const sorted = Object.entries(env).sort(([a], [b]) => (a < b ? -1 : 1));
77
+ return createHash('sha256').update(JSON.stringify(sorted)).digest('hex').slice(0, 16);
78
+ }
79
+ /** The desktop-auth runtime role's name; the plan needs it for the revision before any URL exists. */
80
+ function runtimeRoleFor(org) {
81
+ const home = ORG_SERVICE_SCHEMAS.find((h) => h.schema === DESKTOP_AUTH_HOME);
82
+ if (!home)
83
+ throw new Error('the desktop_auth home is missing from the topology');
84
+ return orgServiceDatabase(org, home).runtimeRole;
85
+ }
86
+ /**
87
+ * The `coolify:desktop-auth` step. Planned when the app is not recorded, when the env it was written for
88
+ * differs (a rename moves the origin), when it never ran healthy, when the callback URI is not on Agora's
89
+ * client, and when Agora carries the upstream but the readiness probe never answered. Not planned while
90
+ * Agora is blocked (its own line says why) or while the `desktop_auth` home cannot exist yet (blocked).
91
+ * `homePlanned` is true when the databases step in this plan creates the home.
92
+ */
93
+ export function planDesktopAuth(org, recorded, input) {
94
+ if (!input.host)
95
+ return { step: null, blocked: null };
96
+ const home = ORG_SERVICE_SCHEMAS.find((h) => h.schema === DESKTOP_AUTH_HOME);
97
+ if (!home || (!recorded[home.scopeKey] && !input.homePlanned)) {
98
+ return {
99
+ step: null,
100
+ blocked: `${org.slug}'s desktop sign-in service is blocked, not planned: its database on the company Postgres server is not recorded yet`,
101
+ };
102
+ }
103
+ const { inputs } = desktopAuthInputs(org, recorded, input.host);
104
+ const callback = desktopAuthCallbackUri(desktopAuthOrigin(org));
105
+ const redirect = recorded[DESKTOP_AUTH_REDIRECT_SCOPE_KEY];
106
+ const blocked = recorded[DESKTOP_AUTH_APP_SCOPE_KEY] && redirect === desktopAuthRedirectUnsupported(callback)
107
+ ? `${org.slug}'s Agora client lacks the desktop callback ${callback}: the providers client cannot change redirect URIs; add it by hand in ZITADEL or update @wtfalch/providers, then remove the recorded key ${DESKTOP_AUTH_REDIRECT_SCOPE_KEY}`
108
+ : null;
109
+ const revision = inputs ? desktopAuthRevision(desktopAuthEnv(inputs, runtimeRoleFor(org))) : null;
110
+ const upstreamLive = Boolean(input.liveAgoraEnv?.AGORA_DESKTOP_AUTH_UPSTREAM);
111
+ const reasons = [];
112
+ if (!recorded[DESKTOP_AUTH_APP_SCOPE_KEY])
113
+ reasons.push('provision');
114
+ else {
115
+ if (revision && recorded[DESKTOP_AUTH_REVISION_SCOPE_KEY] !== revision)
116
+ reasons.push('env');
117
+ if (!recorded[DESKTOP_AUTH_DEPLOYED_SCOPE_KEY])
118
+ reasons.push('never ran healthy');
119
+ if (redirect !== callback && !blocked)
120
+ reasons.push('callback URI not on the Agora client');
121
+ if (upstreamLive && !recorded[DESKTOP_AUTH_READY_SCOPE_KEY])
122
+ reasons.push('not probed');
123
+ }
124
+ if (reasons.length === 0)
125
+ return { step: null, blocked };
126
+ return {
127
+ step: {
128
+ action: {
129
+ system: 'coolify',
130
+ kind: 'desktop-auth',
131
+ key: org.id,
132
+ desired: { host: input.host, agoraOrigin: desktopAuthOrigin(org) },
133
+ },
134
+ step: {
135
+ description: reasons[0] === 'provision'
136
+ ? `provision ${org.slug}'s desktop sign-in service`
137
+ : `update ${org.slug}'s desktop sign-in service (${reasons.join('; ')})`,
138
+ destroys: false,
139
+ migrates: false,
140
+ },
141
+ },
142
+ blocked,
143
+ };
144
+ }
@@ -0,0 +1,26 @@
1
+ import type { DeclaredOrg } from '../org.js';
2
+ import type { DraftStep } from './diff.js';
3
+ import type { LiveResource } from './live.js';
4
+ /**
5
+ * #339: the desktop app signs in through the company's web app, so every
6
+ * `@wtfalch/auth` web client foundry makes registers this path beside
7
+ * `/auth/callback`. Agora's own client (`/callback`) is not one of them.
8
+ */
9
+ export declare const DESKTOP_CALLBACK_PATH = "/auth/desktop/callback";
10
+ export declare function desktopCallbackUri(host: string): string;
11
+ /** The URIs to add to an app's current list: current first, then each wanted one it lacks. */
12
+ export declare function unionRedirectUris(current: readonly string[], wanted: readonly string[]): string[];
13
+ /**
14
+ * The ZITADEL app id of company app `appKey` once its desktop callback was
15
+ * registered. `planDesktopCallbacks` names an app while its recorded id
16
+ * differs, so an app made before the callback existed, or made again under a
17
+ * new id, is repaired once.
18
+ */
19
+ export declare function zitadelAppDesktopCallbackAppliedScopeKey(appKey: string): string;
20
+ /**
21
+ * One `zitadel:desktop-callback` step names every live web sign-in app whose
22
+ * recorded id differs from its marker. Plans nothing when the host's ZITADEL
23
+ * client lacks the two redirect URI calls (`wired`), before the org and
24
+ * project are recorded, or while the platform's sign-in is not ready.
25
+ */
26
+ export declare function planDesktopCallbacks(org: DeclaredOrg, live: readonly LiveResource[], recordedIds: Readonly<Record<string, string>>, plannedSteps: readonly DraftStep[], authReady: boolean, wired: boolean): DraftStep | null;
@@ -0,0 +1,59 @@
1
+ /**
2
+ * #339: the desktop app signs in through the company's web app, so every
3
+ * `@wtfalch/auth` web client foundry makes registers this path beside
4
+ * `/auth/callback`. Agora's own client (`/callback`) is not one of them.
5
+ */
6
+ export const DESKTOP_CALLBACK_PATH = '/auth/desktop/callback';
7
+ export function desktopCallbackUri(host) {
8
+ return `https://${host}${DESKTOP_CALLBACK_PATH}`;
9
+ }
10
+ /** The URIs to add to an app's current list: current first, then each wanted one it lacks. */
11
+ export function unionRedirectUris(current, wanted) {
12
+ return [...new Set([...current, ...wanted])];
13
+ }
14
+ /**
15
+ * The ZITADEL app id of company app `appKey` once its desktop callback was
16
+ * registered. `planDesktopCallbacks` names an app while its recorded id
17
+ * differs, so an app made before the callback existed, or made again under a
18
+ * new id, is repaired once.
19
+ */
20
+ export function zitadelAppDesktopCallbackAppliedScopeKey(appKey) {
21
+ return `zitadel_app_desktop_callback_applied.${appKey}`;
22
+ }
23
+ /** The fixed Agora client keeps its own `/callback`; it is not built on `@wtfalch/auth`. */
24
+ const NO_DESKTOP_CALLBACK_APPS = new Set(['agora']);
25
+ /**
26
+ * One `zitadel:desktop-callback` step names every live web sign-in app whose
27
+ * recorded id differs from its marker. Plans nothing when the host's ZITADEL
28
+ * client lacks the two redirect URI calls (`wired`), before the org and
29
+ * project are recorded, or while the platform's sign-in is not ready.
30
+ */
31
+ export function planDesktopCallbacks(org, live, recordedIds, plannedSteps, authReady, wired) {
32
+ if (!wired || !authReady || !recordedIds.zitadel_org || !recordedIds.zitadel_project)
33
+ return null;
34
+ const apps = live
35
+ .filter((r) => r.system === 'zitadel' &&
36
+ r.kind === 'app' &&
37
+ r.config.appType === 'web' &&
38
+ !NO_DESKTOP_CALLBACK_APPS.has(r.key))
39
+ .map((r) => r.key)
40
+ // An app this plan recreates gets its marker when it is made.
41
+ .filter((appKey) => !plannedSteps.some((s) => s.action.system === 'zitadel' &&
42
+ s.action.kind === 'app' &&
43
+ s.action.key === appKey &&
44
+ s.action.desired.recreate === true))
45
+ .filter((appKey) => {
46
+ const appId = recordedIds[`zitadel:app:${appKey}`];
47
+ return (Boolean(appId) && recordedIds[zitadelAppDesktopCallbackAppliedScopeKey(appKey)] !== appId);
48
+ });
49
+ if (apps.length === 0)
50
+ return null;
51
+ return {
52
+ action: { system: 'zitadel', kind: 'desktop-callback', key: org.id, desired: { apps } },
53
+ step: {
54
+ description: `register the desktop sign-in callback for ${org.slug}'s sign-in apps (${apps.join(', ')})`,
55
+ destroys: false,
56
+ migrates: false,
57
+ },
58
+ };
59
+ }
@@ -2,7 +2,7 @@ import type { DeclaredOrg, OrgId, OrgServiceSchema } from '../org.js';
2
2
  import type { Plan, PlanStep, UnmanagedItem, VersionChangeItem } from '../plan.js';
3
3
  import { type AgoraHostReading } from './agora.js';
4
4
  import { type BlueGreenServer } from './blue-green.js';
5
- import type { LiveResource } from './live.js';
5
+ import { type LiveResource } from './live.js';
6
6
  import { type OrgPlatformReadiness } from './platform-org.js';
7
7
  import { type RemoteDatabaseFacts } from './remote-database.js';
8
8
  /**
@@ -49,10 +49,16 @@ export interface PlanOrgOptions {
49
49
  oikosZoneReady?: boolean;
50
50
  /** #267: the executor was built with a `loginHostBindings` port. */
51
51
  loginHostBindings?: boolean;
52
+ /** The `loginHostBindings` port writes the `clientOrganizations` map (577). */
53
+ loginHostClientOrganizations?: boolean;
52
54
  /** A live read said the org's login host serves OIDC discovery as `https://<host>`. Absent: not read, so Agora keeps the platform issuer. */
53
55
  loginHostServesDiscovery?: boolean;
54
56
  /** The executor's ZITADEL client has `updateOidcAppIdTokenUserinfoAssertion`; without it `planIdTokenUserinfo` plans nothing. */
55
57
  zitadelIdTokenUserinfo?: boolean;
58
+ /** The executor's ZITADEL client has `updateOidcAppPostLogoutRedirectUris`; without it `planPostLogoutUris` plans nothing. */
59
+ zitadelPostLogoutUris?: boolean;
60
+ /** The executor's ZITADEL client has `getOidcAppRedirectUris` and `updateOidcAppRedirectUris` (providers 0.12.0); without them `planDesktopCallbacks` plans nothing. */
61
+ zitadelRedirectUris?: boolean;
56
62
  /** The executor's ZITADEL client has `ensureOidcAppMcpAccess` and `ensureAccessTokenLifetime`; without them `planMcp` is blocked. */
57
63
  zitadelMcp?: boolean;
58
64
  /** The executor's ZITADEL client has `setOrgMetadata` and `removeOrgMetadata`; without them the customers organisation's sign-in metadata is reported blocked. */
@@ -203,9 +209,10 @@ export declare function computePlanDetail(org: DeclaredOrg, live: LiveResource[]
203
209
  export declare function isPlatformOwnOrg(org: Pick<DeclaredOrg, 'id'>, platformOwnerOrgId: string | null): boolean;
204
210
  /**
205
211
  * What {@link planOrgPostgres} reports about the org's one shared Postgres
206
- * server and its fixed schema topology (#444a, "manifest-and-plan-only"):
207
- * missing only, nothing applied. Deliberately **not** folded into
208
- * `plannedSteps` -- the same reason {@link VersionChangeItem} (`../plan.ts`)
212
+ * server and its fixed schema topology (#444a). Since #444 `planOrgPostgresSteps`
213
+ * turns it into `coolify:org-postgres` and `coolify:org-postgres-databases` steps; this
214
+ * report stays. The old note below on why it was **not** folded into
215
+ * `plannedSteps` is kept for the history -- the same reason {@link VersionChangeItem} (`../plan.ts`)
209
216
  * never is, restated in `computePlanDetail`'s own doc comment below, but
210
217
  * sharper here: `plan-apply.ts`'s `runSteps` executes every
211
218
  * `detail.plannedSteps` entry through `StepExecutor.execute`, which
@@ -236,6 +243,8 @@ export interface OrgPostgresPlan {
236
243
  * it as part of the full topology.
237
244
  */
238
245
  missingSchemas: readonly OrgServiceSchema[];
246
+ /** Homes whose data is still in a legacy Coolify database. Empty for a new company. Informational; a step exists only after the confirmation. */
247
+ legacyUnmigrated: readonly OrgServiceSchema[];
239
248
  }
240
249
  /** Restated from `executor.ts`, the same rule: the id of an ai key minted but not delivered. */
241
250
  export declare const AI_KEY_UNDELIVERED_SCOPE_KEY = "ai_key_undelivered";