From 1e371016413dc1512f2549c32e0130b8e298dd1b Mon Sep 17 00:00:00 2001 From: grossmj Date: Fri, 23 Jun 2017 12:00:33 +0200 Subject: [PATCH 01/17] Allow IOU 64-bit images. --- gns3server/compute/iou/iou_vm.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gns3server/compute/iou/iou_vm.py b/gns3server/compute/iou/iou_vm.py index 4768614f..3f51ad6a 100644 --- a/gns3server/compute/iou/iou_vm.py +++ b/gns3server/compute/iou/iou_vm.py @@ -181,9 +181,9 @@ class IOUVM(BaseNode): except OSError as e: raise IOUError("Cannot read ELF header for IOU image '{}': {}".format(self._path, e)) - # IOU images must start with the ELF magic number, be 32-bit, little endian + # IOU images must start with the ELF magic number, be 32-bit or 64-bit, little endian # and have an ELF version of 1 normal IOS image are big endian! - if elf_header_start != b'\x7fELF\x01\x01\x01': + if elf_header_start != b'\x7fELF\x01\x01\x01' and elf_header_start != b'\x7fELF\x02\x01\x01': raise IOUError("'{}' is not a valid IOU image".format(self._path)) if not os.access(self._path, os.X_OK): From c6f9ec37586e77fee77d9d0be44b8bf9fcaf7378 Mon Sep 17 00:00:00 2001 From: ziajka Date: Wed, 5 Jul 2017 10:36:58 +0200 Subject: [PATCH 02/17] More information on Docker WebSocket error --- gns3server/compute/docker/docker_vm.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/gns3server/compute/docker/docker_vm.py b/gns3server/compute/docker/docker_vm.py index f384d5aa..5473074a 100644 --- a/gns3server/compute/docker/docker_vm.py +++ b/gns3server/compute/docker/docker_vm.py @@ -502,6 +502,8 @@ class DockerVM(BaseNode): msg = yield from ws.receive() if msg.tp == aiohttp.MsgType.text: out.feed_data(msg.data.encode()) + elif msg.tp == aiohttp.MsgType.error: + log.critical("Docker WebSocket Error: {}".format(msg.data)) else: out.feed_eof() ws.close() From 8e8b8bc5a59b785b0a1a325bc10b24e99451c568 Mon Sep 17 00:00:00 2001 From: ziajka Date: Thu, 6 Jul 2017 10:13:00 +0200 Subject: [PATCH 03/17] WebSocket binary mode support for docker --- gns3server/compute/docker/docker_vm.py | 6 ++++-- tests/compute/docker/test_docker_vm.py | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/gns3server/compute/docker/docker_vm.py b/gns3server/compute/docker/docker_vm.py index 5473074a..257fc062 100644 --- a/gns3server/compute/docker/docker_vm.py +++ b/gns3server/compute/docker/docker_vm.py @@ -500,9 +500,11 @@ class DockerVM(BaseNode): while True: msg = yield from ws.receive() - if msg.tp == aiohttp.MsgType.text: + if msg.tp == aiohttp.MsgType.TEXT: out.feed_data(msg.data.encode()) - elif msg.tp == aiohttp.MsgType.error: + if msg.tp == aiohttp.MsgType.BINARY: + out.feed_data(msg.data) + elif msg.tp == aiohttp.MsgType.ERROR: log.critical("Docker WebSocket Error: {}".format(msg.data)) else: out.feed_eof() diff --git a/tests/compute/docker/test_docker_vm.py b/tests/compute/docker/test_docker_vm.py index 6ca6fd14..b638e542 100644 --- a/tests/compute/docker/test_docker_vm.py +++ b/tests/compute/docker/test_docker_vm.py @@ -20,6 +20,7 @@ import pytest import uuid import sys import os +from aiohttp._ws_impl import WSMsgType from tests.utils import asyncio_patch, AsyncioMagicMock from gns3server.ubridge.ubridge_error import UbridgeNamespaceError @@ -904,3 +905,27 @@ def test_fix_permission(vm, loop): loop.run_until_complete(vm._fix_permissions()) mock_exec.assert_called_with('docker', 'exec', 'e90e34656842', '/gns3/bin/busybox', 'sh', '-c', '(/gns3/bin/busybox find "/etc" -depth -print0 | /gns3/bin/busybox xargs -0 /gns3/bin/busybox stat -c \'%a:%u:%g:%n\' > "/etc/.gns3_perms") && /gns3/bin/busybox chmod -R u+rX "/etc" && /gns3/bin/busybox chown {}:{} -R "/etc"'.format(os.getuid(), os.getgid())) assert process.wait.called + + +def test_read_console_output_with_binary_mode(vm, loop): + class InputStreamMock(object): + def __init__(self): + self.sent = False + + @asyncio.coroutine + def receive(self): + if not self.sent: + self.sent = True + return MagicMock(tp=WSMsgType.BINARY, data=b"test") + else: + return MagicMock(tp=WSMsgType.CLOSE) + + def close(self): + pass + + input_stream = InputStreamMock() + output_stream = MagicMock() + + with asyncio_patch('gns3server.compute.docker.docker_vm.DockerVM.stop'): + loop.run_until_complete(asyncio.async(vm._read_console_output(input_stream, output_stream))) + output_stream.feed_data.assert_called_once_with(b"test") From e0f0adf3c878f30c00dd55aef19d773039ef1dca Mon Sep 17 00:00:00 2001 From: ziajka Date: Thu, 6 Jul 2017 11:24:55 +0200 Subject: [PATCH 04/17] Added preferred Docker API version. Fixes #2136 --- gns3server/compute/docker/__init__.py | 17 +++++++++-- tests/compute/docker/test_docker.py | 41 +++++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/gns3server/compute/docker/__init__.py b/gns3server/compute/docker/__init__.py index 18ad331f..b85f473a 100644 --- a/gns3server/compute/docker/__init__.py +++ b/gns3server/compute/docker/__init__.py @@ -36,6 +36,7 @@ log = logging.getLogger(__name__) # Be carefull to keep it consistent DOCKER_MINIMUM_API_VERSION = "1.25" DOCKER_MINIMUM_VERSION = "1.13" +DOCKER_PREFERRED_API_VERSION = "1.30" class Docker(BaseManager): @@ -50,6 +51,7 @@ class Docker(BaseManager): self.ubridge_lock = asyncio.Lock() self._connector = None self._session = None + self._api_version = DOCKER_MINIMUM_API_VERSION @asyncio.coroutine def _check_connection(self): @@ -61,8 +63,17 @@ class Docker(BaseManager): except (aiohttp.errors.ClientOSError, FileNotFoundError): self._connected = False raise DockerError("Can't connect to docker daemon") - if parse_version(version["ApiVersion"]) < parse_version(DOCKER_MINIMUM_API_VERSION): - raise DockerError("Docker version is {}. GNS3 requires a minimum version of {}".format(version["Version"], DOCKER_MINIMUM_VERSION)) + + docker_version = parse_version(version['ApiVersion']) + + if docker_version < parse_version(DOCKER_MINIMUM_API_VERSION): + raise DockerError( + "Docker version is {}. GNS3 requires a minimum version of {}".format( + version["Version"], DOCKER_MINIMUM_VERSION)) + + preferred_api_version = parse_version(DOCKER_PREFERRED_API_VERSION) + if docker_version >= preferred_api_version: + self._api_version = DOCKER_PREFERRED_API_VERSION def connector(self): if self._connector is None or self._connector.closed: @@ -165,7 +176,7 @@ class Docker(BaseManager): :returns: Websocket """ - url = "http://docker/v" + DOCKER_MINIMUM_API_VERSION + "/" + path + url = "http://docker/v" + self._api_version + "/" + path connection = yield from aiohttp.ws_connect(url, connector=self.connector(), origin="http://docker", diff --git a/tests/compute/docker/test_docker.py b/tests/compute/docker/test_docker.py index 095a25de..db43c84c 100644 --- a/tests/compute/docker/test_docker.py +++ b/tests/compute/docker/test_docker.py @@ -17,10 +17,10 @@ import pytest import asyncio -from unittest.mock import MagicMock +from unittest.mock import MagicMock, patch from tests.utils import asyncio_patch, AsyncioMagicMock -from gns3server.compute.docker import Docker +from gns3server.compute.docker import Docker, DOCKER_PREFERRED_API_VERSION, DOCKER_MINIMUM_API_VERSION from gns3server.compute.docker.docker_error import DockerError, DockerHttp404Error @@ -162,3 +162,40 @@ def test_pull_image(loop): with asyncio_patch("gns3server.compute.docker.Docker.http_query", return_value=mock_query) as mock: images = loop.run_until_complete(asyncio.async(Docker.instance().pull_image("ubuntu"))) mock.assert_called_with("POST", "images/create", params={"fromImage": "ubuntu"}, timeout=None) + + +def test_docker_check_connection_docker_minimum_version(vm, loop): + response = { + 'ApiVersion': '1.01', + 'Version': '1.12' + } + + with patch("gns3server.compute.docker.Docker.connector"), \ + asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response): + vm._connected = False + with pytest.raises(DockerError): + loop.run_until_complete(asyncio.async(vm._check_connection())) + + +def test_docker_check_connection_docker_preferred_version_against_newer(vm, loop): + response = { + 'ApiVersion': '1.31' + } + + with patch("gns3server.compute.docker.Docker.connector"), \ + asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response): + vm._connected = False + loop.run_until_complete(asyncio.async(vm._check_connection())) + assert vm._api_version == DOCKER_PREFERRED_API_VERSION + + +def test_docker_check_connection_docker_preferred_version_against_older(vm, loop): + response = { + 'ApiVersion': '1.27', + } + + with patch("gns3server.compute.docker.Docker.connector"), \ + asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response): + vm._connected = False + loop.run_until_complete(asyncio.async(vm._check_connection())) + assert vm._api_version == DOCKER_MINIMUM_API_VERSION \ No newline at end of file From 8eb060304ad4d302cf1a764b6b4b632a93a16326 Mon Sep 17 00:00:00 2001 From: grossmj Date: Tue, 11 Jul 2017 20:03:24 +0700 Subject: [PATCH 05/17] Correct typos in schemas. --- gns3server/schemas/iou.py | 2 +- gns3server/schemas/vpcs.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/gns3server/schemas/iou.py b/gns3server/schemas/iou.py index b8a68565..656eb524 100644 --- a/gns3server/schemas/iou.py +++ b/gns3server/schemas/iou.py @@ -184,7 +184,7 @@ IOU_OBJECT_SCHEMA = { "type": ["boolean", "null"] }, "command_line": { - "description": "Last command line used by GNS3 to start QEMU", + "description": "Last command line used by GNS3 to start IOU", "type": "string" }, "application_id": { diff --git a/gns3server/schemas/vpcs.py b/gns3server/schemas/vpcs.py index adbd2043..4e05dd9d 100644 --- a/gns3server/schemas/vpcs.py +++ b/gns3server/schemas/vpcs.py @@ -122,7 +122,7 @@ VPCS_OBJECT_SCHEMA = { "pattern": "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$" }, "command_line": { - "description": "Last command line used by GNS3 to start QEMU", + "description": "Last command line used by GNS3 to start VPCS", "type": "string" } }, From 450c089b6d3d3a7fad435d9c16dc250049869be0 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Tue, 11 Jul 2017 15:28:01 +0200 Subject: [PATCH 06/17] Test if a snapshot name already exists This fix random test failure when testing snapshots. It seem under high load sometimes the previous snapshot folder was not visible on disk. Perhaps a test isolation issue but I don't see how. But in any case it's better to test if the name is not already use. Fix #1118 --- gns3server/controller/project.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/gns3server/controller/project.py b/gns3server/controller/project.py index c185cccd..93e8f31c 100644 --- a/gns3server/controller/project.py +++ b/gns3server/controller/project.py @@ -510,6 +510,9 @@ class Project: :param name: Name of the snapshot """ + if name in [snap.name for snap in self.snapshots.values()]: + raise aiohttp.web_exceptions.HTTPConflict(text="The snapshot {} already exist".format(name)) + snapshot = Snapshot(self, name=name) try: if os.path.exists(snapshot.path): From 357de50a1be80ef36eaaae0cd11980bba68ce99e Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Tue, 11 Jul 2017 15:59:09 +0200 Subject: [PATCH 07/17] Upgrade to last aiohttp version --- gns3server/web/web_server.py | 2 +- requirements.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/gns3server/web/web_server.py b/gns3server/web/web_server.py index be24f2ab..b0ff599c 100644 --- a/gns3server/web/web_server.py +++ b/gns3server/web/web_server.py @@ -43,7 +43,7 @@ import gns3server.handlers import logging log = logging.getLogger(__name__) -if not aiohttp.__version__.startswith("2.0"): +if not aiohttp.__version__.startswith("2.2"): raise RuntimeError("You need aiohttp 2.0 for running GNS3") diff --git a/requirements.txt b/requirements.txt index bcfc7289..f1aecf58 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ jsonschema>=2.4.0 -aiohttp>=2.0.7,<2.1.0 # pyup: ignore +aiohttp>=2.2.0,<2.3.0 # pyup: ignore aiohttp-cors>=0.5.3,<0.6.0 # pyup: ignore yarl>=0.10.2,<0.11 # pyup: ignore Jinja2>=2.7.3 From 8a8de1e2df35bf4dd8732d09957e573390187684 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Tue, 11 Jul 2017 17:30:29 +0200 Subject: [PATCH 08/17] BPF filter support Fix https://github.com/GNS3/gns3-gui/issues/765 --- gns3server/compute/base_node.py | 25 +++++++++++++++++------- gns3server/controller/link.py | 32 ++++++++++++++++++++++++++----- tests/compute/test_base_node.py | 14 +++++++++++++- tests/controller/test_link.py | 7 ++++--- tests/controller/test_udp_link.py | 7 +++++-- 5 files changed, 67 insertions(+), 18 deletions(-) diff --git a/gns3server/compute/base_node.py b/gns3server/compute/base_node.py index c80d5bea..b4dbc216 100644 --- a/gns3server/compute/base_node.py +++ b/gns3server/compute/base_node.py @@ -605,13 +605,24 @@ class BaseNode: yield from self._ubridge_send('bridge reset_packet_filters ' + bridge_name) i = 0 for (filter_type, values) in filters.items(): - cmd = "bridge add_packet_filter {bridge_name} {filter_name} {filter_type} {filter_value}".format( - bridge_name=bridge_name, - filter_name="filter" + str(i), - filter_type=filter_type, - filter_value=" ".join([str(v) for v in values])) - yield from self._ubridge_send(cmd) - i += 1 + if isinstance(values[0], str): + for line in values[0].split('\n'): + line = line.strip() + cmd = "bridge add_packet_filter {bridge_name} {filter_name} {filter_type} {filter_value}".format( + bridge_name=bridge_name, + filter_name="filter" + str(i), + filter_type=filter_type, + filter_value='"{}" {}'.format(line, " ".join([str(v) for v in values[1:]]))).strip() + yield from self._ubridge_send(cmd) + i += 1 + else: + cmd = "bridge add_packet_filter {bridge_name} {filter_name} {filter_type} {filter_value}".format( + bridge_name=bridge_name, + filter_name="filter" + str(i), + filter_type=filter_type, + filter_value=" ".join([str(v) for v in values])) + yield from self._ubridge_send(cmd) + i += 1 @asyncio.coroutine def _add_ubridge_ethernet_connection(self, bridge_name, ethernet_interface, block_host_traffic=True): diff --git a/gns3server/controller/link.py b/gns3server/controller/link.py index 9e202bfb..8ad81fd0 100644 --- a/gns3server/controller/link.py +++ b/gns3server/controller/link.py @@ -36,6 +36,7 @@ FILTERS = [ "name": "Frequency", "minimum": -1, "maximum": 32767, + "type": "int", "unit": "th packet" } ] @@ -49,6 +50,7 @@ FILTERS = [ "name": "Chance", "minimum": 0, "maximum": 100, + "type": "int", "unit": "%" } ] @@ -62,13 +64,15 @@ FILTERS = [ "name": "Latency", "minimum": 0, "maximum": 32767, - "unit": "ms" + "unit": "ms", + "type": "int" }, { "name": "Jitter (-/+)", "minimum": 0, "maximum": 32767, - "unit": "ms" + "unit": "ms", + "type": "int" } ] }, @@ -81,7 +85,19 @@ FILTERS = [ "name": "Chance", "minimum": 0, "maximum": 100, - "unit": "%" + "unit": "%", + "type": "int" + } + ] + }, + { + "type": "bpf", + "name": "BPF", + "description": "Berkeley Packet Filter (BPF) syntax. This filter will drop any packet matching the expression. Put one filter by line", + "parameters": [ + { + "name": "BPF filters", + "type": "text" } ] } @@ -124,8 +140,14 @@ class Link: """ new_filters = {} for (filter, values) in filters.items(): - values = [int(v) for v in values] - if len(values) != 0 and values[0] != 0: + new_values = [] + for value in values: + if isinstance(value, str): + new_values.append(value.strip("\n ")) + else: + new_values.append(int(value)) + values = new_values + if len(values) != 0 and values[0] != 0 and values[0] != '': new_filters[filter] = values if new_filters != self.filters: diff --git a/tests/compute/test_base_node.py b/tests/compute/test_base_node.py index e42f3e98..e942c698 100644 --- a/tests/compute/test_base_node.py +++ b/tests/compute/test_base_node.py @@ -139,9 +139,21 @@ def test_update_ubridge_udp_connection(node, async_run): def test_ubridge_apply_filters(node, async_run): filters = { - "latency": [10] + "latency": [10], + "bpf": ["icmp[icmptype] == 8\ntcp src port 53"] } node._ubridge_send = AsyncioMagicMock() async_run(node._ubridge_apply_filters("VPCS-10", filters)) node._ubridge_send.assert_any_call("bridge reset_packet_filters VPCS-10") node._ubridge_send.assert_any_call("bridge add_packet_filter VPCS-10 filter0 latency 10") + + +def test_ubridge_apply_bpf_filters(node, async_run): + filters = { + "bpf": ["icmp[icmptype] == 8\ntcp src port 53"] + } + node._ubridge_send = AsyncioMagicMock() + async_run(node._ubridge_apply_filters("VPCS-10", filters)) + node._ubridge_send.assert_any_call("bridge reset_packet_filters VPCS-10") + node._ubridge_send.assert_any_call("bridge add_packet_filter VPCS-10 filter0 bpf \"icmp[icmptype] == 8\"") + node._ubridge_send.assert_any_call("bridge add_packet_filter VPCS-10 filter1 bpf \"tcp src port 53\"") diff --git a/tests/controller/test_link.py b/tests/controller/test_link.py index 24638fdb..42015b08 100644 --- a/tests/controller/test_link.py +++ b/tests/controller/test_link.py @@ -369,9 +369,10 @@ def test_update_filters(async_run, project, compute): link.update = AsyncioMagicMock() assert link._created async_run(link.update_filters({ - "packet_loss": ["10"], - "delay": ["50", "10"], - "frequency_drop": ["0"] + "packet_loss": [10], + "delay": [50, 10], + "frequency_drop": [0], + "bpf": [" \n "] })) assert link.filters == { "packet_loss": [10], diff --git a/tests/controller/test_udp_link.py b/tests/controller/test_udp_link.py index 3ab6581f..5c738b4b 100644 --- a/tests/controller/test_udp_link.py +++ b/tests/controller/test_udp_link.py @@ -373,11 +373,14 @@ def test_update(async_run, project): }, timeout=120) assert link.created - async_run(link.update_filters({"drop": [5]})) + async_run(link.update_filters({"drop": [5], "bpf": ["icmp[icmptype] == 8"]})) compute1.put.assert_any_call("/projects/{}/vpcs/nodes/{}/adapters/0/ports/4/nio".format(project.id, node1.id), data={ "lport": 1024, "rhost": "192.168.1.2", "rport": 2048, "type": "nio_udp", - "filters": {"drop": [5]} + "filters": { + "drop": [5], + "bpf": ["icmp[icmptype] == 8"] + } }, timeout=120) From fc87871d46800bca77e16bb1ee0a8defe183869d Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Tue, 11 Jul 2017 19:00:29 +0200 Subject: [PATCH 09/17] Fix a bug in docker binary message handling --- gns3server/compute/docker/docker_vm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gns3server/compute/docker/docker_vm.py b/gns3server/compute/docker/docker_vm.py index 257fc062..7dfa4136 100644 --- a/gns3server/compute/docker/docker_vm.py +++ b/gns3server/compute/docker/docker_vm.py @@ -502,7 +502,7 @@ class DockerVM(BaseNode): msg = yield from ws.receive() if msg.tp == aiohttp.MsgType.TEXT: out.feed_data(msg.data.encode()) - if msg.tp == aiohttp.MsgType.BINARY: + elif msg.tp == aiohttp.MsgType.BINARY: out.feed_data(msg.data) elif msg.tp == aiohttp.MsgType.ERROR: log.critical("Docker WebSocket Error: {}".format(msg.data)) From c8155aa14f4dd3f5656d76d0fe734688a24e7f7b Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Wed, 12 Jul 2017 08:27:49 +0200 Subject: [PATCH 10/17] Upgrade Yarl --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index f1aecf58..66ae7e03 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,7 @@ jsonschema>=2.4.0 aiohttp>=2.2.0,<2.3.0 # pyup: ignore aiohttp-cors>=0.5.3,<0.6.0 # pyup: ignore -yarl>=0.10.2,<0.11 # pyup: ignore +yarl>=0.11,<0.12 # pyup: ignore Jinja2>=2.7.3 raven>=5.23.0 psutil>=3.0.0 From 5747aba83cc153dc3facac9006938f888ace685d Mon Sep 17 00:00:00 2001 From: grossmj Date: Wed, 12 Jul 2017 13:34:18 +0700 Subject: [PATCH 11/17] Update error message to require aiohttp 2.2 --- gns3server/web/web_server.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gns3server/web/web_server.py b/gns3server/web/web_server.py index b0ff599c..8a2f2ff9 100644 --- a/gns3server/web/web_server.py +++ b/gns3server/web/web_server.py @@ -44,7 +44,7 @@ import logging log = logging.getLogger(__name__) if not aiohttp.__version__.startswith("2.2"): - raise RuntimeError("You need aiohttp 2.0 for running GNS3") + raise RuntimeError("aiohttp 2.0 is required to run the GNS3 server") class WebServer: From fd0770158cca6c73c73a4adc6d749c46cfc51860 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Wed, 12 Jul 2017 09:34:09 +0200 Subject: [PATCH 12/17] Fix tests with Yarl 0.11 --- tests/handlers/api/compute/test_project.py | 6 +++--- tests/handlers/api/compute/test_qemu.py | 2 +- tests/handlers/api/controller/test_node.py | 4 ++-- tests/handlers/api/controller/test_project.py | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/handlers/api/compute/test_project.py b/tests/handlers/api/compute/test_project.py index 25d3d8c2..a1e1cd42 100644 --- a/tests/handlers/api/compute/test_project.py +++ b/tests/handlers/api/compute/test_project.py @@ -136,7 +136,7 @@ def test_get_file(http_compute, tmpdir): assert response.status == 404 response = http_compute.get("/projects/{project_id}/files/../hello".format(project_id=project.id), raw=True) - assert response.status == 403 + assert response.status == 404 def test_write_file(http_compute, tmpdir): @@ -151,7 +151,7 @@ def test_write_file(http_compute, tmpdir): assert f.read() == "world" response = http_compute.post("/projects/{project_id}/files/../hello".format(project_id=project.id), raw=True) - assert response.status == 403 + assert response.status == 404 def test_stream_file(http_compute, tmpdir): @@ -170,4 +170,4 @@ def test_stream_file(http_compute, tmpdir): assert response.status == 404 response = http_compute.get("/projects/{project_id}/files/../hello".format(project_id=project.id), raw=True) - assert response.status == 403 + assert response.status == 404 diff --git a/tests/handlers/api/compute/test_qemu.py b/tests/handlers/api/compute/test_qemu.py index 4e049d4f..41dd385a 100644 --- a/tests/handlers/api/compute/test_qemu.py +++ b/tests/handlers/api/compute/test_qemu.py @@ -258,7 +258,7 @@ def test_upload_image_ova(http_compute, tmpdir): def test_upload_image_forbiden_location(http_compute, tmpdir): with patch("gns3server.compute.Qemu.get_images_directory", return_value=str(tmpdir),): response = http_compute.post("/qemu/images/../../test2", body="TEST", raw=True) - assert response.status == 403 + assert response.status == 404 def test_upload_image_permission_denied(http_compute, tmpdir): diff --git a/tests/handlers/api/controller/test_node.py b/tests/handlers/api/controller/test_node.py index eb795c7c..635c1e93 100644 --- a/tests/handlers/api/controller/test_node.py +++ b/tests/handlers/api/controller/test_node.py @@ -234,7 +234,7 @@ def test_get_file(http_controller, tmpdir, project, node, compute): compute.http_query.assert_called_with("GET", "/projects/{project_id}/files/project-files/vpcs/{node_id}/hello".format(project_id=project.id, node_id=node.id), timeout=None, raw=True) response = http_controller.get("/projects/{project_id}/nodes/{node_id}/files/../hello".format(project_id=project.id, node_id=node.id), raw=True) - assert response.status == 403 + assert response.status == 404 def test_post_file(http_controller, tmpdir, project, node, compute): @@ -245,4 +245,4 @@ def test_post_file(http_controller, tmpdir, project, node, compute): compute.http_query.assert_called_with("POST", "/projects/{project_id}/files/project-files/vpcs/{node_id}/hello".format(project_id=project.id, node_id=node.id), data=b'hello', timeout=None, raw=True) response = http_controller.get("/projects/{project_id}/nodes/{node_id}/files/../hello".format(project_id=project.id, node_id=node.id), raw=True) - assert response.status == 403 + assert response.status == 404 diff --git a/tests/handlers/api/controller/test_project.py b/tests/handlers/api/controller/test_project.py index 6290ed2c..7381bd5d 100644 --- a/tests/handlers/api/controller/test_project.py +++ b/tests/handlers/api/controller/test_project.py @@ -203,7 +203,7 @@ def test_get_file(http_controller, tmpdir, loop, project): assert response.status == 404 response = http_controller.get("/projects/{project_id}/files/../hello".format(project_id=project.id), raw=True) - assert response.status == 403 + assert response.status == 404 def test_write_file(http_controller, tmpdir, project): @@ -214,7 +214,7 @@ def test_write_file(http_controller, tmpdir, project): assert f.read() == "world" response = http_controller.post("/projects/{project_id}/files/../hello".format(project_id=project.id), raw=True) - assert response.status == 403 + assert response.status == 404 def test_write_and_get_file_with_leading_slashes_in_filename(http_controller, tmpdir, loop, project): From 7c6ffb3f49bbd75e7e509bbfa56da55dec128182 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Wed, 12 Jul 2017 10:57:03 +0200 Subject: [PATCH 13/17] Update error message when invalid aiohttp --- gns3server/web/web_server.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gns3server/web/web_server.py b/gns3server/web/web_server.py index 8a2f2ff9..9e17f0ba 100644 --- a/gns3server/web/web_server.py +++ b/gns3server/web/web_server.py @@ -44,7 +44,7 @@ import logging log = logging.getLogger(__name__) if not aiohttp.__version__.startswith("2.2"): - raise RuntimeError("aiohttp 2.0 is required to run the GNS3 server") + raise RuntimeError("aiohttp 2.2 is required to run the GNS3 server") class WebServer: @@ -123,7 +123,7 @@ class WebServer: task.cancel() try: yield from asyncio.wait_for(task, 1) - except: + except BaseException: pass self._loop.stop() From b37f9eb4f08c69c83325b1d3b4017507675e2912 Mon Sep 17 00:00:00 2001 From: grossmj Date: Wed, 12 Jul 2017 16:21:11 +0700 Subject: [PATCH 14/17] Update schema for BPF filter. --- gns3server/controller/link.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gns3server/controller/link.py b/gns3server/controller/link.py index 8ad81fd0..78bc033c 100644 --- a/gns3server/controller/link.py +++ b/gns3server/controller/link.py @@ -92,11 +92,11 @@ FILTERS = [ }, { "type": "bpf", - "name": "BPF", - "description": "Berkeley Packet Filter (BPF) syntax. This filter will drop any packet matching the expression. Put one filter by line", + "name": "Berkeley Packet Filter (BPF)", + "description": "This filter will drop any packet matching a BPF expression. Put one expression per line", "parameters": [ { - "name": "BPF filters", + "name": "Filters", "type": "text" } ] From cf17176bfc0806c8b331ded8d3dcbc6e86e2b7d7 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Wed, 12 Jul 2017 11:33:32 +0200 Subject: [PATCH 15/17] Fix recreate a link with dynamips Fix #1122 --- gns3server/compute/dynamips/nios/nio_udp.py | 2 ++ gns3server/compute/dynamips/nodes/router.py | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/gns3server/compute/dynamips/nios/nio_udp.py b/gns3server/compute/dynamips/nios/nio_udp.py index 106e3836..522b3c5a 100644 --- a/gns3server/compute/dynamips/nios/nio_udp.py +++ b/gns3server/compute/dynamips/nios/nio_udp.py @@ -92,6 +92,8 @@ class NIOUDP(NIO): @asyncio.coroutine def close(self): if self._local_tunnel_lport: + name = 'DYNAMIPS-{}-{}'.format(self._local_tunnel_lport, self._local_tunnel_rport) + yield from self._node.ubridge_delete_bridge(name) self._node.manager.port_manager.release_udp_port(self._local_tunnel_lport, self ._node.project) if self._local_tunnel_rport: self._node.manager.port_manager.release_udp_port(self._local_tunnel_rport, self._node.project) diff --git a/gns3server/compute/dynamips/nodes/router.py b/gns3server/compute/dynamips/nodes/router.py index f0a87a1b..e9a0fa47 100644 --- a/gns3server/compute/dynamips/nodes/router.py +++ b/gns3server/compute/dynamips/nodes/router.py @@ -1281,6 +1281,17 @@ class Router(BaseNode): yield from self.slot_enable_nio(slot_number, port_number) adapter.add_nio(port_number, nio) + @asyncio.coroutine + def slot_update_nio_binding(self, slot_number, port_number, nio): + """ + Update a slot NIO binding. + + :param slot_number: slot number + :param port_number: port number + :param nio: NIO instance to add to the slot/port + """ + pass + @asyncio.coroutine def slot_remove_nio_binding(self, slot_number, port_number): """ @@ -1313,8 +1324,7 @@ class Router(BaseNode): nio = adapter.get_nio(port_number) if nio is None: return - if isinstance(nio, NIOUDP): - self.manager.port_manager.release_udp_port(nio.lport, self._project) + yield from nio.close() adapter.remove_nio(port_number) log.info('Router "{name}" [{id}]: NIO {nio_name} removed from port {slot_number}/{port_number}'.format(name=self._name, From 4a55a367e3beea393296c038143e186e3af905fc Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Wed, 12 Jul 2017 11:42:37 +0200 Subject: [PATCH 16/17] Missing file in previous commit --- gns3server/compute/base_node.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/gns3server/compute/base_node.py b/gns3server/compute/base_node.py index b4dbc216..14c6b42d 100644 --- a/gns3server/compute/base_node.py +++ b/gns3server/compute/base_node.py @@ -590,6 +590,14 @@ class BaseNode: yield from self._ubridge_send('bridge start {name}'.format(name=bridge_name)) yield from self._ubridge_apply_filters(bridge_name, destination_nio.filters) + @asyncio.coroutine + def ubridge_delete_bridge(self, name): + """ + :params name: Delete the bridge with this name + """ + if self.ubridge: + yield from self._ubridge_send("bridge delete {name}".format(name=name)) + @asyncio.coroutine def _update_ubridge_udp_connection(self, bridge_name, source_nio, destination_nio): yield from self._ubridge_apply_filters(bridge_name, destination_nio.filters) From 86f12012768d4cc2788b344b7046b6282a9791a2 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Wed, 12 Jul 2017 12:08:02 +0200 Subject: [PATCH 17/17] Support packet filtering for dynamips Fix #1109 --- gns3server/compute/base_node.py | 2 +- gns3server/compute/dynamips/nios/nio_udp.py | 42 +++++++++++++------ gns3server/compute/dynamips/nodes/router.py | 2 +- gns3server/compute/vpcs/vpcs_vm.py | 2 +- gns3server/controller/link.py | 2 +- .../api/compute/dynamips_vm_handler.py | 29 +++++++++++++ tests/compute/test_base_node.py | 2 +- 7 files changed, 64 insertions(+), 17 deletions(-) diff --git a/gns3server/compute/base_node.py b/gns3server/compute/base_node.py index 14c6b42d..56444f43 100644 --- a/gns3server/compute/base_node.py +++ b/gns3server/compute/base_node.py @@ -599,7 +599,7 @@ class BaseNode: yield from self._ubridge_send("bridge delete {name}".format(name=name)) @asyncio.coroutine - def _update_ubridge_udp_connection(self, bridge_name, source_nio, destination_nio): + def update_ubridge_udp_connection(self, bridge_name, source_nio, destination_nio): yield from self._ubridge_apply_filters(bridge_name, destination_nio.filters) @asyncio.coroutine diff --git a/gns3server/compute/dynamips/nios/nio_udp.py b/gns3server/compute/dynamips/nios/nio_udp.py index 522b3c5a..60ddafa0 100644 --- a/gns3server/compute/dynamips/nios/nio_udp.py +++ b/gns3server/compute/dynamips/nios/nio_udp.py @@ -54,6 +54,14 @@ class NIOUDP(NIO): self._node = node super().__init__(name, node.hypervisor) + @property + def filters(self): + return self._filters + + @filters.setter + def filters(self, val): + self._filters = val + @asyncio.coroutine def create(self): if not self._hypervisor: @@ -67,7 +75,7 @@ class NIOUDP(NIO): return self._local_tunnel_lport = self._node.manager.port_manager.get_free_udp_port(self._node.project) self._local_tunnel_rport = self._node.manager.port_manager.get_free_udp_port(self._node.project) - name = 'DYNAMIPS-{}-{}'.format(self._local_tunnel_lport, self._local_tunnel_rport) + self._bridge_name = 'DYNAMIPS-{}-{}'.format(self._local_tunnel_lport, self._local_tunnel_rport) yield from self._hypervisor.send("nio create_udp {name} {lport} {rhost} {rport}".format(name=self._name, lport=self._local_tunnel_lport, rhost='127.0.0.1', @@ -77,23 +85,33 @@ class NIOUDP(NIO): lport=self._lport, rhost=self._rhost, rport=self._rport)) + + self._source_nio = nio_udp.NIOUDP(self._local_tunnel_rport, + '127.0.0.1', + self._local_tunnel_lport, + {}) + self._destination_nio = nio_udp.NIOUDP(self._lport, + self._rhost, + self._rport, + self._filters) yield from self._node.add_ubridge_udp_connection( - name, - nio_udp.NIOUDP(self._local_tunnel_rport, - '127.0.0.1', - self._local_tunnel_lport, - self._filters), - nio_udp.NIOUDP(self._lport, - self._rhost, - self._rport, - self._filters) + self._bridge_name, + self._source_nio, + self._destination_nio ) + @asyncio.coroutine + def update(self): + self._destination_nio.filters = self._filters + yield from self._node.update_ubridge_udp_connection( + self._bridge_name, + self._source_nio, + self._destination_nio) + @asyncio.coroutine def close(self): if self._local_tunnel_lport: - name = 'DYNAMIPS-{}-{}'.format(self._local_tunnel_lport, self._local_tunnel_rport) - yield from self._node.ubridge_delete_bridge(name) + yield from self._node.ubridge_delete_bridge(self._bridge_name) self._node.manager.port_manager.release_udp_port(self._local_tunnel_lport, self ._node.project) if self._local_tunnel_rport: self._node.manager.port_manager.release_udp_port(self._local_tunnel_rport, self._node.project) diff --git a/gns3server/compute/dynamips/nodes/router.py b/gns3server/compute/dynamips/nodes/router.py index e9a0fa47..7ea6c15f 100644 --- a/gns3server/compute/dynamips/nodes/router.py +++ b/gns3server/compute/dynamips/nodes/router.py @@ -1290,7 +1290,7 @@ class Router(BaseNode): :param port_number: port number :param nio: NIO instance to add to the slot/port """ - pass + yield from nio.update() @asyncio.coroutine def slot_remove_nio_binding(self, slot_number, port_number): diff --git a/gns3server/compute/vpcs/vpcs_vm.py b/gns3server/compute/vpcs/vpcs_vm.py index f37f397c..f45d8985 100644 --- a/gns3server/compute/vpcs/vpcs_vm.py +++ b/gns3server/compute/vpcs/vpcs_vm.py @@ -397,7 +397,7 @@ class VPCSVM(BaseNode): raise VPCSError("Port {port_number} doesn't exist in adapter {adapter}".format(adapter=self._ethernet_adapter, port_number=port_number)) if self.ubridge: - yield from self._update_ubridge_udp_connection("VPCS-{}".format(self._id), self._local_udp_tunnel[1], nio) + yield from self.update_ubridge_udp_connection("VPCS-{}".format(self._id), self._local_udp_tunnel[1], nio) elif self.is_running(): raise VPCSError("Sorry, adding a link to a started VPCS instance is not supported without using uBridge.") diff --git a/gns3server/controller/link.py b/gns3server/controller/link.py index 78bc033c..2e1e2956 100644 --- a/gns3server/controller/link.py +++ b/gns3server/controller/link.py @@ -365,7 +365,7 @@ class Link: :returns: None if no node support filtering else the node """ for node in self._nodes: - if node["node"].node_type in ('vpcs', ): + if node["node"].node_type in ('vpcs', 'dynamips'): return node["node"] return None diff --git a/gns3server/handlers/api/compute/dynamips_vm_handler.py b/gns3server/handlers/api/compute/dynamips_vm_handler.py index dd0352de..6626a0d8 100644 --- a/gns3server/handlers/api/compute/dynamips_vm_handler.py +++ b/gns3server/handlers/api/compute/dynamips_vm_handler.py @@ -268,6 +268,35 @@ class DynamipsVMHandler: response.set_status(201) response.json(nio) + @Route.put( + r"/projects/{project_id}/dynamips/nodes/{node_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio", + parameters={ + "project_id": "Project UUID", + "node_id": "Node UUID", + "adapter_number": "Network adapter where the nio is located", + "port_number": "Port from where the nio should be updated" + }, + status_codes={ + 201: "NIO updated", + 400: "Invalid request", + 404: "Instance doesn't exist" + }, + input=NIO_SCHEMA, + output=NIO_SCHEMA, + description="Update a NIO from a Dynamips instance") + def update_nio(request, response): + + dynamips_manager = Dynamips.instance() + vm = dynamips_manager.get_node(request.match_info["node_id"], project_id=request.match_info["project_id"]) + slot_number = int(request.match_info["adapter_number"]) + port_number = int(request.match_info["port_number"]) + nio = vm.slots[slot_number].get_nio(port_number) + if "filters" in request.json and nio: + nio.filters = request.json["filters"] + yield from vm.slot_update_nio_binding(slot_number, port_number, nio) + response.set_status(201) + response.json(request.json) + @Route.delete( r"/projects/{project_id}/dynamips/nodes/{node_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio", parameters={ diff --git a/tests/compute/test_base_node.py b/tests/compute/test_base_node.py index e942c698..635c9966 100644 --- a/tests/compute/test_base_node.py +++ b/tests/compute/test_base_node.py @@ -133,7 +133,7 @@ def test_update_ubridge_udp_connection(node, async_run): snio = NIOUDP(1245, "localhost", 1246, []) dnio = NIOUDP(1245, "localhost", 1244, filters) with asyncio_patch("gns3server.compute.base_node.BaseNode._ubridge_apply_filters") as mock: - async_run(node._update_ubridge_udp_connection('VPCS-10', snio, dnio)) + async_run(node.update_ubridge_udp_connection('VPCS-10', snio, dnio)) mock.assert_called_with("VPCS-10", filters)