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 4 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
9 changes: 6 additions & 3 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 All @@ -58,7 +61,7 @@ func installCmd(streams *cli.IOStreams, cmd *cobra.Command, flags *globalFlags,

// check the lock to ensure that elastic-agent is not already running in this directory
locker := application.NewAppLocker(paths.Data(), agentLockFileName)
if err := locker.TryLock(); err != nil {
if err = locker.TryLock(); err != nil {
narph marked this conversation as resolved.
Show resolved Hide resolved
if err == application.ErrAppAlreadyRunning {
return fmt.Errorf("cannot perform installation as Elastic Agent is already running from this directory")
}
Expand Down
10 changes: 7 additions & 3 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 command while checking for administrator rights, %v", err)
}
if !isAdmin {
return fmt.Errorf("unable to perform command, not executed with %s permissions", install.PermissionUser)
}
status, reason := install.Status()
if status == install.NotInstalled {
Expand Down Expand Up @@ -72,7 +76,7 @@ func uninstallCmd(streams *cli.IOStreams, cmd *cobra.Command, flags *globalFlags
}
}

err := install.Uninstall()
err = install.Uninstall()
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions x-pack/elastic-agent/pkg/agent/install/root_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const (
)

// HasRoot returns true if the user has root permissions.
func HasRoot() bool {
return os.Getegid() == 0
// Added extra `nil` value to return since the HasRoot for windows will return an error as well
func HasRoot() (bool, error) {
return os.Getegid() == 0, nil
}
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 (
"testing"

"github.com/stretchr/testify/assert"
)

func TestHasRoot(t *testing.T) {
t.Run("check if user is admin", func(t *testing.T) {
_, err := HasRoot()
assert.NoError(t, err)
})
}