Skip to content

Commit

Permalink
exec.LookPath never fails for powershell
Browse files Browse the repository at this point in the history
The driver, running as a go routine, doesn't use this poshStdLocation
fallback and works perfectly.
I believe this fallback is not needed.
  • Loading branch information
guillaumerose committed Jun 3, 2021
1 parent f5b3afe commit b1b4a03
Showing 1 changed file with 32 additions and 36 deletions.
68 changes: 32 additions & 36 deletions pkg/os/windows/powershell/powershell_windows.go
Expand Up @@ -4,46 +4,17 @@ import (
"bytes"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"

"os/exec"

"github.com/code-ready/crc/pkg/crc/logging"
)

var (
runAsCmds = []string{
`$myWindowsID = [System.Security.Principal.WindowsIdentity]::GetCurrent();`,
`$myWindowsPrincipal = New-Object System.Security.Principal.WindowsPrincipal($myWindowsID);`,
`$adminRole = [System.Security.Principal.WindowsBuiltInRole]::Administrator;`,
`if (-Not ($myWindowsPrincipal.IsInRole($adminRole))) {`,
` $procInfo = New-Object System.Diagnostics.ProcessStartInfo;`,
` $procInfo.FileName = "` + LocatePowerShell() + `"`,
` $procInfo.WindowStyle = [Diagnostics.ProcessWindowStyle]::Hidden`,
` $procInfo.Arguments = "-ExecutionPolicy RemoteSigned & '" + $script:MyInvocation.MyCommand.Path + "'"`,
` $procInfo.Verb = "runas";`,
` [System.Diagnostics.Process]::Start($procInfo);`,
` Exit;`,
`}`,
}
isAdminCmds = []string{
"$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())",
"$currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)",
}

poshStdLocation = filepath.Join(os.Getenv("SYSTEMROOT"), "System32", "WindowsPowerShell", "v1.0", "powershell.exe")
)

func LocatePowerShell() string {
ps, err := exec.LookPath("powershell.exe")
if err != nil {
logging.Debugf("Could not find powershell.exe on path %s", err.Error())
logging.Debug("Falling back to ", poshStdLocation)
return poshStdLocation
}
return ps
var isAdminCmds = []string{
"$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())",
"$currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)",
}

func IsAdmin() bool {
Expand All @@ -62,16 +33,20 @@ func IsAdmin() bool {
func Execute(args ...string) (string, string, error) {
logging.Debugf("Running '%s'", strings.Join(args, " "))

powershell, err := exec.LookPath("powershell.exe")
if err != nil {
return "", "", err
}
args = append([]string{"-NoProfile", "-NonInteractive", "-ExecutionPolicy", "RemoteSigned", "-Command"}, args...)
cmd := exec.Command(LocatePowerShell(), args...) // #nosec G204
cmd := exec.Command(powershell, args...) // #nosec G204
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}

var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr

err := cmd.Run()
err = cmd.Run()
if err != nil {
logging.Debugf("Command failed: %v", err)
logging.Debugf("stdout: %s", stdout.String())
Expand All @@ -81,7 +56,11 @@ func Execute(args ...string) (string, string, error) {
}

func ExecuteAsAdmin(reason, cmd string) (string, string, error) {
scriptContent := strings.Join(append(runAsCmds, cmd), "\n")
powershell, err := exec.LookPath("powershell.exe")
if err != nil {
return "", "", err
}
scriptContent := strings.Join(append(runAsCmds(powershell), cmd), "\n")

tempDir, err := ioutil.TempDir("", "crcScripts")
if err != nil {
Expand All @@ -104,3 +83,20 @@ func ExecuteAsAdmin(reason, cmd string) (string, string, error) {

return Execute(filename)
}

func runAsCmds(powershell string) []string {
return []string{
`$myWindowsID = [System.Security.Principal.WindowsIdentity]::GetCurrent();`,
`$myWindowsPrincipal = New-Object System.Security.Principal.WindowsPrincipal($myWindowsID);`,
`$adminRole = [System.Security.Principal.WindowsBuiltInRole]::Administrator;`,
`if (-Not ($myWindowsPrincipal.IsInRole($adminRole))) {`,
` $procInfo = New-Object System.Diagnostics.ProcessStartInfo;`,
` $procInfo.FileName = "` + powershell + `"`,
` $procInfo.WindowStyle = [Diagnostics.ProcessWindowStyle]::Hidden`,
` $procInfo.Arguments = "-ExecutionPolicy RemoteSigned & '" + $script:MyInvocation.MyCommand.Path + "'"`,
` $procInfo.Verb = "runas";`,
` [System.Diagnostics.Process]::Start($procInfo);`,
` Exit;`,
`}`,
}
}

0 comments on commit b1b4a03

Please sign in to comment.