Skip to content

Commit 36e2106

Browse files
committed
Add support for UseHttps() without explicit opt-in
1 parent 8041dd1 commit 36e2106

File tree

4 files changed

+44
-10
lines changed

4 files changed

+44
-10
lines changed

src/Servers/Kestrel/Core/src/Internal/KestrelServerOptionsSetup.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,14 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal;
88
internal sealed class KestrelServerOptionsSetup : IConfigureOptions<KestrelServerOptions>
99
{
1010
private readonly IServiceProvider _services;
11-
private readonly bool _disableDefaultCertificate;
1211

1312
public KestrelServerOptionsSetup(IServiceProvider services)
14-
{
15-
_services = services;
16-
_disableDefaultCertificate = true;
17-
}
18-
19-
public KestrelServerOptionsSetup(IServiceProvider services, ITlsConfigurationLoader _)
2013
{
2114
_services = services;
2215
}
2316

2417
public void Configure(KestrelServerOptions options)
2518
{
2619
options.ApplicationServices = _services;
27-
options.DisableDefaultCertificate = _disableDefaultCertificate;
2820
}
2921
}

src/Servers/Kestrel/Core/src/KestrelConfigurationLoader.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
using Microsoft.AspNetCore.Server.Kestrel.Core;
88
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal;
99
using Microsoft.AspNetCore.Server.Kestrel.Https;
10+
using Microsoft.AspNetCore.Server.Kestrel.Https.Internal;
1011
using Microsoft.Extensions.Configuration;
12+
using Microsoft.Extensions.Hosting;
13+
using Microsoft.Extensions.Logging;
1114

1215
namespace Microsoft.AspNetCore.Server.Kestrel;
1316

@@ -16,7 +19,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel;
1619
/// </summary>
1720
public class KestrelConfigurationLoader
1821
{
19-
private readonly ITlsConfigurationLoader? _tlsLoader;
22+
private ITlsConfigurationLoader? _tlsLoader;
2023
private bool _loaded;
2124

2225
internal KestrelConfigurationLoader(
@@ -33,6 +36,24 @@ internal KestrelConfigurationLoader(
3336
_tlsLoader = tlsLoader;
3437
}
3538

39+
internal bool IsTlsConfigurationLoadingEnabled => _tlsLoader is not null;
40+
41+
internal void EnableTlsConfigurationLoading(
42+
IHostEnvironment hostEnvironment,
43+
ILogger<KestrelServer> serverLogger,
44+
ILogger<HttpsConnectionMiddleware> httpsLogger)
45+
{
46+
if (_tlsLoader is null)
47+
{
48+
_tlsLoader = new TlsConfigurationLoader(hostEnvironment, serverLogger, httpsLogger);
49+
50+
if (_loaded)
51+
{
52+
Reload();
53+
}
54+
}
55+
}
56+
3657
/// <summary>
3758
/// Gets the <see cref="KestrelServerOptions"/>.
3859
/// </summary>

src/Servers/Kestrel/Core/src/KestrelServerOptions.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ internal void ApplyHttpsDefaults(HttpsConnectionAdapterOptions httpsOptions)
262262

263263
internal void ApplyDefaultCertificate(HttpsConnectionAdapterOptions httpsOptions)
264264
{
265-
if (DisableDefaultCertificate)
265+
if (ConfigurationLoader is not null && !ConfigurationLoader.IsTlsConfigurationLoadingEnabled)
266266
{
267267
throw new InvalidOperationException("You need to call UseHttpsConfiguration"); // TODO (acasey): message
268268
}
@@ -414,6 +414,24 @@ public KestrelConfigurationLoader Configure(IConfiguration config, bool reloadOn
414414
return loader;
415415
}
416416

417+
internal void EnableTlsConfigurationLoading()
418+
{
419+
// TODO (acasey): keep it around in case someone calls Configure later?
420+
if (ConfigurationLoader is not null)
421+
{
422+
if (ApplicationServices is null)
423+
{
424+
throw new InvalidOperationException($"{nameof(ApplicationServices)} must not be null. This is normally set automatically via {nameof(IConfigureOptions<KestrelServerOptions>)}.");
425+
}
426+
427+
var hostEnvironment = ApplicationServices.GetRequiredService<IHostEnvironment>();
428+
var serverLogger = ApplicationServices.GetRequiredService<ILogger<KestrelServer>>();
429+
var httpsLogger = ApplicationServices.GetRequiredService<ILogger<HttpsConnectionMiddleware>>();
430+
431+
ConfigurationLoader.EnableTlsConfigurationLoading(hostEnvironment, serverLogger, httpsLogger);
432+
}
433+
}
434+
417435
/// <summary>
418436
/// Bind to the given IP address and port.
419437
/// </summary>

src/Servers/Kestrel/Core/src/ListenOptionsHttpsExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ public static ListenOptions UseHttps(this ListenOptions listenOptions, Action<Ht
163163
{
164164
ArgumentNullException.ThrowIfNull(configureOptions);
165165

166+
// TODO (acasey): apply to other overloads?
167+
listenOptions.KestrelServerOptions.EnableTlsConfigurationLoading();
168+
166169
var options = new HttpsConnectionAdapterOptions();
167170

168171
// This will throw if there are configured defaults and defaults are not supported (i.e. in slim scenarios)

0 commit comments

Comments
 (0)