qvm_firewall.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. class TC_10_qvm_firewall(qubesadmin.tests.QubesTestCase):
  88. def setUp(self):
  89. super(TC_10_qvm_firewall, self).setUp()
  90. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  91. b'0\0test-vm class=AppVM state=Halted\n'
  92. def test_000_list(self):
  93. self.app.expected_calls[('test-vm', 'admin.vm.firewall.Get',
  94. None, None)] = \
  95. b'0\0action=accept dsthost=qubes-os.org\n' \
  96. b'action=drop proto=icmp\n'
  97. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  98. qubesadmin.tools.qvm_firewall.main(['test-vm', 'list'], app=self.app)
  99. self.assertEqual(
  100. [l.strip() for l in stdout.getvalue().splitlines()],
  101. ['NO ACTION HOST PROTOCOL PORT(S) SPECIAL '
  102. 'TARGET ICMP TYPE EXPIRE COMMENT',
  103. '0 accept qubes-os.org - - - '
  104. ' - - -',
  105. '1 drop - icmp - - '
  106. ' - - -',
  107. ])
  108. def test_001_list_default(self):
  109. self.app.expected_calls[('test-vm', 'admin.vm.firewall.Get',
  110. None, None)] = \
  111. b'0\0action=accept dsthost=qubes-os.org proto=tcp ' \
  112. b'dstports=443-443\n' \
  113. b'action=drop proto=icmp icmptype=8\n' \
  114. b'action=accept specialtarget=dns comment=Allow DNS\n'
  115. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  116. qubesadmin.tools.qvm_firewall.main(['test-vm'], app=self.app)
  117. self.assertEqual(
  118. [l.strip() for l in stdout.getvalue().splitlines()],
  119. ['NO ACTION HOST PROTOCOL PORT(S) SPECIAL '
  120. 'TARGET ICMP TYPE EXPIRE COMMENT',
  121. '0 accept qubes-os.org tcp 443 - '
  122. ' - - -',
  123. '1 drop - icmp - - '
  124. ' 8 - -',
  125. '2 accept - - - dns '
  126. ' - - Allow DNS',
  127. ])
  128. def test_002_list_expire(self):
  129. in_1h = int(time.time()) + 3600
  130. self.app.expected_calls[('test-vm', 'admin.vm.firewall.Get',
  131. None, None)] = \
  132. '0\0action=accept dsthost=qubes-os.org proto=tcp ' \
  133. 'dstports=443-443 expire={}\n'.format(in_1h).encode()
  134. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  135. qubesadmin.tools.qvm_firewall.main(['test-vm'], app=self.app)
  136. line = stdout.getvalue().splitlines()[-1]
  137. match = re.match(
  138. '0 accept qubes-os.org tcp 443 - '
  139. ' - \+(.{4})s -',
  140. line)
  141. self.assertTrue(match, "no match for: {!r}".format(line))
  142. duration = int(match.group(1))
  143. self.assertTrue(3590 < duration <= 3600)
  144. def test_002_list_raw(self):
  145. self.app.expected_calls[('test-vm', 'admin.vm.firewall.Get',
  146. None, None)] = \
  147. b'0\0action=accept dsthost=qubes-os.org\n' \
  148. b'action=drop proto=icmp\n'
  149. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  150. qubesadmin.tools.qvm_firewall.main(['test-vm', '--raw', 'list'],
  151. app=self.app)
  152. self.assertEqual(
  153. [l.strip() for l in stdout.getvalue().splitlines()],
  154. ['action=accept dsthost=qubes-os.org',
  155. 'action=drop proto=icmp',
  156. ])
  157. def test_003_list_raw_reload(self):
  158. self.app.expected_calls[('test-vm', 'admin.vm.firewall.Get',
  159. None, None)] = \
  160. b'0\0action=accept dsthost=qubes-os.org\n' \
  161. b'action=drop proto=icmp\n'
  162. self.app.expected_calls[('test-vm', 'admin.vm.firewall.Reload',
  163. None, None)] = b'0\0'
  164. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  165. qubesadmin.tools.qvm_firewall.main(
  166. ['test-vm', '--raw', '--reload', 'list'],
  167. app=self.app)
  168. self.assertEqual(
  169. [l.strip() for l in stdout.getvalue().splitlines()],
  170. ['action=accept dsthost=qubes-os.org',
  171. 'action=drop proto=icmp',
  172. ])
  173. def test_010_add_after(self):
  174. self.app.expected_calls[('test-vm', 'admin.vm.firewall.Get',
  175. None, None)] = \
  176. b'0\0action=accept dsthost=qubes-os.org\n' \
  177. b'action=drop proto=icmp\n'
  178. self.app.expected_calls[('test-vm', 'admin.vm.firewall.Set', None,
  179. b'action=accept dsthost=qubes-os.org\n'
  180. b'action=drop proto=icmp\n'
  181. b'action=accept dst4=192.168.0.0/24 comment=Allow LAN\n')] = \
  182. b'0\0'
  183. qubesadmin.tools.qvm_firewall.main(
  184. ['test-vm', 'add', 'accept', '192.168.0.0/24', 'comment=Allow LAN'],
  185. app=self.app
  186. )
  187. def test_011_add_before(self):
  188. self.app.expected_calls[('test-vm', 'admin.vm.firewall.Get',
  189. None, None)] = \
  190. b'0\0action=accept dsthost=qubes-os.org\n' \
  191. b'action=drop proto=icmp\n'
  192. self.app.expected_calls[('test-vm', 'admin.vm.firewall.Set', None,
  193. b'action=accept dsthost=qubes-os.org\n'
  194. b'action=accept dst4=192.168.0.0/24 comment=Allow LAN\n'
  195. b'action=drop proto=icmp\n')] = b'0\0'
  196. qubesadmin.tools.qvm_firewall.main(
  197. ['test-vm', 'add', '--before', '1', 'accept', '192.168.0.0/24',
  198. 'comment=Allow LAN'],
  199. app=self.app
  200. )
  201. def test_020_del_number(self):
  202. self.app.expected_calls[('test-vm', 'admin.vm.firewall.Get',
  203. None, None)] = \
  204. b'0\0action=accept dsthost=qubes-os.org\n' \
  205. b'action=drop proto=icmp\n'
  206. self.app.expected_calls[('test-vm', 'admin.vm.firewall.Set', None,
  207. b'action=accept dsthost=qubes-os.org\n')] = b'0\0'
  208. qubesadmin.tools.qvm_firewall.main(
  209. ['test-vm', 'del', '--rule-no', '1'],
  210. app=self.app
  211. )
  212. def test_021_del_rule(self):
  213. self.app.expected_calls[('test-vm', 'admin.vm.firewall.Get',
  214. None, None)] = \
  215. b'0\0action=accept dsthost=qubes-os.org\n' \
  216. b'action=drop proto=icmp\n'
  217. self.app.expected_calls[('test-vm', 'admin.vm.firewall.Set', None,
  218. b'action=accept dsthost=qubes-os.org\n')] = b'0\0'
  219. qubesadmin.tools.qvm_firewall.main(
  220. ['test-vm', 'del', 'drop', 'proto=icmp'],
  221. app=self.app
  222. )