test_auth.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Mike Place <mp@saltstack.com>
  4. '''
  5. # Import pytohn libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import time
  8. # Import Salt Testing libs
  9. from tests.support.unit import TestCase, skipIf
  10. from tests.support.mock import patch, call, MagicMock
  11. # Import Salt libraries
  12. import salt.master
  13. from tests.support.case import ModuleCase
  14. from salt import auth
  15. from salt.exceptions import SaltDeserializationError
  16. import salt.utils.platform
  17. class LoadAuthTestCase(TestCase):
  18. def setUp(self): # pylint: disable=W0221
  19. patches = (
  20. ('salt.payload.Serial', None),
  21. ('salt.loader.auth', dict(return_value={'pam.auth': 'fake_func_str', 'pam.groups': 'fake_groups_function_str'})),
  22. ('salt.loader.eauth_tokens', dict(return_value={'localfs.mk_token': 'fake_func_mktok',
  23. 'localfs.get_token': 'fake_func_gettok',
  24. 'localfs.rm_roken': 'fake_func_rmtok'}))
  25. )
  26. for mod, mock in patches:
  27. if mock:
  28. patcher = patch(mod, **mock)
  29. else:
  30. patcher = patch(mod)
  31. patcher.start()
  32. self.addCleanup(patcher.stop)
  33. self.lauth = auth.LoadAuth({}) # Load with empty opts
  34. def test_get_tok_with_broken_file_will_remove_bad_token(self):
  35. fake_get_token = MagicMock(side_effect=SaltDeserializationError('hi'))
  36. patch_opts = patch.dict(self.lauth.opts, {'eauth_tokens': 'testfs'})
  37. patch_get_token = patch.dict(
  38. self.lauth.tokens,
  39. {
  40. 'testfs.get_token': fake_get_token
  41. },
  42. )
  43. mock_rm_token = MagicMock()
  44. patch_rm_token = patch.object(self.lauth, 'rm_token', mock_rm_token)
  45. with patch_opts, patch_get_token, patch_rm_token:
  46. expected_token = 'fnord'
  47. self.lauth.get_tok(expected_token)
  48. mock_rm_token.assert_called_with(expected_token)
  49. def test_get_tok_with_no_expiration_should_remove_bad_token(self):
  50. fake_get_token = MagicMock(return_value={'no_expire_here': 'Nope'})
  51. patch_opts = patch.dict(self.lauth.opts, {'eauth_tokens': 'testfs'})
  52. patch_get_token = patch.dict(
  53. self.lauth.tokens,
  54. {
  55. 'testfs.get_token': fake_get_token
  56. },
  57. )
  58. mock_rm_token = MagicMock()
  59. patch_rm_token = patch.object(self.lauth, 'rm_token', mock_rm_token)
  60. with patch_opts, patch_get_token, patch_rm_token:
  61. expected_token = 'fnord'
  62. self.lauth.get_tok(expected_token)
  63. mock_rm_token.assert_called_with(expected_token)
  64. def test_get_tok_with_expire_before_current_time_should_remove_token(self):
  65. fake_get_token = MagicMock(return_value={'expire': time.time()-1})
  66. patch_opts = patch.dict(self.lauth.opts, {'eauth_tokens': 'testfs'})
  67. patch_get_token = patch.dict(
  68. self.lauth.tokens,
  69. {
  70. 'testfs.get_token': fake_get_token
  71. },
  72. )
  73. mock_rm_token = MagicMock()
  74. patch_rm_token = patch.object(self.lauth, 'rm_token', mock_rm_token)
  75. with patch_opts, patch_get_token, patch_rm_token:
  76. expected_token = 'fnord'
  77. self.lauth.get_tok(expected_token)
  78. mock_rm_token.assert_called_with(expected_token)
  79. def test_get_tok_with_valid_expiration_should_return_token(self):
  80. expected_token = {'expire': time.time()+1}
  81. fake_get_token = MagicMock(return_value=expected_token)
  82. patch_opts = patch.dict(self.lauth.opts, {'eauth_tokens': 'testfs'})
  83. patch_get_token = patch.dict(
  84. self.lauth.tokens,
  85. {
  86. 'testfs.get_token': fake_get_token
  87. },
  88. )
  89. mock_rm_token = MagicMock()
  90. patch_rm_token = patch.object(self.lauth, 'rm_token', mock_rm_token)
  91. with patch_opts, patch_get_token, patch_rm_token:
  92. token_name = 'fnord'
  93. actual_token = self.lauth.get_tok(token_name)
  94. mock_rm_token.assert_not_called()
  95. assert expected_token is actual_token, 'Token was not returned'
  96. def test_load_name(self):
  97. valid_eauth_load = {'username': 'test_user',
  98. 'show_timeout': False,
  99. 'test_password': '',
  100. 'eauth': 'pam'}
  101. # Test a case where the loader auth doesn't have the auth type
  102. without_auth_type = dict(valid_eauth_load)
  103. without_auth_type.pop('eauth')
  104. ret = self.lauth.load_name(without_auth_type)
  105. self.assertEqual(ret, '', "Did not bail when the auth loader didn't have the auth type.")
  106. # Test a case with valid params
  107. with patch('salt.utils.args.arg_lookup',
  108. MagicMock(return_value={'args': ['username', 'password']})) as format_call_mock:
  109. expected_ret = call('fake_func_str')
  110. ret = self.lauth.load_name(valid_eauth_load)
  111. format_call_mock.assert_has_calls((expected_ret,), any_order=True)
  112. self.assertEqual(ret, 'test_user')
  113. def test_get_groups(self):
  114. valid_eauth_load = {'username': 'test_user',
  115. 'show_timeout': False,
  116. 'test_password': '',
  117. 'eauth': 'pam'}
  118. with patch('salt.utils.args.format_call') as format_call_mock:
  119. expected_ret = call('fake_groups_function_str', {
  120. 'username': 'test_user',
  121. 'test_password': '',
  122. 'show_timeout': False,
  123. 'eauth': 'pam'
  124. }, expected_extra_kws=auth.AUTH_INTERNAL_KEYWORDS)
  125. self.lauth.get_groups(valid_eauth_load)
  126. format_call_mock.assert_has_calls((expected_ret,), any_order=True)
  127. class MasterACLTestCase(ModuleCase):
  128. '''
  129. A class to check various aspects of the publisher ACL system
  130. '''
  131. def setUp(self):
  132. self.fire_event_mock = MagicMock(return_value='dummy_tag')
  133. self.addCleanup(delattr, self, 'fire_event_mock')
  134. opts = self.get_temp_config('master')
  135. patches = (
  136. ('zmq.Context', MagicMock()),
  137. ('salt.payload.Serial.dumps', MagicMock()),
  138. ('salt.master.tagify', MagicMock()),
  139. ('salt.utils.event.SaltEvent.fire_event', self.fire_event_mock),
  140. ('salt.auth.LoadAuth.time_auth', MagicMock(return_value=True)),
  141. ('salt.minion.MasterMinion', MagicMock()),
  142. ('salt.utils.verify.check_path_traversal', MagicMock()),
  143. ('salt.client.get_local_client', MagicMock(return_value=opts['conf_file'])),
  144. )
  145. for mod, mock in patches:
  146. patcher = patch(mod, mock)
  147. patcher.start()
  148. self.addCleanup(patcher.stop)
  149. opts['publisher_acl'] = {}
  150. opts['publisher_acl_blacklist'] = {}
  151. opts['master_job_cache'] = ''
  152. opts['sign_pub_messages'] = False
  153. opts['con_cache'] = ''
  154. opts['external_auth'] = {}
  155. opts['external_auth']['pam'] = \
  156. {'test_user': [{'*': ['test.ping']},
  157. {'minion_glob*': ['foo.bar']},
  158. {'minion_func_test': ['func_test.*']}],
  159. 'test_group%': [{'*': ['test.echo']}],
  160. 'test_user_mminion': [{'target_minion': ['test.ping']}],
  161. '*': [{'my_minion': ['my_mod.my_func']}],
  162. 'test_user_func': [{'*': [{'test.echo': {'args': ['MSG:.*']}},
  163. {'test.echo': {'kwargs': {'text': 'KWMSG:.*',
  164. 'anything': '.*',
  165. 'none': None}}},
  166. {'my_mod.*': {'args': ['a.*', 'b.*'],
  167. 'kwargs': {'kwa': 'kwa.*',
  168. 'kwb': 'kwb'}}}]},
  169. {'minion1': [{'test.echo': {'args': ['TEST',
  170. None,
  171. 'TEST.*']}},
  172. {'test.empty': {}}]}
  173. ]
  174. }
  175. self.clear = salt.master.ClearFuncs(opts, MagicMock())
  176. self.addCleanup(delattr, self, 'clear')
  177. # overwrite the _send_pub method so we don't have to serialize MagicMock
  178. self.clear._send_pub = lambda payload: True
  179. # make sure to return a JID, instead of a mock
  180. self.clear.mminion.returners = {'.prep_jid': lambda x: 1}
  181. self.valid_clear_load = {'tgt_type': 'glob',
  182. 'jid': '',
  183. 'cmd': 'publish',
  184. 'tgt': 'test_minion',
  185. 'kwargs':
  186. {'username': 'test_user',
  187. 'password': 'test_password',
  188. 'show_timeout': False,
  189. 'eauth': 'pam',
  190. 'show_jid': False},
  191. 'ret': '',
  192. 'user': 'test_user',
  193. 'key': '',
  194. 'arg': '',
  195. 'fun': 'test.ping',
  196. }
  197. self.addCleanup(delattr, self, 'valid_clear_load')
  198. @skipIf(salt.utils.platform.is_windows(), 'PAM eauth not available on Windows')
  199. def test_master_publish_name(self):
  200. '''
  201. Test to ensure a simple name can auth against a given function.
  202. This tests to ensure test_user can access test.ping but *not* sys.doc
  203. '''
  204. _check_minions_return = {'minions': ['some_minions'], 'missing': []}
  205. with patch('salt.utils.minions.CkMinions.check_minions', MagicMock(return_value=_check_minions_return)):
  206. # Can we access test.ping?
  207. self.clear.publish(self.valid_clear_load)
  208. self.assertEqual(self.fire_event_mock.call_args[0][0]['fun'], 'test.ping')
  209. # Are we denied access to sys.doc?
  210. sys_doc_load = self.valid_clear_load
  211. sys_doc_load['fun'] = 'sys.doc'
  212. self.clear.publish(sys_doc_load)
  213. self.assertNotEqual(self.fire_event_mock.call_args[0][0]['fun'], 'sys.doc') # If sys.doc were to fire, this would match
  214. def test_master_publish_group(self):
  215. '''
  216. Tests to ensure test_group can access test.echo but *not* sys.doc
  217. '''
  218. _check_minions_return = {'minions': ['some_minions'], 'missing': []}
  219. with patch('salt.utils.minions.CkMinions.check_minions', MagicMock(return_value=_check_minions_return)):
  220. self.valid_clear_load['kwargs']['user'] = 'new_user'
  221. self.valid_clear_load['fun'] = 'test.echo'
  222. self.valid_clear_load['arg'] = 'hello'
  223. with patch('salt.auth.LoadAuth.get_groups', return_value=['test_group', 'second_test_group']):
  224. self.clear.publish(self.valid_clear_load)
  225. # Did we fire test.echo?
  226. self.assertEqual(self.fire_event_mock.call_args[0][0]['fun'], 'test.echo')
  227. # Request sys.doc
  228. self.valid_clear_load['fun'] = 'sys.doc'
  229. # Did we fire it?
  230. self.assertNotEqual(self.fire_event_mock.call_args[0][0]['fun'], 'sys.doc')
  231. def test_master_publish_some_minions(self):
  232. '''
  233. Tests to ensure we can only target minions for which we
  234. have permission with publisher acl.
  235. Note that in order for these sorts of tests to run correctly that
  236. you should NOT patch check_minions!
  237. '''
  238. self.valid_clear_load['kwargs']['username'] = 'test_user_mminion'
  239. self.valid_clear_load['user'] = 'test_user_mminion'
  240. self.clear.publish(self.valid_clear_load)
  241. self.assertEqual(self.fire_event_mock.mock_calls, [])
  242. def test_master_not_user_glob_all(self):
  243. '''
  244. Test to ensure that we DO NOT access to a given
  245. function to all users with publisher acl. ex:
  246. '*':
  247. my_minion:
  248. - my_func
  249. Yes, this seems like a bit of a no-op test but it's
  250. here to document that this functionality
  251. is NOT supported currently.
  252. WARNING: Do not patch this wit
  253. '''
  254. self.valid_clear_load['kwargs']['username'] = 'NOT_A_VALID_USERNAME'
  255. self.valid_clear_load['user'] = 'NOT_A_VALID_USERNAME'
  256. self.valid_clear_load['fun'] = 'test.ping'
  257. self.clear.publish(self.valid_clear_load)
  258. self.assertEqual(self.fire_event_mock.mock_calls, [])
  259. @skipIf(salt.utils.platform.is_windows(), 'PAM eauth not available on Windows')
  260. def test_master_minion_glob(self):
  261. '''
  262. Test to ensure we can allow access to a given
  263. function for a user to a subset of minions
  264. selected by a glob. ex:
  265. test_user:
  266. 'minion_glob*':
  267. - glob_mod.glob_func
  268. This test is a bit tricky, because ultimately the real functionality
  269. lies in what's returned from check_minions, but this checks a limited
  270. amount of logic on the way there as well. Note the inline patch.
  271. '''
  272. requested_function = 'foo.bar'
  273. requested_tgt = 'minion_glob1'
  274. self.valid_clear_load['tgt'] = requested_tgt
  275. self.valid_clear_load['fun'] = requested_function
  276. _check_minions_return = {'minions': ['minion_glob1'], 'missing': []}
  277. with patch('salt.utils.minions.CkMinions.check_minions', MagicMock(return_value=_check_minions_return)): # Assume that there is a listening minion match
  278. self.clear.publish(self.valid_clear_load)
  279. self.assertTrue(self.fire_event_mock.called, 'Did not fire {0} for minion tgt {1}'.format(requested_function, requested_tgt))
  280. self.assertEqual(self.fire_event_mock.call_args[0][0]['fun'], requested_function, 'Did not fire {0} for minion glob'.format(requested_function))
  281. def test_master_function_glob(self):
  282. '''
  283. Test to ensure that we can allow access to a given
  284. set of functions in an execution module as selected
  285. by a glob. ex:
  286. my_user:
  287. my_minion:
  288. 'test.*'
  289. '''
  290. # Unimplemented
  291. pass
  292. @skipIf(salt.utils.platform.is_windows(), 'PAM eauth not available on Windows')
  293. def test_args_empty_spec(self):
  294. '''
  295. Test simple arg restriction allowed.
  296. 'test_user_func':
  297. minion1:
  298. - test.empty:
  299. '''
  300. _check_minions_return = {'minions': ['minion1'], 'missing': []}
  301. with patch('salt.utils.minions.CkMinions.check_minions', MagicMock(return_value=_check_minions_return)):
  302. self.valid_clear_load['kwargs'].update({'username': 'test_user_func'})
  303. self.valid_clear_load.update({'user': 'test_user_func',
  304. 'tgt': 'minion1',
  305. 'fun': 'test.empty',
  306. 'arg': ['TEST']})
  307. self.clear.publish(self.valid_clear_load)
  308. self.assertEqual(self.fire_event_mock.call_args[0][0]['fun'], 'test.empty')
  309. @skipIf(salt.utils.platform.is_windows(), 'PAM eauth not available on Windows')
  310. def test_args_simple_match(self):
  311. '''
  312. Test simple arg restriction allowed.
  313. 'test_user_func':
  314. minion1:
  315. - test.echo:
  316. args:
  317. - 'TEST'
  318. - 'TEST.*'
  319. '''
  320. _check_minions_return = {'minions': ['minion1'], 'missing': []}
  321. with patch('salt.utils.minions.CkMinions.check_minions', MagicMock(return_value=_check_minions_return)):
  322. self.valid_clear_load['kwargs'].update({'username': 'test_user_func'})
  323. self.valid_clear_load.update({'user': 'test_user_func',
  324. 'tgt': 'minion1',
  325. 'fun': 'test.echo',
  326. 'arg': ['TEST', 'any', 'TEST ABC']})
  327. self.clear.publish(self.valid_clear_load)
  328. self.assertEqual(self.fire_event_mock.call_args[0][0]['fun'], 'test.echo')
  329. @skipIf(salt.utils.platform.is_windows(), 'PAM eauth not available on Windows')
  330. def test_args_more_args(self):
  331. '''
  332. Test simple arg restriction allowed to pass unlisted args.
  333. 'test_user_func':
  334. minion1:
  335. - test.echo:
  336. args:
  337. - 'TEST'
  338. - 'TEST.*'
  339. '''
  340. _check_minions_return = {'minions': ['minion1'], 'missing': []}
  341. with patch('salt.utils.minions.CkMinions.check_minions', MagicMock(return_value=_check_minions_return)):
  342. self.valid_clear_load['kwargs'].update({'username': 'test_user_func'})
  343. self.valid_clear_load.update({'user': 'test_user_func',
  344. 'tgt': 'minion1',
  345. 'fun': 'test.echo',
  346. 'arg': ['TEST',
  347. 'any',
  348. 'TEST ABC',
  349. 'arg 3',
  350. {'kwarg1': 'val1',
  351. '__kwarg__': True}]})
  352. self.clear.publish(self.valid_clear_load)
  353. self.assertEqual(self.fire_event_mock.call_args[0][0]['fun'], 'test.echo')
  354. def test_args_simple_forbidden(self):
  355. '''
  356. Test simple arg restriction forbidden.
  357. 'test_user_func':
  358. minion1:
  359. - test.echo:
  360. args:
  361. - 'TEST'
  362. - 'TEST.*'
  363. '''
  364. _check_minions_return = {'minions': ['minion1'], 'missing': []}
  365. with patch('salt.utils.minions.CkMinions.check_minions', MagicMock(return_value=_check_minions_return)):
  366. self.valid_clear_load['kwargs'].update({'username': 'test_user_func'})
  367. # Wrong last arg
  368. self.valid_clear_load.update({'user': 'test_user_func',
  369. 'tgt': 'minion1',
  370. 'fun': 'test.echo',
  371. 'arg': ['TEST', 'any', 'TESLA']})
  372. self.clear.publish(self.valid_clear_load)
  373. self.assertEqual(self.fire_event_mock.mock_calls, [])
  374. # Wrong first arg
  375. self.valid_clear_load['arg'] = ['TES', 'any', 'TEST1234']
  376. self.clear.publish(self.valid_clear_load)
  377. self.assertEqual(self.fire_event_mock.mock_calls, [])
  378. # Missing the last arg
  379. self.valid_clear_load['arg'] = ['TEST', 'any']
  380. self.clear.publish(self.valid_clear_load)
  381. self.assertEqual(self.fire_event_mock.mock_calls, [])
  382. # No args
  383. self.valid_clear_load['arg'] = []
  384. self.clear.publish(self.valid_clear_load)
  385. self.assertEqual(self.fire_event_mock.mock_calls, [])
  386. @skipIf(salt.utils.platform.is_windows(), 'PAM eauth not available on Windows')
  387. def test_args_kwargs_match(self):
  388. '''
  389. Test simple kwargs restriction allowed.
  390. 'test_user_func':
  391. '*':
  392. - test.echo:
  393. kwargs:
  394. text: 'KWMSG:.*'
  395. '''
  396. _check_minions_return = {'minions': ['some_minions'], 'missing': []}
  397. with patch('salt.utils.minions.CkMinions.check_minions', MagicMock(return_value=_check_minions_return)):
  398. self.valid_clear_load['kwargs'].update({'username': 'test_user_func'})
  399. self.valid_clear_load.update({'user': 'test_user_func',
  400. 'tgt': '*',
  401. 'fun': 'test.echo',
  402. 'arg': [{'text': 'KWMSG: a message',
  403. 'anything': 'hello all',
  404. 'none': 'hello none',
  405. '__kwarg__': True}]})
  406. self.clear.publish(self.valid_clear_load)
  407. self.assertEqual(self.fire_event_mock.call_args[0][0]['fun'], 'test.echo')
  408. def test_args_kwargs_mismatch(self):
  409. '''
  410. Test simple kwargs restriction allowed.
  411. 'test_user_func':
  412. '*':
  413. - test.echo:
  414. kwargs:
  415. text: 'KWMSG:.*'
  416. '''
  417. _check_minions_return = {'minions': ['some_minions'], 'missing': []}
  418. with patch('salt.utils.minions.CkMinions.check_minions', MagicMock(return_value=_check_minions_return)):
  419. self.valid_clear_load['kwargs'].update({'username': 'test_user_func'})
  420. self.valid_clear_load.update({'user': 'test_user_func',
  421. 'tgt': '*',
  422. 'fun': 'test.echo'})
  423. # Wrong kwarg value
  424. self.valid_clear_load['arg'] = [{'text': 'KWMSG a message',
  425. 'anything': 'hello all',
  426. 'none': 'hello none',
  427. '__kwarg__': True}]
  428. self.clear.publish(self.valid_clear_load)
  429. self.assertEqual(self.fire_event_mock.mock_calls, [])
  430. # Missing kwarg value
  431. self.valid_clear_load['arg'] = [{'anything': 'hello all',
  432. 'none': 'hello none',
  433. '__kwarg__': True}]
  434. self.clear.publish(self.valid_clear_load)
  435. self.assertEqual(self.fire_event_mock.mock_calls, [])
  436. self.valid_clear_load['arg'] = [{'__kwarg__': True}]
  437. self.clear.publish(self.valid_clear_load)
  438. self.assertEqual(self.fire_event_mock.mock_calls, [])
  439. self.valid_clear_load['arg'] = [{}]
  440. self.clear.publish(self.valid_clear_load)
  441. self.assertEqual(self.fire_event_mock.mock_calls, [])
  442. self.valid_clear_load['arg'] = []
  443. self.clear.publish(self.valid_clear_load)
  444. self.assertEqual(self.fire_event_mock.mock_calls, [])
  445. # Missing kwarg allowing any value
  446. self.valid_clear_load['arg'] = [{'text': 'KWMSG: a message',
  447. 'none': 'hello none',
  448. '__kwarg__': True}]
  449. self.clear.publish(self.valid_clear_load)
  450. self.assertEqual(self.fire_event_mock.mock_calls, [])
  451. self.valid_clear_load['arg'] = [{'text': 'KWMSG: a message',
  452. 'anything': 'hello all',
  453. '__kwarg__': True}]
  454. self.clear.publish(self.valid_clear_load)
  455. self.assertEqual(self.fire_event_mock.mock_calls, [])
  456. @skipIf(salt.utils.platform.is_windows(), 'PAM eauth not available on Windows')
  457. def test_args_mixed_match(self):
  458. '''
  459. Test mixed args and kwargs restriction allowed.
  460. 'test_user_func':
  461. '*':
  462. - 'my_mod.*':
  463. args:
  464. - 'a.*'
  465. - 'b.*'
  466. kwargs:
  467. 'kwa': 'kwa.*'
  468. 'kwb': 'kwb'
  469. '''
  470. _check_minions_return = {'minions': ['some_minions'], 'missing': []}
  471. with patch('salt.utils.minions.CkMinions.check_minions', MagicMock(return_value=_check_minions_return)):
  472. self.valid_clear_load['kwargs'].update({'username': 'test_user_func'})
  473. self.valid_clear_load.update({'user': 'test_user_func',
  474. 'tgt': '*',
  475. 'fun': 'my_mod.some_func',
  476. 'arg': ['alpha',
  477. 'beta',
  478. 'gamma',
  479. {'kwa': 'kwarg #1',
  480. 'kwb': 'kwb',
  481. 'one_more': 'just one more',
  482. '__kwarg__': True}]})
  483. self.clear.publish(self.valid_clear_load)
  484. self.assertEqual(self.fire_event_mock.call_args[0][0]['fun'],
  485. 'my_mod.some_func')
  486. def test_args_mixed_mismatch(self):
  487. '''
  488. Test mixed args and kwargs restriction forbidden.
  489. 'test_user_func':
  490. '*':
  491. - 'my_mod.*':
  492. args:
  493. - 'a.*'
  494. - 'b.*'
  495. kwargs:
  496. 'kwa': 'kwa.*'
  497. 'kwb': 'kwb'
  498. '''
  499. _check_minions_return = {'minions': ['some_minions'], 'missing': []}
  500. with patch('salt.utils.minions.CkMinions.check_minions', MagicMock(return_value=_check_minions_return)):
  501. self.valid_clear_load['kwargs'].update({'username': 'test_user_func'})
  502. self.valid_clear_load.update({'user': 'test_user_func',
  503. 'tgt': '*',
  504. 'fun': 'my_mod.some_func'})
  505. # Wrong arg value
  506. self.valid_clear_load['arg'] = ['alpha',
  507. 'gamma',
  508. {'kwa': 'kwarg #1',
  509. 'kwb': 'kwb',
  510. 'one_more': 'just one more',
  511. '__kwarg__': True}]
  512. self.clear.publish(self.valid_clear_load)
  513. self.assertEqual(self.fire_event_mock.mock_calls, [])
  514. # Wrong kwarg value
  515. self.valid_clear_load['arg'] = ['alpha',
  516. 'beta',
  517. 'gamma',
  518. {'kwa': 'kkk',
  519. 'kwb': 'kwb',
  520. 'one_more': 'just one more',
  521. '__kwarg__': True}]
  522. self.clear.publish(self.valid_clear_load)
  523. self.assertEqual(self.fire_event_mock.mock_calls, [])
  524. # Missing arg
  525. self.valid_clear_load['arg'] = ['alpha',
  526. {'kwa': 'kwarg #1',
  527. 'kwb': 'kwb',
  528. 'one_more': 'just one more',
  529. '__kwarg__': True}]
  530. self.clear.publish(self.valid_clear_load)
  531. self.assertEqual(self.fire_event_mock.mock_calls, [])
  532. # Missing kwarg
  533. self.valid_clear_load['arg'] = ['alpha',
  534. 'beta',
  535. 'gamma',
  536. {'kwa': 'kwarg #1',
  537. 'one_more': 'just one more',
  538. '__kwarg__': True}]
  539. self.clear.publish(self.valid_clear_load)
  540. self.assertEqual(self.fire_event_mock.mock_calls, [])
  541. class AuthACLTestCase(ModuleCase):
  542. '''
  543. A class to check various aspects of the publisher ACL system
  544. '''
  545. def setUp(self):
  546. self.auth_check_mock = MagicMock(return_value=True)
  547. opts = self.get_temp_config('master')
  548. patches = (
  549. ('salt.minion.MasterMinion', MagicMock()),
  550. ('salt.utils.verify.check_path_traversal', MagicMock()),
  551. ('salt.utils.minions.CkMinions.auth_check', self.auth_check_mock),
  552. ('salt.auth.LoadAuth.time_auth', MagicMock(return_value=True)),
  553. ('salt.client.get_local_client', MagicMock(return_value=opts['conf_file'])),
  554. )
  555. for mod, mock in patches:
  556. patcher = patch(mod, mock)
  557. patcher.start()
  558. self.addCleanup(patcher.stop)
  559. self.addCleanup(delattr, self, 'auth_check_mock')
  560. opts['publisher_acl'] = {}
  561. opts['publisher_acl_blacklist'] = {}
  562. opts['master_job_cache'] = ''
  563. opts['sign_pub_messages'] = False
  564. opts['con_cache'] = ''
  565. opts['external_auth'] = {}
  566. opts['external_auth']['pam'] = {'test_user': [{'alpha_minion': ['test.ping']}]}
  567. self.clear = salt.master.ClearFuncs(opts, MagicMock())
  568. self.addCleanup(delattr, self, 'clear')
  569. # overwrite the _send_pub method so we don't have to serialize MagicMock
  570. self.clear._send_pub = lambda payload: True
  571. # make sure to return a JID, instead of a mock
  572. self.clear.mminion.returners = {'.prep_jid': lambda x: 1}
  573. self.valid_clear_load = {'tgt_type': 'glob',
  574. 'jid': '',
  575. 'cmd': 'publish',
  576. 'tgt': 'test_minion',
  577. 'kwargs':
  578. {'username': 'test_user',
  579. 'password': 'test_password',
  580. 'show_timeout': False,
  581. 'eauth': 'pam',
  582. 'show_jid': False},
  583. 'ret': '',
  584. 'user': 'test_user',
  585. 'key': '',
  586. 'arg': '',
  587. 'fun': 'test.ping',
  588. }
  589. self.addCleanup(delattr, self, 'valid_clear_load')
  590. @skipIf(salt.utils.platform.is_windows(), 'PAM eauth not available on Windows')
  591. def test_acl_simple_allow(self):
  592. self.clear.publish(self.valid_clear_load)
  593. self.assertEqual(self.auth_check_mock.call_args[0][0],
  594. [{'alpha_minion': ['test.ping']}])
  595. def test_acl_simple_deny(self):
  596. with patch('salt.auth.LoadAuth.get_auth_list', MagicMock(return_value=[{'beta_minion': ['test.ping']}])):
  597. self.clear.publish(self.valid_clear_load)
  598. self.assertEqual(self.auth_check_mock.call_args[0][0],
  599. [{'beta_minion': ['test.ping']}])