1
0

test_vault.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. # -*- coding: utf-8 -*-
  2. """
  3. Integration tests for the vault execution module
  4. """
  5. from __future__ import absolute_import, print_function, unicode_literals
  6. import logging
  7. import time
  8. import salt.utils.path
  9. from tests.support.case import ModuleCase
  10. from tests.support.helpers import destructiveTest, slowTest
  11. from tests.support.runtests import RUNTIME_VARS
  12. from tests.support.sminion import create_sminion
  13. from tests.support.unit import SkipTest, skipIf
  14. log = logging.getLogger(__name__)
  15. VAULT_BINARY_PATH = salt.utils.path.which("vault")
  16. @destructiveTest
  17. @skipIf(not salt.utils.path.which("dockerd"), "Docker not installed")
  18. @skipIf(not VAULT_BINARY_PATH, "Vault not installed")
  19. class VaultTestCase(ModuleCase):
  20. """
  21. Test vault module
  22. """
  23. @classmethod
  24. def setUpClass(cls):
  25. cls.sminion = sminion = create_sminion()
  26. config = '{"backend": {"file": {"path": "/vault/file"}}, "default_lease_ttl": "168h", "max_lease_ttl": "720h", "disable_mlock": true}'
  27. sminion.states.docker_image.present(name="vault", tag="0.9.6")
  28. login_attempts = 1
  29. container_created = False
  30. while True:
  31. if container_created:
  32. sminion.states.docker_container.stopped(name="vault")
  33. sminion.states.docker_container.absent(name="vault")
  34. ret = sminion.states.docker_container.running(
  35. name="vault",
  36. image="vault:0.9.6",
  37. port_bindings="8200:8200",
  38. environment={
  39. "VAULT_DEV_ROOT_TOKEN_ID": "testsecret",
  40. "VAULT_LOCAL_CONFIG": config,
  41. },
  42. )
  43. log.debug("docker_container.running return: %s", ret)
  44. container_created = ret["result"]
  45. time.sleep(5)
  46. ret = sminion.functions.cmd.run_all(
  47. cmd="{} login token=testsecret".format(VAULT_BINARY_PATH),
  48. env={"VAULT_ADDR": "http://127.0.0.1:8200"},
  49. hide_output=False,
  50. )
  51. if ret["retcode"] == 0:
  52. break
  53. log.debug("Vault login failed. Return: %s", ret)
  54. login_attempts += 1
  55. if login_attempts >= 3:
  56. raise SkipTest("unable to login to vault")
  57. ret = sminion.functions.cmd.retcode(
  58. cmd="{} policy write testpolicy {}/vault.hcl".format(
  59. VAULT_BINARY_PATH, RUNTIME_VARS.FILES
  60. ),
  61. env={"VAULT_ADDR": "http://127.0.0.1:8200"},
  62. )
  63. if ret != 0:
  64. raise SkipTest("unable to assign policy to vault")
  65. @classmethod
  66. def tearDownClass(cls):
  67. cls.sminion.states.docker_container.stopped(name="vault")
  68. cls.sminion.states.docker_container.absent(name="vault")
  69. cls.sminion.states.docker_image.absent(name="vault", force=True)
  70. cls.sminion = None
  71. @slowTest
  72. def test_write_read_secret(self):
  73. write_return = self.run_function(
  74. "vault.write_secret", path="secret/my/secret", user="foo", password="bar"
  75. )
  76. self.assertEqual(write_return, True)
  77. assert self.run_function("vault.read_secret", arg=["secret/my/secret"]) == {
  78. "password": "bar",
  79. "user": "foo",
  80. }
  81. assert (
  82. self.run_function("vault.read_secret", arg=["secret/my/secret", "user"])
  83. == "foo"
  84. )
  85. @slowTest
  86. def test_write_raw_read_secret(self):
  87. assert (
  88. self.run_function(
  89. "vault.write_raw",
  90. path="secret/my/secret2",
  91. raw={"user2": "foo2", "password2": "bar2"},
  92. )
  93. is True
  94. )
  95. assert self.run_function("vault.read_secret", arg=["secret/my/secret2"]) == {
  96. "password2": "bar2",
  97. "user2": "foo2",
  98. }
  99. @slowTest
  100. def test_delete_secret(self):
  101. assert (
  102. self.run_function(
  103. "vault.write_secret",
  104. path="secret/my/secret",
  105. user="foo",
  106. password="bar",
  107. )
  108. is True
  109. )
  110. assert (
  111. self.run_function("vault.delete_secret", arg=["secret/my/secret"]) is True
  112. )
  113. @slowTest
  114. def test_list_secrets(self):
  115. assert (
  116. self.run_function(
  117. "vault.write_secret",
  118. path="secret/my/secret",
  119. user="foo",
  120. password="bar",
  121. )
  122. is True
  123. )
  124. assert self.run_function("vault.list_secrets", arg=["secret/my/"]) == {
  125. "keys": ["secret"]
  126. }
  127. @destructiveTest
  128. @skipIf(not salt.utils.path.which("dockerd"), "Docker not installed")
  129. @skipIf(not salt.utils.path.which("vault"), "Vault not installed")
  130. class VaultTestCaseCurrent(ModuleCase):
  131. """
  132. Test vault module against current vault
  133. """
  134. @classmethod
  135. def setUpClass(cls):
  136. cls.sminion = sminion = create_sminion()
  137. config = '{"backend": {"file": {"path": "/vault/file"}}, "default_lease_ttl": "168h", "max_lease_ttl": "720h", "disable_mlock": true}'
  138. sminion.states.docker_image.present(name="vault", tag="1.3.1")
  139. login_attempts = 1
  140. container_created = False
  141. while True:
  142. if container_created:
  143. sminion.states.docker_container.stopped(name="vault")
  144. sminion.states.docker_container.absent(name="vault")
  145. ret = sminion.states.docker_container.running(
  146. name="vault",
  147. image="vault:1.3.1",
  148. port_bindings="8200:8200",
  149. environment={
  150. "VAULT_DEV_ROOT_TOKEN_ID": "testsecret",
  151. "VAULT_LOCAL_CONFIG": config,
  152. },
  153. )
  154. log.debug("docker_container.running return: %s", ret)
  155. container_created = ret["result"]
  156. time.sleep(5)
  157. ret = sminion.functions.cmd.run_all(
  158. cmd="{} login token=testsecret".format(VAULT_BINARY_PATH),
  159. env={"VAULT_ADDR": "http://127.0.0.1:8200"},
  160. hide_output=False,
  161. )
  162. if ret["retcode"] == 0:
  163. break
  164. log.debug("Vault login failed. Return: %s", ret)
  165. login_attempts += 1
  166. if login_attempts >= 3:
  167. raise SkipTest("unable to login to vault")
  168. ret = sminion.functions.cmd.retcode(
  169. cmd="{} policy write testpolicy {}/vault.hcl".format(
  170. VAULT_BINARY_PATH, RUNTIME_VARS.FILES
  171. ),
  172. env={"VAULT_ADDR": "http://127.0.0.1:8200"},
  173. )
  174. if ret != 0:
  175. raise SkipTest("unable to assign policy to vault")
  176. @classmethod
  177. def tearDownClass(cls):
  178. cls.sminion.states.docker_container.stopped(name="vault")
  179. cls.sminion.states.docker_container.absent(name="vault")
  180. cls.sminion.states.docker_image.absent(name="vault", force=True)
  181. cls.sminion = None
  182. @slowTest
  183. def test_write_read_secret_kv2(self):
  184. write_return = self.run_function(
  185. "vault.write_secret", path="secret/my/secret", user="foo", password="bar"
  186. )
  187. # write_secret output:
  188. # {'created_time': '2020-01-12T23:09:34.571294241Z', 'destroyed': False,
  189. # 'version': 1, 'deletion_time': ''}
  190. expected_write = {"destroyed": False, "deletion_time": ""}
  191. self.assertDictContainsSubset(expected_write, write_return)
  192. read_return = self.run_function(
  193. "vault.read_secret", arg=["secret/my/secret"], metadata=True
  194. )
  195. # read_secret output:
  196. # {'data': {'password': 'bar', 'user': 'foo'},
  197. # 'metadata': {'created_time': '2020-01-12T23:07:18.829326918Z', 'destroyed': False,
  198. # 'version': 1, 'deletion_time': ''}}
  199. expected_read = {"data": {"password": "bar", "user": "foo"}}
  200. self.assertDictContainsSubset(expected_read, read_return)
  201. expected_read = {"password": "bar", "user": "foo"}
  202. read_return = self.run_function("vault.read_secret", arg=["secret/my/secret"])
  203. self.assertDictContainsSubset(expected_read, read_return)
  204. read_return = self.run_function(
  205. "vault.read_secret", arg=["secret/my/secret", "user"]
  206. )
  207. self.assertEqual(read_return, "foo")
  208. @slowTest
  209. def test_list_secrets_kv2(self):
  210. write_return = self.run_function(
  211. "vault.write_secret", path="secret/my/secret", user="foo", password="bar"
  212. )
  213. expected_write = {"destroyed": False, "deletion_time": ""}
  214. self.assertDictContainsSubset(expected_write, write_return)
  215. list_return = self.run_function("vault.list_secrets", arg=["secret/my/"])
  216. self.assertIn("secret", list_return["keys"])
  217. @slowTest
  218. def test_write_raw_read_secret_kv2(self):
  219. write_return = self.run_function(
  220. "vault.write_raw",
  221. path="secret/my/secret2",
  222. raw={"user2": "foo2", "password2": "bar2"},
  223. )
  224. expected_write = {"destroyed": False, "deletion_time": ""}
  225. self.assertDictContainsSubset(expected_write, write_return)
  226. read_return = self.run_function(
  227. "vault.read_secret", arg=["secret/my/secret2"], metadata=True
  228. )
  229. expected_read = {"data": {"password2": "bar2", "user2": "foo2"}}
  230. self.assertDictContainsSubset(expected_read, read_return)
  231. read_return = self.run_function("vault.read_secret", arg=["secret/my/secret2"])
  232. expected_read = {"password2": "bar2", "user2": "foo2"}
  233. self.assertDictContainsSubset(expected_read, read_return)
  234. @slowTest
  235. def test_delete_secret_kv2(self):
  236. write_return = self.run_function(
  237. "vault.write_secret",
  238. path="secret/my/secret3",
  239. user3="foo3",
  240. password3="bar3",
  241. )
  242. expected_write = {"destroyed": False, "deletion_time": ""}
  243. self.assertDictContainsSubset(expected_write, write_return)
  244. delete_return = self.run_function(
  245. "vault.delete_secret", arg=["secret/my/secret3"]
  246. )
  247. self.assertEqual(delete_return, True)
  248. @slowTest
  249. def test_destroy_secret_kv2(self):
  250. write_return = self.run_function(
  251. "vault.write_secret",
  252. path="secret/my/secret4",
  253. user3="foo4",
  254. password4="bar4",
  255. )
  256. expected_write = {"destroyed": False, "deletion_time": ""}
  257. self.assertDictContainsSubset(expected_write, write_return)
  258. destroy_return = self.run_function(
  259. "vault.destroy_secret", arg=["secret/my/secret4", "1"]
  260. )
  261. self.assertEqual(destroy_return, True)
  262. # self.assertIsNone(self.run_function('vault.read_secret', arg=['secret/my/secret4']))
  263. # list_return = self.run_function('vault.list_secrets', arg=['secret/my/'])
  264. # self.assertNotIn('secret4', list_return['keys'])