Skip to content

Commit e7d9722

Browse files
vkuttypCopilot
andcommitted
fix: add TrustServerCertificate support and fix sp_GetEmployeeById param name
- Add trustServerCertificate: Bool = false to MSSQLConnection.Configuration (equivalent to TrustServerCertificate=true in SQL Server connection strings) - Use it in upgradeTLS() to control certificateVerification (.none vs default) - Test configuration sets trustServerCertificate: true for self-signed certs - Fix mssql_seed.sql: sp_GetEmployeeById parameter renamed @p1@id to match what testCallProcedureWithInputParams expects Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent dcfc35f commit e7d9722

3 files changed

Lines changed: 38 additions & 27 deletions

File tree

Sources/MSSQLNio/MSSQLConnection.swift

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ public final class MSSQLConnection: SQLDatabase, @unchecked Sendable {
3737
/// SQL Server password. Not required when using Windows/NTLM authentication (`domain` is set).
3838
public var password: String = ""
3939
public var tls: SQLTLSConfiguration = .prefer
40+
/// When `true`, the server's TLS certificate is accepted without verification
41+
/// (equivalent to `TrustServerCertificate=true` in a SQL Server connection string).
42+
/// Set this to `true` when connecting to servers with self-signed certificates (e.g. dev/test).
43+
public var trustServerCertificate: Bool = false
4044
public var logger: Logger = Logger(label: "MSSQLNio")
4145
/// Timeout for establishing the TCP + TLS + Login7 handshake (seconds). nil = no limit.
4246
public var connectTimeout: TimeInterval? = 30
@@ -52,18 +56,20 @@ public final class MSSQLConnection: SQLDatabase, @unchecked Sendable {
5256
public init(host: String, port: Int = 1433,
5357
database: String, username: String, password: String,
5458
tls: SQLTLSConfiguration = .prefer,
59+
trustServerCertificate: Bool = false,
5560
connectTimeout: TimeInterval? = 30,
5661
queryTimeout: TimeInterval? = nil,
5762
readOnly: Bool = false) {
58-
self.host = host
59-
self.port = port
60-
self.database = database
61-
self.username = username
62-
self.password = password
63-
self.tls = tls
64-
self.connectTimeout = connectTimeout
65-
self.queryTimeout = queryTimeout
66-
self.readOnly = readOnly
63+
self.host = host
64+
self.port = port
65+
self.database = database
66+
self.username = username
67+
self.password = password
68+
self.tls = tls
69+
self.trustServerCertificate = trustServerCertificate
70+
self.connectTimeout = connectTimeout
71+
self.queryTimeout = queryTimeout
72+
self.readOnly = readOnly
6773
}
6874

6975
/// Windows/NTLM authentication. Username and password are optional;
@@ -75,19 +81,21 @@ public final class MSSQLConnection: SQLDatabase, @unchecked Sendable {
7581
username: String = "",
7682
password: String = "",
7783
tls: SQLTLSConfiguration = .prefer,
84+
trustServerCertificate: Bool = false,
7885
connectTimeout: TimeInterval? = 30,
7986
queryTimeout: TimeInterval? = nil,
8087
readOnly: Bool = false) {
81-
self.host = host
82-
self.port = port
83-
self.database = database
84-
self.domain = domain
85-
self.username = username
86-
self.password = password
87-
self.tls = tls
88-
self.connectTimeout = connectTimeout
89-
self.queryTimeout = queryTimeout
90-
self.readOnly = readOnly
88+
self.host = host
89+
self.port = port
90+
self.database = database
91+
self.domain = domain
92+
self.username = username
93+
self.password = password
94+
self.tls = tls
95+
self.trustServerCertificate = trustServerCertificate
96+
self.connectTimeout = connectTimeout
97+
self.queryTimeout = queryTimeout
98+
self.readOnly = readOnly
9199
}
92100
}
93101

@@ -201,7 +209,9 @@ public final class MSSQLConnection: SQLDatabase, @unchecked Sendable {
201209

202210
private func upgradeTLS() async throws {
203211
var tlsConfig = TLSConfiguration.makeClientConfiguration()
204-
tlsConfig.certificateVerification = .none // dev/test; in production supply a CA cert
212+
if config.trustServerCertificate {
213+
tlsConfig.certificateVerification = .none
214+
}
205215
let sslContext = try NIOSSLContext(configuration: tlsConfig)
206216
// IP addresses cannot be used for SNI — pass nil to disable SNI for IP hosts
207217
let sniHostname: String? = {

Tests/MSSQLNioTests/Support/TestDatabase.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ struct TestDatabase {
2424
static var configuration: MSSQLConnection.Configuration {
2525
let env = ProcessInfo.processInfo.environment
2626
return MSSQLConnection.Configuration(
27-
host: env["MSSQL_TEST_HOST"] ?? "127.0.0.1",
28-
port: Int(env["MSSQL_TEST_PORT"] ?? "1433") ?? 1433,
29-
database: env["MSSQL_TEST_DB"] ?? "MSSQLNioTestDb",
30-
username: env["MSSQL_TEST_USER"] ?? "sa",
31-
password: env["MSSQL_TEST_PASS"] ?? "aBCD111"
27+
host: env["MSSQL_TEST_HOST"] ?? "127.0.0.1",
28+
port: Int(env["MSSQL_TEST_PORT"] ?? "1433") ?? 1433,
29+
database: env["MSSQL_TEST_DB"] ?? "MSSQLNioTestDb",
30+
username: env["MSSQL_TEST_USER"] ?? "sa",
31+
password: env["MSSQL_TEST_PASS"] ?? "aBCD111",
32+
trustServerCertificate: true
3233
)
3334
}
3435

Tests/Resources/mssql_seed.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,13 @@ GO
206206
-- ─── Stored Procedures ───────────────────────────────────────────────────────
207207

208208
CREATE PROCEDURE sp_GetEmployeeById
209-
@p1 UNIQUEIDENTIFIER
209+
@id UNIQUEIDENTIFIER
210210
AS
211211
BEGIN
212212
SET NOCOUNT ON;
213213
SELECT id, name, email, salary, department_id, is_active, notes
214214
FROM Employees
215-
WHERE id = @p1;
215+
WHERE id = @id;
216216
END;
217217
GO
218218

0 commit comments

Comments
 (0)