Skip to content

Commit d405272

Browse files
authored
Merge pull request #243 from rancher-sandbox/valid-user
Map usernames not allowed by useradd to the fallbackUser "lima"
2 parents 97ca3ae + acc453c commit d405272

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

pkg/osutil/user.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ import (
44
"fmt"
55
"github.com/sirupsen/logrus"
66
"os/user"
7+
"regexp"
78
"sync"
8-
9-
"github.com/containerd/containerd/identifiers"
109
)
1110

1211
const (
@@ -24,9 +23,12 @@ func LimaUser(warn bool) (*user.User, error) {
2423
cache.Do(func() {
2524
cache.u, cache.err = user.Current()
2625
if cache.err == nil {
27-
if err := identifiers.Validate(cache.u.Username); err != nil {
28-
cache.warning = fmt.Sprintf("local user %q is not a valid Linux username: %v; using %q username instead",
29-
cache.u.Username, err, fallbackUser)
26+
// `useradd` only allows user and group names matching the following pattern:
27+
// (it allows a trailing '$', but it feels prudent to map those to the fallback user as well)
28+
validName := "^[a-z_][a-z0-9_-]*$"
29+
if !regexp.MustCompile(validName).Match([]byte(cache.u.Username)) {
30+
cache.warning = fmt.Sprintf("local user %q is not a valid Linux username (must match %q); using %q username instead",
31+
cache.u.Username, validName, fallbackUser)
3032
cache.u.Username = fallbackUser
3133
}
3234
}

0 commit comments

Comments
 (0)