diff --git a/apple/TableProMobile/TableProMobile/Coordinators/ConnectionSessionModel.swift b/apple/TableProMobile/TableProMobile/Coordinators/ConnectionSessionModel.swift index 597025a969..1291979f5d 100644 --- a/apple/TableProMobile/TableProMobile/Coordinators/ConnectionSessionModel.swift +++ b/apple/TableProMobile/TableProMobile/Coordinators/ConnectionSessionModel.swift @@ -79,7 +79,8 @@ final class ConnectionSessionModel { @ObservationIgnored private var placementEpoch = 0 @ObservationIgnored private var switchesInFlight = 0 @ObservationIgnored private var placementRestored = false - @ObservationIgnored private var rememberedDatabaseHung = false + // Why the last dial into the remembered database failed with no fallback; the next open starts from the saved one. + @ObservationIgnored private var rememberedDialFailure: (any Error)? @ObservationIgnored private var scenes: [WeakScene] = [] private let appState: AppState @@ -136,6 +137,11 @@ final class ConnectionSessionModel { record.type == .postgresql || record.type == .redshift } + // MySQL, MariaDB and TiDB sign in apart from the database; SQL Server signs contained users in to the named one. + private var dialsRememberedDatabase: Bool { + redialsToSwitchDatabase || record.type == .mysql || record.type == .mariadb || record.type == .tidb + } + func adopt(_ record: DatabaseConnection) { guard record.id == self.record.id, record != self.record else { return } self.record = record @@ -271,6 +277,10 @@ final class ConnectionSessionModel { // The record changed how it dials, so the next window opens it again. func reset() { let attempt = state.attempt + // What failed was the old record's dial. + if !redialsToSwitchDatabase { + rememberedDialFailure = nil + } enter(.cancel) if attempt?.purpose == .open { attempt?.task?.cancel() } leaveDial() @@ -320,12 +330,17 @@ final class ConnectionSessionModel { do { let saved = record // A remembered database that never answered may hang again, so the next open starts from the saved one and - // switches after, as opens did before. - let target = rememberedDatabaseHung ? record : openTarget(restoring: wanted) - rememberedDatabaseHung = false + // switches after, as opens did before. A USE engine does so after any failure it could not fall back from. + let failedBefore = rememberedDialFailure + rememberedDialFailure = nil + let target = failedBefore == nil ? openTarget(restoring: wanted) : record let landed: OpenLanding var restoring = wanted var refused: RefusedDatabase? + // The saved database's list then says whether the remembered one is gone, as after a refused dial. + if let failedBefore, !redialsToSwitchDatabase, !wanted.database.isEmpty, wanted.database != saved.database { + refused = RefusedDatabase(name: wanted.database, error: failedBefore, reportedBySwitch: true) + } do { landed = try await dialForOpen( target, schema: schemaRestored(by: target, restoring: wanted), by: attempt, joining: allowSignIn @@ -333,12 +348,13 @@ final class ConnectionSessionModel { } catch let thrown { let listRefused = thrown as? TableListRefused let error = listRefused?.error ?? thrown - if target.database != saved.database, case PostgreSQLError.unreachable = error { - rememberedDatabaseHung = true + if target.database != saved.database, + startsNextOpenFromSaved(after: error, listRefused: listRefused != nil, by: attempt) { + rememberedDialFailure = error } // A sign-in is the connection's, not the database's: the saved database would fail the same way. guard target.database != saved.database, isCurrent(attempt), - listRefused != nil || Self.mayBlameDatabase(error), + listRefused != nil || blamesDatabase(error), SignInOffer.method(for: error, connection: saved, secureStore: appState.secureStore) == nil else { throw error } Self.logger.warning("Failed to open remembered database \(target.database, privacy: .private(mask: .hash)): \(LogRedaction.publicDescription(of: error), privacy: .public) \(error.localizedDescription, privacy: .private)") @@ -346,8 +362,11 @@ final class ConnectionSessionModel { await appState.connectionManager.disconnect(connectionId) guard isCurrent(attempt) else { return } landed = try await dialForOpen(saved, schema: nil, by: attempt, joining: false) - restoring.database = saved.database - refused = RefusedDatabase(name: target.database, error: error) + // A USE engine still moves there with USE, which says why it fails, as its open always did. + if redialsToSwitchDatabase { + restoring.database = saved.database + } + refused = RefusedDatabase(name: target.database, error: error, reportedBySwitch: !redialsToSwitchDatabase) } guard isCurrent(attempt) else { return } let newSession = landed.outcome.session @@ -391,11 +410,9 @@ final class ConnectionSessionModel { } } - // Where the connection was, so the first table list is already the right one. Only PostgreSQL and Redshift, which - // need a second connection to change database. The others dial the saved one and move with USE, as before, so their - // refusals, which the app cannot always tell apart from a failed sign-in, never decide between two dials. + // Where the connection was, so the first table list is already the right one. private func openTarget(restoring wanted: Placement) -> DatabaseConnection { - guard redialsToSwitchDatabase, !wanted.database.isEmpty else { return record } + guard dialsRememberedDatabase, !wanted.database.isEmpty else { return record } var target = record target.database = wanted.database return target @@ -419,6 +436,8 @@ final class ConnectionSessionModel { private struct RefusedDatabase { let name: String let error: any Error + // The open's own USE tried it again and said why that failed. + let reportedBySwitch: Bool } nonisolated private struct TableListRefused: Error { @@ -440,6 +459,22 @@ final class ConnectionSessionModel { return !notAboutTheDatabase.contains { message.contains($0) } } + // The MySQL driver types a refused database by its code. + private func blamesDatabase(_ error: any Error) -> Bool { + if redialsToSwitchDatabase { return Self.mayBlameDatabase(error) } + if case MySQLError.databaseRefused = error { return true } + return false + } + + // A USE engine's other failure may still be the database's on a server that numbers it its own way. + private func startsNextOpenFromSaved(after error: any Error, listRefused: Bool, by attempt: ConnectionAttempt) -> Bool { + if redialsToSwitchDatabase { + if case PostgreSQLError.unreachable = error { return true } + return false + } + return isCurrent(attempt) && !listRefused && !blamesDatabase(error) && !(error is CancellationError) + } + private nonisolated static let notAboutTheDatabase = [ "password authentication failed", "authentication failed", "fe_sendauth", "no password supplied", "could not connect", "connection refused", "timeout expired", "timed out", "could not translate host name", @@ -502,6 +537,7 @@ final class ConnectionSessionModel { reportingTo scene: ConnectionCoordinator? ) { guard let listed, !listed.contains(refused.name) else { + guard !refused.reportedBySwitch else { return } scene?.presentFailure( title: ConnectionCoordinator.switchDatabaseFailedTitle, message: classify(refused.error, operation: "switchDatabase").message diff --git a/apple/TableProMobile/TableProMobile/Drivers/MySQLDriver.swift b/apple/TableProMobile/TableProMobile/Drivers/MySQLDriver.swift index 47bb123785..0de4d5e048 100644 --- a/apple/TableProMobile/TableProMobile/Drivers/MySQLDriver.swift +++ b/apple/TableProMobile/TableProMobile/Drivers/MySQLDriver.swift @@ -438,6 +438,13 @@ nonisolated final class MySQLDriver: DatabaseDriver, SessionDatabaseReporting, @ } } +// The sign-in went through and the database was refused as a USE there is (scripts/probes/check-mysql-dial-database-refusal.sh). +nonisolated enum MySQLDatabaseRefusal { + static func isRefusal(code: UInt32) -> Bool { + code == 1_044 || code == 1_049 + } +} + nonisolated enum MySQLTokenSignIn { // A token session refuses a server without TLS before the token is sent; this names that refusal. static func connectFailure( @@ -735,8 +742,13 @@ private actor MySQLActor { ) { throw tokenFailure } - throw MariaDBSSLClassifier.classifySSLError(code: failure.code, message: failure.message) - ?? MySQLError.connectionFailed(failure.message) + if let tlsFailure = MariaDBSSLClassifier.classifySSLError(code: failure.code, message: failure.message) { + throw tlsFailure + } + if MySQLDatabaseRefusal.isRefusal(code: failure.code) { + throw MySQLError.databaseRefused(failure.message) + } + throw MySQLError.connectionFailed(failure.message) } guard MariaDBCharacterSet.establishSession(on: handle, encoding: encoding) else { @@ -1258,13 +1270,15 @@ nonisolated private struct RawMySQLResult: Sendable { nonisolated enum MySQLError: Error, LocalizedError { case connectionFailed(String) + // The database the handshake named was refused; it reads as any failed connect. + case databaseRefused(String) case notConnected case queryFailed(String) case unsupported(String) var errorDescription: String? { switch self { - case .connectionFailed(let msg): return "MySQL connection failed: \(msg)" + case .connectionFailed(let msg), .databaseRefused(let msg): return "MySQL connection failed: \(msg)" case .notConnected: return "Not connected to MySQL database" case .queryFailed(let msg): return "MySQL query failed: \(msg)" case .unsupported(let msg): return msg diff --git a/apple/TableProMobile/TableProMobileTests/ConnectionCoordinatorStatusTests.swift b/apple/TableProMobile/TableProMobileTests/ConnectionCoordinatorStatusTests.swift index dc40e04fc1..17b19e114f 100644 --- a/apple/TableProMobile/TableProMobileTests/ConnectionCoordinatorStatusTests.swift +++ b/apple/TableProMobile/TableProMobileTests/ConnectionCoordinatorStatusTests.swift @@ -434,6 +434,13 @@ struct ConnectionCoordinatorStatusTests { #expect(ranUpdate(second)) } + // An open dials the remembered database first; this one refuses it as a dropped database. + private func goneStagingDriver() -> MockDatabaseDriver { + let gone = driver() + gone.connectError = MySQLError.databaseRefused("Unknown database 'app_staging'") + return gone + } + // A window on app_staging whose redial failed; `beforeLoss` runs while it is still connected. private func lostOnStaging( beforeLoss: (ConnectionCoordinator) -> Void = { _ in } @@ -450,8 +457,9 @@ struct ConnectionCoordinatorStatusTests { return coordinator } - // Another window opens the connection, which dials the saved app_prod. + // Another window opens the connection once app_staging is gone, so it lands on the saved app_prod. private func reconnectAnotherWindow() async -> MockDatabaseDriver { + factory.queue(goneStagingDriver()) let second = driver(tables: ["orders"]) factory.queue(second) await ConnectionCoordinator(connection: connection, appState: appState).connect() @@ -497,6 +505,7 @@ struct ConnectionCoordinatorStatusTests { let second = driver(tables: ["orders"]) let loading = TestGate() second.beforeFetchTables = { await loading.enter() } + factory.queue(goneStagingDriver()) factory.queue(second) let other = ConnectionCoordinator(connection: connection, appState: appState) diff --git a/apple/TableProMobile/TableProMobileTests/ConnectionCoordinatorSwitchTests.swift b/apple/TableProMobile/TableProMobileTests/ConnectionCoordinatorSwitchTests.swift index 70bdf7b059..f9db011a6b 100644 --- a/apple/TableProMobile/TableProMobileTests/ConnectionCoordinatorSwitchTests.swift +++ b/apple/TableProMobile/TableProMobileTests/ConnectionCoordinatorSwitchTests.swift @@ -52,6 +52,17 @@ struct ConnectionCoordinatorSwitchTests { return refused } + private func mySQLDriver(tables: [String]) -> MockDatabaseDriver { + let session = driver(tables: tables) + session.currentSchema = nil + session.scriptedDatabases = ["app_prod", "app_staging"] + return session + } + + private func mySQLFamily(_ type: DatabaseType) -> DatabaseConnection { + DatabaseConnection(name: "Prod", type: type, host: "db.example.com", port: 3_306, database: "app_prod") + } + private func postgreSQLDriver(tables: [String]) -> MockDatabaseDriver { let session = driver(tables: tables) session.scriptedDatabases = ["analytics", "staging"] @@ -236,22 +247,183 @@ struct ConnectionCoordinatorSwitchTests { #expect(!coordinator.showFailureAlert) } - @Test("A MySQL open dials the saved database and moves to the remembered one with USE") - func mySQLOpenUsesTheRememberedDatabase() async { + @Test( + "A MySQL-family open dials the remembered database once, with no USE, and reads each list once", + arguments: [DatabaseType.mysql, .mariadb, .tidb] + ) + func mySQLFamilyOpenDialsTheRememberedDatabase(_ type: DatabaseType) async { + let record = mySQLFamily(type) + remember(database: "app_staging", for: record) + defer { forget(record) } + let session = mySQLDriver(tables: ["staging_orders"]) + factory.queue(session) + let coordinator = ConnectionCoordinator(connection: record, appState: appState) + coordinator.restorePersistedState() + + await coordinator.connect() + + #expect(factory.dialed.map { $0.database } == ["app_staging"]) + #expect(session.switchedDatabases.isEmpty) + #expect(session.fetchTablesSchemas == [nil]) + #expect(session.fetchDatabasesCalls == 1) + #expect(session.transactionStateReads == 1) + #expect(coordinator.session?.driver === session) + #expect(coordinator.activeDatabase == "app_staging") + #expect(coordinator.tableQualifier == "app_staging") + #expect(tableNames(coordinator) == ["staging_orders"]) + #expect(!coordinator.showFailureAlert) + } + + // A contained SQL Server user signs in against the database the login names; OceanBase is unmeasured. + @Test( + "A SQL Server or OceanBase open dials the saved database and moves to the remembered one with USE", + arguments: [DatabaseType.mssql, .oceanbase] + ) + func useEngineOpenDialsTheSavedDatabase(_ type: DatabaseType) async { + let record = DatabaseConnection(name: "Ledger", type: type, host: "db.example.com", port: 1_433, database: "app") + remember(database: "history", for: record) + defer { forget(record) } + let session = driver(tables: ["orders"]) + session.currentSchema = "dbo" + session.scriptedSchemas = ["dbo"] + session.scriptedDatabases = ["app", "history"] + factory.queue(session) + let coordinator = ConnectionCoordinator(connection: record, appState: appState) + coordinator.restorePersistedState() + + await coordinator.connect() + + #expect(factory.dialed.map { $0.database } == ["app"]) + #expect(session.switchedDatabases == ["history"]) + #expect(coordinator.activeDatabase == "history") + #expect(!coordinator.showFailureAlert) + } + + @Test("A MySQL database refused at sign-in falls back to the saved one, whose USE says why as an open always did") + func refusedMySQLDatabaseFallsBackToUse() async { remember(database: "app_staging", for: connection) defer { forget(connection) } - let session = driver(tables: ["staging_orders"]) - session.scriptedDatabases = ["app_prod", "app_staging"] - factory.queue(session) + let refused = driver() + refused.connectError = MySQLError.databaseRefused("Access denied for user 'app'@'%' to database 'app_staging'") + let saved = mySQLDriver(tables: ["orders"]) + let useRefusal = MySQLError.queryFailed("Access denied for user 'app'@'%' to database 'app_staging'") + saved.switchDatabaseError = useRefusal + factory.queue(refused) + factory.queue(saved) + let coordinator = ConnectionCoordinator(connection: connection, appState: appState) + coordinator.restorePersistedState() + + await coordinator.connect() + + #expect(factory.dialed.map { $0.database } == ["app_staging", "app_prod"]) + #expect(coordinator.session?.driver === saved) + #expect(coordinator.displayStatus == .connected) + #expect(coordinator.activeDatabase == "app_prod") + #expect(tableNames(coordinator) == ["orders"]) + #expect(coordinator.failureAlertTitle == ConnectionCoordinator.switchDatabaseFailedTitle) + #expect(coordinator.failureAlertMessage == useRefusal.localizedDescription) + #expect(UserDefaults.standard.string(forKey: ConnectionDefaultsKey.lastDB.name(for: connection.id)) == "app_staging") + } + + @Test("A MySQL database the server no longer lists falls back quietly and is forgotten") + func droppedMySQLDatabaseIsForgotten() async { + remember(database: "app_archive", for: connection) + defer { forget(connection) } + let refused = driver() + refused.connectError = MySQLError.databaseRefused("Unknown database 'app_archive'") + let saved = mySQLDriver(tables: ["orders"]) + factory.queue(refused) + factory.queue(saved) let coordinator = ConnectionCoordinator(connection: connection, appState: appState) coordinator.restorePersistedState() await coordinator.connect() - #expect(factory.dialed.map { $0.database } == ["app_prod"]) - #expect(session.switchedDatabases == ["app_staging"]) + #expect(factory.dialed.map { $0.database } == ["app_archive", "app_prod"]) + #expect(coordinator.session?.driver === saved) + #expect(saved.switchedDatabases.isEmpty) + #expect(coordinator.activeDatabase == "app_prod") + #expect(!coordinator.showFailureAlert) + #expect(UserDefaults.standard.string(forKey: ConnectionDefaultsKey.lastDB.name(for: connection.id)) == nil) + } + + @Test("A MySQL sign-in refused on the remembered database fails the open after one dial, and the next open dials the saved one and moves with USE") + func refusedMySQLSignInDialsOnce() async { + remember(database: "app_staging", for: connection) + defer { forget(connection) } + let denied = driver() + let refusal = MySQLError.connectionFailed("Access denied for user 'app'@'10.0.0.2' (using password: YES)") + denied.connectError = refusal + let saved = mySQLDriver(tables: ["staging_orders"]) + factory.queue(denied) + factory.queue(saved) + let coordinator = ConnectionCoordinator(connection: connection, appState: appState) + coordinator.restorePersistedState() + + await coordinator.connect() + + #expect(factory.dialed.map { $0.database } == ["app_staging"]) + #expect(coordinator.session == nil) + #expect(coordinator.displayStatus == .failed(cause: refusal.localizedDescription)) + + await coordinator.connect() + + #expect(factory.dialed.map { $0.database } == ["app_staging", "app_prod"]) + #expect(coordinator.session?.driver === saved) + #expect(saved.switchedDatabases == ["app_staging"]) #expect(coordinator.activeDatabase == "app_staging") - #expect(coordinator.tableQualifier == "app_staging") + #expect(coordinator.displayStatus == .connected) + } + + // Another server speaking the protocol may number a missing database its own way, as 1105. + @Test("A MySQL dial refused in a way it cannot pin on the database fails once, and the next open forgets one no longer listed") + func unrecognizedMySQLRefusalYieldsToTheSavedDatabase() async { + remember(database: "app_archive", for: connection) + defer { forget(connection) } + let refused = driver() + let refusal = MySQLError.connectionFailed("Unknown database 'app_archive'") + refused.connectError = refusal + factory.queue(refused) + let coordinator = ConnectionCoordinator(connection: connection, appState: appState) + coordinator.restorePersistedState() + + await coordinator.connect() + + #expect(factory.dialed.map { $0.database } == ["app_archive"]) + #expect(coordinator.displayStatus == .failed(cause: refusal.localizedDescription)) + + let saved = mySQLDriver(tables: ["orders"]) + factory.queue(saved) + await coordinator.connect() + + #expect(factory.dialed.map { $0.database } == ["app_archive", "app_prod"]) + #expect(coordinator.session?.driver === saved) + #expect(saved.switchedDatabases.isEmpty) + #expect(coordinator.activeDatabase == "app_prod") + #expect(!coordinator.showFailureAlert) + #expect(UserDefaults.standard.string(forKey: ConnectionDefaultsKey.lastDB.name(for: connection.id)) == nil) + } + + @Test("A MySQL database whose table list is refused falls back to the saved one and moves there with USE") + func refusedMySQLTableListFallsBackToUse() async { + remember(database: "app_staging", for: connection) + defer { forget(connection) } + let refusing = driver() + refusing.fetchTablesError = ScriptedFailure.refused + let saved = mySQLDriver(tables: ["staging_orders"]) + factory.queue(refusing) + factory.queue(saved) + let coordinator = ConnectionCoordinator(connection: connection, appState: appState) + coordinator.restorePersistedState() + + await coordinator.connect() + + #expect(factory.dialed.map { $0.database } == ["app_staging", "app_prod"]) + #expect(appState.connectionManager.session(for: connection.id)?.driver === saved) + #expect(saved.switchedDatabases == ["app_staging"]) + #expect(coordinator.activeDatabase == "app_staging") + #expect(tableNames(coordinator) == ["staging_orders"]) + #expect(!coordinator.showFailureAlert) } @Test("A remembered database that refuses the open falls back to the saved one once and says why") diff --git a/apple/TableProMobile/TableProMobileTests/Drivers/FakeMySQLServer.swift b/apple/TableProMobile/TableProMobileTests/Drivers/FakeMySQLServer.swift index cc15f357aa..743eb44377 100644 --- a/apple/TableProMobile/TableProMobileTests/Drivers/FakeMySQLServer.swift +++ b/apple/TableProMobile/TableProMobileTests/Drivers/FakeMySQLServer.swift @@ -13,10 +13,20 @@ import TableProTLSTestFixtures // statement does, or with `killedQueriesComplete` succeeds as one that finished before the kill landed. Unkilled, // or after `release`, it succeeds. A query starting with `streamsRowsStarting` returns `streamedRows` one-column rows, // cut by a kill for its session with 1317. A cleartext password passed to `refuse` is answered with 1045, as an expired -// token is. +// token is. With `refusesLogin` every login gets that error, as a refused sign-in or database does. final class FakeMySQLServer: @unchecked Sendable { struct SetupFailure: Error {} + struct LoginRefusal { + let code: UInt16 + let sqlState: String + let message: String + + var packet: [UInt8] { + [0xFF, UInt8(code & 0xFF), UInt8(code >> 8)] + Array("#\(sqlState)".utf8) + Array(message.utf8) + } + } + struct Identity { let certificate: String let key: String @@ -96,6 +106,7 @@ final class FakeMySQLServer: @unchecked Sendable { private let killedQueriesComplete: Bool private let streamedPrefix: String? private let streamedRows: Int + private let loginRefusal: LoginRefusal? private let fixtures: URL? private let kills = KillBoard() private let refusedPasswords = OSAllocatedUnfairLock>(initialState: []) @@ -122,7 +133,8 @@ final class FakeMySQLServer: @unchecked Sendable { holdSeconds: TimeInterval = 10, killedQueriesComplete: Bool = false, streamsRowsStarting streamedPrefix: String? = nil, - streamedRows: Int = 0 + streamedRows: Int = 0, + refusesLogin loginRefusal: LoginRefusal? = nil ) throws { self.asksForCleartext = asksForCleartext self.asksForFullSHA2Auth = asksForFullSHA2Auth @@ -132,6 +144,7 @@ final class FakeMySQLServer: @unchecked Sendable { self.killedQueriesComplete = killedQueriesComplete self.streamedPrefix = streamedPrefix self.streamedRows = streamedRows + self.loginRefusal = loginRefusal if let identity { let directory = FileManager.default.temporaryDirectory .appendingPathComponent("FakeMySQLServer-\(UUID().uuidString)", isDirectory: true) @@ -276,6 +289,10 @@ final class FakeMySQLServer: @unchecked Sendable { return } } + if let loginRefusal { + _ = channel.send(loginRefusal.packet, sequence: sequence &+ 1) + return + } guard channel.send(Self.okPacket, sequence: sequence &+ 1) else { return } session.loggedIn = true diff --git a/apple/TableProMobile/TableProMobileTests/Drivers/MySQLDialRefusalTests.swift b/apple/TableProMobile/TableProMobileTests/Drivers/MySQLDialRefusalTests.swift new file mode 100644 index 0000000000..59e9d7e251 --- /dev/null +++ b/apple/TableProMobile/TableProMobileTests/Drivers/MySQLDialRefusalTests.swift @@ -0,0 +1,96 @@ +import Foundation +@testable import TableProMobile +import TableProModels +import Testing + +@Suite(.timeLimit(.minutes(1))) +struct MySQLDialRefusalTests { + @Test( + "A database refused at sign-in is told apart, and reads as any failed connect", + arguments: [ + FakeMySQLServer.LoginRefusal(code: 1_049, sqlState: "42000", message: "Unknown database 'app_archive'"), + FakeMySQLServer.LoginRefusal( + code: 1_044, sqlState: "42000", message: "Access denied for user 'app'@'%' to database 'app_archive'" + ), + ] + ) + func refusedDatabase(_ refusal: FakeMySQLServer.LoginRefusal) async throws { + let error = try await connectError(answering: refusal) + + guard case MySQLError.databaseRefused(let message)? = error else { + Issue.record("Expected a refused database, got \(String(describing: error))") + return + } + #expect(message == refusal.message) + #expect(error?.localizedDescription == "MySQL connection failed: \(refusal.message)") + } + + // 1524 is a sign-in plugin the server has not loaded; 1105 is what another server may send for anything. + @Test( + "Any other refusal stays a failed connect, whatever database the dial names", + arguments: [ + FakeMySQLServer.LoginRefusal( + code: 1_045, sqlState: "28000", message: "Access denied for user 'app'@'10.0.0.2' (using password: YES)" + ), + FakeMySQLServer.LoginRefusal( + code: 1_698, sqlState: "28000", message: "Access denied for user 'app'@'10.0.0.2'" + ), + FakeMySQLServer.LoginRefusal( + code: 4_151, sqlState: "HY000", message: "Access denied, this account is locked" + ), + FakeMySQLServer.LoginRefusal(code: 1_040, sqlState: "08004", message: "Too many connections"), + FakeMySQLServer.LoginRefusal( + code: 1_524, sqlState: "HY000", message: "Plugin 'mysql_native_password' is not loaded" + ), + FakeMySQLServer.LoginRefusal(code: 1_105, sqlState: "HY000", message: "Unknown database 'app_archive'"), + ] + ) + func refusedSignIn(_ refusal: FakeMySQLServer.LoginRefusal) async throws { + let error = try await connectError(answering: refusal) + + guard case MySQLError.connectionFailed(let message)? = error else { + Issue.record("Expected a failed connect, got \(String(describing: error))") + return + } + #expect(message == refusal.message) + } + + // Preferred asks for TLS first, then signs in again without it, where the server refuses the database. + @Test("A database refused after Preferred falls back to plaintext is still told apart") + func refusedDatabaseAfterPlaintextRetry() async throws { + let refusal = FakeMySQLServer.LoginRefusal(code: 1_049, sqlState: "42000", message: "Unknown database 'app_archive'") + let error = try await connectError( + answering: refusal, + ssl: DriverSSLConfiguration(mode: .require, isPreferred: true), + sessions: 2 + ) + + guard case MySQLError.databaseRefused(let message)? = error else { + Issue.record("Expected a refused database, got \(String(describing: error))") + return + } + #expect(message == refusal.message) + } + + private func connectError( + answering refusal: FakeMySQLServer.LoginRefusal, + ssl: DriverSSLConfiguration = .disabled, + sessions: Int = 1 + ) async throws -> (any Error)? { + let server = try FakeMySQLServer(identity: nil, refusesLogin: refusal) + defer { server.close() } + async let served = server.serve(sessions: sessions) + let driver = MySQLDriver( + host: "127.0.0.1", port: server.port, user: "app", password: "app-password", database: "app_archive", + ssl: ssl + ) + var thrown: (any Error)? + do { + try await driver.connect() + } catch { + thrown = error + } + _ = await served + return thrown + } +} diff --git a/apple/changelog.d/4252.changed.md b/apple/changelog.d/4252.changed.md new file mode 100644 index 0000000000..9c8c26082e --- /dev/null +++ b/apple/changelog.d/4252.changed.md @@ -0,0 +1 @@ +On iOS, reopening a MySQL, MariaDB or TiDB connection on the database you last used takes fewer round trips. diff --git a/apple/scripts/probes/check-mysql-dial-database-refusal.sh b/apple/scripts/probes/check-mysql-dial-database-refusal.sh new file mode 100755 index 0000000000..6d29d47761 --- /dev/null +++ b/apple/scripts/probes/check-mysql-dial-database-refusal.sh @@ -0,0 +1,201 @@ +#!/usr/bin/env bash +# +# Check that a MySQL-family server refuses the database a handshake names exactly as it refuses a USE there, and keeps +# a failed sign-in apart from both. +# +# The iOS open dials a MySQL, MariaDB or TiDB connection straight into the database it was last on, and falls back to +# the saved one in the same open only when the dial is refused with 1044 or 1049 (MySQLDatabaseRefusal). That is right +# only while: +# +# - a database the user may not use, or that is missing, is refused with 1044 (ER_DBACCESS_DENIED_ERROR) or 1049 +# (ER_BAD_DB_ERROR), with the same code and text a USE there gets after the sign-in; +# - a wrong password or an unknown user is refused with 1045 (ER_ACCESS_DENIED_ERROR) whatever the database names, +# so the sign-in is checked first and never depends on the database. +# +# It builds a client from the shipped Libs/libmariadb.a, creates the users dr_plain (one database) and dr_wide (global +# SELECT) and the databases dr_kept and dr_locked, and drops them again. +# +# Verified against MySQL 8.4.13, MariaDB 11.4.13 and TiDB 8.5.1 with libmariadb 3.4.4. +# +# Usage: +# MYSQL_PWD=secret scripts/probes/check-mysql-dial-database-refusal.sh [host] [port] [admin user] +# +# The admin password is read from MYSQL_PWD only. TLS is required unless the host is a loopback address. Exits 1 when +# an answer differs, 3 when it cannot run. + +set -uo pipefail + +HOST="${1:-127.0.0.1}" +PORT="${2:-3306}" +ADMIN="${3:-root}" + +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" +WORK="$(mktemp -d)" +trap 'rm -rf "$WORK"' EXIT + +[ -f "$ROOT/Libs/libmariadb.a" ] || { + echo "not found: Libs/libmariadb.a (run scripts/download-libs.sh)" >&2 + exit 3 +} + +# Connector/C dials localhost through the Unix socket, which a server in a container does not share. +[ "$HOST" = localhost ] && HOST=127.0.0.1 +LOOPBACK=0 +case "$HOST" in + 127.*|::1) LOOPBACK=1 ;; +esac + +cat > "$WORK/check.c" << 'C' +#include +#include +#include +#include + +static const char *host; +static unsigned port; +static int loopback; +static int failures = 0; + +struct answer { + unsigned code; + char text[512]; +}; + +static MYSQL *dial(const char *user, const char *password, const char *database, struct answer *answer) { + MYSQL *m = mysql_init(NULL); + if (loopback) { + my_bool off = 0; + mysql_options(m, MYSQL_OPT_SSL_ENFORCE, &off); + mysql_options(m, MYSQL_OPT_SSL_VERIFY_SERVER_CERT, &off); + } + mysql_options(m, MYSQL_SET_CHARSET_NAME, "utf8mb4"); + if (mysql_real_connect(m, host, user, password, database, port, NULL, 0)) { + answer->code = 0; + answer->text[0] = 0; + return m; + } + answer->code = mysql_errno(m); + snprintf(answer->text, sizeof answer->text, "%s", mysql_error(m)); + mysql_close(m); + return NULL; +} + +static void use(MYSQL *m, const char *database, struct answer *answer) { + char statement[256]; + snprintf(statement, sizeof statement, "USE `%s`", database); + if (mysql_query(m, statement) == 0) { + answer->code = 0; + answer->text[0] = 0; + return; + } + answer->code = mysql_errno(m); + snprintf(answer->text, sizeof answer->text, "%s", mysql_error(m)); +} + +static void admin(MYSQL *m, const char *sql) { + if (mysql_query(m, sql) != 0) { + printf("FAIL: %s: %s\n", sql, mysql_error(m)); + failures++; + } +} + +static void expect_code(const struct answer *answer, unsigned expected, const char *label) { + if (answer->code == expected) { + printf("PASS: %s (%u %s)\n", label, answer->code, answer->text); + } else { + failures++; + printf("FAIL: %s (expected %u, got %u %s)\n", label, expected, answer->code, answer->text); + } +} + +// The dial's refusal and the USE's are one answer: same code, same text. +static void expect_same_as_use(const char *user, const char *password, const char *database, unsigned expected, + const char *label) { + struct answer dialed, used, signed_in; + MYSQL *unexpected = dial(user, password, database, &dialed); + if (unexpected) mysql_close(unexpected); + expect_code(&dialed, expected, label); + MYSQL *m = dial(user, password, NULL, &signed_in); + if (!m) { + failures++; + printf("FAIL: %s cannot sign in without a database: %u %s\n", user, signed_in.code, signed_in.text); + return; + } + use(m, database, &used); + mysql_close(m); + if (used.code == dialed.code && strcmp(used.text, dialed.text) == 0) { + printf("PASS: a USE there answers the same\n"); + } else { + failures++; + printf("FAIL: a USE there answers %u %s\n", used.code, used.text); + } +} + +int main(int argc, char **argv) { + (void)argc; + host = argv[1]; + port = (unsigned)atoi(argv[2]); + loopback = atoi(argv[4]); + + struct answer answer; + MYSQL *a = dial(argv[3], getenv("MYSQL_PWD"), NULL, &answer); + if (!a) { + fprintf(stderr, "cannot connect as %s: %u %s\n", argv[3], answer.code, answer.text); + return 3; + } + printf("server %s, libmariadb %s\n", mysql_get_server_info(a), mysql_get_client_info()); + const char *cleanup[] = { + "DROP USER IF EXISTS 'dr_plain'@'%'", "DROP USER IF EXISTS 'dr_wide'@'%'", + "DROP DATABASE IF EXISTS dr_kept", "DROP DATABASE IF EXISTS dr_locked", "DROP DATABASE IF EXISTS dr_missing", + }; + for (size_t i = 0; i < sizeof cleanup / sizeof *cleanup; i++) admin(a, cleanup[i]); + admin(a, "CREATE DATABASE dr_kept"); + admin(a, "CREATE DATABASE dr_locked"); + admin(a, "CREATE USER 'dr_plain'@'%' IDENTIFIED BY 'dr-probe-Pass1'"); + admin(a, "GRANT ALL ON dr_kept.* TO 'dr_plain'@'%'"); + admin(a, "CREATE USER 'dr_wide'@'%' IDENTIFIED BY 'dr-probe-Pass1'"); + admin(a, "GRANT SELECT ON *.* TO 'dr_wide'@'%'"); + + MYSQL *kept = dial("dr_plain", "dr-probe-Pass1", "dr_kept", &answer); + expect_code(&answer, 0, "a granted database opens in the handshake"); + if (kept) mysql_close(kept); + + expect_same_as_use("dr_plain", "dr-probe-Pass1", "dr_locked", 1044, "a database without a grant is refused with 1044"); + expect_same_as_use("dr_plain", "dr-probe-Pass1", "dr_missing", 1044, + "a missing database is refused with 1044 to a user without global privileges"); + expect_same_as_use("dr_wide", "dr-probe-Pass1", "dr_missing", 1049, + "a missing database is refused with 1049 to a user with global privileges"); + + const char *databases[] = { "dr_kept", "dr_locked", "dr_missing" }; + for (size_t i = 0; i < 3; i++) { + char label[128]; + MYSQL *unexpected = dial("dr_plain", "wrong-password", databases[i], &answer); + if (unexpected) mysql_close(unexpected); + snprintf(label, sizeof label, "a wrong password is refused with 1045 when the dial names %s", databases[i]); + expect_code(&answer, 1045, label); + unexpected = dial("dr_wide", "wrong-password", databases[i], &answer); + if (unexpected) mysql_close(unexpected); + snprintf(label, sizeof label, "and for a user with global privileges, naming %s", databases[i]); + expect_code(&answer, 1045, label); + } + MYSQL *nobody = dial("dr_nobody", "wrong-password", "dr_missing", &answer); + if (nobody) mysql_close(nobody); + expect_code(&answer, 1045, "an unknown user is refused with 1045 when the dial names a missing database"); + + for (size_t i = 0; i < sizeof cleanup / sizeof *cleanup; i++) admin(a, cleanup[i]); + mysql_close(a); + + printf(failures == 0 ? "OK: every check passed\n" : "%d check(s) failed\n", failures); + return failures == 0 ? 0 : 1; +} +C + +clang -O0 -o "$WORK/check" "$WORK/check.c" -I"$ROOT/Plugins/MySQLDriverPlugin/CMariaDB/include" "$ROOT/Libs/libmariadb.a" \ + -L"$ROOT/Libs/dylibs" -lssl.3 -lcrypto.3 -liconv -lz -framework GSS -Wl,-rpath,"$ROOT/Libs/dylibs" \ + > "$WORK/build.log" 2>&1 || { + echo "the check failed to build" >&2 + cat "$WORK/build.log" >&2 + exit 3 +} + +"$WORK/check" "$HOST" "$PORT" "$ADMIN" "$LOOPBACK"