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

Add new connection parameter tunnelHost to use with tunnel cunnection. #83

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
179 changes: 87 additions & 92 deletions README.md

Large diffs are not rendered by default.

32 changes: 27 additions & 5 deletions src/main/java/org/opensearch/jdbc/config/ConnectionConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class ConnectionConfig {
private String trustStoreType;
private boolean trustSelfSigned;
private boolean hostnameVerification;
private String tunnelHost;

private ConnectionConfig(Builder builder) {
this.url = builder.getUrl();
Expand Down Expand Up @@ -80,6 +81,7 @@ private ConnectionConfig(Builder builder) {
this.trustSelfSigned = builder.getTrustSelfSignedConnectionProperty().getValue();

this.hostnameVerification = builder.getHostnameVerificationConnectionProperty().getValue();
this.tunnelHost = builder.getTunnelHostConnectionProperty().getValue();
}

public static Builder builder() {
Expand Down Expand Up @@ -182,6 +184,10 @@ public boolean hostnameVerification() {
return hostnameVerification;
}

public String tunnelHost() {
return tunnelHost;
}

@Override
public String toString() {
return "ConnectionConfig{" +
Expand Down Expand Up @@ -209,6 +215,7 @@ public String toString() {
", trustStoreType='" + trustStoreType + '\'' +
", trustSelfSigned='" + trustSelfSigned + '\'' +
", hostnameVerification='" + hostnameVerification + '\'' +
", tunnelHost='" + tunnelHost + '\'' +
'}';
}

Expand Down Expand Up @@ -256,6 +263,9 @@ public static class Builder {
private HostnameVerificationConnectionProperty hostnameVerificationConnectionProperty
= new HostnameVerificationConnectionProperty();

private TunnelHostConnectionProperty tunnelHostConnectionProperty
= new TunnelHostConnectionProperty();

ConnectionProperty[] connectionProperties = new ConnectionProperty[]{
hostProperty,
portProperty,
Expand All @@ -278,7 +288,8 @@ public static class Builder {
trustStorePasswordConnectionProperty,
trustStoreTypeConnectionProperty,
trustSelfSignedConnectionProperty,
hostnameVerificationConnectionProperty
hostnameVerificationConnectionProperty,
tunnelHostConnectionProperty
};

private String url = null;
Expand Down Expand Up @@ -385,6 +396,10 @@ public HostnameVerificationConnectionProperty getHostnameVerificationConnectionP
return hostnameVerificationConnectionProperty;
}

public TunnelHostConnectionProperty getTunnelHostConnectionProperty() {
return tunnelHostConnectionProperty;
}

public Builder setLogWriter(PrintWriter printWriter) {
this.logWriter = printWriter;
return this;
Expand All @@ -401,8 +416,12 @@ public Builder setUrl(String url) {

public Builder setPropertyMap(Map<String, Object> map) {
if (map != null) {
propertyMap = new HashMap<>();
propertyMap.putAll(map);
if (propertyMap == null) {
propertyMap = new HashMap<>();
}
for (Map.Entry<String, Object> pair : map.entrySet()) {
propertyMap.put(pair.getKey().toLowerCase(), pair.getValue());
}
}
return this;
}
Expand All @@ -422,7 +441,9 @@ public Builder overrideProperties(Map<String, Object> map) {
if (overrideMap == null) {
overrideMap = new HashMap<>();
}
this.overrideMap.putAll(map);
for (Map.Entry<String, Object> pair : map.entrySet()) {
overrideMap.put(pair.getKey().toLowerCase(), pair.getValue());
}
}
return this;
}
Expand All @@ -435,7 +456,7 @@ public Builder setProperties(Properties properties) {

while (enumeration.hasMoreElements()) {
String key = (String) enumeration.nextElement();
this.properties.setProperty(key, properties.getProperty(key));
this.properties.setProperty(key.toLowerCase(), properties.getProperty(key));
}
}
return this;
Expand Down Expand Up @@ -544,6 +565,7 @@ private void validateConfig() throws ConnectionPropertyException {
* @return effective value
*/
private Object getPropertyValueToSet(String key) {
key = key.toLowerCase();
if (overrideMap != null && overrideMap.containsKey(key)) {
return overrideMap.get(key);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,23 @@

package org.opensearch.jdbc.config;

import java.net.URI;

public class HostConnectionProperty extends StringConnectionProperty {
public static final String KEY = "host";

public HostConnectionProperty() {
super(KEY);
}

@Override
protected String parseValue(Object value) throws ConnectionPropertyException {
String host = super.parseValue(value);
// URI class extracts host from the string. It requires a prefix to be set to parse it properly.
return URI.create(host.startsWith("http") ? host : "https://" + host).getAuthority();
GumpacG marked this conversation as resolved.
Show resolved Hide resolved
}

public String getDefault() {
return "localhost";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.jdbc.config;

public class TunnelHostConnectionProperty extends StringConnectionProperty {
public static final String KEY = "tunnelHost";
acarbonetto marked this conversation as resolved.
Show resolved Hide resolved

public TunnelHostConnectionProperty() {
super(KEY);
}

public String getDefault() {
return null;
}

}
12 changes: 6 additions & 6 deletions src/main/java/org/opensearch/jdbc/internal/util/UrlParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,18 @@ public static Properties parseProperties(final String inputUrl) throws URISyntax
String path = uri.getPath();

if (host != null)
props.setProperty(HostConnectionProperty.KEY, host);
props.setProperty(HostConnectionProperty.KEY.toLowerCase(), host);

if (port != -1)
props.setProperty(PortConnectionProperty.KEY, Integer.toString(port));
props.setProperty(PortConnectionProperty.KEY.toLowerCase(), Integer.toString(port));

if (path != null && path.length() > 0)
props.setProperty(PathConnectionProperty.KEY, path);
props.setProperty(PathConnectionProperty.KEY.toLowerCase(), path);

if ("https".equalsIgnoreCase(scheme)) {
props.setProperty(UseSSLConnectionProperty.KEY, "true");
props.setProperty(UseSSLConnectionProperty.KEY.toLowerCase(), "true");
} else if ("http".equalsIgnoreCase(scheme)) {
props.setProperty(UseSSLConnectionProperty.KEY, "false");
props.setProperty(UseSSLConnectionProperty.KEY.toLowerCase(), "false");
} else {
throw new URISyntaxException(inputUrl, "Invalid scheme:"+scheme+". Only http and https are supported.");
}
Expand All @@ -114,7 +114,7 @@ public static Properties parseProperties(final String inputUrl) throws URISyntax
+ kv[0]
+ ". Expected key=value pairs");
} else {
props.setProperty(kv[0], kv[1]);
props.setProperty(kv[0].toLowerCase(), kv[1]);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ public ApacheHttpTransport(ConnectionConfig connectionConfig, Logger log, String
new AWSRequestSigningApacheInterceptor(
"es",
signer,
provider));
provider,
connectionConfig.tunnelHost()));
}

// TODO - can apply settings retry & backoff
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package org.opensearch.jdbc.transport.http.auth.aws;

import com.amazonaws.DefaultRequest;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.Signer;
import com.amazonaws.http.HttpMethodName;
Expand Down Expand Up @@ -53,6 +54,12 @@ public class AWSRequestSigningApacheInterceptor implements HttpRequestIntercepto
*/
private final AWSCredentialsProvider awsCredentialsProvider;

/**
* Hostname of the OpenSearch cluster, required to make a valid AWS signature in case if
* proxy/tunnel/gateway used.
*/
private final String host;

/**
*
* @param service service that we're connecting to
Expand All @@ -61,10 +68,12 @@ public class AWSRequestSigningApacheInterceptor implements HttpRequestIntercepto
*/
public AWSRequestSigningApacheInterceptor(final String service,
final Signer signer,
final AWSCredentialsProvider awsCredentialsProvider) {
final AWSCredentialsProvider awsCredentialsProvider,
final String host) {
this.service = service;
this.signer = signer;
this.awsCredentialsProvider = awsCredentialsProvider;
this.host = host;
}

/**
Expand All @@ -83,9 +92,14 @@ public void process(final HttpRequest request, final HttpContext context)
// Copy Apache HttpRequest to AWS DefaultRequest
DefaultRequest<?> signableRequest = new DefaultRequest<>(service);

HttpHost host = (HttpHost) context.getAttribute(HTTP_TARGET_HOST);
if (host != null) {
signableRequest.setEndpoint(URI.create(host.toURI()));
if (host != null && !host.equals("")) {
// override host if given by user (`tunnelHost` connection parameter)
signableRequest.setEndpoint(URI.create(host.startsWith("http") ? host : "https://" + host));
} else {
HttpHost host = (HttpHost) context.getAttribute(HTTP_TARGET_HOST);
if (host != null) {
signableRequest.setEndpoint(URI.create(host.toURI()));
}
}
final HttpMethodName httpMethod =
HttpMethodName.fromValue(request.getRequestLine().getMethod());
Expand Down Expand Up @@ -120,6 +134,7 @@ public void process(final HttpRequest request, final HttpContext context)
httpEntityEnclosingRequest.setEntity(basicHttpEntity);
}
}

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public StringKvp(String key, String value) {

public static Properties toProperties(final StringKvp... kvps) {
Properties props = new Properties();
Arrays.stream(kvps).forEach(kvp -> props.setProperty(kvp.getKey(), kvp.getValue()));
Arrays.stream(kvps).forEach(kvp -> props.setProperty(kvp.getKey().toLowerCase(), kvp.getValue()));
return props;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private static AWSRequestSigningApacheInterceptor createInterceptor() {
new AWSStaticCredentialsProvider(new AnonymousAWSCredentials());
return new AWSRequestSigningApacheInterceptor("servicename",
new AddHeaderSigner("Signature", "wuzzle"),
anonymousCredentialsProvider);
anonymousCredentialsProvider, null);

}
}