docker_cli_cp_to_container_unix_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // +build !windows
  2. package main
  3. import (
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "path/filepath"
  8. "strconv"
  9. "strings"
  10. "testing"
  11. "github.com/docker/docker/pkg/system"
  12. "gotest.tools/assert"
  13. )
  14. func (s *DockerSuite) TestCpToContainerWithPermissions(c *testing.T) {
  15. testRequires(c, testEnv.IsLocalDaemon, DaemonIsLinux)
  16. tmpDir := getTestDir(c, "test-cp-to-host-with-permissions")
  17. defer os.RemoveAll(tmpDir)
  18. makeTestContentInDir(c, tmpDir)
  19. containerName := "permtest"
  20. _, exc := dockerCmd(c, "create", "--name", containerName, "debian:jessie", "/bin/bash", "-c", "stat -c '%u %g %a' /permdirtest /permdirtest/permtest")
  21. assert.Equal(c, exc, 0)
  22. defer dockerCmd(c, "rm", "-f", containerName)
  23. srcPath := cpPath(tmpDir, "permdirtest")
  24. dstPath := containerCpPath(containerName, "/")
  25. args := []string{"cp", "-a", srcPath, dstPath}
  26. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, args...))
  27. assert.NilError(c, err, "output: %v", out)
  28. out, err = startContainerGetOutput(c, containerName)
  29. assert.NilError(c, err, "output: %v", out)
  30. assert.Equal(c, strings.TrimSpace(out), "2 2 700\n65534 65534 400", "output: %v", out)
  31. }
  32. // Check ownership is root, both in non-userns and userns enabled modes
  33. func (s *DockerSuite) TestCpCheckDestOwnership(c *testing.T) {
  34. testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
  35. tmpVolDir := getTestDir(c, "test-cp-tmpvol")
  36. containerID := makeTestContainer(c,
  37. testContainerOptions{volumes: []string{fmt.Sprintf("%s:/tmpvol", tmpVolDir)}})
  38. tmpDir := getTestDir(c, "test-cp-to-check-ownership")
  39. defer os.RemoveAll(tmpDir)
  40. makeTestContentInDir(c, tmpDir)
  41. srcPath := cpPath(tmpDir, "file1")
  42. dstPath := containerCpPath(containerID, "/tmpvol", "file1")
  43. assert.NilError(c, runDockerCp(c, srcPath, dstPath))
  44. stat, err := system.Stat(filepath.Join(tmpVolDir, "file1"))
  45. assert.NilError(c, err)
  46. uid, gid, err := getRootUIDGID()
  47. assert.NilError(c, err)
  48. assert.Equal(c, stat.UID(), uint32(uid), "Copied file not owned by container root UID")
  49. assert.Equal(c, stat.GID(), uint32(gid), "Copied file not owned by container root GID")
  50. }
  51. func getRootUIDGID() (int, int, error) {
  52. uidgid := strings.Split(filepath.Base(testEnv.DaemonInfo.DockerRootDir), ".")
  53. if len(uidgid) == 1 {
  54. // user namespace remapping is not turned on; return 0
  55. return 0, 0, nil
  56. }
  57. uid, err := strconv.Atoi(uidgid[0])
  58. if err != nil {
  59. return 0, 0, err
  60. }
  61. gid, err := strconv.Atoi(uidgid[1])
  62. if err != nil {
  63. return 0, 0, err
  64. }
  65. return uid, gid, nil
  66. }