Skip to content
This repository was archived by the owner on Nov 21, 2018. It is now read-only.

Commit e03b256

Browse files
author
Cesar Blum Silveira
committed
Show fwlink on certificate errors (#83).
1 parent eb09fc7 commit e03b256

File tree

5 files changed

+44
-31
lines changed

5 files changed

+44
-31
lines changed

samples/AppSettings/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public static void Main(string[] args)
1313
{
1414
using (WebHost.Start(context => context.Response.WriteAsync("Hello, World!")))
1515
{
16-
Console.WriteLine("Running application: Press any key to shutdown...");
16+
Console.WriteLine("Running application: Press any key to shutdown.");
1717
Console.ReadKey();
1818
}
1919
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
6+
namespace Microsoft.AspNetCore
7+
{
8+
internal class CertificateConfigurationException : Exception
9+
{
10+
public CertificateConfigurationException(string message)
11+
: base(message + ". For information on configuring HTTPS see https://go.microsoft.com/fwlink/?linkid=848054.")
12+
{
13+
}
14+
}
15+
}

src/Microsoft.AspNetCore/CertificateLoader.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ private X509Certificate2 LoadSingle(string certificateName)
9999

100100
if (!certificateConfiguration.Exists())
101101
{
102-
throw new InvalidOperationException($"No certificate named {certificateName} found in configuration");
102+
throw new CertificateConfigurationException($"No certificate named {certificateName} found in configuration for the current environment");
103103
}
104104

105105
return LoadSingle(certificateConfiguration);
@@ -119,7 +119,7 @@ private X509Certificate2 LoadSingle(IConfigurationSection certificateConfigurati
119119
certificateSource = new CertificateStoreSource(_certificateStoreLoader);
120120
break;
121121
default:
122-
throw new InvalidOperationException($"Invalid certificate source kind: {sourceKind}");
122+
throw new CertificateConfigurationException($"Invalid certificate source kind: {sourceKind}");
123123
}
124124

125125
certificateConfiguration.Bind(certificateSource);
@@ -163,7 +163,7 @@ public override X509Certificate2 Load()
163163

164164
if (error != null)
165165
{
166-
throw error;
166+
throw new CertificateConfigurationException($"Unable to load certificate from file '{Path}': {error.Message}");
167167
}
168168

169169
return certificate;
@@ -203,7 +203,7 @@ public override X509Certificate2 Load()
203203
{
204204
if (!Enum.TryParse(StoreLocation, ignoreCase: true, result: out StoreLocation storeLocation))
205205
{
206-
throw new InvalidOperationException($"Invalid store location: {StoreLocation}");
206+
throw new CertificateConfigurationException($"Invalid certificate store location: {StoreLocation}");
207207
}
208208

209209
return _certificateStoreLoader.Load(Subject, StoreName, storeLocation, !AllowInvalid);

src/Microsoft.AspNetCore/KestrelServerOptionsSetup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private void BindEndPoint(
6363

6464
if (certificate == null)
6565
{
66-
throw new InvalidOperationException($"Unable to load certificate for endpoint '{endPoint.Key}'");
66+
throw new CertificateConfigurationException($"Unable to load certificate for endpoint '{endPoint.Key}'");
6767
}
6868

6969
listenOptions.UseHttps(certificate);

test/Microsoft.AspNetCore.FunctionalTests/CertificateLoaderTests.cs

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void Throws_SingleCertificateName_File_KeyNotFound()
6464
Mock.Of<ICertificateFileLoader>(),
6565
Mock.Of<ICertificateStoreLoader>());
6666

67-
var exception = Assert.Throws<InvalidOperationException>(() => certificateLoader.Load(configuration.GetSection("TestConfig:Certificate")));
67+
var exception = Assert.Throws<CertificateConfigurationException>(() => certificateLoader.Load(configuration.GetSection("TestConfig:Certificate")));
6868
Assert.Equal("No certificate named Certificate2 found in configuration", exception.Message);
6969
}
7070

@@ -81,19 +81,18 @@ public void Throws_SingleCertificateName_File_FileNotFound()
8181
})
8282
.Build();
8383

84-
var exception = new Exception();
85-
8684
var certificateFileLoader = new Mock<ICertificateFileLoader>();
8785
certificateFileLoader
8886
.Setup(loader => loader.Load("Certificate1.pfx", "Password1", It.IsAny<X509KeyStorageFlags>()))
89-
.Callback(() => throw exception);
87+
.Callback(() => throw new Exception(nameof(Throws_SingleCertificateName_File_FileNotFound)));
9088

9189
var certificateLoader = new CertificateLoader(
9290
configuration.GetSection("Certificates"),
9391
certificateFileLoader.Object,
9492
Mock.Of<ICertificateStoreLoader>());
9593

96-
Assert.Same(exception, Assert.Throws<Exception>(() => certificateLoader.Load(configuration.GetSection("TestConfig:Certificate"))));
94+
var exception = Assert.Throws<CertificateConfigurationException>(() => certificateLoader.Load(configuration.GetSection("TestConfig:Certificate")));
95+
Assert.Equal($"Failed to load certificate from file 'Certificate1.pfx': {nameof(Throws_SingleCertificateName_File_FileNotFound)}", exception.Message);
9796
}
9897

9998
[Fact]
@@ -147,7 +146,7 @@ public void Throws_SingleCertificateName_Store_KeyNotFound()
147146
Mock.Of<ICertificateFileLoader>(),
148147
Mock.Of<ICertificateStoreLoader>());
149148

150-
var exception = Assert.Throws<InvalidOperationException>(() => certificateLoader.Load(configuration.GetSection("TestConfig:Certificate")));
149+
var exception = Assert.Throws<CertificateConfigurationException>(() => certificateLoader.Load(configuration.GetSection("TestConfig:Certificate")));
151150
Assert.Equal("No certificate named Certificate2 found in configuration", exception.Message);
152151
}
153152

@@ -256,7 +255,7 @@ public void Throws_MultipleCertificateNames_File_KeyNotFound(string certificateN
256255
certificateFileLoader.Object,
257256
Mock.Of<ICertificateStoreLoader>());
258257

259-
var exception = Assert.Throws<InvalidOperationException>(() => certificateLoader.Load(configuration.GetSection("TestConfig:Certificate")));
258+
var exception = Assert.Throws<CertificateConfigurationException>(() => certificateLoader.Load(configuration.GetSection("TestConfig:Certificate")));
260259
Assert.Equal("No certificate named NotFound found in configuration", exception.Message);
261260
}
262261

@@ -279,22 +278,22 @@ public void Throws_MultipleCertificateNames_File_FileNotFound(string certificate
279278
.Build();
280279

281280
var certificate1 = new X509Certificate2();
282-
var exception = new Exception();
283281

284282
var certificateFileLoader = new Mock<ICertificateFileLoader>();
285283
certificateFileLoader
286284
.Setup(loader => loader.Load("Certificate1.pfx", "Password1", It.IsAny<X509KeyStorageFlags>()))
287285
.Returns(certificate1);
288286
certificateFileLoader
289287
.Setup(loader => loader.Load("Certificate2.pfx", "Password2", It.IsAny<X509KeyStorageFlags>()))
290-
.Throws(exception);
288+
.Throws(new Exception(nameof(Throws_MultipleCertificateNames_File_FileNotFound)));
291289

292290
var certificateLoader = new CertificateLoader(
293291
configuration.GetSection("Certificates"),
294292
certificateFileLoader.Object,
295293
Mock.Of<ICertificateStoreLoader>());
296294

297-
Assert.Same(exception, Assert.Throws<Exception>(() => certificateLoader.Load(configuration.GetSection("TestConfig:Certificate"))));
295+
var exception = Assert.Throws<CertificateConfigurationException>(() => certificateLoader.Load(configuration.GetSection("TestConfig:Certificate")));
296+
Assert.Equal($"Failed to load certificate from file 'Certificate2.pfx': {nameof(Throws_MultipleCertificateNames_File_FileNotFound)}", exception.Message);
298297
}
299298

300299
[Fact]
@@ -377,7 +376,7 @@ public void Throws_MultipleCertificateNames_Store_KeyNotFound(string certificate
377376
Mock.Of<ICertificateFileLoader>(),
378377
certificateStoreLoader.Object);
379378

380-
var exception = Assert.Throws<InvalidOperationException>(() => certificateLoader.Load(configuration.GetSection("TestConfig:Certificate")));
379+
var exception = Assert.Throws<CertificateConfigurationException>(() => certificateLoader.Load(configuration.GetSection("TestConfig:Certificate")));
381380
Assert.Equal("No certificate named NotFound found in configuration", exception.Message);
382381
}
383382

@@ -509,7 +508,7 @@ public void Throws_MultipleCertificateNames_FileAndStore_KeyNotFound(string cert
509508
certificateFileLoader.Object,
510509
certificateStoreLoader.Object);
511510

512-
var exception = Assert.Throws<InvalidOperationException>(() => certificateLoader.Load(configuration.GetSection("TestConfig:Certificate")));
511+
var exception = Assert.Throws<CertificateConfigurationException>(() => certificateLoader.Load(configuration.GetSection("TestConfig:Certificate")));
513512
Assert.Equal("No certificate named NotFound found in configuration", exception.Message);
514513
}
515514

@@ -532,13 +531,12 @@ public void Throws_MultipleCertificateNames_FileAndStore_FileNotFound(string cer
532531
})
533532
.Build();
534533

535-
var exception = new Exception();
536534
var storeCertificate = new X509Certificate2();
537535

538536
var certificateFileLoader = new Mock<ICertificateFileLoader>();
539537
certificateFileLoader
540538
.Setup(loader => loader.Load("Certificate1.pfx", "Password1", It.IsAny<X509KeyStorageFlags>()))
541-
.Throws(exception);
539+
.Throws(new Exception(nameof(Throws_MultipleCertificateNames_FileAndStore_FileNotFound)));
542540

543541
var certificateStoreLoader = new Mock<ICertificateStoreLoader>();
544542
certificateStoreLoader
@@ -550,7 +548,8 @@ public void Throws_MultipleCertificateNames_FileAndStore_FileNotFound(string cer
550548
certificateFileLoader.Object,
551549
certificateStoreLoader.Object);
552550

553-
Assert.Same(exception, Assert.Throws<Exception>(() => certificateLoader.Load(configuration.GetSection("TestConfig:Certificate"))));
551+
var exception = Assert.Throws<CertificateConfigurationException>(() => certificateLoader.Load(configuration.GetSection("TestConfig:Certificate")));
552+
Assert.Equal($"Failed to load certificate from file 'Certificate1.pfx': {nameof(Throws_MultipleCertificateNames_FileAndStore_FileNotFound)}", exception.Message);
554553
}
555554

556555
[Theory]
@@ -638,19 +637,18 @@ public void Throws_SingleCertificateInline_FileNotFound()
638637
})
639638
.Build();
640639

641-
var exception = new Exception();
642-
643640
var certificateFileLoader = new Mock<ICertificateFileLoader>();
644641
certificateFileLoader
645642
.Setup(loader => loader.Load("Certificate1.pfx", "Password1", It.IsAny<X509KeyStorageFlags>()))
646-
.Throws(exception);
643+
.Throws(new Exception(nameof(Throws_SingleCertificateInline_FileNotFound)));
647644

648645
var certificateLoader = new CertificateLoader(
649646
null,
650647
certificateFileLoader.Object,
651648
Mock.Of<ICertificateStoreLoader>());
652649

653-
Assert.Same(exception, Assert.Throws<Exception>(() => certificateLoader.Load(configuration.GetSection("TestConfig:Certificate"))));
650+
var exception = Assert.Throws<CertificateConfigurationException>(() => certificateLoader.Load(configuration.GetSection("TestConfig:Certificate")));
651+
Assert.Equal($"Failed to load certificate from file 'Certificate1.pfx': {nameof(Throws_SingleCertificateInline_FileNotFound)}", exception.Message);
654652
certificateFileLoader.VerifyAll();
655653
}
656654

@@ -764,22 +762,22 @@ public void Throws_MultipleCertificatesInline_File_FileNotFound()
764762
.Build();
765763

766764
var certificate1 = new X509Certificate2();
767-
var exception = new Exception();
768765

769766
var certificateFileLoader = new Mock<ICertificateFileLoader>();
770767
certificateFileLoader
771768
.Setup(loader => loader.Load("Certificate1.pfx", "Password1", It.IsAny<X509KeyStorageFlags>()))
772769
.Returns(certificate1);
773770
certificateFileLoader
774771
.Setup(loader => loader.Load("Certificate2.pfx", "Password2", It.IsAny<X509KeyStorageFlags>()))
775-
.Throws(exception);
772+
.Throws(new Exception(nameof(Throws_MultipleCertificatesInline_File_FileNotFound)));
776773

777774
var certificateLoader = new CertificateLoader(
778775
null,
779776
certificateFileLoader.Object,
780777
Mock.Of<ICertificateStoreLoader>());
781778

782-
Assert.Same(exception, Assert.Throws<Exception>(() => certificateLoader.Load(configuration.GetSection("TestConfig:Certificates"))));
779+
var exception = Assert.Throws<CertificateConfigurationException>(() => certificateLoader.Load(configuration.GetSection("TestConfig:Certificates")));
780+
Assert.Equal($"Failed to load certificate from file 'Certificate2.pfx': {nameof(Throws_MultipleCertificatesInline_File_FileNotFound)}", exception.Message);
783781
}
784782

785783
[Fact]
@@ -937,13 +935,12 @@ public void Throws_MultipleCertificatesInline_FileAndStore_FileNotFound()
937935
})
938936
.Build();
939937

940-
var exception = new Exception();
941938
var certificate = new X509Certificate2();
942939

943940
var certificateFileLoader = new Mock<ICertificateFileLoader>();
944941
certificateFileLoader
945942
.Setup(loader => loader.Load("Certificate1.pfx", "Password1", It.IsAny<X509KeyStorageFlags>()))
946-
.Throws(exception);
943+
.Throws(new Exception(nameof(Throws_MultipleCertificatesInline_FileAndStore_FileNotFound)));
947944

948945
var certificateStoreLoader = new Mock<ICertificateStoreLoader>();
949946
certificateStoreLoader
@@ -955,7 +952,8 @@ public void Throws_MultipleCertificatesInline_FileAndStore_FileNotFound()
955952
certificateFileLoader.Object,
956953
certificateStoreLoader.Object);
957954

958-
Assert.Same(exception, Assert.Throws<Exception>(() => certificateLoader.Load(configuration.GetSection("TestConfig:Certificates"))));
955+
var exception = Assert.Throws<CertificateConfigurationException>(() => certificateLoader.Load(configuration.GetSection("TestConfig:Certificates")));
956+
Assert.Equal($"Failed to load certificate from file 'Certificate1.pfx': {nameof(Throws_MultipleCertificatesInline_FileAndStore_FileNotFound)}", exception.Message);
959957
}
960958

961959
[Fact]

0 commit comments

Comments
 (0)