packages/woodpecker-agent-sudo/sudo.patch

112 lines
4.2 KiB
Diff
Raw Normal View History

2023-11-23 23:07:58 +00:00
diff --git a/pipeline/backend/local/clone.go b/pipeline/backend/local/clone.go
index b659a090a..82ae5c5cc 100644
--- a/pipeline/backend/local/clone.go
+++ b/pipeline/backend/local/clone.go
@@ -94,14 +94,13 @@ func (e *local) execClone(ctx context.Context, step *types.Step, state *workflow
}
cmd = exec.CommandContext(ctx, pwsh, "-Command", fmt.Sprintf("%s ; $code=$? ; %s ; if (!$code) {[Environment]::Exit(1)}", state.pluginGitBinary, rmCmd))
} else {
- cmd = exec.CommandContext(ctx, "/bin/sh", "-c", fmt.Sprintf("%s ; export code=$? ; %s ; exit $code", state.pluginGitBinary, rmCmd))
+ cmd = exec.CommandContext(ctx, "sudo", "-E", "-u", state.user, "-D", state.workspaceDir, "/bin/sh", "-c", fmt.Sprintf("%s ; export code=$? ; %s ; exit $code", state.pluginGitBinary, rmCmd))
}
} else {
// if we have NO netrc, we can just exec the clone directly
- cmd = exec.CommandContext(ctx, state.pluginGitBinary)
+ cmd = exec.CommandContext(ctx, "sudo", "-E", "-u", state.user, "-D", state.workspaceDir, state.pluginGitBinary)
}
cmd.Env = env
- cmd.Dir = state.workspaceDir
// Get output and redirect Stderr to Stdout
e.output, _ = cmd.StdoutPipe()
2023-09-14 22:24:51 +00:00
diff --git a/pipeline/backend/local/local.go b/pipeline/backend/local/local.go
2023-11-23 23:07:58 +00:00
index 698a3f0f9..5bef80857 100644
2023-09-14 22:24:51 +00:00
--- a/pipeline/backend/local/local.go
+++ b/pipeline/backend/local/local.go
2023-11-23 23:07:58 +00:00
@@ -36,7 +36,7 @@ import (
2023-09-14 22:24:51 +00:00
type workflowState struct {
2023-11-23 23:07:58 +00:00
stepCMDs map[string]*exec.Cmd
- baseDir string
+ user string
homeDir string
workspaceDir string
pluginGitBinary string
@@ -80,26 +80,20 @@ func (e *local) Load(ctx context.Context) (*types.EngineInfo, error) {
2023-09-14 22:24:51 +00:00
}
2023-11-23 23:07:58 +00:00
// SetupWorkflow the pipeline environment.
-func (e *local) SetupWorkflow(_ context.Context, _ *types.Config, taskUUID string) error {
+func (e *local) SetupWorkflow(ctx context.Context, conf *types.Config, taskUUID string) error {
2023-09-14 22:24:51 +00:00
log.Trace().Str("taskUUID", taskUUID).Msg("create workflow environment")
2023-11-23 23:07:58 +00:00
- baseDir, err := os.MkdirTemp(e.tempDir, "woodpecker-local-*")
2023-09-14 22:24:51 +00:00
- if err != nil {
- return err
- }
+ user := conf.Stages[0].Steps[0].Environment["CI_COMMIT_AUTHOR"]
state := &workflowState{
stepCMDs: make(map[string]*exec.Cmd),
- baseDir: baseDir,
- workspaceDir: filepath.Join(baseDir, "workspace"),
- homeDir: filepath.Join(baseDir, "home"),
+ user: user,
2023-11-23 23:07:58 +00:00
+ workspaceDir: filepath.Join("/home", user, ".cache", "woodpecker", conf.Stages[0].Steps[0].Environment["CI_REPO_NAME"]),
2023-09-14 22:24:51 +00:00
+ homeDir: filepath.Join("/home", user),
}
2023-11-23 23:07:58 +00:00
- if err := os.Mkdir(state.homeDir, 0o700); err != nil {
- return err
- }
-
2023-09-14 22:24:51 +00:00
- if err := os.Mkdir(state.workspaceDir, 0o700); err != nil {
2023-11-23 23:07:58 +00:00
+ err := exec.CommandContext(ctx, "sudo", "-u", state.user, "mkdir", "-p", state.workspaceDir).Run()
2023-09-14 22:24:51 +00:00
+ if err != nil {
return err
}
2023-11-23 23:07:58 +00:00
@@ -152,9 +146,8 @@ func (e *local) execCommands(ctx context.Context, step *types.Step, state *workf
}
// Use "image name" as run command (indicate shell)
- cmd := exec.CommandContext(ctx, step.Image, args...)
+ cmd := exec.CommandContext(ctx, "sudo", append([]string{"-E", "-u", state.user, "-D", state.workspaceDir, step.Image}, args...)...)
cmd.Env = env
- cmd.Dir = state.workspaceDir
// Get output and redirect Stderr to Stdout
e.output, _ = cmd.StdoutPipe()
@@ -178,9 +171,8 @@ func (e *local) execPlugin(ctx context.Context, step *types.Step, state *workflo
return fmt.Errorf("lookup plugin binary: %w", err)
}
- cmd := exec.CommandContext(ctx, binary)
+ cmd := exec.CommandContext(ctx, "sudo", "-E", "-u", state.user, "-D", state.workspaceDir, binary)
cmd.Env = env
- cmd.Dir = state.workspaceDir
2023-09-14 22:24:51 +00:00
2023-11-23 23:07:58 +00:00
// Get output and redirect Stderr to Stdout
e.output, _ = cmd.StdoutPipe()
@@ -237,19 +229,9 @@ func (e *local) DestroyStep(_ context.Context, _ *types.Step, _ string) error {
func (e *local) DestroyWorkflow(_ context.Context, _ *types.Config, taskUUID string) error {
2023-09-14 22:24:51 +00:00
log.Trace().Str("taskUUID", taskUUID).Msgf("delete workflow environment")
2023-11-23 23:07:58 +00:00
- state, err := e.getState(taskUUID)
2023-09-14 22:24:51 +00:00
- if err != nil {
- return err
- }
-
- err = os.RemoveAll(state.baseDir)
- if err != nil {
- return err
- }
-
2023-11-23 23:07:58 +00:00
e.deleteState(taskUUID)
- return err
+ return nil
}
func (e *local) getState(taskUUID string) (*workflowState, error) {