Skip to content

Commit 5da2ff0

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 9a86a7b commit 5da2ff0

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
@@ -469,7 +469,7 @@ def update_buildargs(self):
469469
def builder(self, image):
470470

471471
def _test_malicious_tarball(archive, path):
472-
tar_file = tarfile.open(archive, 'r|gz')
472+
tar_file = tarfile.open(archive, 'r|*')
473473
for n in tar_file.getnames():
474474
if not os.path.abspath(os.path.join(path, n)).startswith(path):
475475
tar_file.close()

kolla/tests/test_build.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,81 @@ def test_process_source(self, mock_get, mock_client,
300300
else:
301301
self.assertIsNotNone(get_result)
302302

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

0 commit comments

Comments
 (0)