Skip to content

Commit

Permalink
SNOW-968787: Fix NullPointerException when property key "gzipDisabled…
Browse files Browse the repository at this point in the history
…" is not specified (#1561)

fix NullPointerException in convertProxyPropertiesToHttpClientKey
  • Loading branch information
sfc-gh-ext-simba-jl committed Nov 16, 2023
1 parent 4625079 commit 7af9144
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/main/java/net/snowflake/client/jdbc/SnowflakeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -610,16 +610,15 @@ public static HttpClientSettingsKey convertProxyPropertiesToHttpClientKey(
String proxyPassword = info.getProperty(SFSessionProperty.PROXY_PASSWORD.getPropertyKey());
String nonProxyHosts = info.getProperty(SFSessionProperty.NON_PROXY_HOSTS.getPropertyKey());
String proxyProtocol = info.getProperty(SFSessionProperty.PROXY_PROTOCOL.getPropertyKey());
String userAgentSuffix =
info.getProperty(SFSessionProperty.USER_AGENT_SUFFIX.getPropertyKey());
Boolean gzipDisabled =
(info.getProperty(SFSessionProperty.GZIP_DISABLED.getPropertyKey()).isEmpty()
Strings.isNullOrEmpty(
info.getProperty(SFSessionProperty.GZIP_DISABLED.getPropertyKey()))
? false
: Boolean.valueOf(
info.getProperty(SFSessionProperty.GZIP_DISABLED.getPropertyKey())));
// Check for any user agent suffix
String userAgentSuffix = "";
if (info.containsKey(SFSessionProperty.USER_AGENT_SUFFIX)) {
userAgentSuffix = (String) info.get(SFSessionProperty.USER_AGENT_SUFFIX);
}
info.getProperty(SFSessionProperty.GZIP_DISABLED.getPropertyKey()));

// create key for proxy properties
return new HttpClientSettingsKey(
mode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import net.snowflake.client.RunningOnGithubAction;
import net.snowflake.client.jdbc.ErrorCode;
import net.snowflake.client.jdbc.SnowflakeSQLException;
import net.snowflake.client.jdbc.SnowflakeUtil;
import org.junit.Test;

public class CoreUtilsMiscellaneousTest {
Expand Down Expand Up @@ -274,6 +275,72 @@ public void testSizeOfHttpClientMapWithGzipAndUserAgentSuffix() {
assertEquals(3, HttpUtil.httpClient.size());
}

@Test
public void testConvertProxyPropertiesToHttpClientKey() throws SnowflakeSQLException {
OCSPMode mode = OCSPMode.FAIL_OPEN;
Properties props = new Properties();
HttpClientSettingsKey expectedNoProxy = new HttpClientSettingsKey(mode);

// Test for null proxy properties
HttpClientSettingsKey settingsKey =
SnowflakeUtil.convertProxyPropertiesToHttpClientKey(mode, props);
assertTrue(expectedNoProxy.equals(settingsKey));

// Set useProxy to false so proxy properties will not be set
props.put("useProxy", "false");
props.put("proxyHost", "localhost");
props.put("proxyPort", "8084");
settingsKey = SnowflakeUtil.convertProxyPropertiesToHttpClientKey(mode, props);
assertTrue(expectedNoProxy.equals(settingsKey));

// Test without gzip_disabled
props.put("useProxy", "true");
props.put("proxyHost", "localhost");
props.put("proxyPort", "8084");
props.put("proxyUser", "testuser");
props.put("proxyPassword", "pw");
props.put("nonProxyHosts", "baz.com | foo.com");
props.put("proxyProtocol", "http");
props.put("user_agent_suffix", "jdbc");
settingsKey = SnowflakeUtil.convertProxyPropertiesToHttpClientKey(mode, props);
HttpClientSettingsKey expectedWithProxy =
new HttpClientSettingsKey(
OCSPMode.FAIL_OPEN,
"localhost",
8084,
"baz.com | foo.com",
"testuser",
"pw",
"http",
"jdbc",
Boolean.valueOf(false));
assertTrue(expectedWithProxy.equals(settingsKey));

// Test with gzip_disabled
props.put("gzipDisabled", "true");
settingsKey = SnowflakeUtil.convertProxyPropertiesToHttpClientKey(mode, props);
expectedWithProxy =
new HttpClientSettingsKey(
OCSPMode.FAIL_OPEN,
"localhost",
8084,
"baz.com | foo.com",
"testuser",
"pw",
"http",
"jdbc",
Boolean.valueOf(true));
assertTrue(expectedWithProxy.equals(settingsKey));

// Test that exception is thrown when port number is invalid
props.put("proxyPort", "invalidnumber");
try {
settingsKey = SnowflakeUtil.convertProxyPropertiesToHttpClientKey(mode, props);
} catch (SnowflakeSQLException e) {
assertEquals((int) ErrorCode.INVALID_PROXY_PROPERTIES.getMessageCode(), e.getErrorCode());
}
}

@Test
public void testNullAndEmptyProxySettingsForS3() {
HttpClientSettingsKey testKey =
Expand Down

0 comments on commit 7af9144

Please sign in to comment.