Skip to content

Commit e4cfb55

Browse files
authored
Merge pull request kubernetes#77989 from ksubrmnn/kubeadm
Use os package for Windows IsPrivilegedUserCheck
2 parents d45cc10 + aa8efc5 commit e4cfb55

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

cmd/kubeadm/app/preflight/checks_windows.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,38 @@ limitations under the License.
1919
package preflight
2020

2121
import (
22-
"os/exec"
23-
"strings"
22+
"os/user"
2423

2524
"github.com/pkg/errors"
2625
)
2726

28-
// Check validates if an user has elevated (administrator) privileges.
27+
// The "Well-known SID" of Administrator group
28+
// https://support.microsoft.com/en-us/help/243330/well-known-security-identifiers-in-windows-operating-systems
29+
const administratorSID = "S-1-5-32-544"
30+
31+
// Check validates if a user has elevated (administrator) privileges.
2932
func (ipuc IsPrivilegedUserCheck) Check() (warnings, errorList []error) {
3033
errorList = []error{}
3134

32-
// The "Well-known SID" of Administrator group is S-1-5-32-544
33-
// The following powershell will return "True" if run as an administrator, "False" otherwise
34-
// See https://msdn.microsoft.com/en-us/library/cc980032.aspx
35-
args := []string{"[bool](([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match \"S-1-5-32-544\")"}
36-
isAdmin, err := exec.Command("powershell", args...).Output()
35+
currUser, err := user.Current()
36+
if err != nil {
37+
errorList = append(errorList, errors.New("cannot get current user"))
38+
return nil, errorList
39+
}
3740

41+
groupIds, err := currUser.GroupIds()
3842
if err != nil {
39-
errorList = append(errorList, errors.Wrap(err, "unable to determine if user is running as administrator"))
40-
} else if strings.EqualFold(strings.TrimSpace(string(isAdmin)), "false") {
41-
errorList = append(errorList, errors.New("user is not running as administrator"))
43+
errorList = append(errorList, errors.New("cannot get group IDs for current user"))
44+
return nil, errorList
45+
}
46+
47+
for _, sid := range groupIds {
48+
if sid == administratorSID {
49+
return nil, errorList
50+
}
4251
}
4352

53+
errorList = append(errorList, errors.New("user is not running as administrator"))
4454
return nil, errorList
4555
}
4656

0 commit comments

Comments
 (0)