Skip to content

Commit

Permalink
Merge pull request #9 from t-cas/sudo_bash_builtins
Browse files Browse the repository at this point in the history
fix run of shell builtins commands when impersonating another user
  • Loading branch information
t-cas authored Jul 14, 2017
2 parents 144ed86 + af46d9f commit a8088be
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion jumpssh/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ def run_cmd(
my_cmd = cmd
if username:
user = username
my_cmd = 'sudo -u %s %s' % (user, cmd)
# need to run full command with shell to support shell builtins commands (source, ...)
my_cmd = 'sudo su - %s -c "%s"' % (user, cmd.replace('"', '\\"'))

if not silent:
logger.debug("Running command '%s' on '%s' as %s..." % (cmd, self.host, user))
Expand Down
12 changes: 12 additions & 0 deletions tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,21 @@ def test_run_cmd(docker_env, capfd):
out, err = capfd.readouterr()
assert len(out) > 0


def test_run_cmd_sudo(docker_env):
gateway_ip, gateway_port = docker_env.get_host_ip_port('gateway')

gateway_session = SSHSession(host=gateway_ip, port=gateway_port,
username='user1', password='password1').open()

# run command as user2
assert gateway_session.run_cmd('whoami', username='user2')[1].strip() == 'user2'

# run bash builtins commands with sudo (here command 'source')
gateway_session.file(remote_path='/home/user2/ssh_setenv', use_sudo=True, owner='user2',
content='MY_VAR=variable_set')
gateway_session.run_cmd('source /home/user2/ssh_setenv', username='user2')


def get_cmd_output(docker_env):
gateway_ip, gateway_port = docker_env.get_host_ip_port('gateway')
Expand Down

0 comments on commit a8088be

Please sign in to comment.