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

fix: work around issue with user.Lookup not returning UnknownUserError #427

Merged
merged 2 commits into from
Jun 11, 2024
Merged
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
18 changes: 18 additions & 0 deletions internals/osutil/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"os"
"os/user"
"strconv"
"strings"
"syscall"

"github.com/canonical/pebble/internals/osutil/sys"
)
Expand All @@ -27,6 +29,8 @@ var (
userCurrent = user.Current
userLookup = user.Lookup
userLookupGroup = user.LookupGroup

enoentMessage = syscall.ENOENT.Error()
)

// RealUser finds the user behind a sudo invocation when root, if applicable
Expand Down Expand Up @@ -56,6 +60,12 @@ func RealUser() (*user.User, error) {
if _, ok := err.(user.UnknownUserError); ok {
return cur, nil
}
// Workaround for https://github.com/golang/go/issues/67912, until our
// minimum Go version has a fix for that. In short, user.Lookup sometimes
// doesn't return UnknownUserError when it should.
if err != nil && strings.Contains(err.Error(), enoentMessage) {
return cur, nil
}
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -90,6 +100,10 @@ func NormalizeUidGid(uid, gid *int, username, group string) (*int, *int, error)
if username != "" {
u, err := userLookup(username)
if err != nil {
if strings.Contains(err.Error(), enoentMessage) {
// Better error message to work around https://github.com/golang/go/issues/67912
return nil, nil, user.UnknownUserError(username)
}
return nil, nil, err
}
n, _ := strconv.Atoi(u.Uid)
Expand All @@ -107,6 +121,10 @@ func NormalizeUidGid(uid, gid *int, username, group string) (*int, *int, error)
if group != "" {
g, err := userLookupGroup(group)
if err != nil {
if strings.Contains(err.Error(), enoentMessage) {
// Better error message to work around https://github.com/golang/go/issues/67912
return nil, nil, user.UnknownGroupError(group)
}
return nil, nil, err
}
n, _ := strconv.Atoi(g.Gid)
Expand Down
Loading