diff --git a/gns3server/schemas/config.py b/gns3server/schemas/config.py index 759af0bc..9d4aae97 100644 --- a/gns3server/schemas/config.py +++ b/gns3server/schemas/config.py @@ -138,6 +138,7 @@ class ServerSettings(BaseModel): allowed_interfaces: List[str] = Field(default_factory=list) default_nat_interface: str = None allow_remote_console: bool = False + enable_builtin_templates: bool = True @validator("additional_images_paths", pre=True) def split_additional_images_paths(cls, v): diff --git a/gns3server/services/templates.py b/gns3server/services/templates.py index 248ca9d1..c0667170 100644 --- a/gns3server/services/templates.py +++ b/gns3server/services/templates.py @@ -23,6 +23,7 @@ from fastapi.encoders import jsonable_encoder from typing import List from gns3server import schemas +from gns3server.config import Config import gns3server.db.models as models from gns3server.db.repositories.templates import TemplatesRepository from gns3server.controller.controller_error import ( @@ -174,8 +175,9 @@ class TemplatesService: db_templates = await self._templates_repo.get_templates() for db_template in db_templates: templates.append(db_template.asjson()) - for builtin_template in BUILTIN_TEMPLATES: - templates.append(jsonable_encoder(builtin_template)) + if Config.instance().settings.Server.enable_builtin_templates: + for builtin_template in BUILTIN_TEMPLATES: + templates.append(jsonable_encoder(builtin_template)) return templates async def _find_image(self, image_path: str): diff --git a/tests/api/routes/controller/test_templates.py b/tests/api/routes/controller/test_templates.py index 18545ada..1a096d5f 100644 --- a/tests/api/routes/controller/test_templates.py +++ b/tests/api/routes/controller/test_templates.py @@ -28,6 +28,7 @@ from tests.utils import asyncio_patch from gns3server.db.repositories.images import ImagesRepository from gns3server.db.repositories.templates import TemplatesRepository from gns3server.controller import Controller +from gns3server.controller import Config from gns3server.services.templates import BUILTIN_TEMPLATES pytestmark = pytest.mark.asyncio @@ -257,6 +258,18 @@ class TestBuiltinTemplates: response = await client.delete(app.url_path_for("delete_template", template_id=template_id)) assert response.status_code == status.HTTP_403_FORBIDDEN + async def test_list_builtin_templates_not_enabled( + self, + app: FastAPI, + client: AsyncClient, + controller: Controller + ) -> None: + + config = Config.instance() + config.settings.Server.enable_builtin_templates = False + response = await client.get(app.url_path_for("get_templates")) + assert not response.json() + class TestDynamipsTemplate: