From 7afba1087499d047cc86048e8d75c25506905c08 Mon Sep 17 00:00:00 2001 From: Tomas Kopecek Date: Jan 02 2020 09:45:56 +0000 Subject: PR#1829: Add a sanity check on remotely opened RPMs Merges #1829 https://pagure.io/koji/pull-request/1829 Fixes: #290 https://pagure.io/koji/issue/290 consider some validation/retry/check for openRemoteFile --- diff --git a/builder/kojid b/builder/kojid index b0f031d..9cc959c 100755 --- a/builder/kojid +++ b/builder/kojid @@ -1106,6 +1106,7 @@ class BuildTask(BaseTaskHandler): opts = dict([(k, getattr(self.options, k)) for k in ('topurl','topdir')]) opts['tempdir'] = self.workdir with koji.openRemoteFile(relpath, **opts) as fo: + koji.check_rpm_file(fo) h = koji.get_rpm_header(fo) if not koji.get_header_field(h, 'sourcepackage'): raise koji.BuildError("%s is not a source package" % srpm) diff --git a/koji/__init__.py b/koji/__init__.py index 69940cb..aa4371e 100644 --- a/koji/__init__.py +++ b/koji/__init__.py @@ -1691,6 +1691,38 @@ def openRemoteFile(relpath, topurl=None, topdir=None, tempdir=None): return fo +def check_rpm_file(rpmfile): + """Do a initial sanity check on an RPM + + rpmfile can either be a file name or a file object + + This check is used to detect issues with RPMs before they break builds + See: https://pagure.io/koji/issue/290 + """ + if isinstance(rpmfile, six.string_types): + with open(rpmfile, 'rb') as fo: + return _check_rpm_file(fo) + else: + return _check_rpm_file(rpmfile) + + +def _check_rpm_file(fo): + """Check that the open file appears to be an rpm""" + # TODO: trap exception and raise something with more infomation + ts = rpm.TransactionSet() + try: + hdr = ts.hdrFromFdno(fo.fileno()) + except rpm.error as ex: + raise GenericError("rpm's header can't be extracted: %s (rpm error: %s)" % \ + (fo.name, ', '.join(ex.args))) + try: + rpm.TransactionSet().hdrCheck(hdr.unload()) + except rpm.error as ex: + raise GenericError("rpm's header can't be checked: %s (rpm error: %s)" % \ + (fo.name, ', '.join(ex.args))) + fo.seek(0) + + def config_directory_contents(dir_name, strict=False): configs = [] try: