Split MDM platform helpers by Android semantics (#50185)

**Related issue:** Resolves #46118
This commit is contained in:
Dante Catalfamo
2026-07-31 15:52:19 -04:00
committed by GitHub
parent cb44e287f2
commit 2fd2a02e2d
8 changed files with 102 additions and 22 deletions
+2 -2
View File
@@ -2192,7 +2192,7 @@ func (ds *Datastore) MDMTurnOff(ctx context.Context, uuid string) (users []*flee
return ctxerr.Wrap(ctx, err, "getting host info from UUID")
}
if !fleet.MDMSupported(host.Platform) {
if !fleet.ClassicMDMSupported(host.Platform) {
return ctxerr.Errorf(ctx, "unsupported host platform: %q", host.Platform)
}
@@ -4534,7 +4534,7 @@ func (ds *Datastore) MDMResetEnrollment(ctx context.Context, hostUUID string, sc
}
host := hosts[0]
if !fleet.MDMSupported(host.Platform) {
if !fleet.ClassicMDMSupported(host.Platform) {
return ctxerr.Errorf(ctx, "unsupported host platform: %q", host.Platform)
}
+1 -1
View File
@@ -337,7 +337,7 @@ WHERE ` + whereTeam
)
}
byUUID[h.UUID] = h
switch fleet.MDMPlatform(h.Platform) {
switch fleet.ClassicMDMPlatform(h.Platform) {
case "darwin":
appleUUIDs = append(appleUUIDs, h.UUID)
case "windows":
+25 -7
View File
@@ -1099,25 +1099,43 @@ func (m MDMConfigAsset) Copy() MDMConfigAsset {
return clone
}
// MDMPlatform returns "darwin" or "windows" as MDM platforms
// derived from a host's platform (hosts.platform field).
// ClassicMDMPlatform returns "darwin" or "windows" as MDM platforms derived
// from a host's platform (a raw hosts.platform value, or the collapsed one
// returned by Host.FleetPlatform), or "" for platforms that don't take part in
// the classic MDM command pipeline.
//
// Note that "darwin" as MDM platform means Apple (we keep it as "darwin"
// to keep backwards compatibility throughout the app).
func MDMPlatform(hostPlatform string) string {
//
// Android is deliberately not part of this list: Android hosts don't take part
// in the classic MDM command pipeline (raw XML/plist commands, the
// nano_commands and mdm_windows_commands listings, the mdmlifecycle hooks and
// the host_mdm turn-off/reset paths MDMTurnOff and MDMResetEnrollment). Android
// has its own commands table and its own unenroll path. To check whether Fleet
// can turn MDM on for a platform at all, use MDMTurnedOnSupported instead.
func ClassicMDMPlatform(hostPlatform string) string {
switch hostPlatform {
case "darwin", "ios", "ipados":
return "darwin"
case "windows":
return "windows"
// TODO(android): add android to this list?
}
return ""
}
// MDMSupported returns whether MDM is supported for a given host platform.
func MDMSupported(hostPlatform string) bool {
return MDMPlatform(hostPlatform) != ""
// ClassicMDMSupported returns whether the given host platform takes part in the
// classic MDM command pipeline. It returns false for Android, see
// ClassicMDMPlatform for details.
func ClassicMDMSupported(hostPlatform string) bool {
return ClassicMDMPlatform(hostPlatform) != ""
}
// MDMTurnedOnSupported returns whether Fleet supports any form of MDM
// enrollment for the given host platform, Android included. Use this for the
// checks that only care about MDM being turned on for the host, such as the
// "Can't <action> the host because it doesn't have MDM turned on." pre-checks.
func MDMTurnedOnSupported(hostPlatform string) bool {
return ClassicMDMSupported(hostPlatform) || IsAndroidPlatform(hostPlatform)
}
// FilterMacOSOnlyProfilesFromIOSIPadOS will filter out profiles that are only for macOS devices
+31
View File
@@ -657,6 +657,37 @@ func TestFleetVarRenewalIDRegexp(t *testing.T) {
}
}
func TestMDMPlatformSupport(t *testing.T) {
cases := []struct {
hostPlatform string
wantClassicPlatform string
wantTurnedOn bool
}{
{"darwin", "darwin", true},
{"ios", "darwin", true},
{"ipados", "darwin", true},
{"windows", "windows", true},
// Android hosts can have MDM turned on, but they don't take part in the
// classic MDM command pipeline.
{"android", "", true},
// "linux" isn't a hosts.platform value, but it is what
// Host.FleetPlatform collapses the distros to.
{"linux", "", false},
{"ubuntu", "", false},
{"rhel", "", false},
{"chrome", "", false},
{"", "", false},
{"unknown", "", false},
}
for _, tc := range cases {
t.Run(tc.hostPlatform, func(t *testing.T) {
require.Equal(t, tc.wantClassicPlatform, fleet.ClassicMDMPlatform(tc.hostPlatform))
require.Equal(t, tc.wantClassicPlatform != "", fleet.ClassicMDMSupported(tc.hostPlatform))
require.Equal(t, tc.wantTurnedOn, fleet.MDMTurnedOnSupported(tc.hostPlatform))
})
}
}
func TestFilterMacOSOnlyProfilesFromIOSIPadOS(t *testing.T) {
for _, tc := range []struct {
profiles []*fleet.MDMAppleProfilePayload
+2 -2
View File
@@ -628,7 +628,7 @@ func (svc *Service) DeleteHosts(ctx context.Context, ids []uint, filter *map[str
lifecycleErrs := []error{}
serialsWithErrs := []string{}
for _, host := range hosts {
if fleet.MDMSupported(host.Platform) {
if fleet.ClassicMDMSupported(host.Platform) {
if err := mdmLifecycle.Do(ctx, mdmlifecycle.HostOptions{
Action: mdmlifecycle.HostActionDelete,
Host: host,
@@ -1153,7 +1153,7 @@ func (svc *Service) DeleteHost(ctx context.Context, id uint) error {
return err
}
if fleet.MDMSupported(host.Platform) {
if fleet.ClassicMDMSupported(host.Platform) {
mdmLifecycle := mdmlifecycle.New(svc.ds, svc.logger, svc.NewActivity)
err = mdmLifecycle.Do(ctx, mdmlifecycle.HostOptions{
Action: mdmlifecycle.HostActionDelete,
+1 -1
View File
@@ -546,7 +546,7 @@ func (svc *Service) RunMDMCommand(ctx context.Context, rawBase64Cmd string, host
for platform := range platforms {
commandPlatform = platform
}
if !fleet.MDMSupported(commandPlatform) {
if !fleet.ClassicMDMSupported(commandPlatform) {
err := fleet.NewInvalidArgumentError("host_uuids", "Invalid platform. You can only run MDM commands on Windows or Apple hosts.")
return nil, ctxerr.Wrap(ctx, err, "check host platform")
}