Flow the ConnectionContext to the SNI callback (#2478)

This commit is contained in:
David Fowler 2018-04-11 15:56:25 -07:00 committed by GitHub
parent e8bb88cb58
commit f6b2880369
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 27 additions and 20 deletions

View File

@ -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; }
}

View File

@ -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).
/// </para>
/// </summary>
public Func<IFeatureCollection, string, X509Certificate2> ServerCertificateSelector { get; set; }
public Func<ConnectionContext, string, X509Certificate2> ServerCertificateSelector { get; set; }
/// <summary>
/// Specifies the client certificate requirements for a HTTPS connection. Defaults to <see cref="ClientCertificateMode.NoCertificate"/>.

View File

@ -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<IAdaptedConnection>(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)

View File

@ -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<IConnectionAdapter> ConnectionAdapters { get; set; }

View File

@ -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);

View File

@ -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<IFeatureCollection, string, X509Certificate2> _serverCertificateSelector;
private readonly Func<ConnectionContext, string, X509Certificate2> _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);

View File

@ -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<SslStream>());
Assert.NotNull(connection);
Assert.NotNull(connection.Features.Get<SslStream>());
#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<SslStream>());
Assert.NotNull(connection);
Assert.NotNull(connection.Features.Get<SslStream>());
#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<SslStream>());
Assert.NotNull(connection);
Assert.NotNull(connection.Features.Get<SslStream>());
#if NETCOREAPP2_1
Assert.Equal("localhost", name);
#else