Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OSD-18058 : Adding a new interactive shell session when we do debug with elevate command #211

Merged
merged 1 commit into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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