Skip to content

Commit 779aa83

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) (cherry picked from commit 5da2ff0)
1 parent 9b350df commit 779aa83

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

kolla/image/build.py

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

493493
def _test_malicious_tarball(archive, path):
494-
tar_file = tarfile.open(archive, 'r|gz')
494+
tar_file = tarfile.open(archive, 'r|*')
495495
for n in tar_file.getnames():
496496
if not os.path.abspath(os.path.join(path, n)).startswith(path):
497497
tar_file.close()

kolla/tests/test_build.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,83 @@ def test_process_source(self, mock_get, mock_client,
301301
else:
302302
self.assertIsNotNone(get_result)
303303

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

0 commit comments

Comments
 (0)