Skip to content

Commit 4598c3f

Browse files
lbergelsonchingor13
authored andcommitted
Option to suppress end user credentials warning. (#207)
* Option to suppress end user credentials warning. Defining a new environment variable SUPPRESS_GCLOUD_CREDS_WARNING. Setting this to true suppresses the end user credentials warning. Fixes #193 * reordering checks to respond to comment
1 parent 108df92 commit 4598c3f

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

oauth2_http/java/com/google/auth/oauth2/DefaultCredentialsProvider.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class DefaultCredentialsProvider {
8787
+ "SDK, you might receive a \"quota exceeded\" or \"API not enabled\" error. For "
8888
+ "more information about service accounts, see "
8989
+ "https://cloud.google.com/docs/authentication/.";
90+
public static final String SUPPRESS_GCLOUD_CREDS_WARNING_ENV_VAR = "SUPPRESS_GCLOUD_CREDS_WARNING";
9091

9192
// These variables should only be accessed inside a synchronized block
9293
private GoogleCredentials cachedCredentials = null;
@@ -211,8 +212,9 @@ private final GoogleCredentials getDefaultCredentialsUnsynchronized(
211212
}
212213

213214
private void warnAboutProblematicCredentials(GoogleCredentials credentials) {
214-
if (credentials instanceof UserCredentials &&
215-
((UserCredentials)credentials).getClientId().equals(CLOUDSDK_CLIENT_ID)) {
215+
if (credentials instanceof UserCredentials
216+
&& ((UserCredentials) credentials).getClientId().equals(CLOUDSDK_CLIENT_ID)
217+
&& !Boolean.parseBoolean(getEnv(SUPPRESS_GCLOUD_CREDS_WARNING_ENV_VAR))) {
216218
LOGGER.log(Level.WARNING, CLOUDSDK_CREDENTIALS_WARNING);
217219
}
218220
}

oauth2_http/javatests/com/google/auth/oauth2/DefaultCredentialsProviderTest.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import static org.junit.Assert.assertEquals;
3535
import static org.junit.Assert.assertFalse;
3636
import static org.junit.Assert.assertNotNull;
37+
import static org.junit.Assert.assertNull;
3738
import static org.junit.Assert.assertSame;
3839
import static org.junit.Assert.assertTrue;
3940
import static org.junit.Assert.fail;
@@ -465,6 +466,19 @@ public void flush() {}
465466

466467
@Test
467468
public void getDefaultCredentials_wellKnownFile_logsGcloudWarning() throws IOException {
469+
LogRecord message = getCredentialsAndReturnLogMessage(false);
470+
assertNotNull(message);
471+
assertEquals(Level.WARNING, message.getLevel());
472+
assertTrue(message.getMessage().contains("end user credentials from Google Cloud SDK"));
473+
}
474+
475+
@Test
476+
public void getDefaultCredentials_wellKnownFile_suppressGcloudWarning() throws IOException {
477+
LogRecord message = getCredentialsAndReturnLogMessage(true);
478+
assertNull(message);
479+
}
480+
481+
private LogRecord getCredentialsAndReturnLogMessage(boolean suppressWarning) throws IOException {
468482
Logger logger = Logger.getLogger(DefaultCredentialsProvider.class.getName());
469483
LogHandler handler = new LogHandler();
470484
logger.addHandler(handler);
@@ -477,16 +491,13 @@ public void getDefaultCredentials_wellKnownFile_logsGcloudWarning() throws IOExc
477491
File wellKnownFile =
478492
new File(cloudConfigDir, DefaultCredentialsProvider.WELL_KNOWN_CREDENTIALS_FILE);
479493
TestDefaultCredentialsProvider testProvider = new TestDefaultCredentialsProvider();
494+
testProvider.setEnv(DefaultCredentialsProvider.SUPPRESS_GCLOUD_CREDS_WARNING_ENV_VAR, Boolean.toString(suppressWarning));
480495
testProvider.setProperty("os.name", "linux");
481496
testProvider.setProperty("user.home", homeDir.getAbsolutePath());
482497
testProvider.addFile(wellKnownFile.getAbsolutePath(), userStream);
483-
484498
testUserProvidesToken(
485499
testProvider, GCLOUDSDK_CLIENT_ID, USER_CLIENT_SECRET, REFRESH_TOKEN);
486-
LogRecord message = handler.getRecord();
487-
assertNotNull(message);
488-
assertEquals(Level.WARNING, message.getLevel());
489-
assertTrue(message.getMessage().contains("end user credentials from Google Cloud SDK"));
500+
return handler.getRecord();
490501
}
491502

492503
private static File getTempDirectory() {

0 commit comments

Comments
 (0)