
Every meta data service should return bytes only for these capabilities: * get_content * get_user_data While `_get_meta_data` and any other method derrived from it (including public keys, certificates etc.) should return homogeneous data types and only strings, not bytes. The decoding procedure is handled at its roots, not in the plugins and is done by only using `encoding.get_as_string` function. Fixed bugs: * invalid certificate splitting under maas service which usually generated an extra invalid certificate (empty string + footer) * text operations on bytes in maas and cloudstack (split, comparing) * multiple types for certificates (now only strings) * not receiving bytes from opennebula service when using `get_user_data` (which leads to crash under later processing through io.BytesIO) * erroneous certificate parsing/stripping/replacing under x509 importing (footer remains, not all possible EOLs replaced as it should) Also added new and refined actual misleading unittests. Change-Id: I704c43f5f784458a881293d761a21e62aed85732
41 lines
1.3 KiB
Python
41 lines
1.3 KiB
Python
# Copyright 2014 Cloudbase Solutions Srl
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
# not use this file except in compliance with the License. You may obtain
|
|
# a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
# License for the specific language governing permissions and limitations
|
|
# under the License.
|
|
|
|
import six
|
|
|
|
from cloudbaseinit.openstack.common import log as logging
|
|
|
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
|
def get_as_string(value):
|
|
if value is None or isinstance(value, six.text_type):
|
|
return value
|
|
else:
|
|
try:
|
|
return value.decode()
|
|
except Exception:
|
|
# This is important, because None will be returned,
|
|
# but not that serious to raise an exception.
|
|
LOG.error("Couldn't decode: %r", value)
|
|
|
|
|
|
def write_file(target_path, data, mode='wb'):
|
|
if isinstance(data, six.text_type) and 'b' in mode:
|
|
data = data.encode()
|
|
|
|
with open(target_path, mode) as f:
|
|
f.write(data)
|