Skip to content

Commit

Permalink
pybridge: Improve beiboot error reporting for fatal login failures
Browse files Browse the repository at this point in the history
After all SSH authentication attemps fail, e.g. entering the wrong
password three times, ferny throws an InteractionError. Merely exiting
cockpit-beiboot gets interpreted as "Internal error" which is unfriendly.

Ferny recognizes the most common errors, like DNS resolution and and
authentication failure. Use that API to convert them to proper cockpit
protocol error codes, so that they get presented and translated properly.
  • Loading branch information
martinpitt committed Mar 21, 2024
1 parent 781a328 commit 6bd679a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
16 changes: 14 additions & 2 deletions src/cockpit/beiboot.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import logging
import os
import shlex
import sys
from pathlib import Path
from typing import Dict, Iterable, Optional, Sequence

Expand Down Expand Up @@ -309,8 +308,21 @@ async def run(args) -> None:
bridge.write_control(message)
bridge.ssh_peer.thaw_endpoint()
except ferny.InteractionError as exc:
sys.exit(str(exc))
error = ferny.ssh_errors.get_exception_for_ssh_stderr(str(exc))
logger.debug("ferny.InteractionError: %s, interpreted as: %r", exc, error)
if isinstance(error, ferny.SshAuthenticationError):
problem = 'authentication-failed'
elif isinstance(error, ferny.SshHostKeyError):
problem = 'unknown-hostkey'
elif isinstance(error, OSError):
# usually DNS/socket errors
problem = 'unknown-host'
else:
problem = 'internal-error'
bridge.write_control(command='init', problem=problem, message=str(error))
return
except CockpitProblem as exc:
logger.debug("CockpitProblem: %s", exc)
bridge.write_control(exc.attrs, command='init')
return

Expand Down
15 changes: 6 additions & 9 deletions test/verify/check-client
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,11 @@ Command = /usr/bin/env python3 -m cockpit.beiboot
b.wait_text("#conversation-prompt", "admin@10.111.113.2's password: ")
b.set_val("#conversation-input", "wrong")
b.click("#login-button")
b.wait_in_text("#login-fatal-message", "admin@10.111.113.2: Permission denied")
b.click("#login-again")
b.wait_text("#brand", "Connect to:")
# resets the host field
b.wait_val("#server-field", "")
b.wait_in_text("#login-error-message", "Authentication failed")
b.wait_val("#server-field", "10.111.113.2")

# connect to most recent host
b.open("/") # reset URL from /metrics and last remote =host
b.click("#recent-hosts-list .host-line button.host-name")
b.wait_text("#conversation-prompt", "admin@10.111.113.2's password: ")
b.set_val("#conversation-input", "foobar")
Expand Down Expand Up @@ -208,17 +206,16 @@ Command = /usr/bin/env python3 -m cockpit.beiboot
# unreachable host
b.set_val("#server-field", "unknownhost")
b.click("#login-button")
b.wait_in_text("#login-fatal-message", "Could not resolve hostname unknownhost")
b.click("#login-again")
b.wait_text("#brand", "Connect to:")
b.wait_in_text("#login-error-message", "Host is unknown")
b.wait_val("#server-field", "unknownhost")
# does not appear in recent hosts
b.wait_in_text("#recent-hosts-list", "10.111.113.2")
self.assertNotIn("unknownhost", b.text("#recent-hosts-list"))

# wrong port
b.set_val("#server-field", "10.111.113.2:222")
b.click("#login-button")
b.wait_in_text("#login-fatal-message", "connect to host 10.111.113.2 port 222: No route to host")
b.wait_in_text("#login-error-message", "Host is unknown")

# unencrypted SSH key login
self.m_client.execute("runuser -u admin -- ssh-keygen -t rsa -N '' -f ~admin/.ssh/id_rsa")
Expand Down

0 comments on commit 6bd679a

Please sign in to comment.