Skip to content

Commit fe560e4

Browse files
authored
Merge pull request #1 from SwifQL/MihaelIsaev-PG_SSL
Implement PG_SSL environment key
2 parents 4958f0b + 0fe05ae commit fe560e4

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

Sources/PostgresBridge/Extensions/DatabaseHost+Initialization.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ extension DatabaseHost {
1515
let port = Int(ProcessInfo.processInfo.environment["PG_PORT"] ?? "5432")
1616
let user = ProcessInfo.processInfo.environment["PG_USER"] ?? "postgres"
1717
let pwd = ProcessInfo.processInfo.environment["PG_PWD"]
18-
return .init(hostname: host, port: port ?? 5432, username: user, password: pwd, tlsConfiguration: nil)
18+
var sslRequired = false
19+
if let tls = ProcessInfo.processInfo.environment["PG_SSL"] {
20+
sslRequired = ["1", "true", "require"].contains(tls.lowercased())
21+
}
22+
return .init(hostname: host, port: port ?? 5432, username: user, password: pwd, tlsConfiguration: sslRequired ? TLSConfiguration.forClient() : nil)
1923
}
2024

2125
public init?(url: URL) {
@@ -49,11 +53,12 @@ extension DatabaseHost {
4953
unixDomainSocketPath: String,
5054
username: String,
5155
password: String,
52-
database: String
56+
database: String,
57+
tlsConfiguration: TLSConfiguration? = nil
5358
) {
5459
let address = {
5560
try SocketAddress.init(unixDomainSocketPath: unixDomainSocketPath)
5661
}
57-
self.init(address: address, username: username, password: password, tlsConfiguration: nil)
62+
self.init(address: address, username: username, password: password, tlsConfiguration: tlsConfiguration)
5863
}
5964
}

0 commit comments

Comments
 (0)