mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-24 14:54:33 +02:00
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:
1 parent
3aaf125b88
commit
fe51feec66
6 files changed
+121
-26
No files matched your search
@@ -14,12 +14,14 @@
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
import pytest_asyncio
|
||||
from fastapi import FastAPI, status
|
||||
from httpx import AsyncClient
|
||||
from httpx_ws import aconnect_ws
|
||||
from httpx_ws.transport import ASGIWebSocketTransport
|
||||
|
||||
from gns3server.compute.project import Project
|
||||
from tests.utils import asyncio_patch
|
||||
@@ -269,6 +271,23 @@ class TestVPCSNodesRoutes:
|
||||
assert response.status_code == status.HTTP_204_NO_CONTENT
|
||||
assert mock.called
|
||||
|
||||
async def test_vpcs_console_ws_rejects_invalid_credentials(
|
||||
self, app: FastAPI, compute_client: AsyncClient, vm: dict
|
||||
) -> None:
|
||||
# regression test: the console handler must not run after compute WebSocket
|
||||
# authentication failed (it used to crash with "AttributeError: 'NoneType'
|
||||
# object has no attribute 'client'", see issue #3076)
|
||||
ws_path = f"/v3/compute/projects/{vm['project_id']}/vpcs/nodes/{vm['node_id']}/console/ws"
|
||||
|
||||
with patch("gns3server.compute.vpcs.vpcs_vm.VPCSVM.start_websocket_console", new_callable=AsyncMock) as mock:
|
||||
async with AsyncClient(base_url="http://test-api", transport=ASGIWebSocketTransport(app=app)) as ws_client:
|
||||
async with aconnect_ws(ws_path, ws_client, auth=("wrong_user", "wrong_password")) as ws:
|
||||
json_error = await ws.receive_json()
|
||||
assert json_error["event"]["message"] == (
|
||||
"Could not authenticate while connecting to compute WebSocket: Could not validate credentials"
|
||||
)
|
||||
assert not mock.called
|
||||
|
||||
# @pytest.mark.asyncio
|
||||
# async def test_vpcs_pcap(self, app: FastAPI, compute_client: AsyncClient, vm, compute_project: Project):
|
||||
#
|
||||
|
||||
@@ -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:
|
||||
"""
|
||||
|
||||
Reference in new issue
Block a user