Skip to content

Commit

Permalink
fix(pkg/commands): guarantee secret cleanup after run commands
Browse files Browse the repository at this point in the history
  • Loading branch information
SasSwart committed Oct 24, 2024
1 parent 8f144a6 commit 8783c3e
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (r *RunCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bui
return runCommandInExec(config, buildArgs, r.cmd, r.output, r.buildSecrets)
}

func runCommandInExec(config *v1.Config, buildArgs *dockerfile.BuildArgs, cmdRun *instructions.RunCommand, output *RunOutput, buildSecrets []string) error {
func runCommandInExec(config *v1.Config, buildArgs *dockerfile.BuildArgs, cmdRun *instructions.RunCommand, output *RunOutput, buildSecrets []string) (err error) {
if output == nil {
output = &RunOutput{}
}
Expand Down Expand Up @@ -153,6 +153,13 @@ func runCommandInExec(config *v1.Config, buildArgs *dockerfile.BuildArgs, cmdRun
}

secretFileManager := FileCreatorCleaner{}
defer func() {
cleanupErr := secretFileManager.Clean()
if err == nil {
err = cleanupErr
}
}()

mounts := instructions.GetMounts(cmdRun)
for _, mount := range mounts {
switch mount.Type {
Expand Down Expand Up @@ -219,11 +226,6 @@ func runCommandInExec(config *v1.Config, buildArgs *dockerfile.BuildArgs, cmdRun
return errors.Wrap(err, "waiting for process to exit")
}

err = secretFileManager.Clean()
if err != nil {
return errors.Wrap(err, "cleaning up secrets")
}

// it's not an error if there are no grandchildren
if err := syscall.Kill(-pgid, syscall.SIGKILL); err != nil && err.Error() != "no such process" {
return err
Expand Down

0 comments on commit 8783c3e

Please sign in to comment.