String-related fixes for Python 3

- Check if a variable is a strings using isinstance and six.
  At least one of the two checks before the fix triggers
  an exception visible in the logs when using internal
  job binaries ("Job binary internal data must be a string of
  length greater than zero").
- Encode a string that is passed to md5, as the error suggests
  ("Unicode-objects must be encoded before hashing")

Change-Id: Icb5d75bdbfb83070c579b9b99a395f344c3120ce
This commit is contained in:
Luigi Toscano 2018-12-04 22:54:37 +01:00
parent be47e5c134
commit 7e24f288c4
3 changed files with 6 additions and 3 deletions

View File

@ -100,7 +100,8 @@ class HashRing(Coordinator):
@staticmethod
def _hash(key):
return int(hashlib.md5(str(key)).hexdigest(), 16) # nosec
return int(
hashlib.md5(str(key).encode('utf-8')).hexdigest(), 16) # nosec
def _build_ring(self):
ring = {}

View File

@ -14,6 +14,7 @@
# limitations under the License.
from oslo_config import cfg
import six
import sahara.exceptions as e
from sahara.i18n import _
@ -31,7 +32,7 @@ def check_job_binary_internal(data, **kwargs):
if not is_internal_db_enabled():
raise e.BadJobBinaryInternalException(
_("Sahara internal db is disabled for storing job binaries."))
if not (type(data) is str and len(data) > 0):
if not (isinstance(data, six.string_types) and len(data) > 0):
raise e.BadJobBinaryInternalException()
if "name" in kwargs:
name = kwargs["name"]

View File

@ -18,6 +18,7 @@ import traceback
import flask
from oslo_log import log as logging
from oslo_middleware import request_id as oslo_req_id
import six
from werkzeug import datastructures
from sahara import context
@ -53,7 +54,7 @@ class Rest(flask.Blueprint):
return self._mroute('PATCH', rule, status_code)
def _mroute(self, methods, rule, status_code=None, **kw):
if type(methods) is str:
if isinstance(methods, six.string_types):
methods = [methods]
return self.route(rule, methods=methods, status_code=status_code, **kw)