arcus-azure / arcus.security

Security for Azure development in a breeze.
https://security.arcus-azure.net/
MIT License
39 stars 12 forks source link

KeyVault provider GetRawSecretAsync continues without returning value #407

Closed Pauwelz closed 1 year ago

Pauwelz commented 1 year ago

Describe the bug When using the GetRawSecretAsync on a secret stored in KeyVault to get a API Key for a Named HTTP Client, the code continues without waiting for the response.

To Reproduce

ConfigureServices(
  (_, services) => {
    services
      .AddHttpClient(
        "SalesForce",
        async (serviceProvider, client) => {
          var secretProvider =
            serviceProvider.GetRequiredService<ISecretProvider>();

          string baseUrl = await secretProvider.GetRawSecretAsync(
            "SalesForceBaseUri"
          );
          client.BaseAddress = new Uri(baseUrl);

          string key = await secretProvider.GetRawSecretAsync(
            "SalesForce-SubscriptionKey"
          );
          client.DefaultRequestHeaders.Add(
            "Ocp-Apim-Subscription-Key",
            key
          );
        }
      );
  }
)

Expected behavior That the secret gets retrieved correctly like it does with a config file.

Additional context Using GetRawSecretAsync on a secret stored in config file or environment works correctly as we can see with the BaseUri image

Switching to GetRawSecret also works fine (although taking 15 seconds) image

Using Arcus.Security 1.9.0 with .Net 6 and Functions v4 (Isolated)

stijnmoreels commented 1 year ago

Usually, when registering services, asynchronous code is not recommended. That is also why we introduced synchronous secret retrieval for the secret store. Could you check if this works for GetRawSecret instead?

Pauwelz commented 1 year ago

With GetRawSecret it works as expected indeed. If it's not recommended to to things async during registration then I guess we will switch in our code.

If we use GetRawSecretAsync during the execution of the function, to retrieve the secret, then it works fine as well.

stijnmoreels commented 1 year ago

With GetRawSecret it works as expected indeed.

Ok, that's great! 🥳 Glad we could be of assistance. Thx for contacting, using Arcus, and promoting it, means a lot 😉.

If it's not recommended to to things async during registration then I guess we will switch in our code.

Something that Microsoft's dependency container is not really build for (unlike StructureMap, for example). So, I would indeed recommend using synchronous stuff there.

If we use GetRawSecretAsync during the execution of the function, to retrieve the secret, then it works fine as well.

Yes, in those cases, it is really 'async all the way', I guess.

stijnmoreels commented 1 year ago

I'll close this, @Pauwelz, as there is a solution found for this. Don't hesitate to re-open this or create a new issue if there is anything else you want to discus. Until then 🫡