Skip to content

Commit f13b951

Browse files
Add support of SOCKS proxies for S3 repository (#2160)
Signed-off-by: Andrey Pleskach <[email protected]>
1 parent 3d5aff4 commit f13b951

File tree

6 files changed

+411
-73
lines changed

6 files changed

+411
-73
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.repositories.s3;
10+
11+
import com.amazonaws.Protocol;
12+
import org.opensearch.common.Strings;
13+
import org.opensearch.common.settings.SettingsException;
14+
15+
import java.net.InetAddress;
16+
import java.net.InetSocketAddress;
17+
import java.net.UnknownHostException;
18+
import java.util.Objects;
19+
20+
public class ProxySettings {
21+
public static final ProxySettings NO_PROXY_SETTINGS = new ProxySettings(ProxyType.DIRECT, null, -1, null, null);
22+
23+
public static enum ProxyType {
24+
HTTP(Protocol.HTTP.name()),
25+
HTTPS(Protocol.HTTPS.name()),
26+
SOCKS("SOCKS"),
27+
DIRECT("DIRECT");
28+
29+
private final String name;
30+
31+
private ProxyType(String name) {
32+
this.name = name;
33+
}
34+
35+
public Protocol toProtocol() {
36+
if (this == DIRECT) {
37+
// We check it in settings,
38+
// the probability that it could be thrown is small, but how knows
39+
throw new SettingsException("Couldn't convert to S3 protocol");
40+
} else if (this == SOCKS) {
41+
throw new SettingsException("Couldn't convert to S3 protocol. SOCKS is not supported");
42+
}
43+
return Protocol.valueOf(name());
44+
}
45+
46+
}
47+
48+
private final ProxyType type;
49+
50+
private final String host;
51+
52+
private final String username;
53+
54+
private final String password;
55+
56+
private final int port;
57+
58+
public String getHost() {
59+
return host;
60+
}
61+
62+
public ProxySettings(final ProxyType type, final String host, final int port, final String username, final String password) {
63+
this.type = type;
64+
this.host = host;
65+
this.port = port;
66+
this.username = username;
67+
this.password = password;
68+
}
69+
70+
public ProxyType getType() {
71+
return this.type;
72+
}
73+
74+
public String getHostName() {
75+
return host;
76+
}
77+
78+
public int getPort() {
79+
return port;
80+
}
81+
82+
public InetSocketAddress getAddress() {
83+
try {
84+
return new InetSocketAddress(InetAddress.getByName(host), port);
85+
} catch (UnknownHostException e) {
86+
// this error won't be thrown since validation of the host name is in the S3ClientSettings
87+
throw new RuntimeException(e);
88+
}
89+
}
90+
91+
public String getUsername() {
92+
return this.username;
93+
}
94+
95+
public String getPassword() {
96+
return this.password;
97+
}
98+
99+
public boolean isAuthenticated() {
100+
return Strings.isNullOrEmpty(username) == false && Strings.isNullOrEmpty(password) == false;
101+
}
102+
103+
public ProxySettings recreateWithNewHostAndPort(final String host, final int port) {
104+
return new ProxySettings(type, host, port, username, password);
105+
}
106+
107+
@Override
108+
public boolean equals(Object o) {
109+
if (this == o) return true;
110+
if (o == null || getClass() != o.getClass()) return false;
111+
final ProxySettings that = (ProxySettings) o;
112+
return port == that.port
113+
&& type == that.type
114+
&& Objects.equals(host, that.host)
115+
&& Objects.equals(username, that.username)
116+
&& Objects.equals(password, that.password);
117+
}
118+
119+
@Override
120+
public int hashCode() {
121+
return Objects.hash(type, host, username, password, port);
122+
}
123+
}

0 commit comments

Comments
 (0)