Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encryption backend will fail for environments migrated from python2 #441

Open
Ashex opened this issue Apr 26, 2021 · 0 comments
Open

Encryption backend will fail for environments migrated from python2 #441

Ashex opened this issue Apr 26, 2021 · 0 comments
Labels
Milestone

Comments

@Ashex
Copy link
Collaborator

Ashex commented Apr 26, 2021

When migrating from python 2.7 runtime to python 3 (3.8 for me) will will fail to load anything from redis with the following exception (once revealed):

Traceback (most recent call last):
File "/opt/willbot/will/backends/encryption/aes.py", line 57, in decrypt_from_b64
return pickle.loads(binascii.a2b_base64(decrypted_data.decode('utf8')))
File "/opt/pyenv/lib/python3.8/site-packages/dill/_dill.py", line 283, in loads
return load(file, ignore, **kwds)
File "/opt/pyenv/lib/python3.8/site-packages/dill/_dill.py", line 278, in load
return Unpickler(file, ignore=ignore, **kwds).load()
File "/opt/pyenv/lib/python3.8/site-packages/dill/_dill.py", line 481, in load
obj = StockUnpickler.load(self)
UnicodeDecodeError: 'ascii' codec can't decode byte 0x80 in position 0: ordinal not in range(128)

The fix is to explicitly tell pickle what encoding to use:

    @staticmethod
    def decrypt_from_b64(enc):
        'decrypt b64-encoded data'
        try:
            if b'/' in enc and enc.index(b'/') == BS:
                iv = enc[:BS]
                encrypted_data = enc[BS+1:]
                cipher = AES.new(key, AES.MODE_CBC, iv)
                decrypted_data = unpad(cipher.decrypt(binascii.a2b_base64(encrypted_data)))
            return pickle.loads(binascii.a2b_base64(decrypted_data), encoding='bytes')
        except (KeyboardInterrupt, SystemExit):
            pass
        except Exception:
            logging.debug("Error decrypting.  Attempting unencrypted load to ease migration.")
            return pickle.loads(binascii.a2b_base64(enc))

Reference: https://stackoverflow.com/a/28218598

@Ashex Ashex added the bug label Apr 26, 2021
@Ashex Ashex added this to the 2.2 milestone Apr 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant