docker_cli_cp_to_container_unix_test.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //go:build !windows
  2. package main
  3. import (
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "path/filepath"
  8. "strconv"
  9. "strings"
  10. "syscall"
  11. "testing"
  12. "gotest.tools/v3/assert"
  13. )
  14. func (s *DockerCLICpSuite) 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, "busybox", "/bin/sh", "-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 *DockerCLICpSuite) 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 := os.Stat(filepath.Join(tmpVolDir, "file1"))
  45. assert.NilError(c, err)
  46. uid, gid, err := getRootUIDGID()
  47. assert.NilError(c, err)
  48. fi := stat.Sys().(*syscall.Stat_t)
  49. assert.Equal(c, fi.Uid, uint32(uid), "Copied file not owned by container root UID")
  50. assert.Equal(c, fi.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. }