Skip to content

Commit

Permalink
OSD-22522 allow args with quote
Browse files Browse the repository at this point in the history
  • Loading branch information
Tof1973 committed Apr 23, 2024
1 parent 6b6c4b4 commit 4c98028
Showing 1 changed file with 11 additions and 30 deletions.
41 changes: 11 additions & 30 deletions pkg/elevate/elevate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"os"
"os/exec"
"strings"

logger "github.com/sirupsen/logrus"
"k8s.io/client-go/tools/clientcmd/api"
Expand Down Expand Up @@ -45,11 +44,11 @@ func AddElevationReasonToRawKubeconfig(config api.Config, elevationReason string
func RunElevate(argv []string) error {
logger.Debugln("Finding target cluster from kubeconfig")
config, err := ReadKubeConfigRaw()

if err != nil {
return err
}

logger.Debug("Adding impersonation RBAC allow permissions to kubeconfig")
err = AddElevationReasonToRawKubeconfig(config, argv[0])
if err != nil {
return err
Expand All @@ -73,42 +72,24 @@ func RunElevate(argv []string) error {
return err
}

logger.Debug("Adding impersonation RBAC allow permissions to kubeconfig")

elevateCmd := "oc " + strings.Join(argv[1:], " ")

// Make the Default SHELL environment variable to /bin/bash
shell := os.Getenv("SHELL")
if shell == "" || !utils.ShellChecker.IsValidShell(shell) {
if utils.ShellChecker.IsValidShell("/bin/bash") {
shell = "/bin/bash"
} else {
return fmt.Errorf("both the SHELL environment variable and /bin/bash are not set or invalid. Please ensure a valid shell is set in your environment")
// As WriteKubeconfigToFile is also creating a tempory file reference by new KUBECONFIG variable setting,
// we need to take care of its cleanup
tempKubeconfigPath, _ := os.LookupEnv("KUBECONFIG")
defer func() {
logger.Debugln("Cleaning up temporary kubeconfig", tempKubeconfigPath)
err := OsRemove(tempKubeconfigPath)
if err != nil {
fmt.Println(err)
}
}
}()

logger.Debugln("Executing command with temporary kubeconfig as backplane-cluster-admin")

ocCmd := ExecCmd(shell, "-c", elevateCmd)
ocCmd := ExecCmd("oc", argv[1:]...)
ocCmd.Env = append(ocCmd.Env, os.Environ()...)
ocCmd.Stdin = os.Stdin
ocCmd.Stderr = os.Stderr
ocCmd.Stdout = os.Stdout

if err != nil {
return err
}

err = ocCmd.Run()

kubeconfigPath, _ := os.LookupEnv("KUBECONFIG")
defer func() {
logger.Debugln("Cleaning up temporary kubeconfig", kubeconfigPath)
err := OsRemove(kubeconfigPath)
if err != nil {
fmt.Println(err)
}
}()
if err != nil {
return err
}
Expand Down

0 comments on commit 4c98028

Please sign in to comment.