diff --git a/server/src/__tests__/ephemeral-server-contract.test.ts b/server/src/__tests__/ephemeral-server-contract.test.ts index e7f73205b2d..b1a2639ae32 100644 --- a/server/src/__tests__/ephemeral-server-contract.test.ts +++ b/server/src/__tests__/ephemeral-server-contract.test.ts @@ -17,6 +17,17 @@ it("requires shared storage and stable secrets without changing volume-backed st } expect(() => assertEphemeralServerConfig({ ...config, storageProvider: "local_disk" }, env)).toThrow("shared S3"); }); +it("waives an absent listener only for a caller that opts in, and still requires a postgres runtime database", () => { + const unset = { ...env, PAPERCLIP_DATABASE_LISTEN_URL: "" }; + expect(() => assertEphemeralServerConfig(config, unset)).toThrow(/PAPERCLIP_DATABASE_LISTEN_URL/); + expect(assertEphemeralServerConfig(config, unset, { allowMissingListener: true })).toBeNull(); + expect(() => assertEphemeralServerConfig({ ...config, databaseUrl: "not a url" }, unset, { allowMissingListener: true })) + .toThrow(/an external postgres DATABASE_URL is required/); + expect(() => assertEphemeralServerConfig({ ...config, databaseUrl: "mysql://runtime@db.test/tenant" }, unset, { allowMissingListener: true })) + .toThrow(/an external postgres DATABASE_URL is required/); + expect(() => assertEphemeralServerConfig(config, { ...env, PAPERCLIP_DATABASE_LISTEN_URL: "postgres://runtime:fixture@other.test/tenant" }, + { allowMissingListener: true })).toThrow(/direct connection to the same database/); +}); it("never connects the listener with migration privileges, another tenant, or through a transaction pooler", () => { for (const url of [env.PAPERCLIP_DATABASE_LISTEN_URL.replace("runtime:", "migration:"), env.PAPERCLIP_DATABASE_LISTEN_URL.replace("/tenant", "/neighbor"), config.databaseUrl, diff --git a/server/src/services/ephemeral-enrollment.test.ts b/server/src/services/ephemeral-enrollment.test.ts index d51d9dbaba1..cd641ba0b29 100644 --- a/server/src/services/ephemeral-enrollment.test.ts +++ b/server/src/services/ephemeral-enrollment.test.ts @@ -65,7 +65,6 @@ it.each([ ["PAPERCLIP_DURABLE_AGENT_FILES", "volume_free_storage_flags_required"], ["PAPERCLIP_SHARED_UI_ASSETS", "volume_free_storage_flags_required"], ["PAPERCLIP_SECRETS_MASTER_KEY", "volume_free_master_key_required"], - ["PAPERCLIP_DATABASE_LISTEN_URL", "volume_free_database_listener_required"], ])("names a missing %s as a blocker in preparation and in the eligibility read", async (setting, blocker) => { vi.stubEnv(setting, ""); await expect(prepareOnlineEphemeralEnrollment(db, attempt, control)).rejects.toMatchObject({ status: 409, details: { blockers: [blocker] } }); @@ -127,3 +126,28 @@ it("names the skill blocker once when installations and packages both still need owners.eligibility.mockResolvedValue({ eligible: false, blockers: ["skills_require_migration"] }); await expect(readOnlineEphemeralEnrollmentEligibility(db)).resolves.toMatchObject({ blockers: ["skills_require_migration"] }); }); + +it("accepts a volume server without a listener because Cloud supplies it to the replacement", async () => { + vi.stubEnv("PAPERCLIP_DATABASE_LISTEN_URL", ""); + await expect(readOnlineEphemeralEnrollmentEligibility(db)).resolves.toEqual({ eligible: true, blockers: [], ephemeralServer: false }); + await expect(prepareOnlineEphemeralEnrollment(db, attempt, control)).resolves.toMatchObject({ enrollmentReady: false, decisionSigningSecret: key }); +}); + +it("still names a listener that points somewhere else", async () => { + vi.stubEnv("PAPERCLIP_DATABASE_LISTEN_URL", "postgres://fixture:fixture@other.test/database"); + await expect(readOnlineEphemeralEnrollmentEligibility(db)).resolves.toMatchObject({ eligible: false, blockers: ["volume_free_database_listener_required"] }); +}); + +it("does not waive an unparseable runtime database because the listener is unset", async () => { + vi.stubEnv("PAPERCLIP_DATABASE_LISTEN_URL", ""); + const config = await import("../config.js"); + const original = config.loadConfig; + vi.spyOn(config, "loadConfig").mockReturnValue({ ...original(), databaseUrl: "not a url" } as never); + await expect(readOnlineEphemeralEnrollmentEligibility(db)).resolves.toMatchObject({ eligible: false, blockers: ["volume_free_configuration_required"] }); +}); + +it("still names another missing setting when the listener is unset", async () => { + vi.stubEnv("PAPERCLIP_DATABASE_LISTEN_URL", ""); + vi.stubEnv("PAPERCLIP_SHARED_UI_ASSETS", ""); + await expect(readOnlineEphemeralEnrollmentEligibility(db)).resolves.toMatchObject({ eligible: false, blockers: ["volume_free_storage_flags_required"] }); +}); diff --git a/server/src/services/ephemeral-enrollment.ts b/server/src/services/ephemeral-enrollment.ts index ab84e46e8d2..b668a4546f6 100644 --- a/server/src/services/ephemeral-enrollment.ts +++ b/server/src/services/ephemeral-enrollment.ts @@ -26,7 +26,7 @@ function volumeFreeConfigurationBlocker(): { blocker: string | null; decisionSig } try { assertEphemeralServerConfig(loadConfig(), { ...process.env, PAPERCLIP_EPHEMERAL_SERVER: "true", - PAPERCLIP_DECISION_SIGNING_SECRET: decisionSigningSecret }); + PAPERCLIP_DECISION_SIGNING_SECRET: decisionSigningSecret }, { allowMissingListener: true }); } catch (error) { const message = error instanceof Error ? error.message : ""; return { blocker: configurationBlockers.find(([pattern]) => pattern.test(message))?.[1] ?? "volume_free_configuration_required" }; diff --git a/server/src/services/ephemeral-server-contract.ts b/server/src/services/ephemeral-server-contract.ts index 955ef14acf8..629bf01b026 100644 --- a/server/src/services/ephemeral-server-contract.ts +++ b/server/src/services/ephemeral-server-contract.ts @@ -18,7 +18,8 @@ export { ephemeralServerEnabled }; /** Validate before consumers can generate a replacement local identity. Error * messages name settings only, never credential values or connection URLs. */ -export function assertEphemeralServerConfig(config: Pick, env = process.env): string | null { +export function assertEphemeralServerConfig(config: Pick, env = process.env, + options: { allowMissingListener?: boolean } = {}): string | null { if (env.PAPERCLIP_EPHEMERAL_SERVER !== "true") return null; const refuse = (reason: string): never => { throw new Error(`Volume-free server configuration: ${reason}`); }; if (config.storageProvider !== "s3" || env.PAPERCLIP_DURABLE_AGENT_FILES !== "true" || env.PAPERCLIP_SHARED_UI_ASSETS !== "true") { @@ -31,7 +32,16 @@ export function assertEphemeralServerConfig(config: Pick ["postgres:", "postgresql:"].includes(url.protocol)) || runtime.username !== listener.username || runtime.password !== listener.password || runtime.pathname !== listener.pathname