-
Notifications
You must be signed in to change notification settings - Fork 551
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First stab at secret resource implementation.
- Loading branch information
1 parent
2a0485a
commit 473f6c9
Showing
6 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// 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; | ||
|
||
public class SecretResource(string name, SecretStoreResource parent) : Resource(name), IResourceWithParent<SecretStoreResource> | ||
{ | ||
public SecretStoreResource Parent => parent; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// 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; | ||
|
||
public class SecretStoreResource(string name) : Resource(name) | ||
{ | ||
} |
59 changes: 59 additions & 0 deletions
59
src/Aspire.Hosting/Extensions/SecretResourceBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// 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.ApplicationModel; | ||
using Aspire.Hosting.Publishing; | ||
|
||
namespace Aspire.Hosting; | ||
|
||
public static class SecretResourceBuilderExtensions | ||
{ | ||
public static IResourceBuilder<SecretStoreResource> AddSecretStore(this IDistributedApplicationBuilder builder, string name) | ||
{ | ||
var resource = new SecretStoreResource(name); | ||
return builder.AddResource(resource) | ||
.WithManifestPublishingCallback(WriteSecretStoreToManifest); | ||
} | ||
|
||
private static void WriteSecretStoreToManifest(ManifestPublishingContext context) | ||
{ | ||
context.Writer.WriteString("type", "secrets.store.v0"); | ||
} | ||
|
||
public static IResourceBuilder<SecretResource> AddSecret(this IResourceBuilder<SecretStoreResource> builder, string name) | ||
{ | ||
var resource = new SecretResource(name, builder.Resource); | ||
return builder.ApplicationBuilder.AddResource(resource) | ||
.WithManifestPublishingCallback(context => WriteSecretToManifest(context, resource)); | ||
} | ||
|
||
private static void WriteSecretToManifest(ManifestPublishingContext context, SecretResource secret) | ||
{ | ||
context.Writer.WriteString("type", "secrets.secret.v0"); | ||
context.Writer.WriteString("parent", secret.Parent.Name); | ||
context.Writer.WriteString("value", $"{{{secret.Name}.inputs.value}}"); | ||
context.Writer.WriteStartObject("inputs"); | ||
context.Writer.WriteStartObject("value"); | ||
context.Writer.WriteString("type", "string"); | ||
context.Writer.WriteString("secret", "true"); | ||
context.Writer.WriteEndObject(); | ||
context.Writer.WriteEndObject(); | ||
} | ||
|
||
public static IResourceBuilder<T> WithEnvironment<T>(this IResourceBuilder<T> builder, string name, IResourceBuilder<SecretResource> secret) where T: IResourceWithEnvironment | ||
{ | ||
return builder.WithEnvironment(context => | ||
{ | ||
if (context.PublisherName == "manifest") | ||
{ | ||
context.EnvironmentVariables[name] = $"{{{secret.Resource.Name}.value}}"; | ||
return; | ||
} | ||
|
||
var configurationKey = $"Secrets:{secret.Resource.Parent.Name}:{secret.Resource.Name}"; | ||
|
||
context.EnvironmentVariables[name] = builder.ApplicationBuilder.Configuration[configurationKey] | ||
?? throw new DistributedApplicationException($"Environment variable '{name}' could not be added because configuration key '{configurationKey}' not present."); | ||
}); | ||
} | ||
} |