Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for setting remote_tmp as a subdirectory of a system temp dir #36143

Merged
merged 1 commit into from
Feb 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 18 additions & 1 deletion lib/ansible/plugins/shell/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
import time

import ansible.constants as C
from ansible.errors import AnsibleError
from ansible.module_utils.six import text_type
from ansible.module_utils.six.moves import shlex_quote
from ansible.module_utils._text import to_native
from ansible.plugins import AnsiblePlugin

_USER_HOME_PATH_RE = re.compile(r'^~[_.A-Za-z0-9][-_.A-Za-z0-9]*$')
Expand All @@ -52,6 +54,19 @@ def set_options(self, task_keys=None, var_options=None, direct=None):
# set env
self.env.update(self.get_option('environment'))

# Normalize the temp directory strings. We don't use expanduser/expandvars because those
# can vary between remote user and become user. Therefore the safest practice will be for
# this to always be specified as full paths)
normalized_system_temps = [d.rstrip('/') for d in self.get_option('system_temps')]

# Make sure all system_temps are absolute otherwise they'd be relative to the login dir
# which is almost certainly going to fail in a cornercase.
if not all(os.path.isabs(d) for d in normalized_system_temps):
raise AnsibleError('The configured system_temps contains a relative path: {0}. All'
' system_temps must be absolute'.format(to_native(normalized_system_temps)))

self.set_option('system_temps', normalized_system_temps)

def env_prefix(self, **kwargs):
return ' '.join(['%s=%s' % (k, shlex_quote(text_type(v))) for k, v in kwargs.items()])

Expand Down Expand Up @@ -113,7 +128,9 @@ def mkdtemp(self, basefile=None, system=False, mode=0o700, tmpdir=None):
# passed in tmpdir if it is valid or the first one from the setting if not.

if system:
if tmpdir.startswith(tuple(self.get_option('system_temps'))):
tmpdir = tmpdir.rstrip('/')

if tmpdir in self.get_option('system_temps'):
basetmpdir = tmpdir
else:
basetmpdir = self.get_option('system_temps')[0]
Expand Down
7 changes: 7 additions & 0 deletions lib/ansible/plugins/shell/powershell.py
Original file line number Diff line number Diff line change
Expand Up @@ -1823,6 +1823,13 @@ class ShellModule(ShellBase):

# TODO: add binary module support

def set_options(self, task_keys=None, var_options=None, direct=None):

super(ShellModule, self).set_options(task_keys=task_keys, var_options=var_options, direct=direct)

# set env
self.env.update(self.get_option('environment'))

def assert_safe_env_key(self, key):
if not self.safe_envkey.match(key):
raise AnsibleError("Invalid PowerShell environment key: %s" % key)
Expand Down