Skip to content

Commit 792778c

Browse files
author
Andrew Theis
committed
Add unit test for testing SSL and new docker image for spinning up postgres with self-signed certificate
1 parent 8ac3869 commit 792778c

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

Sources/PostgreSQL/Connection/PostgreSQLConnection+TCP.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ extension PostgreSQLConnection {
2424
return bootstrap.connect(host: hostname, port: port).flatMap(to: PostgreSQLConnection.self) { channel in
2525
let connection = PostgreSQLConnection(queue: handler, channel: channel)
2626
if let tlsConfiguration = tlsConfiguration {
27-
return connection.attemptSSLConnection(using: tlsConfiguration).transform(to: connection)
27+
return connection.addSSLClientHandler(using: tlsConfiguration).transform(to: connection)
2828
}
2929
return Future.map(on: worker) { connection }
3030
}

Sources/PostgreSQL/Connection/PostgreSQLConnection.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,9 @@ public final class PostgreSQLConnection: DatabaseConnection, BasicWorker {
129129
return new
130130
}
131131

132-
/// Trys opening a SLL connection
133-
internal func attemptSSLConnection(using tlsConfiguration: TLSConfiguration) -> Future<Void> {
132+
/// Ask the server if it supports SSL and adds a new OpenSSLClientHandler to pipeline if it does
133+
/// This will throw an error if the server does not support SSL
134+
internal func addSSLClientHandler(using tlsConfiguration: TLSConfiguration) -> Future<Void> {
134135
return queue.enqueue([.sslSupportRequest(PostgreSQLSSLSupportRequest())]) { message in
135136
guard case .sslSupportResponse(let response) = message else {
136137
throw PostgreSQLError(identifier: "SSL support check", reason: "Unsupported message encountered during SSL support check: \(message).", source: .capture())

Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift

+13-3
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@ class PostgreSQLConnectionTests: XCTestCase {
441441
}
442442

443443
static var allTests = [
444+
("testSSLConnection", testSSLConnection),
444445
("testVersion", testVersion),
445446
("testSelectTypes", testSelectTypes),
446447
("testParse", testParse),
@@ -466,9 +467,18 @@ extension PostgreSQLConnection {
466467
hostname = "localhost"
467468
#endif
468469
let group = MultiThreadedEventLoopGroup(numThreads: 1)
469-
let client = try PostgreSQLConnection.connect(hostname: hostname, tlsConfiguration: tlsConfiguration, on: group) { error in
470-
XCTFail("\(error)")
471-
}.wait()
470+
var client: PostgreSQLConnection
471+
472+
if let tlsConfiguration = tlsConfiguration {
473+
client = try PostgreSQLConnection.connect(hostname: hostname, port: 5433, tlsConfiguration: tlsConfiguration, on: group) { error in
474+
XCTFail("\(error)")
475+
}.wait()
476+
} else {
477+
client = try PostgreSQLConnection.connect(hostname: hostname, on: group) { error in
478+
XCTFail("\(error)")
479+
}.wait()
480+
}
481+
472482
_ = try client.authenticate(username: "vapor_username", database: "vapor_database", password: nil).wait()
473483
return client
474484
}

contribute_bootstrap.sh

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,23 @@ docker-machine start default
44
echo "💧 exporting docker machine environment..."
55
eval $(docker-machine env default)
66

7-
echo "💧 cleaning previous vapor-psql dev db..."
7+
echo "💧 cleaning previous psql containers..."
8+
rm -rf postgres
89
docker stop vapor-psql
10+
docker stop vapor-psql-ssl
911
docker rm vapor-psql
12+
docker rm vapor-psql-ssl
1013

1114
echo "💧 creating vapor-psql dev db..."
1215
docker run --name vapor-psql -e POSTGRES_USER=vapor_username -e POSTGRES_DB=vapor_database -p 5432:5432 -d postgres:latest
1316

17+
echo "💧 building postgres-ssl docker image..."
18+
docker build -t postgres-ssl https://github.com/scenecheck/postgres-ssl.git
19+
# docker build -t postgres-ssl ~/Repositories/Docker-Postgres-SSL #
20+
21+
echo "💧 creating vapor-psql-ssl dev db..."
22+
docker run -d --name vapor-psql-ssl -e POSTGRES_USER=vapor_username -e POSTGRES_DB=vapor_database -p 5433:5432 postgres-ssl
23+
1424
echo "💧 generating xcode proj..."
1525
swift package -Xswiftc -DTEST_DOCKER_HOSTNAME generate-xcodeproj
1626

0 commit comments

Comments
 (0)