I'm currently running with Elasticstack 6.2.3, along with the x-pack plugin. Part of the setup of x-pack was to create SSL encryption on both the host transport layer and the client http layer. The configuration all went fine, but now I am unable to connect to my cluster using the Java Rest Client. I was able to run the client perfectly before, even using basic authentication, but now it fails to connect with "Java.net.ConnectException: Connection refused: no further information". How descriptive! Below is my code:
URL keyStorePathUrl = this.getClass().getClassLoader().getResource("certs/elastic-keystore.jks");
Path keyStorePathRaw = Paths.get(keyStorePathUrl.toURI());
String keyStorePass = "";
KeyStore truststore = KeyStore.getInstance("jks");
try(InputStream is = Files.newInputStream(keyStorePathRaw))
{
truststore.load(is, keyStorePass.toCharArray());
}
SSLContextBuilder sslBuilder = SSLContexts.custom().loadTrustMaterial(truststore, null);
final SSLContext sslContext = sslBuilder.build();
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(
AuthScope.ANY,
new UsernamePasswordCredentials("", ""));
RestClientBuilder lowClient = RestClient.builder(new HttpHost("", 9200, "https"))
.setHttpClientConfigCallback(
new RestClientBuilder.HttpClientConfigCallback()
{
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder)
{
return httpClientBuilder.setSSLContext(sslContext)
.setDefaultCredentialsProvider(credentialsProvider);
}
}
)
.setRequestConfigCallback(
new RestClientBuilder.RequestConfigCallback()
{
@Override
public RequestConfig.Builder customizeRequestConfig(
RequestConfig.Builder requestConfigBuilder)
{
return requestConfigBuilder.setConnectionRequestTimeout(-1).setSocketTimeout(-1);
}
}
);
RestHighLevelClient client = new RestHighLevelClient(lowClient);
DeleteIndexRequest request = new DeleteIndexRequest("twitter");
try
{
DeleteIndexResponse response = client.indices().delete(request);
System.out.println(response.toString());
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
finally
{
client.close();
}