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

Minor improvements to dist app builder config #6524

Merged
merged 3 commits into from
Oct 29, 2024
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
24 changes: 21 additions & 3 deletions src/Aspire.Hosting/DistributedApplicationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,17 @@ public DistributedApplicationBuilder(DistributedApplicationOptions options)
// account for the path the AppHost is running from to disambiguate between different projects
// with the same name as seen in https://github.com/dotnet/aspire/issues/5413. For publish scenarios,
// we want to use a stable hash based only on the project name.
var appHostShaBytes = SHA256.HashData(Encoding.UTF8.GetBytes(AppHostPath));
var appHostNameShaBytes = SHA256.HashData(Encoding.UTF8.GetBytes(appHostName));
var appHostSha = ExecutionContext.IsPublishMode ? Convert.ToHexString(appHostNameShaBytes) : Convert.ToHexString(appHostShaBytes);
string appHostSha;
if (ExecutionContext.IsPublishMode)
{
var appHostNameShaBytes = SHA256.HashData(Encoding.UTF8.GetBytes(appHostName));
appHostSha = Convert.ToHexString(appHostNameShaBytes);
}
else
{
var appHostShaBytes = SHA256.HashData(Encoding.UTF8.GetBytes(AppHostPath));
appHostSha = Convert.ToHexString(appHostShaBytes);
}
_innerBuilder.Configuration.AddInMemoryCollection(new Dictionary<string, string?>
{
["AppHost:Sha256"] = appHostSha
Expand Down Expand Up @@ -244,6 +252,16 @@ public DistributedApplicationBuilder(DistributedApplicationOptions options)
}
);
}
else
{
// The dashboard is enabled but is unsecured. Set auth mode config setting to reflect this state.
_innerBuilder.Configuration.AddInMemoryCollection(
new Dictionary<string, string?>
{
["AppHost:ResourceService:AuthMode"] = nameof(ResourceServiceAuthMode.Unsecured)
}
);
}

_innerBuilder.Services.AddSingleton<DashboardCommandExecutor>();
_innerBuilder.Services.AddOptions<TransportOptions>().ValidateOnStart().PostConfigure(MapTransportOptionsFromCustomKeys);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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.Dashboard;
using Aspire.Hosting.Dcp;
using Aspire.Hosting.Lifecycle;
using Aspire.Hosting.Publishing;
Expand Down Expand Up @@ -95,6 +96,28 @@ public void AppHostDirectoryAvailableViaConfig()
Assert.Equal(appHostDirectory, config["AppHost:Directory"]);
}

[Fact]
public void ResourceServiceConfig_Secured()
{
var appBuilder = DistributedApplication.CreateBuilder();
using var app = appBuilder.Build();

var config = app.Services.GetRequiredService<IConfiguration>();
Assert.Equal(nameof(ResourceServiceAuthMode.ApiKey), config["AppHost:ResourceService:AuthMode"]);
Assert.False(string.IsNullOrEmpty(config["AppHost:ResourceService:ApiKey"]));
}

[Fact]
public void ResourceServiceConfig_Unsecured()
{
var appBuilder = DistributedApplication.CreateBuilder(args: [$"{KnownConfigNames.DashboardUnsecuredAllowAnonymous}=true"]);
using var app = appBuilder.Build();

var config = app.Services.GetRequiredService<IConfiguration>();
Assert.Equal(nameof(ResourceServiceAuthMode.Unsecured), config["AppHost:ResourceService:AuthMode"]);
Assert.True(string.IsNullOrEmpty(config["AppHost:ResourceService:ApiKey"]));
}

[Fact]
public void AddResource_DuplicateResourceNames_SameCasing_Error()
{
Expand Down