Repository: chainguard-dev/apko
Pull request #73 (2022-03-08 15:02:45)

fix: create passwd/group file if missing
Closes https://github.com/chainguard-dev/apko/issues/71


Patch:
diff --git a/pkg/build/accounts.go b/pkg/build/accounts.go
index 4112393..99965d4 100644
--- a/pkg/build/accounts.go
+++ b/pkg/build/accounts.go
@@ -61,7 +61,7 @@ func (bc *Context) MutateAccounts() error {
 		eg.Go(func() error {
 			path := filepath.Join(bc.WorkDir, "etc", "group")
 
-			gf, err := passwd.ReadGroupFile(path)
+			gf, err := passwd.ReadOrCreateGroupFile(path)
 			if err != nil {
 				return err
 			}
@@ -83,7 +83,7 @@ func (bc *Context) MutateAccounts() error {
 		eg.Go(func() error {
 			path := filepath.Join(bc.WorkDir, "etc", "passwd")
 
-			uf, err := passwd.ReadUserFile(path)
+			uf, err := passwd.ReadOrCreateUserFile(path)
 			if err != nil {
 				return err
 			}
diff --git a/pkg/passwd/group.go b/pkg/passwd/group.go
index 62d3b21..9898a58 100644
--- a/pkg/passwd/group.go
+++ b/pkg/passwd/group.go
@@ -37,10 +37,10 @@ type GroupFile struct {
 }
 
 // Parse an /etc/group file into a GroupFile.
-func ReadGroupFile(filePath string) (GroupFile, error) {
+func ReadOrCreateGroupFile(filePath string) (GroupFile, error) {
 	gf := GroupFile{}
 
-	file, err := os.Open(filePath)
+	file, err := os.OpenFile(filePath, os.O_RDONLY|os.O_CREATE, 0o644)
 	if err != nil {
 		return gf, fmt.Errorf("failed to open %s: %w", filePath, err)
 	}
diff --git a/pkg/passwd/passwd.go b/pkg/passwd/passwd.go
index 7626ddc..064b274 100644
--- a/pkg/passwd/passwd.go
+++ b/pkg/passwd/passwd.go
@@ -40,10 +40,10 @@ type UserFile struct {
 }
 
 // Parse an /etc/passwd file into a UserFile.
-func ReadUserFile(filePath string) (UserFile, error) {
+func ReadOrCreateUserFile(filePath string) (UserFile, error) {
 	uf := UserFile{}
 
-	file, err := os.Open(filePath)
+	file, err := os.OpenFile(filePath, os.O_RDONLY|os.O_CREATE, 0o644)
 	if err != nil {
 		return uf, fmt.Errorf("failed to open %s: %w", filePath, err)
 	}


Test patch:
diff --git a/pkg/passwd/group_test.go b/pkg/passwd/group_test.go
index 373412e..d0960b0 100644
--- a/pkg/passwd/group_test.go
+++ b/pkg/passwd/group_test.go
@@ -20,7 +20,7 @@ import (
 )
 
 func TestGroupParser(t *testing.T) {
-	gf, err := ReadGroupFile("testdata/group")
+	gf, err := ReadOrCreateGroupFile("testdata/group")
 	if err != nil {
 		t.Errorf("error while parsing; %v", err)
 	}
@@ -37,7 +37,7 @@ func TestGroupParser(t *testing.T) {
 }
 
 func TestGroupWriter(t *testing.T) {
-	gf, err := ReadGroupFile("testdata/group")
+	gf, err := ReadOrCreateGroupFile("testdata/group")
 	if err != nil {
 		t.Errorf("error while parsing; %v", err)
 	}
diff --git a/pkg/passwd/passwd_test.go b/pkg/passwd/passwd_test.go
index 3e53b55..b79931e 100644
--- a/pkg/passwd/passwd_test.go
+++ b/pkg/passwd/passwd_test.go
@@ -20,7 +20,7 @@ import (
 )
 
 func TestParser(t *testing.T) {
-	uf, err := ReadUserFile("testdata/passwd")
+	uf, err := ReadOrCreateUserFile("testdata/passwd")
 	if err != nil {
 		t.Errorf("error while parsing; %v", err)
 	}
@@ -37,7 +37,7 @@ func TestParser(t *testing.T) {
 }
 
 func TestWriter(t *testing.T) {
-	uf, err := ReadUserFile("testdata/passwd")
+	uf, err := ReadOrCreateUserFile("testdata/passwd")
 	if err != nil {
 		t.Errorf("error while parsing; %v", err)
 	}