fix copy change file owner if cp from container

If copies file from container to local machine, change the file owner to the cp command caller.

Signed-off-by: Qi Wang <qiwan@redhat.com>
This commit is contained in:
Qi Wang 2019-08-08 16:48:17 -04:00
parent 09cedd152d
commit a2561ec58a
2 changed files with 39 additions and 2 deletions

View file

@ -140,7 +140,7 @@ func copyBetweenHostAndContainer(runtime *libpod.Runtime, src string, dest strin
if err != nil {
return errors.Wrapf(err, "error getting IDMappingOptions")
}
containerOwner := idtools.IDPair{UID: int(user.UID), GID: int(user.GID)}
destOwner := idtools.IDPair{UID: int(user.UID), GID: int(user.GID)}
hostUID, hostGID, err := util.GetHostIDs(convertIDMap(idMappingOpts.UIDMap), convertIDMap(idMappingOpts.GIDMap), user.UID, user.GID)
if err != nil {
return err
@ -183,6 +183,7 @@ func copyBetweenHostAndContainer(runtime *libpod.Runtime, src string, dest strin
destPath = cleanedPath
}
} else {
destOwner = idtools.IDPair{UID: os.Getuid(), GID: os.Getgid()}
if isVol, volDestName, volName := isVolumeDestName(srcPath, ctr); isVol {
path, err := pathWithVolumeMount(ctr, runtime, volDestName, volName, srcPath)
if err != nil {
@ -230,7 +231,7 @@ func copyBetweenHostAndContainer(runtime *libpod.Runtime, src string, dest strin
src = os.Stdin.Name()
extract = true
}
err := copy(src, destPath, dest, idMappingOpts, &containerOwner, extract, isFromHostToCtr)
err := copy(src, destPath, dest, idMappingOpts, &destOwner, extract, isFromHostToCtr)
if lastError != nil {
logrus.Error(lastError)
}

View file

@ -209,4 +209,40 @@ var _ = Describe("Podman cp", func() {
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(0))
})
It("podman cp from ctr chown ", func() {
setup := podmanTest.RunTopContainer("testctr")
setup.WaitWithDefaultTimeout()
Expect(setup.ExitCode()).To(Equal(0))
session := podmanTest.Podman([]string{"exec", "testctr", "adduser", "-S", "testuser"})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(0))
session = podmanTest.Podman([]string{"exec", "-u", "testuser", "testctr", "touch", "testfile"})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(0))
session = podmanTest.Podman([]string{"cp", "testctr:testfile", "testfile1"})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(0))
// owner of the file copied to local machine is not testuser
cmd := exec.Command("ls", "-l", "testfile1")
cmdRet, err := cmd.Output()
Expect(err).To(BeNil())
Expect(strings.Contains(string(cmdRet), "testuser")).To(BeFalse())
session = podmanTest.Podman([]string{"cp", "testfile1", "testctr:testfile2"})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(0))
// owner of the file copied to a container is the root user
session = podmanTest.Podman([]string{"exec", "-it", "testctr", "ls", "-l", "testfile2"})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(0))
Expect(session.OutputToString()).To(ContainSubstring("root"))
os.Remove("testfile1")
})
})