Skip to content

Commit

Permalink
ClientCertificateMode now read from config
Browse files Browse the repository at this point in the history
  • Loading branch information
kunal committed Jan 31, 2020
1 parent a66681f commit e479361
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
27 changes: 23 additions & 4 deletions src/Servers/Kestrel/Core/src/Internal/ConfigurationReader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// 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;
Expand All @@ -15,17 +15,32 @@ internal class ConfigurationReader
private const string EndpointDefaultsKey = "EndpointDefaults";
private const string EndpointsKey = "Endpoints";
private const string UrlKey = "Url";
private const string ClientCertificateModeKey = "ClientCertificateMode";

private IConfiguration _configuration;
private IDictionary<string, CertificateConfig> _certificates;
private IList<EndpointConfig> _endpoints;
private EndpointDefaults _endpointDefaults;

private string _clientCertificateMode;
public ConfigurationReader(IConfiguration configuration)
{
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
}


public string ClientCertificateMode
{
get
{
if (string.IsNullOrEmpty(_clientCertificateMode))
{
ReadClientCertificateMode();
}
return _clientCertificateMode;
}
}


public IDictionary<string, CertificateConfig> Certificates
{
get
Expand Down Expand Up @@ -65,6 +80,10 @@ public IEnumerable<EndpointConfig> Endpoints
}
}

private void ReadClientCertificateMode()
{
_clientCertificateMode = _configuration[ClientCertificateModeKey];
}
private void ReadCertificates()
{
_certificates = new Dictionary<string, CertificateConfig>(0);
Expand Down Expand Up @@ -121,8 +140,8 @@ private void ReadEndpoints()
_endpoints.Add(endpoint);
}
}

private static HttpProtocols? ParseProtocols(string protocols)
private static HttpProtocols? ParseProtocols(string protocols)
{
if (Enum.TryParse<HttpProtocols>(protocols, ignoreCase: true, out var result))
{
Expand Down
10 changes: 10 additions & 0 deletions src/Servers/Kestrel/Core/src/KestrelConfigurationLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ public void Load()
// Specified
httpsOptions.ServerCertificate = LoadCertificate(endpoint.Certificate, endpoint.Name)
?? httpsOptions.ServerCertificate;
httpsOptions.ClientCertificateMode = LoadClientCertificateMode(ConfigurationReader) ?? httpsOptions.ClientCertificateMode;

// Fallback
Options.ApplyDefaultCert(httpsOptions);
Expand Down Expand Up @@ -275,6 +276,15 @@ public void Load()
}
}

private ClientCertificateMode? LoadClientCertificateMode(ConfigurationReader configReader)
{
if (Enum.TryParse<ClientCertificateMode>(configReader.ClientCertificateMode, ignoreCase: true, out var clientCertificateMode))
{
return clientCertificateMode;
}
return null;
}

private void LoadDefaultCert(ConfigurationReader configReader)
{
if (configReader.Certificates.TryGetValue("Default", out var defaultCertConfig))
Expand Down
24 changes: 23 additions & 1 deletion src/Servers/Kestrel/Kestrel/test/ConfigurationReaderTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// 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;
Expand All @@ -12,6 +12,28 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Tests
{
public class ConfigurationReaderTests
{

[Fact]
public void ReadClientCertificateMode_ReturnsValue()
{
var config = new ConfigurationBuilder().AddInMemoryCollection(
new[]
{
new KeyValuePair<string, string>("ClientCertificateMode", "AllowCertificate")
}
).Build();
var reader = new ConfigurationReader(config);
var clientCertificateMode = reader.ClientCertificateMode;
Assert.NotNull(clientCertificateMode);
}
[Fact]
public void ReadClientCertificateModeWhenNoClientCertificateMode_ReturnsNull()
{
var config = new ConfigurationBuilder().AddInMemoryCollection().Build();
var reader = new ConfigurationReader(config);
var clientCertificateMode = reader.ClientCertificateMode;
Assert.Null(clientCertificateMode);
}
[Fact]
public void ReadCertificatesWhenNoCertificatesSection_ReturnsEmptyCollection()
{
Expand Down

0 comments on commit e479361

Please sign in to comment.