Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

refactor(api): pass session explicitly into RBACResourceService (#37403) (#42248)

Sean Suna4d74f7c8221
diff --git a/api/controllers/common/rbac/locators.py b/api/controllers/common/rbac/locators.py
index 9fcf88bc3f1..3cc5d5373e6 100644
--- a/api/controllers/common/rbac/locators.py
+++ b/api/controllers/common/rbac/locators.py
@@ -6,6 +6,7 @@ from flask import g
from werkzeug.exceptions import NotFound
from core.rbac import RBACResourceScope
+from extensions.ext_database import db
from services.rbac_resource_service import RBACResourceService
if TYPE_CHECKING:
@@ -52,7 +53,7 @@ def agent_binding(tenant_id: str, app_id: str) -> "Agent | None":
cache: dict[tuple[str, str], Agent | None] = g.setdefault(_AGENT_BINDING_CACHE_KEY, {})
key = (tenant_id, app_id)
if key not in cache:
- cache[key] = RBACResourceService.get_app_agent_binding(tenant_id, app_id)
+ cache[key] = RBACResourceService.get_app_agent_binding(db.session, tenant_id, app_id)
return cache[key]
@@ -102,7 +103,7 @@ class PlainApp(_ParamLocator):
return ResourceIdentity(self.scope, app_id)
def owner_id(self, tenant_id: str, identity: ResourceIdentity) -> str | None:
- return RBACResourceService.get_app_maintainer(tenant_id, identity.id)
+ return RBACResourceService.get_app_maintainer(db.session, tenant_id, identity.id)
class AgentBehindApp(_ParamLocator):
@@ -129,7 +130,7 @@ class DatasetId(_ParamLocator):
return ResourceIdentity(self.scope, _required(path_args, self.param))
def owner_id(self, tenant_id: str, identity: ResourceIdentity) -> str | None:
- return RBACResourceService.get_dataset_maintainer(tenant_id, identity.id)
+ return RBACResourceService.get_dataset_maintainer(db.session, tenant_id, identity.id)
class DatasetByPipeline(DatasetId):
@@ -138,7 +139,7 @@ class DatasetByPipeline(DatasetId):
@override
def locate(self, tenant_id: str, path_args: Mapping[str, object]) -> ResourceIdentity | None:
pipeline_id = _required(path_args, self.param)
- dataset_id = RBACResourceService.get_dataset_id_by_pipeline(tenant_id, pipeline_id)
+ dataset_id = RBACResourceService.get_dataset_id_by_pipeline(db.session, tenant_id, pipeline_id)
if dataset_id is None:
raise NotFound("Dataset not found for pipeline")
return ResourceIdentity(self.scope, dataset_id)
diff --git a/api/services/rbac_resource_service.py b/api/services/rbac_resource_service.py
index 7acc54abedb..a464146ebdb 100644
--- a/api/services/rbac_resource_service.py
+++ b/api/services/rbac_resource_service.py
@@ -3,8 +3,8 @@ from __future__ import annotations
from typing import TYPE_CHECKING
from sqlalchemy import select
+from sqlalchemy.orm import Session, scoped_session
-from extensions.ext_database import db
from models.dataset import Dataset
from models.model import App
@@ -14,27 +14,27 @@ if TYPE_CHECKING:
class RBACResourceService:
@staticmethod
- def get_app_agent_binding(tenant_id: str, app_id: str) -> Agent | None:
- app_model = db.session.scalar(select(App).where(App.id == app_id, App.tenant_id == tenant_id))
+ def get_app_agent_binding(session: Session | scoped_session, tenant_id: str, app_id: str) -> Agent | None:
+ app_model = session.scalar(select(App).where(App.id == app_id, App.tenant_id == tenant_id))
if app_model is None:
return None
- return app_model.agent_app_binding_with_session(session=db.session, include_archived=True)
+ return app_model.agent_app_binding_with_session(session=session, include_archived=True)
@staticmethod
- def get_app_maintainer(tenant_id: str, app_id: str) -> str | None:
- return db.session.scalar(
+ def get_app_maintainer(session: Session | scoped_session, tenant_id: str, app_id: str) -> str | None:
+ return session.scalar(
select(App.maintainer).where(App.id == app_id, App.tenant_id == tenant_id, App.status == "normal")
)
@staticmethod
- def get_dataset_maintainer(tenant_id: str, dataset_id: str) -> str | None:
- return db.session.scalar(
+ def get_dataset_maintainer(session: Session | scoped_session, tenant_id: str, dataset_id: str) -> str | None:
+ return session.scalar(
select(Dataset.maintainer).where(Dataset.id == dataset_id, Dataset.tenant_id == tenant_id)
)
@staticmethod
- def get_dataset_id_by_pipeline(tenant_id: str, pipeline_id: str) -> str | None:
- dataset_id = db.session.scalar(
+ def get_dataset_id_by_pipeline(session: Session | scoped_session, tenant_id: str, pipeline_id: str) -> str | None:
+ dataset_id = session.scalar(
select(Dataset.id).where(Dataset.pipeline_id == pipeline_id, Dataset.tenant_id == tenant_id)
)
return None if dataset_id is None else str(dataset_id)
diff --git a/api/tests/unit_tests/services/test_rbac_resource_service.py b/api/tests/unit_tests/services/test_rbac_resource_service.py
new file mode 100644
index 00000000000..d93834b8eb6
--- /dev/null
+++ b/api/tests/unit_tests/services/test_rbac_resource_service.py
@@ -0,0 +1,135 @@
+"""RBACResourceService reads through the session its caller passes in (#37403)."""
+
+from sqlalchemy.orm import Session
+
+from models.agent import Agent, AgentKind, AgentScope, AgentSource, AgentStatus
+from models.dataset import Dataset
+from models.model import App, AppMode
+from services.rbac_resource_service import RBACResourceService
+
+TENANT_ID = "tenant-1"
+OTHER_TENANT_ID = "tenant-2"
+
+
+def _app(
+ *,
+ app_id: str,
+ tenant_id: str = TENANT_ID,
+ mode: AppMode = AppMode.CHAT,
+ maintainer: str | None = None,
+) -> App:
+ return App(
+ id=app_id,
+ tenant_id=tenant_id,
+ name=f"App {app_id}",
+ description="",
+ mode=mode,
+ enable_site=True,
+ enable_api=True,
+ max_active_requests=0,
+ maintainer=maintainer,
+ )
+
+
+def _agent(*, agent_id: str, app_id: str, status: AgentStatus = AgentStatus.ACTIVE) -> Agent:
+ return Agent(
+ id=agent_id,
+ tenant_id=TENANT_ID,
+ name=agent_id,
+ description="",
+ role="",
+ agent_kind=AgentKind.DIFY_AGENT,
+ scope=AgentScope.ROSTER,
+ source=AgentSource.AGENT_APP,
+ app_id=app_id,
+ status=status,
+ created_by="account-1",
+ updated_by="account-1",
+ )
+
+
+def _dataset(
+ *,
+ dataset_id: str,
+ tenant_id: str = TENANT_ID,
+ maintainer: str | None = None,
+ pipeline_id: str | None = None,
+) -> Dataset:
+ return Dataset(
+ id=dataset_id,
+ tenant_id=tenant_id,
+ name=f"Dataset {dataset_id}",
+ created_by="account-1",
+ maintainer=maintainer,
+ pipeline_id=pipeline_id,
+ )
+
+
+class TestGetAppAgentBinding:
+ def test_returns_none_when_the_app_belongs_to_another_tenant(self, sqlite_session: Session) -> None:
+ sqlite_session.add(_app(app_id="app-1", tenant_id=OTHER_TENANT_ID, mode=AppMode.AGENT))
+ sqlite_session.flush()
+
+ assert RBACResourceService.get_app_agent_binding(sqlite_session, TENANT_ID, "app-1") is None
+
+ def test_returns_none_for_an_app_that_is_not_an_agent_app(self, sqlite_session: Session) -> None:
+ sqlite_session.add_all([_app(app_id="app-1"), _agent(agent_id="agent-1", app_id="app-1")])
+ sqlite_session.flush()
+
+ assert RBACResourceService.get_app_agent_binding(sqlite_session, TENANT_ID, "app-1") is None
+
+ def test_returns_the_bound_agent_including_archived_ones(self, sqlite_session: Session) -> None:
+ sqlite_session.add_all(
+ [
+ _app(app_id="app-1", mode=AppMode.AGENT),
+ _agent(agent_id="agent-1", app_id="app-1", status=AgentStatus.ARCHIVED),
+ ]
+ )
+ sqlite_session.flush()
+
+ binding = RBACResourceService.get_app_agent_binding(sqlite_session, TENANT_ID, "app-1")
+
+ assert binding is not None
+ assert binding.id == "agent-1"
+
+
+class TestGetAppMaintainer:
+ def test_returns_the_maintainer_of_an_app_in_the_tenant(self, sqlite_session: Session) -> None:
+ sqlite_session.add(_app(app_id="app-1", maintainer="account-1"))
+ sqlite_session.flush()
+
+ assert RBACResourceService.get_app_maintainer(sqlite_session, TENANT_ID, "app-1") == "account-1"
+
+ def test_does_not_leak_across_tenants(self, sqlite_session: Session) -> None:
+ sqlite_session.add(_app(app_id="app-1", tenant_id=OTHER_TENANT_ID, maintainer="account-1"))
+ sqlite_session.flush()
+
+ assert RBACResourceService.get_app_maintainer(sqlite_session, TENANT_ID, "app-1") is None
+
+
+class TestGetDatasetMaintainer:
+ def test_returns_the_maintainer_of_a_dataset_in_the_tenant(self, sqlite_session: Session) -> None:
+ sqlite_session.add(_dataset(dataset_id="dataset-1", maintainer="account-2"))
+ sqlite_session.flush()
+
+ assert RBACResourceService.get_dataset_maintainer(sqlite_session, TENANT_ID, "dataset-1") == "account-2"
+
+ def test_does_not_leak_across_tenants(self, sqlite_session: Session) -> None:
+ sqlite_session.add(_dataset(dataset_id="dataset-1", tenant_id=OTHER_TENANT_ID, maintainer="account-2"))
+ sqlite_session.flush()
+
+ assert RBACResourceService.get_dataset_maintainer(sqlite_session, TENANT_ID, "dataset-1") is None
+
+
+class TestGetDatasetIdByPipeline:
+ def test_returns_the_dataset_id_for_the_pipeline(self, sqlite_session: Session) -> None:
+ sqlite_session.add(_dataset(dataset_id="dataset-1", pipeline_id="pipeline-1"))
+ sqlite_session.flush()
+
+ assert RBACResourceService.get_dataset_id_by_pipeline(sqlite_session, TENANT_ID, "pipeline-1") == "dataset-1"
+
+ def test_returns_none_when_no_dataset_in_the_tenant_uses_the_pipeline(self, sqlite_session: Session) -> None:
+ sqlite_session.add(_dataset(dataset_id="dataset-1", tenant_id=OTHER_TENANT_ID, pipeline_id="pipeline-1"))
+ sqlite_session.flush()
+
+ assert RBACResourceService.get_dataset_id_by_pipeline(sqlite_session, TENANT_ID, "pipeline-1") is None