Skip to content

Specify https scheme in RestClient encrypted communication example #26670

@PhaedrusTheGreek

Description

@PhaedrusTheGreek

The example for Encrypted Communication with the Rest Client isn't working. Seems like the main problem was that the "HttpHost" object needed a 3rd "https" scheme parameter. Tested the following on 5.3.1 and 5.6.0

String keyStoreFile = "file:///opt/elasticsearch-5.6.0/config/x-pack/my_trust_store.jks";
char[] keyStorePass = "password".toCharArray();

SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(new URL(keyStoreFile), keyStorePass).build();

final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
UsernamePasswordCredentials creds = new UsernamePasswordCredentials("elastic", "changeme");
credentialsProvider.setCredentials(AuthScope.ANY, creds);

RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200, "https"))
        .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                return httpClientBuilder.setSSLContext(sslContext).setDefaultCredentialsProvider(credentialsProvider);
            }
        }).build();

Response response = restClient.performRequest("GET", "/_cluster/health?pretty");
response.getEntity().writeTo(System.out);
		

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions