mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Do group lookup in listeners package
This used to be handled by go-connections, but now it only takes a group ID (int). Signed-off-by: Brian Goff <cpuguy83@gmail.com>
This commit is contained in:
parent
e5d77c64a2
commit
bdf4cad1d1
3 changed files with 39 additions and 4 deletions
32
pkg/listeners/group_unix.go
Normal file
32
pkg/listeners/group_unix.go
Normal file
|
@ -0,0 +1,32 @@
|
|||
// +build !windows
|
||||
|
||||
package listeners
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/opencontainers/runc/libcontainer/user"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func lookupGID(name string) (int, error) {
|
||||
groupFile, err := user.GetGroupPath()
|
||||
if err != nil {
|
||||
return -1, errors.Wrap(err, "error looking up groups")
|
||||
}
|
||||
groups, err := user.ParseGroupFileFilter(groupFile, func(g user.Group) bool {
|
||||
return g.Name == name || strconv.Itoa(g.Gid) == name
|
||||
})
|
||||
if err != nil {
|
||||
return -1, errors.Wrapf(err, "error parsing groups for %s", name)
|
||||
}
|
||||
if groups != nil && len(groups) > 0 {
|
||||
return groups[0].Gid, nil
|
||||
}
|
||||
gid, err := strconv.Atoi(name)
|
||||
if err == nil {
|
||||
return gid, nil
|
||||
}
|
||||
return -1, fmt.Errorf("group %s not found", name)
|
||||
}
|
|
@ -18,7 +18,11 @@ func Init(proto, addr, socketGroup string, tlsConfig *tls.Config) (ls []net.List
|
|||
}
|
||||
ls = append(ls, l)
|
||||
case "unix":
|
||||
l, err := sockets.NewUnixSocket(addr, socketGroup)
|
||||
gid, err := lookupGID(socketGroup)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
l, err := sockets.NewUnixSocket(addr, gid)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("can't create unix socket %s: %v", addr, err)
|
||||
}
|
||||
|
|
|
@ -31,10 +31,9 @@ func Init(proto, addr, socketGroup string, tlsConfig *tls.Config) ([]net.Listene
|
|||
}
|
||||
ls = append(ls, l)
|
||||
case "unix":
|
||||
|
||||
gid, err := strconv.Atoi(socketGroup)
|
||||
gid, err := lookupGID(socketGroup)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse socket group id: should be a number: %v", socketGroup)
|
||||
return nil, err
|
||||
}
|
||||
l, err := sockets.NewUnixSocket(addr, gid)
|
||||
if err != nil {
|
||||
|
|
Loading…
Add table
Reference in a new issue