test_security_service.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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.key_pairs'])
  14. def test_crud_key_pair_service(self):
  15. def create_kp(name):
  16. return self.provider.security.key_pairs.create(name=name)
  17. def cleanup_kp(kp):
  18. if kp:
  19. self.provider.security.key_pairs.delete(key_pair_id=kp.id)
  20. def extra_tests(kp):
  21. # Recreating existing keypair should raise an exception
  22. with self.assertRaises(DuplicateResourceException):
  23. self.provider.security.key_pairs.create(name=kp.name)
  24. sit.check_crud(self, self.provider.security.key_pairs, KeyPair,
  25. "cb-crudkp", create_kp, cleanup_kp,
  26. extra_test_func=extra_tests)
  27. @helpers.skipIfNoService(['security.key_pairs'])
  28. def test_key_pair_properties(self):
  29. name = 'cb-kpprops-{0}'.format(helpers.get_uuid())
  30. kp = self.provider.security.key_pairs.create(name=name)
  31. with helpers.cleanup_action(lambda: kp.delete()):
  32. self.assertIsNotNone(
  33. kp.material,
  34. "KeyPair material is empty but it should not be.")
  35. # get the keypair again - keypair material should now be empty
  36. kp = self.provider.security.key_pairs.get(kp.id)
  37. self.assertIsNone(kp.material,
  38. "Keypair material should now be empty")
  39. @helpers.skipIfNoService(['security.key_pairs'])
  40. def test_import_key_pair(self):
  41. name = 'cb-kpimport-{0}'.format(helpers.get_uuid())
  42. public_key, _ = cb_helpers.generate_key_pair()
  43. kp = self.provider.security.key_pairs.create(
  44. name=name, public_key_material=public_key)
  45. with helpers.cleanup_action(lambda: kp.delete()):
  46. self.assertIsNone(kp.material, "Private KeyPair material should"
  47. " be None when key is imported.")
  48. @helpers.skipIfNoService(['security.vm_firewalls'])
  49. def test_crud_vm_firewall(self):
  50. subnet = helpers.get_or_create_default_subnet(self.provider)
  51. net = subnet.network
  52. def create_fw(label):
  53. return self.provider.security.vm_firewalls.create(
  54. label=label, description=label, network_id=net.id)
  55. def cleanup_fw(fw):
  56. if fw:
  57. fw.delete()
  58. sit.check_crud(self, self.provider.security.vm_firewalls,
  59. VMFirewall, "cb-crudfw", create_fw, cleanup_fw)
  60. @helpers.skipIfNoService(['security.vm_firewalls'])
  61. def test_vm_firewall_properties(self):
  62. label = 'cb-propfw-{0}'.format(helpers.get_uuid())
  63. # Declare these variables and late binding will allow
  64. # the cleanup method access to the most current values
  65. fw = None
  66. with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
  67. vm_firewall=fw)):
  68. subnet = helpers.get_or_create_default_subnet(self.provider)
  69. net = subnet.network
  70. fw = self.provider.security.vm_firewalls.create(
  71. label=label, description=label, network_id=net.id)
  72. self.assertEqual(label, fw.description)
  73. @helpers.skipIfNoService(['security.vm_firewalls'])
  74. def test_crud_vm_firewall_rules(self):
  75. label = 'cb-crudfw-rules-{0}'.format(helpers.get_uuid())
  76. subnet = helpers.get_or_create_default_subnet(self.provider)
  77. net = subnet.network
  78. fw = None
  79. with helpers.cleanup_action(lambda: fw.delete()):
  80. fw = self.provider.security.vm_firewalls.create(
  81. label=label, description=label, network_id=net.id)
  82. def create_fw_rule(label):
  83. return fw.rules.create(
  84. direction=TrafficDirection.INBOUND, protocol='tcp',
  85. from_port=1111, to_port=1111, cidr='0.0.0.0/0')
  86. def cleanup_fw_rule(rule):
  87. if rule:
  88. rule.delete()
  89. sit.check_crud(self, fw.rules, VMFirewallRule, "cb-crudfwrule",
  90. create_fw_rule, cleanup_fw_rule,
  91. skip_name_check=True)
  92. @helpers.skipIfNoService(['security.vm_firewalls'])
  93. def test_vm_firewall_rule_properties(self):
  94. label = 'cb-propfwrule-{0}'.format(helpers.get_uuid())
  95. # Declare these variables and late binding will allow
  96. # the cleanup method access to the most current values
  97. fw = None
  98. with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
  99. vm_firewall=fw)):
  100. subnet = helpers.get_or_create_default_subnet(self.provider)
  101. net = subnet.network
  102. fw = self.provider.security.vm_firewalls.create(
  103. label=label, description=label, network_id=net.id)
  104. rule = fw.rules.create(
  105. direction=TrafficDirection.INBOUND, protocol='tcp',
  106. from_port=1111, to_port=1111, cidr='0.0.0.0/0')
  107. self.assertEqual(rule.direction, TrafficDirection.INBOUND)
  108. self.assertEqual(rule.protocol, 'tcp')
  109. self.assertEqual(rule.from_port, 1111)
  110. self.assertEqual(rule.to_port, 1111)
  111. self.assertEqual(rule.cidr, '0.0.0.0/0')
  112. @helpers.skipIfNoService(['security.vm_firewalls'])
  113. def test_vm_firewall_rule_add_twice(self):
  114. label = 'cb-fwruletwice-{0}'.format(helpers.get_uuid())
  115. # Declare these variables and late binding will allow
  116. # the cleanup method access to the most current values
  117. fw = None
  118. with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
  119. vm_firewall=fw)):
  120. subnet = helpers.get_or_create_default_subnet(self.provider)
  121. net = subnet.network
  122. fw = self.provider.security.vm_firewalls.create(
  123. label=label, description=label, network_id=net.id)
  124. rule = fw.rules.create(
  125. direction=TrafficDirection.INBOUND, protocol='tcp',
  126. from_port=1111, to_port=1111, cidr='0.0.0.0/0')
  127. # attempting to add the same rule twice should succeed
  128. same_rule = fw.rules.create(
  129. direction=TrafficDirection.INBOUND, protocol='tcp',
  130. from_port=1111, to_port=1111, cidr='0.0.0.0/0')
  131. self.assertEqual(rule, same_rule)
  132. @helpers.skipIfNoService(['security.vm_firewalls'])
  133. def test_vm_firewall_group_rule(self):
  134. label = 'cb-fwrule-{0}'.format(helpers.get_uuid())
  135. # Declare these variables and late binding will allow
  136. # the cleanup method access to the most current values
  137. fw = None
  138. with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
  139. vm_firewall=fw)):
  140. subnet = helpers.get_or_create_default_subnet(self.provider)
  141. net = subnet.network
  142. fw = self.provider.security.vm_firewalls.create(
  143. label=label, description=label, network_id=net.id)
  144. rules = list(fw.rules)
  145. self.assertTrue(
  146. # TODO: This should be made consistent across all providers.
  147. # Currently, OpenStack creates two rules, one for IPV6 and
  148. # another for IPV4
  149. len(rules) >= 1, "Expected a single VM firewall rule allowing"
  150. " all outbound traffic. Got {0}.".format(rules))
  151. self.assertEqual(
  152. rules[0].direction, TrafficDirection.OUTBOUND,
  153. "Expected rule to be outbound. Got {0}.".format(rules))
  154. rule = fw.rules.create(
  155. direction=TrafficDirection.INBOUND, src_dest_fw=fw,
  156. protocol='tcp', from_port=1, to_port=65535)
  157. self.assertTrue(
  158. rule.src_dest_fw.label == fw.label,
  159. "Expected VM firewall rule label {0}. Got {1}."
  160. .format(fw.label, rule.src_dest_fw.label))
  161. for r in fw.rules:
  162. r.delete()
  163. fw = self.provider.security.vm_firewalls.get(fw.id) # update
  164. self.assertTrue(
  165. len(list(fw.rules)) == 0,
  166. "Deleting VMFirewallRule should delete it: {0}".format(
  167. fw.rules))
  168. fwl = self.provider.security.vm_firewalls.list()
  169. found_fw = [f for f in fwl if f.label == label]
  170. self.assertTrue(
  171. len(found_fw) == 0,
  172. "VM firewall {0} should have been deleted but still exists."
  173. .format(label))