Skip to content

Commit 46525c4

Browse files
committed
pkg/osutil: refactor regexps for LimaUser
Signed-off-by: Oleksandr Redko <[email protected]>
1 parent a106845 commit 46525c4

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

pkg/osutil/user.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ type Group struct {
3030
var users map[string]User
3131
var groups map[string]Group
3232

33+
// regexUidGid detects valid Linux uid or gid.
34+
var regexUidGid = regexp.MustCompile("^[0-9]+$") //nolint:revive
35+
36+
// regexUsername matches user and group names to be valid for `useradd`.
37+
// It allows a trailing '$', but it feels prudent to map those to the fallback user as well.
38+
var regexUsername = regexp.MustCompile("^[a-z_][a-z0-9_-]*$")
39+
40+
// regexPath detects valid Linux path.
41+
var regexPath = regexp.MustCompile("^[/a-zA-Z0-9_-]+$")
42+
3343
func LookupUser(name string) (User, error) {
3444
if users == nil {
3545
users = make(map[string]User)
@@ -101,12 +111,9 @@ func LimaUser(warn bool) (*user.User, error) {
101111
cache.Do(func() {
102112
cache.u, cache.err = user.Current()
103113
if cache.err == nil {
104-
// `useradd` only allows user and group names matching the following pattern:
105-
// (it allows a trailing '$', but it feels prudent to map those to the fallback user as well)
106-
validName := "^[a-z_][a-z0-9_-]*$"
107-
if !regexp.MustCompile(validName).Match([]byte(cache.u.Username)) {
114+
if !regexUsername.MatchString(cache.u.Username) {
108115
warning := fmt.Sprintf("local user %q is not a valid Linux username (must match %q); using %q username instead",
109-
cache.u.Username, validName, fallbackUser)
116+
cache.u.Username, regexUsername.String(), fallbackUser)
110117
cache.warnings = append(cache.warnings, warning)
111118
cache.u.Username = fallbackUser
112119
}
@@ -119,7 +126,7 @@ func LimaUser(warn bool) (*user.User, error) {
119126
if err != nil {
120127
uid = fallbackUid
121128
}
122-
if !regexp.MustCompile("^[0-9]+$").Match([]byte(cache.u.Uid)) {
129+
if !regexUidGid.MatchString(cache.u.Uid) {
123130
warning := fmt.Sprintf("local uid %q is not a valid Linux uid (must be integer); using %d uid instead",
124131
cache.u.Uid, uid)
125132
cache.warnings = append(cache.warnings, warning)
@@ -133,7 +140,7 @@ func LimaUser(warn bool) (*user.User, error) {
133140
if err != nil {
134141
gid = fallbackGid
135142
}
136-
if !regexp.MustCompile("^[0-9]+$").Match([]byte(cache.u.Gid)) {
143+
if !regexUidGid.MatchString(cache.u.Gid) {
137144
warning := fmt.Sprintf("local gid %q is not a valid Linux gid (must be integer); using %d gid instead",
138145
cache.u.Gid, gid)
139146
cache.warnings = append(cache.warnings, warning)
@@ -150,10 +157,9 @@ func LimaUser(warn bool) (*user.User, error) {
150157
prefix := strings.ToLower(fmt.Sprintf("/%c", drive[0]))
151158
home = strings.Replace(home, drive, prefix, 1)
152159
}
153-
validPath := "^[/a-zA-Z0-9_-]+$"
154-
if !regexp.MustCompile(validPath).Match([]byte(cache.u.HomeDir)) {
160+
if !regexPath.MatchString(cache.u.HomeDir) {
155161
warning := fmt.Sprintf("local home %q is not a valid Linux path (must match %q); using %q home instead",
156-
cache.u.HomeDir, validPath, home)
162+
cache.u.HomeDir, regexPath.String(), home)
157163
cache.warnings = append(cache.warnings, warning)
158164
cache.u.HomeDir = home
159165
}

pkg/osutil/user_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package osutil
22

33
import (
44
"path"
5-
"regexp"
65
"strconv"
76
"testing"
87

@@ -15,8 +14,7 @@ func TestLimaUserWarn(t *testing.T) {
1514
}
1615

1716
func validUsername(username string) bool {
18-
validName := "^[a-z_][a-z0-9_-]*$"
19-
return regexp.MustCompile(validName).Match([]byte(username))
17+
return regexUsername.MatchString(username)
2018
}
2119

2220
func TestLimaUsername(t *testing.T) {

0 commit comments

Comments
 (0)