qvm_firewall.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. # encoding=utf-8
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2016 Marek Marczykowski-Górecki
  6. # <marmarek@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU Lesser General Public License as published by
  10. # the Free Software Foundation; either version 2.1 of the License, or
  11. # (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 Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License along
  19. # with this program; if not, write to the Free Software Foundation, Inc.,
  20. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. #
  22. import argparse
  23. import datetime
  24. import re
  25. import time
  26. import qubesadmin.firewall
  27. import qubesadmin.tests
  28. import qubesadmin.tests.tools
  29. import qubesadmin.tools.qvm_firewall
  30. class TC_00_RuleAction(qubesadmin.tests.QubesTestCase):
  31. def setUp(self):
  32. super(TC_00_RuleAction, self).setUp()
  33. self.action = qubesadmin.tools.qvm_firewall.RuleAction(
  34. None, dest='rule')
  35. def test_000_named_opts(self):
  36. ns = argparse.Namespace()
  37. self.action(None, ns, ['dsthost=127.0.0.1', 'action=accept'])
  38. self.assertEqual(ns.rule,
  39. qubesadmin.firewall.Rule(
  40. None, action='accept', dsthost='127.0.0.1/32'))
  41. def test_001_unnamed_opts(self):
  42. ns = argparse.Namespace()
  43. self.action(None, ns, ['accept', '127.0.0.1', 'tcp', '80'])
  44. self.assertEqual(ns.rule,
  45. qubesadmin.firewall.Rule(
  46. None, action='accept', dsthost='127.0.0.1/32',
  47. proto='tcp', dstports=80))
  48. def test_002_unnamed_opts(self):
  49. ns = argparse.Namespace()
  50. self.action(None, ns, ['accept', '127.0.0.1', 'icmp', '8'])
  51. self.assertEqual(ns.rule,
  52. qubesadmin.firewall.Rule(
  53. None, action='accept', dsthost='127.0.0.1/32',
  54. proto='icmp', icmptype=8))
  55. def test_003_mixed_opts(self):
  56. ns = argparse.Namespace()
  57. self.action(None, ns, ['dsthost=127.0.0.1', 'accept',
  58. 'dstports=443', 'tcp'])
  59. self.assertEqual(ns.rule,
  60. qubesadmin.firewall.Rule(
  61. None, action='accept', dsthost='127.0.0.1/32',
  62. proto='tcp', dstports=443))
  63. def test_004_expire_absolute(self):
  64. ns = argparse.Namespace()
  65. self.action(None, ns, ['dsthost=127.0.0.1', 'action=accept',
  66. 'expire=1525054180'])
  67. self.assertEqual(ns.rule,
  68. qubesadmin.firewall.Rule(
  69. None, action='accept', dsthost='127.0.0.1/32',
  70. expire=1525054180))
  71. def test_005_expire_relative(self):
  72. ns = argparse.Namespace()
  73. now = int(datetime.datetime.now().strftime('%s'))
  74. self.action(None, ns, ['dsthost=127.0.0.1', 'action=accept',
  75. 'expire=+100'])
  76. self.assertEqual(ns.rule,
  77. qubesadmin.firewall.Rule(
  78. None, action='accept', dsthost='127.0.0.1/32',
  79. expire=now+100))
  80. def test_006_dsthost_aliases(self):
  81. ns = argparse.Namespace()
  82. for name in ['dsthost', 'dst4', 'dst6']:
  83. self.action(None, ns, [name + '=127.0.0.1', 'accept'])
  84. self.assertEqual(ns.rule,
  85. qubesadmin.firewall.Rule(
  86. None, action='accept', dsthost='127.0.0.1/32'))
  87. def test_007_none_errors(self):
  88. ns = argparse.Namespace()
  89. with self.assertRaises(argparse.ArgumentError):
  90. self.action(None, ns, ['dsthost=', 'action=accept'])
  91. with self.assertRaises(argparse.ArgumentError):
  92. self.action(None, ns, ['dsthost=127.0.0.1', 'dstports=',
  93. 'action=accept'])
  94. with self.assertRaises(argparse.ArgumentError):
  95. self.action(None, ns, ['dsthost=127.0.0.1', 'icmptype=',
  96. 'action=accept'])
  97. class TC_10_qvm_firewall(qubesadmin.tests.QubesTestCase):
  98. def setUp(self):
  99. super(TC_10_qvm_firewall, self).setUp()
  100. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  101. b'0\0test-vm class=AppVM state=Halted\n'
  102. def test_000_list(self):
  103. self.app.expected_calls[('test-vm', 'admin.vm.firewall.Get',
  104. None, None)] = \
  105. b'0\0action=accept dsthost=qubes-os.org\n' \
  106. b'action=drop proto=icmp\n'
  107. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  108. qubesadmin.tools.qvm_firewall.main(['test-vm', 'list'], app=self.app)
  109. self.assertEqual(
  110. [l.strip() for l in stdout.getvalue().splitlines()],
  111. ['NO ACTION HOST PROTOCOL PORT(S) SPECIAL '
  112. 'TARGET ICMP TYPE EXPIRE COMMENT',
  113. '0 accept qubes-os.org - - - '
  114. ' - - -',
  115. '1 drop - icmp - - '
  116. ' - - -',
  117. ])
  118. def test_001_list_default(self):
  119. self.app.expected_calls[('test-vm', 'admin.vm.firewall.Get',
  120. None, None)] = \
  121. b'0\0action=accept dsthost=qubes-os.org proto=tcp ' \
  122. b'dstports=443-443\n' \
  123. b'action=drop proto=icmp icmptype=8\n' \
  124. b'action=accept specialtarget=dns comment=Allow DNS\n'
  125. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  126. qubesadmin.tools.qvm_firewall.main(['test-vm'], app=self.app)
  127. self.assertEqual(
  128. [l.strip() for l in stdout.getvalue().splitlines()],
  129. ['NO ACTION HOST PROTOCOL PORT(S) SPECIAL '
  130. 'TARGET ICMP TYPE EXPIRE COMMENT',
  131. '0 accept qubes-os.org tcp 443 - '
  132. ' - - -',
  133. '1 drop - icmp - - '
  134. ' 8 - -',
  135. '2 accept - - - dns '
  136. ' - - Allow DNS',
  137. ])
  138. def test_002_list_expire(self):
  139. in_1h = int(time.time()) + 3600
  140. self.app.expected_calls[('test-vm', 'admin.vm.firewall.Get',
  141. None, None)] = \
  142. '0\0action=accept dsthost=qubes-os.org proto=tcp ' \
  143. 'dstports=443-443 expire={}\n'.format(in_1h).encode()
  144. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  145. qubesadmin.tools.qvm_firewall.main(['test-vm'], app=self.app)
  146. line = stdout.getvalue().splitlines()[-1]
  147. match = re.match(
  148. '0 accept qubes-os.org tcp 443 - '
  149. ' - \+(.{4})s -',
  150. line)
  151. self.assertTrue(match, "no match for: {!r}".format(line))
  152. duration = int(match.group(1))
  153. self.assertTrue(3590 < duration <= 3600)
  154. def test_002_list_raw(self):
  155. self.app.expected_calls[('test-vm', 'admin.vm.firewall.Get',
  156. None, None)] = \
  157. b'0\0action=accept dsthost=qubes-os.org\n' \
  158. b'action=drop proto=icmp\n'
  159. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  160. qubesadmin.tools.qvm_firewall.main(['test-vm', '--raw', 'list'],
  161. app=self.app)
  162. self.assertEqual(
  163. [l.strip() for l in stdout.getvalue().splitlines()],
  164. ['action=accept dsthost=qubes-os.org',
  165. 'action=drop proto=icmp',
  166. ])
  167. def test_003_list_raw_reload(self):
  168. self.app.expected_calls[('test-vm', 'admin.vm.firewall.Get',
  169. None, None)] = \
  170. b'0\0action=accept dsthost=qubes-os.org\n' \
  171. b'action=drop proto=icmp\n'
  172. self.app.expected_calls[('test-vm', 'admin.vm.firewall.Reload',
  173. None, None)] = b'0\0'
  174. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  175. qubesadmin.tools.qvm_firewall.main(
  176. ['test-vm', '--raw', '--reload', 'list'],
  177. app=self.app)
  178. self.assertEqual(
  179. [l.strip() for l in stdout.getvalue().splitlines()],
  180. ['action=accept dsthost=qubes-os.org',
  181. 'action=drop proto=icmp',
  182. ])
  183. def test_010_add_after(self):
  184. self.app.expected_calls[('test-vm', 'admin.vm.firewall.Get',
  185. None, None)] = \
  186. b'0\0action=accept dsthost=qubes-os.org\n' \
  187. b'action=drop proto=icmp\n'
  188. self.app.expected_calls[('test-vm', 'admin.vm.firewall.Set', None,
  189. b'action=accept dsthost=qubes-os.org\n'
  190. b'action=drop proto=icmp\n'
  191. b'action=accept dst4=192.168.0.0/24 comment=Allow LAN\n')] = \
  192. b'0\0'
  193. qubesadmin.tools.qvm_firewall.main(
  194. ['test-vm', 'add', 'accept', '192.168.0.0/24', 'comment=Allow LAN'],
  195. app=self.app
  196. )
  197. def test_011_add_before(self):
  198. self.app.expected_calls[('test-vm', 'admin.vm.firewall.Get',
  199. None, None)] = \
  200. b'0\0action=accept dsthost=qubes-os.org\n' \
  201. b'action=drop proto=icmp\n'
  202. self.app.expected_calls[('test-vm', 'admin.vm.firewall.Set', None,
  203. b'action=accept dsthost=qubes-os.org\n'
  204. b'action=accept dst4=192.168.0.0/24 comment=Allow LAN\n'
  205. b'action=drop proto=icmp\n')] = b'0\0'
  206. qubesadmin.tools.qvm_firewall.main(
  207. ['test-vm', 'add', '--before', '1', 'accept', '192.168.0.0/24',
  208. 'comment=Allow LAN'],
  209. app=self.app
  210. )
  211. def test_020_del_number(self):
  212. self.app.expected_calls[('test-vm', 'admin.vm.firewall.Get',
  213. None, None)] = \
  214. b'0\0action=accept dsthost=qubes-os.org\n' \
  215. b'action=drop proto=icmp\n'
  216. self.app.expected_calls[('test-vm', 'admin.vm.firewall.Set', None,
  217. b'action=accept dsthost=qubes-os.org\n')] = b'0\0'
  218. qubesadmin.tools.qvm_firewall.main(
  219. ['test-vm', 'del', '--rule-no', '1'],
  220. app=self.app
  221. )
  222. def test_021_del_rule(self):
  223. self.app.expected_calls[('test-vm', 'admin.vm.firewall.Get',
  224. None, None)] = \
  225. b'0\0action=accept dsthost=qubes-os.org\n' \
  226. b'action=drop proto=icmp\n'
  227. self.app.expected_calls[('test-vm', 'admin.vm.firewall.Set', None,
  228. b'action=accept dsthost=qubes-os.org\n')] = b'0\0'
  229. qubesadmin.tools.qvm_firewall.main(
  230. ['test-vm', 'del', 'drop', 'proto=icmp'],
  231. app=self.app
  232. )
  233. def test_030_reset(self):
  234. self.app.expected_calls[('test-vm', 'admin.vm.firewall.Get',
  235. None, None)] = \
  236. b'0\0action=accept dsthost=qubes-os.org\n' \
  237. b'action=drop proto=icmp\n'
  238. self.app.expected_calls[('test-vm', 'admin.vm.firewall.Set', None,
  239. b'action=accept\n')] = b'0\0'
  240. qubesadmin.tools.qvm_firewall.main(
  241. ['test-vm', 'reset'],
  242. app=self.app
  243. )