perf: keep RBAC state in memory for permission checks

Permission checks ran several queries per request, including a full
scan of the resources table. The controller already keeps projects,
computes and notifications in process memory: apply the same idea to
the RBAC tables.

Roles, ACEs, group memberships and resource pool memberships are
loaded into a process-wide snapshot. Checks run against the snapshot
in memory. Repository write methods mark the snapshot dirty and the
next read reloads it from the database, so privilege changes apply
right away. The database stays the source of truth.

The evaluation is a faithful port of the previous SQL version, quirks
included (reverse path traversal, deny raising PermissionError, pool
checks with the "/pool" prefix, group ACEs in natural order).

The two RBAC tests that mutated ORM objects directly now write through
the repository, like the rest of the code. The /projects ACE they
relied on was created by another test, so they were order dependent:
they now create it when missing.
This commit is contained in:
YueGuobin committed 2026-11-06 23:47:29 +08:00
1 parent 111a4b756c
commit 2244d730bb
9 files changed
+711 -240

No files matched your search

+5
View File
@@ -27,6 +27,7 @@ from gns3server.compute.project_manager import ProjectManager
from gns3server.config import Config
from gns3server.controller import Controller
from gns3server.db.models import Base, Compute, User
from gns3server.db.rbac_state import RbacState
from gns3server.db.repositories.computes import ComputesRepository
from gns3server.db.repositories.users import UsersRepository
from gns3server.schemas.controller.computes import Protocol
@@ -82,6 +83,10 @@ async def db_session(db_engine):
await conn.run_sync(Base.metadata.drop_all)
await conn.run_sync(Base.metadata.create_all)
# the RBAC snapshot is process-wide: drop it so it reloads from the
# freshly recreated tables instead of holding data from a previous class
RbacState.instance().reset()
session = AsyncSession(db_engine, expire_on_commit=False)
try:
yield session