Skip to content

Commit

Permalink
Option to suppress end user credentials warning. (#207)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
lbergelson authored and chingor13 committed Nov 13, 2018
1 parent 108df92 commit 4598c3f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class DefaultCredentialsProvider {
+ "SDK, you might receive a \"quota exceeded\" or \"API not enabled\" error. For "
+ "more information about service accounts, see "
+ "https://cloud.google.com/docs/authentication/.";
public static final String SUPPRESS_GCLOUD_CREDS_WARNING_ENV_VAR = "SUPPRESS_GCLOUD_CREDS_WARNING";

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

private void warnAboutProblematicCredentials(GoogleCredentials credentials) {
if (credentials instanceof UserCredentials &&
((UserCredentials)credentials).getClientId().equals(CLOUDSDK_CLIENT_ID)) {
if (credentials instanceof UserCredentials
&& ((UserCredentials) credentials).getClientId().equals(CLOUDSDK_CLIENT_ID)
&& !Boolean.parseBoolean(getEnv(SUPPRESS_GCLOUD_CREDS_WARNING_ENV_VAR))) {
LOGGER.log(Level.WARNING, CLOUDSDK_CREDENTIALS_WARNING);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
Expand Down Expand Up @@ -465,6 +466,19 @@ public void flush() {}

@Test
public void getDefaultCredentials_wellKnownFile_logsGcloudWarning() throws IOException {
LogRecord message = getCredentialsAndReturnLogMessage(false);
assertNotNull(message);
assertEquals(Level.WARNING, message.getLevel());
assertTrue(message.getMessage().contains("end user credentials from Google Cloud SDK"));
}

@Test
public void getDefaultCredentials_wellKnownFile_suppressGcloudWarning() throws IOException {
LogRecord message = getCredentialsAndReturnLogMessage(true);
assertNull(message);
}

private LogRecord getCredentialsAndReturnLogMessage(boolean suppressWarning) throws IOException {
Logger logger = Logger.getLogger(DefaultCredentialsProvider.class.getName());
LogHandler handler = new LogHandler();
logger.addHandler(handler);
Expand All @@ -477,16 +491,13 @@ public void getDefaultCredentials_wellKnownFile_logsGcloudWarning() throws IOExc
File wellKnownFile =
new File(cloudConfigDir, DefaultCredentialsProvider.WELL_KNOWN_CREDENTIALS_FILE);
TestDefaultCredentialsProvider testProvider = new TestDefaultCredentialsProvider();
testProvider.setEnv(DefaultCredentialsProvider.SUPPRESS_GCLOUD_CREDS_WARNING_ENV_VAR, Boolean.toString(suppressWarning));
testProvider.setProperty("os.name", "linux");
testProvider.setProperty("user.home", homeDir.getAbsolutePath());
testProvider.addFile(wellKnownFile.getAbsolutePath(), userStream);

testUserProvidesToken(
testProvider, GCLOUDSDK_CLIENT_ID, USER_CLIENT_SECRET, REFRESH_TOKEN);
LogRecord message = handler.getRecord();
assertNotNull(message);
assertEquals(Level.WARNING, message.getLevel());
assertTrue(message.getMessage().contains("end user credentials from Google Cloud SDK"));
return handler.getRecord();
}

private static File getTempDirectory() {
Expand Down

0 comments on commit 4598c3f

Please sign in to comment.