-
Notifications
You must be signed in to change notification settings - Fork 29
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
olevitt
wants to merge
4
commits into
main
Choose a base branch
from
get-error-from-helm
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+82
−30
Draft
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -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() { | ||
|
@@ -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") | ||
.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) { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / SonarCloud
OS commands should not be vulnerable to argument injection attacks Low