Skip to content

Commit

Permalink
fix(decl/scripts): conditionally disable shell script execution
Browse files Browse the repository at this point in the history
Disable shell script execution if both the "before" and "after"
script are not provided.

Signed-off-by: Leonardo Di Giovanna <[email protected]>
Co-authored-by: Aldo Lacuku <[email protected]>
  • Loading branch information
ekoops and alacuku committed Nov 25, 2024
1 parent 42caec1 commit 0b175ea
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions pkg/test/script/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,27 @@ func New(logger logr.Logger, beforeScript, afterScript *string) test.Script {
if afterScript != nil && *afterScript != "" {
after = strings.TrimSpace(*afterScript) + ";"
}
script := fmt.Sprintf(scriptTemplate, before, signalingToken, after)
var script string
if before != "" || after != "" {
script = fmt.Sprintf(scriptTemplate, before, signalingToken, after)
}
s := &shellScript{
logger: logger,
script: script,
}
return s
}

var noopAfterFunc = func(context.Context) error { return nil }

func (s *shellScript) RunBefore(ctx context.Context) (func(context.Context) error, error) {
script := s.script
if s.script == "" {
return noopAfterFunc, nil
}

processCtx, cancel := context.WithCancel(context.Background())
cmd := exec.CommandContext(processCtx, "sh", "-c", s.script) //nolint:gosec // Disable G204
cmd := exec.CommandContext(processCtx, "sh", "-c", script) //nolint:gosec // Disable G204
cmd.Cancel = func() error {
return cmd.Process.Signal(unix.SIGTERM)
}
Expand Down

0 comments on commit 0b175ea

Please sign in to comment.