X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=PLC%2FGPG.py;h=3a73ef8843cf320883dc3da3bab2f4e22aa1c53b;hb=4facb0674ab3f9e2deac6e92717b71cbd05f3c10;hp=ffecff09d36ef26f812895b37a74cabee3618b8a;hpb=4e770b3cf4e6b16150fd479ace9854eaee3399b6;p=plcapi.git diff --git a/PLC/GPG.py b/PLC/GPG.py index ffecff0..3a73ef8 100644 --- a/PLC/GPG.py +++ b/PLC/GPG.py @@ -9,14 +9,12 @@ # import os -import xmlrpclib +import xmlrpc.client import shutil -from types import StringTypes -from StringIO import StringIO -from xml.dom import minidom -from xml.dom.ext import Canonicalize +from io import StringIO from subprocess import Popen, PIPE, call from tempfile import NamedTemporaryFile, mkdtemp +from lxml import etree from PLC.Faults import * @@ -27,17 +25,15 @@ def canonicalize(args, methodname = None, methodresponse = False): True). """ - xml = xmlrpclib.dumps(args, methodname, methodresponse, encoding = 'utf-8', allow_none = 1) - dom = minidom.parseString(xml) - + xml = xmlrpc.client.dumps(args, methodname, methodresponse, encoding = 'utf-8', allow_none = 1) + dom = etree.fromstring(xml) + canonical = etree.tostring(dom) + # pre-f20 version was using Canonicalize from PyXML + # from xml.dom.ext import Canonicalize # Canonicalize(), though it claims to, does not encode unicode # nodes to UTF-8 properly and throws an exception unless you write # the stream to a file object, so just encode it ourselves. - buf = StringIO() - Canonicalize(dom, output = buf) - xml = buf.getvalue().encode('utf-8') - - return xml + return canonical.encode('utf-8') def gpg_export(keyring, armor = True): """ @@ -62,7 +58,7 @@ def gpg_export(keyring, armor = True): shutil.rmtree(homedir) if rc: - raise PLCAuthenticationFailure, "GPG export failed with return code %d: %s" % (rc, err) + raise PLCAuthenticationFailure("GPG export failed with return code %d: %s" % (rc, err)) return export @@ -79,7 +75,7 @@ def gpg_sign(args, secret_keyring, keyring, methodname = None, methodresponse = """ # Accept either an opaque string blob or a Python tuple - if isinstance(args, StringTypes): + if isinstance(args, str): message = args elif isinstance(args, tuple): message = canonicalize(args, methodname, methodresponse) @@ -110,7 +106,7 @@ def gpg_sign(args, secret_keyring, keyring, methodname = None, methodresponse = shutil.rmtree(homedir) if rc: - raise PLCAuthenticationFailure, "GPG signing failed with return code %d: %s" % (rc, err) + raise PLCAuthenticationFailure("GPG signing failed with return code %d: %s" % (rc, err)) return signature @@ -126,7 +122,7 @@ def gpg_verify(args, key, signature = None, methodname = None, methodresponse = """ # Accept either an opaque string blob or a Python tuple - if isinstance(args, StringTypes): + if isinstance(args, str): message = args else: message = canonicalize(args, methodname, methodresponse) @@ -176,6 +172,6 @@ def gpg_verify(args, key, signature = None, methodname = None, methodresponse = keyfile.close() if rc: - raise PLCAuthenticationFailure, "GPG verification failed with return code %d: %s" % (rc, err) + raise PLCAuthenticationFailure("GPG verification failed with return code %d: %s" % (rc, err)) return message