"""How people log in to the firewall: the web interface's auth servers and SSH (NetOrk/netork#662). OPNsense tries a login against each server named in ```` in turn, and the first that accepts it lets the person in (``AuthenticationFactory::authenticate``). The ``System`` service runs the same list for ``sshd``, ``login``, ``su`` and ``sudo``, so an SSH password goes through it too. A key over SSH does not. sshd's settings come from ```` (``openssh_configure_do``). ``config_xml_sample.xml`` is OPNsense's own sample: no ``authmode`` (the Local Database alone) and an ```` without ``enabled`` (sshd off). """ from __future__ import annotations from unittest.mock import MagicMock, patch from xml.etree.ElementTree import ParseError import pytest from napalm_opnsense.login_auth import login_sources, ssh_server_auth from napalm_opnsense.opnsense import OPNsenseDriver from .test_time_sync import CONFIG_PATH, CONFIG_XML, Firewall TOTP_SERVER = """ 65a1 totp TOTP 6 """ LDAP_SERVER = """ 65a2 ldap Directory """ LDAP_TOTP_SERVER = """ 65a3 ldap-totp Directory OTP """ RADIUS_SERVER = """ 65a4 radius RADIUS """ PLUGIN_SERVER = """ 65a5 sso-thing Plugin """ ALL_SERVERS = TOTP_SERVER + LDAP_SERVER + LDAP_TOTP_SERVER + RADIUS_SERVER + PLUGIN_SERVER def _config(*, authmode: str | None = None, servers: str = "", ssh: str | None = None) -> str: """The sample with an ``authmode``, extra ```` entries, another ````.""" xml = CONFIG_XML if authmode is not None: xml = xml.replace( "1", f"1\n {authmode}" ) if ssh is not None: xml = xml.replace("\n admins\n ", f"{ssh}") return xml.replace("", f"{servers}\n ", 1) def _source(name: str, kind: str, factors: str) -> dict: return {"name": name, "kind": kind, "factors": factors} LOCAL = _source("Local Database", "local", "password") TOTP = _source("TOTP", "totp", "password_otp") class TestLoginSources: def test_without_authmode_the_local_database_alone(self): assert login_sources(CONFIG_XML) == [LOCAL] def test_an_empty_authmode_is_the_local_database_too(self): assert login_sources(_config(authmode="")) == [LOCAL] def test_each_server_in_the_order_authmode_names_them(self): xml = _config(authmode="TOTP,Local Database", servers=TOTP_SERVER) assert login_sources(xml) == [TOTP, LOCAL] def test_what_each_kind_of_server_asks_for(self): xml = _config(authmode="TOTP,Directory,Directory OTP,RADIUS,Plugin", servers=ALL_SERVERS) assert login_sources(xml) == [ TOTP, _source("Directory", "ldap", "password"), _source("Directory OTP", "ldap-totp", "password_otp"), _source("RADIUS", "radius", "external"), _source("Plugin", "sso-thing", "unknown"), ] def test_a_name_no_server_has_is_skipped_as_opnsense_skips_it(self): # get() returns null for it, and authenticate() goes on with the next name. # Names are not trimmed: " TOTP" is no server's name either. xml = _config(authmode="Gone,TOTP, TOTP,Local API", servers=TOTP_SERVER) assert login_sources(xml) == [TOTP] def test_a_server_without_type_or_name_is_not_listed(self): nameless = "totp" typeless = "Bare" xml = _config(authmode="Bare", servers=nameless + typeless) assert login_sources(xml) == [] def test_a_configured_server_named_local_database_replaces_the_built_in_one(self): renamed = "totpLocal Database" assert login_sources(_config(servers=renamed)) == [ _source("Local Database", "totp", "password_otp") ] def test_a_text_that_is_no_xml_raises(self): with pytest.raises(ParseError): login_sources("login1")) assert auth["password_authentication"] is True assert auth["kbd_interactive_authentication"] is True def test_presence_counts_as_opnsense_tests_it_with_isset(self): auth = ssh_server_auth(_config(ssh="")) assert auth["server_enabled"] is True assert auth["password_authentication"] is True assert auth["permit_root_login"] == "yes" def test_an_ssh_password_goes_through_the_web_interface_s_servers(self): xml = _config(authmode="TOTP", servers=TOTP_SERVER, ssh=SSH_ON) assert ssh_server_auth(xml)["password_sources"] == [TOTP] def test_no_ssh_element_at_all_is_off(self): xml = CONFIG_XML.replace("\n admins\n ", "") assert ssh_server_auth(xml)["server_enabled"] is False @pytest.fixture def driver(): with patch("napalm_opnsense.opnsense.requests.Session"): drv = OPNsenseDriver( hostname="fw", username="k", password="s", optional_args={"verify": False} ) drv.session = MagicMock() yield drv class TestDriver: def test_the_web_login_from_config_xml(self, driver): firewall = Firewall(config=_config(authmode="TOTP", servers=TOTP_SERVER)) driver.session.get.side_effect = firewall.get assert driver.get_web_login_auth() == {"sources": [TOTP]} assert firewall.paths == [CONFIG_PATH] def test_the_ssh_login_from_config_xml(self, driver): firewall = Firewall(config=_config(ssh=SSH_ON)) driver.session.get.side_effect = firewall.get assert driver.get_ssh_server_auth()["server_enabled"] is True assert firewall.paths == [CONFIG_PATH] def test_a_failed_download_raises(self, driver): import requests driver.session.get.side_effect = Firewall(config=requests.HTTPError("403 Forbidden")).get with pytest.raises(requests.HTTPError): driver.get_web_login_auth() with pytest.raises(requests.HTTPError): driver.get_ssh_server_auth()