test_security_service.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. """Test cloudbridge.security modules."""
  2. import json
  3. from test.helpers import ProviderTestBase
  4. import time
  5. import uuid
  6. import test.helpers as helpers
  7. class CloudSecurityServiceTestCase(ProviderTestBase):
  8. def __init__(self, methodName, provider):
  9. super(CloudSecurityServiceTestCase, self).__init__(
  10. methodName=methodName, provider=provider)
  11. def test_crud_key_pair_service(self):
  12. name = 'cbtestkeypairA-{0}'.format(uuid.uuid4()).lower()
  13. kp = self.provider.security.key_pairs.create(name=name)
  14. with helpers.cleanup_action(
  15. lambda:
  16. self.provider.security.key_pairs.delete(key_pair_id=kp.id)
  17. ):
  18. # test list method
  19. kpl = self.provider.security.key_pairs.list()
  20. list_kpl = [i for i in kpl if i.id == kp.id]
  21. self.assertTrue(
  22. len(list_kpl) == 1,
  23. "List key pairs does not return the expected key pair %s" %
  24. name)
  25. # check iteration
  26. iter_kpl = [i for i in self.provider.security.key_pairs
  27. if i.id == kp.id]
  28. self.assertTrue(
  29. len(iter_kpl) == 1,
  30. "Iter key pairs does not return the expected key pair %s" %
  31. name)
  32. # check find
  33. find_kp = self.provider.security.key_pairs.find(name=kp.name)[0]
  34. self.assertTrue(
  35. find_kp == kp,
  36. "Find key pair did not return the expected key {0}."
  37. .format(name))
  38. # check get
  39. get_kp = self.provider.security.key_pairs.get(kp.id)
  40. self.assertTrue(
  41. get_kp == kp,
  42. "Get key pair did not return the expected key {0}."
  43. .format(name))
  44. # Recreating existing keypair should raise an exception
  45. with self.assertRaises(Exception):
  46. self.provider.security.key_pairs.create(name=name)
  47. kpl = self.provider.security.key_pairs.list()
  48. found_kp = [k for k in kpl if k.id == kp.id]
  49. self.assertTrue(
  50. len(found_kp) == 0,
  51. "Key pair {0} should have been deleted but still exists."
  52. .format(name))
  53. no_kp = self.provider.security.key_pairs.find(name='bogus_kp')
  54. self.assertFalse(
  55. no_kp,
  56. "Found a key pair {0} that should not exist?".format(no_kp))
  57. def test_key_pair(self):
  58. name = 'cbtestkeypairB-{0}'.format(uuid.uuid4()).lower()
  59. kp = self.provider.security.key_pairs.create(name=name)
  60. with helpers.cleanup_action(lambda: kp.delete()):
  61. kpl = self.provider.security.key_pairs.list()
  62. found_kp = [k for k in kpl if k.id == kp.id]
  63. self.assertTrue(
  64. len(found_kp) == 1,
  65. "List key pairs did not return the expected key {0}."
  66. .format(name))
  67. self.assertTrue(
  68. kp.id in repr(kp),
  69. "repr(obj) should contain the object id so that the object"
  70. " can be reconstructed, but does not. eval(repr(obj)) == obj")
  71. self.assertIsNotNone(
  72. kp.material,
  73. "KeyPair material is empty but it should not be.")
  74. self.assertTrue(
  75. kp == kp,
  76. "The same key pair should be equal to self.")
  77. # check json deserialization
  78. self.assertTrue(json.loads(kp.to_json()),
  79. "to_json must yield a valid json string: {0}"
  80. .format(kp.to_json()))
  81. kpl = self.provider.security.key_pairs.list()
  82. found_kp = [k for k in kpl if k.id == kp.id]
  83. self.assertTrue(
  84. len(found_kp) == 0,
  85. "Key pair {0} should have been deleted but still exists."
  86. .format(name))
  87. def cleanup_sg(self, sg, net):
  88. with helpers.cleanup_action(
  89. lambda: self.provider.network.delete(network_id=net.id)):
  90. self.provider.security.security_groups.delete(group_id=sg.id)
  91. def test_crud_security_group_service(self):
  92. name = 'cbtestsecuritygroupA-{0}'.format(uuid.uuid4()).lower()
  93. net = self.provider.network.create(name=name)
  94. sg = self.provider.security.security_groups.create(
  95. name=name, description=name, network_id=net.id)
  96. #Empty security groups don't exist in GCE. Let's add a dummy rule.
  97. sg.add_rule(ip_protocol='tcp', cidr_ip='0.0.0.0/0')
  98. with helpers.cleanup_action(lambda: self.cleanup_sg(sg, net)):
  99. self.assertEqual(name, sg.description)
  100. # test list method
  101. sgl = self.provider.security.security_groups.list()
  102. found_sgl = [i for i in sgl if i.name == name]
  103. self.assertTrue(
  104. len(found_sgl) == 1,
  105. "List security groups does not return the expected group %s" %
  106. name)
  107. # check iteration
  108. found_sgl = [i for i in self.provider.security.security_groups
  109. if i.name == name]
  110. self.assertTrue(
  111. len(found_sgl) == 1,
  112. "Iter security groups does not return the expected group %s" %
  113. name)
  114. # check find
  115. find_sg = self.provider.security.security_groups.find(name=sg.name)
  116. self.assertTrue(
  117. len(find_sg) == 1,
  118. "List security groups returned {0} when expected was: {1}."
  119. .format(find_sg, sg.name))
  120. # check get
  121. get_sg = self.provider.security.security_groups.get(sg.id)
  122. self.assertTrue(
  123. get_sg == sg,
  124. "Get SecurityGroup did not return the expected key {0}."
  125. .format(name))
  126. self.assertTrue(
  127. sg.id in repr(sg),
  128. "repr(obj) should contain the object id so that the object"
  129. " can be reconstructed, but does not. eval(repr(obj)) == obj")
  130. sgl = self.provider.security.security_groups.list()
  131. found_sg = [g for g in sgl if g.name == name]
  132. self.assertTrue(
  133. len(found_sg) == 0,
  134. "Security group {0} should have been deleted but still exists."
  135. .format(name))
  136. no_sg = self.provider.security.security_groups.find(name='bogus_sg')
  137. self.assertTrue(
  138. len(no_sg) == 0,
  139. "Found a bogus security group?!?".format(no_sg))
  140. def test_security_group(self):
  141. """Test for proper creation of a security group."""
  142. name = 'cbtestsecuritygroupB-{0}'.format(uuid.uuid4()).lower()
  143. net = self.provider.network.create(name=name)
  144. sg = self.provider.security.security_groups.create(
  145. name=name, description=name, network_id=net.id)
  146. with helpers.cleanup_action(lambda: self.cleanup_sg(sg, net)):
  147. rule = sg.add_rule(ip_protocol='tcp', from_port=1111, to_port=1111,
  148. cidr_ip='0.0.0.0/0')
  149. found_rule = sg.get_rule(ip_protocol='tcp', from_port=1111,
  150. to_port=1111, cidr_ip='0.0.0.0/0')
  151. self.assertTrue(
  152. rule == found_rule,
  153. "Expected rule {0} not found in security group: {1}".format(
  154. rule, sg.rules))
  155. object_keys = (
  156. sg.rules[0].ip_protocol,
  157. sg.rules[0].from_port,
  158. sg.rules[0].to_port)
  159. self.assertTrue(
  160. all(str(key) in repr(sg.rules[0]) for key in object_keys),
  161. "repr(obj) should contain ip_protocol, form_port, and to_port"
  162. " so that the object can be reconstructed, but does not:"
  163. " {0}; {1}".format(sg.rules[0], object_keys))
  164. self.assertTrue(
  165. sg == sg,
  166. "The same security groups should be equal?")
  167. self.assertFalse(
  168. sg != sg,
  169. "The same security groups should still be equal?")
  170. # json_repr = json.dumps(
  171. # {"description": name, "name": name, "id": sg.id,
  172. # "rules":
  173. # [{"from_port": 1111, "group": "", "cidr_ip": "0.0.0.0/0",
  174. # "parent": sg.id, "to_port": 1111, "ip_protocol": "tcp",
  175. # "id": sg.rules[0].id}]},
  176. # sort_keys=True)
  177. # self.assertTrue(
  178. # sg.to_json() == json_repr,
  179. # "JSON sec group representation {0} does not match expected {1}"
  180. # .format(sg.to_json(), json_repr))
  181. sgl = self.provider.security.security_groups.list()
  182. found_sg = [g for g in sgl if g.name == name]
  183. self.assertTrue(
  184. len(found_sg) == 0,
  185. "Security group {0} should have been deleted but still exists."
  186. .format(name))
  187. def test_security_group_rule_add_twice(self):
  188. """Test whether adding the same rule twice succeeds."""
  189. name = 'cbtestsecuritygroupB-{0}'.format(uuid.uuid4()).lower()
  190. net = self.provider.network.create(name=name)
  191. sg = self.provider.security.security_groups.create(
  192. name=name, description=name, network_id=net.id)
  193. with helpers.cleanup_action(lambda: self.cleanup_sg(sg, net)):
  194. rule = sg.add_rule(ip_protocol='tcp', from_port=1111, to_port=1111,
  195. cidr_ip='0.0.0.0/0')
  196. # attempting to add the same rule twice should succeed
  197. same_rule = sg.add_rule(ip_protocol='tcp', from_port=1111,
  198. to_port=1111, cidr_ip='0.0.0.0/0')
  199. self.assertTrue(
  200. rule == same_rule,
  201. "Expected rule {0} not found in security group: {1}".format(
  202. same_rule, sg.rules))
  203. def test_security_group_group_rule(self):
  204. """Test for proper creation of a security group rule."""
  205. name = 'cbtestsecuritygroupC-{0}'.format(uuid.uuid4()).lower()
  206. net = self.provider.network.create(name=name)
  207. sg = self.provider.security.security_groups.create(
  208. name=name, description=name, network_id=net.id)
  209. with helpers.cleanup_action(lambda: self.cleanup_sg(sg, net)):
  210. self.assertTrue(
  211. len(sg.rules) == 0,
  212. "Expected no security group group rule. Got {0}."
  213. .format(sg.rules))
  214. rule = sg.add_rule(src_group=sg, ip_protocol='tcp', from_port=0,
  215. to_port=65535)
  216. self.assertTrue(
  217. rule.group.name == name,
  218. "Expected security group rule name {0}. Got {1}."
  219. .format(name, rule.group.name))
  220. for r in sg.rules:
  221. r.delete()
  222. sg = self.provider.security.security_groups.get(sg.id) # update
  223. self.assertTrue(
  224. sg is None or len(sg.rules) == 0,
  225. "Deleting SecurityGroupRule should delete it: {0}".format(
  226. [] if sg is None else sg.rules))
  227. sgl = self.provider.security.security_groups.list()
  228. found_sg = [g for g in sgl if g.name == name]
  229. self.assertTrue(
  230. len(found_sg) == 0,
  231. "Security group {0} should have been deleted but still exists."
  232. .format(name))