From dddeaf4b03fc9c0d0d9f67c9ec8800ab4e6c92c1 Mon Sep 17 00:00:00 2001 From: Fede Luzzi Date: Tue, 17 Nov 2026 14:44:05 -0300 Subject: [PATCH] feat(spec-05-auth-fallback-chain): worker (eng_flash_high) implementation --- connpy/services/profile_service.py | 28 ++--- connpy/tests/test_auth_fallback_chain.py | 139 +++++++++++++++++++++++ 2 files changed, 151 insertions(+), 16 deletions(-) diff --git a/connpy/services/profile_service.py b/connpy/services/profile_service.py index 92f9c3d..c1d5460 100644 --- a/connpy/services/profile_service.py +++ b/connpy/services/profile_service.py @@ -141,18 +141,13 @@ class ProfileService(BaseService): resolved = deepcopy(node_data) 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 - ) + for key in ("user", "password"): + value = resolved.get(key) if isinstance(value, str): if not is_compose_expression(value) and value.startswith("@"): ref = value[1:] if key != "password" or ref in profiles: - target_refs.append(ref) + auth_referenced_profiles.append(ref) elif isinstance(value, (list, tuple)): for item in value: if ( @@ -162,7 +157,7 @@ class ProfileService(BaseService): ): ref = item[1:] if key != "password" or ref in profiles: - target_refs.append(ref) + auth_referenced_profiles.append(ref) def _get_resolved_ref(prof_name): if prof_name in _visited or _depth >= MAX_PROFILE_DEPTH: @@ -227,15 +222,16 @@ class ProfileService(BaseService): resolved_list.append(item) resolved[key] = resolved_list - ordered_profiles = list(dict.fromkeys(auth_referenced_profiles + other_referenced_profiles)) - for base_profile_name in ordered_profiles: + dedup_auth_profiles = list(dict.fromkeys(auth_referenced_profiles)) + for base_profile_name in dedup_auth_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)) and ( - value not in ("", [], None) or key not in resolved - ): - resolved[key] = deepcopy(value) + for key in ("user", "password", "fallback"): + val = base_profile.get(key) + if ( + key not in resolved or resolved[key] in ("", [], None) + ) and val not in ("", [], None): + resolved[key] = deepcopy(val) if resolved.get("protocol") in ("", None): try: diff --git a/connpy/tests/test_auth_fallback_chain.py b/connpy/tests/test_auth_fallback_chain.py index cf4eb82..4d03784 100644 --- a/connpy/tests/test_auth_fallback_chain.py +++ b/connpy/tests/test_auth_fallback_chain.py @@ -2141,3 +2141,142 @@ def test_interactive_mode_fallback_proxycommand_sshkey_and_no_password_ux(tmp_pa with patch("builtins.print") as m_print_sync: n_nopw._setup_interact_environment(async_mode=False) m_print_sync.assert_called_once() + + +def test_profile_service_strict_auth_inheritance_no_options_leak_and_no_non_auth_fallback( + tmp_path: Path, +) -> None: + cfg = _make_config( + tmp_path, + { + "profiles": { + "default": { + "protocol": "ssh", + "logs": "/tmp/conn.log", + "options": "", + "fallback": "", + }, + "6wind": {"tags": {"os": "6wind"}, "options": "", "fallback": ""}, + "dish": { + "user": "federico.luzzi", + "password": "dish_password", + "options": "-J jumpconnect", + "fallback": "@sdnc", + }, + "sdnc": { + "user": "sdnc_admin", + "password": "sdnc_password", + "options": "", + "fallback": "", + }, + "aws_with_fb": { + "options": "-i ~/.ssh/id_rsa", + "fallback": "@sdnc", + }, + }, + "connections": { + "dish": { + "type": "folder", + "jumpintconnect": { + "type": "connection", + "host": "10.10.0.1", + "protocol": "ssh", + "port": "", + "user": "jump_u1", + "password": "", + "options": "", + "logs": "", + "tags": "", + "jumphost": "", + }, + "jumpintchy": { + "type": "connection", + "host": "10.10.0.2", + "protocol": "ssh", + "port": "", + "user": "jump_u2", + "password": "", + "options": "", + "logs": "", + "tags": "", + "jumphost": "jumpintconnect@dish", + }, + "xw_int": { + "type": "connection", + "host": "172.31.10.12", + "protocol": "ssh", + "port": "", + "options": "", + "logs": "@default", + "tags": "@6wind", + "user": "@dish", + "password": ["@dish"], + "jumphost": "jumpintchy@dish", + "fallback": "", + }, + }, + }, + }, + ) + + p_svc = ProfileService(cfg) + raw_xw = cfg.getitem("xw_int@dish", extract=False) + resolved_xw = p_svc.resolve_node_data(raw_xw) + + assert resolved_xw["options"] == "" + assert "-J jumpconnect" not in resolved_xw["options"] + assert resolved_xw["user"] == "federico.luzzi" + assert resolved_xw["password"] == ["dish_password"] + assert resolved_xw["fallback"] == "@sdnc" + assert resolved_xw["logs"] == "/tmp/conn.log" + assert resolved_xw["tags"] == {"os": "6wind"} + + n_svc = NodeClass("xw_int@dish", **resolved_xw, config=cfg) + cmd_svc = n_svc._get_cmd() + assert "ProxyCommand=" in cmd_svc + assert "10.10.0.1" in cmd_svc + assert "10.10.0.2" in cmd_svc + assert "-J jumpconnect" not in cmd_svc + + ext_xw = cfg.getitem("xw_int@dish", extract=True) + assert ext_xw["options"] == "" + assert ext_xw["user"] == "federico.luzzi" + assert ext_xw["password"] == ["dish_password"] + n_ext = NodeClass("xw_int@dish", **ext_xw, config=cfg) + assert n_ext._auth_candidates == n_svc._auth_candidates + assert n_ext._get_cmd() == cmd_svc + + # Negative check: non-auth profile refs in jumphost/options/tags/logs never inherit fallback or creds + for non_auth_node in ( + { + "host": "10.20.0.1", + "user": "local_user", + "password": "local_pw", + "jumphost": "@dish", + "fallback": "", + }, + { + "host": "10.20.0.2", + "user": "local_user", + "password": "local_pw", + "options": "@aws_with_fb", + "fallback": "", + }, + { + "host": "10.20.0.3", + "user": "", + "password": "", + "jumphost": "@dish", + "fallback": "", + }, + ): + res_neg = p_svc.resolve_node_data(non_auth_node) + assert res_neg["fallback"] == "" + if non_auth_node["user"] == "local_user": + n_neg = NodeClass("n_neg", **res_neg, config=cfg) + assert [(c.user, c.is_fallback) for c in n_neg._auth_candidates] == [ + ("local_user", False) + ] + else: + assert res_neg["user"] == "" + assert res_neg["password"] == ""