This is an automated email from the ASF dual-hosted git repository. jamesnetherton pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git
commit 14f37828693d9e059eda8ab40412a86507ef4baa Author: Peter Palaga <[email protected]> AuthorDate: Thu Jun 3 18:55:49 2021 +0200 Make Kafka SSL tests work on Quarkus Platform --- .../kafka/sasl/KafkaSaslSslTestResource.java | 36 +++++++++---------- .../src/main/resources/application.properties | 18 ---------- .../quarkus/kafka/sasl/KafkaSaslTestResource.java | 41 ++++++++++++---------- .../quarkus/kafka/ssl/KafkaSslTestResource.java | 36 +++++++++---------- 4 files changed, 56 insertions(+), 75 deletions(-) diff --git a/integration-tests/kafka-sasl-ssl/src/test/java/org/apache/camel/quarkus/kafka/sasl/KafkaSaslSslTestResource.java b/integration-tests/kafka-sasl-ssl/src/test/java/org/apache/camel/quarkus/kafka/sasl/KafkaSaslSslTestResource.java index 3a26d62..a6f55b9 100644 --- a/integration-tests/kafka-sasl-ssl/src/test/java/org/apache/camel/quarkus/kafka/sasl/KafkaSaslSslTestResource.java +++ b/integration-tests/kafka-sasl-ssl/src/test/java/org/apache/camel/quarkus/kafka/sasl/KafkaSaslSslTestResource.java @@ -16,21 +16,21 @@ */ package org.apache.camel.quarkus.kafka.sasl; -import java.io.File; import java.io.IOException; -import java.net.URL; +import java.io.InputStream; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import java.util.Map; +import java.util.stream.Stream; import com.github.dockerjava.api.command.InspectContainerResponse; import org.apache.camel.quarkus.test.support.kafka.KafkaTestResource; import org.apache.camel.util.CollectionHelper; +import org.apache.commons.io.FileUtils; import org.testcontainers.containers.KafkaContainer; import org.testcontainers.containers.wait.strategy.Wait; import org.testcontainers.images.builder.Transferable; -import org.testcontainers.shaded.org.apache.commons.io.FileUtils; import org.testcontainers.utility.DockerImageName; import org.testcontainers.utility.MountableFile; @@ -41,24 +41,23 @@ public class KafkaSaslSslTestResource extends KafkaTestResource { private static final String KAFKA_KEYSTORE_TYPE = "PKCS12"; private static final String KAFKA_SSL_CREDS_FILE = "broker-creds"; private static final String KAFKA_TRUSTSTORE_FILE = "kafka-truststore.p12"; - private static final File TMP_DIR = Paths.get(System.getProperty("java.io.tmpdir"), "kafka").toFile(); + private Path configDir; private SaslSslKafkaContainer container; @Override public Map<String, String> start() { // Set up the SSL key / trust store directory try { - TMP_DIR.mkdirs(); - + configDir = Files.createTempDirectory("KafkaSaslSslTestResource-"); ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); - URL resource = classLoader.getResource("config"); - File serviceBindings = new File(resource.getPath()); - - for (File keyStore : serviceBindings.listFiles()) { - URL serviceBindingResource = classLoader.getResource("config/" + keyStore.getName()); - FileUtils.copyInputStreamToFile(serviceBindingResource.openStream(), - Paths.get(TMP_DIR.getPath(), keyStore.getName()).toFile()); - } + Stream.of("kafka_server_jaas.conf", KAFKA_KEYSTORE_FILE, KAFKA_TRUSTSTORE_FILE) + .forEach(fileName -> { + try (InputStream in = classLoader.getResourceAsStream("config/" + fileName)) { + Files.copy(in, configDir.resolve(fileName)); + } catch (IOException e) { + throw new RuntimeException(e); + } + }); } catch (IOException e) { throw new RuntimeException(e); } @@ -70,17 +69,16 @@ public class KafkaSaslSslTestResource extends KafkaTestResource { + "username=\"alice\" " + "password=\"alice-secret\";"; - Path keystorePath = TMP_DIR.toPath(); return CollectionHelper.mapOf( "camel.component.kafka.brokers", container.getBootstrapServers(), "camel.component.kafka.sasl-mechanism", "SCRAM-SHA-512", "camel.component.kafka.sasl-jaas-config", jaasConfig, "camel.component.kafka.security-protocol", "SASL_SSL", "camel.component.kafka.ssl-key-password", KAFKA_KEYSTORE_PASSWORD, - "camel.component.kafka.ssl-keystore-location", keystorePath.resolve(KAFKA_KEYSTORE_FILE).toString(), + "camel.component.kafka.ssl-keystore-location", configDir.resolve(KAFKA_KEYSTORE_FILE).toString(), "camel.component.kafka.ssl-keystore-password", KAFKA_KEYSTORE_PASSWORD, "camel.component.kafka.ssl-keystore-type", KAFKA_KEYSTORE_TYPE, - "camel.component.kafka.ssl-truststore-location", keystorePath.resolve(KAFKA_TRUSTSTORE_FILE).toString(), + "camel.component.kafka.ssl-truststore-location", configDir.resolve(KAFKA_TRUSTSTORE_FILE).toString(), "camel.component.kafka.ssl-truststore-password", KAFKA_KEYSTORE_PASSWORD, "camel.component.kafka.ssl-truststore-type", KAFKA_KEYSTORE_TYPE); } @@ -90,7 +88,7 @@ public class KafkaSaslSslTestResource extends KafkaTestResource { if (this.container != null) { try { this.container.stop(); - FileUtils.deleteDirectory(TMP_DIR); + FileUtils.deleteDirectory(configDir.toFile()); } catch (Exception e) { // Ignored } diff --git a/integration-tests/kafka-sasl/src/main/resources/application.properties b/integration-tests/kafka-sasl/src/main/resources/application.properties deleted file mode 100644 index c7c9f6b..0000000 --- a/integration-tests/kafka-sasl/src/main/resources/application.properties +++ /dev/null @@ -1,18 +0,0 @@ -## --------------------------------------------------------------------------- -## 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. -## --------------------------------------------------------------------------- - -quarkus.kubernetes-service-binding.root=${java.io.tmpdir}/k8s-sb diff --git a/integration-tests/kafka-sasl/src/test/java/org/apache/camel/quarkus/kafka/sasl/KafkaSaslTestResource.java b/integration-tests/kafka-sasl/src/test/java/org/apache/camel/quarkus/kafka/sasl/KafkaSaslTestResource.java index 6d9aa34..b42ac32 100644 --- a/integration-tests/kafka-sasl/src/test/java/org/apache/camel/quarkus/kafka/sasl/KafkaSaslTestResource.java +++ b/integration-tests/kafka-sasl/src/test/java/org/apache/camel/quarkus/kafka/sasl/KafkaSaslTestResource.java @@ -16,50 +16,53 @@ */ package org.apache.camel.quarkus.kafka.sasl; -import java.io.File; import java.io.IOException; -import java.net.URL; -import java.nio.file.Paths; -import java.util.Collections; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Map; +import java.util.stream.Stream; import com.github.dockerjava.api.command.InspectContainerResponse; import org.apache.camel.quarkus.test.support.kafka.KafkaTestResource; +import org.apache.camel.util.CollectionHelper; +import org.apache.commons.io.FileUtils; import org.apache.kafka.clients.CommonClientConfigs; import org.testcontainers.containers.KafkaContainer; import org.testcontainers.containers.wait.strategy.Wait; -import org.testcontainers.shaded.org.apache.commons.io.FileUtils; import org.testcontainers.utility.DockerImageName; import org.testcontainers.utility.MountableFile; public class KafkaSaslTestResource extends KafkaTestResource { - private static final File TMP_DIR = Paths.get(System.getProperty("java.io.tmpdir"), "k8s-sb", "kafka").toFile(); + private Path serviceBindingDir; private SaslKafkaContainer container; @Override public Map<String, String> start() { // Set up the service binding directory try { - TMP_DIR.mkdirs(); - + serviceBindingDir = Files.createTempDirectory("KafkaSaslTestResource-"); + final Path kafkaDir = serviceBindingDir.resolve("kafka"); + Files.createDirectories(kafkaDir); ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); - URL resource = classLoader.getResource("k8s-sb/kafka"); - File serviceBindings = new File(resource.getPath()); - - for (File serviceBinding : serviceBindings.listFiles()) { - URL serviceBindingResource = classLoader.getResource("k8s-sb/kafka/" + serviceBinding.getName()); - FileUtils.copyInputStreamToFile(serviceBindingResource.openStream(), - Paths.get(TMP_DIR.getPath(), serviceBinding.getName()).toFile()); - } + Stream.of("password", "saslMechanism", "securityProtocol", "type", "user") + .forEach(fileName -> { + try (InputStream in = classLoader.getResourceAsStream("k8s-sb/kafka/" + fileName)) { + Files.copy(in, kafkaDir.resolve(fileName)); + } catch (IOException e) { + throw new RuntimeException(e); + } + }); } catch (IOException e) { throw new RuntimeException(e); } container = new SaslKafkaContainer(KAFKA_IMAGE_NAME); container.start(); - return Collections.singletonMap("kafka." + CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, - container.getBootstrapServers()); + return CollectionHelper.mapOf( + "kafka." + CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, container.getBootstrapServers(), + "quarkus.kubernetes-service-binding.root", serviceBindingDir.toString()); } @Override @@ -67,7 +70,7 @@ public class KafkaSaslTestResource extends KafkaTestResource { if (this.container != null) { try { this.container.stop(); - FileUtils.deleteDirectory(TMP_DIR.getParentFile()); + FileUtils.deleteDirectory(serviceBindingDir.toFile()); } catch (Exception e) { // Ignored } diff --git a/integration-tests/kafka-ssl/src/test/java/org/apache/camel/quarkus/kafka/ssl/KafkaSslTestResource.java b/integration-tests/kafka-ssl/src/test/java/org/apache/camel/quarkus/kafka/ssl/KafkaSslTestResource.java index 869ec2b..130b95a 100644 --- a/integration-tests/kafka-ssl/src/test/java/org/apache/camel/quarkus/kafka/ssl/KafkaSslTestResource.java +++ b/integration-tests/kafka-ssl/src/test/java/org/apache/camel/quarkus/kafka/ssl/KafkaSslTestResource.java @@ -16,21 +16,21 @@ */ package org.apache.camel.quarkus.kafka.ssl; -import java.io.File; import java.io.IOException; -import java.net.URL; +import java.io.InputStream; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import java.util.Map; +import java.util.stream.Stream; import com.github.dockerjava.api.command.InspectContainerResponse; import org.apache.camel.quarkus.test.support.kafka.KafkaTestResource; import org.apache.camel.util.CollectionHelper; +import org.apache.commons.io.FileUtils; import org.testcontainers.containers.KafkaContainer; import org.testcontainers.containers.wait.strategy.Wait; import org.testcontainers.images.builder.Transferable; -import org.testcontainers.shaded.org.apache.commons.io.FileUtils; import org.testcontainers.utility.DockerImageName; import org.testcontainers.utility.MountableFile; @@ -41,24 +41,23 @@ public class KafkaSslTestResource extends KafkaTestResource { private static final String KAFKA_KEYSTORE_TYPE = "PKCS12"; private static final String KAFKA_SSL_CREDS_FILE = "broker-creds"; private static final String KAFKA_TRUSTSTORE_FILE = "kafka-truststore.p12"; - private static final File TMP_DIR = Paths.get(System.getProperty("java.io.tmpdir"), "kafka").toFile(); + private Path configDir; private SSLKafkaContainer container; @Override public Map<String, String> start() { // Set up the SSL key / trust store directory try { - TMP_DIR.mkdirs(); - + configDir = Files.createTempDirectory("KafkaSaslSslTestResource-"); ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); - URL resource = classLoader.getResource("config"); - File serviceBindings = new File(resource.getPath()); - - for (File keyStore : serviceBindings.listFiles()) { - URL serviceBindingResource = classLoader.getResource("config/" + keyStore.getName()); - FileUtils.copyInputStreamToFile(serviceBindingResource.openStream(), - Paths.get(TMP_DIR.getPath(), keyStore.getName()).toFile()); - } + Stream.of(KAFKA_KEYSTORE_FILE, KAFKA_TRUSTSTORE_FILE) + .forEach(fileName -> { + try (InputStream in = classLoader.getResourceAsStream("config/" + fileName)) { + Files.copy(in, configDir.resolve(fileName)); + } catch (IOException e) { + throw new RuntimeException(e); + } + }); } catch (IOException e) { throw new RuntimeException(e); } @@ -66,15 +65,14 @@ public class KafkaSslTestResource extends KafkaTestResource { container = new SSLKafkaContainer(KAFKA_IMAGE_NAME); container.start(); - Path keystorePath = TMP_DIR.toPath(); return CollectionHelper.mapOf( "camel.component.kafka.brokers", container.getBootstrapServers(), "camel.component.kafka.security-protocol", "SSL", "camel.component.kafka.ssl-key-password", KAFKA_KEYSTORE_PASSWORD, - "camel.component.kafka.ssl-keystore-location", keystorePath.resolve(KAFKA_KEYSTORE_FILE).toString(), + "camel.component.kafka.ssl-keystore-location", configDir.resolve(KAFKA_KEYSTORE_FILE).toString(), "camel.component.kafka.ssl-keystore-password", KAFKA_KEYSTORE_PASSWORD, "camel.component.kafka.ssl-keystore-type", KAFKA_KEYSTORE_TYPE, - "camel.component.kafka.ssl-truststore-location", keystorePath.resolve(KAFKA_TRUSTSTORE_FILE).toString(), + "camel.component.kafka.ssl-truststore-location", configDir.resolve(KAFKA_TRUSTSTORE_FILE).toString(), "camel.component.kafka.ssl-truststore-password", KAFKA_KEYSTORE_PASSWORD, "camel.component.kafka.ssl-truststore-type", KAFKA_KEYSTORE_TYPE); } @@ -84,7 +82,7 @@ public class KafkaSslTestResource extends KafkaTestResource { if (this.container != null) { try { this.container.stop(); - FileUtils.deleteDirectory(TMP_DIR); + FileUtils.deleteDirectory(configDir.toFile()); } catch (Exception e) { // Ignored }
