Jelajahi Sumber

Merge pull request #1096 from dotcloud/remove_os_user

* Runtime: Remove the os.user dependency and manually lookup /etc/passwd instead
Guillaume J. Charmes 12 tahun lalu
induk
melakukan
454cd147fb
3 mengubah file dengan 43 tambahan dan 5 penghapusan
  1. 17 0
      container_test.go
  2. 2 5
      sysinit.go
  3. 24 0
      utils/utils.go

+ 17 - 0
container_test.go

@@ -849,6 +849,23 @@ func TestUser(t *testing.T) {
 	if !strings.Contains(string(output), "uid=1(daemon) gid=1(daemon)") {
 	if !strings.Contains(string(output), "uid=1(daemon) gid=1(daemon)") {
 		t.Error(string(output))
 		t.Error(string(output))
 	}
 	}
+
+	// Test an wrong username
+	container, err = builder.Create(&Config{
+		Image: GetTestImage(runtime).ID,
+		Cmd:   []string{"id"},
+
+		User: "unkownuser",
+	},
+	)
+	if err != nil {
+		t.Fatal(err)
+	}
+	defer runtime.Destroy(container)
+	output, err = container.Output()
+	if container.State.ExitCode == 0 {
+		t.Fatal("Starting container with wrong uid should fail but it passed.")
+	}
 }
 }
 
 
 func TestMultipleContainers(t *testing.T) {
 func TestMultipleContainers(t *testing.T) {

+ 2 - 5
sysinit.go

@@ -3,10 +3,10 @@ package docker
 import (
 import (
 	"flag"
 	"flag"
 	"fmt"
 	"fmt"
+	"github.com/dotcloud/docker/utils"
 	"log"
 	"log"
 	"os"
 	"os"
 	"os/exec"
 	"os/exec"
-	"os/user"
 	"strconv"
 	"strconv"
 	"strings"
 	"strings"
 	"syscall"
 	"syscall"
@@ -27,10 +27,7 @@ func changeUser(u string) {
 	if u == "" {
 	if u == "" {
 		return
 		return
 	}
 	}
-	userent, err := user.LookupId(u)
-	if err != nil {
-		userent, err = user.Lookup(u)
-	}
+	userent, err := utils.UserLookup(u)
 	if err != nil {
 	if err != nil {
 		log.Fatalf("Unable to find user %v: %v", u, err)
 		log.Fatalf("Unable to find user %v: %v", u, err)
 	}
 	}

+ 24 - 0
utils/utils.go

@@ -14,6 +14,7 @@ import (
 	"net/http"
 	"net/http"
 	"os"
 	"os"
 	"os/exec"
 	"os/exec"
+	"os/user"
 	"path/filepath"
 	"path/filepath"
 	"runtime"
 	"runtime"
 	"strconv"
 	"strconv"
@@ -700,3 +701,26 @@ func ParseRepositoryTag(repos string) (string, string) {
 	}
 	}
 	return repos, ""
 	return repos, ""
 }
 }
+
+// UserLookup check if the given username or uid is present in /etc/passwd
+// and returns the user struct.
+// If the username is not found, an error is returned.
+func UserLookup(uid string) (*user.User, error) {
+	file, err := ioutil.ReadFile("/etc/passwd")
+	if err != nil {
+		return nil, err
+	}
+	for _, line := range strings.Split(string(file), "\n") {
+		data := strings.Split(line, ":")
+		if len(data) > 5 && (data[0] == uid || data[2] == uid) {
+			return &user.User{
+				Uid:      data[2],
+				Gid:      data[3],
+				Username: data[0],
+				Name:     data[4],
+				HomeDir:  data[5],
+			}, nil
+		}
+	}
+	return nil, fmt.Errorf("User not found in /etc/passwd")
+}