Skip to content

Commit

Permalink
move key cleanup to service layer
Browse files Browse the repository at this point in the history
  • Loading branch information
mostlikelee committed Nov 19, 2024
1 parent d3ace4d commit 5ddb7b3
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 27 deletions.
24 changes: 2 additions & 22 deletions server/datastore/mysql/apple_mdm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3413,27 +3413,6 @@ func (ds *Datastore) CleanupUnusedBootstrapPackages(ctx context.Context, pkgStor
return ctxerr.Wrap(ctx, err, "cleanup unused bootstrap packages")
}

func (ds *Datastore) CleanupDiskEncryptionKeysOnTeamChange(ctx context.Context, hostIDs []uint, newTeamID *uint) error {
return ds.withTx(ctx, func(tx sqlx.ExtContext) error {
return cleanupDiskEncryptionKeysOnTeamChangeDB(ctx, tx, hostIDs, newTeamID)
})
}

func cleanupDiskEncryptionKeysOnTeamChangeDB(ctx context.Context, tx sqlx.ExtContext, hostIDs []uint, newTeamID *uint) error {
_, err := getMDMAppleConfigProfileByTeamAndIdentifierDB(ctx, tx, newTeamID, mobileconfig.FleetFileVaultPayloadIdentifier)
if err != nil {
if fleet.IsNotFound(err) {
// the new team does not have a filevault profile so we need to delete the existing ones
if err := bulkDeleteMacosHostDiskEncryptionKeysDB(ctx, tx, hostIDs); err != nil {
return ctxerr.Wrap(ctx, err, "reconcile filevault profiles on team change bulk delete host disk encryption keys")
}
} else {
return ctxerr.Wrap(ctx, err, "reconcile filevault profiles on team change get profile")
}
}
return nil
}

func getMDMAppleConfigProfileByTeamAndIdentifierDB(ctx context.Context, tx sqlx.QueryerContext, teamID *uint, profileIdentifier string) (*fleet.MDMAppleConfigProfile, error) {
if teamID == nil {
teamID = ptr.Uint(0)
Expand Down Expand Up @@ -3839,7 +3818,8 @@ func (ds *Datastore) updateHostDEPAssignProfileResponses(ctx context.Context, pa
}

func updateHostDEPAssignProfileResponses(ctx context.Context, tx sqlx.ExtContext, logger log.Logger, profileUUID string, serials []string,
status string, abmTokenID *uint) error {
status string, abmTokenID *uint,
) error {
if len(serials) == 0 {
return nil
}
Expand Down
4 changes: 0 additions & 4 deletions server/datastore/mysql/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2850,10 +2850,6 @@ func (ds *Datastore) AddHostsToTeam(ctx context.Context, teamID *uint, hostIDs [
return ctxerr.Wrap(ctx, err, "exec AddHostsToTeam")
}

if err := cleanupDiskEncryptionKeysOnTeamChangeDB(ctx, tx, hostIDsBatch, teamID); err != nil {
return ctxerr.Wrap(ctx, err, "AddHostsToTeam cleanup disk encryption keys")
}

return nil
},
)
Expand Down
24 changes: 23 additions & 1 deletion server/datastore/mysql/mdm.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ func (ds *Datastore) getMDMCommand(ctx context.Context, q sqlx.QueryerContext, c

func (ds *Datastore) BatchSetMDMProfiles(ctx context.Context, tmID *uint, macProfiles []*fleet.MDMAppleConfigProfile,
winProfiles []*fleet.MDMWindowsConfigProfile, macDeclarations []*fleet.MDMAppleDeclaration) (updates fleet.MDMProfilesUpdates,
err error) {
err error,
) {
err = ds.withRetryTxx(ctx, func(tx sqlx.ExtContext) error {
var err error
if updates.WindowsConfigProfile, err = ds.batchSetMDMWindowsProfilesDB(ctx, tx, tmID, winProfiles); err != nil {
Expand Down Expand Up @@ -1450,3 +1451,24 @@ func (ds *Datastore) IsHostConnectedToFleetMDM(ctx context.Context, host *fleet.
}
return mp[host.UUID], nil
}

func (ds *Datastore) CleanupDiskEncryptionKeysOnTeamChange(ctx context.Context, hostIDs []uint, newTeamID *uint) error {
return ds.withTx(ctx, func(tx sqlx.ExtContext) error {
return cleanupDiskEncryptionKeysOnTeamChangeDB(ctx, tx, hostIDs, newTeamID)
})
}

func cleanupDiskEncryptionKeysOnTeamChangeDB(ctx context.Context, tx sqlx.ExtContext, hostIDs []uint, newTeamID *uint) error {
_, err := getMDMAppleConfigProfileByTeamAndIdentifierDB(ctx, tx, newTeamID, mobileconfig.FleetFileVaultPayloadIdentifier)
if err != nil {
if fleet.IsNotFound(err) {
// the new team does not have a filevault profile so we need to delete the existing ones
if err := bulkDeleteMacosHostDiskEncryptionKeysDB(ctx, tx, hostIDs); err != nil {
return ctxerr.Wrap(ctx, err, "reconcile filevault profiles on team change bulk delete host disk encryption keys")
}
} else {
return ctxerr.Wrap(ctx, err, "reconcile filevault profiles on team change get profile")
}
}
return nil
}
1 change: 1 addition & 0 deletions server/fleet/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,7 @@ type Datastore interface {
// GetHostMDMAppleProfiles returns the MDM profile information for the specified host UUID.
GetHostMDMAppleProfiles(ctx context.Context, hostUUID string) ([]HostMDMAppleProfile, error)

// CleanupDiskEncryptionKeysOnTeamChange removes all disk encryption keys for the given hosts on the provided team.
CleanupDiskEncryptionKeysOnTeamChange(ctx context.Context, hostIDs []uint, newTeamID *uint) error

// NewMDMAppleEnrollmentProfile creates and returns new enrollment profile.
Expand Down
36 changes: 36 additions & 0 deletions server/service/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,11 @@ func (svc *Service) AddHostsToTeam(ctx context.Context, teamID *uint, hostIDs []
if err := svc.ds.AddHostsToTeam(ctx, teamID, hostIDs); err != nil {
return err
}

if err := svc.cleanupDiskEncryptionKeys(ctx, teamID, hostIDs); err != nil {
return ctxerr.Wrap(ctx, err, "cleanup disk encryption keys")
}

if !skipBulkPending {
if _, err := svc.ds.BulkSetPendingMDMHostProfiles(ctx, hostIDs, nil, nil, nil); err != nil {
return ctxerr.Wrap(ctx, err, "bulk set pending host profiles")
Expand All @@ -849,6 +854,34 @@ func (svc *Service) AddHostsToTeam(ctx context.Context, teamID *uint, hostIDs []
return svc.createTransferredHostsActivity(ctx, teamID, hostIDs, nil)
}

// removes any existing disk encryption keys for hosts that are being transferred
// to a different team if disk encryption is NOT enabled on the new team
func (svc *Service) cleanupDiskEncryptionKeys(ctx context.Context, teamID *uint, hostIDs []uint) error {
var encryptionEnabled bool
if teamID == nil {
appConfig, err := svc.ds.AppConfig(ctx)
if err != nil {
return ctxerr.Wrap(ctx, err, "get app config")
}
encryptionEnabled = appConfig.MDM.EnableDiskEncryption.Value
} else {
team, err := svc.ds.Team(ctx, *teamID)
if err != nil {
return ctxerr.Wrap(ctx, err, "get team")
}
encryptionEnabled = team.Config.MDM.EnableDiskEncryption
}

if !encryptionEnabled {
err := svc.ds.CleanupDiskEncryptionKeysOnTeamChange(ctx, hostIDs, teamID)
if err != nil {
return ctxerr.Wrap(ctx, err, "cleanup disk encryption keys on team change")
}
}

return nil
}

// creates the transferred hosts activity if hosts were transferred, taking
// care of loading the team name and the hosts names if necessary (hostNames
// may be passed as empty if they were not available to the caller, such as in
Expand Down Expand Up @@ -962,6 +995,9 @@ func (svc *Service) AddHostsToTeamByFilter(ctx context.Context, teamID *uint, fi
if err := svc.ds.AddHostsToTeam(ctx, teamID, hostIDs); err != nil {
return err
}
if err := svc.cleanupDiskEncryptionKeys(ctx, teamID, hostIDs); err != nil {
return ctxerr.Wrap(ctx, err, "cleanup disk encryption keys")
}
if _, err := svc.ds.BulkSetPendingMDMHostProfiles(ctx, hostIDs, nil, nil, nil); err != nil {
return ctxerr.Wrap(ctx, err, "bulk set pending host profiles")
}
Expand Down

0 comments on commit 5ddb7b3

Please sign in to comment.