-
Notifications
You must be signed in to change notification settings - Fork 986
Support DCP dev cert TLS files #17449
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5314bc0
Support DCP dev cert TLS files
danegsta 9e27e37
Enable DCP dev cert TLS by default
danegsta 9658b11
Use async FileStream for DCP TLS files
danegsta b2a184c
Use dev certificate cache for DCP TLS files
danegsta e78584b
Refactor DeveloperCertificateService cache into shared helper
danegsta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |
| using System.Text; | ||
| using Aspire.Dashboard.Utils; | ||
| using Aspire.Hosting.ApplicationModel; | ||
| using Aspire.Hosting.Diagnostics; | ||
| using Aspire.Hosting.Dcp.Process; | ||
| using Aspire.Hosting.Resources; | ||
| using Aspire.Shared; | ||
|
|
@@ -38,6 +39,8 @@ internal sealed class DcpHost | |
| private readonly IConfiguration _configuration; | ||
| private readonly CancellationTokenSource _shutdownCts = new(); | ||
| private string? _dcpTlsCertThumbprint; | ||
| private string? _dcpTlsCertFile; | ||
| private string? _dcpTlsKeyFile; | ||
| private Task? _logProcessorTask; | ||
|
|
||
| // These environment variables should never be inherited by DCP from the app host. | ||
|
|
@@ -176,27 +179,25 @@ internal async Task EnsureDevelopmentCertificateTrustAsync(CancellationToken can | |
| } | ||
| } | ||
|
|
||
| internal Task PrepareDcpTlsCertificateAsync(CancellationToken cancellationToken) | ||
| internal async Task PrepareDcpTlsCertificateAsync(CancellationToken cancellationToken) | ||
| { | ||
| cancellationToken.ThrowIfCancellationRequested(); | ||
|
|
||
| // Using the ASP.NET dev cert for DCP TLS is opt-in; by default DCP uses its own ephemeral certificate. | ||
| if (!_configuration.GetBool(KnownConfigNames.DcpDeveloperCertificate, defaultValue: false)) | ||
| // DCP uses the ASP.NET dev cert for TLS by default. The environment variable remains | ||
| // available as an opt-out if users need DCP's ephemeral certificate behavior. | ||
| if (!_configuration.GetBool(KnownConfigNames.DcpDeveloperCertificate, defaultValue: true)) | ||
| { | ||
| return Task.CompletedTask; | ||
| return; | ||
| } | ||
|
|
||
| if (!OperatingSystem.IsWindows()) | ||
| { | ||
| _logger.LogWarning("Developer certificate thumbprint configuration is only supported on Windows. DCP will use its default certificate."); | ||
| return Task.CompletedTask; | ||
| } | ||
| using var activity = ProfilingTelemetry.StartDcpPrepareTlsCertificate(_configuration); | ||
|
|
||
| // Check if we have a trusted developer certificate with a private key available | ||
| var certificates = _developerCertificateService.Certificates; | ||
| if (certificates.Count == 0) | ||
| { | ||
| return Task.CompletedTask; | ||
| activity.SetDcpTlsCertificateResult(ProfilingTelemetry.Values.DcpTlsCertificateResultNoCertificate); | ||
| return; | ||
| } | ||
|
|
||
| // Use the first (latest/best) certificate that has a private key | ||
|
|
@@ -212,20 +213,51 @@ internal Task PrepareDcpTlsCertificateAsync(CancellationToken cancellationToken) | |
|
|
||
| if (certificate is null) | ||
| { | ||
| return Task.CompletedTask; | ||
| activity.SetDcpTlsCertificateResult(ProfilingTelemetry.Values.DcpTlsCertificateResultNoPrivateKeyCertificate); | ||
| return; | ||
| } | ||
|
|
||
| var thumbprint = certificate.Thumbprint; | ||
| if (string.IsNullOrWhiteSpace(thumbprint)) | ||
| { | ||
| _logger.LogWarning("Failed to read the developer certificate thumbprint. DCP will use its default certificate."); | ||
| return Task.CompletedTask; | ||
| activity.SetDcpTlsCertificateResult(ProfilingTelemetry.Values.DcpTlsCertificateResultMissingThumbprint); | ||
| return; | ||
| } | ||
|
|
||
| _dcpTlsCertThumbprint = thumbprint; | ||
| _logger.LogDebug("Prepared DCP TLS certificate thumbprint {Thumbprint}.", thumbprint); | ||
|
|
||
| return Task.CompletedTask; | ||
| if (OperatingSystem.IsWindows()) | ||
| { | ||
| activity.SetDcpTlsCertificateResult( | ||
| ProfilingTelemetry.Values.DcpTlsCertificateResultPrepared, | ||
| ProfilingTelemetry.Values.DcpTlsCertificateModeThumbprint, | ||
| prepared: true); | ||
| _logger.LogDebug("Prepared DCP TLS certificate thumbprint {Thumbprint}.", thumbprint); | ||
| return; | ||
| } | ||
|
|
||
| var (certificatePath, keyPath, cachedThumbprint) = await DeveloperCertificateService.GetCachedCertificateFilePathsAsync( | ||
| certificate, | ||
| password: null, | ||
| cancellationToken).ConfigureAwait(false); | ||
|
|
||
| if (certificatePath is null || keyPath is null || cachedThumbprint is null) | ||
| { | ||
| _logger.LogWarning("Failed to cache the developer certificate files. DCP will use its default certificate."); | ||
| _dcpTlsCertThumbprint = null; | ||
| activity.SetDcpTlsCertificateResult(ProfilingTelemetry.Values.DcpTlsCertificateResultNoCertificate); | ||
| return; | ||
| } | ||
|
|
||
| _dcpTlsCertThumbprint = cachedThumbprint; | ||
| _dcpTlsCertFile = certificatePath; | ||
| _dcpTlsKeyFile = keyPath; | ||
| activity.SetDcpTlsCertificateResult( | ||
| ProfilingTelemetry.Values.DcpTlsCertificateResultPrepared, | ||
| ProfilingTelemetry.Values.DcpTlsCertificateModeFiles, | ||
| prepared: true); | ||
| _logger.LogDebug("Prepared DCP TLS certificate files for thumbprint {Thumbprint}.", thumbprint); | ||
| } | ||
|
|
||
| public async Task StopAsync() | ||
|
|
@@ -280,6 +312,11 @@ public ProcessSpec CreateDcpProcessSpec(Locations locations) | |
| arguments += $" --tls-cert-thumbprint \"{_dcpTlsCertThumbprint}\""; | ||
| } | ||
|
|
||
| if (!string.IsNullOrWhiteSpace(_dcpTlsCertFile) && !string.IsNullOrWhiteSpace(_dcpTlsKeyFile)) | ||
| { | ||
| arguments += $" --tls-cert-file \"{_dcpTlsCertFile}\" --tls-key-file \"{_dcpTlsKeyFile}\""; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not for this PR but we should update this to use arguments list in the future. I added that to process spec. |
||
| } | ||
|
|
||
| var dcpProcessSpec = new ProcessSpec(dcpExePath) | ||
| { | ||
| WorkingDirectory = Directory.GetCurrentDirectory(), | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.