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

Adding new app setting for key vault so we can set Vault URI #16948

Merged
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
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