Skip to content

Commit

Permalink
Merge pull request #56310 from twangboy/fix_lgpo_admx
Browse files Browse the repository at this point in the history
Only process ADMX files when loading policies
  • Loading branch information
dwoz authored Mar 11, 2020
2 parents 2d78931 + 4bc5b05 commit 19bb6aa
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
17 changes: 10 additions & 7 deletions salt/modules/win_lgpo.py
Original file line number Diff line number Diff line change
Expand Up @@ -4986,6 +4986,12 @@ def _load_policy_definitions(path='c:\\Windows\\PolicyDefinitions',
for root, dirs, files in salt.utils.path.os_walk(path):
if root == path:
for t_admx_file in files:
admx_file_name, admx_file_ext = os.path.splitext(t_admx_file)
# Only process ADMX files, any other file will cause a
# stacktrace later on
if not admx_file_ext == '.admx':
log.debug('{0} is not an ADMX file'.format(t_admx_file))
continue
admx_file = os.path.join(root, t_admx_file)
# Parse xml for the ADMX file
try:
Expand All @@ -5001,9 +5007,6 @@ def _load_policy_definitions(path='c:\\Windows\\PolicyDefinitions',
namespaces['None'] = namespaces[None]
namespaces.pop(None)
namespace_string = 'None:'
this_prefix = xml_tree.xpath(
'/{0}policyDefinitions/{0}policyNamespaces/{0}target/@prefix'.format(namespace_string),
namespaces=namespaces)[0]
this_namespace = xml_tree.xpath(
'/{0}policyDefinitions/{0}policyNamespaces/{0}target/@namespace'.format(namespace_string),
namespaces=namespaces)[0]
Expand Down Expand Up @@ -5038,7 +5041,7 @@ def _load_policy_definitions(path='c:\\Windows\\PolicyDefinitions',
adml_file = os.path.join(
root,
language,
os.path.splitext(t_admx_file)[0] + '.adml')
admx_file_name + '.adml')
if not __salt__['file.file_exists'](adml_file):
log.info('An ADML file in the specified ADML language '
'"%s" does not exist for the ADMX "%s", the '
Expand All @@ -5048,7 +5051,7 @@ def _load_policy_definitions(path='c:\\Windows\\PolicyDefinitions',
adml_file = os.path.join(
root,
language.split('-')[0],
os.path.splitext(t_admx_file)[0] + '.adml')
admx_file_name + '.adml')
if not __salt__['file.file_exists'](adml_file):
log.info('An ADML file in the specified ADML language '
'code %s does not exist for the ADMX "%s", '
Expand All @@ -5058,7 +5061,7 @@ def _load_policy_definitions(path='c:\\Windows\\PolicyDefinitions',
adml_file = os.path.join(
root,
display_language_fallback,
os.path.splitext(t_admx_file)[0] + '.adml')
admx_file_name + '.adml')
if not __salt__['file.file_exists'](adml_file):
log.info('An ADML file in the specified ADML '
'fallback language "%s" '
Expand All @@ -5070,7 +5073,7 @@ def _load_policy_definitions(path='c:\\Windows\\PolicyDefinitions',
adml_file = os.path.join(
root,
display_language_fallback.split('-')[0],
os.path.splitext(t_admx_file)[0] + '.adml')
admx_file_name + '.adml')
if not __salt__['file.file_exists'](adml_file):
msg = ('An ADML file in the specified ADML language '
'"{0}" and the fallback language "{1}" do not '
Expand Down
35 changes: 35 additions & 0 deletions tests/unit/modules/test_win_lgpo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

# Import Python Libs
from __future__ import absolute_import, unicode_literals, print_function
import glob
import os

# Import Salt Testing Libs
Expand All @@ -18,6 +19,7 @@
import salt.loader
import salt.modules.win_lgpo as win_lgpo
import salt.states.win_lgpo
import salt.utils.files
import salt.utils.platform
import salt.utils.stringutils

Expand Down Expand Up @@ -332,6 +334,39 @@ def test_get_policy_name_return_full_names_hierarchical(self):
'Allow Telemetry': 'Not Configured'}}}}}
self.assertDictEqual(result, expected)

@destructiveTest
def test__load_policy_definitions(self):
'''
Test that unexpected files in the PolicyDefinitions directory won't
cause the _load_policy_definitions function to explode
https://gitlab.com/saltstack/enterprise/lock/issues/3826
'''
# The PolicyDefinitions directory should only contain ADMX files. We
# want to make sure the `_load_policy_definitions` function skips non
# ADMX files in this directory.
# Create a bogus ADML file in PolicyDefinitions directory
bogus_fle = os.path.join(
'c:\\Windows\\PolicyDefinitions',
'_bogus.adml')
cache_dir = os.path.join(
win_lgpo.__opts__['cachedir'],
'lgpo',
'policy_defs')
try:
with salt.utils.files.fopen(bogus_fle, 'w+') as fh:
fh.write('<junk></junk>')
# This function doesn't return anything (None), it just loads
# the XPath structures into __context__. We're just making sure it
# doesn't stack trace here
self.assertIsNone(win_lgpo._load_policy_definitions())
finally:
# Remove source file
os.remove(bogus_fle)
# Remove cached file
search_string = '{0}\\_bogus*.adml'.format(cache_dir)
for file_name in glob.glob(search_string):
os.remove(file_name)


@skipIf(not salt.utils.platform.is_windows(), 'System is not Windows')
class WinLGPOGetPolicyFromPolicyInfoTestCase(TestCase, LoaderModuleMockMixin):
Expand Down

0 comments on commit 19bb6aa

Please sign in to comment.