Skip to content

Commit

Permalink
add admin check
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkb7 committed Mar 7, 2025
1 parent 22bdd27 commit e348bc7
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 8 deletions.
6 changes: 2 additions & 4 deletions cmd/installer/subcommands/subcommands.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
package subcommands

import (
"fmt"

"github.com/DataDog/datadog-agent/cmd/installer/command"
"github.com/DataDog/datadog-agent/cmd/installer/subcommands/daemon"
"github.com/DataDog/datadog-agent/cmd/installer/user"
Expand Down Expand Up @@ -42,7 +40,7 @@ func withRoot(factory command.SubcommandFactory) command.SubcommandFactory {
return nil
}
if !user.IsRoot() {
return fmt.Errorf("this command requires root privileges")
return user.ErrRootRequired
}
return user.DatadogAgentToRoot()
})
Expand All @@ -54,7 +52,7 @@ func withDatadogAgent(factory command.SubcommandFactory) command.SubcommandFacto
return nil
}
if !user.IsRoot() {
return fmt.Errorf("this command requires root privileges")
return user.ErrRootRequired
}
return user.RootToDatadogAgent()
})
Expand Down
8 changes: 7 additions & 1 deletion cmd/installer/user/user_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
// Package user provides helpers to change the user of the process.
package user

import "syscall"
import (
"fmt"
"syscall"
)

// ErrRootRequired is the error returned when an operation requires root privileges.
var ErrRootRequired = fmt.Errorf("operation requires root privileges")

// IsRoot always returns true on darwin.
func IsRoot() bool {
Expand Down
3 changes: 3 additions & 0 deletions cmd/installer/user/user_nix.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import (
"syscall"
)

// ErrRootRequired is the error returned when an operation requires root privileges.
var ErrRootRequired = fmt.Errorf("operation requires root privileges")

// IsRoot returns true if the process is running as root.
func IsRoot() bool {
return syscall.Getuid() == 0
Expand Down
17 changes: 15 additions & 2 deletions cmd/installer/user/user_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,22 @@
// Package user provides helpers to change the user of the process.
package user

// IsRoot always returns true on windows.
import (
"fmt"

"github.com/DataDog/datadog-agent/pkg/util/winutil"
)

// ErrRootRequired is the error returned when an operation requires Administrator privileges.
var ErrRootRequired = fmt.Errorf("operation requires Administrator privileges")

// IsRoot returns true if token has Administrators group enabled
func IsRoot() bool {
return true
isAdmin, err := winutil.IsUserAnAdmin()
if err != nil {
fmt.Printf("error checking if user is admin: %v\n", err)
}
return isAdmin
}

// RootToDatadogAgent is a noop on windows.
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/winutil/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func IsUserAnAdmin() (bool, error) {
0, 0, 0, 0, 0, 0,
&administratorsGroup)
if err != nil {
return false, fmt.Errorf("could not get local system SID: %w", err)
return false, fmt.Errorf("could not get Administrators group SID: %w", err)
}
defer windows.FreeSid(administratorsGroup)

Expand Down

0 comments on commit e348bc7

Please sign in to comment.