From 789c02ae79dabcaa5c141baf99532f464472b0c8 Mon Sep 17 00:00:00 2001 From: Michael Sherman Date: Thu, 11 Jun 2026 17:04:47 -0500 Subject: Rework tar normalization to a shared function Add missing tests Change-Id: I91b42fe530f87af7d78bb1a543b29a4b0d50872e Signed-off-by: Michael Sherman --- kolla/image/tasks.py | 44 ++++++++++++++++++++------------------------ 1 file changed, 20 insertions(+), 24 deletions(-) (limited to 'kolla/image') diff --git a/kolla/image/tasks.py b/kolla/image/tasks.py index f8d07e221..9eec24a8f 100644 --- a/kolla/image/tasks.py +++ b/kolla/image/tasks.py @@ -44,6 +44,21 @@ class ArchivingError(Exception): pass +def normalize_tarinfo(tarinfo): + # NOTE(mgoddard): Change ownership of files to root:root. This + # avoids an issue introduced by the fix for git CVE-2022-24765, + # which breaks PBR when the source checkout is not owned by the + # user installing it. LP#1969096 + tarinfo.uid = tarinfo.gid = 0 + tarinfo.uname = tarinfo.gname = "root" + # Pin mtime so archive bytes do not depend on checkout/extraction + # time, keeping the engine's layer cache valid when the content is + # unchanged. An integer mtime also stops tarfile's PAX writer + # emitting per-entry float-mtime extended headers. + tarinfo.mtime = 0 + return tarinfo + + class EngineTask(task.Task): def __init__(self, conf): super(EngineTask, self).__init__() @@ -194,16 +209,6 @@ class BuildTask(EngineTask): dest_archive = os.path.join(image.path, source['name'] + '-archive') - # NOTE(mgoddard): Change ownership of files to root:root. This - # avoids an issue introduced by the fix for git CVE-2022-24765, - # which breaks PBR when the source checkout is not owned by the - # user installing it. LP#1969096 - def reset_userinfo(tarinfo): - tarinfo.uid = tarinfo.gid = 0 - tarinfo.uname = tarinfo.gname = "root" - tarinfo.mtime = 0 - return tarinfo - if source.get('type') == 'url': self.logger.debug("Getting archive from %s", source['source']) try: @@ -263,7 +268,7 @@ class BuildTask(EngineTask): with tarfile.open(dest_archive, 'w') as tar: tar.add(clone_dir, arcname=os.path.basename(clone_dir), - filter=reset_userinfo) + filter=normalize_tarinfo) elif source.get('type') == 'local': self.logger.debug("Getting local archive from %s", @@ -272,7 +277,7 @@ class BuildTask(EngineTask): with tarfile.open(dest_archive, 'w') as tar: tar.add(source['source'], arcname=os.path.basename(source['source']), - filter=reset_userinfo) + filter=normalize_tarinfo) else: shutil.copyfile(source['source'], dest_archive) @@ -349,19 +354,10 @@ class BuildTask(EngineTask): raise ArchivingError arc_path = os.path.join(image.path, '%s-archive' % arcname) - # NOTE(jneumann): Change ownership of files to root:root. This - # avoids an issue introduced by the fix for git CVE-2022-24765, - # which breaks PBR when the source checkout is not owned by the - # user installing it. LP#1969096 - def reset_userinfo(tarinfo): - tarinfo.uid = tarinfo.gid = 0 - tarinfo.uname = tarinfo.gname = "root" - tarinfo.mtime = 0 - return tarinfo - with tarfile.open(arc_path, 'w') as tar: - tar.add(items_path, arcname=arcname, filter=reset_userinfo) - os.utime(arc_path, (0, 0)) + tar.add(items_path, arcname=arcname, + filter=normalize_tarinfo) + return len(os.listdir(items_path)) self.logger.debug('Processing') -- cgit v1.2.3