Skip to content

Commit 2bc8159

Browse files
Resolve users without group files
1 parent 3d2dcd1 commit 2bc8159

2 files changed

Lines changed: 47 additions & 4 deletions

File tree

internal/oci/spec/spec.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,8 @@ type Groups struct {
377377
}
378378

379379
// ParsePasswdFiles parses the passwd and group files at the supplied paths. If
380-
// either path does not exist it returns empty Passwd data.
380+
// the passwd file does not exist it returns empty Passwd data. If the group
381+
// file does not exist it parses passwd data without supplementary groups.
381382
func ParsePasswdFiles(passwd, group string) (Passwd, error) {
382383
p, err := os.Open(passwd) //nolint:gosec // We intentionally take a variable here.
383384
if errors.Is(err, os.ErrNotExist) {
@@ -390,7 +391,7 @@ func ParsePasswdFiles(passwd, group string) (Passwd, error) {
390391

391392
g, err := os.Open(group) //nolint:gosec // We intentionally take a variable here.
392393
if errors.Is(err, os.ErrNotExist) {
393-
return Passwd{}, nil
394+
return ParsePasswd(p, strings.NewReader(""))
394395
}
395396
if err != nil {
396397
return Passwd{}, errors.Wrap(err, errOpenGroupFile)

internal/oci/spec/spec_test.go

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,12 @@ func TestWithHostNetwork(t *testing.T) {
306306
}
307307

308308
func TestWithImageConfig(t *testing.T) {
309+
tmp := t.TempDir()
310+
passwd := filepath.Join(tmp, "passwd")
311+
if err := os.WriteFile(passwd, []byte("negz:x:1000:100::/home/negz:/bin/sh\n"), 0600); err != nil {
312+
t.Fatal(err)
313+
}
314+
309315
type args struct {
310316
cfg *ociv1.ConfigFile
311317
passwd string
@@ -383,6 +389,31 @@ func TestWithImageConfig(t *testing.T) {
383389
},
384390
},
385391
},
392+
"UserWithNoGroupFile": {
393+
reason: "We should resolve image config users when the rootfs has passwd data but no group file.",
394+
s: &runtime.Spec{},
395+
args: args{
396+
cfg: &ociv1.ConfigFile{
397+
Config: ociv1.Config{
398+
Entrypoint: []string{"/bin/sh"},
399+
User: "negz",
400+
},
401+
},
402+
passwd: passwd,
403+
group: filepath.Join(tmp, "group"),
404+
},
405+
want: want{
406+
s: &runtime.Spec{
407+
Process: &runtime.Process{
408+
Args: []string{"/bin/sh"},
409+
User: runtime.User{
410+
UID: 1000,
411+
GID: 100,
412+
},
413+
},
414+
},
415+
},
416+
},
386417
}
387418

388419
for name, tc := range cases {
@@ -578,13 +609,24 @@ users:x:100:primary,doesnotexist
578609
},
579610
},
580611
"NoGroupFile": {
581-
reason: "We should not return an error if the group file doesn't exist.",
612+
reason: "We should parse passwd data without supplementary groups if the group file doesn't exist.",
582613
args: args{
583614
passwd: filepath.Join(tmp, "passwd"),
584615
group: filepath.Join(tmp, "nonexist"),
585616
},
586617
want: want{
587-
p: Passwd{},
618+
p: Passwd{
619+
UID: map[Username]UID{
620+
"root": 0,
621+
"negz": 1000,
622+
"primary": 1001,
623+
},
624+
Groups: map[UID]Groups{
625+
0: {PrimaryGID: 0},
626+
1000: {PrimaryGID: 100},
627+
1001: {PrimaryGID: 100},
628+
},
629+
},
588630
},
589631
},
590632
"Success": {

0 commit comments

Comments
 (0)