This repository was archived by the owner on Dec 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 521
Add a Configuration package and tests. #1878
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a98aa80
#1875 Add a Configuration package and tests.
Tratcher 885d3f1
Work around ConfigureDefaultOptions bug. Fix file name.
Tratcher 7008933
Squash .Configuration back into .Kestrel
Tratcher eda5453
PrivateAssets
Tratcher 0335de8
Resx
Tratcher 96327e3
Tests
Tratcher 969cc41
More resx
Tratcher e274ce1
Change options registration
Tratcher 79da5e7
Fix DI
Tratcher ee9a357
Remove extra resx build task
Tratcher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
src/Microsoft.AspNetCore.Server.Kestrel/Internal/ConfigureDefaultKestrelServerOptions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Security.Cryptography.X509Certificates; | ||
using Microsoft.AspNetCore.Certificates.Configuration; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Server.Kestrel.Core; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.Extensions.Options.Infrastructure; | ||
|
||
namespace Microsoft.AspNetCore.Server.Kestrel.Internal | ||
{ | ||
public class ConfigureDefaultKestrelServerOptions : ConfigureDefaultOptions<KestrelServerOptions> | ||
{ | ||
private const string DefaultCertificateSubjectName = "CN=localhost"; | ||
private const string DevelopmentSSLCertificateName = "localhost"; | ||
|
||
private readonly IServiceProvider _services; | ||
private readonly IHostingEnvironment _hostingEnvironment; | ||
private readonly IConfiguration _configurationRoot; | ||
private readonly ILoggerFactory _loggerFactory; | ||
|
||
public ConfigureDefaultKestrelServerOptions( | ||
IServiceProvider services, | ||
IHostingEnvironment hostingEnvironment, | ||
IConfiguration configurationRoot, | ||
ILoggerFactory loggerFactory) | ||
{ | ||
_services = services; | ||
_hostingEnvironment = hostingEnvironment; | ||
_configurationRoot = configurationRoot; | ||
_loggerFactory = loggerFactory; | ||
} | ||
|
||
public override void Configure(string name, KestrelServerOptions options) | ||
{ | ||
// Don't assume KestrelServerOptionsSetup has already set the services. Needed for UseHttps. | ||
options.ApplicationServices = _services; | ||
BindConfiguration(options); | ||
} | ||
|
||
private void BindConfiguration(KestrelServerOptions options) | ||
{ | ||
var certificateLoader = new CertificateLoader(_configurationRoot.GetSection("Certificates"), _loggerFactory, _hostingEnvironment.EnvironmentName); | ||
|
||
foreach (var endPoint in _configurationRoot.GetSection("Microsoft:AspNetCore:Server:Kestrel:EndPoints").GetChildren()) | ||
{ | ||
BindEndPoint(options, endPoint, certificateLoader); | ||
} | ||
} | ||
|
||
private void BindEndPoint( | ||
KestrelServerOptions options, | ||
IConfigurationSection endPoint, | ||
CertificateLoader certificateLoader) | ||
{ | ||
var configAddress = endPoint.GetValue<string>("Address"); | ||
var configPort = endPoint.GetValue<string>("Port"); | ||
|
||
if (!IPAddress.TryParse(configAddress, out var address)) | ||
{ | ||
throw new InvalidOperationException(KestrelStrings.FormatInvalidIp(configAddress)); | ||
} | ||
|
||
if (!int.TryParse(configPort, out var port)) | ||
{ | ||
throw new InvalidOperationException(KestrelStrings.FormatInvalidPort(configPort)); | ||
} | ||
|
||
options.Listen(address, port, listenOptions => | ||
{ | ||
var certificateConfig = endPoint.GetSection("Certificate"); | ||
X509Certificate2 certificate = null; | ||
if (certificateConfig.Exists()) | ||
{ | ||
try | ||
{ | ||
try | ||
{ | ||
certificate = certificateLoader.Load(certificateConfig).FirstOrDefault(); | ||
} | ||
catch (KeyNotFoundException) when (certificateConfig.Value.Equals(DevelopmentSSLCertificateName, StringComparison.Ordinal) && _hostingEnvironment.IsDevelopment()) | ||
{ | ||
var storeLoader = new CertificateStoreLoader(); | ||
certificate = storeLoader.Load(DefaultCertificateSubjectName, "My", StoreLocation.CurrentUser, validOnly: false) ?? | ||
storeLoader.Load(DefaultCertificateSubjectName, "My", StoreLocation.LocalMachine, validOnly: false); | ||
|
||
if (certificate == null) | ||
{ | ||
var logger = _loggerFactory.CreateLogger("Microsoft.AspNetCore.Server.Kestrel.KestrelServerConfigureOptions"); | ||
logger.LogError(KestrelStrings.NoCertFoundLog); | ||
} | ||
} | ||
|
||
if (certificate == null) | ||
{ | ||
throw new InvalidOperationException(KestrelStrings.FormatNoCertForEndpoint(endPoint.Key)); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
throw new InvalidOperationException(KestrelStrings.UnableToConfigureHttps, ex); | ||
} | ||
|
||
listenOptions.UseHttps(certificate); | ||
} | ||
}); | ||
} | ||
} | ||
} |
135 changes: 135 additions & 0 deletions
135
src/Microsoft.AspNetCore.Server.Kestrel/KestrelStrings.resx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<root> | ||
<!-- | ||
Microsoft ResX Schema | ||
|
||
Version 2.0 | ||
|
||
The primary goals of this format is to allow a simple XML format | ||
that is mostly human readable. The generation and parsing of the | ||
various data types are done through the TypeConverter classes | ||
associated with the data types. | ||
|
||
Example: | ||
|
||
... ado.net/XML headers & schema ... | ||
<resheader name="resmimetype">text/microsoft-resx</resheader> | ||
<resheader name="version">2.0</resheader> | ||
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader> | ||
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader> | ||
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data> | ||
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data> | ||
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64"> | ||
<value>[base64 mime encoded serialized .NET Framework object]</value> | ||
</data> | ||
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64"> | ||
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value> | ||
<comment>This is a comment</comment> | ||
</data> | ||
|
||
There are any number of "resheader" rows that contain simple | ||
name/value pairs. | ||
|
||
Each data row contains a name, and value. The row also contains a | ||
type or mimetype. Type corresponds to a .NET class that support | ||
text/value conversion through the TypeConverter architecture. | ||
Classes that don't support this are serialized and stored with the | ||
mimetype set. | ||
|
||
The mimetype is used for serialized objects, and tells the | ||
ResXResourceReader how to depersist the object. This is currently not | ||
extensible. For a given mimetype the value must be set accordingly: | ||
|
||
Note - application/x-microsoft.net.object.binary.base64 is the format | ||
that the ResXResourceWriter will generate, however the reader can | ||
read any of the formats listed below. | ||
|
||
mimetype: application/x-microsoft.net.object.binary.base64 | ||
value : The object must be serialized with | ||
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter | ||
: and then encoded with base64 encoding. | ||
|
||
mimetype: application/x-microsoft.net.object.soap.base64 | ||
value : The object must be serialized with | ||
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter | ||
: and then encoded with base64 encoding. | ||
|
||
mimetype: application/x-microsoft.net.object.bytearray.base64 | ||
value : The object must be serialized into a byte array | ||
: using a System.ComponentModel.TypeConverter | ||
: and then encoded with base64 encoding. | ||
--> | ||
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> | ||
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" /> | ||
<xsd:element name="root" msdata:IsDataSet="true"> | ||
<xsd:complexType> | ||
<xsd:choice maxOccurs="unbounded"> | ||
<xsd:element name="metadata"> | ||
<xsd:complexType> | ||
<xsd:sequence> | ||
<xsd:element name="value" type="xsd:string" minOccurs="0" /> | ||
</xsd:sequence> | ||
<xsd:attribute name="name" use="required" type="xsd:string" /> | ||
<xsd:attribute name="type" type="xsd:string" /> | ||
<xsd:attribute name="mimetype" type="xsd:string" /> | ||
<xsd:attribute ref="xml:space" /> | ||
</xsd:complexType> | ||
</xsd:element> | ||
<xsd:element name="assembly"> | ||
<xsd:complexType> | ||
<xsd:attribute name="alias" type="xsd:string" /> | ||
<xsd:attribute name="name" type="xsd:string" /> | ||
</xsd:complexType> | ||
</xsd:element> | ||
<xsd:element name="data"> | ||
<xsd:complexType> | ||
<xsd:sequence> | ||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" /> | ||
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" /> | ||
</xsd:sequence> | ||
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" /> | ||
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" /> | ||
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" /> | ||
<xsd:attribute ref="xml:space" /> | ||
</xsd:complexType> | ||
</xsd:element> | ||
<xsd:element name="resheader"> | ||
<xsd:complexType> | ||
<xsd:sequence> | ||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" /> | ||
</xsd:sequence> | ||
<xsd:attribute name="name" type="xsd:string" use="required" /> | ||
</xsd:complexType> | ||
</xsd:element> | ||
</xsd:choice> | ||
</xsd:complexType> | ||
</xsd:element> | ||
</xsd:schema> | ||
<resheader name="resmimetype"> | ||
<value>text/microsoft-resx</value> | ||
</resheader> | ||
<resheader name="version"> | ||
<value>2.0</value> | ||
</resheader> | ||
<resheader name="reader"> | ||
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | ||
</resheader> | ||
<resheader name="writer"> | ||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | ||
</resheader> | ||
<data name="InvalidIp" xml:space="preserve"> | ||
<value>Invalid IP address in configuration: {configAddress}.</value> | ||
</data> | ||
<data name="InvalidPort" xml:space="preserve"> | ||
<value>Invalid port in configuration: {configPort}.</value> | ||
</data> | ||
<data name="NoCertForEndpoint" xml:space="preserve"> | ||
<value>No certificate found for endpoint {endPoint}.</value> | ||
</data> | ||
<data name="UnableToConfigureHttps" xml:space="preserve"> | ||
<value>Unable to configure HTTPS endpoint. For information on configuring HTTPS see https://go.microsoft.com/fwlink/?linkid=848054.</value> | ||
</data> | ||
<data name="NoCertFoundLog" xml:space="preserve"> | ||
<value>No HTTPS certificate was found for development. For information on configuring HTTPS see https://go.microsoft.com/fwlink/?linkid=848054.</value> | ||
</data> | ||
</root> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The cert loader sources package requires it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is the ifdef?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here: https://github.com/aspnet/Hosting/pull/1095/files#diff-b877901ee14c476e81bad19896b94775R182
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, what happens on .NET Framework?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It uses the netstandard version that doesn't have this new feature
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which does what?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just move this to the Core project. Currently only the "meta" project only contains WebHostBuilderKestrelExtensions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nvm. I just realized this depends on both the Core and Https projects.