Skip to content

Commit

Permalink
Use "lima" username if the local user is not a valid Linux name
Browse files Browse the repository at this point in the history
At least some companies set local users via directory services and
choose the email address as the user name. The '@' character is not
valid in Linux usernames.

Signed-off-by: Jan Dubois <jan.dubois@suse.com>
  • Loading branch information
jandubois committed Sep 6, 2021
1 parent be68278 commit 2c41398
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 8 deletions.
4 changes: 2 additions & 2 deletions cmd/limactl/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"fmt"
"os"
"os/exec"
"os/user"
"strings"

"github.com/lima-vm/lima/pkg/osutil"
"github.com/lima-vm/lima/pkg/sshutil"
"github.com/lima-vm/lima/pkg/store"
"github.com/sirupsen/logrus"
Expand All @@ -31,7 +31,7 @@ func copyAction(clicontext *cli.Context) error {
if err != nil {
return err
}
u, err := user.Current()
u, err := osutil.LimaUser(false)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/cidata/cidata.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io/fs"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"strconv"
"strings"
Expand Down Expand Up @@ -39,7 +38,7 @@ func GenerateISO9660(instDir, name string, y *limayaml.LimaYAML) error {
if err := limayaml.Validate(*y); err != nil {
return err
}
u, err := user.Current()
u, err := osutil.LimaUser(true)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/limayaml/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"net"
"os"
"os/user"
"path/filepath"
"runtime"
"strings"
Expand All @@ -13,6 +12,7 @@ import (

"github.com/docker/go-units"
"github.com/lima-vm/lima/pkg/localpathutil"
"github.com/lima-vm/lima/pkg/osutil"
"github.com/lima-vm/lima/pkg/qemu/qemuconst"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -61,7 +61,7 @@ func Validate(y LimaYAML) error {
return fmt.Errorf("field `memory` has an invalid value: %w", err)
}

u, err := user.Current()
u, err := osutil.LimaUser(false)
if err != nil {
return fmt.Errorf("internal error (not an error of YAML): %w", err)
}
Expand Down
38 changes: 38 additions & 0 deletions pkg/osutil/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package osutil

import (
"fmt"
"github.com/sirupsen/logrus"
"os/user"
"sync"

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

const (
fallbackUser = "lima"
)

var cache struct {
sync.Once
u *user.User
err error
warning string
}

func LimaUser(warn bool) (*user.User, error) {
cache.Do(func() {
cache.u, cache.err = user.Current()
if cache.err == nil {
if err := identifiers.Validate(cache.u.Username); err != nil {
cache.warning = fmt.Sprintf("local user %q is not a valid Linux username: %v; using %q username instead",
cache.u.Username, err, fallbackUser)
}
cache.u.Username = fallbackUser
}
})
if warn && cache.warning != "" {
logrus.Warn(cache.warning)
}
return cache.u, cache.err
}
3 changes: 1 addition & 2 deletions pkg/sshutil/sshutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io/fs"
"os"
"os/exec"
"os/user"
"path/filepath"
"strings"

Expand Down Expand Up @@ -163,7 +162,7 @@ func SSHArgs(instDir string, useDotSSH bool) ([]string, error) {
if len(controlSock) >= osutil.UnixPathMax {
return nil, fmt.Errorf("socket path %q is too long: >= UNIX_PATH_MAX=%d", controlSock, osutil.UnixPathMax)
}
u, err := user.Current()
u, err := osutil.LimaUser(false)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 2c41398

Please sign in to comment.