metadata: don't crash proxy on non-unicode user data
We attempt to log every successful metadata response with LOG.debug. But
as per oslo.log docs [1], we should make sure that what we pass into the
library is unicode.
Http.request returns a tuple of Response object and a string, which is
bytes in Python 2.x [2].
That's why we need to convert the response content to unicode before
passing it into oslo.log.
To achieve it, we utilize encodeutils.safe_decode with 'replace' errors
handling strategy, so that we don't get exceptions on input that does
not conform unicode.
For the unit test case, we pass a string that is not expected to convert
to unicode with errors='strict' strategy or similar, and check that we
still don't crash.
While at it, we remove a check for the number of log calls being
triggered, because it's something that we should avoid validating in
test cases, and it cannot trigger a real bug. The mock that was used to
count the number would also hide the bug that we try to reproduce.
Note that the bug does not require debug to be set because the crash
occurs before oslo.log machinery decides it should not log the message.
Reviewed: https:/ /review. openstack. org/233136 /git.openstack. org/cgit/ openstack/ neutron/ commit/ ?id=118a76fd651 c19ad098cdee9d0 a122a00ddc4e3b
Committed: https:/
Submitter: Jenkins
Branch: stable/kilo
commit 118a76fd651c19a d098cdee9d0a122 a00ddc4e3b
Author: Ihar Hrachyshka <email address hidden>
Date: Thu Oct 1 17:13:25 2015 +0200
metadata: don't crash proxy on non-unicode user data
We attempt to log every successful metadata response with LOG.debug. But
as per oslo.log docs [1], we should make sure that what we pass into the
library is unicode.
Http.request returns a tuple of Response object and a string, which is
bytes in Python 2.x [2].
That's why we need to convert the response content to unicode before
passing it into oslo.log.
To achieve it, we utilize encodeutils. safe_decode with 'replace' errors
handling strategy, so that we don't get exceptions on input that does
not conform unicode.
For the unit test case, we pass a string that is not expected to convert
to unicode with errors='strict' strategy or similar, and check that we
still don't crash.
While at it, we remove a check for the number of log calls being
triggered, because it's something that we should avoid validating in
test cases, and it cannot trigger a real bug. The mock that was used to
count the number would also hide the bug that we try to reproduce.
Note that the bug does not require debug to be set because the crash
occurs before oslo.log machinery decides it should not log the message.
[1]: http:// docs.openstack. org/developer/ oslo.log/ usage.html# no-more- implicit- conversion- to-unicode- str bitworking. org/projects/ httplib2/ doc/html/ libhttplib2. html#httplib2. Http.request
[2]: http://
Conflicts: agent/metadata/ namespace_ proxy.py tests/unit/ agent/metadata/ test_namespace_ proxy.py
neutron/
neutron/
Closes-Bug: #1501772 e43913386134c89 81539697ce8 d17377b1d15c839 2b8a43dac6)
Change-Id: I6a32c40ff117fa
(cherry picked from commit 80e3d9be4923eca