TLSConfigurationTests.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright 2025, 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 GRPCNIOTransportHTTP2
  17. import NIOCertificateReloading
  18. import NIOCore
  19. import NIOSSL
  20. import Testing
  21. struct TLSConfigurationTests {
  22. struct NoOpCustomPrivateKey: NIOSSLCustomPrivateKey, Hashable {
  23. var signatureAlgorithms: [SignatureAlgorithm] { [] }
  24. func sign(
  25. channel: any Channel,
  26. algorithm: SignatureAlgorithm,
  27. data: ByteBuffer
  28. ) -> EventLoopFuture<ByteBuffer> {
  29. channel.eventLoop.makeSucceededFuture(ByteBuffer())
  30. }
  31. func decrypt(channel: any Channel, data: ByteBuffer) -> EventLoopFuture<ByteBuffer> {
  32. channel.eventLoop.makeSucceededFuture(ByteBuffer())
  33. }
  34. }
  35. @Test("Client custom private key")
  36. @available(gRPCSwiftNIOTransport 1.2, *)
  37. func clientTLSCustomPrivateKey() throws {
  38. let custom = NoOpCustomPrivateKey()
  39. let config = HTTP2ClientTransport.Posix.TransportSecurity.tls {
  40. $0.privateKey = .customPrivateKey(custom)
  41. }
  42. let tls = try #require(config.tls)
  43. let tlsConfig = try TLSConfiguration(tls)
  44. let privateKey = try #require(tlsConfig.privateKey?.privateKey)
  45. #expect(privateKey == NIOSSLPrivateKey(customPrivateKey: custom))
  46. }
  47. @Test("Server custom private key")
  48. @available(gRPCSwiftNIOTransport 1.2, *)
  49. func serverTLSCustomPrivateKey() throws {
  50. let custom = NoOpCustomPrivateKey()
  51. let config = HTTP2ServerTransport.Posix.TransportSecurity.tls(
  52. certificateChain: [],
  53. privateKey: .customPrivateKey(custom)
  54. )
  55. let tls = try #require(config.tls)
  56. let tlsConfig = try TLSConfiguration(tls)
  57. let privateKey = try #require(tlsConfig.privateKey?.privateKey)
  58. #expect(privateKey == NIOSSLPrivateKey(customPrivateKey: custom))
  59. }
  60. struct StaticCertLoader: CertificateReloader {
  61. var sslContextConfigurationOverride: NIOSSLContextConfigurationOverride {
  62. var override = NIOSSLContextConfigurationOverride()
  63. override.certificateChain = []
  64. override.privateKey = .privateKey(NIOSSLPrivateKey(customPrivateKey: NoOpCustomPrivateKey()))
  65. return override
  66. }
  67. }
  68. @Test("Client cert reloader is set")
  69. @available(gRPCSwiftNIOTransport 1.2, *)
  70. func clientCertificateReloader() throws {
  71. let config = try HTTP2ClientTransport.Posix.TransportSecurity.mTLS(
  72. certificateReloader: StaticCertLoader()
  73. )
  74. let tls = try #require(config.tls)
  75. let tlsConfig = try TLSConfiguration(tls)
  76. let privateKey = try #require(tlsConfig.privateKey?.privateKey)
  77. #expect(privateKey == NIOSSLPrivateKey(customPrivateKey: NoOpCustomPrivateKey()))
  78. #expect(tlsConfig.certificateChain.isEmpty)
  79. #expect(tlsConfig.sslContextCallback != nil)
  80. }
  81. @Test("Server cert reloader is set", arguments: [false, true])
  82. @available(gRPCSwiftNIOTransport 1.2, *)
  83. func serverCertificateReloader(isMTLS: Bool) throws {
  84. let config: HTTP2ServerTransport.Posix.TransportSecurity
  85. if isMTLS {
  86. config = try HTTP2ServerTransport.Posix.TransportSecurity.mTLS(
  87. certificateReloader: StaticCertLoader()
  88. )
  89. } else {
  90. config = try HTTP2ServerTransport.Posix.TransportSecurity.tls(
  91. certificateReloader: StaticCertLoader()
  92. )
  93. }
  94. let tls = try #require(config.tls)
  95. let tlsConfig = try TLSConfiguration(tls)
  96. let privateKey = try #require(tlsConfig.privateKey?.privateKey)
  97. #expect(privateKey == NIOSSLPrivateKey(customPrivateKey: NoOpCustomPrivateKey()))
  98. #expect(tlsConfig.certificateChain.isEmpty)
  99. #expect(tlsConfig.sslContextCallback != nil)
  100. }
  101. }
  102. @available(gRPCSwiftNIOTransport 1.2, *)
  103. extension HTTP2ClientTransport.Posix.TransportSecurity {
  104. var tls: TLS? {
  105. switch self.wrapped {
  106. case .tls(let tls):
  107. return tls
  108. case .plaintext:
  109. return nil
  110. }
  111. }
  112. }
  113. @available(gRPCSwiftNIOTransport 1.2, *)
  114. extension HTTP2ServerTransport.Posix.TransportSecurity {
  115. var tls: TLS? {
  116. switch self.wrapped {
  117. case .tls(let tls):
  118. return tls
  119. case .plaintext:
  120. return nil
  121. }
  122. }
  123. }
  124. @available(gRPCSwiftNIOTransport 1.2, *)
  125. extension NIOSSLPrivateKeySource {
  126. var privateKey: NIOSSLPrivateKey? {
  127. switch self {
  128. case .privateKey(let key):
  129. return key
  130. case .file:
  131. return nil
  132. }
  133. }
  134. }