Files
napalm-proxmox/tests/test_credential_expiry.py
T
Christian Manivong 87aca53ec2 fix(credentials): a token's expiry from the user it belongs to (NetOrk/netork#2728)
GET /access/users/{userid}/token/{tokenid} checks userid-param self, which a
token's request (user@realm!tokenid) never matches, so a token could not read
itself without User.Modify. GET /access/users/{userid} needs Sys.Audit on
/access/groups (PVEAuditor) and returns the user's expire and every token's.
One call now serves both logins; a 403 names the privilege.
2026-11-26 23:08:47 +01:00

152 lines
5.8 KiB
Python

"""When the credential the driver logs in with stops working (NetOrk/netork#2728).
PVE 9.3's datacenter ``token-policy`` can require an expiry on every API token;
the platform keeps one on every token and account anyway. Both are read with one
``GET /access/users/{userid}`` (``read_user``): the account's ``expire`` and
``tokens: {tokenid: {expire, privsep, comment}}``. It needs ``Sys.Audit`` (or
``User.Modify``) on ``/access/groups`` or a group of the user, which PVEAuditor
on ``/`` gives. ``GET /access/users/{userid}/token/{tokenid}`` is not used: a
token's request runs as ``user@realm!tokenid``, which its ``userid-param self``
check never matches. ``0`` is never; a token without an expiry of its own
expires with its user, and one with its own stops working with its user too.
"""
from __future__ import annotations
from typing import Any
from unittest.mock import MagicMock, patch
import pytest
from proxmoxer.core import ResourceException
from napalm_proxmox.driver import ProxmoxDriver
from tests.conftest import _build_mock_api
def _driver(**optional_args: Any) -> tuple[ProxmoxDriver, MagicMock]:
password = "" if optional_args else "secret"
drv = ProxmoxDriver("pve1", "netork", password, optional_args=optional_args)
api = _build_mock_api()
with patch("napalm_proxmox.driver.ProxmoxAPI", return_value=api):
drv.open()
return drv, api
def _token_driver(token_name: str = "netork@pve!poll") -> tuple[ProxmoxDriver, MagicMock]:
return _driver(token_name=token_name, token_value="s3cret", realm="pve")
def _user(api: MagicMock, expire: int | None = None, **tokens: dict[str, Any]) -> MagicMock:
"""``GET /access/users/{userid}`` answers with *expire* and *tokens*."""
answer: dict[str, Any] = {"userid": "netork@pve", "enable": 1}
if expire is not None:
answer["expire"] = expire
if tokens:
answer["tokens"] = tokens
get = api.access.users.return_value.get
get.return_value = answer
return get
def _forbidden() -> ResourceException:
return ResourceException(
403, "Forbidden", "Permission check failed (/access/groups, User.Modify|Sys.Audit)"
)
class TestApiToken:
def test_the_tokens_own_expiry(self):
drv, api = _token_driver()
get = _user(api, 0, poll={"expire": 1767225600, "privsep": 1})
assert drv.get_credential_expiry() == {
"kind": "api-token",
"name": "netork@pve!poll",
"expires": 1767225600,
}
api.access.users.assert_called_once_with("netork@pve")
get.assert_called_once_with()
def test_the_token_endpoint_is_not_asked(self):
drv, api = _token_driver()
_user(api, 0, poll={"expire": 1767225600})
drv.get_credential_expiry()
api.access.users.return_value.token.assert_not_called()
def test_a_token_without_expiry_expires_with_its_user(self):
drv, api = _token_driver()
_user(api, 1798761600, poll={"privsep": 1})
assert drv.get_credential_expiry()["expires"] == 1798761600
def test_a_token_whose_expiry_is_zero_expires_with_its_user(self):
drv, api = _token_driver()
_user(api, 1798761600, poll={"expire": 0})
assert drv.get_credential_expiry()["expires"] == 1798761600
def test_a_token_stops_working_with_its_user(self):
drv, api = _token_driver()
_user(api, 1767225600, poll={"expire": 1798761600})
assert drv.get_credential_expiry()["expires"] == 1767225600
def test_zero_on_both_never_expires(self):
drv, api = _token_driver()
_user(api, 0, poll={"expire": 0})
assert drv.get_credential_expiry()["expires"] is None
@pytest.mark.parametrize("tokens", [{}, {"other": {"expire": 1767225600}}])
def test_a_token_the_user_does_not_list_expires_with_its_user(self, tokens):
drv, api = _token_driver()
_user(api, 1798761600, **tokens)
assert drv.get_credential_expiry()["expires"] == 1798761600
def test_a_token_name_without_user_is_the_login_users(self):
drv, api = _token_driver(token_name="poll")
_user(api, 0, poll={"expire": 1767225600})
assert drv.get_credential_expiry() == {
"kind": "api-token",
"name": "netork@pve!poll",
"expires": 1767225600,
}
api.access.users.assert_called_once_with("netork@pve")
def test_a_refused_read_names_the_privilege(self):
drv, api = _token_driver()
api.access.users.return_value.get.side_effect = _forbidden()
with pytest.raises(PermissionError, match="Sys.Audit on /access/groups"):
drv.get_credential_expiry()
class TestAccount:
def test_a_password_login_reads_its_account(self):
drv, api = _driver()
_user(api, 1767225600)
assert drv.get_credential_expiry() == {
"kind": "account",
"name": "netork@pam",
"expires": 1767225600,
}
api.access.users.assert_called_once_with("netork@pam")
def test_an_account_without_expiry_never_expires(self):
drv, api = _driver()
_user(api)
assert drv.get_credential_expiry()["expires"] is None
def test_a_refused_read_names_the_privilege(self):
drv, api = _driver()
api.access.users.return_value.get.side_effect = _forbidden()
with pytest.raises(PermissionError, match="Sys.Audit on /access/groups"):
drv.get_credential_expiry()
@pytest.mark.parametrize(
"error",
[
ResourceException(500, "Internal Server Error", "cfs lock timeout"),
ConnectionError("reset"),
],
)
def test_another_failure_is_a_runtime_error(self, error):
drv, api = _driver()
api.access.users.return_value.get.side_effect = error
with pytest.raises(RuntimeError, match="netork@pam"):
drv.get_credential_expiry()