Skip to content

Commit

Permalink
utils: T7095: make wrapper use_shell aware and only utilize if vrf or…
Browse files Browse the repository at this point in the history
… netns are requested
  • Loading branch information
xeluior committed Jan 27, 2025
1 parent cb546cb commit a24d2f8
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions python/vyos/utils/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# License along with this library. If not, see <http://www.gnu.org/licenses/>.

import os
import shlex

from subprocess import Popen
from subprocess import PIPE
Expand All @@ -22,11 +23,11 @@


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


Expand Down Expand Up @@ -72,28 +73,33 @@ 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:
raise OSError(
'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
Expand Down

0 comments on commit a24d2f8

Please sign in to comment.