Skip to content

Commit

Permalink
Only ask for sudo password when required (#484)
Browse files Browse the repository at this point in the history
* Only ask for sudo password when required

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.

* Allow password arg from None type in driver class

As password isn't a mandatory requirement for the follwing classes
also accept None type arg for password.
- Nmcli0990Wireless
- NmcliWireless
- WpasupplicantWireless

Replace assert with if not raise RunTime error.
  • Loading branch information
epheo committed Apr 10, 2024
1 parent 3924ed9 commit 1b4b451
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 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,34 @@ 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
This method prompts the user to enter the sudo password from the command line.
It validates the password by running a command with sudo and checking if the password is valid.
Returns:
str: The entered sudo password.
Raises:
RuntimeError: If the password is empty or invalid.
"""
# Need password for sudo
if not self._password:
self._password = getpass("Need to run as sudo. Enter password: ")
if not self._password:
raise RuntimeError("Can't use sudo with empty password.")
# 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 Expand Up @@ -234,7 +259,7 @@ def is_on(self) -> bool:
class NmcliWireless(WifiController):
"""Linux nmcli Driver < 0.9.9.0."""

def __init__(self, password: str, interface: Optional[str] = None) -> None:
def __init__(self, password: str | None, interface: Optional[str] = None) -> None:
WifiController.__init__(self, interface=interface, password=password)

def _clean(self, partial: str) -> None:
Expand Down Expand Up @@ -395,7 +420,7 @@ def power(self, power: bool) -> bool:
class Nmcli0990Wireless(WifiController):
"""Linux nmcli Driver >= 0.9.9.0."""

def __init__(self, password: str, interface: Optional[str] = None) -> None:
def __init__(self, password: str | None, interface: Optional[str] = None) -> None:
WifiController.__init__(self, interface=interface, password=password)

def _clean(self, partial: str) -> None:
Expand Down Expand Up @@ -554,7 +579,7 @@ class WpasupplicantWireless(WifiController):

_file = "/tmp/wpa_supplicant.conf"

def __init__(self, password: str, interface: Optional[str] = None) -> None:
def __init__(self, password: str | None, interface: Optional[str] = None) -> None:
WifiController.__init__(self, interface=interface, password=password)

def connect(self, ssid: str, password: str, timeout: float = 15) -> bool:
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 1b4b451

Please sign in to comment.