Skip to content

Commit 8b08a37

Browse files
committed
Disk encryption keys are now archived when created/updated (#25638)
For #25609 Manual QA in progress. Putting this "In Review" since it is a P1. Video explaining the PR: https://youtu.be/bUwIdjBLqiM # Checklist for submitter - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. - [x] If database migrations are included, checked table schema to confirm autoupdate - For database migrations: - [x] Checked schema for all modified table for columns that will auto-update timestamps during migration. - [x] Confirmed that updating the timestamps is acceptable, and will not cause unwanted side effects. - [x] Ensured the correct collation is explicitly set for character columns (`COLLATE utf8mb4_unicode_ci`). - [x] Added/updated automated tests - [x] A detailed QA plan exists on the associated ticket (if it isn't there, work with the product group's QA engineer to add it) - [x] Manual QA for all new/changed functionality (cherry picked from commit 62b7412)
1 parent 779f325 commit 8b08a37

21 files changed

+599
-267
lines changed

changes/25609-archive-encryption-keys

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Disk encryption keys are now archived when they are created or updated. They are never fully deleted from the database.

server/datastore/mysql/apple_mdm.go

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3551,27 +3551,6 @@ func (ds *Datastore) CleanupUnusedBootstrapPackages(ctx context.Context, pkgStor
35513551
return ctxerr.Wrap(ctx, err, "cleanup unused bootstrap packages")
35523552
}
35533553

3554-
func (ds *Datastore) CleanupDiskEncryptionKeysOnTeamChange(ctx context.Context, hostIDs []uint, newTeamID *uint) error {
3555-
return ds.withTx(ctx, func(tx sqlx.ExtContext) error {
3556-
return cleanupDiskEncryptionKeysOnTeamChangeDB(ctx, tx, hostIDs, newTeamID)
3557-
})
3558-
}
3559-
3560-
func cleanupDiskEncryptionKeysOnTeamChangeDB(ctx context.Context, tx sqlx.ExtContext, hostIDs []uint, newTeamID *uint) error {
3561-
_, err := getMDMAppleConfigProfileByTeamAndIdentifierDB(ctx, tx, newTeamID, mobileconfig.FleetFileVaultPayloadIdentifier)
3562-
if err != nil {
3563-
if fleet.IsNotFound(err) {
3564-
// the new team does not have a filevault profile so we need to delete the existing ones
3565-
if err := bulkDeleteHostDiskEncryptionKeysDB(ctx, tx, hostIDs); err != nil {
3566-
return ctxerr.Wrap(ctx, err, "reconcile filevault profiles on team change bulk delete host disk encryption keys")
3567-
}
3568-
} else {
3569-
return ctxerr.Wrap(ctx, err, "reconcile filevault profiles on team change get profile")
3570-
}
3571-
}
3572-
return nil
3573-
}
3574-
35753554
func getMDMAppleConfigProfileByTeamAndIdentifierDB(ctx context.Context, tx sqlx.QueryerContext, teamID *uint, profileIdentifier string) (*fleet.MDMAppleConfigProfile, error) {
35763555
if teamID == nil {
35773556
teamID = ptr.Uint(0)
@@ -3603,23 +3582,6 @@ WHERE
36033582
return &profile, nil
36043583
}
36053584

3606-
func bulkDeleteHostDiskEncryptionKeysDB(ctx context.Context, tx sqlx.ExtContext, hostIDs []uint) error {
3607-
if len(hostIDs) == 0 {
3608-
return nil
3609-
}
3610-
3611-
query, args, err := sqlx.In(
3612-
"DELETE FROM host_disk_encryption_keys WHERE host_id IN (?)",
3613-
hostIDs,
3614-
)
3615-
if err != nil {
3616-
return ctxerr.Wrap(ctx, err, "building query")
3617-
}
3618-
3619-
_, err = tx.ExecContext(ctx, query, args...)
3620-
return err
3621-
}
3622-
36233585
func (ds *Datastore) SetOrUpdateMDMAppleSetupAssistant(ctx context.Context, asst *fleet.MDMAppleSetupAssistant) (*fleet.MDMAppleSetupAssistant, error) {
36243586
const stmt = `
36253587
INSERT INTO

server/datastore/mysql/apple_mdm_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ func testUpdateHostTablesOnMDMUnenroll(t *testing.T, ds *Datastore) {
984984
var hostID uint
985985
err = sqlx.GetContext(context.Background(), ds.reader(context.Background()), &hostID, `SELECT id FROM hosts WHERE uuid = ?`, testUUID)
986986
require.NoError(t, err)
987-
err = ds.SetOrUpdateHostDiskEncryptionKey(ctx, hostID, "asdf", "", nil)
987+
err = ds.SetOrUpdateHostDiskEncryptionKey(ctx, &fleet.Host{ID: hostID}, "asdf", "", nil)
988988
require.NoError(t, err)
989989

990990
key, err := ds.GetHostDiskEncryptionKey(ctx, hostID)
@@ -1999,7 +1999,7 @@ func testAggregateMacOSSettingsStatusWithFileVault(t *testing.T, ds *Datastore)
19991999
require.Equal(t, uint(0), res.Verifying)
20002000
require.Equal(t, uint(0), res.Verified)
20012001

2002-
err = ds.SetOrUpdateHostDiskEncryptionKey(ctx, hosts[0].ID, "foo", "", nil)
2002+
err = ds.SetOrUpdateHostDiskEncryptionKey(ctx, hosts[0], "foo", "", nil)
20032003
require.NoError(t, err)
20042004
res, err = ds.GetMDMAppleProfilesSummary(ctx, nil)
20052005
require.NoError(t, err)
@@ -2041,7 +2041,7 @@ func testAggregateMacOSSettingsStatusWithFileVault(t *testing.T, ds *Datastore)
20412041
require.Equal(t, uint(0), res.Verifying)
20422042
require.Equal(t, uint(1), res.Verified) // hosts[0] now has filevault fully enforced and verified
20432043

2044-
err = ds.SetOrUpdateHostDiskEncryptionKey(ctx, hosts[1].ID, "bar", "", nil)
2044+
err = ds.SetOrUpdateHostDiskEncryptionKey(ctx, hosts[1], "bar", "", nil)
20452045
require.NoError(t, err)
20462046
err = ds.SetHostsDiskEncryptionKeyStatus(ctx, []uint{hosts[1].ID}, false, time.Now().Add(1*time.Hour))
20472047
require.NoError(t, err)
@@ -2107,7 +2107,7 @@ func testAggregateMacOSSettingsStatusWithFileVault(t *testing.T, ds *Datastore)
21072107
require.Equal(t, uint(0), res.Verifying)
21082108
require.Equal(t, uint(0), res.Verified)
21092109

2110-
err = ds.SetOrUpdateHostDiskEncryptionKey(ctx, hosts[9].ID, "baz", "", nil)
2110+
err = ds.SetOrUpdateHostDiskEncryptionKey(ctx, hosts[9], "baz", "", nil)
21112111
require.NoError(t, err)
21122112
err = ds.SetHostsDiskEncryptionKeyStatus(ctx, []uint{hosts[9].ID}, true, time.Now().Add(1*time.Hour))
21132113
require.NoError(t, err)
@@ -2652,10 +2652,11 @@ func testDeleteMDMAppleProfilesForHost(t *testing.T, ds *Datastore) {
26522652
require.Nil(t, gotProfs)
26532653
}
26542654

2655-
func createDiskEncryptionRecord(ctx context.Context, ds *Datastore, t *testing.T, hostId uint, key string, decryptable bool, threshold time.Time) {
2656-
err := ds.SetOrUpdateHostDiskEncryptionKey(ctx, hostId, key, "", nil)
2655+
func createDiskEncryptionRecord(ctx context.Context, ds *Datastore, t *testing.T, host *fleet.Host, key string, decryptable bool,
2656+
threshold time.Time) {
2657+
err := ds.SetOrUpdateHostDiskEncryptionKey(ctx, host, key, "", nil)
26572658
require.NoError(t, err)
2658-
err = ds.SetHostsDiskEncryptionKeyStatus(ctx, []uint{hostId}, decryptable, threshold)
2659+
err = ds.SetHostsDiskEncryptionKeyStatus(ctx, []uint{host.ID}, decryptable, threshold)
26592660
require.NoError(t, err)
26602661
}
26612662

@@ -2685,7 +2686,7 @@ func TestMDMAppleFileVaultSummary(t *testing.T) {
26852686
ctx, ds, t,
26862687
)
26872688
oneMinuteAfterThreshold := time.Now().Add(+1 * time.Minute)
2688-
createDiskEncryptionRecord(ctx, ds, t, verifyingHost.ID, "key-1", true, oneMinuteAfterThreshold)
2689+
createDiskEncryptionRecord(ctx, ds, t, verifyingHost, "key-1", true, oneMinuteAfterThreshold)
26892690

26902691
fvProfileSummary, err := ds.GetMDMAppleFileVaultSummary(ctx, nil)
26912692
require.NoError(t, err)
@@ -2871,7 +2872,7 @@ func TestMDMAppleFileVaultSummary(t *testing.T) {
28712872
require.NoError(t, err)
28722873

28732874
upsertHostCPs([]*fleet.Host{verifyingTeam1Host}, []*fleet.MDMAppleConfigProfile{team1FVProfile}, fleet.MDMOperationTypeInstall, &fleet.MDMDeliveryVerifying, ctx, ds, t)
2874-
createDiskEncryptionRecord(ctx, ds, t, verifyingTeam1Host.ID, "key-2", true, oneMinuteAfterThreshold)
2875+
createDiskEncryptionRecord(ctx, ds, t, verifyingTeam1Host, "key-2", true, oneMinuteAfterThreshold)
28752876

28762877
fvProfileSummary, err = ds.GetMDMAppleFileVaultSummary(ctx, &tm.ID)
28772878
require.NoError(t, err)

0 commit comments

Comments
 (0)