Azure / azure-sdk-for-net

This repository is for active development of the Azure SDK for .NET. For consumers of the SDK we recommend visiting our public developer docs at https://learn.microsoft.com/dotnet/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-net.
MIT License
5.47k stars 4.8k forks source link

[BUG] Using Azure AppConfiguration and KeyVault as Configuration failes #25648

Closed MPapst closed 2 years ago

MPapst commented 2 years ago

Library name and version

Azure.Identity 1.5.0

Describe the bug

When using the libraries Microsoft.Azure.AppConfiguration.AspNetCore (4.5.1) and Azure.Extensions.AspNetCore.Configuration.Secrets (1.2.1) to built an .NET 6.0 Web Api with Azure App Configuration and Azure Key Vault as configuration providers. The Application startup failes either with

Expected behavior

Using both Configuration Providers should be working with the DefaultAzureCredential.

Actual behavior

Getting described errors in an Azure App Service Environment

Reproduction Steps

public static IHostBuilder ConfigureMyApplication(this IHostBuilder builder, string appName, TokenCredential? credential = null) => builder.ConfigureAppConfiguration((_, config) =>
  {
    config
      .AddEnvironmentVariables()
      .AddJsonFile("appsettings.json", optional: true)
      .AddInMemoryCollection(new Dictionary<string, string>()
      {
        [AppName] = appName
      });

    if (credential != null)
    {
      var built = config.Build();
      var endPoint = built[AppConfig];
      if (!string.IsNullOrEmpty(endPoint) && Uri.TryCreate(endPoint, UriKind.Absolute, out Uri? endpointUri))
      {
        config.AddAzureAppConfiguration(appCfg =>
          appCfg
            .Connect(endpointUri, credential)
            .Select(Microsoft.Extensions.Configuration.AzureAppConfiguration.KeyFilter.Any)
        );
      }

      endPoint = built[KeyVault];
      if (!string.IsNullOrEmpty(endPoint) && Uri.TryCreate(endPoint, UriKind.Absolute, out endpointUri))
      {
        var secretClient = new SecretClient(endpointUri, credential);
        config.AddAzureKeyVault(secretClient, new KeyVaultSecretManager());
      }
    }
  });

Environment

For details see Support Case 2111300050001183

jsquire commented 2 years ago

Thank you for your feedback. Tagging and routing to the team member best able to assist.

heaths commented 2 years ago

Could you share the exception message including the stack trace? What version of Azure.Security.KeyVault.Secrets was imported? Presumably 4.0.2 if you didn't import it explicitly, but we need to know for sure. What version of Azure.Identity did you import?

/cc @pakrym @schaabs as I'll be out on holiday soon and this also involves configuration extensions and identity.

MPapst commented 2 years ago

Thanks for looking into this! Please find the stack trace for the app service logstream attached: log(1).txt

For Azure.Security.KeyVault.Secrets 4.1.0 is imported.

Maybe it helps:

heaths commented 2 years ago

What version of Azure.Identity are you importing to use DefaultAzureCredential? And what options are you passing it?

I do see in the stack that MSI is failing (which explains why it works locally - can't use MSI - but not remotely - which can use MSI), but where did you see the wrong scope is used? We get the scope from the 401 challenge response, so if you configured the DefaultAzureCredential with a different scope (perhaps for another service?) and used that, it would break the auth flow. Perhaps using separate DefaultAzureCredential instances for App Configuration and Key Vault would help.

2021-11-30T09:13:42.643557902Z Azure.Identity.AuthenticationFailedException: ManagedIdentityCredential authentication failed: Service request failed.
2021-11-30T09:13:42.643589603Z Status: 400 (Bad Request)
2021-11-30T09:13:42.643610903Z 
2021-11-30T09:13:42.643615003Z Content:
2021-11-30T09:13:42.643618803Z 
2021-11-30T09:13:42.643622403Z 
2021-11-30T09:13:42.643626203Z Headers:
2021-11-30T09:13:42.643630203Z Date: Tue, 30 Nov 2021 09:13:39 GMT
2021-11-30T09:13:42.643651703Z Server: Kestrel
2021-11-30T09:13:42.643655603Z Transfer-Encoding: chunked
2021-11-30T09:13:42.643659503Z Content-Type: application/json; charset=utf-8
2021-11-30T09:13:42.643663404Z 
2021-11-30T09:13:42.643681904Z See the troubleshooting guide for more information. https://aka.ms/azsdk/net/identity/managedidentitycredential/troubleshoot
2021-11-30T09:13:42.643686804Z  ---> Azure.RequestFailedException: Service request failed.

Could you also log content? See https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/samples/Diagnostics.md#enabling-content-logging. Feel free to just provide the content scrubbed of any PII like service principals, etc.

heaths commented 2 years ago

If you're using the latest Azure.Identity, we did recently add multi-tenant support which is also supported in the most recent Secrets 4.3.0-beta.2. While I don't suspect it should've regressed anything, you might try upgrading to that and let us know if only that solves the problem.

MPapst commented 2 years ago

Please forgive me, I think I mixed up two problems. The wrong scope may be a different one I've seen in the same combination but with older versions of the libraries. I would like to remove that from the GitHub issue and come back to that if I experience it again (especially as I do no longer have the logs :)).

I already tried different instances of DefaultAzureCredential with no other result than the BadRequest. In general I do not pass any parameter to the DefaultAzureCredential ctor, nor have I set the AZURE_CLIENT_ID env var. Nevertheless I've tested both in both combinations (single instance of TokenCredential and seperate instance) with and without AZURE_CLIENT_ID env var. I was curious, because it worked one time (exactly on app startup) with both configuration providers, the next startup it failed again with the BadRequest error.

Do you think the new Secrest version will help? Following the StackTrace, it is failing at the App Configration, which is imported before the Key Vault?

I'll try to do the tests mentioned by you today and come back to this issue.

MPapst commented 2 years ago

Please find the log below.

--> There it is again, the wrong scope (is https://vaulname.vault.azure.net, should be in my opinion https://vault.azure.net) - am I wrong with that? In the past there was the error, that the resource is not found in the tenant, while the Vault is definetely there.

I find it strange, that the error is showing the Key Vault, while the Stack Trace showing the App Configuration. Note that the Key Vault is not attached to the App Configuration in any manner. I am using a central App Configuration for all services with configuration variables and for each service/app a seperate Key Vault to store credentials.

Logs, Seperate DefaultAzureCredential instances, Key Vault included, Azure.Identity 1.5.0, Azure.Security.KeyVault.Secrets 4.3.0-beta.2 log.txt

heaths commented 2 years ago

This could be an issue with mixing the older Microsoft.Azure.AppConfiguration.AspNetCore with the newer Azure.Extensions.AspNetCore.Configuration.Secrets because, yes, the scope is wrong and should be merely https://vault.azure.net for the AzurePublic cloud. Instead, probably what you want to use is https://nuget.org/packages/Microsoft.Extensions.Configuration.AzureAppConfiguration. See this sample for a full walkthrough.

MPapst commented 2 years ago

indeed, I was using the old package - but using this combination does not help either, I am ending with the same error:

        <PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.2.1" />
        <PackageReference Include="Microsoft.Extensions.Configuration.AzureAppConfiguration" Version="4.5.1" />

Just to be clear: I do not want to use Key Vault References in App Configuration, as the app configuration is a shared service, that is used by multiple applications. But each application can have its own key vault with the secrets that are only for that application necessary.

heaths commented 2 years ago

This seems similar to another issue that just came up recently. Since Azure.Extensions.AspNetCore.Configuration.Secrets depends on an old version of Azure.Security.KeyVault.Secrets, could you add an explicit reference to that for version 4.2.0 instead and see if the problem still repros?

MPapst commented 2 years ago

Unfortunately that changed nothing. Don't get fooled by the log entry "Build: 12/02/2021" as I did today.. its the last commit time :)

log ``` 2022-01-06T07:10:45.146556947Z Agent extension 2022-01-06T07:10:45.146571847Z Before if loop >> DotNet Runtime 2022-01-06T07:10:45.661047873Z DotNet Runtime 6.0Writing output script to '/opt/startup/startup.sh' 2022-01-06T07:10:47.146253061Z Running user provided startup command... 2022-01-06T07:10:52.739582587Z [07:10:51 INF] Starting web host for App SMA.HyMon.API.Config 2022-01-06T07:10:53.024570603Z [07:10:52 INF] Application Info: 2.0.29+bcbd2525f7, Git: bcbd2525f7bb292a898f3efb29d68054a6c13525, Build: 12/02/2021 10:54:40 2022-01-06T07:11:15.236056998Z [07:11:13 FTL] Host terminated unexpectedly 2022-01-06T07:11:15.398301251Z Azure.Identity.AuthenticationFailedException: ManagedIdentityCredential authentication failed: Service request failed. 2022-01-06T07:11:15.398318551Z Status: 400 (Bad Request) 2022-01-06T07:11:15.398323351Z 2022-01-06T07:11:15.398327351Z Content: 2022-01-06T07:11:15.398342852Z 2022-01-06T07:11:15.398347952Z 2022-01-06T07:11:15.398351852Z Headers: 2022-01-06T07:11:15.398355752Z Date: Thu, 06 Jan 2022 07:11:11 GMT 2022-01-06T07:11:15.398359752Z Server: Kestrel 2022-01-06T07:11:15.398363652Z Transfer-Encoding: chunked 2022-01-06T07:11:15.398367652Z Content-Type: application/json; charset=utf-8 2022-01-06T07:11:15.453586970Z 2022-01-06T07:11:15.453615671Z See the troubleshooting guide for more information. https://aka.ms/azsdk/net/identity/managedidentitycredential/troubleshoot 2022-01-06T07:11:15.453620871Z ---> Azure.RequestFailedException: Service request failed. 2022-01-06T07:11:15.453625171Z Status: 400 (Bad Request) 2022-01-06T07:11:15.453628771Z 2022-01-06T07:11:15.453632171Z Content: 2022-01-06T07:11:15.453635571Z 2022-01-06T07:11:15.453638872Z 2022-01-06T07:11:15.453642172Z Headers: 2022-01-06T07:11:15.453645472Z Date: Thu, 06 Jan 2022 07:11:11 GMT 2022-01-06T07:11:15.453648972Z Server: Kestrel 2022-01-06T07:11:15.453652272Z Transfer-Encoding: chunked 2022-01-06T07:11:15.453655672Z Content-Type: application/json; charset=utf-8 2022-01-06T07:11:15.453659072Z 2022-01-06T07:11:15.453662572Z at Azure.Identity.ManagedIdentitySource.HandleResponseAsync(Boolean async, TokenRequestContext context, Response response, CancellationToken cancellationToken) 2022-01-06T07:11:15.453666272Z at Azure.Identity.ManagedIdentitySource.AuthenticateAsync(Boolean async, TokenRequestContext context, CancellationToken cancellationToken) 2022-01-06T07:11:15.453669972Z at Azure.Identity.ManagedIdentityClient.AuthenticateAsync(Boolean async, TokenRequestContext context, CancellationToken cancellationToken) 2022-01-06T07:11:15.453673573Z at Azure.Identity.ManagedIdentityCredential.GetTokenImplAsync(Boolean async, TokenRequestContext requestContext, CancellationToken cancellationToken) 2022-01-06T07:11:15.453677273Z --- End of inner exception stack trace --- 2022-01-06T07:11:15.453680873Z at Azure.Identity.CredentialDiagnosticScope.FailWrapAndThrow(Exception ex, String additionalMessage) 2022-01-06T07:11:15.453684373Z at Azure.Identity.ManagedIdentityCredential.GetTokenImplAsync(Boolean async, TokenRequestContext requestContext, CancellationToken cancellationToken) 2022-01-06T07:11:15.453688073Z at Azure.Identity.ManagedIdentityCredential.GetTokenAsync(TokenRequestContext requestContext, CancellationToken cancellationToken) 2022-01-06T07:11:15.453691673Z at Azure.Identity.DefaultAzureCredential.GetTokenFromSourcesAsync(TokenCredential[] sources, TokenRequestContext requestContext, Boolean async, CancellationToken cancellationToken) 2022-01-06T07:11:15.453699973Z at Azure.Identity.DefaultAzureCredential.GetTokenImplAsync(Boolean async, TokenRequestContext requestContext, CancellationToken cancellationToken) 2022-01-06T07:11:15.453703773Z at Azure.Identity.CredentialDiagnosticScope.FailWrapAndThrow(Exception ex, String additionalMessage) 2022-01-06T07:11:15.453707274Z at Azure.Identity.DefaultAzureCredential.GetTokenImplAsync(Boolean async, TokenRequestContext requestContext, CancellationToken cancellationToken) 2022-01-06T07:11:15.453710874Z at Azure.Identity.DefaultAzureCredential.GetTokenAsync(TokenRequestContext requestContext, CancellationToken cancellationToken) 2022-01-06T07:11:15.453714474Z at Azure.Core.Pipeline.BearerTokenAuthenticationPolicy.AccessTokenCache.GetHeaderValueFromCredentialAsync(TokenRequestContext context, Boolean async, CancellationToken cancellationToken) 2022-01-06T07:11:15.453722374Z at Azure.Core.Pipeline.BearerTokenAuthenticationPolicy.AccessTokenCache.GetHeaderValueAsync(HttpMessage message, TokenRequestContext context, Boolean async) 2022-01-06T07:11:15.453738174Z at Azure.Core.Pipeline.BearerTokenAuthenticationPolicy.AccessTokenCache.GetHeaderValueAsync(HttpMessage message, TokenRequestContext context, Boolean async) 2022-01-06T07:11:15.453742075Z at Azure.Core.Pipeline.BearerTokenAuthenticationPolicy.AuthenticateAndAuthorizeRequestAsync(HttpMessage message, TokenRequestContext context) 2022-01-06T07:11:15.453745775Z at Azure.Core.Pipeline.BearerTokenAuthenticationPolicy.ProcessAsync(HttpMessage message, ReadOnlyMemory`1 pipeline, Boolean async) 2022-01-06T07:11:15.453749375Z at Azure.Core.Pipeline.RedirectPolicy.ProcessAsync(HttpMessage message, ReadOnlyMemory`1 pipeline, Boolean async) 2022-01-06T07:11:15.453753175Z at Azure.Core.Pipeline.RetryPolicy.ProcessAsync(HttpMessage message, ReadOnlyMemory`1 pipeline, Boolean async) 2022-01-06T07:11:15.453756875Z at Azure.Core.Pipeline.RetryPolicy.ProcessAsync(HttpMessage message, ReadOnlyMemory`1 pipeline, Boolean async) 2022-01-06T07:11:15.453760575Z at Microsoft.Extensions.Configuration.AzureAppConfiguration.UserAgentHeaderPolicy.ProcessAsync(HttpMessage message, ReadOnlyMemory`1 pipeline, Boolean async) 2022-01-06T07:11:15.453764375Z at Azure.Core.Pipeline.HttpPipeline.SendRequestAsync(Request request, CancellationToken cancellationToken) 2022-01-06T07:11:15.453767975Z at Azure.Data.AppConfiguration.ConfigurationClient.GetConfigurationSettingsPageAsync(SettingSelector selector, String pageLink, CancellationToken cancellationToken) 2022-01-06T07:11:15.453772775Z at Azure.Core.PageResponseEnumerator.FuncAsyncPageable`1.AsPages(String continuationToken, Nullable`1 pageSizeHint)+MoveNext() 2022-01-06T07:11:15.453776576Z at Azure.Core.PageResponseEnumerator.FuncAsyncPageable`1.AsPages(String continuationToken, Nullable`1 pageSizeHint)+System.Threading.Tasks.Sources.IValueTaskSource.GetResult() 2022-01-06T07:11:15.453780776Z at Azure.AsyncPageable`1.GetAsyncEnumerator(CancellationToken cancellationToken)+MoveNext() 2022-01-06T07:11:15.453784376Z at Azure.AsyncPageable`1.GetAsyncEnumerator(CancellationToken cancellationToken)+MoveNext() 2022-01-06T07:11:15.453788176Z at Azure.AsyncPageable`1.GetAsyncEnumerator(CancellationToken cancellationToken)+System.Threading.Tasks.Sources.IValueTaskSource.GetResult() 2022-01-06T07:11:15.453792076Z at Microsoft.Extensions.Configuration.AzureAppConfiguration.AzureAppConfigurationProvider.<>c__DisplayClass20_3.<b__4>d.MoveNext() 2022-01-06T07:11:15.453796176Z --- End of stack trace from previous location --- 2022-01-06T07:11:15.453799776Z at Microsoft.Extensions.Configuration.AzureAppConfiguration.AzureAppConfigurationProvider.<>c__DisplayClass20_3.<b__4>d.MoveNext() 2022-01-06T07:11:15.453803976Z --- End of stack trace from previous location --- 2022-01-06T07:11:15.453807476Z at Microsoft.Extensions.Configuration.AzureAppConfiguration.TracingUtils.CallWithRequestTracing(Boolean tracingEnabled, RequestType requestType, RequestTracingOptions requestTracingOptions, Func`1 clientCall) 2022-01-06T07:11:15.453814677Z at Microsoft.Extensions.Configuration.AzureAppConfiguration.AzureAppConfigurationProvider.CallWithRequestTracing(Func`1 clientCall) 2022-01-06T07:11:15.453818477Z at Microsoft.Extensions.Configuration.AzureAppConfiguration.AzureAppConfigurationProvider.LoadAll(Boolean ignoreFailures) 2022-01-06T07:11:15.453822277Z at Microsoft.Extensions.Configuration.AzureAppConfiguration.AzureAppConfigurationProvider.Load() 2022-01-06T07:11:15.453825877Z at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList`1 providers) 2022-01-06T07:11:15.453829477Z at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build() 2022-01-06T07:11:15.453833077Z at Microsoft.Extensions.Hosting.HostBuilder.BuildAppConfiguration() 2022-01-06T07:11:15.453836677Z at Microsoft.Extensions.Hosting.HostBuilder.Build() 2022-01-06T07:11:15.453840277Z at SMA.HyMon.API.Config.Program.Main(String[] args) in C:\Users\papst\source\repos\HyMon\Hybrid Monitoring Cloud\src\SMA.HyMon.API.Config\Program.cs:line 41 2022-01-06T07:11:15.456405253Z [07:11:15 INF] Application Info: 2.0.29+bcbd2525f7, Git: bcbd2525f7bb292a898f3efb29d68054a6c13525, Build: 12/02/2021 10:54:40 ```
heaths commented 2 years ago

Thanks for the info, though unfortunate. I'm still trying to get a repro. It works when run locally against my user principal, and with a couple more changes to my deployment I'll test Managed Identity but it would help to understand your environment a bit better. So, I have some follow-up questions:

  1. What exactly is using Managed Identity in this scenario? Is it just the web site (App Services)?
  2. Which services are authorizing that Managed Identity? App Configuration, Key Vault, or both? (Assuming you're using RBAC for Key Vault.)
  3. Do you still get the full Key Vault host in the scope e.g., https://myvault.vault.azure.net/.default, when using the newer assemblies, or did that resolve? I assume at this point you probably can't know (assuming your repro code above is the same or close to your production code) because the exception is thrown earlier, but is it possible to comment out the App Configuration code and test that Key Vault authentication is working now? In one other bug I'm investigating that has the same problem with the full host in the scope, it does seem to be a problem with mixing older and newer assemblies (we suspect), so hopefully with your package updates that no longer repros.

It would also help to have more information logged, including the WWW-Authenticate header for both App Configuration and Key Vault, and the resource query parameter for the credentials. That would look something like this:

// Configure Azure App Configuration and Key Vault services using the same DefaultAzureCredential.
DefaultAzureCredential credential = new(new DefaultAzureCredentialOptions()
{
    Diagnostics =
    {
        LoggedQueryParameters = { "resource" },
    },
});

Uri appConfigUri = new(builder.Configuration["APPCONFIG_URI"]);
builder.Configuration.AddAzureAppConfiguration(options =>
{
    options
        .Connect(appConfigUri, credential)
        .ConfigureClientOptions(configure =>
        {
            configure.Diagnostics.LoggedHeaderNames.Add("WWW-Authenticate");
        })
        .Select(Microsoft.Extensions.Configuration.AzureAppConfiguration.KeyFilter.Any);
});

Uri keyVaultUri = new(builder.Configuration["KEYVAULT_URI"]);
SecretClient secretClient = new(keyVaultUri, credential, new()
{
    Diagnostics =
    {
        LoggedHeaderNames = {"WWW-Authenticate" },
    }
});
builder.Configuration.AddAzureKeyVault(secretClient, new KeyVaultSecretManager());

This should produce some logging like so that will be helpful (guids and URIs will of course be different - you can obfuscate if you feel necessary):

Azure-Core: [Informational] Request [b9bce620-a790-4acb-a35a-829824c679db] GET http://169.254.169.254/metadata/identity/oauth2/token?api-version=REDACTED&resource=https%3A%2F%2Fsjnemd7psi63iconfig.azconfig.io&client_id=REDACTED
...
WWW-Authenticate:Bearer authorization="https://login.windows.net/72f988bf-86f1-41af-91ab-2d7cd011db47", resource="https://vault.azure.net"
MPapst commented 2 years ago

Will follow up with more informations and repro tomorrow. So long:

MPapst commented 2 years ago

Test 1: KeyVault & AppConfig, with extended Logging --> Creates the same Log Output as before

Test 1, Code ```csharp public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) //.ConfigureHyMonApplication(AppName, Credential) .ConfigureAppConfiguration((_, config) => { config .AddEnvironmentVariables() .AddJsonFile("appsettings.json", optional: true) .AddInMemoryCollection(new Dictionary() { ["AppName"] = AppName }); var built = config.Build(); var endPoint = built["AzureAppConfigurationEndpoint"]; if (!string.IsNullOrEmpty(endPoint) && Uri.TryCreate(endPoint, UriKind.Absolute, out Uri? endpointUri)) { DefaultAzureCredential credential = new(new DefaultAzureCredentialOptions() { Diagnostics = { LoggedQueryParameters = { "resource" }, }, }); config.AddAzureAppConfiguration(appCfg => appCfg .ConfigureClientOptions(configure => { configure.Diagnostics.LoggedHeaderNames.Add("WWW-Authenticate"); }) .Connect(endpointUri, credential) .Select(Microsoft.Extensions.Configuration.AzureAppConfiguration.KeyFilter.Any) ); } endPoint = built["AzureKeyVaultEndpoint"]; if (!string.IsNullOrEmpty(endPoint) && Uri.TryCreate(endPoint, UriKind.Absolute, out endpointUri)) { DefaultAzureCredential credential = new(new DefaultAzureCredentialOptions() { Diagnostics = { LoggedQueryParameters = { "resource" }, }, }); var secretClient = new SecretClient(endpointUri, credential, new SecretClientOptions() { Diagnostics = { LoggedHeaderNames = {"WWW-Authenticate" }, } }); config.AddAzureKeyVault(secretClient, new KeyVaultSecretManager()); } }) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup() .UseSerilog(logger: Log.Logger); }); ```
Test 1, Log ``` 2022-01-07T11:12:34.741894776Z [11:12:33 INF] Starting web host for App SMA.HyMon.API.Config 2022-01-07T11:12:35.518671963Z [11:12:35 INF] Application Info: 2.0.56+cbe9899f57, Git: cbe9899f5719b7f0d5915d77275dd19d98804c11, Build: 01/07/2022 11:08:23 2022-01-07T11:13:15.874027839Z [11:13:15 FTL] Host terminated unexpectedly 2022-01-07T11:13:15.882465594Z Azure.Identity.AuthenticationFailedException: ManagedIdentityCredential authentication failed: Service request failed. 2022-01-07T11:13:15.882484094Z Status: 400 (Bad Request) 2022-01-07T11:13:15.882488494Z 2022-01-07T11:13:15.882492494Z Content: 2022-01-07T11:13:15.882496994Z 2022-01-07T11:13:15.882500894Z 2022-01-07T11:13:15.882504794Z Headers: 2022-01-07T11:13:15.882509494Z Date: Fri, 07 Jan 2022 11:13:15 GMT 2022-01-07T11:13:15.882518594Z Server: Kestrel 2022-01-07T11:13:15.882522694Z Transfer-Encoding: chunked 2022-01-07T11:13:15.882526693Z Content-Type: application/json; charset=utf-8 2022-01-07T11:13:15.882530693Z 2022-01-07T11:13:15.882534693Z See the troubleshooting guide for more information. https://aka.ms/azsdk/net/identity/managedidentitycredential/troubleshoot 2022-01-07T11:13:15.882549593Z ---> Azure.RequestFailedException: Service request failed. 2022-01-07T11:13:15.882553993Z Status: 400 (Bad Request) 2022-01-07T11:13:15.882557693Z 2022-01-07T11:13:15.882561093Z Content: 2022-01-07T11:13:15.882564493Z 2022-01-07T11:13:15.882568293Z 2022-01-07T11:13:15.882571593Z Headers: 2022-01-07T11:13:15.882575093Z Date: Fri, 07 Jan 2022 11:13:15 GMT 2022-01-07T11:13:15.882578693Z Server: Kestrel 2022-01-07T11:13:15.882582293Z Transfer-Encoding: chunked 2022-01-07T11:13:15.882585993Z Content-Type: application/json; charset=utf-8 2022-01-07T11:13:15.882589593Z 2022-01-07T11:13:15.882593293Z at Azure.Identity.ManagedIdentitySource.HandleResponseAsync(Boolean async, TokenRequestContext context, Response response, CancellationToken cancellationToken) 2022-01-07T11:13:15.882597293Z at Azure.Identity.ManagedIdentitySource.AuthenticateAsync(Boolean async, TokenRequestContext context, CancellationToken cancellationToken) 2022-01-07T11:13:15.882601193Z at Azure.Identity.ManagedIdentityClient.AuthenticateAsync(Boolean async, TokenRequestContext context, CancellationToken cancellationToken) 2022-01-07T11:13:15.882604993Z at Azure.Identity.ManagedIdentityCredential.GetTokenImplAsync(Boolean async, TokenRequestContext requestContext, CancellationToken cancellationToken) 2022-01-07T11:13:15.882608793Z --- End of inner exception stack trace --- 2022-01-07T11:13:15.882612293Z at Azure.Identity.CredentialDiagnosticScope.FailWrapAndThrow(Exception ex, String additionalMessage) 2022-01-07T11:13:15.882615893Z at Azure.Identity.ManagedIdentityCredential.GetTokenImplAsync(Boolean async, TokenRequestContext requestContext, CancellationToken cancellationToken) 2022-01-07T11:13:15.882619693Z at Azure.Identity.ManagedIdentityCredential.GetTokenAsync(TokenRequestContext requestContext, CancellationToken cancellationToken) 2022-01-07T11:13:15.882623393Z at Azure.Identity.DefaultAzureCredential.GetTokenFromSourcesAsync(TokenCredential[] sources, TokenRequestContext requestContext, Boolean async, CancellationToken cancellationToken) 2022-01-07T11:13:15.882627093Z at Azure.Identity.DefaultAzureCredential.GetTokenImplAsync(Boolean async, TokenRequestContext requestContext, CancellationToken cancellationToken) 2022-01-07T11:13:15.882630793Z at Azure.Identity.CredentialDiagnosticScope.FailWrapAndThrow(Exception ex, String additionalMessage) 2022-01-07T11:13:15.882635093Z at Azure.Identity.DefaultAzureCredential.GetTokenImplAsync(Boolean async, TokenRequestContext requestContext, CancellationToken cancellationToken) 2022-01-07T11:13:15.882638793Z at Azure.Identity.DefaultAzureCredential.GetTokenAsync(TokenRequestContext requestContext, CancellationToken cancellationToken) 2022-01-07T11:13:15.882642493Z at Azure.Core.Pipeline.BearerTokenAuthenticationPolicy.AccessTokenCache.GetHeaderValueFromCredentialAsync(TokenRequestContext context, Boolean async, CancellationToken cancellationToken) 2022-01-07T11:13:15.882649093Z at Azure.Core.Pipeline.BearerTokenAuthenticationPolicy.AccessTokenCache.GetHeaderValueAsync(HttpMessage message, TokenRequestContext context, Boolean async) 2022-01-07T11:13:15.882653093Z at Azure.Core.Pipeline.BearerTokenAuthenticationPolicy.AccessTokenCache.GetHeaderValueAsync(HttpMessage message, TokenRequestContext context, Boolean async) 2022-01-07T11:13:15.882656793Z at Azure.Core.Pipeline.BearerTokenAuthenticationPolicy.AuthenticateAndAuthorizeRequestAsync(HttpMessage message, TokenRequestContext context) 2022-01-07T11:13:15.882660493Z at Azure.Core.Pipeline.BearerTokenAuthenticationPolicy.ProcessAsync(HttpMessage message, ReadOnlyMemory`1 pipeline, Boolean async) 2022-01-07T11:13:15.882664293Z at Azure.Core.Pipeline.RedirectPolicy.ProcessAsync(HttpMessage message, ReadOnlyMemory`1 pipeline, Boolean async) 2022-01-07T11:13:15.882668093Z at Azure.Core.Pipeline.RetryPolicy.ProcessAsync(HttpMessage message, ReadOnlyMemory`1 pipeline, Boolean async) 2022-01-07T11:13:15.882671693Z at Azure.Core.Pipeline.RetryPolicy.ProcessAsync(HttpMessage message, ReadOnlyMemory`1 pipeline, Boolean async) 2022-01-07T11:13:15.882675293Z at Microsoft.Extensions.Configuration.AzureAppConfiguration.UserAgentHeaderPolicy.ProcessAsync(HttpMessage message, ReadOnlyMemory`1 pipeline, Boolean async) 2022-01-07T11:13:15.882679093Z at Azure.Core.Pipeline.HttpPipeline.SendRequestAsync(Request request, CancellationToken cancellationToken) 2022-01-07T11:13:15.882682693Z at Azure.Data.AppConfiguration.ConfigurationClient.GetConfigurationSettingsPageAsync(SettingSelector selector, String pageLink, CancellationToken cancellationToken) 2022-01-07T11:13:15.882686393Z at Azure.Core.PageResponseEnumerator.FuncAsyncPageable`1.AsPages(String continuationToken, Nullable`1 pageSizeHint)+MoveNext() 2022-01-07T11:13:15.882690293Z at Azure.Core.PageResponseEnumerator.FuncAsyncPageable`1.AsPages(String continuationToken, Nullable`1 pageSizeHint)+System.Threading.Tasks.Sources.IValueTaskSource.GetResult() 2022-01-07T11:13:15.882695093Z at Azure.AsyncPageable`1.GetAsyncEnumerator(CancellationToken cancellationToken)+MoveNext() 2022-01-07T11:13:15.882698793Z at Azure.AsyncPageable`1.GetAsyncEnumerator(CancellationToken cancellationToken)+MoveNext() 2022-01-07T11:13:15.882702393Z at Azure.AsyncPageable`1.GetAsyncEnumerator(CancellationToken cancellationToken)+System.Threading.Tasks.Sources.IValueTaskSource.GetResult() 2022-01-07T11:13:15.882706293Z at Microsoft.Extensions.Configuration.AzureAppConfiguration.AzureAppConfigurationProvider.<>c__DisplayClass20_3.<b__4>d.MoveNext() 2022-01-07T11:13:15.882710293Z --- End of stack trace from previous location --- 2022-01-07T11:13:15.882713792Z at Microsoft.Extensions.Configuration.AzureAppConfiguration.AzureAppConfigurationProvider.<>c__DisplayClass20_3.<b__4>d.MoveNext() 2022-01-07T11:13:15.882717692Z --- End of stack trace from previous location --- 2022-01-07T11:13:15.882721392Z at Microsoft.Extensions.Configuration.AzureAppConfiguration.TracingUtils.CallWithRequestTracing(Boolean tracingEnabled, RequestType requestType, RequestTracingOptions requestTracingOptions, Func`1 clientCall) 2022-01-07T11:13:15.882728392Z at Microsoft.Extensions.Configuration.AzureAppConfiguration.AzureAppConfigurationProvider.CallWithRequestTracing(Func`1 clientCall) 2022-01-07T11:13:15.882732592Z at Microsoft.Extensions.Configuration.AzureAppConfiguration.AzureAppConfigurationProvider.LoadAll(Boolean ignoreFailures) 2022-01-07T11:13:15.882736292Z at Microsoft.Extensions.Configuration.AzureAppConfiguration.AzureAppConfigurationProvider.Load() 2022-01-07T11:13:15.882739992Z at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList`1 providers) 2022-01-07T11:13:15.882743692Z at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build() 2022-01-07T11:13:15.882747392Z at Microsoft.Extensions.Hosting.HostBuilder.BuildAppConfiguration() 2022-01-07T11:13:15.882751092Z at Microsoft.Extensions.Hosting.HostBuilder.Build() 2022-01-07T11:13:15.882754592Z at SMA.HyMon.API.Config.Program.Main(String[] args) in C:\Users\papst\source\repos\HyMon\Hybrid Monitoring Cloud\src\SMA.HyMon.API.Config\Program.cs:line 41 2022-01-07T11:13:16.004841040Z [11:13:16 INF] Application Info: 2.0.56+cbe9899f57, Git: cbe9899f5719b7f0d5915d77275dd19d98804c11, Build: 01/07/2022 11:08:23 ```

Test 2: Same Code, no App Config only Key Vault --> works, except that all configuration values are missing. But I do not see any additional log information I double checked that the code is the correct one, but the Git commit hash in the log matches the one Git shows

Test 2, Logs ``` 2022-01-07T11:20:54.730111031Z [11:20:54 INF] Starting web host for App SMA.HyMon.API.Config 2022-01-07T11:20:55.768261077Z [11:20:55 INF] Application Info: 2.0.56+cbe9899f57, Git: cbe9899f5719b7f0d5915d77275dd19d98804c11, Build: 01/07/2022 11:08:23 2022-01-07T11:21:22.895334243Z [11:21:22 INF] Azure Web Sites environment detected. Using '/home/ASP.NET/DataProtection-Keys' as key repository; keys will not be encrypted at rest. 2022-01-07T11:21:24.273837897Z [11:21:24 INF] Now listening on: http://[::]:8080 2022-01-07T11:21:24.303906937Z [11:21:24 INF] Application started. Press Ctrl+C to shut down. 2022-01-07T11:21:24.304505633Z [11:21:24 INF] Hosting environment: Production 2022-01-07T11:21:24.311662995Z [11:21:24 INF] Content root path: /home/site/wwwroot 2022-01-07T11:21:24.888928422Z [11:21:24 INF] Request starting HTTP/1.1 GET http://169.254.132.3:8080/robots933456.txt - - 2022-01-07T11:21:24.927549017Z [11:21:24 WRN] Failed to determine the https port for redirect. 2022-01-07T11:21:25.494584298Z [11:21:25 ERR] Connection id "0HMEHPVBI6SU1", Request id "0HMEHPVBI6SU1:00000002": An unhandled exception was thrown by the application. 2022-01-07T11:21:25.494619998Z System.ArgumentNullException: IDW10106: The 'ClientId' option must be provided. 2022-01-07T11:21:25.495833992Z at Microsoft.Identity.Web.MergedOptionsValidation.Validate(MergedOptions options) 2022-01-07T11:21:25.495846592Z at Microsoft.Identity.Web.MicrosoftIdentityWebApiAuthenticationBuilderExtensions.<>c__DisplayClass3_0.b__0(JwtBearerOptions options, IServiceProvider serviceProvider, IOptionsMonitor`1 mergedOptionsMonitor, IOptionsMonitor`1 msIdOptionsMonitor, IOptions`1 msIdOptions) 2022-01-07T11:21:25.495850892Z at Microsoft.Extensions.Options.ConfigureNamedOptions`5.Configure(String name, TOptions options) 2022-01-07T11:21:25.495853992Z at Microsoft.Extensions.Options.OptionsFactory`1.Create(String name) 2022-01-07T11:21:25.495857092Z at Microsoft.Extensions.Options.OptionsMonitor`1.<>c__DisplayClass10_0.b__0() 2022-01-07T11:21:25.495860392Z at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode) 2022-01-07T11:21:25.495868092Z at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor) 2022-01-07T11:21:25.495871092Z at System.Lazy`1.CreateValue() 2022-01-07T11:21:25.495873892Z at System.Lazy`1.get_Value() 2022-01-07T11:21:25.495876792Z at Microsoft.Extensions.Options.OptionsCache`1.GetOrAdd(String name, Func`1 createOptions) 2022-01-07T11:21:25.495879692Z at Microsoft.Extensions.Options.OptionsMonitor`1.Get(String name) 2022-01-07T11:21:25.495882592Z at Microsoft.AspNetCore.Authentication.AuthenticationHandler`1.InitializeAsync(AuthenticationScheme scheme, HttpContext context) 2022-01-07T11:21:25.503983648Z at Microsoft.AspNetCore.Authentication.AuthenticationHandlerProvider.GetHandlerAsync(HttpContext context, String authenticationScheme) 2022-01-07T11:21:25.504005748Z at Microsoft.AspNetCore.Authentication.AuthenticationService.AuthenticateAsync(HttpContext context, String scheme) 2022-01-07T11:21:25.504010148Z at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) 2022-01-07T11:21:25.504269247Z at SMA.HyMon.Runtime.AspNetCore.SecurityExtensions.<>c.<b__0_5>d.MoveNext() 2022-01-07T11:21:25.504276947Z --- End of stack trace from previous location --- 2022-01-07T11:21:25.504280847Z at NWebsec.AspNetCore.Middleware.Middleware.CspMiddleware.Invoke(HttpContext context) 2022-01-07T11:21:25.504289947Z at NWebsec.AspNetCore.Middleware.Middleware.MiddlewareBase.Invoke(HttpContext context) 2022-01-07T11:21:25.504293647Z at NWebsec.AspNetCore.Middleware.Middleware.MiddlewareBase.Invoke(HttpContext context) 2022-01-07T11:21:25.504297147Z at NWebsec.AspNetCore.Middleware.Middleware.MiddlewareBase.Invoke(HttpContext context) 2022-01-07T11:21:25.504300747Z at NWebsec.AspNetCore.Middleware.Middleware.MiddlewareBase.Invoke(HttpContext context) 2022-01-07T11:21:25.504304247Z at NWebsec.AspNetCore.Middleware.Middleware.MiddlewareBase.Invoke(HttpContext context) 2022-01-07T11:21:25.504307847Z at NSwag.AspNetCore.Middlewares.SwaggerUiIndexMiddleware.Invoke(HttpContext context) 2022-01-07T11:21:25.504319147Z at NSwag.AspNetCore.Middlewares.RedirectToIndexMiddleware.Invoke(HttpContext context) 2022-01-07T11:21:25.504322947Z at NSwag.AspNetCore.Middlewares.OpenApiDocumentMiddleware.Invoke(HttpContext context) 2022-01-07T11:21:25.504326547Z at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application) 2022-01-07T11:21:25.583434125Z [11:21:25 INF] Request finished HTTP/1.1 GET http://169.254.132.3:8080/robots933456.txt - - - 500 0 - 705.5956ms ```
heaths commented 2 years ago

I'm so sorry, but I forgot to copy a crucial line from my repro. If you have AppInsights enabled, the logs will already be there, but if not, you need to hook up a logger as well. A console logger will work, which you can do if you add this near the top of your method in the sample code:

using var listener = AzureEventSourceListener.CreateConsoleLogger(EventLevel.Verbose);

See here for an example. It will automatically log crucial information to the console from our event sources.

/cc @annelo-msft @schaabs

Given this is only happening with the App Configuration client, have you double checked to make sure the Managed Identity has the App Configuration Data Reader role assigned as shown here in my sample? I'd expect a 403 in that case instead of a 400, though.

MPapst commented 2 years ago

The roles of the Identity are correct: image

Try 1: With Event Source Listener, only App Config ``` 2022-01-07T18:35:46.022469055Z [18:35:45 INF] Starting web host for App SMA.HyMon.API.Config 2022-01-07T18:35:46.280164827Z [18:35:46 INF] Application Info: 2.0.56+cbe9899f57, Git: cbe9899f5719b7f0d5915d77275dd19d98804c11, Build: 01/07/2022 11:08:23 2022-01-07T18:35:51.572692400Z [Informational] Azure-Identity: DefaultAzureCredential.GetToken invoked. Scopes: [ https://hymon-dev.azconfig.io/.default ] ParentRequestId: 4509b95f-9900-4d4b-a64e-f5f0420afa85 2022-01-07T18:35:51.594208180Z [Informational] Azure-Identity: EnvironmentCredential.GetToken invoked. Scopes: [ https://hymon-dev.azconfig.io/.default ] ParentRequestId: 4509b95f-9900-4d4b-a64e-f5f0420afa85 2022-01-07T18:35:51.612233881Z [Informational] Azure-Identity: EnvironmentCredential.GetToken was unable to retrieve an access token. Scopes: [ https://hymon-dev.azconfig.io/.default ] ParentRequestId: 4509b95f-9900-4d4b-a64e-f5f0420afa85 Exception: Azure.Identity.CredentialUnavailableException (0x80131500): EnvironmentCredential authentication unavailable. Environment variables are not fully configured. See the troubleshooting guide for more information. https://aka.ms/azsdk/net/identity/environmentcredential/troubleshoot 2022-01-07T18:35:51.827084490Z [Informational] Azure-Identity: ManagedIdentityCredential.GetToken invoked. Scopes: [ https://hymon-dev.azconfig.io/.default ] ParentRequestId: 4509b95f-9900-4d4b-a64e-f5f0420afa85 2022-01-07T18:35:51.898078497Z [Informational] Azure-Core: Request [c1123a8e-4312-4d6b-b0f6-cd6bae684043] GET http://169.254.132.2:8081/msi/token?api-version=REDACTED&resource=REDACTED 2022-01-07T18:35:51.898119596Z secret:REDACTED 2022-01-07T18:35:51.995785855Z x-ms-client-request-id:REDACTED 2022-01-07T18:35:51.995800555Z x-ms-return-client-request-id:REDACTED 2022-01-07T18:35:51.995804655Z User-Agent:REDACTED 2022-01-07T18:35:51.995808355Z client assembly: Azure.Identity 2022-01-07T18:36:09.166519233Z [Informational] Azure-Core: Response [c1123a8e-4312-4d6b-b0f6-cd6bae684043] 200 OK (17.2s) 2022-01-07T18:36:09.166543133Z Date:REDACTED 2022-01-07T18:36:09.166548233Z Server:REDACTED 2022-01-07T18:36:09.166552333Z Transfer-Encoding:REDACTED 2022-01-07T18:36:09.166556633Z Content-Type:REDACTED 2022-01-07T18:36:09.166560733Z 2022-01-07T18:36:09.175492783Z [Warning] Azure-Core: Response [c1123a8e-4312-4d6b-b0f6-cd6bae684043] took 17.2s 2022-01-07T18:36:09.296841609Z [Informational] Azure-Identity: ManagedIdentityCredential.GetToken succeeded. Scopes: [ https://hymon-dev.azconfig.io/.default ] ParentRequestId: 4509b95f-9900-4d4b-a64e-f5f0420afa85 ExpiresOn: 2022-01-08T18:36:06.0000000+00:00 2022-01-07T18:36:09.347156629Z [Informational] Azure-Identity: DefaultAzureCredential credential selected: Azure.Identity.ManagedIdentityCredential 2022-01-07T18:36:09.347505627Z [Informational] Azure-Identity: DefaultAzureCredential.GetToken succeeded. Scopes: [ https://hymon-dev.azconfig.io/.default ] ParentRequestId: 4509b95f-9900-4d4b-a64e-f5f0420afa85 ExpiresOn: 2022-01-08T18:36:06.0000000+00:00 2022-01-07T18:36:09.477095107Z [Informational] Azure-Core: Request [4509b95f-9900-4d4b-a64e-f5f0420afa85] GET https://hymon-dev.azconfig.io/kv/?key=REDACTED&label=REDACTED&api-version=REDACTED 2022-01-07T18:36:09.477141907Z Accept:REDACTED 2022-01-07T18:36:09.477147607Z Correlation-Context:REDACTED 2022-01-07T18:36:09.477151707Z User-Agent:REDACTED 2022-01-07T18:36:09.477542005Z x-ms-client-request-id:REDACTED 2022-01-07T18:36:09.478161201Z x-ms-return-client-request-id:REDACTED 2022-01-07T18:36:09.478174301Z Authorization:REDACTED 2022-01-07T18:36:09.478178101Z client assembly: Azure.Data.AppConfiguration 2022-01-07T18:36:10.607788530Z [Informational] Azure-Core: Response [4509b95f-9900-4d4b-a64e-f5f0420afa85] 200 OK (01.1s) 2022-01-07T18:36:10.607818530Z Server:REDACTED 2022-01-07T18:36:10.607824330Z Date:REDACTED 2022-01-07T18:36:10.607828830Z Transfer-Encoding:REDACTED 2022-01-07T18:36:10.607878529Z Connection:REDACTED 2022-01-07T18:36:10.607883529Z Sync-Token:REDACTED 2022-01-07T18:36:10.607887529Z x-ms-request-id:REDACTED 2022-01-07T18:36:10.607891729Z x-ms-client-request-id:REDACTED 2022-01-07T18:36:10.607895829Z x-ms-correlation-request-id:REDACTED 2022-01-07T18:36:10.608250627Z Access-Control-Allow-Origin:REDACTED 2022-01-07T18:36:10.608257927Z Access-Control-Allow-Credentials:REDACTED 2022-01-07T18:36:10.608261227Z Access-Control-Expose-Headers:REDACTED 2022-01-07T18:36:10.608264027Z Strict-Transport-Security:REDACTED 2022-01-07T18:36:10.608267027Z Content-Type:REDACTED 2022-01-07T18:36:10.608270027Z 2022-01-07T18:36:12.772803863Z [18:36:12 INF] Azure Web Sites environment detected. Using '/home/ASP.NET/DataProtection-Keys' as key repository; keys will not be encrypted at rest. 2022-01-07T18:36:15.211522084Z [18:36:15 INF] Now listening on: http://[::]:8080 2022-01-07T18:36:15.221523229Z [18:36:15 INF] Application started. Press Ctrl+C to shut down. 2022-01-07T18:36:15.221540929Z [18:36:15 INF] Hosting environment: Production 2022-01-07T18:36:15.221545629Z [18:36:15 INF] Content root path: /home/site/wwwroot 2022-01-07T18:36:25.744449861Z [18:36:25 INF] Request starting HTTP/1.1 GET http://169.254.132.5:8080/robots933456.txt - - 2022-01-07T18:36:25.774529196Z [18:36:25 WRN] Failed to determine the https port for redirect. 2022-01-07T18:36:26.280135636Z [18:36:26 INF] Request finished HTTP/1.1 GET http://169.254.132.5:8080/robots933456.txt - - - 404 0 - 553.8899ms 2022-01-07T18:36:26.709173586Z [18:36:26 INF] Request starting HTTP/1.1 GET http://169.254.132.6:8081/robots933456.txt - 0 2022-01-07T18:36:26.720370924Z [18:36:26 INF] Request finished HTTP/1.1 GET http://169.254.132.6:8081/robots933456.txt - 0 - 404 0 - 11.2128ms ```

So far so good. I am not sure about all those robots.txt requests, but the seem to come from the Library? Edit: Just seen the Scope for the App Configuration here.. is that correct? But it seems to work.

Try 2: With Event Source Listener, App Config & Key Vault ``` 2022-01-07T18:40:41.278576394Z [18:40:40 INF] Starting web host for App SMA.HyMon.API.Config 2022-01-07T18:40:43.563659119Z [18:40:43 INF] Application Info: 2.0.56+cbe9899f57, Git: cbe9899f5719b7f0d5915d77275dd19d98804c11, Build: 01/07/2022 11:08:23 2022-01-07T18:40:59.178919102Z [Informational] Azure-Identity: DefaultAzureCredential.GetToken invoked. Scopes: [ https://w-hymon-api-dev.vault.azure.net/.default ] ParentRequestId: 6a11017b-6c04-4734-94d5-e7dbff0ab419 2022-01-07T18:40:59.179353000Z [Informational] Azure-Identity: EnvironmentCredential.GetToken invoked. Scopes: [ https://w-hymon-api-dev.vault.azure.net/.default ] ParentRequestId: 6a11017b-6c04-4734-94d5-e7dbff0ab419 2022-01-07T18:40:59.219836083Z [Informational] Azure-Identity: EnvironmentCredential.GetToken was unable to retrieve an access token. Scopes: [ https://w-hymon-api-dev.vault.azure.net/.default ] ParentRequestId: 6a11017b-6c04-4734-94d5-e7dbff0ab419 Exception: Azure.Identity.CredentialUnavailableException (0x80131500): EnvironmentCredential authentication unavailable. Environment variables are not fully configured. See the troubleshooting guide for more information. https://aka.ms/azsdk/net/identity/environmentcredential/troubleshoot 2022-01-07T18:40:59.508454541Z [Informational] Azure-Identity: ManagedIdentityCredential.GetToken invoked. Scopes: [ https://w-hymon-api-dev.vault.azure.net/.default ] ParentRequestId: 6a11017b-6c04-4734-94d5-e7dbff0ab419 2022-01-07T18:41:00.129436023Z [Informational] Azure-Core: Request [88a39a25-e39b-4b2e-930f-7e30527d44e5] GET http://169.254.132.8:8081/msi/token?api-version=REDACTED&resource=REDACTED 2022-01-07T18:41:00.129459023Z secret:REDACTED 2022-01-07T18:41:00.379439087Z x-ms-client-request-id:REDACTED 2022-01-07T18:41:00.379455687Z x-ms-return-client-request-id:REDACTED 2022-01-07T18:41:00.379459987Z User-Agent:REDACTED 2022-01-07T18:41:00.379463587Z client assembly: Azure.Identity 2022-01-07T18:41:27.380147122Z [Warning] Azure-Core: Error response [88a39a25-e39b-4b2e-930f-7e30527d44e5] 400 Bad Request (27.0s) 2022-01-07T18:41:27.380470720Z Date:REDACTED 2022-01-07T18:41:27.380478520Z Server:REDACTED 2022-01-07T18:41:27.380481920Z Transfer-Encoding:REDACTED 2022-01-07T18:41:27.380485220Z Content-Type:REDACTED 2022-01-07T18:41:27.380488520Z 2022-01-07T18:41:27.389636270Z [Warning] Azure-Core: Response [88a39a25-e39b-4b2e-930f-7e30527d44e5] took 27.0s 2022-01-07T18:41:27.591579070Z [Informational] Azure-Identity: ManagedIdentityCredential.GetToken was unable to retrieve an access token. Scopes: [ https://w-hymon-api-dev.vault.azure.net/.default ] ParentRequestId: 6a11017b-6c04-4734-94d5-e7dbff0ab419 Exception: Azure.Identity.AuthenticationFailedException (0x80131500): ManagedIdentityCredential authentication failed: Service request failed. 2022-01-07T18:41:27.591604570Z Status: 400 (Bad Request) 2022-01-07T18:41:27.591608870Z 2022-01-07T18:41:27.591719069Z Content: 2022-01-07T18:41:27.591726169Z 2022-01-07T18:41:27.591729469Z 2022-01-07T18:41:27.591732669Z Headers: 2022-01-07T18:41:27.591735969Z Date: Fri, 07 Jan 2022 18:41:25 GMT 2022-01-07T18:41:27.591739369Z Server: Kestrel 2022-01-07T18:41:27.591742669Z Transfer-Encoding: chunked 2022-01-07T18:41:27.591745969Z Content-Type: application/json; charset=utf-8 2022-01-07T18:41:27.591749169Z 2022-01-07T18:41:27.591752569Z See the troubleshooting guide for more information. https://aka.ms/azsdk/net/identity/managedidentitycredential/troubleshoot 2022-01-07T18:41:27.591755969Z ---> Azure.RequestFailedException (0x80131500): Service request failed. 2022-01-07T18:41:27.591759569Z Status: 400 (Bad Request) 2022-01-07T18:41:27.591762869Z 2022-01-07T18:41:27.591766069Z Content: 2022-01-07T18:41:27.591769269Z 2022-01-07T18:41:27.591772469Z 2022-01-07T18:41:27.591898068Z Headers: 2022-01-07T18:41:27.591903568Z Date: Fri, 07 Jan 2022 18:41:25 GMT 2022-01-07T18:41:27.591950968Z Server: Kestrel 2022-01-07T18:41:27.591955768Z Transfer-Encoding: chunked 2022-01-07T18:41:27.591959168Z Content-Type: application/json; charset=utf-8 2022-01-07T18:41:27.591974068Z 2022-01-07T18:41:27.710831320Z [Informational] Azure-Identity: DefaultAzureCredential.GetToken was unable to retrieve an access token. Scopes: [ https://w-hymon-api-dev.vault.azure.net/.default ] ParentRequestId: 6a11017b-6c04-4734-94d5-e7dbff0ab419 Exception: Azure.Identity.AuthenticationFailedException (0x80131500): ManagedIdentityCredential authentication failed: Service request failed. 2022-01-07T18:41:27.710892420Z Status: 400 (Bad Request) 2022-01-07T18:41:27.710897620Z 2022-01-07T18:41:27.710900820Z Content: 2022-01-07T18:41:27.710903720Z 2022-01-07T18:41:27.710906420Z 2022-01-07T18:41:27.710909320Z Headers: 2022-01-07T18:41:27.710912020Z Date: Fri, 07 Jan 2022 18:41:25 GMT 2022-01-07T18:41:27.710914920Z Server: Kestrel 2022-01-07T18:41:27.710917820Z Transfer-Encoding: chunked 2022-01-07T18:41:27.710920620Z Content-Type: application/json; charset=utf-8 2022-01-07T18:41:27.710923520Z 2022-01-07T18:41:27.713130208Z See the troubleshooting guide for more information. https://aka.ms/azsdk/net/identity/managedidentitycredential/troubleshoot 2022-01-07T18:41:27.713141307Z ---> Azure.RequestFailedException (0x80131500): Service request failed. 2022-01-07T18:41:27.713144807Z Status: 400 (Bad Request) 2022-01-07T18:41:27.713147707Z 2022-01-07T18:41:27.713150507Z Content: 2022-01-07T18:41:27.713153507Z 2022-01-07T18:41:27.713156207Z 2022-01-07T18:41:27.713165007Z Headers: 2022-01-07T18:41:27.713167907Z Date: Fri, 07 Jan 2022 18:41:25 GMT 2022-01-07T18:41:27.713170807Z Server: Kestrel 2022-01-07T18:41:27.713173707Z Transfer-Encoding: chunked 2022-01-07T18:41:27.713176607Z Content-Type: application/json; charset=utf-8 2022-01-07T18:41:27.713179507Z 2022-01-07T18:41:28.405292836Z [18:41:27 FTL] Host terminated unexpectedly 2022-01-07T18:41:28.405314536Z Azure.Identity.AuthenticationFailedException: ManagedIdentityCredential authentication failed: Service request failed. 2022-01-07T18:41:28.405318836Z Status: 400 (Bad Request) 2022-01-07T18:41:28.405321836Z 2022-01-07T18:41:28.414473386Z Content: 2022-01-07T18:41:28.414491086Z 2022-01-07T18:41:28.414494786Z 2022-01-07T18:41:28.414497886Z Headers: 2022-01-07T18:41:28.414501186Z Date: Fri, 07 Jan 2022 18:41:25 GMT 2022-01-07T18:41:28.414506986Z Server: Kestrel 2022-01-07T18:41:28.414510186Z Transfer-Encoding: chunked 2022-01-07T18:41:28.414513586Z Content-Type: application/json; charset=utf-8 2022-01-07T18:41:28.414516986Z 2022-01-07T18:41:28.414520286Z See the troubleshooting guide for more information. https://aka.ms/azsdk/net/identity/managedidentitycredential/troubleshoot 2022-01-07T18:41:28.414536186Z ---> Azure.RequestFailedException: Service request failed. 2022-01-07T18:41:28.414539886Z Status: 400 (Bad Request) 2022-01-07T18:41:28.414543186Z 2022-01-07T18:41:28.414546186Z Content: 2022-01-07T18:41:28.414549386Z 2022-01-07T18:41:28.414552586Z 2022-01-07T18:41:28.414555786Z Headers: 2022-01-07T18:41:28.414558786Z Date: Fri, 07 Jan 2022 18:41:25 GMT 2022-01-07T18:41:28.414561986Z Server: Kestrel 2022-01-07T18:41:28.414565086Z Transfer-Encoding: chunked 2022-01-07T18:41:28.414568286Z Content-Type: application/json; charset=utf-8 2022-01-07T18:41:28.414571386Z 2022-01-07T18:41:28.414574586Z at Azure.Identity.ManagedIdentitySource.HandleResponseAsync(Boolean async, TokenRequestContext context, Response response, CancellationToken cancellationToken) 2022-01-07T18:41:28.414578086Z at Azure.Identity.ManagedIdentitySource.AuthenticateAsync(Boolean async, TokenRequestContext context, CancellationToken cancellationToken) 2022-01-07T18:41:28.414581586Z at Azure.Identity.ManagedIdentityClient.AuthenticateAsync(Boolean async, TokenRequestContext context, CancellationToken cancellationToken) 2022-01-07T18:41:28.414584986Z at Azure.Identity.ManagedIdentityCredential.GetTokenImplAsync(Boolean async, TokenRequestContext requestContext, CancellationToken cancellationToken) 2022-01-07T18:41:28.414588386Z --- End of inner exception stack trace --- 2022-01-07T18:41:28.414591586Z at Azure.Identity.CredentialDiagnosticScope.FailWrapAndThrow(Exception ex, String additionalMessage) 2022-01-07T18:41:28.414594986Z at Azure.Identity.ManagedIdentityCredential.GetTokenImplAsync(Boolean async, TokenRequestContext requestContext, CancellationToken cancellationToken) 2022-01-07T18:41:28.414598386Z at Azure.Identity.ManagedIdentityCredential.GetTokenAsync(TokenRequestContext requestContext, CancellationToken cancellationToken) 2022-01-07T18:41:28.414601786Z at Azure.Identity.DefaultAzureCredential.GetTokenFromSourcesAsync(TokenCredential[] sources, TokenRequestContext requestContext, Boolean async, CancellationToken cancellationToken) 2022-01-07T18:41:28.414605186Z at Azure.Identity.DefaultAzureCredential.GetTokenImplAsync(Boolean async, TokenRequestContext requestContext, CancellationToken cancellationToken) 2022-01-07T18:41:28.414608586Z at Azure.Identity.CredentialDiagnosticScope.FailWrapAndThrow(Exception ex, String additionalMessage) 2022-01-07T18:41:28.414612486Z at Azure.Identity.DefaultAzureCredential.GetTokenImplAsync(Boolean async, TokenRequestContext requestContext, CancellationToken cancellationToken) 2022-01-07T18:41:28.414615886Z at Azure.Identity.DefaultAzureCredential.GetTokenAsync(TokenRequestContext requestContext, CancellationToken cancellationToken) 2022-01-07T18:41:28.414619186Z at Azure.Core.Pipeline.BearerTokenAuthenticationPolicy.AccessTokenCache.GetHeaderValueFromCredentialAsync(TokenRequestContext context, Boolean async, CancellationToken cancellationToken) 2022-01-07T18:41:28.414625186Z at Azure.Core.Pipeline.BearerTokenAuthenticationPolicy.AccessTokenCache.GetHeaderValueAsync(HttpMessage message, TokenRequestContext context, Boolean async) 2022-01-07T18:41:28.414628686Z at Azure.Core.Pipeline.BearerTokenAuthenticationPolicy.AccessTokenCache.GetHeaderValueAsync(HttpMessage message, TokenRequestContext context, Boolean async) 2022-01-07T18:41:28.414631986Z at Azure.Core.Pipeline.BearerTokenAuthenticationPolicy.AuthenticateAndAuthorizeRequestAsync(HttpMessage message, TokenRequestContext context) 2022-01-07T18:41:28.414635286Z at Azure.Core.Pipeline.BearerTokenAuthenticationPolicy.ProcessAsync(HttpMessage message, ReadOnlyMemory`1 pipeline, Boolean async) 2022-01-07T18:41:28.414638586Z at Azure.Core.Pipeline.RedirectPolicy.ProcessAsync(HttpMessage message, ReadOnlyMemory`1 pipeline, Boolean async) 2022-01-07T18:41:28.414641886Z at Azure.Core.Pipeline.RetryPolicy.ProcessAsync(HttpMessage message, ReadOnlyMemory`1 pipeline, Boolean async) 2022-01-07T18:41:28.414645186Z at Azure.Core.Pipeline.RetryPolicy.ProcessAsync(HttpMessage message, ReadOnlyMemory`1 pipeline, Boolean async) 2022-01-07T18:41:28.414648586Z at Microsoft.Extensions.Configuration.AzureAppConfiguration.UserAgentHeaderPolicy.ProcessAsync(HttpMessage message, ReadOnlyMemory`1 pipeline, Boolean async) 2022-01-07T18:41:28.414651986Z at Azure.Core.Pipeline.HttpPipeline.SendRequestAsync(Request request, CancellationToken cancellationToken) 2022-01-07T18:41:28.414655285Z at Azure.Data.AppConfiguration.ConfigurationClient.GetConfigurationSettingsPageAsync(SettingSelector selector, String pageLink, CancellationToken cancellationToken) 2022-01-07T18:41:28.414658685Z at Azure.Core.PageResponseEnumerator.FuncAsyncPageable`1.AsPages(String continuationToken, Nullable`1 pageSizeHint)+MoveNext() 2022-01-07T18:41:28.414662085Z at Azure.Core.PageResponseEnumerator.FuncAsyncPageable`1.AsPages(String continuationToken, Nullable`1 pageSizeHint)+System.Threading.Tasks.Sources.IValueTaskSource.GetResult() 2022-01-07T18:41:28.414666185Z at Azure.AsyncPageable`1.GetAsyncEnumerator(CancellationToken cancellationToken)+MoveNext() 2022-01-07T18:41:28.414669485Z at Azure.AsyncPageable`1.GetAsyncEnumerator(CancellationToken cancellationToken)+MoveNext() 2022-01-07T18:41:28.414672685Z at Azure.AsyncPageable`1.GetAsyncEnumerator(CancellationToken cancellationToken)+System.Threading.Tasks.Sources.IValueTaskSource.GetResult() 2022-01-07T18:41:28.414676285Z at Microsoft.Extensions.Configuration.AzureAppConfiguration.AzureAppConfigurationProvider.<>c__DisplayClass20_3.<b__4>d.MoveNext() 2022-01-07T18:41:28.414679885Z --- End of stack trace from previous location --- 2022-01-07T18:41:28.414683185Z at Microsoft.Extensions.Configuration.AzureAppConfiguration.AzureAppConfigurationProvider.<>c__DisplayClass20_3.<b__4>d.MoveNext() 2022-01-07T18:41:28.414686685Z --- End of stack trace from previous location --- 2022-01-07T18:41:28.414689885Z at Microsoft.Extensions.Configuration.AzureAppConfiguration.TracingUtils.CallWithRequestTracing(Boolean tracingEnabled, RequestType requestType, RequestTracingOptions requestTracingOptions, Func`1 clientCall) 2022-01-07T18:41:28.414695785Z at Microsoft.Extensions.Configuration.AzureAppConfiguration.AzureAppConfigurationProvider.CallWithRequestTracing(Func`1 clientCall) 2022-01-07T18:41:28.414699185Z at Microsoft.Extensions.Configuration.AzureAppConfiguration.AzureAppConfigurationProvider.LoadAll(Boolean ignoreFailures) 2022-01-07T18:41:28.414702485Z at Microsoft.Extensions.Configuration.AzureAppConfiguration.AzureAppConfigurationProvider.Load() 2022-01-07T18:41:28.414705785Z at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList`1 providers) 2022-01-07T18:41:28.414709885Z at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build() 2022-01-07T18:41:28.414713285Z at Microsoft.Extensions.Hosting.HostBuilder.BuildAppConfiguration() 2022-01-07T18:41:28.414716585Z at Microsoft.Extensions.Hosting.HostBuilder.Build() 2022-01-07T18:41:28.414719785Z at SMA.HyMon.API.Config.Program.Main(String[] args) in C:\Users\papst\source\repos\HyMon\Hybrid Monitoring Cloud\src\SMA.HyMon.API.Config\Program.cs:line 42 2022-01-07T18:41:28.484618104Z [18:41:28 INF] Application Info: 2.0.56+cbe9899f57, Git: cbe9899f5719b7f0d5915d77275dd19d98804c11, Build: 01/07/2022 11:08:23 ```

Please note the Scope for the Key Vault.

Try 1: With Event Source Listener, only Key Vault ``` 2022-01-07T18:49:31.879661507Z [18:49:31 INF] Starting web host for App SMA.HyMon.API.Config 2022-01-07T18:49:32.533710838Z [18:49:32 INF] Application Info: 2.0.56+cbe9899f57, Git: cbe9899f5719b7f0d5915d77275dd19d98804c11, Build: 01/07/2022 11:08:23 2022-01-07T18:49:36.274465244Z [Informational] Azure-Core: Request [a67be17d-67e2-49bb-8aef-2a9660ce7d9b] GET https://w-hymon-api-dev.vault.azure.net/secrets/?api-version=REDACTED 2022-01-07T18:49:36.274489544Z Accept:REDACTED 2022-01-07T18:49:36.274494344Z x-ms-client-request-id:REDACTED 2022-01-07T18:49:36.274498544Z x-ms-return-client-request-id:REDACTED 2022-01-07T18:49:36.274502744Z User-Agent:REDACTED 2022-01-07T18:49:36.274506743Z Content-Type:REDACTED 2022-01-07T18:49:36.275814134Z client assembly: Azure.Security.KeyVault.Secrets 2022-01-07T18:49:47.550267296Z [Warning] Azure-Core: Error response [a67be17d-67e2-49bb-8aef-2a9660ce7d9b] 401 Unauthorized (11.2s) 2022-01-07T18:49:47.550324196Z Cache-Control:REDACTED 2022-01-07T18:49:47.550336096Z Pragma:REDACTED 2022-01-07T18:49:47.550340696Z WWW-Authenticate:REDACTED 2022-01-07T18:49:47.550350996Z x-ms-keyvault-region:REDACTED 2022-01-07T18:49:47.550354696Z x-ms-client-request-id:REDACTED 2022-01-07T18:49:47.550358196Z x-ms-request-id:REDACTED 2022-01-07T18:49:47.550361796Z x-ms-keyvault-service-version:REDACTED 2022-01-07T18:49:47.559422445Z x-ms-keyvault-network-info:REDACTED 2022-01-07T18:49:47.559442344Z X-Powered-By:REDACTED 2022-01-07T18:49:47.559446744Z Strict-Transport-Security:REDACTED 2022-01-07T18:49:47.559450444Z X-Content-Type-Options:REDACTED 2022-01-07T18:49:47.559454044Z Date:REDACTED 2022-01-07T18:49:47.559457944Z Content-Length:REDACTED 2022-01-07T18:49:47.559461544Z Content-Type:REDACTED 2022-01-07T18:49:47.559465144Z Expires:REDACTED 2022-01-07T18:49:47.559468644Z 2022-01-07T18:49:47.569284789Z [Warning] Azure-Core: Response [a67be17d-67e2-49bb-8aef-2a9660ce7d9b] took 11.2s 2022-01-07T18:49:47.770304254Z [Informational] Azure-Identity: DefaultAzureCredential.GetToken invoked. Scopes: [ https://vault.azure.net/.default ] ParentRequestId: a67be17d-67e2-49bb-8aef-2a9660ce7d9b 2022-01-07T18:49:47.901066316Z [Informational] Azure-Identity: EnvironmentCredential.GetToken invoked. Scopes: [ https://vault.azure.net/.default ] ParentRequestId: a67be17d-67e2-49bb-8aef-2a9660ce7d9b 2022-01-07T18:49:47.939658398Z [Informational] Azure-Identity: EnvironmentCredential.GetToken was unable to retrieve an access token. Scopes: [ https://vault.azure.net/.default ] ParentRequestId: a67be17d-67e2-49bb-8aef-2a9660ce7d9b Exception: Azure.Identity.CredentialUnavailableException (0x80131500): EnvironmentCredential authentication unavailable. Environment variables are not fully configured. See the troubleshooting guide for more information. https://aka.ms/azsdk/net/identity/environmentcredential/troubleshoot 2022-01-07T18:49:48.021030639Z [Informational] Azure-Identity: ManagedIdentityCredential.GetToken invoked. Scopes: [ https://vault.azure.net/.default ] ParentRequestId: a67be17d-67e2-49bb-8aef-2a9660ce7d9b 2022-01-07T18:49:48.060820914Z [Informational] Azure-Core: Request [e377836b-172a-456a-8384-b86581801586] GET http://169.254.132.7:8081/msi/token?api-version=REDACTED&resource=REDACTED 2022-01-07T18:49:48.060864114Z secret:REDACTED 2022-01-07T18:49:48.069062768Z x-ms-client-request-id:REDACTED 2022-01-07T18:49:48.069080468Z x-ms-return-client-request-id:REDACTED 2022-01-07T18:49:48.069084868Z User-Agent:REDACTED 2022-01-07T18:49:48.069088568Z client assembly: Azure.Identity 2022-01-07T18:49:56.156562734Z [Informational] Azure-Core: Response [e377836b-172a-456a-8384-b86581801586] 200 OK (08.1s) 2022-01-07T18:49:56.156649233Z Date:REDACTED 2022-01-07T18:49:56.156655733Z Server:REDACTED 2022-01-07T18:49:56.156660033Z Transfer-Encoding:REDACTED 2022-01-07T18:49:56.156664233Z Content-Type:REDACTED 2022-01-07T18:49:56.156668333Z 2022-01-07T18:49:56.156756033Z [Warning] Azure-Core: Response [e377836b-172a-456a-8384-b86581801586] took 08.1s 2022-01-07T18:49:56.339270732Z [Informational] Azure-Identity: ManagedIdentityCredential.GetToken succeeded. Scopes: [ https://vault.azure.net/.default ] ParentRequestId: a67be17d-67e2-49bb-8aef-2a9660ce7d9b ExpiresOn: 2022-01-08T18:49:54.0000000+00:00 2022-01-07T18:49:56.348209579Z [Informational] Azure-Identity: DefaultAzureCredential credential selected: Azure.Identity.ManagedIdentityCredential 2022-01-07T18:49:56.348734175Z [Informational] Azure-Identity: DefaultAzureCredential.GetToken succeeded. Scopes: [ https://vault.azure.net/.default ] ParentRequestId: a67be17d-67e2-49bb-8aef-2a9660ce7d9b ExpiresOn: 2022-01-08T18:49:54.0000000+00:00 2022-01-07T18:49:56.387049044Z [Informational] Azure-Core: Request [a67be17d-67e2-49bb-8aef-2a9660ce7d9b] GET https://w-hymon-api-dev.vault.azure.net/secrets/?api-version=REDACTED 2022-01-07T18:49:56.387096644Z Accept:REDACTED 2022-01-07T18:49:56.387101344Z x-ms-client-request-id:REDACTED 2022-01-07T18:49:56.387105144Z x-ms-return-client-request-id:REDACTED 2022-01-07T18:49:56.388277337Z User-Agent:REDACTED 2022-01-07T18:49:56.388290337Z Authorization:REDACTED 2022-01-07T18:49:56.388294337Z Content-Type:REDACTED 2022-01-07T18:49:56.388983133Z client assembly: Azure.Security.KeyVault.Secrets 2022-01-07T18:49:56.479145089Z [Informational] Azure-Core: Response [a67be17d-67e2-49bb-8aef-2a9660ce7d9b] 200 OK (00.1s) 2022-01-07T18:49:56.479169989Z Cache-Control:REDACTED 2022-01-07T18:49:56.479173889Z Pragma:REDACTED 2022-01-07T18:49:56.479176889Z x-ms-keyvault-region:REDACTED 2022-01-07T18:49:56.479179989Z x-ms-client-request-id:REDACTED 2022-01-07T18:49:56.479182889Z x-ms-request-id:REDACTED 2022-01-07T18:49:56.479185689Z x-ms-keyvault-service-version:REDACTED 2022-01-07T18:49:56.479188689Z x-ms-keyvault-network-info:REDACTED 2022-01-07T18:49:56.486654344Z x-ms-keyvault-rbac-assignment-id:REDACTED 2022-01-07T18:49:56.486669744Z x-ms-keyvault-rbac-cache:REDACTED 2022-01-07T18:49:56.486674044Z X-Powered-By:REDACTED 2022-01-07T18:49:56.486677644Z Strict-Transport-Security:REDACTED 2022-01-07T18:49:56.486681144Z X-Content-Type-Options:REDACTED 2022-01-07T18:49:56.486684744Z Date:REDACTED 2022-01-07T18:49:56.486688244Z Content-Type:REDACTED 2022-01-07T18:49:56.486691744Z Expires:REDACTED 2022-01-07T18:49:56.486695244Z Content-Length:REDACTED 2022-01-07T18:49:56.486698844Z 2022-01-07T18:49:56.607497315Z [Informational] Azure-Core: Request [1c5540bb-7c9d-42de-8996-05e6046d7f6b] GET https://w-hymon-api-dev.vault.azure.net/secrets/B2CClient--Secret/?api-version=REDACTED 2022-01-07T18:49:56.607525315Z Accept:REDACTED 2022-01-07T18:49:56.607530315Z x-ms-client-request-id:REDACTED 2022-01-07T18:49:56.607534215Z x-ms-return-client-request-id:REDACTED 2022-01-07T18:49:56.607561115Z User-Agent:REDACTED 2022-01-07T18:49:56.608514409Z Authorization:REDACTED 2022-01-07T18:49:56.608526809Z Content-Type:REDACTED 2022-01-07T18:49:56.608530009Z client assembly: Azure.Security.KeyVault.Secrets 2022-01-07T18:49:56.627870592Z [Informational] Azure-Core: Response [1c5540bb-7c9d-42de-8996-05e6046d7f6b] 200 OK (00.0s) 2022-01-07T18:49:56.627889092Z Cache-Control:REDACTED 2022-01-07T18:49:56.627892892Z Pragma:REDACTED 2022-01-07T18:49:56.627895792Z x-ms-keyvault-region:REDACTED 2022-01-07T18:49:56.627898792Z x-ms-client-request-id:REDACTED 2022-01-07T18:49:56.627901692Z x-ms-request-id:REDACTED 2022-01-07T18:49:56.627904592Z x-ms-keyvault-service-version:REDACTED 2022-01-07T18:49:56.627907392Z x-ms-keyvault-network-info:REDACTED 2022-01-07T18:49:56.629382183Z x-ms-keyvault-rbac-assignment-id:REDACTED 2022-01-07T18:49:56.629404083Z x-ms-keyvault-rbac-cache:REDACTED 2022-01-07T18:49:56.629407683Z X-Powered-By:REDACTED 2022-01-07T18:49:56.629410483Z Strict-Transport-Security:REDACTED 2022-01-07T18:49:56.629413283Z X-Content-Type-Options:REDACTED 2022-01-07T18:49:56.629416083Z Date:REDACTED 2022-01-07T18:49:56.629418883Z Content-Type:REDACTED 2022-01-07T18:49:56.629421583Z Expires:REDACTED 2022-01-07T18:49:56.629424383Z Content-Length:REDACTED 2022-01-07T18:49:56.629427183Z 2022-01-07T18:49:58.045751381Z [18:49:58 INF] Azure Web Sites environment detected. Using '/home/ASP.NET/DataProtection-Keys' as key repository; keys will not be encrypted at rest. 2022-01-07T18:49:59.311248846Z [18:49:59 INF] Now listening on: http://[::]:8080 2022-01-07T18:49:59.361179856Z [18:49:59 INF] Application started. Press Ctrl+C to shut down. 2022-01-07T18:49:59.361990052Z [18:49:59 INF] Hosting environment: Production 2022-01-07T18:49:59.362665448Z [18:49:59 INF] Content root path: /home/site/wwwroot 2022-01-07T18:49:59.542690804Z [18:49:59 INF] Request starting HTTP/1.1 GET http://169.254.132.5:8080/robots933456.txt - - 2022-01-07T18:49:59.579851189Z [18:49:59 WRN] Failed to determine the https port for redirect. 2022-01-07T18:49:59.862335552Z [18:49:59 ERR] Connection id "0HMEI1Q0OGL44", Request id "0HMEI1Q0OGL44:00000002": An unhandled exception was thrown by the application. 2022-01-07T18:49:59.862399751Z System.ArgumentNullException: IDW10106: The 'ClientId' option must be provided. ```

For the last one, I needed to remove the dependencies to Microsoft.Extensions.Configuration.AzureAppConfiguration (4.5.1) in order not to get the previous exception. Please note the correct scope for the Key Vault.

heaths commented 2 years ago

The scope for App Configuration should be the full scheme + host name + "/.default" so in cases like scenario 1 it is correct. For Key Vault, it should be (when using AzureCloud i.e. the public cloud) "https://vault.azure.net/.default" so those scenarios like scenario 3 are correct.

So the only scenario that seems to break is scenario 2 with both App Configuration and Key Vault, which, as you pointed out, uses the wrong scope. In fact, what's really confusing is that it's hosting the full hostname for Key Vault but the request seems to be coming from App Configuration:

2022-01-07T18:40:59.178919102Z [Informational] Azure-Identity: DefaultAzureCredential.GetToken invoked. Scopes: [ https://w-hymon-api-dev.vault.azure.net/.default ] ParentRequestId: 6a11017b-6c04-4734-94d5-e7dbff0ab419
2022-01-07T18:40:59.179353000Z [Informational] Azure-Identity: EnvironmentCredential.GetToken invoked. Scopes: [ https://w-hymon-api-dev.vault.azure.net/.default ] ParentRequestId: 6a11017b-6c04-4734-94d5-e7dbff0ab419
2022-01-07T18:40:59.219836083Z [Informational] Azure-Identity: EnvironmentCredential.GetToken was unable to retrieve an access token. Scopes: [ https://w-hymon-api-dev.vault.azure.net/.default ] ParentRequestId: 6a11017b-6c04-4734-94d5-e7dbff0ab419 Exception: Azure.Identity.CredentialUnavailableException (0x80131500): EnvironmentCredential authentication unavailable. Environment variables are not fully configured. See the troubleshooting guide for more information. https://aka.ms/azsdk/net/identity/environmentcredential/troubleshoot
2022-01-07T18:40:59.508454541Z [Informational] Azure-Identity: ManagedIdentityCredential.GetToken invoked. Scopes: [ https://w-hymon-api-dev.vault.azure.net/.default ] ParentRequestId: 6a11017b-6c04-4734-94d5-e7dbff0ab419
2022-01-07T18:41:00.129436023Z [Informational] Azure-Core: Request [88a39a25-e39b-4b2e-930f-7e30527d44e5] GET http://169.254.132.8:8081/msi/token?api-version=REDACTED&resource=REDACTED
...
2022-01-07T18:41:28.414615886Z    at Azure.Identity.DefaultAzureCredential.GetTokenAsync(TokenRequestContext requestContext, CancellationToken cancellationToken)
2022-01-07T18:41:28.414619186Z    at Azure.Core.Pipeline.BearerTokenAuthenticationPolicy.AccessTokenCache.GetHeaderValueFromCredentialAsync(TokenRequestContext context, Boolean async, CancellationToken cancellationToken)
2022-01-07T18:41:28.414625186Z    at Azure.Core.Pipeline.BearerTokenAuthenticationPolicy.AccessTokenCache.GetHeaderValueAsync(HttpMessage message, TokenRequestContext context, Boolean async)
2022-01-07T18:41:28.414628686Z    at Azure.Core.Pipeline.BearerTokenAuthenticationPolicy.AccessTokenCache.GetHeaderValueAsync(HttpMessage message, TokenRequestContext context, Boolean async)
2022-01-07T18:41:28.414631986Z    at Azure.Core.Pipeline.BearerTokenAuthenticationPolicy.AuthenticateAndAuthorizeRequestAsync(HttpMessage message, TokenRequestContext context)
2022-01-07T18:41:28.414635286Z    at Azure.Core.Pipeline.BearerTokenAuthenticationPolicy.ProcessAsync(HttpMessage message, ReadOnlyMemory`1 pipeline, Boolean async)
2022-01-07T18:41:28.414638586Z    at Azure.Core.Pipeline.RedirectPolicy.ProcessAsync(HttpMessage message, ReadOnlyMemory`1 pipeline, Boolean async)
2022-01-07T18:41:28.414641886Z    at Azure.Core.Pipeline.RetryPolicy.ProcessAsync(HttpMessage message, ReadOnlyMemory`1 pipeline, Boolean async)
2022-01-07T18:41:28.414645186Z    at Azure.Core.Pipeline.RetryPolicy.ProcessAsync(HttpMessage message, ReadOnlyMemory`1 pipeline, Boolean async)
2022-01-07T18:41:28.414648586Z    at Microsoft.Extensions.Configuration.AzureAppConfiguration.UserAgentHeaderPolicy.ProcessAsync(HttpMessage message, ReadOnlyMemory`1 pipeline, Boolean async)
2022-01-07T18:41:28.414651986Z    at Azure.Core.Pipeline.HttpPipeline.SendRequestAsync(Request request, CancellationToken cancellationToken)
2022-01-07T18:41:28.414655285Z    at Azure.Data.AppConfiguration.ConfigurationClient.GetConfigurationSettingsPageAsync(SettingSelector selector, String pageLink, CancellationToken cancellationToken)
2022-01-07T18:41:28.414658685Z    at Azure.Core.PageResponseEnumerator.FuncAsyncPageable`1.AsPages(String continuationToken, Nullable`1 pageSizeHint)+MoveNext()
2022-01-07T18:41:28.414662085Z    at Azure.Core.PageResponseEnumerator.FuncAsyncPageable`1.AsPages(String continuationToken, Nullable`1 pageSizeHint)+System.Threading.Tasks.Sources.IValueTaskSource<System.Boolean>.GetResult()
2022-01-07T18:41:28.414666185Z    at Azure.AsyncPageable`1.GetAsyncEnumerator(CancellationToken cancellationToken)+MoveNext()
2022-01-07T18:41:28.414669485Z    at Azure.AsyncPageable`1.GetAsyncEnumerator(CancellationToken cancellationToken)+MoveNext()
2022-01-07T18:41:28.414672685Z    at Azure.AsyncPageable`1.GetAsyncEnumerator(CancellationToken cancellationToken)+System.Threading.Tasks.Sources.IValueTaskSource<System.Boolean>.GetResult()
2022-01-07T18:41:28.414676285Z    at Microsoft.Extensions.Configuration.AzureAppConfiguration.AzureAppConfigurationProvider.<>c__DisplayClass20_3.<<LoadAll>b__4>d.MoveNext()
2022-01-07T18:41:28.414679885Z --- End of stack trace from previous location ---
2022-01-07T18:41:28.414683185Z    at Microsoft.Extensions.Configuration.AzureAppConfiguration.AzureAppConfigurationProvider.<>c__DisplayClass20_3.<<LoadAll>b__4>d.MoveNext()
2022-01-07T18:41:28.414686685Z --- End of stack trace from previous location ---
2022-01-07T18:41:28.414689885Z    at Microsoft.Extensions.Configuration.AzureAppConfiguration.TracingUtils.CallWithRequestTracing(Boolean tracingEnabled, RequestType requestType, RequestTracingOptions requestTracingOptions, Func`1 clientCall)

Do you try to access any values during startup outside your 'CreateHostBuilder' method? Looking at the stack trace, it seems (though compiler optimizations may have inlined) the exception is thrown outside this method. I'll try to access some App Configuration values in my sample, as currently I'm just configuring it, then configuring Key Vault (much like you are), and have an endpoint that queries Key Vault.

As for the robots.txt query, those seem to be coming from the App Services host itself, given the prefix matches what they are using for host logs. To my knowledge, we don't have any SDKs that would even care to query robots.txt since we're making REST (or gRPC) calls directly to Azure endpoints.

heaths commented 2 years ago

Would you also be willing to share a process dump with your current changes for scenario 2 above with your support representative for case 2111300050001183 you mentioned in your OP? I could also get in contact with you over email and provide a OneDrive for Business link I'll have cleaned up within 48hrs, compliant with GDPR. I realize you're in Germany and there's a time difference, but if you get this before Monday or Tuesday and let me know what works for you, I can login early and get a hold of you that way.

MPapst commented 2 years ago

I am accessing some const string properties of the Program class (like AppName) - nothing else.

But you pointed on something.. that the scope and the exception is coming from the App Config and that for the app config the scope is the complete url... and after trying that, I am totally sorry - this is a terrible mistake from my side.

For better understanding what is happening here: I am creating an Uri?-typed variable to hold the parsed Uri for the App Configuration which I am reusing later for the Key Vault. Despite it looks like it is stored and executed, the code is executed when the Configuration Provider is build, which means that the App Configuration config provider reads the variable endpoint after it is reused for holding the Key Vault endpoint. That is the reason, the App Configuration tries to get a token for the scope for the Key Vault endpoint.

Thanks for the offer to getting in direct contact. I just dropped you a message on Teams while I was working on the fix, to let you know I am still working on it today.

The only two things I do not understand are

Shall I close the issue or is this something that could be added as a feature to the App Configuration Library, that checks the validity of the given endpoint?

heaths commented 2 years ago

I'm glad you found it! I see where the variable was getting captured by reference and resolved later. Sorry I failed to see that initially, too. Good find!

As for why you needed to remove the dependency, I'm not sure. In one of my repro attempts I merely commented out any App Configuration-related code but kept he references the same. IF they don't get called, they shouldn't interfere.

I'll go ahead and close this. We have talked about validating endpoints in the past but decided it was a slippery slope, causing problems when we add new cloud environments, for example, as well as support against Azure Stack which would have a entirely customer-driven endpoint domains. But this is a good lesson in watching out for reference captures in delegate-driven configuration I'll pass along so we'll remember to watch for it again if we see behavior like this. I suspected a static cache somewhere but wasn't finding anything like that in the suspected code paths.