Skip to content

Commit 08071f4

Browse files
mnasiadkammalchuk
authored andcommitted
Fix test malicious tarball fail
Since I650fcbc8f773fad8116338f6fb0cf7b4f4f17b33 builds from git fails on plugins with an exception: 'tarfile.ReadError: not a gzip file' because the test checks only gzip compressed archives but plugins created as plain tar files. This change fixes the issue using transparent compression support and also adds some debug info. Closes-Bug: #1990432 Change-Id: If0f9b4dd058a257d0653332d1b663e150c717304 Signed-off-by: Maksim Malchuk <[email protected]> Co-Authored-by: Michal Nasiadka <[email protected]> (cherry picked from commit 143765f)
1 parent faf6ac9 commit 08071f4

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

kolla/image/build.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ def update_buildargs(self):
502502
def builder(self, image):
503503

504504
def _test_malicious_tarball(archive, path):
505-
tar_file = tarfile.open(archive, 'r|gz')
505+
tar_file = tarfile.open(archive, 'r|*')
506506
for n in tar_file.getnames():
507507
if not os.path.abspath(os.path.join(path, n)).startswith(path):
508508
tar_file.close()

kolla/tests/test_build.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,81 @@ def test_process_source(self, mock_get, mock_client,
305305
else:
306306
self.assertIsNotNone(get_result)
307307

308+
@mock.patch.dict(os.environ, clear=True)
309+
@mock.patch('docker.APIClient')
310+
def test_local_directory(self, mock_client):
311+
tmpdir = tempfile.mkdtemp()
312+
file_name = 'test.txt'
313+
file_path = os.path.join(tmpdir, file_name)
314+
saved_umask = os.umask(0o077)
315+
316+
try:
317+
with open(file_path, 'w') as f:
318+
f.write('Hello')
319+
320+
self.dc = mock_client
321+
self.image.plugins = [{
322+
'name': 'fake-image-base-plugin-test',
323+
'type': 'local',
324+
'enabled': True,
325+
'source': tmpdir}
326+
]
327+
push_queue = mock.Mock()
328+
builder = build.BuildTask(self.conf, self.image, push_queue)
329+
builder.run()
330+
self.assertTrue(builder.success)
331+
332+
except IOError:
333+
print('IOError')
334+
else:
335+
os.remove(file_path)
336+
finally:
337+
os.umask(saved_umask)
338+
os.rmdir(tmpdir)
339+
308340
@mock.patch.dict(os.environ, clear=True)
309341
@mock.patch('docker.APIClient')
310342
def test_malicious_tar(self, mock_client):
343+
tmpdir = tempfile.mkdtemp()
344+
file_name = 'test.txt'
345+
archive_name = 'my_archive.tar'
346+
file_path = os.path.join(tmpdir, file_name)
347+
archive_path = os.path.join(tmpdir, archive_name)
348+
# Ensure the file is read/write by the creator only
349+
saved_umask = os.umask(0o077)
350+
351+
try:
352+
with open(file_path, 'w') as f:
353+
f.write('Hello')
354+
355+
with tarfile.open(archive_path, 'w') as tar:
356+
tar.add(file_path, arcname='../test.txt')
357+
358+
self.dc = mock_client
359+
self.image.plugins = [{
360+
'name': 'fake-image-base-plugin-test',
361+
'type': 'local',
362+
'enabled': True,
363+
'source': archive_path}
364+
]
365+
366+
push_queue = mock.Mock()
367+
builder = build.BuildTask(self.conf, self.image, push_queue)
368+
builder.run()
369+
self.assertFalse(builder.success)
370+
371+
except IOError:
372+
print('IOError')
373+
else:
374+
os.remove(file_path)
375+
os.remove(archive_path)
376+
finally:
377+
os.umask(saved_umask)
378+
os.rmdir(tmpdir)
379+
380+
@mock.patch.dict(os.environ, clear=True)
381+
@mock.patch('docker.APIClient')
382+
def test_malicious_tar_gz(self, mock_client):
311383
tmpdir = tempfile.mkdtemp()
312384
file_name = 'test.txt'
313385
archive_name = 'my_archive.tar.gz'

0 commit comments

Comments
 (0)