Skip to content

Fix to populate host_emails during Linux/Windows SSO enrollment - #45951

Merged
lucasmrod merged 4 commits into
mainfrom
45066-windows-idp-device-mapping
May 21, 2026
Merged

Fix to populate host_emails during Linux/Windows SSO enrollment#45951
lucasmrod merged 4 commits into
mainfrom
45066-windows-idp-device-mapping

Conversation

@lucasmrod

@lucasmrod lucasmrod commented May 21, 2026

Copy link
Copy Markdown
Member

Resolves #45066.

  • Changes file added for user-visible changes in changes/, orbit/changes/ or ee/fleetd-chrome/changes.

Testing

  • Added/updated automated tests
  • QA'd all new/changed functionality manually

Summary by CodeRabbit

  • Bug Fixes

    • Fixed user email/device mapping for Windows and Linux hosts enrolling via end-user authentication (browser-based and EUA-token flows). Host listings and device mapping now reliably show the IdP email after enrollment, improving device identification and inventory accuracy.
  • Tests

    • Expanded integration tests to validate end-to-end enrollment and device mapping across platforms.

Review Change Stack

Copilot AI review requested due to automatic review settings May 21, 2026 00:14
@lucasmrod
lucasmrod requested a review from a team as a code owner May 21, 2026 00:14

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This repository is configured for manual code reviews. Comment @claude review to trigger a review and subscribe this PR to future pushes, or @claude review once for a one-time review.

Tip: disable this comment in your organization's Code Review settings.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes missing device_mapping (specifically mdm_idp_accounts/user email) for newly enrolled Linux/Windows hosts that go through End User Authentication (EUA) during Orbit enrollment, aligning List hosts?device_mapping=true behavior with Get host for the same IdP-linked hosts (issue #45066).

Changes:

  • Defers Windows MSI EUA token processing to return the IdP account UUID, and performs IdP association + host email reconciliation after the hosts row exists.
  • Adds a post-enrollment reconciliation step in EnrollOrbit to populate host_emails from host_mdm_idp_accounts for Linux/Windows EUA enrollments.
  • Adds enterprise integration tests to cover both the browser-based Orbit Setup Experience SSO callback flow and the Windows MSI EUA-token flow.

Reviewed changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
server/service/orbit.go Defers Windows token association and adds post-enrollment reconciliation to ensure host_emails is populated for device mapping.
server/service/orbit_eua_test.go Updates unit tests to match the new processWindowsEUAToken contract (returns IdP UUID; no association side-effect).
server/service/integration_enterprise_test.go Adds end-to-end tests proving device_mapping is populated for Linux/Windows EUA enrollment flows, including Windows EUA token.
server/datastore/mysql/mdm_idp_accounts_test.go Extends reconciliation coverage to include Windows/Linux platforms.
changes/45066-windows-idp-device-mapping Adds user-visible changelog entry for the fix.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread server/service/orbit.go Outdated
Comment thread server/service/orbit.go
@coderabbitai

coderabbitai Bot commented May 21, 2026

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 751ef8a4-1069-4ff7-af7c-5494bcda3983

📥 Commits

Reviewing files that changed from the base of the PR and between 1618a1f and 60cce52.

📒 Files selected for processing (1)
  • server/datastore/mysql/hosts.go

Walkthrough

This PR ensures device_mapping includes IdP email for Windows and Linux EUA enrollments. processWindowsEUAToken now returns an IdP account UUID (UPN, device ID, idpAcctUUID) but defers associating it to a host. EnrollOrbit captures that UUID, persists the host (reads back hosts.uuid), and runs a post-enrollment reconciliation on Linux/Windows to associate the host with mdm_idp_accounts. Integration tests exercise both browser EUA and EUA-token flows; unit tests expand reconciliation coverage and a changelog entry was added.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: fixing host_emails population during Linux/Windows SSO enrollment, which directly addresses the linked issue #45066.
Description check ✅ Passed The description includes the related issue number (#45066) and documents that a changes file was added and tests were included. However, it lacks detailed explanation of the technical implementation and testing rationale.
Linked Issues check ✅ Passed The PR addresses all coding requirements from issue #45066: it populates host_emails/mdm_idp_accounts in device_mapping during Windows/Linux SSO enrollment by deferring IdP account association to post-enrollment and reconciling the host-to-IdP account mapping [#45066].
Out of Scope Changes check ✅ Passed All changes are directly scoped to resolving issue #45066: updating tests for cross-platform coverage, implementing IdP account reconciliation post-enrollment, and ensuring host UUID consistency during enrollment.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch 45066-windows-idp-device-mapping

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
server/service/integration_enterprise_test.go (1)

30761-30766: ⚡ Quick win

Assert the hosts list API response here too.

The regression is about the hosts list endpoint returning device_mapping=null, but these checks only validate s.ds.ListHostDeviceMapping. An API-layer regression in handler wiring or response serialization would still pass. Please add an assertion against the hosts list endpoint with device_mapping=true in both flows.

Also applies to: 30896-30900

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@server/service/integration_enterprise_test.go` around lines 30761 - 30766,
The test currently only checks the datastore via s.ds.ListHostDeviceMapping but
must also assert the hosts list API surface returns device_mapping=true; after
the existing datastore assertions (the s.ds.ListHostDeviceMapping check that
confirms dms[0].Email == idpEmail and dms[0].Source ==
fleet.DeviceMappingMDMIdpAccounts), add an assertion that calls the hosts list
handler/API used elsewhere in tests (the same endpoint exercised in the
integration test) and verify the returned host object(s) include device_mapping:
true (do this in both places referenced by the reviewer, i.e., the block around
the s.ds.ListHostDeviceMapping assertion and the other similar block at the
later location).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@changes/45066-windows-idp-device-mapping`:
- Line 1: Update the changelog entry to explicitly mention that the fix
populates the device_mapping (specifically the mdm_idp_accounts field) for
Windows and Linux hosts enrolled with end-user authentication so that the User
email is correctly returned by GET /api/v1/fleet/hosts; reference
"device_mapping" and "mdm_idp_accounts" in the description to improve clarity
and align with the linked issue.

In `@server/service/orbit.go`:
- Around line 296-304: The lookup for the host's IdP account
(svc.ds.GetMDMIdPAccountByHostUUID) must be forced to read from the primary DB
when idpAcctUUID == "" to avoid replica lag causing missing rows; change the
call to use a context that enforces primary/writer reads (e.g., create a
primaryCtx and pass that into svc.ds.GetMDMIdPAccountByHostUUID) so that the
recently-written SSO callback row is visible and AssociateHostMDMIdPAccount is
not skipped.

---

Nitpick comments:
In `@server/service/integration_enterprise_test.go`:
- Around line 30761-30766: The test currently only checks the datastore via
s.ds.ListHostDeviceMapping but must also assert the hosts list API surface
returns device_mapping=true; after the existing datastore assertions (the
s.ds.ListHostDeviceMapping check that confirms dms[0].Email == idpEmail and
dms[0].Source == fleet.DeviceMappingMDMIdpAccounts), add an assertion that calls
the hosts list handler/API used elsewhere in tests (the same endpoint exercised
in the integration test) and verify the returned host object(s) include
device_mapping: true (do this in both places referenced by the reviewer, i.e.,
the block around the s.ds.ListHostDeviceMapping assertion and the other similar
block at the later location).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: c34f1181-dc7f-4d3c-93a7-c623c49cb7ea

📥 Commits

Reviewing files that changed from the base of the PR and between 48b32e6 and 62bf956.

📒 Files selected for processing (5)
  • changes/45066-windows-idp-device-mapping
  • server/datastore/mysql/mdm_idp_accounts_test.go
  • server/service/integration_enterprise_test.go
  • server/service/orbit.go
  • server/service/orbit_eua_test.go

Comment thread changes/45066-windows-idp-device-mapping Outdated
Comment thread server/service/orbit.go
@codecov

codecov Bot commented May 21, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 53.33333% with 14 lines in your changes missing coverage. Please review.
✅ Project coverage is 66.80%. Comparing base (60fca4b) to head (60cce52).
⚠️ Report is 43 commits behind head on main.

Files with missing lines Patch % Lines
server/service/orbit.go 57.14% 9 Missing and 3 partials ⚠️
server/datastore/mysql/hosts.go 0.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #45951      +/-   ##
==========================================
+ Coverage   66.78%   66.80%   +0.02%     
==========================================
  Files        2751     2754       +3     
  Lines      219892   220115     +223     
  Branches    11008    11008              
==========================================
+ Hits       146848   147050     +202     
- Misses      59771    59772       +1     
- Partials    13273    13293      +20     
Flag Coverage Δ
backend 68.63% <53.33%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@lucasmrod
lucasmrod marked this pull request as draft May 21, 2026 00:43
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
@lucasmrod
lucasmrod marked this pull request as ready for review May 21, 2026 14:54

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@server/datastore/mysql/hosts.go`:
- Around line 2339-2349: The returned host struct is being forced to
hostInfo.HardwareUUID (host.UUID = hostInfo.HardwareUUID) even when the UPDATE
path preserves a different non-empty database UUID via COALESCE(NULLIF(uuid,
''), ?), leading to a divergence when matching was done by osquery_host_id;
change the logic in the enrollment/update flow (around the
matchHostDuringEnrollment handling and the code that sets host.UUID) to read and
return the effective UUID that was actually persisted: after performing the
INSERT/UPDATE, query or capture the resulting hosts.uuid value (or use the DB
RETURNING/SELECT of COALESCE(NULLIF(uuid, ''), ?)) and set host.UUID to that
persisted value instead of unconditionally using hostInfo.HardwareUUID so the
returned host.UUID matches the database.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: e7f12af1-9384-4a25-bac7-e8ac780e88b2

📥 Commits

Reviewing files that changed from the base of the PR and between 62bf956 and 1618a1f.

📒 Files selected for processing (3)
  • changes/45066-windows-idp-device-mapping
  • server/datastore/mysql/hosts.go
  • server/service/orbit.go
✅ Files skipped from review due to trivial changes (1)
  • changes/45066-windows-idp-device-mapping

Comment thread server/datastore/mysql/hosts.go Outdated

@nulmete nulmete left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, holding off approval until coderabbit's comment is resolved.

@lucasmrod
lucasmrod requested a review from nulmete May 21, 2026 19:04
@lucasmrod
lucasmrod merged commit aeac57b into main May 21, 2026
42 checks passed
@lucasmrod
lucasmrod deleted the 45066-windows-idp-device-mapping branch May 21, 2026 19:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Newly enrolled Windows and Linux devices missing mdm_idp_accounts in device_mapping with List hosts API endpoint

3 participants