microsoft/healthcare-shared-components
Publicmirrored from https://github.com/microsoft/healthcare-shared-componentsAvailable
src/Microsoft.Health.Core/Extensions/IAzureClientBuilderExtensions.cs
65lines · modecode
| 1 | // ------------------------------------------------------------------------------------------------- |
| 2 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | // Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. |
| 4 | // ------------------------------------------------------------------------------------------------- |
| 5 | |
| 6 | using System; |
| 7 | using Azure.Core.Extensions; |
| 8 | using Azure.Identity; |
| 9 | using EnsureThat; |
| 10 | using Microsoft.Extensions.Azure; |
| 11 | using Microsoft.Extensions.Configuration; |
| 12 | |
| 13 | namespace Microsoft.Health.Core.Extensions; |
| 14 | |
| 15 | /// <summary> |
| 16 | /// Provides a set of <see langword="static"/> methods for building Azure clients. |
| 17 | /// </summary> |
| 18 | public static class IAzureClientBuilderExtensions |
| 19 | { |
| 20 | private const string ClientIdKey = "clientId"; |
| 21 | private const string CredentialKey = "credential"; |
| 22 | private const string RetrySection = "credentialRetry"; |
| 23 | |
| 24 | /// <summary> |
| 25 | /// Set the credential to use for this client registration with optional retry settings. |
| 26 | /// </summary> |
| 27 | /// <remarks> |
| 28 | /// Currently only the <c>managedidentity</c> credential type supports user-specified retry settings. Other |
| 29 | /// credential types are skipped and instead rely on the underlying client factory for creation. |
| 30 | /// </remarks> |
| 31 | /// <typeparam name="TClient">The type of the client.</typeparam> |
| 32 | /// <typeparam name="TOptions">The options type the client uses.</typeparam> |
| 33 | /// <param name="builder">The client builder instance.</param> |
| 34 | /// <param name="configuration">The configuration containing the settings for the credential.</param> |
| 35 | /// <returns>The client builder instance.</returns> |
| 36 | /// <exception cref="ArgumentNullException"> |
| 37 | /// <paramref name="builder"/> or <paramref name="configuration"/> is <see langword="null"/>. |
| 38 | /// </exception> |
| 39 | [Obsolete("The retry behavior for IMDS has been improved in Azure.Identity and prevents overriding MaxRetries.")] |
| 40 | public static IAzureClientBuilder<TClient, TOptions> WithRetryableCredential<TClient, TOptions>( |
| 41 | this IAzureClientBuilder<TClient, TOptions> builder, |
| 42 | IConfiguration configuration) |
| 43 | where TOptions : class |
| 44 | { |
| 45 | EnsureArg.IsNotNull(builder, nameof(builder)); |
| 46 | EnsureArg.IsNotNull(configuration, nameof(configuration)); |
| 47 | |
| 48 | string clientId = configuration[ClientIdKey]; |
| 49 | string credentialType = configuration[CredentialKey]; |
| 50 | |
| 51 | // TODO: Support other credential types if necessary |
| 52 | if (string.Equals(credentialType, "managedidentity", StringComparison.OrdinalIgnoreCase)) |
| 53 | { |
| 54 | TokenCredentialOptions options = new(); |
| 55 | configuration |
| 56 | .GetSection(RetrySection) |
| 57 | .Bind(options.Retry); |
| 58 | |
| 59 | ManagedIdentityCredential credential = new(clientId, options); |
| 60 | return builder.WithCredential(credential); |
| 61 | } |
| 62 | |
| 63 | return builder; |
| 64 | } |
| 65 | } |
| 66 | |