Skip to content

Commit 3ee8b35

Browse files
committed
Add a unit test to verify default certificate chain is loaded from the certificate path
Fix Formatting
1 parent 91a3373 commit 3ee8b35

File tree

5 files changed

+35
-5
lines changed

5 files changed

+35
-5
lines changed

src/Servers/Kestrel/Core/src/IHttpsConfigurationService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ public CertificateAndConfig(X509Certificate2 certificate, CertificateConfig cert
100100
CertificateChain = [];
101101
}
102102

103-
public CertificateAndConfig(X509Certificate2 certificate, CertificateConfig certificateConfig, X509Certificate2Collection certificateChain){
103+
public CertificateAndConfig(X509Certificate2 certificate, CertificateConfig certificateConfig, X509Certificate2Collection certificateChain)
104+
{
104105
Certificate = certificate;
105106
CertificateConfig = certificateConfig;
106107
CertificateChain = certificateChain;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ internal KestrelConfigurationLoader(
7777
private CertificateConfig? DefaultCertificateConfig { get; set; }
7878
internal X509Certificate2? DefaultCertificate { get; set; }
7979

80-
internal X509Certificate2Collection? DefaultCertificateChain {get; set;}
80+
internal X509Certificate2Collection? DefaultCertificateChain { get; set; }
8181
/// <summary>
8282
/// Specifies a configuration Action to run when an endpoint with the given name is loaded from configuration.
8383
/// </summary>

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,8 @@ internal void ApplyDefaultCertificate(HttpsConnectionAdapterOptions httpsOptions
303303
if (ConfigurationLoader?.DefaultCertificate is X509Certificate2 certificateFromLoader)
304304
{
305305
httpsOptions.ServerCertificate = certificateFromLoader;
306-
if (ConfigurationLoader?.DefaultCertificateChain is X509Certificate2Collection certificateChainFromLoader){
306+
if (ConfigurationLoader?.DefaultCertificateChain is X509Certificate2Collection certificateChainFromLoader)
307+
{
307308
httpsOptions.ServerCertificateChain = certificateChainFromLoader;
308309
}
309310
return;

src/Servers/Kestrel/Core/src/TlsConfigurationLoader.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,9 @@ public ListenOptions UseHttpsWithSni(
131131
var (defaultCert, defaultCertChain) = _certificateConfigLoader.LoadCertificate(defaultCertConfig, "Default");
132132
if (defaultCert != null)
133133
{
134-
if(defaultCertChain != null){
135-
return new CertificateAndConfig(defaultCert,defaultCertConfig,defaultCertChain);
134+
if (defaultCertChain != null)
135+
{
136+
return new CertificateAndConfig(defaultCert, defaultCertConfig, defaultCertChain);
136137
}
137138
return new CertificateAndConfig(defaultCert, defaultCertConfig);
138139
}

src/Servers/Kestrel/Kestrel/test/KestrelConfigurationLoaderTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,33 @@ public void ConfigureDefaultsAppliesToNewConfigureEndpoints()
196196
Assert.False(serverOptions.CodeBackedListenOptions[0].IsTls);
197197
}
198198

199+
[Fact]
200+
public void ConfigureDefaultCertificatePathLoadsChain()
201+
{
202+
var serverOptions = CreateServerOptions();
203+
var testCertPath = TestResources.GetCertPath("leaf.com.crt");
204+
var ran1 = false;
205+
var config = new ConfigurationBuilder().AddInMemoryCollection(new[]
206+
{
207+
new KeyValuePair<string, string>("Endpoints:End1:Url", "https://*:5001"),
208+
new KeyValuePair<string,string>("Certificates:Default:Path",testCertPath)
209+
}).Build();
210+
211+
serverOptions.Configure(config)
212+
.Endpoint("End1", opt =>
213+
{
214+
ran1 = true;
215+
Assert.True(opt.IsHttps);
216+
Assert.NotNull(opt.HttpsOptions.ServerCertificate);
217+
Assert.NotNull(opt.HttpsOptions.ServerCertificateChain);
218+
Assert.Equal(2, opt.HttpsOptions.ServerCertificateChain.Count);
219+
}).Load();
220+
221+
Assert.True(ran1);
222+
223+
Assert.True(serverOptions.ConfigurationBackedListenOptions[0].IsTls);
224+
}
225+
199226
[Fact]
200227
public void ConfigureEndpointDefaultCanEnableHttps()
201228
{

0 commit comments

Comments
 (0)