Skip to content

Commit f8b89f6

Browse files
authored
SNOW-1903631: Add more explicit error message when username or password is missing id DataSource (#2056)
1 parent 873bf87 commit f8b89f6

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/main/java/net/snowflake/client/jdbc/SnowflakeBasicDataSource.java

+8
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,20 @@ public Connection getConnection() throws SQLException {
9494
public Connection getConnection(String username, String password) throws SQLException {
9595
if (!AUTHENTICATOR_OAUTH.equalsIgnoreCase(
9696
authenticator)) { // For OAuth, no username is required
97+
if (username == null) {
98+
throw new SnowflakeSQLException(
99+
"Cannot create connection because username is missing in DataSource properties.");
100+
}
97101
properties.put(SFSessionProperty.USER.getPropertyKey(), username);
98102
}
99103

100104
// The driver needs password for OAUTH as part of SNOW-533673 feature request.
101105
if (!AUTHENTICATOR_SNOWFLAKE_JWT.equalsIgnoreCase(authenticator)
102106
&& !AUTHENTICATOR_EXTERNAL_BROWSER.equalsIgnoreCase(authenticator)) {
107+
if (password == null) {
108+
throw new SnowflakeSQLException(
109+
"Cannot create connection because password is missing in DataSource properties.");
110+
}
103111
properties.put(SFSessionProperty.PASSWORD.getPropertyKey(), password);
104112
}
105113

src/test/java/net/snowflake/client/jdbc/SnowflakeBasicDataSourceTest.java

+19
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import static org.hamcrest.CoreMatchers.is;
77
import static org.hamcrest.MatcherAssert.assertThat;
88
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
import static org.junit.jupiter.api.Assertions.assertThrows;
910

1011
import java.sql.SQLException;
1112
import java.util.Properties;
@@ -112,4 +113,22 @@ public void testDataSourceSetters() {
112113
assertEquals("pwd", props.get(SFSessionProperty.PRIVATE_KEY_PWD.getPropertyKey()));
113114
assertEquals("SNOWFLAKE_JWT", props.get(SFSessionProperty.AUTHENTICATOR.getPropertyKey()));
114115
}
116+
117+
@Test
118+
public void testDataSourceWithoutUsernameOrPasswordThrowsExplicitException() {
119+
SnowflakeBasicDataSource ds = new SnowflakeBasicDataSource();
120+
121+
ds.setAccount("testaccount");
122+
ds.setAuthenticator("snowflake");
123+
assertThrows(
124+
SnowflakeSQLException.class,
125+
ds::getConnection,
126+
"Cannot create connection because username is missing in DataSource properties.");
127+
128+
ds.setUser("testuser");
129+
assertThrows(
130+
SnowflakeSQLException.class,
131+
ds::getConnection,
132+
"Cannot create connection because password is missing in DataSource properties.");
133+
}
115134
}

0 commit comments

Comments
 (0)