Skip to content

Commit

Permalink
Merge pull request #106 from victormlg/CFE-4464-check-sudo
Browse files Browse the repository at this point in the history
Added sudo skipping when UID is zero
  • Loading branch information
olehermanse authored Jan 8, 2025
2 parents 34d43b9 + 49432f4 commit f86e5a4
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions cf_remote/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class LocalConnection:

def __init__(self):
self.ssh_user = pwd.getpwuid(os.getuid()).pw_name
self.needs_sudo = self.run("echo $UID", hide=True).stdout.strip() != "0"

def run(self, command, hide=False):
# to maintain Python 3.5/3.6 compatability the following are used:
Expand Down Expand Up @@ -62,6 +63,8 @@ def __init__(self, host, user, connect_kwargs=None):
control_master_args
) # stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)

self.needs_sudo = self.run("echo $UID", hide=True).stdout.strip() != "0"

def __del__(self):
# If we have an SSH Control Master running, signal it to terminate.
if (
Expand Down Expand Up @@ -198,9 +201,12 @@ def ssh_sudo(connection, cmd, errors=False):
assert connection

log.debug("Running(sudo) over SSH: '%s'" % cmd)
escaped = cmd.replace('"', r"\"")
sudo_cmd = 'sudo bash -c "%s"' % escaped
result = connection.run(sudo_cmd, hide=True)
if connection.needs_sudo:
escaped = cmd.replace('"', r"\"")
sudo_cmd = 'sudo bash -c "%s"' % escaped
result = connection.run(sudo_cmd, hide=True)
else:
result = connection.run(cmd, hide=True)
if result.retcode == 0:
output = result.stdout.strip("\n")
log.debug("'%s' -> '%s'" % (cmd, output))
Expand Down

0 comments on commit f86e5a4

Please sign in to comment.