Skip to content

Commit

Permalink
Add descriptive exception for Azure-Valut service
Browse files Browse the repository at this point in the history
Fix #15160
  • Loading branch information
MikeAlhayek committed Jan 26, 2024
1 parent b23ec3e commit 6783ece
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
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);
}
}
}

0 comments on commit 6783ece

Please sign in to comment.