<p>Does anyone know how to use SSL on Spring Boot application to connect with ElasticSearch which is deployed at Openshift in the form of https? I have a config.java in my Spring Boot application like the following:</p>
<pre><code>@Configuration
@EnableElasticsearchRepositories(basePackages = &quot;com.siolbca.repository&quot;)
@ComponentScan(basePackages = &quot;com.siolbca.services&quot;)
public class Config {
    @Bean
    public RestHighLevelClient client() {
        ClientConfiguration clientConfiguration 
            = ClientConfiguration.builder()
                .connectedTo(&quot;elasticsearch-siol-es-http.siolbca-dev.svc.cluster.local&quot;)
                .usingSsl()
                .withBasicAuth(&quot;elastic&quot;,&quot;G0D1g6TurJ79pcxr1065pU0U&quot;)
                .build();

        return RestClients.create(clientConfiguration).rest();
    }

    @Bean
    public ElasticsearchOperations elasticsearchTemplate() {
        return new ElasticsearchRestTemplate(client());
    }
}
</code></pre>
<p>However, when I run it with Postman to run elasticsearch, an error appears like this:</p>
<pre><code>javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
</code></pre>
<p>I've seen some tutorials on the internet that say it's a certificate issue but I can't get a clue how to implement it in my code because I'm a beginner to Java &amp; Spring Boot.
<a href="https://stackoverflow.com/questions/47334476/using-elasticsearch-java-rest-api-with-self-signed-certificates">using-elasticsearch-java-rest-api-with-self-signed-certificates</a>
<a href="https://stackoverflow.com/questions/56598798/how-to-connect-spring-boot-2-1-with-elasticsearch-6-6-with-cluster-node-https">how-to-connect-spring-boot-2-1-with-elasticsearch-6-6-with-cluster-node-https</a></p>
<p>And here’s my configuration for elasticsearch.yml:</p>
<pre><code>cluster:
  name: elasticsearch-siol
  routing:
    allocation:
      awareness:
        attributes: k8s_node_name
discovery:
  seed_providers: file
http:
  publish_host: ${POD_NAME}.${HEADLESS_SERVICE_NAME}.${NAMESPACE}.svc
network:
  host: &quot;0&quot;
  publish_host: ${POD_IP}
node:
  attr:
    attr_name: attr_value
    k8s_node_name: ${NODE_NAME}
  name: ${POD_NAME}
  roles:
  - master
  - data
  store:
    allow_mmap: false
path:
  data: /usr/share/elasticsearch/data
  logs: /usr/share/elasticsearch/logs
xpack:
  license:
    upload:
      types:
      - trial
      - enterprise
  security:
    authc:
      realms:
        file:
          file1:
            order: -100
        native:
          native1:
            order: -99
      reserved_realm:
        enabled: &quot;false&quot;
    enabled: &quot;true&quot;
    http:
      ssl:
        certificate: /usr/share/elasticsearch/config/http-certs/tls.crt
        certificate_authorities: /usr/share/elasticsearch/config/http-certs/ca.crt
        enabled: true
        key: /usr/share/elasticsearch/config/http-certs/tls.key
    transport:
      ssl:
        certificate: /usr/share/elasticsearch/config/node-transport-cert/transport.tls.crt
        certificate_authorities:
        - /usr/share/elasticsearch/config/transport-certs/ca.crt
        - /usr/share/elasticsearch/config/transport-remote-certs/ca.crt
        enabled: &quot;true&quot;
        key: /usr/share/elasticsearch/config/node-transport-cert/transport.tls.key
        verification_mode: certificate
</code></pre>
<p>Does anyone know how to use the provided certificate in my Spring Boot application? Thank you.</p>

Achmad Fathur Rizki
<p>I solved my problem by ignoring SSL certificate verification while connecting to elasticsearch from my Backend (Spring Boot). I followed some instruction from website below:</p>
<p><a href="https://stackoverflow.com/questions/62270799/ignore-ssl-certificate-verfication-while-connecting-to-elasticsearch-from-spring">Ignore SSL Certificate Verification</a></p>
<p>I also modified the code by adding basic authentication as follows:</p>
<pre><code>@Configuration
@EnableElasticsearchRepositories(basePackages = &quot;com.siolbca.repository&quot;)
@ComponentScan(basePackages = &quot;com.siolbca.services&quot;)
public class Config {
    @Bean
    public RestHighLevelClient createSimpleElasticClient() throws Exception {
        try {
            final CredentialsProvider credentialsProvider =
                    new BasicCredentialsProvider();
                credentialsProvider.setCredentials(AuthScope.ANY,
                    new UsernamePasswordCredentials(&quot;elastic&quot;,&quot;G0D1g6TurJ79pcxr1065pU0U&quot;));
                
            SSLContextBuilder sslBuilder = SSLContexts.custom()
                    .loadTrustMaterial(null, (x509Certificates, s) -&gt; true);
                    final SSLContext sslContext = sslBuilder.build();
            RestHighLevelClient client = new RestHighLevelClient(RestClient
                    .builder(new HttpHost(&quot;elasticsearch-siol-es-http.siolbca-dev.svc.cluster.local&quot;, 9200, &quot;https&quot;)) 
//port number is given as 443 since its https schema
                    .setHttpClientConfigCallback(new HttpClientConfigCallback() {
                        @Override
                        public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                            return httpClientBuilder
                                     .setSSLContext(sslContext)
                                     .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
                                     .setDefaultCredentialsProvider(credentialsProvider);
                        }
                    })
                    .setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
                        @Override
                        public RequestConfig.Builder customizeRequestConfig(
                                RequestConfig.Builder requestConfigBuilder) {
                            return requestConfigBuilder.setConnectTimeout(5000)
                                    .setSocketTimeout(120000);
                        }
                    }));
            System.out.println(&quot;elasticsearch client created&quot;);
            return client;
        } catch (Exception e) {
            System.out.println(e);
            throw new Exception(&quot;Could not create an elasticsearch client!!&quot;);
        }
    }
    
}
</code></pre>
