Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to suppress end user credentials warning. #207

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 (!Boolean.parseBoolean(getEnv(SUPPRESS_GCLOUD_CREDS_WARNING_ENV_VAR))
chingor13 marked this conversation as resolved.
Show resolved Hide resolved
&& credentials instanceof UserCredentials
&& ((UserCredentials) credentials).getClientId().equals(CLOUDSDK_CLIENT_ID)) {
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