signing.py 8.5 KB

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