Backport #38779 to 2016.11 (#39089)

* ignore ADMX/ADML files with syntax errors

* narrow except for XML Syntax Error on admx file

* attempt to remove any "encoding='unicode'" from ADMX/ADML files if lxml does not support the encoding

* lint fixes

* fix bonehead typo
This commit is contained in:
lomeroe 2017-02-01 16:50:42 -06:00 committed by Nicole Thomas
parent 8a00ecfb79
commit 8ce928f050

View file

@ -48,6 +48,7 @@ from salt.exceptions import SaltInvocationError
import salt.utils.dictupdate as dictupdate
from salt.ext.six import string_types
from salt.ext.six.moves import range
from salt.ext.six import StringIO
log = logging.getLogger(__name__)
__virtualname__ = 'lgpo'
@ -2043,7 +2044,21 @@ def _updatePolicyElements(policy_item, regkey):
return policy_item
def _processPolicyDefinitions(policy_def_path='c:\\Windows\\PolicyDefinitions', display_language='en-US'):
def _remove_unicode_encoding(xml_file):
'''
attempts to remove the "encoding='unicode'" from an xml file
as lxml does not support that on a windows node currently
see issue #38100
'''
with open(xml_file, 'rb') as f:
xml_content = f.read()
modified_xml = re.sub(r' encoding=[\'"]+unicode[\'"]+', '', xml_content.decode('utf-16'), count=1)
xmltree = lxml.etree.parse(StringIO(modified_xml))
return xmltree
def _processPolicyDefinitions(policy_def_path='c:\\Windows\\PolicyDefinitions',
display_language='en-US'):
'''
helper function to process all ADMX files in the specified policy_def_path
and build a single XML doc that we can search/use for admx polic processing
@ -2064,7 +2079,17 @@ def _processPolicyDefinitions(policy_def_path='c:\\Windows\\PolicyDefinitions',
for t_admfile in files:
admfile = os.path.join(root, t_admfile)
parser = lxml.etree.XMLParser(remove_comments=True)
xmltree = lxml.etree.parse(admfile, parser=parser)
# see issue #38100
try:
xmltree = lxml.etree.parse(admfile, parser=parser)
except lxml.etree.XMLSyntaxError:
try:
xmltree = _remove_unicode_encoding(admfile)
except Exception:
msg = ('A error was found while processing admx file {0},'
' all policies from this file will be unavailable via this module')
log.error(msg.format(admfile))
continue
namespaces = xmltree.getroot().nsmap
namespace_string = ''
if None in namespaces:
@ -2113,7 +2138,17 @@ def _processPolicyDefinitions(policy_def_path='c:\\Windows\\PolicyDefinitions',
raise SaltInvocationError(msg.format(display_language,
display_language_fallback,
t_admfile))
xmltree = lxml.etree.parse(adml_file)
try:
xmltree = lxml.etree.parse(adml_file)
except lxml.etree.XMLSyntaxError:
# see issue #38100
try:
xmltree = _remove_unicode_encoding(adml_file)
except Exception:
msg = ('An error was found while processing adml file {0}, all policy'
' languange data from this file will be unavailable via this module')
log.error(msg.format(adml_file))
continue
if None in namespaces:
namespaces['None'] = namespaces[None]
namespaces.pop(None)