2
0

test_security_service.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. """Test cloudbridge.security modules."""
  2. import cloudbridge.cloud.base.helpers as cb_helpers
  3. from cloudbridge.cloud.interfaces.exceptions import DuplicateResourceException
  4. from cloudbridge.cloud.interfaces.resources import KeyPair
  5. from cloudbridge.cloud.interfaces.resources import TrafficDirection
  6. from cloudbridge.cloud.interfaces.resources import VMFirewall
  7. from cloudbridge.cloud.interfaces.resources import VMFirewallRule
  8. from test import helpers
  9. from test.helpers import ProviderTestBase
  10. from test.helpers import standard_interface_tests as sit
  11. class CloudSecurityServiceTestCase(ProviderTestBase):
  12. _multiprocess_can_split_ = True
  13. @helpers.skipIfNoService(['security.vm_firewalls'])
  14. def test_storage_services_event_pattern(self):
  15. expected_event = ".".join((self.provider.PROVIDER_ID,
  16. "security.key_pairs"))
  17. self.assertEqual(
  18. self.provider.security.key_pairs._service_event_pattern,
  19. expected_event,
  20. "Event pattern for {} service should be '{}', "
  21. "but found '{}'.".format("key_pairs",
  22. expected_event,
  23. self.provider.security.
  24. key_pairs.
  25. _service_event_pattern))
  26. expected_event = ".".join((self.provider.PROVIDER_ID,
  27. "security.vm_firewalls"))
  28. self.assertEqual(
  29. self.provider.security.vm_firewalls._service_event_pattern,
  30. expected_event,
  31. "Event pattern for {} service should be '{}', "
  32. "but found '{}'.".format("vm_firewalls",
  33. expected_event,
  34. self.provider.security.vm_firewalls.
  35. _service_event_pattern))
  36. @helpers.skipIfNoService(['security.key_pairs'])
  37. def test_crud_key_pair_service(self):
  38. def create_kp(name):
  39. return self.provider.security.key_pairs.create(name=name)
  40. def cleanup_kp(kp):
  41. if kp:
  42. self.provider.security.key_pairs.delete(kp.id)
  43. def extra_tests(kp):
  44. # Recreating existing keypair should raise an exception
  45. with self.assertRaises(DuplicateResourceException):
  46. self.provider.security.key_pairs.create(name=kp.name)
  47. sit.check_crud(self, self.provider.security.key_pairs, KeyPair,
  48. "cb-crudkp", create_kp, cleanup_kp,
  49. extra_test_func=extra_tests)
  50. @helpers.skipIfNoService(['security.key_pairs'])
  51. def test_key_pair_properties(self):
  52. name = 'cb-kpprops-{0}'.format(helpers.get_uuid())
  53. kp = self.provider.security.key_pairs.create(name=name)
  54. with cb_helpers.cleanup_action(lambda: kp.delete()):
  55. self.assertIsNotNone(
  56. kp.material,
  57. "KeyPair material is empty but it should not be.")
  58. # get the keypair again - keypair material should now be empty
  59. kp = self.provider.security.key_pairs.get(kp.id)
  60. self.assertIsNone(kp.material,
  61. "Keypair material should now be empty")
  62. @helpers.skipIfNoService(['security.key_pairs'])
  63. def test_import_key_pair(self):
  64. name = 'cb-kpimport-{0}'.format(helpers.get_uuid())
  65. public_key, _ = cb_helpers.generate_key_pair()
  66. kp = self.provider.security.key_pairs.create(
  67. name=name, public_key_material=public_key)
  68. with cb_helpers.cleanup_action(lambda: kp.delete()):
  69. self.assertIsNone(kp.material, "Private KeyPair material should"
  70. " be None when key is imported.")
  71. @helpers.skipIfNoService(['security.vm_firewalls'])
  72. def test_crud_vm_firewall(self):
  73. subnet = helpers.get_or_create_default_subnet(self.provider)
  74. net = subnet.network
  75. def create_fw(label):
  76. return self.provider.security.vm_firewalls.create(
  77. label=label, description=label, network=net.id)
  78. def cleanup_fw(fw):
  79. if fw:
  80. fw.delete()
  81. def network_id_test(fw):
  82. # Checking that the network ID is returned correctly
  83. self.assertEqual(fw.network_id, net.id)
  84. sit.check_crud(self, self.provider.security.vm_firewalls,
  85. VMFirewall, "cb-crudfw", create_fw, cleanup_fw,
  86. extra_test_func=network_id_test)
  87. @helpers.skipIfNoService(['security.vm_firewalls'])
  88. def test_vm_firewall_properties(self):
  89. label = 'cb-propfw-{0}'.format(helpers.get_uuid())
  90. # Declare these variables and late binding will allow
  91. # the cleanup method access to the most current values
  92. fw = None
  93. with cb_helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
  94. vm_firewall=fw)):
  95. subnet = helpers.get_or_create_default_subnet(self.provider)
  96. net = subnet.network
  97. fw = self.provider.security.vm_firewalls.create(
  98. label=label, description=label, network=net.id)
  99. self.assertEqual(label, fw.description)
  100. @helpers.skipIfNoService(['security.vm_firewalls'])
  101. def test_crud_vm_firewall_rules(self):
  102. label = 'cb-crudfw-rules-{0}'.format(helpers.get_uuid())
  103. subnet = helpers.get_or_create_default_subnet(self.provider)
  104. net = subnet.network
  105. fw = None
  106. with cb_helpers.cleanup_action(lambda: fw.delete()):
  107. fw = self.provider.security.vm_firewalls.create(
  108. label=label, description=label, network=net.id)
  109. def create_fw_rule(label):
  110. return fw.rules.create(
  111. direction=TrafficDirection.INBOUND, protocol='tcp',
  112. from_port=1111, to_port=1111, cidr='0.0.0.0/0')
  113. def cleanup_fw_rule(rule):
  114. if rule:
  115. rule.delete()
  116. sit.check_crud(self, fw.rules, VMFirewallRule, "cb-crudfwrule",
  117. create_fw_rule, cleanup_fw_rule,
  118. skip_name_check=True)
  119. @helpers.skipIfNoService(['security.vm_firewalls'])
  120. def test_vm_firewall_rule_properties(self):
  121. label = 'cb-propfwrule-{0}'.format(helpers.get_uuid())
  122. # Declare these variables and late binding will allow
  123. # the cleanup method access to the most current values
  124. fw = None
  125. with cb_helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
  126. vm_firewall=fw)):
  127. subnet = helpers.get_or_create_default_subnet(self.provider)
  128. net = subnet.network
  129. fw = self.provider.security.vm_firewalls.create(
  130. label=label, description=label, network=net.id)
  131. rule = fw.rules.create(
  132. direction=TrafficDirection.INBOUND, protocol='tcp',
  133. from_port=1111, to_port=1111, cidr='0.0.0.0/0')
  134. self.assertEqual(rule.direction, TrafficDirection.INBOUND)
  135. self.assertEqual(rule.protocol, 'tcp')
  136. self.assertEqual(rule.from_port, 1111)
  137. self.assertEqual(rule.to_port, 1111)
  138. self.assertEqual(rule.cidr, '0.0.0.0/0')
  139. @helpers.skipIfNoService(['security.vm_firewalls'])
  140. def test_vm_firewall_rule_add_twice(self):
  141. label = 'cb-fwruletwice-{0}'.format(helpers.get_uuid())
  142. # Declare these variables and late binding will allow
  143. # the cleanup method access to the most current values
  144. fw = None
  145. with cb_helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
  146. vm_firewall=fw)):
  147. subnet = helpers.get_or_create_default_subnet(self.provider)
  148. net = subnet.network
  149. fw = self.provider.security.vm_firewalls.create(
  150. label=label, description=label, network=net.id)
  151. rule = fw.rules.create(
  152. direction=TrafficDirection.INBOUND, protocol='tcp',
  153. from_port=1111, to_port=1111, cidr='0.0.0.0/0')
  154. # attempting to add the same rule twice should succeed
  155. same_rule = fw.rules.create(
  156. direction=TrafficDirection.INBOUND, protocol='tcp',
  157. from_port=1111, to_port=1111, cidr='0.0.0.0/0')
  158. self.assertEqual(rule, same_rule)
  159. @helpers.skipIfNoService(['security.vm_firewalls'])
  160. def test_vm_firewall_group_rule(self):
  161. label = 'cb-fwrule-{0}'.format(helpers.get_uuid())
  162. # Declare these variables and late binding will allow
  163. # the cleanup method access to the most current values
  164. fw = None
  165. with cb_helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
  166. vm_firewall=fw)):
  167. subnet = helpers.get_or_create_default_subnet(self.provider)
  168. net = subnet.network
  169. fw = self.provider.security.vm_firewalls.create(
  170. label=label, description=label, network=net.id)
  171. rule = fw.rules.create(
  172. direction=TrafficDirection.INBOUND, src_dest_fw=fw,
  173. protocol='tcp', from_port=1, to_port=65535)
  174. self.assertTrue(
  175. rule.src_dest_fw.label == fw.label,
  176. "Expected VM firewall rule label {0}. Got {1}."
  177. .format(fw.label, rule.src_dest_fw.label))
  178. for r in fw.rules:
  179. r.delete()
  180. fw = self.provider.security.vm_firewalls.get(fw.id) # update
  181. self.assertTrue(
  182. len(list(fw.rules)) == 0,
  183. "Deleting VMFirewallRule should delete it: {0}".format(
  184. fw.rules))
  185. fwl = self.provider.security.vm_firewalls.list()
  186. found_fw = [f for f in fwl if f.label == label]
  187. self.assertTrue(
  188. len(found_fw) == 0,
  189. "VM firewall {0} should have been deleted but still exists."
  190. .format(label))