Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 fix(vpn): avoids sudo when running as root #238

Merged
merged 1 commit into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions riocli/vpn/connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from riocli.utils.spinner import with_spinner
from riocli.v2client import Client as v2Client
from riocli.vpn.util import (
get_command,
is_tailscale_up,
stop_tailscale,
install_vpn_tools,
Expand Down Expand Up @@ -107,13 +108,10 @@ def start_tailscale(
client: v2Client,
spinner: Yaspin,
) -> bool:
cmd = ('sudo tailscale up --auth-key={} --login-server={}'
' --reset --force-reauth --accept-routes --accept-dns'
' --advertise-tags={} --timeout=30s')
cmd = get_command('tailscale up --auth-key={} --login-server={} --reset --force-reauth '
'--accept-routes --accept-dns --advertise-tags={} --timeout=30s')
args = generate_tailscale_args(ctx, client, spinner)
command = cmd.format(args.HEADSCALE_PRE_AUTH_KEY,
args.HEADSCALE_URL,
args.HEADSCALE_ACL_TAG)
command = cmd.format(args.HEADSCALE_PRE_AUTH_KEY, args.HEADSCALE_URL, args.HEADSCALE_ACL_TAG)
output, code = run_bash_with_return_code(command)
if code != 0:
spinner.write(
Expand Down
13 changes: 11 additions & 2 deletions riocli/vpn/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import os
import socket
import tempfile
from os.path import exists, join
Expand Down Expand Up @@ -57,11 +58,11 @@ def get_tailscale_ip() -> str:


def stop_tailscale() -> bool:
_, code = run_bash_with_return_code('sudo tailscale down')
_, code = run_bash_with_return_code(get_command('tailscale down'))
if code != 0:
return False

output, code = run_bash_with_return_code('sudo tailscale logout')
output, code = run_bash_with_return_code(get_command('tailscale logout'))
if code != 0 and 'no nodekey to log out' not in output:
return False

Expand Down Expand Up @@ -126,3 +127,11 @@ def is_vpn_enabled_in_project(client: v2Client, project_guid: str) -> bool:
project = client.get_project(project_guid)
return (project.status.status.lower() == 'success' and
project.status.vpn.lower() == 'success')


def get_command(cmd: str) -> str:
"""Returns an effective command to execute."""
if is_linux() and os.geteuid() == 0:
return cmd

return 'sudo {}'.format(cmd)
Loading