From 845e7ea3808226b8e082011ceb428e71d255b73f Mon Sep 17 00:00:00 2001 From: Owais Kazi Date: Thu, 3 Nov 2022 14:51:09 -0700 Subject: [PATCH 1/5] Migrate client transports to Apache HttpClient/Core5.x Signed-off-by: Owais Kazi --- build.gradle | 5 ++- .../java/org/opensearch/sdk/SDKClient.java | 40 ++++++++++++++++--- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/build.gradle b/build.gradle index b1cd9fc1..9ec4cd13 100644 --- a/build.gradle +++ b/build.gradle @@ -65,8 +65,8 @@ dependencies { implementation "org.opensearch.plugin:transport-netty4-client:3.0.0-SNAPSHOT" 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:3.0.0-SNAPSHOT' + implementation 'org.opensearch.client:opensearch-java:3.0.0-SNAPSHOT' 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") { @@ -76,6 +76,7 @@ dependencies { 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.datatype:jackson-datatype-jsr310: 2.12.6.1' 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" diff --git a/src/main/java/org/opensearch/sdk/SDKClient.java b/src/main/java/org/opensearch/sdk/SDKClient.java index 54135663..c5366105 100644 --- a/src/main/java/org/opensearch/sdk/SDKClient.java +++ b/src/main/java/org/opensearch/sdk/SDKClient.java @@ -11,8 +11,17 @@ 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; @@ -20,6 +29,8 @@ 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 */ @@ -35,11 +46,27 @@ public class SDKClient { * @return SDKClient which is internally an OpenSearchClient. The user is responsible for calling {@link #doCloseRestClient()} when finished with the client */ public OpenSearchClient initializeClient(String hostAddress, int port) throws IOException { - RestClientBuilder builder = RestClient.builder(new HttpHost(hostAddress, port)); + RestClientBuilder builder = RestClient.builder(new HttpHost("http", hostAddress, port)); 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 testing cluster just uses the default security configuration + .setHostnameVerifier(NoopHostnameVerifier.INSTANCE) + // See please https://issues.apache.org/jira/browse/HTTPCLIENT-2219 + .setTlsDetailsFactory(new Factory() { + @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); } @@ -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; } From d87b9ad62d6f9d99bc0c44f88e5f92e1c8b422d1 Mon Sep 17 00:00:00 2001 From: Owais Kazi Date: Thu, 3 Nov 2022 15:00:08 -0700 Subject: [PATCH 2/5] Removed scheme Signed-off-by: Owais Kazi --- src/main/java/org/opensearch/sdk/SDKClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/opensearch/sdk/SDKClient.java b/src/main/java/org/opensearch/sdk/SDKClient.java index c5366105..49cc268a 100644 --- a/src/main/java/org/opensearch/sdk/SDKClient.java +++ b/src/main/java/org/opensearch/sdk/SDKClient.java @@ -46,7 +46,7 @@ public class SDKClient { * @return SDKClient which is internally an OpenSearchClient. The user is responsible for calling {@link #doCloseRestClient()} when finished with the client */ public OpenSearchClient initializeClient(String hostAddress, int port) throws IOException { - RestClientBuilder builder = RestClient.builder(new HttpHost("http", hostAddress, port)); + RestClientBuilder builder = RestClient.builder(new HttpHost(hostAddress, port)); builder.setStrictDeprecationMode(true); builder.setHttpClientConfigCallback(httpClientBuilder -> { try { From 010b73efecaa0fab44fe25a724e803064b07b499 Mon Sep 17 00:00:00 2001 From: Owais Kazi Date: Thu, 3 Nov 2022 15:03:09 -0700 Subject: [PATCH 3/5] Cleanep up compiler warnings Signed-off-by: Owais Kazi --- .../opensearch/sdk/handlers/ClusterStateResponseHandler.java | 2 ++ .../sdk/handlers/EnvironmentSettingsResponseHandler.java | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/main/java/org/opensearch/sdk/handlers/ClusterStateResponseHandler.java b/src/main/java/org/opensearch/sdk/handlers/ClusterStateResponseHandler.java index 26539cfa..fa03a3ee 100644 --- a/src/main/java/org/opensearch/sdk/handlers/ClusterStateResponseHandler.java +++ b/src/main/java/org/opensearch/sdk/handlers/ClusterStateResponseHandler.java @@ -68,6 +68,8 @@ public ClusterStateResponse read(StreamInput in) throws IOException { /** * Invokes await on the ClusterStateResponseHandler count down latch + * @throws InterruptedException + * if await returns an exception */ public void awaitResponse() throws InterruptedException { inProgressLatch.await(ExtensionsOrchestrator.EXTENSION_REQUEST_WAIT_TIMEOUT, TimeUnit.SECONDS); diff --git a/src/main/java/org/opensearch/sdk/handlers/EnvironmentSettingsResponseHandler.java b/src/main/java/org/opensearch/sdk/handlers/EnvironmentSettingsResponseHandler.java index a94c00dd..1c58c137 100644 --- a/src/main/java/org/opensearch/sdk/handlers/EnvironmentSettingsResponseHandler.java +++ b/src/main/java/org/opensearch/sdk/handlers/EnvironmentSettingsResponseHandler.java @@ -68,6 +68,8 @@ public EnvironmentSettingsResponse read(StreamInput in) throws IOException { /** * Invokes await on the EnvironmentSettingsResponseHandler count down latch + * @throws InterruptedException + * if await returns an exception */ public void awaitResponse() throws InterruptedException { inProgressLatch.await(ExtensionsOrchestrator.EXTENSION_REQUEST_WAIT_TIMEOUT, TimeUnit.SECONDS); From 138dba77e95e02cf34104ce8b3fd8f4dded67a50 Mon Sep 17 00:00:00 2001 From: Owais Kazi Date: Thu, 3 Nov 2022 15:09:19 -0700 Subject: [PATCH 4/5] Updated comment Signed-off-by: Owais Kazi --- src/main/java/org/opensearch/sdk/SDKClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/opensearch/sdk/SDKClient.java b/src/main/java/org/opensearch/sdk/SDKClient.java index 49cc268a..21076d60 100644 --- a/src/main/java/org/opensearch/sdk/SDKClient.java +++ b/src/main/java/org/opensearch/sdk/SDKClient.java @@ -52,7 +52,7 @@ public OpenSearchClient initializeClient(String hostAddress, int port) throws IO try { final TlsStrategy tlsStrategy = ClientTlsStrategyBuilder.create() .setSslContext(SSLContextBuilder.create().loadTrustMaterial(null, (chains, authType) -> true).build()) - // disable the certificate since our testing cluster just uses the default security configuration + // 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() { From 5309b2cf571831bfbe1e76847cc946ad65f8f107 Mon Sep 17 00:00:00 2001 From: Owais Kazi Date: Fri, 4 Nov 2022 11:29:13 -0700 Subject: [PATCH 5/5] Generalized version numbers to a variable Signed-off-by: Owais Kazi --- build.gradle | 20 +++++++++++-------- .../handlers/ClusterStateResponseHandler.java | 2 +- .../EnvironmentSettingsResponseHandler.java | 2 +- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/build.gradle b/build.gradle index 9ec4cd13..6a59af5f 100644 --- a/build.gradle +++ b/build.gradle @@ -61,12 +61,16 @@ 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:3.0.0-SNAPSHOT' - implementation 'org.opensearch.client:opensearch-java:3.0.0-SNAPSHOT' + 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") { @@ -74,12 +78,12 @@ dependencies { 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.datatype:jackson-datatype-jsr310: 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" } diff --git a/src/main/java/org/opensearch/sdk/handlers/ClusterStateResponseHandler.java b/src/main/java/org/opensearch/sdk/handlers/ClusterStateResponseHandler.java index fa03a3ee..1a3ba5d9 100644 --- a/src/main/java/org/opensearch/sdk/handlers/ClusterStateResponseHandler.java +++ b/src/main/java/org/opensearch/sdk/handlers/ClusterStateResponseHandler.java @@ -69,7 +69,7 @@ public ClusterStateResponse read(StreamInput in) throws IOException { /** * Invokes await on the ClusterStateResponseHandler count down latch * @throws InterruptedException - * if await returns an exception + * if the response times out */ public void awaitResponse() throws InterruptedException { inProgressLatch.await(ExtensionsOrchestrator.EXTENSION_REQUEST_WAIT_TIMEOUT, TimeUnit.SECONDS); diff --git a/src/main/java/org/opensearch/sdk/handlers/EnvironmentSettingsResponseHandler.java b/src/main/java/org/opensearch/sdk/handlers/EnvironmentSettingsResponseHandler.java index 1c58c137..b59094d7 100644 --- a/src/main/java/org/opensearch/sdk/handlers/EnvironmentSettingsResponseHandler.java +++ b/src/main/java/org/opensearch/sdk/handlers/EnvironmentSettingsResponseHandler.java @@ -69,7 +69,7 @@ public EnvironmentSettingsResponse read(StreamInput in) throws IOException { /** * Invokes await on the EnvironmentSettingsResponseHandler count down latch * @throws InterruptedException - * if await returns an exception + * if the response times out */ public void awaitResponse() throws InterruptedException { inProgressLatch.await(ExtensionsOrchestrator.EXTENSION_REQUEST_WAIT_TIMEOUT, TimeUnit.SECONDS);