pkg/user: Bring user package from server and use it here in place of userCurrent.

This commit is contained in:
Harshavardhana
2015-12-08 20:35:01 -08:00
parent dfc0dc3042
commit 727538fa8c
5 changed files with 117 additions and 40 deletions
+6 -5
View File
@@ -22,6 +22,7 @@ import (
"runtime"
"github.com/minio/minio-xl/pkg/probe"
"github.com/minio/minio/pkg/user"
)
// mcCustomConfigDir contains the whole path to config dir. Only access via get/set functions.
@@ -40,16 +41,16 @@ func getMcConfigDir() (string, *probe.Error) {
if mcCustomConfigDir != "" {
return mcCustomConfigDir, nil
}
u, err := userCurrent()
if err != nil {
return "", err.Trace()
homeDir, e := user.HomeDir()
if e != nil {
return "", probe.NewError(e)
}
// For windows the path is slightly different
switch runtime.GOOS {
case "windows":
return filepath.Join(u.HomeDir, globalMCConfigWindowsDir), nil
return filepath.Join(homeDir, globalMCConfigWindowsDir), nil
default:
return filepath.Join(u.HomeDir, globalMCConfigDir), nil
return filepath.Join(homeDir, globalMCConfigDir), nil
}
}
-35
View File
@@ -18,14 +18,8 @@ package main
import (
"math/rand"
"os"
"os/user"
"path/filepath"
"runtime"
"strconv"
"time"
"github.com/minio/minio-xl/pkg/probe"
)
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
@@ -46,32 +40,3 @@ func isBucketVirtualStyle(host string) bool {
googleVirtual, _ := filepath.Match("*.storage.googleapis.com", host)
return s3Virtual || googleVirtual
}
// Workaround for docker images with fully static binary and 32bit linux operating systems.
// For static binaries NSS library will not be a part of the static binary hence user.Current() fails.
// For 32bit linux CGO is not enabled so it will not provide linux specific codebase.
// This code is taken from the minio project:
// (https://github.com/minio/minio/blob/8046b24b975a26d659bda12797a30631bb427e8f/utils.go#L60-L85)
func userCurrent() (*user.User, *probe.Error) {
if os.Getenv("DOCKERIMAGE") == "1" {
wd, err := os.Getwd()
if err != nil {
return nil, probe.NewError(err)
}
return &user.User{Uid: "0", Gid: "0", Username: "root", Name: "root", HomeDir: wd}, nil
}
if runtime.GOARCH == "386" && runtime.GOOS == "linux" {
return &user.User{
Uid: strconv.Itoa(os.Getuid()),
Gid: strconv.Itoa(os.Getgid()),
Username: os.Getenv("USER"),
Name: os.Getenv("USER"),
HomeDir: os.Getenv("HOME"),
}, nil
}
user, err := user.Current()
if err != nil {
return nil, probe.NewError(err)
}
return user, nil
}
+65
View File
@@ -0,0 +1,65 @@
/*
* Minio Cloud Storage, (C) 2015 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package user
import (
"os"
"os/user"
"runtime"
"strconv"
)
// Current is a portable implementation to determine the current user.
// Golang's user.Current does not work reliably under docker or 32bit linux
//
// Two issues this code handles :-
//
// Docker Container - For static binaries NSS library will not be a part of the static binary hence user.Current() fails.
// Linux Intel 32 bit - CGO is not enabled so it will not link with NSS library.
//
func Current() (*user.User, error) {
if os.Getenv("DOCKERIMAGE") == "1" {
wd, err := os.Getwd()
if err != nil {
return nil, err
}
return &user.User{Uid: "0", Gid: "0", Username: "root", Name: "root", HomeDir: wd}, nil
}
if runtime.GOARCH == "386" && runtime.GOOS == "linux" {
return &user.User{
Uid: strconv.Itoa(os.Getuid()),
Gid: strconv.Itoa(os.Getgid()),
Username: os.Getenv("USER"),
Name: os.Getenv("USER"),
HomeDir: os.Getenv("HOME"),
}, nil
}
user, e := user.Current()
if e != nil {
return nil, e
}
return user, nil
}
// HomeDir - return current home directory.
func HomeDir() (string, error) {
user, err := Current()
if err != nil {
return "", err
}
return user.HomeDir, nil
}
+41
View File
@@ -0,0 +1,41 @@
/*
* Minio Cloud Storage, (C) 2015 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package user_test
import (
"testing"
"github.com/minio/minio/pkg/user"
. "gopkg.in/check.v1"
)
func Test(t *testing.T) { TestingT(t) }
type MySuite struct{}
var _ = Suite(&MySuite{})
func (s *MySuite) TestUser(c *C) {
_, err := user.Current()
c.Assert(err, IsNil)
}
func (s *MySuite) TestHomeDir(c *C) {
_, err := user.HomeDir()
c.Assert(err, IsNil)
}
+5
View File
@@ -52,6 +52,11 @@
"revision": "bed528d5696872b03dd32d63599f5b9ed82cb361",
"revisionTime": "2015-12-03T01:11:14-08:00"
},
{
"path": "github.com/minio/minio/pkg/user",
"revision": "71876688282283e8af374bd8592c7d33c547df4d",
"revisionTime": "2015-12-07T14:59:57-08:00"
},
{
"path": "github.com/minio/pb",
"revision": "6a97c3b0da073f974e0b8d1ac1bc42c0a7adca46",