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

Migrate client transports to Apache HttpClient/Core5.x #231

Merged
merged 5 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 12 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,29 @@ configurations {
}

dependencies {
implementation "org.opensearch:opensearch:3.0.0-SNAPSHOT"
implementation "org.opensearch.plugin:transport-netty4-client:3.0.0-SNAPSHOT"

def jacksonDatabindVersion = "2.12.6.1"
def opensearchVersion = "3.0.0-SNAPSHOT"

implementation("org.opensearch:opensearch:${opensearchVersion}")
implementation("org.opensearch.plugin:transport-netty4-client:${opensearchVersion}")
implementation group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.17.1'
implementation group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.19.0'
implementation 'org.opensearch.client:opensearch-rest-client:2.0.0'
implementation 'org.opensearch.client:opensearch-java:2.0.0'
implementation("org.opensearch.client:opensearch-rest-client:${opensearchVersion}")
implementation("org.opensearch.client:opensearch-java:${opensearchVersion}")
implementation "io.netty:netty-all:4.1.73.Final"
implementation "org.apache.lucene:lucene-core:9.4.0-snapshot-ddf0d0a"
testCompileOnly ("junit:junit:4.13.2") {
exclude module : 'hamcrest'
exclude module : 'hamcrest-core'
}
implementation 'javax.xml.bind:jaxb-api:2.2.2'
implementation 'com.fasterxml.jackson.core:jackson-databind: 2.12.6.1'
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml: 2.12.6.1'
implementation("com.fasterxml.jackson.core:jackson-databind:${jacksonDatabindVersion}")
implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:${jacksonDatabindVersion}")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:${jacksonDatabindVersion}")
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
testImplementation "org.opensearch.test:framework:3.0.0-SNAPSHOT"
testImplementation("org.opensearch.test:framework:${opensearchVersion}")
requireJavadoc "org.plumelib:require-javadoc:1.0.4"
}

Expand Down
38 changes: 34 additions & 4 deletions src/main/java/org/opensearch/sdk/SDKClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,26 @@

import java.io.IOException;

import org.apache.http.HttpHost;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.apache.hc.core5.function.Factory;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.client5.http.ssl.NoopHostnameVerifier;
import org.apache.hc.client5.http.ssl.ClientTlsStrategyBuilder;
import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager;
import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder;
import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
import org.apache.hc.core5.reactor.ssl.TlsDetails;
import org.apache.hc.core5.ssl.SSLContextBuilder;
import org.opensearch.client.RestClient;
import org.opensearch.client.RestClientBuilder;
import org.opensearch.client.json.jackson.JacksonJsonpMapper;
import org.opensearch.client.opensearch.OpenSearchClient;
import org.opensearch.client.transport.OpenSearchTransport;
import org.opensearch.client.transport.rest_client.RestClientTransport;

import javax.net.ssl.SSLEngine;

/**
* This class creates SDKClient for an extension to make requests to OpenSearch
*/
Expand All @@ -39,7 +50,23 @@ public OpenSearchClient initializeClient(String hostAddress, int port) throws IO
builder.setStrictDeprecationMode(true);
builder.setHttpClientConfigCallback(httpClientBuilder -> {
try {
return httpClientBuilder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE);
final TlsStrategy tlsStrategy = ClientTlsStrategyBuilder.create()
.setSslContext(SSLContextBuilder.create().loadTrustMaterial(null, (chains, authType) -> true).build())
// disable the certificate since our cluster currently just uses the default security configuration
.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 PoolingAsyncClientConnectionManager connectionManager = PoolingAsyncClientConnectionManagerBuilder.create()
.setTlsStrategy(tlsStrategy)
.build();
return httpClientBuilder.setConnectionManager(connectionManager);
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand All @@ -48,7 +75,10 @@ public OpenSearchClient initializeClient(String hostAddress, int port) throws IO
restClient = builder.build();

// Create Client
OpenSearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
OpenSearchTransport transport = new RestClientTransport(
restClient,
new JacksonJsonpMapper(new ObjectMapper().registerModule(new JavaTimeModule()))
);
javaClient = new OpenSearchClient(transport);
return javaClient;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public ClusterStateResponse read(StreamInput in) throws IOException {

/**
* Invokes await on the ClusterStateResponseHandler count down latch
* @throws InterruptedException
* if the response times out
*/
public void awaitResponse() throws InterruptedException {
inProgressLatch.await(ExtensionsOrchestrator.EXTENSION_REQUEST_WAIT_TIMEOUT, TimeUnit.SECONDS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public EnvironmentSettingsResponse read(StreamInput in) throws IOException {

/**
* Invokes await on the EnvironmentSettingsResponseHandler count down latch
* @throws InterruptedException
* if the response times out
*/
public void awaitResponse() throws InterruptedException {
inProgressLatch.await(ExtensionsOrchestrator.EXTENSION_REQUEST_WAIT_TIMEOUT, TimeUnit.SECONDS);
Expand Down