use of org.opensearch.client.RestClient in project OpenSearch by opensearch-project.
the class ReindexRestClientSslTests method testClientSucceedsWithCertificateAuthorities.
public void testClientSucceedsWithCertificateAuthorities() throws IOException {
final List<Thread> threads = new ArrayList<>();
final Path ca = getDataPath("ca.pem");
final Settings settings = Settings.builder().put("path.home", createTempDir()).putList("reindex.ssl.certificate_authorities", ca.toString()).put("reindex.ssl.supported_protocols", "TLSv1.2").build();
final Environment environment = TestEnvironment.newEnvironment(settings);
final ReindexSslConfig ssl = new ReindexSslConfig(settings, environment, mock(ResourceWatcherService.class));
try (RestClient client = Reindexer.buildRestClient(getRemoteInfo(), ssl, 1L, threads)) {
final Response response = client.performRequest(new Request("GET", "/"));
assertThat(response.getStatusLine().getStatusCode(), Matchers.is(200));
}
}
use of org.opensearch.client.RestClient in project OpenSearch by opensearch-project.
the class ReindexRestClientSslTests method testClientPassesClientCertificate.
public void testClientPassesClientCertificate() throws IOException {
final List<Thread> threads = new ArrayList<>();
final Path ca = getDataPath("ca.pem");
final Path cert = getDataPath("client/client.crt");
final Path key = getDataPath("client/client.key");
final Settings settings = Settings.builder().put("path.home", createTempDir()).putList("reindex.ssl.certificate_authorities", ca.toString()).put("reindex.ssl.certificate", cert).put("reindex.ssl.key", key).put("reindex.ssl.key_passphrase", "client-password").put("reindex.ssl.supported_protocols", "TLSv1.2").build();
AtomicReference<Certificate[]> clientCertificates = new AtomicReference<>();
handler = https -> {
try {
clientCertificates.set(https.getSSLSession().getPeerCertificates());
} catch (SSLPeerUnverifiedException e) {
logger.warn("Client did not provide certificates", e);
clientCertificates.set(null);
}
};
final Environment environment = TestEnvironment.newEnvironment(settings);
final ReindexSslConfig ssl = new ReindexSslConfig(settings, environment, mock(ResourceWatcherService.class));
try (RestClient client = Reindexer.buildRestClient(getRemoteInfo(), ssl, 1L, threads)) {
final Response response = client.performRequest(new Request("GET", "/"));
assertThat(response.getStatusLine().getStatusCode(), Matchers.is(200));
final Certificate[] certs = clientCertificates.get();
assertThat(certs, Matchers.notNullValue());
assertThat(certs, Matchers.arrayWithSize(1));
assertThat(certs[0], Matchers.instanceOf(X509Certificate.class));
final X509Certificate clientCert = (X509Certificate) certs[0];
assertThat(clientCert.getSubjectDN().getName(), Matchers.is("CN=client"));
assertThat(clientCert.getIssuerDN().getName(), Matchers.is("CN=Elastic Certificate Tool Autogenerated CA"));
}
}
use of org.opensearch.client.RestClient in project OpenSearch by opensearch-project.
the class ReindexRestClientSslTests method testClientFailsWithUntrustedCertificate.
public void testClientFailsWithUntrustedCertificate() throws IOException {
assumeFalse("https://github.com/elastic/elasticsearch/issues/49094", inFipsJvm());
final List<Thread> threads = new ArrayList<>();
final Settings settings = Settings.builder().put("path.home", createTempDir()).put("reindex.ssl.supported_protocols", "TLSv1.2").build();
final Environment environment = TestEnvironment.newEnvironment(settings);
final ReindexSslConfig ssl = new ReindexSslConfig(settings, environment, mock(ResourceWatcherService.class));
try (RestClient client = Reindexer.buildRestClient(getRemoteInfo(), ssl, 1L, threads)) {
expectThrows(SSLHandshakeException.class, () -> client.performRequest(new Request("GET", "/")));
}
}
use of org.opensearch.client.RestClient in project OpenSearch by opensearch-project.
the class SearchDocumentationIT method testSearchTemplateWithStoredScript.
public void testSearchTemplateWithStoredScript() throws Exception {
indexSearchTestData();
RestHighLevelClient client = highLevelClient();
RestClient restClient = client();
registerQueryScript(restClient);
// tag::search-template-request-stored
SearchTemplateRequest request = new SearchTemplateRequest();
request.setRequest(new SearchRequest("posts"));
request.setScriptType(ScriptType.STORED);
request.setScript("title_search");
Map<String, Object> params = new HashMap<>();
params.put("field", "title");
params.put("value", "opensearch");
params.put("size", 5);
request.setScriptParams(params);
// end::search-template-request-stored
// tag::search-template-request-options
request.setExplain(true);
request.setProfile(true);
// end::search-template-request-options
// tag::search-template-execute
SearchTemplateResponse response = client.searchTemplate(request, RequestOptions.DEFAULT);
// end::search-template-execute
SearchResponse searchResponse = response.getResponse();
assertNotNull(searchResponse);
assertTrue(searchResponse.getHits().getTotalHits().value > 0);
// tag::search-template-execute-listener
ActionListener<SearchTemplateResponse> listener = new ActionListener<SearchTemplateResponse>() {
@Override
public void onResponse(SearchTemplateResponse response) {
// <1>
}
@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::search-template-execute-listener
// Replace the empty listener by a blocking listener for tests.
CountDownLatch latch = new CountDownLatch(1);
listener = new LatchedActionListener<>(listener, latch);
// tag::search-template-execute-async
// <1>
client.searchTemplateAsync(request, RequestOptions.DEFAULT, listener);
// end::search-template-execute-async
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
use of org.opensearch.client.RestClient in project OpenSearch by opensearch-project.
the class SearchDocumentationIT method testMultiSearchTemplateWithStoredScript.
public void testMultiSearchTemplateWithStoredScript() throws Exception {
indexSearchTestData();
RestHighLevelClient client = highLevelClient();
RestClient restClient = client();
registerQueryScript(restClient);
// tag::multi-search-template-request-stored
MultiSearchTemplateRequest multiRequest = new MultiSearchTemplateRequest();
String[] searchTerms = { "opensearch", "dashboards" };
for (String searchTerm : searchTerms) {
SearchTemplateRequest request = new SearchTemplateRequest();
request.setRequest(new SearchRequest("posts"));
request.setScriptType(ScriptType.STORED);
request.setScript("title_search");
Map<String, Object> params = new HashMap<>();
params.put("field", "title");
params.put("value", searchTerm);
params.put("size", 5);
request.setScriptParams(params);
multiRequest.add(request);
}
// end::multi-search-template-request-stored
// tag::multi-search-template-execute
MultiSearchTemplateResponse multiResponse = client.msearchTemplate(multiRequest, RequestOptions.DEFAULT);
// end::multi-search-template-execute
assertNotNull(multiResponse);
assertEquals(searchTerms.length, multiResponse.getResponses().length);
assertNotNull(multiResponse.getResponses()[0]);
SearchResponse searchResponse = multiResponse.getResponses()[0].getResponse().getResponse();
assertTrue(searchResponse.getHits().getTotalHits().value > 0);
// tag::multi-search-template-execute-listener
ActionListener<MultiSearchTemplateResponse> listener = new ActionListener<MultiSearchTemplateResponse>() {
@Override
public void onResponse(MultiSearchTemplateResponse response) {
// <1>
}
@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::multi-search-template-execute-listener
// Replace the empty listener by a blocking listener for tests.
CountDownLatch latch = new CountDownLatch(1);
listener = new LatchedActionListener<>(listener, latch);
// tag::multi-search-template-execute-async
client.msearchTemplateAsync(multiRequest, RequestOptions.DEFAULT, listener);
// end::multi-search-template-execute-async
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
Aggregations