google-api-dotnet-client: Application Default Credentials not available even if running on Google Compute Engine
So this is a weird one. Our application is running in a managed instance group inside a Docker container on Google Compute Engine, on multiple machines, running in the region us-east4.
Sometimes when we are deploying and the managed instance group starts replacing our instances, we’re receiving the following InvalidOperationException from the new instances.
One or more errors occurred. (The Application Default Credentials are not available.
They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials.
See https://developers.google.com/accounts/docs/application-default-credentials for more information.)
The Application Default Credentials are not available. They are available if running in Google Compute Engine.
Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials.
See https://developers.google.com/accounts/docs/application-default-credentials for more information.
Now the thing is that for some reason this issue starts to go away after a couple of minutes and the Google Cloud Client library suddenly starts finding the default credentials on the Compute Engine machine.
We’re using Firebase authentication and the code for initializing the Firebase Admin SDK in our ASP.NET Core 5 application looks like this:
services.AddSingleton<FirebaseApp>(options =>
{
return FirebaseApp.Create(new AppOptions
{
Credential = GoogleCredential.GetApplicationDefault(),
ProjectId = "the_project_id"
});
});
services.AddSingleton<FirebaseAuth>(options =>
{
var defaultFirebaseApp = options.GetRequiredService<FirebaseApp>();
return FirebaseAuth.GetAuth(defaultFirebaseApp);
});
When a controller is invoked that requires an instance of FirebaseAuth, we’re getting an exception with a stack trace like this:
System.AggregateException:
at System.Threading.Tasks.Task`1.GetResultCore (System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at Google.Apis.Auth.OAuth2.GoogleCredential.GetApplicationDefault (Google.Apis.Auth, Version=1.50.0.0, Culture=neutral, PublicKeyToken=4b01fa6e34db77ab)
at Alexandria.Startup+<>c.<ConfigureServices>b__9_0 (Alexandria, Version=1.0.0.0, Culture=neutral, PublicKeyToken=nullAlexandria, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: /app/src/Alexandria/Startup.csAlexandria, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: 174)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory (Microsoft.Extensions.DependencyInjection, Version=5.0.0.1, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache (Microsoft.Extensions.DependencyInjection, Version=5.0.0.1, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache (Microsoft.Extensions.DependencyInjection, Version=5.0.0.1, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite (Microsoft.Extensions.DependencyInjection, Version=5.0.0.1, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve (Microsoft.Extensions.DependencyInjection, Version=5.0.0.1, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine+<>c__DisplayClass1_0.<RealizeService>b__0 (Microsoft.Extensions.DependencyInjection, Version=5.0.0.1, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService (Microsoft.Extensions.DependencyInjection.Abstractions, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService (Microsoft.Extensions.DependencyInjection.Abstractions, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at Alexandria.Startup+<>c.<ConfigureServices>b__9_1 (Alexandria, Version=1.0.0.0, Culture=neutral, PublicKeyToken=nullAlexandria, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: /app/src/Alexandria/Startup.csAlexandria, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: 177)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory (Microsoft.Extensions.DependencyInjection, Version=5.0.0.1, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache (Microsoft.Extensions.DependencyInjection, Version=5.0.0.1, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache (Microsoft.Extensions.DependencyInjection, Version=5.0.0.1, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite (Microsoft.Extensions.DependencyInjection, Version=5.0.0.1, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor (Microsoft.Extensions.DependencyInjection, Version=5.0.0.1, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache (Microsoft.Extensions.DependencyInjection, Version=5.0.0.1, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache (Microsoft.Extensions.DependencyInjection, Version=5.0.0.1, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite (Microsoft.Extensions.DependencyInjection, Version=5.0.0.1, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor (Microsoft.Extensions.DependencyInjection, Version=5.0.0.1, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache (Microsoft.Extensions.DependencyInjection, Version=5.0.0.1, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache (Microsoft.Extensions.DependencyInjection, Version=5.0.0.1, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite (Microsoft.Extensions.DependencyInjection, Version=5.0.0.1, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve (Microsoft.Extensions.DependencyInjection, Version=5.0.0.1, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine+<>c__DisplayClass1_0.<RealizeService>b__0 (Microsoft.Extensions.DependencyInjection, Version=5.0.0.1, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService (Microsoft.Extensions.DependencyInjection.Abstractions, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at lambda_method292 (Anonymously Hosted DynamicMethods Assembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null)
at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider+<>c__DisplayClass5_0.<CreateControllerFactory>g__CreateController|0 (Microsoft.AspNetCore.Mvc.Core, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next (Microsoft.AspNetCore.Mvc.Core, Version=5.0.0.0,
--- *snip a bunch of middleware*---
Inner exception System.InvalidOperationException handled at System.Threading.Tasks.Task`1.GetResultCore:
at Google.Apis.Auth.OAuth2.DefaultCredentialProvider+<CreateDefaultCredentialAsync>d__11.MoveNext (Google.Apis.Auth, Version=1.50.0.0, Culture=neutral, PublicKeyToken=4b01fa6e34db77ab)
To me it really doesn’t make any sense that the Compute Engine metadata isn’t available right after starting the application and suddenly becomes available after a few minutes. I found an issue that looks very similar here https://github.com/googleapis/google-cloud-dotnet/issues/1394, but apparently this has been resolved inside of the library itself, by switching from a URL to an IP address for the metadata server.
Is there anything dumb that I’m doing wrong here when initializing the GoogleCredential, or is this some kind of deeper issue? Anything that I can do to help debug or resolve this problem? Unfortunately I have no idea how to reproduce this, it happens sporadically after deploying and suddenly resolved after a short time.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 15 (1 by maintainers)
@flagbug We had a similar issue a while back with someone using the Google.Cloud.Logging.V2 library, just didn’t ring any bells until you mentioned the thread starvation sympton. Unfurtunately the issue was tracked in an internal system so I cannot point you there, but I’ll do my best to “translate” what was happening there to this case. I also think I know how you could fix this issue, and that’s by not deferring the obtention of the credentials until your first request, but doing it directly in Startup (you’ll see the rationale for this after reading below my “translation” from the other issue).
But basically, I think that your code that looks like this:
should look like this to avoid this problem:
Or even:
What I think it’s happening:
That’s why creating the singletons on Startup, which means that effectively the credential is fetched before requests start comming in an depleting the thread pool, should fix it for you. It also explains why it happens sometimes, it all depends on how soon the credential is fetched and how lucky that operation is in finding a thread to return to, which in turn depends on the amount of requests you are receiving right after startup.
If we had async DI, this wouldn’t be a problem either.
Hope this helps.