Skip to content

Commit 55e5708

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 64b8a81 commit 55e5708

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
@@ -893,9 +893,9 @@ func WithAppendAdditionalGroups(groups ...string) SpecOpts {
893893
if err != nil {
894894
return err
895895
}
896-
ugroups, err := user.ParseGroupFile(gpath)
897-
if err != nil {
898-
return err
896+
ugroups, groupErr := user.ParseGroupFile(gpath)
897+
if groupErr != nil && !os.IsNotExist(groupErr) {
898+
return groupErr
899899
}
900900
groupMap := make(map[string]user.Group)
901901
for _, group := range ugroups {
@@ -909,6 +909,9 @@ func WithAppendAdditionalGroups(groups ...string) SpecOpts {
909909
} else {
910910
g, ok := groupMap[group]
911911
if !ok {
912+
if groupErr != nil {
913+
return fmt.Errorf("unable to find group %s: %w", group, groupErr)
914+
}
912915
return fmt.Errorf("unable to find group %s", group)
913916
}
914917
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
@@ -517,6 +517,65 @@ daemon:x:2:root,bin,daemon
517517
})
518518
}
519519
}
520+
521+
func TestWithAppendAdditionalGroupsNoEtcGroup(t *testing.T) {
522+
t.Parallel()
523+
td := t.TempDir()
524+
apply := fstest.Apply()
525+
if err := apply.Apply(td); err != nil {
526+
t.Fatalf("failed to apply: %v", err)
527+
}
528+
c := containers.Container{ID: t.Name()}
529+
530+
testCases := []struct {
531+
name string
532+
additionalGIDs []uint32
533+
groups []string
534+
expected []uint32
535+
err string
536+
}{
537+
{
538+
name: "no additional gids",
539+
groups: []string{},
540+
expected: []uint32{0},
541+
},
542+
{
543+
name: "no additional gids, append root group",
544+
groups: []string{"root"},
545+
err: fmt.Sprintf("unable to find group root: open %s: no such file or directory", filepath.Join(td, "etc", "group")),
546+
expected: []uint32{0},
547+
},
548+
{
549+
name: "append group id",
550+
groups: []string{"999"},
551+
expected: []uint32{0, 999},
552+
},
553+
}
554+
555+
for _, testCase := range testCases {
556+
testCase := testCase
557+
t.Run(testCase.name, func(t *testing.T) {
558+
t.Parallel()
559+
s := Spec{
560+
Version: specs.Version,
561+
Root: &specs.Root{
562+
Path: td,
563+
},
564+
Process: &specs.Process{
565+
User: specs.User{
566+
AdditionalGids: testCase.additionalGIDs,
567+
},
568+
},
569+
}
570+
err := WithAppendAdditionalGroups(testCase.groups...)(context.Background(), nil, &c, &s)
571+
if err != nil {
572+
assert.EqualError(t, err, testCase.err)
573+
}
574+
assert.Equal(t, testCase.expected, s.Process.User.AdditionalGids)
575+
})
576+
}
577+
}
578+
520579
func TestWithLinuxDeviceFollowSymlinks(t *testing.T) {
521580

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

0 commit comments

Comments
 (0)