feat(spec-05-auth-fallback-chain): worker (eng_flash_high) implementation

This commit is contained in:
fluzzi32 committed 2026-11-17 12:58:00 -03:00
1 parent d2da49221f
commit c6f260efc9
5 files changed
+456 -61

No files matched your search

+35 -12
View File
@@ -137,11 +137,7 @@ class Forms:
questions.append(inquirer.List("password", message="Password: Use a local password, no password or a list of profiles to reference?", choices=["Local Password", "Profiles", "No Password"]))
else:
node["password"] = defaults["password"]
if edit.get("fallback"):
raw_def_fb = defaults.get("fallback", "")
def_fb_str = json.dumps(raw_def_fb) if isinstance(raw_def_fb, (list, dict)) else str(raw_def_fb or "")
questions.append(inquirer.Text("fallback", message="Add fallback credentials (JSON list/dict, @profile, or compose:)", validate=self.validators.fallback_validation, default=def_fb_str.replace("{", "{{").replace("}", "}}")))
else:
if not edit.get("fallback"):
node["fallback"] = defaults.get("fallback", "")
answer = inquirer.prompt(questions)
@@ -160,14 +156,28 @@ class Forms:
passa = inquirer.prompt(passq)
if passa is None:
return False
answer["password"] = passa["password"].split(",")
answer["password"] = [p.strip() for p in passa["password"].split(",") if p.strip()]
elif answer["password"] == "No Password":
answer["password"] = ""
if edit.get("fallback"):
raw_def_fb = defaults.get("fallback", "")
def_fb_str = json.dumps(raw_def_fb) if isinstance(raw_def_fb, (list, dict)) else str(raw_def_fb or "")
fbq = [
inquirer.Text(
"fallback",
message="Add fallback credentials (JSON list/dict, @profile, or compose:)",
validate=self.validators.fallback_validation,
default=def_fb_str.replace("{", "{{").replace("}", "}}"),
)
]
fba = inquirer.prompt(fbq)
if fba is None:
return False
answer["fallback"] = _parse_fallback_input(fba["fallback"])
if "tags" in answer and answer["tags"] and not str(answer["tags"]).startswith("@"):
answer["tags"] = _parse_tags_input(answer["tags"])
if "fallback" in answer:
answer["fallback"] = _parse_fallback_input(answer["fallback"])
result = {**uniques, **answer, **node}
result["type"] = "connection"
@@ -262,7 +272,6 @@ class Forms:
questions.append(inquirer.Text("jumphost", message="Add Jumphost node", validate=self.validators.jumphost_validation))
questions.append(inquirer.Text("user", message="Pick username", validate=self.validators.default_validation))
questions.append(inquirer.List("password", message="Password: Use a local password, no password or a list of profiles to reference?", choices=["Local Password", "Profiles", "No Password"]))
questions.append(inquirer.Text("fallback", message="Add fallback credentials (JSON list/dict, @profile, or compose:)", validate=self.validators.fallback_validation))
answer = inquirer.prompt(questions)
if answer is None:
@@ -272,19 +281,33 @@ class Forms:
if answer["password"] == "Local Password":
passq = [inquirer.Password("password", message="Set Password")]
passa = inquirer.prompt(passq)
if passa is None:
return False
answer["password"] = self.app.services.config_svc.encrypt_password(passa["password"])
elif answer["password"] == "Profiles":
passq = [(inquirer.Text("password", message="Set a @profile or a comma separated list of @profiles", validate=self.validators.pass_validation))]
passa = inquirer.prompt(passq)
answer["password"] = passa["password"].split(",")
if passa is None:
return False
answer["password"] = [p.strip() for p in passa["password"].split(",") if p.strip()]
elif answer["password"] == "No Password":
answer["password"] = ""
fbq = [
inquirer.Text(
"fallback",
message="Add fallback credentials (JSON list/dict, @profile, or compose:)",
validate=self.validators.fallback_validation,
)
]
fba = inquirer.prompt(fbq)
if fba is None:
return False
answer["fallback"] = _parse_fallback_input(fba["fallback"])
answer["type"] = "connection"
if "tags" in answer and answer["tags"] and not str(answer["tags"]).startswith("@"):
answer["tags"] = _parse_tags_input(answer["tags"])
if "fallback" in answer:
answer["fallback"] = _parse_fallback_input(answer["fallback"])
return answer
+23 -2
View File
@@ -610,7 +610,15 @@ class configfile:
def _find_inherited_profile_fallback_unlocked(self, raw_user, raw_pw):
"""Inherit fallback from a primary @profile referenced by node user/password."""
for candidate_val, field in ((raw_user, "user"), (raw_pw, "password")):
candidates = []
for raw_val, field in ((raw_user, "user"), (raw_pw, "password")):
if isinstance(raw_val, (list, tuple)):
for item in raw_val:
candidates.append((item, field))
else:
candidates.append((raw_val, field))
for candidate_val, field in candidates:
if (
isinstance(candidate_val, str)
and not is_compose_expression(candidate_val)
@@ -630,7 +638,20 @@ class configfile:
if prof_fb not in (None, "", []):
return deepcopy(prof_fb)
next_val = prof.get(field)
if (
if isinstance(next_val, (list, tuple)):
next_ref = next(
(
item
for item in next_val
if isinstance(item, str)
and not is_compose_expression(item)
and item.startswith("@")
and "+" not in item
),
None,
)
prof_name = next_ref[1:] if next_ref else ""
elif (
isinstance(next_val, str)
and not is_compose_expression(next_val)
and next_val.startswith("@")
+43 -40
View File
@@ -1444,8 +1444,8 @@ class node:
log_thread = threading.Thread(target=self._savelog)
log_thread.daemon = True
log_thread.start()
if 'missingtext' in dir(self):
print(self.child.after.decode(), end='')
if getattr(self, "missingtext", False) and not async_mode:
print(self.child.after.decode(), end="")
if self.idletime > 0 and not async_mode:
x = threading.Thread(target=self._keepalive)
x.daemon = True
@@ -2841,6 +2841,7 @@ class node:
unattended=False,
attempt_tracker=None,
):
self.__dict__.pop("missingtext", None)
self.last_fallback_used = False
effective_protocol = "ssh" if force_ssh else self.protocol
if effective_protocol == "mosh" and shutil.which("mosh") is None:
@@ -2934,6 +2935,9 @@ class node:
)
return str(san)
has_configured_passwords = any(c.has_password for c in runtime_candidates)
use_auth_chain = bool(unattended or has_configured_passwords)
if self.logs != '':
self.logfile = self._logfile()
default_prompt = r'>$|#$|\$$|>.$|#.$|\$.$'
@@ -3152,9 +3156,13 @@ class node:
}
def _handle_username_prompt(child_obj) -> tuple[bool, str | None]:
if not unattended:
if self.user:
child_obj.sendline(self.user)
if not use_auth_chain:
manual_user = (
(runtime_candidates[0].user if runtime_candidates else "")
or str(self.user or "")
)
if manual_user:
child_obj.sendline(manual_user)
st["last_observed_bytes_len"] = 0
return False, None
self.missingtext = True
@@ -3221,28 +3229,7 @@ class node:
cur_cand = st["cur_cand"]
cur_user = str(st["cur_user"])
if not unattended:
if cur_cand is not None and cur_cand.has_password and not st["cand_sent_pw_this_conn"]:
child_obj.sendline(cur_cand.password)
st["cand_sent_pw_this_conn"] = True
st["target_creds_sent"] = True
st["handshake_progressed"] = True
st["stage"] = "auth"
st["last_observed_bytes_len"] = 0
return False, None
if (cand_idx_cur + 1) < len(runtime_candidates) and not runtime_candidates[cand_idx_cur + 1].is_fallback:
cand_idx_cur += 1
st["cand_idx"] = cand_idx_cur
cur_cand = runtime_candidates[cand_idx_cur]
st["cur_cand"] = cur_cand
if cur_cand.has_password:
child_obj.sendline(cur_cand.password)
st["cand_sent_pw_this_conn"] = True
st["target_creds_sent"] = True
st["handshake_progressed"] = True
st["stage"] = "auth"
st["last_observed_bytes_len"] = 0
return False, None
if not use_auth_chain:
self.missingtext = True
return True, None
@@ -3453,7 +3440,7 @@ class node:
continue
if (
unattended
use_auth_chain
and st["stage"] == "auth"
and st["target_creds_sent"]
and (int(st["cand_idx"]) + 1) < len(runtime_candidates)
@@ -3492,7 +3479,7 @@ class node:
and b"permission denied" in (before_bytes + after_bytes).lower()
)
if (
unattended
use_auth_chain
and is_perm_denied
and (int(st["cand_idx"]) + 1) < len(runtime_candidates)
and remaining > 0
@@ -3579,15 +3566,29 @@ class node:
if kind == "target":
hop_cursor = len(jumphost_hops)
else:
allow_ambig = bool(
tags_dict.get("allow_ambiguous_single_hop_prompt", False)
no_pending_hop_pws = all(
hop_pw_idx[i] >= len(hop_runtime_passwords[i])
for i in range(hop_cursor, len(jumphost_hops))
)
if len(jumphost_hops) > 1 or not allow_ambig:
return str(
sanitize_exception(None, code="JUMP_AUTH_FAILED", secrets=raw_secrets)
if (
st["target_creds_sent"]
or hop_cursor >= len(jumphost_hops)
or no_pending_hop_pws
):
hop_cursor = len(jumphost_hops)
else:
allow_ambig = bool(
tags_dict.get("allow_ambiguous_single_hop_prompt", False)
)
if not st["target_creds_sent"] and hop_pw_idx[0] < len(hop_runtime_passwords[0]):
hop_obj = jumphost_hops[0]
if len(jumphost_hops) > 1 or not allow_ambig:
return str(
sanitize_exception(
None,
code="JUMP_AUTH_FAILED",
secrets=raw_secrets,
)
)
hop_obj = jumphost_hops[hop_cursor]
if not tracker.try_acquire_hop(
hop_obj["host"],
hop_obj["port"],
@@ -3601,8 +3602,10 @@ class node:
secrets=raw_secrets,
)
)
child.sendline(hop_runtime_passwords[0][hop_pw_idx[0]])
hop_pw_idx[0] += 1
child.sendline(
hop_runtime_passwords[hop_cursor][hop_pw_idx[hop_cursor]]
)
hop_pw_idx[hop_cursor] += 1
st["handshake_progressed"] = True
st["last_observed_bytes_len"] = 0
continue
@@ -3636,7 +3639,7 @@ class node:
and b"permission denied" in m.group(0).lower()
)
if (
unattended
use_auth_chain
and is_pd
and (int(st["cand_idx"]) + 1) < len(runtime_candidates)
and remaining > 0
@@ -3658,7 +3661,7 @@ class node:
next_action = "next_candidate"
break
if unattended and not transport_error_seen:
if use_auth_chain and not transport_error_seen:
if st["target_creds_sent"]:
if (int(st["cand_idx"]) + 1) < len(runtime_candidates):
eof_adv = int(st["eof_fallback_advances"])
+13 -7
View File
@@ -140,16 +140,20 @@ class ProfileService(BaseService):
)
resolved = deepcopy(node_data)
referenced_profiles = []
auth_referenced_profiles = []
other_referenced_profiles = []
for key, value in resolved.items():
if key == "fallback":
continue
target_refs = (
auth_referenced_profiles if key in ("user", "password") else other_referenced_profiles
)
if isinstance(value, str):
if not is_compose_expression(value) and value.startswith("@"):
ref = value[1:]
if key != "password" or ref in profiles:
referenced_profiles.append(ref)
elif isinstance(value, list):
target_refs.append(ref)
elif isinstance(value, (list, tuple)):
for item in value:
if (
isinstance(item, str)
@@ -158,7 +162,7 @@ class ProfileService(BaseService):
):
ref = item[1:]
if key != "password" or ref in profiles:
referenced_profiles.append(ref)
target_refs.append(ref)
def _get_resolved_ref(prof_name):
if prof_name in _visited or _depth >= MAX_PROFILE_DEPTH:
@@ -223,12 +227,14 @@ class ProfileService(BaseService):
resolved_list.append(item)
resolved[key] = resolved_list
if referenced_profiles:
base_profile_name = referenced_profiles[0]
ordered_profiles = list(dict.fromkeys(auth_referenced_profiles + other_referenced_profiles))
for base_profile_name in ordered_profiles:
with suppress(ProfileNotFoundError):
base_profile = _get_resolved_ref(base_profile_name)
for key, value in base_profile.items():
if key not in resolved or resolved[key] in ("", [], None):
if (key not in resolved or resolved[key] in ("", [], None)) and (
value not in ("", [], None) or key not in resolved
):
resolved[key] = deepcopy(value)
if resolved.get("protocol") in ("", None):
+342
View File
@@ -1799,3 +1799,345 @@ def test_signoff_ronda2_eof_empty_profiles_host_re_shlex_and_lazy_import() -> No
for bad_user_limit in (True, False, 0, -1, 1001, "2"):
with pytest.raises(ValueError):
AuthAttemptTracker(default_max_global_user_attempts=bad_user_limit)
def test_forms_prompt_sequence_and_safe_cancel(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setenv("HOME", str(tmp_path))
cfg = _make_config(
tmp_path,
{
"profiles": {
"default": {"protocol": "ssh", "fallback": ""},
"p1": {"user": "u1", "password": "pw1"},
"p2": {"user": "u2", "password": "pw2"},
"fb": {"user": "fb_u", "password": "fb_p"},
}
},
)
app = MagicMock()
app.config = cfg
app.profiles = list(cfg.profiles.keys())
app.nodes_list = []
app.services = ServiceProvider(cfg)
forms = Forms(app)
# 1. questions_nodes: base questions -> passq -> fallback; spaces stripped in Profiles list
recorded_nodes: list[list[tuple[str, str]]] = []
node_answers = iter([
{
"host": "10.0.0.1",
"protocol": "ssh",
"port": "22",
"options": "",
"logs": "",
"tags": "",
"jumphost": "",
"user": "u",
"password": "Profiles",
},
{"password": " @p1 , @p2 "},
{"fallback": "@fb"},
])
def _mock_prompt_nodes(qs, **_kw):
recorded_nodes.append([(q.name, str(getattr(q, "message", ""))) for q in qs])
return next(node_answers)
with patch("inquirer.prompt", side_effect=_mock_prompt_nodes):
node_res = forms.questions_nodes("r_ux", uniques={"type": "connection"})
assert isinstance(node_res, dict)
assert node_res["password"] == ["@p1", "@p2"]
assert node_res["fallback"] == "@fb"
assert len(recorded_nodes) == 3
first_names = [n for n, _ in recorded_nodes[0]]
assert "password" in first_names
assert "fallback" not in first_names
assert [n for n, _ in recorded_nodes[1]] == ["password"]
assert "@profile" in recorded_nodes[1][0][1]
assert [n for n, _ in recorded_nodes[2]] == ["fallback"]
# Cancel during passq returns False safely without raising
with patch(
"inquirer.prompt",
side_effect=[{"host": "10.0.0.1", "password": "Local Password"}, None],
):
assert forms.questions_nodes("r_cancel", uniques={"type": "connection"}) is False
# Cancel during fbq returns False safely
with patch(
"inquirer.prompt",
side_effect=[{"host": "10.0.0.1", "password": "No Password"}, None],
):
assert forms.questions_nodes("r_cancel_fb", uniques={"type": "connection"}) is False
# 2. questions_bulk: base questions -> passq -> fallback, plus cancel on passq & fbq
recorded_bulk: list[list[tuple[str, str]]] = []
bulk_answers = iter([
{
"ids": "r_b1",
"location": "",
"host": "10.1.1.1",
"protocol": "ssh",
"port": "22",
"options": "",
"logs": "",
"tags": "",
"jumphost": "",
"user": "u1",
"password": "Local Password",
},
{"password": "MyBulkPassword"},
{"fallback": '[{"user": "bak", "password": "p2"}]'},
])
def _mock_prompt_bulk(qs, **_kw):
recorded_bulk.append([(q.name, str(getattr(q, "message", ""))) for q in qs])
return next(bulk_answers)
with patch("inquirer.prompt", side_effect=_mock_prompt_bulk):
bulk_res = forms.questions_bulk()
assert isinstance(bulk_res, dict)
assert bulk_res["fallback"] == [{"user": "bak", "password": "p2"}]
assert len(recorded_bulk) == 3
bulk_first = [n for n, _ in recorded_bulk[0]]
assert "password" in bulk_first
assert "fallback" not in bulk_first
assert recorded_bulk[1] == [("password", "Set Password")]
assert [n for n, _ in recorded_bulk[2]] == ["fallback"]
with patch(
"inquirer.prompt",
side_effect=[{"ids": "r_b2", "password": "Local Password"}, None],
):
assert forms.questions_bulk() is False
with patch(
"inquirer.prompt",
side_effect=[{"ids": "r_b3", "password": "No Password"}, None],
):
assert forms.questions_bulk() is False
def test_multi_profile_fallback_inheritance_priority_and_password_list(tmp_path: Path) -> None:
cfg = _make_config(
tmp_path,
{
"profiles": {
"default": {"protocol": "ssh", "fallback": ""},
"aws": {"options": "-i ~/.ssh/id_rsa", "fallback": ""},
"dish": {"user": "federico.luzzi", "password": "dish_password", "fallback": "@sdnc"},
"sdnc": {"user": "sdnc_admin", "password": "sdnc_password", "fallback": ""},
}
},
)
cfg._connections_add(
id="n_multi",
host="10.230.86.213",
options="@aws",
jumphost="",
user="@dish",
password=["@dish"],
fallback="",
)
cfg._saveconfig(cfg.file)
p_svc = ProfileService(cfg)
raw_details = cfg.getitem("n_multi", extract=False)
resolved_svc = p_svc.resolve_node_data(raw_details)
assert resolved_svc["user"] == "federico.luzzi"
assert resolved_svc["password"] == ["dish_password"]
assert resolved_svc["options"] == "-i ~/.ssh/id_rsa"
assert resolved_svc["fallback"] == "@sdnc"
resolved_jh_prio = p_svc.resolve_node_data({
"host": "10.230.86.214",
"jumphost": "@aws",
"user": "@dish",
"password": ["@dish"],
"fallback": "",
})
assert resolved_jh_prio["fallback"] == "@sdnc"
extracted = cfg.getitem("n_multi", extract=True)
assert extracted["tags"]["_fallback"] == [{"user": "sdnc_admin", "password": "sdnc_password"}]
n_multi = NodeClass("n_multi", **extracted, config=cfg)
assert [(c.user, c.password_spec, c.is_fallback) for c in n_multi._auth_candidates] == [
("federico.luzzi", "dish_password", False),
("sdnc_admin", "sdnc_password", True),
]
def test_interactive_mode_fallback_proxycommand_sshkey_and_no_password_ux(tmp_path: Path) -> None:
cfg = _make_config(
tmp_path,
{
"profiles": {
"default": {"protocol": "ssh", "fallback": ""},
"aws": {"user": "ec2-user", "password": "", "options": "-i ~/.ssh/id_rsa", "fallback": ""},
"dish": {"user": "federico.luzzi", "password": "dish_password", "fallback": "@sdnc"},
"sdnc": {"user": "sdnc_admin", "password": "sdnc_password", "fallback": ""},
},
"connections": {
"aws_bastion": {
"host": "bastion.example.internal",
"protocol": "ssh",
"port": "",
"user": "ec2-user",
"password": "",
"options": "-i ~/.ssh/id_rsa",
"logs": "",
"tags": "",
"jumphost": "",
"type": "connection",
},
"field_router": {
"host": "10.230.86.213",
"protocol": "ssh",
"port": "",
"user": "@dish",
"password": ["@dish"],
"options": "",
"logs": "",
"tags": "",
"jumphost": "aws_bastion",
"fallback": "",
"type": "connection",
},
},
},
)
extracted = cfg.getitem("field_router", extract=True)
n = NodeClass("field_router", **extracted, config=cfg)
assert len(n._jumphost_hops) == 1
assert n._jumphost_hops[0]["passwords"] == ()
# Case 1: Interactive mode (unattended=False) hits primary reject -> generic password prompt ->
# respawns as sdnc_admin, handles generic password prompt via SSH-key hop disambiguation, succeeds!
spawn_cmds: list[str] = []
sent_passwords: list[str] = []
events: list[str] = []
def _logger(level: str, msg: str) -> None:
events.append(f"{level}:{msg}")
def _spawn_field(*args, **_kw):
cmd_tokens: list[str] = []
if args:
if isinstance(args[0], (list, tuple)):
cmd_tokens.extend(str(x) for x in args[0])
else:
cmd_tokens.append(str(args[0]))
if len(args) > 1 and isinstance(args[1], (list, tuple)):
cmd_tokens.extend(str(x) for x in args[1])
spawn_cmds.append(" ".join(cmd_tokens))
conn_no = len(spawn_cmds)
step = {"i": 0}
ch = None
def _exp(_p, timeout=None):
i = step["i"]
step["i"] += 1
if conn_no == 1:
if i == 0:
ch.before = b"federico.luzzi@10.230.86.213's "
ch.after = b"password:"
return 8
# Router without TACACS sends generic password: prompt on retry
ch.before = b""
ch.after = b"password:"
return 8
if i == 0:
# Spawn #2 receives generic password: prompt with SSH-key ProxyCommand hop
ch.before = b""
ch.after = b"password:"
return 8
ch.before = b""
ch.after = b"router#"
return 9
ch = _make_mock_child(expect_fn=_exp)
ch.sendline.side_effect = lambda v="": (
events.append(f"send:{v}"),
sent_passwords.append(str(v)),
)[-1]
return ch
with (
patch("connpy.core.pexpect.spawn", side_effect=_spawn_field),
patch("pexpect.fdpexpect.fdspawn", return_value=MagicMock()),
):
res_ok = n._connect(unattended=False, logger=_logger)
assert res_ok is True
assert n.last_fallback_used is True
assert getattr(n, "missingtext", False) is False
assert len(spawn_cmds) == 2
assert "federico.luzzi@10.230.86.213" in spawn_cmds[0]
assert "sdnc_admin@10.230.86.213" in spawn_cmds[1]
assert sent_passwords == ["dish_password", "sdnc_password", ""]
aaa_idx = next(i for i, e in enumerate(events) if "[AAA-FALLBACK]" in e)
send_fb_idx = next(i for i, e in enumerate(events) if e == "send:sdnc_password")
assert aaa_idx < send_fb_idx
# Case 2: Configured credentials all rejected in unattended=False -> [AUTH_FAILED], no missingtext, no Connected
n_fail = NodeClass("field_router", **cfg.getitem("field_router", extract=True), config=cfg)
fail_logs: list[str] = []
def _spawn_all_reject(*_args, **_kw):
ch = _make_mock_child(
expect_fn=lambda _p, timeout=None: (
setattr(ch, "before", b""),
setattr(ch, "after", b"password:"),
8,
)[-1]
)
return ch
with patch("connpy.core.pexpect.spawn", side_effect=_spawn_all_reject):
res_fail = n_fail._connect(unattended=False)
assert res_fail is not True
assert "[AUTH_FAILED]" in str(res_fail)
assert getattr(n_fail, "missingtext", False) is False
with (
patch("connpy.core.pexpect.spawn", side_effect=_spawn_all_reject),
pytest.raises(SystemExit) as sys_exit_info,
):
n_fail.interact(logger=lambda lvl, msg: fail_logs.append(f"{lvl}:{msg}"))
assert sys_exit_info.value.code == 1
assert not any("Connected to" in entry for entry in fail_logs)
# Case 3: "No Password" node sets missingtext=True in unattended=False and avoids duplicate print in async_mode=True
n_nopw = NodeClass("r_nopw", host="10.0.0.99", user="op", password="", fallback="")
def _spawn_nopw(*_args, **_kw):
ch = _make_mock_child(
expect_fn=lambda _p, timeout=None: (
setattr(ch, "before", b""),
setattr(ch, "after", b"op@10.0.0.99's password:"),
8,
)[-1]
)
return ch
with (
patch("connpy.core.pexpect.spawn", side_effect=_spawn_nopw),
patch("pexpect.fdpexpect.fdspawn", return_value=MagicMock()),
):
res_nopw = n_nopw._connect(unattended=False)
assert res_nopw is True
assert getattr(n_nopw, "missingtext", False) is True
n_nopw.idletime = 0
with patch("builtins.print") as m_print:
n_nopw._setup_interact_environment(async_mode=True)
m_print.assert_not_called()
with patch("builtins.print") as m_print_sync:
n_nopw._setup_interact_environment(async_mode=False)
m_print_sync.assert_called_once()