users_stage: Don't allow empty passwords

Make sure empty passwords are set to nil so they result in a locked
account.

Also add a test for the password hashing in NewUserStageOptions()
This commit is contained in:
Brian C. Lane 2022-07-15 10:59:07 -07:00 committed by Tom Gundersen
parent 6adf3f5b7b
commit d3af314e58
2 changed files with 50 additions and 0 deletions

View file

@ -36,6 +36,12 @@ func NewUsersStageOptions(userCustomizations []blueprint.UserCustomization, omit
users := make(map[string]UsersStageOptionsUser, len(userCustomizations))
for _, uc := range userCustomizations {
// Don't hash empty passwords, set to nil to lock account
if uc.Password != nil && len(*uc.Password) == 0 {
uc.Password = nil
}
// Hash non-empty un-hashed passwords
if uc.Password != nil && !crypt.PasswordIsCrypted(*uc.Password) {
cryptedPassword, err := crypt.CryptSHA512(*uc.Password)
if err != nil {

View file

@ -1,9 +1,13 @@
package osbuild
import (
"strings"
"testing"
"github.com/osbuild/osbuild-composer/internal/blueprint"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewUsersStage(t *testing.T) {
@ -14,3 +18,43 @@ func TestNewUsersStage(t *testing.T) {
actualStage := NewUsersStage(&UsersStageOptions{})
assert.Equal(t, expectedStage, actualStage)
}
func TestNewUsersStageOptionsPassword(t *testing.T) {
Pass := "testpass"
EmptyPass := ""
CryptPass := "$6$RWdHzrPfoM6BMuIP$gKYlBXQuJgP.G2j2twbOyxYjFDPUQw8Jp.gWe1WD/obX0RMyfgw5vt.Mn/tLLX4mQjaklSiIzoAW3HrVQRg4Q." // #nosec G101
users := []blueprint.UserCustomization{
blueprint.UserCustomization{
Name: "bart",
Password: &Pass,
},
blueprint.UserCustomization{
Name: "lisa",
Password: &CryptPass,
},
blueprint.UserCustomization{
Name: "maggie",
Password: &EmptyPass,
},
blueprint.UserCustomization{
Name: "homer",
},
}
options, err := NewUsersStageOptions(users, false)
require.Nil(t, err)
require.NotNil(t, options)
// bart's password should now be a hash
assert.True(t, strings.HasPrefix(*options.Users["bart"].Password, "$6$"))
// lisa's password should be left alone (already hashed)
assert.Equal(t, CryptPass, *options.Users["lisa"].Password)
// maggie's password should now be nil (locked account)
assert.Nil(t, options.Users["maggie"].Password)
// homer's password should still be nil (locked account)
assert.Nil(t, options.Users["homer"].Password)
}