Skip to content

Commit

Permalink
Add descriptive exception for Azure-Valut service (#15178)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek authored and hishamco committed Feb 1, 2024
1 parent 554dc2b commit 9de4c4a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 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 @@ -70,26 +70,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))
{
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))
{
reloadInterval = TimeSpan.FromSeconds(interval);
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 9de4c4a

Please sign in to comment.