Skip to content

Commit 328c0c8

Browse files
committed
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 <maksim.malchuk@gmail.com> Co-Authored-by: Michal Nasiadka <mnasiadka@gmail.com> (cherry picked from commit 143765f)
1 parent a8d0098 commit 328c0c8

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

kolla/image/tasks.py

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

267267
def _test_malicious_tarball(archive, path):
268-
tar_file = tarfile.open(archive, 'r|gz')
268+
tar_file = tarfile.open(archive, 'r|*')
269269
for n in tar_file.getnames():
270270
if not os.path.abspath(os.path.join(path, n)).startswith(path):
271271
tar_file.close()

kolla/tests/test_build.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,81 @@ def test_process_source(self, mock_get, mock_client,
291291
else:
292292
self.assertIsNotNone(get_result)
293293

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

0 commit comments

Comments
 (0)