Skip to content

Commit

Permalink
Add getSslBundle to ElasticSearchConnectionDetails
Browse files Browse the repository at this point in the history
  • Loading branch information
mhalbritter committed Aug 12, 2024
1 parent 2b55a32 commit 8ea90e1
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 101 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,7 @@
import java.util.List;

import org.springframework.boot.autoconfigure.service.connection.ConnectionDetails;
import org.springframework.boot.ssl.SslBundle;

/**
* Details required to establish a connection to an Elasticsearch service.
Expand Down Expand Up @@ -63,6 +64,13 @@ default String getPathPrefix() {
return null;
}

/**
* @since 3.4.0
*/
default SslBundle getSslBundle() {
return null;
}

/**
* An Elasticsearch node.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -94,9 +94,15 @@ RestClientBuilder elasticsearchRestClientBuilder(ElasticsearchConnectionDetails
.toArray(HttpHost[]::new));
builder.setHttpClientConfigCallback((httpClientBuilder) -> {
builderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(httpClientBuilder));
String sslBundleName = this.properties.getRestclient().getSsl().getBundle();
if (StringUtils.hasText(sslBundleName)) {
configureSsl(httpClientBuilder, sslBundles.getObject().getBundle(sslBundleName));
SslBundle sslBundle = connectionDetails.getSslBundle();
if (sslBundle == null) {
String sslBundleName = this.properties.getRestclient().getSsl().getBundle();
if (StringUtils.hasText(sslBundleName)) {
sslBundle = sslBundles.getObject().getBundle(sslBundleName);
}
}
if (sslBundle != null) {
configureSsl(httpClientBuilder, sslBundle);
}
return httpClientBuilder;
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,12 +26,10 @@

import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration;
import org.springframework.boot.testsupport.testcontainers.DockerImageNames;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -45,7 +43,7 @@
* @author Scott Frederick
*/
@Testcontainers(disabledWithoutDocker = true)
@DataElasticsearchTest(properties = { "spring.elasticsearch.restclient.ssl.bundle=elasticsearch-container" })
@DataElasticsearchTest
class DataElasticsearchTestVersion8IntegrationTests {

@Container
Expand Down Expand Up @@ -89,14 +87,4 @@ void serviceConnectionAutoConfigurationWasImported() {
assertThat(this.applicationContext).has(importedAutoConfiguration(ServiceConnectionAutoConfiguration.class));
}

@TestConfiguration
static class ElasticsearchContainerSslBundleConfiguration {

@Bean
ElasticsearchContainerSslBundleRegistrar elasticsearchContainerSslBundleRegistrar() {
return new ElasticsearchContainerSslBundleRegistrar("elasticsearch-container", elasticsearch);
}

}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,10 +24,8 @@
import org.testcontainers.junit.jupiter.Testcontainers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.boot.testsupport.testcontainers.DockerImageNames;
import org.springframework.context.annotation.Bean;
import org.springframework.data.elasticsearch.client.elc.ReactiveElasticsearchTemplate;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -39,7 +37,7 @@
* @author Scott Frederick
*/
@Testcontainers(disabledWithoutDocker = true)
@DataElasticsearchTest(properties = { "spring.elasticsearch.restclient.ssl.bundle=elasticsearch-container" })
@DataElasticsearchTest
class DataElasticsearchTestVersion8ReactiveIntegrationTests {

@Container
Expand All @@ -65,14 +63,4 @@ void testRepository() {
.block(Duration.ofSeconds(30))).isTrue();
}

@TestConfiguration
static class ElasticsearchContainerSslBundleConfiguration {

@Bean
ElasticsearchContainerSslBundleRegistrar elasticsearchContainerSslBundleRegistrar() {
return new ElasticsearchContainerSslBundleRegistrar("elasticsearch-container", elasticsearch);
}

}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,12 +18,18 @@

import java.util.List;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;

import org.testcontainers.elasticsearch.ElasticsearchContainer;
import org.testcontainers.utility.ComparableVersion;
import org.testcontainers.utility.DockerImageName;

import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchConnectionDetails;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchConnectionDetails.Node.Protocol;
import org.springframework.boot.ssl.SslBundle;
import org.springframework.boot.ssl.SslManagerBundle;
import org.springframework.boot.testcontainers.service.connection.ContainerConnectionDetailsFactory;
import org.springframework.boot.testcontainers.service.connection.ContainerConnectionSource;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
Expand Down Expand Up @@ -81,6 +87,30 @@ public String getPassword() {
return isVersion8OrGreater() ? getContainer().getEnvMap().get(PASSWORD_ENV_KEY) : null;
}

@Override
public SslBundle getSslBundle() {
return isVersion8OrGreater() ? createSslBundle() : null;
}

private SslBundle createSslBundle() {
return SslBundle.of(null, null, null, null, new SslManagerBundle() {
@Override
public KeyManagerFactory getKeyManagerFactory() {
return null;
}

@Override
public TrustManagerFactory getTrustManagerFactory() {
return null;
}

@Override
public SSLContext createSslContext(String protocol) {
return getContainer().createSslContextFromCa();
}
});
}

private boolean isVersion8OrGreater() {
DockerImageName dockerImageName = DockerImageName.parse(getContainer().getDockerImageName());
return new ComparableVersion(dockerImageName.getVersionPart()).isGreaterThanOrEqualTo("8.0.0");
Expand Down

0 comments on commit 8ea90e1

Please sign in to comment.