1
0

test_master.py 16 KB

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