-
Notifications
You must be signed in to change notification settings - Fork 9.8k
/
KestrelConfigurationLoader.cs
485 lines (397 loc) · 17.4 KB
/
KestrelConfigurationLoader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal;
using Microsoft.AspNetCore.Server.Kestrel.Https;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Primitives;
namespace Microsoft.AspNetCore.Server.Kestrel;
/// <summary>
/// Configuration loader for Kestrel.
/// </summary>
public class KestrelConfigurationLoader
{
private readonly IHttpsConfigurationService _httpsConfigurationService;
/// <remarks>
/// Non-null only makes sense if <see cref="ReloadOnChange"/> is true.
/// </remarks>
private readonly CertificatePathWatcher? _certificatePathWatcher;
private bool _loaded;
private bool _endpointsToAddProcessed;
// This is not used to trigger reloads but to suppress redundant reloads triggered in other ways
private IChangeToken? _reloadToken;
internal KestrelConfigurationLoader(
KestrelServerOptions options,
IConfiguration configuration,
IHttpsConfigurationService httpsConfigurationService,
CertificatePathWatcher? certificatePathWatcher,
bool reloadOnChange)
{
Options = options;
Configuration = configuration;
ReloadOnChange = reloadOnChange;
ConfigurationReader = new ConfigurationReader(configuration);
_httpsConfigurationService = httpsConfigurationService;
_certificatePathWatcher = certificatePathWatcher;
Debug.Assert(reloadOnChange || (certificatePathWatcher is null), "If reloadOnChange is false, then certificatePathWatcher should be null");
}
/// <summary>
/// Gets the <see cref="KestrelServerOptions"/>.
/// </summary>
public KestrelServerOptions Options { get; }
/// <summary>
/// Gets the application <see cref="IConfiguration"/>.
/// </summary>
public IConfiguration Configuration { get; internal set; } // Setter internal for testing
/// <summary>
/// If <see langword="true" />, Kestrel will dynamically update endpoint bindings when configuration changes.
/// This will only reload endpoints defined in the "Endpoints" section of your Kestrel configuration. Endpoints defined in code will not be reloaded.
/// </summary>
internal bool ReloadOnChange { get; }
private ConfigurationReader ConfigurationReader { get; set; }
private IDictionary<string, Action<EndpointConfiguration>> EndpointConfigurations { get; }
= new Dictionary<string, Action<EndpointConfiguration>>(0, StringComparer.OrdinalIgnoreCase);
// Actions that will be delayed until Load so that they aren't applied if the configuration loader is replaced.
private IList<Action> EndpointsToAdd { get; } = new List<Action>();
private CertificateConfig? DefaultCertificateConfig { get; set; }
internal X509Certificate2? DefaultCertificate { get; set; }
/// <summary>
/// Specifies a configuration Action to run when an endpoint with the given name is loaded from configuration.
/// </summary>
public KestrelConfigurationLoader Endpoint(string name, Action<EndpointConfiguration> configureOptions)
{
ArgumentException.ThrowIfNullOrEmpty(name);
EndpointConfigurations[name] = configureOptions ?? throw new ArgumentNullException(nameof(configureOptions));
return this;
}
/// <summary>
/// Bind to given IP address and port.
/// </summary>
public KestrelConfigurationLoader Endpoint(IPAddress address, int port) => Endpoint(address, port, _ => { });
/// <summary>
/// Bind to given IP address and port.
/// </summary>
public KestrelConfigurationLoader Endpoint(IPAddress address, int port, Action<ListenOptions> configure)
{
ArgumentNullException.ThrowIfNull(address);
return Endpoint(new IPEndPoint(address, port), configure);
}
/// <summary>
/// Bind to given IP endpoint.
/// </summary>
public KestrelConfigurationLoader Endpoint(IPEndPoint endPoint) => Endpoint(endPoint, _ => { });
/// <summary>
/// Bind to given IP address and port.
/// </summary>
public KestrelConfigurationLoader Endpoint(IPEndPoint endPoint, Action<ListenOptions> configure)
{
ArgumentNullException.ThrowIfNull(endPoint);
ArgumentNullException.ThrowIfNull(configure);
EndpointsToAdd.Add(() =>
{
Options.Listen(endPoint, configure);
});
return this;
}
/// <summary>
/// Listens on ::1 and 127.0.0.1 with the given port. Requesting a dynamic port by specifying 0 is not supported
/// for this type of endpoint.
/// </summary>
public KestrelConfigurationLoader LocalhostEndpoint(int port) => LocalhostEndpoint(port, options => { });
/// <summary>
/// Listens on ::1 and 127.0.0.1 with the given port. Requesting a dynamic port by specifying 0 is not supported
/// for this type of endpoint.
/// </summary>
public KestrelConfigurationLoader LocalhostEndpoint(int port, Action<ListenOptions> configure)
{
ArgumentNullException.ThrowIfNull(configure);
EndpointsToAdd.Add(() =>
{
Options.ListenLocalhost(port, configure);
});
return this;
}
/// <summary>
/// Listens on all IPs using IPv6 [::], or IPv4 0.0.0.0 if IPv6 is not supported.
/// </summary>
public KestrelConfigurationLoader AnyIPEndpoint(int port) => AnyIPEndpoint(port, options => { });
/// <summary>
/// Listens on all IPs using IPv6 [::], or IPv4 0.0.0.0 if IPv6 is not supported.
/// </summary>
public KestrelConfigurationLoader AnyIPEndpoint(int port, Action<ListenOptions> configure)
{
ArgumentNullException.ThrowIfNull(configure);
EndpointsToAdd.Add(() =>
{
Options.ListenAnyIP(port, configure);
});
return this;
}
/// <summary>
/// Bind to given Unix domain socket path.
/// </summary>
public KestrelConfigurationLoader UnixSocketEndpoint(string socketPath) => UnixSocketEndpoint(socketPath, _ => { });
/// <summary>
/// Bind to given Unix domain socket path.
/// </summary>
public KestrelConfigurationLoader UnixSocketEndpoint(string socketPath, Action<ListenOptions> configure)
{
ArgumentNullException.ThrowIfNull(socketPath);
if (socketPath.Length == 0 || socketPath[0] != '/')
{
throw new ArgumentException(CoreStrings.UnixSocketPathMustBeAbsolute, nameof(socketPath));
}
ArgumentNullException.ThrowIfNull(configure);
EndpointsToAdd.Add(() =>
{
Options.ListenUnixSocket(socketPath, configure);
});
return this;
}
/// <summary>
/// Open a socket file descriptor.
/// </summary>
public KestrelConfigurationLoader HandleEndpoint(ulong handle) => HandleEndpoint(handle, _ => { });
/// <summary>
/// Open a socket file descriptor.
/// </summary>
public KestrelConfigurationLoader HandleEndpoint(ulong handle, Action<ListenOptions> configure)
{
ArgumentNullException.ThrowIfNull(configure);
EndpointsToAdd.Add(() =>
{
Options.ListenHandle(handle, configure);
});
return this;
}
// Called from KestrelServerOptions.ApplyEndpointDefaults so it applies to even explicit Listen endpoints.
// Does not require a call to Load.
internal void ApplyEndpointDefaults(ListenOptions listenOptions)
{
var defaults = ConfigurationReader.EndpointDefaults;
if (defaults.Protocols.HasValue)
{
listenOptions.Protocols = defaults.Protocols.Value;
}
}
// Called from KestrelServerOptions.ApplyHttpsDefaults so it applies to even explicit Listen endpoints.
// Does not require a call to Load.
internal void ApplyHttpsDefaults(HttpsConnectionAdapterOptions httpsOptions)
{
var defaults = ConfigurationReader.EndpointDefaults;
if (defaults.SslProtocols.HasValue)
{
httpsOptions.SslProtocols = defaults.SslProtocols.Value;
}
if (defaults.ClientCertificateMode.HasValue)
{
httpsOptions.ClientCertificateMode = defaults.ClientCertificateMode.Value;
}
}
// Note: This method is obsolete, but we have to keep it around to avoid breaking the public API.
// Internally, we should always use <see cref="LoadInternal"/>.
/// <summary>
/// Loads the configuration. Does nothing if it has previously been invoked (including implicitly).
/// </summary>
public void Load()
{
if (!_loaded)
{
LoadInternal();
}
// Has its own logic for skipping subsequent invocations
ProcessEndpointsToAdd();
}
/// <remarks>
/// Always prefer this to <see cref="Load"/> since it can be called repeatedly and no-ops if
/// there's a change token indicating nothing has changed.
/// </remarks>
internal void LoadInternal()
{
if (!_loaded || ReloadOnChange)
{
Debug.Assert(!!_loaded || _reloadToken is null, "Shouldn't have a reload token before first load");
Debug.Assert(!!ReloadOnChange || _reloadToken is null, "Shouldn't have a reload token unless reload-on-change is set");
_loaded = true;
if (_reloadToken is null || _reloadToken.HasChanged)
{
// Will update _reloadToken
_ = Reload();
}
}
}
internal void ProcessEndpointsToAdd()
{
if (_endpointsToAddProcessed)
{
return;
}
// Set this *before* invoking delegates, in case one throws
_endpointsToAddProcessed = true;
foreach (var action in EndpointsToAdd)
{
action();
}
}
internal IChangeToken? GetReloadToken()
{
Debug.Assert(ReloadOnChange);
var configToken = Configuration.GetReloadToken();
if (_certificatePathWatcher is null)
{
return configToken;
}
var watcherToken = _certificatePathWatcher.GetChangeToken();
return new CompositeChangeToken(new[] { configToken, watcherToken });
}
// Adds endpoints from config to KestrelServerOptions.ConfigurationBackedListenOptions and configures some other options.
// Any endpoints that were removed from the last time endpoints were loaded are returned.
internal (List<ListenOptions>, List<ListenOptions>) Reload()
{
if (ReloadOnChange)
{
_reloadToken = GetReloadToken();
}
var endpointsToStop = Options.ConfigurationBackedListenOptions.ToList();
var endpointsToStart = new List<ListenOptions>();
var endpointsToReuse = new List<ListenOptions>();
var oldDefaultCertificateConfig = DefaultCertificateConfig;
DefaultCertificateConfig = null;
DefaultCertificate = null;
ConfigurationReader = new ConfigurationReader(Configuration);
if (_httpsConfigurationService.IsInitialized && _httpsConfigurationService.LoadDefaultCertificate(ConfigurationReader) is CertificateAndConfig certPair)
{
DefaultCertificate = certPair.Certificate;
DefaultCertificateConfig = certPair.CertificateConfig;
}
foreach (var endpoint in ConfigurationReader.Endpoints)
{
var listenOptions = AddressBinder.ParseAddress(endpoint.Url, out var https);
if (!https)
{
ConfigurationReader.ThrowIfContainsHttpsOnlyConfiguration(endpoint);
}
Options.ApplyEndpointDefaults(listenOptions);
if (endpoint.Protocols.HasValue)
{
listenOptions.Protocols = endpoint.Protocols.Value;
}
else
{
// Ensure endpoint is reloaded if it used the default protocol and the protocol changed.
// listenOptions.Protocols should already be set to this by ApplyEndpointDefaults.
endpoint.Protocols = ConfigurationReader.EndpointDefaults.Protocols;
}
// Compare to UseHttps(httpsOptions => { })
var httpsOptions = new HttpsConnectionAdapterOptions();
if (https)
{
// Throws an appropriate exception if https configuration isn't enabled
_httpsConfigurationService.ApplyHttpsConfiguration(httpsOptions, endpoint, Options, DefaultCertificateConfig, ConfigurationReader);
}
// Now that defaults have been loaded, we can compare to the currently bound endpoints to see if the config changed.
// There's no reason to rerun an EndpointConfigurations callback if nothing changed.
var matchingBoundEndpoints = new List<ListenOptions>();
foreach (var o in endpointsToStop)
{
if (o.EndpointConfig == endpoint)
{
Debug.Assert(o.EndpointConfig?.Certificate?.FileHasChanged != true, "Preserving an endpoint with file changes");
matchingBoundEndpoints.Add(o);
}
}
if (matchingBoundEndpoints.Count > 0)
{
endpointsToStop.RemoveAll(o => o.EndpointConfig == endpoint);
endpointsToReuse.AddRange(matchingBoundEndpoints);
continue;
}
if (EndpointConfigurations.TryGetValue(endpoint.Name, out var configureEndpoint))
{
var endpointConfig = new EndpointConfiguration(https, listenOptions, httpsOptions, endpoint.ConfigSection);
configureEndpoint(endpointConfig);
}
// EndpointDefaults or configureEndpoint may have added an https adapter.
if (https)
{
// This would throw if it were invoked without https configuration having been enabled,
// but that won't happen because ApplyHttpsConfiguration would throw above under those
// circumstances.
_httpsConfigurationService.UseHttpsWithSni(listenOptions, httpsOptions, endpoint);
}
listenOptions.EndpointConfig = endpoint;
endpointsToStart.Add(listenOptions);
}
// Update ConfigurationBackedListenOptions after everything else has been processed so that
// it's left in a good state (i.e. its former state) if something above throws an exception.
// Note that this isn't foolproof - we could run out of memory or something - but it covers
// exceptions resulting from user misconfiguration (e.g. bad endpoint cert password).
Options.ConfigurationBackedListenOptions.Clear();
Options.ConfigurationBackedListenOptions.AddRange(endpointsToReuse);
Options.ConfigurationBackedListenOptions.AddRange(endpointsToStart);
if (ReloadOnChange && _certificatePathWatcher is not null)
{
var certificateConfigsToRemove = new List<CertificateConfig>();
var certificateConfigsToAdd = new List<CertificateConfig>();
if (DefaultCertificateConfig != oldDefaultCertificateConfig)
{
if (DefaultCertificateConfig?.IsFileCert == true)
{
certificateConfigsToAdd.Add(DefaultCertificateConfig);
}
if (oldDefaultCertificateConfig is not null)
{
certificateConfigsToRemove.Add(oldDefaultCertificateConfig);
}
}
foreach (var endpointToStart in endpointsToStart)
{
var endpointConfig = endpointToStart.EndpointConfig;
if (endpointConfig is null)
{
continue;
}
var certConfig = endpointConfig.Certificate;
if (certConfig?.IsFileCert == true)
{
certificateConfigsToAdd.Add(certConfig);
}
foreach (var sniConfig in endpointConfig.Sni.Values)
{
var sniCertConfig = sniConfig.Certificate;
if (sniCertConfig?.IsFileCert == true)
{
certificateConfigsToAdd.Add(sniCertConfig);
}
}
}
foreach (var endpointToStop in endpointsToStop)
{
var endpointConfig = endpointToStop.EndpointConfig;
if (endpointConfig is null)
{
continue;
}
var certConfig = endpointConfig.Certificate;
if (certConfig?.IsFileCert == true)
{
certificateConfigsToRemove.Add(certConfig);
}
foreach (var sniConfig in endpointConfig.Sni.Values)
{
var sniCertConfig = sniConfig.Certificate;
if (sniCertConfig?.IsFileCert == true)
{
certificateConfigsToRemove.Add(sniCertConfig);
}
}
}
_certificatePathWatcher.UpdateWatches(certificateConfigsToRemove, certificateConfigsToAdd);
}
return (endpointsToStop, endpointsToStart);
}
}