Skip to content

Commit 996b340

Browse files
authored
Merge pull request #2673 from pvdvreede/cidata-validation-change
Make lima user fallback regex and cidata user validation consistent
2 parents 16fd466 + 2ce5707 commit 996b340

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

Diff for: pkg/cidata/template.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/lima-vm/lima/pkg/iso9660util"
1212

1313
"github.com/containerd/containerd/identifiers"
14+
"github.com/lima-vm/lima/pkg/osutil"
1415
"github.com/lima-vm/lima/pkg/textutil"
1516
)
1617

@@ -92,8 +93,8 @@ func ValidateTemplateArgs(args TemplateArgs) error {
9293
if err := identifiers.Validate(args.Name); err != nil {
9394
return err
9495
}
95-
if err := identifiers.Validate(args.User); err != nil {
96-
return err
96+
if !osutil.ValidateUsername(args.User) {
97+
return errors.New("field User must be valid linux username")
9798
}
9899
if args.User == "root" {
99100
return errors.New("field User must not be \"root\"")

Diff for: pkg/osutil/user.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ var regexUsername = regexp.MustCompile("^[a-z_][a-z0-9_-]*$")
4040
// regexPath detects valid Linux path.
4141
var regexPath = regexp.MustCompile("^[/a-zA-Z0-9_-]+$")
4242

43+
func ValidateUsername(name string) bool {
44+
return regexUsername.MatchString(name)
45+
}
46+
4347
func LookupUser(name string) (User, error) {
4448
if users == nil {
4549
users = make(map[string]User)
@@ -111,7 +115,7 @@ func LimaUser(warn bool) (*user.User, error) {
111115
cache.Do(func() {
112116
cache.u, cache.err = user.Current()
113117
if cache.err == nil {
114-
if !regexUsername.MatchString(cache.u.Username) {
118+
if !ValidateUsername(cache.u.Username) {
115119
warning := fmt.Sprintf("local user %q is not a valid Linux username (must match %q); using %q username instead",
116120
cache.u.Username, regexUsername.String(), fallbackUser)
117121
cache.warnings = append(cache.warnings, warning)

Diff for: pkg/osutil/user_test.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,11 @@ func TestLimaUserWarn(t *testing.T) {
1313
assert.NilError(t, err)
1414
}
1515

16-
func validUsername(username string) bool {
17-
return regexUsername.MatchString(username)
18-
}
19-
2016
func TestLimaUsername(t *testing.T) {
2117
user, err := LimaUser(false)
2218
assert.NilError(t, err)
2319
// check for reasonable unix user name
24-
assert.Assert(t, validUsername(user.Username), user.Username)
20+
assert.Assert(t, ValidateUsername(user.Username), user.Username)
2521
}
2622

2723
func TestLimaUserUid(t *testing.T) {

0 commit comments

Comments
 (0)