Skip to content

Commit 6a5cc75

Browse files
committed
FIX docker-archive-public#2232 Check that the user is an Administrator
Signed-off-by: David Gageot <david@gageot.net>
1 parent d5e11fb commit 6a5cc75

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

drivers/hyperv/hyperv.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,24 @@ func (d *Driver) GetState() (state.State, error) {
124124

125125
// PreCreateCheck checks that the machine creation process can be started safely.
126126
func (d *Driver) PreCreateCheck() error {
127+
// Check that hyperv is installed
127128
if err := hypervAvailable(); err != nil {
128129
return err
129130
}
130131

131-
// Check that there is a virtual switch already configured
132-
_, err := d.chooseVirtualSwitch()
132+
// Check that the user is an Administrator
133+
isAdmin, err := isAdministrator()
133134
if err != nil {
134135
return err
135136
}
137+
if !isAdmin {
138+
return ErrNotAdministrator
139+
}
140+
141+
// Check that there is a virtual switch already configured
142+
if _, err := d.chooseVirtualSwitch(); err != nil {
143+
return err
144+
}
136145

137146
// Downloading boot2docker to cache should be done here to make sure
138147
// that a download failure will not leave a machine half created.

drivers/hyperv/powershell.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package hyperv
33
import (
44
"bufio"
55
"bytes"
6-
"fmt"
6+
"errors"
77
"os"
88
"os/exec"
99
"path/filepath"
@@ -14,6 +14,11 @@ import (
1414

1515
var powershell string
1616

17+
var (
18+
ErrNotAdministrator = errors.New("Hyper-v commands have to be run as an Administrator")
19+
ErrNotInstalled = errors.New("Hyper-V PowerShell Module is not available")
20+
)
21+
1722
func init() {
1823
systemPath := strings.Split(os.Getenv("PATH"), ";")
1924
for _, path := range systemPath {
@@ -61,8 +66,18 @@ func hypervAvailable() error {
6166

6267
resp := parseLines(stdout)
6368
if resp[0] != "Hyper-V" {
64-
return fmt.Errorf("Hyper-V PowerShell Module is not available")
69+
return ErrNotInstalled
6570
}
6671

6772
return nil
6873
}
74+
75+
func isAdministrator() (bool, error) {
76+
stdout, err := cmdOut(`@([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")`)
77+
if err != nil {
78+
return false, err
79+
}
80+
81+
resp := parseLines(stdout)
82+
return resp[0] == "True", nil
83+
}

0 commit comments

Comments
 (0)