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

Support processing --useHttps flag on Functions applications #7789

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ private static IResourceBuilder<AzureFunctionsProjectResource> WithFunctionsHttp
}
var launchProfile = builder.Resource.GetEffectiveLaunchProfile();
int? port = null;
var useHttps = false;
if (launchProfile is not null)
{
var commandLineArgs = CommandLineArgsParser.Parse(launchProfile.LaunchProfile.CommandLineArgs ?? string.Empty);
Expand All @@ -162,30 +163,40 @@ private static IResourceBuilder<AzureFunctionsProjectResource> WithFunctionsHttp
{
port = parsedPort;
}

useHttps = commandLineArgs is { Count: > 0 } &&
commandLineArgs.IndexOf("--useHttps") > -1;
}
// When a port is defined in the launch profile, Azure Functions will favor that port over
// the port configured in the `WithArgs` callback when starting the project. To that end
// we register an endpoint where the target port matches the port the Azure Functions worker
// is actually configured to listen on and the endpoint is not proxied by DCP.
return builder
.WithHttpEndpoint(port: port, targetPort: port, isProxied: port == null)
.WithArgs(context =>
if (useHttps)
{
builder.WithHttpsEndpoint(port: port, targetPort: port, isProxied: port == null);
}
else
{
builder.WithHttpEndpoint(port: port, targetPort: port, isProxied: port == null);
}

return builder.WithArgs(context =>
{
// Only pass the --port argument to the functions host if
// it has not been explicitly defined in the launch profile
// already. This covers the case where the user has defined
// a launch profile without a `commandLineArgs` property.
// We only do this when not in publish mode since the Azure
// Functions container image overrides the default port to 80.
if (builder.ApplicationBuilder.ExecutionContext.IsPublishMode
|| port is not null)
{
// Only pass the --port argument to the functions host if
// it has not been explicitly defined in the launch profile
// already. This covers the case where the user has defined
// a launch profile without a `commandLineArgs` property.
// We only do this when not in publish mode since the Azure
// Functions container image overrides the default port to 80.
if (builder.ApplicationBuilder.ExecutionContext.IsPublishMode
|| port is not null)
{
return;
}
var http = builder.Resource.GetEndpoint("http");
context.Args.Add("--port");
context.Args.Add(http.Property(EndpointProperty.TargetPort));
});
return;
}
var targetEndpoint = builder.Resource.GetEndpoint(useHttps ? "https" : "http");
context.Args.Add("--port");
context.Args.Add(targetEndpoint.Property(EndpointProperty.TargetPort));
});
}

/// <summary>
Expand Down
98 changes: 98 additions & 0 deletions tests/Aspire.Hosting.Azure.Tests/AzureFunctionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,70 @@ public void AddAzureFunctionsProject_UsesCorrectNameUnderPublish()
Assert.StartsWith(AzureFunctionsProjectResourceExtensions.DefaultAzureFunctionsHostStorageName, resource.Name);
}

[Fact]
public void AddAzureFunctionsProject_WiresUpHttpsEndpointCorrectly_WhenUseHttpsArgumentIsProvided()
{
using var builder = TestDistributedApplicationBuilder.Create();
builder.AddAzureFunctionsProject<TestProjectWithHttps>("funcapp");

// Assert that the EndpointAnnotation is configured correctly
var functionsResource = Assert.Single(builder.Resources.OfType<AzureFunctionsProjectResource>());
Assert.True(functionsResource.TryGetLastAnnotation<EndpointAnnotation>(out var endpointAnnotation));
Assert.Equal(7071, endpointAnnotation.Port);
Assert.Equal(7071, endpointAnnotation.TargetPort);
Assert.False(endpointAnnotation.IsProxied);
Assert.Equal("https", endpointAnnotation.UriScheme);
}

[Fact]
public void AddAzureFunctionsProject_ConfiguresEnvironmentVariables_WhenInPublishMode()
{
using var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Publish);
builder.AddAzureFunctionsProject<TestProject>("funcapp");

var functionsResource = Assert.Single(builder.Resources.OfType<AzureFunctionsProjectResource>());
Assert.True(functionsResource.TryGetLastAnnotation<EnvironmentCallbackAnnotation>(out var envAnnotation));

var context = new EnvironmentCallbackContext(builder.ExecutionContext);
envAnnotation.Callback(context);

// Verify ASPNETCORE_URLS is set correctly with the target port
var aspNetCoreUrls = context.EnvironmentVariables["ASPNETCORE_URLS"];
Assert.NotNull(aspNetCoreUrls);
Assert.Contains("8080", aspNetCoreUrls.ToString());
}

[Fact]
public async Task AddAzureFunctionsProject_WiresUpHttpsEndpointCorrectly_WhenOnlyUseHttpsArgumentIsProvided()
{
using var builder = TestDistributedApplicationBuilder.Create();
builder.AddAzureFunctionsProject<TestProjectWithHttpsNoPort>("funcapp")
// Explicit set endpoint values for assertions later
.WithEndpoint("https", e =>
{
e.UriScheme = "https";
e.AllocatedEndpoint = new(e, "localhost", 1234);
e.TargetPort = 9876;
});

// Assert that the EndpointAnnotation is configured correctly
var functionsResource = Assert.Single(builder.Resources.OfType<AzureFunctionsProjectResource>());
Assert.True(functionsResource.TryGetLastAnnotation<EndpointAnnotation>(out var endpointAnnotation));
Assert.Null(endpointAnnotation.Port);
Assert.Equal(9876, endpointAnnotation.TargetPort);
Assert.True(endpointAnnotation.IsProxied);
Assert.Equal("https", endpointAnnotation.UriScheme);

// Check that `--port` is present in the args
using var app = builder.Build();
var args = await ArgumentEvaluator.GetArgumentListAsync(functionsResource);

Assert.Collection(args,
arg => Assert.Equal("--port", arg),
arg => Assert.Equal("9876", arg)
);
}

private sealed class TestProject : IProjectMetadata
{
public string ProjectPath => "some-path";
Expand Down Expand Up @@ -267,4 +331,38 @@ private sealed class TestProjectWithMultiplePorts : IProjectMetadata
}
};
}

private sealed class TestProjectWithHttps : IProjectMetadata
{
public string ProjectPath => "some-path";

public LaunchSettings LaunchSettings => new()
{
Profiles = new Dictionary<string, LaunchProfile>
{
["funcapp"] = new()
{
CommandLineArgs = "--port 7071 --useHttps",
LaunchBrowser = false,
}
}
};
}

private sealed class TestProjectWithHttpsNoPort : IProjectMetadata
{
public string ProjectPath => "some-path";

public LaunchSettings LaunchSettings => new()
{
Profiles = new Dictionary<string, LaunchProfile>
{
["funcapp"] = new()
{
CommandLineArgs = "--useHttps",
LaunchBrowser = false,
}
}
};
}
}
Loading