From 4166da70b30291e8de74cf8c18e27753d0902ace Mon Sep 17 00:00:00 2001 From: Feng Xi Yan Date: Wed, 24 Sep 2014 18:37:02 +0800 Subject: [PATCH] Fix signature validation failure when using qpid message queue When using qpid message queue, the metering message will get signature validation failure because of extra unicode encoding given by json loads. This change peels off unicode of the message so that this issue can be avoided. Change-Id: Ia23af789460ad8597867902f713b8a78f4a09e06 Closes-Bug: 1373356 --- ceilometer/tests/test_utils.py | 12 +++-------- ceilometer/utils.py | 38 ++++++++++++++++++++-------------- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/ceilometer/tests/test_utils.py b/ceilometer/tests/test_utils.py index bda3de191..b77410187 100644 --- a/ceilometer/tests/test_utils.py +++ b/ceilometer/tests/test_utils.py @@ -72,7 +72,7 @@ class TestUtils(base.BaseTestCase): big = 1 << 64 expected = [('a', 'A'), ('b', 'B'), - ('nested:list', ['{%d: 99, %d: 42}' % (small, big)])] + ('nested:list', [{small: 99, big: 42}])] data = {'a': 'A', 'b': 'B', 'nested': {'list': [{small: 99, big: 42}]}} @@ -82,14 +82,8 @@ class TestUtils(base.BaseTestCase): # the keys 1 and 1<<64 cause a hash collision on 64bit platforms if k == 'nested:list': self.assertIn(v, - [[('{%d: 99, %d: 42}' - % (small, big)).encode('ascii')], - [('{%d: 99, %dL: 42}' - % (small, big)).encode('ascii')], - [('{%d: 42, %d: 99}' - % (big, small)).encode('ascii')], - [('{%dL: 42, %d: 99}' - % (big, small)).encode('ascii')]]) + [[{small: 99, big: 42}], + [{big: 42, small: 99}]]) else: self.assertIn((k, v), expected) diff --git a/ceilometer/utils.py b/ceilometer/utils.py index 016fb8a32..6b2ab15b1 100644 --- a/ceilometer/utils.py +++ b/ceilometer/utils.py @@ -33,7 +33,6 @@ from oslo.utils import timeutils from oslo.utils import units import six - rootwrap_conf = cfg.StrOpt('rootwrap_config', default="/etc/ceilometer/rootwrap.conf", help='Path to the rootwrap configuration file to' @@ -53,6 +52,28 @@ def execute(*cmd, **kwargs): return processutils.execute(*cmd, **kwargs) +def decode_unicode(input): + """Decode the unicode of the message, and encode it into utf-8.""" + if isinstance(input, dict): + temp = {} + # If the input data is a dict, create an equivalent dict with a + # predictable insertion order to avoid inconsistencies in the + # message signature computation for equivalent payloads modulo + # ordering + for key, value in sorted(six.iteritems(input)): + temp[decode_unicode(key)] = decode_unicode(value) + return temp + elif isinstance(input, (tuple, list)): + # When doing a pair of JSON encode/decode operations to the tuple, + # the tuple would become list. So we have to generate the value as + # list here. + return [decode_unicode(element) for element in input] + elif isinstance(input, six.text_type): + return input.encode('utf-8') + else: + return input + + def recursive_keypairs(d, separator=':'): """Generator that produces sequence of keypairs for nested dictionaries.""" for name, value in sorted(six.iteritems(d)): @@ -60,20 +81,7 @@ def recursive_keypairs(d, separator=':'): for subname, subvalue in recursive_keypairs(value, separator): yield ('%s%s%s' % (name, separator, subname), subvalue) elif isinstance(value, (tuple, list)): - # When doing a pair of JSON encode/decode operations to the tuple, - # the tuple would become list. So we have to generate the value as - # list here. - - # in the special case of the list item itself being a dict, - # create an equivalent dict with a predictable insertion order - # to avoid inconsistencies in the message signature computation - # for equivalent payloads modulo ordering - first = lambda i: i[0] - m = map(lambda x: six.text_type(dict(sorted(x.items(), key=first)) - if isinstance(x, dict) - else x).encode('utf-8'), - value) - yield name, list(m) + yield name, decode_unicode(value) else: yield name, value