Skip to content

A native macOS / windows / Linux database IDE built with Swift and AppKit. Connect to PostgreSQL, MySQL, SQLite, and Redis from a single app with a fast, keyboard-driven interface.

C++

fix(postgres): persist 5-state SSLMode and gate PostgresClient.run()

cyenx18d8ace715ff
diff --git a/macos/Core/Models/Database/ConnectionConfig.swift b/macos/Core/Models/Database/ConnectionConfig.swift
index aeaadd2..244d55b 100644
--- a/macos/Core/Models/Database/ConnectionConfig.swift
+++ b/macos/Core/Models/Database/ConnectionConfig.swift
@@ -14,6 +14,9 @@ struct ConnectionConfig: Codable, Sendable, Hashable, Identifiable {
var database: String?
var username: String?
var sslEnabled: Bool
+ /// Fine-grained SSL posture; `nil` falls back to `sslEnabled` via
+ /// `effectiveSSLMode` for rows persisted before incident `6a4aad0`.
+ var sslMode: SSLMode?
var colorTag: ColorTag?
var group: String?
@@ -45,6 +48,7 @@ struct ConnectionConfig: Codable, Sendable, Hashable, Identifiable {
database: String? = nil,
username: String? = nil,
sslEnabled: Bool = false,
+ sslMode: SSLMode? = nil,
colorTag: ColorTag? = nil,
group: String? = nil,
sslKeyPath: String? = nil,
@@ -63,6 +67,7 @@ struct ConnectionConfig: Codable, Sendable, Hashable, Identifiable {
self.database = database
self.username = username
self.sslEnabled = sslEnabled
+ self.sslMode = sslMode
self.colorTag = colorTag
self.group = group
self.sslKeyPath = sslKeyPath
@@ -80,6 +85,13 @@ struct ConnectionConfig: Codable, Sendable, Hashable, Identifiable {
}
return "\(host ?? "localhost"):\(port ?? databaseType.defaultPort)"
}
+
+ /// Resolved SSL posture. Honors the persisted 5-state `sslMode`; falls back
+ /// to `sslEnabled` for rows saved before the field existed (legacy
+ /// `sslEnabled=true` maps to `.preferred`, matching the pre-fix adapter).
+ var effectiveSSLMode: SSLMode {
+ sslMode ?? (sslEnabled ? .preferred : .disabled)
+ }
}
struct SSHTunnelConfig: Codable, Sendable, Hashable {
diff --git a/macos/Data/Adapters/PostgreSQL/PostgreSQLAdapter.swift b/macos/Data/Adapters/PostgreSQL/PostgreSQLAdapter.swift
index c690f1f..99f1ee9 100644
--- a/macos/Data/Adapters/PostgreSQL/PostgreSQLAdapter.swift
+++ b/macos/Data/Adapters/PostgreSQL/PostgreSQLAdapter.swift
@@ -28,31 +28,7 @@ final class PostgreSQLAdapter: DatabaseAdapter, SchemaInspectable, @unchecked Se
let username = config.username ?? "postgres"
let database = config.database ?? username
- let tlsConfig: PostgresClient.Configuration.TLS
- if config.sslEnabled {
- var tls = TLSConfiguration.makeClientConfiguration()
-
- // Load client certificate and key for mTLS (e.g., Teleport)
- if let certPath = config.sslCertPath, !certPath.isEmpty,
- let keyPath = config.sslKeyPath, !keyPath.isEmpty {
- let cert = try NIOSSLCertificate.fromPEMFile(certPath)
- let key = try NIOSSLPrivateKey(file: keyPath, format: .pem)
- tls.certificateChain = cert.map { .certificate($0) }
- tls.privateKey = .privateKey(key)
- }
-
- // Load CA certificate for server verification
- if let caPath = config.sslCACertPath, !caPath.isEmpty {
- tls.trustRoots = .file(caPath)
- tls.certificateVerification = .fullVerification
- } else {
- tls.certificateVerification = .none
- }
-
- tlsConfig = .prefer(tls)
- } else {
- tlsConfig = .disable
- }
+ let tlsConfig = try Self.makeTLSConfig(for: config.effectiveSSLMode, config: config)
let pgConfig = PostgresClient.Configuration(
host: host,
@@ -66,14 +42,57 @@ final class PostgreSQLAdapter: DatabaseAdapter, SchemaInspectable, @unchecked Se
let newClient = PostgresClient(configuration: pgConfig)
self.client = newClient
- // PostgresClient requires run() in a background task
- self.clientTask = Task { await newClient.run() }
+ // Querying before PostgresClient.run() is scheduled surfaces a bare
+ // `_ConnectionPoolModule.ConnectionPoolError` that hides the real cause;
+ // reproduces on slow handshakes (e.g. HighGo Secure login-audit NOTICE).
+ // The continuation guarantees the spawned Task body has been entered;
+ // the trailing yield lets run() reach its first suspension point.
+ await withCheckedContinuation { (cont: CheckedContinuation<Void, Never>) in
+ self.clientTask = Task {
+ cont.resume()
+ await newClient.run()
+ }
+ }
+ await Task.yield()
- // Verify the connection works
_ = try await newClient.query("SELECT 1")
isConnected = true
}
+ private static func makeTLSConfig(
+ for mode: SSLMode,
+ config: ConnectionConfig
+ ) throws -> PostgresClient.Configuration.TLS {
+ if mode == .disabled { return .disable }
+
+ var tls = TLSConfiguration.makeClientConfiguration()
+
+ // mTLS client cert (e.g. Teleport).
+ if let certPath = config.sslCertPath, !certPath.isEmpty,
+ let keyPath = config.sslKeyPath, !keyPath.isEmpty {
+ let cert = try NIOSSLCertificate.fromPEMFile(certPath)
+ let key = try NIOSSLPrivateKey(file: keyPath, format: .pem)
+ tls.certificateChain = cert.map { .certificate($0) }
+ tls.privateKey = .privateKey(key)
+ }
+
+ // libpq semantics: PREFERRED/REQUIRED encrypt only; VERIFY_CA verifies
+ // the chain; VERIFY_IDENTITY also verifies the hostname.
+ switch mode {
+ case .verifyCA, .verifyIdentity:
+ if let caPath = config.sslCACertPath, !caPath.isEmpty {
+ tls.trustRoots = .file(caPath)
+ }
+ tls.certificateVerification = (mode == .verifyIdentity) ? .fullVerification : .noHostnameVerification
+ default:
+ tls.certificateVerification = .none
+ }
+
+ // .prefer falls back to plaintext when the server refuses TLS;
+ // every stricter mode requires a successful TLS handshake.
+ return (mode == .preferred) ? .prefer(tls) : .require(tls)
+ }
+
func disconnect() async throws {
clientTask?.cancel()
clientTask = nil
diff --git a/macos/Data/Persistence/Models/SavedConnectionEntity.swift b/macos/Data/Persistence/Models/SavedConnectionEntity.swift
index 5474cac..799741e 100644
--- a/macos/Data/Persistence/Models/SavedConnectionEntity.swift
+++ b/macos/Data/Persistence/Models/SavedConnectionEntity.swift
@@ -16,6 +16,9 @@ final class SavedConnectionEntity {
var database: String?
var username: String?
var sslEnabled: Bool
+ /// Fine-grained SSL posture (raw value of `SSLMode`). Optional for SwiftData
+ /// lightweight migration of pre-existing rows. Read site falls back to `sslEnabled`.
+ var sslMode: String?
var sshEnabled: Bool
var sshHost: String?
var sshPort: Int?
@@ -42,6 +45,7 @@ final class SavedConnectionEntity {
database: String? = nil,
username: String? = nil,
sslEnabled: Bool = false,
+ sslMode: String? = nil,
sshEnabled: Bool = false,
sshHost: String? = nil,
sshPort: Int? = nil,
@@ -65,6 +69,7 @@ final class SavedConnectionEntity {
self.database = database
self.username = username
self.sslEnabled = sslEnabled
+ self.sslMode = sslMode
self.sshEnabled = sshEnabled
self.sshHost = sshHost
self.sshPort = sshPort
@@ -112,6 +117,7 @@ final class SavedConnectionEntity {
database: database,
username: username,
sslEnabled: sslEnabled,
+ sslMode: sslMode.flatMap { SSLMode(rawValue: $0) },
colorTag: colorTag.flatMap { ColorTag(rawValue: $0) },
group: group,
filePath: filePath,
diff --git a/macos/Data/Persistence/Repositories/SwiftDataConnectionRepository.swift b/macos/Data/Persistence/Repositories/SwiftDataConnectionRepository.swift
index 3764b3b..4d7baa7 100644
--- a/macos/Data/Persistence/Repositories/SwiftDataConnectionRepository.swift
+++ b/macos/Data/Persistence/Repositories/SwiftDataConnectionRepository.swift
@@ -55,6 +55,7 @@ final class SwiftDataConnectionRepository: ConnectionRepository, @unchecked Send
database: config.database,
username: config.username,
sslEnabled: config.sslEnabled,
+ sslMode: config.sslMode?.rawValue,
sshEnabled: config.sshConfig != nil,
sshHost: config.sshConfig?.host,
sshPort: config.sshConfig?.port,
@@ -88,6 +89,7 @@ final class SwiftDataConnectionRepository: ConnectionRepository, @unchecked Send
entity.database = config.database
entity.username = config.username
entity.sslEnabled = config.sslEnabled
+ entity.sslMode = config.sslMode?.rawValue
entity.colorTag = config.colorTag?.rawValue
entity.group = config.group
entity.filePath = config.filePath
diff --git a/macos/Presentation/Views/ConnectionForm/ConnectionFormView.swift b/macos/Presentation/Views/ConnectionForm/ConnectionFormView.swift
index a4a2e96..cae78d5 100644
--- a/macos/Presentation/Views/ConnectionForm/ConnectionFormView.swift
+++ b/macos/Presentation/Views/ConnectionForm/ConnectionFormView.swift
@@ -376,7 +376,7 @@ struct ConnectionFormView: View {
port = String(config.port ?? databaseType.defaultPort)
database = config.database ?? ""
username = config.username ?? ""
- sslMode = config.sslEnabled ? .required : .preferred
+ sslMode = config.effectiveSSLMode
colorTag = config.colorTag ?? .blue
sqliteFilePath = config.filePath ?? ""
@@ -450,6 +450,7 @@ struct ConnectionFormView: View {
database: isFileDB ? nil : (database.isEmpty ? nil : database),
username: (isFileDB || databaseType == .redis) ? nil : username,
sslEnabled: isFileDB ? false : sslEnabled,
+ sslMode: isFileDB ? nil : sslMode,
colorTag: colorTag,
group: existingConfig?.group,
sslKeyPath: sslKeyPath.isEmpty ? nil : sslKeyPath,