Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Commit e1de1d5

Browse files
committed
Relayering. Still needs cleanup.
1 parent 05b637e commit e1de1d5

31 files changed

+176
-465
lines changed

build/dependencies.props

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
<MicrosoftAspNetCoreHttpPackageVersion>2.1.0-preview1-27845</MicrosoftAspNetCoreHttpPackageVersion>
1717
<MicrosoftAspNetCoreTestingPackageVersion>2.1.0-preview1-27845</MicrosoftAspNetCoreTestingPackageVersion>
1818
<MicrosoftAspNetCoreWebUtilitiesPackageVersion>2.1.0-preview1-27845</MicrosoftAspNetCoreWebUtilitiesPackageVersion>
19+
<MicrosoftExtensionsConfigurationBinderPackageVersion>2.1.0-preview1-27845</MicrosoftExtensionsConfigurationBinderPackageVersion>
1920
<MicrosoftExtensionsConfigurationJsonPackageVersion>2.1.0-preview1-27845</MicrosoftExtensionsConfigurationJsonPackageVersion>
21+
<MicrosoftExtensionsDependencyInjectionPackageVersion>2.1.0-preview1-27845</MicrosoftExtensionsDependencyInjectionPackageVersion>
2022
<MicrosoftExtensionsLoggingAbstractionsPackageVersion>2.1.0-preview1-27845</MicrosoftExtensionsLoggingAbstractionsPackageVersion>
2123
<MicrosoftExtensionsLoggingConsolePackageVersion>2.1.0-preview1-27845</MicrosoftExtensionsLoggingConsolePackageVersion>
2224
<MicrosoftExtensionsLoggingPackageVersion>2.1.0-preview1-27845</MicrosoftExtensionsLoggingPackageVersion>

src/Kestrel.Core/CoreStrings.resx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,4 +486,13 @@
486486
<data name="CertNotFoundInStore" xml:space="preserve">
487487
<value>The requested certificate {subject} could not be found in {storeLocation}/{storeName} with AllowInvalid setting: {allowInvalid}.</value>
488488
</data>
489+
<data name="EndpointMissingUrl" xml:space="preserve">
490+
<value>The endpoint {endpointName} is missing the required 'Url' parameter.</value>
491+
</data>
492+
<data name="HttpsUrlProvidedButNoDevelopmentCertificateFound" xml:space="preserve">
493+
<value>Unable to configure HTTPS endpoint. Try running 'dotnet developercertificates https -t' to setup a developer certificate for use with localhost. For information on configuring HTTPS see https://go.microsoft.com/fwlink/?linkid=848054</value>
494+
</data>
495+
<data name="MultipleCertificateSources" xml:space="preserve">
496+
<value>The endpoint {endpointName} specified multiple certificate sources.</value>
497+
</data>
489498
</root>

src/Kestrel.Core/Internal/AddressBindContext.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ internal class AddressBindContext
1414
public List<ListenOptions> ListenOptions { get; set; }
1515
public KestrelServerOptions ServerOptions { get; set; }
1616
public ILogger Logger { get; set; }
17-
public IDefaultHttpsProvider DefaultHttpsProvider { get; set; }
1817

1918
public Func<ListenOptions, Task> CreateBinding { get; set; }
2019
}

src/Kestrel.Core/Internal/AddressBinder.cs

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
using System.IO;
77
using System.Linq;
88
using System.Net;
9-
using System.Security.Cryptography.X509Certificates;
109
using System.Threading.Tasks;
1110
using Microsoft.AspNetCore.Builder;
11+
using Microsoft.AspNetCore.Hosting;
1212
using Microsoft.AspNetCore.Hosting.Server.Features;
1313
using Microsoft.AspNetCore.Protocols;
1414
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
@@ -21,7 +21,6 @@ internal class AddressBinder
2121
public static async Task BindAsync(IServerAddressesFeature addresses,
2222
KestrelServerOptions serverOptions,
2323
ILogger logger,
24-
IDefaultHttpsProvider defaultHttpsProvider,
2524
Func<ListenOptions, Task> createBinding)
2625
{
2726
var listenOptions = serverOptions.ListenOptions;
@@ -36,7 +35,6 @@ public static async Task BindAsync(IServerAddressesFeature addresses,
3635
ListenOptions = listenOptions,
3736
ServerOptions = serverOptions,
3837
Logger = logger,
39-
DefaultHttpsProvider = defaultHttpsProvider ?? UnconfiguredDefaultHttpsProvider.Instance,
4038
CreateBinding = createBinding
4139
};
4240

@@ -178,7 +176,7 @@ public async Task BindAsync(AddressBindContext context)
178176
{
179177
try
180178
{
181-
context.DefaultHttpsProvider.ConfigureHttps(httpsDefault);
179+
httpsDefault.UseHttps();
182180
}
183181
catch (Exception)
184182
{
@@ -266,31 +264,12 @@ public virtual async Task BindAsync(AddressBindContext context)
266264

267265
if (https && !options.ConnectionAdapters.Any(f => f.IsHttps))
268266
{
269-
context.DefaultHttpsProvider.ConfigureHttps(options);
267+
options.UseHttps();
270268
}
271269

272270
await options.BindAsync(context).ConfigureAwait(false);
273271
}
274272
}
275273
}
276-
277-
private class UnconfiguredDefaultHttpsProvider : IDefaultHttpsProvider
278-
{
279-
public static readonly UnconfiguredDefaultHttpsProvider Instance = new UnconfiguredDefaultHttpsProvider();
280-
281-
private UnconfiguredDefaultHttpsProvider()
282-
{
283-
}
284-
285-
public X509Certificate2 Certificate => null;
286-
287-
public void ConfigureHttps(ListenOptions listenOptions)
288-
{
289-
// We have to throw here. If this is called, it's because the user asked for "https" binding but for some
290-
// reason didn't provide a certificate and didn't use the "DefaultHttpsProvider". This means if we no-op,
291-
// we'll silently downgrade to HTTP, which is bad.
292-
throw new InvalidOperationException(CoreStrings.UnableToConfigureHttpsBindings);
293-
}
294-
}
295274
}
296275
}

src/Kestrel/Internal/ConfigurationReader.cs renamed to src/Kestrel.Core/Internal/ConfigurationReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ private void ReadEndpoints()
7474
var url = endpointConfig["Url"];
7575
if (string.IsNullOrEmpty(url))
7676
{
77-
throw new InvalidOperationException(KestrelStrings.FormatEndpointMissingUrl(endpointConfig.Key));
77+
throw new InvalidOperationException(CoreStrings.FormatEndpointMissingUrl(endpointConfig.Key));
7878
}
7979

8080
var endpoint = new EndpointConfig()

src/Kestrel.Core/Internal/IDefaultHttpsProvider.cs

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/Kestrel.Core/Internal/IKestrelConfigurationLoader.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/Kestrel.Core/Kestrel.Core.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@
1212
</PropertyGroup>
1313

1414
<ItemGroup>
15+
<PackageReference Include="Microsoft.AspNetCore.Certificates.Generation.Sources" PrivateAssets="All" Version="$(MicrosoftAspNetCoreCertificatesGenerationSourcesPackageVersion)" />
1516
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="$(MicrosoftAspNetCoreHostingAbstractionsPackageVersion)" />
1617
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="$(MicrosoftAspNetCoreWebUtilitiesPackageVersion)" />
1718
<PackageReference Include="Microsoft.Net.Http.Headers" Version="$(MicrosoftNetHttpHeadersPackageVersion)" />
19+
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="$(MicrosoftExtensionsConfigurationBinderPackageVersion)" />
1820
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="$(MicrosoftExtensionsLoggingAbstractionsPackageVersion)" />
1921
<PackageReference Include="Microsoft.Extensions.Options" Version="$(MicrosoftExtensionsOptionsPackageVersion)" />
2022
<PackageReference Include="System.Threading.Tasks.Extensions" Version="$(SystemThreadingTasksExtensionsPackageVersion)" />
2123
<PackageReference Include="System.Memory" Version="$(SystemMemoryPackageVersion)" />
2224
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="$(SystemRuntimeCompilerServicesUnsafePackageVersion)" />
25+
<PackageReference Include="System.Security.Cryptography.Cng" Version="$(SystemSecurityCryptographyCngPackageVersion)" />
2326
</ItemGroup>
2427

2528
<ItemGroup>

src/Kestrel/KestrelConfigurationLoader.cs renamed to src/Kestrel.Core/KestrelConfigurationLoader.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
namespace Microsoft.AspNetCore.Server.Kestrel
1919
{
20-
public class KestrelConfigurationLoader : IKestrelConfigurationLoader
20+
public class KestrelConfigurationLoader
2121
{
2222
internal KestrelConfigurationLoader(KestrelServerOptions options, IConfiguration configuration)
2323
{
@@ -216,13 +216,8 @@ public void Load()
216216
if (https)
217217
{
218218
// Defaults
219-
httpsOptions.ServerCertificate = listenOptions.KestrelServerOptions.GetOverriddenDefaultCertificate();
220-
Options.GetHttpsDefaults()(httpsOptions);
221-
if (httpsOptions.ServerCertificate == null)
222-
{
223-
var provider = Options.ApplicationServices.GetRequiredService<IDefaultHttpsProvider>();
224-
httpsOptions.ServerCertificate = provider.Certificate; // May be null.
225-
}
219+
httpsOptions.ServerCertificate = listenOptions.KestrelServerOptions.DefaultCertificate;
220+
Options.HttpsDefaults(httpsOptions);
226221

227222
// Specified
228223
httpsOptions.ServerCertificate = LoadCertificate(endpoint.Certificate, endpoint.Name)
@@ -259,7 +254,7 @@ private void LoadDefaultCert(ConfigurationReader configReader)
259254
var defaultCert = LoadCertificate(defaultCertConfig, "Default");
260255
if (defaultCert != null)
261256
{
262-
Options.OverrideDefaultCertificate(defaultCert);
257+
Options.DefaultCertificate = defaultCert;
263258
}
264259
}
265260
}
@@ -268,7 +263,7 @@ private X509Certificate2 LoadCertificate(CertificateConfig certInfo, string endp
268263
{
269264
if (certInfo.IsFileCert && certInfo.IsStoreCert)
270265
{
271-
throw new InvalidOperationException(KestrelStrings.FormatMultipleCertificateSources(endpointName));
266+
throw new InvalidOperationException(CoreStrings.FormatMultipleCertificateSources(endpointName));
272267
}
273268
else if (certInfo.IsFileCert)
274269
{

src/Kestrel.Core/KestrelServer.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public class KestrelServer : IServer
2222
private readonly List<ITransport> _transports = new List<ITransport>();
2323
private readonly Heartbeat _heartbeat;
2424
private readonly IServerAddressesFeature _serverAddresses;
25-
private readonly IDefaultHttpsProvider _defaultHttpsProvider;
2625
private readonly ITransportFactory _transportFactory;
2726

2827
private bool _hasStarted;
@@ -34,12 +33,6 @@ public KestrelServer(IOptions<KestrelServerOptions> options, ITransportFactory t
3433
{
3534
}
3635

37-
public KestrelServer(IOptions<KestrelServerOptions> options, ITransportFactory transportFactory, ILoggerFactory loggerFactory, IDefaultHttpsProvider defaultHttpsProvider)
38-
: this(transportFactory, CreateServiceContext(options, loggerFactory))
39-
{
40-
_defaultHttpsProvider = defaultHttpsProvider;
41-
}
42-
4336
// For testing
4437
internal KestrelServer(ITransportFactory transportFactory, ServiceContext serviceContext)
4538
{
@@ -159,7 +152,7 @@ async Task OnBind(ListenOptions endpoint)
159152
await transport.BindAsync().ConfigureAwait(false);
160153
}
161154

162-
await AddressBinder.BindAsync(_serverAddresses, Options, Trace, _defaultHttpsProvider, OnBind).ConfigureAwait(false);
155+
await AddressBinder.BindAsync(_serverAddresses, Options, Trace, OnBind).ConfigureAwait(false);
163156
}
164157
catch (Exception ex)
165158
{

src/Kestrel.Core/KestrelServerOptions.cs

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Linq;
67
using System.Net;
8+
using System.Security.Cryptography.X509Certificates;
9+
using Microsoft.AspNetCore.Certificates.Generation;
710
using Microsoft.AspNetCore.Http;
8-
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal;
11+
using Microsoft.AspNetCore.Server.Kestrel.Https;
12+
using Microsoft.AspNetCore.Server.Kestrel.Internal;
913
using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.Internal;
1014
using Microsoft.Extensions.Configuration;
15+
using Microsoft.Extensions.DependencyInjection;
16+
using Microsoft.Extensions.Logging;
1117

1218
namespace Microsoft.AspNetCore.Server.Kestrel.Core
1319
{
@@ -61,17 +67,22 @@ public class KestrelServerOptions
6167
/// Provides a configuration source where endpoints will be loaded from on server start.
6268
/// The default is null.
6369
/// </summary>
64-
public IKestrelConfigurationLoader ConfigurationLoader { get; set; }
70+
public KestrelConfigurationLoader ConfigurationLoader { get; set; }
6571

6672
/// <summary>
6773
/// A default configuration action for all endpoints. Use for Listen, configuration, the default url, and URLs.
6874
/// </summary>
6975
internal Action<ListenOptions> EndpointDefaults { get; set; } = _ => { };
7076

7177
/// <summary>
72-
/// Used to flow settings for connection adapters and other extensions.
78+
/// A default configuration action for all https endpoints.
7379
/// </summary>
74-
public IDictionary<string, object> AdapterData { get; } = new Dictionary<string, object>(0);
80+
internal Action<HttpsConnectionAdapterOptions> HttpsDefaults { get; set; } = _ => { };
81+
82+
/// <summary>
83+
/// The default server certificate for https endpoints. This is applied before HttpsDefaults.
84+
/// </summary>
85+
internal X509Certificate2 DefaultCertificate { get; set; }
7586

7687
/// <summary>
7788
/// Specifies a configuration Action to run for each newly created endpoint. Calling this again will replace
@@ -82,6 +93,53 @@ public void ConfigureEndpointDefaults(Action<ListenOptions> configureOptions)
8293
EndpointDefaults = configureOptions ?? throw new ArgumentNullException(nameof(configureOptions));
8394
}
8495

96+
/// <summary>
97+
/// Specifies a configuration Action to run for each newly created https endpoint. Calling this again will replace
98+
/// the prior action.
99+
/// </summary>
100+
public void ConfigureHttpsDefaults(Action<HttpsConnectionAdapterOptions> configureOptions)
101+
{
102+
HttpsDefaults = configureOptions ?? throw new ArgumentNullException(nameof(configureOptions));
103+
}
104+
105+
public void UseDefaultDeveloperCertificate()
106+
{
107+
var certificateManager = new CertificateManager();
108+
var certificate = certificateManager.ListCertificates(CertificatePurpose.HTTPS, StoreName.My, StoreLocation.CurrentUser, isValid: true)
109+
.FirstOrDefault();
110+
var logger = ApplicationServices?.GetService<ILogger<KestrelServer>>();
111+
if (certificate != null)
112+
{
113+
logger?.LocatedDevelopmentCertificate(certificate);
114+
DefaultCertificate = certificate;
115+
}
116+
else
117+
{
118+
logger?.UnableToLocateDevelopmentCertificate();
119+
}
120+
}
121+
122+
/// <summary>
123+
/// Creates a configuration loader for setting up Kestrel.
124+
/// </summary>
125+
public KestrelConfigurationLoader Configure()
126+
{
127+
var loader = new KestrelConfigurationLoader(this, new ConfigurationBuilder().Build());
128+
ConfigurationLoader = loader;
129+
return loader;
130+
}
131+
132+
/// <summary>
133+
/// Creates a configuration loader for setting up Kestrel that takes an IConfiguration as input.
134+
/// This configuration must be scoped to the configuration section for Kestrel.
135+
/// </summary>
136+
public KestrelConfigurationLoader Configure(IConfiguration config)
137+
{
138+
var loader = new KestrelConfigurationLoader(this, config);
139+
ConfigurationLoader = loader;
140+
return loader;
141+
}
142+
85143
/// <summary>
86144
/// Bind to given IP address and port.
87145
/// </summary>

src/Kestrel.Core/KestrelServerOptionsHttpsExtensions.cs

Lines changed: 0 additions & 53 deletions
This file was deleted.

0 commit comments

Comments
 (0)