blob: 9c710bd0fffd190be4768b7198368ef7b953f3ab [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001#
2# SPDX-License-Identifier: MIT
3#
4
Brad Bishopd7bf8c12018-02-25 22:55:05 -05005from oeqa.selftest.case import OESelftestTestCase
6from oeqa.utils.commands import runCmd, bitbake, get_bb_var, get_bb_vars
7import os
Brad Bishopf86d0552018-12-04 14:18:15 -08008import oe
Brad Bishopd7bf8c12018-02-25 22:55:05 -05009import glob
10import re
11import shutil
12import tempfile
Brad Bishopf86d0552018-12-04 14:18:15 -080013from contextlib import contextmanager
Brad Bishopd7bf8c12018-02-25 22:55:05 -050014from oeqa.utils.ftools import write_file
15
16
17class Signing(OESelftestTestCase):
18
19 gpg_dir = ""
20 pub_key_path = ""
21 secret_key_path = ""
22
Brad Bishopf86d0552018-12-04 14:18:15 -080023 def setup_gpg(self):
24 bitbake('gnupg-native -c addto_recipe_sysroot')
Brad Bishopd7bf8c12018-02-25 22:55:05 -050025
Brad Bishopf86d0552018-12-04 14:18:15 -080026 self.gpg_dir = tempfile.mkdtemp(prefix="oeqa-signing-")
27 self.track_for_cleanup(self.gpg_dir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050028
Brad Bishopf86d0552018-12-04 14:18:15 -080029 self.pub_key_path = os.path.join(self.testlayer_path, 'files', 'signing', "key.pub")
30 self.secret_key_path = os.path.join(self.testlayer_path, 'files', 'signing', "key.secret")
Brad Bishopd7bf8c12018-02-25 22:55:05 -050031
Brad Bishopf86d0552018-12-04 14:18:15 -080032 nsysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "gnupg-native")
33 runCmd('gpg --batch --homedir %s --import %s %s' % (self.gpg_dir, self.pub_key_path, self.secret_key_path), native_sysroot=nsysroot)
34 return nsysroot + get_bb_var("bindir_native")
Brad Bishopd7bf8c12018-02-25 22:55:05 -050035
Brad Bishopf86d0552018-12-04 14:18:15 -080036
37 @contextmanager
38 def create_new_builddir(self, builddir, newbuilddir):
39 bb.utils.mkdirhier(newbuilddir)
40 oe.path.copytree(builddir + "/conf", newbuilddir + "/conf")
41 oe.path.copytree(builddir + "/cache", newbuilddir + "/cache")
42
43 origenv = os.environ.copy()
44
45 for e in os.environ:
46 if builddir in os.environ[e]:
47 os.environ[e] = os.environ[e].replace(builddir, newbuilddir)
48
49 os.chdir(newbuilddir)
50 try:
51 yield
52 finally:
53 for e in origenv:
54 os.environ[e] = origenv[e]
55 os.chdir(builddir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050056
Brad Bishopd7bf8c12018-02-25 22:55:05 -050057 def test_signing_packages(self):
58 """
59 Summary: Test that packages can be signed in the package feed
60 Expected: Package should be signed with the correct key
61 Expected: Images can be created from signed packages
62 Product: oe-core
63 Author: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080064 Author: Alexander Kanavin <alex.kanavin@gmail.com>
Brad Bishopd7bf8c12018-02-25 22:55:05 -050065 AutomatedBy: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
66 """
67 import oe.packagedata
68
Brad Bishopf86d0552018-12-04 14:18:15 -080069 self.setup_gpg()
70
Brad Bishopd7bf8c12018-02-25 22:55:05 -050071 package_classes = get_bb_var('PACKAGE_CLASSES')
72 if 'package_rpm' not in package_classes:
73 self.skipTest('This test requires RPM Packaging.')
74
75 test_recipe = 'ed'
76
77 feature = 'INHERIT += "sign_rpm"\n'
78 feature += 'RPM_GPG_PASSPHRASE = "test123"\n'
79 feature += 'RPM_GPG_NAME = "testuser"\n'
80 feature += 'GPG_PATH = "%s"\n' % self.gpg_dir
81
82 self.write_config(feature)
83
84 bitbake('-c clean %s' % test_recipe)
85 bitbake('-f -c package_write_rpm %s' % test_recipe)
86
87 self.add_command_to_tearDown('bitbake -c clean %s' % test_recipe)
88
89 needed_vars = ['PKGDATA_DIR', 'DEPLOY_DIR_RPM', 'PACKAGE_ARCH', 'STAGING_BINDIR_NATIVE']
90 bb_vars = get_bb_vars(needed_vars, test_recipe)
91 pkgdatadir = bb_vars['PKGDATA_DIR']
92 pkgdata = oe.packagedata.read_pkgdatafile(pkgdatadir + "/runtime/ed")
93 if 'PKGE' in pkgdata:
94 pf = pkgdata['PN'] + "-" + pkgdata['PKGE'] + pkgdata['PKGV'] + '-' + pkgdata['PKGR']
95 else:
96 pf = pkgdata['PN'] + "-" + pkgdata['PKGV'] + '-' + pkgdata['PKGR']
97 deploy_dir_rpm = bb_vars['DEPLOY_DIR_RPM']
98 package_arch = bb_vars['PACKAGE_ARCH'].replace('-', '_')
99 staging_bindir_native = bb_vars['STAGING_BINDIR_NATIVE']
100
101 pkg_deploy = os.path.join(deploy_dir_rpm, package_arch, '.'.join((pf, package_arch, 'rpm')))
102
103 # Use a temporary rpmdb
104 rpmdb = tempfile.mkdtemp(prefix='oeqa-rpmdb')
105
106 runCmd('%s/rpmkeys --define "_dbpath %s" --import %s' %
107 (staging_bindir_native, rpmdb, self.pub_key_path))
108
109 ret = runCmd('%s/rpmkeys --define "_dbpath %s" --checksig %s' %
110 (staging_bindir_native, rpmdb, pkg_deploy))
111 # tmp/deploy/rpm/i586/ed-1.9-r0.i586.rpm: rsa sha1 md5 OK
Brad Bishop316dfdd2018-06-25 12:45:53 -0400112 self.assertIn('digests signatures OK', ret.output, 'Package signed incorrectly.')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500113 shutil.rmtree(rpmdb)
114
115 #Check that an image can be built from signed packages
116 self.add_command_to_tearDown('bitbake -c clean core-image-minimal')
117 bitbake('-c clean core-image-minimal')
118 bitbake('core-image-minimal')
119
120
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500121 def test_signing_sstate_archive(self):
122 """
123 Summary: Test that sstate archives can be signed
124 Expected: Package should be signed with the correct key
125 Product: oe-core
126 Author: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
127 AutomatedBy: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
128 """
129
130 test_recipe = 'ed'
131
Brad Bishopf86d0552018-12-04 14:18:15 -0800132 # Since we need gpg but we can't use gpg-native for sstate signatures, we
133 # build gpg-native in our original builddir then run the tests in a second one.
134 builddir = os.environ.get('BUILDDIR') + "-testsign"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500135 sstatedir = os.path.join(builddir, 'test-sstate')
136
Brad Bishopf86d0552018-12-04 14:18:15 -0800137 nsysroot = self.setup_gpg()
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500138
139 feature = 'SSTATE_SIG_KEY ?= "testuser"\n'
140 feature += 'SSTATE_SIG_PASSPHRASE ?= "test123"\n'
141 feature += 'SSTATE_VERIFY_SIG ?= "1"\n'
142 feature += 'GPG_PATH = "%s"\n' % self.gpg_dir
143 feature += 'SSTATE_DIR = "%s"\n' % sstatedir
144 # Any mirror might have partial sstate without .sig files, triggering failures
145 feature += 'SSTATE_MIRRORS_forcevariable = ""\n'
146
147 self.write_config(feature)
148
Brad Bishopf86d0552018-12-04 14:18:15 -0800149 with self.create_new_builddir(os.environ['BUILDDIR'], builddir):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500150
Brad Bishopf86d0552018-12-04 14:18:15 -0800151 os.environ["PATH"] = nsysroot + ":" + os.environ["PATH"]
152 self.add_command_to_tearDown('bitbake -c clean %s' % test_recipe)
153 self.add_command_to_tearDown('rm -rf %s' % sstatedir)
154 self.add_command_to_tearDown('rm -rf %s' % builddir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500155
Brad Bishopf86d0552018-12-04 14:18:15 -0800156 bitbake('-c clean %s' % test_recipe)
157 bitbake('-c populate_lic %s' % test_recipe)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500158
Brad Bishopf86d0552018-12-04 14:18:15 -0800159 recipe_sig = glob.glob(sstatedir + '/*/*:ed:*_populate_lic.tgz.sig')
160 recipe_tgz = glob.glob(sstatedir + '/*/*:ed:*_populate_lic.tgz')
161
162 self.assertEqual(len(recipe_sig), 1, 'Failed to find .sig file.')
163 self.assertEqual(len(recipe_tgz), 1, 'Failed to find .tgz file.')
164
165 ret = runCmd('gpg --homedir %s --verify %s %s' % (self.gpg_dir, recipe_sig[0], recipe_tgz[0]))
166 # gpg: Signature made Thu 22 Oct 2015 01:45:09 PM EEST using RSA key ID 61EEFB30
167 # gpg: Good signature from "testuser (nocomment) <testuser@email.com>"
168 self.assertIn('gpg: Good signature from', ret.output, 'Package signed incorrectly.')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500169
170
171class LockedSignatures(OESelftestTestCase):
172
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500173 def test_locked_signatures(self):
174 """
175 Summary: Test locked signature mechanism
176 Expected: Locked signatures will prevent task to run
177 Product: oe-core
178 Author: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
179 AutomatedBy: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
180 """
181
182 test_recipe = 'ed'
183 locked_sigs_file = 'locked-sigs.inc'
184
185 self.add_command_to_tearDown('rm -f %s' % os.path.join(self.builddir, locked_sigs_file))
186
187 bitbake(test_recipe)
188 # Generate locked sigs include file
189 bitbake('-S none %s' % test_recipe)
190
191 feature = 'require %s\n' % locked_sigs_file
192 feature += 'SIGGEN_LOCKEDSIGS_TASKSIG_CHECK = "warn"\n'
193 self.write_config(feature)
194
195 # Build a locked recipe
196 bitbake(test_recipe)
197
198 # Make a change that should cause the locked task signature to change
199 recipe_append_file = test_recipe + '_' + get_bb_var('PV', test_recipe) + '.bbappend'
200 recipe_append_path = os.path.join(self.testlayer_path, 'recipes-test', test_recipe, recipe_append_file)
201 feature = 'SUMMARY += "test locked signature"\n'
202
203 os.mkdir(os.path.join(self.testlayer_path, 'recipes-test', test_recipe))
204 write_file(recipe_append_path, feature)
205
206 self.add_command_to_tearDown('rm -rf %s' % os.path.join(self.testlayer_path, 'recipes-test', test_recipe))
207
208 # Build the recipe again
209 ret = bitbake(test_recipe)
210
211 # Verify you get the warning and that the real task *isn't* run (i.e. the locked signature has worked)
212 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
213 found_warn = re.search(patt, ret.output)
214
215 self.assertIsNotNone(found_warn, "Didn't find the expected warning message. Output: %s" % ret.output)