mirror of
https://github.com/opencontainers/runc.git
synced 2026-07-11 06:03:57 +08:00
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: d4ece29c0b ("refactor GetAdditionalGroupsPath")
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
This commit is contained in:
+17
-11
@@ -349,17 +349,12 @@ func GetExecUser(userSpec string, defaults *ExecUser, passwd, group io.Reader) (
|
|||||||
return user, nil
|
return user, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAdditionalGroupsPath looks up a list of groups by name or group id
|
// GetAdditionalGroups looks up a list of groups by name or group id against
|
||||||
// against the group file. If a group name cannot be found, an error will be
|
// against the given /etc/group formatted data. If a group name cannot be found,
|
||||||
// returned. If a group id cannot be found, it will be returned as-is.
|
// an error will be returned. If a group id cannot be found, it will be returned
|
||||||
func GetAdditionalGroupsPath(additionalGroups []string, groupPath string) ([]int, error) {
|
// as-is.
|
||||||
groupReader, err := os.Open(groupPath)
|
func GetAdditionalGroups(additionalGroups []string, group io.Reader) ([]int, error) {
|
||||||
if err != nil {
|
groups, err := ParseGroupFilter(group, func(g Group) bool {
|
||||||
return nil, fmt.Errorf("Failed to open group file: %v", err)
|
|
||||||
}
|
|
||||||
defer groupReader.Close()
|
|
||||||
|
|
||||||
groups, err := ParseGroupFilter(groupReader, func(g Group) bool {
|
|
||||||
for _, ag := range additionalGroups {
|
for _, ag := range additionalGroups {
|
||||||
if g.Name == ag || strconv.Itoa(g.Gid) == ag {
|
if g.Name == ag || strconv.Itoa(g.Gid) == ag {
|
||||||
return true
|
return true
|
||||||
@@ -405,3 +400,14 @@ func GetAdditionalGroupsPath(additionalGroups []string, groupPath string) ([]int
|
|||||||
}
|
}
|
||||||
return gids, nil
|
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)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user