Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

osbuild: use New{Group,Users}StageOptions() in Gen{Group,Users}Stage() #429

Merged
merged 2 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions pkg/osbuild/groups_stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,5 @@ func NewGroupsStageOptions(groups []users.Group) *GroupsStageOptions {
}

func GenGroupsStage(groups []users.Group) *Stage {
options := &GroupsStageOptions{
Groups: make(map[string]GroupsStageOptionsGroup, len(groups)),
}
for _, group := range groups {
options.Groups[group.Name] = GroupsStageOptionsGroup{
GID: group.GID,
}
}
return NewGroupsStage(options)
return NewGroupsStage(NewGroupsStageOptions(groups))
}
38 changes: 3 additions & 35 deletions pkg/osbuild/users_stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,41 +71,9 @@ func NewUsersStageOptions(userCustomizations []users.User, omitKey bool) (*Users
}

func GenUsersStage(users []users.User, omitKey bool) (*Stage, error) {
options := &UsersStageOptions{
Users: make(map[string]UsersStageOptionsUser, len(users)),
options, err := NewUsersStageOptions(users, omitKey)
if err != nil {
return nil, err
}

for _, user := range users {
// Don't hash empty passwords, set to nil to lock account
if user.Password != nil && len(*user.Password) == 0 {
user.Password = nil
}

// Hash non-empty un-hashed passwords
if user.Password != nil && !crypt.PasswordIsCrypted(*user.Password) {
cryptedPassword, err := crypt.CryptSHA512(*user.Password)
if err != nil {
return nil, err
}

user.Password = &cryptedPassword
}

userOptions := UsersStageOptionsUser{
UID: user.UID,
GID: user.GID,
Groups: user.Groups,
Description: user.Description,
Home: user.Home,
Shell: user.Shell,
Password: user.Password,
Key: nil,
}
if !omitKey {
userOptions.Key = user.Key
}
options.Users[user.Name] = userOptions
}

return NewUsersStage(options), nil
}
44 changes: 43 additions & 1 deletion pkg/osbuild/users_stage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"strings"
"testing"

"github.com/osbuild/images/pkg/customizations/users"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/osbuild/images/internal/common"
"github.com/osbuild/images/pkg/customizations/users"
)

func TestNewUsersStage(t *testing.T) {
Expand Down Expand Up @@ -57,3 +59,43 @@ func TestNewUsersStageOptionsPassword(t *testing.T) {
// homer's password should still be nil (locked account)
assert.Nil(t, options.Users["homer"].Password)
}

func TestGenUsersStageSameAsNewUsersStageOptions(t *testing.T) {
users := []users.User{
{
Name: "user1", UID: common.ToPtr(1000), GID: common.ToPtr(1000),
Groups: []string{"grp1"},
Description: common.ToPtr("some-descr"),
Home: common.ToPtr("/home/user1"),
Shell: common.ToPtr("/bin/zsh"),
Key: common.ToPtr("some-key"),
},
}
expected := &UsersStageOptions{
Users: map[string]UsersStageOptionsUser{
"user1": {
UID: common.ToPtr(1000),
GID: common.ToPtr(1000),
Groups: []string{"grp1"},
Description: common.ToPtr("some-descr"),
Home: common.ToPtr("/home/user1"),
Shell: common.ToPtr("/bin/zsh"),
Key: common.ToPtr("some-key")},
},
}

// check that NewUsersStageOptions creates the expected user options
opts, err := NewUsersStageOptions(users, false)
require.Nil(t, err)
assert.Equal(t, opts, expected)

// check that GenUsersStage creates the expected user options
st, err := GenUsersStage(users, false)
require.Nil(t, err)
usrStageOptions := st.Options.(*UsersStageOptions)
assert.Equal(t, usrStageOptions, expected)

// and (for good measure, not really needed) check that both gen
// the same
assert.Equal(t, usrStageOptions, opts)
}
Loading