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

Get error from helm wrapper #534

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 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 @@ -188,11 +188,13 @@ public HelmInstaller installChart(
if (reuseValues) {
command.append(" --reuse-values");
}
String res =
Command.executeAndGetResponseAsJson(configuration, command.toString())
.getOutput()
.getString();
return new ObjectMapper().readValue(res, HelmInstaller.class);
Command.ProcessResultWithError result =
Command.executeAndGetResponseAsJson(configuration, command.toString());
if (result.getProcessResult().getExitValue() != 0) {
throw new RuntimeException(result.getError());
}
return new ObjectMapper()
.readValue(result.getProcessResult().getOutput().getString(), HelmInstaller.class);
}

public int uninstaller(HelmConfiguration configuration, String name, String namespace)
Expand All @@ -201,7 +203,7 @@ public int uninstaller(HelmConfiguration configuration, String name, String name
safeConcat(command, name);
command.append(" -n ");
safeConcat(command, namespace);
return Command.execute(configuration, command.toString()).getExitValue();
return Command.execute(configuration, command.toString()).getProcessResult().getExitValue();
}

public HelmLs[] listChartInstall(HelmConfiguration configuration, String namespace)
Expand All @@ -214,6 +216,7 @@ public HelmLs[] listChartInstall(HelmConfiguration configuration, String namespa
return new ObjectMapper()
.readValue(
Command.executeAndGetResponseAsJson(configuration, command.toString())
.getProcessResult()
.getOutput()
.getString(),
HelmLs[].class);
Expand All @@ -238,7 +241,10 @@ public HelmReleaseInfo getAll(HelmConfiguration configuration, String id, String
safeConcat(command, namespace);
try {
String unparsedReleaseInfo =
Command.execute(configuration, command.toString()).getOutput().getString();
Command.execute(configuration, command.toString())
.getProcessResult()
.getOutput()
.getString();
return helmReleaseInfoParser.parseReleaseInfo(unparsedReleaseInfo);
} catch (IOException | InterruptedException | TimeoutException e) {
LOGGER.warn("Exception occurred", e);
Expand All @@ -259,14 +265,19 @@ private String getReleaseInfo(
safeConcat(command, namespace);
if (infoType.equals(NOTES_INFO_TYPE)) {
return Command.executeAndGetResponseAsRaw(configuration, command.toString())
.getProcessResult()
.getOutput()
.getString();
} else if (infoType.equals(VALUES_INFO_TYPE)) {
return Command.executeAndGetResponseAsJson(configuration, command.toString())
.getProcessResult()
.getOutput()
.getString();
} else {
return Command.execute(configuration, command.toString()).getOutput().getString();
return Command.execute(configuration, command.toString())
.getProcessResult()
.getOutput()
.getString();
}
} catch (IOException | InterruptedException | TimeoutException e) {
LOGGER.warn("Exception occurred", e);
Expand Down Expand Up @@ -302,6 +313,7 @@ public HelmLs getAppById(HelmConfiguration configuration, String appId, String n
.readValue(
Command.executeAndGetResponseAsJson(
configuration, command.toString())
.getProcessResult()
.getOutput()
.getString(),
HelmLs[].class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public HelmRepo[] getHelmRepo()
new ObjectMapper()
.readValue(
Command.executeAndGetResponseAsJson("helm search repo")
.getProcessResult()
.getOutput()
.getString(StandardCharsets.UTF_8.name()),
HelmRepo[].class);
Expand All @@ -39,7 +40,7 @@ public String addHelmRepo(
"--ca-file " + System.getenv("CACERTS_DIR") + "/" + caFile + " ");
}
command = command.concat(nomRepo + " " + url);
return Command.execute(command).getOutput().getString();
return Command.execute(command).getProcessResult().getOutput().getString();
}

public void repoUpdate() throws InterruptedException, TimeoutException, IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class HelmVersionService {
public String getVersion()
throws InvalidExitValueException, IOException, InterruptedException, TimeoutException {
return Command.executeAndGetResponseAsRaw("helm version --template={{.Version}}")
.getProcessResult()
.getOutput()
.getString(StandardCharsets.UTF_8.name());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package io.github.inseefrlab.helmwrapper.utils;

import io.github.inseefrlab.helmwrapper.configuration.HelmConfiguration;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;
Expand All @@ -22,9 +25,11 @@
"^[ ]*[a-z0-9]([-a-z0-9 ]*[a-z0-9 ])?(\\.[a-z0-9 ]([-a-z0-9 ]*[a-z0-9 ])?)*[ ]*$");
private static final Logger LOGGER = LoggerFactory.getLogger(Command.class);

private static ProcessExecutor getProcessExecutor() {
private static ProcessExecutor getProcessExecutor(OutputStream errorOutputStream) {
ProcessExecutor processExecutor = new ProcessExecutor();
processExecutor.redirectError(System.err);
if (errorOutputStream != null) {
processExecutor.redirectError(errorOutputStream);
}
processExecutor.readOutput(true);
processExecutor.addListener(
new ProcessListener() {
Expand All @@ -37,47 +42,81 @@
return processExecutor;
}

public static ProcessResult executeAndGetResponseAsJson(
public static ProcessResultWithError executeAndGetResponseAsJson(
HelmConfiguration helmConfiguration, String command)
throws InvalidExitValueException, IOException, InterruptedException, TimeoutException {
return getProcessExecutor()
.environment(getEnv(helmConfiguration))
.commandSplit(addConfigToCommand(command, helmConfiguration) + " --output json")
.execute();
ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
ProcessResult processResult =
getProcessExecutor(errorStream)
.environment(getEnv(helmConfiguration))
.commandSplit(
addConfigToCommand(command, helmConfiguration) + " --output json")
Comment on lines +50 to +53

Check notice

Code scanning / SonarCloud

OS commands should not be vulnerable to argument injection attacks Low

Change this code to not construct OS command arguments from user-controlled data. See more on SonarQube Cloud
.execute();
return new ProcessResultWithError(processResult, errorStream);
}

public static ProcessResult executeAndGetResponseAsJson(String command)
public static ProcessResultWithError executeAndGetResponseAsJson(String command)
throws InvalidExitValueException, IOException, InterruptedException, TimeoutException {
return executeAndGetResponseAsJson(null, command);
}

public static ProcessResult executeAndGetResponseAsRaw(
public static ProcessResultWithError executeAndGetResponseAsRaw(
HelmConfiguration helmConfiguration, String command)
throws InvalidExitValueException, IOException, InterruptedException, TimeoutException {
return getProcessExecutor()
.environment(getEnv(helmConfiguration))
.commandSplit(addConfigToCommand(command, helmConfiguration))
.execute();
ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
ProcessResult processResult =
getProcessExecutor(errorStream)
.environment(getEnv(helmConfiguration))
.commandSplit(addConfigToCommand(command, helmConfiguration))
.execute();
return new ProcessResultWithError(processResult, errorStream);
}

public static ProcessResult executeAndGetResponseAsRaw(String command)
public static ProcessResultWithError executeAndGetResponseAsRaw(String command)
throws InvalidExitValueException, IOException, InterruptedException, TimeoutException {
return executeAndGetResponseAsRaw(null, command);
}

public static ProcessResult execute(HelmConfiguration helmConfiguration, String command)
public static ProcessResultWithError execute(
HelmConfiguration helmConfiguration, String command)
throws InvalidExitValueException, IOException, InterruptedException, TimeoutException {
return getProcessExecutor()
.environment(getEnv(helmConfiguration))
.commandSplit(addConfigToCommand(command, helmConfiguration))
.execute();
ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
ProcessResult processResult =
getProcessExecutor(errorStream)
.environment(getEnv(helmConfiguration))
.commandSplit(addConfigToCommand(command, helmConfiguration))
Comment on lines +85 to +87

Check notice

Code scanning / SonarCloud

OS commands should not be vulnerable to argument injection attacks Low

Change this code to not construct OS command arguments from user-controlled data. See more on SonarQube Cloud
.execute();
return new ProcessResultWithError(processResult, errorStream);
}

public static ProcessResult execute(String command)
public static ProcessResultWithError execute(String command)
throws InvalidExitValueException, IOException, InterruptedException, TimeoutException {
return execute(null, command);
}

public static class ProcessResultWithError {

private ProcessResult processResult;
private String error = null;

public ProcessResultWithError() {}

public ProcessResultWithError(ProcessResult processResult, ByteArrayOutputStream boas) {
this.processResult = processResult;
if (boas != null) {
error = boas.toString(Charset.defaultCharset());
}
}

public ProcessResult getProcessResult() {
return processResult;
}

public String getError() {
return error;
}
}

private static Map<String, String> getEnv(HelmConfiguration helmConfiguration) {
Map<String, String> env = new HashMap<>();
if (System.getProperty("http.proxyHost") != null) {
Expand Down
Loading