Skip to content

Commit 25961cf

Browse files
aspire-repo-bot[bot]danegstaCopilot
authored
[release/13.2] Updated to cache pfx dev certs on Windows and Linux to avoid binary level changes between runs (#15774)
* Updated to cache pfx dev certs on Windows and Linux to avoid binary changes in persistent container usage * Fix garbled line * Update src/Aspire.Hosting/DeveloperCertificateService.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: David Negstad <David.Negstad@microsoft.com> Co-authored-by: David Negstad <50252651+danegsta@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 46a37df commit 25961cf

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

src/Aspire.Hosting/DeveloperCertificateService.cs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ private static IEnumerable<X509Certificate2> FindDevCertificates(X509Store store
193193
}
194194

195195
// Well-known location on disk where dev-cert key material is cached on macOS.
196-
private static readonly string s_macOSUserDevCertificateLocation = Path.Combine(
196+
private static readonly string s_userDevCertificateLocation = Path.Combine(
197197
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".aspire", "dev-certs", "https");
198198

199199
private static readonly SemaphoreSlim s_certificateCacheSemaphore = new(1, 1);
@@ -257,13 +257,12 @@ private static IEnumerable<X509Certificate2> FindDevCertificates(X509Store store
257257
CancellationToken cancellationToken)
258258
{
259259
char[]? pemKey = null;
260-
var keyFileName = Path.Join(s_macOSUserDevCertificateLocation, $"{lookup}.key");
260+
var keyFileName = Path.Join(s_userDevCertificateLocation, $"{lookup}.key");
261261

262+
// We only cache PEM certificates on MacOS to avoid repeated keychain prompts.
263+
// There's no concern of binary differences for PEM certs with persistent containers.
262264
if (OperatingSystem.IsMacOS() && certificate.IsAspNetCoreDevelopmentCertificate())
263265
{
264-
// On macOS, we cache development certificate key material to avoid triggering repeated
265-
// keychain prompts when referencing the development certificate key. We don't do this
266-
// for other OSes or other certificates.
267266
try
268267
{
269268
if (File.Exists(keyFileName))
@@ -316,7 +315,7 @@ private static IEnumerable<X509Certificate2> FindDevCertificates(X509Store store
316315
// On macOS, cache the development certificate key material
317316
try
318317
{
319-
Directory.CreateDirectory(s_macOSUserDevCertificateLocation, UnixFileMode.UserExecute | UnixFileMode.UserWrite | UnixFileMode.UserRead);
318+
Directory.CreateDirectory(s_userDevCertificateLocation, UnixFileMode.UserExecute | UnixFileMode.UserWrite | UnixFileMode.UserRead);
320319

321320
await File.WriteAllTextAsync(keyFileName, new string(pemKey), cancellationToken).ConfigureAwait(false);
322321
}
@@ -336,12 +335,12 @@ private static IEnumerable<X509Certificate2> FindDevCertificates(X509Store store
336335
string lookup)
337336
{
338337
byte[]? pfxBytes = null;
339-
var pfxFileName = Path.Join(s_macOSUserDevCertificateLocation, $"{lookup}.pfx");
338+
var pfxFileName = Path.Join(s_userDevCertificateLocation, $"{lookup}.pfx");
340339

341-
if (OperatingSystem.IsMacOS() && certificate.IsAspNetCoreDevelopmentCertificate())
340+
// We cache PFX dev certs for all OSes to ensure consistent binary output for persistent containers
341+
// in addition to avoiding repeated keychain prompts on MacOS.
342+
if (certificate.IsAspNetCoreDevelopmentCertificate())
342343
{
343-
// On macOS, we cache development certificate key material to avoid triggering repeated
344-
// keychain prompts when referencing the development certificate key.
345344
try
346345
{
347346
if (File.Exists(pfxFileName))
@@ -367,11 +366,18 @@ private static IEnumerable<X509Certificate2> FindDevCertificates(X509Store store
367366
{
368367
pfxBytes = certificate.Export(X509ContentType.Pfx, password);
369368

370-
if (pfxBytes is not null && OperatingSystem.IsMacOS() && certificate.IsAspNetCoreDevelopmentCertificate())
369+
if (pfxBytes is not null && certificate.IsAspNetCoreDevelopmentCertificate())
371370
{
372371
try
373372
{
374-
Directory.CreateDirectory(s_macOSUserDevCertificateLocation, UnixFileMode.UserExecute | UnixFileMode.UserWrite | UnixFileMode.UserRead);
373+
if (OperatingSystem.IsWindows())
374+
{
375+
Directory.CreateDirectory(s_userDevCertificateLocation);
376+
}
377+
else
378+
{
379+
Directory.CreateDirectory(s_userDevCertificateLocation, UnixFileMode.UserExecute | UnixFileMode.UserWrite | UnixFileMode.UserRead);
380+
}
375381

376382
File.WriteAllBytes(pfxFileName, pfxBytes);
377383
}

0 commit comments

Comments
 (0)