Skip to content

Commit

Permalink
Adding new app setting for key vault so we can set Vault URI (#16948)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Mike Alhayek <[email protected]>
  • Loading branch information
mohit-naroliya and MikeAlhayek authored Nov 6, 2024
1 parent 49b2431 commit e8b37b3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,7 @@ public static ConfigurationManager AddOrchardCoreAzureKeyVault(
private static void AddOrchardCoreAzureKeyVault(
this IConfigurationBuilder builder, IConfiguration configuration, TokenCredential tokenCredential)
{
var keyVaultName = configuration["OrchardCore:OrchardCore_KeyVault_Azure:KeyVaultName"];

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))
{
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 = GetVaultHostUri(configuration);

var configOptions = new AzureKeyVaultConfigurationOptions()
{
Expand All @@ -93,4 +83,33 @@ private static void AddOrchardCoreAzureKeyVault(

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

private static Uri GetVaultHostUri(IConfiguration configuration)
{
var vaultUri = configuration["OrchardCore:OrchardCore_KeyVault_Azure:VaultURI"];

if (!string.IsNullOrWhiteSpace(vaultUri))
{
if (!Uri.TryCreate(vaultUri, UriKind.Absolute, out var uri))
{
throw new Exception("Invalid value used for 'VaultURI' property. Please provide a valid vault host name using the 'OrchardCore:OrchardCore_KeyVault_Azure:VaultURI' settings key.");
}

return uri;
}

var keyVaultName = configuration["OrchardCore:OrchardCore_KeyVault_Azure:KeyVaultName"];

if (string.IsNullOrEmpty(keyVaultName))
{
throw new Exception("The 'KeyVaultName' property is not 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 host))
{
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.");
}

return host;
}
}
3 changes: 2 additions & 1 deletion src/docs/reference/core/KeyVault.Azure/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ In addition, you will need to specify the name of your Azure Key Vault and optio

```json
"OrchardCore_KeyVault_Azure": {
"KeyVaultName": "", // Set the name of your Azure Key Vault.
"KeyVaultName": "", // Set the name of your Azure Key Vault (not required if you are adding VaultURI).
"VaultURI": "", // Set the Vault URI of your Azure Key Vault (not required if you are adding KeyVaultName).
"ReloadInterval": "" // Optional, sets the timespan to wait between attempts at polling the Azure KeyVault for changes. Leave blank to disable reloading.
}
```
Expand Down

0 comments on commit e8b37b3

Please sign in to comment.