Skip to content

Commit 1398186

Browse files
committed
WithAppendAdditionalGroups: better /etc/group handling
Scratch images don't necessarily have the /etc/group file, so we shouldn't fail if opening/parsing it is not needed: if all the group to add are numeric. Signed-off-by: Djordje Lukic <[email protected]>
1 parent 75f72d6 commit 1398186

2 files changed

Lines changed: 65 additions & 3 deletions

File tree

oci/spec_opts.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -894,9 +894,9 @@ func WithAppendAdditionalGroups(groups ...string) SpecOpts {
894894
if err != nil {
895895
return err
896896
}
897-
ugroups, err := user.ParseGroupFile(gpath)
898-
if err != nil {
899-
return err
897+
ugroups, groupErr := user.ParseGroupFile(gpath)
898+
if groupErr != nil && !os.IsNotExist(groupErr) {
899+
return groupErr
900900
}
901901
groupMap := make(map[string]user.Group)
902902
for _, group := range ugroups {
@@ -910,6 +910,9 @@ func WithAppendAdditionalGroups(groups ...string) SpecOpts {
910910
} else {
911911
g, ok := groupMap[group]
912912
if !ok {
913+
if groupErr != nil {
914+
return fmt.Errorf("unable to find group %s: %w", group, groupErr)
915+
}
913916
return fmt.Errorf("unable to find group %s", group)
914917
}
915918
gids = append(gids, uint32(g.Gid))

oci/spec_opts_linux_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,65 @@ daemon:x:2:root,bin,daemon
627627
})
628628
}
629629
}
630+
631+
func TestWithAppendAdditionalGroupsNoEtcGroup(t *testing.T) {
632+
t.Parallel()
633+
td := t.TempDir()
634+
apply := fstest.Apply()
635+
if err := apply.Apply(td); err != nil {
636+
t.Fatalf("failed to apply: %v", err)
637+
}
638+
c := containers.Container{ID: t.Name()}
639+
640+
testCases := []struct {
641+
name string
642+
additionalGIDs []uint32
643+
groups []string
644+
expected []uint32
645+
err string
646+
}{
647+
{
648+
name: "no additional gids",
649+
groups: []string{},
650+
expected: []uint32{0},
651+
},
652+
{
653+
name: "no additional gids, append root group",
654+
groups: []string{"root"},
655+
err: fmt.Sprintf("unable to find group root: open %s: no such file or directory", filepath.Join(td, "etc", "group")),
656+
expected: []uint32{0},
657+
},
658+
{
659+
name: "append group id",
660+
groups: []string{"999"},
661+
expected: []uint32{0, 999},
662+
},
663+
}
664+
665+
for _, testCase := range testCases {
666+
testCase := testCase
667+
t.Run(testCase.name, func(t *testing.T) {
668+
t.Parallel()
669+
s := Spec{
670+
Version: specs.Version,
671+
Root: &specs.Root{
672+
Path: td,
673+
},
674+
Process: &specs.Process{
675+
User: specs.User{
676+
AdditionalGids: testCase.additionalGIDs,
677+
},
678+
},
679+
}
680+
err := WithAppendAdditionalGroups(testCase.groups...)(context.Background(), nil, &c, &s)
681+
if err != nil {
682+
assert.EqualError(t, err, testCase.err)
683+
}
684+
assert.Equal(t, testCase.expected, s.Process.User.AdditionalGids)
685+
})
686+
}
687+
}
688+
630689
func TestWithLinuxDeviceFollowSymlinks(t *testing.T) {
631690

632691
// Create symlink to /dev/zero for the symlink test case

0 commit comments

Comments
 (0)