Skip to content

Commit

Permalink
Update all client generation
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Nied <[email protected]>
  • Loading branch information
peternied committed Sep 21, 2023
1 parent 842b0c0 commit 7b7bc73
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
package org.opensearch.test.framework.cluster;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.SSLContext;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.conn.routing.HttpRoutePlanner;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
Expand All @@ -31,7 +34,7 @@ class CloseableHttpClientFactory {

private final HttpRoutePlanner routePlanner;

private final String[] supportedCipherSuit;
private final String[] supportedCipherSuites;

public CloseableHttpClientFactory(
SSLContext sslContext,
Expand All @@ -42,7 +45,7 @@ public CloseableHttpClientFactory(
this.sslContext = Objects.requireNonNull(sslContext, "SSL context is required.");
this.requestConfig = requestConfig;
this.routePlanner = routePlanner;
this.supportedCipherSuit = supportedCipherSuit;
this.supportedCipherSuites = supportedCipherSuit;
}

public CloseableHttpClient getHTTPClient() {
Expand All @@ -51,17 +54,19 @@ public CloseableHttpClient getHTTPClient() {

final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
this.sslContext,
null,
supportedCipherSuit,
/* Uses default supported protocals */ null,
supportedCipherSuites,
NoopHostnameVerifier.INSTANCE
);

final HttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
/** TODO: HOW SHOULD THIS BE REPLACED ??? */
// final HttpClientConnectionManager cm = PoolingHttpClientConnectionManagerBuilder.create()
// .setSSLSocketFactory(sslsf)
// .setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(60, TimeUnit.SECONDS).build())
// .build();
final HttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(
RegistryBuilder.<ConnectionSocketFactory>create().register("https", sslsf).build(),
/* Uses default connnction factory */ null,
/* Uses default scheme port resolver */ null,
/* Uses default dns resolver */ null,
60,
TimeUnit.SECONDS
);
hcb.setConnectionManager(cm);
if (routePlanner != null) {
hcb.setRoutePlanner(routePlanner);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@
import java.net.InetAddress;
import java.util.Objects;

import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.impl.conn.DefaultRoutePlanner;
import org.apache.http.impl.conn.DefaultSchemePortResolver;
import org.apache.http.protocol.HttpContext;

/**
* Class which can be used to bind Apache HTTP client to a particular network interface or its IP address so that the IP address of
Expand All @@ -36,12 +43,14 @@ public LocalAddressRoutePlanner(InetAddress localAddress) {
this.localAddress = Objects.requireNonNull(localAddress);
}

/** TODO: HOW SHOULD THIS BE REPLACED ??? */
// /**
// * Determines IP address used by the client socket of Apache HTTP client
// */
// @Override
// protected InetAddress determineLocalAddress(HttpHost firstHop, HttpContext context) {
// return localAddress;
// }
@Override
public HttpRoute determineRoute(final HttpHost host, final HttpRequest request, final HttpContext context) throws HttpException {
final HttpClientContext clientContext = HttpClientContext.adapt(context);
final RequestConfig localRequsetConfig = RequestConfig.copy(clientContext.getRequestConfig())
.setLocalAddress(this.localAddress)
.build();
clientContext.setRequestConfig(localRequsetConfig);

return super.determineRoute(host, request, clientContext);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

package org.opensearch.test.framework.cluster;

import static org.opensearch.test.framework.cluster.TestRestClientConfiguration.getBasicAuthHeader;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.URI;
Expand All @@ -38,6 +40,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -46,20 +49,20 @@
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;

import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.nio.conn.ssl.SSLIOSessionStrategy;
import org.opensearch.client.RestClient;
import org.opensearch.client.RestClientBuilder;
import org.opensearch.client.RestHighLevelClient;
import org.opensearch.security.support.PemKeyReader;
import org.opensearch.test.framework.certificate.CertificateData;
import org.opensearch.test.framework.certificate.TestCertificates;

import static org.opensearch.test.framework.cluster.TestRestClientConfiguration.getBasicAuthHeader;

/**
* OpenSearchClientProvider provides methods to get a REST client for an underlying cluster or node.
*
Expand Down Expand Up @@ -132,26 +135,15 @@ default RestHighLevelClient getRestHighLevelClient(
Collection<? extends Header> defaultHeaders
) {
RestClientBuilder.HttpClientConfigCallback configCallback = httpClientBuilder -> {
/** TODO: HOW SHOULD THIS BE REPLACED ??? */
// TlsStrategy tlsStrategy = ClientTlsStrategyBuilder.create()
// .setSslContext(getSSLContext())
// .setHostnameVerifier(NoopHostnameVerifier.INSTANCE)
// // See please https://issues.apache.org/jira/browse/HTTPCLIENT-2219
// .setTlsDetailsFactory(new Factory<SSLEngine, TlsDetails>() {
// @Override
// public TlsDetails create(final SSLEngine sslEngine) {
// return new TlsDetails(sslEngine.getSession(), sslEngine.getApplicationProtocol());
// }
// })
// .build();

// final AsyncClientConnectionManager cm =
// PoolingAsyncClientConnectionManagerBuilder.create().setTlsStrategy(tlsStrategy).build();

if (credentialsProvider != null) {
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
// httpClientBuilder.setConnectionManager(cm);
Optional.ofNullable(credentialsProvider).ifPresent(httpClientBuilder::setDefaultCredentialsProvider);
httpClientBuilder.setSSLStrategy(
new SSLIOSessionStrategy(
getSSLContext(),
/* Use default supported protocols */ null,
/* Use default supported cipher suites */ null,
NoopHostnameVerifier.INSTANCE
)
);
httpClientBuilder.setDefaultHeaders(defaultHeaders);
return httpClientBuilder;
};
Expand Down

0 comments on commit 7b7bc73

Please sign in to comment.