mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-04 22:41:12 +02:00
"Local" command line parameter is only for stopping
a server that has been started by the desktop GUI
This commit is contained in:
1 parent
8975f63e2f
commit
900d4f79ee
18 files changed
+138
-186
No files matched your search
@@ -25,7 +25,7 @@ import psutil
|
||||
from gns3server.config import Config
|
||||
from gns3server.utils.cpu_percent import CpuPercent
|
||||
from gns3server.version import __version__
|
||||
from gns3server.utils.path import get_default_project_directory
|
||||
from gns3server.utils.path import get_default_project_directory, is_safe_path
|
||||
from gns3server.compute.port_manager import PortManager
|
||||
from gns3server.compute.project_manager import ProjectManager
|
||||
from gns3server.utils.interfaces import interfaces
|
||||
@@ -81,8 +81,7 @@ def compute_version() -> dict:
|
||||
Retrieve the server version number.
|
||||
"""
|
||||
|
||||
local_server = Config.instance().settings.Server.local
|
||||
return {"version": __version__, "local": local_server}
|
||||
return {"version": __version__}
|
||||
|
||||
|
||||
@router.get("/statistics")
|
||||
@@ -155,12 +154,17 @@ async def create_qemu_image(image_data: schemas.QemuImageCreate) -> Response:
|
||||
Create a Qemu image.
|
||||
"""
|
||||
|
||||
if os.path.isabs(image_data.path):
|
||||
if Config.instance().settings.Server.local is False:
|
||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
||||
#FIXME: move to controller
|
||||
raise NotImplementedError()
|
||||
|
||||
# Raise error if user try to escape
|
||||
#if not is_safe_path(image_data.path, project.path):
|
||||
# raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
||||
|
||||
await Qemu.instance().create_disk(
|
||||
image_data.qemu_img, image_data.path, jsonable_encoder(image_data, exclude_unset=True)
|
||||
image_data.qemu_img,
|
||||
image_data.path,
|
||||
jsonable_encoder(image_data, exclude_unset=True, exclude={"qemu_img", "path"})
|
||||
)
|
||||
|
||||
return Response(status_code=status.HTTP_204_NO_CONTENT)
|
||||
@@ -176,9 +180,12 @@ async def update_qemu_image(image_data: schemas.QemuImageUpdate) -> Response:
|
||||
Update a Qemu image.
|
||||
"""
|
||||
|
||||
if os.path.isabs(image_data.path):
|
||||
if Config.instance().settings.Server.local is False:
|
||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
||||
#FIXME: move to controller
|
||||
raise NotImplementedError()
|
||||
|
||||
# Raise error if user try to escape
|
||||
#if not is_safe_path(image_data.path, project.path):
|
||||
# raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
||||
|
||||
if image_data.extend:
|
||||
await Qemu.instance().resize_disk(image_data.qemu_img, image_data.path, image_data.extend)
|
||||
|
||||
@@ -85,7 +85,6 @@ async def upload_image(
|
||||
if os.path.commonprefix([base_images_directory, full_path]) != base_images_directory:
|
||||
raise ControllerForbiddenError(f"Cannot write image, '{image_path}' is forbidden")
|
||||
|
||||
print(image_path)
|
||||
if await images_repo.get_image(image_path):
|
||||
raise ControllerBadRequestError(f"Image '{image_path}' already exists")
|
||||
|
||||
|
||||
@@ -342,10 +342,10 @@ async def import_project(
|
||||
Import a project from a portable archive.
|
||||
"""
|
||||
|
||||
controller = Controller.instance()
|
||||
if Config.instance().settings.Server.local is False:
|
||||
raise ControllerForbiddenError("The server is not local")
|
||||
#TODO: import project remotely
|
||||
raise NotImplementedError()
|
||||
|
||||
controller = Controller.instance()
|
||||
# We write the content to a temporary location and after we extract it all.
|
||||
# It could be more optimal to stream this but it is not implemented in Python.
|
||||
try:
|
||||
@@ -385,16 +385,9 @@ async def duplicate_project(
|
||||
Duplicate a project.
|
||||
"""
|
||||
|
||||
if project_data.path:
|
||||
if Config.instance().settings.Server.local is False:
|
||||
raise ControllerForbiddenError("The server is not a local server")
|
||||
location = project_data.path
|
||||
else:
|
||||
location = None
|
||||
|
||||
reset_mac_addresses = project_data.reset_mac_addresses
|
||||
new_project = await project.duplicate(
|
||||
name=project_data.name, location=location, reset_mac_addresses=reset_mac_addresses
|
||||
name=project_data.name, reset_mac_addresses=reset_mac_addresses
|
||||
)
|
||||
await rbac_repo.add_permission_to_user_with_path(current_user.user_id, f"/projects/{new_project.id}/*")
|
||||
return new_project.asdict()
|
||||
@@ -423,7 +416,7 @@ async def get_file(file_path: str, project: Project = Depends(dep_project)) -> F
|
||||
@router.post("/{project_id}/files/{file_path:path}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
async def write_file(file_path: str, request: Request, project: Project = Depends(dep_project)) -> Response:
|
||||
"""
|
||||
Write a file from a project.
|
||||
Write a file to a project.
|
||||
"""
|
||||
|
||||
file_path = urllib.parse.unquote(file_path)
|
||||
|
||||
@@ -85,10 +85,6 @@ class Project:
|
||||
"variables": self._variables
|
||||
}
|
||||
|
||||
def is_local(self):
|
||||
|
||||
return Config.instance().settings.Server.local
|
||||
|
||||
@property
|
||||
def id(self):
|
||||
|
||||
@@ -101,12 +97,12 @@ class Project:
|
||||
|
||||
@path.setter
|
||||
def path(self, path):
|
||||
check_path_allowed(path)
|
||||
|
||||
if hasattr(self, "_path"):
|
||||
if path != self._path and self.is_local() is False:
|
||||
if path != self._path:
|
||||
raise ComputeForbiddenError("Changing the project directory path is not allowed")
|
||||
|
||||
check_path_allowed(path)
|
||||
self._path = path
|
||||
|
||||
@property
|
||||
|
||||
@@ -267,6 +267,7 @@ class Qemu(BaseManager):
|
||||
command.append(path)
|
||||
command.append(f"{img_size}M")
|
||||
|
||||
print(command)
|
||||
process = await asyncio.create_subprocess_exec(*command)
|
||||
await process.wait()
|
||||
except (OSError, subprocess.SubprocessError) as e:
|
||||
|
||||
@@ -1038,7 +1038,7 @@ class Project:
|
||||
while self._loading:
|
||||
await asyncio.sleep(0.5)
|
||||
|
||||
async def duplicate(self, name=None, location=None, reset_mac_addresses=True):
|
||||
async def duplicate(self, name=None, reset_mac_addresses=True):
|
||||
"""
|
||||
Duplicate a project
|
||||
|
||||
@@ -1047,7 +1047,6 @@ class Project:
|
||||
It's a little slower but we have only one implementation to maintain.
|
||||
|
||||
:param name: Name of the new project. A new one will be generated in case of conflicts
|
||||
:param location: Parent directory of the new project
|
||||
:param reset_mac_addresses: Reset MAC addresses for the new project
|
||||
"""
|
||||
# If the project was not open we open it temporary
|
||||
@@ -1062,11 +1061,8 @@ class Project:
|
||||
|
||||
# use the parent directory of the project we are duplicating as a
|
||||
# temporary directory to avoid no space left issues when '/tmp'
|
||||
# is location on another partition.
|
||||
if location:
|
||||
working_dir = os.path.abspath(os.path.join(location, os.pardir))
|
||||
else:
|
||||
working_dir = os.path.abspath(os.path.join(self.path, os.pardir))
|
||||
# is located on another partition.
|
||||
working_dir = os.path.abspath(os.path.join(self.path, os.pardir))
|
||||
|
||||
with tempfile.TemporaryDirectory(dir=working_dir) as tmpdir:
|
||||
# Do not compress the exported project when duplicating
|
||||
@@ -1090,7 +1086,11 @@ class Project:
|
||||
# import the temporary project
|
||||
with open(project_path, "rb") as f:
|
||||
project = await import_project(
|
||||
self._controller, str(uuid.uuid4()), f, location=location, name=name, keep_compute_id=True
|
||||
self._controller,
|
||||
str(uuid.uuid4()),
|
||||
f,
|
||||
name=name,
|
||||
keep_compute_id=True
|
||||
)
|
||||
|
||||
log.info(f"Project '{project.name}' duplicated in {time.time() - begin:.4f} seconds")
|
||||
|
||||
@@ -255,9 +255,6 @@ class Server:
|
||||
self._set_config_defaults_from_command_line(args)
|
||||
config = Config.instance().settings
|
||||
|
||||
if config.Server.local:
|
||||
log.warning("Local mode is enabled. Beware, clients will have full control on your filesystem")
|
||||
|
||||
if not config.Server.compute_password.get_secret_value():
|
||||
alphabet = string.ascii_letters + string.digits + string.punctuation
|
||||
generated_password = ''.join(secrets.choice(alphabet) for _ in range(16))
|
||||
|
||||
@@ -60,8 +60,7 @@ def check_path_allowed(path: str):
|
||||
if len(os.path.commonprefix([project_directory, path])) == len(project_directory):
|
||||
return
|
||||
|
||||
if Config.instance().settings.Server.local is False:
|
||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="The path is not allowed")
|
||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail=f"The path {path} is not allowed")
|
||||
|
||||
|
||||
def get_mountpoint(path: str):
|
||||
|
||||
Reference in new issue
Block a user