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

Use the server hostname override as the :authority, if present #1033

Merged
merged 1 commit into from
Nov 5, 2020
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
2 changes: 1 addition & 1 deletion Sources/GRPC/ClientConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public class ClientConnection {
public init(configuration: Configuration) {
self.configuration = configuration
self.scheme = configuration.tls == nil ? "http" : "https"
self.authority = configuration.target.host
self.authority = configuration.tls?.hostnameOverride ?? configuration.target.host
self.connectionManager = ConnectionManager(
configuration: configuration,
logger: configuration.backgroundActivityLogger
Expand Down
72 changes: 72 additions & 0 deletions Tests/GRPCTests/ClientTLSTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,76 @@ class ClientTLSHostnameOverrideTests: GRPCTestCase {

try self.doTestUnary()
}

func testAuthorityUsesTLSHostnameOverride() throws {
// This test validates that when suppled with a server hostname override, the client uses it
// as the ":authority" pseudo-header.

self.server = try Server.secure(
group: self.eventLoopGroup,
certificateChain: [SampleCertificate.exampleServer.certificate],
privateKey: SamplePrivateKey.exampleServer
)
.withTLS(trustRoots: .certificates([SampleCertificate.ca.certificate]))
.withServiceProviders([AuthorityCheckingEcho()])
.withLogger(self.serverLogger)
.bind(host: "localhost", port: 0)
.wait()

guard let port = self.server.channel.localAddress?.port else {
XCTFail("could not get server port")
return
}

self.connection = ClientConnection.secure(group: self.eventLoopGroup)
.withTLS(trustRoots: .certificates([SampleCertificate.ca.certificate]))
.withTLS(serverHostnameOverride: "example.com")
.withBackgroundActivityLogger(self.clientLogger)
.connect(host: "localhost", port: port)

try self.doTestUnary()
}
}

private class AuthorityCheckingEcho: Echo_EchoProvider {
func get(
request: Echo_EchoRequest,
context: StatusOnlyCallContext
) -> EventLoopFuture<Echo_EchoResponse> {
// Since we currently go via the HTTP 2-to-1 handler, ':authority' gets normalized to 'host'.
// TODO: Use ':authority' when we fully switch to HTTP/2
guard let authority = context.headers.first(name: "host") else {
let status = GRPCStatus(
code: .failedPrecondition,
message: "Missing ':authority' pseudo header"
)
return context.eventLoop.makeFailedFuture(status)
}

XCTAssertEqual(authority, SampleCertificate.exampleServer.commonName)
XCTAssertNotEqual(authority, "localhost")

return context.eventLoop.makeSucceededFuture(.with {
$0.text = "Swift echo get: \(request.text)"
})
}

func expand(
request: Echo_EchoRequest,
context: StreamingResponseCallContext<Echo_EchoResponse>
) -> EventLoopFuture<GRPCStatus> {
preconditionFailure("Not implemented")
}

func collect(
context: UnaryResponseCallContext<Echo_EchoResponse>
) -> EventLoopFuture<(StreamEvent<Echo_EchoRequest>) -> Void> {
preconditionFailure("Not implemented")
}

func update(
context: StreamingResponseCallContext<Echo_EchoResponse>
) -> EventLoopFuture<(StreamEvent<Echo_EchoRequest>) -> Void> {
preconditionFailure("Not implemented")
}
}
1 change: 1 addition & 0 deletions Tests/GRPCTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ extension ClientTLSHostnameOverrideTests {
// `swift test --generate-linuxmain`
// to regenerate.
static let __allTests__ClientTLSHostnameOverrideTests = [
("testAuthorityUsesTLSHostnameOverride", testAuthorityUsesTLSHostnameOverride),
("testTLSWithHostnameOverride", testTLSWithHostnameOverride),
("testTLSWithNoCertificateVerification", testTLSWithNoCertificateVerification),
("testTLSWithoutHostnameOverride", testTLSWithoutHostnameOverride),
Expand Down