Skip to content
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

Add descriptive exception for Azure-Vault service #15178

Merged
merged 2 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/OrchardCore.Cms.Web/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
// Add 'AddOrchardCoreAzureKeyVault()' to the Generic Host in 'CreateHostBuilder() section'.
//"OrchardCore_KeyVault_Azure": {
// "KeyVaultName": "", // Set the name of your Azure Key Vault.
// "ReloadInterval": // Optional, timespan to wait between attempts at polling the Azure KeyVault for changes. Leave blank to disable reloading.
// "ReloadInterval": null // Optional, timespan to wait between attempts at polling the Azure KeyVault for changes. Leave blank to disable reloading.
//},
// See https://docs.orchardcore.net/en/latest/docs/reference/modules/Users/Configuration/#custom-paths
//"OrchardCore_Users": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ public static IHostBuilder AddOrchardCoreAzureKeyVault(this IHostBuilder builder
/// </summary>
public static IWebHostBuilder AddOrchardCoreAzureKeyVault(this IWebHostBuilder builder, TokenCredential tokenCredential = null)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
ArgumentNullException.ThrowIfNull(builder);

builder.ConfigureAppConfiguration((context, builder) =>
{
Expand All @@ -62,10 +59,7 @@ public static IWebHostBuilder AddOrchardCoreAzureKeyVault(this IWebHostBuilder b
public static ConfigurationManager AddOrchardCoreAzureKeyVault(
this ConfigurationManager manager, TokenCredential tokenCredential = null)
{
if (manager == null)
{
throw new ArgumentNullException(nameof(manager));
}
ArgumentNullException.ThrowIfNull(manager);

// The 'ConfigurationManager' is a builder and also an 'IConfigurationRoot' allowing to
// get values from the providers already added without having to build a configuration.
Expand All @@ -79,26 +73,29 @@ private static void AddOrchardCoreAzureKeyVault(
{
var keyVaultName = configuration["OrchardCore:OrchardCore_KeyVault_Azure:KeyVaultName"];

TimeSpan? reloadInterval = null;
if (double.TryParse(configuration["OrchardCore:OrchardCore_KeyVault_Azure:ReloadInterval"], out var interval))
if (string.IsNullOrEmpty(keyVaultName))
{
reloadInterval = TimeSpan.FromSeconds(interval);
throw new Exception("The 'KeyVaultName' property is no configured. Please configure it by specifying the 'OrchardCore:OrchardCore_KeyVault_Azure:KeyVaultName' settings key.");
}

if (!Uri.TryCreate($"https://{keyVaultName}.vault.azure.net", UriKind.Absolute, out var keyVaultEndpointUri))
{
throw new Exception("Invalid value used for 'KeyVaultName' property. Please provide a valid key-vault name using the 'OrchardCore:OrchardCore_KeyVault_Azure:KeyVaultName' settings key.");
}

var keyVaultEndpointUri = new Uri("https://" + keyVaultName + ".vault.azure.net");
var configOptions = new AzureKeyVaultConfigurationOptions()
{
Manager = new AzureKeyVaultSecretManager(),
ReloadInterval = reloadInterval,
};

if (double.TryParse(configuration["OrchardCore:OrchardCore_KeyVault_Azure:ReloadInterval"], out var interval))
{
configOptions.ReloadInterval = TimeSpan.FromSeconds(interval);
}

tokenCredential ??= new DefaultAzureCredential(includeInteractiveCredentials: true);

builder.AddAzureKeyVault(
keyVaultEndpointUri,
tokenCredential,
configOptions
);
builder.AddAzureKeyVault(keyVaultEndpointUri, tokenCredential, configOptions);
}
}
}
Loading