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

Bash executor should not tolerate non-zero exit codes by default #406

Closed
wants to merge 1 commit into from
Closed
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
11 changes: 9 additions & 2 deletions pkg/blocks/basicstep.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (b *BasicStep) Validate(execCtx TTPExecutionContext) error {
// Set Executor to "bash" if it is not provided
if b.Executor == "" && b.Inline != "" {
logging.L().Debug("defaulting to bash since executor was not provided")
b.Executor = "bash"
b.Executor = ExecutorBash
}

// Return if Executor is ExecutorBinary
Expand Down Expand Up @@ -153,8 +153,15 @@ func (b *BasicStep) executeBashStdin(ptx context.Context, execCtx TTPExecutionCo
return result, nil
}

func (b *BasicStep) buildCommand(ctx context.Context, executor string) *exec.Cmd {
if executor == ExecutorBash {
return exec.CommandContext(ctx, executor, "-o", "errexit")
}
return exec.CommandContext(ctx, executor)
}

func (b *BasicStep) prepareCommand(ctx context.Context, execCtx TTPExecutionContext, envAsList []string, inline string) *exec.Cmd {
cmd := exec.CommandContext(ctx, b.Executor)
cmd := b.buildCommand(ctx, b.Executor)
cmd.Env = envAsList
cmd.Dir = execCtx.WorkDir
cmd.Stdin = strings.NewReader(inline)
Expand Down
28 changes: 28 additions & 0 deletions pkg/blocks/step_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,34 @@ inline: this will error`,
content: `inline: echo should_error_before_execution`,
wantUnmarshalError: true,
},
{
name: "Basic bash executor doesn't tolerate non-zero exit codes in inline scripts",
content: `name: inline_step
description: this is a test
inline: |
false
echo executing
cleanup:
inline: echo cleanup
`,
wantExecuteError: true,
expectedExecuteStdout: "",
expectedCleanupStdout: "cleanup\n",
},
{
name: "Basic bash supports setting error processing option to ignore errors",
content: `name: inline_step
inline: |
set +e
false
echo executing
cleanup:
inline: echo cleanup
`,
wantExecuteError: false,
expectedExecuteStdout: "executing\n",
expectedCleanupStdout: "cleanup\n",
},
}

for _, tc := range testCases {
Expand Down