Skip to content

Commit

Permalink
feat: sudo prompts in non-tty mode (#431)
Browse files Browse the repository at this point in the history
  • Loading branch information
ocervell authored Sep 15, 2024
1 parent a44a36d commit 0e26b55
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
1 change: 1 addition & 0 deletions secator/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def build(self):
'title': self.title,
'runner': self.runner.__class__.__name__,
'name': self.runner.config.name,
'status': self.runner.status,
'targets': self.runner.targets,
'total_time': str(self.runner.elapsed),
'total_human': self.runner.elapsed_human,
Expand Down
14 changes: 9 additions & 5 deletions secator/runners/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,8 @@ def yielder(self):

# Check for sudo requirements and prepare the password if needed
sudo_password = self._prompt_sudo(self.cmd)
if sudo_password and sudo_password == -1:
return

# Prepare cmds
command = self.cmd if self.shell else shlex.split(self.cmd)
Expand Down Expand Up @@ -475,13 +477,14 @@ def _prompt_sudo(self, command):
return None

# Check if sudo can be executed without a password
if subprocess.run(['sudo', '-n', 'true'], capture_output=True).returncode == 0:
if subprocess.run(['sudo', '-n', 'true'], capture_output=False).returncode == 0:
return None

# Check if we have a tty
if not os.isatty(sys.stdin.fileno()):
self._print("No TTY detected. Sudo password prompt requires a TTY to proceed.", color='bold red')
sys.exit(1)
error = "No TTY detected. Sudo password prompt requires a TTY to proceed."
self.errors.append(error)
return -1

# If not, prompt the user for a password
self._print('[bold red]Please enter sudo password to continue.[/]')
Expand All @@ -497,8 +500,9 @@ def _prompt_sudo(self, command):
if result.returncode == 0:
return sudo_password # Password is correct
self._print("Sorry, try again.")
self._print("Sudo password verification failed after 3 attempts.")
return None
error = "Sudo password verification failed after 3 attempts."
self.errors.append(error)
return -1

def _wait_for_end(self):
"""Wait for process to finish and process output and return code."""
Expand Down

0 comments on commit 0e26b55

Please sign in to comment.