forked from vapor/postgres-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgreSQLConnection+TransportConfig.swift
81 lines (69 loc) · 3.49 KB
/
PostgreSQLConnection+TransportConfig.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import NIOOpenSSL
extension PostgreSQLConnection {
/// Transport-layer security configuration for the PostgreSQL connection.
public struct TransportConfig {
/// Does not attempt to enable TLS (this is the default).
public static var cleartext: TransportConfig {
return .init(method: .cleartext)
}
/// Enables TLS requiring a minimum version of TLS v1.1 on the server, but disables certificate verification.
/// This is what you would commonly use for paid Heroku PostgreSQL plans.
public static var unverifiedTLS: TransportConfig {
return .init(method: .tls(.forClient(certificateVerification: .none)))
}
/// Enables TLS requiring a minimum version of TLS v1.1 on the server.
public static var standardTLS: TransportConfig {
return .init(method: .tls(.forClient()))
}
/// Enables TLS requiring a minimum version of TLS v1.2 on the server.
public static var modernTLS: TransportConfig {
return .init(method: .tls(.forClient(minimumTLSVersion: .tlsv12)))
}
/// Enables TLS requiring a minimum version of TLS v1.3 on the server.
/// TLS v1.3 specification is still a draft and unlikely to be supported by most servers.
/// See https://tools.ietf.org/html/draft-ietf-tls-tls13-28 for more info.
public static var edgeTLS: TransportConfig {
return .init(method: .tls(.forClient(minimumTLSVersion: .tlsv13)))
}
/// Enables TLS using the given `TLSConfiguration`.
/// - parameter tlsConfiguration: See `TLSConfiguration` for more info.
public static func customTLS(_ tlsConfiguration: TLSConfiguration)-> TransportConfig {
return .init(method: .tls(tlsConfiguration))
}
/// Returns `true` if this configuration uses TLS.
public var isTLS: Bool {
switch storage {
case .cleartext: return false
case .tls: return true
}
}
/// Internal storage type.
internal enum Storage {
case cleartext
case tls(TLSConfiguration)
}
/// Internal storage.
internal let storage: Storage
/// Internal init.
internal init(method: Storage) {
self.storage = method
}
}
/// Ask the server if it supports SSL and adds a new OpenSSLClientHandler to pipeline if it does
/// This will throw an error if the server does not support SSL
internal func addSSLClientHandler(using tlsConfiguration: TLSConfiguration) -> Future<Void> {
return queue.enqueue([.sslSupportRequest(.init())]) { message in
guard case .sslSupportResponse(let response) = message else {
throw PostgreSQLError(identifier: "SSL support check", reason: "Unsupported message encountered during SSL support check: \(message).")
}
guard response == .supported else {
throw PostgreSQLError(identifier: "SSL support check", reason: "tlsConfiguration given in PostgresSQLConfiguration, but SSL connection not supported by PostgreSQL server.")
}
return true
}.flatMap {
let sslContext = try SSLContext(configuration: tlsConfiguration)
let handler = try OpenSSLClientHandler(context: sslContext)
return self.channel.pipeline.add(handler: handler, first: true)
}
}
}