From 85f722dea018724f8aa083c50d268a5888aea424 Mon Sep 17 00:00:00 2001 From: Aleksa Sarai Date: Sun, 28 Jun 2015 11:14:24 +1000 Subject: [PATCH] libcontainer: user: fix GetAdditionalGroupsPath to match API The old GetAdditionalGroups* API didn't match the rest of libcontainer/user, we make functions that take io.Readers and then make wrappers around them. Otherwise we have to do dodgy stuff when testing our code. Fixes: d4ece29c0bd38 ("refactor GetAdditionalGroupsPath") Signed-off-by: Aleksa Sarai --- libcontainer/user/user.go | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/libcontainer/user/user.go b/libcontainer/user/user.go index 13226dbf..964e31bf 100644 --- a/libcontainer/user/user.go +++ b/libcontainer/user/user.go @@ -349,17 +349,12 @@ func GetExecUser(userSpec string, defaults *ExecUser, passwd, group io.Reader) ( return user, nil } -// GetAdditionalGroupsPath looks up a list of groups by name or group id -// against the group file. If a group name cannot be found, an error will be -// returned. If a group id cannot be found, it will be returned as-is. -func GetAdditionalGroupsPath(additionalGroups []string, groupPath string) ([]int, error) { - groupReader, err := os.Open(groupPath) - if err != nil { - return nil, fmt.Errorf("Failed to open group file: %v", err) - } - defer groupReader.Close() - - groups, err := ParseGroupFilter(groupReader, func(g Group) bool { +// GetAdditionalGroups looks up a list of groups by name or group id against +// against the given /etc/group formatted data. If a group name cannot be found, +// an error will be returned. If a group id cannot be found, it will be returned +// as-is. +func GetAdditionalGroups(additionalGroups []string, group io.Reader) ([]int, error) { + groups, err := ParseGroupFilter(group, func(g Group) bool { for _, ag := range additionalGroups { if g.Name == ag || strconv.Itoa(g.Gid) == ag { return true @@ -405,3 +400,14 @@ func GetAdditionalGroupsPath(additionalGroups []string, groupPath string) ([]int } return gids, nil } + +// Wrapper around GetAdditionalGroups that opens the groupPath given and gives +// it as an argument to GetAdditionalGroups. +func GetAdditionalGroupsPath(additionalGroups []string, groupPath string) ([]int, error) { + group, err := os.Open(groupPath) + if err != nil { + return nil, fmt.Errorf("Failed to open group file: %v", err) + } + defer group.Close() + return GetAdditionalGroups(additionalGroups, group) +}