Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,23 @@ 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:
throw CancellationError()
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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?,
Expand Down Expand Up @@ -49,4 +52,8 @@ public extension DriverFactory {
func dialsSSHTunnel(for connection: DatabaseConnection) -> Bool {
connection.sshEnabled
}

func resolvingEndpointAlias(for connection: DatabaseConnection) async throws -> DatabaseConnection {
connection
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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?,
Expand Down Expand Up @@ -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"],
Expand Down Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
import Foundation
@testable import TableProPluginKit
import Testing

private struct FakeCNAMEResolver: CNAMEResolving {
let records: [String: String]
let failing: Set<String>
let queried: QueriedNames

init(_ records: [String: String], failing: Set<String> = [], 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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading
Loading