From 4ced80c8afa4573fad490cded8547fa9f75358d7 Mon Sep 17 00:00:00 2001 From: Sam Barker Date: Thu, 9 Nov 2023 11:43:48 +0100 Subject: [PATCH] Address sonar security warnings Signed-off-by: Sam Barker rh-pre-commit.version: 2.0.1 rh-pre-commit.check-secrets: ENABLED --- .../kroxylicious/KroxyliciousApp.java | 64 +++++++++++-------- 1 file changed, 39 insertions(+), 25 deletions(-) diff --git a/kroxylicious-systemtests/src/main/java/io/kroxylicious/systemtests/installation/kroxylicious/KroxyliciousApp.java b/kroxylicious-systemtests/src/main/java/io/kroxylicious/systemtests/installation/kroxylicious/KroxyliciousApp.java index 76e1f690d2..c78b658f1a 100644 --- a/kroxylicious-systemtests/src/main/java/io/kroxylicious/systemtests/installation/kroxylicious/KroxyliciousApp.java +++ b/kroxylicious-systemtests/src/main/java/io/kroxylicious/systemtests/installation/kroxylicious/KroxyliciousApp.java @@ -6,7 +6,6 @@ package io.kroxylicious.systemtests.installation.kroxylicious; -import java.io.File; import java.io.IOException; import java.net.InetAddress; import java.net.NetworkInterface; @@ -15,7 +14,7 @@ import java.nio.file.Path; import java.util.Iterator; import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicReference; +import java.util.Optional; import java.util.stream.Stream; import org.slf4j.Logger; @@ -48,24 +47,12 @@ public KroxyliciousApp(String clusterIp) { public void run() { LOGGER.info("Launching kroxylicious app"); - Path path = Path.of(System.getProperty("user.dir")).getParent(); - File dir = new File(path.toString() + File.separator + "kroxylicious-app/target"); - AtomicReference kroxyStart = new AtomicReference<>(); - File file; - try (Stream walkStream = Files.walk(dir.toPath())) { - walkStream.filter(p -> p.toFile().isFile()).forEach(f -> { - if (f.toString().endsWith("kroxylicious-start.sh")) { - kroxyStart.set(f); - } - }); - file = File.createTempFile("config", ".yaml"); - Files.writeString(file.toPath(), KroxyConfigTemplates.getDefaultExternalKroxyConfigMap(clusterIp)); - file.deleteOnExit(); - } - catch (IOException e) { - throw new RuntimeException(e); - } - pid = Exec.execWithoutWait(kroxyStart.toString(), "-c", file.getAbsolutePath()); + Path parentPath = Path.of(System.getProperty("user.dir")).getParent(); + final Path targetPath = parentPath.resolve("kroxylicious-app").resolve("target"); + + final Path startScript = resolveStartScript(targetPath); + final Path configFile = generateKroxyliciousConfiguration(); + pid = Exec.execWithoutWait(startScript.toAbsolutePath().toString(), "-c", configFile.toAbsolutePath().toString()); while (!thread.isInterrupted()) { try { Thread.sleep(1000); @@ -76,6 +63,33 @@ public void run() { } } + private Path generateKroxyliciousConfiguration() { + try { + final Path configDir = Files.createTempDirectory("kroxylicious-app"); + final Path configFile = Files.writeString(configDir.resolve("config.yaml"), KroxyConfigTemplates.getDefaultExternalKroxyConfigMap(clusterIp)); + configFile.toFile().deleteOnExit(); + return configFile; + } + catch (IOException e) { + throw new IllegalStateException("Unable to generate kroxylicious configuration file", e); + } + } + + private static Path resolveStartScript(Path targetPath) { + try (Stream walkStream = Files.walk(targetPath)) { + final Optional startScript = walkStream.filter(Files::isRegularFile).filter(f -> f.endsWith("kroxylicious-start.sh")).findFirst(); + if (startScript.isEmpty()) { + throw new IllegalStateException("unable to find kroxylicious-start.sh"); + } + else { + return startScript.get(); + } + } + catch (IOException e) { + throw new IllegalStateException("unable to find kroxylicious-start.sh", e); + } + } + /** * Gets bootstrap. * @@ -85,9 +99,9 @@ public String getBootstrap() { String clusterIP = null; try { var nis = NetworkInterface.getNetworkInterfaces(); - for (Iterator it = nis.asIterator(); it.hasNext();) { + for (Iterator it = nis.asIterator(); it.hasNext(); ) { var ni = it.next(); - for (Iterator iter = ni.getInetAddresses().asIterator(); iter.hasNext();) { + for (Iterator iter = ni.getInetAddresses().asIterator(); iter.hasNext(); ) { var i = iter.next(); if (i.getHostAddress().startsWith("10")) { clusterIP = i.getHostAddress(); @@ -96,10 +110,10 @@ public String getBootstrap() { } } catch (SocketException e) { - throw new RuntimeException(e); + throw new IllegalStateException("unable to determine bootstrap address", e); } String bootstrap = clusterIP + ":9292"; - LOGGER.debug("Kroxylicious bootstrap: " + bootstrap); + LOGGER.debug("Kroxylicious bootstrap: {}", bootstrap); return bootstrap; } @@ -123,4 +137,4 @@ public void stop() { thread.interrupt(); ProcessHandle.of(pid).ifPresent(ProcessHandle::destroy); } -} +} \ No newline at end of file