provisioning failed if the image have a non-ascii property and using qpid

Bug #1180377 reported by Zhi Yan Liu
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Glance Client
Fix Released
Medium
Zhi Yan Liu
OpenStack Compute (nova)
Fix Released
Medium
Zhi Yan Liu

Bug Description

If user set a non-ascii character to the key of an image property and using qpid backend, the instance provisioning will be failed.

Use case:
1. Set a non-ascii character to the key of an image property:
"glance image-update --property '中文'='中文' 5a7e6de5-15b9-49d2-977e-eda0a221c0b9"
2. Launch a instance with that image:
"nova boot --image cirros-0.3.0-x86_64 --flavor m1.tiny --nic net-id=881cbe80-3876-4f4c-b8c8-7b23cedc14f4 lzy-1"
3. Then the provisioning failed and nova-api service catch the exception and write to log:
  File "/usr/lib/python2.6/site-packages/qpid/messaging/driver.py", line 1052, in process
    self.send(snd, msg)
  File "/usr/lib/python2.6/site-packages/qpid/messaging/driver.py", line 1263, in send
    body = enc(msg.content)
  File "/usr/lib/python2.6/site-packages/qpid/messaging/message.py", line 28, in encode
    sc.write_primitive(type, x)
  File "/usr/lib/python2.6/site-packages/qpid/codec010.py", line 73, in write_primitive
    getattr(self, "write_%s" % type.NAME)(v)
  File "/usr/lib/python2.6/site-packages/qpid/codec010.py", line 257, in write_map
    sc.write(string.joinfields(map(self._write_map_elem, m.keys(), m.values()), ""))
  File "/usr/lib/python2.6/site-packages/qpid/codec010.py", line 250, in _write_map_elem
    sc.write_primitive(type, v)
  File "/usr/lib/python2.6/site-packages/qpid/codec010.py", line 73, in write_primitive
    getattr(self, "write_%s" % type.NAME)(v)
  File "/usr/lib/python2.6/site-packages/qpid/codec010.py", line 257, in write_map
    sc.write(string.joinfields(map(self._write_map_elem, m.keys(), m.values()), ""))
  File "/usr/lib/python2.6/site-packages/qpid/codec010.py", line 250, in _write_map_elem
    sc.write_primitive(type, v)
  File "/usr/lib/python2.6/site-packages/qpid/codec010.py", line 73, in write_primitive
    getattr(self, "write_%s" % type.NAME)(v)
  File "/usr/lib/python2.6/site-packages/qpid/codec010.py", line 257, in write_map
    sc.write(string.joinfields(map(self._write_map_elem, m.keys(), m.values()), ""))
  File "/usr/lib/python2.6/site-packages/qpid/codec010.py", line 250, in _write_map_elem
    sc.write_primitive(type, v)
  File "/usr/lib/python2.6/site-packages/qpid/codec010.py", line 73, in write_primitive
    getattr(self, "write_%s" % type.NAME)(v)
  File "/usr/lib/python2.6/site-packages/qpid/codec010.py", line 257, in write_map
    sc.write(string.joinfields(map(self._write_map_elem, m.keys(), m.values()), ""))
  File "/usr/lib/python2.6/site-packages/qpid/codec010.py", line 250, in _write_map_elem
    sc.write_primitive(type, v)
  File "/usr/lib/python2.6/site-packages/qpid/codec010.py", line 73, in write_primitive
    getattr(self, "write_%s" % type.NAME)(v)
  File "/usr/lib/python2.6/site-packages/qpid/codec010.py", line 257, in write_map
    sc.write(string.joinfields(map(self._write_map_elem, m.keys(), m.values()), ""))
  File "/usr/lib/python2.6/site-packages/qpid/codec010.py", line 248, in _write_map_elem
    sc.write_str8(k)
  File "/usr/lib/python2.6/site-packages/qpid/codec010.py", line 185, in write_str8
    self.write_vbin8(s.encode("utf8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)

Analysis:
In nova-api service, the provisioning request will be handled in "nova/compute/api.py' file "_validate_and_provision_instance" method ("create_instance" method called), the image and its properties will be primitive and make a request spec and pass to nova-scheduler by rpc calls via message queue. And nova-api call glance api v1.0 ("get" method) to get image's infromation, under current glanceclient implementation, those non-ascii character in the image property part will be encoded by utf-8, so the root cause is that qpid (with qpid_python-0.18 lib) can not handle those utf-8 string.

Solution:
There are two choices to resolve this issue:
1. Resolve in glanceclient. glance-api server return utf-8 property (key/value pair) to glanceclient, so we need convert the them back to unicode from utf-8 ("_image_meta_from_headers" method in "image.py" file), this will cause client.get() method return unicode property string (key/value pair) and nova-api will get those uncode strings and pass them to nova-scheduler via rpc. this unicode encoding logic just like image property update logic ("_image_meta_to_headers" method), update method always pass unicode image property to glance-api server but not utf-8, so this change will keep the encoding consistency.
And this fix also need change 2 points in nova-api code ("nova/compute/api.py"), they are call str() on the image properties, it should be unicode() after above change.

2. Resolve in nova-api. This way probably is not good then above one since the area affected is greater the it. in this way, we need convert utf-8 string to unicode then to put it into queue, so the change/fix in "to_primitive" method of "jsonutils.py" file. for now, there are two problems:
a. it not take care key within a dict object, that caused utf-8 encoded key string (in this case, it's a key of an image property) will not be primitive.
b. need ask to_primitive take care utf-8 string, IMO, need convert it to unicode but not pass the utf-8 (basestring object) directly. So this fix will affect more area then above glanceclient fixing way, this fix has commonality, even user use rabbitmq backend, any utf-8 encoded key within a dict will be convert include glance properties and any other dict objects which want to primitive and put into queue.

IMO, I prefer use #1 way to address this issue.

Zhi Yan Liu (lzy-dev)
Changed in glance:
assignee: nobody → Zhi Yan Liu (lzy-dev)
status: New → In Progress
Revision history for this message
Flavio Percoco (flaper87) wrote :
Changed in glance:
importance: Undecided → Medium
affects: glance → python-glanceclient
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to python-glanceclient (master)

Reviewed: https://review.openstack.org/29245
Committed: http://github.com/openstack/python-glanceclient/commit/8c70c5b08d6ecae90c2df1ee5c7ba995e3329926
Submitter: Jenkins
Branch: master

commit 8c70c5b08d6ecae90c2df1ee5c7ba995e3329926
Author: Zhi Yan Liu <email address hidden>
Date: Wed May 15 22:45:08 2013 +0800

    Convert non-ascii characters within image property to unicode

    Convert non-ascii characters within image property (key/value pair) to
    unicode but utf-8 to prevent provisioning failure when cloud using qpid
    backend.
    This change also make the image property encoding consistency between
    the image updating and the receiving. Before this, image property
    updating use unicode, but receiving (get) result is utf-8.

    Fixes: Bug #1180377

    Change-Id: I010760c598a7e008c79f1240255708265352cdb5
    Signed-off-by: Zhi Yan Liu <email address hidden>

Changed in python-glanceclient:
status: In Progress → Fix Committed
Revision history for this message
Mark McLoughlin (markmc) wrote :
Changed in nova:
status: New → In Progress
importance: Undecided → Medium
assignee: nobody → Zhi Yan Liu (lzy-dev)
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to nova (master)

Reviewed: https://review.openstack.org/30046
Committed: http://github.com/openstack/nova/commit/832a66cf0bd8f7ccb0878889174c1ce4da7f42f3
Submitter: Jenkins
Branch: master

commit 832a66cf0bd8f7ccb0878889174c1ce4da7f42f3
Author: Zhi Yan Liu <email address hidden>
Date: Tue May 21 12:27:30 2013 +0800

    Using unicode() to handle image's properties

    Replace str() to unicode() to allow nova-api work well with the fix I
    did in glanceclient (https://review.openstack.org/#/c/29245/6).

    Fixes: Bug #1180377

    Change-Id: I31e532bb50e12e355b9dd26c9ce1e129da858cbe
    Signed-off-by: Zhi Yan Liu <email address hidden>

Changed in nova:
status: In Progress → Fix Committed
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to nova (stable/grizzly)

Fix proposed to branch: stable/grizzly
Review: https://review.openstack.org/33157

Revision history for this message
hanzhf (hanzhf) wrote :

We also met this problem in grizzly, need this problem to be fixed in stable/grizzly.

tags: added: grizzly-backport-potential
Revision history for this message
OpenStack Infra (hudson-openstack) wrote :

Fix proposed to branch: stable/grizzly
Review: https://review.openstack.org/33798

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to nova (stable/folsom)

Fix proposed to branch: stable/folsom
Review: https://review.openstack.org/33803

Revision history for this message
hanzhf (hanzhf) wrote :

We also met this problem in folsom, need this problem to be fixed in stable/grizzly.

Thierry Carrez (ttx)
Changed in nova:
milestone: none → havana-2
status: Fix Committed → Fix Released
Thierry Carrez (ttx)
Changed in nova:
milestone: havana-2 → 2013.2
Alan Pevec (apevec)
tags: removed: grizzly-backport-potential
Louis Taylor (kragniz)
Changed in python-glanceclient:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.