Files
fleet/server
Nico 714ae967fe Fix: SCIM user creation 500s when host already has a SCIM mapping (#44275)
**Related issue:** Resolves #43656

## Summary

When a new SCIM user is associated to a host that was previously
associated to another SCIM user (different username/email), we the
host_scim_user record is upserted with the new SCIM user's ID (instead
of 500ing).

# Checklist for submitter

- [x] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
See [Changes
files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files)
for more information.

- [x] Input data is properly validated, `SELECT *` is avoided, SQL
injection is prevented (using placeholders for values in statements), JS
inline code is prevented especially for url redirects, and untrusted
data interpolated into shell scripts/commands is validated against shell
metacharacters.

## Testing

- [x] Added/updated automated tests
- [x] QA'd all new/changed functionality manually

Setup:

- Host associated with a SCIM user. I first enrolled a Linux host with
end user authentication enabled, and logged in with
**nico+testeua@fleetdm.com**. This creates a record in the
**mdm_idp_accounts** table.
- Provisioned the **nico+testeua@fleetdm.com** user from Okta to Fleet
by following this guide:
https://fleetdm.com/guides/foreign-vitals-map-idp-users-to-hosts#step-1-create-application-in-okta.
This creates records in **scim_users** and **host_scim_user** tables.

<img width="705" height="269" alt="Screenshot 2026-05-11 at 10 22 33 AM"
src="https://github.com/user-attachments/assets/1f1a03ee-494c-4b09-a020-fe38b73a6b0b"
/>

<img width="502" height="136" alt="Screenshot 2026-05-11 at 10 26 59 AM"
src="https://github.com/user-attachments/assets/42cd84d7-0a43-4937-90ed-0dbb0066ff32"
/>




#### Before (main branch)

- Changed **username** to **nico+test500main** and **email** to
**nico+test500main@fleetdm.com** on the **mdm_idp_accounts** record from
the setup.
- Replayed the **POST /api/v1/fleet/scim/Users** request from the Setup,
but modified **username** and **email** to
**nico+test500main@fleetdm.com**. This reproduced the 500.

<img width="1201" height="599" alt="Screenshot 2026-05-11 at 10 30
26 AM"
src="https://github.com/user-attachments/assets/abd4541a-b900-4171-9cee-03c7780045a0"
/>

#### After

Performed the same steps as above, now with **nico+test500**:

Request:
<img width="1235" height="708" alt="Screenshot 2026-05-11 at 10 26
42 AM"
src="https://github.com/user-attachments/assets/ef0fde5e-7b2f-48af-ac4b-ed2897a4e54b"
/>

UI:
<img width="697" height="294" alt="Screenshot 2026-05-11 at 10 27 46 AM"
src="https://github.com/user-attachments/assets/2c033f1c-483c-43c5-b901-d949a24a8a5a"
/>

DB: **scim_users** table contains both records and **host_scim_user**
mapping was updated to the new scim_user ID.
<img width="795" height="159" alt="Screenshot 2026-05-11 at 10 26 53 AM"
src="https://github.com/user-attachments/assets/73ecfcd7-d6be-4e47-90ea-760d5f6d6cc3"
/>
<img width="501" height="134" alt="Screenshot 2026-05-11 at 10 27 05 AM"
src="https://github.com/user-attachments/assets/a24140f9-070f-4236-b80d-910a21504efd"
/>



<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **Bug Fixes**
* Fixed HTTP 500 errors on the SCIM Users endpoint when associating a
user with a host that already had an existing SCIM user mapping; host
mappings are now reassigned to the newly created SCIM user as needed.

* **Tests**
* Added a test ensuring creating a second SCIM user for the same host
succeeds and does not create duplicate host–user mappings.

[![Review Change
Stack](https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg)](https://app.coderabbit.ai/change-stack/fleetdm/fleet/pull/44275)
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-05-11 16:28:46 +02:00
..
2026-04-23 12:43:23 -05:00
2026-05-08 11:17:40 -04:00
2026-05-11 08:46:02 -05:00
2026-05-11 08:46:02 -05:00
2026-05-11 08:46:02 -05:00
2026-05-11 08:46:02 -05:00