Skip to content

Commit

Permalink
Recategorize MDM endpoints to new mdm-less paths (#17372)
Browse files Browse the repository at this point in the history
  • Loading branch information
mna authored Mar 13, 2024
1 parent c358bde commit 2522cc5
Show file tree
Hide file tree
Showing 17 changed files with 431 additions and 210 deletions.
1 change: 1 addition & 0 deletions changes/16260-recategorize-mdm-api-endpoints
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Migrate MDM-related endpoints to new paths, deprecating (but still supporting indefinitely) the old endpoints.
2 changes: 1 addition & 1 deletion ee/server/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func NewService(
svc.SetEnterpriseOverrides(fleet.EnterpriseOverrides{
HostFeatures: eeservice.HostFeatures,
TeamByIDOrName: eeservice.teamByIDOrName,
UpdateTeamMDMAppleSettings: eeservice.updateTeamMDMAppleSettings,
UpdateTeamMDMDiskEncryption: eeservice.updateTeamMDMDiskEncryption,
MDMAppleEnableFileVaultAndEscrow: eeservice.MDMAppleEnableFileVaultAndEscrow,
MDMAppleDisableFileVaultAndEscrow: eeservice.MDMAppleDisableFileVaultAndEscrow,
DeleteMDMAppleSetupAssistant: eeservice.DeleteMDMAppleSetupAssistant,
Expand Down
8 changes: 4 additions & 4 deletions ee/server/service/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -1181,11 +1181,11 @@ func unmarshalWithGlobalDefaults(b *json.RawMessage) (fleet.Features, error) {
return *defaults, nil
}

func (svc *Service) updateTeamMDMAppleSettings(ctx context.Context, tm *fleet.Team, payload fleet.MDMAppleSettingsPayload) error {
func (svc *Service) updateTeamMDMDiskEncryption(ctx context.Context, tm *fleet.Team, enable *bool) error {
var didUpdate, didUpdateMacOSDiskEncryption bool
if payload.EnableDiskEncryption != nil {
if tm.Config.MDM.EnableDiskEncryption != *payload.EnableDiskEncryption {
tm.Config.MDM.EnableDiskEncryption = *payload.EnableDiskEncryption
if enable != nil {
if tm.Config.MDM.EnableDiskEncryption != *enable {
tm.Config.MDM.EnableDiskEncryption = *enable
didUpdate = true
didUpdateMacOSDiskEncryption = true
}
Expand Down
10 changes: 5 additions & 5 deletions server/fleet/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import (
type EnterpriseOverrides struct {
HostFeatures func(context context.Context, host *Host) (*Features, error)
TeamByIDOrName func(ctx context.Context, id *uint, name *string) (*Team, error)
// UpdateTeamMDMAppleSettings is the team-specific service method for when
// a team ID is provided to the UpdateMDMAppleSettings method.
UpdateTeamMDMAppleSettings func(ctx context.Context, tm *Team, payload MDMAppleSettingsPayload) error
// UpdateTeamMDMDiskEncryption is the team-specific service method for when
// a team ID is provided to the UpdateMDMDiskEncryption method.
UpdateTeamMDMDiskEncryption func(ctx context.Context, tm *Team, enable *bool) error

// The next two functions are implemented by the ee/service, and called
// properly when called from an ee/service method (e.g. Modify Team), but
Expand Down Expand Up @@ -761,9 +761,9 @@ type Service interface {
// profile for the given team.
MDMAppleDisableFileVaultAndEscrow(ctx context.Context, teamID *uint) error

// UpdateMDMAppleSettings updates the specified MDM Apple settings for a
// UpdateMDMDiskEncryption updates the disk encryption setting for a
// specified team or for hosts with no team.
UpdateMDMAppleSettings(ctx context.Context, payload MDMAppleSettingsPayload) error
UpdateMDMDiskEncryption(ctx context.Context, teamID *uint, enableDiskEncryption *bool) error

// VerifyMDMAppleConfigured verifies that the server is configured for
// Apple MDM. If an error is returned, authorization is skipped so the
Expand Down
34 changes: 5 additions & 29 deletions server/service/apple_mdm.go
Original file line number Diff line number Diff line change
Expand Up @@ -1631,37 +1631,13 @@ func (r updateMDMAppleSettingsResponse) Status() int { return http.StatusNoConte
// team endpoints only allow write access to admins.
func updateMDMAppleSettingsEndpoint(ctx context.Context, request interface{}, svc fleet.Service) (errorer, error) {
req := request.(*updateMDMAppleSettingsRequest)
if err := svc.UpdateMDMAppleSettings(ctx, req.MDMAppleSettingsPayload); err != nil {
if err := svc.UpdateMDMDiskEncryption(ctx, req.MDMAppleSettingsPayload.TeamID, req.MDMAppleSettingsPayload.EnableDiskEncryption); err != nil {
return updateMDMAppleSettingsResponse{Err: err}, nil
}
return updateMDMAppleSettingsResponse{}, nil
}

func (svc *Service) UpdateMDMAppleSettings(ctx context.Context, payload fleet.MDMAppleSettingsPayload) error {
// for now, assume all settings require premium (this is true for the first
// supported setting, enable_disk_encryption. Adjust as needed in the future
// if this is not always the case).
lic, _ := license.FromContext(ctx)
if lic == nil || !lic.IsPremium() {
svc.authz.SkipAuthorization(ctx) // so that the error message is not replaced by "forbidden"
return ErrMissingLicense
}

if err := svc.authz.Authorize(ctx, payload, fleet.ActionWrite); err != nil {
return ctxerr.Wrap(ctx, err)
}

if payload.TeamID != nil {
tm, err := svc.EnterpriseOverrides.TeamByIDOrName(ctx, payload.TeamID, nil)
if err != nil {
return err
}
return svc.EnterpriseOverrides.UpdateTeamMDMAppleSettings(ctx, tm, payload)
}
return svc.updateAppConfigMDMAppleSettings(ctx, payload)
}

func (svc *Service) updateAppConfigMDMAppleSettings(ctx context.Context, payload fleet.MDMAppleSettingsPayload) error {
func (svc *Service) updateAppConfigMDMDiskEncryption(ctx context.Context, enabled *bool) error {
// appconfig is only used internally, it's fine to read it unobfuscated
// (svc.AppConfigObfuscated must not be used because the write-only users
// such as gitops will fail to access it).
Expand All @@ -1671,9 +1647,9 @@ func (svc *Service) updateAppConfigMDMAppleSettings(ctx context.Context, payload
}

var didUpdate, didUpdateMacOSDiskEncryption bool
if payload.EnableDiskEncryption != nil {
if ac.MDM.EnableDiskEncryption.Value != *payload.EnableDiskEncryption {
ac.MDM.EnableDiskEncryption = optjson.SetBool(*payload.EnableDiskEncryption)
if enabled != nil {
if ac.MDM.EnableDiskEncryption.Value != *enabled {
ac.MDM.EnableDiskEncryption = optjson.SetBool(*enabled)
didUpdate = true
didUpdateMacOSDiskEncryption = true
}
Expand Down
2 changes: 1 addition & 1 deletion server/service/apple_mdm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1903,7 +1903,7 @@ func TestUpdateMDMAppleSettings(t *testing.T) {
}
ctx = license.NewContext(ctx, &fleet.LicenseInfo{Tier: tier})

err := svc.UpdateMDMAppleSettings(ctx, fleet.MDMAppleSettingsPayload{TeamID: tt.teamID})
err := svc.UpdateMDMDiskEncryption(ctx, tt.teamID, nil)
if tt.wantErr == "" {
require.NoError(t, err)
return
Expand Down
Loading

0 comments on commit 2522cc5

Please sign in to comment.