Skip to content

Commit 92aaca1

Browse files
committed
Exception fixes
1 parent b3c7303 commit 92aaca1

File tree

2 files changed

+43
-27
lines changed

2 files changed

+43
-27
lines changed

databricks-sdk-java/src/main/java/com/databricks/sdk/core/oauth/EnvVarIDTokenSource.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
/** Implementation of {@link IDTokenSource} that reads the ID token from an environment variable. */
88
public class EnvVarIDTokenSource implements IDTokenSource {
9+
private static final String ERROR_ENV_VAR_NULL_OR_EMPTY =
10+
"Environment variable name cannot be null or empty";
11+
private static final String ERROR_EMPTY_TOKEN =
12+
"Received empty ID token from environment variable %s";
13+
914
/* The name of the environment variable to read the ID token from. */
1015
private final String envVarName;
1116
/* The environment to read variables from. */
@@ -30,23 +35,18 @@ public EnvVarIDTokenSource(String envVarName, Environment env) {
3035
* @throws IllegalArgumentException if the environment variable name is null or empty, or the
3136
* environment is null.
3237
* @throws DatabricksException if the environment variable is not set or is empty.
33-
* @throws ClassCastException if the environment variable is not valid type.
3438
*/
3539
@Override
3640
public IDToken getIDToken(String audience) {
3741
if (Strings.isNullOrEmpty(envVarName)) {
38-
throw new IllegalArgumentException("Environment variable name cannot be null or empty");
42+
throw new IllegalArgumentException(ERROR_ENV_VAR_NULL_OR_EMPTY);
3943
}
4044

4145
try {
4246
String token = env.get(envVarName);
4347
return new IDToken(token);
4448
} catch (IllegalArgumentException e) {
45-
throw new DatabricksException(
46-
"Received empty ID token from environment variable " + envVarName);
47-
} catch (ClassCastException e) {
48-
throw new DatabricksException(
49-
"Environment variable " + envVarName + " has invalid type: " + e.getMessage(), e);
49+
throw new DatabricksException(String.format(ERROR_EMPTY_TOKEN, envVarName), e);
5050
}
5151
}
5252
}

databricks-sdk-java/src/main/java/com/databricks/sdk/core/oauth/FileIDTokenSource.java

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,28 @@
99
import java.nio.file.Path;
1010
import java.nio.file.Paths;
1111
import java.util.List;
12+
import java.util.stream.Collectors;
1213

1314
/**
1415
* Implementation of {@link IDTokenSource} that reads the ID token from a file. The token is read
15-
* using UTF-8 encoding and any leading/trailing whitespace is trimmed.
16+
* using UTF-8 encoding and any leading/trailing whitespace is trimmed. The file should contain
17+
* exactly one non-empty line with the token value. Files with multiple non-empty lines or only
18+
* empty lines will result in an error.
1619
*
1720
* @see IDTokenSource
1821
*/
1922
public class FileIDTokenSource implements IDTokenSource {
23+
private static final String ERROR_FILE_NOT_FOUND = "File %s does not exist";
24+
private static final String ERROR_SECURITY_CHECK =
25+
"Security permission denied when checking if file %s exists: %s";
26+
private static final String ERROR_READ_FAILED = "Failed to read ID token from file %s: %s";
27+
private static final String ERROR_SECURITY_READ =
28+
"Security permission denied when reading file %s: %s";
29+
private static final String ERROR_EMPTY_FILE = "File %s contains only empty lines";
30+
private static final String ERROR_MULTIPLE_LINES =
31+
"The token should be a single line but the file %s contains %d non-empty lines";
32+
private static final String ERROR_EMPTY_TOKEN = "Received empty ID token from file %s";
33+
2034
/* The path to the file containing the ID token. */
2135
private final String filePath;
2236

@@ -64,43 +78,45 @@ public IDToken getIDToken(String audience) {
6478

6579
try {
6680
if (!Files.exists(path)) {
67-
throw new DatabricksException("File " + filePath + " does not exist");
81+
throw new DatabricksException(String.format(ERROR_FILE_NOT_FOUND, filePath));
6882
}
6983
} catch (SecurityException e) {
7084
throw new DatabricksException(
71-
"Security permission denied when checking if file "
72-
+ filePath
73-
+ " exists: "
74-
+ e.getMessage(),
75-
e);
85+
String.format(ERROR_SECURITY_CHECK, filePath, e.getMessage()), e);
7686
}
7787

78-
List<String> lines;
88+
List<String> rawLines;
7989
try {
80-
lines = Files.readAllLines(path, StandardCharsets.UTF_8);
90+
rawLines = Files.readAllLines(path, StandardCharsets.UTF_8);
8191
} catch (IOException e) {
82-
throw new DatabricksException(
83-
"Failed to read ID token from file " + filePath + ": " + e.getMessage(), e);
92+
throw new DatabricksException(String.format(ERROR_READ_FAILED, filePath, e.getMessage()), e);
8493
} catch (SecurityException e) {
8594
throw new DatabricksException(
86-
"Security permission denied when reading file " + filePath + ": " + e.getMessage(), e);
95+
String.format(ERROR_SECURITY_READ, filePath, e.getMessage()), e);
8796
}
8897

89-
if (lines.isEmpty()) {
90-
throw new DatabricksException("File " + filePath + " is empty");
98+
// Filter out empty lines
99+
List<String> nonEmptyLines =
100+
rawLines.stream()
101+
.map(String::trim)
102+
.filter(line -> !line.isEmpty())
103+
.collect(Collectors.toList());
104+
105+
if (nonEmptyLines.isEmpty()) {
106+
throw new DatabricksException(String.format(ERROR_EMPTY_FILE, filePath));
91107
}
92108

93-
String token;
94-
try {
95-
token = lines.get(0).trim();
96-
} catch (IndexOutOfBoundsException e) {
97-
throw new DatabricksException("Invalid token format in file " + filePath);
109+
if (nonEmptyLines.size() > 1) {
110+
throw new DatabricksException(
111+
String.format(ERROR_MULTIPLE_LINES, filePath, nonEmptyLines.size()));
98112
}
99113

114+
String token = nonEmptyLines.get(0);
115+
100116
try {
101117
return new IDToken(token);
102118
} catch (IllegalArgumentException e) {
103-
throw new DatabricksException("Received empty ID token from file " + filePath);
119+
throw new DatabricksException(String.format(ERROR_EMPTY_TOKEN, filePath));
104120
}
105121
}
106122
}

0 commit comments

Comments
 (0)