diff --git a/packages/cli/src/runtime-services.ts b/packages/cli/src/runtime-services.ts index 02f69100..b13f8d0e 100644 --- a/packages/cli/src/runtime-services.ts +++ b/packages/cli/src/runtime-services.ts @@ -25,6 +25,22 @@ export interface RuntimeServiceControlResult { reason?: string } +type ExternalMysqlFailureCode = "env-unavailable" | "invalid-port" | "network-policy-denied" | "network-policy-host-not-allowed" | "boundary-missing" | "boundary-host-not-allowlisted" | "boundary-host-blocked" | "boundary-writes-not-permitted" | "writes-not-approved" + +interface ExternalMysqlFailureReason { + code: ExternalMysqlFailureCode + message: string + field?: string + envName?: string +} + +class ExternalMysqlConfigurationError extends Error { + constructor(readonly reason: ExternalMysqlFailureReason) { + super(reason.message) + this.name = "ExternalMysqlConfigurationError" + } +} + export interface RuntimeServiceEvidence { id: string kind: string @@ -36,6 +52,7 @@ export interface RuntimeServiceEvidence { diagnostic?: { code: "readiness-failed" | "provision-failed" | "provider-unavailable" | "teardown-failed" | "interrupted" command?: string + reason?: ExternalMysqlFailureReason cause?: { code: string; message: string } } controls?: RuntimeServiceControlResult[] @@ -1379,11 +1396,13 @@ async function provisionMysqlExternalService(service: WorkspaceRecipeRuntimeServ try { connection = externalMysqlConnection(service, dependencies.environment ?? process.env) assertExternalMysqlAuthorization(service, connection.host, connection.port, context) - } catch { + } catch (error) { evidence.readiness = "failed" evidence.lifecycle = "failed" - evidence.diagnostic = { code: "provision-failed" } - throw new RuntimeServiceProvisionError(`Managed runtime service failed: ${service.id}`, evidenceList) + const reason = error instanceof ExternalMysqlConfigurationError ? error.reason : undefined + evidence.diagnostic = { code: "provision-failed", ...(reason ? { reason } : {}) } + const detail = reason ? ` (${reason.code}: ${reason.message})` : "" + throw new RuntimeServiceProvisionError(`Managed runtime service failed: ${service.id}${detail}`, evidenceList) } const client = engine === "mariadb" ? "mariadb" : "mysql" const suffix = dependencies.randomBytes(12).toString("hex") @@ -1478,25 +1497,26 @@ function externalMysqlConnection(service: WorkspaceRecipeRuntimeService, environ const password = configuredEnvironmentValue(configuration?.passwordEnv, environment, "passwordEnv", true) const portValue = configuration?.portEnv ? configuredEnvironmentValue(configuration.portEnv, environment, "portEnv") : "3306" const port = Number(portValue) - if (!Number.isInteger(port) || port < 1 || port > 65535) throw new Error("External MySQL port environment value must be an integer between 1 and 65535") + if (!Number.isInteger(port) || port < 1 || port > 65535) throw new ExternalMysqlConfigurationError({ code: "invalid-port", message: "External MySQL port environment value must be an integer between 1 and 65535" }) return { host, port, username, password } } function assertExternalMysqlAuthorization(service: WorkspaceRecipeRuntimeService, host: string, port: number, context: RuntimeServiceProvisionContext): void { const policy = context.policy - if (!policy || policy.network === "deny") throw new Error("External MySQL access is denied by runtime network policy") + if (!policy || policy.network === "deny") throw new ExternalMysqlConfigurationError({ code: "network-policy-denied", message: "External MySQL access is denied by runtime network policy" }) if (typeof policy.network === "object" && !hostListAllows(policy.network.allowHosts, host, port)) { - throw new Error("External MySQL host is not allowed by runtime network policy") + throw new ExternalMysqlConfigurationError({ code: "network-policy-host-not-allowed", message: "External MySQL host is not allowed by runtime network policy" }) } const boundaryId = service.configuration?.externalService const boundary = context.externalServices.find((candidate) => candidate.id === boundaryId) - if (!boundary || boundary.allowedHosts?.length === 0 || !hostListAllows(boundary.allowedHosts ?? [], host, port)) { - throw new Error("External MySQL host is not explicitly allowlisted by its external-service boundary") + if (!boundary) throw new ExternalMysqlConfigurationError({ code: "boundary-missing", message: "External MySQL external-service boundary is missing" }) + if (!hostListAllows(boundary.allowedHosts ?? [], host, port)) { + throw new ExternalMysqlConfigurationError({ code: "boundary-host-not-allowlisted", message: "External MySQL host is not explicitly allowlisted by its external-service boundary" }) } - if (hostListAllows(boundary.blockedHosts ?? [], host, port)) throw new Error("External MySQL host is blocked by its external-service boundary") - if (boundary.writes !== "allowed-with-approval") throw new Error("External MySQL boundary does not permit managed writes") - if (policy.approvals !== "on-write" || !context.externalServiceWritesApproved) throw new Error("External MySQL managed writes were not explicitly approved") + if (hostListAllows(boundary.blockedHosts ?? [], host, port)) throw new ExternalMysqlConfigurationError({ code: "boundary-host-blocked", message: "External MySQL host is blocked by its external-service boundary" }) + if (boundary.writes !== "allowed-with-approval") throw new ExternalMysqlConfigurationError({ code: "boundary-writes-not-permitted", message: "External MySQL boundary does not permit managed writes" }) + if (policy.approvals !== "on-write" || !context.externalServiceWritesApproved) throw new ExternalMysqlConfigurationError({ code: "writes-not-approved", message: "External MySQL managed writes were not explicitly approved" }) } function hostListAllows(allowedHosts: readonly string[], host: string, port: number): boolean { @@ -1509,9 +1529,10 @@ function hostListAllows(allowedHosts: readonly string[], host: string, port: num } function configuredEnvironmentValue(name: string | undefined, environment: Record, field: string, allowEmpty = false): string { - if (!name || !/^[A-Z_][A-Z0-9_]*$/.test(name)) throw new Error(`External MySQL ${field} must reference a runtime environment variable`) + // Only validated variable names may enter diagnostics; never include environment values. + if (!name || !/^[A-Z_][A-Z0-9_]*$/.test(name)) throw new ExternalMysqlConfigurationError({ code: "env-unavailable", field, message: `${field} must reference a runtime environment variable` }) const value = environment[name] - if (value === undefined || (!allowEmpty && value.trim() === "")) throw new Error(`External MySQL ${field} environment variable is unavailable`) + if (value === undefined || (!allowEmpty && value.trim() === "")) throw new ExternalMysqlConfigurationError({ code: "env-unavailable", field, envName: name, message: `${field} ${name} environment variable is unavailable` }) return value } diff --git a/tests/external-mysql-runtime-service.test.ts b/tests/external-mysql-runtime-service.test.ts index 83592159..e33a0fda 100644 --- a/tests/external-mysql-runtime-service.test.ts +++ b/tests/external-mysql-runtime-service.test.ts @@ -115,6 +115,62 @@ function fakeDependencies(handle?: (call: FakeCall, controller?: AbortController } } +// Every preflight refusal must retain a stable, secret-free reason without connecting. +type ProvisionOptions = NonNullable[1]> +async function assertPreflightReason( + code: string, + message: string, + options: ProvisionOptions = {}, + service = externalService, + environment: Record = {}, + details: Record = {}, +): Promise { + const fake = fakeDependencies() + Object.assign(fake.dependencies.environment!, environment) + await assert.rejects(provisionRuntimeServices([service], { dependencies: fake.dependencies, ...authorization, ...options }), (error: unknown) => { + assert.ok(error instanceof RuntimeServiceProvisionError) + const diagnostic = error.evidence[0]?.diagnostic + assert.equal(diagnostic?.code, "provision-failed") + assert.deepEqual(diagnostic?.reason, { code, message, ...details }) + assert.equal(error.message, `Managed runtime service failed: external-db (${code}: ${message})`) + const serialized = JSON.stringify({ message: error.message, evidence: error.evidence }) + for (const value of Object.values(fake.dependencies.environment!)) { + if (value) assert.equal(serialized.includes(value), false, "environment values must never enter diagnostics") + } + assert.equal(serialized.includes(adminPassword), false) + return true + }) + assert.equal(fake.calls.length, 0) +} + +for (const [field, envName] of Object.entries({ hostEnv: "MYSQL_ADMIN_HOST", usernameEnv: "MYSQL_ADMIN_USER", passwordEnv: "MYSQL_ADMIN_PASSWORD", portEnv: "MYSQL_ADMIN_PORT" })) { + await assertPreflightReason("env-unavailable", `${field} ${envName} environment variable is unavailable`, {}, externalService, { [envName]: undefined }, { field, envName }) +} +for (const name of [undefined, "not-a-variable", adminPassword]) { + await assertPreflightReason("env-unavailable", "passwordEnv must reference a runtime environment variable", {}, { + ...externalService, configuration: { ...externalService.configuration, passwordEnv: name }, + }, {}, { field: "passwordEnv" }) +} +for (const envName of ["MYSQL_ADMIN_HOST", "MYSQL_ADMIN_USER", "MYSQL_ADMIN_PORT"]) { + const field = { MYSQL_ADMIN_HOST: "hostEnv", MYSQL_ADMIN_USER: "usernameEnv", MYSQL_ADMIN_PORT: "portEnv" }[envName]! + await assertPreflightReason("env-unavailable", `${field} ${envName} environment variable is unavailable`, {}, externalService, { [envName]: " " }, { field, envName }) +} +for (const port of ["not-a-port", "0", "65536", "3307.5"]) { + await assertPreflightReason("invalid-port", "External MySQL port environment value must be an integer between 1 and 65535", {}, externalService, { MYSQL_ADMIN_PORT: port }) +} +for (const deniedPolicy of [undefined, { ...policy, network: "deny" as const }]) { + await assertPreflightReason("network-policy-denied", "External MySQL access is denied by runtime network policy", { policy: deniedPolicy }) +} +await assertPreflightReason("network-policy-host-not-allowed", "External MySQL host is not allowed by runtime network policy", { policy: { ...policy, network: { allowHosts: [] } } }) +await assertPreflightReason("boundary-missing", "External MySQL external-service boundary is missing", { externalServices: [] }) +for (const allowedHosts of [undefined, [], ["other.internal"]]) { + await assertPreflightReason("boundary-host-not-allowlisted", "External MySQL host is not explicitly allowlisted by its external-service boundary", { externalServices: [{ ...externalServices[0]!, allowedHosts }] }) +} +await assertPreflightReason("boundary-host-blocked", "External MySQL host is blocked by its external-service boundary", { externalServices: [{ ...externalServices[0]!, blockedHosts: ["database.internal"] }] }) +await assertPreflightReason("boundary-writes-not-permitted", "External MySQL boundary does not permit managed writes", { externalServices: [{ ...externalServices[0]!, writes: "forbidden" }] }) +await assertPreflightReason("writes-not-approved", "External MySQL managed writes were not explicitly approved", { externalServiceWritesApproved: false }) +await assertPreflightReason("writes-not-approved", "External MySQL managed writes were not explicitly approved", { policy: { ...policy, approvals: "never" } }) + const deniedPolicyFake = fakeDependencies() await assert.rejects(provisionRuntimeServices([externalService], { dependencies: deniedPolicyFake.dependencies, ...authorization, policy: { ...policy, network: "deny" } }), RuntimeServiceProvisionError) assert.equal(deniedPolicyFake.calls.length, 0, "denied network policy fails before connecting")