1
0

test_win_dacl.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. # -*- coding: utf-8 -*-
  2. # Import Python Libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. import os
  5. import tempfile
  6. # Import Salt Libs
  7. import salt.utils.platform
  8. import salt.utils.win_dacl as win_dacl
  9. import salt.utils.win_reg as win_reg
  10. from salt.exceptions import CommandExecutionError
  11. # Import Salt Testing Libs
  12. from tests.support.helpers import TstSuiteLoggingHandler, destructiveTest, random_string
  13. from tests.support.mixins import LoaderModuleMockMixin
  14. from tests.support.mock import patch
  15. from tests.support.unit import TestCase, skipIf
  16. try:
  17. import pywintypes
  18. import win32security
  19. HAS_WIN32 = True
  20. except ImportError:
  21. HAS_WIN32 = False
  22. FAKE_KEY = "SOFTWARE\\{0}".format(random_string("SaltTesting-", lowercase=False))
  23. @skipIf(not HAS_WIN32, "Requires pywin32")
  24. @skipIf(not salt.utils.platform.is_windows(), "System is not Windows")
  25. class WinDaclTestCase(TestCase):
  26. """
  27. Test cases for salt.utils.win_dacl in the registry
  28. """
  29. def test_get_sid_string(self):
  30. """
  31. Validate getting a pysid object from a name
  32. """
  33. sid_obj = win_dacl.get_sid("Administrators")
  34. self.assertTrue(isinstance(sid_obj, pywintypes.SIDType))
  35. self.assertEqual(
  36. win32security.LookupAccountSid(None, sid_obj)[0], "Administrators"
  37. )
  38. def test_get_sid_sid_string(self):
  39. """
  40. Validate getting a pysid object from a SID string
  41. """
  42. sid_obj = win_dacl.get_sid("S-1-5-32-544")
  43. self.assertTrue(isinstance(sid_obj, pywintypes.SIDType))
  44. self.assertEqual(
  45. win32security.LookupAccountSid(None, sid_obj)[0], "Administrators"
  46. )
  47. def test_get_sid_string_name(self):
  48. """
  49. Validate getting a pysid object from a SID string
  50. """
  51. sid_obj = win_dacl.get_sid("Administrators")
  52. self.assertTrue(isinstance(sid_obj, pywintypes.SIDType))
  53. self.assertEqual(win_dacl.get_sid_string(sid_obj), "S-1-5-32-544")
  54. def test_get_sid_string_none(self):
  55. """
  56. Validate getting a pysid object from None (NULL SID)
  57. """
  58. sid_obj = win_dacl.get_sid(None)
  59. self.assertTrue(isinstance(sid_obj, pywintypes.SIDType))
  60. self.assertEqual(win_dacl.get_sid_string(sid_obj), "S-1-0-0")
  61. def test_get_name_odd_case(self):
  62. """
  63. Test get_name by passing a name with inconsistent case characters.
  64. Should return the name in the correct case
  65. """
  66. # Case
  67. self.assertEqual(win_dacl.get_name("adMiniStrAtorS"), "Administrators")
  68. def test_get_name_using_sid(self):
  69. """
  70. Test get_name passing a SID String. Should return the string name
  71. """
  72. # SID String
  73. self.assertEqual(win_dacl.get_name("S-1-5-32-544"), "Administrators")
  74. def test_get_name_using_sid_object(self):
  75. """
  76. Test get_name passing a SID Object. Should return the string name
  77. """
  78. # SID Object
  79. sid_obj = win_dacl.get_sid("Administrators")
  80. self.assertTrue(isinstance(sid_obj, pywintypes.SIDType))
  81. self.assertEqual(win_dacl.get_name(sid_obj), "Administrators")
  82. def test_get_name_capability_sid(self):
  83. """
  84. Test get_name with a compatibility SID. Should return `None` as we want
  85. to ignore these SIDs
  86. """
  87. cap_sid = "S-1-15-3-1024-1065365936-1281604716-3511738428-1654721687-432734479-3232135806-4053264122-3456934681"
  88. sid_obj = win32security.ConvertStringSidToSid(cap_sid)
  89. self.assertIsNone(win_dacl.get_name(sid_obj))
  90. def test_get_name_error(self):
  91. """
  92. Test get_name with an un mapped SID, should throw a
  93. CommandExecutionError
  94. """
  95. test_sid = "S-1-2-3-4"
  96. sid_obj = win32security.ConvertStringSidToSid(test_sid)
  97. with TstSuiteLoggingHandler() as handler:
  98. self.assertRaises(CommandExecutionError, win_dacl.get_name, sid_obj)
  99. expected_message = 'ERROR:Error resolving "PySID:S-1-2-3-4"'
  100. self.assertIn(expected_message, handler.messages[0])
  101. @skipIf(not HAS_WIN32, "Requires pywin32")
  102. @skipIf(not salt.utils.platform.is_windows(), "System is not Windows")
  103. class WinDaclRegTestCase(TestCase, LoaderModuleMockMixin):
  104. obj_name = "HKLM\\" + FAKE_KEY
  105. obj_type = "registry"
  106. """
  107. Test cases for salt.utils.win_dacl in the registry
  108. """
  109. def setup_loader_modules(self):
  110. return {win_dacl: {}}
  111. def setUp(self):
  112. self.assertTrue(
  113. win_reg.set_value(
  114. hive="HKLM", key=FAKE_KEY, vname="fake_name", vdata="fake_data"
  115. )
  116. )
  117. def tearDown(self):
  118. win_reg.delete_key_recursive(hive="HKLM", key=FAKE_KEY)
  119. @destructiveTest
  120. def test_owner(self):
  121. """
  122. Test the set_owner function
  123. Test the get_owner function
  124. """
  125. self.assertTrue(
  126. win_dacl.set_owner(
  127. obj_name=self.obj_name,
  128. principal="Backup Operators",
  129. obj_type=self.obj_type,
  130. )
  131. )
  132. self.assertEqual(
  133. win_dacl.get_owner(obj_name=self.obj_name, obj_type=self.obj_type),
  134. "Backup Operators",
  135. )
  136. @destructiveTest
  137. def test_primary_group(self):
  138. """
  139. Test the set_primary_group function
  140. Test the get_primary_group function
  141. """
  142. self.assertTrue(
  143. win_dacl.set_primary_group(
  144. obj_name=self.obj_name,
  145. principal="Backup Operators",
  146. obj_type=self.obj_type,
  147. )
  148. )
  149. self.assertEqual(
  150. win_dacl.get_primary_group(obj_name=self.obj_name, obj_type=self.obj_type),
  151. "Backup Operators",
  152. )
  153. @destructiveTest
  154. def test_set_permissions(self):
  155. """
  156. Test the set_permissions function
  157. """
  158. self.assertTrue(
  159. win_dacl.set_permissions(
  160. obj_name=self.obj_name,
  161. principal="Backup Operators",
  162. permissions="full_control",
  163. access_mode="grant",
  164. obj_type=self.obj_type,
  165. reset_perms=False,
  166. protected=None,
  167. )
  168. )
  169. expected = {
  170. "Not Inherited": {
  171. "Backup Operators": {
  172. "grant": {
  173. "applies to": "This key and subkeys",
  174. "permissions": "Full Control",
  175. }
  176. }
  177. }
  178. }
  179. self.assertEqual(
  180. win_dacl.get_permissions(
  181. obj_name=self.obj_name,
  182. principal="Backup Operators",
  183. obj_type=self.obj_type,
  184. ),
  185. expected,
  186. )
  187. @destructiveTest
  188. def test_get_permissions(self):
  189. """
  190. Test the get_permissions function
  191. """
  192. self.assertTrue(
  193. win_dacl.set_permissions(
  194. obj_name=self.obj_name,
  195. principal="Backup Operators",
  196. permissions="full_control",
  197. access_mode="grant",
  198. obj_type=self.obj_type,
  199. reset_perms=False,
  200. protected=None,
  201. )
  202. )
  203. expected = {
  204. "Not Inherited": {
  205. "Backup Operators": {
  206. "grant": {
  207. "applies to": "This key and subkeys",
  208. "permissions": "Full Control",
  209. }
  210. }
  211. }
  212. }
  213. self.assertEqual(
  214. win_dacl.get_permissions(
  215. obj_name=self.obj_name,
  216. principal="Backup Operators",
  217. obj_type=self.obj_type,
  218. ),
  219. expected,
  220. )
  221. @destructiveTest
  222. def test_has_permission(self):
  223. """
  224. Test the has_permission function
  225. """
  226. self.assertTrue(
  227. win_dacl.set_permissions(
  228. obj_name=self.obj_name,
  229. principal="Backup Operators",
  230. permissions="full_control",
  231. access_mode="grant",
  232. obj_type=self.obj_type,
  233. reset_perms=False,
  234. protected=None,
  235. )
  236. )
  237. # Test has_permission exact
  238. self.assertTrue(
  239. win_dacl.has_permission(
  240. obj_name=self.obj_name,
  241. principal="Backup Operators",
  242. permission="full_control",
  243. access_mode="grant",
  244. obj_type=self.obj_type,
  245. exact=True,
  246. )
  247. )
  248. # Test has_permission contains
  249. self.assertTrue(
  250. win_dacl.has_permission(
  251. obj_name=self.obj_name,
  252. principal="Backup Operators",
  253. permission="read",
  254. access_mode="grant",
  255. obj_type=self.obj_type,
  256. exact=False,
  257. )
  258. )
  259. @destructiveTest
  260. def test_rm_permissions(self):
  261. """
  262. Test the rm_permissions function
  263. """
  264. self.assertTrue(
  265. win_dacl.set_permissions(
  266. obj_name=self.obj_name,
  267. principal="Backup Operators",
  268. permissions="full_control",
  269. access_mode="grant",
  270. obj_type=self.obj_type,
  271. reset_perms=False,
  272. protected=None,
  273. )
  274. )
  275. self.assertTrue(
  276. win_dacl.rm_permissions(
  277. obj_name=self.obj_name,
  278. principal="Backup Operators",
  279. obj_type=self.obj_type,
  280. )
  281. )
  282. self.assertEqual(
  283. win_dacl.get_permissions(
  284. obj_name=self.obj_name,
  285. principal="Backup Operators",
  286. obj_type=self.obj_type,
  287. ),
  288. {},
  289. )
  290. @destructiveTest
  291. def test_inheritance(self):
  292. """
  293. Test the set_inheritance function
  294. Test the get_inheritance function
  295. """
  296. self.assertTrue(
  297. win_dacl.set_inheritance(
  298. obj_name=self.obj_name,
  299. enabled=True,
  300. obj_type=self.obj_type,
  301. clear=False,
  302. )
  303. )
  304. self.assertTrue(
  305. win_dacl.get_inheritance(obj_name=self.obj_name, obj_type=self.obj_type)
  306. )
  307. self.assertTrue(
  308. win_dacl.set_inheritance(
  309. obj_name=self.obj_name,
  310. enabled=False,
  311. obj_type=self.obj_type,
  312. clear=False,
  313. )
  314. )
  315. self.assertFalse(
  316. win_dacl.get_inheritance(obj_name=self.obj_name, obj_type=self.obj_type)
  317. )
  318. @destructiveTest
  319. def test_check_perms(self):
  320. """
  321. Test the check_perms function
  322. """
  323. with patch.dict(win_dacl.__opts__, {"test": False}):
  324. result = win_dacl.check_perms(
  325. obj_name=self.obj_name,
  326. obj_type=self.obj_type,
  327. ret={},
  328. owner="Users",
  329. grant_perms={"Backup Operators": {"perms": "read"}},
  330. deny_perms={
  331. "Backup Operators": {"perms": ["delete"]},
  332. "NETWORK SERVICE": {
  333. "perms": ["delete", "set_value", "write_dac", "write_owner"]
  334. },
  335. },
  336. inheritance=True,
  337. reset=False,
  338. )
  339. expected = {
  340. "changes": {
  341. "owner": "Users",
  342. "perms": {
  343. "Backup Operators": {"grant": "read", "deny": ["delete"]},
  344. "NETWORK SERVICE": {
  345. "deny": ["delete", "set_value", "write_dac", "write_owner"]
  346. },
  347. },
  348. },
  349. "comment": "",
  350. "name": self.obj_name,
  351. "result": True,
  352. }
  353. self.assertDictEqual(result, expected)
  354. expected = {
  355. "Not Inherited": {
  356. "Backup Operators": {
  357. "grant": {
  358. "applies to": "This key and subkeys",
  359. "permissions": "Read",
  360. },
  361. "deny": {
  362. "applies to": "This key and subkeys",
  363. "permissions": ["Delete"],
  364. },
  365. }
  366. }
  367. }
  368. self.assertDictEqual(
  369. win_dacl.get_permissions(
  370. obj_name=self.obj_name,
  371. principal="Backup Operators",
  372. obj_type=self.obj_type,
  373. ),
  374. expected,
  375. )
  376. expected = {
  377. "Not Inherited": {
  378. "NETWORK SERVICE": {
  379. "deny": {
  380. "applies to": "This key and subkeys",
  381. "permissions": [
  382. "Delete",
  383. "Set Value",
  384. "Write DAC",
  385. "Write Owner",
  386. ],
  387. }
  388. }
  389. }
  390. }
  391. self.assertDictEqual(
  392. win_dacl.get_permissions(
  393. obj_name=self.obj_name,
  394. principal="NETWORK SERVICE",
  395. obj_type=self.obj_type,
  396. ),
  397. expected,
  398. )
  399. self.assertEqual(
  400. win_dacl.get_owner(obj_name=self.obj_name, obj_type=self.obj_type), "Users"
  401. )
  402. @destructiveTest
  403. def test_check_perms_test_true(self):
  404. """
  405. Test the check_perms function
  406. """
  407. with patch.dict(win_dacl.__opts__, {"test": True}):
  408. result = win_dacl.check_perms(
  409. obj_name=self.obj_name,
  410. obj_type=self.obj_type,
  411. ret=None,
  412. owner="Users",
  413. grant_perms={"Backup Operators": {"perms": "read"}},
  414. deny_perms={
  415. "NETWORK SERVICE": {
  416. "perms": ["delete", "set_value", "write_dac", "write_owner"]
  417. },
  418. "Backup Operators": {"perms": ["delete"]},
  419. },
  420. inheritance=True,
  421. reset=False,
  422. )
  423. expected = {
  424. "changes": {
  425. "owner": "Users",
  426. "perms": {
  427. "Backup Operators": {"grant": "read", "deny": ["delete"]},
  428. "NETWORK SERVICE": {
  429. "deny": ["delete", "set_value", "write_dac", "write_owner"]
  430. },
  431. },
  432. },
  433. "comment": "",
  434. "name": self.obj_name,
  435. "result": None,
  436. }
  437. self.assertDictEqual(result, expected)
  438. self.assertNotEqual(
  439. win_dacl.get_owner(obj_name=self.obj_name, obj_type=self.obj_type), "Users"
  440. )
  441. self.assertEqual(
  442. win_dacl.get_permissions(
  443. obj_name=self.obj_name,
  444. principal="Backup Operators",
  445. obj_type=self.obj_type,
  446. ),
  447. {},
  448. )
  449. def test_set_perms(self):
  450. """
  451. Test the set_perms function
  452. """
  453. result = win_dacl.set_perms(
  454. obj_name=self.obj_name,
  455. obj_type=self.obj_type,
  456. grant_perms={"Backup Operators": {"perms": "read"}},
  457. deny_perms={
  458. "NETWORK SERVICE": {
  459. "perms": ["delete", "set_value", "write_dac", "write_owner"]
  460. }
  461. },
  462. inheritance=True,
  463. reset=False,
  464. )
  465. expected = {
  466. "deny": {
  467. "NETWORK SERVICE": {
  468. "perms": ["delete", "set_value", "write_dac", "write_owner"]
  469. }
  470. },
  471. "grant": {"Backup Operators": {"perms": "read"}},
  472. }
  473. self.assertDictEqual(result, expected)
  474. @skipIf(not HAS_WIN32, "Requires pywin32")
  475. @skipIf(not salt.utils.platform.is_windows(), "System is not Windows")
  476. class WinDaclFileTestCase(TestCase, LoaderModuleMockMixin):
  477. obj_name = ""
  478. obj_type = "file"
  479. """
  480. Test cases for salt.utils.win_dacl in the file system
  481. """
  482. def setup_loader_modules(self):
  483. return {win_dacl: {}}
  484. def setUp(self):
  485. config_file_fd, self.obj_name = tempfile.mkstemp(
  486. prefix="SaltTesting-", suffix="txt"
  487. )
  488. os.close(config_file_fd)
  489. def tearDown(self):
  490. os.remove(self.obj_name)
  491. @destructiveTest
  492. def test_owner(self):
  493. """
  494. Test the set_owner function
  495. Test the get_owner function
  496. """
  497. self.assertTrue(
  498. win_dacl.set_owner(
  499. obj_name=self.obj_name,
  500. principal="Backup Operators",
  501. obj_type=self.obj_type,
  502. )
  503. )
  504. self.assertEqual(
  505. win_dacl.get_owner(obj_name=self.obj_name, obj_type=self.obj_type),
  506. "Backup Operators",
  507. )
  508. @destructiveTest
  509. def test_primary_group(self):
  510. """
  511. Test the set_primary_group function
  512. Test the get_primary_group function
  513. """
  514. self.assertTrue(
  515. win_dacl.set_primary_group(
  516. obj_name=self.obj_name,
  517. principal="Backup Operators",
  518. obj_type=self.obj_type,
  519. )
  520. )
  521. self.assertEqual(
  522. win_dacl.get_primary_group(obj_name=self.obj_name, obj_type=self.obj_type),
  523. "Backup Operators",
  524. )
  525. @destructiveTest
  526. def test_set_permissions(self):
  527. """
  528. Test the set_permissions function
  529. """
  530. self.assertTrue(
  531. win_dacl.set_permissions(
  532. obj_name=self.obj_name,
  533. principal="Backup Operators",
  534. permissions="full_control",
  535. access_mode="grant",
  536. obj_type=self.obj_type,
  537. reset_perms=False,
  538. protected=None,
  539. )
  540. )
  541. expected = {
  542. "Not Inherited": {
  543. "Backup Operators": {
  544. "grant": {
  545. "applies to": "Not Inherited (file)",
  546. "permissions": "Full control",
  547. }
  548. }
  549. }
  550. }
  551. self.assertEqual(
  552. win_dacl.get_permissions(
  553. obj_name=self.obj_name,
  554. principal="Backup Operators",
  555. obj_type=self.obj_type,
  556. ),
  557. expected,
  558. )
  559. @destructiveTest
  560. def test_get_permissions(self):
  561. """
  562. Test the get_permissions function
  563. """
  564. self.assertTrue(
  565. win_dacl.set_permissions(
  566. obj_name=self.obj_name,
  567. principal="Backup Operators",
  568. permissions="full_control",
  569. access_mode="grant",
  570. obj_type=self.obj_type,
  571. reset_perms=False,
  572. protected=None,
  573. )
  574. )
  575. expected = {
  576. "Not Inherited": {
  577. "Backup Operators": {
  578. "grant": {
  579. "applies to": "Not Inherited (file)",
  580. "permissions": "Full control",
  581. }
  582. }
  583. }
  584. }
  585. self.assertEqual(
  586. win_dacl.get_permissions(
  587. obj_name=self.obj_name,
  588. principal="Backup Operators",
  589. obj_type=self.obj_type,
  590. ),
  591. expected,
  592. )
  593. @destructiveTest
  594. def test_has_permission(self):
  595. """
  596. Test the has_permission function
  597. """
  598. self.assertTrue(
  599. win_dacl.set_permissions(
  600. obj_name=self.obj_name,
  601. principal="Backup Operators",
  602. permissions="full_control",
  603. access_mode="grant",
  604. obj_type=self.obj_type,
  605. reset_perms=False,
  606. protected=None,
  607. )
  608. )
  609. # Test has_permission exact
  610. self.assertTrue(
  611. win_dacl.has_permission(
  612. obj_name=self.obj_name,
  613. principal="Backup Operators",
  614. permission="full_control",
  615. access_mode="grant",
  616. obj_type=self.obj_type,
  617. exact=True,
  618. )
  619. )
  620. # Test has_permission contains
  621. self.assertTrue(
  622. win_dacl.has_permission(
  623. obj_name=self.obj_name,
  624. principal="Backup Operators",
  625. permission="read",
  626. access_mode="grant",
  627. obj_type=self.obj_type,
  628. exact=False,
  629. )
  630. )
  631. @destructiveTest
  632. def test_rm_permissions(self):
  633. """
  634. Test the rm_permissions function
  635. """
  636. self.assertTrue(
  637. win_dacl.set_permissions(
  638. obj_name=self.obj_name,
  639. principal="Backup Operators",
  640. permissions="full_control",
  641. access_mode="grant",
  642. obj_type=self.obj_type,
  643. reset_perms=False,
  644. protected=None,
  645. )
  646. )
  647. self.assertTrue(
  648. win_dacl.rm_permissions(
  649. obj_name=self.obj_name,
  650. principal="Backup Operators",
  651. obj_type=self.obj_type,
  652. )
  653. )
  654. self.assertEqual(
  655. win_dacl.get_permissions(
  656. obj_name=self.obj_name,
  657. principal="Backup Operators",
  658. obj_type=self.obj_type,
  659. ),
  660. {},
  661. )
  662. @destructiveTest
  663. def test_inheritance(self):
  664. """
  665. Test the set_inheritance function
  666. Test the get_inheritance function
  667. """
  668. self.assertTrue(
  669. win_dacl.set_inheritance(
  670. obj_name=self.obj_name,
  671. enabled=True,
  672. obj_type=self.obj_type,
  673. clear=False,
  674. )
  675. )
  676. self.assertTrue(
  677. win_dacl.get_inheritance(obj_name=self.obj_name, obj_type=self.obj_type)
  678. )
  679. self.assertTrue(
  680. win_dacl.set_inheritance(
  681. obj_name=self.obj_name,
  682. enabled=False,
  683. obj_type=self.obj_type,
  684. clear=False,
  685. )
  686. )
  687. self.assertFalse(
  688. win_dacl.get_inheritance(obj_name=self.obj_name, obj_type=self.obj_type)
  689. )
  690. @destructiveTest
  691. def test_check_perms(self):
  692. """
  693. Test the check_perms function
  694. """
  695. with patch.dict(win_dacl.__opts__, {"test": False}):
  696. result = win_dacl.check_perms(
  697. obj_name=self.obj_name,
  698. obj_type=self.obj_type,
  699. ret={},
  700. owner="Users",
  701. grant_perms={"Backup Operators": {"perms": "read"}},
  702. deny_perms={
  703. "Backup Operators": {"perms": ["delete"]},
  704. "NETWORK SERVICE": {
  705. "perms": [
  706. "delete",
  707. "change_permissions",
  708. "write_attributes",
  709. "write_data",
  710. ]
  711. },
  712. },
  713. inheritance=True,
  714. reset=False,
  715. )
  716. expected = {
  717. "changes": {
  718. "owner": "Users",
  719. "perms": {
  720. "Backup Operators": {"grant": "read", "deny": ["delete"]},
  721. "NETWORK SERVICE": {
  722. "deny": [
  723. "delete",
  724. "change_permissions",
  725. "write_attributes",
  726. "write_data",
  727. ]
  728. },
  729. },
  730. },
  731. "comment": "",
  732. "name": self.obj_name,
  733. "result": True,
  734. }
  735. self.assertDictEqual(result, expected)
  736. expected = {
  737. "Not Inherited": {
  738. "Backup Operators": {
  739. "grant": {
  740. "applies to": "Not Inherited (file)",
  741. "permissions": "Read",
  742. },
  743. "deny": {
  744. "applies to": "Not Inherited (file)",
  745. "permissions": ["Delete"],
  746. },
  747. }
  748. }
  749. }
  750. self.assertDictEqual(
  751. win_dacl.get_permissions(
  752. obj_name=self.obj_name,
  753. principal="Backup Operators",
  754. obj_type=self.obj_type,
  755. ),
  756. expected,
  757. )
  758. expected = {
  759. "Not Inherited": {
  760. "NETWORK SERVICE": {
  761. "deny": {
  762. "applies to": "Not Inherited (file)",
  763. "permissions": [
  764. "Change permissions",
  765. "Create files / write data",
  766. "Delete",
  767. "Write attributes",
  768. ],
  769. }
  770. }
  771. }
  772. }
  773. self.assertDictEqual(
  774. win_dacl.get_permissions(
  775. obj_name=self.obj_name,
  776. principal="NETWORK SERVICE",
  777. obj_type=self.obj_type,
  778. ),
  779. expected,
  780. )
  781. self.assertEqual(
  782. win_dacl.get_owner(obj_name=self.obj_name, obj_type=self.obj_type), "Users"
  783. )
  784. @destructiveTest
  785. def test_check_perms_test_true(self):
  786. """
  787. Test the check_perms function
  788. """
  789. with patch.dict(win_dacl.__opts__, {"test": True}):
  790. result = win_dacl.check_perms(
  791. obj_name=self.obj_name,
  792. obj_type=self.obj_type,
  793. ret=None,
  794. owner="Users",
  795. grant_perms={"Backup Operators": {"perms": "read"}},
  796. deny_perms={
  797. "NETWORK SERVICE": {
  798. "perms": ["delete", "set_value", "write_dac", "write_owner"]
  799. },
  800. "Backup Operators": {"perms": ["delete"]},
  801. },
  802. inheritance=True,
  803. reset=False,
  804. )
  805. expected = {
  806. "changes": {
  807. "owner": "Users",
  808. "perms": {
  809. "Backup Operators": {"grant": "read", "deny": ["delete"]},
  810. "NETWORK SERVICE": {
  811. "deny": ["delete", "set_value", "write_dac", "write_owner"]
  812. },
  813. },
  814. },
  815. "comment": "",
  816. "name": self.obj_name,
  817. "result": None,
  818. }
  819. self.assertDictEqual(result, expected)
  820. self.assertNotEqual(
  821. win_dacl.get_owner(obj_name=self.obj_name, obj_type=self.obj_type), "Users"
  822. )
  823. self.assertEqual(
  824. win_dacl.get_permissions(
  825. obj_name=self.obj_name,
  826. principal="Backup Operators",
  827. obj_type=self.obj_type,
  828. ),
  829. {},
  830. )
  831. def test_set_perms(self):
  832. """
  833. Test the set_perms function
  834. """
  835. result = win_dacl.set_perms(
  836. obj_name=self.obj_name,
  837. obj_type=self.obj_type,
  838. grant_perms={"Backup Operators": {"perms": "read"}},
  839. deny_perms={
  840. "NETWORK SERVICE": {
  841. "perms": [
  842. "delete",
  843. "change_permissions",
  844. "write_attributes",
  845. "write_data",
  846. ]
  847. }
  848. },
  849. inheritance=True,
  850. reset=False,
  851. )
  852. expected = {
  853. "deny": {
  854. "NETWORK SERVICE": {
  855. "perms": [
  856. "delete",
  857. "change_permissions",
  858. "write_attributes",
  859. "write_data",
  860. ]
  861. }
  862. },
  863. "grant": {"Backup Operators": {"perms": "read"}},
  864. }
  865. self.assertDictEqual(result, expected)