Skip to content

Commit 0d58f22

Browse files
author
Andrew Theis
committed
Updates based on PR feedback
1 parent b36f057 commit 0d58f22

7 files changed

+28
-24
lines changed

Sources/PostgreSQL/Connection/PostgreSQLConnection+TCP.swift

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ extension PostgreSQLConnection {
77
public static func connect(
88
hostname: String = "localhost",
99
port: Int = 5432,
10-
transportConfig: PostgreSQLTransportConfig = .cleartext,
10+
transport: PostgreSQLTransportConfig = .cleartext,
1111
on worker: Worker,
1212
onError: @escaping (Error) -> ()
1313
) throws -> Future<PostgreSQLConnection> {
@@ -21,12 +21,13 @@ extension PostgreSQLConnection {
2121
}
2222
}
2323

24-
return bootstrap.connect(host: hostname, port: port).flatMap(to: PostgreSQLConnection.self) { channel in
24+
return bootstrap.connect(host: hostname, port: port).flatMap { channel in
2525
let connection = PostgreSQLConnection(queue: handler, channel: channel)
26-
if case .tls(let tlsConfiguration) = transportConfig.method {
26+
if case .tls(let tlsConfiguration) = transport.method {
2727
return connection.addSSLClientHandler(using: tlsConfiguration).transform(to: connection)
28+
} else {
29+
return worker.eventLoop.newSucceededFuture(result: connection)
2830
}
29-
return worker.eventLoop.newSucceededFuture(result: connection)
3031
}
3132
}
3233
}

Sources/PostgreSQL/Connection/PostgreSQLConnection.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public final class PostgreSQLConnection: DatabaseConnection, BasicWorker {
137137
throw PostgreSQLError(identifier: "SSL support check", reason: "Unsupported message encountered during SSL support check: \(message).", source: .capture())
138138
}
139139
guard response == .supported else {
140-
throw PostgreSQLError(identifier: "SSL support check", reason: "tlsConfiguration given in PostgresSQLConfiguration, but SSL connection not supported by PostgreSQL server", source: .capture())
140+
throw PostgreSQLError(identifier: "SSL support check", reason: "tlsConfiguration given in PostgresSQLConfiguration, but SSL connection not supported by PostgreSQL server.", source: .capture())
141141
}
142142
return true
143143
}.flatMap {

Sources/PostgreSQL/Database/PostgreSQLDatabase.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public final class PostgreSQLDatabase: Database, LogSupporting {
1919
public func newConnection(on worker: Worker) -> Future<PostgreSQLConnection> {
2020
let config = self.config
2121
return Future.flatMap(on: worker) {
22-
return try PostgreSQLConnection.connect(hostname: config.hostname, port: config.port, transportConfig: config.transportConfig, on: worker) { error in
22+
return try PostgreSQLConnection.connect(hostname: config.hostname, port: config.port, transport: config.transportConfig, on: worker) { error in
2323
print("[PostgreSQL] \(error)")
2424
}.flatMap(to: PostgreSQLConnection.self) { client in
2525
return client.authenticate(

Sources/PostgreSQL/Database/PostgreSQLTransportConfig.swift

+10-9
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,36 @@ import NIOOpenSSL
33

44

55
public struct PostgreSQLTransportConfig {
6-
/// Does not attempt to enable TLS (this is the default)
6+
/// Does not attempt to enable TLS (this is the default).
77
public static var cleartext: PostgreSQLTransportConfig {
88
return .init(method: .cleartext)
99
}
1010

11-
/// Enables TLS requiring a minimum version of TLS v1.1 on the server, but disables certificate verification
12-
/// This is what you would commonly use for paid Heroku PostgreSQL plans
11+
/// Enables TLS requiring a minimum version of TLS v1.1 on the server, but disables certificate verification.
12+
/// This is what you would commonly use for paid Heroku PostgreSQL plans.
1313
public static var unverifiedTLS: PostgreSQLTransportConfig {
1414
return .init(method: .tls(.forClient(certificateVerification: .none)))
1515
}
1616

17-
/// Enables TLS requiring a minimum version of TLS v1.1 on the server
17+
/// Enables TLS requiring a minimum version of TLS v1.1 on the server.
1818
public static var standardTLS: PostgreSQLTransportConfig {
1919
return .init(method: .tls(.forClient()))
2020
}
2121

22-
/// Enables TLS requiring a minimum version of TLS v1.2 on the server
22+
/// Enables TLS requiring a minimum version of TLS v1.2 on the server.
2323
public static var modernTLS: PostgreSQLTransportConfig {
2424
return .init(method: .tls(.forClient(minimumTLSVersion: .tlsv12)))
2525
}
2626

27-
/// Enables TLS requiring a minimum version of TLS v1.3 on the server
28-
/// TLS v1.3 specification is still a draft and unlikely to be supported by most servers
29-
/// See https://tools.ietf.org/html/draft-ietf-tls-tls13-28 for more info
27+
/// Enables TLS requiring a minimum version of TLS v1.3 on the server.
28+
/// TLS v1.3 specification is still a draft and unlikely to be supported by most servers.
29+
/// See https://tools.ietf.org/html/draft-ietf-tls-tls13-28 for more info.
3030
public static var edgeTLS: PostgreSQLTransportConfig {
3131
return .init(method: .tls(.forClient(minimumTLSVersion: .tlsv13)))
3232
}
3333

34-
/// Enables TLS and allows you to use a set `TLSConfiguration`
34+
/// Enables TLS using the given `TLSConfiguration`.
35+
/// - parameter tlsConfiguration: See `TLSConfiguration` for more info.
3536
public static func customTLS(_ tlsConfiguration: TLSConfiguration)-> PostgreSQLTransportConfig {
3637
return .init(method: .tls(tlsConfiguration))
3738
}

Sources/PostgreSQL/Message+Parse/PostgreSQLMessageDecoder.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ final class PostgreSQLMessageDecoder: ByteToMessageDecoder {
3535
ctx.fireChannelRead(wrapInboundOut(message))
3636
VERBOSE(" [message=\(message)]")
3737
return .continue
38+
} else {
39+
VERBOSE(" [needMoreData: messageSize=nil]")
40+
return .needMoreData
3841
}
39-
40-
VERBOSE(" [needMoreData: messageSize=nil]")
41-
return .needMoreData
4242
}
4343

4444
/// ensure message is large enough or reject

Sources/PostgreSQL/Message/PostgreSQLSSLSupportResponse.swift

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import Core
33
/// Response given after sending a PostgreSQLSSLSupportRequest
44
/// For more info, see https://www.postgresql.org/docs/10/static/protocol-flow.html#id-1.10.5.7.11
55
enum PostgreSQLSSLSupportResponse: UInt8, Decodable {
6+
/// The server supports SSL (char S).
67
case supported = 0x53
8+
/// The server does not support SSL (char N).
79
case notSupported = 0x4E
810
}

Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class PostgreSQLConnectionTests: XCTestCase {
1414
}
1515

1616
func testUnverifiedSSLConnection() throws {
17-
let client = try PostgreSQLConnection.makeTest(transportConfig: .unverifiedTLS)
17+
let client = try PostgreSQLConnection.makeTest(transport: .unverifiedTLS)
1818
let results = try client.simpleQuery("SELECT version();").wait()
1919
try XCTAssert(results[0].firstValue(forColumn: "version")?.decode(String.self).contains("10.") == true)
2020
}
@@ -468,17 +468,17 @@ extension PostgreSQLConnection {
468468
}
469469

470470
/// Creates a test event loop and psql client over ssl
471-
static func makeTest(transportConfig: PostgreSQLTransportConfig) throws -> PostgreSQLConnection {
471+
static func makeTest(transport: PostgreSQLTransportConfig) throws -> PostgreSQLConnection {
472472
#if Xcode
473-
return try _makeTest(hostname: self.dockerMachineHostname, port: 5433, transportConfig: transportConfig)
473+
return try _makeTest(hostname: self.dockerMachineHostname, port: 5433, transport: transport)
474474
#else
475-
return try _makeTest(hostname: "localhost-ssl", password: "vapor_password", transportConfig: transportConfig)
475+
return try _makeTest(hostname: "localhost-ssl", password: "vapor_password", transport: transport)
476476
#endif
477477
}
478478

479-
private static func _makeTest(hostname: String, password: String? = nil, port: Int = 5432, transportConfig: PostgreSQLTransportConfig = .cleartext) throws -> PostgreSQLConnection {
479+
private static func _makeTest(hostname: String, password: String? = nil, port: Int = 5432, transport: PostgreSQLTransportConfig = .cleartext) throws -> PostgreSQLConnection {
480480
let group = MultiThreadedEventLoopGroup(numThreads: 1)
481-
let client = try PostgreSQLConnection.connect(hostname: hostname, port: port, transportConfig: transportConfig, on: group) { error in
481+
let client = try PostgreSQLConnection.connect(hostname: hostname, port: port, transport: transport, on: group) { error in
482482
XCTFail("\(error)")
483483
}.wait()
484484
_ = try client.authenticate(username: "vapor_username", database: "vapor_database", password: password).wait()

0 commit comments

Comments
 (0)