@@ -19,28 +19,38 @@ limitations under the License.
19
19
package preflight
20
20
21
21
import (
22
- "os/exec"
23
- "strings"
22
+ "os/user"
24
23
25
24
"github.com/pkg/errors"
26
25
)
27
26
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.
29
32
func (ipuc IsPrivilegedUserCheck ) Check () (warnings , errorList []error ) {
30
33
errorList = []error {}
31
34
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
+ }
37
40
41
+ groupIds , err := currUser .GroupIds ()
38
42
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
+ }
42
51
}
43
52
53
+ errorList = append (errorList , errors .New ("user is not running as administrator" ))
44
54
return nil , errorList
45
55
}
46
56
0 commit comments