From a5ba711694e89c8fe799d2cbbe4a3bb0d0334ad1 Mon Sep 17 00:00:00 2001 From: anthonyA1214 Date: Fri, 27 Nov 2026 18:44:49 +0800 Subject: [PATCH] feat(machines,config)!: require auth and permissions on machine API - Require session token or Django session auth on MachineController - Guard each machine route with a HasPermission check - Add custom restore, issue token, revoke token, and stream permissions to Machine, with the matching migration - Grant all machine permissions to the SystemAdmin role - Read CSRF_TRUSTED_ORIGINS from the environment - Add a users migration for the CustomUserManager state BREAKING CHANGE: /machines endpoints now require authentication and the matching permission. Deployments must also set CSRF_TRUSTED_ORIGINS. --- apps/machines/controllers.py | 60 +++++++++++++++---- .../migrations/0007_alter_machine_options.py | 17 ++++++ apps/machines/models.py | 6 ++ .../migrations/0005_alter_user_managers.py | 20 +++++++ config/roles.py | 10 ++++ config/settings/base.py | 2 + 6 files changed, 105 insertions(+), 10 deletions(-) create mode 100644 apps/machines/migrations/0007_alter_machine_options.py create mode 100644 apps/users/migrations/0005_alter_user_managers.py diff --git a/apps/machines/controllers.py b/apps/machines/controllers.py index 4a5cb90..2bc0efc 100644 --- a/apps/machines/controllers.py +++ b/apps/machines/controllers.py @@ -1,10 +1,12 @@ from uuid import UUID # noqa: TC003 +from allauth.headless.contrib.ninja.security import x_session_token_auth from django.http import StreamingHttpResponse from django.shortcuts import aget_object_or_404 from injector import inject from ninja import Query # noqa: TC002 from ninja.responses import Status +from ninja.security import django_auth from ninja_extra import api_controller from ninja_extra import paginate from ninja_extra import route @@ -15,6 +17,8 @@ from ninja_extra.searching import Searching from ninja_extra.searching import searching +from config.permissions import HasPermission + from .filters import MachineFilterSchema # noqa: TC001 from .models import Machine from .schemas import MachineCreateIn @@ -26,13 +30,21 @@ from .services import MachineService # noqa: TC001 -@api_controller("/machines", tags=["Machines"]) +@api_controller( + "/machines", + tags=["Machines"], + auth=[x_session_token_auth, django_auth], +) class MachineController: @inject def __init__(self, machines_service: MachineService): self.machines_service = machines_service - @route.get("/", response=PaginatedResponseSchema[MachineOut]) + @route.get( + "/", + response=PaginatedResponseSchema[MachineOut], + permissions=[HasPermission("view_machine")], + ) @paginate(PageNumberPaginationExtra, page_size=25) @ordering( Ordering, @@ -42,38 +54,66 @@ def __init__(self, machines_service: MachineService): def get_machines(self, filters: Query[MachineFilterSchema]): return self.machines_service.list_machines(filters) - @route.post("/", response={201: MachineCreateOut}) + @route.post( + "/", + response={201: MachineCreateOut}, + permissions=[HasPermission("add_machine")], + ) def create_machine(self, payload: MachineCreateIn): return Status(201, self.machines_service.create_machine(payload)) - @route.get("/{machine_id}/", response={200: MachineOut}) + @route.get( + "/{machine_id}/", + response={200: MachineOut}, + permissions=[HasPermission("view_machine")], + ) def get_machine(self, machine_id: UUID): return self.machines_service.get_machine(machine_id) - @route.patch("/{machine_id}/", response={200: MachineUpdateOut}) + @route.patch( + "/{machine_id}/", + response={200: MachineUpdateOut}, + permissions=[HasPermission("change_machine")], + ) def update_machine(self, machine_id: UUID, payload: MachineUpdateIn): return Status(200, self.machines_service.update_machine(machine_id, payload)) - @route.delete("/{machine_id}/", response={204: None}) + @route.delete( + "/{machine_id}/", + response={204: None}, + permissions=[HasPermission("delete_machine")], + ) def delete_machine(self, machine_id: UUID): self.machines_service.delete_machine(machine_id) return Status(204, None) - @route.post("/{machine_id}/restore/", response={204: None}) + @route.post( + "/{machine_id}/restore/", + response={204: None}, + permissions=[HasPermission("restore_machine")], + ) def restore_machine(self, machine_id: UUID): self.machines_service.restore_machine(machine_id) return Status(204, None) - @route.post("/{machine_id}/token/", response={200: MachineIssueTokenOut}) + @route.post( + "/{machine_id}/token/", + response={200: MachineIssueTokenOut}, + permissions=[HasPermission("issue_machine_token")], + ) def issue_token(self, machine_id: UUID): return Status(200, self.machines_service.issue_token(machine_id)) - @route.post("/{machine_id}/token/revoke/", response={204: None}) + @route.post( + "/{machine_id}/token/revoke/", + response={204: None}, + permissions=[HasPermission("revoke_machine_token")], + ) def revoke_token(self, machine_id: UUID): self.machines_service.revoke_token(machine_id) return Status(204, None) - @route.get("/{machine_id}/stream/") + @route.get("/{machine_id}/stream/", permissions=[HasPermission("stream_machine")]) async def stream(self, machine_id: UUID): machine = await aget_object_or_404(Machine, id=machine_id) res = StreamingHttpResponse( diff --git a/apps/machines/migrations/0007_alter_machine_options.py b/apps/machines/migrations/0007_alter_machine_options.py new file mode 100644 index 0000000..d98195b --- /dev/null +++ b/apps/machines/migrations/0007_alter_machine_options.py @@ -0,0 +1,17 @@ +# Generated by Django 6.1.3 on 2026-11-27 10:42 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('machines', '0006_machine_revoked_at'), + ] + + operations = [ + migrations.AlterModelOptions( + name='machine', + options={'permissions': [('restore_machine', 'Can restore deleted machine'), ('issue_machine_token', 'Can issue machine token'), ('revoke_machine_token', 'Can revoke machine token'), ('stream_machine', 'Can stream machine events')]}, + ), + ] diff --git a/apps/machines/models.py b/apps/machines/models.py index 4a1fbf4..550cc0b 100644 --- a/apps/machines/models.py +++ b/apps/machines/models.py @@ -25,6 +25,12 @@ class TokenStatus(models.TextChoices): revoked_at = models.DateTimeField(blank=True, null=True, editable=False) class Meta(BaseModel.Meta): + permissions = [ + ("restore_machine", "Can restore deleted machine"), + ("issue_machine_token", "Can issue machine token"), + ("revoke_machine_token", "Can revoke machine token"), + ("stream_machine", "Can stream machine events"), + ] constraints = [ models.UniqueConstraint( fields=["name"], diff --git a/apps/users/migrations/0005_alter_user_managers.py b/apps/users/migrations/0005_alter_user_managers.py new file mode 100644 index 0000000..d56b265 --- /dev/null +++ b/apps/users/migrations/0005_alter_user_managers.py @@ -0,0 +1,20 @@ +# Generated by Django 6.1.3 on 2026-11-27 10:42 + +import apps.users.managers +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0004_alter_user_options_alter_user_managers_and_more'), + ] + + operations = [ + migrations.AlterModelManagers( + name='user', + managers=[ + ('objects', apps.users.managers.CustomUserManager()), + ], + ), + ] diff --git a/config/roles.py b/config/roles.py index c3b9d5f..4488657 100644 --- a/config/roles.py +++ b/config/roles.py @@ -3,10 +3,20 @@ class SystemAdmin(AbstractUserRole): available_permissions = { # type: ignore[var-annotated] + # users "add_user": True, "change_user": True, "delete_user": True, "view_user": True, + # machines + "add_machine": True, + "change_machine": True, + "delete_machine": True, + "view_machine": True, + "restore_machine": True, + "issue_machine_token": True, + "revoke_machine_token": True, + "stream_machine": True, } diff --git a/config/settings/base.py b/config/settings/base.py index dcee7ea..9427cdf 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -65,6 +65,8 @@ CORS_ALLOWED_ORIGINS = env.list("CORS_ALLOWED_ORIGINS") CORS_ALLOW_CREDENTIALS = True +CSRF_TRUSTED_ORIGINS = env.list("CSRF_TRUSTED_ORIGINS") + HEADLESS_ONLY = True HEADLESS_SERVE_SPECIFICATION = True