ClientTLSTests.swift 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright 2019, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import EchoImplementation
  17. import EchoModel
  18. import Foundation
  19. import GRPC
  20. import GRPCSampleData
  21. import NIO
  22. import NIOSSL
  23. import XCTest
  24. class ClientTLSHostnameOverrideTests: GRPCTestCase {
  25. var eventLoopGroup: EventLoopGroup!
  26. var server: Server!
  27. var connection: ClientConnection!
  28. override func setUp() {
  29. super.setUp()
  30. self.eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  31. }
  32. override func tearDown() {
  33. XCTAssertNoThrow(try self.server.close().wait())
  34. XCTAssertNoThrow(try self.connection.close().wait())
  35. XCTAssertNoThrow(try self.eventLoopGroup.syncShutdownGracefully())
  36. super.tearDown()
  37. }
  38. func doTestUnary() throws {
  39. let client = Echo_EchoClient(
  40. channel: self.connection,
  41. defaultCallOptions: self.callOptionsWithLogger
  42. )
  43. let get = client.get(.with { $0.text = "foo" })
  44. let response = try get.response.wait()
  45. XCTAssertEqual(response.text, "Swift echo get: foo")
  46. let status = try get.status.wait()
  47. XCTAssertEqual(status.code, .ok)
  48. }
  49. func testTLSWithHostnameOverride() throws {
  50. // Run a server presenting a certificate for example.com on localhost.
  51. let cert = SampleCertificate.exampleServer.certificate
  52. let key = SamplePrivateKey.exampleServer
  53. self.server = try Server.secure(
  54. group: self.eventLoopGroup,
  55. certificateChain: [cert],
  56. privateKey: key
  57. )
  58. .withTLS(trustRoots: .certificates([SampleCertificate.ca.certificate]))
  59. .withServiceProviders([EchoProvider()])
  60. .withLogger(self.serverLogger)
  61. .bind(host: "localhost", port: 0)
  62. .wait()
  63. guard let port = self.server.channel.localAddress?.port else {
  64. XCTFail("could not get server port")
  65. return
  66. }
  67. self.connection = ClientConnection.secure(group: self.eventLoopGroup)
  68. .withTLS(trustRoots: .certificates([SampleCertificate.ca.certificate]))
  69. .withTLS(serverHostnameOverride: "example.com")
  70. .withBackgroundActivityLogger(self.clientLogger)
  71. .connect(host: "localhost", port: port)
  72. try self.doTestUnary()
  73. }
  74. func testTLSWithoutHostnameOverride() throws {
  75. // Run a server presenting a certificate for localhost on localhost.
  76. let cert = SampleCertificate.server.certificate
  77. let key = SamplePrivateKey.server
  78. self.server = try Server.secure(
  79. group: self.eventLoopGroup,
  80. certificateChain: [cert],
  81. privateKey: key
  82. )
  83. .withTLS(trustRoots: .certificates([SampleCertificate.ca.certificate]))
  84. .withServiceProviders([EchoProvider()])
  85. .withLogger(self.serverLogger)
  86. .bind(host: "localhost", port: 0)
  87. .wait()
  88. guard let port = self.server.channel.localAddress?.port else {
  89. XCTFail("could not get server port")
  90. return
  91. }
  92. self.connection = ClientConnection.secure(group: self.eventLoopGroup)
  93. .withTLS(trustRoots: .certificates([SampleCertificate.ca.certificate]))
  94. .withBackgroundActivityLogger(self.clientLogger)
  95. .connect(host: "localhost", port: port)
  96. try self.doTestUnary()
  97. }
  98. func testTLSWithNoCertificateVerification() throws {
  99. self.server = try Server.secure(
  100. group: self.eventLoopGroup,
  101. certificateChain: [SampleCertificate.server.certificate],
  102. privateKey: SamplePrivateKey.server
  103. )
  104. .withServiceProviders([EchoProvider()])
  105. .withLogger(self.serverLogger)
  106. .bind(host: "localhost", port: 0)
  107. .wait()
  108. guard let port = self.server.channel.localAddress?.port else {
  109. XCTFail("could not get server port")
  110. return
  111. }
  112. self.connection = ClientConnection.secure(group: self.eventLoopGroup)
  113. .withTLS(trustRoots: .certificates([]))
  114. .withTLS(certificateVerification: .none)
  115. .withBackgroundActivityLogger(self.clientLogger)
  116. .connect(host: "localhost", port: port)
  117. try self.doTestUnary()
  118. }
  119. func testAuthorityUsesTLSHostnameOverride() throws {
  120. // This test validates that when suppled with a server hostname override, the client uses it
  121. // as the ":authority" pseudo-header.
  122. self.server = try Server.secure(
  123. group: self.eventLoopGroup,
  124. certificateChain: [SampleCertificate.exampleServer.certificate],
  125. privateKey: SamplePrivateKey.exampleServer
  126. )
  127. .withTLS(trustRoots: .certificates([SampleCertificate.ca.certificate]))
  128. .withServiceProviders([AuthorityCheckingEcho()])
  129. .withLogger(self.serverLogger)
  130. .bind(host: "localhost", port: 0)
  131. .wait()
  132. guard let port = self.server.channel.localAddress?.port else {
  133. XCTFail("could not get server port")
  134. return
  135. }
  136. self.connection = ClientConnection.secure(group: self.eventLoopGroup)
  137. .withTLS(trustRoots: .certificates([SampleCertificate.ca.certificate]))
  138. .withTLS(serverHostnameOverride: "example.com")
  139. .withBackgroundActivityLogger(self.clientLogger)
  140. .connect(host: "localhost", port: port)
  141. try self.doTestUnary()
  142. }
  143. }
  144. private class AuthorityCheckingEcho: Echo_EchoProvider {
  145. var interceptors: Echo_EchoServerInterceptorFactoryProtocol?
  146. func get(
  147. request: Echo_EchoRequest,
  148. context: StatusOnlyCallContext
  149. ) -> EventLoopFuture<Echo_EchoResponse> {
  150. guard let authority = context.headers.first(name: ":authority") else {
  151. let status = GRPCStatus(
  152. code: .failedPrecondition,
  153. message: "Missing ':authority' pseudo header"
  154. )
  155. return context.eventLoop.makeFailedFuture(status)
  156. }
  157. XCTAssertEqual(authority, SampleCertificate.exampleServer.commonName)
  158. XCTAssertNotEqual(authority, "localhost")
  159. return context.eventLoop.makeSucceededFuture(.with {
  160. $0.text = "Swift echo get: \(request.text)"
  161. })
  162. }
  163. func expand(
  164. request: Echo_EchoRequest,
  165. context: StreamingResponseCallContext<Echo_EchoResponse>
  166. ) -> EventLoopFuture<GRPCStatus> {
  167. preconditionFailure("Not implemented")
  168. }
  169. func collect(
  170. context: UnaryResponseCallContext<Echo_EchoResponse>
  171. ) -> EventLoopFuture<(StreamEvent<Echo_EchoRequest>) -> Void> {
  172. preconditionFailure("Not implemented")
  173. }
  174. func update(
  175. context: StreamingResponseCallContext<Echo_EchoResponse>
  176. ) -> EventLoopFuture<(StreamEvent<Echo_EchoRequest>) -> Void> {
  177. preconditionFailure("Not implemented")
  178. }
  179. }