test_master.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import
  4. # Import Salt libs
  5. import salt.config
  6. import salt.master
  7. # Import Salt Testing Libs
  8. from tests.support.unit import TestCase
  9. from tests.support.unit import skipIf, WAR_ROOM_SKIP # WAR ROOM temp import
  10. from tests.support.mock import (
  11. patch,
  12. MagicMock,
  13. )
  14. @skipIf(WAR_ROOM_SKIP, 'WAR ROOM TEMPORARY SKIP')
  15. class ClearFuncsTestCase(TestCase):
  16. '''
  17. TestCase for salt.master.ClearFuncs class
  18. '''
  19. def setUp(self):
  20. opts = salt.config.master_config(None)
  21. self.clear_funcs = salt.master.ClearFuncs(opts, {})
  22. # runner tests
  23. def test_runner_token_not_authenticated(self):
  24. '''
  25. Asserts that a TokenAuthenticationError is returned when the token can't authenticate.
  26. '''
  27. mock_ret = {'error': {'name': 'TokenAuthenticationError',
  28. 'message': 'Authentication failure of type "token" occurred.'}}
  29. ret = self.clear_funcs.runner({'token': 'asdfasdfasdfasdf'})
  30. self.assertDictEqual(mock_ret, ret)
  31. def test_runner_token_authorization_error(self):
  32. '''
  33. Asserts that a TokenAuthenticationError is returned when the token authenticates, but is
  34. not authorized.
  35. '''
  36. token = 'asdfasdfasdfasdf'
  37. clear_load = {'token': token, 'fun': 'test.arg'}
  38. mock_token = {'token': token, 'eauth': 'foo', 'name': 'test'}
  39. mock_ret = {'error': {'name': 'TokenAuthenticationError',
  40. 'message': 'Authentication failure of type "token" occurred '
  41. 'for user test.'}}
  42. with patch('salt.auth.LoadAuth.authenticate_token', MagicMock(return_value=mock_token)), \
  43. patch('salt.auth.LoadAuth.get_auth_list', MagicMock(return_value=[])):
  44. ret = self.clear_funcs.runner(clear_load)
  45. self.assertDictEqual(mock_ret, ret)
  46. def test_runner_token_salt_invocation_error(self):
  47. '''
  48. Asserts that a SaltInvocationError is returned when the token authenticates, but the
  49. command is malformed.
  50. '''
  51. token = 'asdfasdfasdfasdf'
  52. clear_load = {'token': token, 'fun': 'badtestarg'}
  53. mock_token = {'token': token, 'eauth': 'foo', 'name': 'test'}
  54. mock_ret = {'error': {'name': 'SaltInvocationError',
  55. 'message': 'A command invocation error occurred: Check syntax.'}}
  56. with patch('salt.auth.LoadAuth.authenticate_token', MagicMock(return_value=mock_token)), \
  57. patch('salt.auth.LoadAuth.get_auth_list', MagicMock(return_value=['testing'])):
  58. ret = self.clear_funcs.runner(clear_load)
  59. self.assertDictEqual(mock_ret, ret)
  60. def test_runner_eauth_not_authenticated(self):
  61. '''
  62. Asserts that an EauthAuthenticationError is returned when the user can't authenticate.
  63. '''
  64. mock_ret = {'error': {'name': 'EauthAuthenticationError',
  65. 'message': 'Authentication failure of type "eauth" occurred for '
  66. 'user UNKNOWN.'}}
  67. ret = self.clear_funcs.runner({'eauth': 'foo'})
  68. self.assertDictEqual(mock_ret, ret)
  69. def test_runner_eauth_authorization_error(self):
  70. '''
  71. Asserts that an EauthAuthenticationError is returned when the user authenticates, but is
  72. not authorized.
  73. '''
  74. clear_load = {'eauth': 'foo', 'username': 'test', 'fun': 'test.arg'}
  75. mock_ret = {'error': {'name': 'EauthAuthenticationError',
  76. 'message': 'Authentication failure of type "eauth" occurred for '
  77. 'user test.'}}
  78. with patch('salt.auth.LoadAuth.authenticate_eauth', MagicMock(return_value=True)), \
  79. patch('salt.auth.LoadAuth.get_auth_list', MagicMock(return_value=[])):
  80. ret = self.clear_funcs.runner(clear_load)
  81. self.assertDictEqual(mock_ret, ret)
  82. def test_runner_eauth_salt_invocation_error(self):
  83. '''
  84. Asserts that an EauthAuthenticationError is returned when the user authenticates, but the
  85. command is malformed.
  86. '''
  87. clear_load = {'eauth': 'foo', 'username': 'test', 'fun': 'bad.test.arg.func'}
  88. mock_ret = {'error': {'name': 'SaltInvocationError',
  89. 'message': 'A command invocation error occurred: Check syntax.'}}
  90. with patch('salt.auth.LoadAuth.authenticate_eauth', MagicMock(return_value=True)), \
  91. patch('salt.auth.LoadAuth.get_auth_list', MagicMock(return_value=['testing'])):
  92. ret = self.clear_funcs.runner(clear_load)
  93. self.assertDictEqual(mock_ret, ret)
  94. def test_runner_user_not_authenticated(self):
  95. '''
  96. Asserts that an UserAuthenticationError is returned when the user can't authenticate.
  97. '''
  98. mock_ret = {'error': {'name': 'UserAuthenticationError',
  99. 'message': 'Authentication failure of type "user" occurred'}}
  100. ret = self.clear_funcs.runner({})
  101. self.assertDictEqual(mock_ret, ret)
  102. # wheel tests
  103. def test_wheel_token_not_authenticated(self):
  104. '''
  105. Asserts that a TokenAuthenticationError is returned when the token can't authenticate.
  106. '''
  107. mock_ret = {'error': {'name': 'TokenAuthenticationError',
  108. 'message': 'Authentication failure of type "token" occurred.'}}
  109. ret = self.clear_funcs.wheel({'token': 'asdfasdfasdfasdf'})
  110. self.assertDictEqual(mock_ret, ret)
  111. def test_wheel_token_authorization_error(self):
  112. '''
  113. Asserts that a TokenAuthenticationError is returned when the token authenticates, but is
  114. not authorized.
  115. '''
  116. token = 'asdfasdfasdfasdf'
  117. clear_load = {'token': token, 'fun': 'test.arg'}
  118. mock_token = {'token': token, 'eauth': 'foo', 'name': 'test'}
  119. mock_ret = {'error': {'name': 'TokenAuthenticationError',
  120. 'message': 'Authentication failure of type "token" occurred '
  121. 'for user test.'}}
  122. with patch('salt.auth.LoadAuth.authenticate_token', MagicMock(return_value=mock_token)), \
  123. patch('salt.auth.LoadAuth.get_auth_list', MagicMock(return_value=[])):
  124. ret = self.clear_funcs.wheel(clear_load)
  125. self.assertDictEqual(mock_ret, ret)
  126. def test_wheel_token_salt_invocation_error(self):
  127. '''
  128. Asserts that a SaltInvocationError is returned when the token authenticates, but the
  129. command is malformed.
  130. '''
  131. token = 'asdfasdfasdfasdf'
  132. clear_load = {'token': token, 'fun': 'badtestarg'}
  133. mock_token = {'token': token, 'eauth': 'foo', 'name': 'test'}
  134. mock_ret = {'error': {'name': 'SaltInvocationError',
  135. 'message': 'A command invocation error occurred: Check syntax.'}}
  136. with patch('salt.auth.LoadAuth.authenticate_token', MagicMock(return_value=mock_token)), \
  137. patch('salt.auth.LoadAuth.get_auth_list', MagicMock(return_value=['testing'])):
  138. ret = self.clear_funcs.wheel(clear_load)
  139. self.assertDictEqual(mock_ret, ret)
  140. def test_wheel_eauth_not_authenticated(self):
  141. '''
  142. Asserts that an EauthAuthenticationError is returned when the user can't authenticate.
  143. '''
  144. mock_ret = {'error': {'name': 'EauthAuthenticationError',
  145. 'message': 'Authentication failure of type "eauth" occurred for '
  146. 'user UNKNOWN.'}}
  147. ret = self.clear_funcs.wheel({'eauth': 'foo'})
  148. self.assertDictEqual(mock_ret, ret)
  149. def test_wheel_eauth_authorization_error(self):
  150. '''
  151. Asserts that an EauthAuthenticationError is returned when the user authenticates, but is
  152. not authorized.
  153. '''
  154. clear_load = {'eauth': 'foo', 'username': 'test', 'fun': 'test.arg'}
  155. mock_ret = {'error': {'name': 'EauthAuthenticationError',
  156. 'message': 'Authentication failure of type "eauth" occurred for '
  157. 'user test.'}}
  158. with patch('salt.auth.LoadAuth.authenticate_eauth', MagicMock(return_value=True)), \
  159. patch('salt.auth.LoadAuth.get_auth_list', MagicMock(return_value=[])):
  160. ret = self.clear_funcs.wheel(clear_load)
  161. self.assertDictEqual(mock_ret, ret)
  162. def test_wheel_eauth_salt_invocation_error(self):
  163. '''
  164. Asserts that an EauthAuthenticationError is returned when the user authenticates, but the
  165. command is malformed.
  166. '''
  167. clear_load = {'eauth': 'foo', 'username': 'test', 'fun': 'bad.test.arg.func'}
  168. mock_ret = {'error': {'name': 'SaltInvocationError',
  169. 'message': 'A command invocation error occurred: Check syntax.'}}
  170. with patch('salt.auth.LoadAuth.authenticate_eauth', MagicMock(return_value=True)), \
  171. patch('salt.auth.LoadAuth.get_auth_list', MagicMock(return_value=['testing'])):
  172. ret = self.clear_funcs.wheel(clear_load)
  173. self.assertDictEqual(mock_ret, ret)
  174. def test_wheel_user_not_authenticated(self):
  175. '''
  176. Asserts that an UserAuthenticationError is returned when the user can't authenticate.
  177. '''
  178. mock_ret = {'error': {'name': 'UserAuthenticationError',
  179. 'message': 'Authentication failure of type "user" occurred'}}
  180. ret = self.clear_funcs.wheel({})
  181. self.assertDictEqual(mock_ret, ret)
  182. # publish tests
  183. def test_publish_user_is_blacklisted(self):
  184. '''
  185. Asserts that an AuthorizationError is returned when the user has been blacklisted.
  186. '''
  187. mock_ret = {'error': {'name': 'AuthorizationError',
  188. 'message': 'Authorization error occurred.'}}
  189. with patch('salt.acl.PublisherACL.user_is_blacklisted', MagicMock(return_value=True)):
  190. self.assertEqual(mock_ret, self.clear_funcs.publish({'user': 'foo', 'fun': 'test.arg'}))
  191. def test_publish_cmd_blacklisted(self):
  192. '''
  193. Asserts that an AuthorizationError is returned when the command has been blacklisted.
  194. '''
  195. mock_ret = {'error': {'name': 'AuthorizationError',
  196. 'message': 'Authorization error occurred.'}}
  197. with patch('salt.acl.PublisherACL.user_is_blacklisted', MagicMock(return_value=False)), \
  198. patch('salt.acl.PublisherACL.cmd_is_blacklisted', MagicMock(return_value=True)):
  199. self.assertEqual(mock_ret, self.clear_funcs.publish({'user': 'foo', 'fun': 'test.arg'}))
  200. def test_publish_token_not_authenticated(self):
  201. '''
  202. Asserts that an AuthenticationError is returned when the token can't authenticate.
  203. '''
  204. mock_ret = {'error': {'name': 'AuthenticationError',
  205. 'message': 'Authentication error occurred.'}}
  206. load = {'user': 'foo', 'fun': 'test.arg', 'tgt': 'test_minion',
  207. 'kwargs': {'token': 'asdfasdfasdfasdf'}}
  208. with patch('salt.acl.PublisherACL.user_is_blacklisted', MagicMock(return_value=False)), \
  209. patch('salt.acl.PublisherACL.cmd_is_blacklisted', MagicMock(return_value=False)):
  210. self.assertEqual(mock_ret, self.clear_funcs.publish(load))
  211. def test_publish_token_authorization_error(self):
  212. '''
  213. Asserts that an AuthorizationError is returned when the token authenticates, but is not
  214. authorized.
  215. '''
  216. token = 'asdfasdfasdfasdf'
  217. load = {'user': 'foo', 'fun': 'test.arg', 'tgt': 'test_minion',
  218. 'arg': 'bar', 'kwargs': {'token': token}}
  219. mock_token = {'token': token, 'eauth': 'foo', 'name': 'test'}
  220. mock_ret = {'error': {'name': 'AuthorizationError',
  221. 'message': 'Authorization error occurred.'}}
  222. with patch('salt.acl.PublisherACL.user_is_blacklisted', MagicMock(return_value=False)), \
  223. patch('salt.acl.PublisherACL.cmd_is_blacklisted', MagicMock(return_value=False)), \
  224. patch('salt.auth.LoadAuth.authenticate_token', MagicMock(return_value=mock_token)), \
  225. patch('salt.auth.LoadAuth.get_auth_list', MagicMock(return_value=[])):
  226. self.assertEqual(mock_ret, self.clear_funcs.publish(load))
  227. def test_publish_eauth_not_authenticated(self):
  228. '''
  229. Asserts that an AuthenticationError is returned when the user can't authenticate.
  230. '''
  231. load = {'user': 'test', 'fun': 'test.arg', 'tgt': 'test_minion',
  232. 'kwargs': {'eauth': 'foo'}}
  233. mock_ret = {'error': {'name': 'AuthenticationError',
  234. 'message': 'Authentication error occurred.'}}
  235. with patch('salt.acl.PublisherACL.user_is_blacklisted', MagicMock(return_value=False)), \
  236. patch('salt.acl.PublisherACL.cmd_is_blacklisted', MagicMock(return_value=False)):
  237. self.assertEqual(mock_ret, self.clear_funcs.publish(load))
  238. def test_publish_eauth_authorization_error(self):
  239. '''
  240. Asserts that an AuthorizationError is returned when the user authenticates, but is not
  241. authorized.
  242. '''
  243. load = {'user': 'test', 'fun': 'test.arg', 'tgt': 'test_minion',
  244. 'kwargs': {'eauth': 'foo'}, 'arg': 'bar'}
  245. mock_ret = {'error': {'name': 'AuthorizationError',
  246. 'message': 'Authorization error occurred.'}}
  247. with patch('salt.acl.PublisherACL.user_is_blacklisted', MagicMock(return_value=False)), \
  248. patch('salt.acl.PublisherACL.cmd_is_blacklisted', MagicMock(return_value=False)), \
  249. patch('salt.auth.LoadAuth.authenticate_eauth', MagicMock(return_value=True)), \
  250. patch('salt.auth.LoadAuth.get_auth_list', MagicMock(return_value=[])):
  251. self.assertEqual(mock_ret, self.clear_funcs.publish(load))
  252. def test_publish_user_not_authenticated(self):
  253. '''
  254. Asserts that an AuthenticationError is returned when the user can't authenticate.
  255. '''
  256. load = {'user': 'test', 'fun': 'test.arg', 'tgt': 'test_minion'}
  257. mock_ret = {'error': {'name': 'AuthenticationError',
  258. 'message': 'Authentication error occurred.'}}
  259. with patch('salt.acl.PublisherACL.user_is_blacklisted', MagicMock(return_value=False)), \
  260. patch('salt.acl.PublisherACL.cmd_is_blacklisted', MagicMock(return_value=False)):
  261. self.assertEqual(mock_ret, self.clear_funcs.publish(load))
  262. def test_publish_user_authenticated_missing_auth_list(self):
  263. '''
  264. Asserts that an AuthenticationError is returned when the user has an effective user id and is
  265. authenticated, but the auth_list is empty.
  266. '''
  267. load = {'user': 'test', 'fun': 'test.arg', 'tgt': 'test_minion',
  268. 'kwargs': {'user': 'test'}, 'arg': 'foo'}
  269. mock_ret = {'error': {'name': 'AuthenticationError',
  270. 'message': 'Authentication error occurred.'}}
  271. with patch('salt.acl.PublisherACL.user_is_blacklisted', MagicMock(return_value=False)), \
  272. patch('salt.acl.PublisherACL.cmd_is_blacklisted', MagicMock(return_value=False)), \
  273. patch('salt.auth.LoadAuth.authenticate_key', MagicMock(return_value='fake-user-key')), \
  274. patch('salt.utils.master.get_values_of_matching_keys', MagicMock(return_value=[])):
  275. self.assertEqual(mock_ret, self.clear_funcs.publish(load))
  276. def test_publish_user_authorization_error(self):
  277. '''
  278. Asserts that an AuthorizationError is returned when the user authenticates, but is not
  279. authorized.
  280. '''
  281. load = {'user': 'test', 'fun': 'test.arg', 'tgt': 'test_minion',
  282. 'kwargs': {'user': 'test'}, 'arg': 'foo'}
  283. mock_ret = {'error': {'name': 'AuthorizationError',
  284. 'message': 'Authorization error occurred.'}}
  285. with patch('salt.acl.PublisherACL.user_is_blacklisted', MagicMock(return_value=False)), \
  286. patch('salt.acl.PublisherACL.cmd_is_blacklisted', MagicMock(return_value=False)), \
  287. patch('salt.auth.LoadAuth.authenticate_key', MagicMock(return_value='fake-user-key')), \
  288. patch('salt.utils.master.get_values_of_matching_keys', MagicMock(return_value=['test'])), \
  289. patch('salt.utils.minions.CkMinions.auth_check', MagicMock(return_value=False)):
  290. self.assertEqual(mock_ret, self.clear_funcs.publish(load))