Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite check for admin #23970

Merged
merged 6 commits into from
Feb 25, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions x-pack/elastic-agent/pkg/agent/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@ would like the Agent to operate.
}

func installCmd(streams *cli.IOStreams, cmd *cobra.Command, flags *globalFlags, args []string) error {
var err error
if !install.HasRoot() {
isAdmin, err := install.HasRoot()
if err != nil {
return fmt.Errorf("unable to perform install command while checking for administrator rights, %v", err)
}
if !isAdmin {
return fmt.Errorf("unable to perform install command, not executed with %s permissions", install.PermissionUser)
}
status, reason := install.Status()
Expand Down
8 changes: 6 additions & 2 deletions x-pack/elastic-agent/pkg/agent/cmd/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ Unless -f is used this command will ask confirmation before performing removal.
}

func uninstallCmd(streams *cli.IOStreams, cmd *cobra.Command, flags *globalFlags, args []string) error {
if !install.HasRoot() {
return fmt.Errorf("unable to perform uninstall command, not executed with %s permissions", install.PermissionUser)
isAdmin, err := install.HasRoot()
if err != nil {
return fmt.Errorf("unable to perform perform command while checking for administrator rights, %v", err)
}
if !isAdmin {
return fmt.Errorf("unable to perform perform command, not executed with %s permissions", install.PermissionUser)
narph marked this conversation as resolved.
Show resolved Hide resolved
}
status, reason := install.Status()
if status == install.NotInstalled {
Expand Down
29 changes: 22 additions & 7 deletions x-pack/elastic-agent/pkg/agent/install/root_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
package install

import (
"os"
"github.com/pkg/errors"
"golang.org/x/sys/windows"
)

const (
Expand All @@ -16,12 +17,26 @@ const (
)

// HasRoot returns true if the user has Administrator/SYSTEM permissions.
func HasRoot() bool {
// only valid rights can open the physical drive
f, err := os.Open("\\\\.\\PHYSICALDRIVE0")
func HasRoot() (bool, error) {
var sid *windows.SID
// See https://docs.microsoft.com/en-us/windows/desktop/api/securitybaseapi/nf-securitybaseapi-checktokenmembership for more on the api
err := windows.AllocateAndInitializeSid(
&windows.SECURITY_NT_AUTHORITY,
2,
windows.SECURITY_BUILTIN_DOMAIN_RID,
windows.DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0,
&sid)
if err != nil {
return false
return false, errors.Errorf("sid error: %s", err)
}
defer f.Close()
return true

token := windows.Token(0)

member, err := token.IsMember(sid)
if err != nil {
return false, errors.Errorf("token membership error: %s", err)
}

return member, nil
}
20 changes: 20 additions & 0 deletions x-pack/elastic-agent/pkg/agent/install/root_windows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

// +build windows

package install

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestHasRoot(t *testing.T) {
t.Run("check if user is admin", func(t *testing.T) {
result, err := HasRoot()
assert.NoError(t, err)
assert.True(t, result)
narph marked this conversation as resolved.
Show resolved Hide resolved
})
}