Skip to content

feat(mdm): lock Windows hosts with BitLocker on Fleet Free CU-17tkuw6a8c2 - #296

Merged
andrii-flamingo merged 3 commits into
mainfrom
feat/windows-lock
Dec 3, 2026
Merged

andrii-flamingo merged 3 commits into
mainfrom
feat/windows-lock

Conversation

@andrii-flamingo

@andrii-flamingo andrii-flamingo commented Dec 3, 2026 •

Copy link
Copy Markdown

Upstream locks Windows hosts on Premium only, by disabling local accounts. On Free in OpenFrame mode the fork locks them with BitLocker instead: the system drive is forced into recovery and the PC restarts to the BitLocker recovery screen, where it starts again only with the recovery key Fleet holds. Outside OpenFrame and shared mode, and for other platforms, the request goes to upstream as before.

Lock

  • POST /api/_version_/fleet/hosts/{id}/lock, with an optional body {"message": "..."} (up to 240 characters, no line breaks) that is set as the custom message of the recovery screen. The body stays optional: the request reads it itself, as the wipe does, so upstream's callers that send none are not refused.
  • Refused without a current recovery key Fleet can decrypt ("Lock needs BitLocker"), when fleetd doesn't run scripts on the host, with a pending lock, unlock or wipe, or when the host is locked or wiped. Another tenant's host is 404.
  • The host's lock, unlock and wipe references are read with its status, both on the primary, and the lock is queued only if they are unchanged, in one transaction under the host's row lock: of two concurrent locks one is queued, the other answers 422.
  • On the PC the script checks, before it changes anything, that BitLocker protects the drive and that Fleet's key is one of its recovery passwords (it carries the key's SHA-256, never the key). Then it sets the message, registers a startup task that will undo the lock, runs manage-bde -forcerecovery and restarts the PC in 30 seconds. The message reaches the script in base64.
  • Each request has its own id, kept on the PC: fleetd keeps no record of the scripts it ran, so when a lock's result is lost before the restart and fleetd runs it again after the unlock, it does nothing.

Unlock

  • There is no unlock command: someone types the key at the PC. The startup task then adds back the TPM protector that -forcerecovery deleted (otherwise Windows asks for the key at every start) and restores the recovery message policy, without depending on fleetd or on Fleet's state of the host.
  • When a locked host checks in at least 2 minutes after the lock, Fleet queues the same script as an internal unlock; its success makes the host unlocked. Whether the PC has restarted is told by a volatile registry key that Windows drops at restart, not by the clock.
  • A failed unlock is tried again after 15 minutes, then after twice as long each time, up to a day. A PC that hasn't restarted yet records nothing; any other failure records failed_unlock.

Scripts in OpenFrame mode

  • fleetd on Windows runs scripts only with --enable-scripts. With that flag on, the fork refuses every script that isn't Fleet's own in OpenFrame and shared mode (422), so only the lock and unlock reach the PC.

Activities

locked_host with the request, completed_lock or failed_lock (with the exit code) with the result, unlocked_host with no user when the unlock succeeds, failed_unlock (with the exit code) when it fails for another reason than a PC that hasn't restarted.

Callers and order

No caller yet. This Fleet image goes first. Then OpenFrame: --enable-scripts for the agent, the SDK method with its README row, and the GraphQL contract fix (the lock in it still describes the accounts lock). No migration.

Known limits

  • Not checked on a real Windows PC yet: the message on the recovery screen (and how it shows characters outside ASCII), the startup task, the volatile key and adding the TPM protector at startup.
  • The recovery key is not rotated after it was read out; that is a separate change.
  • A TPM protector with a PIN or a startup key can't be restored, so such a drive is not locked.

Tests

Two-tenant MySQL tests (openframe_windows_lock_test.go in the datastore: the unlock schedule, concurrent locks, the script refusal) and service tests (openframe_windows_lock_test.go: the request body, the checks, the scripts, the activities). server/service and server/fleet unit tests pass, except TestEmbeddedCleanupScriptMatchesDocs, which fails on main, too: the docs copy it compares with is not in the fork. MySQL TestScripts, TestOpenframe*, TestMDMWindows, TestActivity and TestHosts pass, except TestHosts/DeleteHosts, which fails on main, too, on host_software_windows_details.

Details: openframe/docs/windows-lock.md.

Change-Set: windows-lock

Change set flamingo-stack/fleetmdm#296: these pull requests are one change, reviewed together.

  • flamingo-stack/fleetmdm#296 (this pull request): feat(mdm): lock Windows hosts with BitLocker on Fleet Free CU-17tkuw6a8c2 · landed
  • …and 1 pull request not visible

Linked work

Linked by the Depends-On / Change-Set lines in these descriptions; this block is maintained by the hub.

@flamingo

flamingo Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

🦩 flamingo Code Review / security

✅ No findings on the current head.

Advisory: findings do not block the merge.

Files Lines 🔴 Action required 🟠 Recommended 🔵 Informational
17 +1579 / -1 0 0 0
5 possible problems checked and ruled out, 4 after reading more of the repository
  • server/service/openframe_windows_lock.go:43 Lock endpoint reveals a host's platform to users with no write access to its team (platform check runs before the team authorization check): The diff shows the order plainly: host.FleetPlatform() != "windows" returns (false, nil) at lines 43-45 before the team-scoped MDMCommandAuthz{TeamID: host.TeamID} check at line 46, so a caller who passes only the team-less Host/ActionList check gets a different outcome for a Windows vs a non-Windows host in another team. The ordering is a genuine deviation (authorize-then-branch is the…
  • server/datastore/mysql/scripts.go:124 OpenFrame script refusal also blocks setup-experience scripts, stalling the setup flow instead of a user script: The chain is factually right (scripts.go:38-39 passes isInternal=false, so setup_experience.go:375 is refused), but SetupExperienceNextStep lives only in ee/server/service (premium): the fork's own agent patch at orbit/cmd/orbit/orbit.go:1822 says setup experience is "disabled or does not license[d]" in OpenFrame, and windows-lock.md:3-9 scopes OpenFrame to Fleet Free, so the path is not…
  • server/datastore/mysql/openframe_windows_lock.go:99 Automatic Windows unlock clears any successful windows lock_ref, including one not queued by the BitLocker lock script: The query (openframe_windows_lock.go:95-106) indeed matches any windows host_mdm_actions row with a successful lock_ref, but in an OpenFrame deployment there is no other producer of a windows script lock_ref: upstream's windows script lock is premium (LockHost 402 on Free, which openframe/docs/windows-lock.md:9 states is the fallback), and the lock endpoint always routes windows to the fork path…
  • server/service/openframe_windows_lock_test.go:224 "another tenant's host" subtest never exercises the team scope of the lock, it only hits the mock's not-found path: In this fork "another tenant" means the OpenFrame tenant fence inside the datastore's Host method (openframe/docs/windows-lock.md:98-99: "The lock reads the host through the fenced Host, so another tenant's host is 404"), which the fixture's HostFunc returning not-found for id!=1 stands in for at unit level; the team-scoped Authorize at openframe_windows_lock.go:46 is real and present, so this is…
  • server/service/openframe_windows_lock_test.go:224 The "another tenant's host" lock test never exercises cross-team authorization, it only hits the not-found path: Same claim as candidate 2 on the same subtest: the enforcement it asks to be pinned (openframe_windows_lock.go:46 MDMCommandAuthz{TeamID: host.TeamID}) exists, and the subtest emulates the OpenFrame tenant fence documented at windows-lock.md:98-99; a missing cross-team unit case is a coverage wish, not a defect in the changed code.

Review again. New commits are not reviewed until you ask:

  • Security-check the new commits: only what was pushed since this security review
  • Security-check the whole diff again: everything, including what was already checked

Or comment @flamingo-review security (new commits) or @flamingo-review security full (everything). Add the flamingo-security-review-always label to security-review every push.

Updated 2026-12-03 19:21 UTC · reviewed from Andrii Koropets's machine

@flamingo

flamingo Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

🦩 flamingo Code Review

❌ Tests and checks failed on this commit: tests (go test). What each printed is in the Tests and mutation section.

6 findings: 🔴 0 action required · 🟠 0 recommended · 🔵 6 informational

Advisory: findings do not block the merge.

Checks: missing-test ×1, dup-scan ×5

Files Lines Languages Checks 🔴 Action required 🟠 Recommended 🔵 Informational
17 +1579 / -1 Go (1 go test lane), Shell (1 bash -n lane) ✅ 1 / 1 passed 0 0 6

🔵 Informational (6)

# Where Finding Flagged by
1 openframe_windows_lock.go:25 2 new or changed definitions have no test referencing them (Minor) missing-test Tool-checked
2 datastore_mock.go:8344 OpenframeRecordDiskEncryptionResult duplicates a near-identical definition (Minor) dup-scan Tool-checked
3 datastore_mock.go:8351 OpenframeQueueWindowsUnlock duplicates an existing definition (Minor) dup-scan Tool-checked
4 datastore_mock.go:8358 OpenframeHostMDMActionRefs duplicates a near-identical definition (Minor) dup-scan Tool-checked
5 datastore_mock.go:8365 OpenframeLockWindowsHostViaScript duplicates an existing definition (Minor) dup-scan Tool-checked
6 datastore_mock.go:8372 OpenframeRefuseSharedHardwareUUID duplicates an existing definition (Minor) dup-scan Tool-checked

✅ What this change does well

  • server/fleet/openframe_windows_lock.go replaces the json:"message" tag with an explicit DecodeBody, which keeps the lock body genuinely optional (the endpointer rejects a nil body whenever a json tag is present) while still decoding the message.
  • server/datastore/mysql/openframe_windows_lock.go makes the lock queue compare-and-set under SELECT ... FOR UPDATE in one transaction, closing the double-lock race, and TestOpenframeWindowsLockRaced exercises both the stale-read and the truly concurrent path.
  • openframe/docs/windows-lock.md is kept in step with the code (new exit codes, startup task, backoff schedule) and the datastore interface comments in server/fleet/datastore.go were updated with the signature change.
  • server/service/openframe_windows_lock.go replaces the clock-based "has the PC restarted" check with a volatile registry key, removing a real false-unlock risk from clock skew.
  • The lock script now registers the unlock as an at-startup SYSTEM scheduled task before running manage-bde -forcerecovery, so the TPM protector is restored even if fleetd never comes back, and server/service/openframe_windows_lock_test.go asserts that ordering explicitly.
  • server/datastore/mysql/openframe_windows_lock.go queues a lock only if the host's references are the ones its status was read from, under the host's row lock, with a test of two concurrent locks.
  • server/service/openframe_windows_lock.go gives each lock its own id and registers a startup task on the PC, so a lost result can't lock the PC twice and the TPM protector comes back without Fleet.
  • server/fleet/openframe_windows_lock.go keeps the lock body optional with its own DecodeBody, so upstream's body-less callers are not refused.

Inline comments: 4 new · 1 already on this pull request from an earlier review (not posted again; the existing thread is linked below)

Already commented on by an earlier review

  • 🔵 Informational · info · Minor · dup-scan Tool-checked · server/mock/datastore_mock.go:8372: OpenframeRefuseSharedHardwareUUID duplicates an existing definition (existing thread)

Outside the lines this diff changes (no inline comment possible)

  • 🔵 Informational · info · Minor · dup-scan Tool-checked · server/mock/datastore_mock.go:8344: OpenframeRecordDiskEncryptionResult duplicates a near-identical definition
    Nearly the same structure as FleetMDM:server/mock/nanodep/storage.go:84 (go:github.com/fleetdm/fleet/v4/server/mock/nanodep.Storage.RetrieveAssignerProfile, hamming 6) and 1 more. Reuse the existing definition, or extract one shared implementation.
    func (s *DataStore) OpenframeRecordDiskEncryptionResult(ctx context.Context, host *fleet.Host, clientError string, keyStored bool) error {
    
2 possible problems checked and ruled out, 1 after reading more of the repository
  • server/service/openframe_windows_lock.go:171 Unlock exit code 11 (PC unlocked, TPM protector not restored) keeps the host "locked" forever and re-records failed_unlock on every retry: Script exits 11 after the PC is already unlocked (openframe_windows_lock.go:237-239) without Remove-LockState, and the failure path nulls unlock_ref (scripts.go:2489), so lock_ref stays set: the host keeps showing as locked and each re-queue records another failed_unlock activity. Real, but only on the rare Add-BitLockerKeyProtector failure.
  • server/datastore/mysql/openframe_windows_lock.go:99 Unlock retry backoff is keyed on the unlock script's MD5, so changing the script resets attempts to zero: Verified in openframe_windows_lock.go:101-106,122-123: attempts are counted only for host_script_results whose script_content_id matches the MD5 of the script text passed in, so an edit to the server-shipped unlock script makes attempts=0 and restarts backoff. Real but only triggered on a script/version change, so a rarely hit edge.

🧪 Tests and mutation

Tests per lane

Language Project Command Status Counts Time
Go (go test) . go vet ./server/datastore/mysql ./server/fleet ./server/mock ./server/mock/service ./server/service && go test -cover ./orbit/... ❌ failed 0 s
Shell (bash -n) . bash -n openframe/scripts/verify.sh ✅ passed 0 s
Tests failed in .: what it printed last
bash: go: command not found

Checks

Check Kind Language Project Status Time
OpenGrep security Go (OpenGrep) . ✅ passed 13.4 s

The test services of flamingo-stack/fleetmdm (server/mdm/nanodep/storage/mysql/docker-compose-test.yml) did not come up: the integration tests may fail for that reason.

The Go lane failed on this machine for the environment: go is not installed here (bash: go: command not found). The same packages were run in Docker golang:1.26.6 with MySQL 8.0.44 at this commit: the 4 new datastore tests and 13 new service tests pass; server/service and server/fleet pass except TestEmbeddedCleanupScriptMatchesDocs, and MySQL TestScripts, TestOpenframe*, TestMDMWindows, TestActiv.

No mutation tool is configured for Go, so no mutation results.

OpenGrep passed on the changed files.

The PowerShell lock and unlock scripts are not run by any test here; they are checked by content only and not yet on a real Windows PC.

2 gaps (not tested here):

  • Go (go test) .: no mutation testing (no mutation tool for Go is configured: read the coverage of the changed packages instead)
  • Shell (bash -n) .: no mutation testing (no tool configured)
🤖 Prompt for AI agents: fix every finding
Address these code-review findings on pull request #296 of flamingo-stack/fleetmdm, most important first.
For each one, make the minimal change that resolves it and do not refactor unrelated code.
If you can show a finding is wrong, skip it and say why.

1. [Informational] server/fleet/openframe_windows_lock.go:25 2 new or changed definitions have no test referencing them
   No test file in this checkout mentions these definitions. Add or extend tests for the ones that carry logic.
2. [Informational] server/mock/datastore_mock.go:8344 OpenframeRecordDiskEncryptionResult duplicates a near-identical definition
   Nearly the same structure as [FleetMDM:server/mock/nanodep/storage.go:84](https://github.com/flamingo-stack/fleetmdm/blob/c055dde90a6b33e6f57d85f726eb8ec17ff5f622/server/mock/nanodep/storage.go#L84-L89) (go:github.com/fleetdm/fleet/v4/server/mock/nanodep.Storage.RetrieveAssignerProfile, hamming 6) and 1 more. Reuse the existing definition, or extract one shared implementation.
3. [Informational] server/mock/datastore_mock.go:8351 OpenframeQueueWindowsUnlock duplicates an existing definition
   Same structure as [FleetMDM:server/mock/datastore_mock.go:12011](https://github.com/flamingo-stack/fleetmdm/blob/c055dde90a6b33e6f57d85f726eb8ec17ff5f622/server/mock/datastore_mock.go#L12011-L12016) (go:github.com/fleetdm/fleet/v4/server/mock.DataStore.CreatePendingCertificateTemplatesForNewHost) and 9 more. Reuse the existing definition, or extract one shared implementation.
4. [Informational] server/mock/datastore_mock.go:8358 OpenframeHostMDMActionRefs duplicates a near-identical definition
   Nearly the same structure as [FleetMDM:server/mock/datastore_mock.go:11367](https://github.com/flamingo-stack/fleetmdm/blob/c055dde90a6b33e6f57d85f726eb8ec17ff5f622/server/mock/datastore_mock.go#L11367-L11372) (go:github.com/fleetdm/fleet/v4/server/mock.DataStore.GetIncludedHostUUIDMapForAppStoreApp, hamming 6). Reuse the existing definition, or extract one shared implementation.
5. [Informational] server/mock/datastore_mock.go:8365 OpenframeLockWindowsHostViaScript duplicates an existing definition
   Same structure as [FleetMDM:server/mock/datastore_mock.go:9540](https://github.com/flamingo-stack/fleetmdm/blob/c055dde90a6b33e6f57d85f726eb8ec17ff5f622/server/mock/datastore_mock.go#L9540-L9545) (go:github.com/fleetdm/fleet/v4/server/mock.DataStore.InsertMDMAppleBootstrapPackage) and 2 more. Reuse the existing definition, or extract one shared implementation.
6. [Informational] server/mock/datastore_mock.go:8372 OpenframeRefuseSharedHardwareUUID duplicates an existing definition
   Same structure as [FleetMDM:server/mock/datastore_mock.go:7615](https://github.com/flamingo-stack/fleetmdm/blob/c055dde90a6b33e6f57d85f726eb8ec17ff5f622/server/mock/datastore_mock.go#L7615-L7620) (go:github.com/fleetdm/fleet/v4/server/mock.DataStore.ActivateNextUpcomingActivityForHost) and 39 more. Reuse the existing definition, or extract one shared implementation.

Next: commit a suggestion, or hand a comment's prompt to your coding agent. React 👍 on a finding that helped and 👎 on one that is wrong: the reviewer learns from both.

Review again. New commits are not reviewed until you ask:

  • Review the new commits: only what was pushed since this review
  • Review the whole diff again: everything, including what was already reviewed

Or comment @flamingo-review (new commits) or @flamingo-review full (everything). Add the flamingo-review-always label to review every push.

Updated 2026-12-03 19:28 UTC · reviewed from Andrii Koropets's machine

Comment thread server/mock/service/service_mock.go Resolved
Comment thread server/fleet/api_scripts.go Resolved
Comment thread server/mock/service/service_mock.go Resolved
Comment thread server/fleet/openframe_windows_lock.go Resolved
Comment thread server/fleet/openframe_windows_lock.go Resolved
Comment thread server/mock/datastore_mock.go Resolved
Comment thread server/mock/datastore_mock.go Resolved
Comment thread server/mock/datastore_mock.go Resolved
@andrii-flamingo
andrii-flamingo merged commit 1efa55d into main Dec 3, 2026
20 checks passed
@andrii-flamingo
andrii-flamingo deleted the feat/windows-lock branch December 3, 2026 21:47
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.

2 participants