Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion server/datastore/mysql/apple_mdm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2662,7 +2662,8 @@ func (ds *Datastore) bulkDeleteMDMAppleHostsConfigProfilesDB(ctx context.Context
// NOTE If onlyProfileUUIDs is provided (not nil), only profiles with
// those UUIDs will be update instead of rebuilding all pending
// profiles for hosts
func (ds *Datastore) bulkSetPendingMDMAppleHostProfilesDB(
// TODO(MHJ): Marked for deletion
func (ds *Datastore) bulkSetPendingMDMAppleHostProfilesDB( //nolint:unused // Will be cleaned up in a follow up PR.
ctx context.Context,
tx sqlx.ExtContext,
hostUUIDs []string,
Expand Down
49 changes: 21 additions & 28 deletions server/datastore/mysql/apple_mdm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ import (
"github.com/fleetdm/fleet/v4/server/mdm/nanodep/godep"
"github.com/fleetdm/fleet/v4/server/mdm/nanomdm/mdm"
"github.com/fleetdm/fleet/v4/server/mdm/nanomdm/push"
"github.com/fleetdm/fleet/v4/server/mock"
"github.com/fleetdm/fleet/v4/server/platform/logging"
common_mysql "github.com/fleetdm/fleet/v4/server/platform/mysql"
"github.com/fleetdm/fleet/v4/server/ptr"
"github.com/fleetdm/fleet/v4/server/service"
"github.com/fleetdm/fleet/v4/server/test"
"github.com/go-sql-driver/mysql"
"github.com/google/uuid"
Expand Down Expand Up @@ -6536,6 +6538,8 @@ func testScreenDEPAssignProfileSerialsForCooldown(t *testing.T, ds *Datastore) {

func testMDMAppleDDMDeclarationsToken(t *testing.T, ds *Datastore) {
ctx := t.Context()
SetTestABMAssets(t, ds, "fleet")

toks, err := ds.MDMAppleDDMDeclarationsToken(ctx, "not-exists")
require.NoError(t, err)
require.Empty(t, toks.DeclarationsToken)
Expand All @@ -6546,11 +6550,8 @@ func testMDMAppleDDMDeclarationsToken(t *testing.T, ds *Datastore) {
RawJSON: json.RawMessage(`{"Identifier": "decl-1"}`),
}, nil)
require.NoError(t, err)
updates, err := ds.BulkSetPendingMDMHostProfiles(ctx, nil, nil, []string{decl.DeclarationUUID}, nil)
require.NoError(t, err)
assert.False(t, updates.AppleConfigProfile)
assert.False(t, updates.AppleDeclaration)
assert.False(t, updates.WindowsConfigProfile)
commander, _ := createMDMAppleCommanderAndStorage(t, ds)
require.NoError(t, service.ReconcileAppleDeclarationsBatched(ctx, ds, commander, ds.logger))
Comment thread
MagnusHJensen marked this conversation as resolved.

toks, err = ds.MDMAppleDDMDeclarationsToken(ctx, "not-exists")
require.NoError(t, err)
Expand All @@ -6567,29 +6568,22 @@ func testMDMAppleDDMDeclarationsToken(t *testing.T, ds *Datastore) {
})
require.NoError(t, err)
nanoEnroll(t, ds, host1, true)
updates, err = ds.BulkSetPendingMDMHostProfiles(ctx, nil, nil, []string{decl.DeclarationUUID}, nil)
require.NoError(t, err)
assert.False(t, updates.AppleConfigProfile)
assert.True(t, updates.AppleDeclaration)
assert.False(t, updates.WindowsConfigProfile)

require.NoError(t, service.ReconcileAppleDeclarationsBatched(ctx, ds, commander, ds.logger))

toks, err = ds.MDMAppleDDMDeclarationsToken(ctx, host1.UUID)
require.NoError(t, err)
require.NotEmpty(t, toks.DeclarationsToken)
require.NotZero(t, toks.Timestamp)
oldTok := toks.DeclarationsToken

decl2, err := ds.NewMDMAppleDeclaration(ctx, &fleet.MDMAppleDeclaration{
_, err = ds.NewMDMAppleDeclaration(ctx, &fleet.MDMAppleDeclaration{
Identifier: "decl-2",
Name: "decl-2",
RawJSON: json.RawMessage(`{"Identifier": "decl-2"}`),
}, nil)
require.NoError(t, err)
updates, err = ds.BulkSetPendingMDMHostProfiles(ctx, nil, nil, []string{decl2.DeclarationUUID}, nil)
require.NoError(t, err)
assert.False(t, updates.AppleConfigProfile)
assert.True(t, updates.AppleDeclaration)
assert.False(t, updates.WindowsConfigProfile)
require.NoError(t, service.ReconcileAppleDeclarationsBatched(ctx, ds, commander, ds.logger))

toks, err = ds.MDMAppleDDMDeclarationsToken(ctx, host1.UUID)
require.NoError(t, err)
Expand All @@ -6600,11 +6594,7 @@ func testMDMAppleDDMDeclarationsToken(t *testing.T, ds *Datastore) {

err = ds.DeleteMDMAppleDeclaration(ctx, decl.DeclarationUUID)
require.NoError(t, err)
updates, err = ds.BulkSetPendingMDMHostProfiles(ctx, nil, nil, []string{decl2.DeclarationUUID}, nil)
require.NoError(t, err)
assert.False(t, updates.AppleConfigProfile)
assert.False(t, updates.AppleDeclaration) // This is false because we delete references in `host_mdm_apple_declarations` for declarations that aren't sent to the host
assert.False(t, updates.WindowsConfigProfile)
require.NoError(t, service.ReconcileAppleDeclarationsBatched(ctx, ds, commander, ds.logger))

toks, err = ds.MDMAppleDDMDeclarationsToken(ctx, host1.UUID)
require.NoError(t, err)
Expand Down Expand Up @@ -6770,6 +6760,7 @@ func testSetOrUpdateMDMAppleDDMDeclaration(t *testing.T, ds *Datastore) {

func testDeleteMDMAppleDeclarationWithPendingInstalls(t *testing.T, ds *Datastore) {
ctx := t.Context()
SetTestABMAssets(t, ds, "fleet")

decl, err := ds.NewMDMAppleDeclaration(ctx, &fleet.MDMAppleDeclaration{
Identifier: "decl-1",
Expand All @@ -6789,8 +6780,8 @@ func testDeleteMDMAppleDeclarationWithPendingInstalls(t *testing.T, ds *Datastor
require.NoError(t, err)
nanoEnroll(t, ds, host, true)

_, err = ds.BulkSetPendingMDMHostProfiles(ctx, nil, nil, []string{decl.DeclarationUUID}, nil)
require.NoError(t, err)
commander, _ := createMDMAppleCommanderAndStorage(t, ds)
require.NoError(t, service.ReconcileAppleDeclarationsBatched(ctx, ds, commander, ds.logger))
Comment thread
MagnusHJensen marked this conversation as resolved.

// verify the correct state of the declaration for the host
profs, err := ds.GetHostMDMAppleProfiles(ctx, host.UUID)
Expand Down Expand Up @@ -7912,6 +7903,7 @@ func testIngestMDMAppleDevicesFromDEPSyncIOSIPadOS(t *testing.T, ds *Datastore)

func testMDMAppleProfilesOnIOSIPadOS(t *testing.T, ds *Datastore) {
ctx := t.Context()
SetTestABMAssets(t, ds, "fleet")

// Add the Fleetd configuration and profile that are only for macOS.
params := mobileconfig.FleetdProfileOptions{
Expand Down Expand Up @@ -7977,11 +7969,12 @@ func testMDMAppleProfilesOnIOSIPadOS(t *testing.T, ds *Datastore) {
someProfile, err := ds.NewMDMAppleConfigProfile(ctx, *generateAppleCP("a", "a", 0), nil)
require.NoError(t, err)

updates, err := ds.BulkSetPendingMDMHostProfiles(ctx, nil, []uint{0}, nil, nil)
require.NoError(t, err)
assert.True(t, updates.AppleConfigProfile)
assert.False(t, updates.AppleDeclaration)
assert.False(t, updates.WindowsConfigProfile)
commander, _ := createMDMAppleCommanderAndStorage(t, ds)
mockKV := new(mock.AdvancedKVStore)
mockKV.MGetFunc = func(ctx context.Context, keys []string) (map[string]*string, error) {
return make(map[string]*string), nil
}
require.NoError(t, service.ReconcileAppleProfilesBatched(ctx, ds, commander, mockKV, ds.logger, 0))

profiles, err := ds.GetHostMDMAppleProfiles(ctx, "iOS0_UUID")
require.NoError(t, err)
Expand Down
29 changes: 4 additions & 25 deletions server/datastore/mysql/mdm.go
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,7 @@ func (ds *Datastore) CleanupAllHostMDMProfilesForPlatform(ctx context.Context, p
})
}

// TODO(MHJ): Document the oddity this now only handles Android

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth dropping a note in #g-power-to-pc about this I saw them making some other Android profile reconciliation changes

func (ds *Datastore) BulkSetPendingMDMHostProfiles(
ctx context.Context,
hostIDs, teamIDs []uint,
Expand Down Expand Up @@ -917,6 +918,7 @@ func (ds *Datastore) BulkSetPendingMDMHostProfiles(
// batchSetMDMWindowsProfilesDB, which is the transactional source of
// truth. Tests that opt in via Datastore.EnableTestWindowsEagerHook
// get Apple-parity synchronous reconciliation here.
// TODO(MHJ): Marked for deletion
if ds.testWindowsEagerHook != nil {
updates.WindowsConfigProfile, err = ds.testWindowsEagerHook(ctx, winHosts, profileUUIDs)
if err != nil {
Expand All @@ -935,6 +937,7 @@ func (ds *Datastore) BulkSetPendingMDMHostProfiles(
// reconciliation is NOT performed here; the resolved Windows host UUIDs are
// returned so the caller can optionally reconcile them (only the test path
// does so; production relies on the mdm_windows_profile_manager cron).
// TODO(MHJ): Major cleanup required here
func (ds *Datastore) bulkSetPendingMDMHostProfilesDB(
ctx context.Context,
tx sqlx.ExtContext,
Expand Down Expand Up @@ -1108,7 +1111,7 @@ OR
for _, h := range hosts {
switch h.Platform {
case "darwin", "ios", "ipados":
appleHosts = append(appleHosts, h.UUID)
appleHosts = append(appleHosts, h.UUID) //nolint:staticcheck // Will be cleaned up in follow-up PR
case "windows":
Comment thread
MagnusHJensen marked this conversation as resolved.
if collectWinHosts {
winHosts = append(winHosts, h.UUID)
Expand All @@ -1123,35 +1126,11 @@ OR
}
}

updates.AppleConfigProfile, err = ds.bulkSetPendingMDMAppleHostProfilesDB(ctx, tx, appleHosts, profileUUIDs)
if err != nil {
return updates, nil, ctxerr.Wrap(ctx, err, "bulk set pending apple host profiles")
}

updates.AndroidConfigProfile, err = ds.bulkSetPendingMDMAndroidHostProfilesDB(ctx, androidHosts)
if err != nil {
return updates, nil, ctxerr.Wrap(ctx, err, "bulk set pending android host profiles")
}

const defaultBatchSize = 1000
batchSize := defaultBatchSize
if ds.testUpsertMDMDesiredProfilesBatchSize > 0 {
batchSize = ds.testUpsertMDMDesiredProfilesBatchSize
}
// TODO(roberto): this method currently sets the state of all
// declarations for all hosts. I don't see an immediate concern
// (and my hunch is that we could even do the same for
// profiles) but this could be optimized to use only a provided
// set of host uuids.
//
// Note(victor): Why is the status being set to nil? Shouldn't it be set to pending?
// Or at least pending for install and nil for remove profiles. Please update this comment if you know.
// This method is called bulkSetPendingMDMHostProfilesDB, so it is confusing that the status is NOT explicitly set to pending.
_, updates.AppleDeclaration, err = mdmAppleBatchSetHostDeclarationStateDB(ctx, tx, batchSize, nil)
if err != nil {
return updates, nil, ctxerr.Wrap(ctx, err, "bulk set pending apple declarations")
}

return updates, winHosts, nil
}

Expand Down
Loading
Loading