From cfb3253d89adb3b1bf1021487dc57f823880ffc2 Mon Sep 17 00:00:00 2001 From: Deeptimayee Maharana Date: Tue, 26 Sep 2023 08:49:28 +0530 Subject: [PATCH] Adding a new shell when elevate debug command is run --- pkg/elevate/elevate.go | 13 +++++++++++-- pkg/elevate/elevate_test.go | 6 +++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/pkg/elevate/elevate.go b/pkg/elevate/elevate.go index 0dc1bb1c..fb4afd6e 100644 --- a/pkg/elevate/elevate.go +++ b/pkg/elevate/elevate.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "os/exec" + "strings" logger "github.com/sirupsen/logrus" "k8s.io/client-go/tools/clientcmd/api" @@ -61,12 +62,19 @@ func RunElevate(argv []string) error { logger.Debug("Adding impersonation RBAC allow permissions to kubeconfig") - elevateCmd := argv[1:] + elevateCmd := "oc " + strings.Join(argv[1:], " ") + + shell := "/bin/bash" + + if len(argv) > 3 { + shell = argv[4] + } logger.Debugln("Executing command with temporary kubeconfig as backplane-cluster-admin") - ocCmd := ExecCmd("oc", elevateCmd...) + ocCmd := ExecCmd(shell, "-c", elevateCmd) ocCmd.Env = append(ocCmd.Env, os.Environ()...) + ocCmd.Stdin = os.Stdin ocCmd.Stderr = os.Stderr ocCmd.Stdout = os.Stdout @@ -75,6 +83,7 @@ func RunElevate(argv []string) error { } err = ocCmd.Run() + kubeconfigPath, _ := os.LookupEnv("KUBECONFIG") defer func() { logger.Debugln("Command error; Cleaning up temporary kubeconfig") diff --git a/pkg/elevate/elevate_test.go b/pkg/elevate/elevate_test.go index b256d880..f69fe182 100644 --- a/pkg/elevate/elevate_test.go +++ b/pkg/elevate/elevate_test.go @@ -109,7 +109,7 @@ func TestAddElevationReasonToRawKubeconfig(t *testing.T) { } func TestRunElevate(t *testing.T) { - t.Run("It errors if we cannot load the kubeconfig", func(t *testing.T) { + t.Run("It returns an error if we cannot load the kubeconfig", func(t *testing.T) { ExecCmd = exec.Command OsRemove = os.Remove ReadKubeConfigRaw = func() (api.Config, error) { @@ -120,7 +120,7 @@ func TestRunElevate(t *testing.T) { } }) - t.Run("It errors if kubeconfig has no current context", func(t *testing.T) { + t.Run("It returns an error if kubeconfig has no current context", func(t *testing.T) { ExecCmd = exec.Command OsRemove = os.Remove ReadKubeConfigRaw = func() (api.Config, error) { @@ -131,7 +131,7 @@ func TestRunElevate(t *testing.T) { } }) - t.Run("It errors if the exec command errors", func(t *testing.T) { + t.Run("It returns an error if the exec command has errors", func(t *testing.T) { ExecCmd = fakeExecCommandError OsRemove = os.Remove ReadKubeConfigRaw = func() (api.Config, error) {