Skip to content

Commit

Permalink
fix: updates the exec cmd to start user application (keploy#1643)
Browse files Browse the repository at this point in the history
* fix(launch.go): runs the user cmd in the user logged shell to preserve user env variables

Signed-off-by: re-Tick <[email protected]>

---------

Signed-off-by: re-Tick <[email protected]>
  • Loading branch information
re-Tick authored Mar 4, 2024
1 parent 4b0bfa6 commit c389dbf
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions pkg/hooks/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,9 +474,15 @@ func (h *Hook) processDockerEnv(appCmd, appContainer, appNetwork string, buildDe

// It runs the application using the given command
func (h *Hook) runApp(appCmd string, isUnitTestIntegration bool) error {
// Create a new command with your appCmd
cmd := exec.Command("sh", "-c", appCmd)

// Create a new command with your appCmd'
username := os.Getenv("SUDO_USER")
var cmd *exec.Cmd
if username != "" {
// Run the command as the user who invoked sudo to preserve the user environment variables and PATH
cmd = exec.Command("sudo", "-E", "-u", os.Getenv("SUDO_USER"), "env", "PATH="+os.Getenv("PATH"), "sh", "-c", appCmd)
} else {
cmd = exec.Command("sh", "-c", appCmd)
}
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
}
Expand All @@ -487,7 +493,6 @@ func (h *Hook) runApp(appCmd string, isUnitTestIntegration bool) error {
h.userAppCmd = cmd

// Run the app as the user who invoked sudo
username := os.Getenv("SUDO_USER")
if username != "" {
uidCmd := exec.Command("id", "-u", username)
gidCmd := exec.Command("id", "-g", username)
Expand Down

0 comments on commit c389dbf

Please sign in to comment.