-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[VMware to KVM Migration] Fix unused convert env vars #11947
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -43,6 +43,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.cloudstack.utils.security.KeyStoreUtils; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.commons.io.IOUtils; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.commons.lang3.ArrayUtils; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.logging.log4j.LogManager; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.logging.log4j.Logger; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import org.joda.time.Duration; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -236,6 +237,14 @@ static String stackTraceAsString(Throwable throwable) { | |||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| public String execute(OutputInterpreter interpreter) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| return execute(interpreter, null); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| public String execute(OutputInterpreter interpreter, String[] environment) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| return executeInternal(interpreter, environment); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| public String executeInternal(OutputInterpreter interpreter, String[] environment) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
| public String executeInternal(OutputInterpreter interpreter, String[] environment) { | |
| private String executeInternal(OutputInterpreter interpreter, String[] environment) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| public String execute(OutputInterpreter interpreter, String[] environment) { | |
| return executeInternal(interpreter, environment); | |
| } | |
| public String executeInternal(OutputInterpreter interpreter, String[] environment) { | |
| public String execute(OutputInterpreter interpreter, String[] environment) { |
no need for the extra call on the stack, that I can see
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using shell invocation may be comparatively unsafe versus ProcessBuilder. Not sure if possible to avoid this?
No log is added for this else block, may help
Javadoc for new execute overloaded method may help
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agree, co-pilots comments are giving some hints.
Copilot
AI
Nov 3, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If System.getenv(\"PATH\") returns null (which can happen in some environments), this will add "PATH=null" to the environment variables, potentially breaking command execution. Add a null check and handle the case where PATH is not defined.
| String[] env = ArrayUtils.add(environment, String.format("PATH=%s", System.getenv("PATH"))); | |
| String pathEnv = System.getenv("PATH"); | |
| String[] env = environment; | |
| if (pathEnv != null) { | |
| env = ArrayUtils.add(environment, String.format("PATH=%s", pathEnv)); | |
| } else { | |
| _logger.warn("System environment variable PATH is not set; indirect command calls may fail."); | |
| } |
Copilot
AI
Nov 3, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrapping the command in a shell (sh -c) could introduce command injection vulnerabilities if commandLine contains untrusted input. Ensure that all components of _command are properly validated or sanitized before being passed to this method, or consider using ProcessBuilder with proper environment variable support instead.
| // Since Runtime.exec() does not support redirecting the error stream, then append 2>&1 to the command | |
| String[] commands = new String[] {"sh", "-c", String.format("%s 2>&1", commandLine)}; | |
| // The PATH variable must be added for indirect calls within the running command | |
| // Example: virt-v2v invokes qemu-img, which cannot be found if PATH is not set | |
| String[] env = ArrayUtils.add(environment, String.format("PATH=%s", System.getenv("PATH"))); | |
| _process = Runtime.getRuntime().exec(commands, env, _workDir != null ? new File(_workDir) : null); | |
| // Use ProcessBuilder to set environment variables and redirect error stream safely | |
| ProcessBuilder pb = new ProcessBuilder(command); | |
| pb.redirectErrorStream(true); | |
| if (_workDir != null) | |
| pb.directory(new File(_workDir)); | |
| // Set environment variables | |
| for (String envVar : environment) { | |
| int idx = envVar.indexOf('='); | |
| if (idx > 0) { | |
| String key = envVar.substring(0, idx); | |
| String value = envVar.substring(idx + 1); | |
| pb.environment().put(key, value); | |
| } | |
| } | |
| // Ensure PATH is set | |
| pb.environment().put("PATH", System.getenv("PATH")); | |
| _process = pb.start(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The conditional check and branching can be simplified. The
execute(outputLogger)method already callsexecute(outputLogger, null), and the implementation handles null/empty arrays. You can directly callscript.execute(outputLogger, convertInstanceEnv)without the conditional check, simplifying the code.