Sudden datastore exceptions : DatastoreFailureException: Missing or invalid authentication

Viewed 142

I have a Google Appengine app (Java) and the datastore was recently auto-migrated into "Cloud Firestore in Datastore mode" (completed on 6/6/2022) that has been running for years.

It has now started throwing these exceptions in new deployments without any relevant code change to trigger them:

com.google.appengine.api.datastore.DatastoreFailureException: Missing or invalid authentication.
    at com.google.appengine.api.datastore.DatastoreApiHelper.translateError(DatastoreApiHelper.java:69)
    at com.google.appengine.api.datastore.DatastoreApiHelper$1.convertException(DatastoreApiHelper.java:127)
    at com.google.appengine.api.utils.FutureWrapper.get(FutureWrapper.java:97)
    at com.google.appengine.api.utils.FutureWrapper.get(FutureWrapper.java:89)
    at com.google.appengine.api.datastore.FutureHelper.getInternal(FutureHelper.java:68)
    at com.google.appengine.api.datastore.FutureHelper.quietGet(FutureHelper.java:32)
    at com.google.appengine.api.datastore.BaseQueryResultsSource.getIndexList(BaseQueryResultsSource.java:154)
    at com.google.appengine.api.datastore.BaseQueryResultsSource.loadMoreEntities(BaseQueryResultsSource.java:187)
    at com.google.appengine.api.datastore.BaseQueryResultsSource.loadMoreEntities(BaseQueryResultsSource.java:166)
    at com.google.appengine.api.datastore.QueryResultIteratorImpl.ensureLoaded(QueryResultIteratorImpl.java:146)
    at com.google.appengine.api.datastore.QueryResultIteratorImpl.hasNext(QueryResultIteratorImpl.java:64)
    at com.googlecode.objectify.impl.KeysOnlyIterator.hasNext(KeysOnlyIterator.java:29)
    at com.googlecode.objectify.impl.ChunkIterator.next(ChunkIterator.java:48)
    at com.googlecode.objectify.impl.ChunkIterator.next(ChunkIterator.java:20)
    at com.google.common.collect.MultitransformedIterator.hasNext(MultitransformedIterator.java:52)
    at com.google.common.collect.MultitransformedIterator.hasNext(MultitransformedIterator.java:50)
    at com.google.common.collect.Iterators$PeekingImpl.hasNext(Iterators.java:1105)
    at com.googlecode.objectify.impl.ChunkingIterator.hasNext(ChunkingIterator.java:51)
    at com.google.common.collect.Iterators.addAll(Iterators.java:366)
    at com.google.common.collect.Lists.newArrayList(Lists.java:163)
    at com.googlecode.objectify.util.MakeListResult.translate(MakeListResult.java:22)
    at com.googlecode.objectify.util.MakeListResult.translate(MakeListResult.java:12)
    at com.googlecode.objectify.util.ResultTranslator.nowUncached(ResultTranslator.java:21)
    at com.googlecode.objectify.util.ResultCache.now(ResultCache.java:30)
    at com.googlecode.objectify.util.ResultProxy.invoke(ResultProxy.java:34)
    at com.sun.proxy.$Proxy68.isEmpty(Unknown Source)

My code. After loading a list of entities, the exception is thrown on the isEmpty() call on the returned list:

List<SystemSetting> applicationSettings = ofy().load().type(SystemSetting.class).filter("name", name).list(); 

if (applicationSettings.isEmpty()) { // Exception thrown here

It's worth also noting that on previous versions, datastore reads and writes are still happening without any issues. It is only new deployments that exhibit this issue. However, redeploying previous versions from previous commits using the same CI service to deploy them are failing.

Is this caused by the datastore migration?

Slight update: these errors are only happening on newly deployed versions... In fact, I deployed a new version (from CI, bitbucket pipeline) that worked, but when I redeployed it with no changes it no longer works. Same app engine version id, code and users etc.

Things we've tried without success:

  1. Re-deploying last known version
  2. Clearing memcache
  3. Deploying from local dev
  4. Stopping instances
  5. Trying alternative service account key
  6. Expanding service account permissions
  7. Clearing BitBucket pipeline caches (the CI builder)
  8. Have recreated the issue with datastore api directly (no objectify)
  9. Have redeployed with different user account - no success
  10. Have redeployed with new service account - no success

Update 1:

If I run the following code on a local java application (pointing at live datastore using same service account) it works locally, but fails if I deploy to app engine with my project-id.

DatastoreOptions datastoreOptions = DatastoreOptions.getDefaultInstance();
Datastore datastore = datastoreOptions.getService();

// A known entity in my live datastore
Key key = datastore.newKeyFactory()
                   .setKind("MyEntityType")
                   .newKey(123123123l);
Entity retrieved = datastore.get(key);

When it fails, I can get slightly more information:

java.io.IOException: Unexpected Error code 500 trying to get security access token from Compute Engine metadata for the default service account: Could not fetch URI /computeMetadata/v1/instance/service-accounts/default/token

full stack:

com.google.cloud.datastore.DatastoreException: I/O error
    at com.google.cloud.datastore.spi.v1.HttpDatastoreRpc.translate(HttpDatastoreRpc.java:138)
    at com.google.cloud.datastore.spi.v1.HttpDatastoreRpc.translate(HttpDatastoreRpc.java:123)
    at com.google.cloud.datastore.spi.v1.HttpDatastoreRpc.lookup(HttpDatastoreRpc.java:173)
    at com.google.cloud.datastore.DatastoreImpl$3.call(DatastoreImpl.java:434)
    at com.google.cloud.datastore.DatastoreImpl$3.call(DatastoreImpl.java:431)
    at com.google.api.gax.retrying.DirectRetryingExecutor.submit(DirectRetryingExecutor.java:103)
    at com.google.cloud.RetryHelper.run(RetryHelper.java:76)
    at com.google.cloud.RetryHelper.runWithRetries(RetryHelper.java:50)
    at com.google.cloud.datastore.DatastoreImpl.lookup(DatastoreImpl.java:430)
...
Caused by: com.google.datastore.v1.client.DatastoreException: I/O error, code=UNAVAILABLE
    at com.google.datastore.v1.client.RemoteRpc.makeException(RemoteRpc.java:171)
    at com.google.datastore.v1.client.RemoteRpc.call(RemoteRpc.java:117)
    at com.google.datastore.v1.client.Datastore.lookup(Datastore.java:93)
    at com.google.cloud.datastore.spi.v1.HttpDatastoreRpc.lookup(HttpDatastoreRpc.java:171)
    ... 74 more
Caused by: java.io.IOException: Unexpected Error code 500 trying to get security access token from Compute Engine metadata for the default service account: Could not fetch URI /computeMetadata/v1/instance/service-accounts/default/token

    at com.google.auth.oauth2.ComputeEngineCredentials.refreshAccessToken(ComputeEngineCredentials.java:206)
    at com.google.auth.oauth2.OAuth2Credentials$1.call(OAuth2Credentials.java:257)
    at com.google.auth.oauth2.OAuth2Credentials$1.call(OAuth2Credentials.java:254)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at com.google.common.util.concurrent.DirectExecutor.execute(DirectExecutor.java:31)
    at com.google.auth.oauth2.OAuth2Credentials$AsyncRefreshResult.executeIfNew(OAuth2Credentials.java:580)
    at com.google.auth.oauth2.OAuth2Credentials.asyncFetch(OAuth2Credentials.java:220)
    at com.google.auth.oauth2.OAuth2Credentials.getRequestMetadata(OAuth2Credentials.java:170)
    at com.google.auth.http.HttpCredentialsAdapter.initialize(HttpCredentialsAdapter.java:96)
    at com.google.cloud.http.HttpTransportOptions$1.initialize(HttpTransportOptions.java:159)
    at com.google.cloud.http.CensusHttpModule$CensusHttpRequestInitializer.initialize(CensusHttpModule.java:109)
    at com.google.cloud.datastore.spi.v1.HttpDatastoreRpc$1.initialize(HttpDatastoreRpc.java:91)
    at com.google.datastore.v1.client.RemoteRpc.call(RemoteRpc.java:95)
    ... 76 more
    

Update 2:

I've since paid for Google support and they have now confirmed it "is linked to a known issue related to the GAE standard Service Agent and access token which is affecting some users"

0 Answers
Related