From f36ac6022a2a3d5b067387908aa31932234a31e1 Mon Sep 17 00:00:00 2001 From: Lumir Balhar Date: Fri, 17 Apr 2020 08:01:22 +0200 Subject: [PATCH] Backwards compatibility for base64 module --- dns/tsigkeyring.py | 16 ++++++++++++---- tests/test_tsigkeyring.py | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 tests/test_tsigkeyring.py diff --git a/dns/tsigkeyring.py b/dns/tsigkeyring.py index 5e5fe1c..1dab2aa 100644 --- a/dns/tsigkeyring.py +++ b/dns/tsigkeyring.py @@ -19,7 +19,14 @@ from dns._compat import maybe_decode, maybe_encode -import base64 + +try: + # New in version 3.1 + from base64 import decodebytes, encodebytes +except ImportError: + # Deprecated since version 3.1 and removed since 3.9 + from base64 import decodestring as decodebytes + from base64 import encodestring as encodebytes import dns.name @@ -32,7 +39,7 @@ def from_text(textring): keyring = {} for keytext in textring: keyname = dns.name.from_text(keytext) - secret = base64.decodestring(maybe_encode(textring[keytext])) + secret = decodebytes(textring[keytext].encode()) keyring[keyname] = secret return keyring @@ -44,7 +51,8 @@ def to_text(keyring): textring = {} for keyname in keyring: - keytext = maybe_decode(keyname.to_text()) - secret = maybe_decode(base64.encodestring(keyring[keyname])) + keytext = keyname.to_text() + # rstrip to get rid of the \n encoding adds + secret = encodebytes(keyring[keyname]).decode().rstrip() textring[keytext] = secret return textring diff --git a/tests/test_tsigkeyring.py b/tests/test_tsigkeyring.py new file mode 100644 index 0000000..17177c0 --- /dev/null +++ b/tests/test_tsigkeyring.py @@ -0,0 +1,39 @@ +# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license + +try: + # New in version 3.1 + from base64 import decodebytes +except ImportError: + # Deprecated since version 3.1 and removed since 3.9 + from base64 import decodestring as decodebytes + +import unittest + +import dns.tsigkeyring + +text_keyring = { + 'keyname.' : 'NjHwPsMKjdN++dOfE5iAiQ==' +} + +rich_keyring = { + dns.name.from_text('keyname.') : \ + decodebytes('NjHwPsMKjdN++dOfE5iAiQ=='.encode()) +} + +class TSIGKeyRingTestCase(unittest.TestCase): + + def test_from_text(self): + """text keyring -> rich keyring""" + rkeyring = dns.tsigkeyring.from_text(text_keyring) + self.assertEqual(rkeyring, rich_keyring) + + def test_to_text(self): + """text keyring -> rich keyring -> text keyring""" + tkeyring = dns.tsigkeyring.to_text(rich_keyring) + self.assertEqual(tkeyring, text_keyring) + + def test_from_and_to_text(self): + """text keyring -> rich keyring -> text keyring""" + rkeyring = dns.tsigkeyring.from_text(text_keyring) + tkeyring = dns.tsigkeyring.to_text(rkeyring) + self.assertEqual(tkeyring, text_keyring) -- 2.25.2