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

Hide sensitive data #124

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
22 changes: 22 additions & 0 deletions manual/Tasks/sshexec.html
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,28 @@ <h3>Parameters</h3>
not <q>0</q>. <em>since Ant 1.9.7</em></td>
<td>No; defaults to <q>3</q></td>
</tr>
<tr>
<td>hideSensitive</td>
<td>Allows to hide sensitive data in logs without output supressing. It makes easier debugging
with sensitive data hiding.
</td>
<td>No; defaults to <q>false</q></td>
</tr>
<tr>
<td>bindSensitive</td>
<td>Contains string with key=values pairs are divided by <var>sensitiveDelimiter</var></td>
<td>Yes if <var>hideSensitive</var> set to <q>true</q></td>
</tr>
<tr>
<td>sensitiveDelimiter</td>
<td>Sensitive data delimiter of key=values pairs</td>
<td>No, defaults to <q>;</q></td>
</tr>
<tr>
<td>placeholderBrackets</td>
<td>Contains symbols to destinguish placeholder are needed to replace with sensitive data</td>
<td>No, defaults to <q>:</q></td>
</tr>
</table>

<h3>Examples</h3>
Expand Down
69 changes: 69 additions & 0 deletions src/main/org/apache/tools/ant/taskdefs/optional/ssh/SSHExec.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import java.io.StringReader;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
Expand Down Expand Up @@ -74,6 +76,10 @@ public class SSHExec extends SSHBase {
private boolean appenderr = false;
private boolean usePty = false;
private boolean useSystemIn = false;
private boolean hideSensitive = false;
private String bindSensitive = null;
private String sensitiveDelimiter = ";";
private String placeholderBrackets = ":";

private Resource commandResource = null;

Expand Down Expand Up @@ -273,6 +279,67 @@ public void setSuppressSystemErr(final boolean suppressSystemErr) {
this.suppressSystemErr = suppressSystemErr;
}

/**
* If hideSensitive is <code>true</code>, command will be checked for placeholders to replace,
* If hideSensitive is <code>false</code>, command will be executed as is,
* Prevents from sensitive data appearance in logs
* @param hideSensitive boolean
*/
public void setHideSensitive(final boolean hideSensitive) {
this.hideSensitive = hideSensitive;
}

/**
* Sets the placeholders with sensitive data to replace in command,
* Prevents from sensitive data appearance in logs
* @param bindSensitive String
*/
public void setBindSensitive(final String bindSensitive) {
this.bindSensitive = bindSensitive;
}

/**
* Sets the delimiter of sensitive data key values pairs in bindSensitive
*
* @param sensitiveDelimiter String
*/
public void setSensitiveDelimiter(final String sensitiveDelimiter) {
this.sensitiveDelimiter = sensitiveDelimiter;
}

/**
* Sets delimiter to find placeholders and replace them
*
* @param placeholderBrackets String
*/
public void setPlaceholderBrackets(final String placeholderBrackets) {
this.placeholderBrackets = placeholderBrackets;
}

/**
* Replaces placeholders with sensitive data in command to avoid sensitive data appearance in logs
*
* @param cmd String
* @return String
*/
private String replacePlaceholders(String cmd) {
if (hideSensitive && bindSensitive != null) {
try {
for (String pair : bindSensitive.split(Pattern.quote(sensitiveDelimiter))) {
String[] kv = pair.split("=");
cmd = cmd.replace(placeholderBrackets + kv[0] + placeholderBrackets, kv[1]);
}
}
catch (ArrayIndexOutOfBoundsException e) {
log("Requested array index not found: " + e.getMessage());
}
catch (PatternSyntaxException e) {
log("Wrong pattern to split: " + e.getMessage());
}
}
return cmd;
}

/**
* Execute the command on the remote host.
*
Expand Down Expand Up @@ -317,6 +384,7 @@ && getUserInfo().getPassword() == null) {
/* called once */
if (command != null) {
log("cmd : " + command, Project.MSG_INFO);
command = replacePlaceholders(command);
executeCommand(session, command, output);
} else { // read command resource and execute for each command
try (final BufferedReader br = new BufferedReader(
Expand All @@ -325,6 +393,7 @@ && getUserInfo().getPassword() == null) {
br.lines().forEach(cmd -> {
log("cmd : " + cmd, Project.MSG_INFO);
output.append(cmd).append(" : ");
cmd = replacePlaceholders(cmd);
executeCommand(s, cmd, output);
output.append("\n");
});
Expand Down