Skip to content

Commit 8826699

Browse files
authored
SNOW-1042432: Remove dependency on com.amazonaws.Protocol from HttpClientSettingsKey (#1627)
1 parent 557659e commit 8826699

File tree

9 files changed

+174
-74
lines changed

9 files changed

+174
-74
lines changed

src/main/java/net/snowflake/client/core/HttpClientSettingsKey.java

+15-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
package net.snowflake.client.core;
66

7-
import com.amazonaws.Protocol;
87
import com.google.common.base.Strings;
98
import java.io.Serializable;
109

@@ -124,16 +123,28 @@ public String getUserAgentSuffix() {
124123
}
125124

126125
/** Be careful of using this! Should only be called when password is later masked. */
127-
String getProxyPassword() {
126+
@SnowflakeJdbcInternalApi
127+
public String getProxyPassword() {
128128
return this.proxyPassword;
129129
}
130130

131131
public String getNonProxyHosts() {
132132
return this.nonProxyHosts;
133133
}
134134

135-
public Protocol getProxyProtocol() {
136-
return this.proxyProtocol.equalsIgnoreCase("https") ? Protocol.HTTPS : Protocol.HTTP;
135+
/**
136+
* @deprecated Use {@link #getProxyHttpProtocol()}
137+
* @return ProxyProtocol
138+
*/
139+
@Deprecated
140+
public com.amazonaws.Protocol getProxyProtocol() {
141+
return this.proxyProtocol.equalsIgnoreCase("https")
142+
? com.amazonaws.Protocol.HTTPS
143+
: com.amazonaws.Protocol.HTTP;
144+
}
145+
146+
public HttpProtocol getProxyHttpProtocol() {
147+
return this.proxyProtocol.equalsIgnoreCase("https") ? HttpProtocol.HTTPS : HttpProtocol.HTTP;
137148
}
138149

139150
public Boolean getGzipDisabled() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright (c) 2024 Snowflake Computing Inc. All rights reserved.
3+
*/
4+
5+
package net.snowflake.client.core;
6+
7+
public enum HttpProtocol {
8+
HTTP("http"),
9+
10+
HTTPS("https");
11+
12+
private final String scheme;
13+
14+
HttpProtocol(String scheme) {
15+
this.scheme = scheme;
16+
}
17+
18+
public String getScheme() {
19+
return scheme;
20+
}
21+
}

src/main/java/net/snowflake/client/core/HttpUtil.java

+9-56
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import static org.apache.http.client.config.CookieSpecs.IGNORE_COOKIES;
1010

1111
import com.amazonaws.ClientConfiguration;
12-
import com.amazonaws.Protocol;
1312
import com.google.common.annotations.VisibleForTesting;
1413
import com.google.common.base.Strings;
1514
import com.microsoft.azure.storage.OperationContext;
@@ -36,6 +35,7 @@
3635
import net.snowflake.client.jdbc.SnowflakeDriver;
3736
import net.snowflake.client.jdbc.SnowflakeSQLException;
3837
import net.snowflake.client.jdbc.SnowflakeUtil;
38+
import net.snowflake.client.jdbc.cloud.storage.S3HttpUtil;
3939
import net.snowflake.client.log.ArgSupplier;
4040
import net.snowflake.client.log.SFLogger;
4141
import net.snowflake.client.log.SFLoggerFactory;
@@ -143,19 +143,11 @@ public static void closeExpiredAndIdleConnections() {
143143
*
144144
* @param key key to HttpClient map containing OCSP and proxy info
145145
* @param clientConfig the configuration needed by S3 to set the proxy
146+
* @deprecated use S3HttpUtil.setProxyForS3(HttpClientSettingsKey, ClientConfiguration) instead
146147
*/
148+
@Deprecated
147149
public static void setProxyForS3(HttpClientSettingsKey key, ClientConfiguration clientConfig) {
148-
if (key != null && key.usesProxy()) {
149-
clientConfig.setProxyProtocol(key.getProxyProtocol());
150-
clientConfig.setProxyHost(key.getProxyHost());
151-
clientConfig.setProxyPort(key.getProxyPort());
152-
clientConfig.setNonProxyHosts(key.getNonProxyHosts());
153-
if (!Strings.isNullOrEmpty(key.getProxyUser())
154-
&& !Strings.isNullOrEmpty(key.getProxyPassword())) {
155-
clientConfig.setProxyUsername(key.getProxyUser());
156-
clientConfig.setProxyPassword(key.getProxyPassword());
157-
}
158-
}
150+
S3HttpUtil.setProxyForS3(key, clientConfig);
159151
}
160152

161153
/**
@@ -165,51 +157,12 @@ public static void setProxyForS3(HttpClientSettingsKey key, ClientConfiguration
165157
* @param proxyProperties proxy properties
166158
* @param clientConfig the configuration needed by S3 to set the proxy
167159
* @throws SnowflakeSQLException
160+
* @deprecated use S3HttpUtil.setSessionlessProxyForS3(Properties, ClientConfiguration) instead
168161
*/
162+
@Deprecated
169163
public static void setSessionlessProxyForS3(
170164
Properties proxyProperties, ClientConfiguration clientConfig) throws SnowflakeSQLException {
171-
// do nothing yet
172-
if (proxyProperties != null
173-
&& proxyProperties.size() > 0
174-
&& proxyProperties.getProperty(SFSessionProperty.USE_PROXY.getPropertyKey()) != null) {
175-
Boolean useProxy =
176-
Boolean.valueOf(
177-
proxyProperties.getProperty(SFSessionProperty.USE_PROXY.getPropertyKey()));
178-
if (useProxy) {
179-
// set up other proxy related values.
180-
String proxyHost =
181-
proxyProperties.getProperty(SFSessionProperty.PROXY_HOST.getPropertyKey());
182-
int proxyPort;
183-
try {
184-
proxyPort =
185-
Integer.parseInt(
186-
proxyProperties.getProperty(SFSessionProperty.PROXY_PORT.getPropertyKey()));
187-
} catch (NumberFormatException | NullPointerException e) {
188-
throw new SnowflakeSQLException(
189-
ErrorCode.INVALID_PROXY_PROPERTIES, "Could not parse port number");
190-
}
191-
String proxyUser =
192-
proxyProperties.getProperty(SFSessionProperty.PROXY_USER.getPropertyKey());
193-
String proxyPassword =
194-
proxyProperties.getProperty(SFSessionProperty.PROXY_PASSWORD.getPropertyKey());
195-
String nonProxyHosts =
196-
proxyProperties.getProperty(SFSessionProperty.NON_PROXY_HOSTS.getPropertyKey());
197-
String proxyProtocol =
198-
proxyProperties.getProperty(SFSessionProperty.PROXY_PROTOCOL.getPropertyKey());
199-
Protocol protocolEnum =
200-
(!Strings.isNullOrEmpty(proxyProtocol) && proxyProtocol.equalsIgnoreCase("https"))
201-
? Protocol.HTTPS
202-
: Protocol.HTTP;
203-
clientConfig.setProxyHost(proxyHost);
204-
clientConfig.setProxyPort(proxyPort);
205-
clientConfig.setNonProxyHosts(nonProxyHosts);
206-
clientConfig.setProxyProtocol(protocolEnum);
207-
if (!Strings.isNullOrEmpty(proxyUser) && !Strings.isNullOrEmpty(proxyPassword)) {
208-
clientConfig.setProxyUsername(proxyUser);
209-
clientConfig.setProxyPassword(proxyPassword);
210-
}
211-
}
212-
}
165+
S3HttpUtil.setSessionlessProxyForS3(proxyProperties, clientConfig);
213166
}
214167

215168
/**
@@ -324,7 +277,7 @@ public static CloseableHttpClient buildHttpClient(
324277
HttpHost proxy =
325278
(key != null && key.usesProxy())
326279
? new HttpHost(
327-
key.getProxyHost(), key.getProxyPort(), key.getProxyProtocol().toString())
280+
key.getProxyHost(), key.getProxyPort(), key.getProxyHttpProtocol().getScheme())
328281
: null;
329282
// If defaultrequestconfig is not initialized or its proxy settings do not match current proxy
330283
// settings, re-build it (current or old proxy settings could be null, so null check is
@@ -408,7 +361,7 @@ public static CloseableHttpClient buildHttpClient(
408361
new SnowflakeMutableProxyRoutePlanner(
409362
key.getProxyHost(),
410363
key.getProxyPort(),
411-
key.getProxyProtocol(),
364+
key.getProxyHttpProtocol(),
412365
key.getNonProxyHosts()));
413366
httpClientBuilder = httpClientBuilder.setProxy(proxy).setRoutePlanner(sdkProxyRoutePlanner);
414367
if (!Strings.isNullOrEmpty(key.getProxyUser())

src/main/java/net/snowflake/client/core/SFSession.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ public synchronized void open() throws SFException, SnowflakeSQLException {
550550
httpClientSettingsKey.getProxyUser(),
551551
!Strings.isNullOrEmpty(httpClientSettingsKey.getProxyPassword()) ? "***" : "(empty)",
552552
httpClientSettingsKey.getNonProxyHosts(),
553-
httpClientSettingsKey.getProxyProtocol());
553+
httpClientSettingsKey.getProxyHttpProtocol());
554554

555555
// TODO: temporarily hardcode sessionParameter debug info. will be changed in the future
556556
SFLoginInput loginInput = new SFLoginInput();

src/main/java/net/snowflake/client/core/SnowflakeMutableProxyRoutePlanner.java

+23-3
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,22 @@ public class SnowflakeMutableProxyRoutePlanner implements HttpRoutePlanner, Seri
2424
private String host;
2525
private int proxyPort;
2626
private String nonProxyHosts;
27-
private Protocol protocol;
27+
private HttpProtocol protocol;
2828

29+
/**
30+
* @deprecated use SnowflakeMutableProxyRoutePlanner(String host, int proxyPort, HttpProtocol
31+
* protocol, String nonProxyHosts)
32+
*/
33+
@Deprecated
2934
public SnowflakeMutableProxyRoutePlanner(
3035
String host, int proxyPort, Protocol proxyProtocol, String nonProxyHosts) {
31-
proxyRoutePlanner = new SdkProxyRoutePlanner(host, proxyPort, proxyProtocol, nonProxyHosts);
36+
this(host, proxyPort, toSnowflakeProtocol(proxyProtocol), nonProxyHosts);
37+
}
38+
39+
public SnowflakeMutableProxyRoutePlanner(
40+
String host, int proxyPort, HttpProtocol proxyProtocol, String nonProxyHosts) {
41+
proxyRoutePlanner =
42+
new SdkProxyRoutePlanner(host, proxyPort, toAwsProtocol(proxyProtocol), nonProxyHosts);
3243
this.host = host;
3344
this.proxyPort = proxyPort;
3445
this.nonProxyHosts = nonProxyHosts;
@@ -37,7 +48,8 @@ public SnowflakeMutableProxyRoutePlanner(
3748

3849
public void setNonProxyHosts(String nonProxyHosts) {
3950
this.nonProxyHosts = nonProxyHosts;
40-
proxyRoutePlanner = new SdkProxyRoutePlanner(host, proxyPort, protocol, nonProxyHosts);
51+
proxyRoutePlanner =
52+
new SdkProxyRoutePlanner(host, proxyPort, toAwsProtocol(protocol), nonProxyHosts);
4153
}
4254

4355
public String getNonProxyHosts() {
@@ -49,4 +61,12 @@ public HttpRoute determineRoute(HttpHost target, HttpRequest request, HttpContex
4961
throws HttpException {
5062
return proxyRoutePlanner.determineRoute(target, request, context);
5163
}
64+
65+
private static Protocol toAwsProtocol(HttpProtocol protocol) {
66+
return protocol == HttpProtocol.HTTP ? Protocol.HTTP : Protocol.HTTPS;
67+
}
68+
69+
private static HttpProtocol toSnowflakeProtocol(Protocol protocol) {
70+
return protocol == Protocol.HTTP ? HttpProtocol.HTTP : HttpProtocol.HTTPS;
71+
}
5272
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright (c) 2024 Snowflake Computing Inc. All rights reserved.
3+
*/
4+
5+
package net.snowflake.client.jdbc.cloud.storage;
6+
7+
import com.amazonaws.ClientConfiguration;
8+
import com.amazonaws.Protocol;
9+
import com.google.common.base.Strings;
10+
import java.util.Properties;
11+
import net.snowflake.client.core.HttpClientSettingsKey;
12+
import net.snowflake.client.core.HttpProtocol;
13+
import net.snowflake.client.core.SFSessionProperty;
14+
import net.snowflake.client.core.SnowflakeJdbcInternalApi;
15+
import net.snowflake.client.jdbc.ErrorCode;
16+
import net.snowflake.client.jdbc.SnowflakeSQLException;
17+
18+
@SnowflakeJdbcInternalApi
19+
public class S3HttpUtil {
20+
/**
21+
* A static function to set S3 proxy params when there is a valid session
22+
*
23+
* @param key key to HttpClient map containing OCSP and proxy info
24+
* @param clientConfig the configuration needed by S3 to set the proxy
25+
*/
26+
public static void setProxyForS3(HttpClientSettingsKey key, ClientConfiguration clientConfig) {
27+
if (key != null && key.usesProxy()) {
28+
clientConfig.setProxyProtocol(
29+
key.getProxyHttpProtocol() == HttpProtocol.HTTPS ? Protocol.HTTPS : Protocol.HTTP);
30+
clientConfig.setProxyHost(key.getProxyHost());
31+
clientConfig.setProxyPort(key.getProxyPort());
32+
clientConfig.setNonProxyHosts(key.getNonProxyHosts());
33+
if (!Strings.isNullOrEmpty(key.getProxyUser())
34+
&& !Strings.isNullOrEmpty(key.getProxyPassword())) {
35+
clientConfig.setProxyUsername(key.getProxyUser());
36+
clientConfig.setProxyPassword(key.getProxyPassword());
37+
}
38+
}
39+
}
40+
41+
/**
42+
* A static function to set S3 proxy params for sessionless connections using the proxy params
43+
* from the StageInfo
44+
*
45+
* @param proxyProperties proxy properties
46+
* @param clientConfig the configuration needed by S3 to set the proxy
47+
* @throws SnowflakeSQLException
48+
*/
49+
public static void setSessionlessProxyForS3(
50+
Properties proxyProperties, ClientConfiguration clientConfig) throws SnowflakeSQLException {
51+
// do nothing yet
52+
if (proxyProperties != null
53+
&& proxyProperties.size() > 0
54+
&& proxyProperties.getProperty(SFSessionProperty.USE_PROXY.getPropertyKey()) != null) {
55+
Boolean useProxy =
56+
Boolean.valueOf(
57+
proxyProperties.getProperty(SFSessionProperty.USE_PROXY.getPropertyKey()));
58+
if (useProxy) {
59+
// set up other proxy related values.
60+
String proxyHost =
61+
proxyProperties.getProperty(SFSessionProperty.PROXY_HOST.getPropertyKey());
62+
int proxyPort;
63+
try {
64+
proxyPort =
65+
Integer.parseInt(
66+
proxyProperties.getProperty(SFSessionProperty.PROXY_PORT.getPropertyKey()));
67+
} catch (NumberFormatException | NullPointerException e) {
68+
throw new SnowflakeSQLException(
69+
ErrorCode.INVALID_PROXY_PROPERTIES, "Could not parse port number");
70+
}
71+
String proxyUser =
72+
proxyProperties.getProperty(SFSessionProperty.PROXY_USER.getPropertyKey());
73+
String proxyPassword =
74+
proxyProperties.getProperty(SFSessionProperty.PROXY_PASSWORD.getPropertyKey());
75+
String nonProxyHosts =
76+
proxyProperties.getProperty(SFSessionProperty.NON_PROXY_HOSTS.getPropertyKey());
77+
String proxyProtocol =
78+
proxyProperties.getProperty(SFSessionProperty.PROXY_PROTOCOL.getPropertyKey());
79+
Protocol protocolEnum =
80+
(!Strings.isNullOrEmpty(proxyProtocol) && proxyProtocol.equalsIgnoreCase("https"))
81+
? Protocol.HTTPS
82+
: Protocol.HTTP;
83+
clientConfig.setProxyHost(proxyHost);
84+
clientConfig.setProxyPort(proxyPort);
85+
clientConfig.setNonProxyHosts(nonProxyHosts);
86+
clientConfig.setProxyProtocol(protocolEnum);
87+
if (!Strings.isNullOrEmpty(proxyUser) && !Strings.isNullOrEmpty(proxyPassword)) {
88+
clientConfig.setProxyUsername(proxyUser);
89+
clientConfig.setProxyPassword(proxyPassword);
90+
}
91+
}
92+
}
93+
}
94+
}

src/main/java/net/snowflake/client/jdbc/cloud/storage/SnowflakeS3Client.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,9 @@ private void setupSnowflakeS3Client(
166166
clientConfig.withSignerOverride("AWSS3V4SignerType");
167167
clientConfig.getApacheHttpClientConfig().setSslSocketFactory(getSSLConnectionSocketFactory());
168168
if (session != null) {
169-
HttpUtil.setProxyForS3(session.getHttpClientKey(), clientConfig);
169+
S3HttpUtil.setProxyForS3(session.getHttpClientKey(), clientConfig);
170170
} else {
171-
HttpUtil.setSessionlessProxyForS3(proxyProperties, clientConfig);
171+
S3HttpUtil.setSessionlessProxyForS3(proxyProperties, clientConfig);
172172
}
173173
AmazonS3Builder<?, ?> amazonS3Builder = AmazonS3Client.builder();
174174
if (encMat != null) {

src/test/java/net/snowflake/client/core/CoreUtilsMiscellaneousTest.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import net.snowflake.client.jdbc.ErrorCode;
2222
import net.snowflake.client.jdbc.SnowflakeSQLException;
2323
import net.snowflake.client.jdbc.SnowflakeUtil;
24+
import net.snowflake.client.jdbc.cloud.storage.S3HttpUtil;
2425
import org.junit.Test;
2526

2627
public class CoreUtilsMiscellaneousTest {
@@ -109,7 +110,7 @@ public void testSetProxyForS3() {
109110
"jdbc",
110111
false);
111112
ClientConfiguration clientConfig = new ClientConfiguration();
112-
HttpUtil.setProxyForS3(testKey, clientConfig);
113+
S3HttpUtil.setProxyForS3(testKey, clientConfig);
113114
assertEquals(Protocol.HTTPS, clientConfig.getProxyProtocol());
114115
assertEquals("snowflakecomputing.com", clientConfig.getProxyHost());
115116
assertEquals(443, clientConfig.getProxyPort());
@@ -129,7 +130,7 @@ public void testSetSessionlessProxyForS3() throws SnowflakeSQLException {
129130
props.put("nonProxyHosts", "baz.com | foo.com");
130131
props.put("proxyProtocol", "http");
131132
ClientConfiguration clientConfig = new ClientConfiguration();
132-
HttpUtil.setSessionlessProxyForS3(props, clientConfig);
133+
S3HttpUtil.setSessionlessProxyForS3(props, clientConfig);
133134
assertEquals(Protocol.HTTP, clientConfig.getProxyProtocol());
134135
assertEquals("localhost", clientConfig.getProxyHost());
135136
assertEquals(8084, clientConfig.getProxyPort());
@@ -139,7 +140,7 @@ public void testSetSessionlessProxyForS3() throws SnowflakeSQLException {
139140
// Test that exception is thrown when port number is invalid
140141
props.put("proxyPort", "invalidnumber");
141142
try {
142-
HttpUtil.setSessionlessProxyForS3(props, clientConfig);
143+
S3HttpUtil.setSessionlessProxyForS3(props, clientConfig);
143144
} catch (SnowflakeSQLException e) {
144145
assertEquals((int) ErrorCode.INVALID_PROXY_PROPERTIES.getMessageCode(), e.getErrorCode());
145146
}
@@ -349,7 +350,7 @@ public void testNullAndEmptyProxySettingsForS3() {
349350
HttpClientSettingsKey testKey =
350351
new HttpClientSettingsKey(OCSPMode.FAIL_OPEN, null, 443, null, null, null, "", "", false);
351352
ClientConfiguration clientConfig = new ClientConfiguration();
352-
HttpUtil.setProxyForS3(testKey, clientConfig);
353+
S3HttpUtil.setProxyForS3(testKey, clientConfig);
353354
assertEquals(Protocol.HTTP, clientConfig.getProxyProtocol());
354355
assertEquals("", clientConfig.getProxyHost());
355356
assertEquals(443, clientConfig.getProxyPort());

0 commit comments

Comments
 (0)