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
Fix sso users not added to entra synced team (#17331)
rioiart98a244450e1d
diff --git a/litellm/proxy/management_endpoints/ui_sso.py b/litellm/proxy/management_endpoints/ui_sso.pyindex a033e2cf5f4..59a93f3c486 100644--- a/litellm/proxy/management_endpoints/ui_sso.py+++ b/litellm/proxy/management_endpoints/ui_sso.py@@ -435,9 +435,9 @@ async def add_missing_team_member(- Get missing teams (diff b/w user_info.team_ids and sso_teams)- Add missing user to missing teams"""- if user_info.teams is None:- return- missing_teams = set(sso_teams) - set(user_info.teams)+ # Handle None as empty list for new users+ user_teams = user_info.teams if user_info.teams is not None else []+ missing_teams = set(sso_teams) - set(user_teams)missing_teams_list = list(missing_teams)tasks = []tasks = [diff --git a/tests/test_litellm/proxy/management_endpoints/test_ui_sso.py b/tests/test_litellm/proxy/management_endpoints/test_ui_sso.pyindex f01813fa587..8d7aa51fa0f 100644--- a/tests/test_litellm/proxy/management_endpoints/test_ui_sso.py+++ b/tests/test_litellm/proxy/management_endpoints/test_ui_sso.py@@ -16,7 +16,7 @@ sys.path.insert() # Adds the parent directory to the system pathimport litellm-from litellm.proxy._types import NewTeamRequest+from litellm.proxy._types import LiteLLM_UserTable, NewTeamRequest, NewUserResponsefrom litellm.proxy.auth.handle_jwt import JWTHandlerfrom litellm.proxy.management_endpoints.types import CustomOpenIDfrom litellm.proxy.management_endpoints.ui_sso import (@@ -2573,3 +2573,210 @@ class TestPKCEFunctionality:assert "code_challenge=" in updated_locationassert "code_challenge_method=S256" in updated_locationassert f"state={test_state}" in updated_location+++# Tests for SSO user team assignment bug (Issue: SSO Users Not Added to Entra-Synced Teams on First Login)+class TestAddMissingTeamMember:+ """Tests for the add_missing_team_member function"""++ @pytest.mark.asyncio+ async def test_add_missing_team_member_with_new_user_response_teams_none(self):+ """+ Bug reproduction: When a NewUserResponse has teams=None (new SSO user),+ add_missing_team_member() should still add the user to the SSO teams.++ Currently FAILS: The function returns early when teams is None.+ """+ from litellm.proxy._types import NewUserResponse+ from litellm.proxy.management_endpoints.ui_sso import add_missing_team_member++ # Simulate a new SSO user - NewUserResponse has teams=None by default+ new_user = NewUserResponse(+ user_id="new-sso-user-123",+ key="sk-xxxxx",+ teams=None, # This is the default for NewUserResponse+ )++ sso_teams = ["team-from-entra-1", "team-from-entra-2"]++ with patch(+ "litellm.proxy.management_endpoints.ui_sso.create_team_member_add_task"+ ) as mock_add_task:+ mock_add_task.return_value = AsyncMock()++ await add_missing_team_member(user_info=new_user, sso_teams=sso_teams)++ # Bug: This assertion currently FAILS - no teams are added+ # because function returns early when teams is None+ assert (+ mock_add_task.call_count == 2+ ), f"Expected 2 calls to add user to teams, but got {mock_add_task.call_count}"+ called_team_ids = [call.args[0] for call in mock_add_task.call_args_list]+ assert set(called_team_ids) == {+ "team-from-entra-1",+ "team-from-entra-2",+ }++ @pytest.mark.asyncio+ async def test_add_missing_team_member_with_litellm_user_table_empty_teams(self):+ """+ Control test: When a LiteLLM_UserTable has teams=[] (existing user, no teams),+ add_missing_team_member() should add the user to SSO teams.++ This test PASSES because LiteLLM_UserTable defaults teams to [] not None.+ """+ from litellm.proxy._types import LiteLLM_UserTable+ from litellm.proxy.management_endpoints.ui_sso import add_missing_team_member++ # Existing user has teams=[] by default (not None)+ existing_user = LiteLLM_UserTable(+ user_id="existing-user-456",+ teams=[], # Empty list, not None+ )++ sso_teams = ["team-from-entra-1", "team-from-entra-2"]++ with patch(+ "litellm.proxy.management_endpoints.ui_sso.create_team_member_add_task"+ ) as mock_add_task:+ mock_add_task.return_value = AsyncMock()++ await add_missing_team_member(user_info=existing_user, sso_teams=sso_teams)++ # This PASSES - teams are added because teams=[] not None+ assert mock_add_task.call_count == 2++ @pytest.mark.asyncio+ async def test_add_user_to_teams_from_sso_response_new_user(self):+ """+ Integration test: Simulates the SSO response handler with a new user+ that has teams=None from NewUserResponse.+ """+ from litellm.proxy._types import NewUserResponse+ from litellm.proxy.management_endpoints.types import CustomOpenID+ from litellm.proxy.management_endpoints.ui_sso import (+ SSOAuthenticationHandler,+ )++ # SSO response with team_ids from Entra ID+ sso_result = CustomOpenID(+ id="new-sso-user-id",+ email="[email protected]",+ team_ids=["entra-group-1", "entra-group-2"],+ )++ # New user response (simulates what new_user() returns)+ new_user_info = NewUserResponse(+ user_id="new-sso-user-id",+ key="sk-xxxxx",+ teams=None, # Bug: NewUserResponse defaults to None+ )++ with patch(+ "litellm.proxy.management_endpoints.ui_sso.add_missing_team_member"+ ) as mock_add_member:+ await SSOAuthenticationHandler.add_user_to_teams_from_sso_response(+ result=sso_result,+ user_info=new_user_info,+ )++ # Verify add_missing_team_member was called with correct args+ mock_add_member.assert_called_once_with(+ user_info=new_user_info, sso_teams=["entra-group-1", "entra-group-2"]+ )++ @pytest.mark.asyncio+ async def test_sso_first_login_full_flow_adds_user_to_teams(self):+ """+ End-to-end test: Simulates complete first-time SSO login with Entra groups.+ Verifies teams are created AND user is added as a member.+ """+ from litellm.proxy._types import NewUserResponse+ from litellm.proxy.management_endpoints.ui_sso import add_missing_team_member++ team_member_calls = []++ async def track_team_member_add(team_id, user_info):+ team_member_calls.append(+ {"team_id": team_id, "user_id": user_info.user_id}+ )++ # New SSO user with Entra groups+ new_user = NewUserResponse(+ user_id="first-time-sso-user",+ key="sk-xxxxx",+ teams=None, # The problematic default+ )++ sso_teams = ["entra-team-alpha", "entra-team-beta"]++ with patch(+ "litellm.proxy.management_endpoints.ui_sso.create_team_member_add_task",+ side_effect=track_team_member_add,+ ):+ await add_missing_team_member(user_info=new_user, sso_teams=sso_teams)++ # Bug: With current code, team_member_calls will be empty+ # After fix: Should have 2 entries+ assert (+ len(team_member_calls) == 2+ ), f"Expected 2 teams to be added, but got {len(team_member_calls)}"+ assert {c["team_id"] for c in team_member_calls} == {+ "entra-team-alpha",+ "entra-team-beta",+ }+ assert all(c["user_id"] == "first-time-sso-user" for c in team_member_calls)++ @pytest.mark.asyncio+ @pytest.mark.parametrize(+ "user_info_factory,teams_value,expected_teams_added",+ [+ # Bug case: NewUserResponse with teams=None+ pytest.param(+ lambda uid: NewUserResponse(user_id=uid, key="sk-xxx", teams=None),+ None,+ ["team-1", "team-2"], # Should still add teams+ id="new_user_teams_none",+ ),+ # Working case: LiteLLM_UserTable with teams=[]+ pytest.param(+ lambda uid: LiteLLM_UserTable(user_id=uid, teams=[]),+ [],+ ["team-1", "team-2"],+ id="existing_user_empty_teams",+ ),+ # Existing user with some teams already+ pytest.param(+ lambda uid: LiteLLM_UserTable(user_id=uid, teams=["team-1"]),+ ["team-1"],+ ["team-2"], # Only missing team should be added+ id="existing_user_partial_teams",+ ),+ ],+ )+ async def test_add_missing_team_member_handles_all_user_types(+ self, user_info_factory, teams_value, expected_teams_added+ ):+ """+ Parametrized test ensuring add_missing_team_member works for all user types.+ """+ from litellm.proxy._types import LiteLLM_UserTable+ from litellm.proxy.management_endpoints.ui_sso import add_missing_team_member++ user_info = user_info_factory("test-user-id")+ sso_teams = ["team-1", "team-2"]++ added_teams = []++ async def mock_create_task(team_id, user):+ added_teams.append(team_id)++ with patch(+ "litellm.proxy.management_endpoints.ui_sso.create_team_member_add_task",+ side_effect=mock_create_task,+ ):+ await add_missing_team_member(user_info=user_info, sso_teams=sso_teams)++ assert set(added_teams) == set(+ expected_teams_added+ ), f"Expected teams {expected_teams_added}, but got {added_teams}"