diff --git a/gns3server/api/routes/controller/nodes.py b/gns3server/api/routes/controller/nodes.py index 9a5ff928b..e16442f3b 100644 --- a/gns3server/api/routes/controller/nodes.py +++ b/gns3server/api/routes/controller/nodes.py @@ -866,11 +866,13 @@ async def ws_console( raise ControllerForbiddenError("Compute username is not set") user = user.strip() if user and password: - auth = aiohttp.BasicAuth(user, password.get_secret_value(), "utf-8") + auth_header = aiohttp.encode_basic_auth(user, password.get_secret_value(), encoding="utf-8") else: - auth = aiohttp.BasicAuth(user, "") + auth_header = aiohttp.encode_basic_auth(user, "", encoding="utf-8") ssl_context = Controller.instance().ssl_context() - async with HTTPClient.get_client().ws_connect(ws_console_compute_url, auth=auth, ssl_context=ssl_context) as ws: + async with HTTPClient.get_client().ws_connect( + ws_console_compute_url, headers={"Authorization": auth_header}, ssl_context=ssl_context + ) as ws: tasks = [ asyncio.ensure_future(ws_receive(ws)), asyncio.ensure_future(ws_send(ws)), @@ -964,12 +966,12 @@ async def vnc_console( raise ControllerForbiddenError("Compute username is not set") user = user.strip() if user and password: - auth = aiohttp.BasicAuth(user, password.get_secret_value(), "utf-8") + auth_header = aiohttp.encode_basic_auth(user, password.get_secret_value(), encoding="utf-8") else: - auth = aiohttp.BasicAuth(user, "") + auth_header = aiohttp.encode_basic_auth(user, "", encoding="utf-8") ssl_context = Controller.instance().ssl_context() async with HTTPClient.get_client().ws_connect( - vnc_console_compute_url, auth=auth, ssl_context=ssl_context + vnc_console_compute_url, headers={"Authorization": auth_header}, ssl_context=ssl_context ) as ws: tasks = [ asyncio.ensure_future(vnc_receive(ws)), diff --git a/gns3server/controller/compute.py b/gns3server/controller/compute.py index 71d69011a..8a9e6fe87 100644 --- a/gns3server/controller/compute.py +++ b/gns3server/controller/compute.py @@ -134,12 +134,14 @@ class Compute: if password: self._password = password try: - self._auth = aiohttp.BasicAuth(self._user, self._password.get_secret_value(), "utf-8") + self._auth = aiohttp.encode_basic_auth( + self._user, self._password.get_secret_value(), encoding="utf-8" + ) except ValueError as e: log.error(str(e)) else: self._password = None - self._auth = aiohttp.BasicAuth(self._user, "") + self._auth = aiohttp.encode_basic_auth(self._user, "", encoding="utf-8") def set_last_error(self, msg): """ @@ -353,7 +355,10 @@ class Compute: """ url = self._getUrl(f"/projects/{project.id}/files/{path}") - response = await self._session().request("GET", url, auth=self._auth) + request_options = {"auth": None} + if self._auth: + request_options["headers"] = {"Authorization": self._auth} + response = await self._session().request("GET", url, **request_options) if response.status == 404: raise ControllerNotFoundError(f"{path} not found on compute") return response @@ -368,7 +373,10 @@ class Compute: """ url = self._getUrl(f"/{image_type}/images/{image}") - response = await self._session().request("GET", url, auth=self._auth) + request_options = {"auth": None} + if self._auth: + request_options["headers"] = {"Authorization": self._auth} + response = await self._session().request("GET", url, **request_options) if response.status == 404: raise ControllerNotFoundError(f"{image} not found on compute") return response @@ -523,7 +531,10 @@ class Compute: ws_url = self._getUrl("/notifications/ws") try: - async with self._session().ws_connect(ws_url, auth=self._auth, heartbeat=10) as ws: + connection_options = {"heartbeat": 10} + if self._auth: + connection_options["headers"] = {"Authorization": self._auth} + async with self._session().ws_connect(ws_url, **connection_options) as ws: log.info(f"Connected to compute '{self._id}' WebSocket '{ws_url}'") async for response in ws: if response.type == aiohttp.WSMsgType.TEXT: @@ -598,6 +609,8 @@ class Compute: async with asynctimeout(delay=timeout): url = self._getUrl(path) headers = {"content-type": "application/json"} + if self._auth: + headers["Authorization"] = self._auth chunked = None if data == {}: data = None @@ -622,13 +635,12 @@ class Compute: else: data = json.dumps(data).encode("utf-8") try: - log.debug(f"Attempting request to compute: {method} {url} {headers}") + log.debug(f"Attempting request to compute: {method} {url}") response = await self._session().request( method, url, headers=headers, data=data, - auth=self._auth, params=params, chunked=chunked, timeout=timeout, diff --git a/gns3server/utils/http_client.py b/gns3server/utils/http_client.py index 62841e4ec..e18b1b1e5 100644 --- a/gns3server/utils/http_client.py +++ b/gns3server/utils/http_client.py @@ -59,11 +59,18 @@ class HTTPClient: basic_auth = None if user: try: - basic_auth = aiohttp.BasicAuth(user, password.get_secret_value() if password else "", "utf-8") + basic_auth = aiohttp.encode_basic_auth( + user, password.get_secret_value() if password else "", encoding="utf-8" + ) except ValueError as e: log.error(f"Basic authentication set-up error: {e}") - return client.request(method, url, auth=basic_auth, **kwargs) + headers = kwargs.pop("headers", None) + if basic_auth is not None: + headers = {key: value for key, value in (headers or {}).items() if key.lower() != "authorization"} + headers["Authorization"] = basic_auth + kwargs["headers"] = headers + return client.request(method, url, **kwargs) @classmethod def get(cls, path, **kwargs): diff --git a/tests/api/routes/controller/test_appliances.py b/tests/api/routes/controller/test_appliances.py index e2b853f5f..d74e16091 100644 --- a/tests/api/routes/controller/test_appliances.py +++ b/tests/api/routes/controller/test_appliances.py @@ -130,4 +130,4 @@ class TestApplianceRoutes: app.url_path_for("add_appliance_version", appliance_id=appliance_id), json={"name": "bad", "registry_version": 7, "images": {"hda_disk_image": "empty99G.qcow2"}}, ) - assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY + assert response.status_code == status.HTTP_422_UNPROCESSABLE_CONTENT diff --git a/tests/api/routes/controller/test_links.py b/tests/api/routes/controller/test_links.py index 14e5b9699..750bc40fb 100644 --- a/tests/api/routes/controller/test_links.py +++ b/tests/api/routes/controller/test_links.py @@ -256,7 +256,7 @@ class TestLinkRoutes: response = await client.post( app.url_path_for("create_link", project_id=project.id), json={"nodes": [bad, valid]} ) - assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY + assert response.status_code == status.HTTP_422_UNPROCESSABLE_CONTENT async def test_get_link( self, app: FastAPI, client: AsyncClient, project: Project, nodes: Tuple[Node, Node] diff --git a/tests/api/routes/controller/test_markers.py b/tests/api/routes/controller/test_markers.py index bc4fecf30..9590ac111 100644 --- a/tests/api/routes/controller/test_markers.py +++ b/tests/api/routes/controller/test_markers.py @@ -142,7 +142,7 @@ class TestMarkerRoutes: app.url_path_for("create_marker", project_id=project.id, link_id=link.id), json={"name": "bad name!", "bpf": "icmp"}, ) - assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY + assert response.status_code == status.HTTP_422_UNPROCESSABLE_CONTENT async def test_create_marker_name_too_long_rejected( self, app: FastAPI, client: AsyncClient, project: Project @@ -155,7 +155,7 @@ class TestMarkerRoutes: app.url_path_for("create_marker", project_id=project.id, link_id=link.id), json={"name": "x" * 33, "bpf": "icmp"}, # max_length is 32 ) - assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY + assert response.status_code == status.HTTP_422_UNPROCESSABLE_CONTENT async def test_get_markers(self, app: FastAPI, client: AsyncClient, project: Project) -> None: diff --git a/tests/controller/test_compute.py b/tests/controller/test_compute.py index 66fc638f9..40f09efaf 100644 --- a/tests/controller/test_compute.py +++ b/tests/controller/test_compute.py @@ -107,7 +107,6 @@ async def test_compute_httpQuery(compute): "https://example.com:84/v3/compute/projects", headers={"content-type": "application/json"}, data=b'{"a": "b"}', - auth=None, params=None, chunked=None, timeout=120, @@ -129,15 +128,13 @@ async def test_compute_httpQueryAuth(compute): mock.assert_called_with( "POST", "https://example.com:84/v3/compute/projects", - headers={"content-type": "application/json"}, + headers={"content-type": "application/json", "Authorization": aiohttp.encode_basic_auth("root", "toor")}, data=b'{"a": "b"}', - auth=compute._auth, params=None, chunked=None, timeout=120, ) - assert compute._auth.login == "root" - assert compute._auth.password == "toor" + assert compute._auth == aiohttp.encode_basic_auth("root", "toor") # @pytest.mark.asyncio @@ -200,7 +197,6 @@ async def test_compute_httpQueryNotConnectedInvalidVersion(compute): "https://example.com:84/v3/compute/capabilities", headers={"content-type": "application/json"}, data=None, - auth=None, params=None, chunked=None, timeout=120, @@ -223,7 +219,6 @@ async def test_compute_httpQueryNotConnectedNonGNS3Server(compute): "https://example.com:84/v3/compute/capabilities", headers={"content-type": "application/json"}, data=None, - auth=None, params=None, chunked=None, timeout=120, @@ -246,7 +241,6 @@ async def test_compute_httpQueryNotConnectedNonGNS3Server2(compute): "https://example.com:84/v3/compute/capabilities", headers={"content-type": "application/json"}, data=None, - auth=None, params=None, chunked=None, timeout=120, @@ -293,7 +287,6 @@ async def test_compute_httpQuery_project(compute): "https://example.com:84/v3/compute/projects", headers={"content-type": "application/json"}, data=json.dumps(project.asdict()), - auth=None, params=None, chunked=None, timeout=120, @@ -455,7 +448,6 @@ async def test_forward_get(compute): "https://example.com:84/v3/compute/qemu/images", headers={"content-type": "application/json"}, data=None, - auth=None, params=None, chunked=None, timeout=None, @@ -487,7 +479,6 @@ async def test_forward_post(compute): "https://example.com:84/v3/compute/qemu/img", headers={"content-type": "application/json"}, data=b'{"id": 42}', - auth=None, params=None, chunked=None, timeout=None, @@ -509,7 +500,6 @@ async def test_list_files(project, compute): f"https://example.com:84/v3/compute/projects/{project.id}/files", headers={"content-type": "application/json"}, data=None, - auth=None, params=None, chunked=None, timeout=None, @@ -540,7 +530,6 @@ async def test_interfaces(compute): "https://example.com:84/v3/compute/network/interfaces", headers={"content-type": "application/json"}, data=None, - auth=None, params=None, chunked=None, timeout=120, diff --git a/tests/controller/test_import_project.py b/tests/controller/test_import_project.py index bf5897bcd..3b4d50895 100644 --- a/tests/controller/test_import_project.py +++ b/tests/controller/test_import_project.py @@ -120,7 +120,7 @@ async def write_file(path, z): @pytest.fixture def export_project_with_symlink(tmpdir, controller): async def _export(symlink_target): - project = Project(controller=controller, name="test") + project = Project(controller=controller, name="test", filename="project.gns3") project.dump = MagicMock() topology = { @@ -148,6 +148,9 @@ def export_project_with_symlink(tmpdir, controller): await export_project(z, project, str(tmpdir), include_images=False) await write_file(zip_path, z) + with zipfile.ZipFile(zip_path) as archive: + assert archive.namelist().count("project.gns3") == 1 + return zip_path return _export diff --git a/tests/pytest.ini b/tests/pytest.ini index 8914391ec..a360c2bd9 100644 --- a/tests/pytest.ini +++ b/tests/pytest.ini @@ -7,3 +7,7 @@ asyncio_default_fixture_loop_scope=function markers = e2e: live end-to-end tests (real server, kernel interfaces, emulators) addopts = -m "not e2e" + +filterwarnings = + ignore:'asyncio\.iscoroutinefunction' is deprecated:DeprecationWarning:langsmith\._internal\._beta_decorator + ignore:_pytest\.python\.CallSpec2 has been renamed to CallSpec\.:pytest.PytestRemovedIn10Warning:anyio\.pytest_plugin \ No newline at end of file