TLSConfigurationTests.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. func clientTLSCustomPrivateKey() throws {
  37. let custom = NoOpCustomPrivateKey()
  38. let config = HTTP2ClientTransport.Posix.TransportSecurity.tls {
  39. $0.privateKey = .customPrivateKey(custom)
  40. }
  41. let tls = try #require(config.tls)
  42. let tlsConfig = try TLSConfiguration(tls)
  43. let privateKey = try #require(tlsConfig.privateKey?.privateKey)
  44. #expect(privateKey == NIOSSLPrivateKey(customPrivateKey: custom))
  45. }
  46. @Test("Server custom private key")
  47. func serverTLSCustomPrivateKey() throws {
  48. let custom = NoOpCustomPrivateKey()
  49. let config = HTTP2ServerTransport.Posix.TransportSecurity.tls(
  50. certificateChain: [],
  51. privateKey: .customPrivateKey(custom)
  52. )
  53. let tls = try #require(config.tls)
  54. let tlsConfig = try TLSConfiguration(tls)
  55. let privateKey = try #require(tlsConfig.privateKey?.privateKey)
  56. #expect(privateKey == NIOSSLPrivateKey(customPrivateKey: custom))
  57. }
  58. struct StaticCertLoader: CertificateReloader {
  59. var sslContextConfigurationOverride: NIOSSLContextConfigurationOverride {
  60. var override = NIOSSLContextConfigurationOverride()
  61. override.certificateChain = []
  62. override.privateKey = .privateKey(NIOSSLPrivateKey(customPrivateKey: NoOpCustomPrivateKey()))
  63. return override
  64. }
  65. }
  66. @Test("Client cert reloader is set")
  67. func clientCertificateReloader() throws {
  68. let config = try HTTP2ClientTransport.Posix.TransportSecurity.mTLS(
  69. certificateReloader: StaticCertLoader()
  70. )
  71. let tls = try #require(config.tls)
  72. let tlsConfig = try TLSConfiguration(tls)
  73. let privateKey = try #require(tlsConfig.privateKey?.privateKey)
  74. #expect(privateKey == NIOSSLPrivateKey(customPrivateKey: NoOpCustomPrivateKey()))
  75. #expect(tlsConfig.certificateChain.isEmpty)
  76. #expect(tlsConfig.sslContextCallback != nil)
  77. }
  78. @Test("Server cert reloader is set", arguments: [false, true])
  79. func serverCertificateReloader(isMTLS: Bool) throws {
  80. let config: HTTP2ServerTransport.Posix.TransportSecurity
  81. if isMTLS {
  82. config = try HTTP2ServerTransport.Posix.TransportSecurity.mTLS(
  83. certificateReloader: StaticCertLoader()
  84. )
  85. } else {
  86. config = try HTTP2ServerTransport.Posix.TransportSecurity.tls(
  87. certificateReloader: StaticCertLoader()
  88. )
  89. }
  90. let tls = try #require(config.tls)
  91. let tlsConfig = try TLSConfiguration(tls)
  92. let privateKey = try #require(tlsConfig.privateKey?.privateKey)
  93. #expect(privateKey == NIOSSLPrivateKey(customPrivateKey: NoOpCustomPrivateKey()))
  94. #expect(tlsConfig.certificateChain.isEmpty)
  95. #expect(tlsConfig.sslContextCallback != nil)
  96. }
  97. }
  98. extension HTTP2ClientTransport.Posix.TransportSecurity {
  99. var tls: TLS? {
  100. switch self.wrapped {
  101. case .tls(let tls):
  102. return tls
  103. case .plaintext:
  104. return nil
  105. }
  106. }
  107. }
  108. extension HTTP2ServerTransport.Posix.TransportSecurity {
  109. var tls: TLS? {
  110. switch self.wrapped {
  111. case .tls(let tls):
  112. return tls
  113. case .plaintext:
  114. return nil
  115. }
  116. }
  117. }
  118. extension NIOSSLPrivateKeySource {
  119. var privateKey: NIOSSLPrivateKey? {
  120. switch self {
  121. case .privateKey(let key):
  122. return key
  123. case .file:
  124. return nil
  125. }
  126. }
  127. }