fix random_seed module
This commit is contained in:
parent
4e9c73303c
commit
a0e6b01032
@ -22,7 +22,7 @@
|
|||||||
import base64
|
import base64
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from six import StringIO
|
from six import BytesIO
|
||||||
|
|
||||||
from cloudinit.settings import PER_INSTANCE
|
from cloudinit.settings import PER_INSTANCE
|
||||||
from cloudinit import log as logging
|
from cloudinit import log as logging
|
||||||
@ -34,13 +34,13 @@ LOG = logging.getLogger(__name__)
|
|||||||
|
|
||||||
def _decode(data, encoding=None):
|
def _decode(data, encoding=None):
|
||||||
if not data:
|
if not data:
|
||||||
return ''
|
return b''
|
||||||
if not encoding or encoding.lower() in ['raw']:
|
if not encoding or encoding.lower() in ['raw']:
|
||||||
return data
|
return util.encode_text(data)
|
||||||
elif encoding.lower() in ['base64', 'b64']:
|
elif encoding.lower() in ['base64', 'b64']:
|
||||||
return util.b64d(data)
|
return base64.b64decode(data)
|
||||||
elif encoding.lower() in ['gzip', 'gz']:
|
elif encoding.lower() in ['gzip', 'gz']:
|
||||||
return util.decomp_gzip(data, quiet=False)
|
return util.decomp_gzip(data, quiet=False, decode=None)
|
||||||
else:
|
else:
|
||||||
raise IOError("Unknown random_seed encoding: %s" % (encoding))
|
raise IOError("Unknown random_seed encoding: %s" % (encoding))
|
||||||
|
|
||||||
@ -65,9 +65,9 @@ def handle_random_seed_command(command, required, env=None):
|
|||||||
def handle(name, cfg, cloud, log, _args):
|
def handle(name, cfg, cloud, log, _args):
|
||||||
mycfg = cfg.get('random_seed', {})
|
mycfg = cfg.get('random_seed', {})
|
||||||
seed_path = mycfg.get('file', '/dev/urandom')
|
seed_path = mycfg.get('file', '/dev/urandom')
|
||||||
seed_data = mycfg.get('data', '')
|
seed_data = mycfg.get('data', b'')
|
||||||
|
|
||||||
seed_buf = StringIO()
|
seed_buf = BytesIO()
|
||||||
if seed_data:
|
if seed_data:
|
||||||
seed_buf.write(_decode(seed_data, encoding=mycfg.get('encoding')))
|
seed_buf.write(_decode(seed_data, encoding=mycfg.get('encoding')))
|
||||||
|
|
||||||
@ -75,7 +75,7 @@ def handle(name, cfg, cloud, log, _args):
|
|||||||
# openstack meta_data.json
|
# openstack meta_data.json
|
||||||
metadata = cloud.datasource.metadata
|
metadata = cloud.datasource.metadata
|
||||||
if metadata and 'random_seed' in metadata:
|
if metadata and 'random_seed' in metadata:
|
||||||
seed_buf.write(metadata['random_seed'])
|
seed_buf.write(util.encode_text(metadata['random_seed']))
|
||||||
|
|
||||||
seed_data = seed_buf.getvalue()
|
seed_data = seed_buf.getvalue()
|
||||||
if len(seed_data):
|
if len(seed_data):
|
||||||
|
@ -124,7 +124,8 @@ class DataSourceAzureNet(sources.DataSource):
|
|||||||
LOG.debug("using files cached in %s", ddir)
|
LOG.debug("using files cached in %s", ddir)
|
||||||
|
|
||||||
# azure / hyper-v provides random data here
|
# azure / hyper-v provides random data here
|
||||||
seed = util.load_file("/sys/firmware/acpi/tables/OEM0", quiet=True)
|
seed = util.load_file("/sys/firmware/acpi/tables/OEM0",
|
||||||
|
quiet=True, decode=False)
|
||||||
if seed:
|
if seed:
|
||||||
self.metadata['random_seed'] = seed
|
self.metadata['random_seed'] = seed
|
||||||
|
|
||||||
|
@ -96,7 +96,6 @@ def b64d(source):
|
|||||||
# Base64 decode some data, accepting bytes or unicode/str, and returning
|
# Base64 decode some data, accepting bytes or unicode/str, and returning
|
||||||
# str/unicode if the result is utf-8 compatible, otherwise returning bytes.
|
# str/unicode if the result is utf-8 compatible, otherwise returning bytes.
|
||||||
decoded = b64decode(source)
|
decoded = b64decode(source)
|
||||||
if isinstance(decoded, bytes):
|
|
||||||
try:
|
try:
|
||||||
return decoded.decode('utf-8')
|
return decoded.decode('utf-8')
|
||||||
except UnicodeDecodeError:
|
except UnicodeDecodeError:
|
||||||
@ -354,11 +353,14 @@ def clean_filename(fn):
|
|||||||
return fn
|
return fn
|
||||||
|
|
||||||
|
|
||||||
def decomp_gzip(data, quiet=True):
|
def decomp_gzip(data, quiet=True, decode=True):
|
||||||
try:
|
try:
|
||||||
buf = six.BytesIO(encode_text(data))
|
buf = six.BytesIO(encode_text(data))
|
||||||
with contextlib.closing(gzip.GzipFile(None, "rb", 1, buf)) as gh:
|
with contextlib.closing(gzip.GzipFile(None, "rb", 1, buf)) as gh:
|
||||||
|
if decode:
|
||||||
return decode_binary(gh.read())
|
return decode_binary(gh.read())
|
||||||
|
else:
|
||||||
|
return gh.read()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if quiet:
|
if quiet:
|
||||||
return data
|
return data
|
||||||
|
Loading…
x
Reference in New Issue
Block a user