The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
refactor(core_utils): move resource_ownership out of the proxy (#45805)
nate-berric582bc6e68a1
diff --git a/litellm/litellm_core_utils/resource_ownership.py b/litellm/litellm_core_utils/resource_ownership.pynew file mode 100644index 00000000000..e0894b825a7--- /dev/null+++ b/litellm/litellm_core_utils/resource_ownership.py@@ -0,0 +1,93 @@+from typing import Final++from litellm.types.proxy.auth.user_api_key_auth import UserAPIKeyAuth+from litellm.types.proxy.auth.user_roles import LitellmUserRoles+++def is_proxy_admin(user_api_key_dict: UserAPIKeyAuth | None) -> bool:+ if user_api_key_dict is None:+ return False++ return (+ user_api_key_dict.user_role == LitellmUserRoles.PROXY_ADMIN+ or user_api_key_dict.user_role == LitellmUserRoles.PROXY_ADMIN.value+ )+++def get_resource_owner_scopes(+ user_api_key_dict: UserAPIKeyAuth | None,+) -> list[str]:+ """+ Return ownership scopes that may access a user-created proxy resource.++ Raw user_id is included for rows created before scope prefixes existed.+ Prefixes avoid collisions when falling back to team/org/key ownership+ for keys that do not have a user_id.++ Identity-less callers (no user_id, team_id, org_id, api_key, or token)+ return ``[]`` — they share no scope with any other caller, so access+ checks against an existing owner always fail and creates that depend+ on a primary scope must reject up front. Returning a shared sentinel+ here would let any two identity-less callers see each other's data.+ """+ if user_api_key_dict is None:+ return []++ scopes: Final[list[str]] = []++ def _add(scope: str | None) -> None:+ if scope and scope not in scopes:+ scopes.append(scope)++ if user_api_key_dict.user_id:+ _add(user_api_key_dict.user_id)+ _add(f"user:{user_api_key_dict.user_id}")+ if user_api_key_dict.team_id:+ _add(f"team:{user_api_key_dict.team_id}")+ if user_api_key_dict.org_id:+ _add(f"org:{user_api_key_dict.org_id}")+ if user_api_key_dict.api_key:+ _add(f"key:{user_api_key_dict.api_key}")+ if user_api_key_dict.token:+ _add(f"key:{user_api_key_dict.token}")++ return scopes+++def get_primary_resource_owner_scope(+ user_api_key_dict: UserAPIKeyAuth | None,+) -> str | None:+ """Return the canonical owner scope to stamp on newly-created rows.++ ``None`` for identity-less callers — callers that depend on a primary+ scope to record ownership must surface that as a hard error rather+ than fall back to a shared sentinel (which would collapse every+ identity-less caller into the same logical owner).+ """+ if user_api_key_dict is None:+ return None++ if user_api_key_dict.user_id:+ return user_api_key_dict.user_id+ if user_api_key_dict.team_id:+ return f"team:{user_api_key_dict.team_id}"+ if user_api_key_dict.org_id:+ return f"org:{user_api_key_dict.org_id}"+ if user_api_key_dict.api_key:+ return f"key:{user_api_key_dict.api_key}"+ if user_api_key_dict.token:+ return f"key:{user_api_key_dict.token}"+ return None+++def user_can_access_resource_owner(+ owner: str | None,+ user_api_key_dict: UserAPIKeyAuth | None,+) -> bool:+ if user_api_key_dict is None:+ return True+ if is_proxy_admin(user_api_key_dict):+ return True+ if owner is None:+ return False+ return owner in get_resource_owner_scopes(user_api_key_dict)diff --git a/litellm/llms/base_llm/managed_resources/isolation.py b/litellm/llms/base_llm/managed_resources/isolation.pyindex 6a71e8e9223..0a09de79891 100644--- a/litellm/llms/base_llm/managed_resources/isolation.py+++ b/litellm/llms/base_llm/managed_resources/isolation.py@@ -29,7 +29,7 @@ def resolve_resource_owner_id(A key with neither a user_id nor a team_id would otherwise stamp``created_by=None`` and be locked out of its own resources, so it ownsthem under its hashed token instead, using the ``key:`` scope prefix- already used by ``proxy/common_utils/resource_ownership.py``. ``None``+ already used by ``litellm_core_utils/resource_ownership.py``. ``None``means the caller has no usable identity of its own and must fall backto team scoping, or be denied."""diff --git a/litellm/llms/litellm_proxy/skills/handler.py b/litellm/llms/litellm_proxy/skills/handler.pyindex 0dbe47791fb..d56dca1699b 100644--- a/litellm/llms/litellm_proxy/skills/handler.py+++ b/litellm/llms/litellm_proxy/skills/handler.py@@ -11,18 +11,18 @@ from typing import Finalfrom litellm._logging import verbose_loggerfrom litellm.caching.in_memory_cache import InMemoryCache+from litellm.litellm_core_utils.resource_ownership import (+ get_primary_resource_owner_scope,+ get_resource_owner_scopes,+ is_proxy_admin,+ user_can_access_resource_owner,+)from litellm.llms.litellm_proxy.skills.constants import (LITELLM_SKILL_ID_PREFIX,MAX_SKILLS_PER_SEARCH,)from litellm.models.skills import LiteLLM_SkillsTable, NewSkillRequestfrom litellm.proxy._types import UserAPIKeyAuth-from litellm.proxy.common_utils.resource_ownership import (- get_primary_resource_owner_scope,- get_resource_owner_scopes,- is_proxy_admin,- user_can_access_resource_owner,-)from litellm.repositories.table_repositories import SkillsRepository# Skills are looked up on every chat completion that has skills enableddiff --git a/litellm/proxy/common_utils/resource_ownership.py b/litellm/proxy/common_utils/resource_ownership.pyindex c75a5ba8e6e..985ad8a6f77 100644--- a/litellm/proxy/common_utils/resource_ownership.py+++ b/litellm/proxy/common_utils/resource_ownership.py@@ -1,92 +1,20 @@from typing import Final-from litellm.proxy._types import LitellmUserRoles, UserAPIKeyAuth---def is_proxy_admin(user_api_key_dict: UserAPIKeyAuth | None) -> bool:- if user_api_key_dict is None:- return False-- return (- user_api_key_dict.user_role == LitellmUserRoles.PROXY_ADMIN- or user_api_key_dict.user_role == LitellmUserRoles.PROXY_ADMIN.value- )---def get_resource_owner_scopes(- user_api_key_dict: UserAPIKeyAuth | None,-) -> list[str]:- """- Return ownership scopes that may access a user-created proxy resource.-- Raw user_id is included for rows created before scope prefixes existed.- Prefixes avoid collisions when falling back to team/org/key ownership- for keys that do not have a user_id.-- Identity-less callers (no user_id, team_id, org_id, api_key, or token)- return ``[]`` — they share no scope with any other caller, so access- checks against an existing owner always fail and creates that depend- on a primary scope must reject up front. Returning a shared sentinel- here would let any two identity-less callers see each other's data.- """- if user_api_key_dict is None:- return []-- scopes: Final[list[str]] = []-- def _add(scope: str | None) -> None:- if scope and scope not in scopes:- scopes.append(scope)-- if user_api_key_dict.user_id:- _add(user_api_key_dict.user_id)- _add(f"user:{user_api_key_dict.user_id}")- if user_api_key_dict.team_id:- _add(f"team:{user_api_key_dict.team_id}")- if user_api_key_dict.org_id:- _add(f"org:{user_api_key_dict.org_id}")- if user_api_key_dict.api_key:- _add(f"key:{user_api_key_dict.api_key}")- if user_api_key_dict.token:- _add(f"key:{user_api_key_dict.token}")-- return scopes---def get_primary_resource_owner_scope(- user_api_key_dict: UserAPIKeyAuth | None,-) -> str | None:- """Return the canonical owner scope to stamp on newly-created rows.-- ``None`` for identity-less callers — callers that depend on a primary- scope to record ownership must surface that as a hard error rather- than fall back to a shared sentinel (which would collapse every- identity-less caller into the same logical owner).- """- if user_api_key_dict is None:- return None-- if user_api_key_dict.user_id:- return user_api_key_dict.user_id- if user_api_key_dict.team_id:- return f"team:{user_api_key_dict.team_id}"- if user_api_key_dict.org_id:- return f"org:{user_api_key_dict.org_id}"- if user_api_key_dict.api_key:- return f"key:{user_api_key_dict.api_key}"- if user_api_key_dict.token:- return f"key:{user_api_key_dict.token}"- return None---def user_can_access_resource_owner(- owner: str | None,- user_api_key_dict: UserAPIKeyAuth | None,-) -> bool:- if user_api_key_dict is None:- return True- if is_proxy_admin(user_api_key_dict):- return True- if owner is None:- return False- return owner in get_resource_owner_scopes(user_api_key_dict)+from litellm.litellm_core_utils.resource_ownership import (+ get_primary_resource_owner_scope,+ get_resource_owner_scopes,+ is_proxy_admin,+ user_can_access_resource_owner,+)+from litellm.types.proxy.auth.user_api_key_auth import UserAPIKeyAuth+from litellm.types.proxy.auth.user_roles import LitellmUserRoles++__all__ = [+ "Final",+ "LitellmUserRoles",+ "UserAPIKeyAuth",+ "get_primary_resource_owner_scope",+ "get_resource_owner_scopes",+ "is_proxy_admin",+ "user_can_access_resource_owner",+]diff --git a/tests/unit/proxy/common_utils/test_resource_ownership.py b/tests/unit/proxy/common_utils/test_resource_ownership.pynew file mode 100644index 00000000000..a13f66dd59c--- /dev/null+++ b/tests/unit/proxy/common_utils/test_resource_ownership.py@@ -0,0 +1,29 @@+from typing import Final++from litellm.litellm_core_utils import resource_ownership as core_resource_ownership+from litellm.proxy._types import LitellmUserRoles, UserAPIKeyAuth+from litellm.proxy.common_utils import resource_ownership as proxy_resource_ownership++PREVIOUSLY_EXPOSED_NAMES: Final = (+ "Final",+ "LitellmUserRoles",+ "UserAPIKeyAuth",+ "get_primary_resource_owner_scope",+ "get_resource_owner_scopes",+ "is_proxy_admin",+ "user_can_access_resource_owner",+)+++def test_proxy_path_reexports_core_objects() -> None:+ mismatched: Final = [+ name+ for name in PREVIOUSLY_EXPOSED_NAMES+ if getattr(proxy_resource_ownership, name) is not getattr(core_resource_ownership, name)+ ]+ assert mismatched == []+++def test_moved_module_uses_the_proxy_types_objects() -> None:+ assert core_resource_ownership.LitellmUserRoles is LitellmUserRoles+ assert core_resource_ownership.UserAPIKeyAuth is UserAPIKeyAuth