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(rbac): narrow org-admin restriction to key generation only
Ryan Crabbe57c6a1b38b82
diff --git a/litellm/proxy/common_utils/rbac_utils.py b/litellm/proxy/common_utils/rbac_utils.pyindex 32ad199d180..0c3ddf8e6fe 100644--- a/litellm/proxy/common_utils/rbac_utils.py+++ b/litellm/proxy/common_utils/rbac_utils.py@@ -13,14 +13,6 @@ from litellm.proxy._types import LitellmUserRoles, UserAPIKeyAuthFeatureName = Literal["agents", "vector_stores"]-OrgAdminFeatureName = Literal["key_generate", "team_create", "model_add"]--_ORG_ADMIN_FEATURE_LABELS: dict[str, str] = {- "key_generate": "key generation",- "team_create": "team creation",- "model_add": "model creation",-}-async def check_feature_access_for_user(user_api_key_dict: UserAPIKeyAuth,@@ -78,12 +70,11 @@ async def check_feature_access_for_user()-async def check_org_admin_feature_access(+async def check_org_admin_can_generate_keys(user_api_key_dict: UserAPIKeyAuth,- feature_name: OrgAdminFeatureName,) -> None:"""- Raise HTTP 403 if the user is an org admin and the given feature is+ Raise HTTP 403 if the caller is an org admin and key generation isdisabled for org admins via UI settings.Only blocks the ORG_ADMIN role — proxy admins and all other roles are@@ -98,14 +89,12 @@ async def check_org_admin_feature_access(from litellm.proxy.proxy_server import general_settings- disable_flag = f"disable_{feature_name}_for_org_admin"- if not general_settings.get(disable_flag, False):+ if not general_settings.get("disable_key_generate_for_org_admin", False):return- label = _ORG_ADMIN_FEATURE_LABELS.get(feature_name, feature_name)raise HTTPException(status_code=403,detail={- "error": f"{label} is disabled for org admins. Contact your proxy admin."+ "error": "key generation is disabled for org admins. Contact your proxy admin."},)diff --git a/litellm/proxy/management_endpoints/key_management_endpoints.py b/litellm/proxy/management_endpoints/key_management_endpoints.pyindex c6701990dce..4b8cf1dbe99 100644--- a/litellm/proxy/management_endpoints/key_management_endpoints.py+++ b/litellm/proxy/management_endpoints/key_management_endpoints.py@@ -49,7 +49,7 @@ from litellm.proxy.auth.auth_checks import ()from litellm.proxy.auth.auth_utils import abbreviate_api_keyfrom litellm.proxy.auth.user_api_key_auth import user_api_key_auth-from litellm.proxy.common_utils.rbac_utils import check_org_admin_feature_access+from litellm.proxy.common_utils.rbac_utils import check_org_admin_can_generate_keysfrom litellm.proxy.common_utils.timezone_utils import get_budget_reset_timefrom litellm.proxy.hooks.key_management_event_hooks import KeyManagementEventHooksfrom litellm.proxy.management_endpoints.common_utils import (@@ -1257,9 +1257,7 @@ async def generate_key_fn(verbose_proxy_logger.debug("entered /key/generate")- await check_org_admin_feature_access(- user_api_key_dict=user_api_key_dict, feature_name="key_generate"- )+ await check_org_admin_can_generate_keys(user_api_key_dict=user_api_key_dict)# Validate budget values are not negativeif data.max_budget is not None and data.max_budget < 0:diff --git a/litellm/proxy/management_endpoints/model_management_endpoints.py b/litellm/proxy/management_endpoints/model_management_endpoints.pyindex fbba94ffa46..754727d4716 100644--- a/litellm/proxy/management_endpoints/model_management_endpoints.py+++ b/litellm/proxy/management_endpoints/model_management_endpoints.py@@ -37,7 +37,6 @@ from litellm.proxy._types import ()from litellm.proxy.auth.user_api_key_auth import user_api_key_authfrom litellm.proxy.common_utils.encrypt_decrypt_utils import encrypt_value_helper-from litellm.proxy.common_utils.rbac_utils import check_org_admin_feature_accessfrom litellm.proxy.management_endpoints.common_utils import _is_user_team_adminfrom litellm.proxy.management_endpoints.team_endpoints import (team_model_add,@@ -975,10 +974,6 @@ async def add_new_model(},)- await check_org_admin_feature_access(- user_api_key_dict=user_api_key_dict, feature_name="model_add"- )-## Auth checkawait ModelManagementAuthChecks.can_user_make_model_call(model_params=model_params,diff --git a/litellm/proxy/management_endpoints/team_endpoints.py b/litellm/proxy/management_endpoints/team_endpoints.pyindex a6b034720f3..1ef40f0685f 100644--- a/litellm/proxy/management_endpoints/team_endpoints.py+++ b/litellm/proxy/management_endpoints/team_endpoints.py@@ -70,7 +70,6 @@ from litellm.proxy.auth.auth_checks import (get_user_object,)from litellm.proxy.auth.user_api_key_auth import user_api_key_auth-from litellm.proxy.common_utils.rbac_utils import check_org_admin_feature_accessfrom litellm.proxy.management_endpoints.common_utils import (_is_user_org_admin_for_team,_is_user_team_admin,@@ -897,10 +896,6 @@ async def new_team( # noqa: PLR0915if prisma_client is None:raise HTTPException(status_code=500, detail={"error": "No db connected"})- await check_org_admin_feature_access(- user_api_key_dict=user_api_key_dict, feature_name="team_create"- )-# Validate budget values are not negativeif data.max_budget is not None and data.max_budget < 0:raise HTTPException(diff --git a/litellm/proxy/ui_crud_endpoints/proxy_setting_endpoints.py b/litellm/proxy/ui_crud_endpoints/proxy_setting_endpoints.pyindex 6b175f940c3..2c07d0927b6 100644--- a/litellm/proxy/ui_crud_endpoints/proxy_setting_endpoints.py+++ b/litellm/proxy/ui_crud_endpoints/proxy_setting_endpoints.py@@ -157,16 +157,6 @@ class UISettings(BaseModel):description="If true, org admins cannot generate API keys via /key/generate.",)- disable_team_create_for_org_admin: bool = Field(- default=False,- description="If true, org admins cannot create teams via /team/new.",- )-- disable_model_add_for_org_admin: bool = Field(- default=False,- description="If true, org admins cannot add models via /model/new.",- )-class UISettingsResponse(SettingsResponse):"""Response model for UI settings"""@@ -190,8 +180,6 @@ ALLOWED_UI_SETTINGS_FIELDS = {"scope_user_search_to_org","disable_custom_api_keys","disable_key_generate_for_org_admin",- "disable_team_create_for_org_admin",- "disable_model_add_for_org_admin",}# Flags that must be synced from the persisted UISettings into@@ -204,8 +192,6 @@ _RUNTIME_GENERAL_SETTINGS_FLAGS = ["disable_vector_stores_for_internal_users","allow_vector_stores_for_team_admins","disable_key_generate_for_org_admin",- "disable_team_create_for_org_admin",- "disable_model_add_for_org_admin",]diff --git a/tests/litellm/proxy/common_utils/test_rbac_utils.py b/tests/litellm/proxy/common_utils/test_rbac_utils.pyindex 9adc34ed407..54b465a94ba 100644--- a/tests/litellm/proxy/common_utils/test_rbac_utils.py+++ b/tests/litellm/proxy/common_utils/test_rbac_utils.py@@ -2,7 +2,7 @@Tests for litellm/proxy/common_utils/rbac_utils.pyCovers check_feature_access_for_user for agents and vector_stores features,-plus check_org_admin_feature_access for key/team/model creation.+plus check_org_admin_can_generate_keys for key generation gating."""from unittest.mock import AsyncMock, patch@@ -13,7 +13,7 @@ from fastapi import HTTPExceptionfrom litellm.proxy._types import LitellmUserRoles, UserAPIKeyAuthfrom litellm.proxy.common_utils.rbac_utils import (check_feature_access_for_user,- check_org_admin_feature_access,+ check_org_admin_can_generate_keys,)@@ -185,35 +185,23 @@ async def test_vector_stores_disabled_non_team_admin_blocked():# ----------------------------------------------------------------------------# check_org_admin_feature_access+# check_org_admin_can_generate_keys# ---------------------------------------------------------------------------- "feature_name",- ["key_generate", "team_create", "model_add"],-)@pytest.mark.asyncio-async def test_org_admin_feature_not_disabled_allows_org_admin(feature_name):+async def test_org_admin_key_generate_not_disabled_allows_org_admin():user = _make_user(LitellmUserRoles.ORG_ADMIN.value)with patch.dict(_GS_PATH, {}, clear=True):- await check_org_admin_feature_access(user, feature_name)+ await check_org_admin_can_generate_keys(user)- "feature_name,flag_name",- [- ("key_generate", "disable_key_generate_for_org_admin"),- ("team_create", "disable_team_create_for_org_admin"),- ("model_add", "disable_model_add_for_org_admin"),- ],-)@pytest.mark.asyncio-async def test_org_admin_feature_disabled_blocks_org_admin(feature_name, flag_name):+async def test_org_admin_key_generate_disabled_blocks_org_admin():user = _make_user(LitellmUserRoles.ORG_ADMIN.value)- with patch.dict(_GS_PATH, {flag_name: True}, clear=True):+ with patch.dict(_GS_PATH, {"disable_key_generate_for_org_admin": True}, clear=True):with pytest.raises(HTTPException) as exc_info:- await check_org_admin_feature_access(user, feature_name)+ await check_org_admin_can_generate_keys(user)assert exc_info.value.status_code == 403@@ -228,24 +216,18 @@ async def test_org_admin_feature_disabled_blocks_org_admin(feature_name, flag_na],)@pytest.mark.asyncio-async def test_org_admin_disable_flag_does_not_affect_other_roles(role):- """Only ORG_ADMIN should be gated by these flags — other roles pass through."""+async def test_key_generate_disable_flag_does_not_affect_other_roles(role):+ """Only ORG_ADMIN is gated by this flag — other roles pass through."""user = _make_user(role)with patch.dict(_GS_PATH,- {- "disable_key_generate_for_org_admin": True,- "disable_team_create_for_org_admin": True,- "disable_model_add_for_org_admin": True,- },+ {"disable_key_generate_for_org_admin": True},clear=True,):# No exception expected — these roles are not org admins, so the flag# should be a no-op. Other auth checks (in the endpoint itself) still# apply.- await check_org_admin_feature_access(user, "key_generate")- await check_org_admin_feature_access(user, "team_create")- await check_org_admin_feature_access(user, "model_add")+ await check_org_admin_can_generate_keys(user)@pytest.mark.asyncio@@ -254,10 +236,10 @@ async def test_org_admin_role_enum_and_string_both_blocked():with patch.dict(_GS_PATH, {"disable_key_generate_for_org_admin": True}, clear=True):user_str = _make_user(LitellmUserRoles.ORG_ADMIN.value)with pytest.raises(HTTPException):- await check_org_admin_feature_access(user_str, "key_generate")+ await check_org_admin_can_generate_keys(user_str)user_enum = UserAPIKeyAuth(user_role=LitellmUserRoles.ORG_ADMIN, user_id="user-1")with pytest.raises(HTTPException):- await check_org_admin_feature_access(user_enum, "key_generate")+ await check_org_admin_can_generate_keys(user_enum)diff --git a/tests/test_litellm/proxy/ui_crud_endpoints/test_proxy_setting_endpoints.py b/tests/test_litellm/proxy/ui_crud_endpoints/test_proxy_setting_endpoints.pyindex 1535acaadd2..d6162eba27f 100644--- a/tests/test_litellm/proxy/ui_crud_endpoints/test_proxy_setting_endpoints.py+++ b/tests/test_litellm/proxy/ui_crud_endpoints/test_proxy_setting_endpoints.py@@ -1098,18 +1098,10 @@ class TestProxySettingEndpoints:assert response.status_code == 200assert general_settings.get("forward_llm_provider_auth_headers") is True- @pytest.mark.parametrize(- "flag_name",- [- "disable_key_generate_for_org_admin",- "disable_team_create_for_org_admin",- "disable_model_add_for_org_admin",- ],- )- def test_update_ui_settings_persists_and_syncs_org_admin_flags(- self, mock_auth, monkeypatch, flag_name+ def test_update_ui_settings_persists_and_syncs_disable_key_generate_for_org_admin(+ self, mock_auth, monkeypatch):- """Org-admin restriction flags must be allowlisted, persisted, and synced to general_settings."""+ """disable_key_generate_for_org_admin must be allowlisted, persisted, and synced to general_settings."""from unittest.mock import AsyncMock, MagicMockfrom litellm.proxy._types import UserAPIKeyAuth@@ -1133,6 +1125,7 @@ class TestProxySettingEndpoints:mock_prisma.db.litellm_uisettings.find_unique = AsyncMock(return_value=None)monkeypatch.setattr("litellm.proxy.proxy_server.prisma_client", mock_prisma)+ flag_name = "disable_key_generate_for_org_admin"payload = {flag_name: True}try: