signing.py 7.5 KB

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