Skip to content

Commit

Permalink
Add InputAnnotation
Browse files Browse the repository at this point in the history
This annotation allows for "inputs" to be written to the manifest
when needing to generate a value, like a password, at publish time.

It is also used in Parameters to model the "input.value" of the parameter.
  • Loading branch information
eerhardt committed Feb 29, 2024
1 parent 55d081d commit decd0a1
Show file tree
Hide file tree
Showing 23 changed files with 420 additions and 144 deletions.
78 changes: 78 additions & 0 deletions src/Aspire.Hosting/ApplicationModel/InputAnnotation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using Aspire.Hosting.Publishing;

namespace Aspire.Hosting.ApplicationModel;

/// <summary>
/// Represents a input annotation that describes an input value.
/// </summary>
/// <remarks>
/// This class is used to specify generated passwords, usernames, etc.
/// </remarks>
[DebuggerDisplay("Type = {GetType().Name,nq}, Name = {Name}")]
public sealed class InputAnnotation : IResourceAnnotation
{
/// <summary>
/// Initializes a new instance of <see cref="InputAnnotation"/>.
/// </summary>
public InputAnnotation(string name, string? type = null, bool secret = false)
{
Name = name;
Type = type;
Secret = secret;
}

/// <summary>
/// Name of the input.
/// </summary>
public string Name { get; set; }

/// <summary>
/// The type of the input.
/// </summary>
public string? Type { get; set; }

/// <summary>
/// Indicates if the input is a secret.
/// </summary>
public bool Secret { get; set; }

/// <summary>
/// Represents what the
/// </summary>
public InputDefault? Default { get; set; }
}

/// <summary>
/// Represents how a default value should be retrieved.
/// </summary>
public abstract class InputDefault
{
/// <summary>
/// Writes the current <see cref="InputDefault"/> to the manifest context.
/// </summary>
/// <param name="context">The context for the manifest publishing operation.</param>
public abstract void WriteToManifest(ManifestPublishingContext context);
}

/// <summary>
/// Represents that a default value should be generated.
/// </summary>
public sealed class GenerateInputDefault : InputDefault
{
/// <summary>
/// The minimum length of the generated value.
/// </summary>
public int MinLength { get; set; }

/// <inheritdoc/>
public override void WriteToManifest(ManifestPublishingContext context)
{
context.Writer.WriteStartObject("generate");
context.Writer.WriteNumber("minLength", MinLength);
context.Writer.WriteEndObject();
}
}
21 changes: 21 additions & 0 deletions src/Aspire.Hosting/ApplicationModel/InputAnnotationExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Aspire.Hosting.ApplicationModel;

/// <summary>
/// Provides extension methods for <see cref="InputAnnotation"/>.
/// </summary>
internal static class InputAnnotationExtensions
{
internal static T WithDefaultGeneratedPasswordAnnotation<T>(this T builder)
where T : IResourceBuilder<ContainerResource>
{
builder.WithAnnotation(new InputAnnotation("password", secret: true)
{
Default = new GenerateInputDefault { MinLength = 10 }
});

return builder;
}
}
25 changes: 19 additions & 6 deletions src/Aspire.Hosting/ApplicationModel/ParameterResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,33 @@ namespace Aspire.Hosting.ApplicationModel;
/// <summary>
/// Represents a parameter resource.
/// </summary>
/// <param name="name">The name of the parameter resource.</param>
/// <param name="callback">The callback function to retrieve the value of the parameter.</param>
/// <param name="secret">A flag indicating whether the parameter is secret.</param>
public sealed class ParameterResource(string name, Func<string> callback, bool secret = false) : Resource(name)
public sealed class ParameterResource : Resource
{
private readonly Func<string> _callback;

/// <summary>
/// Initializes a new instance of <see cref="ParameterResource"/>.
/// </summary>
/// <param name="name">The name of the parameter resource.</param>
/// <param name="callback">The callback function to retrieve the value of the parameter.</param>
/// <param name="secret">A flag indicating whether the parameter is secret.</param>
public ParameterResource(string name, Func<string> callback, bool secret = false) : base(name)
{
_callback = callback;
Secret = secret;

Annotations.Add(new InputAnnotation("value", secret: secret));
}

/// <summary>
/// Gets the value of the parameter.
/// </summary>
public string Value => callback();
public string Value => _callback();

/// <summary>
/// Gets a value indicating whether the parameter is secret.
/// </summary>
public bool Secret { get; } = secret;
public bool Secret { get; }

/// <summary>
/// Gets the expression used in the manifest to reference the value of the parameter.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,7 @@ private static void WriteParameterResourceToManifest(ManifestPublishingContext c
}

context.Writer.WriteString("value", $"{{{resource.Name}.inputs.value}}");
context.Writer.WriteStartObject("inputs");
context.Writer.WriteStartObject("value");
context.Writer.WriteString("type", "string");

if (resource.Secret)
{
context.Writer.WriteBoolean("secret", resource.Secret);
}

context.Writer.WriteEndObject();
context.Writer.WriteEndObject();
context.WriteInputs(resource);
}

/// <summary>
Expand Down
3 changes: 2 additions & 1 deletion src/Aspire.Hosting/MySql/MySqlBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public static IResourceBuilder<MySqlServerResource> AddMySql(this IDistributedAp
return builder.AddResource(resource)
.WithAnnotation(new EndpointAnnotation(ProtocolType.Tcp, port: port, containerPort: 3306)) // Internal port is always 3306.
.WithAnnotation(new ContainerImageAnnotation { Image = "mysql", Tag = "8.3.0" })
.WithDefaultGeneratedPasswordAnnotation()
.WithEnvironment(context =>
{
if (context.ExecutionContext.IsPublishMode)
Expand Down Expand Up @@ -99,6 +100,6 @@ public static IResourceBuilder<T> WithPhpMyAdmin<T>(this IResourceBuilder<T> bui
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<MySqlServerResource> PublishAsContainer(this IResourceBuilder<MySqlServerResource> builder)
{
return builder.WithManifestPublishingCallback(builder.Resource.WriteToManifestAsync);
return builder.WithManifestPublishingCallback(context => context.WriteContainerAsync(builder.Resource));
}
}
18 changes: 0 additions & 18 deletions src/Aspire.Hosting/MySql/MySqlServerResource.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// 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.Publishing;
using Aspire.Hosting.Utils;

namespace Aspire.Hosting.ApplicationModel;
Expand Down Expand Up @@ -52,21 +51,4 @@ internal void AddDatabase(string name, string databaseName)
{
_databases.TryAdd(name, databaseName);
}

internal async Task WriteToManifestAsync(ManifestPublishingContext context)
{
await context.WriteContainerAsync(this).ConfigureAwait(false);

context.Writer.WriteStartObject("inputs"); // "inputs": {
context.Writer.WriteStartObject("password"); // "password": {
context.Writer.WriteString("type", "string"); // "type": "string",
context.Writer.WriteBoolean("secret", true); // "secret": true,
context.Writer.WriteStartObject("default"); // "default": {
context.Writer.WriteStartObject("generate"); // "generate": {
context.Writer.WriteNumber("minLength", 10); // "minLength": 10,
context.Writer.WriteEndObject(); // }
context.Writer.WriteEndObject(); // }
context.Writer.WriteEndObject(); // }
context.Writer.WriteEndObject(); // }
}
}
3 changes: 2 additions & 1 deletion src/Aspire.Hosting/Oracle/OracleDatabaseBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public static IResourceBuilder<OracleDatabaseServerResource> AddOracleDatabase(t
return builder.AddResource(oracleDatabaseServer)
.WithAnnotation(new EndpointAnnotation(ProtocolType.Tcp, port: port, containerPort: 1521))
.WithAnnotation(new ContainerImageAnnotation { Image = "database/free", Tag = "23.3.0.0", Registry = "container-registry.oracle.com" })
.WithDefaultGeneratedPasswordAnnotation()
.WithEnvironment(context =>
{
if (context.ExecutionContext.IsPublishMode)
Expand Down Expand Up @@ -69,6 +70,6 @@ public static IResourceBuilder<OracleDatabaseResource> AddDatabase(this IResourc
/// <returns></returns>
public static IResourceBuilder<OracleDatabaseServerResource> PublishAsContainer(this IResourceBuilder<OracleDatabaseServerResource> builder)
{
return builder.WithManifestPublishingCallback(builder.Resource.WriteToManifestAsync);
return builder.WithManifestPublishingCallback(context => context.WriteContainerAsync(builder.Resource));
}
}
18 changes: 0 additions & 18 deletions src/Aspire.Hosting/Oracle/OracleDatabaseServerResource.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// 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.Publishing;
using Aspire.Hosting.Utils;

namespace Aspire.Hosting.ApplicationModel;
Expand Down Expand Up @@ -52,21 +51,4 @@ internal void AddDatabase(string name, string databaseName)
{
_databases.TryAdd(name, databaseName);
}

internal async Task WriteToManifestAsync(ManifestPublishingContext context)
{
await context.WriteContainerAsync(this).ConfigureAwait(false);

context.Writer.WriteStartObject("inputs"); // "inputs": {
context.Writer.WriteStartObject("password"); // "password": {
context.Writer.WriteString("type", "string"); // "type": "string",
context.Writer.WriteBoolean("secret", true); // "secret": true,
context.Writer.WriteStartObject("default"); // "default": {
context.Writer.WriteStartObject("generate"); // "generate": {
context.Writer.WriteNumber("minLength", 10); // "minLength": 10,
context.Writer.WriteEndObject(); // }
context.Writer.WriteEndObject(); // }
context.Writer.WriteEndObject(); // }
context.Writer.WriteEndObject(); // }
}
}
3 changes: 2 additions & 1 deletion src/Aspire.Hosting/Postgres/PostgresBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public static IResourceBuilder<PostgresServerResource> AddPostgres(this IDistrib
return builder.AddResource(postgresServer)
.WithAnnotation(new EndpointAnnotation(ProtocolType.Tcp, port: port, containerPort: 5432)) // Internal port is always 5432.
.WithAnnotation(new ContainerImageAnnotation { Image = "postgres", Tag = "16.2" })
.WithDefaultGeneratedPasswordAnnotation()
.WithEnvironment("POSTGRES_HOST_AUTH_METHOD", "scram-sha-256")
.WithEnvironment("POSTGRES_INITDB_ARGS", "--auth-host=scram-sha-256 --auth-local=scram-sha-256")
.WithEnvironment(context =>
Expand Down Expand Up @@ -113,6 +114,6 @@ private static void SetPgAdminEnvironmentVariables(EnvironmentCallbackContext co
/// <returns></returns>
public static IResourceBuilder<PostgresServerResource> PublishAsContainer(this IResourceBuilder<PostgresServerResource> builder)
{
return builder.WithManifestPublishingCallback(builder.Resource.WriteToManifestAsync);
return builder.WithManifestPublishingCallback(context => context.WriteContainerAsync(builder.Resource));
}
}
22 changes: 2 additions & 20 deletions src/Aspire.Hosting/Postgres/PostgresServerResource.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// 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.Publishing;
using Aspire.Hosting.Utils;

namespace Aspire.Hosting.ApplicationModel;
Expand Down Expand Up @@ -35,10 +34,10 @@ public string? ConnectionStringExpression
}

/// <summary>
/// Gets the connection string for the PostgreSQL server.
/// Gets the connection string for the SQL Server.
/// </summary>
/// <param name="cancellationToken"> A <see cref="CancellationToken"/> to observe while waiting for the task to complete.</param>
/// <returns>A connection string for the PostgreSQL server in the form "Host=host;Port=port;Username=postgres;Password=password".</returns>
/// <returns>A connection string for the SQL Server in the form "Server=host,port;User ID=sa;Password=password;TrustServerCertificate=true".</returns>
public ValueTask<string?> GetConnectionStringAsync(CancellationToken cancellationToken = default)
{
if (this.TryGetLastAnnotation<ConnectionStringRedirectAnnotation>(out var connectionStringAnnotation))
Expand Down Expand Up @@ -82,21 +81,4 @@ internal void AddDatabase(string name, string databaseName)
{
_databases.TryAdd(name, databaseName);
}

internal async Task WriteToManifestAsync(ManifestPublishingContext context)
{
await context.WriteContainerAsync(this).ConfigureAwait(false);

context.Writer.WriteStartObject("inputs"); // "inputs": {
context.Writer.WriteStartObject("password"); // "password": {
context.Writer.WriteString("type", "string"); // "type": "string",
context.Writer.WriteBoolean("secret", true); // "secret": true,
context.Writer.WriteStartObject("default"); // "default": {
context.Writer.WriteStartObject("generate"); // "generate": {
context.Writer.WriteNumber("minLength", 10); // "minLength": 10,
context.Writer.WriteEndObject(); // }
context.Writer.WriteEndObject(); // }
context.Writer.WriteEndObject(); // }
context.Writer.WriteEndObject(); // }
}
}
33 changes: 33 additions & 0 deletions src/Aspire.Hosting/Publishing/ManifestPublishingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public async Task WriteContainerAsync(ContainerResource container)

await WriteEnvironmentVariablesAsync(container).ConfigureAwait(false);
WriteBindings(container, emitContainerPort: true);
WriteInputs(container);
}

/// <summary>
Expand Down Expand Up @@ -185,6 +186,38 @@ public async Task WriteEnvironmentVariablesAsync(IResource resource)
}
}

/// <summary>
/// Writes the "inputs" annotations for the underlying resource.
/// </summary>
/// <param name="resource">The resource to write inputs for.</param>
public void WriteInputs(IResource resource)
{
if (resource.TryGetAnnotationsOfType<InputAnnotation>(out var inputs))
{
Writer.WriteStartObject("inputs");
foreach (var input in inputs)
{
Writer.WriteStartObject(input.Name);
Writer.WriteString("type", input.Type ?? "string");

if (input.Secret)
{
Writer.WriteBoolean("secret", true);
}

if (input.Default is not null)
{
Writer.WriteStartObject("default");
input.Default.WriteToManifest(this);
Writer.WriteEndObject();
}

Writer.WriteEndObject();
}
Writer.WriteEndObject();
}
}

/// <summary>
/// TODO: Doc Comments
/// </summary>
Expand Down
3 changes: 2 additions & 1 deletion src/Aspire.Hosting/RabbitMQ/RabbitMQBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public static IResourceBuilder<RabbitMQServerResource> AddRabbitMQ(this IDistrib
return builder.AddResource(rabbitMq)
.WithAnnotation(new EndpointAnnotation(ProtocolType.Tcp, port: port, containerPort: 5672))
.WithAnnotation(new ContainerImageAnnotation { Image = "rabbitmq", Tag = "3" })
.WithDefaultGeneratedPasswordAnnotation()
.WithEnvironment("RABBITMQ_DEFAULT_USER", "guest")
.WithEnvironment(context =>
{
Expand All @@ -49,6 +50,6 @@ public static IResourceBuilder<RabbitMQServerResource> AddRabbitMQ(this IDistrib
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<RabbitMQServerResource> PublishAsContainer(this IResourceBuilder<RabbitMQServerResource> builder)
{
return builder.WithManifestPublishingCallback(builder.Resource.WriteToManifestAsync);
return builder.WithManifestPublishingCallback(context => context.WriteContainerAsync(builder.Resource));
}
}
Loading

0 comments on commit decd0a1

Please sign in to comment.