From cb546cb6caa0e1f52c425eb13a093e66343e1bd1 Mon Sep 17 00:00:00 2001 From: Robert Gingras Date: Mon, 27 Jan 2025 11:18:27 -0500 Subject: utils: T7095: remove unused `auth` parameter --- python/vyos/utils/process.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) (limited to 'python') diff --git a/python/vyos/utils/process.py b/python/vyos/utils/process.py index 054088325..d47e64588 100644 --- a/python/vyos/utils/process.py +++ b/python/vyos/utils/process.py @@ -21,20 +21,17 @@ from subprocess import STDOUT from subprocess import DEVNULL -def get_wrapper(vrf, netns, auth): +def get_wrapper(vrf, netns): wrapper = '' if vrf: wrapper = f'ip vrf exec {vrf} ' elif netns: wrapper = f'ip netns exec {netns} ' - if auth: - wrapper = f'{auth} {wrapper}' return wrapper def popen(command, flag='', shell=None, input=None, timeout=None, env=None, - stdout=PIPE, stderr=PIPE, decode='utf-8', auth='', vrf=None, - netns=None): + stdout=PIPE, stderr=PIPE, decode='utf-8', vrf=None, netns=None): """ popen is a wrapper helper around subprocess.Popen with it default setting it will return a tuple (out, err) @@ -82,7 +79,7 @@ def popen(command, flag='', shell=None, input=None, timeout=None, env=None, 'Permission denied: cannot execute commands in VRF and netns contexts as an unprivileged user' ) - wrapper = get_wrapper(vrf, netns, auth) + wrapper = get_wrapper(vrf, netns) command = f'{wrapper} {command}' cmd_msg = f"cmd '{command}'" @@ -155,7 +152,7 @@ def run(command, flag='', shell=None, input=None, timeout=None, env=None, def cmd(command, flag='', shell=None, input=None, timeout=None, env=None, stdout=PIPE, stderr=PIPE, decode='utf-8', raising=None, message='', - expect=[0], auth='', vrf=None, netns=None): + expect=[0], vrf=None, netns=None): """ A wrapper around popen, which returns the stdout and will raise the error code of a command @@ -171,12 +168,11 @@ def cmd(command, flag='', shell=None, input=None, timeout=None, env=None, input=input, timeout=timeout, env=env, shell=shell, decode=decode, - auth=auth, vrf=vrf, netns=netns, ) if code not in expect: - wrapper = get_wrapper(vrf, netns, auth='') + wrapper = get_wrapper(vrf, netns) command = f'{wrapper} {command}' feedback = message + '\n' if message else '' feedback += f'failed to run command: {command}\n' -- cgit v1.2.3 From a24d2f87fdde466625d9b6173657f07cf4401f30 Mon Sep 17 00:00:00 2001 From: Robert Gingras Date: Mon, 27 Jan 2025 11:30:46 -0500 Subject: utils: T7095: make wrapper use_shell aware and only utilize if vrf or netns are requested --- python/vyos/utils/process.py | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) (limited to 'python') diff --git a/python/vyos/utils/process.py b/python/vyos/utils/process.py index d47e64588..faed8f1b0 100644 --- a/python/vyos/utils/process.py +++ b/python/vyos/utils/process.py @@ -14,6 +14,7 @@ # License along with this library. If not, see . import os +import shlex from subprocess import Popen from subprocess import PIPE @@ -22,11 +23,11 @@ from subprocess import DEVNULL def get_wrapper(vrf, netns): - wrapper = '' + wrapper = None if vrf: - wrapper = f'ip vrf exec {vrf} ' + wrapper = ['ip', 'vrf', 'exec', vrf] elif netns: - wrapper = f'ip netns exec {netns} ' + wrapper = ['ip', 'netns', 'exec', netns] return wrapper @@ -72,6 +73,15 @@ def popen(command, flag='', shell=None, input=None, timeout=None, env=None, if not debug.enabled(flag): flag = 'command' + use_shell = shell + stdin = None + if shell is None: + use_shell = False + if ' ' in command: + use_shell = True + if env: + use_shell = True + # Must be run as root to execute command in VRF or network namespace if vrf or netns: if os.getuid() != 0: @@ -79,21 +89,17 @@ def popen(command, flag='', shell=None, input=None, timeout=None, env=None, 'Permission denied: cannot execute commands in VRF and netns contexts as an unprivileged user' ) - wrapper = get_wrapper(vrf, netns) - command = f'{wrapper} {command}' + wrapper = get_wrapper(vrf, netns) + if use_shell: + command = f'{shlex.join(wrapper)} {command}' + else: + if type(command) is not list: + command = [command] + command = wrapper + command - cmd_msg = f"cmd '{command}'" + cmd_msg = f"cmd '{command}'" if use_shell else f"cmd '{shlex.join(command)}'" debug.message(cmd_msg, flag) - use_shell = shell - stdin = None - if shell is None: - use_shell = False - if ' ' in command: - use_shell = True - if env: - use_shell = True - if input: stdin = PIPE input = input.encode() if type(input) is str else input -- cgit v1.2.3