dom0_update.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # The Qubes OS Project, http://www.qubes-os.org
  5. #
  6. # Copyright (C) 2015 Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU General Public License
  10. # as published by the Free Software Foundation; either version 2
  11. # of the License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  21. # USA.
  22. #
  23. import os
  24. import shutil
  25. import subprocess
  26. import tempfile
  27. import unittest
  28. from qubes.qubes import QubesVmCollection
  29. import qubes.qubes
  30. VM_PREFIX = "test-"
  31. @unittest.skipUnless(os.path.exists('/usr/bin/rpmsign') and
  32. os.path.exists('/usr/bin/rpmbuild'),
  33. 'rpm-sign and/or rpm-build not installed')
  34. class TC_00_Dom0Upgrade(qubes.tests.QubesTestCase):
  35. cleanup_paths = []
  36. pkg_name = 'qubes-test-pkg'
  37. dom0_update_common_opts = ['--disablerepo=*', '--enablerepo=test',
  38. '--setopt=test.copy_local=1']
  39. @classmethod
  40. def generate_key(cls, keydir):
  41. gpg_opts = ['gpg', '--quiet', '--no-default-keyring',
  42. '--homedir', keydir]
  43. p = subprocess.Popen(gpg_opts + ['--gen-key', '--batch'],
  44. stdin=subprocess.PIPE,
  45. stderr=open(os.devnull, 'w'))
  46. p.stdin.write('''
  47. Key-Type: RSA
  48. Key-Length: 1024
  49. Key-Usage: sign
  50. Name-Real: Qubes test
  51. Expire-Date: 0
  52. %commit
  53. '''.format(keydir=keydir))
  54. p.stdin.close()
  55. p.wait()
  56. subprocess.check_call(gpg_opts + ['-a', '--export',
  57. '--output', os.path.join(keydir, 'pubkey.asc')])
  58. p = subprocess.Popen(gpg_opts + ['--with-colons', '--list-keys'],
  59. stdout=subprocess.PIPE)
  60. for line in p.stdout.readlines():
  61. fields = line.split(':')
  62. if fields[0] == 'pub':
  63. return fields[4][-8:].lower()
  64. raise RuntimeError
  65. @classmethod
  66. def setUpClass(cls):
  67. super(TC_00_Dom0Upgrade, cls).setUpClass()
  68. cls.tmpdir = tempfile.mkdtemp()
  69. cls.cleanup_paths += [cls.tmpdir]
  70. cls.keyid = cls.generate_key(cls.tmpdir)
  71. p = subprocess.Popen(['sudo', 'dd',
  72. 'status=none', 'of=/etc/yum.repos.d/test.repo'],
  73. stdin=subprocess.PIPE)
  74. p.stdin.write('''
  75. [test]
  76. name = Test
  77. baseurl = file:///tmp/repo
  78. enabled = 1
  79. ''')
  80. p.stdin.close()
  81. p.wait()
  82. @classmethod
  83. def tearDownClass(cls):
  84. subprocess.check_call(['sudo', 'rm', '-f',
  85. '/etc/yum.repos.d/test.repo'])
  86. for dir in cls.cleanup_paths:
  87. shutil.rmtree(dir)
  88. cls.cleanup_paths = []
  89. def setUp(self):
  90. self.qc = QubesVmCollection()
  91. self.qc.lock_db_for_writing()
  92. self.qc.load()
  93. self.updatevm = self.qc.add_new_vm("QubesProxyVm",
  94. name="%supdatevm" % VM_PREFIX,
  95. template=self.qc.get_default_template())
  96. self.updatevm.create_on_disk(verbose=False)
  97. self.saved_updatevm = self.qc.get_updatevm_vm()
  98. self.qc.set_updatevm_vm(self.updatevm)
  99. self.qc.save()
  100. self.qc.unlock_db()
  101. subprocess.call(['sudo', 'rpm', '-e', self.pkg_name],
  102. stderr=open(os.devnull, 'w'))
  103. subprocess.check_call(['sudo', 'rpm', '--import',
  104. os.path.join(self.tmpdir, 'pubkey.asc')])
  105. self.updatevm.start()
  106. def remove_vms(self, vms):
  107. self.qc.lock_db_for_writing()
  108. self.qc.load()
  109. self.qc.set_updatevm_vm(self.qc[self.saved_updatevm.qid])
  110. for vm in vms:
  111. if isinstance(vm, str):
  112. vm = self.qc.get_vm_by_name(vm)
  113. else:
  114. vm = self.qc[vm.qid]
  115. if vm.is_running():
  116. try:
  117. vm.force_shutdown()
  118. except:
  119. pass
  120. try:
  121. vm.remove_from_disk()
  122. except OSError:
  123. pass
  124. self.qc.pop(vm.qid)
  125. self.qc.save()
  126. self.qc.unlock_db()
  127. def tearDown(self):
  128. vmlist = [vm for vm in self.qc.values() if vm.name.startswith(
  129. VM_PREFIX)]
  130. self.remove_vms(vmlist)
  131. subprocess.call(['sudo', 'rpm', '-e', self.pkg_name], stderr=open(
  132. os.devnull, 'w'))
  133. subprocess.call(['sudo', 'rpm', '-e', 'gpg-pubkey-{}'.format(
  134. self.keyid)], stderr=open(os.devnull, 'w'))
  135. for pkg in os.listdir(self.tmpdir):
  136. if pkg.endswith('.rpm'):
  137. os.unlink(pkg)
  138. def create_pkg(self, dir, name, version):
  139. spec_path = os.path.join(dir, name+'.spec')
  140. spec = open(spec_path, 'w')
  141. spec.write(
  142. '''
  143. Name: {name}
  144. Summary: Test Package
  145. Version: {version}
  146. Release: 1
  147. Vendor: Invisible Things Lab
  148. License: GPL
  149. Group: Qubes
  150. URL: http://www.qubes-os.org
  151. %description
  152. Test package
  153. %install
  154. %files
  155. '''.format(name=name, version=version)
  156. )
  157. spec.close()
  158. subprocess.check_call(
  159. ['rpmbuild', '--quiet', '-bb', '--define', '_rpmdir {}'.format(dir),
  160. spec_path])
  161. pkg_path = os.path.join(dir, 'x86_64',
  162. '{}-{}-1.x86_64.rpm'.format(name, version))
  163. subprocess.check_call(['sudo', 'chmod', 'go-rw', '/dev/tty'])
  164. subprocess.check_call(
  165. ['rpm', '--quiet', '--define=_gpg_path {}'.format(dir),
  166. '--define=_gpg_name {}'.format("Qubes test"),
  167. '--addsign', pkg_path],
  168. stdin=open(os.devnull),
  169. stdout=open(os.devnull, 'w'),
  170. stderr=subprocess.STDOUT)
  171. subprocess.check_call(['sudo', 'chmod', 'go+rw', '/dev/tty'])
  172. return pkg_path
  173. def send_pkg(self, filename):
  174. p = self.updatevm.run('mkdir -p /tmp/repo; cat > /tmp/repo/{}'.format(
  175. os.path.basename(
  176. filename)), passio_popen=True)
  177. p.stdin.write(open(filename).read())
  178. p.stdin.close()
  179. p.wait()
  180. self.updatevm.run('cd /tmp/repo; createrepo .', wait=True)
  181. def test_000_update(self):
  182. filename = self.create_pkg(self.tmpdir, self.pkg_name, '1.0')
  183. subprocess.check_call(['sudo', 'rpm', '-i', filename])
  184. filename = self.create_pkg(self.tmpdir, self.pkg_name, '2.0')
  185. self.send_pkg(filename)
  186. logpath = os.path.join(self.tmpdir, 'dom0-update-output.txt')
  187. try:
  188. subprocess.check_call(['sudo', 'qubes-dom0-update', '-y'] +
  189. self.dom0_update_common_opts,
  190. stdout=open(logpath, 'w'),
  191. stderr=subprocess.STDOUT)
  192. except subprocess.CalledProcessError:
  193. self.fail("qubes-dom0-update failed: " + open(
  194. logpath).read())
  195. retcode = subprocess.call(['rpm', '-q', '{}-1.0'.format(
  196. self.pkg_name)], stdout=open(os.devnull, 'w'))
  197. self.assertEqual(retcode, 1, 'Package {}-1.0 still installed after '
  198. 'update'.format(self.pkg_name))
  199. retcode = subprocess.call(['rpm', '-q', '{}-2.0'.format(
  200. self.pkg_name)], stdout=open(os.devnull, 'w'))
  201. self.assertEqual(retcode, 0, 'Package {}-2.0 not installed after '
  202. 'update'.format(self.pkg_name))
  203. def test_010_instal(self):
  204. filename = self.create_pkg(self.tmpdir, self.pkg_name, '1.0')
  205. self.send_pkg(filename)
  206. logpath = os.path.join(self.tmpdir, 'dom0-update-output.txt')
  207. try:
  208. subprocess.check_call(['sudo', 'qubes-dom0-update', '-y'] +
  209. self.dom0_update_common_opts + [
  210. self.pkg_name],
  211. stdout=open(logpath, 'w'),
  212. stderr=subprocess.STDOUT)
  213. except subprocess.CalledProcessError:
  214. self.fail("qubes-dom0-update failed: " + open(
  215. logpath).read())
  216. retcode = subprocess.call(['rpm', '-q', '{}-1.0'.format(
  217. self.pkg_name)], stdout=open('/dev/null', 'w'))
  218. self.assertEqual(retcode, 0, 'Package {}-1.0 not installed'.format(
  219. self.pkg_name))
  220. def test_020_install_wrong_sign(self):
  221. subprocess.call(['sudo', 'rpm', '-e', 'gpg-pubkey-{}'.format(
  222. self.keyid)])
  223. filename = self.create_pkg(self.tmpdir, self.pkg_name, '1.0')
  224. self.send_pkg(filename)
  225. logpath = os.path.join(self.tmpdir, 'dom0-update-output.txt')
  226. try:
  227. subprocess.check_call(['sudo', 'qubes-dom0-update', '-y'] +
  228. self.dom0_update_common_opts + [
  229. self.pkg_name],
  230. stdout=open(logpath, 'w'),
  231. stderr=subprocess.STDOUT)
  232. self.fail("qubes-dom0-update unexpectedly succeeded: " + open(
  233. logpath).read())
  234. except subprocess.CalledProcessError:
  235. pass
  236. retcode = subprocess.call(['rpm', '-q', '{}-1.0'.format(
  237. self.pkg_name)], stdout=open('/dev/null', 'w'))
  238. self.assertEqual(retcode, 1,
  239. 'Package {}-1.0 installed although '
  240. 'signature is invalid'.format(self.pkg_name))
  241. def test_030_install_unsigned(self):
  242. filename = self.create_pkg(self.tmpdir, self.pkg_name, '1.0')
  243. subprocess.check_call(['rpm', '--delsign', filename],
  244. stdout=open(os.devnull, 'w'),
  245. stderr=subprocess.STDOUT)
  246. self.send_pkg(filename)
  247. logpath = os.path.join(self.tmpdir, 'dom0-update-output.txt')
  248. try:
  249. subprocess.check_call(['sudo', 'qubes-dom0-update', '-y'] +
  250. self.dom0_update_common_opts +
  251. [self.pkg_name],
  252. stdout=open(logpath, 'w'),
  253. stderr=subprocess.STDOUT
  254. )
  255. self.fail("qubes-dom0-update unexpectedly succeeded: " + open(
  256. logpath).read())
  257. except subprocess.CalledProcessError:
  258. pass
  259. retcode = subprocess.call(['rpm', '-q', '{}-1.0'.format(
  260. self.pkg_name)], stdout=open('/dev/null', 'w'))
  261. self.assertEqual(retcode, 1,
  262. 'UNSIGNED package {}-1.0 installed'.format(self.pkg_name))