FIX #2232 Check that the user is an Administrator

Signed-off-by: David Gageot <david@gageot.net>
This commit is contained in:
David Gageot 2015-12-22 14:03:05 +01:00
parent d5e11fb961
commit 6a5cc751bb
2 changed files with 28 additions and 4 deletions

View File

@ -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.

View File

@ -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
}