-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix that log sensitive infomation in cmd of script #12024
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
Open
YLChen-007
wants to merge
11
commits into
apache:4.20
Choose a base branch
from
YLChen-007:my-fix-420-mask-script-cmd-sensitive-info
base: 4.20
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.
+95
−42
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5bae182
fix that log sensitive infomation in cmd of script
cyl-auth f9a05f5
Remove unnecessary line break in Script.java
YLChen-007 7dc5b2b
Update utils/src/main/java/com/cloud/utils/script/Script.java
YLChen-007 d15379b
Refactor logging in Script class to simplify handling of sensitive ar…
cyl-auth 30fed1a
Merge branch 'my-fix-420-mask-script-cmd-sensitive-info' of https://g…
cyl-auth 64a6547
Improve command logging in Script class to include full command line …
cyl-auth a617571
Remove unused _passwordCommand flag from Script class to simplify code
cyl-auth 2468400
Update utils/src/main/java/com/cloud/utils/script/Script.java
YLChen-007 45a66f0
Remove unused import for KeyStoreUtils
YLChen-007 1988400
Merge branch '4.20' into my-fix-420-mask-script-cmd-sensitive-info
DaanHoogland c6ee04f
Update utils/src/main/java/com/cloud/utils/script/Script.java
DaanHoogland 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -30,8 +30,10 @@ | |
| import java.net.URL; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Properties; | ||
| import java.util.Set; | ||
| import java.util.concurrent.Callable; | ||
| import java.util.concurrent.ExecutionException; | ||
| import java.util.concurrent.Executors; | ||
|
|
@@ -43,7 +45,6 @@ | |
| import java.util.concurrent.atomic.AtomicReference; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import org.apache.cloudstack.utils.security.KeyStoreUtils; | ||
| import org.apache.commons.collections.CollectionUtils; | ||
| import org.apache.commons.io.IOUtils; | ||
| import org.apache.logging.log4j.LogManager; | ||
|
|
@@ -66,8 +67,8 @@ public class Script implements Callable<String> { | |
| private static final int DEFAULT_TIMEOUT = 3600 * 1000; /* 1 hour */ | ||
| private volatile boolean _isTimeOut = false; | ||
|
|
||
| private boolean _passwordCommand = false; | ||
| private boolean avoidLoggingCommand = false; | ||
| private final Set<Integer> sensitiveArgIndices = new HashSet<>(); | ||
|
|
||
| private static final ScheduledExecutorService s_executors = Executors.newScheduledThreadPool(10, new NamedThreadFactory("Script")); | ||
|
|
||
|
|
@@ -145,6 +146,11 @@ public void add(String param) { | |
| _command.add(param); | ||
| } | ||
|
|
||
| public void addSensitive(String param) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how about defining this as add(String param, boolean sensitive).
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like your solution but let’s do it in a new PR and merge this one when verified. |
||
| _command.add(param); | ||
| sensitiveArgIndices.add(_command.size() - 1); | ||
| } | ||
|
|
||
| public Script set(String name, String value) { | ||
| _command.add(name); | ||
| _command.add(value); | ||
|
|
@@ -163,7 +169,7 @@ protected String buildCommandLine(String[] command) { | |
| if (sanitizeViCmdParameter(cmd, builder) || sanitizeRbdFileFormatCmdParameter(cmd, builder)) { | ||
| continue; | ||
| } | ||
| if (obscureParam) { | ||
| if (obscureParam || sensitiveArgIndices.contains(i)) { | ||
| builder.append("******").append(" "); | ||
| obscureParam = false; | ||
| } else { | ||
|
|
@@ -172,7 +178,6 @@ protected String buildCommandLine(String[] command) { | |
|
|
||
| if ("-y".equals(cmd) || "-z".equals(cmd)) { | ||
| obscureParam = true; | ||
| _passwordCommand = true; | ||
| } | ||
| } | ||
| return builder.toString(); | ||
|
|
@@ -240,8 +245,8 @@ static String stackTraceAsString(Throwable throwable) { | |
| public String execute(OutputInterpreter interpreter) { | ||
| String[] command = _command.toArray(new String[_command.size()]); | ||
| String commandLine = buildCommandLine(command); | ||
| if (_logger.isDebugEnabled() && !avoidLoggingCommand) { | ||
| _logger.debug(String.format("Executing command [%s].", commandLine.split(KeyStoreUtils.KS_FILENAME)[0])); | ||
| if (_logger.isDebugEnabled() ) { | ||
| _logger.debug(String.format("Executing command [%s].", commandLine)); | ||
| } | ||
|
|
||
| try { | ||
|
|
@@ -263,48 +268,62 @@ public String execute(OutputInterpreter interpreter) { | |
| _thread = Thread.currentThread(); | ||
| ScheduledFuture<String> future = null; | ||
| if (_timeout > 0) { | ||
| _logger.trace(String.format("Scheduling the execution of command [%s] with a timeout of [%s] milliseconds.", commandLine, _timeout)); | ||
| _logger.trace(String.format( | ||
| "Scheduling the execution of command [%s] with a timeout of [%s] milliseconds.", | ||
| commandLine, _timeout)); | ||
| future = s_executors.schedule(this, _timeout, TimeUnit.MILLISECONDS); | ||
| } | ||
|
|
||
| long processPid = _process.pid(); | ||
| Task task = null; | ||
| if (interpreter != null && interpreter.drain()) { | ||
| _logger.trace(String.format("Executing interpreting task of process [%s] for command [%s].", processPid, commandLine)); | ||
| _logger.trace(String.format("Executing interpreting task of process [%s] for command [%s].", | ||
| processPid, commandLine)); | ||
| task = new Task(interpreter, ir); | ||
| s_executors.execute(task); | ||
| } | ||
|
|
||
| while (true) { | ||
| _logger.trace(String.format("Attempting process [%s] execution for command [%s] with timeout [%s].", processPid, commandLine, _timeout)); | ||
| _logger.trace(String.format("Attempting process [%s] execution for command [%s] with timeout [%s].", | ||
| processPid, commandLine, _timeout)); | ||
| try { | ||
| if (_process.waitFor(_timeout, TimeUnit.MILLISECONDS)) { | ||
| _logger.trace(String.format("Process [%s] execution for command [%s] completed within timeout period [%s].", processPid, commandLine, | ||
| _logger.trace(String.format( | ||
| "Process [%s] execution for command [%s] completed within timeout period [%s].", | ||
| processPid, commandLine, | ||
| _timeout)); | ||
| if (_process.exitValue() == 0) { | ||
| _logger.debug(String.format("Successfully executed process [%s] for command [%s].", processPid, commandLine)); | ||
| _logger.debug(String.format("Successfully executed process [%s] for command [%s].", | ||
| processPid, commandLine)); | ||
| if (interpreter != null) { | ||
| if (interpreter.drain()) { | ||
| _logger.trace(String.format("Returning task result of process [%s] for command [%s].", processPid, commandLine)); | ||
| _logger.trace( | ||
| String.format("Returning task result of process [%s] for command [%s].", | ||
| processPid, commandLine)); | ||
| return task.getResult(); | ||
| } | ||
| _logger.trace(String.format("Returning interpretation of process [%s] for command [%s].", processPid, commandLine)); | ||
| _logger.trace( | ||
| String.format("Returning interpretation of process [%s] for command [%s].", | ||
| processPid, commandLine)); | ||
| return interpreter.interpret(ir); | ||
| } else { | ||
| // null return exitValue apparently | ||
| _logger.trace(String.format("Process [%s] for command [%s] exited with value [%s].", processPid, commandLine, | ||
| _logger.trace(String.format("Process [%s] for command [%s] exited with value [%s].", | ||
| processPid, commandLine, | ||
| _process.exitValue())); | ||
| return String.valueOf(_process.exitValue()); | ||
| } | ||
| } else { | ||
| _logger.warn(String.format("Execution of process [%s] for command [%s] failed.", processPid, commandLine)); | ||
| _logger.warn(String.format("Execution of process [%s] for command [%s] failed.", | ||
| processPid, commandLine)); | ||
| break; | ||
| } | ||
| } | ||
| } catch (InterruptedException e) { | ||
| if (!_isTimeOut) { | ||
| _logger.debug(String.format("Exception [%s] occurred; however, it was not a timeout. Therefore, proceeding with the execution of process [%s] for command " | ||
| + "[%s].", e.getMessage(), processPid, commandLine), e); | ||
| _logger.debug(String.format( | ||
| "Exception [%s] occurred; however, it was not a timeout. Therefore, proceeding with the execution of process [%s] for command [%s].", | ||
| e.getMessage(), processPid, commandLine), e); | ||
| continue; | ||
| } | ||
| } finally { | ||
|
|
@@ -317,18 +336,17 @@ public String execute(OutputInterpreter interpreter) { | |
| TimedOutLogger log = new TimedOutLogger(_process); | ||
| Task timedoutTask = new Task(log, ir); | ||
|
|
||
| _logger.trace(String.format("Running timed out task of process [%s] for command [%s].", processPid, commandLine)); | ||
| _logger.trace(String.format("Running timed out task of process [%s] for command [%s].", processPid, | ||
| commandLine)); | ||
| timedoutTask.run(); | ||
| if (!_passwordCommand) { | ||
| _logger.warn(String.format("Process [%s] for command [%s] timed out. Output is [%s].", processPid, commandLine, timedoutTask.getResult())); | ||
| } else { | ||
| _logger.warn(String.format("Process [%s] for command [%s] timed out.", processPid, commandLine)); | ||
| } | ||
| _logger.warn(String.format("Process [%s] for command [%s] timed out. Output is [%s].", processPid, | ||
| commandLine, timedoutTask.getResult())); | ||
|
|
||
| return ERR_TIMEOUT; | ||
| } | ||
|
|
||
| _logger.debug(String.format("Exit value of process [%s] for command [%s] is [%s].", processPid, commandLine, _process.exitValue())); | ||
| _logger.debug(String.format("Exit value of process [%s] for command [%s] is [%s].", processPid, | ||
| commandLine, _process.exitValue())); | ||
|
|
||
| BufferedReader reader = new BufferedReader(new InputStreamReader(_process.getInputStream()), 128); | ||
|
|
||
|
|
@@ -339,19 +357,24 @@ public String execute(OutputInterpreter interpreter) { | |
| error = String.valueOf(_process.exitValue()); | ||
| } | ||
|
|
||
| _logger.warn(String.format("Process [%s] for command [%s] encountered the error: [%s].", processPid, commandLine, error)); | ||
| _logger.warn(String.format("Process [%s] for command [%s] encountered the error: [%s].", processPid, | ||
| commandLine, error)); | ||
|
|
||
| return error; | ||
| } catch (SecurityException ex) { | ||
| _logger.warn(String.format("Exception [%s] occurred. This may be due to an attempt of executing command [%s] as non root.", ex.getMessage(), commandLine), | ||
| _logger.warn(String.format( | ||
| "Exception [%s] occurred. This may be due to an attempt of executing command [%s] as non root.", | ||
| ex.getMessage(), commandLine), | ||
| ex); | ||
| return stackTraceAsString(ex); | ||
| } catch (Exception ex) { | ||
| _logger.warn(String.format("Exception [%s] occurred when attempting to run command [%s].", ex.getMessage(), commandLine), ex); | ||
| _logger.warn(String.format("Exception [%s] occurred when attempting to run command [%s].", | ||
| ex.getMessage(), commandLine), ex); | ||
| return stackTraceAsString(ex); | ||
| } finally { | ||
| if (_process != null) { | ||
| _logger.trace(String.format("Destroying process [%s] for command [%s].", _process.pid(), commandLine)); | ||
| _logger.trace( | ||
| String.format("Destroying process [%s] for command [%s].", _process.pid(), commandLine)); | ||
| IOUtils.closeQuietly(_process.getErrorStream()); | ||
| IOUtils.closeQuietly(_process.getOutputStream()); | ||
| IOUtils.closeQuietly(_process.getInputStream()); | ||
|
|
@@ -362,9 +385,10 @@ public String execute(OutputInterpreter interpreter) { | |
|
|
||
| public String executeIgnoreExitValue(OutputInterpreter interpreter, int exitValue) { | ||
| String[] command = _command.toArray(new String[_command.size()]); | ||
| String commandLine = buildCommandLine(command); | ||
|
|
||
| if (_logger.isDebugEnabled()) { | ||
| _logger.debug(String.format("Executing: %s", buildCommandLine(command).split(KeyStoreUtils.KS_FILENAME)[0])); | ||
| _logger.debug(String.format("Executing: %s", commandLine)); | ||
| } | ||
|
|
||
| try { | ||
|
|
@@ -375,7 +399,7 @@ public String executeIgnoreExitValue(OutputInterpreter interpreter, int exitValu | |
|
|
||
| _process = pb.start(); | ||
| if (_process == null) { | ||
| _logger.warn(String.format("Unable to execute: %s", buildCommandLine(command))); | ||
| _logger.warn(String.format("Unable to execute: %s", commandLine)); | ||
| return String.format("Unable to execute the command: %s", command[0]); | ||
| } | ||
|
|
||
|
|
@@ -439,11 +463,8 @@ public String executeIgnoreExitValue(OutputInterpreter interpreter, int exitValu | |
| Task timedoutTask = new Task(log, ir); | ||
|
|
||
| timedoutTask.run(); | ||
| if (!_passwordCommand) { | ||
| _logger.warn(String.format("Timed out: %s. Output is: %s", buildCommandLine(command), timedoutTask.getResult())); | ||
| } else { | ||
| _logger.warn(String.format("Timed out: %s", buildCommandLine(command))); | ||
| } | ||
| _logger.warn(String.format("Timed out: %s. Output is: %s", commandLine, | ||
| timedoutTask.getResult())); | ||
YLChen-007 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return ERR_TIMEOUT; | ||
| } | ||
|
|
@@ -467,7 +488,7 @@ public String executeIgnoreExitValue(OutputInterpreter interpreter, int exitValu | |
| _logger.warn("Security Exception....not running as root?", ex); | ||
| return stackTraceAsString(ex); | ||
| } catch (Exception ex) { | ||
| _logger.warn(String.format("Exception: %s", buildCommandLine(command)), ex); | ||
| _logger.warn(String.format("Exception: %s", commandLine), ex); | ||
| return stackTraceAsString(ex); | ||
| } finally { | ||
| if (_process != null) { | ||
|
|
@@ -516,9 +537,9 @@ public void run() { | |
| } catch (Exception ex) { | ||
| result = stackTraceAsString(ex); | ||
| } finally { | ||
| done = true; | ||
| notifyAll(); | ||
| IOUtils.closeQuietly(reader); | ||
| done = true; | ||
| notifyAll(); | ||
| IOUtils.closeQuietly(reader); | ||
| } | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.