Skip to content

Commit

Permalink
Merge pull request #2673 from pvdvreede/cidata-validation-change
Browse files Browse the repository at this point in the history
Make lima user fallback regex and cidata user validation consistent
  • Loading branch information
jandubois authored Oct 3, 2024
2 parents 16fd466 + 2ce5707 commit 996b340
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
5 changes: 3 additions & 2 deletions pkg/cidata/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/lima-vm/lima/pkg/iso9660util"

"github.com/containerd/containerd/identifiers"
"github.com/lima-vm/lima/pkg/osutil"
"github.com/lima-vm/lima/pkg/textutil"
)

Expand Down Expand Up @@ -92,8 +93,8 @@ func ValidateTemplateArgs(args TemplateArgs) error {
if err := identifiers.Validate(args.Name); err != nil {
return err
}
if err := identifiers.Validate(args.User); err != nil {
return err
if !osutil.ValidateUsername(args.User) {
return errors.New("field User must be valid linux username")
}
if args.User == "root" {
return errors.New("field User must not be \"root\"")
Expand Down
6 changes: 5 additions & 1 deletion pkg/osutil/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ var regexUsername = regexp.MustCompile("^[a-z_][a-z0-9_-]*$")
// regexPath detects valid Linux path.
var regexPath = regexp.MustCompile("^[/a-zA-Z0-9_-]+$")

func ValidateUsername(name string) bool {
return regexUsername.MatchString(name)
}

func LookupUser(name string) (User, error) {
if users == nil {
users = make(map[string]User)
Expand Down Expand Up @@ -111,7 +115,7 @@ func LimaUser(warn bool) (*user.User, error) {
cache.Do(func() {
cache.u, cache.err = user.Current()
if cache.err == nil {
if !regexUsername.MatchString(cache.u.Username) {
if !ValidateUsername(cache.u.Username) {
warning := fmt.Sprintf("local user %q is not a valid Linux username (must match %q); using %q username instead",
cache.u.Username, regexUsername.String(), fallbackUser)
cache.warnings = append(cache.warnings, warning)
Expand Down
6 changes: 1 addition & 5 deletions pkg/osutil/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,11 @@ func TestLimaUserWarn(t *testing.T) {
assert.NilError(t, err)
}

func validUsername(username string) bool {
return regexUsername.MatchString(username)
}

func TestLimaUsername(t *testing.T) {
user, err := LimaUser(false)
assert.NilError(t, err)
// check for reasonable unix user name
assert.Assert(t, validUsername(user.Username), user.Username)
assert.Assert(t, ValidateUsername(user.Username), user.Username)
}

func TestLimaUserUid(t *testing.T) {
Expand Down

0 comments on commit 996b340

Please sign in to comment.