Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Aspire.Hosting.Yarp/YarpResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public class YarpResource : ContainerResource, IResourceWithServiceDiscovery, IC

internal List<YarpCluster> Clusters { get; } = [];

/// <summary>
/// Gets or sets the HTTPS host port for the YARP resource.
/// </summary>
internal int? HostHttpsPort { get; set; }

/// <param name="name">The name of the resource.</param>
public YarpResource(string name) : base(name)
{
Expand Down
15 changes: 10 additions & 5 deletions src/Aspire.Hosting.Yarp/YarpResourceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,13 @@ public static IResourceBuilder<YarpResource> AddYarp(
// If a TLS certificate is configured, ensure the YARP resource has an HTTPS endpoint and
// configure the environment variables to use it.
yarpBuilder
.WithHttpsEndpoint(targetPort: HttpsPort)
.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)}");
}
Expand Down Expand Up @@ -150,10 +156,9 @@ public static IResourceBuilder<YarpResource> WithHostHttpsPort(this IResourceBui
{
ArgumentNullException.ThrowIfNull(builder);

return builder.WithEndpoint("https", endpoint =>
{
endpoint.Port = port;
}, createIfNotExists: false);
builder.Resource.HostHttpsPort = port;

return builder.WithEndpoint("https", ep => ep.Port = port, createIfNotExists: false);
}

/// <summary>
Expand Down
52 changes: 52 additions & 0 deletions tests/Aspire.Hosting.Yarp.Tests/AddYarpTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Aspire.Hosting.Dcp;
using Aspire.Hosting.Tests.Utils;
using Aspire.Hosting.Utils;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

namespace Aspire.Hosting.Yarp.Tests;
Expand Down Expand Up @@ -396,6 +397,57 @@ public async Task VerifyPublishWithStaticFilesGeneratesCorrectDockerfileWithMult
await Verify(dockerfile);
}

[Fact]
public async Task VerifyWithHostHttpsPortCreatesHttpsEndpointWithSpecifiedPort()
{
using var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Run);

builder.Services.AddSingleton<IDeveloperCertificateService>(new TestDeveloperCertificateService(
new List<X509Certificate2>(),
supportsContainerTrust: true,
trustCertificate: true,
tlsTerminate: false));

const int httpsPort = 12345;

var yarp = builder.AddYarp("yarp")
.WithHttpsDeveloperCertificate()
.WithHostHttpsPort(httpsPort);

using var app = builder.Build();
var model = app.Services.GetRequiredService<DistributedApplicationModel>();

var beforeStartEvent = new BeforeStartEvent(app.Services, model);
await builder.Eventing.PublishAsync(beforeStartEvent);

var httpsEndpoint = Assert.Single(yarp.Resource.Annotations.OfType<EndpointAnnotation>(), e => e.Name == "https");
Assert.Equal(httpsPort, httpsEndpoint.Port);
Assert.Equal("https", httpsEndpoint.UriScheme);
}

[Fact]
public async Task VerifyWithHostHttpsPortDoesNotCreateHttpsEndpointWithoutCertificateConfiguration()
{
using var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Run);

builder.Services.AddSingleton<IDeveloperCertificateService>(new TestDeveloperCertificateService(
new List<X509Certificate2>(),
supportsContainerTrust: true,
trustCertificate: true,
tlsTerminate: false));

var yarp = builder.AddYarp("yarp")
.WithHostHttpsPort(12345);

using var app = builder.Build();
var model = app.Services.GetRequiredService<DistributedApplicationModel>();

var beforeStartEvent = new BeforeStartEvent(app.Services, model);
await builder.Eventing.PublishAsync(beforeStartEvent);

Assert.DoesNotContain(yarp.Resource.Annotations.OfType<EndpointAnnotation>(), e => e.Name == "https");
}

private sealed class TestContainerFilesResource(string name) : ContainerResource(name), IResourceWithContainerFiles
{
}
Expand Down