Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure lima user fallback uses same validation as template #2655

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions pkg/osutil/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"
"sync"

"github.com/containerd/containerd/identifiers"
"github.com/sirupsen/logrus"
)

Expand All @@ -32,11 +33,6 @@ var (
groups map[string]Group
)

// regexUsername matches user and group names to be valid for `useradd`.
// `useradd` allows names with a trailing '$', but it feels prudent to map those
// names to the fallback user as well, so the regex does not allow them.
var regexUsername = regexp.MustCompile("^[a-z_][a-z0-9_-]*$")

// regexPath detects valid Linux path.
var regexPath = regexp.MustCompile("^[/a-zA-Z0-9_-]+$")

Expand Down Expand Up @@ -111,9 +107,8 @@ 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) {
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)
if err := identifiers.Validate(cache.u.Username); err != nil {
warning := fmt.Sprintf("%s; using %q username instead", err.Error(), fallbackUser)
cache.warnings = append(cache.warnings, warning)
cache.u.Username = fallbackUser
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/osutil/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strconv"
"testing"

"github.com/containerd/containerd/identifiers"
"gotest.tools/v3/assert"
)

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

func validUsername(username string) bool {
return regexUsername.MatchString(username)
return identifiers.Validate(username) == nil
}

func TestLimaUsername(t *testing.T) {
Expand Down
Loading