test_security_service.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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=net.id)
  55. def cleanup_fw(fw):
  56. if fw:
  57. fw.delete()
  58. def network_id_test(fw):
  59. # Checking that the network ID is returned correctly
  60. self.assertEqual(fw.network_id, net.id)
  61. sit.check_crud(self, self.provider.security.vm_firewalls,
  62. VMFirewall, "cb-crudfw", create_fw, cleanup_fw,
  63. extra_test_func=network_id_test)
  64. @helpers.skipIfNoService(['security.vm_firewalls'])
  65. def test_vm_firewall_properties(self):
  66. label = 'cb-propfw-{0}'.format(helpers.get_uuid())
  67. # Declare these variables and late binding will allow
  68. # the cleanup method access to the most current values
  69. fw = None
  70. with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
  71. vm_firewall=fw)):
  72. subnet = helpers.get_or_create_default_subnet(self.provider)
  73. net = subnet.network
  74. fw = self.provider.security.vm_firewalls.create(
  75. label=label, description=label, network_id=net.id)
  76. self.assertEqual(label, fw.description)
  77. @helpers.skipIfNoService(['security.vm_firewalls'])
  78. def test_crud_vm_firewall_rules(self):
  79. label = 'cb-crudfw-rules-{0}'.format(helpers.get_uuid())
  80. subnet = helpers.get_or_create_default_subnet(self.provider)
  81. net = subnet.network
  82. fw = None
  83. with helpers.cleanup_action(lambda: fw.delete()):
  84. fw = self.provider.security.vm_firewalls.create(
  85. label=label, description=label, network_id=net.id)
  86. def create_fw_rule(label):
  87. return fw.rules.create(
  88. direction=TrafficDirection.INBOUND, protocol='tcp',
  89. from_port=1111, to_port=1111, cidr='0.0.0.0/0')
  90. def cleanup_fw_rule(rule):
  91. if rule:
  92. rule.delete()
  93. sit.check_crud(self, fw.rules, VMFirewallRule, "cb-crudfwrule",
  94. create_fw_rule, cleanup_fw_rule,
  95. skip_name_check=True)
  96. @helpers.skipIfNoService(['security.vm_firewalls'])
  97. def test_vm_firewall_rule_properties(self):
  98. label = 'cb-propfwrule-{0}'.format(helpers.get_uuid())
  99. # Declare these variables and late binding will allow
  100. # the cleanup method access to the most current values
  101. fw = None
  102. with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
  103. vm_firewall=fw)):
  104. subnet = helpers.get_or_create_default_subnet(self.provider)
  105. net = subnet.network
  106. fw = self.provider.security.vm_firewalls.create(
  107. label=label, description=label, network_id=net.id)
  108. rule = fw.rules.create(
  109. direction=TrafficDirection.INBOUND, protocol='tcp',
  110. from_port=1111, to_port=1111, cidr='0.0.0.0/0')
  111. self.assertEqual(rule.direction, TrafficDirection.INBOUND)
  112. self.assertEqual(rule.protocol, 'tcp')
  113. self.assertEqual(rule.from_port, 1111)
  114. self.assertEqual(rule.to_port, 1111)
  115. self.assertEqual(rule.cidr, '0.0.0.0/0')
  116. @helpers.skipIfNoService(['security.vm_firewalls'])
  117. def test_vm_firewall_rule_add_twice(self):
  118. label = 'cb-fwruletwice-{0}'.format(helpers.get_uuid())
  119. # Declare these variables and late binding will allow
  120. # the cleanup method access to the most current values
  121. fw = None
  122. with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
  123. vm_firewall=fw)):
  124. subnet = helpers.get_or_create_default_subnet(self.provider)
  125. net = subnet.network
  126. fw = self.provider.security.vm_firewalls.create(
  127. label=label, description=label, network_id=net.id)
  128. rule = fw.rules.create(
  129. direction=TrafficDirection.INBOUND, protocol='tcp',
  130. from_port=1111, to_port=1111, cidr='0.0.0.0/0')
  131. # attempting to add the same rule twice should succeed
  132. same_rule = fw.rules.create(
  133. direction=TrafficDirection.INBOUND, protocol='tcp',
  134. from_port=1111, to_port=1111, cidr='0.0.0.0/0')
  135. self.assertEqual(rule, same_rule)
  136. @helpers.skipIfNoService(['security.vm_firewalls'])
  137. def test_vm_firewall_group_rule(self):
  138. label = 'cb-fwrule-{0}'.format(helpers.get_uuid())
  139. # Declare these variables and late binding will allow
  140. # the cleanup method access to the most current values
  141. fw = None
  142. with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
  143. vm_firewall=fw)):
  144. subnet = helpers.get_or_create_default_subnet(self.provider)
  145. net = subnet.network
  146. fw = self.provider.security.vm_firewalls.create(
  147. label=label, description=label, network_id=net.id)
  148. rule = fw.rules.create(
  149. direction=TrafficDirection.INBOUND, src_dest_fw=fw,
  150. protocol='tcp', from_port=1, to_port=65535)
  151. self.assertTrue(
  152. rule.src_dest_fw.label == fw.label,
  153. "Expected VM firewall rule label {0}. Got {1}."
  154. .format(fw.label, rule.src_dest_fw.label))
  155. for r in fw.rules:
  156. r.delete()
  157. fw = self.provider.security.vm_firewalls.get(fw.id) # update
  158. self.assertTrue(
  159. len(list(fw.rules)) == 0,
  160. "Deleting VMFirewallRule should delete it: {0}".format(
  161. fw.rules))
  162. fwl = self.provider.security.vm_firewalls.list()
  163. found_fw = [f for f in fwl if f.label == label]
  164. self.assertTrue(
  165. len(found_fw) == 0,
  166. "VM firewall {0} should have been deleted but still exists."
  167. .format(label))