Skip to content

Commit cb39145

Browse files
committed
Cleanups
1 parent 00e1b41 commit cb39145

File tree

4 files changed

+58
-56
lines changed

4 files changed

+58
-56
lines changed

src/Components/Web.JS/dist/Release/blazor.server.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,17 +157,17 @@ private void EnsureDefaultCert()
157157

158158
if (DefaultCertificate != null)
159159
{
160-
var (result, message) = CertificateManager.Instance.CheckCertificateState(DefaultCertificate, interactive: false);
161-
if (!result)
160+
var status = CertificateManager.Instance.CheckCertificateState(DefaultCertificate, interactive: false);
161+
if (!status.Result)
162162
{
163163
// Display a warning indicating to the user that a prompt might appear and provide instructions on what to do in that
164164
// case. The underlying implementation of this check is specific to Mac OS and is handled within CheckCertificateState.
165-
logger.DeveloperCertificateFirstRun(message);
165+
logger.DeveloperCertificateFirstRun(status.Message);
166166

167167
// Now that we've displayed a warning in the logs so that the user gets a notification that a prompt might appear, try
168168
// and access the certificate key, which might trigger a prompt.
169-
(result, message) = CertificateManager.Instance.CheckCertificateState(DefaultCertificate, interactive: true);
170-
if (!result)
169+
status = CertificateManager.Instance.CheckCertificateState(DefaultCertificate, interactive: true);
170+
if (!status.Result)
171171
{
172172
logger.BadDeveloperCertificateState();
173173
}

src/Shared/CertificateGeneration/CertificateManager.cs

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -70,48 +70,46 @@ public IList<X509Certificate2> ListCertificates(
7070
var certificates = new List<X509Certificate2>();
7171
try
7272
{
73-
using (var store = new X509Store(storeName, location))
74-
{
75-
store.Open(OpenFlags.ReadOnly);
76-
certificates.AddRange(store.Certificates.OfType<X509Certificate2>());
77-
IEnumerable<X509Certificate2> matchingCertificates = certificates;
78-
matchingCertificates = matchingCertificates
79-
.Where(c => HasOid(c, AspNetHttpsOid));
73+
using var store = new X509Store(storeName, location);
74+
store.Open(OpenFlags.ReadOnly);
75+
certificates.AddRange(store.Certificates.OfType<X509Certificate2>());
76+
IEnumerable<X509Certificate2> matchingCertificates = certificates;
77+
matchingCertificates = matchingCertificates
78+
.Where(c => HasOid(c, AspNetHttpsOid));
8079

81-
Log.DescribeFoundCertificates(ToCertificateDescription(matchingCertificates));
80+
Log.DescribeFoundCertificates(ToCertificateDescription(matchingCertificates));
8281

83-
if (isValid)
84-
{
85-
// Ensure the certificate hasn't expired, has a private key and its exportable
86-
// (for container/unix scenarios).
87-
Log.CheckCertificatesValidity();
88-
var now = DateTimeOffset.Now;
89-
var validCertificates = matchingCertificates
90-
.Where(c => c.NotBefore <= now &&
91-
now <= c.NotAfter &&
92-
(!requireExportable || IsExportable(c))
93-
&& MatchesVersion(c))
94-
.ToArray();
95-
96-
var invalidCertificates = matchingCertificates.Except(validCertificates);
97-
98-
Log.DescribeValidCertificates(ToCertificateDescription(validCertificates));
99-
Log.DescribeInvalidValidCertificates(ToCertificateDescription(invalidCertificates));
100-
101-
matchingCertificates = validCertificates;
102-
}
82+
if (isValid)
83+
{
84+
// Ensure the certificate hasn't expired, has a private key and its exportable
85+
// (for container/unix scenarios).
86+
Log.CheckCertificatesValidity();
87+
var now = DateTimeOffset.Now;
88+
var validCertificates = matchingCertificates
89+
.Where(c => c.NotBefore <= now &&
90+
now <= c.NotAfter &&
91+
(!requireExportable || IsExportable(c))
92+
&& MatchesVersion(c))
93+
.ToArray();
94+
95+
var invalidCertificates = matchingCertificates.Except(validCertificates);
96+
97+
Log.DescribeValidCertificates(ToCertificateDescription(validCertificates));
98+
Log.DescribeInvalidValidCertificates(ToCertificateDescription(invalidCertificates));
99+
100+
matchingCertificates = validCertificates;
101+
}
103102

104-
// We need to enumerate the certificates early to prevent disposing issues.
105-
matchingCertificates = matchingCertificates.ToList();
103+
// We need to enumerate the certificates early to prevent disposing issues.
104+
matchingCertificates = matchingCertificates.ToList();
106105

107-
var certificatesToDispose = certificates.Except(matchingCertificates);
108-
DisposeCertificates(certificatesToDispose);
106+
var certificatesToDispose = certificates.Except(matchingCertificates);
107+
DisposeCertificates(certificatesToDispose);
109108

110-
store.Close();
109+
store.Close();
111110

112-
Log.ListCertificatesEnd();
113-
return (IList<X509Certificate2>)matchingCertificates;
114-
}
111+
Log.ListCertificatesEnd();
112+
return (IList<X509Certificate2>)matchingCertificates;
115113
}
116114
catch (Exception e)
117115
{
@@ -174,6 +172,7 @@ public EnsureCertificateResult EnsureAspNetCoreHttpsDevelopmentCertificate(
174172
if (certificates.Any())
175173
{
176174
certificate = certificates.First();
175+
var failedToFixCertificateState = false;
177176
if (isInteractive)
178177
{
179178
// Skip this step if the command is not interactive,
@@ -193,12 +192,16 @@ public EnsureCertificateResult EnsureAspNetCoreHttpsDevelopmentCertificate(
193192
{
194193
Log.CorrectCertificateStateError(e.ToString());
195194
result = EnsureCertificateResult.FailedToMakeKeyAccessible;
196-
return result;
195+
// We don't return early on this type of failure to allow for tooling to
196+
// export or trust the certificate even in this situation, as that enables
197+
// exporting the certificate to perform any necessary fix with native tooling.
198+
failedToFixCertificateState = true;
197199
}
198200
}
199201
}
200202
}
201-
else
203+
204+
if (!failedToFixCertificateState)
202205
{
203206
Log.ValidCertificatesFound(ToCertificateDescription(certificates));
204207
certificate = certificates.First();
@@ -244,8 +247,10 @@ public EnsureCertificateResult EnsureAspNetCoreHttpsDevelopmentCertificate(
244247
catch (Exception e)
245248
{
246249
Log.CorrectCertificateStateError(e.ToString());
250+
// We don't return early on this type of failure to allow for tooling to
251+
// export or trust the certificate even in this situation, as that enables
252+
// exporting the certificate to perform any necessary fix with native tooling.
247253
result = EnsureCertificateResult.FailedToMakeKeyAccessible;
248-
return result;
249254
}
250255
}
251256
}
@@ -259,7 +264,11 @@ public EnsureCertificateResult EnsureAspNetCoreHttpsDevelopmentCertificate(
259264
catch (Exception e)
260265
{
261266
Log.ExportCertificateError(e.ToString());
262-
result = EnsureCertificateResult.ErrorExportingTheCertificate;
267+
// We don't want to mask the original source of the error here.
268+
result = result != EnsureCertificateResult.Succeeded || result != EnsureCertificateResult.ValidCertificatePresent ?
269+
result :
270+
EnsureCertificateResult.ErrorExportingTheCertificate;
271+
263272
return result;
264273
}
265274
}
@@ -751,12 +760,6 @@ public CheckCertificateStateResult(bool result, string message)
751760
Result = result;
752761
Message = message;
753762
}
754-
755-
public void Deconstruct(out bool result, out string message)
756-
{
757-
result = Result;
758-
message = Message;
759-
}
760763
}
761764

762765
internal enum RemoveLocations

src/Shared/CertificateGeneration/MacOSCertificateManager.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11

22
using System;
3-
using System.Buffers.Text;
43
using System.Collections.Generic;
54
using System.Diagnostics;
65
using System.IO;
@@ -35,7 +34,7 @@ internal class MacOSCertificateManager : CertificateManager
3534
"On macOS or Windows, use 'dotnet dev-certs https --trust' to trust the new certificate.";
3635

3736
public const string KeyNotAccessibleWithoutUserInteraction =
38-
"Kestrel is trying to access the ASP.NET Core developer certificate key. " +
37+
"The application is trying to access the ASP.NET Core developer certificate key. " +
3938
"A prompt might appear to ask for permission to access the key. " +
4039
"When that happens, select 'Always Allow' to grant 'dotnet' access to the certificate key in the future.";
4140

0 commit comments

Comments
 (0)