Skip to content
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

Improvements to RunWithHttpsDevCert extensions #667

Open
wants to merge 29 commits into
base: main
Choose a base branch
from

Conversation

DamianEdwards
Copy link
Member

@DamianEdwards DamianEdwards commented Jan 28, 2025

Changes the dev cert extensions to put the exported cert files in /obj/aspire and falls back to user temp when /obj isn't known.

Also added support for using the dev cert in PFX file format with a password and updated the Node sample to use that. Also can now separately export the dev cert for scenarios where you need the PEM file for trusting (Node.js can listen on TLS using a PFX file but adding extra trusted certs requires a PEM file). The password is generated via the generated parameters helpers so it's stored in user secrets.

Updated the Metrics sample to account for changes.

Fixes #666

@DamianEdwards DamianEdwards marked this pull request as draft January 28, 2025 19:52

private static readonly char[] _invalidPathChars = Path.GetInvalidPathChars();

private static string Sanitize(string name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assuming this is not hot and you're already using Linq here how about making this a one liner

return new string(name.Select(c => Path.GetInvalidPathChars().Contains(c) ? '_' : c).ToArray());

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It coulde be LINQ, but this is derived from the existing method we have in Aspire.Hosting for volume name sanitization, so I'm inclined to just keep it this way for now.

@DamianEdwards DamianEdwards changed the title Replace use of Path.GetTempDir with Directory.CreateTempSubdirectory Improvements to RunWithHttpsDevCert extensions Jan 30, 2025
@@ -0,0 +1,10 @@
<Project>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It wouldn't hurt to stick a comment in here in case someone stumbles in and asks what this is for.

@@ -17,96 +18,162 @@ public static class DevCertHostingExtensions
/// Use <see cref="ResourceBuilderExtensions.WithHttpsEndpoint{TResource}"/> to configure an HTTPS endpoint.
/// </remarks>
public static IResourceBuilder<TResource> RunWithHttpsDevCertificate<TResource>(
this IResourceBuilder<TResource> builder, string certFileEnv, string certKeyFileEnv, Action<string, string>? onSuccessfulExport = null)
this IResourceBuilder<TResource> builder, CertificateFileFormat certificateFileFormat, string certFileEnv, string? certPasswordOrKeyEnv, Func<IServiceProvider, string, string?, Task>? onSuccessfulExport = null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name certPasswordOrKeyEnv is a bit confusing. is there a better API or documentation or something we can do here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe an options class (one per type?) that has all the information for each cert type?


if (File.Exists(certExportPath))
{
logger.LogTrace("Previously exported key file is present but dev cert file '{CertPath}' is missing. Deleting dev cert file and re-exporting.", certExportPath);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we warn above in PfxWithPassword, but only trace here?

Comment on lines +176 to 177
string[] args = ["dev-certs", "https", "--export-path", $"\"{certExportPath}\"", "--format", formatArg, passwordArgs];
var argsString = string.Join(' ', args);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(nit) why make a string[] and then join it? Why not just make a single string?

var argsString = $"dev-certs https --export-path \"{certExportPath}\" --format {formatArg} {passwordArgs}";

var objDir = GetMetadataValue(assemblyMetadata, "AppHostProjectBaseIntermediateOutputPath");
var dirPath = projectDir is not null && objDir is not null
? Path.Join(projectDir, objDir, "aspire")
: Directory.CreateTempSubdirectory(GetAppHostSpecificTempDirPrefix(builder)).FullName;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we even mess with the temp directory stuff if the OutputPath isn't found in the assembly? Can we just throw and make the code simpler?

Comment on lines +269 to +270
var projectDir = GetMetadataValue(assemblyMetadata, "AppHostProjectPath");
var objDir = GetMetadataValue(assemblyMetadata, "AppHostProjectBaseIntermediateOutputPath");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I half wonder if we should just stamp the full path into the file during the build and then we just need to read the 1 value out and use it.

@DamianEdwards DamianEdwards force-pushed the damianedwards/CreateTempSubdirectory branch from fed7983 to 33fa711 Compare February 2, 2025 04:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Replace uses of Path.GetTempDir with Directory.CreateTempSubdirectory instead
3 participants