File tree 2 files changed +27
-0
lines changed
main/java/net/snowflake/client/jdbc
test/java/net/snowflake/client/jdbc
2 files changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -94,12 +94,20 @@ public Connection getConnection() throws SQLException {
94
94
public Connection getConnection (String username , String password ) throws SQLException {
95
95
if (!AUTHENTICATOR_OAUTH .equalsIgnoreCase (
96
96
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
+ }
97
101
properties .put (SFSessionProperty .USER .getPropertyKey (), username );
98
102
}
99
103
100
104
// The driver needs password for OAUTH as part of SNOW-533673 feature request.
101
105
if (!AUTHENTICATOR_SNOWFLAKE_JWT .equalsIgnoreCase (authenticator )
102
106
&& !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
+ }
103
111
properties .put (SFSessionProperty .PASSWORD .getPropertyKey (), password );
104
112
}
105
113
Original file line number Diff line number Diff line change 6
6
import static org .hamcrest .CoreMatchers .is ;
7
7
import static org .hamcrest .MatcherAssert .assertThat ;
8
8
import static org .junit .jupiter .api .Assertions .assertEquals ;
9
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
9
10
10
11
import java .sql .SQLException ;
11
12
import java .util .Properties ;
@@ -112,4 +113,22 @@ public void testDataSourceSetters() {
112
113
assertEquals ("pwd" , props .get (SFSessionProperty .PRIVATE_KEY_PWD .getPropertyKey ()));
113
114
assertEquals ("SNOWFLAKE_JWT" , props .get (SFSessionProperty .AUTHENTICATOR .getPropertyKey ()));
114
115
}
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
+ }
115
134
}
You can’t perform that action at this time.
0 commit comments