Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add E2E tests with TLS enabled #12

Merged
merged 6 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ let dependencies: [Package.Dependency] = [
url: "https://github.com/apple/swift-nio-extras.git",
from: "1.4.0"
),
.package(
url: "https://github.com/apple/swift-certificates.git",
from: "1.5.0"
),
]

let defaultSwiftSettings: [SwiftSetting] = [
Expand Down Expand Up @@ -104,6 +108,7 @@ let targets: [Target] = [
.target(name: "GRPCNIOTransportCore"),
.product(name: "GRPCCore", package: "grpc-swift"),
.product(name: "NIOPosix", package: "swift-nio"),
.product(name: "NIOSSL", package: "swift-nio-ssl"),
],
swiftSettings: defaultSwiftSettings
),
Expand Down Expand Up @@ -132,6 +137,8 @@ let targets: [Target] = [
name: "GRPCNIOTransportHTTP2Tests",
dependencies: [
.target(name: "GRPCNIOTransportHTTP2"),
.product(name: "X509", package: "swift-certificates"),
.product(name: "NIOSSL", package: "swift-nio-ssl"),
]
)
]
Expand Down
90 changes: 81 additions & 9 deletions Sources/GRPCNIOTransportHTTP2Posix/TLSConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,27 @@ extension HTTP2ServerTransport.Posix.Config {
/// If this is set to `true` but the client does not support ALPN, then the connection will be rejected.
public var requireALPN: Bool

/// Create a new HTTP2 NIO Posix server transport TLS config.
/// - Parameters:
/// - certificateChain: The certificates the server will offer during negotiation.
/// - privateKey: The private key associated with the leaf certificate.
/// - clientCertificateVerification: How to verify the client certificate, if one is presented.
/// - trustRoots: The trust roots to be used when verifying client certificates.
/// - requireALPN: Whether ALPN is required.
public init(
certificateChain: [TLSConfig.CertificateSource],
privateKey: TLSConfig.PrivateKeySource,
clientCertificateVerification: TLSConfig.CertificateVerification,
trustRoots: TLSConfig.TrustRootsSource,
requireALPN: Bool
) {
self.certificateChain = certificateChain
self.privateKey = privateKey
self.clientCertificateVerification = clientCertificateVerification
self.trustRoots = trustRoots
self.requireALPN = requireALPN
}

/// Create a new HTTP2 NIO Posix transport TLS config, with some values defaulted:
/// - `clientCertificateVerificationMode` equals `doNotVerify`
/// - `trustRoots` equals `systemDefault`
Expand All @@ -180,18 +201,22 @@ extension HTTP2ServerTransport.Posix.Config {
/// - Parameters:
/// - certificateChain: The certificates the server will offer during negotiation.
/// - privateKey: The private key associated with the leaf certificate.
/// - configure: A closure which allows you to modify the defaults before returning them.
/// - Returns: A new HTTP2 NIO Posix transport TLS config.
public static func defaults(
certificateChain: [TLSConfig.CertificateSource],
privateKey: TLSConfig.PrivateKeySource
privateKey: TLSConfig.PrivateKeySource,
configure: (_ config: inout Self) -> Void = { _ in }
) -> Self {
Self(
var config = Self(
certificateChain: certificateChain,
privateKey: privateKey,
clientCertificateVerification: .noVerification,
trustRoots: .systemDefault,
requireALPN: false
)
configure(&config)
return config
}

/// Create a new HTTP2 NIO Posix transport TLS config, with some values defaulted to match
Expand All @@ -203,18 +228,22 @@ extension HTTP2ServerTransport.Posix.Config {
/// - Parameters:
/// - certificateChain: The certificates the server will offer during negotiation.
/// - privateKey: The private key associated with the leaf certificate.
/// - configure: A closure which allows you to modify the defaults before returning them.
/// - Returns: A new HTTP2 NIO Posix transport TLS config.
public static func mTLS(
certificateChain: [TLSConfig.CertificateSource],
privateKey: TLSConfig.PrivateKeySource
privateKey: TLSConfig.PrivateKeySource,
configure: (_ config: inout Self) -> Void = { _ in }
) -> Self {
Self(
var config = Self(
certificateChain: certificateChain,
privateKey: privateKey,
clientCertificateVerification: .noHostnameVerification,
trustRoots: .systemDefault,
requireALPN: false
)
configure(&config)
return config
}
}
}
Expand Down Expand Up @@ -256,42 +285,85 @@ extension HTTP2ClientTransport.Posix.Config {
/// An optional server hostname to use when verifying certificates.
public var serverHostname: String?

/// Create a new HTTP2 NIO Posix client transport TLS config.
/// - Parameters:
/// - certificateChain: The certificates the client will offer during negotiation.
/// - privateKey: The private key associated with the leaf certificate.
/// - serverCertificateVerification: How to verify the server certificate, if one is presented.
/// - trustRoots: The trust roots to be used when verifying server certificates.
/// - serverHostname: An optional server hostname to use when verifying certificates.
public init(
certificateChain: [TLSConfig.CertificateSource],
privateKey: TLSConfig.PrivateKeySource?,
serverCertificateVerification: TLSConfig.CertificateVerification,
trustRoots: TLSConfig.TrustRootsSource,
serverHostname: String?
) {
self.certificateChain = certificateChain
self.privateKey = privateKey
self.serverCertificateVerification = serverCertificateVerification
self.trustRoots = trustRoots
self.serverHostname = serverHostname
}

/// Create a new HTTP2 NIO Posix transport TLS config, with some values defaulted:
/// - `certificateChain` equals `[]`
/// - `privateKey` equals `nil`
/// - `serverCertificateVerification` equals `fullVerification`
/// - `trustRoots` equals `systemDefault`
/// - `serverHostname` equals `nil`
///
/// - Parameters:
/// - configure: A closure which allows you to modify the defaults before returning them.
/// - Returns: A new HTTP2 NIO Posix transport TLS config.
public static var defaults: Self {
Self(
public static func defaults(
configure: (_ config: inout Self) -> Void = { _ in }
) -> Self {
gjcairo marked this conversation as resolved.
Show resolved Hide resolved
var config = Self(
certificateChain: [],
privateKey: nil,
serverCertificateVerification: .fullVerification,
trustRoots: .systemDefault,
serverHostname: nil
)
configure(&config)
return config
}

/// Create a new HTTP2 NIO Posix transport TLS config, with some values defaulted:
/// - `certificateChain` equals `[]`
/// - `privateKey` equals `nil`
/// - `serverCertificateVerification` equals `fullVerification`
/// - `trustRoots` equals `systemDefault`
/// - `serverHostname` equals `nil`
public static var defaults: Self {
Self.defaults()
}

/// Create a new HTTP2 NIO Posix transport TLS config, with some values defaulted to match
/// the requirements of mTLS:
/// - `trustRoots` equals `systemDefault`
/// - `serverCertificateVerification` equals `fullVerification`
///
/// - Parameters:
/// - certificateChain: The certificates the client will offer during negotiation.
/// - privateKey: The private key associated with the leaf certificate.
/// - configure: A closure which allows you to modify the defaults before returning them.
/// - Returns: A new HTTP2 NIO Posix transport TLS config.
public static func mTLS(
certificateChain: [TLSConfig.CertificateSource],
privateKey: TLSConfig.PrivateKeySource
privateKey: TLSConfig.PrivateKeySource,
configure: (_ config: inout Self) -> Void = { _ in }
) -> Self {
Self(
var config = Self(
certificateChain: certificateChain,
privateKey: privateKey,
serverCertificateVerification: .fullVerification,
trustRoots: .systemDefault
trustRoots: .systemDefault,
serverHostname: nil
)
configure(&config)
return config
}
}
}
17 changes: 13 additions & 4 deletions Sources/GRPCNIOTransportHTTP2TransportServices/TLSConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,26 @@ extension HTTP2ServerTransport.TransportServices.Config {
/// If this is set to `true` but the client does not support ALPN, then the connection will be rejected.
public var requireALPN: Bool

/// Create a new HTTP2 NIO Transport Services transport TLS config.
/// - Parameters:
/// - requireALPN: Whether ALPN is required.
/// - identityProvider: A provider for the `SecIdentity` to be used when setting up TLS.
public init(
requireALPN: Bool,
identityProvider: @Sendable @escaping () throws -> SecIdentity
) {
self.requireALPN = requireALPN
self.identityProvider = identityProvider
}

/// Create a new HTTP2 NIO Transport Services transport TLS config, with some values defaulted:
/// - `requireALPN` equals `false`
///
/// - Returns: A new HTTP2 NIO Transport Services transport TLS config.
public static func defaults(
identityProvider: @Sendable @escaping () throws -> SecIdentity
) -> Self {
Self(
identityProvider: identityProvider,
requireALPN: false
)
Self(requireALPN: false, identityProvider: identityProvider)
}
}
}
Expand Down
Loading
Loading