Repository navigation
fix(scim): sync team roster and dedup teams for existing-user email upsert - #34183
yassin-berriai merged 3 commits into
Conversation
…psert When POST /scim/v2/Users matched an already-existing user by email, handle_existing_user_by_email raw-wrote the user's teams array but never touched the team roster, so the user appeared in the group on their profile yet was absent from the team directly (members_with_roles stayed empty). It also did not dedup the teams built from repeated SCIM groups. Route the existing-user team assignment through the same _handle_team_membership_changes / team_member_add path the PUT update_user handler uses, so members_with_roles, LiteLLM_TeamMembership, and the user's teams array stay in sync, and dedup the teams derived from user.groups. The user_id rewrite to the new userName is preserved and sequenced before the roster sync so the roster never references a stale primary key.
|
|
Greptile SummaryThis PR keeps SCIM user teams and team rosters in sync during existing-email upserts. The main changes are:
Confidence Score: 5/5This looks safe to merge.
|
| Filename | Overview |
|---|---|
| litellm/proxy/management_endpoints/scim/scim_v2.py | Adds deduplicated roster reconciliation and strict, symmetric error propagation for existing-email upserts. |
| tests/test_litellm/proxy/management_endpoints/scim/test_scim_v2_endpoints.py | Adds tests for roster synchronization, duplicate teams, genuine failures, and idempotent membership operations. |
Reviews (4): Last reviewed commit: "fix(scim): surface roster removal failur..." | Re-trigger Greptile
Greptile SummaryThis PR synchronizes team rosters during existing-email SCIM user upserts. The main changes are:
Confidence Score: 4/5The existing-email upsert can leave the user record and team roster inconsistent when a write fails.
litellm/proxy/management_endpoints/scim/scim_v2.py
|
| Filename | Overview |
|---|---|
| litellm/proxy/management_endpoints/scim/scim_v2.py | Adds team deduplication and roster synchronization, but the separate writes and suppressed membership failures can leave inconsistent state. |
| tests/test_litellm/proxy/management_endpoints/scim/test_scim_v2_endpoints.py | Adds focused assertions for deduplication, rename order, and membership-helper invocation. |
Reviews (1): Last reviewed commit: "fix(scim): sync team roster and dedup te..." | Re-trigger Greptile
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Route the existing-email upsert's roster sync through patch_team_membership with a new opt-in raise_on_error flag so a genuine team_member_add failure propagates instead of being swallowed, and the deduped teams array is only persisted after the roster sync succeeds. Without this, a failed add left the endpoint reporting success while user.teams listed a team members_with_roles never received. The benign already-a-member case stays a no-op even under the strict path, and the flag defaults to False so the PUT update_user, PATCH patch_user, and group callers keep their existing best-effort behavior. SCIM POST is idempotent, so surfacing the error lets the IdP retry and converge.
|
@greptileai please review the current head d498cf0 |
| if new_user_request.user_id != existing_user.user_id: | ||
| await UserRepository(prisma_client).table.update( | ||
| where={"user_id": existing_user.user_id}, | ||
| data={"user_id": new_user_request.user_id}, | ||
| ) |
There was a problem hiding this comment.
Rename remains partially committed
This update commits the new user ID before roster synchronization and the final field update. If synchronization fails, the request returns an error after the ID changed, leaving the user's previous teams and profile fields in place. If synchronization succeeds but the final update fails, the rename and roster changes remain committed without the authoritative teams write. These operations need a shared transaction or explicit rollback so later failures cannot leave partial state.
Make team_member_delete failures fail loud under the strict roster sync used by the existing-email upsert, mirroring the add path, so a swallowed removal can no longer let the user's teams array drop a team the roster still holds. The idempotent case where the user is already absent from the team stays a no-op, matching how an add treats the user already being in the team. Best-effort behavior is preserved for the default raise_on_error=False callers.
|
Addressed the removal half of the roster-consistency finding in aeb2240: On the remaining point (the user-ID rename committing before the later writes): closing that fully means wrapping the primary-key rename and its foreign-key cascade in a single transaction. That rename orphaning is not introduced here; the prior code performed the same single-write rename, and any reordering within this function that renames without cascading the roster keys would orphan |
|
@greptileai please review the current head aeb2240 |
Relevant issues
Follow-up to #34162
Linear ticket
Part of LIT-4676
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
@greptileaito re-request a review after pushing changes)Delays in PR merge?
If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).
Screenshots / Proof of Fix
Independent e2e (Devin): existing-user-by-email POST with duplicated groups dedups user.teams and syncs the roster (members_with_roles populated).
Admin UI user detail Teams panel: the team is listed once (parent listed it twice, with an empty roster).
SCIM management endpoints, no LLM call needed. Live proxy on localhost:4290 backed by a real Postgres 16,
litellm_settings.scim_upsert_user: trueRepro: create user
ua(emaile@t.com), create teamTvia POST /scim/v2/Groups with an empty roster, then POST /scim/v2/Users with a different userName but the same email andgroups=[{value: T}]. The user then lists T on their profile but is absent from T's rosterBefore (unfixed, commit
2b2ae4ca49)Database and SCIM API afterward show the user in the group but not in the roster:
After (fixed, commit
e5f9d1e10c)Same flow, plus a deliberately duplicated group in the POST to exercise dedup (
groups=[{value:T-fix},{value:T-fix}]):Database and SCIM API afterward show the user in the roster, teams deduped, and the old primary key renamed away:
A
LiteLLM_TeamMembershiprow is only written when a team budget applies, which matches the existing PUTupdate_userbehavior this change now reuses;members_with_rolesis the roster source of truth and is now correct.Roster-sync failure now surfaces (fixed, commit
d498cf07de)The roster sync used to swallow genuine
team_member_addfailures, so a failed add still reported success and persisted a teams array the roster never received. The upsert now runs the sync strictly and only persists the deduped teams once it succeeds. A POST whose group points at a team that does not exist returns an error instead of a false 201, and the user's teams array is left untouched so it cannot diverge from the roster; the IdP retries and converges:The happy path (a group pointing at a real team) still returns 201 and adds the user to
members_with_roles, verified on the same head. The benign "already a member" case stays a no-opType
🐛 Bug Fix
Changes
UserProvisionerHelpers.handle_existing_user_by_emailnow routes the existing-user team assignment through the same_handle_team_membership_changeshelper the PUTupdate_userhandler already uses, somembers_with_roles,LiteLLM_TeamMembership, and the user'steamsarray stay in sync instead of the roster being silently skipped. The teams derived fromuser.groupsare deduped before they are written or diffedThe roster sync runs strictly for this path via a new opt-in
raise_on_errorflag onpatch_team_membershipand_handle_team_membership_changes: a genuine add or remove failure propagates so the deduped teams array is only persisted once the roster actually received the change. Adds and removes are treated symmetrically; the idempotent no-ops (the user is already in the team on add, or already absent on remove) stay no-ops even under the strict path. The flag defaults to False, so the PUTupdate_user, PATCHpatch_user, and group callers keep their existing best-effort behaviorThe user_id rewrite to the new userName is left intact so this stays a single isolated fix; it is sequenced as its own update before the roster sync so the roster keys off the final primary key, and the authoritative field write runs last so the returned SCIM user and the persisted
teamsarray reflect the deduped set. On a mid-flight failure the endpoint returns an error and the IdP retries; because the second attempt finds the already-renamed user by email, the rename is idempotent and the teams array and roster converge without diverging. Making the primary-key rename plus its foreign-key cascade a single atomic transaction is a broader concern that predates this change (the prior code renamed the same way) and belongs with the transactional-write work, so it is intentionally out of scope hereFinal Attestation