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

os/user: Current fails to return user #31949

Closed
sflxn opened this issue May 9, 2019 · 6 comments
Closed

os/user: Current fails to return user #31949

sflxn opened this issue May 9, 2019 · 6 comments

Comments

@sflxn
Copy link

sflxn commented May 9, 2019

What version of Go are you using (go version)?

1.12.5

Does this issue reproduce with the latest release?

YES

What operating system and processor architecture are you using (go env)?

GOARCH="amd64" GOBIN="" GOCACHE="/Users/loc/Library/Caches/go-build" GOEXE="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="darwin" GOOS="darwin" GOPATH="/Users/loc/go" GOPROXY="" GORACE="" GOROOT="/usr/local/Cellar/go/1.12.1/libexec" GOTMPDIR="" GOTOOLDIR="/usr/local/Cellar/go/1.12.1/libexec/pkg/tool/darwin_amd64" GCCGO="gccgo" CC="clang" CXX="clang++" CGO_ENABLED="1" GOMOD="" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/w_/ws12k2vx2xl164l7qfz30f2w0000gn/T/go-build455020799=/tmp/go-build -gno-record-gcc-switches -fno-common" Output
$ go env

What did you do?

We're running user.Current() in a k8s controller on k8s cluster on Kind. The k8s controller is implemented in a linux amd64 container.

What did you expect to see?

The actual user name -- root

What did you see instead?

failed to get current user: user: Current not implemented on linux/amd64

@sflxn
Copy link
Author

sflxn commented May 9, 2019

I know exactly what the problem is, but I'm not sure what is the best solution. The problem is user.Current() is caching old data. It's not the root cause, but it is why the function is returning incorrect user.

We're running inside an amd64 linux container. The user should be root. But Current() returns a cached error instead. When I duplicate the code for the internal function current() (lower case) in our code, it returns the correct user.

@bradfitz
Copy link
Contributor

bradfitz commented May 9, 2019

If you built with CGO_ENABLED=0 (as is default if you're cross-compiling), then it falls back to using the USER and HOME env variables.

The code says:

        homeDir, _ := os.UserHomeDir()  // (which falls back to $HOME)
        u = &User{
                Uid:      uid,
                Gid:      currentGID(),
                Username: os.Getenv("USER"),
                Name:     "", // ignored                                                                                                                                              
                HomeDir:  homeDir,
        }
....
        // cgo isn't available, but if we found the minimum information
        // without it, use it:
        if u.Uid != "" && u.Username != "" && u.HomeDir != "" {
                return u, nil
        }
        return u, fmt.Errorf("user: Current not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
}

So, set those in your environment.

As this comes up often enough, I'll make this bug be about changing the error message.

@gopherbot
Copy link

Change https://golang.org/cl/176337 mentions this issue: os/user: make Current return better error w/o cgo & complete environment

@bradfitz
Copy link
Contributor

bradfitz commented May 9, 2019

@sflxn, what is the caching problem? Are you changing your uid at runtime?

@bradfitz
Copy link
Contributor

bradfitz commented May 9, 2019

Are you sure you're using Go 1.12? The kubernetes-sigs/cluster-api-provider-vsphere#260 bug says you're using a Go 1.11 container.

@sflxn
Copy link
Author

sflxn commented May 9, 2019

@bradfitz Sorry, I showed the output of golang on my Mac but we actually build using the golang 1.12 container. Your response made me went back and check. Ben's PR uses that version of the golang container and our CI was reporting that message.

In my own test, I hadn't updated my Dockerfile to use golang 1.12 so I was getting the old behavior. Once I updated it to the golang 1.12 container, it worked for me. Therefore, the fact that it worked locally for me but not in our CI says there's something in our CI environment and not golang. We will investigate there.

Thanks for the quick response. Feel free to close this issue.

@golang golang locked and limited conversation to collaborators May 8, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants