Issue 694: Using "here-document" redirection to wrap commands being passed into sudo

This commit is contained in:
Adam Lowe 2011-09-27 22:02:33 +01:00 committed by Jason King
parent cea43e6c66
commit 466072c811
1 changed files with 4 additions and 2 deletions

View File

@ -44,6 +44,8 @@ import com.google.inject.assistedinject.AssistedInject;
* @author Adrian Cole * @author Adrian Cole
*/ */
public class RunScriptOnNodeUsingSsh implements RunScriptOnNode { public class RunScriptOnNodeUsingSsh implements RunScriptOnNode {
public static final String MARKER = "RUN_SCRIPT_AS_ROOT_SSH";
@Resource @Resource
@Named(ComputeServiceConstants.COMPUTE_LOGGER) @Named(ComputeServiceConstants.COMPUTE_LOGGER)
protected Logger logger = Logger.NULL; protected Logger logger = Logger.NULL;
@ -102,9 +104,9 @@ public class RunScriptOnNodeUsingSsh implements RunScriptOnNode {
public String execAsRoot(String command) { public String execAsRoot(String command) {
if (node.getCredentials().identity.equals("root")) { if (node.getCredentials().identity.equals("root")) {
} else if (node.getAdminPassword() != null) { } else if (node.getAdminPassword() != null) {
command = String.format("echo '%s'|sudo -S %s", node.getAdminPassword(), command); command = String.format("sudo -S sh <<'%s'%n%s%n%s%n%s%n", MARKER, node.getAdminPassword(), command, MARKER);
} else { } else {
command = "sudo " + command; command = String.format("sudo sh <<'%s'%n%s%n%s%n", MARKER, command, MARKER);
} }
return command; return command;
} }