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

[release/9.1] Ensure EH and SB emulator files can be used from non-root containers #7765

Open
wants to merge 8 commits into
base: release/9.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
4 changes: 2 additions & 2 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<!-- This repo version -->
<MajorVersion>9</MajorVersion>
<MinorVersion>1</MinorVersion>
<PatchVersion>0</PatchVersion>
<PatchVersion>1</PatchVersion>
<VersionPrefix>$(MajorVersion).$(MinorVersion).$(PatchVersion)</VersionPrefix>
<PreReleaseVersionLabel>preview.1</PreReleaseVersionLabel>
<PreReleaseVersionLabel>servicing</PreReleaseVersionLabel>
<DefaultTargetFramework>net8.0</DefaultTargetFramework>
<AllTargetFrameworks>$(DefaultTargetFramework);net9.0</AllTargetFrameworks>
<!-- dotnet 8.0 versions for running tests -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAspireHost>true</IsAspireHost>
<UserSecretsId>f8cd0caa-3990-4f6f-889f-3cf66ac52de9</UserSecretsId>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions playground/mysql/MySqlDb.AppHost/MySqlDb.AppHost.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAspireHost>true</IsAspireHost>
<UserSecretsId>7359b387-0386-4cd0-9f26-7b40bdb8348d</UserSecretsId>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ namespace Aspire.Hosting;
/// </summary>
public static class AzureEventHubsExtensions
{
private const UnixFileMode FileMode644 = UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.GroupRead | UnixFileMode.OtherRead;

/// <summary>
/// Adds an Azure Event Hubs Namespace resource to the application model. This resource can be used to create Event Hub resources.
/// </summary>
Expand Down Expand Up @@ -322,6 +324,12 @@ public static IResourceBuilder<AzureEventHubsResource> RunAsEmulator(this IResou
// Deterministic file path for the configuration file based on its content
var configJsonPath = aspireStore.GetFileNameWithContent($"{builder.Resource.Name}-Config.json", tempConfigFile);

// The docker container runs as a non-root user, so we need to grant other user's read/write permission
if (!OperatingSystem.IsWindows())
{
File.SetUnixFileMode(configJsonPath, FileMode644);
}

builder.WithAnnotation(new ContainerMountAnnotation(
configJsonPath,
AzureEventHubsEmulatorResource.EmulatorConfigJsonPath,
Expand Down Expand Up @@ -425,6 +433,7 @@ public static IResourceBuilder<AzureEventHubsEmulatorResource> WithConfiguration

private static string WriteEmulatorConfigJson(AzureEventHubsResource emulatorResource)
{
// This temporary file is not used by the container, it will be copied and then deleted
var filePath = Path.GetTempFileName();

using var stream = new FileStream(filePath, FileMode.Open, FileAccess.Write);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ namespace Aspire.Hosting;
/// </summary>
public static class AzureServiceBusExtensions
{
private const UnixFileMode FileMode644 = UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.GroupRead | UnixFileMode.OtherRead;

/// <summary>
/// Adds an Azure Service Bus Namespace resource to the application model. This resource can be used to create queue, topic, and subscription resources.
/// </summary>
Expand Down Expand Up @@ -423,6 +425,12 @@ public static IResourceBuilder<AzureServiceBusResource> RunAsEmulator(this IReso
// Deterministic file path for the configuration file based on its content
var configJsonPath = aspireStore.GetFileNameWithContent($"{builder.Resource.Name}-Config.json", tempConfigFile);

// The docker container runs as a non-root user, so we need to grant other user's read/write permission
if (!OperatingSystem.IsWindows())
{
File.SetUnixFileMode(configJsonPath, FileMode644);
}

builder.WithAnnotation(new ContainerMountAnnotation(
configJsonPath,
AzureServiceBusEmulatorResource.EmulatorConfigJsonPath,
Expand Down Expand Up @@ -550,6 +558,7 @@ public static IResourceBuilder<AzureServiceBusEmulatorResource> WithHostPort(thi

private static string WriteEmulatorConfigJson(AzureServiceBusResource emulatorResource)
{
// This temporary file is not used by the container, it will be copied and then deleted
var filePath = Path.GetTempFileName();

using var stream = new FileStream(filePath, FileMode.Open, FileAccess.Write);
Expand Down
81 changes: 57 additions & 24 deletions src/Aspire.Hosting.MySql/MySqlBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace Aspire.Hosting;
public static class MySqlBuilderExtensions
{
private const string PasswordEnvVarName = "MYSQL_ROOT_PASSWORD";
private const UnixFileMode FileMode644 = UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.GroupRead | UnixFileMode.OtherRead;

/// <summary>
/// Adds a MySQL server resource to the application model. For local development a container is used.
Expand Down Expand Up @@ -102,14 +103,11 @@ public static IResourceBuilder<T> WithPhpMyAdmin<T>(this IResourceBuilder<T> bui

containerName ??= $"{builder.Resource.Name}-phpmyadmin";

var configurationTempFileName = Path.GetTempFileName();

var phpMyAdminContainer = new PhpMyAdminContainerResource(containerName);
var phpMyAdminContainerBuilder = builder.ApplicationBuilder.AddResource(phpMyAdminContainer)
.WithImage(MySqlContainerImageTags.PhpMyAdminImage, MySqlContainerImageTags.PhpMyAdminTag)
.WithImageRegistry(MySqlContainerImageTags.Registry)
.WithHttpEndpoint(targetPort: 80, name: "http")
.WithBindMount(configurationTempFileName, "/etc/phpmyadmin/config.user.inc.php")
.ExcludeFromManifest();

builder.ApplicationBuilder.Eventing.Subscribe<AfterEndpointsAllocatedEvent>((e, ct) =>
Expand Down Expand Up @@ -140,32 +138,33 @@ public static IResourceBuilder<T> WithPhpMyAdmin<T>(this IResourceBuilder<T> bui
}
else
{
using var stream = new FileStream(configurationTempFileName, FileMode.Create);
using var writer = new StreamWriter(stream);
var tempConfigFile = WritePhpMyAdminConfiguration(mySqlInstances);

writer.WriteLine("<?php");
writer.WriteLine();
writer.WriteLine("$i = 0;");
writer.WriteLine();
foreach (var mySqlInstance in mySqlInstances)
try
{
if (mySqlInstance.PrimaryEndpoint.IsAllocated)
var aspireStore = e.Services.GetRequiredService<IAspireStore>();

// Deterministic file path for the configuration file based on its content
var configStoreFilename = aspireStore.GetFileNameWithContent($"{builder.Resource.Name}-config.user.inc.php", tempConfigFile);

// Need to grant read access to the config file on unix like systems.
if (!OperatingSystem.IsWindows())
{
File.SetUnixFileMode(configStoreFilename, FileMode644);
}

phpMyAdminContainerBuilder.WithBindMount(configStoreFilename, "/etc/phpmyadmin/config.user.inc.php");
}
finally
{
try
{
File.Delete(tempConfigFile);
}
catch
{
var endpoint = mySqlInstance.PrimaryEndpoint;
writer.WriteLine("$i++;");
// PhpMyAdmin assumes MySql is being accessed over a default Aspire container network and hardcodes the resource address
// This will need to be refactored once updated service discovery APIs are available
writer.WriteLine($"$cfg['Servers'][$i]['host'] = '{endpoint.Resource.Name}:{endpoint.TargetPort}';");
writer.WriteLine($"$cfg['Servers'][$i]['verbose'] = '{mySqlInstance.Name}';");
writer.WriteLine($"$cfg['Servers'][$i]['auth_type'] = 'cookie';");
writer.WriteLine($"$cfg['Servers'][$i]['user'] = 'root';");
writer.WriteLine($"$cfg['Servers'][$i]['password'] = '{mySqlInstance.PasswordParameter.Value}';");
writer.WriteLine($"$cfg['Servers'][$i]['AllowNoPassword'] = true;");
writer.WriteLine();
}
}
writer.WriteLine("$cfg['DefaultServer'] = 1;");
writer.WriteLine("?>");
}

return Task.CompletedTask;
Expand Down Expand Up @@ -235,4 +234,38 @@ public static IResourceBuilder<MySqlServerResource> WithInitBindMount(this IReso

return builder.WithBindMount(source, "/docker-entrypoint-initdb.d", isReadOnly);
}

private static string WritePhpMyAdminConfiguration(IEnumerable<MySqlServerResource> mySqlInstances)
{
// This temporary file is not used by the container, it will be copied and then deleted
var filePath = Path.GetTempFileName();

using var writer = new StreamWriter(filePath);

writer.WriteLine("<?php");
writer.WriteLine();
writer.WriteLine("$i = 0;");
writer.WriteLine();
foreach (var mySqlInstance in mySqlInstances)
{
if (mySqlInstance.PrimaryEndpoint.IsAllocated)
{
var endpoint = mySqlInstance.PrimaryEndpoint;
writer.WriteLine("$i++;");
// PhpMyAdmin assumes MySql is being accessed over a default Aspire container network and hardcodes the resource address
// This will need to be refactored once updated service discovery APIs are available
writer.WriteLine($"$cfg['Servers'][$i]['host'] = '{endpoint.Resource.Name}:{endpoint.TargetPort}';");
writer.WriteLine($"$cfg['Servers'][$i]['verbose'] = '{mySqlInstance.Name}';");
writer.WriteLine($"$cfg['Servers'][$i]['auth_type'] = 'cookie';");
writer.WriteLine($"$cfg['Servers'][$i]['user'] = 'root';");
writer.WriteLine($"$cfg['Servers'][$i]['password'] = '{mySqlInstance.PasswordParameter.Value}';");
writer.WriteLine($"$cfg['Servers'][$i]['AllowNoPassword'] = true;");
writer.WriteLine();
}
}
writer.WriteLine("$cfg['DefaultServer'] = 1;");
writer.WriteLine("?>");

return filePath;
}
}
Loading