This is an automated email from the ASF dual-hosted git repository. rouazana pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit f8a5fb607bd47a479f055ea45363bb00d72fbd9d Author: Tran Tien Duc <[email protected]> AuthorDate: Tue Nov 12 16:36:58 2019 +0700 JAMES-2905 Expand ClientProvider tests with Auth ES --- ...iderImplConnectionAuthESIgnoreSSLCheckTest.java | 44 +++++++++++++ ...ImplConnectionAuthESOverrideTrustStoreTest.java | 46 +++++++++++++ ...a => ClientProviderImplConnectionContract.java} | 75 ++++++---------------- .../ClientProviderImplConnectionNoAuthESTest.java | 31 +++++++++ .../es/DockerAuthElasticSearchSingleton.java | 28 ++++++++ 5 files changed, 169 insertions(+), 55 deletions(-) diff --git a/backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/ClientProviderImplConnectionAuthESIgnoreSSLCheckTest.java b/backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/ClientProviderImplConnectionAuthESIgnoreSSLCheckTest.java new file mode 100644 index 0000000..1de1996 --- /dev/null +++ b/backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/ClientProviderImplConnectionAuthESIgnoreSSLCheckTest.java @@ -0,0 +1,44 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.backends.es; + +import static org.apache.james.backends.es.ElasticSearchClusterExtension.ElasticSearchCluster; + +import java.util.Optional; + +import org.apache.james.backends.es.ElasticSearchConfiguration.HostScheme; +import org.apache.james.backends.es.ElasticSearchConfiguration.SSLTrustConfiguration; +import org.junit.jupiter.api.extension.RegisterExtension; + +class ClientProviderImplConnectionAuthESIgnoreSSLCheckTest implements ClientProviderImplConnectionContract { + + @RegisterExtension + static ElasticSearchClusterExtension extension = new ElasticSearchClusterExtension(new ElasticSearchCluster( + DockerAuthElasticSearchSingleton.INSTANCE, + new DockerElasticSearch.WithAuth())); + + @Override + public ElasticSearchConfiguration.Builder configurationBuilder() { + return ElasticSearchConfiguration.builder() + .credential(Optional.of(DockerElasticSearch.WithAuth.DEFAULT_CREDENTIAL)) + .hostScheme(Optional.of(HostScheme.HTTPS)) + .sslTrustConfiguration(SSLTrustConfiguration.ignore()); + } +} diff --git a/backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/ClientProviderImplConnectionAuthESOverrideTrustStoreTest.java b/backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/ClientProviderImplConnectionAuthESOverrideTrustStoreTest.java new file mode 100644 index 0000000..5b92cf7 --- /dev/null +++ b/backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/ClientProviderImplConnectionAuthESOverrideTrustStoreTest.java @@ -0,0 +1,46 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.backends.es; + +import java.util.Optional; + +import org.apache.james.backends.es.ElasticSearchConfiguration.SSLTrustConfiguration; +import org.apache.james.backends.es.ElasticSearchConfiguration.SSLTrustConfiguration.SSLTrustStore; +import org.junit.jupiter.api.extension.RegisterExtension; + +public class ClientProviderImplConnectionAuthESOverrideTrustStoreTest implements ClientProviderImplConnectionContract { + + private static final String TRUST_STORE_PASSWORD = "mypass"; + private static final String TRUST_STORE_FILE_PATH = "src/test/resources/auth-es/server.jks"; + + @RegisterExtension + static ElasticSearchClusterExtension extension = new ElasticSearchClusterExtension(new ElasticSearchClusterExtension.ElasticSearchCluster( + DockerAuthElasticSearchSingleton.INSTANCE, + new DockerElasticSearch.WithAuth())); + + @Override + public ElasticSearchConfiguration.Builder configurationBuilder() { + return ElasticSearchConfiguration.builder() + .credential(Optional.of(DockerElasticSearch.WithAuth.DEFAULT_CREDENTIAL)) + .hostScheme(Optional.of(ElasticSearchConfiguration.HostScheme.HTTPS)) + .sslTrustConfiguration(SSLTrustConfiguration.override( + SSLTrustStore.of(TRUST_STORE_FILE_PATH, TRUST_STORE_PASSWORD))); + } +} \ No newline at end of file diff --git a/backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/ClientProviderImplConnectionTest.java b/backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/ClientProviderImplConnectionContract.java similarity index 56% rename from backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/ClientProviderImplConnectionTest.java rename to backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/ClientProviderImplConnectionContract.java index db6ae42..05a7ec4 100644 --- a/backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/ClientProviderImplConnectionTest.java +++ b/backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/ClientProviderImplConnectionContract.java @@ -21,61 +21,25 @@ package org.apache.james.backends.es; import java.util.concurrent.TimeUnit; -import org.apache.james.util.Host; -import org.apache.james.util.docker.DockerContainer; -import org.apache.james.util.docker.Images; +import org.apache.james.backends.es.ElasticSearchClusterExtension.ElasticSearchCluster; import org.awaitility.Awaitility; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.builder.SearchSourceBuilder; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -class ClientProviderImplConnectionTest { - private static final Logger LOGGER = LoggerFactory.getLogger(ClientProviderImplConnectionTest.class); - private static final int ES_APPLICATIVE_PORT = 9200; +interface ClientProviderImplConnectionContract { - static DockerContainer es1 = DockerContainer.fromName(Images.ELASTICSEARCH_6) - .withEnv("discovery.type", "single-node") - .withAffinityToContainer() - .withExposedPorts(ES_APPLICATIVE_PORT); - - DockerContainer es2 = DockerContainer.fromName(Images.ELASTICSEARCH_6) - .withEnv("discovery.type", "single-node") - .withAffinityToContainer() - .withExposedPorts(ES_APPLICATIVE_PORT); - - @BeforeAll - static void setUpClass() { - es1.start(); - } - - @BeforeEach - void setUp() { - es2.start(); - } - - @AfterEach - void tearDown() { - es2.stop(); - } - - @AfterAll - static void tearDownClass() { - es1.stop(); - } + Logger LOGGER = LoggerFactory.getLogger(ClientProviderImplConnectionContract.class); @Test - void connectingASingleServerShouldWork() { - ElasticSearchConfiguration configuration = ElasticSearchConfiguration.builder() - .addHost(Host.from(es1.getContainerIp(), ES_APPLICATIVE_PORT)) + default void connectingASingleServerShouldWork(ElasticSearchCluster esCluster) { + ElasticSearchConfiguration configuration = configurationBuilder() + .addHost(esCluster.es1.getHttpHost()) .build(); Awaitility.await() @@ -85,10 +49,9 @@ class ClientProviderImplConnectionTest { } @Test - void connectingAClusterShouldWork() { - ElasticSearchConfiguration configuration = ElasticSearchConfiguration.builder() - .addHost(Host.from(es1.getContainerIp(), ES_APPLICATIVE_PORT)) - .addHost(Host.from(es2.getContainerIp(), ES_APPLICATIVE_PORT)) + default void connectingAClusterShouldWork(ElasticSearchCluster esCluster) { + ElasticSearchConfiguration configuration = configurationBuilder() + .addHosts(esCluster.getHosts()) .build(); Awaitility.await() @@ -98,23 +61,20 @@ class ClientProviderImplConnectionTest { } @Test - void connectingAClusterWithAFailedNodeShouldWork() { - String es1Ip = es1.getContainerIp(); - String es2Ip = es2.getContainerIp(); - es2.stop(); - - ElasticSearchConfiguration configuration = ElasticSearchConfiguration.builder() - .addHost(Host.from(es1Ip, ES_APPLICATIVE_PORT)) - .addHost(Host.from(es2Ip, ES_APPLICATIVE_PORT)) + default void connectingAClusterWithAFailedNodeShouldWork(ElasticSearchCluster esCluster) { + ElasticSearchConfiguration configuration = configurationBuilder() + .addHosts(esCluster.getHosts()) .build(); + esCluster.es2.stop(); + Awaitility.await() .atMost(1, TimeUnit.MINUTES) .pollInterval(5, TimeUnit.SECONDS) .until(() -> isConnected(new ClientProvider(configuration))); } - private boolean isConnected(ClientProvider clientProvider) { + default boolean isConnected(ClientProvider clientProvider) { try (RestHighLevelClient client = clientProvider.get()) { client.search( new SearchRequest() @@ -126,4 +86,9 @@ class ClientProviderImplConnectionTest { return false; } } + + default ElasticSearchConfiguration.Builder configurationBuilder() { + return ElasticSearchConfiguration.builder(); + } } + diff --git a/backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/ClientProviderImplConnectionNoAuthESTest.java b/backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/ClientProviderImplConnectionNoAuthESTest.java new file mode 100644 index 0000000..6fa92d3 --- /dev/null +++ b/backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/ClientProviderImplConnectionNoAuthESTest.java @@ -0,0 +1,31 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.backends.es; + +import org.apache.james.backends.es.ElasticSearchClusterExtension.ElasticSearchCluster; +import org.junit.jupiter.api.extension.RegisterExtension; + +class ClientProviderImplConnectionNoAuthESTest implements ClientProviderImplConnectionContract { + + @RegisterExtension + static ElasticSearchClusterExtension extension = new ElasticSearchClusterExtension(new ElasticSearchCluster( + DockerElasticSearchSingleton.INSTANCE, + new DockerElasticSearch.NoAuth())); +} diff --git a/backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/DockerAuthElasticSearchSingleton.java b/backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/DockerAuthElasticSearchSingleton.java new file mode 100644 index 0000000..5346ef4 --- /dev/null +++ b/backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/DockerAuthElasticSearchSingleton.java @@ -0,0 +1,28 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.backends.es; + +public class DockerAuthElasticSearchSingleton { + public static DockerElasticSearch INSTANCE = new DockerElasticSearch.WithAuth(); + + static { + INSTANCE.start(); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
