#pragma warning disable ASPIREDOCKERFILEBUILDER001 #pragma warning disable ASPIRECERTIFICATES001 // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using Aspire.Hosting.ApplicationModel; using Aspire.Hosting.ApplicationModel.Docker; using Aspire.Hosting.Yarp; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; namespace Aspire.Hosting; /// /// Provides extension methods for adding YARP resources to the application model. /// public static class YarpResourceExtensions { private const int Port = 5000; private const int HttpsPort = 5001; /// /// Adds a YARP container to the application model. /// /// The . /// The name of the resource. This name will be used as the connection string name when referenced in a dependency. /// A reference to the . [AspireExport(Description = "Adds a YARP container to the application model.")] public static IResourceBuilder AddYarp( this IDistributedApplicationBuilder builder, [ResourceName] string name) { var resource = new YarpResource(name); var yarpBuilder = builder.AddResource(resource) .WithHttpEndpoint(name: "http", targetPort: Port) .WithImage(YarpContainerImageTags.Image) .WithImageRegistry(YarpContainerImageTags.Registry) .WithImageTag(YarpContainerImageTags.Tag) .WithEnvironment("ASPNETCORE_ENVIRONMENT", builder.Environment.EnvironmentName) .WithEntrypoint("dotnet") .WithArgs("/app/yarp.dll") .WithOtlpExporter() .WithHttpsCertificateConfiguration(ctx => { ctx.EnvironmentVariables["Kestrel__Certificates__Default__Path"] = ctx.CertificatePath; ctx.EnvironmentVariables["Kestrel__Certificates__Default__KeyPath"] = ctx.KeyPath; if (ctx.Password is not null) { ctx.EnvironmentVariables["Kestrel__Certificates__Default__Password"] = ctx.Password; } return Task.CompletedTask; }); if (builder.ExecutionContext.IsRunMode) { yarpBuilder.SubscribeHttpsEndpointsUpdate(ctx => { // If a TLS certificate is configured, ensure the YARP resource has an HTTPS endpoint and // configure the environment variables to use it. yarpBuilder .WithEndpoint("https", ep => { // Create or update the HTTPS endpoint ep.TargetPort ??= HttpsPort; ep.UriScheme = "https"; ep.Port ??= resource.HostHttpsPort; }, createIfNotExists: true) .WithEnvironment("ASPNETCORE_HTTPS_PORT", resource.GetEndpoint("https").Property(EndpointProperty.Port)) .WithEnvironment("ASPNETCORE_URLS", $"{resource.GetEndpoint("https").Property(EndpointProperty.Scheme)}://*:{resource.GetEndpoint("https").Property(EndpointProperty.TargetPort)};{resource.GetEndpoint("http").Property(EndpointProperty.Scheme)}://*:{resource.GetEndpoint("http").Property(EndpointProperty.TargetPort)}"); }); } if (builder.ExecutionContext.IsRunMode) { yarpBuilder.WithEnvironment(ctx => { var developerCertificateService = ctx.ExecutionContext.ServiceProvider.GetRequiredService(); if (!developerCertificateService.SupportsContainerTrust) { // On systems without the ASP.NET DevCert updates introduced in .NET 10, YARP will not trust the cert used // by Aspire otlp endpoint when running locally. The Aspire otlp endpoint uses the dev cert, and prior to // .NET 10, it was only valid for localhost, but from the container perspective, the url will be something // like https://docker.host.internal, so it will NOT be valid. This is not necessary when using the latest // dev cert. ctx.EnvironmentVariables["YARP_UNSAFE_OLTP_CERT_ACCEPT_ANY_SERVER_CERTIFICATE"] = "true"; } }); } yarpBuilder.WithEnvironment(ctx => { YarpEnvConfigGenerator.PopulateEnvVariables(ctx.EnvironmentVariables, yarpBuilder.Resource.Routes, yarpBuilder.Resource.Clusters); }); return yarpBuilder; } /// /// Configure the YARP resource. /// /// The YARP resource to configure. /// The delegate to configure YARP. [AspireExport(Description = "Configure the YARP resource.", RunSyncOnBackgroundThread = true)] public static IResourceBuilder WithConfiguration(this IResourceBuilder builder, Action configurationBuilder) { var configBuilder = new YarpConfigurationBuilder(builder); configurationBuilder(configBuilder); return builder; } /// /// Configures the host port that the YARP resource is exposed on instead of using randomly assigned port. /// /// The resource builder for YARP. /// The port to bind on the host. If is used random port will be assigned. [AspireExport(Description = "Configures the host port that the YARP resource is exposed on instead of using randomly assigned port.")] public static IResourceBuilder WithHostPort(this IResourceBuilder builder, int? port) { ArgumentNullException.ThrowIfNull(builder); return builder.WithEndpoint("http", endpoint => { endpoint.Port = port; }); } /// /// Configures the host HTTPS port that the YARP resource is exposed on instead of using randomly assigned port. /// This will only have effect if an HTTPS endpoint is configured on the YARP resource due to TLS termination being enabled. /// /// The resource builder for YARP. /// The port to bind on the host. If is used random port will be assigned. /// The updated resource builder. [AspireExport(Description = "Configures the host HTTPS port that the YARP resource is exposed on instead of using randomly assigned port.")] public static IResourceBuilder WithHostHttpsPort(this IResourceBuilder builder, int? port) { ArgumentNullException.ThrowIfNull(builder); builder.Resource.HostHttpsPort = port; return builder.WithEndpoint("https", ep => ep.Port = port, createIfNotExists: false); } /// /// Enables static file serving in the YARP resource. Static files are served from the wwwroot folder. /// /// The resource builder for YARP. /// The . [AspireExportIgnore(Reason = "A single internal export with an optional sourcePath parameter provides the polyglot API without changing the public C# overloads.")] public static IResourceBuilder WithStaticFiles(this IResourceBuilder builder) { ArgumentNullException.ThrowIfNull(builder); return ConfigureStaticFiles(builder, sourcePath: null); } /// /// Enables static file serving. In run mode: bind mounts to /wwwroot. /// In publish mode: generates a Dockerfile whose build context is and /// copies its contents into /app/wwwroot baked into the image. /// /// The resource builder for YARP. /// The source path containing static files to serve. /// The . [AspireExportIgnore(Reason = "A single internal export with an optional sourcePath parameter provides the polyglot API without changing the public C# overloads.")] public static IResourceBuilder WithStaticFiles(this IResourceBuilder builder, string sourcePath) { ArgumentNullException.ThrowIfNull(builder); ArgumentNullException.ThrowIfNull(sourcePath); return ConfigureStaticFiles(builder, sourcePath); } [AspireExport("withStaticFiles", Description = "Enables static file serving in the YARP resource.")] internal static IResourceBuilder WithStaticFilesPolyglot(this IResourceBuilder builder, string? sourcePath = null) { ArgumentNullException.ThrowIfNull(builder); return ConfigureStaticFiles(builder, sourcePath); } private static IResourceBuilder ConfigureStaticFiles(IResourceBuilder builder, string? sourcePath) { builder = builder.WithEnvironment("YARP_ENABLE_STATIC_FILES", "true"); if (sourcePath is null) { return builder; } if (builder.ApplicationBuilder.ExecutionContext.IsPublishMode) { builder.WithDockerfileFactory(sourcePath, ctx => { var imageName = GetYarpImageName(ctx.Resource); return $""" FROM {imageName} AS yarp WORKDIR /app COPY . /app/wwwroot """; }); } else { builder = builder.WithContainerFiles("/wwwroot", sourcePath); } return builder; } /// /// In publish mode, generates a Dockerfile that copies static files from the specified resource into /app/wwwroot. /// /// The resource builder for YARP. /// The resource with container files. /// The updated resource builder. [AspireExport(Description = "In publish mode, generates a Dockerfile that copies static files from the specified resource into /app/wwwroot.")] public static IResourceBuilder PublishWithStaticFiles(this IResourceBuilder builder, IResourceBuilder resourceWithFiles) { if (!builder.ApplicationBuilder.ExecutionContext.IsPublishMode) { return builder; } // In publish mode, generate a Dockerfile that copies the container files into /app/wwwroot return builder .PublishWithContainerFiles(resourceWithFiles, "/app/wwwroot") .WithStaticFiles() .EnsurePublishWithStaticFilesDockerFileBuilder(); } private static IResourceBuilder EnsurePublishWithStaticFilesDockerFileBuilder(this IResourceBuilder builder) { if (builder.Resource.HasAnnotationOfType()) { // Dockerfile builder already configured, skip adding it again return builder; } return builder.WithDockerfileBuilder(".", ctx => { var logger = ctx.Services.GetService>(); var imageName = GetYarpImageName(ctx.Resource); ctx.Builder.AddContainerFilesStages(ctx.Resource, logger); ctx.Builder.From(imageName) .WorkDir("/app") .AddContainerFiles(ctx.Resource, "/app/wwwroot", logger); }); } private static string GetYarpImageName(IResource resource) { if (!resource.TryGetContainerImageName(useBuiltImage: false, out var imageName) || string.IsNullOrEmpty(imageName)) { imageName = $"{YarpContainerImageTags.Image}:{YarpContainerImageTags.Tag}"; } return imageName; } }