test_win_lgpo.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Shane Lee <slee@saltstack.com>
  4. """
  5. from __future__ import absolute_import, print_function, unicode_literals
  6. import glob
  7. import os
  8. import salt.config
  9. import salt.ext.six as six
  10. import salt.loader
  11. import salt.modules.win_lgpo as win_lgpo
  12. import salt.states.win_lgpo
  13. import salt.utils.files
  14. import salt.utils.platform
  15. import salt.utils.stringutils
  16. from tests.support.helpers import destructiveTest, slowTest
  17. from tests.support.mixins import LoaderModuleMockMixin
  18. from tests.support.mock import MagicMock, Mock, patch
  19. from tests.support.unit import TestCase, skipIf
  20. # We're going to actually use the loader, without grains (slow)
  21. opts = salt.config.DEFAULT_MINION_OPTS.copy()
  22. utils = salt.loader.utils(opts)
  23. modules = salt.loader.minion_mods(opts, utils=utils)
  24. LOADER_DICTS = {win_lgpo: {"__opts__": opts, "__salt__": modules, "__utils__": utils}}
  25. class WinLGPOTestCase(TestCase):
  26. """
  27. Test cases for salt.modules.win_lgpo
  28. """
  29. encoded_null = chr(0).encode("utf-16-le")
  30. def test__getAdmlDisplayName(self):
  31. display_name = "$(string.KeepAliveTime1)"
  32. adml_xml_data = "junk, we are mocking the return"
  33. obj_xpath = Mock()
  34. obj_xpath.text = "300000 or 5 minutes (recommended) "
  35. mock_xpath_obj = MagicMock(return_value=[obj_xpath])
  36. with patch.object(win_lgpo, "ADML_DISPLAY_NAME_XPATH", mock_xpath_obj):
  37. result = win_lgpo._getAdmlDisplayName(
  38. adml_xml_data=adml_xml_data, display_name=display_name
  39. )
  40. expected = "300000 or 5 minutes (recommended)"
  41. self.assertEqual(result, expected)
  42. def test__regexSearchKeyValueCombo_enabled(self):
  43. """
  44. Make sure
  45. """
  46. policy_data = (
  47. b"[\x00s\x00o\x00f\x00t\x00w\x00a\x00r\x00e\x00\\\x00p"
  48. b"\x00o\x00l\x00i\x00c\x00i\x00e\x00s\x00\\\x00m\x00i"
  49. b"\x00c\x00r\x00o\x00s\x00o\x00f\x00t\x00\\\x00w\x00i"
  50. b"\x00n\x00d\x00o\x00w\x00s\x00\\\x00w\x00i\x00n\x00d"
  51. b"\x00o\x00w\x00s\x00 \x00e\x00r\x00r\x00o\x00r\x00 "
  52. b"\x00r\x00e\x00p\x00o\x00r\x00t\x00i\x00n\x00g\x00\\"
  53. b"\x00c\x00o\x00n\x00s\x00e\x00n\x00t\x00\x00\x00;\x00D"
  54. b"\x00e\x00f\x00a\x00u\x00l\x00t\x00C\x00o\x00n\x00s"
  55. b"\x00e\x00n\x00t\x00\x00\x00;\x00\x01\x00\x00\x00;\x00"
  56. b"\x04\x00\x00\x00;\x00\x02\x00\x00\x00]\x00"
  57. )
  58. policy_regpath = (
  59. b"\x00s\x00o\x00f\x00t\x00w\x00a\x00r\x00e\x00\\\x00p"
  60. b"\x00o\x00l\x00i\x00c\x00i\x00e\x00s\x00\\\x00m\x00i"
  61. b"\x00c\x00r\x00o\x00s\x00o\x00f\x00t\x00\\\x00w\x00i"
  62. b"\x00n\x00d\x00o\x00w\x00s\x00\\\x00w\x00i\x00n\x00d"
  63. b"\x00o\x00w\x00s\x00 \x00e\x00r\x00r\x00o\x00r\x00 "
  64. b"\x00r\x00e\x00p\x00o\x00r\x00t\x00i\x00n\x00g\x00\\"
  65. b"\x00c\x00o\x00n\x00s\x00e\x00n\x00t\x00\x00"
  66. )
  67. policy_regkey = (
  68. b"\x00D\x00e\x00f\x00a\x00u\x00l\x00t\x00C\x00o\x00n"
  69. b"\x00s\x00e\x00n\x00t\x00\x00"
  70. )
  71. test = win_lgpo._regexSearchKeyValueCombo(
  72. policy_data=policy_data,
  73. policy_regpath=policy_regpath,
  74. policy_regkey=policy_regkey,
  75. )
  76. self.assertEqual(test, policy_data)
  77. def test__regexSearchKeyValueCombo_not_configured(self):
  78. """
  79. Make sure
  80. """
  81. policy_data = b""
  82. policy_regpath = (
  83. b"\x00s\x00o\x00f\x00t\x00w\x00a\x00r\x00e\x00\\\x00p"
  84. b"\x00o\x00l\x00i\x00c\x00i\x00e\x00s\x00\\\x00m\x00i"
  85. b"\x00c\x00r\x00o\x00s\x00o\x00f\x00t\x00\\\x00w\x00i"
  86. b"\x00n\x00d\x00o\x00w\x00s\x00\\\x00w\x00i\x00n\x00d"
  87. b"\x00o\x00w\x00s\x00 \x00e\x00r\x00r\x00o\x00r\x00 "
  88. b"\x00r\x00e\x00p\x00o\x00r\x00t\x00i\x00n\x00g\x00\\"
  89. b"\x00c\x00o\x00n\x00s\x00e\x00n\x00t\x00\x00"
  90. )
  91. policy_regkey = (
  92. b"\x00D\x00e\x00f\x00a\x00u\x00l\x00t\x00C\x00o\x00n"
  93. b"\x00s\x00e\x00n\x00t\x00\x00"
  94. )
  95. test = win_lgpo._regexSearchKeyValueCombo(
  96. policy_data=policy_data,
  97. policy_regpath=policy_regpath,
  98. policy_regkey=policy_regkey,
  99. )
  100. self.assertIsNone(test)
  101. def test__regexSearchKeyValueCombo_disabled(self):
  102. """
  103. Make sure
  104. """
  105. policy_data = (
  106. b"[\x00s\x00o\x00f\x00t\x00w\x00a\x00r\x00e\x00\\\x00p"
  107. b"\x00o\x00l\x00i\x00c\x00i\x00e\x00s\x00\\\x00m\x00i"
  108. b"\x00c\x00r\x00o\x00s\x00o\x00f\x00t\x00\\\x00w\x00i"
  109. b"\x00n\x00d\x00o\x00w\x00s\x00\\\x00w\x00i\x00n\x00d"
  110. b"\x00o\x00w\x00s\x00 \x00e\x00r\x00r\x00o\x00r\x00 "
  111. b"\x00r\x00e\x00p\x00o\x00r\x00t\x00i\x00n\x00g\x00\\"
  112. b"\x00c\x00o\x00n\x00s\x00e\x00n\x00t\x00\x00\x00;\x00*"
  113. b"\x00*\x00d\x00e\x00l\x00.\x00D\x00e\x00f\x00a\x00u"
  114. b"\x00l\x00t\x00C\x00o\x00n\x00s\x00e\x00n\x00t\x00\x00"
  115. b"\x00;\x00\x01\x00\x00\x00;\x00\x04\x00\x00\x00;\x00 "
  116. b"\x00\x00\x00]\x00"
  117. )
  118. policy_regpath = (
  119. b"\x00s\x00o\x00f\x00t\x00w\x00a\x00r\x00e\x00\\\x00p"
  120. b"\x00o\x00l\x00i\x00c\x00i\x00e\x00s\x00\\\x00m\x00i"
  121. b"\x00c\x00r\x00o\x00s\x00o\x00f\x00t\x00\\\x00w\x00i"
  122. b"\x00n\x00d\x00o\x00w\x00s\x00\\\x00w\x00i\x00n\x00d"
  123. b"\x00o\x00w\x00s\x00 \x00e\x00r\x00r\x00o\x00r\x00 "
  124. b"\x00r\x00e\x00p\x00o\x00r\x00t\x00i\x00n\x00g\x00\\"
  125. b"\x00c\x00o\x00n\x00s\x00e\x00n\x00t\x00\x00"
  126. )
  127. policy_regkey = (
  128. b"\x00D\x00e\x00f\x00a\x00u\x00l\x00t\x00C\x00o\x00n"
  129. b"\x00s\x00e\x00n\x00t\x00\x00"
  130. )
  131. test = win_lgpo._regexSearchKeyValueCombo(
  132. policy_data=policy_data,
  133. policy_regpath=policy_regpath,
  134. policy_regkey=policy_regkey,
  135. )
  136. self.assertEqual(test, policy_data)
  137. def test__encode_string(self):
  138. """
  139. ``_encode_string`` should return a null terminated ``utf-16-le`` encoded
  140. string when a string value is passed
  141. """
  142. encoded_value = b"".join(
  143. ["Salt is awesome".encode("utf-16-le"), self.encoded_null]
  144. )
  145. value = win_lgpo._encode_string("Salt is awesome")
  146. self.assertEqual(value, encoded_value)
  147. def test__encode_string_empty_string(self):
  148. """
  149. ``_encode_string`` should return an encoded null when an empty string
  150. value is passed
  151. """
  152. value = win_lgpo._encode_string("")
  153. self.assertEqual(value, self.encoded_null)
  154. def test__encode_string_error(self):
  155. """
  156. ``_encode_string`` should raise an error if a non-string value is passed
  157. """
  158. self.assertRaises(TypeError, win_lgpo._encode_string, [1])
  159. test_list = ["item1", "item2"]
  160. self.assertRaises(TypeError, win_lgpo._encode_string, [test_list])
  161. test_dict = {"key1": "value1", "key2": "value2"}
  162. self.assertRaises(TypeError, win_lgpo._encode_string, [test_dict])
  163. def test__encode_string_none(self):
  164. """
  165. ``_encode_string`` should return an encoded null when ``None`` is passed
  166. """
  167. value = win_lgpo._encode_string(None)
  168. self.assertEqual(value, self.encoded_null)
  169. def test__multi_string_get_transform_list(self):
  170. """
  171. ``_multi_string_get_transform`` should return the list when a list is
  172. passed
  173. """
  174. test_value = ["Spongebob", "Squarepants"]
  175. value = win_lgpo._policy_info._multi_string_get_transform(item=test_value)
  176. self.assertEqual(value, test_value)
  177. def test__multi_string_get_transform_none(self):
  178. """
  179. ``_multi_string_get_transform`` should return "Not Defined" when
  180. ``None`` is passed
  181. """
  182. test_value = None
  183. value = win_lgpo._policy_info._multi_string_get_transform(item=test_value)
  184. self.assertEqual(value, "Not Defined")
  185. def test__multi_string_get_transform_invalid(self):
  186. """
  187. ``_multi_string_get_transform`` should return "Not Defined" when
  188. ``None`` is passed
  189. """
  190. test_value = "Some String"
  191. value = win_lgpo._policy_info._multi_string_get_transform(item=test_value)
  192. self.assertEqual(value, "Invalid Value")
  193. def test__multi_string_put_transform_list(self):
  194. """
  195. ``_multi_string_put_transform`` should return the list when a list is
  196. passed
  197. """
  198. test_value = ["Spongebob", "Squarepants"]
  199. value = win_lgpo._policy_info._multi_string_put_transform(item=test_value)
  200. self.assertEqual(value, test_value)
  201. def test__multi_string_put_transform_none(self):
  202. """
  203. ``_multi_string_put_transform`` should return ``None`` when
  204. "Not Defined" is passed
  205. """
  206. test_value = "Not Defined"
  207. value = win_lgpo._policy_info._multi_string_put_transform(item=test_value)
  208. self.assertEqual(value, None)
  209. def test__multi_string_put_transform_list_from_string(self):
  210. """
  211. ``_multi_string_put_transform`` should return a list when a comma
  212. delimited string is passed
  213. """
  214. test_value = "Spongebob,Squarepants"
  215. value = win_lgpo._policy_info._multi_string_put_transform(item=test_value)
  216. self.assertEqual(value, ["Spongebob", "Squarepants"])
  217. def test__multi_string_put_transform_invalid(self):
  218. """
  219. ``_multi_string_put_transform`` should return "Invalid" value if neither
  220. string nor list is passed
  221. """
  222. test_value = None
  223. value = win_lgpo._policy_info._multi_string_put_transform(item=test_value)
  224. self.assertEqual(value, "Invalid Value")
  225. @skipIf(not salt.utils.platform.is_windows(), "System is not Windows")
  226. class WinLGPOGetPolicyADMXTestCase(TestCase, LoaderModuleMockMixin):
  227. """
  228. Test functions related to the ``get_policy`` function using policy templates
  229. (admx/adml)
  230. """
  231. def setup_loader_modules(self):
  232. return LOADER_DICTS
  233. def test_get_policy_name(self):
  234. result = win_lgpo.get_policy(
  235. policy_name="Allow Telemetry",
  236. policy_class="machine",
  237. return_value_only=True,
  238. return_full_policy_names=True,
  239. hierarchical_return=False,
  240. )
  241. expected = "Not Configured"
  242. self.assertEqual(result, expected)
  243. def test_get_policy_id(self):
  244. result = win_lgpo.get_policy(
  245. policy_name="AllowTelemetry",
  246. policy_class="machine",
  247. return_value_only=True,
  248. return_full_policy_names=True,
  249. hierarchical_return=False,
  250. )
  251. expected = "Not Configured"
  252. self.assertEqual(result, expected)
  253. def test_get_policy_name_full_return_full_names(self):
  254. result = win_lgpo.get_policy(
  255. policy_name="Allow Telemetry",
  256. policy_class="machine",
  257. return_value_only=False,
  258. return_full_policy_names=True,
  259. hierarchical_return=False,
  260. )
  261. expected = {
  262. "Windows Components\\Data Collection and Preview Builds\\"
  263. "Allow Telemetry": "Not Configured"
  264. }
  265. self.assertDictEqual(result, expected)
  266. def test_get_policy_id_full_return_full_names(self):
  267. result = win_lgpo.get_policy(
  268. policy_name="AllowTelemetry",
  269. policy_class="machine",
  270. return_value_only=False,
  271. return_full_policy_names=True,
  272. hierarchical_return=False,
  273. )
  274. expected = {
  275. "Windows Components\\Data Collection and Preview Builds\\"
  276. "Allow Telemetry": "Not Configured"
  277. }
  278. self.assertDictEqual(result, expected)
  279. def test_get_policy_name_full_return_ids(self):
  280. result = win_lgpo.get_policy(
  281. policy_name="Allow Telemetry",
  282. policy_class="machine",
  283. return_value_only=False,
  284. return_full_policy_names=False,
  285. hierarchical_return=False,
  286. )
  287. expected = {"AllowTelemetry": "Not Configured"}
  288. self.assertDictEqual(result, expected)
  289. def test_get_policy_id_full_return_ids(self):
  290. result = win_lgpo.get_policy(
  291. policy_name="AllowTelemetry",
  292. policy_class="machine",
  293. return_value_only=False,
  294. return_full_policy_names=False,
  295. hierarchical_return=False,
  296. )
  297. expected = {"AllowTelemetry": "Not Configured"}
  298. self.assertDictEqual(result, expected)
  299. def test_get_policy_id_full_return_ids_hierarchical(self):
  300. result = win_lgpo.get_policy(
  301. policy_name="AllowTelemetry",
  302. policy_class="machine",
  303. return_value_only=False,
  304. return_full_policy_names=False,
  305. hierarchical_return=True,
  306. )
  307. expected = {
  308. "Computer Configuration": {
  309. "Administrative Templates": {
  310. "WindowsComponents": {
  311. "DataCollectionAndPreviewBuilds": {
  312. "AllowTelemetry": "Not Configured"
  313. }
  314. }
  315. }
  316. }
  317. }
  318. self.assertDictEqual(result, expected)
  319. def test_get_policy_name_return_full_names_hierarchical(self):
  320. result = win_lgpo.get_policy(
  321. policy_name="Allow Telemetry",
  322. policy_class="machine",
  323. return_value_only=False,
  324. return_full_policy_names=True,
  325. hierarchical_return=True,
  326. )
  327. expected = {
  328. "Computer Configuration": {
  329. "Administrative Templates": {
  330. "Windows Components": {
  331. "Data Collection and Preview Builds": {
  332. "Allow Telemetry": "Not Configured"
  333. }
  334. }
  335. }
  336. }
  337. }
  338. self.assertDictEqual(result, expected)
  339. @destructiveTest
  340. def test__load_policy_definitions(self):
  341. """
  342. Test that unexpected files in the PolicyDefinitions directory won't
  343. cause the _load_policy_definitions function to explode
  344. https://gitlab.com/saltstack/enterprise/lock/issues/3826
  345. """
  346. # The PolicyDefinitions directory should only contain ADMX files. We
  347. # want to make sure the `_load_policy_definitions` function skips non
  348. # ADMX files in this directory.
  349. # Create a bogus ADML file in PolicyDefinitions directory
  350. bogus_fle = os.path.join("c:\\Windows\\PolicyDefinitions", "_bogus.adml")
  351. cache_dir = os.path.join(win_lgpo.__opts__["cachedir"], "lgpo", "policy_defs")
  352. try:
  353. with salt.utils.files.fopen(bogus_fle, "w+") as fh:
  354. fh.write("<junk></junk>")
  355. # This function doesn't return anything (None), it just loads
  356. # the XPath structures into __context__. We're just making sure it
  357. # doesn't stack trace here
  358. self.assertIsNone(win_lgpo._load_policy_definitions())
  359. finally:
  360. # Remove source file
  361. os.remove(bogus_fle)
  362. # Remove cached file
  363. search_string = "{0}\\_bogus*.adml".format(cache_dir)
  364. for file_name in glob.glob(search_string):
  365. os.remove(file_name)
  366. @skipIf(not salt.utils.platform.is_windows(), "System is not Windows")
  367. class WinLGPOGetPolicyFromPolicyInfoTestCase(TestCase, LoaderModuleMockMixin):
  368. """
  369. Test functions related to the ``get_policy`` function using _policy_info
  370. object
  371. """
  372. def setup_loader_modules(self):
  373. return LOADER_DICTS
  374. def test_get_policy_name(self):
  375. result = win_lgpo.get_policy(
  376. policy_name="Network firewall: Public: Settings: Display a notification",
  377. policy_class="machine",
  378. return_value_only=True,
  379. return_full_policy_names=True,
  380. hierarchical_return=False,
  381. )
  382. expected = "Not configured"
  383. self.assertEqual(result, expected)
  384. def test_get_policy_id(self):
  385. result = win_lgpo.get_policy(
  386. policy_name="WfwPublicSettingsNotification",
  387. policy_class="machine",
  388. return_value_only=True,
  389. return_full_policy_names=True,
  390. hierarchical_return=False,
  391. )
  392. expected = "Not configured"
  393. self.assertEqual(result, expected)
  394. def test_get_policy_name_full_return(self):
  395. result = win_lgpo.get_policy(
  396. policy_name="Network firewall: Public: Settings: Display a notification",
  397. policy_class="machine",
  398. return_value_only=False,
  399. return_full_policy_names=True,
  400. hierarchical_return=False,
  401. )
  402. expected = {
  403. "Network firewall: Public: Settings: Display a notification": "Not configured"
  404. }
  405. self.assertDictEqual(result, expected)
  406. def test_get_policy_id_full_return(self):
  407. result = win_lgpo.get_policy(
  408. policy_name="WfwPublicSettingsNotification",
  409. policy_class="machine",
  410. return_value_only=False,
  411. return_full_policy_names=True,
  412. hierarchical_return=False,
  413. )
  414. expected = {
  415. "Network firewall: Public: Settings: Display a notification": "Not configured"
  416. }
  417. self.assertDictEqual(result, expected)
  418. def test_get_policy_name_full_return_ids(self):
  419. result = win_lgpo.get_policy(
  420. policy_name="Network firewall: Public: Settings: Display a notification",
  421. policy_class="machine",
  422. return_value_only=False,
  423. return_full_policy_names=False,
  424. hierarchical_return=False,
  425. )
  426. expected = {
  427. "Network firewall: Public: Settings: Display a notification": "Not configured"
  428. }
  429. self.assertDictEqual(result, expected)
  430. def test_get_policy_id_full_return_ids(self):
  431. result = win_lgpo.get_policy(
  432. policy_name="WfwPublicSettingsNotification",
  433. policy_class="machine",
  434. return_value_only=False,
  435. return_full_policy_names=False,
  436. hierarchical_return=False,
  437. )
  438. expected = {"WfwPublicSettingsNotification": "Not configured"}
  439. self.assertDictEqual(result, expected)
  440. def test_get_policy_id_full_return_ids_hierarchical(self):
  441. result = win_lgpo.get_policy(
  442. policy_name="WfwPublicSettingsNotification",
  443. policy_class="machine",
  444. return_value_only=False,
  445. return_full_policy_names=False,
  446. hierarchical_return=True,
  447. )
  448. expected = {
  449. "Computer Configuration": {
  450. "Windows Settings": {
  451. "Security Settings": {
  452. "Windows Firewall with Advanced Security": {
  453. "Windows Firewall with Advanced Security - Local "
  454. "Group Policy Object": {
  455. "WfwPublicSettingsNotification": "Not configured"
  456. }
  457. }
  458. }
  459. }
  460. }
  461. }
  462. self.assertDictEqual(result, expected)
  463. def test_get_policy_id_full_return_full_names_hierarchical(self):
  464. result = win_lgpo.get_policy(
  465. policy_name="WfwPublicSettingsNotification",
  466. policy_class="machine",
  467. return_value_only=False,
  468. return_full_policy_names=True,
  469. hierarchical_return=True,
  470. )
  471. expected = {
  472. "Computer Configuration": {
  473. "Windows Settings": {
  474. "Security Settings": {
  475. "Windows Firewall with Advanced Security": {
  476. "Windows Firewall with Advanced Security - Local "
  477. "Group Policy Object": {
  478. "Network firewall: Public: Settings: Display a "
  479. "notification": "Not configured"
  480. }
  481. }
  482. }
  483. }
  484. }
  485. }
  486. self.assertDictEqual(result, expected)
  487. @skipIf(not salt.utils.platform.is_windows(), "System is not Windows")
  488. class WinLGPOPolicyInfoMechanismsTestCase(TestCase, LoaderModuleMockMixin):
  489. """
  490. Test getting local group policy settings defined in the _policy_info object
  491. Go through each mechanism
  492. """
  493. def setup_loader_modules(self):
  494. return LOADER_DICTS
  495. @classmethod
  496. def setUpClass(cls):
  497. cls.policy_data = salt.modules.win_lgpo._policy_info()
  498. def _test_policy(self, policy_name):
  499. """
  500. Helper function to get current setting
  501. """
  502. policy_definition = self.policy_data.policies["Machine"]["policies"][
  503. policy_name
  504. ]
  505. return salt.modules.win_lgpo._get_policy_info_setting(policy_definition)
  506. def test_registry_mechanism(self):
  507. """
  508. Test getting policy value using the Registry mechanism
  509. """
  510. policy_name = "RemoteRegistryExactPaths"
  511. result = self._test_policy(policy_name=policy_name)
  512. expected = [
  513. "System\\CurrentControlSet\\Control\\ProductOptions",
  514. "System\\CurrentControlSet\\Control\\Server Applications",
  515. "Software\\Microsoft\\Windows NT\\CurrentVersion",
  516. ]
  517. self.assertListEqual(result, expected)
  518. def test_secedit_mechanism(self):
  519. """
  520. Test getting policy value using the Secedit mechanism
  521. """
  522. policy_name = "LSAAnonymousNameLookup"
  523. result = self._test_policy(policy_name=policy_name)
  524. expected = "Disabled"
  525. self.assertEqual(result, expected)
  526. def test_netsh_mechanism(self):
  527. """
  528. Test getting the policy value using the NetSH mechanism
  529. """
  530. policy_name = "WfwDomainState"
  531. result = self._test_policy(policy_name=policy_name)
  532. expected = "Not configured"
  533. self.assertEqual(result, expected)
  534. @destructiveTest
  535. def test_adv_audit_mechanism(self):
  536. """
  537. Test getting the policy value using the AdvAudit mechanism
  538. """
  539. system_root = os.environ.get("SystemRoot", "C:\\Windows")
  540. f_audit = os.path.join(system_root, "security", "audit", "audit.csv")
  541. f_audit_gpo = os.path.join(
  542. system_root,
  543. "System32",
  544. "GroupPolicy",
  545. "Machine",
  546. "Microsoft",
  547. "Windows NT",
  548. "Audit",
  549. "audit.csv",
  550. )
  551. if os.path.exists(f_audit):
  552. os.remove(f_audit)
  553. if os.path.exists(f_audit_gpo):
  554. os.remove(f_audit_gpo)
  555. policy_name = "AuditCredentialValidation"
  556. result = self._test_policy(policy_name=policy_name)
  557. expected = "Not Configured"
  558. self.assertEqual(result, expected)
  559. def test_net_user_modal_mechanism(self):
  560. """
  561. Test getting the policy value using the NetUserModal mechanism
  562. """
  563. policy_name = "PasswordHistory"
  564. result = self._test_policy(policy_name=policy_name)
  565. expected = 0
  566. self.assertEqual(result, expected)
  567. def test_lsa_rights_mechanism(self):
  568. """
  569. Test getting the policy value using the LsaRights mechanism
  570. """
  571. policy_name = "SeTrustedCredManAccessPrivilege"
  572. result = self._test_policy(policy_name=policy_name)
  573. expected = []
  574. self.assertEqual(result, expected)
  575. def test_script_ini_mechanism(self):
  576. """
  577. Test getting the policy value using the ScriptIni value
  578. """
  579. policy_name = "StartupScripts"
  580. result = self._test_policy(policy_name=policy_name)
  581. expected = None
  582. self.assertEqual(result, expected)
  583. @destructiveTest
  584. @skipIf(not salt.utils.platform.is_windows(), "System is not Windows")
  585. class WinLGPOGetPointAndPrintNCTestCase(TestCase, LoaderModuleMockMixin):
  586. """
  587. Test variations of the Point and Print Restrictions policy when Not
  588. Configured (NC)
  589. """
  590. not_configured = False
  591. def setup_loader_modules(self):
  592. return LOADER_DICTS
  593. def setUp(self):
  594. if not self.not_configured:
  595. computer_policy = {"Point and Print Restrictions": "Not Configured"}
  596. win_lgpo.set_(computer_policy=computer_policy)
  597. self.not_configured = True
  598. def _get_policy_adm_setting(
  599. self, policy_name, policy_class, return_full_policy_names, hierarchical_return
  600. ):
  601. """
  602. Helper function to get current setting
  603. """
  604. # Get the policy
  605. success, policy_obj, _, _ = salt.modules.win_lgpo._lookup_admin_template(
  606. policy_name=policy_name, policy_class=policy_class, adml_language="en-US"
  607. )
  608. if success:
  609. return salt.modules.win_lgpo._get_policy_adm_setting(
  610. admx_policy=policy_obj,
  611. policy_class=policy_class,
  612. adml_language="en-US",
  613. return_full_policy_names=return_full_policy_names,
  614. hierarchical_return=hierarchical_return,
  615. )
  616. return "Policy Not Found"
  617. def test_point_and_print_not_configured(self):
  618. result = self._get_policy_adm_setting(
  619. policy_name="Point and Print Restrictions",
  620. policy_class="Machine",
  621. return_full_policy_names=False,
  622. hierarchical_return=False,
  623. )
  624. expected = {"PointAndPrint_Restrictions_Win7": "Not Configured"}
  625. self.assertDictEqual(result, expected)
  626. def test_point_and_print_not_configured_hierarchical(self):
  627. result = self._get_policy_adm_setting(
  628. policy_name="Point and Print Restrictions",
  629. policy_class="Machine",
  630. return_full_policy_names=False,
  631. hierarchical_return=True,
  632. )
  633. expected = {
  634. "Computer Configuration": {
  635. "Administrative Templates": {
  636. "Printers": {"PointAndPrint_Restrictions_Win7": "Not Configured"}
  637. }
  638. }
  639. }
  640. self.assertDictEqual(result, expected)
  641. def test_point_and_print_not_configured_full_names(self):
  642. result = self._get_policy_adm_setting(
  643. policy_name="Point and Print Restrictions",
  644. policy_class="Machine",
  645. return_full_policy_names=True,
  646. hierarchical_return=False,
  647. )
  648. expected = {"Printers\\Point and Print Restrictions": "Not Configured"}
  649. self.assertDictEqual(result, expected)
  650. def test_point_and_print_not_configured_full_names_hierarchical(self):
  651. result = self._get_policy_adm_setting(
  652. policy_name="Point and Print Restrictions",
  653. policy_class="Machine",
  654. return_full_policy_names=True,
  655. hierarchical_return=True,
  656. )
  657. expected = {
  658. "Computer Configuration": {
  659. "Administrative Templates": {
  660. "Printers": {"Point and Print Restrictions": "Not Configured"}
  661. }
  662. }
  663. }
  664. self.assertDictEqual(result, expected)
  665. @destructiveTest
  666. @skipIf(not salt.utils.platform.is_windows(), "System is not Windows")
  667. class WinLGPOGetPointAndPrintENTestCase(TestCase, LoaderModuleMockMixin):
  668. """
  669. Test variations of the Point and Print Restrictions policy when Enabled (EN)
  670. """
  671. configured = False
  672. def setup_loader_modules(self):
  673. return LOADER_DICTS
  674. def setUp(self):
  675. if not self.configured:
  676. computer_policy = {
  677. "Point and Print Restrictions": {
  678. "Users can only point and print to these servers": True,
  679. "Enter fully qualified server names separated by "
  680. "semicolons": "fakeserver1;fakeserver2",
  681. "Users can only point and print to machines in their "
  682. "forest": True,
  683. "When installing drivers for a new connection": "Show warning and elevation prompt",
  684. "When updating drivers for an existing connection": "Show warning only",
  685. },
  686. }
  687. win_lgpo.set_(computer_policy=computer_policy)
  688. self.configured = True
  689. def _get_policy_adm_setting(
  690. self, policy_name, policy_class, return_full_policy_names, hierarchical_return
  691. ):
  692. """
  693. Helper function to get current setting
  694. """
  695. # Get the policy
  696. success, policy_obj, _, _ = salt.modules.win_lgpo._lookup_admin_template(
  697. policy_name=policy_name, policy_class=policy_class, adml_language="en-US"
  698. )
  699. if success:
  700. results = salt.modules.win_lgpo._get_policy_adm_setting(
  701. admx_policy=policy_obj,
  702. policy_class=policy_class,
  703. adml_language="en-US",
  704. return_full_policy_names=return_full_policy_names,
  705. hierarchical_return=hierarchical_return,
  706. )
  707. if six.PY2:
  708. results = salt.states.win_lgpo._convert_to_unicode(results)
  709. return results
  710. return "Policy Not Found"
  711. @slowTest
  712. def test_point_and_print_enabled(self):
  713. result = self._get_policy_adm_setting(
  714. policy_name="Point and Print Restrictions",
  715. policy_class="Machine",
  716. return_full_policy_names=False,
  717. hierarchical_return=False,
  718. )
  719. expected = {
  720. "PointAndPrint_Restrictions_Win7": {
  721. "PointAndPrint_NoWarningNoElevationOnInstall_Enum": "Show warning and elevation prompt",
  722. "PointAndPrint_NoWarningNoElevationOnUpdate_Enum": "Show warning only",
  723. "PointAndPrint_TrustedForest_Chk": True,
  724. "PointAndPrint_TrustedServers_Chk": True,
  725. "PointAndPrint_TrustedServers_Edit": "fakeserver1;fakeserver2",
  726. }
  727. }
  728. self.assertDictEqual(result, expected)
  729. def test_point_and_print_enabled_hierarchical(self):
  730. result = self._get_policy_adm_setting(
  731. policy_name="Point and Print Restrictions",
  732. policy_class="Machine",
  733. return_full_policy_names=False,
  734. hierarchical_return=True,
  735. )
  736. expected = {
  737. "Computer Configuration": {
  738. "Administrative Templates": {
  739. "Printers": {
  740. "PointAndPrint_Restrictions_Win7": {
  741. "PointAndPrint_NoWarningNoElevationOnInstall_Enum": "Show warning and elevation prompt",
  742. "PointAndPrint_NoWarningNoElevationOnUpdate_Enum": "Show warning only",
  743. "PointAndPrint_TrustedForest_Chk": True,
  744. "PointAndPrint_TrustedServers_Chk": True,
  745. "PointAndPrint_TrustedServers_Edit": "fakeserver1;fakeserver2",
  746. }
  747. }
  748. }
  749. }
  750. }
  751. self.assertDictEqual(result, expected)
  752. def test_point_and_print_enabled_full_names(self):
  753. result = self._get_policy_adm_setting(
  754. policy_name="Point and Print Restrictions",
  755. policy_class="Machine",
  756. return_full_policy_names=True,
  757. hierarchical_return=False,
  758. )
  759. expected = {
  760. "Printers\\Point and Print Restrictions": {
  761. "Enter fully qualified server names separated by semicolons": "fakeserver1;fakeserver2",
  762. "When installing drivers for a new connection": "Show warning and elevation prompt",
  763. "Users can only point and print to machines in their forest": True,
  764. "Users can only point and print to these servers": True,
  765. "When updating drivers for an existing connection": "Show warning only",
  766. }
  767. }
  768. self.assertDictEqual(result, expected)
  769. @slowTest
  770. def test_point_and_print_enabled_full_names_hierarchical(self):
  771. result = self._get_policy_adm_setting(
  772. policy_name="Point and Print Restrictions",
  773. policy_class="Machine",
  774. return_full_policy_names=True,
  775. hierarchical_return=True,
  776. )
  777. expected = {
  778. "Computer Configuration": {
  779. "Administrative Templates": {
  780. "Printers": {
  781. "Point and Print Restrictions": {
  782. "Enter fully qualified server names separated by "
  783. "semicolons": "fakeserver1;fakeserver2",
  784. "When installing drivers for a new connection": "Show warning and elevation prompt",
  785. "Users can only point and print to machines in "
  786. "their forest": True,
  787. "Users can only point and print to these servers": True,
  788. "When updating drivers for an existing connection": "Show warning only",
  789. }
  790. }
  791. }
  792. }
  793. }
  794. self.assertDictEqual(result, expected)
  795. @skipIf(not salt.utils.platform.is_windows(), "System is not Windows")
  796. class WinLGPOGetPolicyFromPolicyResources(TestCase, LoaderModuleMockMixin):
  797. """
  798. Test functions related to policy info gathered from ADMX/ADML files
  799. """
  800. adml_data = None
  801. def setup_loader_modules(self):
  802. return LOADER_DICTS
  803. def setUp(self):
  804. if self.adml_data is None:
  805. self.adml_data = win_lgpo._get_policy_resources(language="en-US")
  806. def test__getAdmlPresentationRefId(self):
  807. ref_id = "LetAppsAccessAccountInfo_Enum"
  808. expected = "Default for all apps"
  809. result = win_lgpo._getAdmlPresentationRefId(self.adml_data, ref_id)
  810. self.assertEqual(result, expected)
  811. def test__getAdmlPresentationRefId_result_text_is_none(self):
  812. ref_id = "LetAppsAccessAccountInfo_UserInControlOfTheseApps_List"
  813. expected = (
  814. "Put user in control of these specific apps (use Package Family Names)"
  815. )
  816. result = win_lgpo._getAdmlPresentationRefId(self.adml_data, ref_id)
  817. self.assertEqual(result, expected)