Use xml.etree in ipa-client-automount script

The ipa-client-automount script used lxml.etree to modify
/etc/autofs_ldap_auth.conf.

Signed-off-by: Christian Heimes <cheimes@redhat.com>
Reviewed-By: Martin Basti <mbasti@redhat.com>
This commit is contained in:
Christian Heimes 2016-11-16 11:11:13 +01:00 committed by Martin Basti
parent 64af88fee4
commit 9fbd29cc10

View File

@ -29,6 +29,11 @@ import time
import tempfile import tempfile
import gssapi import gssapi
try:
from xml.etree import cElementTree as etree
except ImportError:
from xml.etree import ElementTree as etree
import SSSDConfig import SSSDConfig
# pylint: disable=import-error # pylint: disable=import-error
from six.moves.urllib.parse import urlsplit from six.moves.urllib.parse import urlsplit
@ -94,40 +99,34 @@ def wait_for_sssd():
print("This may mean that sssd didn't re-start properly after the configuration changes.") print("This may mean that sssd didn't re-start properly after the configuration changes.")
def configure_xml(fstore): def configure_xml(fstore):
from lxml import etree authconf = paths.AUTOFS_LDAP_AUTH_CONF
fstore.backup_file(authconf)
fstore.backup_file(paths.AUTOFS_LDAP_AUTH_CONF)
try: try:
f = open(paths.AUTOFS_LDAP_AUTH_CONF, 'r') tree = etree.parse(authconf)
lines = f.read()
f.close()
saslconf = etree.fromstring(lines)
element = saslconf.xpath('//autofs_ldap_sasl_conf')
root = saslconf.getroottree()
except IOError as e: except IOError as e:
root_logger.debug('Unable to open file %s' % e) root_logger.debug('Unable to open file %s' % e)
root_logger.debug('Creating new from template') root_logger.debug('Creating new from template')
element = [etree.Element('autofs_ldap_sasl_conf')] tree = etree.ElementTree(
root = element[0].getroottree() element=etree.Element('autofs_ldap_sasl_conf')
)
if len(element) != 1: element = tree.getroot()
raise RuntimeError('Unable to parse %s' % paths.AUTOFS_LDAP_AUTH_CONF) if element.tag != 'autofs_ldap_sasl_conf':
raise RuntimeError('Invalid XML root in file %s' % authconf)
element[0].set('usetls', 'no') element.set('usetls', 'no')
element[0].set('tlsrequired', 'no') element.set('tlsrequired', 'no')
element[0].set('authrequired', 'yes') element.set('authrequired', 'yes')
element[0].set('authtype', 'GSSAPI') element.set('authtype', 'GSSAPI')
element[0].set('clientprinc', 'host/%s@%s' % (api.env.host, api.env.realm)) element.set('clientprinc', 'host/%s@%s' % (api.env.host, api.env.realm))
newconf = open(paths.AUTOFS_LDAP_AUTH_CONF, 'w')
try: try:
root.write(newconf, pretty_print=True, xml_declaration=True, encoding='UTF-8') tree.write(authconf, xml_declaration=True, encoding='UTF-8')
newconf.close()
except IOError as e: except IOError as e:
print("Unable to write %s: %s" % (paths.AUTOFS_LDAP_AUTH_CONF, e)) print("Unable to write %s: %s" % (authconf, e))
print("Configured %s" % paths.AUTOFS_LDAP_AUTH_CONF) else:
print("Configured %s" % authconf)
def configure_nsswitch(fstore, options): def configure_nsswitch(fstore, options):
""" """