test_vault.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. # -*- coding: utf-8 -*-
  2. """
  3. Integration tests for the vault modules
  4. """
  5. # Import Python Libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import inspect
  8. import logging
  9. import time
  10. import salt.utils.path
  11. from tests.support.case import ModuleCase, ShellCase
  12. from tests.support.helpers import destructiveTest, flaky
  13. from tests.support.runtests import RUNTIME_VARS
  14. # Import Salt Testing Libs
  15. from tests.support.unit import skipIf
  16. log = logging.getLogger(__name__)
  17. @skipIf(not salt.utils.path.which("dockerd"), "Docker not installed")
  18. @skipIf(not salt.utils.path.which("vault"), "Vault not installed")
  19. class VaultTestCase(ModuleCase, ShellCase):
  20. """
  21. Test vault module
  22. """
  23. count = 0
  24. def setUp(self):
  25. """
  26. SetUp vault container
  27. """
  28. vault_binary = salt.utils.path.which("vault")
  29. if VaultTestCase.count == 0:
  30. config = '{"backend": {"file": {"path": "/vault/file"}}, "default_lease_ttl": "168h", "max_lease_ttl": "720h"}'
  31. self.run_state("docker_image.present", name="vault", tag="0.9.6")
  32. self.run_state(
  33. "docker_container.running",
  34. name="vault",
  35. image="vault:0.9.6",
  36. port_bindings="8200:8200",
  37. environment={
  38. "VAULT_DEV_ROOT_TOKEN_ID": "testsecret",
  39. "VAULT_LOCAL_CONFIG": config,
  40. },
  41. cap_add="IPC_LOCK",
  42. )
  43. time.sleep(5)
  44. ret = self.run_function(
  45. "cmd.retcode",
  46. cmd="{} login token=testsecret".format(vault_binary),
  47. env={"VAULT_ADDR": "http://127.0.0.1:8200"},
  48. )
  49. login_attempts = 1
  50. # If the login failed, container might have stopped
  51. # attempt again, maximum of three times before
  52. # skipping.
  53. while ret != 0:
  54. self.run_state(
  55. "docker_container.running",
  56. name="vault",
  57. image="vault:0.9.6",
  58. port_bindings="8200:8200",
  59. environment={
  60. "VAULT_DEV_ROOT_TOKEN_ID": "testsecret",
  61. "VAULT_LOCAL_CONFIG": config,
  62. },
  63. cap_add="IPC_LOCK",
  64. )
  65. time.sleep(5)
  66. ret = self.run_function(
  67. "cmd.retcode",
  68. cmd="{} login token=testsecret".format(vault_binary),
  69. env={"VAULT_ADDR": "http://127.0.0.1:8200"},
  70. )
  71. login_attempts += 1
  72. if login_attempts >= 3:
  73. self.skipTest("unable to login to vault")
  74. ret = self.run_function(
  75. "cmd.retcode",
  76. cmd="{} policy write testpolicy {}/vault.hcl".format(
  77. vault_binary, RUNTIME_VARS.FILES
  78. ),
  79. env={"VAULT_ADDR": "http://127.0.0.1:8200"},
  80. )
  81. if ret != 0:
  82. self.skipTest("unable to assign policy to vault")
  83. VaultTestCase.count += 1
  84. def tearDown(self):
  85. """
  86. TearDown vault container
  87. """
  88. def count_tests(funcobj):
  89. return (
  90. inspect.ismethod(funcobj)
  91. or inspect.isfunction(funcobj)
  92. and funcobj.__name__.startswith("test_")
  93. )
  94. numtests = len(inspect.getmembers(VaultTestCase, predicate=count_tests))
  95. if VaultTestCase.count >= numtests:
  96. self.run_state("docker_container.stopped", name="vault")
  97. self.run_state("docker_container.absent", name="vault")
  98. self.run_state("docker_image.absent", name="vault", force=True)
  99. @flaky
  100. @skipIf(True, "SLOWTEST skip")
  101. def test_sdb(self):
  102. set_output = self.run_function(
  103. "sdb.set", uri="sdb://sdbvault/secret/test/test_sdb/foo", value="bar"
  104. )
  105. self.assertEqual(set_output, True)
  106. get_output = self.run_function(
  107. "sdb.get", arg=["sdb://sdbvault/secret/test/test_sdb/foo"]
  108. )
  109. self.assertEqual(get_output, "bar")
  110. @flaky
  111. @skipIf(True, "SLOWTEST skip")
  112. def test_sdb_runner(self):
  113. set_output = self.run_run(
  114. "sdb.set sdb://sdbvault/secret/test/test_sdb_runner/foo bar"
  115. )
  116. self.assertEqual(set_output, ["True"])
  117. get_output = self.run_run(
  118. "sdb.get sdb://sdbvault/secret/test/test_sdb_runner/foo"
  119. )
  120. self.assertEqual(get_output, ["bar"])
  121. @flaky
  122. @skipIf(True, "SLOWTEST skip")
  123. def test_config(self):
  124. set_output = self.run_function(
  125. "sdb.set", uri="sdb://sdbvault/secret/test/test_pillar_sdb/foo", value="bar"
  126. )
  127. self.assertEqual(set_output, True)
  128. get_output = self.run_function("config.get", arg=["test_vault_pillar_sdb"])
  129. self.assertEqual(get_output, "bar")
  130. @destructiveTest
  131. @skipIf(not salt.utils.path.which("dockerd"), "Docker not installed")
  132. @skipIf(not salt.utils.path.which("vault"), "Vault not installed")
  133. class VaultTestCaseCurrent(ModuleCase, ShellCase):
  134. """
  135. Test vault module
  136. """
  137. count = 0
  138. def setUp(self):
  139. """
  140. SetUp vault container
  141. """
  142. if self.count == 0:
  143. config = '{"backend": {"file": {"path": "/vault/file"}}, "default_lease_ttl": "168h", "max_lease_ttl": "720h"}'
  144. self.run_state("docker_image.present", name="vault", tag="1.3.1")
  145. self.run_state(
  146. "docker_container.running",
  147. name="vault",
  148. image="vault:1.3.1",
  149. port_bindings="8200:8200",
  150. environment={
  151. "VAULT_DEV_ROOT_TOKEN_ID": "testsecret",
  152. "VAULT_LOCAL_CONFIG": config,
  153. },
  154. cap_add="IPC_LOCK",
  155. )
  156. time.sleep(5)
  157. ret = self.run_function(
  158. "cmd.retcode",
  159. cmd="/usr/local/bin/vault login token=testsecret",
  160. env={"VAULT_ADDR": "http://127.0.0.1:8200"},
  161. )
  162. login_attempts = 1
  163. # If the login failed, container might have stopped
  164. # attempt again, maximum of three times before
  165. # skipping.
  166. while ret != 0:
  167. self.run_state(
  168. "docker_container.running",
  169. name="vault",
  170. image="vault:1.3.1",
  171. port_bindings="8200:8200",
  172. environment={
  173. "VAULT_DEV_ROOT_TOKEN_ID": "testsecret",
  174. "VAULT_LOCAL_CONFIG": config,
  175. },
  176. cap_add="IPC_LOCK",
  177. )
  178. time.sleep(5)
  179. ret = self.run_function(
  180. "cmd.retcode",
  181. cmd="/usr/local/bin/vault login token=testsecret",
  182. env={"VAULT_ADDR": "http://127.0.0.1:8200"},
  183. )
  184. login_attempts += 1
  185. if login_attempts >= 3:
  186. self.skipTest("unable to login to vault")
  187. ret = self.run_function(
  188. "cmd.retcode",
  189. cmd="/usr/local/bin/vault policy write testpolicy {0}/vault.hcl".format(
  190. RUNTIME_VARS.FILES
  191. ),
  192. env={"VAULT_ADDR": "http://127.0.0.1:8200"},
  193. )
  194. if ret != 0:
  195. self.skipTest("unable to assign policy to vault")
  196. self.count += 1
  197. def tearDown(self):
  198. """
  199. TearDown vault container
  200. """
  201. def count_tests(funcobj):
  202. return (
  203. inspect.ismethod(funcobj)
  204. or inspect.isfunction(funcobj)
  205. and funcobj.__name__.startswith("test_")
  206. )
  207. numtests = len(inspect.getmembers(VaultTestCaseCurrent, predicate=count_tests))
  208. if self.count >= numtests:
  209. self.run_state("docker_container.stopped", name="vault")
  210. self.run_state("docker_container.absent", name="vault")
  211. self.run_state("docker_image.absent", name="vault", force=True)
  212. @flaky
  213. @skipIf(True, "SLOWTEST skip")
  214. def test_sdb_kv2(self):
  215. set_output = self.run_function(
  216. "sdb.set", uri="sdb://sdbvault/secret/test/test_sdb/foo", value="bar"
  217. )
  218. self.assertEqual(set_output, True)
  219. get_output = self.run_function(
  220. "sdb.get", arg=["sdb://sdbvault/secret/test/test_sdb/foo"]
  221. )
  222. self.assertEqual(get_output, "bar")
  223. @flaky
  224. @skipIf(True, "SLOWTEST skip")
  225. def test_sdb_runner_kv2(self):
  226. set_output = self.run_run(
  227. "sdb.set sdb://sdbvault/secret/test/test_sdb_runner/foo bar"
  228. )
  229. self.assertEqual(set_output, ["True"])
  230. get_output = self.run_run(
  231. "sdb.get sdb://sdbvault/secret/test/test_sdb_runner/foo"
  232. )
  233. self.assertEqual(get_output, ["bar"])
  234. @flaky
  235. @skipIf(True, "SLOWTEST skip")
  236. def test_config_kv2(self):
  237. set_output = self.run_function(
  238. "sdb.set", uri="sdb://sdbvault/secret/test/test_pillar_sdb/foo", value="bar"
  239. )
  240. self.assertEqual(set_output, True)
  241. get_output = self.run_function("config.get", arg=["test_vault_pillar_sdb"])
  242. self.assertEqual(get_output, "bar")