Skip to content

Commit

Permalink
try to not require root if we are running as the user we want
Browse files Browse the repository at this point in the history
  • Loading branch information
b-dean committed Jan 26, 2024
1 parent 061bbdf commit efcd179
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
13 changes: 9 additions & 4 deletions ensureuserisroot.go → checkuser.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@ import (
"os/user"
)

func ensureUserIsRoot() error {
user, err := user.Current()
func checkUser(username string) error {
cur, err := user.Current()
if err != nil {
return err
}

if user.Uid != "0" {
return fmt.Errorf("must be run as root")
other, err := user.Lookup(username)
if err != nil {
return err
}

if cur.Uid != other.Uid {
return fmt.Errorf("must be run as %s", username)
}

return nil
Expand Down
7 changes: 6 additions & 1 deletion instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,12 @@ func (i *Instance) ExecuteAsManager() error {
// This command only functions if the calling program is running as root.
// It returns any error encountered.
func (i *Instance) ExecuteAsUser(execUser string) error {
if err := ensureUserIsRoot(); err != nil {
// no need to switch users if we're already who we want to be
if err := checkUser(execUser); err == nil {
return nil
}

if err := checkUser("root"); err != nil {
return err
}

Expand Down

0 comments on commit efcd179

Please sign in to comment.