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

SNOW-1903631 Add more explicit error message when username or password is missing id DataSource #2056

Merged
merged 2 commits into from
Feb 7, 2025
Merged
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 @@ -94,12 +94,20 @@ public Connection getConnection() throws SQLException {
public Connection getConnection(String username, String password) throws SQLException {
if (!AUTHENTICATOR_OAUTH.equalsIgnoreCase(
authenticator)) { // For OAuth, no username is required
if (username == null) {
sfc-gh-dprzybysz marked this conversation as resolved.
Show resolved Hide resolved
throw new SnowflakeSQLException(
sfc-gh-dprzybysz marked this conversation as resolved.
Show resolved Hide resolved
"Cannot create connection because username is missing in DataSource properties.");
}
properties.put(SFSessionProperty.USER.getPropertyKey(), username);
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.sql.SQLException;
import java.util.Properties;
Expand Down Expand Up @@ -112,4 +113,22 @@ public void testDataSourceSetters() {
assertEquals("pwd", props.get(SFSessionProperty.PRIVATE_KEY_PWD.getPropertyKey()));
assertEquals("SNOWFLAKE_JWT", props.get(SFSessionProperty.AUTHENTICATOR.getPropertyKey()));
}

@Test
public void testDataSourceWithoutUsernameOrPasswordThrowsExplicitException() {
SnowflakeBasicDataSource ds = new SnowflakeBasicDataSource();

ds.setAccount("testaccount");
ds.setAuthenticator("snowflake");
assertThrows(
SnowflakeSQLException.class,
ds::getConnection,
"Cannot create connection because username is missing in DataSource properties.");

ds.setUser("testuser");
assertThrows(
SnowflakeSQLException.class,
ds::getConnection,
"Cannot create connection because password is missing in DataSource properties.");
}
}
Loading