From c389dbfa7c335fa10950633ddc5c9121a1bb88f3 Mon Sep 17 00:00:00 2001 From: Ritik Jain <60597329+re-Tick@users.noreply.github.com> Date: Mon, 4 Mar 2024 15:40:57 +0530 Subject: [PATCH] fix: updates the exec cmd to start user application (#1643) * fix(launch.go): runs the user cmd in the user logged shell to preserve user env variables Signed-off-by: re-Tick --------- Signed-off-by: re-Tick --- pkg/hooks/launch.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/pkg/hooks/launch.go b/pkg/hooks/launch.go index ed7a4d2a6..aa0abad25 100755 --- a/pkg/hooks/launch.go +++ b/pkg/hooks/launch.go @@ -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, } @@ -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)