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

Fix Azure ServiceBus persistent container support #7136

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -14,6 +14,7 @@

<ItemGroup>
<Compile Include="$(RepoRoot)src\Aspire.Hosting\Utils\PasswordGenerator.cs" Link="Utils\PasswordGenerator.cs" />
<Compile Include="$(SharedDir)SecretsStore.cs" Link="Utils\SecretsStore.cs" />
</ItemGroup>

<ItemGroup>
Expand Down
64 changes: 53 additions & 11 deletions src/Aspire.Hosting.Azure.ServiceBus/AzureServiceBusExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Reflection;
using System.Text.Json;
using System.Text.Json.Nodes;
using Aspire.Hosting.ApplicationModel;
using Aspire.Hosting.Azure;
using Aspire.Hosting.Azure.ServiceBus;
using Aspire.Hosting.Utils;
using Azure.Messaging.ServiceBus;
using Azure.Provisioning;
using Microsoft.Extensions.Configuration.UserSecrets;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.SecretManager.Tools.Internal;
using AzureProvisioning = Azure.Provisioning.ServiceBus;

namespace Aspire.Hosting;
Expand Down Expand Up @@ -233,9 +235,29 @@ public static IResourceBuilder<AzureServiceBusResource> RunAsEmulator(this IReso
return builder;
}

var lifetime = ContainerLifetime.Session;

if (configureContainer != null)
{
var surrogate = new AzureServiceBusEmulatorResource(builder.Resource);
var surrogateBuilder = builder.ApplicationBuilder.CreateResourceBuilder(surrogate);
configureContainer(surrogateBuilder);

if (surrogate.TryGetLastAnnotation<ContainerLifetimeAnnotation>(out var lifetimeAnnotation))
{
lifetime = lifetimeAnnotation.Lifetime;
}
}

// Create a default file mount. This could be replaced by a user-provided file mount.

var configHostFile = Path.Combine(Directory.CreateTempSubdirectory("AspireServiceBusEmulator").FullName, "Config.json");

if (lifetime == ContainerLifetime.Persistent && builder.ApplicationBuilder.ExecutionContext.IsRunMode && builder.ApplicationBuilder.AppHostAssembly is not null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we can centralize this logic.

{
configHostFile = GetOrSetUserSecret(builder.ApplicationBuilder.AppHostAssembly, "Parameters:ServiceBusEmulatorConfigFile", configHostFile);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment as to what this does? It is using the same volume path across runs?

}

var defaultConfigFileMount = new ContainerMountAnnotation(
configHostFile,
AzureServiceBusEmulatorResource.EmulatorConfigJsonPath,
Expand All @@ -246,7 +268,8 @@ public static IResourceBuilder<AzureServiceBusResource> RunAsEmulator(this IReso

// Add emulator container

var password = PasswordGenerator.Generate(16, true, true, true, true, 0, 0, 0, 0);
// The password must be at least 8 characters long and contain characters from three of the following four sets: Uppercase letters, Lowercase letters, Base 10 digits, and Symbols
var passwordParameter = ParameterResourceBuilderExtensions.CreateDefaultPasswordParameter(builder.ApplicationBuilder, $"{builder.Resource.Name}-sqledge-pwd", minLower: 1, minUpper: 1, minNumeric: 1);

builder
.WithEndpoint(name: "emulator", targetPort: 5672)
Expand All @@ -264,15 +287,19 @@ public static IResourceBuilder<AzureServiceBusResource> RunAsEmulator(this IReso
.WithImageRegistry(ServiceBusEmulatorContainerImageTags.AzureSqlEdgeRegistry)
.WithEndpoint(targetPort: 1433, name: "tcp")
.WithEnvironment("ACCEPT_EULA", "Y")
.WithEnvironment("MSSQL_SA_PASSWORD", password);
.WithEnvironment(context =>
{
context.EnvironmentVariables["MSSQL_SA_PASSWORD"] = passwordParameter;
})
.WithLifetime(lifetime);

builder.WithAnnotation(new EnvironmentCallbackAnnotation((EnvironmentCallbackContext context) =>
{
var sqlEndpoint = sqlEdgeResource.Resource.GetEndpoint("tcp");

context.EnvironmentVariables.Add("ACCEPT_EULA", "Y");
context.EnvironmentVariables.Add("SQL_SERVER", $"{sqlEndpoint.Resource.Name}:{sqlEndpoint.TargetPort}");
context.EnvironmentVariables.Add("MSSQL_SA_PASSWORD", password);
context.EnvironmentVariables.Add("MSSQL_SA_PASSWORD", passwordParameter);
}));

ServiceBusClient? serviceBusClient = null;
Expand Down Expand Up @@ -411,13 +438,6 @@ public static IResourceBuilder<AzureServiceBusResource> RunAsEmulator(this IReso

var healthCheckKey = $"{builder.Resource.Name}_check";

if (configureContainer != null)
{
var surrogate = new AzureServiceBusEmulatorResource(builder.Resource);
var surrogateBuilder = builder.ApplicationBuilder.CreateResourceBuilder(surrogate);
configureContainer(surrogateBuilder);
}

// To use the existing ServiceBus health check we would need to know if there is any queue or topic defined.
// We can register a health check for a queue and then no-op if there are no queues. Same for topics.
// If no queues or no topics are defined then the health check will be successful.
Expand Down Expand Up @@ -484,4 +504,26 @@ public static IResourceBuilder<AzureServiceBusEmulatorResource> WithHostPort(thi
endpoint.Port = port;
});
}

private static string GetOrSetUserSecret(Assembly assembly, string name, string value)
{
if (assembly.GetCustomAttribute<UserSecretsIdAttribute>()?.UserSecretsId is { } userSecretsId)
{
// Save the value to the secret store
try
{
var secretsStore = new SecretsStore(userSecretsId);
if(secretsStore.ContainsKey(name))
{
return secretsStore[name]!;
}
secretsStore.Set(name, value);
secretsStore.Save();
return value;
}
catch (Exception) { }
}

return value;
}
}
Loading