Setting up injection/usage of new OpenSearch Java Client. (#17760)

* Adding new additional dependencies.

* Setting up injection infrastructure for new client in parallel.

* Adding license headers.

* Including new deps in test jar.

* Removing jakarta deps from assembly.

* Building separate jars for integration tests & distribution.

* Fix class loader issue

Exclude "module-info.class" files from the graylog-storage-opensearch
JAR file to avoid class loader issues.

Revert previous assembly and dependency changes that worked around the
issue in tests.

* Adjusting to Jakarta and recent data node changes.

* fixing wrong builder usage

---------

Co-authored-by: Bernd Ahlers <bernd@graylog.com>
Co-authored-by: Jan Heise <jan.heise@graylog.com>
This commit is contained in:
Dennis Oelkers
2024-01-19 10:37:44 +01:00
committed by GitHub
parent 2749333825
commit aacaa0c4d2
31 changed files with 854 additions and 107 deletions

View File

@@ -35,6 +35,9 @@ import org.graylog.storage.opensearch2.OpenSearchClient;
import org.graylog2.cluster.nodes.DataNodeDto;
import org.graylog2.cluster.nodes.NodeService;
import org.graylog2.security.CustomCAX509TrustManager;
import org.opensearch.client.RestClient;
import org.opensearch.client.json.jackson.JacksonJsonpMapper;
import org.opensearch.client.transport.rest_client.RestClientTransport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -97,6 +100,10 @@ class OpensearchProcessImpl implements OpensearchProcess, ProcessListener {
return OpensearchRestClient.build(configuration, datanodeConfiguration, trustManager);
}
private RestClient createNewRestClient(OpensearchConfiguration configuration) {
return OpensearchRestClient.buildNewClient(configuration, datanodeConfiguration, trustManager);
}
@Override
public List<String> stdOutLogs() {
return stdout.stream().toList();
@@ -195,7 +202,9 @@ class OpensearchProcessImpl implements OpensearchProcess, ProcessListener {
commandLineProcess = new OpensearchCommandLineProcess(config, this);
commandLineProcess.start();
restClient = Optional.of(createRestClient(config));
openSearchClient = restClient.map(c -> new OpenSearchClient(c, objectMapper));
final var transport = new RestClientTransport(createNewRestClient(config), new JacksonJsonpMapper(objectMapper));
final var client = new org.opensearch.client.opensearch.OpenSearchClient(transport);
openSearchClient = restClient.map(c -> new OpenSearchClient(c, client, objectMapper));
}),
() -> {throw new IllegalArgumentException("Opensearch configuration required but not supplied!");}
);

View File

@@ -22,6 +22,7 @@ import org.graylog.shaded.opensearch2.org.apache.http.HttpHost;
import org.graylog.shaded.opensearch2.org.apache.http.HttpRequestInterceptor;
import org.graylog.shaded.opensearch2.org.opensearch.client.RestHighLevelClient;
import org.graylog2.security.CustomCAX509TrustManager;
import org.opensearch.client.RestClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -58,4 +59,29 @@ public class OpensearchRestClient {
}
return new RestHighLevelClient(builder);
}
public static RestClient buildNewClient(OpensearchConfiguration configuration, DatanodeConfiguration datanodeConfiguration, CustomCAX509TrustManager tm) {
final HttpHost host = configuration.getRestBaseUrl();
final var builder = org.opensearch.client.RestClient.builder(new org.apache.http.HttpHost(host.getHostName(), host.getPort(), host.getSchemeName()));
if ("https".equals(host.getSchemeName())) {
try {
final var sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[]{tm}, new SecureRandom());
builder.setHttpClientConfigCallback(httpClientBuilder -> {
httpClientBuilder.addInterceptorLast((org.apache.http.HttpRequestInterceptor) (request, context) -> {
final String jwtToken = datanodeConfiguration.indexerJwtAuthTokenProvider().get();
request.addHeader("Authorization", jwtToken);
});
httpClientBuilder.setSSLContext(sslContext);
return httpClientBuilder;
});
} catch (NoSuchAlgorithmException | KeyManagementException ex) {
LOG.error("Could not initialize SSL correctly: {}", ex.getMessage(), ex);
}
}
return builder.build();
}
}