Change how access tokens are refreshed

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=212880971
This commit is contained in:
jianglai 2018-09-13 14:38:14 -07:00 committed by Ben McIlwain
parent 414b2e4db1
commit 5e2831b562
3 changed files with 23 additions and 21 deletions

View file

@ -37,7 +37,7 @@ public class ProxyConfig {
public String projectId; public String projectId;
public List<String> gcpScopes; public List<String> gcpScopes;
public int accessTokenValidPeriodSeconds; public int accessTokenRefreshBeforeExpirationSeconds;
public int serverCertificateCacheSeconds; public int serverCertificateCacheSeconds;
public Gcs gcs; public Gcs gcs;
public Kms kms; public Kms kms;

View file

@ -15,9 +15,7 @@
package google.registry.proxy; package google.registry.proxy;
import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Suppliers.memoizeWithExpiration;
import static google.registry.proxy.ProxyConfig.getProxyConfig; import static google.registry.proxy.ProxyConfig.getProxyConfig;
import static java.util.concurrent.TimeUnit.SECONDS;
import com.beust.jcommander.JCommander; import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter; import com.beust.jcommander.Parameter;
@ -229,17 +227,19 @@ public class ProxyModule {
@Named("accessToken") @Named("accessToken")
static Supplier<String> provideAccessTokenSupplier( static Supplier<String> provideAccessTokenSupplier(
GoogleCredential credential, ProxyConfig config) { GoogleCredential credential, ProxyConfig config) {
return memoizeWithExpiration( return () -> {
() -> { // If we never obtained an access token, the expiration time is null.
try { if (credential.getExpiresInSeconds() == null
credential.refreshToken(); // If we have an access token, make sure to refresh it ahead of time.
} catch (IOException e) { || credential.getExpiresInSeconds() < config.accessTokenRefreshBeforeExpirationSeconds) {
throw new RuntimeException("Cannot refresh access token.", e); try {
} credential.refreshToken();
return credential.getAccessToken(); } catch (IOException e) {
}, throw new RuntimeException("Cannot refresh access token.", e);
config.accessTokenValidPeriodSeconds, }
SECONDS); }
return credential.getAccessToken();
};
} }
@Singleton @Singleton

View file

@ -20,7 +20,7 @@ gcpScopes:
# to authenticate. # to authenticate.
- https://www.googleapis.com/auth/userinfo.email - https://www.googleapis.com/auth/userinfo.email
# Access token is cached for 15 minutes. # Refresh the access token 5 minutes before it expires.
# #
# Depending on how the credential is obtained, its renewal behavior is # Depending on how the credential is obtained, its renewal behavior is
# different. A credential backed by a private key (like the ADC obtained # different. A credential backed by a private key (like the ADC obtained
@ -30,12 +30,14 @@ gcpScopes:
# this, I got this number by logging in a GCE VM, calling curl on the metatdata # this, I got this number by logging in a GCE VM, calling curl on the metatdata
# server every minute, and check the expiration time of the response). Calling # server every minute, and check the expiration time of the response). Calling
# refreshToken() does *not* get a new token. The token is only refreshed by # refreshToken() does *not* get a new token. The token is only refreshed by
# metadata server itself (every 3599 - 1699 = 1900 seconds). We cache the token # metadata server itself (every 3599 - 1699 = 1900 seconds).
# for 900 seconds, which should be good for both cases. The private key #
# generated token is in theory valid for 1h, and the token obtained from the # We refresh the token 5 minutes before it expires, which should work in both
# metadata token is at least valid for 1699 seconds, so we can know for sure # cases. This is better than caching the token for a pre-defined period, because
# that during the period that it is cached, the token will not expire. # even right after #refreshToken() is called on the client side, tokens obtained
accessTokenValidPeriodSeconds: 900 # from GCE metadata server may not be valid for the entirety of 3599 seconds.
accessTokenRefreshBeforeExpirationSeconds: 300
# Server certificate is cached for 30 minutes. # Server certificate is cached for 30 minutes.
# #