Add the ability to clear the context policy cached

This commit is contained in:
Shane Lee 2024-04-12 13:47:28 -06:00 committed by Pedro Algarvio
parent 483ca17006
commit 6fc0fa2e02
4 changed files with 57 additions and 0 deletions

3
changelog/62734.fixed.md Normal file
View file

@ -0,0 +1,3 @@
Fixed an issue with adding new machine policies and applying those same
policies in the same state by adding a ``refresh_cache`` option to the
``lgpo.set`` state.

View file

@ -5314,6 +5314,25 @@ def _get_policy_definitions(path="c:\\Windows\\PolicyDefinitions", language="en-
return __context__["lgpo.policy_definitions"]
def clear_policy_cache():
"""
Clears the policy definitions and resource stored in ``__context__``. They
will be rebuilt the next time a policy is applied.
CLI Example:
.. code-block:: bash
salt '*' lgpo.clear_policy_cache
"""
if "lgpo.policy_definitions" in __context__:
log.debug("LGPO: Removing cached policy definitions")
__context__.pop("lgpo.policy_definitions")
if "lgpo.policy_resources" in __context__:
log.debug("LGPO: Removing cached policy resources")
__context__.pop("lgpo.policy_resources")
def _get_policy_resources(path="c:\\Windows\\PolicyDefinitions", language="en-US"):
if "lgpo.policy_resources" not in __context__:
log.debug("LGPO: Loading policy resources")

View file

@ -285,6 +285,7 @@ def set_(
user_policy=None,
cumulative_rights_assignments=True,
adml_language="en-US",
refresh_cache=False,
):
"""
Ensure the specified policy is set.
@ -323,6 +324,18 @@ def set_(
adml_language (str):
The adml language to use for AMDX policy data/display conversions.
Default is ``en-US``
refresh_cache (bool):
Clear the cached policy definitions before applying the state. This
is useful when the underlying policy files (ADMX/ADML) have been
added/modified in the same state. This will allow those new policies
to be picked up. This adds time to the state run when applied to
multiple states within the same run. Therefore, it is best to only
apply this to the first policy that is applied. For individual runs
this will have no effect. Default is ``False``
.. versionadded:: 3006.8
.. versionadded:: 3007.1
"""
ret = {"name": name, "result": True, "changes": {}, "comment": ""}
policy_classes = ["machine", "computer", "user", "both"]
@ -387,6 +400,10 @@ def set_(
"machine": {"requested_policy": computer_policy, "policy_lookup": {}},
}
if refresh_cache:
# Remove cached policies so new policies can be picked up
__salt__["lgpo.clear_policy_cache"]()
current_policy = {}
deprecation_comments = []
for p_class, p_data in pol_data.items():

View file

@ -18,6 +18,7 @@ import salt.modules.win_file as win_file
import salt.modules.win_lgpo as win_lgpo
import salt.utils.files
import salt.utils.win_dacl as win_dacl
from tests.support.mock import patch
log = logging.getLogger(__name__)
@ -108,6 +109,19 @@ def lgpo_bin():
yield str(sys_dir / "lgpo.exe")
def test_clear_policy_cache():
context = {
"lgpo.policy_definitions": "spongebob",
"lgpo.policy_resources": "squarepants",
}
with patch.dict(win_lgpo.__context__, context):
assert "lgpo.policy_definitions" in win_lgpo.__context__
assert "lgpo.policy_resources" in win_lgpo.__context__
win_lgpo.clear_policy_cache()
assert "lgpo.policy_definitions" not in win_lgpo.__context__
assert "lgpo.policy_resources" not in win_lgpo.__context__
@pytest.mark.destructive_test
def test__load_policy_definitions():
"""
@ -449,6 +463,7 @@ def _test_set_user_policy(lgpo_bin, shell, name, setting, exp_regexes):
),
],
)
@pytest.mark.destructive_test
def test_set_computer_policy(clean_comp, lgpo_bin, shell, name, setting, exp_regexes):
_test_set_computer_policy(
lgpo_bin=lgpo_bin,
@ -519,6 +534,7 @@ def test_set_computer_policy(clean_comp, lgpo_bin, shell, name, setting, exp_reg
),
],
)
@pytest.mark.destructive_test
def test_set_user_policy(clean_user, lgpo_bin, shell, name, setting, exp_regexes):
_test_set_user_policy(
lgpo_bin=lgpo_bin,
@ -529,6 +545,7 @@ def test_set_user_policy(clean_user, lgpo_bin, shell, name, setting, exp_regexes
)
@pytest.mark.destructive_test
def test_set_computer_policy_windows_update(clean_comp, lgpo_bin, shell):
"""
Test setting/unsetting/changing WindowsUpdate policy
@ -668,6 +685,7 @@ def test_set_computer_policy_windows_update(clean_comp, lgpo_bin, shell):
)
@pytest.mark.destructive_test
def test_set_computer_policy_multiple_policies(clean_comp, lgpo_bin, shell):
"""
Tests setting several ADMX policies in succession and validating the