qvm_firewall.py 9.9 KB

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