fix(remote): discover cloud login accounts before asking users
This commit is contained in:
1 parent
e6d0303511
commit
f4e45c98d7
4 files changed
+230
-13
No files matched your search
@@ -10,6 +10,7 @@ import pytest
|
||||
from typer.testing import CliRunner
|
||||
|
||||
from nanobot.cli import remote
|
||||
from nanobot.cli.remote import _default_ssh_user
|
||||
from nanobot.webui import remote_pair_server
|
||||
from nanobot.webui.remote_pairing import PairStore, read_request
|
||||
from nanobot.webui.remote_ssh import RemoteError
|
||||
@@ -199,3 +200,93 @@ def test_explicit_host_skips_address_detection(protected, monkeypatch):
|
||||
assert result.exit_code == 0, result.output
|
||||
assert "on this server (ubuntu@server.example)" in result.output
|
||||
detect.assert_not_called()
|
||||
|
||||
|
||||
def test_root_console_with_two_accounts_goes_straight_to_device_consent(protected, monkeypatch):
|
||||
pwd = pytest.importorskip("pwd")
|
||||
protected.metadata.side_effect = None
|
||||
protected.metadata.return_value = {"port": 8765}
|
||||
monkeypatch.setattr(remote.os, "getuid", lambda: 0)
|
||||
monkeypatch.setattr(pwd, "getpwuid", lambda _: SimpleNamespace(pw_uid=0, pw_name="root"))
|
||||
monkeypatch.delenv("SUDO_USER", raising=False)
|
||||
monkeypatch.setattr(remote, "_ssh_login_users", lambda: ["lighthouse", "ubuntu"])
|
||||
monkeypatch.setattr(remote, "_cloud_ssh_user", lambda: "ubuntu")
|
||||
monkeypatch.setattr(remote, "_default_ssh_user", _default_ssh_user)
|
||||
result = CliRunner().invoke(remote.app, protected.args, input="n\n")
|
||||
assert result.exit_code == 0, result.output
|
||||
assert "on this server (ubuntu@8.8.8.8)" in result.output
|
||||
assert "Authorize this computer?" in result.output
|
||||
assert "SSH login account" not in result.output
|
||||
assert "Choose an account" not in result.output
|
||||
protected.authorize.assert_not_called()
|
||||
|
||||
|
||||
def test_ambiguous_accounts_are_numbered_and_explained_before_consent(protected, monkeypatch):
|
||||
protected.metadata.side_effect = None
|
||||
protected.metadata.return_value = {"port": 8765}
|
||||
monkeypatch.setattr(remote, "_default_ssh_user", lambda: None)
|
||||
monkeypatch.setattr(remote, "_ssh_login_users", lambda: ["deploy", "ubuntu"])
|
||||
result = CliRunner().invoke(remote.app, protected.args, input="not-a-number\n0\n3\n2\nn\n")
|
||||
assert result.exit_code == 0, result.output
|
||||
assert "1. deploy" in result.output and "2. ubuntu" in result.output
|
||||
assert "not your cloud website account" in result.output
|
||||
assert "No password is needed here" in result.output
|
||||
assert "on this server (ubuntu@8.8.8.8)" in result.output
|
||||
assert "SSH login account:" not in result.output
|
||||
assert result.output.count("Choose an account number") == 4
|
||||
assert result.output.count("Please choose a number from 1 to 2") == 2
|
||||
protected.authorize.assert_not_called()
|
||||
|
||||
|
||||
def test_account_picker_can_be_cancelled_without_authorizing(protected, monkeypatch):
|
||||
protected.metadata.side_effect = None
|
||||
protected.metadata.return_value = {"port": 8765}
|
||||
monkeypatch.setattr(remote, "_default_ssh_user", lambda: None)
|
||||
monkeypatch.setattr(remote, "_ssh_login_users", lambda: ["deploy", "ubuntu"])
|
||||
result = CliRunner().invoke(remote.app, protected.args, input="")
|
||||
assert result.exit_code == 1, result.output
|
||||
assert "Authorize this computer?" not in result.output
|
||||
protected.authorize.assert_not_called()
|
||||
|
||||
|
||||
def test_no_login_accounts_explains_recovery_without_asking_for_a_name(protected, monkeypatch):
|
||||
protected.metadata.side_effect = None
|
||||
protected.metadata.return_value = {"port": 8765}
|
||||
monkeypatch.setattr(remote, "_default_ssh_user", lambda: None)
|
||||
monkeypatch.setattr(remote, "_ssh_login_users", lambda: [])
|
||||
result = CliRunner().invoke(remote.app, protected.args)
|
||||
assert result.exit_code == 1, result.output
|
||||
assert "No regular login account was found on this server" in result.output
|
||||
assert "No device was authorized" in result.output
|
||||
assert "Choose an account number" not in result.output
|
||||
assert "Authorize this computer?" not in result.output
|
||||
protected.authorize.assert_not_called()
|
||||
|
||||
|
||||
def test_explicit_account_skips_discovery_and_selection(protected, monkeypatch):
|
||||
protected.metadata.side_effect = None
|
||||
protected.metadata.return_value = {"port": 8765}
|
||||
choose = Mock(side_effect=AssertionError("explicit account must win"))
|
||||
monkeypatch.setattr(remote, "_choose_ssh_user", choose)
|
||||
result = CliRunner().invoke(remote.app, protected.args + ["--ssh-user", "deploy"], input="n\n")
|
||||
assert result.exit_code == 0, result.output
|
||||
assert "on this server (deploy@8.8.8.8)" in result.output
|
||||
choose.assert_not_called()
|
||||
protected.authorize.assert_not_called()
|
||||
|
||||
|
||||
def test_multiple_nanobots_keep_invalid_choices_in_the_selection_step(protected):
|
||||
protected.metadata.side_effect = None
|
||||
protected.metadata.return_value = {"port": 8765}
|
||||
protected.run.return_value = SimpleNamespace(stdout="NANOBOT_REMOTE:" + json.dumps({
|
||||
"hostname": "team", "incomplete": False, "candidates": [
|
||||
{"config_path": str(protected.config), "runtime_user": "nanobot", "service": "nanobot-team.service"},
|
||||
{"config_path": str(protected.config.parent / "other.json"), "runtime_user": "nanobot", "service": "other.service"},
|
||||
],
|
||||
}))
|
||||
result = CliRunner().invoke(remote.app, ["pair", protected.request], input="0\n3\n1\nn\n")
|
||||
assert result.exit_code == 0, result.output
|
||||
assert result.output.count("Which nanobot?") == 3
|
||||
assert result.output.count("Please choose a number from 1 to 2") == 2
|
||||
protected.metadata.assert_called_once_with(protected.config)
|
||||
protected.authorize.assert_not_called()
|
||||
@@ -1,6 +1,8 @@
|
||||
"""Server pairing suggests a real login account, never blindly trusts SUDO_USER."""
|
||||
|
||||
import subprocess
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import Mock
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -14,6 +16,7 @@ from nanobot.cli import remote
|
||||
)
|
||||
def test_pair_suggests_existing_login_account(monkeypatch, uid, sudo_user, expected):
|
||||
pwd = pytest.importorskip("pwd")
|
||||
monkeypatch.setattr(remote, "_cloud_ssh_user", lambda: None)
|
||||
users = [SimpleNamespace(pw_name=name, pw_uid=user_uid, pw_shell=shell) for name, user_uid, shell in
|
||||
[("root", 0, "/bin/bash"), ("ubuntu", 1001, "/bin/bash"),
|
||||
("other", 1002, "/bin/bash"), ("nanobot", 1003, "/usr/sbin/nologin")]]
|
||||
@@ -24,6 +27,67 @@ def test_pair_suggests_existing_login_account(monkeypatch, uid, sudo_user, expec
|
||||
assert remote._default_ssh_user() == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize("configured, expected", [
|
||||
("ubuntu", "ubuntu"), ("lighthouse", "lighthouse"), ("root", None),
|
||||
("nanobot", None), ("missing", None), ("ubuntu\nlighthouse", None), (None, None),
|
||||
])
|
||||
def test_root_cloud_console_uses_only_an_existing_regular_cloud_account(monkeypatch, configured, expected):
|
||||
pwd = pytest.importorskip("pwd")
|
||||
monkeypatch.setattr(remote.os, "getuid", lambda: 0)
|
||||
monkeypatch.setattr(pwd, "getpwuid", lambda _: SimpleNamespace(pw_uid=0, pw_name="root"))
|
||||
monkeypatch.delenv("SUDO_USER", raising=False)
|
||||
monkeypatch.setattr(remote, "_ssh_login_users", lambda: ["lighthouse", "ubuntu"])
|
||||
monkeypatch.setattr(remote, "_cloud_ssh_user", lambda: configured)
|
||||
assert remote._default_ssh_user() == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize("uid,sudo_user,users,expected", [
|
||||
(1001, "", ["deploy", "ubuntu"], "deploy"),
|
||||
(0, "deploy", ["deploy", "ubuntu"], "deploy"),
|
||||
(0, "", ["deploy"], "deploy"), (0, "", [], None),
|
||||
])
|
||||
def test_known_login_and_single_account_do_not_need_cloud_lookup(monkeypatch, uid, sudo_user, users, expected):
|
||||
pwd = pytest.importorskip("pwd")
|
||||
monkeypatch.setattr(remote.os, "getuid", lambda: uid)
|
||||
monkeypatch.setattr(pwd, "getpwuid", lambda _: SimpleNamespace(pw_uid=uid, pw_name="deploy" if uid else "root"))
|
||||
monkeypatch.setenv("SUDO_USER", sudo_user)
|
||||
monkeypatch.setattr(remote, "_ssh_login_users", lambda: users)
|
||||
lookup = Mock(side_effect=AssertionError("unnecessary cloud lookup"))
|
||||
monkeypatch.setattr(remote, "_cloud_ssh_user", lookup)
|
||||
assert remote._default_ssh_user() == expected
|
||||
lookup.assert_not_called()
|
||||
|
||||
|
||||
def test_cloud_lookup_reads_only_the_configured_username_with_a_timeout(monkeypatch):
|
||||
monkeypatch.setattr(remote.shutil, "which", lambda _: "/usr/bin/cloud-init")
|
||||
run = Mock(return_value=SimpleNamespace(returncode=0, stdout="ubuntu\n"))
|
||||
monkeypatch.setattr(remote.subprocess, "run", run)
|
||||
assert remote._cloud_ssh_user() == "ubuntu"
|
||||
run.assert_called_once_with(["/usr/bin/cloud-init", "query", "system_info.default_user.name"],
|
||||
capture_output=True, text=True, timeout=2, check=False)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("failure", [OSError(), UnicodeError(), subprocess.TimeoutExpired("cloud-init", 2)])
|
||||
def test_unavailable_cloud_data_keeps_the_account_picker(monkeypatch, failure):
|
||||
monkeypatch.setattr(remote.shutil, "which", lambda _: "/usr/bin/cloud-init")
|
||||
monkeypatch.setattr(remote.subprocess, "run", Mock(side_effect=failure))
|
||||
assert remote._cloud_ssh_user() is None
|
||||
|
||||
|
||||
def test_failed_cloud_query_does_not_use_error_output_as_an_account(monkeypatch):
|
||||
monkeypatch.setattr(remote.shutil, "which", lambda _: "/usr/bin/cloud-init")
|
||||
monkeypatch.setattr(remote.subprocess, "run", Mock(return_value=SimpleNamespace(returncode=1, stdout="ubuntu")))
|
||||
assert remote._cloud_ssh_user() is None
|
||||
|
||||
|
||||
def test_missing_cloud_init_does_not_run_an_external_command(monkeypatch):
|
||||
monkeypatch.setattr(remote.shutil, "which", lambda _: None)
|
||||
run = Mock()
|
||||
monkeypatch.setattr(remote.subprocess, "run", run)
|
||||
assert remote._cloud_ssh_user() is None
|
||||
run.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(("connection", "expected"), [
|
||||
("1.1.1.1 50000 8.8.8.8 22", "8.8.8.8"),
|
||||
("1.1.1.1 50000 10.0.0.8 22", ""),
|
||||
|
||||
Reference in new issue
Block a user