Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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 @@ -32,6 +32,8 @@
package com.google.auth.oauth2;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.UncheckedIOException;

/**
* This public class provides shared utilities for common OAuth2 utils or ADC. It also exposes
Expand Down Expand Up @@ -70,7 +72,14 @@ static final File getWellKnownCredentialsFile(DefaultCredentialsProvider provide
if (envPath != null) {
cloudConfigPath = new File(envPath);
} else if (provider.getOsName().indexOf("windows") >= 0) {
File appDataPath = new File(provider.getEnv("APPDATA"));
String appDataEnv = provider.getEnv("APPDATA");
if (appDataEnv == null || appDataEnv.trim().isEmpty()) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Guava has Strings.isNullOrEmpty for a convenient API

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this check would reject if the env var was set to a string like " ", which is probably more robust than just checking for Null or Empty. If we decide to keep it this way, we might want to update it in here too, so that our checks for APPDATA are consistent.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From a quick search, I don't think " " is a valid in Windows and probably not realistic (IIUC APPDATA is used throughout Windows and should be valid). Since the other location already uses Guava, let's just make it consistent and use Guava as well (both impls resolve the main NPE possibility and are fine with me).

throw new UncheckedIOException(
new FileNotFoundException(
"APPDATA environment variable is not set or empty; cannot locate the well-known"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you update the message to be something like ... is not set or is empty for clarity?

+ " credentials file on Windows."));
}
File appDataPath = new File(appDataEnv);
cloudConfigPath = new File(appDataPath, provider.CLOUDSDK_CONFIG_DIRECTORY);
} else {
File configPath = new File(provider.getProperty("user.home", ""), ".config");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,64 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.UncheckedIOException;
import org.junit.jupiter.api.Test;

class GoogleAuthUtilsTest {

@Test
void getWellKnownCredentialsFile_windows_nullAppData_throwsUncheckedIOException() {
DefaultCredentialsProviderTest.TestDefaultCredentialsProvider provider =
new DefaultCredentialsProviderTest.TestDefaultCredentialsProvider();
provider.setProperty("os.name", "windows");
// APPDATA is intentionally not set, so getEnv("APPDATA") returns null

UncheckedIOException thrown =
assertThrows(
UncheckedIOException.class,
() -> GoogleAuthUtils.getWellKnownCredentialsFile(provider));
assertTrue(thrown.getCause() instanceof FileNotFoundException);
assertTrue(thrown.getCause().getMessage().contains("APPDATA"));
assertTrue(thrown.getCause().getMessage().contains("not set or empty"));
}

@Test
void getWellKnownCredentialsFile_windows_emptyAppData_throwsUncheckedIOException() {
DefaultCredentialsProviderTest.TestDefaultCredentialsProvider provider =
new DefaultCredentialsProviderTest.TestDefaultCredentialsProvider();
provider.setProperty("os.name", "windows");
provider.setEnv("APPDATA", "");

UncheckedIOException thrown =
assertThrows(
UncheckedIOException.class,
() -> GoogleAuthUtils.getWellKnownCredentialsFile(provider));
assertTrue(thrown.getCause() instanceof FileNotFoundException);
assertTrue(thrown.getCause().getMessage().contains("APPDATA"));
assertTrue(thrown.getCause().getMessage().contains("not set or empty"));
}

@Test
void getWellKnownCredentialsFile_windows_blankAppData_throwsUncheckedIOException() {
DefaultCredentialsProviderTest.TestDefaultCredentialsProvider provider =
new DefaultCredentialsProviderTest.TestDefaultCredentialsProvider();
provider.setProperty("os.name", "windows");
provider.setEnv("APPDATA", " ");

UncheckedIOException thrown =
assertThrows(
UncheckedIOException.class,
() -> GoogleAuthUtils.getWellKnownCredentialsFile(provider));
assertTrue(thrown.getCause() instanceof FileNotFoundException);
assertTrue(thrown.getCause().getMessage().contains("APPDATA"));
assertTrue(thrown.getCause().getMessage().contains("not set or empty"));
}

@Test
void getWellKnownCredentialsPath_correct() {
DefaultCredentialsProvider provider =
Expand Down
Loading