signing.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #
  2. # Copyright OpenEmbedded Contributors
  3. #
  4. # SPDX-License-Identifier: MIT
  5. #
  6. from oeqa.selftest.case import OESelftestTestCase
  7. from oeqa.utils.commands import runCmd, bitbake, get_bb_var, get_bb_vars, create_temp_layer
  8. import os
  9. import oe
  10. import glob
  11. import re
  12. import shutil
  13. import tempfile
  14. from contextlib import contextmanager
  15. from oeqa.utils.ftools import write_file
  16. class Signing(OESelftestTestCase):
  17. gpg_dir = ""
  18. pub_key_path = ""
  19. secret_key_path = ""
  20. def setup_gpg(self):
  21. bitbake('gnupg-native -c addto_recipe_sysroot')
  22. self.gpg_dir = tempfile.mkdtemp(prefix="oeqa-signing-")
  23. self.track_for_cleanup(self.gpg_dir)
  24. self.pub_key_path = os.path.join(self.testlayer_path, 'files', 'signing', "key.pub")
  25. self.secret_key_path = os.path.join(self.testlayer_path, 'files', 'signing', "key.secret")
  26. nsysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "gnupg-native")
  27. runCmd('gpg --agent-program=`which gpg-agent`\|--auto-expand-secmem --batch --homedir %s --import %s %s' % (self.gpg_dir, self.pub_key_path, self.secret_key_path), native_sysroot=nsysroot)
  28. return nsysroot + get_bb_var("bindir_native")
  29. @contextmanager
  30. def create_new_builddir(self, builddir, newbuilddir):
  31. bb.utils.mkdirhier(newbuilddir)
  32. oe.path.copytree(builddir + "/conf", newbuilddir + "/conf")
  33. oe.path.copytree(builddir + "/cache", newbuilddir + "/cache")
  34. origenv = os.environ.copy()
  35. for e in os.environ:
  36. if builddir + "/" in os.environ[e]:
  37. os.environ[e] = os.environ[e].replace(builddir + "/", newbuilddir + "/")
  38. if os.environ[e].endswith(builddir):
  39. os.environ[e] = os.environ[e].replace(builddir, newbuilddir)
  40. os.chdir(newbuilddir)
  41. try:
  42. yield
  43. finally:
  44. for e in origenv:
  45. os.environ[e] = origenv[e]
  46. os.chdir(builddir)
  47. def test_signing_packages(self):
  48. """
  49. Summary: Test that packages can be signed in the package feed
  50. Expected: Package should be signed with the correct key
  51. Expected: Images can be created from signed packages
  52. Product: oe-core
  53. Author: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
  54. Author: Alexander Kanavin <alex.kanavin@gmail.com>
  55. AutomatedBy: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
  56. """
  57. import oe.packagedata
  58. self.setup_gpg()
  59. package_classes = get_bb_var('PACKAGE_CLASSES')
  60. if 'package_rpm' not in package_classes:
  61. self.skipTest('This test requires RPM Packaging.')
  62. test_recipe = 'ed'
  63. feature = 'INHERIT += "sign_rpm"\n'
  64. feature += 'RPM_GPG_PASSPHRASE = "test123"\n'
  65. feature += 'RPM_GPG_NAME = "testuser"\n'
  66. feature += 'GPG_PATH = "%s"\n' % self.gpg_dir
  67. feature += 'PACKAGECONFIG:append:pn-rpm-native = " sequoia"\n'
  68. feature += 'PACKAGECONFIG:append:pn-rpm = " sequoia"\n'
  69. self.write_config(feature)
  70. bitbake('-c clean %s' % test_recipe)
  71. bitbake('-f -c package_write_rpm %s' % test_recipe)
  72. self.add_command_to_tearDown('bitbake -c clean %s' % test_recipe)
  73. needed_vars = ['PKGDATA_DIR', 'DEPLOY_DIR_RPM', 'PACKAGE_ARCH', 'STAGING_BINDIR_NATIVE']
  74. bb_vars = get_bb_vars(needed_vars, test_recipe)
  75. pkgdatadir = bb_vars['PKGDATA_DIR']
  76. pkgdata = oe.packagedata.read_pkgdatafile(pkgdatadir + "/runtime/ed")
  77. if 'PKGE' in pkgdata:
  78. pf = pkgdata['PN'] + "-" + pkgdata['PKGE'] + pkgdata['PKGV'] + '-' + pkgdata['PKGR']
  79. else:
  80. pf = pkgdata['PN'] + "-" + pkgdata['PKGV'] + '-' + pkgdata['PKGR']
  81. deploy_dir_rpm = bb_vars['DEPLOY_DIR_RPM']
  82. package_arch = bb_vars['PACKAGE_ARCH'].replace('-', '_')
  83. staging_bindir_native = bb_vars['STAGING_BINDIR_NATIVE']
  84. pkg_deploy = os.path.join(deploy_dir_rpm, package_arch, '.'.join((pf, package_arch, 'rpm')))
  85. # Use a temporary rpmdb
  86. rpmdb = tempfile.mkdtemp(prefix='oeqa-rpmdb')
  87. runCmd('%s/rpmkeys --define "_dbpath %s" --import %s' %
  88. (staging_bindir_native, rpmdb, self.pub_key_path))
  89. ret = runCmd('%s/rpmkeys --define "_dbpath %s" --checksig %s' %
  90. (staging_bindir_native, rpmdb, pkg_deploy))
  91. # tmp/deploy/rpm/i586/ed-1.9-r0.i586.rpm: rsa sha1 md5 OK
  92. self.assertIn('digests signatures OK', ret.output, 'Package signed incorrectly.')
  93. shutil.rmtree(rpmdb)
  94. #Check that an image can be built from signed packages
  95. self.add_command_to_tearDown('bitbake -c clean core-image-minimal')
  96. bitbake('-c clean core-image-minimal')
  97. bitbake('core-image-minimal')
  98. def test_signing_sstate_archive(self):
  99. """
  100. Summary: Test that sstate archives can be signed
  101. Expected: Package should be signed with the correct key
  102. Product: oe-core
  103. Author: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
  104. AutomatedBy: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
  105. """
  106. test_recipe = 'ed'
  107. # Since we need gpg but we can't use gpg-native for sstate signatures, we
  108. # build gpg-native in our original builddir then run the tests in a second one.
  109. builddir = os.environ.get('BUILDDIR') + "-testsign"
  110. sstatedir = os.path.join(builddir, 'test-sstate')
  111. nsysroot = self.setup_gpg()
  112. feature = 'SSTATE_SIG_KEY ?= "testuser"\n'
  113. feature += 'SSTATE_SIG_PASSPHRASE ?= "test123"\n'
  114. feature += 'SSTATE_VERIFY_SIG ?= "1"\n'
  115. feature += 'GPG_PATH = "%s"\n' % self.gpg_dir
  116. feature += 'SSTATE_DIR = "%s"\n' % sstatedir
  117. # Any mirror might have partial sstate without .sig files, triggering failures
  118. feature += 'SSTATE_MIRRORS:forcevariable = ""\n'
  119. self.write_config(feature)
  120. with self.create_new_builddir(os.environ['BUILDDIR'], builddir):
  121. os.environ["PATH"] = nsysroot + ":" + os.environ["PATH"]
  122. self.add_command_to_tearDown('bitbake -c clean %s' % test_recipe)
  123. self.add_command_to_tearDown('rm -rf %s' % sstatedir)
  124. self.add_command_to_tearDown('rm -rf %s' % builddir)
  125. bitbake('-c clean %s' % test_recipe)
  126. bitbake('-c populate_lic %s' % test_recipe)
  127. recipe_sig = glob.glob(sstatedir + '/*/*/*:ed:*_populate_lic.tar.zst.sig')
  128. recipe_archive = glob.glob(sstatedir + '/*/*/*:ed:*_populate_lic.tar.zst')
  129. self.assertEqual(len(recipe_sig), 1, 'Failed to find .sig file.')
  130. self.assertEqual(len(recipe_archive), 1, 'Failed to find .tar.zst file.')
  131. ret = runCmd('gpg --homedir %s --verify %s %s' % (self.gpg_dir, recipe_sig[0], recipe_archive[0]))
  132. # gpg: Signature made Thu 22 Oct 2015 01:45:09 PM EEST using RSA key ID 61EEFB30
  133. # gpg: Good signature from "testuser (nocomment) <testuser@email.com>"
  134. self.assertIn('gpg: Good signature from', ret.output, 'Package signed incorrectly.')
  135. class LockedSignatures(OESelftestTestCase):
  136. def test_locked_signatures(self):
  137. """
  138. Summary: Test locked signature mechanism
  139. Expected: Locked signatures will prevent task to run
  140. Product: oe-core
  141. Author: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
  142. AutomatedBy: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
  143. """
  144. import uuid
  145. test_recipe = 'ed'
  146. locked_sigs_file = 'locked-sigs.inc'
  147. bitbake(test_recipe)
  148. # Generate locked sigs include file
  149. bitbake('-S lockedsigs %s' % test_recipe)
  150. feature = 'require %s\n' % locked_sigs_file
  151. feature += 'SIGGEN_LOCKEDSIGS_TASKSIG_CHECK = "warn"\n'
  152. self.write_config(feature)
  153. # Build a locked recipe
  154. bitbake(test_recipe)
  155. templayerdir = tempfile.mkdtemp(prefix='signingqa')
  156. create_temp_layer(templayerdir, 'selftestsigning')
  157. runCmd('bitbake-layers add-layer %s' % templayerdir)
  158. # Make a change that should cause the locked task signature to change
  159. # Use uuid so hash equivalance server isn't triggered
  160. recipe_append_file = test_recipe + '_' + get_bb_var('PV', test_recipe) + '.bbappend'
  161. recipe_append_path = os.path.join(templayerdir, 'recipes-test', test_recipe, recipe_append_file)
  162. feature = 'SUMMARY:${PN} = "test locked signature%s"\n' % uuid.uuid4()
  163. os.mkdir(os.path.join(templayerdir, 'recipes-test'))
  164. os.mkdir(os.path.join(templayerdir, 'recipes-test', test_recipe))
  165. write_file(recipe_append_path, feature)
  166. self.add_command_to_tearDown('bitbake-layers remove-layer %s' % templayerdir)
  167. self.add_command_to_tearDown('rm -f %s' % os.path.join(self.builddir, locked_sigs_file))
  168. self.add_command_to_tearDown('rm -rf %s' % templayerdir)
  169. # Build the recipe again
  170. ret = bitbake(test_recipe)
  171. # Verify you get the warning and that the real task *isn't* run (i.e. the locked signature has worked)
  172. patt = r'The %s:do_package sig is computed to be \S+, but the sig is locked to \S+ in SIGGEN_LOCKEDSIGS\S+' % test_recipe
  173. found_warn = re.search(patt, ret.output)
  174. self.assertIsNotNone(found_warn, "Didn't find the expected warning message. Output: %s" % ret.output)