test_vault.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. # -*- coding: utf-8 -*-
  2. """
  3. Test case for the vault execution module
  4. """
  5. # Import python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import salt.modules.vault as vault
  8. # Import Salt libs
  9. from salt.exceptions import CommandExecutionError
  10. from tests.support.mixins import LoaderModuleMockMixin
  11. from tests.support.mock import MagicMock, patch
  12. # Import Salt Testing libs
  13. from tests.support.unit import TestCase
  14. class TestVaultModule(LoaderModuleMockMixin, TestCase):
  15. """
  16. Test case for the vault execution module
  17. """
  18. def setup_loader_modules(self):
  19. return {
  20. vault: {
  21. "__opts__": {
  22. "vault": {
  23. "url": "http://127.0.0.1",
  24. "auth": {"token": "test", "method": "token"},
  25. }
  26. },
  27. "__grains__": {"id": "test-minion"},
  28. }
  29. }
  30. def test_read_secret_v1(self):
  31. """
  32. Test salt.modules.vault.read_secret function
  33. """
  34. version = {"v2": False, "data": None, "metadata": None, "type": None}
  35. mock_version = MagicMock(return_value=version)
  36. mock_vault = MagicMock()
  37. mock_vault.return_value.status_code = 200
  38. mock_vault.return_value.json.return_value = {"data": {"key": "test"}}
  39. with patch.dict(
  40. vault.__utils__, {"vault.make_request": mock_vault}
  41. ), patch.dict(vault.__utils__, {"vault.is_v2": mock_version}):
  42. vault_return = vault.read_secret("/secret/my/secret")
  43. self.assertDictEqual(vault_return, {"key": "test"})
  44. def test_read_secret_v1_key(self):
  45. """
  46. Test salt.modules.vault.read_secret function specifying key
  47. """
  48. version = {"v2": False, "data": None, "metadata": None, "type": None}
  49. mock_version = MagicMock(return_value=version)
  50. mock_vault = MagicMock()
  51. mock_vault.return_value.status_code = 200
  52. mock_vault.return_value.json.return_value = {"data": {"key": "somevalue"}}
  53. with patch.dict(
  54. vault.__utils__, {"vault.make_request": mock_vault}
  55. ), patch.dict(vault.__utils__, {"vault.is_v2": mock_version}):
  56. vault_return = vault.read_secret("/secret/my/secret", "key")
  57. self.assertEqual(vault_return, "somevalue")
  58. def test_read_secret_v2(self):
  59. """
  60. Test salt.modules.vault.read_secret function for v2 of kv secret backend
  61. """
  62. # given path secrets/mysecret generate v2 output
  63. version = {
  64. "v2": True,
  65. "data": "secrets/data/mysecret",
  66. "metadata": "secrets/metadata/mysecret",
  67. "type": "kv",
  68. }
  69. mock_version = MagicMock(return_value=version)
  70. mock_vault = MagicMock()
  71. mock_vault.return_value.status_code = 200
  72. v2_return = {
  73. "data": {
  74. "data": {"akey": "avalue"},
  75. "metadata": {
  76. "created_time": "2018-10-23T20:21:55.042755098Z",
  77. "destroyed": False,
  78. "version": 13,
  79. "deletion_time": "",
  80. },
  81. }
  82. }
  83. mock_vault.return_value.json.return_value = v2_return
  84. with patch.dict(
  85. vault.__utils__, {"vault.make_request": mock_vault}
  86. ), patch.dict(vault.__utils__, {"vault.is_v2": mock_version}):
  87. # Validate metadata returned
  88. vault_return = vault.read_secret("/secret/my/secret", metadata=True)
  89. self.assertDictContainsSubset({"data": {"akey": "avalue"}}, vault_return)
  90. # Validate just data returned
  91. vault_return = vault.read_secret("/secret/my/secret")
  92. self.assertDictContainsSubset({"akey": "avalue"}, vault_return)
  93. def test_read_secret_v2_key(self):
  94. """
  95. Test salt.modules.vault.read_secret function for v2 of kv secret backend
  96. with specified key
  97. """
  98. # given path secrets/mysecret generate v2 output
  99. version = {
  100. "v2": True,
  101. "data": "secrets/data/mysecret",
  102. "metadata": "secrets/metadata/mysecret",
  103. "type": "kv",
  104. }
  105. mock_version = MagicMock(return_value=version)
  106. mock_vault = MagicMock()
  107. mock_vault.return_value.status_code = 200
  108. v2_return = {
  109. "data": {
  110. "data": {"akey": "avalue"},
  111. "metadata": {
  112. "created_time": "2018-10-23T20:21:55.042755098Z",
  113. "destroyed": False,
  114. "version": 13,
  115. "deletion_time": "",
  116. },
  117. }
  118. }
  119. mock_vault.return_value.json.return_value = v2_return
  120. with patch.dict(
  121. vault.__utils__, {"vault.make_request": mock_vault}
  122. ), patch.dict(vault.__utils__, {"vault.is_v2": mock_version}):
  123. vault_return = vault.read_secret("/secret/my/secret", "akey")
  124. self.assertEqual(vault_return, "avalue")
  125. class VaultDefaultTestCase(TestCase, LoaderModuleMockMixin):
  126. """
  127. Test cases for the default argument in the vault module
  128. NOTE: This test class is crafted such that the vault.make_request call will
  129. always fail. If you want to add other unit tests, you should put them in a
  130. separate class.
  131. """
  132. def setup_loader_modules(self):
  133. return {
  134. vault: {
  135. "__grains__": {"id": "foo"},
  136. "__utils__": {
  137. "vault.make_request": MagicMock(side_effect=Exception("FAILED")),
  138. "vault.is_v2": MagicMock(
  139. return_value={
  140. "v2": True,
  141. "data": "secrets/data/mysecret",
  142. "metadata": "secrets/metadata/mysecret",
  143. "type": "kv",
  144. }
  145. ),
  146. },
  147. },
  148. }
  149. def setUp(self):
  150. self.path = "foo/bar/"
  151. def test_read_secret_with_default(self):
  152. assert vault.read_secret(self.path, default="baz") == "baz"
  153. def test_read_secret_no_default(self):
  154. try:
  155. vault.read_secret(self.path)
  156. except CommandExecutionError:
  157. # This is expected
  158. pass
  159. else:
  160. raise Exception("Should have raised a CommandExecutionError")
  161. def test_list_secrets_with_default(self):
  162. assert vault.list_secrets(self.path, default=["baz"]) == ["baz"]
  163. def test_list_secrets_no_default(self):
  164. try:
  165. vault.list_secrets(self.path)
  166. except CommandExecutionError:
  167. # This is expected
  168. pass
  169. else:
  170. raise Exception("Should have raised a CommandExecutionError")