| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using Azure.Core; |
| | 6 | | using Azure.Extensions.AspNetCore.Configuration.Secrets; |
| | 7 | | using Azure.Security.KeyVault.Secrets; |
| | 8 | |
|
| | 9 | | #pragma warning disable AZC0001 // Extension methods have to be in the correct namespace to appear in intellisense. |
| | 10 | | namespace Microsoft.Extensions.Configuration |
| | 11 | | #pragma warning restore |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// Extension methods for registering <see cref="AzureKeyVaultConfigurationProvider"/> with <see cref="IConfiguratio |
| | 15 | | /// </summary> |
| | 16 | | public static class AzureKeyVaultConfigurationExtensions |
| | 17 | | { |
| | 18 | | /// <summary> |
| | 19 | | /// Adds an <see cref="IConfigurationProvider"/> that reads configuration values from the Azure KeyVault. |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="configurationBuilder">The <see cref="IConfigurationBuilder"/> to add to.</param> |
| | 22 | | /// <param name="vaultUri">The Azure Key Vault uri.</param> |
| | 23 | | /// <param name="credential">The credential to to use for authentication.</param> |
| | 24 | | /// <returns>The <see cref="IConfigurationBuilder"/>.</returns> |
| | 25 | | public static IConfigurationBuilder AddAzureKeyVault( |
| | 26 | | this IConfigurationBuilder configurationBuilder, |
| | 27 | | Uri vaultUri, |
| | 28 | | TokenCredential credential) |
| | 29 | | { |
| 0 | 30 | | return AddAzureKeyVault(configurationBuilder, vaultUri, credential, KeyVaultSecretManager.Instance); |
| | 31 | | } |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// Adds an <see cref="IConfigurationProvider"/> that reads configuration values from the Azure KeyVault. |
| | 35 | | /// </summary> |
| | 36 | | /// <param name="configurationBuilder">The <see cref="IConfigurationBuilder"/> to add to.</param> |
| | 37 | | /// <param name="vaultUri">Azure Key Vault uri.</param> |
| | 38 | | /// <param name="credential">The credential to to use for authentication.</param> |
| | 39 | | /// <param name="manager">The <see cref="KeyVaultSecretManager"/> instance used to control secret loading.</para |
| | 40 | | /// <returns>The <see cref="IConfigurationBuilder"/>.</returns> |
| | 41 | | public static IConfigurationBuilder AddAzureKeyVault( |
| | 42 | | this IConfigurationBuilder configurationBuilder, |
| | 43 | | Uri vaultUri, |
| | 44 | | TokenCredential credential, |
| | 45 | | KeyVaultSecretManager manager) |
| | 46 | | { |
| 0 | 47 | | return AddAzureKeyVault(configurationBuilder, new AzureKeyVaultConfigurationOptions(vaultUri, credential) |
| 0 | 48 | | { |
| 0 | 49 | | Manager = manager |
| 0 | 50 | | }); |
| | 51 | | } |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// Adds an <see cref="IConfigurationProvider"/> that reads configuration values from the Azure KeyVault. |
| | 55 | | /// </summary> |
| | 56 | | /// <param name="configurationBuilder">The <see cref="IConfigurationBuilder"/> to add to.</param> |
| | 57 | | /// <param name="client">The <see cref="SecretClient"/> to use for retrieving values.</param> |
| | 58 | | /// <param name="manager">The <see cref="KeyVaultSecretManager"/> instance used to control secret loading.</para |
| | 59 | | /// <returns>The <see cref="IConfigurationBuilder"/>.</returns> |
| | 60 | | public static IConfigurationBuilder AddAzureKeyVault( |
| | 61 | | this IConfigurationBuilder configurationBuilder, |
| | 62 | | SecretClient client, |
| | 63 | | KeyVaultSecretManager manager) |
| | 64 | | { |
| 0 | 65 | | return configurationBuilder.Add(new AzureKeyVaultConfigurationSource(new AzureKeyVaultConfigurationOptions() |
| 0 | 66 | | { |
| 0 | 67 | | Client = client, |
| 0 | 68 | | Manager = manager |
| 0 | 69 | | })); |
| | 70 | | } |
| | 71 | |
|
| | 72 | | /// <summary> |
| | 73 | | /// Adds an <see cref="IConfigurationProvider"/> that reads configuration values from the Azure KeyVault. |
| | 74 | | /// </summary> |
| | 75 | | /// <param name="configurationBuilder">The <see cref="IConfigurationBuilder"/> to add to.</param> |
| | 76 | | /// <param name="options">The <see cref="AzureKeyVaultConfigurationOptions"/> to use.</param> |
| | 77 | | /// <returns>The <see cref="IConfigurationBuilder"/>.</returns> |
| | 78 | | private static IConfigurationBuilder AddAzureKeyVault(this IConfigurationBuilder configurationBuilder, AzureKeyV |
| | 79 | | { |
| 0 | 80 | | Argument.AssertNotNull(configurationBuilder, nameof(configurationBuilder)); |
| 0 | 81 | | Argument.AssertNotNull(options, nameof(configurationBuilder)); |
| 0 | 82 | | Argument.AssertNotNull(options.Client, $"{nameof(options)}.{nameof(options.Client)}"); |
| 0 | 83 | | Argument.AssertNotNull(options.Manager, $"{nameof(options)}.{nameof(options.Manager)}"); |
| | 84 | |
|
| 0 | 85 | | configurationBuilder.Add(new AzureKeyVaultConfigurationSource(options)); |
| | 86 | |
|
| 0 | 87 | | return configurationBuilder; |
| | 88 | | } |
| | 89 | | } |
| | 90 | | } |