Skip to content

Commit

Permalink
Adding a new shell when elevate debug command is run
Browse files Browse the repository at this point in the history
  • Loading branch information
Dee-6777 committed Sep 26, 2023
1 parent 265ba04 commit cfb3253
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
13 changes: 11 additions & 2 deletions pkg/elevate/elevate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"os/exec"
"strings"

logger "github.com/sirupsen/logrus"
"k8s.io/client-go/tools/clientcmd/api"
Expand Down Expand Up @@ -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

Expand All @@ -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")
Expand Down
6 changes: 3 additions & 3 deletions pkg/elevate/elevate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit cfb3253

Please sign in to comment.