I have a problem with the Azure app configuration - key vault reference. Currently, I have an app configuration key (FunctionApp:Replication:Regions) with many values (asia, we, sae).So the connection is one to many. I added key-vault reference to each value. But the problem is that I don't know how to read the data from the function app, to get the value in the key vault.
Currently, I am using two services:
ConfigurationClient
- To get the all values in one key.
IConfigurationRefresher
to refresh the app configuration, to get the latest changes that are made, without the need of a function restart.
Example (StorageService):
private readonly ConfigurationClient _client;
private readonly IConfigurationRefresher _configurationRefresher;
public StorageService(ConfigurationClient client, IConfigurationRefresherProvider refresherProvider)
{
_client = client,
_configurationRefresher = refresherProvider.Refreshers.First();
}
SettingSelector settingSelector = new SettingSelector()
{
KeyFilter = "FunctionApp:Replication:Regions"
};
var regions = _client.GetConfigurationSettings(settingSelector).ToList();
Example (Startup):
public override void Configure(IFunctionsHostBuilder builder){
ConfigurationClient appConfigurations = new
ConfigurationClient(appConfigurationsConnectionString);
builder.Services.AddSingleton<ConfigurationClient>(appConfigurations);
}
public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
{
builder.ConfigurationBuilder.AddAzureAppConfiguration(options =>
{
options.Connect(Environment.GetEnvironmentVariable("ConnectionString")).Select("FunctionApp:*").ConfigureRefresh(refreshOptions =>
{
refreshOptions.Register("FunctionApp:Replication:Regions", refreshAll: true);
});
});
}
When i try to get the values - var regions = _client.GetConfigurationSettings(settingSelector).ToList();
, it returns this :
{"uri":"https://appconfigurationkeyvalut.vault.azure.net/secrets/tableasia"},
{"uri":"https://appconfigurationkeyvalut.vault.azure.net/secrets/tablewe"},
{"uri":"https://appconfigurationkeyvalut.vault.azure.net/secrets/tablesea"}
And i dont know how to get the value from the key vault. Any ideas ?
ConfigurationClient
. It will not resolve the key vault reference for you. The Microsoft.Extensions.Configuration.AzureAppConfiguration
library will resolve the key vault reference and make secret values available in IConfiguration
.startup.cs
looks something like this:public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
{
builder.ConfigurationBuilder.AddAzureAppConfiguration(options =>
{
options.Connect(Environment.GetEnvironmentVariable("ConnectionString"))
// Load all keys that start with `FunctionApp:` and have no label
.Select("FunctionApp:*")
// Configure to reload configuration if the registered sentinel key is modified
.ConfigureRefresh(refreshOptions =>
refreshOptions.Register("FunctionApp:Sentinel", refreshAll: true));
});
}
IConfiguration
automatically in your Run
function. For example, _configuration["FunctionApp:Replication:Regions:Asia"]
.