Search in sources :

Example 6 with RestClient

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));
    }
}
Also used : Path(java.nio.file.Path) Response(org.opensearch.client.Response) ArrayList(java.util.ArrayList) RestClient(org.opensearch.client.RestClient) Request(org.opensearch.client.Request) TestEnvironment(org.opensearch.env.TestEnvironment) Environment(org.opensearch.env.Environment) ResourceWatcherService(org.opensearch.watcher.ResourceWatcherService) Settings(org.opensearch.common.settings.Settings)

Example 7 with RestClient

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"));
    }
}
Also used : Path(java.nio.file.Path) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) ArrayList(java.util.ArrayList) RestClient(org.opensearch.client.RestClient) Request(org.opensearch.client.Request) AtomicReference(java.util.concurrent.atomic.AtomicReference) X509Certificate(java.security.cert.X509Certificate) Response(org.opensearch.client.Response) TestEnvironment(org.opensearch.env.TestEnvironment) Environment(org.opensearch.env.Environment) ResourceWatcherService(org.opensearch.watcher.ResourceWatcherService) Settings(org.opensearch.common.settings.Settings) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 8 with RestClient

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", "/")));
    }
}
Also used : ArrayList(java.util.ArrayList) RestClient(org.opensearch.client.RestClient) Request(org.opensearch.client.Request) TestEnvironment(org.opensearch.env.TestEnvironment) Environment(org.opensearch.env.Environment) ResourceWatcherService(org.opensearch.watcher.ResourceWatcherService) Settings(org.opensearch.common.settings.Settings)

Example 9 with RestClient

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));
}
Also used : MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) HashMap(java.util.HashMap) RestClient(org.opensearch.client.RestClient) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) SearchTemplateRequest(org.opensearch.script.mustache.SearchTemplateRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) SearchTemplateResponse(org.opensearch.script.mustache.SearchTemplateResponse) MultiSearchTemplateResponse(org.opensearch.script.mustache.MultiSearchTemplateResponse)

Example 10 with RestClient

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));
}
Also used : MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) HashMap(java.util.HashMap) RestClient(org.opensearch.client.RestClient) MultiSearchTemplateResponse(org.opensearch.script.mustache.MultiSearchTemplateResponse) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) SearchTemplateRequest(org.opensearch.script.mustache.SearchTemplateRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest)

Aggregations

RestClient (org.opensearch.client.RestClient)44 Request (org.opensearch.client.Request)20 Response (org.opensearch.client.Response)16 HttpHost (org.apache.http.HttpHost)13 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)7 Settings (org.opensearch.common.settings.Settings)7 List (java.util.List)6 DefaultScheduler (org.opensearch.client.sniff.Sniffer.DefaultScheduler)6 Scheduler (org.opensearch.client.sniff.Sniffer.Scheduler)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 Node (org.opensearch.client.Node)5 Map (java.util.Map)4 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 ResponseException (org.opensearch.client.ResponseException)4 Environment (org.opensearch.env.Environment)4 TestEnvironment (org.opensearch.env.TestEnvironment)4