Improve MIME multipart userdata handling
The 'exec_file' will now try to check for the headers inside the file, in case that it doesn't recognize the format or no format has been provided. Also the processing part of the user data now checks if the 'Content-Type' is in the file instead of checking if the file starts with the header, in order to comply with RFC2045. Change-Id: I53fda9f5c17f35cb35d93a86434ecc4c7c579802 Closes-Bug: #1623393 Closes-Bug: #1672222
This commit is contained in:
@@ -12,35 +12,23 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import os
|
||||
|
||||
from oslo_log import log as oslo_logging
|
||||
|
||||
from cloudbaseinit.plugins.common import execcmd
|
||||
|
||||
from cloudbaseinit.plugins.common import userdatautils
|
||||
|
||||
LOG = oslo_logging.getLogger(__name__)
|
||||
|
||||
FORMATS = {
|
||||
"cmd": execcmd.Shell,
|
||||
"exe": execcmd.Shell,
|
||||
"sh": execcmd.Bash,
|
||||
"py": execcmd.Python,
|
||||
"ps1": execcmd.PowershellSysnative,
|
||||
}
|
||||
|
||||
|
||||
def exec_file(file_path):
|
||||
ret_val = 0
|
||||
ext = os.path.splitext(file_path)[1][1:].lower()
|
||||
command = FORMATS.get(ext)
|
||||
command = userdatautils.get_command_from_path(file_path)
|
||||
if not command:
|
||||
# Unsupported
|
||||
LOG.warning('Unsupported script file type: %s', ext)
|
||||
# File format not provided or not recognized
|
||||
LOG.debug('No valid extension or header found in the '
|
||||
'userdata: %s' % file_path)
|
||||
return ret_val
|
||||
|
||||
try:
|
||||
out, err, ret_val = command(file_path).execute()
|
||||
out, err, ret_val = command.execute()
|
||||
except Exception as ex:
|
||||
LOG.warning('An error occurred during file execution: \'%s\'', ex)
|
||||
else:
|
||||
|
||||
@@ -83,11 +83,29 @@ class UserDataPlugin(base.BasePlugin):
|
||||
LOG.debug('User data content:\n%s', user_data_str)
|
||||
return email.message_from_string(user_data_str).walk()
|
||||
|
||||
@staticmethod
|
||||
def _get_headers(user_data):
|
||||
"""Returns the header of the given user data.
|
||||
|
||||
:param user_data: Represents the content of the user data.
|
||||
:rtype: A string chunk containing the header or None.
|
||||
.. note :: In case the content type is not valid,
|
||||
None will be returned.
|
||||
"""
|
||||
content = encoding.get_as_string(user_data)
|
||||
if content:
|
||||
return content.split("\n\n")[0]
|
||||
else:
|
||||
raise exception.CloudbaseInitException("No header could be found."
|
||||
"The user data content is "
|
||||
"either invalid or empty.")
|
||||
|
||||
def _process_user_data(self, user_data):
|
||||
plugin_status = base.PLUGIN_EXECUTION_DONE
|
||||
reboot = False
|
||||
LOG.debug("Processing userdata")
|
||||
if user_data.startswith(b'Content-Type: multipart'):
|
||||
headers = self._get_headers(user_data)
|
||||
if 'Content-Type: multipart' in headers:
|
||||
LOG.debug("Processing userdata")
|
||||
user_data_plugins = factory.load_plugins()
|
||||
user_handlers = {}
|
||||
|
||||
|
||||
@@ -12,8 +12,8 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
|
||||
import functools
|
||||
import collections
|
||||
import os
|
||||
import re
|
||||
|
||||
from oslo_log import log as oslo_logging
|
||||
@@ -23,37 +23,69 @@ from cloudbaseinit.plugins.common import execcmd
|
||||
|
||||
LOG = oslo_logging.getLogger(__name__)
|
||||
|
||||
# Avoid 80+ length by using a local variable, which
|
||||
# is deleted afterwards.
|
||||
_compile = functools.partial(re.compile, flags=re.I)
|
||||
FORMATS = (
|
||||
(_compile(br'^rem\s+cmd\s'), execcmd.Shell),
|
||||
(_compile(br'^#!\s*/usr/bin/env\s+python\s'), execcmd.Python),
|
||||
(_compile(br'^#!'), execcmd.Bash),
|
||||
(_compile(br'^#(ps1|ps1_sysnative)\s'), execcmd.PowershellSysnative),
|
||||
(_compile(br'^#ps1_x86\s'), execcmd.Powershell),
|
||||
(_compile(br'</?(script|powershell)>'), execcmd.EC2Config),
|
||||
)
|
||||
del _compile
|
||||
_Script = collections.namedtuple('Script', ['extension', 'script_type',
|
||||
'executor'])
|
||||
_SCRIPTS = (
|
||||
_Script(extension='cmd', executor=execcmd.Shell,
|
||||
script_type=re.compile(br'^rem\s+cmd\s')),
|
||||
_Script(script_type=re.compile(br'^#!\s*/usr/bin/env\s+python\s'),
|
||||
extension='py', executor=execcmd.Python),
|
||||
_Script(extension='exe', script_type=None, executor=execcmd.Shell),
|
||||
_Script(extension='sh', script_type=re.compile(br'^#!'),
|
||||
executor=execcmd.Bash),
|
||||
_Script(extension='ps1', executor=execcmd.PowershellSysnative,
|
||||
script_type=re.compile(br'^#(ps1|ps1_sysnative)\s')),
|
||||
_Script(extension=None, executor=execcmd.Powershell,
|
||||
script_type=re.compile(br'^#ps1_x86\s')),
|
||||
_Script(extension=None, executor=execcmd.EC2Config,
|
||||
script_type=re.compile(br'</?(script|powershell)>')))
|
||||
|
||||
|
||||
def _get_command(data):
|
||||
# Get the command which should process the given data.
|
||||
for pattern, command_class in FORMATS:
|
||||
if pattern.search(data):
|
||||
return command_class.from_data(data)
|
||||
def _get_command(data, is_path=False):
|
||||
"""Returns a specific command executor if the data type is found.
|
||||
|
||||
:param data: It can be either a file or content of user_data type.
|
||||
:param is_path: Determines whether :data: is a file path or it
|
||||
contains the user_data content.
|
||||
:rtype: An `execcmd` command type or `None`.
|
||||
.. note :: In case the data doesn't have a valid extension or
|
||||
header, it will return `None`.
|
||||
"""
|
||||
if is_path:
|
||||
extension = os.path.splitext(data)[1][1:].lower()
|
||||
for script in _SCRIPTS:
|
||||
if extension == script.extension:
|
||||
return script.executor(data)
|
||||
with open(data, 'rb') as file_handler:
|
||||
file_handler.seek(0)
|
||||
user_data = file_handler.read()
|
||||
else:
|
||||
user_data = data
|
||||
|
||||
for script in _SCRIPTS:
|
||||
if script.script_type and script.script_type.search(user_data):
|
||||
return script.executor.from_data(user_data)
|
||||
return None
|
||||
|
||||
|
||||
def get_command(data):
|
||||
return _get_command(data)
|
||||
|
||||
|
||||
def get_command_from_path(path):
|
||||
return _get_command(path, is_path=True)
|
||||
|
||||
|
||||
def execute_user_data_script(user_data):
|
||||
ret_val = 0
|
||||
out = err = None
|
||||
command = _get_command(user_data)
|
||||
command = get_command(user_data)
|
||||
if not command:
|
||||
LOG.warning('Unsupported user_data format')
|
||||
return ret_val
|
||||
|
||||
try:
|
||||
out, err, ret_val = command()
|
||||
out, err, ret_val = command.execute()
|
||||
except Exception as exc:
|
||||
LOG.warning('An error occurred during user_data execution: \'%s\'',
|
||||
exc)
|
||||
|
||||
Reference in New Issue
Block a user