summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoffrey F <f.joffrey@gmail.com>2018-02-05 13:37:01 -0800
committerGitHub <noreply@github.com>2018-02-05 13:37:01 -0800
commit7c19772eb681b514b12e2e5764537605632608b1 (patch)
tree66d3994b4094f1f108c6ddcfc1982e1d9b50fdb3
parent855b71eabe54733c49ae2814c88ac91a34ec5da6 (diff)
parent58639aecfa50e0bcfbd1415dc8bab2b4448f4d81 (diff)
downloaddocker-py-7c19772eb681b514b12e2e5764537605632608b1.tar.gz
Merge pull request #1901 from docker/1899-create_archive_fix
Rewrite access check in create_archive with EAFP
-rw-r--r--docker/utils/utils.py8
-rw-r--r--tests/unit/utils_test.py8
2 files changed, 9 insertions, 7 deletions
diff --git a/docker/utils/utils.py b/docker/utils/utils.py
index b145f11..3cd2be8 100644
--- a/docker/utils/utils.py
+++ b/docker/utils/utils.py
@@ -97,10 +97,6 @@ def create_archive(root, files=None, fileobj=None, gzip=False):
for path in files:
full_path = os.path.join(root, path)
- if os.lstat(full_path).st_mode & os.R_OK == 0:
- raise IOError(
- 'Can not access file in context: {}'.format(full_path)
- )
i = t.gettarinfo(full_path, arcname=path)
if i is None:
# This happens when we encounter a socket file. We can safely
@@ -121,7 +117,9 @@ def create_archive(root, files=None, fileobj=None, gzip=False):
with open(full_path, 'rb') as f:
t.addfile(i, f)
except IOError:
- t.addfile(i, None)
+ raise IOError(
+ 'Can not read file in context: {}'.format(full_path)
+ )
else:
# Directories, FIFOs, symlinks... don't need to be read.
t.addfile(i, None)
diff --git a/tests/unit/utils_test.py b/tests/unit/utils_test.py
index 1558891..eedcf71 100644
--- a/tests/unit/utils_test.py
+++ b/tests/unit/utils_test.py
@@ -933,7 +933,10 @@ class TarTest(unittest.TestCase):
tar_data = tarfile.open(fileobj=archive)
assert sorted(tar_data.getnames()) == ['bar', 'foo']
- @pytest.mark.skipif(IS_WINDOWS_PLATFORM, reason='No chmod on Windows')
+ @pytest.mark.skipif(
+ IS_WINDOWS_PLATFORM or os.geteuid() == 0,
+ reason='root user always has access ; no chmod on Windows'
+ )
def test_tar_with_inaccessible_file(self):
base = tempfile.mkdtemp()
full_path = os.path.join(base, 'foo')
@@ -944,8 +947,9 @@ class TarTest(unittest.TestCase):
with pytest.raises(IOError) as ei:
tar(base)
- assert 'Can not access file in context: {}'.format(full_path) in \
+ assert 'Can not read file in context: {}'.format(full_path) in (
ei.exconly()
+ )
@pytest.mark.skipif(IS_WINDOWS_PLATFORM, reason='No symlinks on Windows')
def test_tar_with_file_symlinks(self):