FetchAuthTokenCache returning expired tokens
I am the owner of the Cloud Spanner PHP client library investigating the following customer issue:
The customer uses PHP Spanner client lib and they deploy their application on GKE.
- Workload Identity keeps a token for 59min in the metadata server and retrieves a new one every 60min.
- PHP Spanner client lib keeps a token for 1500sec (25min). When the token expires, client lib retrieves a new access token from the metadata server.
- The problem is that PHP Spanner client lib keeps a token for 1500sec even when GKE metadata server refreshes new token and previous token expires.
- Finally Spanner client lib uses the expired token and error for a while.
I believe this issue is caused by the tokens remaining in the cache after the they expire on the backend because the cache expiry uses the cache config lifetime. The cache expiry should use the token expiry as hinted at in the following fetchAuthToken TODO:
// TODO: correct caching; update the call to setCachedValue to set the expiry
// to the value returned with the auth token.
Implementing the TODO should resolve this issue.
We've seen this as well in production, we now have a sidecar application updating the tokens
We do check expiry here, but only if expires_at was present when the token was cached. The metadata tokens have an "expires_in" field, so if the cache can use this as well, that should fix the issue:
// in FetchAuthTokenCache.php
if (empty($auth_token['expires_at']) && isset($auth_token['expires_in'])) {
$auth_token['expires_at'] = time() + $auth_token['expires_in'];
}
Alternatively this could be done in GCECredentials:
$json['expires_at'] = time() + $json['expires_in'];
// store this so we can retrieve it later
$this->lastReceivedToken = $json;
return $json;
@bshaffer Is there any way I can help with this?
Would adding the GCECredentials changes you specified and some tests be a sufficient fix?
@bshaffer is it possible for @larkee or someone else from the Spanner team to help implement a fix? This issue is currently affecting a customer.
@skuruppu @larkee please test with the fix we've merged to master in #314. Pull down the latest version (v1.14.3)[https://github.com/googleapis/google-auth-library-php/releases/tag/v1.14.3] to test this!
Running composer update should suffice.
Thanks a lot for the fix @bshaffer. I've forwarded your instructions so that the customer can try the fix.
@skuruppu I will keep this bug open until the customer has confirmed the fix. Please close it when this happens, or comment back here with any updates. Thank you!
Thanks @bshaffer. I will follow-up to make sure the issue is fixed on their side and comment back here.
@skuruppu change of plan, I have been told the proper process is to close this issue and if the customer still experiences the problem we can reopen it or open a new issue.