X-Git-Url: https://code.grnet.gr/git/ganeti-local/blobdiff_plain/615aaabaef1d13695ed625f6631c5785a966c06d..247ee81f3e3970506f082530f1eaf8f407bfa85f:/lib/serializer.py diff --git a/lib/serializer.py b/lib/serializer.py index 66f69d5..81f1add 100644 --- a/lib/serializer.py +++ b/lib/serializer.py @@ -38,7 +38,7 @@ from ganeti import utils _JSON_INDENT = 2 -_RE_EOLSP = re.compile('[ \t]+$', re.MULTILINE) +_RE_EOLSP = re.compile("[ \t]+$", re.MULTILINE) def _GetJsonDumpers(_encoder_class=simplejson.JSONEncoder): @@ -79,8 +79,8 @@ def DumpJson(data, indent=True): fn = _DumpJson txt = _RE_EOLSP.sub("", fn(data)) - if not txt.endswith('\n'): - txt += '\n' + if not txt.endswith("\n"): + txt += "\n" return txt @@ -108,17 +108,18 @@ def DumpSignedJson(data, key, salt=None, key_selector=None): """ txt = DumpJson(data, indent=False) if salt is None: - salt = '' + salt = "" signed_dict = { - 'msg': txt, - 'salt': salt, - } + "msg": txt, + "salt": salt, + } + if key_selector: signed_dict["key_selector"] = key_selector - message = salt + key_selector + txt else: - message = salt + txt - signed_dict["hmac"] = utils.Sha1Hmac(key, message) + key_selector = "" + + signed_dict["hmac"] = utils.Sha1Hmac(key, txt, salt=salt + key_selector) return DumpJson(signed_dict, indent=False) @@ -137,13 +138,13 @@ def LoadSignedJson(txt, key): """ signed_dict = LoadJson(txt) if not isinstance(signed_dict, dict): - raise errors.SignatureError('Invalid external message') + raise errors.SignatureError("Invalid external message") try: - msg = signed_dict['msg'] - salt = signed_dict['salt'] - hmac_sign = signed_dict['hmac'] + msg = signed_dict["msg"] + salt = signed_dict["salt"] + hmac_sign = signed_dict["hmac"] except KeyError: - raise errors.SignatureError('Invalid external message') + raise errors.SignatureError("Invalid external message") if callable(key): # pylint: disable-msg=E1103 @@ -156,8 +157,9 @@ def LoadSignedJson(txt, key): key_selector = "" hmac_key = key - if not utils.VerifySha1Hmac(hmac_key, salt + key_selector + msg, hmac_sign): - raise errors.SignatureError('Invalid Signature') + if not utils.VerifySha1Hmac(hmac_key, msg, hmac_sign, + salt=salt + key_selector): + raise errors.SignatureError("Invalid Signature") return LoadJson(msg), salt