1+ // Licensed to the .NET Foundation under one or more agreements.
2+ // The .NET Foundation licenses this file to you under the MIT license.
3+
4+ namespace Aspire . Hosting . ApplicationModel ;
5+
6+ /// <summary>
7+ /// An annotation that indicates a resource wants to manage how custom certificate trust is configured.
8+ /// </summary>
9+ /// <param name="callback">The callback used to customize certificate trust for the resource.</param>
10+ public sealed class CertificateTrustConfigurationCallbackAnnotation ( Func < CertificateTrustConfigurationCallbackAnnotationContext , Task > callback ) : IResourceAnnotation
11+ {
12+ /// <summary>
13+ /// Gets the callback to invoke to populate or modify the certificate authority collection.
14+ /// </summary>
15+ public Func < CertificateTrustConfigurationCallbackAnnotationContext , Task > Callback { get ; } = callback ?? throw new ArgumentNullException ( nameof ( callback ) ) ;
16+ }
17+
18+ /// <summary>
19+ /// Context provided to a <see cref="CertificateTrustConfigurationCallbackAnnotation"/> callback.
20+ /// </summary>
21+ public sealed class CertificateTrustConfigurationCallbackAnnotationContext
22+ {
23+ /// <summary>
24+ /// Gets the <see cref="DistributedApplicationExecutionContext"/> for this session.
25+ /// </summary>
26+ public required DistributedApplicationExecutionContext ExecutionContext { get ; init ; }
27+
28+ /// <summary>
29+ /// Gets the resource to which the annotation is applied.
30+ /// </summary>
31+ public required IResource Resource { get ; init ; }
32+
33+ /// <summary>
34+ /// Gets the command line arguments associated with the callback context. Values can be either a string or a path
35+ /// value provider such as <see cref="CertificateBundlePath"/> or <see cref="CertificateDirectoriesPath"/>.
36+ /// </summary>
37+ /// <remarks>
38+ /// <example>
39+ /// <code language="csharp">
40+ /// builder.AddContainer("my-resource", "my-image:latest")
41+ /// .WithCertificateTrustConfigurationCallback(ctx =>
42+ /// {
43+ /// ctx.Arguments.Add("--use-system-ca");
44+ /// return Task.CompletedTask;
45+ /// });
46+ /// </code>
47+ /// </example>
48+ /// </remarks>
49+ public required List < object > Arguments { get ; init ; }
50+
51+ /// <summary>
52+ /// Gets the environment variables required to configure certificate trust for the resource.
53+ /// The dictionary key is the environment variable name; the value can be either a string or a path
54+ /// value provider such as <see cref="CertificateBundlePath"/> or <see cref="CertificateDirectoriesPath"/>.
55+ /// By default the environment will always include an entry for `SSL_CERT_DIR` and may include `SSL_CERT_FILE` if
56+ /// <see cref="CertificateTrustScope.Override"/> or <see cref="CertificateTrustScope.System"/> is configured.
57+ /// </summary>
58+ /// <remarks>
59+ /// <example>
60+ /// <code language="csharp">
61+ /// builder.AddContainer("my-resource", "my-image:latest")
62+ /// .WithCertificateTrustConfigurationCallback(ctx =>
63+ /// {
64+ /// ctx.EnvironmentVariables["MY_CUSTOM_CERT_VAR"] = ctx.CertificateBundlePath;
65+ /// ctx.EnvironmentVariables["CERTS_DIR"] = ctx.CertificateDirectoriesPath;
66+ /// return Task.CompletedTask;
67+ /// });
68+ /// </code>
69+ /// </example>
70+ /// </remarks>
71+ public required Dictionary < string , object > EnvironmentVariables { get ; init ; }
72+
73+ /// <summary>
74+ /// A value provider that will resolve to a path to a custom certificate bundle.
75+ /// </summary>
76+ public required ReferenceExpression CertificateBundlePath { get ; init ; }
77+
78+ /// <summary>
79+ /// A value provider that will resolve to paths containing individual certificates.
80+ /// </summary>
81+ public required ReferenceExpression CertificateDirectoriesPath { get ; init ; }
82+
83+ /// <summary>
84+ /// Gets the <see cref="CertificateTrustScope"/> for the resource.
85+ /// </summary>
86+ public required CertificateTrustScope Scope { get ; init ; }
87+
88+ /// <summary>
89+ /// Gets the <see cref="CancellationToken"/> that can be used to cancel the operation.
90+ /// </summary>
91+ public required CancellationToken CancellationToken { get ; init ; }
92+ }
93+
94+ internal sealed class CertificateTrustConfigurationPathsProvider
95+ {
96+ /// <summary>
97+ /// The actual path to the certificate bundle file to be resolved at runtime
98+ /// </summary>
99+ public string ? CertificateBundlePath { get ; set ; }
100+
101+ /// <summary>
102+ /// The actual path to the certificate directories to be resolved at runtime
103+ /// </summary>
104+ public string ? CertificateDirectoriesPath { get ; set ; }
105+
106+ /// <summary>
107+ /// Gets a reference expression that resolves to the certificate bundle path.
108+ /// </summary>
109+ public ReferenceExpression CertificateBundlePathReference => ReferenceExpression . Create ( $ "{ CertificateBundlePath } ") ;
110+
111+ /// <summary>
112+ /// Gets a reference expression that resolves to the certificate directories path.
113+ /// </summary>
114+ public ReferenceExpression CertificateDirectoriesPathReference => ReferenceExpression . Create ( $ "{ CertificateDirectoriesPath } ") ;
115+ }
0 commit comments