Skip to content

Commit

Permalink
Fix MIME policy failure on python version upgrade (#934)
Browse files Browse the repository at this point in the history
Python 3.6 added a new `policy` attribute to `MIMEMultipart`.
MIMEMultipart may be part of the cached object pickle of a datasource.
Upgrading from an old version of python to 3.6+ will cause the
datasource to be invalid after pickle load.

This commit uses the upgrade framework to attempt to access the mime
message and fail early (thus discarding the cache) if we cannot.
Commit 78e89b0 should fix this issue more generally.
  • Loading branch information
TheRealFalcon committed Jul 15, 2021
1 parent 881be6e commit eacb035
Show file tree
Hide file tree
Showing 4 changed files with 622 additions and 0 deletions.
18 changes: 18 additions & 0 deletions cloudinit/sources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@
_NETCFG_SOURCE_NAMES)(*_NETCFG_SOURCE_NAMES)


class DatasourceUnpickleUserDataError(Exception):
"""Raised when userdata is unable to be unpickled due to python upgrades"""


class DataSourceNotFoundException(Exception):
pass

Expand Down Expand Up @@ -239,6 +243,20 @@ def _unpickle(self, ci_pkl_version: int) -> None:
self.vendordata2 = None
if not hasattr(self, 'vendordata2_raw'):
self.vendordata2_raw = None
if hasattr(self, 'userdata') and self.userdata is not None:
# If userdata stores MIME data, on < python3.6 it will be
# missing the 'policy' attribute that exists on >=python3.6.
# Calling str() on the userdata will attempt to access this
# policy attribute. This will raise an exception, causing
# the pickle load to fail, so cloud-init will discard the cache
try:
str(self.userdata)
except AttributeError as e:
LOG.debug(
"Unable to unpickle datasource: %s."
" Ignoring current cache.", e
)
raise DatasourceUnpickleUserDataError() from e

def __str__(self):
return type_utils.obj_name(self)
Expand Down
2 changes: 2 additions & 0 deletions cloudinit/stages.py
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,8 @@ def _pkl_load(fname):
return None
try:
return pickle.loads(pickle_contents)
except sources.DatasourceUnpickleUserDataError:
return None
except Exception:
util.logexc(LOG, "Failed loading pickled blob from %s", fname)
return None
Expand Down
Loading

0 comments on commit eacb035

Please sign in to comment.