Skip to contentSkip to conversation

Sign packages in RPM feeds

PATCH In review Markus Lehtonen 1 participant 6 messages Aug 27, 2015 – Aug 28, 2015

lore
Share & tools
lore
#0 lore raw

v2 cover Sign packages in RPM feeds

Second iteration of my patchset. I tried to address the issues pointed out by Mark:
1. The gpg key is not imported to the (temporary) rpm databases used by createrepo. Instead, createrepo is patched to ignore signature verification altogether.
2. There is a new optional config variable GPG_BIN which can be used to define the gpg binary used for signing.
3. The filename of the public keys (published with the package feed and depoyed into the target rootfs as part of os-release package) is now postfixed with "-${DISTRO_VERSION}".

[YOCTO #8134]

*** BLURB HERE ***

Markus Lehtonen (4):
createrepo: disable RPM signature validation
package_rpm: support signing of rpm packages
os-release: add the public package-signing key
package_manager: support for signed RPM package feeds

 meta/classes/package_rpm.bbclass                   |  5 ++
 meta/classes/sign_rpm.bbclass                      | 60 ++++++++++++++++++++++
 meta/lib/oe/package_manager.py                     | 40 +++++++++++++++
 meta/recipes-core/os-release/os-release.bb         | 11 ++++
 ...dumpMetadata-disable-signature-validation.patch | 31 +++++++++++
 .../createrepo/createrepo_0.4.11.bb                | 17 +++---
 6 files changed, 156 insertions(+), 8 deletions(-)
 create mode 100644 meta/classes/sign_rpm.bbclass
 create mode 100644 meta/recipes-support/createrepo/createrepo/dumpMetadata-disable-signature-validation.patch
Signature · 1 line
2.1.4
#1 lore raw

PATCH v2 1/4 createrepo: disable RPM signature validation

Disable RPM signature validation so that it is possible to create package feeds of signed RPM packages without importing the public part of the signing key into the RPM database. In any case, the signatures are validated when the packages in the feed are used (e.g. in image generation of manually installing packages from the feed).

The original idea idea of this patch is from Mark Hatle <[email protected]>.

[YOCTO #8134]

---
 ...dumpMetadata-disable-signature-validation.patch | 31 ++++++++++++++++++++++
 .../createrepo/createrepo_0.4.11.bb                | 17 ++++++------
 2 files changed, 40 insertions(+), 8 deletions(-)
 create mode 100644 meta/recipes-support/createrepo/createrepo/dumpMetadata-disable-signature-validation.patch
meta/recipes-support/createrepo/createrepo/dumpMetadata-disable-signature-validation.patch new file +31 -0
new file mode 100644
index 0000000..905531f
@@ -0,0 +1,31 @@
+dumpMetadata: disable signature validation
+
+Makes it possible to work on RPM repositories that contain signed packages
+without the need of importing the public part of the signing key into the RPM
+database.
+
+Upstream-Status: Pending
+
+Signed-off-by: Markus Lehtonen <[email protected]>
+---
+ dumpMetadata.py | 4 +++-
+ 1 file changed, 3 insertions(+), 1 deletion(-)
+
+diff --git a/dumpMetadata.py b/dumpMetadata.py
+index e40e8ac..ca6a82d 100644
+--- a/dumpMetadata.py
++++ b/dumpMetadata.py
+@@ -92,7 +92,9 @@ def returnHdr(ts, package):
+             fdno = package # let's assume this is an fdno and go with it :)
+     except OSError:
+         raise MDError, "Error opening file"
+-    ts.setVSFlags((rpm.RPMVSF_NOMD5|rpm.RPMVSF_NEEDPAYLOAD))
++    ts.setVSFlags((rpm.RPMVSF_NOMD5|rpm.RPMVSF_NEEDPAYLOAD|
++                   rpm.RPMVSF_NODSA|rpm.RPMVSF_NORSA|
++                   rpm.RPMVSF_NODSAHEADER|rpm.RPMVSF_NORSAHEADER))
+     try:
+         hdr = ts.hdrFromFdno(fdno)
+     except rpm.error:
+-- 
+2.1.4
+
meta/recipes-support/createrepo/createrepo_0.4.11.bb +9 -8
index adc193e..debbaec 100644
@@ -9,14 +9,15 @@ RDEPENDS_${PN}_class-target = "libxml2-python"
 
 PR = "r9"
 
-SRC_URI= "http://createrepo.baseurl.org/download/${BP}.tar.gz \
-          file://fix-native-install.patch \
-          file://python-scripts-should-use-interpreter-from-env.patch \
-	  file://createrepo-rpm549.patch \
-	  file://recommends.patch \
-	  file://createrepo-dbpath.patch \
-	  file://rpm-createsolvedb.py \
-         "
+SRC_URI = "http://createrepo.baseurl.org/download/${BP}.tar.gz \
+           file://fix-native-install.patch \
+           file://python-scripts-should-use-interpreter-from-env.patch \
+           file://createrepo-rpm549.patch \
+           file://recommends.patch \
+           file://createrepo-dbpath.patch \
+           file://dumpMetadata-disable-signature-validation.patch \
+           file://rpm-createsolvedb.py \
+           "
 
 SRC_URI[md5sum] = "3e9ccf4abcffe3f49af078c83611eda2"
 SRC_URI[sha256sum] = "a73ae11a0dcde8bde36d900bc3f7f8f1083ba752c70a5c61b72d1e1e7608f21b"
Signature · 1 line
2.1.4
#2 lore raw

PATCH v2 2/4 package_rpm: support signing of rpm packages

In reply to Markus Lehtonen

This patch adds a new bbclass for generating rpm packages that are signed with a user defined key. The packages are signed as part of the "package_write_rpm" task.

In order to enable the feature you need to
1. 'INHERIT += " sign_rpm"' in bitbake config (e.g. local or distro)
2. Create a file that contains the passphrase to your gpg secret key
3. 'RPM_GPG_PASSPHRASE_FILE = "<path_to_file>" in bitbake config, pointing to the passphrase file created in 2.
4. Define GPG key name to use by either defining 'RPM_GPG_NAME = "<key_id>" in bitbake config OR by defining %_gpg_name <key_id> in your ~/.oerpmmacros file
5. 'RPM_GPG_PUBKEY = "<path_to_pubkey>" in bitbake config pointing to the public key (in "armor" format)

The user may optionally define "GPG_CMD" variable in the bitbake configuration in order to specify a specific gpg binary/wrapper to use.

The sign_rpm.bbclass implements a simple scenario of locally signing the packages. It could be replaced by a more advanced class that would utilize a separate signing server for signing the packages, for example.

[YOCTO #8134]

---
 meta/classes/package_rpm.bbclass |  5 ++++
 meta/classes/sign_rpm.bbclass    | 60 ++++++++++++++++++++++++++++++++++++++++
 meta/lib/oe/package_manager.py   | 17 ++++++++++++
 3 files changed, 82 insertions(+)
 create mode 100644 meta/classes/sign_rpm.bbclass
meta/classes/package_rpm.bbclass +5 -0
index 8fd0685..3e933ef 100644
@@ -695,6 +695,8 @@ python do_package_rpm () {
     else:
         d.setVar('PACKAGE_ARCH_EXTEND', package_arch)
     pkgwritedir = d.expand('${PKGWRITEDIRRPM}/${PACKAGE_ARCH_EXTEND}')
+    d.setVar('RPM_PKGWRITEDIR', pkgwritedir)
+    bb.debug(1, 'PKGWRITEDIR: %s' % d.getVar('RPM_PKGWRITEDIR', True))
     pkgarch = d.expand('${PACKAGE_ARCH_EXTEND}${HOST_VENDOR}-${HOST_OS}')
     magicfile = d.expand('${STAGING_DIR_NATIVE}${datadir_native}/misc/magic.mgc')
     bb.utils.mkdirhier(pkgwritedir)
@@ -730,6 +732,9 @@ python do_package_rpm () {
     d.setVar('BUILDSPEC', cmd + "\n")
     d.setVarFlag('BUILDSPEC', 'func', '1')
     bb.build.exec_func('BUILDSPEC', d)
+
+    if d.getVar('RPM_SIGN_PACKAGES', True) == '1':
+        bb.build.exec_func("sign_rpm", d)
 }
 
 python () {
meta/classes/sign_rpm.bbclass new file +60 -0

60 lines not on this page · raw message

meta/lib/oe/package_manager.py +17 -0
index 2ab1d78..936887c 100644
@@ -108,6 +108,7 @@ class RpmIndexer(Indexer):
         archs = archs.union(set(sdk_pkg_archs))
 
         rpm_createrepo = bb.utils.which(os.getenv('PATH'), "createrepo")
+
         index_cmds = []
         rpm_dirs_found = False
         for arch in archs:
@@ -127,9 +128,16 @@ class RpmIndexer(Indexer):
             bb.note("There are no packages in %s" % self.deploy_dir)
             return
 
+        # Create repodata
         result = oe.utils.multiprocess_exec(index_cmds, create_index)
         if result:
             bb.fatal('%s' % ('\n'.join(result)))
+        # Copy pubkey to repo
+        distro_version = self.d.getVar('DISTRO_VERSION', True) or "oe.0"
+        if self.d.getVar('RPM_SIGN_PACKAGES', True) == '1':
+            shutil.copy2(self.d.getVar('RPM_GPG_PUBKEY', True),
+                         os.path.join(self.deploy_dir,
+                                      'RPM-GPG-KEY-%s' % distro_version))
 
 
 class OpkgIndexer(Indexer):
@@ -352,6 +360,9 @@ class RpmPkgsList(PkgsList):
             pkg = line.split()[0]
             arch = line.split()[1]
             ver = line.split()[2]
+            # Skip GPG keys
+            if pkg == 'gpg-pubkey':
+                continue
             if self.rpm_version == 4:
                 pkgorigin = "unknown"
             else:
@@ -864,6 +875,12 @@ class RpmPM(PackageManager):
         except subprocess.CalledProcessError as e:
             bb.fatal("Create rpm database failed. Command '%s' "
                      "returned %d:\n%s" % (cmd, e.returncode, e.output))
+        # Import GPG key to RPM database of the target system
+        if self.d.getVar('RPM_SIGN_PACKAGES', True) == '1':
+            pubkey_path = self.d.getVar('RPM_GPG_PUBKEY', True)
+            cmd = "%s --root %s --dbpath /var/lib/rpm --import %s > /dev/null" % (
+                  self.rpm_cmd, self.target_rootfs, pubkey_path)
+            subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True)
 
         # Configure smart
         bb.note("configuring Smart settings")
Signature · 1 line
2.1.4
#3 lore raw

PATCH v2 3/4 os-release: add the public package-signing key

In reply to Markus Lehtonen

Adds the public package-signing key into this package. It will be installed under /etc/pki/rpm-gpg if the RPM signing feature is used. The key file is not currently directly used by anything in the target system. It is merely there for possible later use.

[YOCTO #8134]

---
 meta/recipes-core/os-release/os-release.bb | 11 +++++++++++
 1 file changed, 11 insertions(+)
meta/recipes-core/os-release/os-release.bb +11 -0
index 87fea6f..f24882a 100644
@@ -23,15 +23,26 @@ PRETTY_NAME = "${DISTRO_NAME} ${VERSION}"
 BUILD_ID ?= "${DATETIME}"
 
 python do_compile () {
+    import shutil
     with open(d.expand('${B}/os-release'), 'w') as f:
         for field in d.getVar('OS_RELEASE_FIELDS', True).split():
             value = d.getVar(field, True)
             if value:
                 f.write('{0}={1}\n'.format(field, value))
+    if d.getVar('RPM_SIGN_PACKAGES', True) == '1':
+        rpm_gpg_pubkey = d.getVar('RPM_GPG_PUBKEY', True)
+        os.mkdir('${B}/rpm-gpg')
+        distro_version = self.d.getVar('DISTRO_VERSION', True) or "oe.0"
+        shutil.copy2(rpm_gpg_pubkey, d.expand('${B}/rpm-gpg/RPM-GPG-KEY-%s' % distro_version))
 }
 do_compile[vardeps] += "${OS_RELEASE_FIELDS}"
 
 do_install () {
     install -d ${D}${sysconfdir}
     install -m 0644 os-release ${D}${sysconfdir}/
+
+    if [ -d "rpm-gpg" ]; then
+        install -d "${D}${sysconfdir}/pki"
+        cp -r "rpm-gpg" "${D}${sysconfdir}/pki/"
+    fi
 }
Signature · 1 line
2.1.4
#4 lore raw

PATCH v2 4/4 package_manager: support for signed RPM package feeds

In reply to Markus Lehtonen

This change makes it possible to create GPG signed RPM package feeds - i.e. package feed with GPG signed metadata (repodata). All deployed RPM repositories will be signed and the GPG public key is copied to the rpm deployment directory.

In order to enable the new feature one needs to define four variables in bitbake configuration.
1. 'PACKAGE_FEED_SIGN = "1"' enabling the feature
2. 'PACKAGE_FEED_GPG_NAME = "<key_id>"' defining the GPG key to use for signing
3. 'PACKAGE_FEED_GPG_PASSPHRASE_FILE = "<path_to_file>"' pointing to a file containing the passphrase for the secret signing key
4. 'PACKAGE_FEED_GPG_PUBKEY = "<path_to_pubkey>"' pointing to the corresponding public key (in "armor" format)
The user may define "GPG_CMD" in the bitbake configuration in order to specify a specific the gpg binary/wrapper to use for signing.

[YOCTO #8134]

---
 meta/lib/oe/package_manager.py | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)
meta/lib/oe/package_manager.py +24 -1
index 936887c..83c119a 100644
@@ -108,8 +108,17 @@ class RpmIndexer(Indexer):
         archs = archs.union(set(sdk_pkg_archs))
 
         rpm_createrepo = bb.utils.which(os.getenv('PATH'), "createrepo")
+        if self.d.getVar('PACKAGE_FEED_SIGN', True) == '1':
+            pkgfeed_gpg_name = self.d.getVar('PACKAGE_FEED_GPG_NAME', True)
+            pkgfeed_gpg_pass = self.d.getVar('PACKAGE_FEED_GPG_PASSPHRASE_FILE', True)
+        else:
+            pkgfeed_gpg_name = None
+            pkgfeed_gpg_pass = None
+        gpg_bin = self.d.getVar('GPG_BIN', True) or \
+                  bb.utils.which(os.getenv('PATH'), "gpg")
 
         index_cmds = []
+        repo_sign_cmds = []
         rpm_dirs_found = False
         for arch in archs:
             dbpath = os.path.join(self.d.getVar('WORKDIR', True), 'rpmdb', arch)
@@ -121,6 +130,12 @@ class RpmIndexer(Indexer):
 
             index_cmds.append("%s --dbpath %s --update -q %s" % \
                              (rpm_createrepo, dbpath, arch_dir))
+            if pkgfeed_gpg_name:
+                repomd_file = os.path.join(arch_dir, 'repodata', 'repomd.xml')
+                gpg_cmd = "%s --detach-sign --armor --batch --no-tty --yes " \
+                          "--passphrase-file '%s' -u '%s' %s" % (gpg_bin,
+                          pkgfeed_gpg_pass, pkgfeed_gpg_name, repomd_file)
+                repo_sign_cmds.append(gpg_cmd)
 
             rpm_dirs_found = True
 
@@ -132,12 +147,20 @@ class RpmIndexer(Indexer):
         result = oe.utils.multiprocess_exec(index_cmds, create_index)
         if result:
             bb.fatal('%s' % ('\n'.join(result)))
-        # Copy pubkey to repo
+        # Sign repomd
+        result = oe.utils.multiprocess_exec(repo_sign_cmds, create_index)
+        if result:
+            bb.fatal('%s' % ('\n'.join(result)))
+        # Copy pubkey(s) to repo
         distro_version = self.d.getVar('DISTRO_VERSION', True) or "oe.0"
         if self.d.getVar('RPM_SIGN_PACKAGES', True) == '1':
             shutil.copy2(self.d.getVar('RPM_GPG_PUBKEY', True),
                          os.path.join(self.deploy_dir,
                                       'RPM-GPG-KEY-%s' % distro_version))
+        if self.d.getVar('PACKAGE_FEED_SIGN', True) == '1':
+            shutil.copy2(self.d.getVar('PACKAGE_FEED_GPG_PUBKEY', True),
+                         os.path.join(self.deploy_dir,
+                                      'REPODATA-GPG-KEY-%s' % distro_version))
 
 
 class OpkgIndexer(Indexer):
Signature · 1 line
2.1.4
#5 lore raw
In reply to Markus Lehtonen

Hi,

Quoted 30 lines · Markus Lehtonen

Second iteration of my patchset. I tried to address the issues pointed out by
Mark:
1. The gpg key is not imported to the (temporary) rpm databases used by createrepo. Instead, createrepo is patched to ignore signature verification altogether.
2. There is a new optional config variable GPG_BIN which can be used to define the gpg binary used for signing.
3. The filename of the public keys (published with the package feed and depoyed into the target rootfs as part of os-release package) is now postfixed with "-${DISTRO_VERSION}".

[YOCTO #8134]

*** BLURB HERE ***

Markus Lehtonen (4):
createrepo: disable RPM signature validation
package_rpm: support signing of rpm packages
os-release: add the public package-signing key
package_manager: support for signed RPM package feeds

meta/classes/package_rpm.bbclass | 5 ++
meta/classes/sign_rpm.bbclass | 60
++++++++++++++++++++++

meta/lib/oe/package_manager.py                     | 40 +++++++++++++++
meta/recipes-core/os-release/os-release.bb         | 11 ++++
...dumpMetadata-disable-signature-validation.patch | 31 +++++++++++
.../createrepo/createrepo_0.4.11.bb                | 17 +++---
6 files changed, 156 insertions(+), 8 deletions(-)
Quoted 4 lines

create mode 100644 meta/classes/sign_rpm.bbclass
create mode 100644 meta/recipes-support/createrepo/createrepo/dumpMetadata-disable-signature- validation.patch

Please use a slightly updated version of the patchset found here:
git://git.openembedded.org/openembedded-core-contrib marquiz/rpmsign

http://git.openembedded.org/openembedded-core-contrib/log/?h=marquiz/rpmsig
n

I noticed some typos in the commit messages of my v2 patchset (GPG_CMD vs. GPG_BIN). Also, I added a comment header to the sign_rpm.bbclass file.

Thanks,
Markus