From aed64f31afdf04bdd6e22f3b1efdd61a2d63174a Mon Sep 17 00:00:00 2001 From: parmentelat Date: Sun, 16 Dec 2018 17:42:31 +0100 Subject: [PATCH] a pass on some explicit encode calls that are obviously wrong in python3; some may remain --- PLC/Auth.py | 16 ++-------------- PLC/GPG.py | 2 +- PLC/Methods/GenerateNodeConfFile.py | 6 +++--- PLC/Methods/GetBootMedium.py | 1 - PLC/Methods/ResetPassword.py | 4 ++-- PLC/Methods/VerifyPerson.py | 4 ++-- PLC/Sessions.py | 4 ++-- 7 files changed, 12 insertions(+), 25 deletions(-) diff --git a/PLC/Auth.py b/PLC/Auth.py index c1684b5..927850e 100644 --- a/PLC/Auth.py +++ b/PLC/Auth.py @@ -6,10 +6,7 @@ # import crypt -try: - from hashlib import sha1 as sha -except ImportError: - import sha +from hashlib import sha1 as sha import hmac import time import os @@ -184,12 +181,6 @@ class BootAuth(Auth): PlanetLab version 3.x node authentication structure. Used by the Boot Manager to make authenticated calls to the API based on a unique node key or boot nonce value. - - The original parameter serialization code did not define the byte - encoding of strings, or the string encoding of all other types. We - define the byte encoding to be UTF-8, and the string encoding of - all other types to be however Python version 2.3 unicode() encodes - them. """ def __init__(self): @@ -245,10 +236,7 @@ class BootAuth(Auth): args.sort() msg = "[" + "".join(args) + "]" - # We encode in UTF-8 before calculating the HMAC, which is - # an 8-bit algorithm. - # python 2.6 insists on receiving a 'str' as opposed to a 'unicode' - digest = hmac.new(str(key), msg.encode('utf-8'), sha).hexdigest() + digest = hmac.new(key.encode('utf-8'), msg.encode('utf-8'), sha).hexdigest() if digest != auth['value']: raise PLCAuthenticationFailure( diff --git a/PLC/GPG.py b/PLC/GPG.py index 4a99884..3a73ef8 100644 --- a/PLC/GPG.py +++ b/PLC/GPG.py @@ -27,7 +27,7 @@ def canonicalize(args, methodname = None, methodresponse = False): xml = xmlrpc.client.dumps(args, methodname, methodresponse, encoding = 'utf-8', allow_none = 1) dom = etree.fromstring(xml) - canonical=etree.tostring(dom) + 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 diff --git a/PLC/Methods/GenerateNodeConfFile.py b/PLC/Methods/GenerateNodeConfFile.py index f2af06c..13a17b7 100644 --- a/PLC/Methods/GenerateNodeConfFile.py +++ b/PLC/Methods/GenerateNodeConfFile.py @@ -66,11 +66,11 @@ class GenerateNodeConfFile(Method): if regenerate_node_key: # Generate 32 random bytes - bytes = random.sample(range(0, 256), 32) + int8s = random.sample(range(0, 256), 32) # Base64 encode their string representation - node['key'] = base64.b64encode("".join(map(chr, bytes))) + node['key'] = base64.b64encode(bytes(int8s)) # XXX Boot Manager cannot handle = in the key - node['key'] = node['key'].replace("=", "") + node['key'] = node['key'].replace(b"=", b"") # Save it node.sync() diff --git a/PLC/Methods/GetBootMedium.py b/PLC/Methods/GetBootMedium.py index 00a4ea9..d9fc671 100644 --- a/PLC/Methods/GetBootMedium.py +++ b/PLC/Methods/GetBootMedium.py @@ -46,7 +46,6 @@ def compute_key(): # Base64 encode their string representation key = base64.b64encode(bytes(int8s)) # Boot Manager cannot handle = in the key - # XXX this sounds wrong, as it might prevent proper decoding key = key.replace(b"=", b"") return key diff --git a/PLC/Methods/ResetPassword.py b/PLC/Methods/ResetPassword.py index 8036969..6a35a4d 100644 --- a/PLC/Methods/ResetPassword.py +++ b/PLC/Methods/ResetPassword.py @@ -63,9 +63,9 @@ class ResetPassword(Method): raise PLCInvalidArgument("Cannot reset admin passwords") # Generate 32 random bytes - bytes = random.sample(range(0, 256), 32) + int8s = random.sample(range(0, 256), 32) # Base64 encode their string representation - random_key = base64.b64encode("".join(map(chr, bytes))) + random_key = base64.b64encode(bytes(int8s)) if verification_key is not None: if person['verification_key'] is None or \ diff --git a/PLC/Methods/VerifyPerson.py b/PLC/Methods/VerifyPerson.py index ef4e988..10ad1c2 100644 --- a/PLC/Methods/VerifyPerson.py +++ b/PLC/Methods/VerifyPerson.py @@ -63,9 +63,9 @@ class VerifyPerson(Method): site_name = "No Site" # Generate 32 random bytes - bytes = random.sample(range(0, 256), 32) + int8s = random.sample(range(0, 256), 32) # Base64 encode their string representation - random_key = base64.b64encode("".join(map(chr, bytes))) + random_key = base64.b64encode(bytes(int8s)) if verification_key is None or \ (verification_key is not None and person['verification_expires'] and \ diff --git a/PLC/Sessions.py b/PLC/Sessions.py index 511ed93..8dacc2e 100644 --- a/PLC/Sessions.py +++ b/PLC/Sessions.py @@ -50,9 +50,9 @@ class Session(Row): session.delete(commit) # Generate 32 random bytes - bytes = random.sample(range(0, 256), 32) + int8s = random.sample(range(0, 256), 32) # Base64 encode their string representation - self['session_id'] = base64.b64encode("".join(map(chr, bytes))) + self['session_id'] = base64.b64encode(bytes(int8s)) # Force insert insert = True -- 2.43.0