From bf1ee443b4322709f445a6f94c53c4c1a6cafa90 Mon Sep 17 00:00:00 2001 From: parmentelat Date: Thu, 20 Dec 2018 12:38:41 +0100 Subject: [PATCH] second pass on all uses of base64encodings, so at to always return str like it was with python2 otherwise we are breaking compatibility --- PLC/Methods/GenerateNodeConfFile.py | 4 ++-- PLC/Methods/GetBootMedium.py | 13 +++++++------ PLC/Methods/ResetPassword.py | 2 +- PLC/Methods/VerifyPerson.py | 2 +- PLC/Sessions.py | 2 +- 5 files changed, 12 insertions(+), 11 deletions(-) diff --git a/PLC/Methods/GenerateNodeConfFile.py b/PLC/Methods/GenerateNodeConfFile.py index 13a17b7..f7f7bcf 100644 --- a/PLC/Methods/GenerateNodeConfFile.py +++ b/PLC/Methods/GenerateNodeConfFile.py @@ -68,9 +68,9 @@ class GenerateNodeConfFile(Method): # Generate 32 random bytes int8s = random.sample(range(0, 256), 32) # Base64 encode their string representation - node['key'] = base64.b64encode(bytes(int8s)) + node['key'] = base64.b64encode(bytes(int8s)).decode() # XXX Boot Manager cannot handle = in the key - node['key'] = node['key'].replace(b"=", b"") + node['key'] = node['key'].replace("=", "") # Save it node.sync() diff --git a/PLC/Methods/GetBootMedium.py b/PLC/Methods/GetBootMedium.py index 7b73a93..203b968 100644 --- a/PLC/Methods/GetBootMedium.py +++ b/PLC/Methods/GetBootMedium.py @@ -44,9 +44,11 @@ def compute_key(): # Generate 32 random bytes int8s = random.sample(range(0, 256), 32) # Base64 encode their string representation - key = base64.b64encode(bytes(int8s)) + # and, importantly, decode back to return a str + # this is important as otherwise we pollute node.txt + key = base64.b64encode(bytes(int8s)).decode() # Boot Manager cannot handle = in the key - key = key.replace(b"=", b"") + key = key.replace("=", "") return key class GetBootMedium(Method): @@ -538,8 +540,8 @@ class GetBootMedium(Method): .format(generic_path, filename)) else: ### return the generic medium content as-is, just base64 encoded - with open(generic_path) as feed: - return base64.b64encode(feed.read()) + with open(generic_path, "rb") as feed: + return base64.b64encode(feed.read()).decode() ### config file preview or regenerated if action == 'node-preview' or action == 'node-floppy': @@ -625,10 +627,9 @@ class GetBootMedium(Method): self.trash.append(node_image) self.cleantrash() logger.info("GetBootMedium - done with build.sh") - encoded_result = base64.b64encode(result) # stupidly enough, we need to decode this as str now # so that we remain compatible with former python2 PLCAPI - encoded_result = encoded_result.decode() + encoded_result = base64.b64encode(result).decode() logger.info("GetBootMedium - done with base64 encoding -" " lengths: raw={} - b64={}" .format(len(result), len(encoded_result))) diff --git a/PLC/Methods/ResetPassword.py b/PLC/Methods/ResetPassword.py index 6a35a4d..aecf67f 100644 --- a/PLC/Methods/ResetPassword.py +++ b/PLC/Methods/ResetPassword.py @@ -65,7 +65,7 @@ class ResetPassword(Method): # Generate 32 random bytes int8s = random.sample(range(0, 256), 32) # Base64 encode their string representation - random_key = base64.b64encode(bytes(int8s)) + random_key = base64.b64encode(bytes(int8s)).decode() 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 10ad1c2..6dc4484 100644 --- a/PLC/Methods/VerifyPerson.py +++ b/PLC/Methods/VerifyPerson.py @@ -65,7 +65,7 @@ class VerifyPerson(Method): # Generate 32 random bytes int8s = random.sample(range(0, 256), 32) # Base64 encode their string representation - random_key = base64.b64encode(bytes(int8s)) + random_key = base64.b64encode(bytes(int8s)).decode() 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 8dacc2e..720565d 100644 --- a/PLC/Sessions.py +++ b/PLC/Sessions.py @@ -52,7 +52,7 @@ class Session(Row): # Generate 32 random bytes int8s = random.sample(range(0, 256), 32) # Base64 encode their string representation - self['session_id'] = base64.b64encode(bytes(int8s)) + self['session_id'] = base64.b64encode(bytes(int8s)).decode() # Force insert insert = True -- 2.43.0