1
0

test_auth.py 27 KB

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