Skip to content

Commit e2e138d

Browse files
authored
Add an option to set 'certificateVerification' to the client builder (#980)
Motivation: We allow users to set the `certificateVerification` if they configure their client directly using `ClientConnection.Configuration` but not via the builder API. Modifications: - Add `withTLS(certificateVerification:)` to the client connection builder. (The same option is already available on the server builder) Result: Users can set the certificate verification mode on the client builder.
1 parent 5c20271 commit e2e138d

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

Sources/GRPC/GRPCChannel/GRPCChannelBuilder.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,14 @@ extension ClientConnection.Builder.Secure {
227227
self.tls.trustRoots = trustRoots
228228
return self
229229
}
230+
231+
/// Whether to verify remote certificates. Defaults to `.fullVerification` if not otherwise
232+
/// configured.
233+
@discardableResult
234+
public func withTLS(certificateVerification: CertificateVerification) -> Self {
235+
self.tls.certificateVerification = certificateVerification
236+
return self
237+
}
230238
}
231239

232240
extension ClientConnection.Builder {

Tests/GRPCTests/ClientTLSTests.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,29 @@ class ClientTLSHostnameOverrideTests: GRPCTestCase {
111111

112112
try self.doTestUnary()
113113
}
114+
115+
func testTLSWithNoCertificateVerification() throws {
116+
self.server = try Server.secure(
117+
group: self.eventLoopGroup,
118+
certificateChain: [SampleCertificate.server.certificate],
119+
privateKey: SamplePrivateKey.server
120+
)
121+
.withServiceProviders([EchoProvider()])
122+
.withLogger(self.serverLogger)
123+
.bind(host: "localhost", port: 0)
124+
.wait()
125+
126+
guard let port = self.server.channel.localAddress?.port else {
127+
XCTFail("could not get server port")
128+
return
129+
}
130+
131+
self.connection = ClientConnection.secure(group: self.eventLoopGroup)
132+
.withTLS(trustRoots: .certificates([]))
133+
.withTLS(certificateVerification: .none)
134+
.withBackgroundActivityLogger(self.clientLogger)
135+
.connect(host: "localhost", port: port)
136+
137+
try self.doTestUnary()
138+
}
114139
}

Tests/GRPCTests/XCTestManifests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ extension ClientTLSHostnameOverrideTests {
9696
// to regenerate.
9797
static let __allTests__ClientTLSHostnameOverrideTests = [
9898
("testTLSWithHostnameOverride", testTLSWithHostnameOverride),
99+
("testTLSWithNoCertificateVerification", testTLSWithNoCertificateVerification),
99100
("testTLSWithoutHostnameOverride", testTLSWithoutHostnameOverride),
100101
]
101102
}

0 commit comments

Comments
 (0)