github.com/noxiouz/docker@v0.7.3-0.20160629055221-3d231c78e8c5/integration-cli/docker_cli_userns_test.go (about) 1 // +build !windows 2 3 package main 4 5 import ( 6 "fmt" 7 "io/ioutil" 8 "os" 9 "os/exec" 10 "path/filepath" 11 "strconv" 12 "strings" 13 14 "github.com/docker/docker/pkg/integration/checker" 15 "github.com/docker/docker/pkg/system" 16 "github.com/go-check/check" 17 ) 18 19 // user namespaces test: run daemon with remapped root setting 20 // 1. validate uid/gid maps are set properly 21 // 2. verify that files created are owned by remapped root 22 func (s *DockerDaemonSuite) TestDaemonUserNamespaceRootSetting(c *check.C) { 23 testRequires(c, DaemonIsLinux, SameHostDaemon, UserNamespaceInKernel) 24 25 c.Assert(s.d.StartWithBusybox("--userns-remap", "default"), checker.IsNil) 26 27 tmpDir, err := ioutil.TempDir("", "userns") 28 c.Assert(err, checker.IsNil) 29 30 defer os.RemoveAll(tmpDir) 31 32 // we need to find the uid and gid of the remapped root from the daemon's root dir info 33 uidgid := strings.Split(filepath.Base(s.d.root), ".") 34 c.Assert(uidgid, checker.HasLen, 2, check.Commentf("Should have gotten uid/gid strings from root dirname: %s", filepath.Base(s.d.root))) 35 uid, err := strconv.Atoi(uidgid[0]) 36 c.Assert(err, checker.IsNil, check.Commentf("Can't parse uid")) 37 gid, err := strconv.Atoi(uidgid[1]) 38 c.Assert(err, checker.IsNil, check.Commentf("Can't parse gid")) 39 40 // writable by the remapped root UID/GID pair 41 c.Assert(os.Chown(tmpDir, uid, gid), checker.IsNil) 42 43 out, err := s.d.Cmd("run", "-d", "--name", "userns", "-v", tmpDir+":/goofy", "busybox", "sh", "-c", "touch /goofy/testfile; top") 44 c.Assert(err, checker.IsNil, check.Commentf("Output: %s", out)) 45 user := s.findUser(c, "userns") 46 c.Assert(uidgid[0], checker.Equals, user) 47 48 pid, err := s.d.Cmd("inspect", "--format='{{.State.Pid}}'", "userns") 49 c.Assert(err, checker.IsNil, check.Commentf("Could not inspect running container: out: %q", pid)) 50 // check the uid and gid maps for the PID to ensure root is remapped 51 // (cmd = cat /proc/<pid>/uid_map | grep -E '0\s+9999\s+1') 52 out, rc1, err := runCommandPipelineWithOutput( 53 exec.Command("cat", "/proc/"+strings.TrimSpace(pid)+"/uid_map"), 54 exec.Command("grep", "-E", fmt.Sprintf("0[[:space:]]+%d[[:space:]]+", uid))) 55 c.Assert(rc1, checker.Equals, 0, check.Commentf("Didn't match uid_map: output: %s", out)) 56 57 out, rc2, err := runCommandPipelineWithOutput( 58 exec.Command("cat", "/proc/"+strings.TrimSpace(pid)+"/gid_map"), 59 exec.Command("grep", "-E", fmt.Sprintf("0[[:space:]]+%d[[:space:]]+", gid))) 60 c.Assert(rc2, checker.Equals, 0, check.Commentf("Didn't match gid_map: output: %s", out)) 61 62 // check that the touched file is owned by remapped uid:gid 63 stat, err := system.Stat(filepath.Join(tmpDir, "testfile")) 64 c.Assert(err, checker.IsNil) 65 c.Assert(stat.UID(), checker.Equals, uint32(uid), check.Commentf("Touched file not owned by remapped root UID")) 66 c.Assert(stat.GID(), checker.Equals, uint32(gid), check.Commentf("Touched file not owned by remapped root GID")) 67 68 // use host usernamespace 69 out, err = s.d.Cmd("run", "-d", "--name", "userns_skip", "--userns", "host", "busybox", "sh", "-c", "touch /goofy/testfile; top") 70 c.Assert(err, checker.IsNil, check.Commentf("Output: %s", out)) 71 user = s.findUser(c, "userns_skip") 72 // userns are skipped, user is root 73 c.Assert(user, checker.Equals, "root") 74 } 75 76 // findUser finds the uid or name of the user of the first process that runs in a container 77 func (s *DockerDaemonSuite) findUser(c *check.C, container string) string { 78 out, err := s.d.Cmd("top", container) 79 c.Assert(err, checker.IsNil, check.Commentf("Output: %s", out)) 80 rows := strings.Split(out, "\n") 81 if len(rows) < 2 { 82 // No process rows founds 83 c.FailNow() 84 } 85 return strings.Fields(rows[1])[0] 86 }