Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address sonar security warnings #51

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<Path> kroxyStart = new AtomicReference<>();
File file;
try (Stream<Path> 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);
Expand All @@ -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<Path> walkStream = Files.walk(targetPath)) {
final Optional<Path> 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.
*
Expand All @@ -85,9 +99,9 @@ public String getBootstrap() {
String clusterIP = null;
try {
var nis = NetworkInterface.getNetworkInterfaces();
for (Iterator<NetworkInterface> it = nis.asIterator(); it.hasNext();) {
for (Iterator<NetworkInterface> it = nis.asIterator(); it.hasNext(); ) {
var ni = it.next();
for (Iterator<InetAddress> iter = ni.getInetAddresses().asIterator(); iter.hasNext();) {
for (Iterator<InetAddress> iter = ni.getInetAddresses().asIterator(); iter.hasNext(); ) {
var i = iter.next();
if (i.getHostAddress().startsWith("10")) {
clusterIP = i.getHostAddress();
Expand All @@ -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;
}

Expand All @@ -123,4 +137,4 @@ public void stop() {
thread.interrupt();
ProcessHandle.of(pid).ifPresent(ProcessHandle::destroy);
}
}
}
Loading