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

Do not auto-select Hyper-V driver if session has no privilege #9588

Merged
merged 6 commits into from
Nov 23, 2020
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
1 change: 1 addition & 0 deletions pkg/minikube/reason/reason.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ var (
DrvNotDetected = Kind{ID: "DRV_NOT_DETECTED", ExitCode: ExDriverNotFound}
DrvAsRoot = Kind{ID: "DRV_AS_ROOT", ExitCode: ExDriverPermission}
DrvNeedsRoot = Kind{ID: "DRV_NEEDS_ROOT", ExitCode: ExDriverPermission}
DrvNeedsAdministrator = Kind{ID: "DRV_NEEDS_ADMINISTRATOR", ExitCode: ExDriverPermission}

GuestCacheLoad = Kind{ID: "GUEST_CACHE_LOAD", ExitCode: ExGuestError}
GuestCert = Kind{ID: "GUEST_CERT", ExitCode: ExGuestError}
Expand Down
31 changes: 29 additions & 2 deletions pkg/minikube/registry/drvs/hyperv/hyperv.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func status() registry.State {
ctx, cancel := context.WithTimeout(context.Background(), 8*time.Second)
defer cancel()

cmd := exec.CommandContext(ctx, path, "@(Get-Wmiobject Win32_ComputerSystem).HypervisorPresent")
cmd := exec.CommandContext(ctx, path, "-NoProfile", "-NonInteractive","@(Get-Wmiobject Win32_ComputerSystem).HypervisorPresent")
out, err := cmd.CombinedOutput()

if err != nil {
Expand All @@ -105,5 +105,32 @@ func status() registry.State {
return registry.State{Installed: false, Running: false, Error: errorMessage, Fix: fixMessage, Doc: docURL}
}

// Ensure user is either a Windows Administrator or a Hyper-V Administrator.
adminCheckCmd := exec.CommandContext(ctx, path, "-NoProfile", "-NonInteractive",`@([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")`)
adminCheckOut, adminCheckErr := adminCheckCmd.CombinedOutput()

if adminCheckErr != nil {
errorMessage := fmt.Errorf("%s returned %q", strings.Join(adminCheckCmd.Args, " "), adminCheckOut)
fixMessage := "Unable to determine current user's administrator privileges"
return registry.State{Installed: true, Running: false, Error: errorMessage, Fix: fixMessage}
}

hypervAdminCheckCmd := exec.CommandContext(ctx, path, "-NoProfile", "-NonInteractive", `@([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(([System.Security.Principal.SecurityIdentifier]::new("S-1-5-32-578")))`)
hypervAdminCheckOut, hypervAdminCheckErr := hypervAdminCheckCmd.CombinedOutput()

if hypervAdminCheckErr != nil {
errorMessage := fmt.Errorf("%s returned %q", strings.Join(hypervAdminCheckCmd.Args, " "), hypervAdminCheckOut)
fixMessage := "Unable to determine current user's Hyper-V administrator privileges."
return registry.State{Installed: true, Running: false, Error: errorMessage, Fix: fixMessage}
}


if (strings.TrimSpace(string(adminCheckOut)) != "True") && (strings.TrimSpace(string(hypervAdminCheckOut)) != "True") {
err := fmt.Errorf("Hyper-V requires Administrator privileges")
fixMessage := "Right-click the PowerShell icon and select Run as Administrator to open PowerShell in elevated mode."
return registry.State{Installed: true, Running: false, Error: err, Fix: fixMessage}
}


return registry.State{Installed: true, Healthy: true}
}
}