From 6a5cc751bb07e382383cf5eecb7127cd7eead864 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Tue, 22 Dec 2015 14:03:05 +0100 Subject: [PATCH] FIX #2232 Check that the user is an Administrator Signed-off-by: David Gageot --- drivers/hyperv/hyperv.go | 13 +++++++++++-- drivers/hyperv/powershell.go | 19 +++++++++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/drivers/hyperv/hyperv.go b/drivers/hyperv/hyperv.go index 31c5e98fd0..e416b41e3f 100644 --- a/drivers/hyperv/hyperv.go +++ b/drivers/hyperv/hyperv.go @@ -124,15 +124,24 @@ func (d *Driver) GetState() (state.State, error) { // PreCreateCheck checks that the machine creation process can be started safely. func (d *Driver) PreCreateCheck() error { + // Check that hyperv is installed if err := hypervAvailable(); err != nil { return err } - // Check that there is a virtual switch already configured - _, err := d.chooseVirtualSwitch() + // Check that the user is an Administrator + isAdmin, err := isAdministrator() if err != nil { return err } + if !isAdmin { + return ErrNotAdministrator + } + + // Check that there is a virtual switch already configured + if _, err := d.chooseVirtualSwitch(); err != nil { + return err + } // Downloading boot2docker to cache should be done here to make sure // that a download failure will not leave a machine half created. diff --git a/drivers/hyperv/powershell.go b/drivers/hyperv/powershell.go index 2e36e24910..eef256d1d1 100644 --- a/drivers/hyperv/powershell.go +++ b/drivers/hyperv/powershell.go @@ -3,7 +3,7 @@ package hyperv import ( "bufio" "bytes" - "fmt" + "errors" "os" "os/exec" "path/filepath" @@ -14,6 +14,11 @@ import ( var powershell string +var ( + ErrNotAdministrator = errors.New("Hyper-v commands have to be run as an Administrator") + ErrNotInstalled = errors.New("Hyper-V PowerShell Module is not available") +) + func init() { systemPath := strings.Split(os.Getenv("PATH"), ";") for _, path := range systemPath { @@ -61,8 +66,18 @@ func hypervAvailable() error { resp := parseLines(stdout) if resp[0] != "Hyper-V" { - return fmt.Errorf("Hyper-V PowerShell Module is not available") + return ErrNotInstalled } return nil } + +func isAdministrator() (bool, error) { + stdout, err := cmdOut(`@([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")`) + if err != nil { + return false, err + } + + resp := parseLines(stdout) + return resp[0] == "True", nil +}