Skip to content

Commit

Permalink
Mcsalgado's change to use shlex.quote instead of pipes.quote (ansible…
Browse files Browse the repository at this point in the history
…#18534)

* Replace pipes.quote for shlex_quote

* More migration of pipes.quote to shlex_quote

Note that we cannot yet move module code over.  Modules have six-1.4
bundled which does not have shlex_quote.  This shouldn't be a problem as
the function is still importable from pipes.quote.  It's just that this
has become an implementation detail that makes us want to import from
shlex instead.

Once we get rid of the python2.4 dependency we can update to a newer
version of bundled six module-side and then we're free to use
shlex_quote everywhere.
  • Loading branch information
abadger authored Nov 17, 2016
1 parent 5d043b6 commit ed00741
Show file tree
Hide file tree
Showing 16 changed files with 62 additions and 63 deletions.
10 changes: 5 additions & 5 deletions lib/ansible/playbook/play_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
__metaclass__ = type

import os
import pipes
import pwd
import random
import re
import string

from ansible.compat.six import iteritems, string_types
from ansible.compat.six.moves import shlex_quote
from ansible import constants as C
from ansible.errors import AnsibleError
from ansible.module_utils._text import to_bytes
Expand Down Expand Up @@ -467,7 +467,7 @@ def make_become_cmd(self, cmd, executable=None):
becomecmd = None
randbits = ''.join(random.choice(string.ascii_lowercase) for x in range(32))
success_key = 'BECOME-SUCCESS-%s' % randbits
success_cmd = pipes.quote('echo %s; %s' % (success_key, cmd))
success_cmd = shlex_quote('echo %s; %s' % (success_key, cmd))

if executable:
command = '%s -c %s' % (executable, success_cmd)
Expand Down Expand Up @@ -496,7 +496,7 @@ def make_become_cmd(self, cmd, executable=None):
# done for older versions of sudo that do not support the option.
#
# Passing a quoted compound command to sudo (or sudo -s)
# directly doesn't work, so we shellquote it with pipes.quote()
# directly doesn't work, so we shellquote it with shlex_quote()
# and pass the quoted string to the user's shell.

# force quick error if password is required but not supplied, should prevent sudo hangs.
Expand All @@ -518,7 +518,7 @@ def detect_su_prompt(b_data):
return bool(b_SU_PROMPT_LOCALIZATIONS_RE.match(b_data))
prompt = detect_su_prompt

becomecmd = '%s %s %s -c %s' % (exe, flags, self.become_user, pipes.quote(command))
becomecmd = '%s %s %s -c %s' % (exe, flags, self.become_user, shlex_quote(command))

elif self.become_method == 'pbrun':

Expand Down Expand Up @@ -562,7 +562,7 @@ def detect_ksu_prompt(b_data):
exe = self.become_exe or 'dzdo'
if self.become_pass:
prompt = '[dzdo via ansible, key=%s] password: ' % randbits
becomecmd = '%s -p %s -u %s %s' % (exe, pipes.quote(prompt), self.become_user, command)
becomecmd = '%s -p %s -u %s %s' % (exe, shlex_quote(prompt), self.become_user, command)
else:
becomecmd = '%s -u %s %s' % (exe, self.become_user, command)

Expand Down
9 changes: 4 additions & 5 deletions lib/ansible/plugins/action/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,16 @@
import base64
import json
import os
import pipes
import random
import re
import stat
import tempfile
import time
from abc import ABCMeta, abstractmethod

from ansible.compat.six import binary_type, text_type, iteritems, with_metaclass

from ansible import constants as C
from ansible.compat.six import binary_type, text_type, iteritems, with_metaclass
from ansible.compat.six.moves import shlex_quote
from ansible.errors import AnsibleError, AnsibleConnectionFailure
from ansible.executor.module_common import modify_module
from ansible.module_utils._text import to_bytes, to_native, to_text
Expand Down Expand Up @@ -596,7 +595,7 @@ def _execute_module(self, module_name=None, module_args=None, tmp=None, task_var
# the remote system, which can be read and parsed by the module
args_data = ""
for k,v in iteritems(module_args):
args_data += '%s=%s ' % (k, pipes.quote(text_type(v)))
args_data += '%s=%s ' % (k, shlex_quote(text_type(v)))
self._transfer_data(args_file_path, args_data)
elif module_style in ('non_native_want_json', 'binary'):
self._transfer_data(args_file_path, json.dumps(module_args))
Expand Down Expand Up @@ -748,7 +747,7 @@ def _low_level_execute_command(self, cmd, sudoable=True, in_data=None, executabl
# only applied for the default executable to avoid interfering with the raw action
cmd = self._connection._shell.append_command(cmd, 'sleep 0')
if executable:
cmd = executable + ' -c ' + pipes.quote(cmd)
cmd = executable + ' -c ' + shlex_quote(cmd)

display.debug("_low_level_execute_command(): executing: %s" % (cmd,))

Expand Down
6 changes: 2 additions & 4 deletions lib/ansible/plugins/action/async.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
__metaclass__ = type

import json
import pipes
import random

from ansible import constants as C
from ansible.compat.six import iteritems
from ansible.compat.six.moves import shlex_quote
from ansible.module_utils._text import to_text
from ansible.plugins.action import ActionBase

Expand All @@ -48,8 +48,6 @@ def run(self, tmp=None, task_vars=None):

module_name = self._task.action



env_string = self._compute_environment_string()

module_args = self._task.args.copy()
Expand Down Expand Up @@ -77,7 +75,7 @@ def run(self, tmp=None, task_vars=None):
elif module_style == 'old':
args_data = ""
for k, v in iteritems(module_args):
args_data += '%s="%s" ' % (k, pipes.quote(to_text(v)))
args_data += '%s="%s" ' % (k, shlex_quote(to_text(v)))
argsfile = self._transfer_data(self._connection._shell.join_path(tmp, 'arguments'), args_data)

remote_paths = tmp, remote_module_path, remote_async_module_path
Expand Down
6 changes: 3 additions & 3 deletions lib/ansible/plugins/connection/chroot.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
import distutils.spawn
import os
import os.path
import pipes
import subprocess
import traceback

from ansible import constants as C
from ansible.compat.six.moves import shlex_quote
from ansible.errors import AnsibleError
from ansible.module_utils.basic import is_executable
from ansible.module_utils._text import to_bytes
Expand Down Expand Up @@ -128,7 +128,7 @@ def put_file(self, in_path, out_path):
super(Connection, self).put_file(in_path, out_path)
display.vvv("PUT %s TO %s" % (in_path, out_path), host=self.chroot)

out_path = pipes.quote(self._prefix_login_path(out_path))
out_path = shlex_quote(self._prefix_login_path(out_path))
try:
with open(to_bytes(in_path, errors='surrogate_or_strict'), 'rb') as in_file:
try:
Expand All @@ -150,7 +150,7 @@ def fetch_file(self, in_path, out_path):
super(Connection, self).fetch_file(in_path, out_path)
display.vvv("FETCH %s TO %s" % (in_path, out_path), host=self.chroot)

in_path = pipes.quote(self._prefix_login_path(in_path))
in_path = shlex_quote(self._prefix_login_path(in_path))
try:
p = self._buffered_exec_command('dd if=%s bs=%s' % (in_path, BUFSIZE))
except OSError:
Expand Down
4 changes: 2 additions & 2 deletions lib/ansible/plugins/connection/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
import distutils.spawn
import os
import os.path
import pipes
import subprocess
import re

from distutils.version import LooseVersion

import ansible.constants as C
from ansible.compat.six.moves import shlex_quote
from ansible.errors import AnsibleError, AnsibleFileNotFound
from ansible.module_utils._text import to_bytes
from ansible.plugins.connection import ConnectionBase, BUFSIZE
Expand Down Expand Up @@ -228,7 +228,7 @@ def put_file(self, in_path, out_path):
raise AnsibleFileNotFound(
"file or module does not exist: %s" % in_path)

out_path = pipes.quote(out_path)
out_path = shlex_quote(out_path)
# Older docker doesn't have native support for copying files into
# running containers, so we use docker exec to implement this
# Although docker version 1.8 and later provide support, the
Expand Down
6 changes: 3 additions & 3 deletions lib/ansible/plugins/connection/jail.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
import distutils.spawn
import os
import os.path
import pipes
import subprocess
import traceback

from ansible.compat.six.moves import shlex_quote
from ansible.errors import AnsibleError
from ansible.module_utils._text import to_bytes
from ansible.plugins.connection import ConnectionBase, BUFSIZE
Expand Down Expand Up @@ -150,7 +150,7 @@ def put_file(self, in_path, out_path):
super(Connection, self).put_file(in_path, out_path)
display.vvv("PUT %s TO %s" % (in_path, out_path), host=self.jail)

out_path = pipes.quote(self._prefix_login_path(out_path))
out_path = shlex_quote(self._prefix_login_path(out_path))
try:
with open(to_bytes(in_path, errors='surrogate_or_strict'), 'rb') as in_file:
try:
Expand All @@ -172,7 +172,7 @@ def fetch_file(self, in_path, out_path):
super(Connection, self).fetch_file(in_path, out_path)
display.vvv("FETCH %s TO %s" % (in_path, out_path), host=self.jail)

in_path = pipes.quote(self._prefix_login_path(in_path))
in_path = shlex_quote(self._prefix_login_path(in_path))
try:
p = self._buffered_exec_command('dd if=%s bs=%s' % (in_path, BUFSIZE))
except OSError:
Expand Down
6 changes: 3 additions & 3 deletions lib/ansible/plugins/connection/libvirt_lxc.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
import distutils.spawn
import os
import os.path
import pipes
import subprocess
import traceback

from ansible import constants as C
from ansible.compat.six.moves import shlex_quote
from ansible.errors import AnsibleError
from ansible.module_utils._text import to_bytes
from ansible.plugins.connection import ConnectionBase, BUFSIZE
Expand Down Expand Up @@ -129,7 +129,7 @@ def put_file(self, in_path, out_path):
super(Connection, self).put_file(in_path, out_path)
display.vvv("PUT %s TO %s" % (in_path, out_path), host=self.lxc)

out_path = pipes.quote(self._prefix_login_path(out_path))
out_path = shlex_quote(self._prefix_login_path(out_path))
try:
with open(to_bytes(in_path, errors='surrogate_or_strict'), 'rb') as in_file:
try:
Expand All @@ -151,7 +151,7 @@ def fetch_file(self, in_path, out_path):
super(Connection, self).fetch_file(in_path, out_path)
display.vvv("FETCH %s TO %s" % (in_path, out_path), host=self.lxc)

in_path = pipes.quote(self._prefix_login_path(in_path))
in_path = shlex_quote(self._prefix_login_path(in_path))
try:
p = self._buffered_exec_command('dd if=%s bs=%s' % (in_path, BUFSIZE))
except OSError:
Expand Down
8 changes: 4 additions & 4 deletions lib/ansible/plugins/connection/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
import errno
import fcntl
import os
import pipes
import pty
import select
import subprocess
import time

from ansible import constants as C
from ansible.compat.six import PY3, text_type, binary_type
from ansible.compat.six.moves import shlex_quote
from ansible.errors import AnsibleError, AnsibleConnectionFailure, AnsibleFileNotFound
from ansible.errors import AnsibleOptionsError
from ansible.module_utils.basic import BOOLEANS
Expand Down Expand Up @@ -324,7 +324,7 @@ def _run(self, cmd, in_data, sudoable=True, checkrc=True):
Starts the command and communicates with it until it ends.
'''

display_cmd = list(map(pipes.quote, map(to_text, cmd)))
display_cmd = list(map(shlex_quote, map(to_text, cmd)))
display.vvv(u'SSH: EXEC {0}'.format(u' '.join(display_cmd)), host=self.host)

# Start the given command. If we don't need to pipeline data, we can try
Expand Down Expand Up @@ -626,9 +626,9 @@ def _file_transport_command(self, in_path, out_path, sftp_action):
for method in methods:
if method == 'sftp':
cmd = self._build_command('sftp', to_bytes(host))
in_data = u"{0} {1} {2}\n".format(sftp_action, pipes.quote(in_path), pipes.quote(out_path))
in_data = u"{0} {1} {2}\n".format(sftp_action, shlex_quote(in_path), shlex_quote(out_path))
elif method == 'scp':
cmd = self._build_command('scp', in_path, u'{0}:{1}'.format(host, pipes.quote(out_path)))
cmd = self._build_command('scp', in_path, u'{0}:{1}'.format(host, shlex_quote(out_path)))
in_data = None

in_data = to_bytes(in_data, nonstring='passthru')
Expand Down
6 changes: 3 additions & 3 deletions lib/ansible/plugins/connection/zone.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
import distutils.spawn
import os
import os.path
import pipes
import subprocess
import traceback

from ansible import constants as C
from ansible.compat.six.moves import shlex_quote
from ansible.errors import AnsibleError
from ansible.plugins.connection import ConnectionBase, BUFSIZE
from ansible.module_utils._text import to_bytes
Expand Down Expand Up @@ -149,7 +149,7 @@ def put_file(self, in_path, out_path):
super(Connection, self).put_file(in_path, out_path)
display.vvv("PUT %s TO %s" % (in_path, out_path), host=self.zone)

out_path = pipes.quote(self._prefix_login_path(out_path))
out_path = shlex_quote(self._prefix_login_path(out_path))
try:
with open(in_path, 'rb') as in_file:
try:
Expand All @@ -171,7 +171,7 @@ def fetch_file(self, in_path, out_path):
super(Connection, self).fetch_file(in_path, out_path)
display.vvv("FETCH %s TO %s" % (in_path, out_path), host=self.zone)

in_path = pipes.quote(self._prefix_login_path(in_path))
in_path = shlex_quote(self._prefix_login_path(in_path))
try:
p = self._buffered_exec_command('dd if=%s bs=%s' % (in_path, BUFSIZE))
except OSError:
Expand Down
4 changes: 2 additions & 2 deletions lib/ansible/plugins/filter/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import json
import os.path
import ntpath
import pipes
import glob
import re
import crypt
Expand All @@ -48,6 +47,7 @@
from ansible import errors
from ansible.compat.six import iteritems, string_types
from ansible.compat.six.moves import reduce
from ansible.compat.six.moves import shlex_quote
from ansible.module_utils._text import to_text
from ansible.parsing.yaml.dumper import AnsibleDumper
from ansible.utils.hashing import md5s, checksum_s
Expand Down Expand Up @@ -123,7 +123,7 @@ def to_datetime(string, format="%Y-%d-%m %H:%M:%S"):

def quote(a):
''' return its argument quoted for shell usage '''
return pipes.quote(a)
return shlex_quote(a)

def fileglob(pathname):
''' return list of matched regular files for glob '''
Expand Down
Loading

0 comments on commit ed00741

Please sign in to comment.