-
Notifications
You must be signed in to change notification settings - Fork 56
Add support for run.shell task #268
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
Open
mcruzdev
wants to merge
1
commit into
serverlessworkflow:main
Choose a base branch
from
mcruzdev:issue-246
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| // Copyright 2025 The Serverless Workflow Specification Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package impl | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/serverlessworkflow/sdk-go/v3/model" | ||
| ) | ||
|
|
||
| type RunTaskRunner struct { | ||
| Task *model.RunTask | ||
| TaskName string | ||
| } | ||
|
|
||
| func (d *RunTaskRunner) GetTaskName() string { | ||
| return d.TaskName | ||
| } | ||
|
|
||
| func NewRunTaskRunner(taskName string, task *model.RunTask) (*RunTaskRunner, error) { | ||
|
|
||
| if task == nil { | ||
| return nil, model.NewErrValidation(fmt.Errorf("no set configuration provided for RunTask %s", taskName), taskName) | ||
| } | ||
|
|
||
| return &RunTaskRunner{ | ||
| Task: task, | ||
| TaskName: taskName, | ||
| }, nil | ||
| } | ||
|
|
||
| func (d *RunTaskRunner) Run(input interface{}, taskSupport TaskSupport) (output interface{}, err error) { | ||
|
|
||
| if d.Task.Run.Shell != nil { | ||
| shellTask := NewRunTaskShell() | ||
| return shellTask.RunTask(d, input, taskSupport) | ||
| } | ||
|
|
||
| return nil, fmt.Errorf("no set configuration provided for RunTask %s", d.TaskName) | ||
|
|
||
| } | ||
|
|
||
| // ProcessResult Describes the result of a process. | ||
| type ProcessResult struct { | ||
| Stdout string | ||
| Stderr string | ||
| Code int | ||
| } | ||
|
|
||
| // NewProcessResult creates a new ProcessResult instance. | ||
| func NewProcessResult(stdout, stderr string, code int) *ProcessResult { | ||
| return &ProcessResult{ | ||
| Stdout: stdout, | ||
| Stderr: stderr, | ||
| Code: code, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| // Copyright 2025 The Serverless Workflow Specification Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package impl | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
| "strings" | ||
|
|
||
| "github.com/serverlessworkflow/sdk-go/v3/impl/expr" | ||
| "github.com/serverlessworkflow/sdk-go/v3/model" | ||
| ) | ||
|
|
||
| // RunTaskShell defines the shell configuration for RunTask. | ||
| // It implements the RunTask.shell definition. | ||
| type RunTaskShell struct { | ||
| } | ||
|
|
||
| // NewRunTaskShell creates a new RunTaskShell instance. | ||
| func NewRunTaskShell() *RunTaskShell { | ||
| return &RunTaskShell{} | ||
| } | ||
|
|
||
| func (shellTask *RunTaskShell) RunTask(r *RunTaskRunner, input interface{}, taskSupport TaskSupport) (interface{}, error) { | ||
| await := r.Task.Run.Await | ||
| shell := r.Task.Run.Shell | ||
|
|
||
| if shell == nil { | ||
| return nil, model.NewErrValidation(fmt.Errorf("no shell configuration provided for RunTask %s", r.TaskName), r.TaskName) | ||
| } | ||
|
|
||
| if shell.Command == "" { | ||
| return nil, model.NewErrValidation(fmt.Errorf("no command provided for RunTask shell: %s", r.TaskName), r.TaskName) | ||
| } | ||
|
|
||
| // Build the environment for the command without mutating the parent process' | ||
| // global environment. | ||
| env := os.Environ() | ||
| for key, value := range shell.Environment { | ||
| evaluated, evalErr := expr.TraverseAndEvaluate(value, input, taskSupport.GetContext()) | ||
| if evalErr != nil { | ||
| return nil, model.NewErrRuntime(fmt.Errorf("error evaluating environment variable value for RunTask shell: %s", r.TaskName), r.TaskName) | ||
| } | ||
| env = append(env, fmt.Sprintf("%s=%s", key, fmt.Sprint(evaluated))) | ||
| } | ||
|
|
||
| evaluated, err := expr.TraverseAndEvaluate(shell.Command, input, taskSupport.GetContext()) | ||
| if err != nil { | ||
| return nil, model.NewErrRuntime(fmt.Errorf("error evaluating command for RunTask shell: %s", r.TaskName), r.TaskName) | ||
| } | ||
| cmdEvaluated := fmt.Sprint(evaluated) | ||
|
|
||
| args, err := shellTask.buildArguments(r, shell.Arguments, input, taskSupport) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| var fullCmd strings.Builder | ||
| fullCmd.WriteString(cmdEvaluated) | ||
| for _, arg := range args { | ||
| fullCmd.WriteString(" ") | ||
| fullCmd.WriteString(arg) | ||
| } | ||
|
|
||
| newCmd := func() *exec.Cmd { | ||
| cmd := exec.Command("sh", "-c", fullCmd.String()) | ||
| cmd.Env = env | ||
| return cmd | ||
| } | ||
|
|
||
| // When the task is explicitly not awaited, fire and forget. | ||
| if await != nil && !*await { | ||
| go func() { | ||
| cmd := newCmd() | ||
| _ = cmd.Start() | ||
| _ = cmd.Wait() | ||
| }() | ||
| return input, nil | ||
| } | ||
|
|
||
| cmd := newCmd() | ||
| var stdout, stderr bytes.Buffer | ||
| cmd.Stdout = &stdout | ||
| cmd.Stderr = &stderr | ||
|
|
||
| err = cmd.Run() | ||
| exitCode := 0 | ||
| if err != nil { | ||
| var exitErr *exec.ExitError | ||
| if errors.As(err, &exitErr) { | ||
| exitCode = exitErr.ExitCode() | ||
| } | ||
| } else if cmd.ProcessState != nil { | ||
| exitCode = cmd.ProcessState.ExitCode() | ||
| } | ||
|
|
||
| stdoutStr := strings.TrimSpace(stdout.String()) | ||
| stderrStr := strings.TrimSpace(stderr.String()) | ||
|
|
||
| switch r.Task.Run.Return { | ||
| case "all": | ||
| return NewProcessResult(stdoutStr, stderrStr, exitCode), nil | ||
| case "stderr": | ||
| return stderrStr, nil | ||
| case "code": | ||
| return exitCode, nil | ||
| case "none": | ||
| return nil, nil | ||
| default: | ||
| return stdoutStr, nil | ||
| } | ||
| } | ||
|
|
||
| // buildArguments resolves the shell arguments into an ordered list of strings. | ||
| // Arguments may be provided either as an ordered array (preferred when order | ||
| // matters) or as a key/value map (order is not guaranteed by Go map iteration). | ||
| func (shellTask *RunTaskShell) buildArguments(r *RunTaskRunner, arguments *model.RunArguments, input interface{}, taskSupport TaskSupport) ([]string, error) { | ||
| if arguments == nil { | ||
| return nil, nil | ||
| } | ||
|
|
||
| eval := func(value interface{}) (string, error) { | ||
| evaluated, evalErr := expr.TraverseAndEvaluate(value, input, taskSupport.GetContext()) | ||
| if evalErr != nil { | ||
| return "", model.NewErrRuntime(fmt.Errorf("error evaluating argument for RunTask shell: %s", r.TaskName), r.TaskName) | ||
| } | ||
| return fmt.Sprint(evaluated), nil | ||
| } | ||
|
|
||
| if slice := arguments.AsSlice(); slice != nil { | ||
| args := make([]string, 0, len(slice)) | ||
| for _, value := range slice { | ||
| arg, err := eval(value) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| args = append(args, arg) | ||
| } | ||
| return args, nil | ||
| } | ||
|
|
||
| if m := arguments.AsMap(); m != nil { | ||
| args := make([]string, 0, len(m)) | ||
| for key, value := range m { | ||
| keyStr, err := eval(key) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if value != nil { | ||
| valueStr, err := eval(value) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| args = append(args, fmt.Sprintf("%s=%s", keyStr, valueStr)) | ||
| } else { | ||
| args = append(args, keyStr) | ||
| } | ||
| } | ||
| return args, nil | ||
| } | ||
|
|
||
| return nil, nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't do this directly, as it is a huge security flaw. These commands must run on an isolated process/shell without privileged access. If the workflow process is running with a user with privileged access (aka root), one can inject a script and destroy the environment.
I must do some research around this topic; we can't have a naive implementation for this one.