2020-10-02 06:37:50 +00:00
|
|
|
#
|
2023-09-02 10:54:24 +00:00
|
|
|
# Copyright (C) 2023 GNS3 Technologies Inc.
|
2020-10-02 06:37:50 +00:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
"""
|
2020-11-19 04:51:03 +00:00
|
|
|
API routes for links.
|
2020-10-02 06:37:50 +00:00
|
|
|
"""
|
|
|
|
|
2020-10-19 05:25:29 +00:00
|
|
|
import multidict
|
2020-10-22 05:49:44 +00:00
|
|
|
import aiohttp
|
2020-10-19 05:25:29 +00:00
|
|
|
|
2023-09-02 10:54:24 +00:00
|
|
|
from fastapi import APIRouter, Depends, Request, status
|
2020-10-02 06:37:50 +00:00
|
|
|
from fastapi.responses import StreamingResponse
|
2020-10-19 05:25:29 +00:00
|
|
|
from fastapi.encoders import jsonable_encoder
|
2020-10-02 06:37:50 +00:00
|
|
|
from typing import List
|
|
|
|
from uuid import UUID
|
|
|
|
|
|
|
|
from gns3server.controller import Controller
|
|
|
|
from gns3server.controller.controller_error import ControllerError
|
2023-09-02 10:54:24 +00:00
|
|
|
from gns3server.db.repositories.rbac import RbacRepository
|
2020-10-14 00:19:29 +00:00
|
|
|
from gns3server.controller.link import Link
|
2020-10-22 05:49:44 +00:00
|
|
|
from gns3server.utils.http_client import HTTPClient
|
2020-10-31 05:32:21 +00:00
|
|
|
from gns3server import schemas
|
2020-10-02 06:37:50 +00:00
|
|
|
|
2023-09-02 10:54:24 +00:00
|
|
|
from .dependencies.database import get_repository
|
|
|
|
from .dependencies.rbac import has_privilege
|
|
|
|
|
2020-10-22 05:49:44 +00:00
|
|
|
import logging
|
2021-04-13 09:16:50 +00:00
|
|
|
|
2020-10-22 05:49:44 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2021-04-13 09:16:50 +00:00
|
|
|
responses = {404: {"model": schemas.ErrorMessage, "description": "Could not find project or link"}}
|
2020-10-02 06:37:50 +00:00
|
|
|
|
2021-04-13 06:49:56 +00:00
|
|
|
router = APIRouter(responses=responses)
|
|
|
|
|
2020-10-14 00:19:29 +00:00
|
|
|
|
2021-04-18 06:10:38 +00:00
|
|
|
async def dep_link(project_id: UUID, link_id: UUID) -> Link:
|
2020-10-14 00:19:29 +00:00
|
|
|
"""
|
|
|
|
Dependency to retrieve a link.
|
|
|
|
"""
|
|
|
|
|
|
|
|
project = await Controller.instance().get_loaded_project(str(project_id))
|
|
|
|
link = project.get_link(str(link_id))
|
|
|
|
return link
|
2020-10-02 06:37:50 +00:00
|
|
|
|
|
|
|
|
2023-09-02 10:54:24 +00:00
|
|
|
@router.get(
|
|
|
|
"",
|
|
|
|
response_model=List[schemas.Link],
|
|
|
|
response_model_exclude_unset=True,
|
|
|
|
dependencies=[Depends(has_privilege("Link.Audit"))]
|
|
|
|
)
|
2021-04-18 06:10:38 +00:00
|
|
|
async def get_links(project_id: UUID) -> List[schemas.Link]:
|
2020-10-14 00:19:29 +00:00
|
|
|
"""
|
|
|
|
Return all links for a given project.
|
2023-09-02 10:54:24 +00:00
|
|
|
|
|
|
|
Required privilege: Link.Audit
|
2020-10-14 00:19:29 +00:00
|
|
|
"""
|
2020-10-02 06:37:50 +00:00
|
|
|
|
|
|
|
project = await Controller.instance().get_loaded_project(str(project_id))
|
2021-04-17 14:04:28 +00:00
|
|
|
return [v.asdict() for v in project.links.values()]
|
2020-10-02 06:37:50 +00:00
|
|
|
|
|
|
|
|
2021-04-13 09:16:50 +00:00
|
|
|
@router.post(
|
|
|
|
"",
|
|
|
|
status_code=status.HTTP_201_CREATED,
|
|
|
|
response_model=schemas.Link,
|
|
|
|
responses={
|
|
|
|
404: {"model": schemas.ErrorMessage, "description": "Could not find project"},
|
|
|
|
409: {"model": schemas.ErrorMessage, "description": "Could not create link"},
|
|
|
|
},
|
2023-09-02 10:54:24 +00:00
|
|
|
dependencies=[Depends(has_privilege("Link.Allocate"))]
|
2021-04-13 09:16:50 +00:00
|
|
|
)
|
2021-04-18 06:10:38 +00:00
|
|
|
async def create_link(project_id: UUID, link_data: schemas.LinkCreate) -> schemas.Link:
|
2020-10-02 06:37:50 +00:00
|
|
|
"""
|
2020-10-14 00:19:29 +00:00
|
|
|
Create a new link.
|
2023-09-02 10:54:24 +00:00
|
|
|
|
|
|
|
Required privilege: Link.Allocate
|
2020-10-02 06:37:50 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
project = await Controller.instance().get_loaded_project(str(project_id))
|
|
|
|
link = await project.add_link()
|
|
|
|
link_data = jsonable_encoder(link_data, exclude_unset=True)
|
|
|
|
if "filters" in link_data:
|
|
|
|
await link.update_filters(link_data["filters"])
|
2021-06-12 05:56:35 +00:00
|
|
|
if "link_style" in link_data:
|
|
|
|
await link.update_link_style(link_data["link_style"])
|
2020-10-02 06:37:50 +00:00
|
|
|
if "suspend" in link_data:
|
|
|
|
await link.update_suspend(link_data["suspend"])
|
|
|
|
try:
|
|
|
|
for node in link_data["nodes"]:
|
2021-04-13 09:16:50 +00:00
|
|
|
await link.add_node(
|
|
|
|
project.get_node(node["node_id"]),
|
|
|
|
node.get("adapter_number", 0),
|
|
|
|
node.get("port_number", 0),
|
|
|
|
label=node.get("label"),
|
|
|
|
)
|
2020-10-02 06:37:50 +00:00
|
|
|
except ControllerError as e:
|
|
|
|
await project.delete_link(link.id)
|
|
|
|
raise e
|
2021-04-17 14:04:28 +00:00
|
|
|
return link.asdict()
|
2020-10-02 06:37:50 +00:00
|
|
|
|
|
|
|
|
2023-09-02 10:54:24 +00:00
|
|
|
@router.get(
|
|
|
|
"/{link_id}/available_filters",
|
|
|
|
dependencies=[Depends(has_privilege("Link.Audit"))]
|
|
|
|
)
|
2021-04-18 06:10:38 +00:00
|
|
|
async def get_filters(link: Link = Depends(dep_link)) -> List[dict]:
|
2020-10-02 06:37:50 +00:00
|
|
|
"""
|
2020-10-14 00:19:29 +00:00
|
|
|
Return all filters available for a given link.
|
2023-09-02 10:54:24 +00:00
|
|
|
|
|
|
|
Required privilege: Link.Audit
|
2020-10-02 06:37:50 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
return link.available_filters()
|
|
|
|
|
|
|
|
|
2023-09-02 10:54:24 +00:00
|
|
|
@router.get(
|
|
|
|
"/{link_id}",
|
|
|
|
response_model=schemas.Link,
|
|
|
|
response_model_exclude_unset=True,
|
|
|
|
dependencies=[Depends(has_privilege("Link.Audit"))]
|
|
|
|
)
|
2021-04-18 06:10:38 +00:00
|
|
|
async def get_link(link: Link = Depends(dep_link)) -> schemas.Link:
|
2020-10-14 00:19:29 +00:00
|
|
|
"""
|
|
|
|
Return a link.
|
2023-09-02 10:54:24 +00:00
|
|
|
|
|
|
|
Required privilege: Link.Audit
|
2020-10-14 00:19:29 +00:00
|
|
|
"""
|
2020-10-02 06:37:50 +00:00
|
|
|
|
2021-04-17 14:04:28 +00:00
|
|
|
return link.asdict()
|
2020-10-02 06:37:50 +00:00
|
|
|
|
|
|
|
|
2023-09-02 10:54:24 +00:00
|
|
|
@router.put(
|
|
|
|
"/{link_id}",
|
|
|
|
response_model=schemas.Link,
|
|
|
|
response_model_exclude_unset=True,
|
|
|
|
dependencies=[Depends(has_privilege("Link.Modify"))]
|
|
|
|
)
|
2021-04-18 06:10:38 +00:00
|
|
|
async def update_link(link_data: schemas.LinkUpdate, link: Link = Depends(dep_link)) -> schemas.Link:
|
2020-10-14 00:19:29 +00:00
|
|
|
"""
|
|
|
|
Update a link.
|
2023-09-02 10:54:24 +00:00
|
|
|
|
|
|
|
Required privilege: Link.Modify
|
2020-10-14 00:19:29 +00:00
|
|
|
"""
|
2020-10-02 06:37:50 +00:00
|
|
|
|
|
|
|
link_data = jsonable_encoder(link_data, exclude_unset=True)
|
|
|
|
if "filters" in link_data:
|
|
|
|
await link.update_filters(link_data["filters"])
|
2021-06-12 05:56:35 +00:00
|
|
|
if "link_style" in link_data:
|
|
|
|
await link.update_link_style(link_data["link_style"])
|
2020-10-02 06:37:50 +00:00
|
|
|
if "suspend" in link_data:
|
|
|
|
await link.update_suspend(link_data["suspend"])
|
|
|
|
if "nodes" in link_data:
|
|
|
|
await link.update_nodes(link_data["nodes"])
|
2021-04-17 14:04:28 +00:00
|
|
|
return link.asdict()
|
2020-10-02 06:37:50 +00:00
|
|
|
|
|
|
|
|
2023-09-02 10:54:24 +00:00
|
|
|
@router.delete(
|
|
|
|
"/{link_id}",
|
|
|
|
status_code=status.HTTP_204_NO_CONTENT,
|
|
|
|
dependencies=[Depends(has_privilege("Link.Allocate"))]
|
|
|
|
)
|
|
|
|
async def delete_link(
|
|
|
|
project_id: UUID,
|
|
|
|
link: Link = Depends(dep_link),
|
|
|
|
rbac_repo: RbacRepository = Depends(get_repository(RbacRepository))
|
|
|
|
) -> None:
|
2020-10-02 06:37:50 +00:00
|
|
|
"""
|
2020-10-14 00:19:29 +00:00
|
|
|
Delete a link.
|
2023-09-02 10:54:24 +00:00
|
|
|
|
|
|
|
Required privilege: Link.Allocate
|
2020-10-02 06:37:50 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
project = await Controller.instance().get_loaded_project(str(project_id))
|
2020-10-14 00:19:29 +00:00
|
|
|
await project.delete_link(link.id)
|
2023-09-02 10:54:24 +00:00
|
|
|
await rbac_repo.delete_all_ace_starting_with_path(f"/links/{link.id}")
|
2020-10-02 06:37:50 +00:00
|
|
|
|
|
|
|
|
2023-09-02 10:54:24 +00:00
|
|
|
@router.post(
|
|
|
|
"/{link_id}/reset",
|
|
|
|
response_model=schemas.Link,
|
|
|
|
dependencies=[Depends(has_privilege("Link.Modify"))]
|
|
|
|
)
|
2021-04-18 06:10:38 +00:00
|
|
|
async def reset_link(link: Link = Depends(dep_link)) -> schemas.Link:
|
2020-10-14 00:19:29 +00:00
|
|
|
"""
|
|
|
|
Reset a link.
|
2023-09-02 10:54:24 +00:00
|
|
|
|
|
|
|
Required privilege: Link.Modify
|
2020-10-14 00:19:29 +00:00
|
|
|
"""
|
2020-10-02 06:37:50 +00:00
|
|
|
|
|
|
|
await link.reset()
|
2021-04-17 14:04:28 +00:00
|
|
|
return link.asdict()
|
2020-10-02 06:37:50 +00:00
|
|
|
|
|
|
|
|
2023-09-02 10:54:24 +00:00
|
|
|
@router.post(
|
|
|
|
"/{link_id}/capture/start",
|
|
|
|
status_code=status.HTTP_201_CREATED,
|
|
|
|
response_model=schemas.Link,
|
|
|
|
dependencies=[Depends(has_privilege("Link.Capture"))]
|
|
|
|
)
|
2021-04-18 06:10:38 +00:00
|
|
|
async def start_capture(capture_data: dict, link: Link = Depends(dep_link)) -> schemas.Link:
|
2020-11-02 01:35:32 +00:00
|
|
|
"""
|
|
|
|
Start packet capture on the link.
|
2023-09-02 10:54:24 +00:00
|
|
|
|
|
|
|
Required privilege: Link.Capture
|
2020-11-02 01:35:32 +00:00
|
|
|
"""
|
|
|
|
|
2021-04-13 09:16:50 +00:00
|
|
|
await link.start_capture(
|
|
|
|
data_link_type=capture_data.get("data_link_type", "DLT_EN10MB"),
|
|
|
|
capture_file_name=capture_data.get("capture_file_name"),
|
|
|
|
)
|
2021-04-17 14:04:28 +00:00
|
|
|
return link.asdict()
|
2020-11-02 01:35:32 +00:00
|
|
|
|
|
|
|
|
2023-09-02 10:54:24 +00:00
|
|
|
@router.post(
|
|
|
|
"/{link_id}/capture/stop",
|
|
|
|
status_code=status.HTTP_204_NO_CONTENT,
|
|
|
|
dependencies=[Depends(has_privilege("Link.Capture"))]
|
|
|
|
)
|
2022-07-15 22:12:18 +00:00
|
|
|
async def stop_capture(link: Link = Depends(dep_link)) -> None:
|
2020-11-02 01:35:32 +00:00
|
|
|
"""
|
|
|
|
Stop packet capture on the link.
|
2023-09-02 10:54:24 +00:00
|
|
|
|
|
|
|
Required privilege: Link.Capture
|
2020-11-02 01:35:32 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
await link.stop_capture()
|
|
|
|
|
|
|
|
|
2023-09-02 10:54:24 +00:00
|
|
|
@router.get(
|
|
|
|
"/{link_id}/capture/stream",
|
|
|
|
dependencies=[Depends(has_privilege("Link.Capture"))]
|
|
|
|
)
|
2021-04-18 06:10:38 +00:00
|
|
|
async def stream_pcap(request: Request, link: Link = Depends(dep_link)) -> StreamingResponse:
|
2020-10-19 05:25:29 +00:00
|
|
|
"""
|
|
|
|
Stream the PCAP capture file from compute.
|
2023-09-02 10:54:24 +00:00
|
|
|
|
|
|
|
Required privilege: Link.Capture
|
2020-10-19 05:25:29 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
if not link.capturing:
|
|
|
|
raise ControllerError("This link has no active packet capture")
|
|
|
|
|
|
|
|
compute = link.compute
|
|
|
|
pcap_streaming_url = link.pcap_streaming_url()
|
|
|
|
headers = multidict.MultiDict(request.headers)
|
2021-04-13 09:16:50 +00:00
|
|
|
headers["Host"] = compute.host
|
|
|
|
headers["Router-Host"] = request.client.host
|
2020-10-19 05:25:29 +00:00
|
|
|
body = await request.body()
|
|
|
|
|
2020-10-19 07:58:01 +00:00
|
|
|
async def compute_pcap_stream():
|
2020-10-19 05:25:29 +00:00
|
|
|
|
2020-10-22 05:49:44 +00:00
|
|
|
try:
|
2021-12-02 07:37:09 +00:00
|
|
|
async with HTTPClient.request(
|
|
|
|
request.method,
|
|
|
|
pcap_streaming_url,
|
|
|
|
user=compute.user,
|
|
|
|
password=compute.password,
|
|
|
|
timeout=None,
|
|
|
|
data=body
|
|
|
|
) as response:
|
2020-10-22 05:49:44 +00:00
|
|
|
async for data in response.content.iter_any():
|
2020-10-19 05:25:29 +00:00
|
|
|
if not data:
|
|
|
|
break
|
|
|
|
yield data
|
2020-10-22 05:49:44 +00:00
|
|
|
except aiohttp.ClientError as e:
|
|
|
|
raise ControllerError(f"Client error received when receiving pcap stream from compute: {e}")
|
2020-10-19 05:25:29 +00:00
|
|
|
|
2020-10-19 07:58:01 +00:00
|
|
|
return StreamingResponse(compute_pcap_stream(), media_type="application/vnd.tcpdump.pcap")
|