-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Add support for Microsoft.Extensions.Configuration #53527
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
Draft
m-nash
wants to merge
6
commits into
main
Choose a base branch
from
ioptions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6fe419d
Initial draft of IConfiguration
m-nash 7d2b64e
merge main
m-nash 716c94f
update api
m-nash 7ae7e49
add missing annotations
m-nash 5fbf899
add missing annotations
m-nash 3d9c526
add ClientConnection ctor to ArmClient
m-nash 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
65 changes: 65 additions & 0 deletions
65
sdk/core/System.ClientModel/src/Convenience/ConfigurationManagerExtensions.cs
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,65 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.ClientModel.Primitives; | ||
| using Microsoft.Extensions.Configuration; | ||
|
|
||
| namespace System.ClientModel; | ||
|
|
||
| /// <summary> | ||
| /// . | ||
| /// </summary> | ||
| public static class ConfigurationManagerExtensions | ||
| { | ||
| private static readonly HashSet<string> FirstClassProperties = new() { "CredentialSource", "Key", "KEY", "Endpoint" }; | ||
|
|
||
| /// <summary> | ||
| /// . | ||
| /// </summary> | ||
| /// <param name="configuration"></param> | ||
| /// <param name="sectionName"></param> | ||
| /// <returns></returns> | ||
| public static ClientConnection GetConnection(this IConfigurationManager configuration, string sectionName) | ||
| => configuration.GetSection(sectionName).GetConnection(); | ||
|
|
||
| /// <summary> | ||
| /// . | ||
| /// </summary> | ||
| /// <param name="section"></param> | ||
| /// <returns></returns> | ||
| public static ClientConnection GetConnection(this IConfigurationSection section) | ||
| { | ||
| var credential = CreateCredentials(section); | ||
christothes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Dictionary<string, string>? metadata = null; | ||
| foreach (var child in section.GetChildren()) | ||
| { | ||
| if (!FirstClassProperties.Contains(child.Key) && !string.IsNullOrEmpty(child.Value)) | ||
| { | ||
| metadata ??= new Dictionary<string, string>(); | ||
| metadata[child.Key] = child.Value!; | ||
| } | ||
| } | ||
| return new ClientConnection(section.Key, section["Endpoint"] ?? "$auto", credential.Credential, credential.Kind, metadata, section); | ||
m-nash marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| internal static (object? Credential, CredentialKind Kind) CreateCredentials(IConfigurationSection section) | ||
| { | ||
| CredentialKind credentialKind; | ||
| object? credential = default; | ||
| if (section["Credential:CredentialSource"] is null) | ||
| { | ||
| credentialKind = CredentialKind.None; | ||
| } | ||
| else if (section["Credential:CredentialSource"]!.Equals("ApiKey", StringComparison.Ordinal)) | ||
| { | ||
| credentialKind = CredentialKind.ApiKeyString; | ||
m-nash marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| credential = section["Credential:Key"]; | ||
| } | ||
| else | ||
| { | ||
| throw new Exception($"Unsupported credential source '{section["Credential:CredentialSource"]}'."); | ||
| } | ||
|
|
||
| return (credential, credentialKind); | ||
| } | ||
| } | ||
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
72 changes: 72 additions & 0 deletions
72
sdk/identity/Azure.Identity/src/ConfigurationManagerExtensions.cs
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,72 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.ClientModel.Primitives; | ||
| using System.Collections.Generic; | ||
| using Microsoft.Extensions.Configuration; | ||
|
|
||
| namespace Azure.Identity | ||
| { | ||
| /// <summary> | ||
| /// . | ||
| /// </summary> | ||
| public static class ConfigurationManagerExtensions | ||
| { | ||
| private static readonly HashSet<string> FirstClassProperties = new() { "CredentialSource", "Key", "KEY", "Endpoint" }; | ||
|
|
||
| /// <summary> | ||
| /// . | ||
| /// </summary> | ||
| /// <param name="configuration"></param> | ||
| /// <param name="sectionName"></param> | ||
| /// <returns></returns> | ||
| public static ClientConnection GetAzureConnection(this IConfigurationManager configuration, string sectionName) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we should not give it a nice name: GetClientConnection. This method will work with both Azure and non-Azure scenarios. I even wonder if we could call the SCM variant of the method the same and use the new C# attribute (overload priority) to resolve ambiguity |
||
| { | ||
| IConfigurationSection section = configuration.GetSection(sectionName); | ||
| var credential = CreateCredentials(section.GetSection("Credential")); | ||
| Dictionary<string, string> metadata = null; | ||
| foreach (var child in section.GetChildren()) | ||
| { | ||
| if (!FirstClassProperties.Contains(child.Key) && !string.IsNullOrEmpty(child.Value)) | ||
| { | ||
| metadata ??= new Dictionary<string, string>(); | ||
| metadata[child.Key] = child.Value!; | ||
| } | ||
| } | ||
| return new ClientConnection(section.Key, section["Endpoint"] ?? "$auto", credential.Credential, credential.Kind, metadata, section); | ||
| } | ||
|
|
||
| private static (object Credential, CredentialKind Kind) CreateCredentials(IConfigurationSection credentialSection) | ||
| { | ||
| CredentialKind credentialKind; | ||
| object credential = default; | ||
| if (credentialSection["CredentialSource"] is null) | ||
| { | ||
| credentialKind = CredentialKind.None; | ||
| } | ||
| else if (credentialSection["CredentialSource"].Equals("ApiKey", StringComparison.Ordinal)) | ||
| { | ||
| credentialKind = CredentialKind.ApiKeyString; | ||
| credential = credentialSection["Key"]; | ||
| } | ||
| else | ||
| { | ||
| credentialKind = CredentialKind.TokenCredential; | ||
| DefaultAzureCredentialOptions dacOptions = new(); | ||
| ConfigureDefaultAzureCredentialOptions(credentialSection, dacOptions); | ||
| credential = new DefaultAzureCredential(dacOptions); | ||
| } | ||
|
|
||
| return (credential, credentialKind); | ||
| } | ||
|
|
||
| internal static void ConfigureDefaultAzureCredentialOptions(IConfigurationSection section, DefaultAzureCredentialOptions options) | ||
| { | ||
| if (section["CredentialSource"] is string credentialSource) | ||
| { | ||
| options.CredentialSource = credentialSource; | ||
| } | ||
| } | ||
| } | ||
| } | ||
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,36 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.ClientModel; | ||
| using Azure.Core; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Hosting; | ||
|
|
||
| namespace Azure.Identity | ||
| { | ||
| /// <summary> | ||
| /// . | ||
| /// </summary> | ||
| public static class HostBuilderExtensions | ||
| { | ||
| /// <summary> | ||
| /// . | ||
| /// </summary> | ||
| /// <param name="host"></param> | ||
| /// <param name="sectionName"></param> | ||
| /// <returns></returns> | ||
| public static IHostBuilder AddAzureCredential(this IHostBuilder host, string sectionName) | ||
| { | ||
| host.ConfigureServices((context, services) => | ||
| { | ||
| DefaultAzureCredentialOptions options = new(); | ||
| ConfigurationManagerExtensions.ConfigureDefaultAzureCredentialOptions(context.Configuration.GetSection(sectionName), options); | ||
| DefaultAzureCredential credential = new DefaultAzureCredential(options); | ||
| services.AddSingleton<TokenCredential>(sp => credential); | ||
| services.AddSingleton<AuthenticationTokenProvider>(sp => credential); | ||
| }); | ||
|
|
||
| return host; | ||
| } | ||
| } | ||
| } |
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
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.
I wonder if we dont need an optional parameter called "format" (or something like that). This would allow us to add support for all the combinations in aspire. Unless we are fine with probing all the formats.