diff --git a/lib/compat.py b/lib/compat.py index 692ab246f9845ec7f48379049e2e835a8b325c2d..db3ae2874cf955c78af59c4408dbb5ad42bf0a09 100644 --- a/lib/compat.py +++ b/lib/compat.py @@ -31,6 +31,25 @@ except ImportError: functools = None +# compat.md5_hash and compat.sha1_hash can be called to generate and md5 and a +# sha1 hashing modules, under python 2.4, 2.5 and 2.6, even though some changes +# went on. compat.sha1 is python-version specific and is used for python +# modules (hmac, for example) which have changed their behavior as well from +# one version to the other. +try: + # Yes, we're not using the imports in this module. + # pylint: disable-msg=W0611 + from hashlib import md5 as md5_hash + from hashlib import sha1 as sha1_hash + # this additional version is needed for compatibility with the hmac module + sha1 = sha1_hash +except ImportError: + from md5 import new as md5_hash + import sha + sha1 = sha + sha1_hash = sha.new + + def all(seq, pred=bool): # pylint: disable-msg=W0622 """Returns True if pred(x) is True for every element in the iterable. diff --git a/lib/http/auth.py b/lib/http/auth.py index f71ede0979e43656f0b4d4219f4a2c2a6169e6d2..f66f54bf9eba186c7bb0b33b04cdf78f86efd410 100644 --- a/lib/http/auth.py +++ b/lib/http/auth.py @@ -28,16 +28,11 @@ import base64 import binascii from ganeti import utils +from ganeti import compat from ganeti import http from cStringIO import StringIO -try: - from hashlib import md5 -except ImportError: - from md5 import new as md5 - - # Digest types from RFC2617 HTTP_BASIC_AUTH = "Basic" HTTP_DIGEST_AUTH = "Digest" @@ -271,7 +266,7 @@ class HttpServerRequestAuthentication(object): # There can not be a valid password for this case raise AssertionError("No authentication realm") - expha1 = md5() + expha1 = compat.md5_hash() expha1.update("%s:%s:%s" % (username, realm, password)) return (expected_password.lower() == expha1.hexdigest().lower()) diff --git a/lib/utils.py b/lib/utils.py index 7b93870d898164c2f81f402fbbb7b8368d611a71..f2f00b56b89e2ecd7634e36306b4e35ac9e168dc 100644 --- a/lib/utils.py +++ b/lib/utils.py @@ -54,11 +54,6 @@ import IN from cStringIO import StringIO -try: - from hashlib import sha1 -except ImportError: - import sha as sha1 - try: import ctypes except ImportError: @@ -66,6 +61,7 @@ except ImportError: from ganeti import errors from ganeti import constants +from ganeti import compat _locksheld = [] @@ -748,10 +744,7 @@ def _FingerprintFile(filename): f = open(filename) - if callable(sha1): - fp = sha1() - else: - fp = sha1.new() + fp = compat.sha1_hash() while True: data = f.read(4096) if not data: @@ -2799,7 +2792,7 @@ def Sha1Hmac(key, text, salt=None): else: salted_text = text - return hmac.new(key, salted_text, sha1).hexdigest() + return hmac.new(key, salted_text, compat.sha1).hexdigest() def VerifySha1Hmac(key, text, digest, salt=None): diff --git a/test/ganeti.utils_unittest.py b/test/ganeti.utils_unittest.py index 511e48a546b24c32a0ada1ccdef650dcb6fc5590..d72232ba470fd7dc68d869b6ed8a7aa580cc6943 100755 --- a/test/ganeti.utils_unittest.py +++ b/test/ganeti.utils_unittest.py @@ -39,12 +39,12 @@ import OpenSSL import warnings import distutils.version import glob -import md5 import errno import ganeti import testutils from ganeti import constants +from ganeti import compat from ganeti import utils from ganeti import errors from ganeti import serializer @@ -670,7 +670,7 @@ class TestReadFile(testutils.GanetiTestCase): data = utils.ReadFile(self._TestDataFilename("cert1.pem")) self.assertEqual(len(data), 814) - h = md5.new() + h = compat.md5_hash() h.update(data) self.assertEqual(h.hexdigest(), "a491efb3efe56a0535f924d5f8680fd4") @@ -679,7 +679,7 @@ class TestReadFile(testutils.GanetiTestCase): size=100) self.assertEqual(len(data), 100) - h = md5.new() + h = compat.md5_hash() h.update(data) self.assertEqual(h.hexdigest(), "893772354e4e690b9efd073eed433ce7")