From 7a247180994b8be4bc7925bdcd082bd8bab7c16f Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Wed, 11 Nov 2026 12:26:42 +0700 Subject: [PATCH 1/2] feat(aws-iam): connect to RDS through a custom CNAME host --- .../TableProDatabase/ConnectionManager.swift | 11 +- .../Protocols/DriverFactory.swift | 9 +- .../ConnectionManagerSignInTests.swift | 72 ++++- .../AWSEndpointHostTests.swift | 218 +++++++++++++++ .../AWSIAMTokenTargetTests.swift | 9 +- .../RDSAuthTokenTests.swift | 59 +++- .../MySQLDriverPlugin/MySQLPluginDriver.swift | 3 +- .../TableProPluginKit/AWS/AWSAuthError.swift | 13 + .../AWS/AWSEndpointHost.swift | 146 ++++++++++ .../AWS/AWSIAMTokenTarget.swift | 3 +- .../TableProPluginKit/AWS/AWSPartition.swift | 2 + .../TableProPluginKit/AWS/CNAMEResolver.swift | 222 +++++++++++++++ .../TableProPluginKit/AWS/RDSEndpoint.swift | 15 - .../AWS/RDSSigningEndpoint.swift | 31 +- .../Core/Database/AWS/RedshiftIAMSignIn.swift | 112 +++++--- .../ConnectionCredentialResolver.swift | 6 + .../Database/DatabaseManager+Health.swift | 60 ++-- .../Database/DatabaseManager+Queries.swift | 8 +- .../DatabaseManager+RDSEndpoint.swift | 53 ++++ .../Database/DatabaseManager+Sessions.swift | 56 ++-- .../Core/Database/DatabaseManager.swift | 3 + .../DatabaseConnection+IAMSignIn.swift | 18 +- .../TablePro/Resources/Localizable.xcstrings | 14 + .../TableProMobile/Helpers/AppError+IAM.swift | 16 ++ .../TableProMobile/Localizable.xcstrings | 20 ++ .../Platform/IOSDriverFactory.swift | 33 ++- .../SignIn/IAMErrorClassifierTests.swift | 21 ++ .../SignIn/IAMPasswordMintTests.swift | 3 +- .../IOSDriverFactoryEndpointAliasTests.swift | 85 ++++++ .../AWS/RedshiftIAMSignInTests.swift | 101 ++++++- .../DatabaseManagerRDSEndpointTests.swift | 264 ++++++++++++++++++ .../EnvVarReferenceConsentTests.swift | 15 + .../Helpers/FakeCNAMEResolver.swift | 38 +++ apple/scripts/probes/check-rds-cname-chain.sh | 91 ++++++ 34 files changed, 1685 insertions(+), 145 deletions(-) create mode 100644 apple/Packages/TableProCore/Tests/TableProPluginKitTests/AWSEndpointHostTests.swift create mode 100644 apple/Plugins/TableProPluginKit/AWS/AWSEndpointHost.swift create mode 100644 apple/Plugins/TableProPluginKit/AWS/CNAMEResolver.swift delete mode 100644 apple/Plugins/TableProPluginKit/AWS/RDSEndpoint.swift create mode 100644 apple/TablePro/Core/Database/DatabaseManager+RDSEndpoint.swift create mode 100644 apple/TableProMobile/TableProMobileTests/SignIn/IOSDriverFactoryEndpointAliasTests.swift create mode 100644 apple/TableProTests/Core/Database/DatabaseManagerRDSEndpointTests.swift create mode 100644 apple/TableProTests/Helpers/FakeCNAMEResolver.swift create mode 100755 apple/scripts/probes/check-rds-cname-chain.sh diff --git a/apple/Packages/TableProCore/Sources/TableProDatabase/ConnectionManager.swift b/apple/Packages/TableProCore/Sources/TableProDatabase/ConnectionManager.swift index 317be54e12..905b25e6cf 100644 --- a/apple/Packages/TableProCore/Sources/TableProDatabase/ConnectionManager.swift +++ b/apple/Packages/TableProCore/Sources/TableProDatabase/ConnectionManager.swift @@ -45,13 +45,13 @@ public final class ConnectionManager: @unchecked Sendable { // A teardown wait that gives up leaves the teardown running and counting as suspension-blocking. public func connect( - _ connection: DatabaseConnection, + _ saved: DatabaseConnection, prompter: (any ConnectionPrompter)? = nil ) async throws -> ConnectionSession { // Ahead of beginAttempt, so a refusal neither cancels an attempt in flight nor tears down the open session. - if let refusal = driverFactory.refusal(for: connection) { throw refusal } - let generation = beginAttempt(for: connection.id) - switch await awaitTeardown(of: connection.id, bound: .after(teardownWaitLimit)) { + if let refusal = driverFactory.refusal(for: saved) { throw refusal } + let generation = beginAttempt(for: saved.id) + switch await awaitTeardown(of: saved.id, bound: .after(teardownWaitLimit)) { case .cleared: break case .cancelled: @@ -59,6 +59,9 @@ public final class ConnectionManager: @unchecked Sendable { case .stillClosing: throw ConnectionError.previousSessionStillClosing } + guard isCurrentAttempt(generation, for: saved.id) else { throw CancellationError() } + // Everything after this mints for, tunnels to and dials the endpoint this names. + let connection = try await driverFactory.resolvingEndpointAlias(for: saved) guard isCurrentAttempt(generation, for: connection.id) else { throw CancellationError() } let stored = try secureStore.retrieve(forKey: Self.passwordKey(for: connection.id)) let password = try await driverFactory.resolvePassword(for: connection, stored: stored, secureStore: secureStore) diff --git a/apple/Packages/TableProCore/Sources/TableProDatabase/Protocols/DriverFactory.swift b/apple/Packages/TableProCore/Sources/TableProDatabase/Protocols/DriverFactory.swift index e2ef856f52..5f2e49d1f4 100644 --- a/apple/Packages/TableProCore/Sources/TableProDatabase/Protocols/DriverFactory.swift +++ b/apple/Packages/TableProCore/Sources/TableProDatabase/Protocols/DriverFactory.swift @@ -18,7 +18,10 @@ public protocol DriverFactory: Sendable { preTunnelHost: String?, refreshPassword: (@Sendable () async throws -> String?)? ) throws -> any DatabaseDriver - // Runs before the tunnel, on the connection as saved, so a sign-in failure comes before any SSH prompt. + // Runs first, on the connection as saved. The connect mints for, tunnels to and dials what it returns, which can + // name another host, such as the RDS endpoint a CNAME stands for. It is never saved. + func resolvingEndpointAlias(for connection: DatabaseConnection) async throws -> DatabaseConnection + // Runs before the tunnel, so a sign-in failure comes before any SSH prompt. func resolvePassword( for connection: DatabaseConnection, stored: String?, @@ -49,4 +52,8 @@ public extension DriverFactory { func dialsSSHTunnel(for connection: DatabaseConnection) -> Bool { connection.sshEnabled } + + func resolvingEndpointAlias(for connection: DatabaseConnection) async throws -> DatabaseConnection { + connection + } } diff --git a/apple/Packages/TableProCore/Tests/TableProDatabaseTests/ConnectionManagerSignInTests.swift b/apple/Packages/TableProCore/Tests/TableProDatabaseTests/ConnectionManagerSignInTests.swift index fe26cf08e1..b8cadb50d5 100644 --- a/apple/Packages/TableProCore/Tests/TableProDatabaseTests/ConnectionManagerSignInTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProDatabaseTests/ConnectionManagerSignInTests.swift @@ -24,10 +24,15 @@ private struct DriverRequest: Sendable { private final class SigningDriverFactory: DriverFactory, @unchecked Sendable { var mint: @Sendable (String?) async throws -> String? = { stored in stored } + var alias: @Sendable (DatabaseConnection) async -> DatabaseConnection = { $0 } var opensCatalogs = false private(set) var mintRequests: [MintRequest] = [] private(set) var driverRequests: [DriverRequest] = [] + func resolvingEndpointAlias(for connection: DatabaseConnection) async throws -> DatabaseConnection { + await alias(connection) + } + func resolvePassword( for connection: DatabaseConnection, stored: String?, @@ -85,11 +90,19 @@ private func passwordKey(for connection: DatabaseConnection) -> String { "com.TablePro.password.\(connection.id.uuidString)" } -private func rdsConnection(tunneled: Bool) -> DatabaseConnection { +private let replicaEndpoint = "replica-a.abc123.us-east-1.rds.amazonaws.com" + +private func pointingAtReplica(_ connection: DatabaseConnection) -> DatabaseConnection { + var resolved = connection + resolved.host = replicaEndpoint + return resolved +} + +private func rdsConnection(tunneled: Bool, host: String = "mydb.abc123.us-east-1.rds.amazonaws.com") -> DatabaseConnection { DatabaseConnection( name: "RDS", type: mockType, - host: "mydb.abc123.us-east-1.rds.amazonaws.com", + host: host, port: 5_432, username: "iam_user", additionalFields: ["awsAuth": "accessKey"], @@ -255,4 +268,59 @@ struct ConnectionManagerSignInTests { #expect(driver.host == connection.host) #expect(driver.preTunnelHost == nil) } + + @Test("The endpoint a CNAME stands for is what the connect mints for, dials and mints for again") + func aliasReachesMintDriverAndRefresh() async throws { + let factory = SigningDriverFactory() + factory.alias = { pointingAtReplica($0) } + let manager = ConnectionManager(driverFactory: factory, secureStore: MockSecureStore()) + let connection = rdsConnection(tunneled: false, host: "reader.db.example.com") + + _ = try await manager.connect(connection) + let driver = try #require(factory.driverRequests.first) + let refresh = try #require(driver.refreshPassword) + _ = try await refresh() + + #expect(factory.mintRequests.map(\.host) == [replicaEndpoint, replicaEndpoint]) + #expect(driver.host == replicaEndpoint) + #expect(driver.preTunnelHost == nil) + } + + @Test("Whatever endpoint the factory returns, a tunnel forwards to it and the driver names it behind the tunnel") + func aliasMovesTheTunnelDestination() async throws { + let factory = SigningDriverFactory() + factory.alias = { pointingAtReplica($0) } + let ssh = MockSSHProvider() + let manager = ConnectionManager(driverFactory: factory, secureStore: MockSecureStore(), sshProvider: ssh) + let connection = rdsConnection(tunneled: true, host: "reader.db.example.com") + + _ = try await manager.connect(connection) + + #expect(factory.mintRequests.first?.host == replicaEndpoint) + #expect(ssh.receivedDestinations == [.tcp(host: replicaEndpoint, port: 5_432)]) + let driver = try #require(factory.driverRequests.first) + #expect(driver.host == "127.0.0.1") + #expect(driver.preTunnelHost == replicaEndpoint) + } + + @Test("An attempt invalidated during the lookup throws CancellationError and mints nothing") + func attemptInvalidatedDuringTheLookupStops() async throws { + let gate = AsyncGate() + let factory = SigningDriverFactory() + factory.alias = { connection in + await gate.wait() + return pointingAtReplica(connection) + } + let manager = ConnectionManager(driverFactory: factory, secureStore: MockSecureStore()) + let connection = rdsConnection(tunneled: false, host: "reader.db.example.com") + + let attempt = Task { try await manager.connect(connection) } + await gate.arrived(1) + manager.invalidateAttempt(for: connection.id) + await gate.open() + + await #expect(throws: CancellationError.self) { try await attempt.value } + #expect(factory.mintRequests.isEmpty) + #expect(manager.session(for: connection.id) == nil) + } } diff --git a/apple/Packages/TableProCore/Tests/TableProPluginKitTests/AWSEndpointHostTests.swift b/apple/Packages/TableProCore/Tests/TableProPluginKitTests/AWSEndpointHostTests.swift new file mode 100644 index 0000000000..e706bb5b06 --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProPluginKitTests/AWSEndpointHostTests.swift @@ -0,0 +1,218 @@ +import Foundation +@testable import TableProPluginKit +import Testing + +private struct FakeCNAMEResolver: CNAMEResolving { + let records: [String: String] + let failing: Set + let queried: QueriedNames + + init(_ records: [String: String], failing: Set = [], queried: QueriedNames = QueriedNames()) { + self.records = records + self.failing = failing + self.queried = queried + } + + func cnameTarget(of name: String, within timeout: Duration?) async -> CNAMELookup { + queried.append(name) + if failing.contains(name) { + return .failed + } + return records[name].map(CNAMELookup.found) ?? .notFound + } +} + +private final class QueriedNames: @unchecked Sendable { + private let lock = NSLock() + private var storage: [String] = [] + + var names: [String] { + lock.withLock { storage } + } + + func append(_ name: String) { + lock.withLock { storage.append(name) } + } +} + +struct AWSEndpointHostTests { + @Test("Every AWS-issued RDS form is recognised, with its region") + func rdsForms() { + let regions: [String: String?] = [ + "mydb.abc123.us-east-1.rds.amazonaws.com": "us-east-1", + "mydb.cluster-abc123.eu-west-1.rds.amazonaws.com": "eu-west-1", + "mydb.cluster-ro-abc123.ap-south-1.rds.amazonaws.com": "ap-south-1", + "mydb.cluster-custom-abc123.us-west-2.rds.amazonaws.com": "us-west-2", + "proxy.proxy-abc123.us-east-1.rds.amazonaws.com": "us-east-1", + "reader.endpoint.proxy-abc123.us-east-1.rds.amazonaws.com": "us-east-1", + "mydb.abc123.rds.cn-northwest-1.amazonaws.com.cn": "cn-northwest-1", + "mydb.abc123.cn-north-1.rds.amazonaws.com.cn": "cn-north-1", + "mydb.abc123.us-gov-west-1.rds.amazonaws.com": "us-gov-west-1", + "mydb.abc123.rds.us-gov-west-1.amazonaws.com": "us-gov-west-1", + "mydb.abc123.eusc-de-east-1.rds.amazonaws.eu": "eusc-de-east-1", + "mydb.abc123.rds.us-iso-east-1.c2s.ic.gov": "us-iso-east-1", + "mydb.global-abc123.global.rds.amazonaws.com": nil + ] + for (host, region) in regions { + #expect(AWSEndpointHost.isRDSEndpoint(host), "\(host)") + #expect(AWSEndpointHost.rdsRegion(forHost: host) == region, "\(host)") + } + } + + @Test("A name only counts as AWS's when it ends in a partition's suffix") + func lookalikesAreNotRDS() { + for host in [ + "mydb.abc123.us-east-1.rds.amazonaws.com.evil.example", + "mydb.abc123.us-east-1.rds.amazonaws.attacker.com", + "abc123.us-east-1.rds.amazonaws.com", + "mydb.abc123.cn-north-1.rds.amazonaws.com", + "mydb.abc123.us-east-1.rds.amazonaws.com.cn", + "mydb..us-east-1.rds.amazonaws.com", + "ec2-54-1-2-3.compute-1.amazonaws.com", + "a.b.rds.s3.amazonaws.com", + "a.b.us-east-1.rds.s3.amazonaws.com", + "mydb.abc123.east.rds.amazonaws.com", + "my-cluster.abc123.us-east-1.redshift.amazonaws.com", + "reader.db.example.com", + "localhost" + ] { + #expect(!AWSEndpointHost.isRDSEndpoint(host), "\(host)") + #expect(AWSEndpointHost.rdsRegion(forHost: host) == nil, "\(host)") + } + } + + @Test("Case and one trailing dot do not change the answer") + func normalization() { + #expect(AWSEndpointHost.normalized(" MyDB.ABC123.US-EAST-1.RDS.AmazonAWS.com. ") == "mydb.abc123.us-east-1.rds.amazonaws.com") + #expect(AWSEndpointHost.rdsRegion(forHost: "MyDB.ABC123.US-EAST-1.RDS.AmazonAWS.com.") == "us-east-1") + } + + @Test("Only literal loopback addresses and names are loopback") + func loopback() { + for host in [ + "localhost", "LocalHost", "127.0.0.1", "127.9.8.7", "127.1", "0x7f.1", "2130706433", + "::1", "0:0:0:0:0:0:0:1", "::ffff:127.0.0.1", "0.0.0.0" + ] { + #expect(AWSEndpointHost.isLoopback(host), "\(host)") + } + for host in [ + "localhost.localdomain", "localhost.", "127.0.0.1.", "127.0.0.1 ", " localhost", "127.0.0.1.attacker.example", + "127.evil.example.com", "0177.0.0.1", "0177.12.34.56", "[::1]", "::1%lo0", "::1%x,db.example.com", + "127.0.0.1,db.example.com", + "127.0.0.256", "128.0.0.1", + "::2", "::ffff:10.0.0.1", "db.example.com", "" + ] { + #expect(!AWSEndpointHost.isLoopback(host), "\(host)") + } + } + + @Test("Literals, loopback, single labels and .local names are never looked up") + func aliasCandidates() { + for host in ["reader.db.example.com", "READER.db.example.com."] { + #expect(AWSEndpointHost.isAliasCandidate(host), "\(host)") + } + for host in [ + "", "db", "localhost", "127.0.0.1", "10.0.0.5", "10.1", "0177.12.34.56", "::1", "fe80::1", "printer.local", + "/tmp/mysql.sock", "a.example.com,b.example.com" + ] { + #expect(!AWSEndpointHost.isAliasCandidate(host), "\(host)") + } + } + + @Test("A custom name leads to the first RDS endpoint in its chain") + func walksToFirstRDSName() async { + let resolver = FakeCNAMEResolver([ + "reader.db.example.com": "mycluster.cluster-ro-abc123.us-east-1.rds.amazonaws.com", + "mycluster.cluster-ro-abc123.us-east-1.rds.amazonaws.com": "instance-2.abc123.us-east-1.rds.amazonaws.com", + "instance-2.abc123.us-east-1.rds.amazonaws.com": "ec2-54-1-2-3.compute-1.amazonaws.com" + ]) + let found = await AWSEndpointHost.rdsEndpoint(behind: "Reader.DB.example.com.", resolver: resolver, within: nil) + #expect(found == .found("mycluster.cluster-ro-abc123.us-east-1.rds.amazonaws.com")) + } + + @Test("Hops through names of the user's own before reaching RDS") + func walksThroughIntermediateNames() async { + let resolver = FakeCNAMEResolver([ + "db.example.com": "db.internal.example.net", + "db.internal.example.net": "replica-b.abc123.us-east-1.rds.amazonaws.com" + ]) + let found = await AWSEndpointHost.rdsEndpoint(behind: "db.example.com", resolver: resolver, within: nil) + #expect(found == .found("replica-b.abc123.us-east-1.rds.amazonaws.com")) + } + + @Test("No lookup for a host that already is an RDS endpoint, or cannot have a CNAME") + func skipsHostsThatNeedNoLookup() async { + let queried = QueriedNames() + let resolver = FakeCNAMEResolver([:], queried: queried) + for host in ["mydb.abc123.us-east-1.rds.amazonaws.com", "127.0.0.1", "10.0.0.5", "localhost", "db"] { + #expect(await AWSEndpointHost.rdsEndpoint(behind: host, resolver: resolver, within: nil) == .notFound) + } + #expect(queried.names.isEmpty) + } + + @Test("A chain with no RDS endpoint, a loop or too many hops finds nothing") + func chainsThatFindNothing() async { + let noRDS = FakeCNAMEResolver(["db.example.com": "ec2-54-1-2-3.compute-1.amazonaws.com"]) + #expect(await AWSEndpointHost.rdsEndpoint(behind: "db.example.com", resolver: noRDS, within: nil) == .notFound) + + let loopQueried = QueriedNames() + let loop = FakeCNAMEResolver(["a.example.com": "b.example.com", "b.example.com": "a.example.com"], queried: loopQueried) + #expect(await AWSEndpointHost.rdsEndpoint(behind: "a.example.com", resolver: loop, within: nil) == .notFound) + #expect(loopQueried.names == ["a.example.com", "b.example.com"]) + + var long: [String: String] = [:] + for hop in 0 ..< 20 { + long["h\(hop).example.com"] = "h\(hop + 1).example.com" + } + long["h20.example.com"] = "mydb.abc123.us-east-1.rds.amazonaws.com" + let longQueried = QueriedNames() + let chain = FakeCNAMEResolver(long, queried: longQueried) + #expect(await AWSEndpointHost.rdsEndpoint(behind: "h0.example.com", resolver: chain, within: nil) == .notFound) + #expect(longQueried.names.count == 8) + } + + @Test("A spent time budget stops the walk before it asks") + func spentBudgetAsksNothing() async { + let queried = QueriedNames() + let resolver = FakeCNAMEResolver(["db.example.com": "mydb.abc123.us-east-1.rds.amazonaws.com"], queried: queried) + #expect(await AWSEndpointHost.rdsEndpoint(behind: "db.example.com", resolver: resolver, within: .zero) == .failed) + #expect(queried.names.isEmpty) + } + + @Test("A lookup that fails partway is a failure, not a name with no RDS endpoint") + func failedHopIsAFailure() async { + let resolver = FakeCNAMEResolver(["db.example.com": "edge.example.net"], failing: ["edge.example.net"]) + #expect(await AWSEndpointHost.rdsEndpoint(behind: "db.example.com", resolver: resolver, within: nil) == .failed) + } +} + +struct DNSWireNameTests { + private func decode(_ bytes: [UInt8]) -> String? { + bytes.withUnsafeBytes { DNSWireName.decode($0) } + } + + private func wire(_ labels: [String]) -> [UInt8] { + labels.flatMap { [UInt8($0.utf8.count)] + Array($0.utf8) } + [0] + } + + @Test("Decodes an uncompressed name into lowercase labels without the root dot") + func decodesName() { + #expect(decode(wire(["Replica-A", "abc123", "us-east-1", "rds", "amazonaws", "com"])) + == "replica-a.abc123.us-east-1.rds.amazonaws.com") + } + + @Test("Refuses compression pointers, truncation, the bare root, trailing bytes and dots inside a label") + func refusesMalformedNames() { + #expect(decode([0xC0, 0x0C]) == nil) + #expect(decode([3, 0x61, 0x62]) == nil) + #expect(decode([0]) == nil) + #expect(decode(wire(["a", "com"]) + [1]) == nil) + #expect(decode([3, 0x61, 0x2E, 0x62, 0]) == nil) + #expect(decode([1, 0x00, 0]) == nil) + for byte in [UInt8(ascii: " "), UInt8(ascii: "'"), UInt8(ascii: "="), UInt8(ascii: "/"), UInt8(ascii: ":"), 0xC3] { + #expect(decode([3, 0x61, byte, 0x62, 3, 0x63, 0x6F, 0x6D, 0]) == nil) + } + #expect(decode(wire(["_dmarc", "example", "com"])) == "_dmarc.example.com") + #expect(decode([64] + Array(repeating: 0x61, count: 64) + [0]) == nil) + } +} diff --git a/apple/Packages/TableProCore/Tests/TableProPluginKitTests/AWSIAMTokenTargetTests.swift b/apple/Packages/TableProCore/Tests/TableProPluginKitTests/AWSIAMTokenTargetTests.swift index c16eef5fa7..0e01f4c803 100644 --- a/apple/Packages/TableProCore/Tests/TableProPluginKitTests/AWSIAMTokenTargetTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProPluginKitTests/AWSIAMTokenTargetTests.swift @@ -86,10 +86,13 @@ struct AWSIAMTokenTargetTests { #expect(mixedCase == lowercase) } - @Test("The AWS Region field wins over the region in the host") - func rdsExplicitRegionWins() throws { + @Test("The region in the endpoint's name wins over the AWS Region field, which covers a name with none") + func rdsEndpointRegionWins() throws { let signed = try token(rds(), fields: ["awsRegion": "eu-west-1"]) - #expect(signed.contains("%2Feu-west-1%2Frds-db%2Faws4_request")) + #expect(signed.contains("%2Fus-east-1%2Frds-db%2Faws4_request")) + + let global = try token(rds(host: "app.global-abc123.global.rds.amazonaws.com"), fields: ["awsRegion": "eu-west-1"]) + #expect(global.contains("%2Feu-west-1%2Frds-db%2Faws4_request")) } @Test("A host with no region and no AWS Region field is rejected") diff --git a/apple/Packages/TableProCore/Tests/TableProPluginKitTests/RDSAuthTokenTests.swift b/apple/Packages/TableProCore/Tests/TableProPluginKitTests/RDSAuthTokenTests.swift index e5fd04c5ef..b6024ac1a6 100644 --- a/apple/Packages/TableProCore/Tests/TableProPluginKitTests/RDSAuthTokenTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProPluginKitTests/RDSAuthTokenTests.swift @@ -79,32 +79,32 @@ struct RDSAuthTokenGeneratorTests { } } -struct RDSEndpointTests { +struct RDSRegionTests { @Test("Derives region from cluster hostname") func clusterHostname() { - #expect(RDSEndpoint.region(forHost: "mydb.cluster-abc123.us-east-1.rds.amazonaws.com") == "us-east-1") + #expect(AWSEndpointHost.rdsRegion(forHost: "mydb.cluster-abc123.us-east-1.rds.amazonaws.com") == "us-east-1") } @Test("Derives region from instance hostname") func instanceHostname() { - #expect(RDSEndpoint.region(forHost: "mydb.abc123.us-west-2.rds.amazonaws.com") == "us-west-2") + #expect(AWSEndpointHost.rdsRegion(forHost: "mydb.abc123.us-west-2.rds.amazonaws.com") == "us-west-2") } @Test("Derives region from China partition hostname") func chinaHostname() { - #expect(RDSEndpoint.region(forHost: "mydb.abc.cn-north-1.rds.amazonaws.com.cn") == "cn-north-1") + #expect(AWSEndpointHost.rdsRegion(forHost: "mydb.abc.cn-north-1.rds.amazonaws.com.cn") == "cn-north-1") } @Test("Returns nil for non-RDS hosts") func nonRDSHost() { - #expect(RDSEndpoint.region(forHost: "localhost") == nil) - #expect(RDSEndpoint.region(forHost: "db.example.com") == nil) + #expect(AWSEndpointHost.rdsRegion(forHost: "localhost") == nil) + #expect(AWSEndpointHost.rdsRegion(forHost: "db.example.com") == nil) } @Test("Derives a lowercase region from a hostname typed with capitals") func mixedCaseHostname() { - #expect(RDSEndpoint.region(forHost: "MyDB.abc123.US-EAST-1.RDS.AmazonAWS.com") == "us-east-1") - #expect(RDSEndpoint.region(forHost: "MyDB.Cluster-ABC.EU-West-1.Rds.Amazonaws.Com") == "eu-west-1") + #expect(AWSEndpointHost.rdsRegion(forHost: "MyDB.abc123.US-EAST-1.RDS.AmazonAWS.com") == "us-east-1") + #expect(AWSEndpointHost.rdsRegion(forHost: "MyDB.Cluster-ABC.EU-West-1.Rds.Amazonaws.Com") == "eu-west-1") } } @@ -206,7 +206,11 @@ struct RDSSigningEndpointResolverTests { @Test("An override may not redirect the token away from the host being dialled") func overrideCannotRedirectToAnotherHost() { - #expect(throws: AWSAuthError.rdsEndpointUnresolved(host: "evil.example.com")) { + let refusal = AWSAuthError.rdsEndpointNotDialed( + endpoint: "prod.abc123.us-east-1.rds.amazonaws.com", + host: "evil.example.com" + ) + #expect(throws: refusal) { _ = try resolve( host: "evil.example.com", port: 5_432, @@ -215,6 +219,41 @@ struct RDSSigningEndpointResolverTests { } } + @Test("A DNS name that starts like 127.0.0.1 is not a local forward, so the override is refused") + func loopbackLookalikeNameCannotTakeTheOverride() { + for host in ["127.0.0.1.attacker.example", "127.evil.example.com", "127.0.0.1.nip.io", "127.0.0.1.", "0177.12.34.56", "[::1]", "127.0.0.1,db.example.com"] { + let refusal = AWSAuthError.rdsEndpointNotDialed( + endpoint: "prod.abc123.us-east-1.rds.amazonaws.com", + host: AWSEndpointHost.normalized(host) + ) + #expect(throws: refusal) { + _ = try resolve(host: host, port: 3_306, override: "prod.abc123.us-east-1.rds.amazonaws.com:3306") + } + } + } + + @Test("Every 127/8 literal and the loopback names count as a local forward") + func loopbackLiteralsTakeTheOverride() throws { + for host in ["127.0.0.1", "127.1.2.3", "127.255.255.255", "127.1", "localhost", "LOCALHOST", "::1", "0.0.0.0"] { + let endpoint = try resolve(host: host, port: 54_321, override: "mydb.abc123.us-east-1.rds.amazonaws.com") + #expect(endpoint.host == "mydb.abc123.us-east-1.rds.amazonaws.com") + } + } + + @Test("A trailing dot on the host or the override is not signed") + func trailingDotIsDropped() throws { + let bare = RDSSigningEndpoint(host: "mydb.abc123.us-east-1.rds.amazonaws.com", port: 5_432) + #expect(try resolve(host: "mydb.abc123.us-east-1.rds.amazonaws.com.") == bare) + #expect(try resolve(host: "127.0.0.1", port: 62_000, override: "mydb.abc123.us-east-1.rds.amazonaws.com.:5432") == bare) + #expect(try resolve( + host: "127.0.0.1", + port: 62_000, + preTunnelHost: "mydb.abc123.us-east-1.rds.amazonaws.com.", + preTunnelPort: 5_432 + ) == bare) + #expect(try resolve(override: "mydb.abc123.us-east-1.rds.amazonaws.com.") == bare) + } + @Test("An override that restates the dialled host is accepted") func overrideMatchingDialledHostIsAccepted() throws { let endpoint = try resolve( @@ -244,7 +283,7 @@ struct RDSSigningEndpointResolverTests { preTunnelHost: "mydb.abc123.ap-south-1.rds.amazonaws.com", preTunnelPort: 5_432 ) - #expect(RDSEndpoint.region(forHost: endpoint.host) == "ap-south-1") + #expect(AWSEndpointHost.rdsRegion(forHost: endpoint.host) == "ap-south-1") } @Test("A host typed with capitals resolves to the lowercase endpoint") diff --git a/apple/Plugins/MySQLDriverPlugin/MySQLPluginDriver.swift b/apple/Plugins/MySQLDriverPlugin/MySQLPluginDriver.swift index e63ca8032d..6778d15221 100644 --- a/apple/Plugins/MySQLDriverPlugin/MySQLPluginDriver.swift +++ b/apple/Plugins/MySQLDriverPlugin/MySQLPluginDriver.swift @@ -819,7 +819,8 @@ final class MySQLPluginDriver: PluginDatabaseDriver, @unchecked Sendable { mariadbConnection?.disconnect() mariadbConnection = nil sessionLock.withLock { footprint.reset() } - try await connect() + // A minted IAM token is good for 15 minutes, and this sign-in can come long after the first. + try await open(password: try await config.passwordForNewSignIn()) if let seconds = sessionLock.withLock({ appliedQueryTimeoutSeconds }) { try await applyQueryTimeout(seconds) } diff --git a/apple/Plugins/TableProPluginKit/AWS/AWSAuthError.swift b/apple/Plugins/TableProPluginKit/AWS/AWSAuthError.swift index 8d6cef9abd..18e61e205a 100644 --- a/apple/Plugins/TableProPluginKit/AWS/AWSAuthError.swift +++ b/apple/Plugins/TableProPluginKit/AWS/AWSAuthError.swift @@ -12,6 +12,8 @@ public enum AWSAuthError: Error, LocalizedError, Equatable { case regionUnknown(host: String) case rdsEndpointUnresolved(host: String) case rdsEndpointInvalid(String) + case rdsEndpointNotDialed(endpoint: String, host: String) + case endpointLookupFailed(host: String) case credentialProcessInvalid(String) case credentialProcessLaunchFailed(profile: String, underlying: String) case credentialProcessFailed(profile: String, status: Int, message: String) @@ -48,6 +50,17 @@ public enum AWSAuthError: Error, LocalizedError, Equatable { format: String(localized: "TablePro cannot sign an RDS token for \"%@\". Enter the RDS Endpoint (for example mydb.abc123.us-east-1.rds.amazonaws.com:5432) when you connect through a port forward or bastion."), host ) + case .rdsEndpointNotDialed(let endpoint, let host): + return String( + // swiftlint:disable:next line_length + format: String(localized: "The RDS Endpoint \"%1$@\" is not the database at \"%2$@\", where the connection goes. Clear the RDS Endpoint unless you connect through a port forward."), + endpoint, host + ) + case .endpointLookupFailed(let host): + return String( + format: String(localized: "TablePro could not look up \"%@\" in DNS to find the AWS endpoint it points to. Check the network and try again."), + host + ) case .rdsEndpointInvalid(let value): return String( format: String(localized: "\"%@\" is not a valid RDS endpoint. Use the form host or host:port."), diff --git a/apple/Plugins/TableProPluginKit/AWS/AWSEndpointHost.swift b/apple/Plugins/TableProPluginKit/AWS/AWSEndpointHost.swift new file mode 100644 index 0000000000..2a365a3f57 --- /dev/null +++ b/apple/Plugins/TableProPluginKit/AWS/AWSEndpointHost.swift @@ -0,0 +1,146 @@ +import Darwin +import Foundation + +// An IAM credential is good for whatever endpoint it names, so these answers decide where one may go. A name counts +// as an RDS endpoint only in the shapes RDS publishes, under a partition's own DNS suffix and with a real region. +public enum AWSEndpointHost { + private static let loopbackNames: Set = ["localhost"] + + public static func normalized(_ host: String) -> String { + var name = host.trimmingCharacters(in: .whitespaces).lowercased() + if name.hasSuffix(".") { + name.removeLast() + } + return name + } + + // Decided by the same getaddrinfo call the driver's dial makes, so `127.1` and `::ffff:127.0.0.1` are loopback, + // while `127.0.0.1.example.com`, `127.0.0.1.`, `127.0.0.1 ` and `[::1]` are names it asks DNS about, which can + // answer anything. The host is read exactly as the driver gets it. + public static func isLoopback(_ host: String) -> Bool { + let name = host.lowercased() + if loopbackNames.contains(name) { + return true + } + switch numericAddress(name) { + case .ipv4(let address): + return address >> 24 == 127 || address == 0 + case .ipv6(let bytes): + let isLoopbackAddress = bytes[0 ..< 15].allSatisfy { $0 == 0 } && bytes[15] == 1 + let mapped = bytes[0 ..< 10].allSatisfy { $0 == 0 } && bytes[10] == 0xFF && bytes[11] == 0xFF + return isLoopbackAddress || (mapped && bytes[12] == 127) + case nil: + return false + } + } + + public static func isRDSEndpoint(_ host: String) -> Bool { + rdsName(normalized(host)) != nil + } + + // Nil for a name AWS did not issue, and for a global database endpoint, which names no region. + public static func rdsRegion(forHost host: String) -> String? { + rdsName(normalized(host))?.region + } + + // The RDS endpoint a name of the user's own stands for, found by following its CNAME records. `notFound` when + // `host` already is one, cannot have one, or leads to none. + public static func rdsEndpoint( + behind host: String, + resolver: any CNAMEResolving, + within timeout: Duration? + ) async -> CNAMELookup { + let name = normalized(host) + guard isAliasCandidate(name), !isRDSEndpoint(name) else { return .notFound } + return await resolver.firstName(behind: name, within: timeout, where: isRDSEndpoint) + } + + // A literal address has no CNAME, a single label is not looked up in DNS, and asking about a `.local` name sends + // multicast that iOS gates behind the Local Network permission. + public static func isAliasCandidate(_ host: String) -> Bool { + let name = normalized(host) + guard name.contains("."), !name.contains(":"), !name.contains("/"), !name.contains(","), + !name.hasSuffix(".local") + else { return false } + return !isLoopback(name) && numericAddress(name) == nil + } + + private struct RDSName { + let region: String? + } + + private static let dnsSuffixes: [String] = AWSPartition.all.reduce(into: []) { suffixes, partition in + if !suffixes.contains(partition.dnsSuffix) { + suffixes.append(partition.dnsSuffix) + } + } + + // `...rds.` everywhere, `..rds..` in China, GovCloud and the + // ISO partitions, and `.global-.global.rds.` for a global database. A Proxy's own endpoints add + // labels in front. + private static func rdsName(_ name: String) -> RDSName? { + for suffix in dnsSuffixes where name.hasSuffix("." + suffix) { + let labels = name.dropLast(suffix.count + 1).split(separator: ".", omittingEmptySubsequences: false) + guard labels.count >= 4, !labels.contains(where: \.isEmpty) else { continue } + let last = String(labels[labels.count - 1]) + let previous = String(labels[labels.count - 2]) + if last == "rds" { + if previous == "global" { + return RDSName(region: nil) + } + if isRegion(previous, under: suffix) { + return RDSName(region: previous) + } + } else if previous == "rds", isRegion(last, under: suffix) { + return RDSName(region: last) + } + } + return nil + } + + // Other services let customers pick labels under the same suffix (`.s3.amazonaws.com`), so the label has + // to read as a region, such as `us-east-1`, `cn-northwest-1` or `us-isob-east-1`. + private static func isRegion(_ label: String, under suffix: String) -> Bool { + let parts = label.split(separator: "-", omittingEmptySubsequences: false) + guard parts.count >= 3, + let number = parts.last, !number.isEmpty, number.allSatisfy({ $0.isASCII && $0.isNumber }), + parts.dropLast().allSatisfy({ !$0.isEmpty && $0.allSatisfy { $0 >= "a" && $0 <= "z" } }) + else { return false } + return AWSPartition.resolve(region: label).dnsSuffix == suffix + } + + private enum NumericAddress: Equatable { + case ipv4(UInt32) + case ipv6([UInt8]) + } + + // AI_NUMERICHOST parses without a lookup. Measured on macOS 27: `127.1`, `0x7f.1` and `2130706433` are + // 127.0.0.1, and `0177.0.0.1` is 177.0.0.1, not the octal 127.0.0.1 inet_aton reads. It also takes + // `::1%x,db.example.com` as ::1, while libpq dials each comma-separated host in turn, so only the characters of a + // bare address are let through. + private static func numericAddress(_ host: String) -> NumericAddress? { + guard !host.isEmpty, host.allSatisfy({ $0.isASCII && ($0.isHexDigit || $0 == "." || $0 == ":" || $0 == "x") }) else { + return nil + } + var hints = addrinfo() + hints.ai_flags = AI_NUMERICHOST + hints.ai_family = AF_UNSPEC + hints.ai_socktype = SOCK_STREAM + var list: UnsafeMutablePointer? + guard getaddrinfo(host, nil, &hints, &list) == 0, let list else { return nil } + defer { freeaddrinfo(list) } + guard let address = list.pointee.ai_addr else { return nil } + switch Int32(address.pointee.sa_family) { + case AF_INET: + return address.withMemoryRebound(to: sockaddr_in.self, capacity: 1) { + .ipv4(UInt32(bigEndian: $0.pointee.sin_addr.s_addr)) + } + case AF_INET6: + return address.withMemoryRebound(to: sockaddr_in6.self, capacity: 1) { pointer in + .ipv6(withUnsafeBytes(of: pointer.pointee.sin6_addr) { Array($0) }) + } + default: + return nil + } + } +} diff --git a/apple/Plugins/TableProPluginKit/AWS/AWSIAMTokenTarget.swift b/apple/Plugins/TableProPluginKit/AWS/AWSIAMTokenTarget.swift index 12e6444385..3fc6dfd570 100644 --- a/apple/Plugins/TableProPluginKit/AWS/AWSIAMTokenTarget.swift +++ b/apple/Plugins/TableProPluginKit/AWS/AWSIAMTokenTarget.swift @@ -47,7 +47,8 @@ public enum AWSIAMTokenTarget: Sendable, Equatable { override: fields["awsRDSEndpoint"], defaultPort: defaultPort ) - guard let region = Self.nonEmpty(fields["awsRegion"]) ?? RDSEndpoint.region(forHost: endpoint.host) else { + // The endpoint's own name decides: a token scoped to another region is refused, whatever the field says. + guard let region = AWSEndpointHost.rdsRegion(forHost: endpoint.host) ?? Self.nonEmpty(fields["awsRegion"]) else { throw AWSAuthError.regionUnknown(host: endpoint.host) } return RDSAuthTokenGenerator.generateToken( diff --git a/apple/Plugins/TableProPluginKit/AWS/AWSPartition.swift b/apple/Plugins/TableProPluginKit/AWS/AWSPartition.swift index 466c6721a7..a5e21e28ca 100644 --- a/apple/Plugins/TableProPluginKit/AWS/AWSPartition.swift +++ b/apple/Plugins/TableProPluginKit/AWS/AWSPartition.swift @@ -36,6 +36,8 @@ public struct AWSPartition: Sendable, Equatable { id: "aws-iso-f", dnsSuffix: "csp.hci.ic.gov", defaultRegion: "us-isof-east-1" ) + static let all: [AWSPartition] = [standard, china, govCloud, europeanSovereign, iso, isoB, isoE, isoF] + private static let byRegionPrefix: [(prefix: String, partition: AWSPartition)] = [ ("cn-", china), ("us-gov-", govCloud), diff --git a/apple/Plugins/TableProPluginKit/AWS/CNAMEResolver.swift b/apple/Plugins/TableProPluginKit/AWS/CNAMEResolver.swift new file mode 100644 index 0000000000..36be624dea --- /dev/null +++ b/apple/Plugins/TableProPluginKit/AWS/CNAMEResolver.swift @@ -0,0 +1,222 @@ +import dnssd +import Foundation + +// A lookup that failed is kept apart from a name with no CNAME: only the second is an answer. The three cases are +// every outcome a lookup has, so the enum is frozen. +@frozen +public enum CNAMELookup: Sendable, Equatable { + case found(String) + case notFound + case failed +} + +public protocol CNAMEResolving: Sendable { + // The target of `name`'s CNAME record: `notFound` when DNS says it has none, `failed` when the lookup errs or + // runs out of `timeout`. + func cnameTarget(of name: String, within timeout: Duration?) async -> CNAMELookup +} + +public extension CNAMEResolving { + // Follows the chain one record at a time, so it can stop at a name in the middle: the end of an RDS chain is an + // EC2, VPC endpoint or replica name that no token is signed for. A loop or an over-long chain finds nothing. + func firstName( + behind host: String, + within timeout: Duration?, + where matches: (String) -> Bool + ) async -> CNAMELookup { + let clock = ContinuousClock() + let deadline = timeout.map { clock.now.advanced(by: $0) } + var visited: Set = [DNSWireName.normalized(host)] + var current = host + for _ in 0 ..< DNSWireName.maximumChainLength { + let remaining = deadline.map { clock.now.duration(to: $0) } + if let remaining, remaining <= .zero { + return .failed + } + switch await cnameTarget(of: current, within: remaining) { + case .found(let target): + if matches(target) { + return .found(target) + } + guard visited.insert(target).inserted else { return .notFound } + current = target + case .notFound: + return .notFound + case .failed: + return .failed + } + } + return .notFound + } +} + +// The system resolver, which is what the driver's own lookup uses: it follows VPN split DNS and scoped resolvers, +// and libresolv does not. +public struct SystemCNAMEResolver: CNAMEResolving { + public init() {} + + public func cnameTarget(of name: String, within timeout: Duration?) async -> CNAMELookup { + let query = CNAMEQuery(name: name) + return await withTaskCancellationHandler { + await withCheckedContinuation { continuation in + query.start(timeout: timeout, continuation: continuation) + } + } onCancel: { + query.cancel() + } + } +} + +enum DNSWireName { + static let maximumChainLength = 8 + + static func normalized(_ name: String) -> String { + var text = name.trimmingCharacters(in: .whitespaces).lowercased() + if text.hasSuffix(".") { + text.removeLast() + } + return text + } + + // dnssd hands over the record's rdata uncompressed, so a pointer here means a malformed answer. + static func decode(_ bytes: UnsafeRawBufferPointer) -> String? { + var labels: [String] = [] + var index = 0 + while index < bytes.count { + let length = Int(bytes[index]) + index += 1 + if length == 0 { + guard !labels.isEmpty, index == bytes.count else { return nil } + let name = labels.joined(separator: ".") + return name.utf8.count <= 253 ? name : nil + } + guard length <= 63, index + length <= bytes.count else { return nil } + let label = bytes[index ..< index + length] + // The name can become the host a driver dials, so it holds only what a host name may. + guard label.allSatisfy(isHostNameByte), let text = String(bytes: label, encoding: .ascii) else { return nil } + labels.append(text.lowercased()) + index += length + } + return nil + } + + private static func isHostNameByte(_ byte: UInt8) -> Bool { + switch byte { + case UInt8(ascii: "a") ... UInt8(ascii: "z"), UInt8(ascii: "A") ... UInt8(ascii: "Z"), + UInt8(ascii: "0") ... UInt8(ascii: "9"), UInt8(ascii: "-"), UInt8(ascii: "_"): + true + default: + false + } + } +} + +// dnssd delivers on the queue a ref was given and wants the ref deallocated there, so all state lives on that queue. +private final class CNAMEQuery: @unchecked Sendable { + private static let queue = DispatchQueue(label: "com.TablePro.CNAMEQuery") + + private let name: String + private var ref: DNSServiceRef? + private var retained: Unmanaged? + private var continuation: CheckedContinuation? + private var isFinished = false + + init(name: String) { + self.name = DNSWireName.normalized(name) + } + + func start(timeout: Duration?, continuation: CheckedContinuation) { + Self.queue.async { + guard !self.isFinished else { + continuation.resume(returning: .failed) + return + } + self.continuation = continuation + if let timeout, timeout <= .zero { + self.finish(.failed) + return + } + let retained = Unmanaged.passRetained(self) + self.retained = retained + var ref: DNSServiceRef? + // Without ReturnIntermediates a name with no CNAME never answers, and the query waits out the system + // timeout of about 30 seconds (measured). + let status = DNSServiceQueryRecord( + &ref, + DNSServiceFlags(kDNSServiceFlagsReturnIntermediates | kDNSServiceFlagsTimeout), + UInt32(kDNSServiceInterfaceIndexAny), + self.name, + UInt16(kDNSServiceType_CNAME), + UInt16(kDNSServiceClass_IN), + { _, flags, _, error, fullname, rrtype, _, length, rdata, _, context in + guard let context else { return } + let query = Unmanaged.fromOpaque(context).takeUnretainedValue() + let owner = fullname.map { String(cString: $0) } + let bytes = rdata.map { UnsafeRawBufferPointer(start: $0, count: Int(length)) } + query.receive(flags: flags, error: error, owner: owner, rrtype: rrtype, rdata: bytes) + }, + retained.toOpaque() + ) + guard status == kDNSServiceErr_NoError, let ref else { + self.finish(.failed) + return + } + self.ref = ref + DNSServiceSetDispatchQueue(ref, Self.queue) + if let timeout { + Self.queue.asyncAfter(deadline: .now() + Self.seconds(timeout)) { + self.finish(.failed) + } + } + } + } + + func cancel() { + Self.queue.async { + self.finish(.failed) + } + } + + private func receive( + flags: DNSServiceFlags, + error: DNSServiceErrorType, + owner: String?, + rrtype: UInt16, + rdata: UnsafeRawBufferPointer? + ) { + guard !isFinished else { return } + guard error == kDNSServiceErr_NoError else { + // With ReturnIntermediates, NoSuchRecord is DNS saying the name has no CNAME, or does not exist. + finish(error == kDNSServiceErr_NoSuchRecord ? .notFound : .failed) + return + } + guard flags & DNSServiceFlags(kDNSServiceFlagsAdd) != 0, + Int(rrtype) == kDNSServiceType_CNAME, + owner.map(DNSWireName.normalized) == name, + let rdata + else { return } + finish(DNSWireName.decode(rdata).map(CNAMELookup.found) ?? .failed) + } + + // A cancel can land before the start block runs: it marks the query finished, and the start block then resumes + // the continuation it was handed. + private func finish(_ outcome: CNAMELookup) { + guard !isFinished else { return } + isFinished = true + if let ref { + DNSServiceRefDeallocate(ref) + self.ref = nil + } + if let continuation { + continuation.resume(returning: outcome) + self.continuation = nil + } + retained?.release() + retained = nil + } + + private static func seconds(_ duration: Duration) -> Double { + let components = duration.components + return Double(components.seconds) + Double(components.attoseconds) / 1e18 + } +} diff --git a/apple/Plugins/TableProPluginKit/AWS/RDSEndpoint.swift b/apple/Plugins/TableProPluginKit/AWS/RDSEndpoint.swift deleted file mode 100644 index ef574bbe44..0000000000 --- a/apple/Plugins/TableProPluginKit/AWS/RDSEndpoint.swift +++ /dev/null @@ -1,15 +0,0 @@ -import Foundation - -enum RDSEndpoint { - static func region(forHost host: String) -> String? { - let parts = host.lowercased().split(separator: ".").map(String.init) - guard let rdsIndex = parts.firstIndex(of: "rds"), - rdsIndex > 0, - parts.count > rdsIndex + 1, - parts[rdsIndex + 1] == "amazonaws" else { - return nil - } - let region = parts[rdsIndex - 1] - return region.isEmpty ? nil : region - } -} diff --git a/apple/Plugins/TableProPluginKit/AWS/RDSSigningEndpoint.swift b/apple/Plugins/TableProPluginKit/AWS/RDSSigningEndpoint.swift index 172b6cdd4c..f6100cc7a7 100644 --- a/apple/Plugins/TableProPluginKit/AWS/RDSSigningEndpoint.swift +++ b/apple/Plugins/TableProPluginKit/AWS/RDSSigningEndpoint.swift @@ -8,8 +8,6 @@ struct RDSSigningEndpoint: Equatable { // RDS validates the token against its own endpoint, while a tunnel or a port forward run outside // TablePro makes the driver dial loopback. This picks the endpoint the token is signed for. enum RDSSigningEndpointResolver { - private static let loopbackHosts: Set = ["localhost", "localhost.localdomain", "::1", "[::1]", "0.0.0.0"] - static func resolve( configuredHost: String, configuredPort: Int, @@ -18,22 +16,23 @@ enum RDSSigningEndpointResolver { override: String?, defaultPort: Int ) throws -> RDSSigningEndpoint { - let dialsLoopback = isLoopback(configuredHost) + let dialledHost = AWSEndpointHost.normalized(configuredHost) + let dialsLoopback = AWSEndpointHost.isLoopback(configuredHost) if let override, !override.trimmingCharacters(in: .whitespaces).isEmpty { let endpoint = try parse(override, defaultPort: defaultPort) - guard dialsLoopback || endpoint.host.caseInsensitiveCompare(configuredHost) == .orderedSame else { - throw AWSAuthError.rdsEndpointUnresolved(host: configuredHost) + guard dialsLoopback || endpoint.host == dialledHost else { + throw AWSAuthError.rdsEndpointNotDialed(endpoint: endpoint.host, host: dialledHost) } return endpoint } - let host = dialsLoopback ? (preTunnelHost ?? configuredHost) : configuredHost + let host = dialsLoopback ? (preTunnelHost.map(AWSEndpointHost.normalized) ?? dialledHost) : dialledHost let port = dialsLoopback ? (preTunnelPort ?? configuredPort) : configuredPort - guard !isLoopback(host) else { + guard !AWSEndpointHost.isLoopback(host) else { throw AWSAuthError.rdsEndpointUnresolved(host: host) } - return RDSSigningEndpoint(host: host.lowercased(), port: port) + return RDSSigningEndpoint(host: host, port: port) } static func parse(_ value: String, defaultPort: Int) throws -> RDSSigningEndpoint { @@ -58,18 +57,14 @@ enum RDSSigningEndpointResolver { return try makeEndpoint(host: String(text[.. Bool { - let normalized = host.trimmingCharacters(in: .whitespaces).lowercased() - return loopbackHosts.contains(normalized) || normalized.hasPrefix("127.") - } - private static func makeEndpoint(host: String, port: Int, rawValue: String) throws -> RDSSigningEndpoint { - let isHostname = !host.isEmpty - && !host.contains(":") - && host.rangeOfCharacter(from: .whitespacesAndNewlines) == nil - guard isHostname, !isLoopback(host) else { + let name = AWSEndpointHost.normalized(host) + let isHostname = !name.isEmpty + && !name.contains(":") + && name.rangeOfCharacter(from: .whitespacesAndNewlines) == nil + guard isHostname, !AWSEndpointHost.isLoopback(name) else { throw AWSAuthError.rdsEndpointInvalid(rawValue) } - return RDSSigningEndpoint(host: host.lowercased(), port: port) + return RDSSigningEndpoint(host: name, port: port) } } diff --git a/apple/TablePro/Core/Database/AWS/RedshiftIAMSignIn.swift b/apple/TablePro/Core/Database/AWS/RedshiftIAMSignIn.swift index 9ba0729632..0755cb44cc 100644 --- a/apple/TablePro/Core/Database/AWS/RedshiftIAMSignIn.swift +++ b/apple/TablePro/Core/Database/AWS/RedshiftIAMSignIn.swift @@ -9,15 +9,32 @@ import TableProPluginKit // Redshift issues the user and a temporary password together, so a sign-in mints both. struct RedshiftIAMSignIn: Sendable { + // A custom domain is asked for by name: AWS answers only for a domain associated with a cluster or workgroup in + // the caller's account, so the password still goes only to that database. enum Endpoint: Equatable, Sendable { case cluster(identifier: String, region: String) case serverless(workgroup: String, region: String) + case clusterDomain(name: String, region: String) + case serverlessDomain(name: String, region: String) var region: String { switch self { - case .cluster(_, let region), .serverless(_, let region): region + case .cluster(_, let region), .serverless(_, let region), + .clusterDomain(_, let region), .serverlessDomain(_, let region): + region } } + + var isServerless: Bool { + switch self { + case .serverless, .serverlessDomain: true + case .cluster, .clusterDomain: false + } + } + + func onCustomDomain(_ name: String) -> Endpoint { + isServerless ? .serverlessDomain(name: name, region: region) : .clusterDomain(name: name, region: region) + } } static let endpointFieldId = "awsRedshiftEndpoint" @@ -28,9 +45,10 @@ struct RedshiftIAMSignIn: Sendable { let fields: [String: String] let configuredHost: String let preTunnelHost: String? + var aliasResolver: any CNAMEResolving = SystemCNAMEResolver() func signIn(deadline: ConnectionDeadline?) async throws -> ResolvedSignIn { - let endpoint = try resolveEndpoint() + let endpoint = try await resolveEndpoint(within: deadline?.remainingDuration) let region = nonEmpty(fields["awsRegion"]) ?? endpoint.region return try await AWSDeadlineSession.run(within: deadline) { session in let credentials = try await AWSCredentialResolver.resolve(source: source, fields: fields, session: session) @@ -50,32 +68,59 @@ struct RedshiftIAMSignIn: Sendable { } } - func resolveEndpoint() throws -> Endpoint { + func resolveEndpoint(within timeout: Duration?) async throws -> Endpoint { + let dialledHost = AWSEndpointHost.normalized(configuredHost) + let dialsLoopback = AWSEndpointHost.isLoopback(configuredHost) + // The password goes to this name: the far end of a forward, else the host the driver dials. + let reachedHost = dialsLoopback ? preTunnelHost.map(AWSEndpointHost.normalized) : dialledHost if let override = nonEmpty(fields[Self.endpointFieldId]) { - let overrideHost = Self.hostName(in: override) + let overrideHost = AWSEndpointHost.normalized(Self.hostName(in: override)) guard let endpoint = Self.endpoint(forHost: overrideHost) else { throw RedshiftIAMError.endpointInvalid(override) } - // The password goes to the host the driver dials, so it must be a forward or that endpoint itself. - guard Self.isLoopback(configuredHost) - || overrideHost.caseInsensitiveCompare(configuredHost) == .orderedSame - else { + if dialsLoopback || overrideHost == dialledHost { + return endpoint + } + // Only a DNS name can be a custom domain; an address or another AWS endpoint is not the one named. + guard AWSEndpointHost.isAliasCandidate(dialledHost), Self.endpoint(forHost: dialledHost) == nil else { throw RedshiftIAMError.endpointNotDialed(host: configuredHost) } - return endpoint + return endpoint.onCustomDomain(dialledHost) } for host in [preTunnelHost, configuredHost].compactMap({ $0 }) { if let endpoint = Self.endpoint(forHost: host) { return endpoint } } + if let reachedHost, AWSEndpointHost.isAliasCandidate(reachedHost) { + let lookup = await aliasResolver.firstName( + behind: reachedHost, + within: timeout, + where: { Self.endpoint(forHost: $0) != nil } + ) + switch lookup { + case .found(let target): + if let endpoint = Self.endpoint(forHost: target) { + return endpoint.onCustomDomain(reachedHost) + } + case .failed: + throw AWSAuthError.endpointLookupFailed(host: reachedHost) + case .notFound: + break + } + } throw RedshiftIAMError.endpointUnresolved(host: preTunnelHost ?? configuredHost) } func request(for endpoint: Endpoint, region: String, credentials: AWSCredentials, now: Date = Date()) throws -> URLRequest { switch endpoint { - case .cluster(let identifier, _): - var parameters = ["Version": "2012-12-01", "ClusterIdentifier": identifier] + case .cluster, .clusterDomain: + var parameters = ["Version": "2012-12-01"] + switch endpoint { + case .clusterDomain(let name, _): parameters["CustomDomainName"] = name + case .cluster(let identifier, _): parameters["ClusterIdentifier"] = identifier + case .serverless, .serverlessDomain: break + } if let databaseUser = nonEmpty(databaseUser) { parameters["Action"] = "GetClusterCredentials" parameters["DbUser"] = databaseUser @@ -87,8 +132,13 @@ struct RedshiftIAMSignIn: Sendable { } return try AWSQueryRequest(service: "redshift", region: region, parameters: parameters) .signedURLRequest(credentials: credentials, now: now) - case .serverless(let workgroup, _): - var body = ["workgroupName": workgroup] + case .serverless, .serverlessDomain: + var body: [String: String] = [:] + switch endpoint { + case .serverlessDomain(let name, _): body["customDomainName"] = name + case .serverless(let workgroup, _): body["workgroupName"] = workgroup + case .cluster, .clusterDomain: break + } if let database = nonEmpty(database) { body["dbName"] = database } @@ -104,10 +154,7 @@ struct RedshiftIAMSignIn: Sendable { // Only a name AWS issued, under the region's own DNS suffix: anything else is someone else's host. static func endpoint(forHost host: String) -> Endpoint? { - var name = host.trimmingCharacters(in: .whitespaces).lowercased() - if name.hasSuffix(".") { - name.removeLast() - } + let name = AWSEndpointHost.normalized(host) let labels = name.split(separator: ".", omittingEmptySubsequences: false).map(String.init) guard labels.count > 4, !labels[0 ..< 3].contains(where: \.isEmpty) else { return nil } let region = labels[2] @@ -122,12 +169,11 @@ struct RedshiftIAMSignIn: Sendable { static func signIn(from data: Data, for endpoint: Endpoint) throws -> ResolvedSignIn { let user: String? let password: String? - switch endpoint { - case .cluster: + if !endpoint.isServerless { let parser = ClusterCredentialsXMLParser() guard parser.parse(data) else { throw RedshiftIAMError.unreadableResponse } (user, password) = (parser.databaseUser, parser.databasePassword) - case .serverless: + } else { let object = try? JSONSerialization.jsonObject(with: data) as? [String: Any] (user, password) = (object?["dbUser"] as? String, object?["dbPassword"] as? String) } @@ -138,21 +184,13 @@ struct RedshiftIAMSignIn: Sendable { } static func errorMessage(in data: Data, for endpoint: Endpoint) -> String? { - switch endpoint { - case .cluster: + guard endpoint.isServerless else { return AWSQueryErrorResponse.parse(data)?.summary - case .serverless: - guard let object = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else { return nil } - let code = (object["__type"] as? String).map { $0.split(separator: "#").last.map(String.init) ?? $0 } - let message = object["message"] as? String ?? object["Message"] as? String - return AWSQueryErrorResponse(code: code, message: message).summary } - } - - private static func isLoopback(_ host: String) -> Bool { - let normalized = host.trimmingCharacters(in: .whitespaces).lowercased() - return ["localhost", "localhost.localdomain", "::1", "[::1]", "0.0.0.0"].contains(normalized) - || normalized.hasPrefix("127.") + guard let object = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else { return nil } + let code = (object["__type"] as? String).map { $0.split(separator: "#").last.map(String.init) ?? $0 } + let message = object["message"] as? String ?? object["Message"] as? String + return AWSQueryErrorResponse(code: code, message: message).summary } private static func hostName(in value: String) -> String { @@ -203,8 +241,8 @@ enum RedshiftIAMError: LocalizedError, Equatable { return String( format: String(localized: """ TablePro cannot tell which Redshift cluster or workgroup "%@" is. Set Host to the \ - endpoint AWS issued, such as my-cluster.abc123.us-east-1.redshift.amazonaws.com, or \ - enter that endpoint as the Redshift Endpoint when you connect through a port forward. + endpoint AWS issued, such as my-cluster.abc123.us-east-1.redshift.amazonaws.com, or to a \ + custom domain name that points to it, or enter that endpoint as the Redshift Endpoint. """), host ) @@ -216,8 +254,8 @@ enum RedshiftIAMError: LocalizedError, Equatable { case .endpointNotDialed(let host): return String( format: String(localized: """ - The Redshift Endpoint applies only when Host is a local port forward or that same \ - endpoint, and "%@" is neither. + The Redshift Endpoint applies only when Host is a local port forward, a custom domain \ + name or that same endpoint, and "%@" is none of them. """), host ) diff --git a/apple/TablePro/Core/Database/ConnectionCredentialResolver.swift b/apple/TablePro/Core/Database/ConnectionCredentialResolver.swift index 74e46c8b23..ee7d29c157 100644 --- a/apple/TablePro/Core/Database/ConnectionCredentialResolver.swift +++ b/apple/TablePro/Core/Database/ConnectionCredentialResolver.swift @@ -171,6 +171,12 @@ enum ConnectionCredentialResolver { ) } + // The connections `iamPasswordMinter` gives an RDS token, which names the host it is signed for. + nonisolated static func signsRDSToken(for connection: DatabaseConnection) -> Bool { + guard case .aws = connection.iamSignIn else { return false } + return connection.type != .redshift && connection.type != .redis && !connection.resolvesAWSIAMInDriver + } + // The AWS token is signed for the resolved username: `connection.username` can lag behind its profile. static func iamPasswordMinter( for connection: DatabaseConnection, diff --git a/apple/TablePro/Core/Database/DatabaseManager+Health.swift b/apple/TablePro/Core/Database/DatabaseManager+Health.swift index b42ed9eae7..c44dcc514a 100644 --- a/apple/TablePro/Core/Database/DatabaseManager+Health.swift +++ b/apple/TablePro/Core/Database/DatabaseManager+Health.swift @@ -197,7 +197,8 @@ extension DatabaseManager { return ConnectionFailureClassifier.reconnectVerdict(for: tlsError) } // A refusal that needs a person gets the same answer on every retry. - if error is EnvVarReferenceConsentError || error is PreConnectScriptConsentError { + if error is EnvVarReferenceConsentError || error is PreConnectScriptConsentError + || error is LocalCredentialConsentError { return .stop(ConnectionFailureClassifier.info(for: error)) } guard isAuthenticationFailure(error) else { return .retry(nil) } @@ -229,6 +230,12 @@ extension DatabaseManager { message: String(localized: "The connection was closed and reconnecting was cancelled.") ) + // A direct dial reaches another host when the CNAME of an AWS IAM connection now leads to another RDS endpoint. + internal func movesEndpoint(of session: ConnectionSession, to rebuilt: DatabaseConnection) -> Bool { + let previous = session.effectiveConnection ?? session.connection + return rebuilt.host.caseInsensitiveCompare(previous.host) != .orderedSame + } + internal struct ReconnectResult { let driver: DatabaseDriver let effectiveConnection: DatabaseConnection @@ -246,23 +253,26 @@ extension DatabaseManager { noteTransactionLost(with: session.driver, for: connectionId) session.driver?.disconnect() // Resolved as the connect resolved it; a reference that lost its approval needs a person. - let connection = try await envVarReferenceConsent.resolve(session.connection, trigger: .background) - let preparedConfiguration = try await DatabaseDriverFactory.prepareConfiguration(for: connection) + let resolved = try await envVarReferenceConsent.resolve(session.connection, trigger: .background) + // Followed again, for a direct dial too: the CNAME may lead to another RDS endpoint than at the connect. + let connection = try await resolvingRDSEndpointAlias(of: resolved, trigger: .background) + let preparedConfiguration = try await DatabaseDriverFactory.prepareConfiguration(for: resolved) let deadline = ConnectionDeadline(configuredSeconds: connection.connectTimeoutSeconds) try requireCurrentAttempt(generation, for: connectionId) - let connectionForDriver: DatabaseConnection - if connection.activeTunnelKind != nil { + let tunneled = connection.activeTunnelKind != nil + if tunneled { // A rebuilt tunnel moves to a new local port, stranding every other database's connection. await closeTransportLanes(for: connectionId) { self.isCurrentAttempt(generation, for: connectionId) } - connectionForDriver = try await buildEffectiveConnection( - for: connection, - deadline: deadline, - ownsAttempt: { self.isCurrentAttempt(generation, for: connectionId) } - ) - try requireCurrentAttempt(generation, for: connectionId) - } else { - connectionForDriver = session.effectiveConnection ?? session.connection + } + let connectionForDriver = try await buildEffectiveConnection( + for: connection, + deadline: deadline, + ownsAttempt: { self.isCurrentAttempt(generation, for: connectionId) } + ) + try requireCurrentAttempt(generation, for: connectionId) + if !tunneled, movesEndpoint(of: session, to: connectionForDriver) { + await closeTransportLanes(for: connectionId) { self.isCurrentAttempt(generation, for: connectionId) } } guard let connectResult = try await connectReconnectDriver( @@ -417,14 +427,32 @@ extension DatabaseManager { } try requireCurrentAttempt(generation, for: sessionId) - let preparedConfiguration = try await DatabaseDriverFactory.prepareConfiguration(for: session.connection) - let deadline = ConnectionDeadline(configuredSeconds: session.connection.connectTimeoutSeconds) + // Resolved as the connect resolved it: the session keeps the row as saved, `${VAR}` references included. + let resolved = try await envVarReferenceConsent.resolve(session.connection, trigger: .request) + try requireCurrentAttempt(generation, for: sessionId) + let connection = try await resolvingRDSEndpointAlias(of: resolved, trigger: .request) + try requireCurrentAttempt(generation, for: sessionId) + let preparedConfiguration = try await DatabaseDriverFactory.prepareConfiguration(for: resolved) + let deadline = ConnectionDeadline(configuredSeconds: connection.connectTimeoutSeconds) let effectiveConnection = try await buildEffectiveConnection( - for: session.connection, + for: connection, deadline: deadline, ownsAttempt: { self.isCurrentAttempt(generation, for: sessionId) } ) try requireCurrentAttempt(generation, for: sessionId) + // Pooled and parked connections stand on the old endpoint, so they go the way a rebuilt tunnel's do. + let movesPooledEndpoint = !replacesPooledTransport && movesEndpoint(of: session, to: effectiveConnection) + if movesPooledEndpoint { + await RoutineDebugSessions.shared.stopAll(connectionId: sessionId) + MetadataConnectionPool.shared.beginTransportReplacement(connectionId: sessionId) + await closeTransportLanes(for: sessionId) { self.isCurrentAttempt(generation, for: sessionId) } + } + defer { + if movesPooledEndpoint { + MetadataConnectionPool.shared.endTransportReplacement(connectionId: sessionId) + RoutineDebugSessions.shared.reopenAdmission(for: sessionId) + } + } // Nil means the user declined the re-prompt after an auth failure: a cancellation, not a refusal. guard let connectResult = try await connectReconnectDriver( diff --git a/apple/TablePro/Core/Database/DatabaseManager+Queries.swift b/apple/TablePro/Core/Database/DatabaseManager+Queries.swift index 16d495e9d6..0dd30526dd 100644 --- a/apple/TablePro/Core/Database/DatabaseManager+Queries.swift +++ b/apple/TablePro/Core/Database/DatabaseManager+Queries.swift @@ -59,13 +59,19 @@ extension DatabaseManager { } } + let dialled: DatabaseConnection + do { + dialled = try await resolvingRDSEndpointAlias(of: connection, trigger: .request) + } catch { + throw await diagnosedConnectFailure(error, for: connection) + } let preparedConfiguration = try await DatabaseDriverFactory.prepareConfiguration(for: connection) let deadline = ConnectionDeadline(configuredSeconds: connection.connectTimeoutSeconds) let testConnection: DatabaseConnection do { testConnection = try await buildEffectiveConnection( - for: connection, + for: dialled, sshPasswordOverride: sshPassword, deadline: deadline ) diff --git a/apple/TablePro/Core/Database/DatabaseManager+RDSEndpoint.swift b/apple/TablePro/Core/Database/DatabaseManager+RDSEndpoint.swift new file mode 100644 index 0000000000..d985d6a459 --- /dev/null +++ b/apple/TablePro/Core/Database/DatabaseManager+RDSEndpoint.swift @@ -0,0 +1,53 @@ +// +// DatabaseManager+RDSEndpoint.swift +// TablePro +// + +import Foundation +import os +import TableProPluginKit + +extension DatabaseManager { + // RDS accepts a token only for the endpoint AWS issued, never a CNAME of the user's own. The connection dials + // that endpoint as well: signing it while dialling the CNAME would let whoever answers the CNAME's next lookup + // receive the token. The found name is never saved, so each connect follows the CNAME again. + // + // Only a direct dial: behind a tunnel the far end resolves the name, its DNS can disagree with this Mac's, and a + // bastion's PermitOpen matches the name as typed. The RDS Endpoint field covers a forward. + // + // Runs before the connect's deadline starts, as the first sign-in approval does, because it can ask a person. + internal func resolvingRDSEndpointAlias( + of connection: DatabaseConnection, + trigger: ConnectTrigger + ) async throws -> DatabaseConnection { + guard ConnectionCredentialResolver.signsRDSToken(for: connection), + connection.enabledTunnelKinds.isEmpty, + AWSEndpointHost.isAliasCandidate(connection.host) + else { return connection } + let lookupDeadline = ConnectionDeadline(configuredSeconds: connection.connectTimeoutSeconds) + let lookup = await AWSEndpointHost.rdsEndpoint( + behind: connection.host, + resolver: cnameResolver, + within: lookupDeadline.remainingDuration + ) + try Task.checkCancellation() + switch lookup { + case .notFound: + return connection + case .failed: + // Signing the typed name instead only earns an access denied, which reads as a credential problem and + // ends a reconnect that a working DNS would have let through. + throw AWSAuthError.endpointLookupFailed(host: connection.host) + case .found(let endpoint): + Self.logger.info("AWS IAM signs in as RDS endpoint \(endpoint, privacy: .private), which \(connection.host, privacy: .private) points to") + var resolved = connection + resolved.host = endpoint + // A definition from someone else was approved for the name it carries, and whoever runs that name's DNS + // can point it at another database at any time, so each endpoint behind it is approved on its own. + if connection.origin != .thisMac { + try await localCredentialConsent.check(resolved, trigger: trigger) + } + return resolved + } + } +} diff --git a/apple/TablePro/Core/Database/DatabaseManager+Sessions.swift b/apple/TablePro/Core/Database/DatabaseManager+Sessions.swift index 2e0bfa525e..513ff016d2 100644 --- a/apple/TablePro/Core/Database/DatabaseManager+Sessions.swift +++ b/apple/TablePro/Core/Database/DatabaseManager+Sessions.swift @@ -49,24 +49,11 @@ extension DatabaseManager { let effectiveQueryTimeoutSeconds = prepareConnectingSession(for: connection) lastActiveSessionId = connection.id - let resolvedConnection: DatabaseConnection - do { - // Before anything signs in: the host resolves some credentials below, and a plugin the rest in `connect()`. - try await localCredentialConsent.check(connection, trigger: trigger) - resolvedConnection = try await envVarReferenceConsent.resolve(connection, trigger: trigger) - try await preConnectScriptConsent.authorize(connection, trigger: trigger) - // The answer can take a minute, long enough for the person to cancel the attempt. - guard !isAttemptCancelled(attempt, for: connection.id) else { throw CancellationError() } - try await runPreConnectScriptIfNeeded(connection, resolvedConnection: resolvedConnection, attempt: attempt) - } catch { - finalizeConnectionFailure( - for: connection.id, - cancelled: isAttemptCancelled(attempt, for: connection.id), - error: error, - attempt: attempt - ) - throw error - } + let (resolvedConnection, dialledConnection) = try await authorizeConnect( + connection, + trigger: trigger, + attempt: attempt + ) let (passwordOverride, promptsForPassword, promptCacheKey) = try await resolvePasswordOverride( for: connection, @@ -87,7 +74,7 @@ extension DatabaseManager { reportStage(.resolvingTunnel, attempt: attempt, for: connection.id) } effectiveConnection = try await buildEffectiveConnectionReportingStages( - for: resolvedConnection, + for: dialledConnection, sshPasswordOverride: sshPasswordOverride, deadline: deadline, attempt: (attempt, connection.id) @@ -1083,6 +1070,37 @@ extension DatabaseManager { connectionStages.removeValue(forKey: connectionId) } + // What a connect settles before it dials, in order: approvals for this Mac's credentials, `${VAR}` references + // and the pre-connect script, the script itself, then the RDS endpoint a CNAME stands for, which can ask again. + // All of it runs before the connect's deadline starts, since each step can wait on a person. + private func authorizeConnect( + _ connection: DatabaseConnection, + trigger: ConnectTrigger, + attempt: Int + ) async throws -> (resolved: DatabaseConnection, dialled: DatabaseConnection) { + do { + // Before anything signs in: the host resolves some credentials below, and a plugin the rest in `connect()`. + try await localCredentialConsent.check(connection, trigger: trigger) + let resolved = try await envVarReferenceConsent.resolve(connection, trigger: trigger) + try await preConnectScriptConsent.authorize(connection, trigger: trigger) + // The answer can take a minute, long enough for the person to cancel the attempt. + guard !isAttemptCancelled(attempt, for: connection.id) else { throw CancellationError() } + try await runPreConnectScriptIfNeeded(connection, resolvedConnection: resolved, attempt: attempt) + // After the script, which can be what makes the name resolve, such as a VPN it brings up. + let dialled = try await resolvingRDSEndpointAlias(of: resolved, trigger: trigger) + guard !isAttemptCancelled(attempt, for: connection.id) else { throw CancellationError() } + return (resolved, dialled) + } catch { + finalizeConnectionFailure( + for: connection.id, + cancelled: isAttemptCancelled(attempt, for: connection.id), + error: error, + attempt: attempt + ) + throw error + } + } + // The proxy reports its download as a stage of this attempt. private func buildEffectiveConnectionReportingStages( for resolvedConnection: DatabaseConnection, diff --git a/apple/TablePro/Core/Database/DatabaseManager.swift b/apple/TablePro/Core/Database/DatabaseManager.swift index 15072aca99..909930b084 100644 --- a/apple/TablePro/Core/Database/DatabaseManager.swift +++ b/apple/TablePro/Core/Database/DatabaseManager.swift @@ -25,6 +25,7 @@ final class DatabaseManager: ObservableObject { internal let localCredentialConsent: LocalCredentialConsent internal let postDisconnectScripts: PostDisconnectScripts internal let localNetworkProbe: any LocalNetworkAccessProbing + internal let cnameResolver: any CNAMEResolving internal var historyRecorder: QueryHistoryRecording = QueryHistoryManager.shared // Keyed by the credential profile, or by the connection and where it dials: a linked-folder row keeps its id @@ -191,6 +192,7 @@ final class DatabaseManager: ObservableObject { localCredentialConsent: LocalCredentialConsent = LocalCredentialConsent(), postDisconnectScripts: PostDisconnectScripts = PostDisconnectScripts(), localNetworkProbe: any LocalNetworkAccessProbing = NetworkFrameworkLocalNetworkProbe(), + cnameResolver: any CNAMEResolving = SystemCNAMEResolver(), recoveryClock: any Clock = ContinuousClock() ) { self.connectionStorage = connectionStorage @@ -202,6 +204,7 @@ final class DatabaseManager: ObservableObject { self.localCredentialConsent = localCredentialConsent self.postDisconnectScripts = postDisconnectScripts self.localNetworkProbe = localNetworkProbe + self.cnameResolver = cnameResolver self.recoveryClock = recoveryClock sessionLanes.onClose = { [weak self] connectionId in self?.sessionTransactions.prune(connectionId) diff --git a/apple/TablePro/Models/Connection/DatabaseConnection+IAMSignIn.swift b/apple/TablePro/Models/Connection/DatabaseConnection+IAMSignIn.swift index 8b3c0fa05e..ee33750411 100644 --- a/apple/TablePro/Models/Connection/DatabaseConnection+IAMSignIn.swift +++ b/apple/TablePro/Models/Connection/DatabaseConnection+IAMSignIn.swift @@ -36,8 +36,24 @@ enum GoogleCloudSQLCredentialKind: Equatable, Sendable { extension DatabaseConnection { static let iamSignInFieldId = "awsAuth" + // Only a value the type's own Authentication picker offers. A definition from someone else can carry the field on + // an engine that takes no IAM token, which would sign one and send it to whatever servers that engine dials. The + // Keyspaces engines are the exception for AWS: their plugin signs in itself and this host mints nothing for them. var iamSignIn: IAMSignInMethod? { - IAMSignInMethod(pickerValue: additionalFields[Self.iamSignInFieldId]) + guard let value = additionalFields[Self.iamSignInFieldId], + let method = IAMSignInMethod(pickerValue: value) + else { return nil } + if resolvesAWSIAMInDriver, case .aws = method { + return method + } + return Self.offersSignIn(value, for: type) ? method : nil + } + + private static func offersSignIn(_ value: String, for type: DatabaseType) -> Bool { + let field = PluginMetadataRegistry.shared.snapshot(for: type)?.connection.additionalConnectionFields + .first { $0.id == iamSignInFieldId } + guard case .dropdown(let options) = field?.fieldType else { return false } + return options.contains { $0.value == value } } var usesAWSIAM: Bool { diff --git a/apple/TablePro/Resources/Localizable.xcstrings b/apple/TablePro/Resources/Localizable.xcstrings index 01982c4328..a749d83696 100644 --- a/apple/TablePro/Resources/Localizable.xcstrings +++ b/apple/TablePro/Resources/Localizable.xcstrings @@ -181205,6 +181205,9 @@ }, "TablePro cannot tell which Redshift cluster or workgroup \"%@\" is. Set Host to the endpoint AWS issued, such as my-cluster.abc123.us-east-1.redshift.amazonaws.com, or enter that endpoint as the Redshift Endpoint when you connect through a port forward." : { + }, + "TablePro cannot tell which Redshift cluster or workgroup \"%@\" is. Set Host to the endpoint AWS issued, such as my-cluster.abc123.us-east-1.redshift.amazonaws.com, or to a custom domain name that points to it, or enter that endpoint as the Redshift Endpoint." : { + }, "TablePro cannot upload changes until there is space in iCloud. Changes from this Mac are kept here and upload once you free up space or add storage. To manage storage, open System Settings, click your name, then click iCloud." : { @@ -181347,6 +181350,10 @@ } } }, + "TablePro could not look up \"%@\" in DNS to find the AWS endpoint it points to. Check the network and try again." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "TablePro could not open its query history database, so nothing is being recorded and nothing can be shown. Your existing history is still on disk." : { "comment" : "A description of the error that occurs when the query history database cannot be opened.", "isCommentAutoGenerated" : true, @@ -191819,6 +191826,10 @@ } } }, + "The RDS Endpoint \"%1$@\" is not the database at \"%2$@\", where the connection goes. Clear the RDS Endpoint unless you connect through a port forward." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "The Realtime Database cannot filter with %@. It supports =, <, <=, >, >=, BETWEEN, STARTS WITH and IS NULL." : { "extractionState" : "manual", "generatesSymbol" : false @@ -191877,6 +191888,9 @@ }, "The Redshift Endpoint applies only when Host is a local port forward or that same endpoint, and \"%@\" is neither." : { + }, + "The Redshift Endpoint applies only when Host is a local port forward, a custom domain name or that same endpoint, and \"%@\" is none of them." : { + }, "the reference “%@” is inside itself" : { "extractionState" : "manual", diff --git a/apple/TableProMobile/TableProMobile/Helpers/AppError+IAM.swift b/apple/TableProMobile/TableProMobile/Helpers/AppError+IAM.swift index 5a2a5a3cea..744f505dd1 100644 --- a/apple/TableProMobile/TableProMobile/Helpers/AppError+IAM.swift +++ b/apple/TableProMobile/TableProMobile/Helpers/AppError+IAM.swift @@ -84,6 +84,22 @@ nonisolated extension ErrorClassifier { recovery: String(localized: "Enter the RDS Endpoint in this connection's Authentication section."), opensEditor: true ) + case .endpointLookupFailed: + return failure( + error, + .network, + title: String(localized: "DNS Lookup Failed"), + recovery: String(localized: "Check the network connection and try again."), + opensEditor: false + ) + case .rdsEndpointNotDialed: + return failure( + error, + .config, + title: String(localized: "RDS Endpoint Does Not Match"), + recovery: String(localized: "Clear the RDS Endpoint in this connection's Authentication section."), + opensEditor: true + ) case .rdsEndpointInvalid: return failure( error, diff --git a/apple/TableProMobile/TableProMobile/Localizable.xcstrings b/apple/TableProMobile/TableProMobile/Localizable.xcstrings index 869b707d46..e7bdcc8f4d 100644 --- a/apple/TableProMobile/TableProMobile/Localizable.xcstrings +++ b/apple/TableProMobile/TableProMobile/Localizable.xcstrings @@ -4825,6 +4825,9 @@ }, "Check the host name, or set SSL Mode to Verify CA to skip the name check." : { + }, + "Check the network connection and try again." : { + }, "Check the server's TLS settings and this connection's SSL Mode." : { @@ -5423,6 +5426,9 @@ } } } + }, + "Clear the RDS Endpoint in this connection's Authentication section." : { + }, "ClickHouse ended the result partway through a row, so the rows that arrived are incomplete." : { @@ -7481,6 +7487,9 @@ "Crown" : { "extractionState" : "manual", "generatesSymbol" : false + }, + "DNS Lookup Failed" : { + }, "Data & Analytics" : { "extractionState" : "manual", @@ -18456,6 +18465,9 @@ }, "RDS Endpoint (optional)" : { + }, + "RDS Endpoint Does Not Match" : { + }, "RDS Endpoint Invalid" : { @@ -23242,6 +23254,10 @@ }, "TablePro closed %@ when it went to the background, which rolled back its open transaction. It has been reopened; run the statements again, and commit before you leave the app." : { + }, + "TablePro could not look up \"%@\" in DNS to find the AWS endpoint it points to. Check the network and try again." : { + "extractionState" : "manual", + "generatesSymbol" : false }, "TablePro could not read that file." : { "localizations" : { @@ -26313,6 +26329,10 @@ }, "The socket path must start with /." : { + }, + "The RDS Endpoint \"%1$@\" is not the database at \"%2$@\", where the connection goes. Clear the RDS Endpoint unless you connect through a port forward." : { + "extractionState" : "manual", + "generatesSymbol" : false }, "The SQLite extensions for this connection could not be loaded." : { "extractionState" : "manual", diff --git a/apple/TableProMobile/TableProMobile/Platform/IOSDriverFactory.swift b/apple/TableProMobile/TableProMobile/Platform/IOSDriverFactory.swift index d2d895b1bb..ce74cc3eab 100644 --- a/apple/TableProMobile/TableProMobile/Platform/IOSDriverFactory.swift +++ b/apple/TableProMobile/TableProMobile/Platform/IOSDriverFactory.swift @@ -8,6 +8,8 @@ import TableProRedisCore nonisolated final class IOSDriverFactory: DriverFactory { private static let logger = Logger(subsystem: "com.TablePro", category: "IOSDriverFactory") + // A lookup still waiting after this has failed; each hop would otherwise wait out dnssd's 30-second timeout. + private static let endpointLookupSeconds = 10 private let bookmarkStore: FileBookmarkStore private let materializer: CertificateMaterializer @@ -18,6 +20,7 @@ nonisolated final class IOSDriverFactory: DriverFactory { private let apiSessionConfiguration: @Sendable () -> URLSessionConfiguration // Times how long a MySQL kill may reuse the token its session signed in with. private let clock: any Clock + private let cnameResolver: any CNAMEResolving init( bookmarkStore: FileBookmarkStore = FileBookmarkStore(), @@ -26,7 +29,8 @@ nonisolated final class IOSDriverFactory: DriverFactory { mint: IAMPasswordMint = IAMPasswordMint(), secureStore: any SecureStore = KeychainSecureStore(), apiSessionConfiguration: @escaping @Sendable () -> URLSessionConfiguration = { .default }, - clock: any Clock = ContinuousClock() + clock: any Clock = ContinuousClock(), + cnameResolver: any CNAMEResolving = SystemCNAMEResolver() ) { self.bookmarkStore = bookmarkStore self.materializer = materializer @@ -35,6 +39,7 @@ nonisolated final class IOSDriverFactory: DriverFactory { self.secureStore = secureStore self.apiSessionConfiguration = apiSessionConfiguration self.clock = clock + self.cnameResolver = cnameResolver } private func sqliteSource(for connection: DatabaseConnection) throws -> LocalDatabaseFileSource { @@ -106,6 +111,32 @@ nonisolated final class IOSDriverFactory: DriverFactory { return resolved } + // RDS takes a token only for the endpoint AWS issued, so a CNAME of the user's own is dialled as that endpoint + // too: signing it while dialling the CNAME would hand the token to whoever answers the CNAME's next lookup. Not + // behind SSH, where the server resolves the name and its PermitOpen matches it as typed. + func resolvingEndpointAlias(for connection: DatabaseConnection) async throws -> DatabaseConnection { + guard connection.iamSignIn?.isAWS == true, + IAMSignIn.engine(for: connection.type)?.awsService == .rds, + !dialsSSHTunnel(for: connection) + else { return connection } + let lookup = await AWSEndpointHost.rdsEndpoint( + behind: connection.host, + resolver: cnameResolver, + within: .seconds(connection.connectTimeoutSeconds ?? Self.endpointLookupSeconds) + ) + try Task.checkCancellation() + switch lookup { + case .notFound: + return connection + case .failed: + throw AWSAuthError.endpointLookupFailed(host: connection.host) + case .found(let endpoint): + var resolved = connection + resolved.host = endpoint + return resolved + } + } + func resolvePassword( for connection: DatabaseConnection, stored: String?, diff --git a/apple/TableProMobile/TableProMobileTests/SignIn/IAMErrorClassifierTests.swift b/apple/TableProMobile/TableProMobileTests/SignIn/IAMErrorClassifierTests.swift index deefd96c8f..206b6a0f35 100644 --- a/apple/TableProMobile/TableProMobileTests/SignIn/IAMErrorClassifierTests.swift +++ b/apple/TableProMobile/TableProMobileTests/SignIn/IAMErrorClassifierTests.swift @@ -29,6 +29,27 @@ struct IAMErrorClassifierTests { #expect(error.opensEditor) } + @Test("A DNS lookup that failed reads as a network problem") + func endpointLookupFailed() { + let error = classify(AWSAuthError.endpointLookupFailed(host: "reader.db.example.com")) + + #expect(error.title == String(localized: "DNS Lookup Failed")) + #expect(error.category == .network) + #expect(!error.opensEditor) + } + + @Test("An RDS Endpoint that names another database than Host says to clear it") + func rdsEndpointNotDialed() { + let error = classify(AWSAuthError.rdsEndpointNotDialed( + endpoint: "prod.abc123.us-east-1.rds.amazonaws.com", + host: "db.internal.example.com" + )) + + #expect(error.title == String(localized: "RDS Endpoint Does Not Match")) + #expect(error.category == .config) + #expect(error.opensEditor) + } + @Test("The PluginKit missing-key error reads as the same problem, though its message says authentication") func pluginKitMissingAccessKey() { let error = classify(AWSAuthError.missingAccessKey) diff --git a/apple/TableProMobile/TableProMobileTests/SignIn/IAMPasswordMintTests.swift b/apple/TableProMobile/TableProMobileTests/SignIn/IAMPasswordMintTests.swift index 0e004aea96..ee1cde7e7b 100644 --- a/apple/TableProMobile/TableProMobileTests/SignIn/IAMPasswordMintTests.swift +++ b/apple/TableProMobile/TableProMobileTests/SignIn/IAMPasswordMintTests.swift @@ -146,7 +146,8 @@ struct IAMPasswordMintTests { fields: [IAMSignIn.Field.rdsEndpoint: "\(Self.rdsHost):5432"] ) - await #expect(throws: AWSAuthError.rdsEndpointUnresolved(host: "db.internal.example.com")) { + let refusal = AWSAuthError.rdsEndpointNotDialed(endpoint: Self.rdsHost, host: "db.internal.example.com") + await #expect(throws: refusal) { try await mint.password(for: connection, method: .awsAccessKey, secureStore: store) } } diff --git a/apple/TableProMobile/TableProMobileTests/SignIn/IOSDriverFactoryEndpointAliasTests.swift b/apple/TableProMobile/TableProMobileTests/SignIn/IOSDriverFactoryEndpointAliasTests.swift new file mode 100644 index 0000000000..6429ce3418 --- /dev/null +++ b/apple/TableProMobile/TableProMobileTests/SignIn/IOSDriverFactoryEndpointAliasTests.swift @@ -0,0 +1,85 @@ +import Foundation +@testable import TableProMobile +import TableProModels +import TableProPluginKit +import Testing + +private final class FixedCNAMEResolver: CNAMEResolving, @unchecked Sendable { + private let lock = NSLock() + private let records: [String: String] + private var asked: [String] = [] + + init(_ records: [String: String]) { + self.records = records + } + + var queried: [String] { + lock.withLock { asked } + } + + func cnameTarget(of name: String, within timeout: Duration?) async -> CNAMELookup { + lock.withLock { + asked.append(name) + return records[name].map(CNAMELookup.found) ?? .failed + } + } +} + +struct IOSDriverFactoryEndpointAliasTests { + private static let alias = "reader.db.example.com" + private static let endpoint = "replica-a.abc123.us-east-1.rds.amazonaws.com" + + private func connection( + type: DatabaseType = .mysql, + method: IAMSignIn? = .awsProfile, + sshEnabled: Bool = false + ) -> DatabaseConnection { + var fields: [String: String] = [:] + fields[IAMSignIn.Field.method] = method?.pickerValue + return DatabaseConnection( + type: type, + host: Self.alias, + port: 3_306, + username: "iam_user", + additionalFields: fields, + sshEnabled: sshEnabled, + sshConfiguration: sshEnabled ? SSHConfiguration(host: "bastion.example.com", username: "ec2-user") : nil + ) + } + + @Test("An AWS IAM connection to a CNAME dials the RDS endpoint it stands for") + func awsConnectionDialsTheEndpoint() async throws { + let resolver = FixedCNAMEResolver([Self.alias: Self.endpoint]) + let factory = IOSDriverFactory(cnameResolver: resolver) + + for type in [DatabaseType.mysql, .mariadb, .postgresql] { + let resolved = try await factory.resolvingEndpointAlias(for: connection(type: type)) + #expect(resolved.host == Self.endpoint, "\(type)") + } + } + + @Test("Nothing is looked up behind SSH, without AWS IAM, or for ElastiCache") + func otherConnectionsAreNotLookedUp() async throws { + let resolver = FixedCNAMEResolver([Self.alias: Self.endpoint]) + let factory = IOSDriverFactory(cnameResolver: resolver) + + for saved in [ + connection(sshEnabled: true), + connection(method: nil), + connection(method: .googleApplicationDefault), + connection(type: .redis) + ] { + #expect(try await factory.resolvingEndpointAlias(for: saved).host == Self.alias) + } + #expect(resolver.queried.isEmpty) + } + + @Test("A lookup that fails stops the connect with an error naming the host") + func failedLookupThrows() async { + let factory = IOSDriverFactory(cnameResolver: FixedCNAMEResolver([:])) + + await #expect(throws: AWSAuthError.endpointLookupFailed(host: Self.alias)) { + _ = try await factory.resolvingEndpointAlias(for: connection()) + } + } +} diff --git a/apple/TableProTests/AWS/RedshiftIAMSignInTests.swift b/apple/TableProTests/AWS/RedshiftIAMSignInTests.swift index 7d42e1d7f2..26a8297152 100644 --- a/apple/TableProTests/AWS/RedshiftIAMSignInTests.swift +++ b/apple/TableProTests/AWS/RedshiftIAMSignInTests.swift @@ -24,7 +24,8 @@ struct RedshiftIAMSignInTests { user: String = "", host: String = clusterHost, preTunnelHost: String? = nil, - fields: [String: String] = ["awsAuth": "profile"] + fields: [String: String] = ["awsAuth": "profile"], + aliases: FakeCNAMEResolver = FakeCNAMEResolver() ) -> RedshiftIAMSignIn { RedshiftIAMSignIn( source: "profile", @@ -32,7 +33,8 @@ struct RedshiftIAMSignInTests { database: "dev", fields: fields, configuredHost: host, - preTunnelHost: preTunnelHost + preTunnelHost: preTunnelHost, + aliasResolver: aliases ) } @@ -85,35 +87,106 @@ struct RedshiftIAMSignInTests { } @Test("A tunneled connection signs in for the endpoint typed before the tunnel") - func tunneledConnectionUsesPreTunnelHost() throws { + func tunneledConnectionUsesPreTunnelHost() async throws { let tunneled = Self.signIn(host: "127.0.0.1", preTunnelHost: Self.clusterHost) - #expect(try tunneled.resolveEndpoint() == .cluster(identifier: "analytics", region: "us-east-1")) + #expect(try await tunneled.resolveEndpoint(within: nil) == .cluster(identifier: "analytics", region: "us-east-1")) } @Test("The Redshift Endpoint field names a forward's target, and a host that names nothing says what to enter") - func endpointOverride() throws { + func endpointOverride() async throws { let forwarded = Self.signIn( host: "127.0.0.1", fields: ["awsAuth": "sso", RedshiftIAMSignIn.endpointFieldId: "https://\(Self.serverlessHost):5439/dev"] ) - #expect(try forwarded.resolveEndpoint() == .serverless(workgroup: "reporting", region: "eu-west-1")) + #expect(try await forwarded.resolveEndpoint(within: nil) == .serverless(workgroup: "reporting", region: "eu-west-1")) - let elsewhere = Self.signIn( - host: "warehouse.corp.example", + let otherCluster = Self.signIn( + host: "other.abc123xyz.us-east-1.redshift.amazonaws.com", fields: [RedshiftIAMSignIn.endpointFieldId: Self.serverlessHost] ) - #expect(throws: RedshiftIAMError.endpointNotDialed(host: "warehouse.corp.example")) { - try elsewhere.resolveEndpoint() + await #expect(throws: RedshiftIAMError.endpointNotDialed(host: "other.abc123xyz.us-east-1.redshift.amazonaws.com")) { + try await otherCluster.resolveEndpoint(within: nil) } - #expect(throws: RedshiftIAMError.endpointUnresolved(host: "warehouse.corp.example")) { - try Self.signIn(host: "warehouse.corp.example").resolveEndpoint() + await #expect(throws: RedshiftIAMError.endpointNotDialed(host: "10.0.0.5")) { + try await Self.signIn(host: "10.0.0.5", fields: [RedshiftIAMSignIn.endpointFieldId: Self.clusterHost]) + .resolveEndpoint(within: nil) } - #expect(throws: RedshiftIAMError.endpointInvalid("not-an-endpoint")) { - try Self.signIn(host: "localhost", fields: [RedshiftIAMSignIn.endpointFieldId: "not-an-endpoint"]).resolveEndpoint() + + await #expect(throws: RedshiftIAMError.endpointUnresolved(host: "warehouse.corp.example")) { + try await Self.signIn(host: "warehouse.corp.example").resolveEndpoint(within: nil) + } + await #expect(throws: RedshiftIAMError.endpointInvalid("not-an-endpoint")) { + try await Self.signIn(host: "localhost", fields: [RedshiftIAMSignIn.endpointFieldId: "not-an-endpoint"]) + .resolveEndpoint(within: nil) } } + @Test("A custom domain Host is asked for by name, with the kind and region from the Redshift Endpoint field") + func customDomainWithEndpointField() async throws { + let serverless = Self.signIn( + host: "Warehouse.Corp.Example.", + fields: [RedshiftIAMSignIn.endpointFieldId: Self.serverlessHost] + ) + #expect(try await serverless.resolveEndpoint(within: nil) + == .serverlessDomain(name: "warehouse.corp.example", region: "eu-west-1")) + + let cluster = Self.signIn(host: "warehouse.corp.example", fields: [RedshiftIAMSignIn.endpointFieldId: Self.clusterHost]) + #expect(try await cluster.resolveEndpoint(within: nil) == .clusterDomain(name: "warehouse.corp.example", region: "us-east-1")) + } + + @Test("A custom domain Host finds its kind and region through its CNAME, and is still asked for by name") + func customDomainThroughCNAME() async throws { + let aliases = FakeCNAMEResolver([ + "warehouse.corp.example": "edge.corp.example", + "edge.corp.example": Self.clusterHost + ]) + let direct = Self.signIn(host: "warehouse.corp.example", aliases: aliases) + #expect(try await direct.resolveEndpoint(within: nil) == .clusterDomain(name: "warehouse.corp.example", region: "us-east-1")) + + let tunneled = Self.signIn( + host: "127.0.0.1", + preTunnelHost: "bi.corp.example", + aliases: FakeCNAMEResolver(["bi.corp.example": Self.serverlessHost]) + ) + #expect(try await tunneled.resolveEndpoint(within: nil) == .serverlessDomain(name: "bi.corp.example", region: "eu-west-1")) + } + + @Test("A DNS name that starts like 127.0.0.1 is not a forward, so the endpoint field cannot point it at a real cluster") + func loopbackLookalikeIsACustomDomain() async throws { + let fields = [RedshiftIAMSignIn.endpointFieldId: Self.clusterHost] + #expect(try await Self.signIn(host: "127.0.0.1.attacker.example", fields: fields).resolveEndpoint(within: nil) + == .clusterDomain(name: "127.0.0.1.attacker.example", region: "us-east-1")) + await #expect(throws: RedshiftIAMError.endpointNotDialed(host: "127.0.0.1.")) { + try await Self.signIn(host: "127.0.0.1.", fields: fields).resolveEndpoint(within: nil) + } + } + + @Test("A custom domain asks by CustomDomainName, never by the cluster or workgroup name") + func customDomainRequests() throws { + let cluster = try Self.signIn(user: "reporting").request( + for: .clusterDomain(name: "warehouse.corp.example", region: "us-east-1"), + region: "us-east-1", + credentials: Self.credentials + ) + #expect(Self.formParameters(cluster) == [ + "Action": "GetClusterCredentials", + "Version": "2012-12-01", + "CustomDomainName": "warehouse.corp.example", + "DbUser": "reporting", + "DbName": "dev" + ]) + + let serverless = try Self.signIn().request( + for: .serverlessDomain(name: "bi.corp.example", region: "eu-west-1"), + region: "eu-west-1", + credentials: Self.credentials, + now: Self.signedAt + ) + #expect(serverless.value(forHTTPHeaderField: "x-amz-target") == "RedshiftServerless.GetCredentials") + #expect(String(data: serverless.httpBody ?? Data(), encoding: .utf8) == #"{"customDomainName":"bi.corp.example","dbName":"dev"}"#) + } + @Test("A cluster with a database user asks GetClusterCredentials, without one GetClusterCredentialsWithIAM") func clusterRequests() throws { let endpoint = RedshiftIAMSignIn.Endpoint.cluster(identifier: "analytics", region: "us-east-1") diff --git a/apple/TableProTests/Core/Database/DatabaseManagerRDSEndpointTests.swift b/apple/TableProTests/Core/Database/DatabaseManagerRDSEndpointTests.swift new file mode 100644 index 0000000000..a10f369e57 --- /dev/null +++ b/apple/TableProTests/Core/Database/DatabaseManagerRDSEndpointTests.swift @@ -0,0 +1,264 @@ +// +// DatabaseManagerRDSEndpointTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import TableProPluginKit +import TableProSSHConfig +import TableProSSHTransport +import Testing + +@MainActor +private final class EndpointSignInSpy { + var answer: ConsentAnswer = .approved + private(set) var destinations: [String] = [] + + func ask(_ question: LocalCredentialQuestion) async -> ConsentAnswer { + destinations.append(question.destination) + return answer + } +} + +@MainActor +struct DatabaseManagerRDSEndpointTests { + private static let alias = "reader.db.example.com" + private static let replicaA = "replica-a.abc123.us-east-1.rds.amazonaws.com" + private static let replicaB = "replica-b.abc123.us-east-1.rds.amazonaws.com" + + private func connection( + type: DatabaseType = .mysql, + host: String = alias, + signIn: String? = "profile" + ) -> DatabaseConnection { + var connection = DatabaseConnection(name: "RDS", host: host, port: 3_306, type: type) + connection.additionalFields[DatabaseConnection.iamSignInFieldId] = signIn + return connection + } + + @Test("The CNAME host of an AWS IAM connection becomes the RDS endpoint it stands for") + func directConnectionDialsTheEndpoint() async throws { + let resolver = FakeCNAMEResolver([Self.alias: Self.replicaA]) + let manager = DatabaseManager(cnameResolver: resolver) + + let effective = try await manager.resolvingRDSEndpointAlias(of: connection(), trigger: .request) + + #expect(effective.host == Self.replicaA) + #expect(effective.port == 3_306) + #expect(resolver.queried == [Self.alias]) + } + + @Test("Every lookup follows the CNAME again, so a moved CNAME gives the new endpoint and the saved row keeps its host") + func movedCNAMEIsFollowed() async throws { + let resolver = FakeCNAMEResolver([Self.alias: Self.replicaA]) + let manager = DatabaseManager(cnameResolver: resolver) + let saved = connection() + + let first = try await manager.resolvingRDSEndpointAlias(of: saved, trigger: .request) + resolver.point(Self.alias, at: Self.replicaB) + let second = try await manager.resolvingRDSEndpointAlias(of: saved, trigger: .request) + + #expect(first.host == Self.replicaA) + #expect(second.host == Self.replicaB) + #expect(saved.host == Self.alias) + } + + @Test("A host with no RDS endpoint behind it is dialled and signed as typed") + func unresolvedHostIsUnchanged() async throws { + let manager = DatabaseManager(cnameResolver: FakeCNAMEResolver()) + + let effective = try await manager.resolvingRDSEndpointAlias(of: connection(), trigger: .request) + + #expect(effective.host == Self.alias) + } + + @Test("Nothing is looked up without an RDS token: password sign-in, Google IAM, Redshift, ElastiCache, Keyspaces") + func otherSignInsAreNotLookedUp() async throws { + let resolver = FakeCNAMEResolver([Self.alias: Self.replicaA]) + let manager = DatabaseManager(cnameResolver: resolver) + let untouched = [ + connection(signIn: nil), + connection(signIn: GoogleCloudSQLAuthFields.applicationDefault), + connection(type: .redshift), + connection(type: .redis), + connection(type: .cassandra) + ] + + for saved in untouched { + let effective = try await manager.resolvingRDSEndpointAlias(of: saved, trigger: .request) + #expect(effective.host == Self.alias) + } + #expect(resolver.queried.isEmpty) + } + + @Test("Behind a tunnel the far end resolves the name, so the forward keeps the name as typed") + func tunnelKeepsTheTypedName() async throws { + let resolver = FakeCNAMEResolver([Self.alias: Self.replicaA]) + let manager = DatabaseManager(cnameResolver: resolver) + var saved = connection(type: .postgresql) + saved.port = 5_432 + saved.sshTunnelMode = .inline(SSHConfiguration(enabled: true, host: "bastion.example.com")) + + let resolved = try await manager.resolvingRDSEndpointAlias(of: saved, trigger: .request) + + #expect(resolved.sshForwardDestination == .tcp(host: Self.alias, port: 5_432)) + #expect(resolver.queried.isEmpty) + } + + @Test("The lookup runs for exactly the connections the minter gives an RDS token") + func lookupMatchesTheMinter() { + for type in [DatabaseType.mysql, .mariadb, .postgresql, .redshift, .redis, .cassandra, .scylladb] { + for signIn in ["profile", "accessKey", "sso", GoogleCloudSQLAuthFields.applicationDefault, nil] { + let saved = connection(type: type, signIn: signIn) + let minter = ConnectionCredentialResolver.iamPasswordMinter(for: saved, fields: saved.additionalFields) + var mintsRDSToken = false + if case .aws(let signer) = minter, case .rds = signer.target { + mintsRDSToken = true + } + #expect(ConnectionCredentialResolver.signsRDSToken(for: saved) == mintsRDSToken, "\(type) \(signIn ?? "password")") + } + } + } + + @Test("An endpoint counts as moved only when the dialled host changes, whatever its case") + func movedEndpointIsTheDialledHost() { + let manager = DatabaseManager(cnameResolver: FakeCNAMEResolver()) + var dialled = connection() + dialled.host = Self.replicaA + var session = ConnectionSession(connection: connection()) + session.effectiveConnection = dialled + + var moved = dialled + moved.host = Self.replicaB + var sameInCapitals = dialled + sameInCapitals.host = Self.replicaA.uppercased() + + #expect(manager.movesEndpoint(of: session, to: moved)) + #expect(!manager.movesEndpoint(of: session, to: sameInCapitals)) + #expect(!manager.movesEndpoint(of: ConnectionSession(connection: connection()), to: connection())) + } + + @Test("A failed lookup is an error that names the host, not a token signed for the CNAME") + func failedLookupThrows() async { + let manager = DatabaseManager(cnameResolver: FakeCNAMEResolver(failing: [Self.alias])) + + await #expect(throws: AWSAuthError.endpointLookupFailed(host: Self.alias)) { + _ = try await manager.resolvingRDSEndpointAlias(of: connection(), trigger: .request) + } + } + + @Test("AWS IAM signs in only with a value the type's own Authentication picker offers") + func signInNeedsTheTypesOwnPicker() { + #expect(connection(type: .mysql, signIn: "profile").iamSignIn == .aws(source: "profile")) + #expect(connection(type: .postgresql, signIn: GoogleCloudSQLAuthFields.applicationDefault).iamSignIn + == .googleCloud(.applicationDefault)) + // ScyllaDB has no AWS picker, but its plugin signs in to Keyspaces with the field, so TLS is still raised. + #expect(connection(type: .scylladb, signIn: "profile").iamSignIn == .aws(source: "profile")) + for (type, signIn) in [ + (DatabaseType.redis, GoogleCloudSQLAuthFields.applicationDefault), + (DatabaseType.mariadb, GoogleCloudSQLAuthFields.serviceAccount), + (DatabaseType(rawValue: "Elasticsearch"), "profile"), + (DatabaseType.mssql, "profile"), + (DatabaseType.mysql, "someday"), + (DatabaseType.scylladb, GoogleCloudSQLAuthFields.applicationDefault) + ] { + let saved = connection(type: type, signIn: signIn) + #expect(saved.iamSignIn == nil, "\(type) \(signIn)") + #expect(ConnectionCredentialResolver.iamPasswordMinter(for: saved, fields: saved.additionalFields) == nil) + } + } + + @Test("A row from someone else asks again for each RDS endpoint its CNAME leads to, and never from the background") + func sharedRowApprovesEachEndpoint() async throws { + let suite = try EphemeralDefaults() + let spy = EndpointSignInSpy() + let consent = LocalCredentialConsent( + approvals: ConnectionApprovalStore(kind: .localCredentials, defaults: suite.defaults, savedConnections: { [] }), + ask: { [spy] question in await spy.ask(question) } + ) + let resolver = FakeCNAMEResolver([Self.alias: Self.replicaA]) + let manager = DatabaseManager(localCredentialConsent: consent, cnameResolver: resolver) + var shared = connection() + shared.origin = .teamLibrary + + let first = try await manager.resolvingRDSEndpointAlias(of: shared, trigger: .request) + let again = try await manager.resolvingRDSEndpointAlias(of: shared, trigger: .background) + resolver.point(Self.alias, at: Self.replicaB) + await #expect(throws: LocalCredentialConsentError.needsPerson) { + _ = try await manager.resolvingRDSEndpointAlias(of: shared, trigger: .background) + } + spy.answer = .declined + await #expect(throws: LocalCredentialConsentError.declined) { + _ = try await manager.resolvingRDSEndpointAlias(of: shared, trigger: .request) + } + + #expect(first.host == Self.replicaA) + #expect(again.host == Self.replicaA) + #expect(spy.destinations.count == 2) + #expect(spy.destinations.allSatisfy { !$0.contains(Self.alias) }) + #expect(spy.destinations.first?.contains(Self.replicaA) == true) + #expect(spy.destinations.last?.contains(Self.replicaB) == true) + } + + @Test("A row made on this Mac follows its own CNAME without asking") + func ownRowDoesNotAsk() async throws { + let suite = try EphemeralDefaults() + let spy = EndpointSignInSpy() + let consent = LocalCredentialConsent( + approvals: ConnectionApprovalStore(kind: .localCredentials, defaults: suite.defaults, savedConnections: { [] }), + ask: { [spy] question in await spy.ask(question) } + ) + let manager = DatabaseManager( + localCredentialConsent: consent, + cnameResolver: FakeCNAMEResolver([Self.alias: Self.replicaA]) + ) + + let effective = try await manager.resolvingRDSEndpointAlias(of: connection(), trigger: .background) + + #expect(effective.host == Self.replicaA) + #expect(spy.destinations.isEmpty) + } + + private func approvingManager(failingLookup: Bool) throws -> (DatabaseManager, EphemeralDefaults) { + let suite = try EphemeralDefaults() + let consent = LocalCredentialConsent( + approvals: ConnectionApprovalStore(kind: .localCredentials, defaults: suite.defaults, savedConnections: { [] }), + ask: { _ in .approved } + ) + let resolver = FakeCNAMEResolver(failing: failingLookup ? [Self.alias] : []) + return (DatabaseManager(localCredentialConsent: consent, cnameResolver: resolver), suite) + } + + @Test("Connect, Test Connection and both reconnects look the CNAME up before they dial") + func everyDialPathLooksUp() async throws { + let (manager, suite) = try approvingManager(failingLookup: true) + defer { withExtendedLifetime(suite) {} } + let lookupFailed = AWSAuthError.endpointLookupFailed(host: Self.alias) + let saved = connection() + + await #expect(throws: lookupFailed) { + try await manager.connectToSession(saved, trigger: .request) + } + manager.removeSession(for: saved.id) + + await #expect(throws: lookupFailed) { + _ = try await manager.testConnection(saved) + } + + var session = ConnectionSession(connection: saved, driver: MockDatabaseDriver()) + session.status = .connected + manager.injectSession(session, for: saved.id) + defer { manager.removeSession(for: saved.id) } + let generation = manager.connectionAttempts.begin(for: saved.id) + await #expect(throws: lookupFailed) { + _ = try await manager.reconnectDriver(for: session, allowsCredentialPrompt: false, generation: generation) + } + manager.connectionAttempts.finish(generation, for: saved.id) + + await #expect(throws: lookupFailed) { + try await manager.reconnectSession(saved.id) + } + } +} + diff --git a/apple/TableProTests/Core/Database/EnvVarReferenceConsentTests.swift b/apple/TableProTests/Core/Database/EnvVarReferenceConsentTests.swift index c0c2db09cf..9f5c8b911b 100644 --- a/apple/TableProTests/Core/Database/EnvVarReferenceConsentTests.swift +++ b/apple/TableProTests/Core/Database/EnvVarReferenceConsentTests.swift @@ -145,6 +145,21 @@ extension GlobalStateSuites { }) } + @Test("A manual reconnect resolves the references as the connect does, so a declined prompt sends nothing") + func manualReconnectResolvesReferences() async throws { + let manager = DatabaseManager(envVarReferenceConsent: makeConsent()) + let connection = importedFromAttacker() + manager.setSession(ConnectionSession(connection: connection), for: connection.id) + defer { manager.removeSession(for: connection.id) } + + await withReportVariables { + await #expect(throws: EnvVarReferenceConsentError.declined) { + try await manager.reconnectSession(connection.id) + } + } + #expect(spy.questions.map(\.connectionName) == ["Staging"]) + } + @Test("MCP, the assistant and AppleScript get a declined prompt as a refusal") func bridgeReportsDeclinedAsForbidden() async throws { let connection = importedFromAttacker() diff --git a/apple/TableProTests/Helpers/FakeCNAMEResolver.swift b/apple/TableProTests/Helpers/FakeCNAMEResolver.swift new file mode 100644 index 0000000000..7dd8d23819 --- /dev/null +++ b/apple/TableProTests/Helpers/FakeCNAMEResolver.swift @@ -0,0 +1,38 @@ +// +// FakeCNAMEResolver.swift +// TableProTests +// + +import Foundation +import TableProPluginKit + +// Answers from a fixed table and records each name asked, so a test never reaches real DNS. +final class FakeCNAMEResolver: CNAMEResolving, @unchecked Sendable { + private let lock = NSLock() + private var records: [String: String] + private let failing: Set + private var asked: [String] = [] + + init(_ records: [String: String] = [:], failing: Set = []) { + self.records = records + self.failing = failing + } + + var queried: [String] { + lock.withLock { asked } + } + + func point(_ name: String, at target: String?) { + lock.withLock { records[name] = target } + } + + func cnameTarget(of name: String, within timeout: Duration?) async -> CNAMELookup { + lock.withLock { + asked.append(name) + if failing.contains(name) { + return .failed + } + return records[name].map(CNAMELookup.found) ?? .notFound + } + } +} diff --git a/apple/scripts/probes/check-rds-cname-chain.sh b/apple/scripts/probes/check-rds-cname-chain.sh new file mode 100755 index 0000000000..1c6bda8e5e --- /dev/null +++ b/apple/scripts/probes/check-rds-cname-chain.sh @@ -0,0 +1,91 @@ +#!/usr/bin/env bash +# +# Check how AWSEndpointHost finds the RDS endpoint behind a custom DNS name, against the system resolver: the +# hop-by-hop CNAME walk TablePro signs and dials, beside what getaddrinfo's AI_CANONNAME reports, which is the end +# of the chain (an ec2, vpce or replica name) and never the name a token is signed for. +# +# Usage: +# apple/scripts/probes/check-rds-cname-chain.sh [name...] +# +# With no names it uses public CNAMEs that point at RDS instances. Needs network access. +# Exits non-zero when a default name no longer leads to an RDS endpoint, 3 when a prerequisite is missing. + +set -uo pipefail + +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" +AWS="$ROOT/Plugins/TableProPluginKit/AWS" +WORK="$(mktemp -d)" +trap 'rm -rf "$WORK"' EXIT + +if [ -z "${DEVELOPER_DIR:-}" ]; then + DEVELOPER_DIR="$(xcode-select -p)" + case "$DEVELOPER_DIR" in + *CommandLineTools*) DEVELOPER_DIR=/Applications/Xcode.app/Contents/Developer ;; + esac +fi +export DEVELOPER_DIR +command -v xcrun > /dev/null || { echo "xcrun not found" >&2; exit 3; } + +cat > "$WORK/main.swift" << 'SWIFT' +import Darwin +import Foundation + +struct Hop: CNAMEResolving { + let inner = SystemCNAMEResolver() + + func cnameTarget(of name: String, within timeout: Duration?) async -> CNAMELookup { + let started = ContinuousClock.now + let lookup = await inner.cnameTarget(of: name, within: timeout) + print(" CNAME \(name) -> \(lookup) (\(started.duration(to: .now)))") + return lookup + } +} + +func canonicalName(_ name: String) -> String { + var hints = addrinfo() + hints.ai_flags = AI_CANONNAME + hints.ai_socktype = SOCK_STREAM + var result: UnsafeMutablePointer? + guard getaddrinfo(name, nil, &hints, &result) == 0, let result else { return "lookup failed" } + defer { freeaddrinfo(result) } + return result.pointee.ai_canonname.map { String(cString: $0) } ?? "none" +} + +@main +enum Probe { + static func main() async { + let checksDefaults = CommandLine.arguments.contains("--defaults") + var failures = 0 + for name in CommandLine.arguments.dropFirst() where name != "--defaults" { + print("== \(name)") + print(" AI_CANONNAME: \(canonicalName(name))") + switch await AWSEndpointHost.rdsEndpoint(behind: name, resolver: Hop(), within: .seconds(10)) { + case .found(let endpoint): + print(" signs and dials: \(endpoint), region \(AWSEndpointHost.rdsRegion(forHost: endpoint) ?? "none")") + case .notFound: + print(" signs and dials: the name as typed") + if checksDefaults { failures += 1 } + case .failed: + print(" the lookup failed") + failures += 1 + } + } + exit(failures == 0 ? 0 : 1) + } +} +SWIFT + +xcrun swiftc -swift-version 6 -parse-as-library -module-name CNAMEProbe \ + "$AWS/AWSEndpointHost.swift" "$AWS/CNAMEResolver.swift" "$AWS/AWSPartition.swift" "$AWS/AWSAuthError.swift" \ + "$WORK/main.swift" -o "$WORK/probe" 2> "$WORK/build.log" || { + cat "$WORK/build.log" >&2 + exit 3 +} + +if [ "$#" -eq 0 ]; then + "$WORK/probe" --defaults \ + users-mysql-1.machines.pythonanywhere-services.com \ + users-mysql-3.machines.pythonanywhere-services.com +else + "$WORK/probe" "$@" +fi From 6637ed7f1e0a2d2042cb43a00338fefb3ce7a8fa Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Wed, 11 Nov 2026 12:27:22 +0700 Subject: [PATCH 2/2] docs(changelog): note the RDS CNAME sign-in and the IAM fixes --- apple/changelog.d/4183.added.1.md | 1 + apple/changelog.d/4183.added.md | 1 + apple/changelog.d/4183.fixed.1.md | 1 + apple/changelog.d/4183.fixed.md | 1 + apple/changelog.d/4183.security.1.md | 1 + apple/changelog.d/4183.security.md | 1 + 6 files changed, 6 insertions(+) create mode 100644 apple/changelog.d/4183.added.1.md create mode 100644 apple/changelog.d/4183.added.md create mode 100644 apple/changelog.d/4183.fixed.1.md create mode 100644 apple/changelog.d/4183.fixed.md create mode 100644 apple/changelog.d/4183.security.1.md create mode 100644 apple/changelog.d/4183.security.md diff --git a/apple/changelog.d/4183.added.1.md b/apple/changelog.d/4183.added.1.md new file mode 100644 index 0000000000..57b421accc --- /dev/null +++ b/apple/changelog.d/4183.added.1.md @@ -0,0 +1 @@ +Redshift AWS IAM sign-in accepts a custom domain name as Host. diff --git a/apple/changelog.d/4183.added.md b/apple/changelog.d/4183.added.md new file mode 100644 index 0000000000..e7f62dfa45 --- /dev/null +++ b/apple/changelog.d/4183.added.md @@ -0,0 +1 @@ +AWS IAM connections to Amazon RDS accept a custom CNAME as Host and follow it to the RDS endpoint on every connect. diff --git a/apple/changelog.d/4183.fixed.1.md b/apple/changelog.d/4183.fixed.1.md new file mode 100644 index 0000000000..4096669b2a --- /dev/null +++ b/apple/changelog.d/4183.fixed.1.md @@ -0,0 +1 @@ +Reconnect used ${VAR} references in a connection as typed instead of resolving them. diff --git a/apple/changelog.d/4183.fixed.md b/apple/changelog.d/4183.fixed.md new file mode 100644 index 0000000000..c56309813e --- /dev/null +++ b/apple/changelog.d/4183.fixed.md @@ -0,0 +1 @@ +MySQL and MariaDB connections on AWS IAM failed to reconnect after a dropped connection once the first token expired. diff --git a/apple/changelog.d/4183.security.1.md b/apple/changelog.d/4183.security.1.md new file mode 100644 index 0000000000..ae49313814 --- /dev/null +++ b/apple/changelog.d/4183.security.1.md @@ -0,0 +1 @@ +A shared Elasticsearch, Kafka or MongoDB connection could mint an AWS IAM token on this Mac and send it to its servers. diff --git a/apple/changelog.d/4183.security.md b/apple/changelog.d/4183.security.md new file mode 100644 index 0000000000..ffb8a8437f --- /dev/null +++ b/apple/changelog.d/4183.security.md @@ -0,0 +1 @@ +A shared connection could send an AWS IAM token to its author's server through a host like 127.0.0.1.example.com.