Skip to content

Commit 9489c0e

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 3dd1e88 commit 9489c0e

2 files changed

Lines changed: 64 additions & 3 deletions

File tree

oci/spec_opts.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -873,9 +873,9 @@ func WithAppendAdditionalGroups(groups ...string) SpecOpts {
873873
if err != nil {
874874
return err
875875
}
876-
ugroups, err := user.ParseGroupFile(gpath)
877-
if err != nil {
878-
return err
876+
ugroups, groupErr := user.ParseGroupFile(gpath)
877+
if groupErr != nil && !os.IsNotExist(groupErr) {
878+
return groupErr
879879
}
880880
groupMap := make(map[string]user.Group)
881881
for _, group := range ugroups {
@@ -889,6 +889,9 @@ func WithAppendAdditionalGroups(groups ...string) SpecOpts {
889889
} else {
890890
g, ok := groupMap[group]
891891
if !ok {
892+
if groupErr != nil {
893+
return fmt.Errorf("unable to find group %s: %w", group, groupErr)
894+
}
892895
return fmt.Errorf("unable to find group %s", group)
893896
}
894897
gids = append(gids, uint32(g.Gid))

oci/spec_opts_linux_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,3 +516,61 @@ daemon:x:2:root,bin,daemon
516516
})
517517
}
518518
}
519+
520+
func TestWithAppendAdditionalGroupsNoEtcGroup(t *testing.T) {
521+
t.Parallel()
522+
td := t.TempDir()
523+
apply := fstest.Apply()
524+
if err := apply.Apply(td); err != nil {
525+
t.Fatalf("failed to apply: %v", err)
526+
}
527+
c := containers.Container{ID: t.Name()}
528+
529+
testCases := []struct {
530+
name string
531+
additionalGIDs []uint32
532+
groups []string
533+
expected []uint32
534+
err string
535+
}{
536+
{
537+
name: "no additional gids",
538+
groups: []string{},
539+
expected: []uint32{0},
540+
},
541+
{
542+
name: "no additional gids, append root group",
543+
groups: []string{"root"},
544+
err: fmt.Sprintf("unable to find group root: open %s: no such file or directory", filepath.Join(td, "etc", "group")),
545+
expected: []uint32{0},
546+
},
547+
{
548+
name: "append group id",
549+
groups: []string{"999"},
550+
expected: []uint32{0, 999},
551+
},
552+
}
553+
554+
for _, testCase := range testCases {
555+
testCase := testCase
556+
t.Run(testCase.name, func(t *testing.T) {
557+
t.Parallel()
558+
s := Spec{
559+
Version: specs.Version,
560+
Root: &specs.Root{
561+
Path: td,
562+
},
563+
Process: &specs.Process{
564+
User: specs.User{
565+
AdditionalGids: testCase.additionalGIDs,
566+
},
567+
},
568+
}
569+
err := WithAppendAdditionalGroups(testCase.groups...)(context.Background(), nil, &c, &s)
570+
if err != nil {
571+
assert.EqualError(t, err, testCase.err)
572+
}
573+
assert.Equal(t, testCase.expected, s.Process.User.AdditionalGids)
574+
})
575+
}
576+
}

0 commit comments

Comments
 (0)