driver / lint (pull_request) Successful in 15s
driver / standard (pull_request) Failing after 18s
driver / types (pull_request) Successful in 58s
driver / test (3.13) (pull_request) Successful in 1m1s
driver / test (3.12) (pull_request) Successful in 1m10s
driver / lowest (pull_request) Successful in 32s
driver / build (pull_request) Successful in 31s
driver / security (pull_request) Successful in 1m2s
CI / driver (pull_request) Failing after 2m21s
driver / publish (pull_request) Skipped
get_web_login_auth() names the auth servers <system><webgui><authmode> tries, in order (the Local Database when it names none), each with what it asks for: password for local, ldap and voucher, password_otp for totp and ldap-totp, external for radius, unknown otherwise. A name no server has is skipped, as OPNsense skips it. get_ssh_server_auth() reads <system><ssh> as openssh_configure_do writes sshd_config: on only with enabled, password and challenge-response login with passwordauth, root with permitrootlogin, a key always. An SSH password goes through the same servers (password_sources). Needs napalm-device-types 4.50.0.
213 lines
7.5 KiB
Python
213 lines
7.5 KiB
Python
"""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 ``<system><webgui><authmode>``
|
|
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 ``<system><ssh>``
|
|
(``openssh_configure_do``).
|
|
|
|
``config_xml_sample.xml`` is OPNsense's own sample: no ``authmode`` (the Local
|
|
Database alone) and an ``<ssh>`` 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 = """
|
|
<authserver>
|
|
<refid>65a1</refid>
|
|
<type>totp</type>
|
|
<name>TOTP</name>
|
|
<otpLength>6</otpLength>
|
|
</authserver>"""
|
|
LDAP_SERVER = """
|
|
<authserver>
|
|
<refid>65a2</refid>
|
|
<type>ldap</type>
|
|
<name>Directory</name>
|
|
</authserver>"""
|
|
LDAP_TOTP_SERVER = """
|
|
<authserver>
|
|
<refid>65a3</refid>
|
|
<type>ldap-totp</type>
|
|
<name>Directory OTP</name>
|
|
</authserver>"""
|
|
RADIUS_SERVER = """
|
|
<authserver>
|
|
<refid>65a4</refid>
|
|
<type>radius</type>
|
|
<name>RADIUS</name>
|
|
</authserver>"""
|
|
PLUGIN_SERVER = """
|
|
<authserver>
|
|
<refid>65a5</refid>
|
|
<type>sso-thing</type>
|
|
<name>Plugin</name>
|
|
</authserver>"""
|
|
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 ``<authserver>`` entries, another ``<ssh>``."""
|
|
xml = CONFIG_XML
|
|
if authmode is not None:
|
|
xml = xml.replace(
|
|
"<noroot>1</noroot>", f"<noroot>1</noroot>\n <authmode>{authmode}</authmode>"
|
|
)
|
|
if ssh is not None:
|
|
xml = xml.replace("<ssh>\n <group>admins</group>\n </ssh>", f"<ssh>{ssh}</ssh>")
|
|
return xml.replace("</system>", f"{servers}\n </system>", 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 = "<authserver><type>totp</type><name/></authserver>"
|
|
typeless = "<authserver><type/><name>Bare</name></authserver>"
|
|
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 = "<authserver><type>totp</type><name>Local Database</name></authserver>"
|
|
|
|
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("<html>login</html")
|
|
|
|
|
|
SSH_ON = "<enabled>enabled</enabled><group>admins</group>"
|
|
SSH_KEYS_ONLY = {
|
|
"password_authentication": False,
|
|
"kbd_interactive_authentication": False,
|
|
"pubkey_authentication": True,
|
|
"hostbased_authentication": False,
|
|
"gssapi_authentication": False,
|
|
"authentication_methods": [],
|
|
"permit_root_login": "no",
|
|
"use_pam": True,
|
|
"pam_auth_stack": None,
|
|
}
|
|
|
|
|
|
class TestSshServerAuth:
|
|
def test_without_enabled_sshd_is_off(self):
|
|
auth = ssh_server_auth(CONFIG_XML)
|
|
|
|
assert auth["server_enabled"] is False
|
|
assert auth["password_sources"] == [LOCAL]
|
|
|
|
def test_enabled_with_keys_only(self):
|
|
auth = ssh_server_auth(_config(ssh=SSH_ON))
|
|
|
|
assert auth == {**SSH_KEYS_ONLY, "password_sources": [LOCAL], "server_enabled": True}
|
|
|
|
def test_password_login_switches_challenge_response_on_with_it(self):
|
|
auth = ssh_server_auth(_config(ssh=SSH_ON + "<passwordauth>1</passwordauth>"))
|
|
|
|
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="<enabled/><passwordauth/><permitrootlogin/>"))
|
|
|
|
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("<ssh>\n <group>admins</group>\n </ssh>", "")
|
|
|
|
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()
|