Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClientCertificateMode now read from config #18759

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this type ClientCertificateMode?

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


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


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: remove the extra newline above and below property.

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

private void ReadClientCertificateMode()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should do the enum parsing instead of LoadClientCertificateMode in KestrelConfigurationLoader.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compare to ParseProtocols

{
_clientCertificateMode = _configuration[ClientCertificateModeKey];
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
}

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Undo this.

{
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
{

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

[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);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
}

[Fact]
public void ReadClientCertificateModeWhenNoClientCertificateMode_ReturnsNull()
{
var config = new ConfigurationBuilder().AddInMemoryCollection().Build();
var reader = new ConfigurationReader(config);
var clientCertificateMode = reader.ClientCertificateMode;
Assert.Null(clientCertificateMode);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
}

[Fact]
public void ReadCertificatesWhenNoCertificatesSection_ReturnsEmptyCollection()
{
Expand Down