mirror of
https://github.com/kemko/nomad.git
synced 2026-01-05 01:45:44 +03:00
Refactor out userLookup to helper package
Also replaces user.Lookup in exec driver
This commit is contained in:
@@ -5,11 +5,10 @@ package allocdir
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"github.com/hashicorp/nomad/helper/user-lookup"
|
||||
"os"
|
||||
"os/user"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
@@ -28,7 +27,7 @@ func (d *AllocDir) dropDirPermissions(path string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
u, err := userLookup("nobody")
|
||||
u, err := userlookup.Lookup("nobody")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -71,28 +70,3 @@ func getGid(u *user.User) (int, error) {
|
||||
|
||||
return gid, nil
|
||||
}
|
||||
|
||||
// userLookup checks if the given username or uid is present in /etc/passwd
|
||||
// and returns the user struct.
|
||||
// If the username is not found, an error is returned.
|
||||
// Credit to @creak, https://github.com/docker/docker/pull/1096
|
||||
func userLookup(uid string) (*user.User, error) {
|
||||
file, err := ioutil.ReadFile("/etc/passwd")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, line := range strings.Split(string(file), "\n") {
|
||||
data := strings.Split(line, ":")
|
||||
if len(data) > 5 && (data[0] == uid || data[2] == uid) {
|
||||
return &user.User{
|
||||
Uid: data[2],
|
||||
Gid: data[3],
|
||||
Username: data[0],
|
||||
Name: data[4],
|
||||
HomeDir: data[5],
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("User not found in /etc/passwd")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user