Fix console WebSocket authentication for nodes on remote computes (#3078)

* Use the compute credentials for console and VNC WebSocket forwarding

The controller forwarded the console and VNC WebSocket to a compute with
its own local server credentials instead of the credentials stored on the
compute. Any remote compute with its own credentials rejected the
connection with a 401 error, so the web console could not open for nodes
running on a remote compute.

Fixes #3076

* Do not start the console when compute WebSocket authentication fails

The VPCS and VirtualBox console WebSocket routes still called
start_websocket_console() with None after the authentication dependency
rejected the client and closed the socket. The handler crashed with
"AttributeError: 'NoneType' object has no attribute 'client'".

Add the same guard the other node types already have. See #3076
This commit is contained in:
Guobin Yue authored and GitHub committed 2026-11-14 18:58:44 +01:00
1 parent 3aaf125b88
commit fe51feec66
6 files changed
+121 -26

No files matched your search

+75
View File
@@ -996,6 +996,81 @@ class TestNodeConsoleWebSocketRoutes:
assert websocket.sent == []
assert compute_ws.closed
@staticmethod
def _capturing_ws_connect(monkeypatch, messages: List[aiohttp.WSMessage], captured: dict) -> None:
"""
Same as _forward_compute_ws but records the URL and headers passed to
the compute ws_connect() call.
"""
compute_ws = FakeComputeConsoleWebSocket(messages)
def ws_connect(url, **kwargs):
captured["url"] = url
captured["headers"] = kwargs.get("headers")
return compute_ws
monkeypatch.setattr(
HTTPClient,
"get_client",
classmethod(lambda cls: SimpleNamespace(ws_connect=ws_connect)),
)
async def test_console_ws_authenticates_with_compute_credentials(
self, compute_credentials, project: Project, controller, monkeypatch
) -> None:
"""
The console WebSocket must authenticate with the credentials stored on the
compute, not with the local server credentials: a remote compute has its own
credentials and would always reject the local ones (issue #3076).
"""
remote_compute = Compute(
"remote.example.com",
controller,
host="127.0.0.1",
port=3080,
user="remote-user",
password=SecretStr("remote-password"),
)
node = Node(project, remote_compute, "test", node_type="vpcs")
project._nodes[node.id] = node
captured: dict = {}
self._capturing_ws_connect(
monkeypatch, [aiohttp.WSMessage(aiohttp.WSMsgType.TEXT, "device output", None)], captured
)
websocket = FakeClientWebSocket()
await ws_console(websocket, current_user=MagicMock(), node=node)
# the local server credentials ("admin"/"password", set by compute_credentials) must not be used
assert captured["headers"]["Authorization"] == aiohttp.encode_basic_auth(
"remote-user", "remote-password", encoding="utf-8"
)
async def test_console_ws_without_compute_user_sends_no_auth_header(
self, compute_credentials, project: Project, controller, monkeypatch
) -> None:
"""
A compute without a user connects without an Authorization header, like any
other controller to compute request.
"""
remote_compute = Compute("remote.example.com", controller, host="127.0.0.1", port=3080)
node = Node(project, remote_compute, "test", node_type="vpcs")
project._nodes[node.id] = node
captured: dict = {}
self._capturing_ws_connect(
monkeypatch, [aiohttp.WSMessage(aiohttp.WSMsgType.TEXT, "device output", None)], captured
)
websocket = FakeClientWebSocket()
await ws_console(websocket, current_user=MagicMock(), node=node)
assert "Authorization" not in captured["headers"]
class _FakeComputeWebSocket:
"""