Skip to content

Commit

Permalink
Add an option to set 'certificateVerification' to the client builder (#…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
glbrntt authored Sep 30, 2020
1 parent 5c20271 commit e2e138d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Sources/GRPC/GRPCChannel/GRPCChannelBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,14 @@ extension ClientConnection.Builder.Secure {
self.tls.trustRoots = trustRoots
return self
}

/// Whether to verify remote certificates. Defaults to `.fullVerification` if not otherwise
/// configured.
@discardableResult
public func withTLS(certificateVerification: CertificateVerification) -> Self {
self.tls.certificateVerification = certificateVerification
return self
}
}

extension ClientConnection.Builder {
Expand Down
25 changes: 25 additions & 0 deletions Tests/GRPCTests/ClientTLSTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,29 @@ class ClientTLSHostnameOverrideTests: GRPCTestCase {

try self.doTestUnary()
}

func testTLSWithNoCertificateVerification() throws {
self.server = try Server.secure(
group: self.eventLoopGroup,
certificateChain: [SampleCertificate.server.certificate],
privateKey: SamplePrivateKey.server
)
.withServiceProviders([EchoProvider()])
.withLogger(self.serverLogger)
.bind(host: "localhost", port: 0)
.wait()

guard let port = self.server.channel.localAddress?.port else {
XCTFail("could not get server port")
return
}

self.connection = ClientConnection.secure(group: self.eventLoopGroup)
.withTLS(trustRoots: .certificates([]))
.withTLS(certificateVerification: .none)
.withBackgroundActivityLogger(self.clientLogger)
.connect(host: "localhost", port: port)

try self.doTestUnary()
}
}
1 change: 1 addition & 0 deletions Tests/GRPCTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ extension ClientTLSHostnameOverrideTests {
// to regenerate.
static let __allTests__ClientTLSHostnameOverrideTests = [
("testTLSWithHostnameOverride", testTLSWithHostnameOverride),
("testTLSWithNoCertificateVerification", testTLSWithNoCertificateVerification),
("testTLSWithoutHostnameOverride", testTLSWithoutHostnameOverride),
]
}
Expand Down

0 comments on commit e2e138d

Please sign in to comment.