diff --git a/docs/guide/platform/installation/ci.md b/docs/guide/platform/installation/ci.md index 321676163..c1d8ab0bd 100644 --- a/docs/guide/platform/installation/ci.md +++ b/docs/guide/platform/installation/ci.md @@ -18,7 +18,8 @@ Inject the following values from protected CI secrets. Do not commit them in a w | `VOID_PLATFORM_DATABASE_URL` | Dedicated PostgreSQL URL | | `VOID_PLATFORM_HYPERDRIVE_ID` | Existing externally managed Hyperdrive ID, when applicable | | `VOID_PLATFORM_HYPERDRIVE_ORIGIN_HOST` | Origin host of that Hyperdrive; set with its ID and user | -| `VOID_PLATFORM_HYPERDRIVE_ORIGIN_USER` | Runtime database user of that Hyperdrive; set with its ID and host | +| `VOID_PLATFORM_HYPERDRIVE_ORIGIN_USER` | Runtime database user of that Hyperdrive; set with its ID and host (or service ID) | +| `VOID_PLATFORM_HYPERDRIVE_ORIGIN_SERVICE_ID` | For a Workers VPC origin, set the service ID in place of the origin host | | `VOID_PLATFORM_R2_ACCESS_KEY_ID` | R2 Access Key ID | | `VOID_PLATFORM_R2_SECRET_ACCESS_KEY` | R2 Secret Access Key | | `VOID_PLATFORM_JWT_SECRET` | Original JWT signing secret | diff --git a/docs/guide/platform/installation/prerequisites.md b/docs/guide/platform/installation/prerequisites.md index 120f793a7..3200c6a69 100644 --- a/docs/guide/platform/installation/prerequisites.md +++ b/docs/guide/platform/installation/prerequisites.md @@ -170,7 +170,7 @@ Save the printed URL in your password manager and paste it into Void's `PostgreS The installer can create Hyperdrive for you or use one you manage separately. For an existing configuration: 1. Point it at the dedicated platform database using a runtime user, and disable SQL result caching. -2. Select it in the installer and review the database, host, port, and runtime user. +2. Select it in the installer and review the database, host and port (or Workers VPC service ID), and runtime user. 3. Supply a database owner connection at the PostgreSQL URL prompt so Void can apply migrations. To create one first, choose **Set up a separately managed Hyperdrive** and follow the printed instructions or [Cloudflare’s setup guide](https://developers.cloudflare.com/hyperdrive/get-started/). Rerun the installer once it is ready. Void leaves its configuration under your control. For unattended setup, use the Hyperdrive variables in [Install from CI](/guide/platform/installation/ci). diff --git a/packages/void/src/cli/platform-auth-database.ts b/packages/void/src/cli/platform-auth-database.ts index 9534ad576..0951668c4 100644 --- a/packages/void/src/cli/platform-auth-database.ts +++ b/packages/void/src/cli/platform-auth-database.ts @@ -2,8 +2,10 @@ import type { InstallationResource } from '@void/platform'; import { hyperdriveOriginConfigHash, hyperdriveOriginFromConnectionString, + hyperdriveOriginLocationMatches, hyperdriveOriginMatches, type CloudflareHyperdriveConfig, + type CloudflareHyperdriveOriginLocation, type PlatformCloudflareClient, } from './platform-cloudflare.ts'; import type { PlatformExternalHyperdrive } from './platform-plan.ts'; @@ -21,6 +23,10 @@ export function readPlatformHyperdriveSelection( } catch { throw new Error('platform: The saved Hyperdrive selection is invalid.'); } + return parsePlatformHyperdriveSelection(value); +} + +export function parsePlatformHyperdriveSelection(value: unknown): PlatformExternalHyperdrive { const selected = value as Partial | null; const nonempty = (value: unknown): value is string => typeof value === 'string' && value.trim() !== ''; @@ -29,7 +35,9 @@ export function readPlatformHyperdriveSelection( typeof selected !== 'object' || Array.isArray(selected) || !nonempty(selected.id) || - !nonempty(selected.host) || + (nonempty(selected.serviceId) + ? selected.host !== undefined + : selected.serviceId !== undefined || !nonempty(selected.host)) || !nonempty(selected.user) || (selected.name !== undefined && (typeof selected.name !== 'string' || !selected.name)) || (selected.database !== undefined && @@ -39,7 +47,9 @@ export function readPlatformHyperdriveSelection( } return { id: selected.id, - host: selected.host, + ...(selected.serviceId !== undefined + ? { serviceId: selected.serviceId } + : { host: selected.host! }), user: selected.user, ...(selected.name ? { name: selected.name } : {}), ...(selected.database ? { database: selected.database } : {}), @@ -67,19 +77,28 @@ export function platformDatabaseNameFromOwnerUrl(databaseUrl: string): string { /** Pin an externally managed Hyperdrive without treating the owner URL as its runtime origin. */ export function verifyExternalPlatformHyperdrive(input: { config: CloudflareHyperdriveConfig; - databaseUrl: string; - expected: { id: string; name: string; host: string; user: string }; + databaseUrl?: string; + expected: CloudflareHyperdriveOriginLocation & { + id: string; + name?: string; + database?: string; + user: string; + }; recordedHash?: string; }): string { const { config, expected } = input; - const database = platformDatabaseNameFromOwnerUrl(input.databaseUrl); + const database = + input.databaseUrl === undefined + ? undefined + : platformDatabaseNameFromOwnerUrl(input.databaseUrl); const hash = hyperdriveOriginConfigHash(config.origin); if ( config.id !== expected.id || - config.name !== expected.name || - config.origin.host !== expected.host.toLowerCase() || + (expected.name !== undefined && config.name !== expected.name) || + !hyperdriveOriginLocationMatches(config.origin, expected) || config.origin.user !== expected.user || - config.origin.database !== database || + (database !== undefined && config.origin.database !== database) || + (expected.database !== undefined && config.origin.database !== expected.database) || (input.recordedHash && input.recordedHash !== hash) ) { throw new Error( diff --git a/packages/void/src/cli/platform-cloudflare.ts b/packages/void/src/cli/platform-cloudflare.ts index f430acb61..20cfc8789 100644 --- a/packages/void/src/cli/platform-cloudflare.ts +++ b/packages/void/src/cli/platform-cloudflare.ts @@ -84,15 +84,25 @@ export type CloudflareNamedResource = Readonly<{ id: string; name: string; }>; -export type CloudflareHyperdriveOrigin = Readonly<{ - accessClientId?: string; +export type CloudflareHyperdriveOriginLocation = Readonly< + { host: string; serviceId?: never } | { serviceId: string; host?: never } +>; +type CloudflareHyperdriveDatabase = Readonly<{ database: string; - host: string; - /** Tunnel-backed origins get their database port from the tunnel service. */ - port: number | null; scheme: 'postgresql'; user: string | null; }>; +type CloudflareHyperdriveHostOrigin = CloudflareHyperdriveDatabase & + Readonly<{ + accessClientId?: string; + host: string; + serviceId?: never; + /** Tunnel-backed origins get their database port from the tunnel service. */ + port: number | null; + }>; +export type CloudflareHyperdriveOrigin = + | CloudflareHyperdriveHostOrigin + | (CloudflareHyperdriveDatabase & Readonly<{ serviceId: string; host?: never }>); export type CloudflareHyperdriveConfig = CloudflareNamedResource & Readonly<{ origin: CloudflareHyperdriveOrigin; cachingDisabled?: boolean }>; export type CloudflareDnsRecord = CloudflareNamedResource & @@ -1691,7 +1701,9 @@ export function parsePostgresConnectionString(value: string): { }; } -export function hyperdriveOriginFromConnectionString(value: string): CloudflareHyperdriveOrigin { +export function hyperdriveOriginFromConnectionString( + value: string, +): CloudflareHyperdriveHostOrigin { const parsed = parsePostgresConnectionString(value); return { database: parsed.database, @@ -1707,21 +1719,34 @@ export function hyperdriveOriginConfigHash(origin: CloudflareHyperdriveOrigin): .update( JSON.stringify({ database: origin.database, - host: origin.host.toLowerCase(), - port: origin.port, + ...(origin.serviceId !== undefined + ? { serviceId: origin.serviceId.toLowerCase() } + : { host: origin.host.toLowerCase(), port: origin.port }), scheme: 'postgresql', user: origin.user, - ...(origin.accessClientId ? { accessClientId: origin.accessClientId } : {}), + ...(origin.serviceId === undefined && origin.accessClientId + ? { accessClientId: origin.accessClientId } + : {}), }), ) .digest('hex'); } +export function hyperdriveOriginLocationMatches( + live: CloudflareHyperdriveOriginLocation, + expected: CloudflareHyperdriveOriginLocation, +): boolean { + return expected.serviceId !== undefined + ? live.serviceId?.toLowerCase() === expected.serviceId.toLowerCase() + : live.host?.toLowerCase() === expected.host.toLowerCase(); +} + export function hyperdriveOriginMatches( live: CloudflareHyperdriveOrigin, - expected: CloudflareHyperdriveOrigin, + expected: CloudflareHyperdriveHostOrigin, ): boolean { return ( + live.serviceId === undefined && live.database === expected.database && live.host.toLowerCase() === expected.host.toLowerCase() && live.port === expected.port && @@ -1741,18 +1766,40 @@ function parseHyperdriveConfig(value: Record): CloudflareHyperd if (scheme !== 'postgres' && scheme !== 'postgresql') { throw new Error(`platform: Unsupported Hyperdrive origin scheme ${JSON.stringify(scheme)}.`); } - const accessClientId = - typeof record.access_client_id === 'string' && record.access_client_id - ? record.access_client_id - : undefined; - const port = - accessClientId && record.port == null - ? null - : typeof record.port === 'number' || typeof record.port === 'string' - ? Number(record.port) - : NaN; - if (port !== null && (!Number.isInteger(port) || port <= 0 || port > 65_535)) { - throw new Error('platform: Cloudflare Hyperdrive config has an invalid origin port.'); + const database: CloudflareHyperdriveDatabase = { + database: requiredString(record.database, 'Hyperdrive origin database'), + scheme: 'postgresql', + user: typeof record.user === 'string' && record.user ? record.user : null, + }; + let parsedOrigin: CloudflareHyperdriveOrigin; + if (record.service_id != null) { + const serviceId = requiredString(record.service_id, 'Hyperdrive origin service ID'); + if (record.host != null || record.port != null || record.access_client_id != null) { + throw new Error( + 'platform: Cloudflare Hyperdrive config mixes Workers VPC and host origin fields.', + ); + } + parsedOrigin = { ...database, serviceId }; + } else { + const accessClientId = + typeof record.access_client_id === 'string' && record.access_client_id + ? record.access_client_id + : undefined; + const port = + accessClientId && record.port == null + ? null + : typeof record.port === 'number' || typeof record.port === 'string' + ? Number(record.port) + : NaN; + if (port !== null && (!Number.isInteger(port) || port <= 0 || port > 65_535)) { + throw new Error('platform: Cloudflare Hyperdrive config has an invalid origin port.'); + } + parsedOrigin = { + ...database, + host: requiredString(record.host, 'Hyperdrive origin host').toLowerCase(), + port, + ...(accessClientId ? { accessClientId } : {}), + }; } return { id: requiredString(value.id, 'Hyperdrive id'), @@ -1762,13 +1809,6 @@ function parseHyperdriveConfig(value: Record): CloudflareHyperd typeof value.caching === 'object' && !Array.isArray(value.caching) && (value.caching as Record).disabled === true, - origin: { - ...(accessClientId ? { accessClientId } : {}), - database: requiredString(record.database, 'Hyperdrive origin database'), - host: requiredString(record.host, 'Hyperdrive origin host').toLowerCase(), - port, - scheme: 'postgresql', - user: typeof record.user === 'string' && record.user ? record.user : null, - }, + origin: parsedOrigin, }; } diff --git a/packages/void/src/cli/platform-installer.ts b/packages/void/src/cli/platform-installer.ts index 137c822a9..b5bab3be4 100644 --- a/packages/void/src/cli/platform-installer.ts +++ b/packages/void/src/cli/platform-installer.ts @@ -301,9 +301,9 @@ export async function installPlatform( config: live, databaseUrl: secrets.databaseUrl, expected: { + ...(plan.externalHyperdrive ?? live.origin), id: hyperdriveAction.id, name: hyperdriveAction.name, - host: plan.externalHyperdrive?.host ?? live.origin.host, user: plan.externalHyperdrive?.user ?? live.origin.user ?? '', }, ...(recordedHyperdrive ? { recordedHash: recordedHyperdrive.configHash } : {}), diff --git a/packages/void/src/cli/platform-lifecycle.ts b/packages/void/src/cli/platform-lifecycle.ts index 566bf6457..e5da1b570 100644 --- a/packages/void/src/cli/platform-lifecycle.ts +++ b/packages/void/src/cli/platform-lifecycle.ts @@ -21,7 +21,10 @@ import { prepareInstallationAppAccess, } from './platform-app-access.ts'; import type { PlatformArgs } from './platform-args.ts'; -import { readPlatformHyperdriveSelection } from './platform-auth-database.ts'; +import { + readPlatformHyperdriveSelection, + verifyExternalPlatformHyperdrive, +} from './platform-auth-database.ts'; import { assertPlatformOperatorCredentialCanApplyPlan, platformOperatorNeedsManagementToken, @@ -30,6 +33,7 @@ import { resolvePlatformCloudflareCredentialWithInput, } from './platform-auth.ts'; import { + hyperdriveOriginLocationMatches, PlatformCloudflareClient, PlatformCloudflareApiError, PlatformRuntimeTokenEmailPermissionError, @@ -78,6 +82,7 @@ import { updateProjectWorkerRuntimeLogs, } from './platform-runtime-logs.ts'; import { + findSavedPlatformInstallPlan, listSavedPlatformInstallPlans, savePlatformInstallChoices, removeSavedPlatformInstallPlan, @@ -196,6 +201,7 @@ export async function runPlatformLifecycleCommand(commandArgs: PlatformArgs): Pr let existing = args.resume ? resolveInstallationManifest(args.name) : null; let selectedInstall = false; let selectedDraft = false; + let reuseSavedPlan = !args.plan; let installationName = args.name; const { input: installationSlug, credential: operatorCredential } = await resolvePlatformCloudflareCredentialWithInput(async () => { @@ -284,6 +290,7 @@ export async function runPlatformLifecycleCommand(commandArgs: PlatformArgs): Pr throw new Error('platform: Cancelled.'); } if (choice === 'new') { + reuseSavedPlan = false; // Do not let --name silently reselect the draft the user just declined. requestedName = ''; } else if (choice.startsWith('plan:')) { @@ -291,6 +298,7 @@ export async function runPlatformLifecycleCommand(commandArgs: PlatformArgs): Pr if (!saved) { throw new Error('platform: The selected plan is no longer available.'); } + reuseSavedPlan = true; args = { ...saved.args, plan: args.plan, yes: args.yes }; runtime = loadPlatformRuntime(args.runtime); installationName = args.name; @@ -520,13 +528,22 @@ export async function runPlatformLifecycleCommand(commandArgs: PlatformArgs): Pr } } } - const savedHyperdrive = readPlatformHyperdriveSelection(checkpoint); - const requestedHyperdriveId = args.hyperdrive || savedHyperdrive?.id; + const externalHyperdriveId = process.env.VOID_PLATFORM_HYPERDRIVE_ID?.trim() || ''; + const selectedHyperdriveId = args.hyperdrive || externalHyperdriveId; + const savedPlanHyperdrive = + !existing && reuseSavedPlan && selectedHyperdriveId && selectedHyperdriveId !== 'create' + ? findSavedPlatformInstallPlan(accountId, installationSlug)?.externalHyperdrive + : undefined; + const checkpointHyperdrive = readPlatformHyperdriveSelection(checkpoint); + const savedHyperdrive = + checkpointHyperdrive ?? + (savedPlanHyperdrive?.id === selectedHyperdriveId ? savedPlanHyperdrive : undefined); + const requestedHyperdriveId = args.hyperdrive || checkpointHyperdrive?.id; const recordedHyperdrive = existing?.resources.find( (resource) => resource.key === 'hyperdrive', ); if ( - (savedHyperdrive && requestedHyperdriveId !== savedHyperdrive.id) || + (checkpointHyperdrive && requestedHyperdriveId !== checkpointHyperdrive.id) || (recordedHyperdrive && requestedHyperdriveId && (requestedHyperdriveId === 'create' @@ -541,21 +558,27 @@ export async function runPlatformLifecycleCommand(commandArgs: PlatformArgs): Pr 'platform: The selected Hyperdrive differs from the saved installation. Resume with its original Hyperdrive.', ); } - const externalHyperdriveId = process.env.VOID_PLATFORM_HYPERDRIVE_ID?.trim() || ''; const externalHyperdriveHost = process.env.VOID_PLATFORM_HYPERDRIVE_ORIGIN_HOST?.trim() || ''; + const serviceId = + process.env.VOID_PLATFORM_HYPERDRIVE_ORIGIN_SERVICE_ID?.trim().toLowerCase() || ''; const externalHyperdriveUser = process.env.VOID_PLATFORM_HYPERDRIVE_ORIGIN_USER?.trim() || ''; + if (externalHyperdriveHost && serviceId) { + throw new Error( + 'platform: Specify either a Hyperdrive origin host or a Workers VPC service ID.', + ); + } if ( - [externalHyperdriveId, externalHyperdriveHost, externalHyperdriveUser].some(Boolean) && - ![externalHyperdriveId, externalHyperdriveHost, externalHyperdriveUser].every(Boolean) + (externalHyperdriveId || externalHyperdriveHost || serviceId || externalHyperdriveUser) && + !(externalHyperdriveId && (externalHyperdriveHost || serviceId) && externalHyperdriveUser) ) { throw new Error( - 'platform: VOID_PLATFORM_HYPERDRIVE_ID, VOID_PLATFORM_HYPERDRIVE_ORIGIN_HOST, and VOID_PLATFORM_HYPERDRIVE_ORIGIN_USER must be set together.', + 'platform: Set VOID_PLATFORM_HYPERDRIVE_ID and VOID_PLATFORM_HYPERDRIVE_ORIGIN_USER with either VOID_PLATFORM_HYPERDRIVE_ORIGIN_HOST or VOID_PLATFORM_HYPERDRIVE_ORIGIN_SERVICE_ID.', ); } let externalHyperdrive: PlatformExternalHyperdrive | undefined = externalHyperdriveId ? { id: externalHyperdriveId, - host: externalHyperdriveHost, + ...(serviceId ? { serviceId } : { host: externalHyperdriveHost }), user: externalHyperdriveUser, } : undefined; @@ -569,14 +592,18 @@ export async function runPlatformLifecycleCommand(commandArgs: PlatformArgs): Pr if ( savedHyperdrive && externalHyperdrive && - (savedHyperdrive.host.toLowerCase() !== externalHyperdrive.host.toLowerCase() || + (!hyperdriveOriginLocationMatches(savedHyperdrive, externalHyperdrive) || savedHyperdrive.user !== externalHyperdrive.user) ) { throw new Error('platform: The Hyperdrive origin differs from the saved installation.'); } - if (savedHyperdrive) { - externalHyperdrive = recordedHyperdrive ? savedHyperdrive : undefined; + if (checkpointHyperdrive) { + externalHyperdrive = recordedHyperdrive ? checkpointHyperdrive : undefined; + } else if (savedHyperdrive) { + externalHyperdrive = savedHyperdrive; } + const configuredServiceOrigin = + externalHyperdrive?.serviceId !== undefined ? externalHyperdrive : undefined; if ( !externalHyperdrive && requestedHyperdriveId !== 'create' && @@ -649,7 +676,7 @@ export async function runPlatformLifecycleCommand(commandArgs: PlatformArgs): Pr } if ( (savedHyperdrive && - (live.origin.host !== savedHyperdrive.host.toLowerCase() || + (!hyperdriveOriginLocationMatches(live.origin, savedHyperdrive) || live.origin.user !== savedHyperdrive.user || (savedHyperdrive.name && live.name !== savedHyperdrive.name) || (savedHyperdrive.database && live.origin.database !== savedHyperdrive.database))) || @@ -665,8 +692,12 @@ export async function runPlatformLifecycleCommand(commandArgs: PlatformArgs): Pr note( [ `${pc.bold('Database:')} ${live.origin.database}`, - `${pc.bold('Host:')} ${live.origin.host}`, - `${pc.bold('Port:')} ${live.origin.port ?? 'configured by Cloudflare Tunnel'}`, + ...(live.origin.serviceId !== undefined + ? [`${pc.bold('Workers VPC service:')} ${live.origin.serviceId}`] + : [ + `${pc.bold('Host:')} ${live.origin.host}`, + `${pc.bold('Port:')} ${live.origin.port ?? 'configured by Cloudflare Tunnel'}`, + ]), `${pc.bold('User:')} ${live.origin.user}`, `${pc.bold('SQL result caching:')} ${live.cachingDisabled ? 'disabled' : 'enabled'}`, ].join('\n'), @@ -682,11 +713,37 @@ export async function runPlatformLifecycleCommand(commandArgs: PlatformArgs): Pr id: live.id, name: live.name, database: live.origin.database, - host: live.origin.host, + ...(live.origin.serviceId !== undefined + ? { serviceId: live.origin.serviceId } + : { host: live.origin.host }), user: live.origin.user, }; + if (externalHyperdrive.serviceId !== undefined) { + verifyExternalPlatformHyperdrive({ + config: live, + databaseUrl: databaseUrl || undefined, + expected: externalHyperdrive, + }); + } } } + if (configuredServiceOrigin) { + const live = await client.getHyperdriveConfig(accountId, configuredServiceOrigin.id); + verifyExternalPlatformHyperdrive({ + config: live, + databaseUrl: databaseUrl || undefined, + expected: { + ...configuredServiceOrigin, + name: recordedHyperdrive?.name ?? configuredServiceOrigin.name, + }, + recordedHash: recordedHyperdrive?.configHash, + }); + externalHyperdrive = { + ...configuredServiceOrigin, + name: live.name, + database: live.origin.database, + }; + } const authenticationSelection = await selectInstallationAuthentication( args.authConfig, existing?.deployment.authentication, @@ -789,6 +846,8 @@ export async function runPlatformLifecycleCommand(commandArgs: PlatformArgs): Pr yes: false, }).slice(2), plan.account.name, + undefined, + externalHyperdrive?.serviceId ? externalHyperdrive : undefined, ); } note( diff --git a/packages/void/src/cli/platform-management.ts b/packages/void/src/cli/platform-management.ts index 862c02f56..106aab050 100644 --- a/packages/void/src/cli/platform-management.ts +++ b/packages/void/src/cli/platform-management.ts @@ -1957,6 +1957,9 @@ async function verifyPlatformMutationOwnership( if (exists) { const legacyHash = legacyResourceConfigHash(resource.kind, resource.name); const malformedLegacyHash = !/^[a-f0-9]{64}$/.test(resource.configHash); + if (live.origin.serviceId !== undefined && resource.ownership !== 'adopted') { + throw ownershipError(`hyperdrive ${resource.name} changed to a Workers VPC origin`); + } if (options.databaseUrl && resource.ownership !== 'adopted') { const expected = hyperdriveOriginFromConnectionString(options.databaseUrl); if (!hyperdriveOriginMatches(live.origin, expected)) { diff --git a/packages/void/src/cli/platform-plan.ts b/packages/void/src/cli/platform-plan.ts index 693366400..528ed5736 100644 --- a/packages/void/src/cli/platform-plan.ts +++ b/packages/void/src/cli/platform-plan.ts @@ -26,6 +26,7 @@ import { import type { CloudflareAccount, CloudflareDnsRecord, + CloudflareHyperdriveOriginLocation, CloudflareNamedResource, CloudflareWorkerDomain, CloudflareWorkerRoute, @@ -55,13 +56,13 @@ export type PlatformInstallPlan = Readonly<{ zone: CloudflareZone | null; }>; -export type PlatformExternalHyperdrive = Readonly<{ - id: string; - name?: string; - database?: string; - host: string; - user: string; -}>; +export type PlatformExternalHyperdrive = CloudflareHyperdriveOriginLocation & + Readonly<{ + id: string; + name?: string; + database?: string; + user: string; + }>; type PlatformInstallPlanInput = Readonly<{ appAccess?: PlatformAppAccess; diff --git a/packages/void/src/cli/platform-saved-plans.ts b/packages/void/src/cli/platform-saved-plans.ts index cfeb5716d..93b5f1c12 100644 --- a/packages/void/src/cli/platform-saved-plans.ts +++ b/packages/void/src/cli/platform-saved-plans.ts @@ -3,6 +3,8 @@ import { mkdirSync, readFileSync, readdirSync, rmSync } from 'node:fs'; import { join } from 'pathslash'; import { writeFileAtomic } from '../shared/atomic-write.ts'; import { parsePlatformArgs, type PlatformArgs } from './platform-args.ts'; +import { parsePlatformHyperdriveSelection } from './platform-auth-database.ts'; +import type { PlatformExternalHyperdrive } from './platform-plan.ts'; import { platformRegistryDirectory } from './platform-registry.ts'; type SavedPlatformInstallPlan = Readonly<{ @@ -10,9 +12,10 @@ type SavedPlatformInstallPlan = Readonly<{ accountName: string; savedAt: string; args: Extract; + externalHyperdrive?: PlatformExternalHyperdrive; }>; -function planId(args: SavedPlatformInstallPlan['args']): string { +function planId(args: Pick): string { return createHash('sha256') .update(JSON.stringify([args.account, args.name])) .digest('hex'); @@ -50,19 +53,34 @@ function parseSavedPlan(value: unknown): SavedPlatformInstallPlan { ) { throw new Error('Invalid installation plan metadata.'); } - return { id: planId(args), args, accountName: record.accountName, savedAt: record.savedAt }; + const externalHyperdrive = + record.externalHyperdrive === undefined + ? undefined + : parsePlatformHyperdriveSelection(record.externalHyperdrive); + if (externalHyperdrive && externalHyperdrive.id !== args.hyperdrive) { + throw new Error('Saved Hyperdrive selection does not match the plan.'); + } + return { + id: planId(args), + args, + accountName: record.accountName, + savedAt: record.savedAt, + ...(externalHyperdrive ? { externalHyperdrive } : {}), + }; } export function savePlatformInstallChoices( arguments_: Array, accountName: string, directory: string = platformRegistryDirectory(), + externalHyperdrive?: PlatformExternalHyperdrive, ): SavedPlatformInstallPlan { const record = { schemaVersion: 1, arguments: arguments_, accountName, savedAt: new Date().toISOString(), + externalHyperdrive, }; const plan = parseSavedPlan(record); mkdirSync(directory, { recursive: true, mode: 0o700 }); @@ -74,6 +92,39 @@ export function savePlatformInstallChoices( return plan; } +function readSavedPlatformInstallPlan(path: string, id: string): SavedPlatformInstallPlan { + try { + const plan = parseSavedPlan(JSON.parse(readFileSync(path, 'utf8'))); + if (id !== plan.id) { + throw new Error('Plan identity does not match its file.'); + } + return plan; + } catch (error) { + if ((error as NodeJS.ErrnoException).code === 'ENOENT') { + throw error; + } + throw new Error( + `platform: Invalid saved installation plan ${path}: ${error instanceof Error ? error.message : String(error)}`, + ); + } +} + +export function findSavedPlatformInstallPlan( + account: string, + name: string, + directory: string = platformRegistryDirectory(), +): SavedPlatformInstallPlan | undefined { + const id = planId({ account, name }); + try { + return readSavedPlatformInstallPlan(join(directory, `plan-${id}.json`), id); + } catch (error) { + if ((error as NodeJS.ErrnoException).code === 'ENOENT') { + return undefined; + } + throw error; + } +} + export function listSavedPlatformInstallPlans( directory: string = platformRegistryDirectory(), ): ReadonlyArray { @@ -88,20 +139,9 @@ export function listSavedPlatformInstallPlans( } return entries .filter((entry) => entry.isFile() && /^plan-[a-f0-9]{64}\.json$/.test(entry.name)) - .map((entry) => { - const path = join(directory, entry.name); - try { - const plan = parseSavedPlan(JSON.parse(readFileSync(path, 'utf8'))); - if (entry.name !== `plan-${plan.id}.json`) { - throw new Error('Plan identity does not match its file.'); - } - return plan; - } catch (error) { - throw new Error( - `platform: Invalid saved installation plan ${path}: ${error instanceof Error ? error.message : String(error)}`, - ); - } - }) + .map((entry) => + readSavedPlatformInstallPlan(join(directory, entry.name), entry.name.slice(5, -5)), + ) .sort( (left, right) => right.savedAt.localeCompare(left.savedAt) || left.id.localeCompare(right.id), ); diff --git a/packages/void/test/unit/platform-auth-database.test.ts b/packages/void/test/unit/platform-auth-database.test.ts index 5932ee373..addf2fa22 100644 --- a/packages/void/test/unit/platform-auth-database.test.ts +++ b/packages/void/test/unit/platform-auth-database.test.ts @@ -29,6 +29,43 @@ const config = { }; describe('fresh platform authentication database reads', () => { + it('restores and verifies a saved Workers VPC origin for a separate owner URL', () => { + const selected = { + id: resource.id, + name: resource.name, + database: 'platform', + serviceId: 'f537187c-73f7-4ad8-bcb6-38e3e3ac1177', + user: 'runtime', + }; + const ownerUrl = 'postgresql://iam_owner@127.0.0.1/platform?sslmode=disable'; + expect( + readPlatformHyperdriveSelection({ externalHyperdriveJson: JSON.stringify(selected) }), + ).toEqual(selected); + const live = { + ...config, + origin: { + database: 'platform', + scheme: 'postgresql' as const, + user: selected.user, + serviceId: selected.serviceId, + }, + }; + const recordedHash = verifyExternalPlatformHyperdrive({ + config: live, + databaseUrl: ownerUrl, + expected: selected, + }); + expect(recordedHash).toMatch(/^[a-f0-9]{64}$/); + expect(() => + verifyExternalPlatformHyperdrive({ + config: { ...live, origin: { ...live.origin, serviceId: 'different-service' } }, + databaseUrl: ownerUrl, + expected: selected, + recordedHash, + }), + ).toThrow('expected database origin'); + }); + it('rejects incomplete saved Hyperdrive selections without exposing checkpoint contents', () => { expect(readPlatformHyperdriveSelection(null)).toBeUndefined(); for (const externalHyperdriveJson of [ diff --git a/packages/void/test/unit/platform-cloudflare.test.ts b/packages/void/test/unit/platform-cloudflare.test.ts index ee757d202..ca8c4a395 100644 --- a/packages/void/test/unit/platform-cloudflare.test.ts +++ b/packages/void/test/unit/platform-cloudflare.test.ts @@ -478,12 +478,51 @@ describe('platform Cloudflare client', () => { accessClientId: 'tunnel-client-id', }, }); - expect(hyperdriveOriginConfigHash(config.origin)).not.toBe( - hyperdriveOriginConfigHash({ ...config.origin, accessClientId: 'different-client-id' }), + const origin = config.origin; + if (origin.serviceId !== undefined) { + throw new Error('Expected a Tunnel-backed origin.'); + } + expect(hyperdriveOriginConfigHash(origin)).not.toBe( + hyperdriveOriginConfigHash({ ...origin, accessClientId: 'different-client-id' }), + ); + expect(hyperdriveOriginConfigHash(config.origin)).toBe( + '299b4b480634c413c7bad8cee0e3419c836da5bf8e6227af3cbb3273d0a359df', ); expect(JSON.stringify(config)).not.toContain('secret'); }); + it('reads a Workers VPC origin without a hostname or port', async () => { + const client = new PlatformCloudflareClient('token', { + fetcher: async () => + Response.json({ + success: true, + result: { + id: 'hyperdrive-id', + name: 'managed-db', + caching: { disabled: true }, + origin: { + database: 'void', + service_id: 'f537187c-73f7-4ad8-bcb6-38e3e3ac1177', + scheme: 'postgres', + user: 'runtime-user', + }, + }, + }), + }); + + await expect(client.getHyperdriveConfig('account', 'hyperdrive-id')).resolves.toEqual({ + id: 'hyperdrive-id', + name: 'managed-db', + cachingDisabled: true, + origin: { + database: 'void', + serviceId: 'f537187c-73f7-4ad8-bcb6-38e3e3ac1177', + scheme: 'postgresql', + user: 'runtime-user', + }, + }); + }); + it.each([undefined, null, 0, 65_536, 5432.5, 'invalid', true, [5432]])( 'rejects a public Hyperdrive with invalid port %s', async (port) => { diff --git a/packages/void/test/unit/platform-install-prompts.test.ts b/packages/void/test/unit/platform-install-prompts.test.ts index da766fd33..bc4d737ba 100644 --- a/packages/void/test/unit/platform-install-prompts.test.ts +++ b/packages/void/test/unit/platform-install-prompts.test.ts @@ -10,6 +10,7 @@ import { parsePlatformArgs } from '../../src/cli/platform-args.ts'; import { PlatformCloudflareClient } from '../../src/cli/platform-cloudflare.ts'; import { loadPlatformRuntime } from '../../src/cli/platform-installer.ts'; import { runPlatformLifecycleCommand } from '../../src/cli/platform-lifecycle.ts'; +import { listSavedPlatformInstallPlans } from '../../src/cli/platform-saved-plans.ts'; const mocks = vi.hoisted(() => ({ confirm: vi.fn(), @@ -239,6 +240,181 @@ describe('interactive platform installation prompts', () => { ); }); + it.each([ + { origin: { serviceId: 'f537187c-73f7-4ad8-bcb6-38e3e3ac1178' } }, + { origin: { user: 'different_user' } }, + { origin: { database: 'different_database' } }, + { name: 'renamed-postgres' }, + ])('rejects Workers VPC selection drift on saved-plan replay: %j', async (drift) => { + const serviceId = 'f537187c-73f7-4ad8-bcb6-38e3e3ac1177'; + vi.stubEnv('VOID_PLATFORM_HYPERDRIVE_ID', 'terraform-id'); + vi.stubEnv('VOID_PLATFORM_HYPERDRIVE_ORIGIN_SERVICE_ID', serviceId); + vi.stubEnv('VOID_PLATFORM_HYPERDRIVE_ORIGIN_USER', 'runtime_user'); + vi.stubEnv('VOID_PLATFORM_DATABASE_URL', 'postgresql://iam_owner@127.0.0.1/void_platform'); + const live = { + id: 'terraform-id', + name: 'company-managed-postgres', + cachingDisabled: true, + origin: { + database: 'void_platform', + scheme: 'postgresql' as const, + serviceId, + user: 'runtime_user', + }, + }; + const getConfig = vi.spyOn(PlatformCloudflareClient.prototype, 'getHyperdriveConfig'); + const changed = { + ...live, + name: drift.name ?? live.name, + origin: { ...live.origin, ...drift.origin }, + }; + getConfig.mockResolvedValueOnce(live).mockResolvedValueOnce(changed); + + const command = parsePlatformArgs([...args, '--workers-dev']); + await runPlatformLifecycleCommand(command); + const saved = listSavedPlatformInstallPlans()[0]!; + expect(saved.args.hyperdrive).toBe(live.id); + expect(saved.externalHyperdrive).toEqual({ + id: live.id, + name: live.name, + database: live.origin.database, + serviceId, + user: live.origin.user, + }); + vi.stubEnv('VOID_PLATFORM_HYPERDRIVE_ID', undefined); + vi.stubEnv('VOID_PLATFORM_HYPERDRIVE_ORIGIN_SERVICE_ID', undefined); + vi.stubEnv('VOID_PLATFORM_HYPERDRIVE_ORIGIN_USER', undefined); + vi.mocked(PlatformCloudflareClient.prototype.listHyperdriveConfigs).mockResolvedValue([ + { id: changed.id, name: changed.name }, + ]); + await expect(runPlatformLifecycleCommand({ ...saved.args, yes: true })).rejects.toThrow( + 'expected database origin', + ); + expect(mocks.plan).toHaveBeenCalledTimes(1); + }); + + it('selects a Workers VPC Hyperdrive by configuration ID', async () => { + const origin = { + serviceId: 'f537187c-73f7-4ad8-bcb6-38e3e3ac1177', + database: 'platform', + scheme: 'postgresql' as const, + user: 'runtime', + }; + const live = { id: 'terraform-id', name: 'managed-postgres', origin, cachingDisabled: true }; + vi.mocked(PlatformCloudflareClient.prototype.listHyperdriveConfigs).mockResolvedValue([ + { id: live.id, name: live.name }, + ]); + vi.spyOn(PlatformCloudflareClient.prototype, 'getHyperdriveConfig').mockResolvedValue(live); + await runPlatformLifecycleCommand( + parsePlatformArgs([...args, '--workers-dev', '--hyperdrive', live.id]), + ); + expect(mocks.plan.mock.calls[0]?.[1].externalHyperdrive).toMatchObject({ + id: live.id, + serviceId: origin.serviceId, + user: origin.user, + }); + }); + + it('uses the new Hyperdrive selection after declining a saved plan', async () => { + const live = { + id: 'terraform-id', + name: 'managed-postgres', + cachingDisabled: true, + origin: { + database: 'platform', + scheme: 'postgresql' as const, + serviceId: 'f537187c-73f7-4ad8-bcb6-38e3e3ac1177', + user: 'runtime', + }, + }; + vi.stubEnv('VOID_PLATFORM_HYPERDRIVE_ID', live.id); + vi.stubEnv('VOID_PLATFORM_HYPERDRIVE_ORIGIN_SERVICE_ID', live.origin.serviceId); + vi.stubEnv('VOID_PLATFORM_HYPERDRIVE_ORIGIN_USER', live.origin.user); + const getConfig = vi + .spyOn(PlatformCloudflareClient.prototype, 'getHyperdriveConfig') + .mockResolvedValueOnce(live); + await runPlatformLifecycleCommand(parsePlatformArgs([...args, '--workers-dev'])); + + const currentUser = 'replacement_runtime'; + vi.stubEnv('VOID_PLATFORM_HYPERDRIVE_ORIGIN_USER', currentUser); + getConfig.mockResolvedValueOnce({ ...live, origin: { ...live.origin, user: currentUser } }); + mocks.select.mockResolvedValueOnce('new').mockResolvedValueOnce('workers-dev'); + mocks.text.mockResolvedValueOnce('test'); + + await runPlatformLifecycleCommand(parsePlatformArgs(['install', '--account', 'account'])); + expect(mocks.select).toHaveBeenCalledWith( + expect.objectContaining({ + message: 'Continue a previous platform install?', + options: expect.arrayContaining([ + expect.objectContaining({ label: 'Start a new platform install' }), + ]), + }), + ); + expect(listSavedPlatformInstallPlans()).toEqual([ + expect.objectContaining({ + args: expect.objectContaining({ name: 'test', hyperdrive: live.id }), + externalHyperdrive: expect.objectContaining({ user: currentUser }), + }), + ]); + }); + + it('refreshes a saved Workers VPC origin when asked for a new CI plan', async () => { + Object.defineProperty(process.stdin, 'isTTY', { configurable: true, value: false }); + const serviceId = 'f537187c-73f7-4ad8-bcb6-38e3e3ac1177'; + vi.stubEnv('VOID_PLATFORM_HYPERDRIVE_ID', 'terraform-id'); + vi.stubEnv('VOID_PLATFORM_HYPERDRIVE_ORIGIN_SERVICE_ID', serviceId); + vi.stubEnv('VOID_PLATFORM_HYPERDRIVE_ORIGIN_USER', 'runtime'); + const live = { + id: 'terraform-id', + name: 'managed-postgres', + cachingDisabled: true, + origin: { database: 'platform', scheme: 'postgresql' as const, serviceId, user: 'runtime' }, + }; + const getConfig = vi + .spyOn(PlatformCloudflareClient.prototype, 'getHyperdriveConfig') + .mockResolvedValueOnce(live); + const command = parsePlatformArgs([ + ...args, + '--workers-dev', + '--login-methods', + 'github', + '--no-access-protection', + '--app-access', + 'public', + ]); + await runPlatformLifecycleCommand(command); + + vi.stubEnv('VOID_PLATFORM_HYPERDRIVE_ORIGIN_USER', 'replacement_runtime'); + getConfig.mockResolvedValueOnce({ + ...live, + origin: { ...live.origin, user: 'replacement_runtime' }, + }); + await runPlatformLifecycleCommand(command); + + expect(listSavedPlatformInstallPlans()).toEqual([ + expect.objectContaining({ + args: expect.objectContaining({ name: 'test', hyperdrive: 'terraform-id' }), + externalHyperdrive: expect.objectContaining({ user: 'replacement_runtime' }), + }), + ]); + }); + + it('accepts an uppercase Workers VPC service ID in CI', async () => { + const serviceId = 'f537187c-73f7-4ad8-bcb6-38e3e3ac1177'; + vi.stubEnv('VOID_PLATFORM_HYPERDRIVE_ID', 'terraform-id'); + vi.stubEnv('VOID_PLATFORM_HYPERDRIVE_ORIGIN_SERVICE_ID', serviceId.toUpperCase()); + vi.stubEnv('VOID_PLATFORM_HYPERDRIVE_ORIGIN_USER', 'runtime'); + vi.spyOn(PlatformCloudflareClient.prototype, 'getHyperdriveConfig').mockResolvedValue({ + id: 'terraform-id', + name: 'managed-postgres', + cachingDisabled: true, + origin: { database: 'platform', scheme: 'postgresql', serviceId, user: 'runtime' }, + }); + + await runPlatformLifecycleCommand(parsePlatformArgs([...args, '--workers-dev'])); + expect(listSavedPlatformInstallPlans()[0]?.externalHyperdrive?.serviceId).toBe(serviceId); + }); + it('suggests a matching name without requiring it', async () => { vi.spyOn(PlatformCloudflareClient.prototype, 'listHyperdriveConfigs').mockResolvedValue([ { id: 'other-id', name: 'company-managed-postgres' }, diff --git a/packages/void/test/unit/platform-installer.test.ts b/packages/void/test/unit/platform-installer.test.ts index a49ae3907..29a50dbb8 100644 --- a/packages/void/test/unit/platform-installer.test.ts +++ b/packages/void/test/unit/platform-installer.test.ts @@ -4774,6 +4774,64 @@ describe('platform installer', () => { expect(JSON.parse(checkpoint.externalHyperdriveJson!)).toEqual(plan.externalHyperdrive); }); + it('rechecks the Workers VPC service before claiming the database on apply', async () => { + useRegistryDirectory(); + const databaseUrl = 'postgresql://iam_owner@127.0.0.1/void?sslmode=disable'; + const plan = await createPlatformInstallPlan( + { + ...planningClient(), + listHyperdriveConfigs: async () => [{ id: 'external-hyperdrive', name: 'managed-db' }], + }, + { + ...EXAMPLE_PLAN_INPUT, + externalHyperdrive: { + id: 'external-hyperdrive', + serviceId: 'f537187c-73f7-4ad8-bcb6-38e3e3ac1177', + user: 'runtime', + }, + }, + null, + ); + const config = { + id: 'external-hyperdrive', + name: 'managed-db', + cachingDisabled: true, + origin: { + database: 'void', + scheme: 'postgresql' as const, + serviceId: 'f537187c-73f7-4ad8-bcb6-38e3e3ac1178', + user: 'runtime', + }, + }; + const claimDatabase = vi.fn(async () => { + throw new Error('stop after claim'); + }); + const getHyperdriveConfig = vi.fn(async () => config); + const dependencies = { + acquireDatabaseLifecycleLock, + claimDatabase, + client: { ...unusedClient(), getHyperdriveConfig }, + revalidateBeforeWorkerDeploy: async () => {}, + }; + await expect( + installPlatform(plan, freshSecrets({ databaseUrl }), null, dependencies), + ).rejects.toThrow('expected database origin'); + expect(claimDatabase).not.toHaveBeenCalled(); + + getHyperdriveConfig.mockResolvedValue({ + ...config, + origin: { ...config.origin, serviceId: 'f537187c-73f7-4ad8-bcb6-38e3e3ac1177' }, + }); + await expect( + installPlatform(plan, freshSecrets({ databaseUrl }), null, dependencies), + ).rejects.toThrow('stop after claim'); + expect(claimDatabase).toHaveBeenCalledExactlyOnceWith( + databaseUrl, + expect.any(String), + expect.any(String), + ); + }); + it('resumes a claimed verify-full database URL by creating Hyperdrive with its TLS mode', async () => { const directory = useRegistryDirectory(); const databaseUrl = 'postgresql://user:password@db.example.com:5432/void?sslmode=verify-full'; diff --git a/packages/void/test/unit/platform-recovery.test.ts b/packages/void/test/unit/platform-recovery.test.ts index 2f0ce6e5e..0c5f3da9b 100644 --- a/packages/void/test/unit/platform-recovery.test.ts +++ b/packages/void/test/unit/platform-recovery.test.ts @@ -318,6 +318,67 @@ describe('platform recovery', () => { }); }); + it('recovers a Workers VPC Hyperdrive by origin identity even when caching is enabled', async () => { + const directory = useRegistryDir(); + const remote = remoteManifest(); + const resource = remote.resources.find((entry) => entry.key === 'hyperdrive')!; + const manifest = recordInstallationResource( + remote, + { + ...resource, + ownership: 'adopted', + configHash: 'fa8ba14273c6fe1dce2e7b58bfac73a3049b0a8a7e30e37d59f47544cc1702f2', + }, + remote.updatedAt, + ); + const live = { + id: resource.id, + name: resource.name, + cachingDisabled: true, + origin: { + database: 'void', + scheme: 'postgresql' as const, + serviceId: 'f537187c-73f7-4ad8-bcb6-38e3e3ac1177', + user: 'user', + }, + }; + const fake = client(manifest); + await expect( + recoverPlatformInstallations( + { + ...fake, + getHyperdriveConfig: async () => ({ + ...live, + origin: { ...live.origin, serviceId: live.origin.serviceId.toUpperCase() }, + }), + }, + 'account', + async () => {}, + ), + ).resolves.toHaveLength(1); + expect(listInstallationManifests(directory)[0]?.id).toBe(remote.id); + await expect( + recoverPlatformInstallations( + { + ...fake, + getHyperdriveConfig: async () => ({ + ...live, + origin: { ...live.origin, serviceId: 'different-service' }, + }), + }, + 'account', + async () => {}, + ), + ).rejects.toThrow('changed configuration'); + await expect( + recoverPlatformInstallations( + { ...fake, getHyperdriveConfig: async () => ({ ...live, cachingDisabled: false }) }, + 'account', + async () => {}, + ), + ).resolves.toHaveLength(1); + }); + it('recovers a verified installation with a missing owned Worker so repair can recreate it', async () => { const directory = useRegistryDir(); const remote = remoteManifest();