1
0

test_vault.py 9.5 KB

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