summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFeng Xi Yan <yanfengxi@cn.ibm.com>2014-09-24 18:37:02 +0800
committerEoghan Glynn <eglynn@redhat.com>2014-12-02 11:01:46 +0000
commit3489129cb5a412457650add59cd7198095b6b27c (patch)
treef0b557cbd4d61d53d351384eb60405bd57c663c1
parent6eb39a9a2c76cefc5a2c1af071516a68f1e0672f (diff)
downloadceilometer-3489129cb5a412457650add59cd7198095b6b27c.tar.gz
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 (cherry picked from commit 4166da70b30291e8de74cf8c18e27753d0902ace)
-rw-r--r--ceilometer/tests/test_utils.py12
-rw-r--r--ceilometer/utils.py38
2 files changed, 26 insertions, 24 deletions
diff --git a/ceilometer/tests/test_utils.py b/ceilometer/tests/test_utils.py
index bda3de19..b7741018 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 016fb8a3..6b2ab15b 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