Skip to content

Commit

Permalink
Fail fast on errors in powershell TTP steps (#518)
Browse files Browse the repository at this point in the history
Summary:

Set ErrorActionPreference variable as first thing when executing the powershell TTPs

Resolves #517
Brings parity with bash fail-fast logic introduced in #406

Differential Revision: D65080213
  • Loading branch information
inesusvet authored and facebook-github-bot committed Oct 29, 2024
1 parent c789c78 commit 25963ca
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/blocks/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (e *ScriptExecutor) Execute(ctx context.Context, execCtx TTPExecutionContex
body := expandedInlines[0]
if e.Name == ExecutorPowershellOnLinux || e.Name == ExecutorPowershell {
// Wrap the PowerShell command in a script block
body = fmt.Sprintf("&{%s}\n\n", body)
body = fmt.Sprintf("$ErrorActionPreference = 'Stop' ; &{%s}\n\n", body)
}

// expand variables in environment
Expand Down
34 changes: 34 additions & 0 deletions pkg/blocks/executor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package blocks

import (
"context"
"testing"
)

func TestBashExecutor_Echo_Success(t *testing.T) {
emptyEnvironment := map[string]string{}
execCtx := TTPExecutionContext{Vars: &TTPExecutionVars{}}
executor := NewExecutor("bash", "echo success", "", []string{}, emptyEnvironment)

result, err := executor.Execute(context.Background(), execCtx)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if result.Stdout != "success\n" {
t.Fatalf("expected output 'success\\n', got %#v", result.Stdout)
}
}

func TestBashExecutor_False_FailFast(t *testing.T) {
emptyEnvironment := map[string]string{}
execCtx := TTPExecutionContext{Vars: &TTPExecutionVars{}}
executor := NewExecutor("bash", "false; echo success", "", []string{}, emptyEnvironment)

result, err := executor.Execute(context.Background(), execCtx)
if err == nil {
t.Fatalf("expected error, got result %#v", result)
}
if err.Error() != "exit status 1" {
t.Fatalf("expected error 'exit status 1', got %#v", err.Error())
}
}

0 comments on commit 25963ca

Please sign in to comment.