Search Tutorials


Connect Spring Boot Application to Elasticsearch Docker Container using HTTPS | JavaInUse

Connect Spring Boot Application to Elasticsearch Docker Container using HTTPS

Previously we had deployed the elasticsearch application to docker container. Also another previous tutorial we had implemented Spring Boot + Elasticsearch CRUD example where we made a HTTPS connection to elasticsearch instance. In this tutorial we will be connecting to the deployed elasticsearch docker container using Spring Boot Application.
Connect Spring Boot Application to Elasticsearch Docker Container using HTTPS

Video

This tutorial is explained in the below Youtube Video.


Implementation

In previous tutorial we had deployed the elasticsearch application to docker container. We will be making use of the code we had implemented for Spring Boot + Elasticsearch CRUD Tutorial. In this code we had implemented code to configure the spring boot elasticsearch connection-
package com.javainuse.bootelastisearchcrud.config;

import java.io.File;

import javax.net.ssl.SSLContext;

import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.SSLContexts;
import org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HttpClientConfigImpl implements HttpClientConfigCallback {

	@Override
	public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
		try {
			final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
			UsernamePasswordCredentials usernamePasswordCredentials = new UsernamePasswordCredentials("elastic",
					"javainuse");
			credentialsProvider.setCredentials(AuthScope.ANY, usernamePasswordCredentials);
			httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);

			String trustLocationStore = "E:\\trial\\elasticsearch-8.13.4-windows-x86_64\\elasticsearch-8.13.4\\config\\certs\\truststore.p12";
			File trustLocationFile = new File(trustLocationStore);

			SSLContextBuilder sslContextBuilder = SSLContexts.custom().loadTrustMaterial(trustLocationFile,
					"javainuse".toCharArray());
			SSLContext sslContext = sslContextBuilder.build();
			httpClientBuilder.setSSLContext(sslContext);

		} catch (Exception e) {
		}
		return httpClientBuilder;
	}
}




In above code we will be changing the elasticsearch credentials to the one we got on elasticsearch docker container start up as follows-
Elasticsearch 8 Container
package com.javainuse.bootelastisearchcrud.config;

import java.io.File;

import javax.net.ssl.SSLContext;

import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.SSLContexts;
import org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HttpClientConfigImpl implements HttpClientConfigCallback {

	@Override
	public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
		try {
			final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
			UsernamePasswordCredentials usernamePasswordCredentials = new UsernamePasswordCredentials("elastic",
					"jjlw62g1HKis5NGzfJlA");
			credentialsProvider.setCredentials(AuthScope.ANY, usernamePasswordCredentials);
			httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);

			String trustLocationStore = "E:\\trial\\elasticsearch-8.13.4-windows-x86_64\\elasticsearch-8.13.4\\config\\certs\\truststore.p12";
			File trustLocationFile = new File(trustLocationStore);

			SSLContextBuilder sslContextBuilder = SSLContexts.custom().loadTrustMaterial(trustLocationFile,
					"javainuse".toCharArray());
			SSLContext sslContext = sslContextBuilder.build();
			httpClientBuilder.setSSLContext(sslContext);

		} catch (Exception e) {
		}
		return httpClientBuilder;
	}
}
Next we will need to configure the elasticsearch trustore to connect to the elasticsearch docker container. Using the docker exec command we will be creating a truststore for the docker elasticsearch.
  • Using exec command we will list the contents of the elasticsearch -> config -> certs folder
    docker exec -it es01 ls /usr/share/elasticsearch/config/certs
    

    Elasticsearch 8 docker exec
  • Next we will create a new trustore for the elasticsearch docker instance. In this truststire we will import the http_ca.crt file.
    docker exec -it es01 /usr/share/elasticsearch/jdk/bin/keytool -import -file /usr/share/elasticsearch/config/certs/http_ca.crt -keystore "/usr/share/elasticsearch/config/certs/truststore.p12" -storepass javainuse -noprompt -storetype pkcs12
    

    Elasticsearch 8 Container new trustore

    Elasticsearch 8 Container
  • Once this truststore is created, we will copy it from the docker container to the outside folder location so that we can use it with the spring boot project.
    docker cp es01:/usr/share/elasticsearch/config/certs/truststore.p12 D:\elast-cert
    

    Elasticsearch 8 Container export cert
Next we will modify the HttpClientConfigImpl to use this truststore.
package com.javainuse.bootelastisearchcrud.config;

import java.io.File;

import javax.net.ssl.SSLContext;

import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.SSLContexts;
import org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HttpClientConfigImpl implements HttpClientConfigCallback {

	@Override
	public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
		try {
			final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
			UsernamePasswordCredentials usernamePasswordCredentials = new UsernamePasswordCredentials("elastic",
					"jjlw62g1HKis5NGzfJlA");
			credentialsProvider.setCredentials(AuthScope.ANY, usernamePasswordCredentials);
			httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);

			String trustLocationStore = "D:\\elast-cert\\truststore.p12";
			File trustLocationFile = new File(trustLocationStore);

			SSLContextBuilder sslContextBuilder = SSLContexts.custom().loadTrustMaterial(trustLocationFile,
					"javainuse".toCharArray());
			SSLContext sslContext = sslContextBuilder.build();
			httpClientBuilder.setSSLContext(sslContext);

		} catch (Exception e) {
		}
		return httpClientBuilder;
	}
}
If we now start spring boot application we can connect to the containarized elasticsearch application and perform CRUD operation.
spring boot application Elasticsearch 8 Container
If we now go to localhost:8080/employees we can retrieve the employee documents from elasticsearch. Currently there are no employee records in the elasticsearch container so this returns an empty list.
spring boot application Elasticsearch 8 Container
We can now perform CRUD operations using the elasticsearch docker container.

Download Source Code

Download it -
Spring Boot 3 + Elasticsearch Container CRUD Example