|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftNIO open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 Apple Inc. and the SwiftNIO project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftNIO project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import NIOCore |
| 16 | +import NIOSSL |
| 17 | + |
| 18 | +/// A protocol that defines a certificate reloader. |
| 19 | +/// |
| 20 | +/// A certificate reloader is a service that can provide you with updated versions of a certificate and private key pair, in |
| 21 | +/// the form of a `NIOSSLContextConfigurationOverride`, which will be used when performing a TLS handshake in NIO. |
| 22 | +/// Each implementation can choose how to observe for changes, but they all require an ``sslContextConfigurationOverride`` |
| 23 | +/// to be exposed. |
| 24 | +public protocol CertificateReloader: Sendable { |
| 25 | + /// A `NIOSSLContextConfigurationOverride` that will be used as part of the NIO application's TLS configuration. |
| 26 | + /// Its certificate and private key will be kept up-to-date via whatever mechanism the specific ``CertificateReloader`` |
| 27 | + /// implementation provides. |
| 28 | + var sslContextConfigurationOverride: NIOSSLContextConfigurationOverride { get } |
| 29 | +} |
| 30 | + |
| 31 | +extension TLSConfiguration { |
| 32 | + /// Errors thrown when creating a ``NIOSSL/TLSConfiguration`` with a ``CertificateReloader``. |
| 33 | + public struct CertificateReloaderError: Error, Hashable, CustomStringConvertible { |
| 34 | + private enum _Backing: CustomStringConvertible { |
| 35 | + case missingCertificateChain |
| 36 | + case missingPrivateKey |
| 37 | + |
| 38 | + var description: String { |
| 39 | + switch self { |
| 40 | + case .missingCertificateChain: |
| 41 | + return "Missing certificate chain" |
| 42 | + case .missingPrivateKey: |
| 43 | + return "Missing private key" |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + private let _backing: _Backing |
| 49 | + |
| 50 | + private init(backing: _Backing) { |
| 51 | + self._backing = backing |
| 52 | + } |
| 53 | + |
| 54 | + public var description: String { |
| 55 | + self._backing.description |
| 56 | + } |
| 57 | + |
| 58 | + /// The given ``CertificateReloader`` could not provide a certificate chain with which to create this config. |
| 59 | + public static var missingCertificateChain: Self { .init(backing: .missingCertificateChain) } |
| 60 | + |
| 61 | + /// The given ``CertificateReloader`` could not provide a private key with which to create this config. |
| 62 | + public static var missingPrivateKey: Self { .init(backing: .missingPrivateKey) } |
| 63 | + } |
| 64 | + |
| 65 | + /// Create a ``NIOSSL/TLSConfiguration`` for use with server-side contexts, with certificate reloading enabled. |
| 66 | + /// - Parameter certificateReloader: A ``CertificateReloader`` to watch for certificate and key pair updates. |
| 67 | + /// - Returns: A ``NIOSSL/TLSConfiguration`` for use with server-side contexts, that reloads the certificate and key |
| 68 | + /// used in its SSL handshake. |
| 69 | + /// - Throws: This method will throw if an override isn't present. This may happen if a certificate or private key could not be |
| 70 | + /// loaded from the given paths. |
| 71 | + public static func makeServerConfiguration( |
| 72 | + certificateReloader: some CertificateReloader |
| 73 | + ) throws -> Self { |
| 74 | + let override = certificateReloader.sslContextConfigurationOverride |
| 75 | + |
| 76 | + guard let certificateChain = override.certificateChain else { |
| 77 | + throw CertificateReloaderError.missingCertificateChain |
| 78 | + } |
| 79 | + |
| 80 | + guard let privateKey = override.privateKey else { |
| 81 | + throw CertificateReloaderError.missingPrivateKey |
| 82 | + } |
| 83 | + |
| 84 | + var configuration = Self.makeServerConfiguration( |
| 85 | + certificateChain: certificateChain, |
| 86 | + privateKey: privateKey |
| 87 | + ) |
| 88 | + configuration.setCertificateReloader(certificateReloader) |
| 89 | + return configuration |
| 90 | + } |
| 91 | + |
| 92 | + /// Create a ``NIOSSL/TLSConfiguration`` for use with client-side contexts, with certificate reloading enabled. |
| 93 | + /// - Parameter certificateReloader: A ``CertificateReloader`` to watch for certificate and key pair updates. |
| 94 | + /// - Returns: A ``NIOSSL/TLSConfiguration`` for use with client-side contexts, that reloads the certificate and key |
| 95 | + /// used in its SSL handshake. |
| 96 | + /// - Throws: This method will throw if an override isn't present. This may happen if a certificate or private key could not be |
| 97 | + /// loaded from the given paths. |
| 98 | + public static func makeClientConfiguration( |
| 99 | + certificateReloader: some CertificateReloader |
| 100 | + ) throws -> Self { |
| 101 | + let override = certificateReloader.sslContextConfigurationOverride |
| 102 | + |
| 103 | + guard let certificateChain = override.certificateChain else { |
| 104 | + throw CertificateReloaderError.missingCertificateChain |
| 105 | + } |
| 106 | + |
| 107 | + guard let privateKey = override.privateKey else { |
| 108 | + throw CertificateReloaderError.missingPrivateKey |
| 109 | + } |
| 110 | + |
| 111 | + var configuration = Self.makeClientConfiguration() |
| 112 | + configuration.certificateChain = certificateChain |
| 113 | + configuration.privateKey = privateKey |
| 114 | + configuration.setCertificateReloader(certificateReloader) |
| 115 | + return configuration |
| 116 | + } |
| 117 | + |
| 118 | + /// Configure a ``CertificateReloader`` to observe updates for the certificate and key pair used. |
| 119 | + /// - Parameter reloader: A ``CertificateReloader`` to watch for certificate and key pair updates. |
| 120 | + public mutating func setCertificateReloader(_ reloader: some CertificateReloader) { |
| 121 | + self.sslContextCallback = { _, promise in |
| 122 | + promise.succeed(reloader.sslContextConfigurationOverride) |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments