persisting OpenStack metadata fails

Bug #1801364 reported by Robert Schweikert on 2018-11-02
This bug affects 4 people
Affects Status Importance Assigned to Milestone

Bug Description

Persistsing OpenStack metadata may fail.

OpenStack has an entry named "random_seed" in the metadata [1]. This entry is treaded specially in the helper when the metadat is read [2]. The attempt to decode the read data my result in a string that is not utf-8 compliant which eventually results in an error when we attempt to persist the metadata as follows:

2018-11-02 12:14:13,755 -[WARNING]: Error persisting instance-data.json: 'utf8' codec can't decode byte 0xc8 in position 2: invalid continuation byte


Related branches

Robert Schweikert (rjschwei) wrote :

Example data for the case that triggered this issue:

From debug output inserted into util.load_json, which is called from read_v2() in

"random_seed": "Cr7LysUKbg/wjTuPKS2qq7rZ9BCWJQAnckkvxCv7fmfOWmHARAWj8mPEfIT+tl8Q1v9Kz3QMKBC4SfgfjLF+doX84pjcDw87lL4UvDzzQfANmvUPeQSc8wpbMe9l74hDEvd/U7Rf1/ag7E/tlQZRwlhQFcHrMI1TUSLKe70vSDspK0NNB1p1GnwKB1Mp683iBtWcSW9zURrdXmYGvl+CvW/EJcesbC59XPX4AezVqejKwI4TtFLDQZ+A9HNLJOXi4E8/gFQO2hEhD56vDzqh1VZNGj2aUdJ7gBoBBE3qtEwdaAkwWS2hS6BV6e1kLgio2M6r/Qz44uf8im9JJdyFlDsVycr6+ZOdCIQTHdEhVQnjwvkxaZFFxp4CaVhzb75FgESkUoNzr/Hr7GILAThJvPLB+a94Q8uSjElSP9r5+0LbB6CGNvEAXDE0IlupzltNS1K9gHVm7mDziA7wU6jtNrMb22gXgVs4pi3QUJlBZYwR9dIPZIRwBCCrzWtyuW+0LMZmoADSosJ1ixFfX34thN/6CmVSjqOZNErBEcSnJ6apSQ8dpTgdn+yORZDs7O1cGGOzkMf4zS9t4TdLpmPTUyfFBi8ykjPLb0Ec8eAAAK/FwUN30LOqJ3GPRI9SN6UBu+6feg96NAu9Surwpkucdg3L94Z0hNi21E7G9ET19Gw="

After processing this turned into:

u'random_seed': '\xb8!\')\xe0\x0e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\xce\x9c\xc8\x8ai\xa1\xf1Ec\x95U\x88.\xc1\xce\xb5\x92\x98\xb5\xe7\xf3\xc4\xd5\xc2\x1fR\xe5`-\xbd\xeaBC\xc0\xad\xdda\xb0oX\xf2b\xf88a\x12\x94R^\x0812\x7f\xa2v\x06Q\xc8\x13X\x8e\x8d-\x82\x1b\x82\x17\x18\x1c\xdd>\x8c\x13\x190\\"z\x8f\xa6\x18\xf3\xbf\x9e\x95\xa7\x8d\x89\x91\x83\x1a\xe5\xbe\xcfK\x08\xff[!\xe5\xba\x9f\xfah~\x85\x16\x18\xd9GX\x9f\x0c\xa4\xa9\xb7\xdcn\xd2\xaf5\xc6\xbc', u'uuid': u'4f9baeab-1f8c-48ca-8766-37c4c59927cf'

per debug output inserted into _crawl_metadata(). With this data in the metadata dictionary persisting the data via persist_instance_data() fails with the message shown in the description.

The question is whether "random_seed" should be persisted in the first place. The value for "random_seed" is different every time the metadata server is accessed. Thus the value is not useful for comparison. However I do not know for what other reasons the persisted data would be used.

Robert Schweikert (rjschwei) wrote :

The patch is one possible implementation of avoiding the utf-8 error when persisting the metadata.

Depending on whether random_seed is actually useful to persist it may also be possible to simply drop the data in

if 'random_seed' in metadata:
    del metadata['random_seed']

Scott Moser (smoser) on 2018-11-02
summary: - persisting OpenStack metadat fails
+ persisting OpenStack metadata fails
Scott Moser (smoser) wrote :

Can you please attach logs?

cloud-init collect-logs

The 'persist_instance_data' in DataSource tries to accomodate for this.

it uses util.json_dumps which uses json_serialize_default.

Here is an example:

$ python3 -c 'from cloudinit import util; print(util.json_dumps({"bin": open("/dev/urandom", "rb").read(8)}))'
 "bin": "ci-b64:doWuT19jT3g="

Notice that the dump adds 'ci-b64' in front of the value.

I'm guessing that there is a differnce in python versions that is causing a problem.

Please attach logs.

Changed in cloud-init:
status: New → Incomplete
Robert Schweikert (rjschwei) wrote :

Yes, sorry, this is Python version dependent, fails with Python 2.7, works with Python 3.

Robert Schweikert (rjschwei) wrote :

Log file that shows the failure.

Note in this case we ran with cloud-init 18.2 but issue reproduces the same way in 18.4, to which the attached patch applies.

As stated in previous comment this happens with Python 2.7, Python 3.6 works with no modification.

Scott Moser (smoser) wrote :

I'm attaching a test case that shows the failure on python2.7.
The issue is in the way that we're serializing binary data into
json. The implementation we have (using default=json_serialize_default)
just does not work in python2.7.

I recall that I had seen a stackexchange question basically covering
this and the dsubmitter eventually just throwing up their hands.

This adds the test case to show the problem, but does not do anything to fix it.

Changed in cloud-init:
status: Incomplete → Confirmed
Scott Moser (smoser) wrote :

If someone fixes this correctly/generically then they should probably remove the Azure specific fix at .

There is good discussion on the general dumps/JsonEncoder issue at

Just to be clear, our python 3 implementation works as designed.

Marcus Furlong (furlongm) wrote :

The patch in fixes this issue for me using python2.7 on rhel7 and centos7.

This bug is fixed with commit 067516d7 to cloud-init on branch master.
To view that commit see the following URL:

Changed in cloud-init:
status: Confirmed → Fix Committed
To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers

Bug attachments