fleet icon indicating copy to clipboard operation
fleet copied to clipboard

Clear state on Android host re-enrollment

Open melpike opened this issue 7 months ago • 9 comments

Goal

User story
As an IT admin,
I want Fleet to clear labels, other non-osquery vitals, and cancel pending commands, scripts, software when a Android host re-enrolls
so that I don't have to delete the host to make sure it gets the right configuration profiles and isn't accidentally locked when it re-enrolls.

Changes

Product

  • [ ] Changes: On Android re-enroll clear:
    • Labels
      • For all platforms, we want to keep manual label membership intact, otherwise, clear.
    • Other non-osquery vitals
    • Cancel pending commands, scripts, software
  • [ ] UI changes: No changes
  • [ ] CLI (fleetctl) usage changes: No changes
  • [ ] YAML changes: No changes
  • [ ] REST API changes: No changes
  • [ ] Fleet's agent (fleetd) changes: No changes
  • [ ] Fleet server configuration changes: No changes
  • [ ] Exposed, public API endpoint changes: No changes
  • [ ] fleetdm.com changes: No changes
  • [ ] GitOps mode UI changes: No changes
  • [ ] GitOps generation changes: No changes
  • [ ] Activity changes: No changes
  • [ ] Permissions changes: No changes
  • [ ] Changes to paid features or tiers: No changes
  • [ ] My device and fleetdm.com/better changes: No changes
  • [ ] Usage statistics: No changes
  • [ ] Other reference documentation changes: No changes
  • [ ] First draft of test plan added
  • [ ] Once shipped, requester has been notified
  • [ ] Once shipped, dogfooding issue has been filed: https://github.com/fleetdm/fleet/issues/54550

Engineering

Sub-tasks:

  • #50174 — Datastore reset + Pub/Sub wiring (backend)

  • #50175 — Documentation and engineering QA

  • [x] Test plan is finalized

  • [x] Contributor API changes: No changes

  • [x] Feature guide changes: No changes

  • [x] Database schema migrations: No changes — the reset only deletes/updates rows in existing tables; the preserve_host_activities_on_reenrollment config already exists.

  • [x] Load testing: Not required — the reset runs once per re-enroll inside the existing Pub/Sub transaction, touching only host-scoped rows via indexed keys; no steady-state load change.

  • [x] Pre-QA load test: Not required (same rationale).

  • [x] Load testing/osquery-perf improvements: None.

  • [x] This is a premium only feature: No

ℹ️  Please read this issue carefully and understand it. Pay special attention to UI wireframes, especially "dev notes".

Risk assessment

  • Requires testing in a hosted environment: Yes — QA needs a real Android device with Android MDM configured.
  • Risk level: Low — host-scoped deletes/updates on indexed keys inside the existing re-enroll transaction; no new load profile and no schema change. Watch that manual labels are preserved and that the reset fires only on genuine re-enroll, not every status report.

Test plan

Make sure to go through the list and consider all events that might be related to this story, so we catch edge cases earlier.

Core flow

  1. Enroll an Android host and apply labels (manual and dynamic/built-in), configuration profiles, a pending MDM command, and a pending software install
  2. Re-enroll the device by removing and re-adding the Android Work Profile (arrives as an ENROLLMENT Pub/Sub notification)
  3. Verify that on re-enrollment:
  • Manual label membership is intact
  • Dynamic and built-in label memberships are cleared
  • Non-osquery vitals (disk, OS) are cleared
  • Pending commands are cancelled
  • Pending scripts: N/A — Android does not support scripts
  • Pending software installs are cancelled (marked canceled, not left perpetually pending)
  • The host is not in a locked/wiped state
  1. Verify the host receives fresh configuration profiles based on its current team assignment
  2. Verify dynamic/built-in label memberships repopulate from the next Android status report (Android hosts don't run osquery, so this is status-report-driven, not osquery re-evaluation)

Edge cases

  • First enrollment (device never enrolled before): no reset runs; a new host row is created normally with no state cleared.
  • STATUS_REPORT recovery path: a device whose Fleet host row was deleted sends a status report → a fresh host row is created and no reset runs (there is no stale state to clear).
  • preserve_host_activities_on_reenrollment on vs. off: past host activities are retained when on and cleared when off.
  • Multi-host isolation: re-enrolling one Android host does not affect another Android host's labels, vitals, or pending items.
  • Re-enroll into a different team (new enroll secret): host gets the new team's profiles; stale prior-team state is cleared.

Supplemental testing

Testing notes

Confirmation

  1. [x] Engineer: Added comment to user story confirming successful completion of test plan (include any special setup, test data, or configuration used during development/testing if applicable).
  2. [x] QA: Added comment to user story confirming successful completion of test plan.
  3. [x] QA: Determined whether this story needs Playwright automation.
    • Needs automation: Yes / No
    • If yes, filed a follow-up issue in the :help-qa project with status "Needs automation":

melpike avatar Apr 13 '26 20:04 melpike

Thanks for splitting the stories @melpike! Heads up that I added the 4.87 milestone so we can follow up quickly. I left this one on the drafting board and assigned you.

@JordanMontgomery can you please help us T-shirt size this one?

cc @georgekarrv

noahtalerman avatar Apr 14 '26 13:04 noahtalerman

@melpike I think we want to split it one more time.

One story for Linux, and Apple non-AB hosts, and one for Android and Windows.

Since g-mdm only owns Apple and Linux, and g-power-to-pc now owns Android and Windows.

cc @noahtalerman

MagnusHJensen avatar Apr 21 '26 18:04 MagnusHJensen

FYI @marko-lisica moved this to fast draft. Up to you on if it actually needs full draft.

noahtalerman avatar Apr 24 '26 21:04 noahtalerman

Fast draft sounds good to me.

marko-lisica avatar Apr 27 '26 09:04 marko-lisica

This can be moved to the "Ready" column.

marko-lisica avatar May 13 '26 15:05 marko-lisica

Victor: I think we're doing most of this. The remaining work is to determine what is not being deleted that should be.

lukeheath avatar Jun 29 '26 14:06 lukeheath

@lukeheath, for visibility only. BYOD added, PWR-TO-PC removed.

sharon-fdm avatar Jul 20 '26 13:07 sharon-fdm

QA complete — passed

Tested on 4.92.0-rc.2609041753 (rc-minor-fleet-v4.92.0, 2cc7341a84) against a physical Samsung SM-S906U1 in both ownership modes, Fleet Premium, team "Mobile":

  • BYOD work profile — host 2844, keyed on enterpriseSpecificId
  • Fully managed — host 2849, keyed on sha256(brand:serial)

Switching ownership modes creates a second host row for the same physical device. That's by design (getAndroidHostKey, server/mdm/android/service/pubsub.go:1251 — AMAPI only populates enterpriseSpecificId for personally-owned devices), not a defect.

Core flow

Check Result
Pending command cancelled ✅ LOCK on BYOD and RESET_PASSWORD on fully managed both deleted; pending_action cleared
Only pending commands touched ✅ nine acknowledged commands on host 2849 (3× LOCK, 3× RESET_PASSWORD, 2× WIPE, 1 more) all survived — commands went 10 → 9
Pending software installs failed ✅ 4 of 4 on BYOD, 3 of 3 on fully managed, each carrying its original command UUID, so these are provably the prior enrollment's rows
Completed installs untouched ✅ Chrome's finished install (8a16b63c) was not failed — correctly excluded by the verification_at IS NULL filter
Manual label membership intact ✅ all 4 labels on host 2844 survived (built-in manual, built-in dynamic, host_vitals, regular manual)
Host not left locked/wiped ✅ device_status stayed unlocked across every re-enroll
Fresh profiles for current team ✅ 14 profiles (3 configuration profiles + 11 certificate templates) re-delivered and re-verified on both hosts
Past activities preserved ✅ preserve_host_activities_on_reenrollment: true — 16 → 36 on BYOD and 14 → 33 on fully managed, with zero prior activities lost
Existing host row reused ✅ same host ID, created_at unchanged — no duplicate host from a re-enroll within one ownership mode

Edge cases

  • First enrollment — no reset; host 2849 was created through addNewHost with nothing cleared.
  • Multi-host isolation — verified four times across the session, and once in the strongest possible form: hosts 2844 and 2849 are the same physical phone, each holding a pending clear-passcode command. Re-enrolling 2849 cancelled only its own; 2844 kept its pending RESET_PASSWORD 8f50a9b5, its 37 activities and its 14 profiles.
  • Cross-platform isolation — the reset writes to four tables shared by every platform (label_membership, host_mdm_actions, host_vpp_software_installs, activity_host_past). A macOS host (8 pending commands), an iPhone (3 pending commands) and two Windows hosts — one of which shares host_vitals label 1980 with the Android host — came out byte-identical on every run.
  • preserve_host_activities_on_reenrollment = false — not covered. It's a global setting shared with the Apple reset path, and every run here was with it on.

Three test-plan items the implementation deliberately does differently

@dantecatalfamo could you confirm these are intended? They read as considered decisions in the code, so I've treated them as such rather than filing bugs.

  1. "Non-osquery vitals (disk, OS) are cleared" — the reset deliberately does not delete host_disks / host_operating_system (server/datastore/mysql/android.go:346), because the enrollment overwrites both and deleting is unsafe with a read replica.
  2. "Dynamic and built-in label memberships are cleared" — built-in labels are kept on purpose (otherwise a host disappears from the host list permanently) and host_vitals membership is kept too (profile-scope window), android.go:376-391. Only non-built-in dynamic membership is cleared.
  3. "Pending software installs are cancelled (marked canceled, not left perpetually pending)" — they're marked failed, not canceled, and emit failed-install activities. This matches the changes file ("reported as failed") and is the better behaviour, since canceling would hide them from the host software list and the activity feed.

Not reachable on Android

The dynamic-label clause cannot fire on a real Android host: dynamic labels are osquery-query-driven and Android hosts don't run osquery, so the only memberships they can hold are built-in, host_vitals or manual — all of which the reset preserves. Verifying that clause needs a hand-inserted label_membership row, which the datastore tests already cover.

Bugs found while testing

All three pre-date this story and are not blockers for it:

  • fleetdm/fleet#52605 — disk space shows "Not supported" after re-enrollment (reproduces on BYOD and fully managed)
  • fleetdm/fleet#52606 — re-enrollment doesn't update the host's enrollment or last-seen time
  • fleetdm/fleet#52617 — setup-experience apps can be marked "Failed" after installing successfully

Automation

  • Needs automation: No — every path requires a physical Android device enrolled through AMAPI with real Pub/Sub notifications, plus offline-device timing to stage pending commands and installs. Not reachable from Playwright. The datastore and Pub/Sub layers are already covered by unit and integration tests in the implementing PR (#51290).

AndreyKizimenko avatar Sep 04 '26 21:09 AndreyKizimenko

Device returns home renewed old tasks fade like morning mist fresh orders take root

fleet-release avatar Oct 01 '26 16:10 fleet-release