Skip to content

Commit 6293e19

Browse files
committed
feat: allow cached ADC to be refreshed
Fixes #2541 This introduces the new method `public static GoogleCredential getApplicationDefault(boolean resetCachedCredentials)` which will reset the cached credentials when the argument is `true`.
1 parent 57882ad commit 6293e19

File tree

3 files changed

+78
-5
lines changed

3 files changed

+78
-5
lines changed

google-api-client/src/main/java/com/google/api/client/googleapis/auth/oauth2/DefaultCredentialProvider.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ private static enum Environment {
8282
* authorize the whole application. This is the built-in service account if running on Google
8383
* Compute Engine or the credentials file from the path in the environment variable
8484
* GOOGLE_APPLICATION_CREDENTIALS.
85+
* If the credentials have been cached, the cached credential will be returned.
8586
*
8687
* @param transport the transport for Http calls.
8788
* @param jsonFactory the factory for Json parsing and formatting.
@@ -90,8 +91,28 @@ private static enum Environment {
9091
*/
9192
final GoogleCredential getDefaultCredential(HttpTransport transport, JsonFactory jsonFactory)
9293
throws IOException {
94+
return getDefaultCredential(transport, jsonFactory, false);
95+
}
96+
97+
/**
98+
* {@link Beta} <br>
99+
* Returns the Application Default Credentials.
100+
*
101+
* <p>Returns the Application Default Credentials which are credentials that identify and
102+
* authorize the whole application. This is the built-in service account if running on Google
103+
* Compute Engine or the credentials file from the path in the environment variable
104+
* GOOGLE_APPLICATION_CREDENTIALS.
105+
*
106+
* @param transport the transport for Http calls.
107+
* @param jsonFactory the factory for Json parsing and formatting.
108+
* @param resetCachedCredentials if true, the cached credential will be reset.
109+
* @return the credential instance.
110+
* @throws IOException if the credential cannot be created in the current environment.
111+
*/
112+
final GoogleCredential getDefaultCredential(HttpTransport transport, JsonFactory jsonFactory, boolean resetCachedCredentials)
113+
throws IOException {
93114
synchronized (this) {
94-
if (cachedCredential == null) {
115+
if (cachedCredential == null || resetCachedCredentials) {
95116
cachedCredential = getDefaultCredentialUnsynchronized(transport, jsonFactory);
96117
}
97118
if (cachedCredential != null) {

google-api-client/src/main/java/com/google/api/client/googleapis/auth/oauth2/GoogleCredential.java

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public class GoogleCredential extends Credential {
153153
static final String SERVICE_ACCOUNT_FILE_TYPE = "service_account";
154154

155155
@Beta
156-
private static DefaultCredentialProvider defaultCredentialProvider =
156+
private static final DefaultCredentialProvider defaultCredentialProvider =
157157
new DefaultCredentialProvider();
158158

159159
/**
@@ -170,7 +170,7 @@ public class GoogleCredential extends Credential {
170170
*/
171171
@Beta
172172
public static GoogleCredential getApplicationDefault() throws IOException {
173-
return getApplicationDefault(Utils.getDefaultTransport(), Utils.getDefaultJsonFactory());
173+
return getApplicationDefault(Utils.getDefaultTransport(), Utils.getDefaultJsonFactory(), false);
174174
}
175175

176176
/**
@@ -181,18 +181,56 @@ public static GoogleCredential getApplicationDefault() throws IOException {
181181
* authorize the whole application. This is the built-in service account if running on Google
182182
* Compute Engine or the credentials file from the path in the environment variable
183183
* GOOGLE_APPLICATION_CREDENTIALS.
184+
* @param resetCachedCredentials whether to reset the cached credentials
185+
* @return the credential instance.
186+
* @throws IOException if the credential cannot be created in the current environment.
187+
*/
188+
@Beta
189+
public static GoogleCredential getApplicationDefault(boolean resetCachedCredentials) throws IOException {
190+
return getApplicationDefault(Utils.getDefaultTransport(), Utils.getDefaultJsonFactory(), resetCachedCredentials);
191+
}
192+
193+
/**
194+
* {@link Beta} <br>
195+
* Returns the Application Default Credentials.
184196
*
197+
* <p>Returns the Application Default Credentials which are credentials that identify and
198+
* authorize the whole application. This is the built-in service account if running on Google
199+
* Compute Engine or the credentials file from the path in the environment variable
200+
* GOOGLE_APPLICATION_CREDENTIALS.
201+
*
202+
* @param resetCachedCredentials whether to reset the cached credentials.
185203
* @param transport the transport for Http calls.
186204
* @param jsonFactory the factory for Json parsing and formatting.
187205
* @return the credential instance.
188206
* @throws IOException if the credential cannot be created in the current environment.
189207
*/
190208
@Beta
191209
public static GoogleCredential getApplicationDefault(
192-
HttpTransport transport, JsonFactory jsonFactory) throws IOException {
210+
HttpTransport transport, JsonFactory jsonFactory, boolean resetCachedCredentials) throws IOException {
193211
Preconditions.checkNotNull(transport);
194212
Preconditions.checkNotNull(jsonFactory);
195-
return defaultCredentialProvider.getDefaultCredential(transport, jsonFactory);
213+
return defaultCredentialProvider.getDefaultCredential(transport, jsonFactory, resetCachedCredentials);
214+
}
215+
216+
/**
217+
* {@link Beta} <br>
218+
* Returns the Application Default Credentials.
219+
*
220+
* <p>Returns the Application Default Credentials which are credentials that identify and
221+
* authorize the whole application. This is the built-in service account if running on Google
222+
* Compute Engine or the credentials file from the path in the environment variable
223+
* GOOGLE_APPLICATION_CREDENTIALS.
224+
*
225+
* @param transport the transport for Http calls.
226+
* @param jsonFactory the factory for Json parsing and formatting.
227+
* @return the credential instance.
228+
* @throws IOException if the credential cannot be created in the current environment.
229+
*/
230+
@Beta
231+
public static GoogleCredential getApplicationDefault(
232+
HttpTransport transport, JsonFactory jsonFactory) throws IOException {
233+
return getApplicationDefault(transport, jsonFactory, false);
196234
}
197235

198236
/**

google-api-client/src/test/java/com/google/api/client/googleapis/auth/oauth2/DefaultCredentialProviderTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,20 @@ public void testDefaultCredentialAppEngineDeployed() throws IOException {
8787
assertSame(JSON_FACTORY, defaultCredential.getJsonFactory());
8888
}
8989

90+
91+
public void testGetApplicationDefaultResetCacheTrueReturnsNewCredentials() throws IOException {
92+
TestDefaultCredentialProvider testProvider = new TestDefaultCredentialProvider();
93+
HttpTransport transport = new MockHttpTransport();
94+
testProvider.addType(
95+
DefaultCredentialProvider.APP_ENGINE_CREDENTIAL_CLASS, MockAppEngineCredential.class);
96+
testProvider.addType(GAE_SIGNAL_CLASS, MockAppEngineSystemProperty.class);
97+
Credential credential1 = testProvider.getDefaultCredential(transport, JSON_FACTORY, false);
98+
Credential credential2 = testProvider.getDefaultCredential(transport, JSON_FACTORY, false);
99+
Credential credential3 = testProvider.getDefaultCredential(transport, JSON_FACTORY, true);
100+
assertSame(credential1, credential2);
101+
assertNotSame(credential2, credential3);
102+
}
103+
90104
public void testDefaultCredentialAppEngineComponentOffAppEngineGivesNotFoundError() {
91105
HttpTransport transport = new MockHttpTransport();
92106
TestDefaultCredentialProvider testProvider = new TestDefaultCredentialProvider();

0 commit comments

Comments
 (0)