| ############################################################################ | |
| # Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | |
| ############################################################################ | |
| """Test authentication of the slurmctld /readyz?verbose HTTP probe. | |
| Only root and SlurmUser may read the verbose output, authenticated with a JWT | |
| checked against SlurmctldHttpAuthParameters. The plain probes stay open. | |
| """ | |
| import pytest | |
| import requests | |
| import atf | |
| REASON = ( | |
| "Issue 50707 and 50898: /readyz?verbose authentication and" | |
| " SlurmctldHttpAuthParameters added in 26.05" | |
| ) | |
| PROBES = {"readyz": "READY", "livez": "LIVE", "healthz": "HEALTHY"} | |
| # root and SlurmUser are allowed the verbose output, so they cannot be refused. | |
| PRIVILEGED_TEST_USER = ( | |
| atf.properties["test-user-uid"] == 0 | |
| or atf.properties["test-user"] == atf.properties["slurm-user"] | |
| ) | |
| @pytest.fixture(scope="module", autouse=True) | |
| def setup(): | |
| atf.require_version((26, 5), "bin/scontrol", reason=REASON) | |
| atf.require_config_parameter_excludes("CommunicationParameters", "disable_http") | |
| atf.require_config_parameter_includes("AuthAltTypes", "auth/jwt") | |
| atf.require_config_parameter( | |
| "SlurmctldHttpAuthParameters", | |
| f"jwt_key={atf.properties['slurm-config-dir']}/jwt_hs256.key", | |
| ) | |
| atf.require_slurm_running() | |
| @pytest.mark.parametrize("endpoint", [*PROBES, ""]) | |
| def test_without_auth(endpoint): | |
| """The plain probes and the endpoint index do not require authentication.""" | |
| r = atf.request_component("sbin/slurmctld", endpoint) | |
| # Ticket 25321: probes return 200 with a status body since 26.11 | |
| if endpoint and atf.get_version("sbin/slurmctld") >= (26, 11): | |
| assert (r.status_code, r.text) == (200, PROBES[endpoint]), ( | |
| f"/{endpoint} returned {r.status_code} {r.text!r}, " | |
| f"expected 200 {PROBES[endpoint]!r}" | |
| ) | |
| else: | |
| assert r.ok, f"/{endpoint} returned {r.status_code}, expected success" | |
| def test_verbose_without_auth(): | |
| """An unauthenticated /readyz?verbose is rejected.""" | |
| r = atf.request_component("sbin/slurmctld", "readyz?verbose") | |
| assert ( | |
| r.status_code == 401 | |
| ), f"Unauthenticated /readyz?verbose returned {r.status_code}, expected 401" | |
| def _slurm_user_token_with_bad_signature(): | |
| slurm_user = atf.properties["slurm-user"] | |
| token = atf.get_jwt_token( | |
| token_user=slurm_user, fatal=True, quiet=True, user=slurm_user | |
| ) | |
| header, payload, signature = token.split(".") | |
| # The first base64url character of the signature carries only signature | |
| # bits, so replacing it always yields a different signature. | |
| bad = "B" if signature[0] == "A" else "A" | |
| return f"{header}.{payload}.{bad}{signature[1:]}" | |
| @pytest.mark.parametrize("kind", ["malformed", "bad_signature"]) | |
| def test_verbose_with_invalid_token(kind): | |
| """A token that does not verify is rejected, even naming SlurmUser.""" | |
| if kind == "malformed": | |
| token = "invalid" | |
| else: | |
| token = _slurm_user_token_with_bad_signature() | |
| r = requests.get( | |
| f"http://{atf.properties['slurmctld_host']}:" | |
| f"{atf.properties['slurmctld_port']}/readyz?verbose", | |
| headers={ | |
| "X-SLURM-USER-NAME": atf.properties["slurm-user"], | |
| "X-SLURM-USER-TOKEN": token, | |
| }, | |
| timeout=atf.default_command_timeout, | |
| ) | |
| assert ( | |
| r.status_code == 401 | |
| ), f"/readyz?verbose with a {kind} token returned {r.status_code}, expected 401" | |
| @pytest.mark.skipif( | |
| PRIVILEGED_TEST_USER, | |
| reason="Issue 50707: needs a test user other than root or SlurmUser", | |
| ) | |
| @pytest.mark.parametrize("claimed_user", [atf.properties["slurm-user"], "root"]) | |
| def test_verbose_with_other_users_token(claimed_user): | |
| """A valid token of an unprivileged user is rejected when naming another.""" | |
| user = atf.properties["test-user"] | |
| token = atf.get_jwt_token( | |
| token_user=user, fatal=True, quiet=True, user=atf.properties["slurm-user"] | |
| ) | |
| assert token, f"scontrol token returned no token for {user}" | |
| r = requests.get( | |
| f"http://{atf.properties['slurmctld_host']}:" | |
| f"{atf.properties['slurmctld_port']}/readyz?verbose", | |
| headers={"X-SLURM-USER-NAME": claimed_user, "X-SLURM-USER-TOKEN": token}, | |
| timeout=atf.default_command_timeout, | |
| ) | |
| assert r.status_code == 401, ( | |
| f"/readyz?verbose with {user}'s token as {claimed_user} returned " | |
| f"{r.status_code}, expected 401" | |
| ) | |
| @pytest.mark.skipif( | |
| PRIVILEGED_TEST_USER, | |
| reason="Issue 50707: needs a test user other than root or SlurmUser", | |
| ) | |
| def test_verbose_as_unprivileged_user(): | |
| """A user other than root and SlurmUser is refused /readyz?verbose.""" | |
| user = atf.properties["test-user"] | |
| r = atf.request_component("sbin/slurmctld", "readyz?verbose", user=user) | |
| assert ( | |
| r.status_code == 403 | |
| ), f"/readyz?verbose as {user} returned {r.status_code}, expected 403" | |
| @pytest.mark.parametrize("user", [atf.properties["slurm-user"], "root"]) | |
| def test_verbose_as_privileged_user(user): | |
| """root and SlurmUser get a non-empty verbose output.""" | |
| r = atf.request_component("sbin/slurmctld", "readyz?verbose", user=user) | |
| assert ( | |
| r.status_code == 200 | |
| ), f"/readyz?verbose as {user} returned {r.status_code}, expected 200" | |
| assert r.text.strip(), f"/readyz?verbose as {user} returned an empty body" |