docker_cli_cp_to_container_unix_test.go 2.5 KB

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