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

AzureAppConfigurationBuilder does not match keys in "Strict" mode #226

Open
rehret opened this issue Sep 6, 2023 · 0 comments
Open

AzureAppConfigurationBuilder does not match keys in "Strict" mode #226

rehret opened this issue Sep 6, 2023 · 0 comments

Comments

@rehret
Copy link

rehret commented Sep 6, 2023

AzureAppConfigurationBuilder does not match keys in "Strict" mode

When operating in "Strict" mode, AzureAppConfigurationBuilder does not properly match keys from the config section. However, the keys are properly matched in "Greedy" mode.

Functional impact

When utilizing AzureAppConfigurationBuilder in "Strict" mode and using a key filter and prefix, keys are never matched. That is to say values specified in Azure App Configuration are never returned.

Minimal repro steps

Sample project can be found here: rehret/ConfigBuilders.AzureAppConfig.StrictModeTest.
The README at the root contains instructions for setup and execution of the test scenario.

Generally speaking, the reproduction steps are:

  1. Define an Azure App Configuration config builder in the Web.config / App.config for the project.
    • The mode must be "Strict"
    • A key filter and prefix must be used
    • stripPrefix must be "true"
  2. Utilize the defined config builder in any configuration section by adding it to the section's configBuilders="..." attribute.
  3. Attempt to read a key defined in the configuration section whose value should be provided by the Azure App Configuration config builder.
  4. The returned value will be null.

Expected result

When reading configuration backed by an Azure App Configuration config builder in "Strict" mode with a key filter and prefix, it is expected that the value specified in Azure App Configuration with a matching key ("key" here being prefix + key) is returned.

Actual result

In "Strict" mode with a key filter and prefix, rather than returning the value specified in Azure App Configuration, AzureAppConfigurationBuilder returns null.

Further technical details

I believe the following is contributing to this:

  • AzureAppConfigurationBuilder performs greedy initialization in "Strict" mode in order to reduce the number of calls to Azure App Configuration. 1
  • Greedy intialization calls GetAllValues(string prefix) so that all values can be cached immediately. 2
  • In AzureAppConfigurationBuilder's GetAllValuesAsync(string prefix), the keys (and values) matching the prefix are retrieved and returned. Note that the prefix is left inact in the KeyValuePair key. 3
  • The returned keys and values are then stored in _cachedValues with the prefix being removed from the keys. 4
  • Later, in ProcessConfigurationSection(ConfigurationSection configSection), GetValueInternal(string key) is called when in "Strict" mode in order to retrieve the value from _cachedValues, if it exists there, or to read the value from the configuration source if it does not exist in _cachedValues. 5
  • The key used for cache lookup in GetValueInternal(string key) is prefix + key. 6 Since the keys were added to _cachedValues without the prefix, it's a cache miss, so GetValue(string key) is called. 7
  • In AzureAppConfigurationBuilder's GetValue(string key), if a key filter is in use it simply returns null. 8 The expectation is that GetValue(string key) is never called for AzureAppConfigurationBuilder because it tries to greedily initialize, even in "Strict" mode, meaning it should already have the keys and values preloaded in _cachedValues. However, since the prefix is inconsistently handled in this particular path, every lookup results in a cache miss, which means a null value is always returned here.

Footnotes

  1. https://github.com/aspnet/MicrosoftConfigurationBuilders/blob/ea50da297ce168877f5121b8a1409b89698e2690/src/AzureAppConfig/AzureAppConfigurationBuilder.cs#L159

  2. https://github.com/aspnet/MicrosoftConfigurationBuilders/blob/ea50da297ce168877f5121b8a1409b89698e2690/src/Base/KeyValueConfigBuilder.cs#L247

  3. https://github.com/aspnet/MicrosoftConfigurationBuilders/blob/ea50da297ce168877f5121b8a1409b89698e2690/src/AzureAppConfig/AzureAppConfigurationBuilder.cs#L355C41-L355C41

  4. https://github.com/aspnet/MicrosoftConfigurationBuilders/blob/ea50da297ce168877f5121b8a1409b89698e2690/src/Base/KeyValueConfigBuilder.cs#L249C41-L249C41

  5. https://github.com/aspnet/MicrosoftConfigurationBuilders/blob/ea50da297ce168877f5121b8a1409b89698e2690/src/Base/KeyValueConfigBuilder.cs#L292C57-L292C57

  6. https://github.com/aspnet/MicrosoftConfigurationBuilders/blob/ea50da297ce168877f5121b8a1409b89698e2690/src/Base/KeyValueConfigBuilder.cs#L382

  7. https://github.com/aspnet/MicrosoftConfigurationBuilders/blob/ea50da297ce168877f5121b8a1409b89698e2690/src/Base/KeyValueConfigBuilder.cs#L387

  8. https://github.com/aspnet/MicrosoftConfigurationBuilders/blob/ea50da297ce168877f5121b8a1409b89698e2690/src/AzureAppConfig/AzureAppConfigurationBuilder.cs#L201C29-L201C29

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant