Skip to content

Commit 0e67f9e

Browse files
committed
Make sure lima user fallback uses same validation as template
The regex currently being used is different from the identifier's validation from containerd. The fallback test does allow an `_` but the validation for the identifier does not. This results in a bug where the a user that starts with an `_` will pass fallback validation (ie not be set to lima for the user), but will then fail the cidata validation here: https://github.com/lima-vm/lima/blob/master/pkg/cidata/template.go#L95. Error log shows as: ` ERRO[0000] [hostagent] identifier "_nixbld1" must match ^[A-Za-z0-9]+(?:[._-](?:[A-Za-z0-9]+))*$: invalid argument fields.level=fatal` This PR sets the same validation check in both spots to fix this and make sure they stay in sync in the future. Update warning message to use error msg fix bad err method call. Signed-off-by: pvdvreede <[email protected]>
1 parent 5b87d2d commit 0e67f9e

File tree

2 files changed

+5
-9
lines changed

2 files changed

+5
-9
lines changed

pkg/osutil/user.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"strings"
1212
"sync"
1313

14+
"github.com/containerd/containerd/identifiers"
1415
"github.com/sirupsen/logrus"
1516
)
1617

@@ -32,11 +33,6 @@ var (
3233
groups map[string]Group
3334
)
3435

35-
// regexUsername matches user and group names to be valid for `useradd`.
36-
// `useradd` allows names with a trailing '$', but it feels prudent to map those
37-
// names to the fallback user as well, so the regex does not allow them.
38-
var regexUsername = regexp.MustCompile("^[a-z_][a-z0-9_-]*$")
39-
4036
// regexPath detects valid Linux path.
4137
var regexPath = regexp.MustCompile("^[/a-zA-Z0-9_-]+$")
4238

@@ -111,9 +107,8 @@ func LimaUser(warn bool) (*user.User, error) {
111107
cache.Do(func() {
112108
cache.u, cache.err = user.Current()
113109
if cache.err == nil {
114-
if !regexUsername.MatchString(cache.u.Username) {
115-
warning := fmt.Sprintf("local user %q is not a valid Linux username (must match %q); using %q username instead",
116-
cache.u.Username, regexUsername.String(), fallbackUser)
110+
if err := identifiers.Validate(cache.u.Username); err != nil {
111+
warning := fmt.Sprintf("%s; using %q username instead", err.Error(), fallbackUser)
117112
cache.warnings = append(cache.warnings, warning)
118113
cache.u.Username = fallbackUser
119114
}

pkg/osutil/user_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"strconv"
66
"testing"
77

8+
"github.com/containerd/containerd/identifiers"
89
"gotest.tools/v3/assert"
910
)
1011

@@ -14,7 +15,7 @@ func TestLimaUserWarn(t *testing.T) {
1415
}
1516

1617
func validUsername(username string) bool {
17-
return regexUsername.MatchString(username)
18+
return identifiers.Validate(username) == nil
1819
}
1920

2021
func TestLimaUsername(t *testing.T) {

0 commit comments

Comments
 (0)