Skip to content

Commit

Permalink
Consolidate build user/group strings
Browse files Browse the repository at this point in the history
Signed-off-by: egibs <20933572+egibs@users.noreply.github.com>
  • Loading branch information
egibs committed Feb 7, 2025
1 parent cf2e844 commit 94e2c14
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
11 changes: 5 additions & 6 deletions pkg/build/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ var requireErrInvalidConfiguration require.ErrorAssertionFunc = func(t require.T
// TestConfiguration_Load is the main set of tests for loading a configuration
// file. When in doubt, add your test here.
func TestConfiguration_Load(t *testing.T) {
gid1000 := uint32(1000)
tests := []struct {
name string
skipConfigCleanStep bool
Expand Down Expand Up @@ -209,8 +208,8 @@ func TestConfiguration_Load(t *testing.T) {
"GOPATH": "/var/cache/melange/go",
},
Accounts: apko_types.ImageAccounts{
Users: []apko_types.User{{UserName: "build", UID: 1000, GID: apko_types.GID(&gid1000)}},
Groups: []apko_types.Group{{GroupName: "build", GID: 1000, Members: []string{"build"}}},
Users: []apko_types.User{{UserName: buildUser, UID: 1000, GID: apko_types.GID(&gid1000)}},
Groups: []apko_types.Group{{GroupName: buildUser, GID: 1000, Members: []string{buildUser}}},
},
},
Subpackages: []config.Subpackage{},
Expand Down Expand Up @@ -285,14 +284,14 @@ package:
}
gid1000 := uint32(1000)
expected.Environment.Accounts.Users = []apko_types.User{{
UserName: "build",
UserName: buildUser,
UID: 1000,
GID: apko_types.GID(&gid1000),
}}
expected.Environment.Accounts.Groups = []apko_types.Group{{
GroupName: "build",
GroupName: buildUser,
GID: 1000,
Members: []string{"build"},
Members: []string{buildUser},
}}
expected.Environment.Environment = map[string]string{
"HOME": "/home/build",
Expand Down
8 changes: 4 additions & 4 deletions pkg/build/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ const (
homeBuild = "/home/build"
)

var (
gid1000 = uint32(1000)
)
const buildUser = "build"

var gid1000 = uint32(1000)

func defaultEnv(opts ...func(*apko_types.ImageConfiguration)) apko_types.ImageConfiguration {
env := apko_types.ImageConfiguration{
Accounts: types.ImageAccounts{
Groups: []types.Group{{GroupName: "build", GID: 1000, Members: []string{"build"}}},
Groups: []types.Group{{GroupName: "build", GID: 1000, Members: []string{buildUser}}},
Users: []apko_types.User{{UserName: "build", UID: 1000, GID: apko_types.GID(&gid1000)}},
},
}
Expand Down
24 changes: 14 additions & 10 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ import (
"chainguard.dev/melange/pkg/util"
)

const purlTypeAPK = "apk"
const (
buildUser = "build"
purlTypeAPK = "apk"
)

type Trigger struct {
// Optional: The script to run
Expand Down Expand Up @@ -1360,12 +1363,19 @@ func ParseConfiguration(_ context.Context, configurationFilePath string, opts ..
cfg.Data = nil // TODO: zero this out or not?

// TODO: validate that subpackage ranges have been consumed and applied

grpName := buildUser
grp := apko_types.Group{
GroupName: "build",
GroupName: grpName,
GID: 1000,
Members: []string{"build"},
Members: []string{buildUser},
}

usr := apko_types.User{
UserName: buildUser,
UID: 1000,
GID: apko_types.GID(&grp.GID),
}

cfg.Environment.Accounts.Groups = append(cfg.Environment.Accounts.Groups, grp)
if cfg.Test != nil {
cfg.Test.Environment.Accounts.Groups = append(cfg.Test.Environment.Accounts.Groups, grp)
Expand All @@ -1377,12 +1387,6 @@ func ParseConfiguration(_ context.Context, configurationFilePath string, opts ..
sub.Test.Environment.Accounts.Groups = append(sub.Test.Environment.Accounts.Groups, grp)
}

gid1000 := uint32(1000)
usr := apko_types.User{
UserName: "build",
UID: 1000,
GID: apko_types.GID(&gid1000),
}
cfg.Environment.Accounts.Users = append(cfg.Environment.Accounts.Users, usr)
if cfg.Test != nil {
cfg.Test.Environment.Accounts.Users = append(cfg.Test.Environment.Accounts.Users, usr)
Expand Down

0 comments on commit 94e2c14

Please sign in to comment.