fleet/server/service/middleware/mdmconfigured/mdmconfigured.go
Nathan Cochran e5f46dd4b1
Fix android profile download (#35196)
This commit fixes two related bugs with Android MDM:

1. Android profiles now download correctly as .json files instead of
.xml
   - Before: profiles downloaded as .xml with content '[object Object]'
- After: profiles download as .json with properly formatted JSON content
- Fixed by adding Android platform check in createProfileExtension() and
createFileContent()

2. Custom Settings page now recognizes Android MDM
- Before: showed 'MDM must be turned on' error even when Android MDM was
enabled
   - After: properly detects Android MDM and allows profile management
   - Fixed by adding android_enabled_and_configured check to mdmEnabled
   - Backend middleware now supports Android MDM for profile endpoints
a) Added VerifyAnyMDMConfigured() to support Apple, Windows, and Android
MDM
       b) Updated profile endpoints to use VerifyAnyMDM() middleware

**Related issue:** Resolves #35023

# Checklist for submitter

- [x] Changes file added for user-visible changes in `changes/`
- [x] Input data is properly validated, `SELECT *` is avoided, SQL
injection is prevented (using placeholders for values in statements)
- [x] If paths of existing endpoints are modified without backwards
compatibility, checked the frontend/CLI for any necessary changes

## Testing

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

## Database migrations

_No database migrations in this PR_

## New Fleet configuration settings

_No new Fleet configuration settings in this PR_

## fleetd/orbit/Fleet Desktop

_This PR does not affect fleetd/orbit/Fleet Desktop_
2025-11-17 11:52:59 -03:00

76 lines
2.3 KiB
Go

// Package mdmconfigured implements middleware functions for the supported platform-specific MDM
// solutions to ensure MDM is configured and fail fast before reaching the handler if that is not the case.
package mdmconfigured
import (
"context"
"github.com/fleetdm/fleet/v4/server/contexts/ctxerr"
hostctx "github.com/fleetdm/fleet/v4/server/contexts/host"
"github.com/fleetdm/fleet/v4/server/fleet"
"github.com/go-kit/kit/endpoint"
)
type Middleware struct {
svc fleet.Service
}
func NewMDMConfigMiddleware(svc fleet.Service) *Middleware {
return &Middleware{svc: svc}
}
func (m *Middleware) VerifyAppleMDM() endpoint.Middleware {
return func(next endpoint.Endpoint) endpoint.Endpoint {
return func(ctx context.Context, req interface{}) (interface{}, error) {
if err := m.svc.VerifyMDMAppleConfigured(ctx); err != nil {
return nil, err
}
return next(ctx, req)
}
}
}
// VerifyAppleMDMOnMacOSHosts verifies that MDM is enabled and configured when it's an Apple host making the request.
// This is used on API endpoints that are reused on Linux hosts (which don't require Apple MDM to be configured).
func (m *Middleware) VerifyAppleMDMOnMacOSHosts() endpoint.Middleware {
return func(next endpoint.Endpoint) endpoint.Endpoint {
return func(ctx context.Context, req interface{}) (interface{}, error) {
host, ok := hostctx.FromContext(ctx)
if !ok {
return nil, ctxerr.Wrap(ctx, fleet.NewAuthRequiredError("internal error: missing host from request context"))
}
if fleet.IsApplePlatform(host.Platform) {
if err := m.svc.VerifyMDMAppleConfigured(ctx); err != nil {
return nil, err
}
}
return next(ctx, req)
}
}
}
func (m *Middleware) VerifyWindowsMDM() endpoint.Middleware {
return func(next endpoint.Endpoint) endpoint.Endpoint {
return func(ctx context.Context, req interface{}) (interface{}, error) {
if err := m.svc.VerifyMDMWindowsConfigured(ctx); err != nil {
return nil, err
}
return next(ctx, req)
}
}
}
func (m *Middleware) VerifyAnyMDM() endpoint.Middleware {
return func(next endpoint.Endpoint) endpoint.Endpoint {
return func(ctx context.Context, req interface{}) (interface{}, error) {
if err := m.svc.VerifyAnyMDMConfigured(ctx); err != nil {
return nil, err
}
return next(ctx, req)
}
}
}