Skip to content

Commit

Permalink
Merge pull request #1146 from joshrwolf/home-dir
Browse files Browse the repository at this point in the history
plumb through HomeDir as optional build configuration
  • Loading branch information
imjasonh authored May 31, 2024
2 parents 74186bc + 04b6f83 commit 3cdec39
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 5 deletions.
6 changes: 5 additions & 1 deletion pkg/build/types/image_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,18 @@ func (ic *ImageConfiguration) Validate() error {
}
}

for _, u := range ic.Accounts.Users {
for i, u := range ic.Accounts.Users {
if u.UserName == "" {
return fmt.Errorf("configured user %v has no configured user name", u)
}

if u.UID == 0 {
return fmt.Errorf("configured user %v has UID 0 (to run as root, use `run-as: 0`)", u)
}

if u.HomeDir == "" {
ic.Accounts.Users[i].HomeDir = "/home/" + u.UserName
}
}

for _, g := range ic.Accounts.Groups {
Expand Down
16 changes: 16 additions & 0 deletions pkg/build/types/image_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,19 @@ func TestOverlayWithAdditionalPackages(t *testing.T) {
require.ElementsMatch(t, ic.Contents.Keyring, []string{"key"})
require.ElementsMatch(t, ic.Contents.Packages, []string{"package", "other_package"})
}

func TestUserContents(t *testing.T) {
ctx := context.Background()

configPath := filepath.Join("testdata", "users.apko.yaml")
hasher := sha256.New()
ic := types.ImageConfiguration{}

require.NoError(t, ic.Load(ctx, configPath, hasher))
if err := ic.Validate(); err != nil {
t.Fatal(err)
}

require.Equal(t, "/not/home", ic.Accounts.Users[0].HomeDir)
require.Equal(t, "/home/user", ic.Accounts.Users[1].HomeDir)
}
6 changes: 5 additions & 1 deletion pkg/build/types/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,11 @@
},
"shell": {
"type": "string",
"description": "Required: The user's shell"
"description": "Optional: The user's shell"
},
"homedir": {
"type": "string",
"description": "Optional: The user's home directory"
}
},
"additionalProperties": false,
Expand Down
17 changes: 17 additions & 0 deletions pkg/build/types/testdata/users.apko.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
contents:
repositories:
- "repository"
keyring:
- "key"
packages:
- "package"

accounts:
users:
- gid: 1
uid: 2
homedir: "/not/home"
username: "something-else"
- gid: 2
uid: 3
username: "user"
8 changes: 5 additions & 3 deletions pkg/build/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ type User struct {
UID uint32 `json:"uid,omitempty"`
// Required: The user's group ID
GID uint32 `json:"gid,omitempty"`
// Required: The user's shell
// Optional: The user's shell
Shell string `json:"shell,omitempty"`
// Optional: The user's home directory
HomeDir string `json:"homedir,omitempty"`
}

type Group struct {
Expand Down Expand Up @@ -138,9 +140,9 @@ type ImageAccounts struct {
// Required: The user to run the container as. This can be a username or UID.
RunAs string `json:"run-as,omitempty" yaml:"run-as"`
// Required: List of users to populate the image with
Users []User `json:"users,omitempty"`
Users []User `json:"users,omitempty" yaml:"users"`
// Required: List of groups to populate the image with
Groups []Group `json:"groups,omitempty"`
Groups []Group `json:"groups,omitempty" yaml:"groups"`
}

type ImageConfiguration struct {
Expand Down

0 comments on commit 3cdec39

Please sign in to comment.