Skip to content

Commit 96ed89f

Browse files
authored
Correctly place the SSL channel handler in front of the PostgresChannelHandler (#527)
1 parent f2a6394 commit 96ed89f

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

Diff for: Sources/PostgresNIO/Connection/PostgresConnection.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,18 @@ public final class PostgresConnection: @unchecked Sendable {
6060
func start(configuration: InternalConfiguration) -> EventLoopFuture<Void> {
6161
// 1. configure handlers
6262

63-
let configureSSLCallback: ((Channel) throws -> ())?
63+
let configureSSLCallback: ((Channel, PostgresChannelHandler) throws -> ())?
6464

6565
switch configuration.tls.base {
6666
case .prefer(let context), .require(let context):
67-
configureSSLCallback = { channel in
67+
configureSSLCallback = { channel, postgresChannelHandler in
6868
channel.eventLoop.assertInEventLoop()
6969

7070
let sslHandler = try NIOSSLClientHandler(
7171
context: context,
7272
serverHostname: configuration.serverNameForTLS
7373
)
74-
try channel.pipeline.syncOperations.addHandler(sslHandler, position: .first)
74+
try channel.pipeline.syncOperations.addHandler(sslHandler, position: .before(postgresChannelHandler))
7575
}
7676
case .disable:
7777
configureSSLCallback = nil

Diff for: Sources/PostgresNIO/New/PostgresChannelHandler.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ final class PostgresChannelHandler: ChannelDuplexHandler {
2020
private var decoder: NIOSingleStepByteToMessageProcessor<PostgresBackendMessageDecoder>
2121
private var encoder: PostgresFrontendMessageEncoder!
2222
private let configuration: PostgresConnection.InternalConfiguration
23-
private let configureSSLCallback: ((Channel) throws -> Void)?
23+
private let configureSSLCallback: ((Channel, PostgresChannelHandler) throws -> Void)?
2424

2525
private var listenState = ListenStateMachine()
2626
private var preparedStatementState = PreparedStatementStateMachine()
@@ -29,7 +29,7 @@ final class PostgresChannelHandler: ChannelDuplexHandler {
2929
configuration: PostgresConnection.InternalConfiguration,
3030
eventLoop: EventLoop,
3131
logger: Logger,
32-
configureSSLCallback: ((Channel) throws -> Void)?
32+
configureSSLCallback: ((Channel, PostgresChannelHandler) throws -> Void)?
3333
) {
3434
self.state = ConnectionStateMachine(requireBackendKeyData: configuration.options.requireBackendKeyData)
3535
self.eventLoop = eventLoop
@@ -46,7 +46,7 @@ final class PostgresChannelHandler: ChannelDuplexHandler {
4646
eventLoop: EventLoop,
4747
state: ConnectionStateMachine = .init(.initialized),
4848
logger: Logger = .psqlNoOpLogger,
49-
configureSSLCallback: ((Channel) throws -> Void)?
49+
configureSSLCallback: ((Channel, PostgresChannelHandler) throws -> Void)?
5050
) {
5151
self.state = state
5252
self.eventLoop = eventLoop
@@ -439,7 +439,7 @@ final class PostgresChannelHandler: ChannelDuplexHandler {
439439
// This method must only be called, if we signalized the StateMachine before that we are
440440
// able to setup a SSL connection.
441441
do {
442-
try self.configureSSLCallback!(context.channel)
442+
try self.configureSSLCallback!(context.channel, self)
443443
let action = self.state.sslHandlerAdded()
444444
self.run(action, with: context)
445445
} catch {

Diff for: Tests/PostgresNIOTests/New/PostgresChannelHandlerTests.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class PostgresChannelHandlerTests: XCTestCase {
4848
var config = self.testConnectionConfiguration()
4949
XCTAssertNoThrow(config.tls = .require(try NIOSSLContext(configuration: .makeClientConfiguration())))
5050
var addSSLCallbackIsHit = false
51-
let handler = PostgresChannelHandler(configuration: config, eventLoop: self.eventLoop) { channel in
51+
let handler = PostgresChannelHandler(configuration: config, eventLoop: self.eventLoop) { channel, _ in
5252
addSSLCallbackIsHit = true
5353
}
5454
let embedded = EmbeddedChannel(handlers: [
@@ -84,7 +84,7 @@ class PostgresChannelHandlerTests: XCTestCase {
8484
var config = self.testConnectionConfiguration()
8585
XCTAssertNoThrow(config.tls = .require(try NIOSSLContext(configuration: .makeClientConfiguration())))
8686
var addSSLCallbackIsHit = false
87-
let handler = PostgresChannelHandler(configuration: config, eventLoop: self.eventLoop) { channel in
87+
let handler = PostgresChannelHandler(configuration: config, eventLoop: self.eventLoop) { channel, _ in
8888
addSSLCallbackIsHit = true
8989
}
9090
let eventHandler = TestEventHandler()
@@ -114,7 +114,7 @@ class PostgresChannelHandlerTests: XCTestCase {
114114
func testSSLUnsupportedClosesConnection() throws {
115115
let config = self.testConnectionConfiguration(tls: .require(try NIOSSLContext(configuration: .makeClientConfiguration())))
116116

117-
let handler = PostgresChannelHandler(configuration: config, eventLoop: self.eventLoop) { channel in
117+
let handler = PostgresChannelHandler(configuration: config, eventLoop: self.eventLoop) { channel, _ in
118118
XCTFail("This callback should never be exectuded")
119119
throw PSQLError.sslUnsupported
120120
}

0 commit comments

Comments
 (0)