Skip to content

Commit

Permalink
Ensure quotes and new lines are escaped in echo
Browse files Browse the repository at this point in the history
  • Loading branch information
ssalinas committed Nov 2, 2016
1 parent 551c9d5 commit 5cc113b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.google.inject.Singleton;
import com.google.inject.name.Named;
import com.hubspot.singularity.executor.handlebars.BashEscapedHelper;
import com.hubspot.singularity.executor.handlebars.EscapedNewLinesHelper;
import com.hubspot.singularity.executor.handlebars.EscapeNewLinesAndQuotesHelper;
import com.hubspot.singularity.executor.handlebars.IfHasNewLinesHelper;
import com.hubspot.singularity.executor.handlebars.IfPresentHelper;
import com.hubspot.singularity.runner.base.config.SingularityRunnerBaseLogging;
Expand Down Expand Up @@ -93,7 +93,7 @@ public Handlebars providesHandlebars() {
handlebars.registerHelper(BashEscapedHelper.NAME, new BashEscapedHelper());
handlebars.registerHelper(IfPresentHelper.NAME, new IfPresentHelper());
handlebars.registerHelper(IfHasNewLinesHelper.NAME, new IfHasNewLinesHelper());
handlebars.registerHelper(EscapedNewLinesHelper.NAME, new EscapedNewLinesHelper());
handlebars.registerHelper(EscapeNewLinesAndQuotesHelper.NAME, new EscapeNewLinesAndQuotesHelper());

return handlebars;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import com.github.jknack.handlebars.Helper;
import com.github.jknack.handlebars.Options;

public class EscapedNewLinesHelper implements Helper<Object> {
public class EscapeNewLinesAndQuotesHelper implements Helper<Object> {

public static final String NAME = "escapedNewLines";
public static final String NAME = "escapeNewLinesAndQuotes";

@Override
public CharSequence apply(Object context, Options options) throws IOException {
Expand All @@ -23,6 +23,9 @@ public CharSequence apply(Object context, Options options) throws IOException {
if (c == '\n') {
sb.append('\\');
sb.append('n');
} else if (c == '"') {
sb.append('\\');
sb.append('"');
} else {
sb.append(c);
}
Expand Down
4 changes: 2 additions & 2 deletions SingularityExecutor/src/main/resources/docker.sh.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ mkdir -p {{{ runContext.taskAppDirectory }}}
sudo chown -R {{{ runContext.user }}} {{{ runContext.taskAppDirectory }}}

# Start up the container
echo "Creating continer with: sudo -E -H -u {{{ runContext.user }}} docker create $DOCKER_OPTIONS {{#each envContext.env}}{{#ifHasNewLines value}}-e {{{name}}}={{#escapedNewLines value}}{{/escapedNewLines}}{{/ifHasNewLines}}{{/each}} $DOCKER_IMAGE {{{ runContext.cmd }}}"
cid=`sudo -E -H -u {{{ runContext.user }}} docker create $DOCKER_OPTIONS {{#each envContext.env}}{{#ifHasNewLines value}}-e {{{name}}}={{#escapedNewLines value}}{{/escapedNewLines}}{{/ifHasNewLines}}{{/each}} $DOCKER_IMAGE {{{runContext.cmd }}}`
echo -e "Creating continer with: sudo -E -H -u {{{ runContext.user }}} docker create $DOCKER_OPTIONS {{#each envContext.env}}{{#ifHasNewLines value}}-e {{{name}}}={{#escapeNewLinesAndQuotes value}}{{/escapeNewLinesAndQuotes}}{{/ifHasNewLines}}{{/each}} $DOCKER_IMAGE {{{ runContext.cmd }}}"
cid=`sudo -E -H -u {{{ runContext.user }}} docker create $DOCKER_OPTIONS {{#each envContext.env}}{{#ifHasNewLines value}}-e {{{name}}}={{#escapeNewLinesAndQuotes value}}{{/escapeNewLinesAndQuotes}}{{/ifHasNewLines}}{{/each}} $DOCKER_IMAGE {{{runContext.cmd }}}`
sudo -E -H -u {{{ runContext.user }}} docker start -a $cid >> {{{ runContext.logFile }}} 2>&1 &
running=1

Expand Down

1 comment on commit 5cc113b

@tpetr
Copy link
Contributor

@tpetr tpetr commented on 5cc113b Nov 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚢

Please sign in to comment.