From f6b2880369f8ff81052f15ad7021753b0710f44e Mon Sep 17 00:00:00 2001 From: David Fowler Date: Wed, 11 Apr 2018 15:56:25 -0700 Subject: [PATCH] Flow the ConnectionContext to the SNI callback (#2478) --- .../Internal/ConnectionAdapterContext.cs | 9 ++++++--- .../HttpsConnectionAdapterOptions.cs | 4 ++-- src/Kestrel.Core/Internal/HttpConnection.cs | 4 ++-- .../Internal/HttpConnectionContext.cs | 2 ++ .../Internal/HttpConnectionMiddleware.cs | 3 ++- .../Internal/HttpsConnectionAdapter.cs | 7 ++++--- .../HttpsConnectionAdapterTests.cs | 18 +++++++++--------- 7 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/Kestrel.Core/Adapter/Internal/ConnectionAdapterContext.cs b/src/Kestrel.Core/Adapter/Internal/ConnectionAdapterContext.cs index 1f87c8311e..3896e1cf85 100644 --- a/src/Kestrel.Core/Adapter/Internal/ConnectionAdapterContext.cs +++ b/src/Kestrel.Core/Adapter/Internal/ConnectionAdapterContext.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.IO; +using Microsoft.AspNetCore.Connections; using Microsoft.AspNetCore.Http.Features; namespace Microsoft.AspNetCore.Server.Kestrel.Core.Adapter.Internal @@ -10,13 +11,15 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Adapter.Internal // we want to add more connection metadata later. public class ConnectionAdapterContext { - internal ConnectionAdapterContext(IFeatureCollection features, Stream connectionStream) + internal ConnectionAdapterContext(ConnectionContext connectionContext, Stream connectionStream) { - Features = features; + ConnectionContext = connectionContext; ConnectionStream = connectionStream; } - public IFeatureCollection Features { get; } + internal ConnectionContext ConnectionContext { get; } + + public IFeatureCollection Features => ConnectionContext.Features; public Stream ConnectionStream { get; } } diff --git a/src/Kestrel.Core/HttpsConnectionAdapterOptions.cs b/src/Kestrel.Core/HttpsConnectionAdapterOptions.cs index 760c29cfc1..cf6bd88236 100644 --- a/src/Kestrel.Core/HttpsConnectionAdapterOptions.cs +++ b/src/Kestrel.Core/HttpsConnectionAdapterOptions.cs @@ -6,7 +6,7 @@ using System.Net.Security; using System.Security.Authentication; using System.Security.Cryptography.X509Certificates; using System.Threading; -using Microsoft.AspNetCore.Http.Features; +using Microsoft.AspNetCore.Connections; using Microsoft.AspNetCore.Server.Kestrel.Core; namespace Microsoft.AspNetCore.Server.Kestrel.Https @@ -47,7 +47,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Https /// If the server certificate has an Extended Key Usage extension, the usages must include Server Authentication (OID 1.3.6.1.5.5.7.3.1). /// /// - public Func ServerCertificateSelector { get; set; } + public Func ServerCertificateSelector { get; set; } /// /// Specifies the client certificate requirements for a HTTPS connection. Defaults to . diff --git a/src/Kestrel.Core/Internal/HttpConnection.cs b/src/Kestrel.Core/Internal/HttpConnection.cs index 91b0887f01..d57a7f920c 100644 --- a/src/Kestrel.Core/Internal/HttpConnection.cs +++ b/src/Kestrel.Core/Internal/HttpConnection.cs @@ -300,7 +300,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal { var connectionAdapters = _context.ConnectionAdapters; var stream = new RawStream(_context.Transport.Input, _context.Transport.Output); - var adapterContext = new ConnectionAdapterContext(_context.ConnectionFeatures, stream); + var adapterContext = new ConnectionAdapterContext(_context.ConnectionContext, stream); _adaptedConnections = new List(connectionAdapters.Count); try @@ -309,7 +309,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal { var adaptedConnection = await connectionAdapters[i].OnConnectionAsync(adapterContext); _adaptedConnections.Add(adaptedConnection); - adapterContext = new ConnectionAdapterContext(_context.ConnectionFeatures, adaptedConnection.ConnectionStream); + adapterContext = new ConnectionAdapterContext(_context.ConnectionContext, adaptedConnection.ConnectionStream); } } catch (Exception ex) diff --git a/src/Kestrel.Core/Internal/HttpConnectionContext.cs b/src/Kestrel.Core/Internal/HttpConnectionContext.cs index b60d5702ac..161ca647a7 100644 --- a/src/Kestrel.Core/Internal/HttpConnectionContext.cs +++ b/src/Kestrel.Core/Internal/HttpConnectionContext.cs @@ -5,6 +5,7 @@ using System.Buffers; using System.Collections.Generic; using System.IO.Pipelines; using System.Net; +using Microsoft.AspNetCore.Connections; using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Server.Kestrel.Core.Adapter.Internal; @@ -15,6 +16,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal public string ConnectionId { get; set; } public long HttpConnectionId { get; set; } public HttpProtocols Protocols { get; set; } + public ConnectionContext ConnectionContext { get; set; } public ServiceContext ServiceContext { get; set; } public IFeatureCollection ConnectionFeatures { get; set; } public IList ConnectionAdapters { get; set; } diff --git a/src/Kestrel.Core/Internal/HttpConnectionMiddleware.cs b/src/Kestrel.Core/Internal/HttpConnectionMiddleware.cs index 87ae45c154..fad29ae0db 100644 --- a/src/Kestrel.Core/Internal/HttpConnectionMiddleware.cs +++ b/src/Kestrel.Core/Internal/HttpConnectionMiddleware.cs @@ -41,6 +41,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal var httpConnectionContext = new HttpConnectionContext { ConnectionId = connectionContext.ConnectionId, + ConnectionContext = connectionContext, HttpConnectionId = httpConnectionId, Protocols = _protocols, ServiceContext = _serviceContext, @@ -69,7 +70,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal var connection = new HttpConnection(httpConnectionContext); var processingTask = connection.StartRequestProcessing(_application); - + connectionContext.Transport.Input.OnWriterCompleted((error, state) => { ((HttpConnection)state).Abort(error); diff --git a/src/Kestrel.Core/Internal/HttpsConnectionAdapter.cs b/src/Kestrel.Core/Internal/HttpsConnectionAdapter.cs index 4107da5118..cab926bb63 100644 --- a/src/Kestrel.Core/Internal/HttpsConnectionAdapter.cs +++ b/src/Kestrel.Core/Internal/HttpsConnectionAdapter.cs @@ -8,6 +8,7 @@ using System.Net.Security; using System.Security.Cryptography.X509Certificates; using System.Threading; using System.Threading.Tasks; +using Microsoft.AspNetCore.Connections; using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.AspNetCore.Server.Kestrel.Core.Adapter.Internal; @@ -22,7 +23,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Https.Internal private readonly HttpsConnectionAdapterOptions _options; private readonly X509Certificate2 _serverCertificate; - private readonly Func _serverCertificateSelector; + private readonly Func _serverCertificateSelector; private readonly ILogger _logger; @@ -133,7 +134,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Https.Internal selector = (sender, name) => { context.Features.Set(sslStream); - var cert = _serverCertificateSelector(context.Features, name); + var cert = _serverCertificateSelector(context.ConnectionContext, name); if (cert != null) { EnsureCertificateIsAllowedForServerAuth(cert); @@ -169,7 +170,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Https.Internal if (_serverCertificateSelector != null) { context.Features.Set(sslStream); - serverCert = _serverCertificateSelector(context.Features, null); + serverCert = _serverCertificateSelector(context.ConnectionContext, null); if (serverCert != null) { EnsureCertificateIsAllowedForServerAuth(serverCert); diff --git a/test/Kestrel.FunctionalTests/HttpsConnectionAdapterTests.cs b/test/Kestrel.FunctionalTests/HttpsConnectionAdapterTests.cs index 43ff38ce26..37bc3e4d87 100644 --- a/test/Kestrel.FunctionalTests/HttpsConnectionAdapterTests.cs +++ b/test/Kestrel.FunctionalTests/HttpsConnectionAdapterTests.cs @@ -160,10 +160,10 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests { new HttpsConnectionAdapter(new HttpsConnectionAdapterOptions { - ServerCertificateSelector = (features, name) => + ServerCertificateSelector = (connection, name) => { - Assert.NotNull(features); - Assert.NotNull(features.Get()); + Assert.NotNull(connection); + Assert.NotNull(connection.Features.Get()); #if NETCOREAPP2_1 Assert.Equal("localhost", name); #else @@ -201,10 +201,10 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests { new HttpsConnectionAdapter(new HttpsConnectionAdapterOptions { - ServerCertificateSelector = (features, name) => + ServerCertificateSelector = (connection, name) => { - Assert.NotNull(features); - Assert.NotNull(features.Get()); + Assert.NotNull(connection); + Assert.NotNull(connection.Features.Get()); #if NETCOREAPP2_1 Assert.Equal("localhost", name); #else @@ -291,10 +291,10 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests new HttpsConnectionAdapter(new HttpsConnectionAdapterOptions { ServerCertificate = _x509Certificate2NoExt, - ServerCertificateSelector = (features, name) => + ServerCertificateSelector = (connection, name) => { - Assert.NotNull(features); - Assert.NotNull(features.Get()); + Assert.NotNull(connection); + Assert.NotNull(connection.Features.Get()); #if NETCOREAPP2_1 Assert.Equal("localhost", name); #else