Skip to content

Commit

Permalink
Merge pull request containerd#9543 from rumpl/1.6_backport_fix-append…
Browse files Browse the repository at this point in the history
…-additonal-groups

[release/1.6 backport] WithAppendAdditionalGroups: better /etc/group handling
  • Loading branch information
dmcgowan authored Dec 18, 2023
2 parents b1c88a9 + 9489c0e commit 5c13c78
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
9 changes: 6 additions & 3 deletions oci/spec_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -873,9 +873,9 @@ func WithAppendAdditionalGroups(groups ...string) SpecOpts {
if err != nil {
return err
}
ugroups, err := user.ParseGroupFile(gpath)
if err != nil {
return err
ugroups, groupErr := user.ParseGroupFile(gpath)
if groupErr != nil && !os.IsNotExist(groupErr) {
return groupErr
}
groupMap := make(map[string]user.Group)
for _, group := range ugroups {
Expand All @@ -889,6 +889,9 @@ func WithAppendAdditionalGroups(groups ...string) SpecOpts {
} else {
g, ok := groupMap[group]
if !ok {
if groupErr != nil {
return fmt.Errorf("unable to find group %s: %w", group, groupErr)
}
return fmt.Errorf("unable to find group %s", group)
}
gids = append(gids, uint32(g.Gid))
Expand Down
58 changes: 58 additions & 0 deletions oci/spec_opts_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,3 +516,61 @@ daemon:x:2:root,bin,daemon
})
}
}

func TestWithAppendAdditionalGroupsNoEtcGroup(t *testing.T) {
t.Parallel()
td := t.TempDir()
apply := fstest.Apply()
if err := apply.Apply(td); err != nil {
t.Fatalf("failed to apply: %v", err)
}
c := containers.Container{ID: t.Name()}

testCases := []struct {
name string
additionalGIDs []uint32
groups []string
expected []uint32
err string
}{
{
name: "no additional gids",
groups: []string{},
expected: []uint32{0},
},
{
name: "no additional gids, append root group",
groups: []string{"root"},
err: fmt.Sprintf("unable to find group root: open %s: no such file or directory", filepath.Join(td, "etc", "group")),
expected: []uint32{0},
},
{
name: "append group id",
groups: []string{"999"},
expected: []uint32{0, 999},
},
}

for _, testCase := range testCases {
testCase := testCase
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()
s := Spec{
Version: specs.Version,
Root: &specs.Root{
Path: td,
},
Process: &specs.Process{
User: specs.User{
AdditionalGids: testCase.additionalGIDs,
},
},
}
err := WithAppendAdditionalGroups(testCase.groups...)(context.Background(), nil, &c, &s)
if err != nil {
assert.EqualError(t, err, testCase.err)
}
assert.Equal(t, testCase.expected, s.Process.User.AdditionalGids)
})
}
}

0 comments on commit 5c13c78

Please sign in to comment.