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

Use "lima" username if the local user is not a valid Linux name #214

Merged
merged 1 commit into from
Sep 6, 2021
Merged
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
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