Skip to content

Commit

Permalink
Only ask for sudo password when required
Browse files Browse the repository at this point in the history
Check nmcli permissions and only ask for password if required.

Move getpass to separate func and validate password input using assert
Otherwise raise AssertionError

Change sudo func to return an empty string if no sudo password is
provided in order to prevent formating of the nmcli commands with sudo
prefix if not required.
Raise condition is moved at password input.

This commit solves gopro#483
  • Loading branch information
epheo committed Feb 15, 2024
1 parent 75b5578 commit 0fd0452
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,16 @@ def _detect_driver(self) -> WifiController:
if which("networksetup"):
return NetworksetupWireless()

# Try Linux options. Need password for sudo
if not self._password:
self._password = getpass("Need to run as sudo. Enter password: ")
# Validate password
if "VALID PASSWORD" not in cmd(f'echo "{self._password}" | sudo -S echo "VALID PASSWORD"'):
raise RuntimeError("Invalid password")

# Try Linux options.
# try nmcli (Ubuntu 14.04). Allow for use in Snap Package
if which("nmcli") or which("nmcli", path="/snap/bin/"):

ctrl_wifi = cmd("nmcli general permissions |grep enable-disable-wifi")
scan_wifi = cmd("nmcli general permissions |grep scan")

if not "yes" in ctrl_wifi or not "yes" in scan_wifi:
self._sudo_from_stdin()

version = cmd("nmcli --version").split()[-1]
# On RHEL based systems, the version is in the form of 1.44.2-1.fc39
# wich raises an error when trying to compare it with the Version class
Expand All @@ -138,10 +139,27 @@ def _detect_driver(self) -> WifiController:
)
# try nmcli (Ubuntu w/o network-manager)
if which("wpa_supplicant"):
self._sudo_from_stdin()
return WpasupplicantWireless(password=self._password)

raise RuntimeError("Unable to find compatible wireless driver.")

def _sudo_from_stdin(self) -> str:
"""Ask for sudo password input from stdin
Returns:
str: password
"""
# Need password for sudo
if not self._password:
self._password = getpass("Need to run as sudo. Enter password: ")
assert self._password, 'Password is required'
# Validate password
if "VALID PASSWORD" not in cmd(f'echo "{self._password}" | sudo -S echo "VALID PASSWORD"'):
raise RuntimeError("Invalid password")

return self._password

@pass_through_to_driver
def connect(self, ssid: str, password: str, timeout: float = 15) -> bool: # type: ignore
"""Connect to a network.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,9 @@ def is_on(self) -> bool:
def sudo(self) -> str:
"""Return the sudo encapsulated password
Raises:
RuntimeError: No password has been supplied
Returns:
str: echo "**********" | sudo -S
"""
if not self._password:
raise RuntimeError("Can't use sudo with empty password.")
return ""
return f'echo "{self._password}" | sudo -S'

0 comments on commit 0fd0452

Please sign in to comment.