-
Notifications
You must be signed in to change notification settings - Fork 534
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Save password parameter default values to user secrets
- Loading branch information
1 parent
bda3168
commit 03ffc39
Showing
24 changed files
with
234 additions
and
157 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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
56 changes: 56 additions & 0 deletions
56
src/Aspire.Hosting/ApplicationModel/UserSecretsParameterDefault.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,56 @@ | ||
// 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 Aspire.Hosting.Publishing; | ||
using Microsoft.Extensions.Configuration.UserSecrets; | ||
using Microsoft.Extensions.SecretManager.Tools.Internal; | ||
|
||
namespace Aspire.Hosting.ApplicationModel; | ||
|
||
/// <summary> | ||
/// Wraps a <see cref="ParameterDefault"/> such that the default value is saved to the project's user secrets store. | ||
/// </summary> | ||
/// <param name="applicationName">The application name.</param> | ||
/// <param name="parameterName">The parameter name.</param> | ||
/// <param name="parameterDefault">The parameter with default value.</param> | ||
public sealed class UserSecretsParameterDefault(string applicationName, string parameterName, ParameterDefault parameterDefault) | ||
: ParameterDefault | ||
{ | ||
/// <inheritdoc/> | ||
public override string GetDefaultValue() | ||
{ | ||
var value = parameterDefault.GetDefaultValue(); | ||
var configurationKey = $"{ParameterResourceBuilderExtensions.ConfigurationSectionKey}:{parameterName}"; | ||
if (!TrySetUserSecret(applicationName, configurationKey, value)) | ||
{ | ||
throw new DistributedApplicationException($"Failed to set value for parameter '{parameterName}' in application '{applicationName}' to user secrets."); | ||
} | ||
return value; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override void WriteToManifest(ManifestPublishingContext context) => parameterDefault.WriteToManifest(context); | ||
|
||
private static bool TrySetUserSecret(string applicationName, string name, string value) | ||
{ | ||
if (!string.IsNullOrEmpty(applicationName)) | ||
{ | ||
var appAssembly = Assembly.Load(new AssemblyName(applicationName)); | ||
if (appAssembly is not null && appAssembly.GetCustomAttribute<UserSecretsIdAttribute>()?.UserSecretsId is { } userSecretsId) | ||
{ | ||
// Save the value to the secret store | ||
try | ||
{ | ||
var secretsStore = new SecretsStore(userSecretsId); | ||
secretsStore.Set(name, value); | ||
secretsStore.Save(); | ||
return true; | ||
} | ||
catch (Exception) { } | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,2 +1,6 @@ | ||
#nullable enable | ||
|
||
Aspire.Hosting.ApplicationModel.UserSecretsParameterDefault | ||
Aspire.Hosting.ApplicationModel.UserSecretsParameterDefault.UserSecretsParameterDefault(string! applicationName, string! parameterName, Aspire.Hosting.ApplicationModel.ParameterDefault! parameterDefault) -> void | ||
const Aspire.Hosting.ParameterResourceBuilderExtensions.ConfigurationSectionKey = "Parameters" -> string! | ||
override Aspire.Hosting.ApplicationModel.UserSecretsParameterDefault.GetDefaultValue() -> string! | ||
override Aspire.Hosting.ApplicationModel.UserSecretsParameterDefault.WriteToManifest(Aspire.Hosting.Publishing.ManifestPublishingContext! context) -> void |
Oops, something went wrong.