From 55cb0257ec633d31242223dbaef41717ffc918f1 Mon Sep 17 00:00:00 2001 From: Twangboy Date: Tue, 30 Aug 2022 17:29:10 -0600 Subject: [PATCH] Fix get_policy KeyError on some policies Some policies with throw a KeyError when getting the policy setting using the get_policy function. --- changelog/61860.fixed | 1 + salt/modules/win_lgpo.py | 2 +- .../modules/win_lgpo/test_get_policy.py | 53 +++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 changelog/61860.fixed create mode 100644 tests/pytests/functional/modules/win_lgpo/test_get_policy.py diff --git a/changelog/61860.fixed b/changelog/61860.fixed new file mode 100644 index 00000000000..3b64fade195 --- /dev/null +++ b/changelog/61860.fixed @@ -0,0 +1 @@ +win_lgpo: Fixed intermittent KeyError when getting policy setting using lgpo.get_policy diff --git a/salt/modules/win_lgpo.py b/salt/modules/win_lgpo.py index 4521be68195..741c4b11ea1 100644 --- a/salt/modules/win_lgpo.py +++ b/salt/modules/win_lgpo.py @@ -9705,7 +9705,7 @@ def _get_policy_adm_setting( full_name ] = policy_item # go back and remove any "unpathed" policies that need a full path - for path_needed in unpathed_dict[policy_namespace]: + for path_needed in unpathed_dict.get(policy_namespace, {}): # remove the item with the same full name and re-add it w/a path'd version full_path_list = hierarchy[policy_namespace][ unpathed_dict[policy_namespace][path_needed] diff --git a/tests/pytests/functional/modules/win_lgpo/test_get_policy.py b/tests/pytests/functional/modules/win_lgpo/test_get_policy.py new file mode 100644 index 00000000000..58651f28c52 --- /dev/null +++ b/tests/pytests/functional/modules/win_lgpo/test_get_policy.py @@ -0,0 +1,53 @@ +import pytest + +pytestmark = [ + pytest.mark.windows_whitelisted, + pytest.mark.skip_unless_on_windows, +] + + +@pytest.fixture(scope="module") +def lgpo(modules): + return modules.lgpo + + +def test_hierarchical_return(lgpo): + result = lgpo.get_policy( + policy_name="Calculator", + policy_class="Machine", + hierarchical_return=True, + ) + result = result["Administrative Templates"] + result = result["Windows Components"] + result = result["Microsoft User Experience Virtualization"] + result = result["Applications"] + result = result["Calculator"] + assert result in ("Enabled", "Disabled", "Not Configured") + + +def test_return_value_only_false(lgpo): + result = lgpo.get_policy( + policy_name="Calculator", + policy_class="Machine", + return_value_only=False, + ) + assert result[ + r"Windows Components\Microsoft User Experience Virtualization\Applications\Calculator" + ] in ("Enabled", "Disabled", "Not Configured") + + +def test_return_full_policy_names_false(lgpo): + result = lgpo.get_policy( + policy_name="Calculator", + policy_class="Machine", + return_full_policy_names=False, + return_value_only=False, + ) + assert result["Calculator"] in ("Enabled", "Disabled", "Not Configured") + + +def test_61860_calculator(lgpo): + result = lgpo.get_policy(policy_name="Calculator", policy_class="Machine") + # Any of the following are valid settings. We're only making sure it doesn't + # throw a stacktrace + assert result in ("Enabled", "Disabled", "Not Configured")