-
Notifications
You must be signed in to change notification settings - Fork 931
Refactor user secrets management to use DI-based factory pattern with thread-safe synchronization #12482
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
Closed
Closed
Refactor user secrets management to use DI-based factory pattern with thread-safe synchronization #12482
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
bdfddf4
Initial plan
Copilot 9222e94
Fix SecretsStore concurrent access issue with locking mechanism
Copilot 02de777
Add specific test case for SQL Server + RabbitMQ concurrent secret wr…
Copilot b852826
Replace SemaphoreSlim with simple object locks
Copilot ed54150
Share lock between SecretsStore and UserSecretsDeploymentStateManager
Copilot 2c9ad74
Refactor to use SemaphoreSlim and allow DeploymentStateManagerBase ov…
Copilot 482e759
Remove unnecessary Task.Run in UserSecretsDeploymentStateManager
Copilot 8ab3628
Addressing PR comments
Copilot c83d9cf
Complete refactoring to use DI-based UserSecretsManager with factory …
Copilot 6308f05
Change UserSecretsDeploymentStateManager to take IUserSecretsManager …
Copilot f557876
Make IUserSecretsManager and UserSecretsDeploymentStateManager internal
Copilot 65060b4
Make IUserSecretsManager injectable via constructor with factory fall…
Copilot 998374d
Extract FlattenJsonObject to JsonFlattener static class to fix Azure …
Copilot 17002e3
Make IUserSecretsManager nullable in VersionCheckService to handle ap…
Copilot 61f9af9
Add NoopUserSecretsManager to handle apps without UserSecretsId with …
Copilot 4f29f84
Fix NoopUserSecretsManager to implement all IUserSecretsManager inter…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,93 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Text.Json.Nodes; | ||
|
|
||
| namespace Aspire.Hosting.Publishing.Internal; | ||
|
|
||
| /// <summary> | ||
| /// Provides utility methods for flattening and unflattening JSON objects using colon-separated keys. | ||
| /// </summary> | ||
| public static class JsonFlattener | ||
| { | ||
| /// <summary> | ||
| /// Flattens a JsonObject using colon-separated keys for configuration compatibility. | ||
| /// Handles both nested objects and arrays with indexed keys. | ||
| /// </summary> | ||
| /// <param name="source">The source JsonObject to flatten.</param> | ||
| /// <returns>A flattened JsonObject.</returns> | ||
| public static JsonObject FlattenJsonObject(JsonObject source) | ||
| { | ||
| var result = new JsonObject(); | ||
| FlattenJsonObjectRecursive(source, string.Empty, result); | ||
| return result; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Unflattens a JsonObject that uses colon-separated keys back into a nested structure. | ||
| /// Handles both nested objects and arrays with indexed keys. | ||
| /// </summary> | ||
| /// <param name="source">The flattened JsonObject to unflatten.</param> | ||
| /// <returns>An unflattened JsonObject with nested structure.</returns> | ||
| public static JsonObject UnflattenJsonObject(JsonObject source) | ||
| { | ||
| var result = new JsonObject(); | ||
|
|
||
| foreach (var kvp in source) | ||
| { | ||
| var keys = kvp.Key.Split(':'); | ||
| var current = result; | ||
|
|
||
| for (var i = 0; i < keys.Length - 1; i++) | ||
| { | ||
| var key = keys[i]; | ||
| if (!current.TryGetPropertyValue(key, out var existing) || existing is not JsonObject) | ||
| { | ||
| var newObject = new JsonObject(); | ||
| current[key] = newObject; | ||
| current = newObject; | ||
| } | ||
| else | ||
| { | ||
| current = existing.AsObject(); | ||
| } | ||
| } | ||
|
|
||
| current[keys[^1]] = kvp.Value?.DeepClone(); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| private static void FlattenJsonObjectRecursive(JsonObject source, string prefix, JsonObject result) | ||
| { | ||
| foreach (var kvp in source) | ||
| { | ||
| var key = string.IsNullOrEmpty(prefix) ? kvp.Key : $"{prefix}:{kvp.Key}"; | ||
|
|
||
| if (kvp.Value is JsonObject nestedObject) | ||
| { | ||
| FlattenJsonObjectRecursive(nestedObject, key, result); | ||
| } | ||
| else if (kvp.Value is JsonArray array) | ||
| { | ||
| for (var i = 0; i < array.Count; i++) | ||
| { | ||
| var arrayKey = $"{key}:{i}"; | ||
| if (array[i] is JsonObject arrayObject) | ||
| { | ||
| FlattenJsonObjectRecursive(arrayObject, arrayKey, result); | ||
| } | ||
| else | ||
| { | ||
| result[arrayKey] = array[i]?.DeepClone(); | ||
| } | ||
| } | ||
| } | ||
| else | ||
| { | ||
| result[key] = kvp.Value?.DeepClone(); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,50 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Text.Json.Nodes; | ||
| using Microsoft.Extensions.Configuration; | ||
|
|
||
| namespace Aspire.Hosting.UserSecrets; | ||
|
|
||
| /// <summary> | ||
| /// Manages user secrets for an application, providing thread-safe read and write operations. | ||
| /// </summary> | ||
| internal interface IUserSecretsManager | ||
| { | ||
| /// <summary> | ||
| /// Gets the path to the user secrets file. | ||
| /// </summary> | ||
| string FilePath { get; } | ||
|
|
||
| /// <summary> | ||
| /// Attempts to set a user secret value synchronously. | ||
| /// </summary> | ||
| /// <param name="name">The name of the secret.</param> | ||
| /// <param name="value">The value of the secret.</param> | ||
| /// <returns>True if the secret was set successfully; otherwise, false.</returns> | ||
| bool TrySetSecret(string name, string value); | ||
|
|
||
| /// <summary> | ||
| /// Attempts to set a user secret value asynchronously. | ||
| /// </summary> | ||
| /// <param name="name">The name of the secret.</param> | ||
| /// <param name="value">The value of the secret.</param> | ||
| /// <param name="cancellationToken">Cancellation token.</param> | ||
| /// <returns>True if the secret was set successfully; otherwise, false.</returns> | ||
| Task<bool> TrySetSecretAsync(string name, string value, CancellationToken cancellationToken = default); | ||
|
|
||
| /// <summary> | ||
| /// Gets a secret value if it exists in configuration, or sets it using the value generator if it doesn't. | ||
| /// </summary> | ||
| /// <param name="configuration">The configuration manager to check and update.</param> | ||
| /// <param name="name">The name of the secret.</param> | ||
| /// <param name="valueGenerator">Function to generate the value if it doesn't exist.</param> | ||
| void GetOrSetSecret(IConfigurationManager configuration, string name, Func<string> valueGenerator); | ||
|
|
||
| /// <summary> | ||
| /// Saves state to user secrets asynchronously (for deployment state manager). | ||
| /// </summary> | ||
| /// <param name="state">The state to save as a JSON object.</param> | ||
| /// <param name="cancellationToken">Cancellation token.</param> | ||
| Task SaveStateAsync(JsonObject state, CancellationToken cancellationToken = default); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot This should no longer be nullable.