-
Notifications
You must be signed in to change notification settings - Fork 734
Rework certificate environment and argument config to be more idiomatic #12358
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
8 commits
Select commit
Hold shift + click to select a range
9e329f0
Rework certificate environment and argument config to be more idiomatic
danegsta f46e91d
Don't double apply SSL_CERT_DIR
danegsta f36dc53
Add extra examples
danegsta 0c456f9
Respond to PR feedback
danegsta fa8a710
Switch to factories for certificate path value providers
danegsta 3a84019
Ensure service is available
danegsta a8d7b28
Fix logging tests
danegsta 786c7ff
Fix another logging test case
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
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
115 changes: 115 additions & 0 deletions
115
src/Aspire.Hosting/ApplicationModel/CertificateTrustConfigurationCallbackAnnotation.cs
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 |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace Aspire.Hosting.ApplicationModel; | ||
|
|
||
| /// <summary> | ||
| /// An annotation that indicates a resource wants to manage how custom certificate trust is configured. | ||
| /// </summary> | ||
| /// <param name="callback">The callback used to customize certificate trust for the resource.</param> | ||
| public sealed class CertificateTrustConfigurationCallbackAnnotation(Func<CertificateTrustConfigurationCallbackAnnotationContext, Task> callback) : IResourceAnnotation | ||
| { | ||
| /// <summary> | ||
| /// Gets the callback to invoke to populate or modify the certificate authority collection. | ||
| /// </summary> | ||
| public Func<CertificateTrustConfigurationCallbackAnnotationContext, Task> Callback { get; } = callback ?? throw new ArgumentNullException(nameof(callback)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Context provided to a <see cref="CertificateTrustConfigurationCallbackAnnotation"/> callback. | ||
| /// </summary> | ||
| public sealed class CertificateTrustConfigurationCallbackAnnotationContext | ||
| { | ||
| /// <summary> | ||
| /// Gets the <see cref="DistributedApplicationExecutionContext"/> for this session. | ||
| /// </summary> | ||
| public required DistributedApplicationExecutionContext ExecutionContext { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the resource to which the annotation is applied. | ||
| /// </summary> | ||
| public required IResource Resource { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the command line arguments associated with the callback context. Values can be either a string or a path | ||
| /// value provider such as <see cref="CertificateBundlePath"/> or <see cref="CertificateDirectoriesPath"/>. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <example> | ||
| /// <code language="csharp"> | ||
| /// builder.AddContainer("my-resource", "my-image:latest") | ||
| /// .WithCertificateTrustConfigurationCallback(ctx => | ||
| /// { | ||
| /// ctx.Arguments.Add("--use-system-ca"); | ||
| /// return Task.CompletedTask; | ||
| /// }); | ||
| /// </code> | ||
| /// </example> | ||
| /// </remarks> | ||
| public required List<object> Arguments { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the environment variables required to configure certificate trust for the resource. | ||
| /// The dictionary key is the environment variable name; the value can be either a string or a path | ||
| /// value provider such as <see cref="CertificateBundlePath"/> or <see cref="CertificateDirectoriesPath"/>. | ||
| /// By default the environment will always include an entry for `SSL_CERT_DIR` and may include `SSL_CERT_FILE` if | ||
| /// <see cref="CertificateTrustScope.Override"/> or <see cref="CertificateTrustScope.System"/> is configured. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <example> | ||
| /// <code language="csharp"> | ||
| /// builder.AddContainer("my-resource", "my-image:latest") | ||
| /// .WithCertificateTrustConfigurationCallback(ctx => | ||
| /// { | ||
| /// ctx.EnvironmentVariables["MY_CUSTOM_CERT_VAR"] = ctx.CertificateBundlePath; | ||
| /// ctx.EnvironmentVariables["CERTS_DIR"] = ctx.CertificateDirectoriesPath; | ||
| /// return Task.CompletedTask; | ||
| /// }); | ||
| /// </code> | ||
| /// </example> | ||
| /// </remarks> | ||
| public required Dictionary<string, object> EnvironmentVariables { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// A value provider that will resolve to a path to a custom certificate bundle. | ||
| /// </summary> | ||
| public required ReferenceExpression CertificateBundlePath { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// A value provider that will resolve to paths containing individual certificates. | ||
| /// </summary> | ||
| public required ReferenceExpression CertificateDirectoriesPath { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the <see cref="CertificateTrustScope"/> for the resource. | ||
| /// </summary> | ||
| public required CertificateTrustScope Scope { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the <see cref="CancellationToken"/> that can be used to cancel the operation. | ||
| /// </summary> | ||
| public required CancellationToken CancellationToken { get; init; } | ||
| } | ||
|
|
||
| internal sealed class CertificateTrustConfigurationPathsProvider | ||
| { | ||
| /// <summary> | ||
| /// The actual path to the certificate bundle file to be resolved at runtime | ||
| /// </summary> | ||
| public string? CertificateBundlePath { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// The actual path to the certificate directories to be resolved at runtime | ||
| /// </summary> | ||
| public string? CertificateDirectoriesPath { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets a reference expression that resolves to the certificate bundle path. | ||
| /// </summary> | ||
| public ReferenceExpression CertificateBundlePathReference => ReferenceExpression.Create($"{CertificateBundlePath}"); | ||
|
|
||
| /// <summary> | ||
| /// Gets a reference expression that resolves to the certificate directories path. | ||
| /// </summary> | ||
| public ReferenceExpression CertificateDirectoriesPathReference => ReferenceExpression.Create($"{CertificateDirectoriesPath}"); | ||
| } | ||
63 changes: 63 additions & 0 deletions
63
src/Aspire.Hosting/ApplicationModel/ContainerCertificatePathsAnnotation.cs
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 |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Collections.Immutable; | ||
|
|
||
| namespace Aspire.Hosting.ApplicationModel; | ||
|
|
||
| /// <summary> | ||
| /// An annotation that allows overriding default certificate paths for container resources. | ||
| /// </summary> | ||
| public sealed class ContainerCertificatePathsAnnotation : IResourceAnnotation | ||
| { | ||
| /// <summary> | ||
| /// The default destination path in the container under which custom certificates will be placed. | ||
| /// </summary> | ||
| public const string DefaultCustomCertificatesDestination = "/usr/lib/ssl/aspire"; | ||
|
|
||
| /// <summary> | ||
| /// Default paths to default certificate bundle files in a container. | ||
| /// </summary> | ||
| public static ImmutableList<string> DefaultCertificateBundlePaths = ImmutableList.Create( | ||
| // Debian/Ubuntu/Gentoo etc. | ||
| "/etc/ssl/certs/ca-certificates.crt", | ||
| // Fedora/RHEL 6 | ||
| "/etc/pki/tls/certs/ca-bundle.crt", | ||
| // OpenSUSE | ||
| "/etc/ssl/ca-bundle.pem", | ||
| // OpenELEC | ||
| "/etc/pki/tls/cacert.pem", | ||
| // CentOS/RHEL 7 | ||
| "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem", | ||
| // Alpine Linux | ||
| "/etc/ssl/cert.pem"); | ||
|
|
||
| /// <summary> | ||
| /// Default paths to default directories containing individual CA certificates in a container. | ||
| /// </summary> | ||
| public static ImmutableList<string> DefaultCertificateDirectoriesPaths = ImmutableList.Create( | ||
| "/etc/ssl/certs", | ||
| "/usr/local/share/ca-certificates", | ||
| "/etc/pki/tls/certs"); | ||
|
|
||
| /// <summary> | ||
| /// Paths to default certificate bundle files in the container that should be replaced when the resource's | ||
| /// <see cref="CertificateTrustScope"/> is set to <see cref="CertificateTrustScope.Override"/> or | ||
| /// <see cref="CertificateTrustScope.System"/>. | ||
| /// If not set, a set of common default paths for popular Linux distributions will be used. | ||
| /// </summary> | ||
| public List<string>? DefaultCertificateBundles { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// Paths to default directories containing individual CA certificates in the container that should be appended | ||
| /// when the resource's <see cref="CertificateTrustScope"/> is set to <see cref="CertificateTrustScope.Append"/>. | ||
| /// If not set, a set of common default paths for popular Linux distributions will be used. | ||
| /// </summary> | ||
| public List<string>? DefaultCertificateDirectories { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// The destination path in the container under which custom certificates will be placed. | ||
| /// If not set, defaults to /usr/lib/ssl/aspire. | ||
| /// </summary> | ||
| public string? CustomCertificatesDestination { get; init; } | ||
| } |
111 changes: 0 additions & 111 deletions
111
src/Aspire.Hosting/ApplicationModel/ContainerCertificateTrustCallbackAnnotation.cs
This file was deleted.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion (feel free to disregard): instead of calling this just "callback", maybe we can use a name that reflects what the purpose of this function is, like
ConfigureCertificateTrustor something like that.