v78
v78

Reputation: 2933

Elasticsearch rest client http over https issue

I am trying the following API,

@Autowire private final RestHighLevelClient restHighLevelClient;
var restClient = restHighLevelClient.getLowLevelClient();
    Response response = null;
    try {
      var request = new Request("GET", "/_cat/indices?v&format=json");
      response = restClient.performRequest(request);
    } catch (IOException e) {
      log.warn(e.toString(), e);
    }

    var entity = response.getEntity();
    if (entity != null) {
      log.info("content:" + IOUtils.toString(entity.getContent(), StandardCharsets.UTF_8));
    }

The elasticsearch server is running on https, and this is bean to create the client

@Bean(destroyMethod = "close")
  public RestHighLevelClient client() {
    var connUri = URI.create(esClusterUrl);
    String[] auth = connUri.getUserInfo().split(":");

    var credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(
        AuthScope.ANY, new UsernamePasswordCredentials(auth[0], auth[1]));

    var builder =
        RestClient.builder(new HttpHost(connUri.getHost(), connUri.getPort()))
            .setHttpClientConfigCallback(
                httpClientBuilder ->
                    httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
    return new RestHighLevelClient(builder);

The error response, I am getting is

2021-04-11 21:13:07.822  WARN 40680 --- [           main] com.document.feed.util.ESMaintenance     : org.elasticsearch.client.ResponseException: method [GET], host [http://yew-739425389.eu-west-1.bonsaisearch.net:443], URI [/_cat/indices?v&format=json], status line [HTTP/1.1 400 Bad Request]
<html>
<head><title>400 The plain HTTP request was sent to HTTPS port</title></head>
<body>
<center><h1>400 Bad Request</h1></center>
<center>The plain HTTP request was sent to HTTPS port</center>
</body>
</html>

which is pretty much self explainatory, meaning I should be making https calls to the server, while I am doing http rest call. What can be the resolution?

Upvotes: 1

Views: 2729

Answers (1)

v78
v78

Reputation: 2933

Resolved with switching to the https scheme while building the client.

var builder =
        RestClient.builder(new HttpHost(connUri.getHost(), connUri.getPort(), "https"))
            .setHttpClientConfigCallback(
                httpClientBuilder ->
                    httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
    return new RestHighLevelClient(builder);

Upvotes: 2

Related Questions