diff --git a/changes/47963-enroll-500-os-updates-settings-not-found b/changes/47963-enroll-500-os-updates-settings-not-found new file mode 100644 index 0000000000..e67a0a6489 --- /dev/null +++ b/changes/47963-enroll-500-os-updates-settings-not-found @@ -0,0 +1 @@ +- Fixed a 500 error during Apple MDM enrollment when a host had no DEP assignment yet (e.g. the enrollment request arrived before the host/DEP assignment row was created or replicated). The OS updates settings lookup now returns a not-found error so enrollment proceeds gracefully instead of failing. diff --git a/server/datastore/mysql/apple_mdm.go b/server/datastore/mysql/apple_mdm.go index 124cb90ae0..554f81f73f 100644 --- a/server/datastore/mysql/apple_mdm.go +++ b/server/datastore/mysql/apple_mdm.go @@ -6747,6 +6747,13 @@ LIMIT 1` Platform string `db:"platform"` } if err := sqlx.GetContext(ctx, ds.reader(ctx), &dest, stmt, serial); err != nil { + if errors.Is(err, sql.ErrNoRows) { + // The host may not have a DEP assignment yet (e.g. the enrollment + // request arrived before the host/DEP assignment row was created or + // replicated). Return a not-found error so callers can skip the OS + // updates check and allow enrollment to proceed. + return "", nil, ctxerr.Wrap(ctx, notFound("Host").WithName(serial), "getting team id for host") + } return "", nil, ctxerr.Wrap(ctx, err, "getting team id for host") } diff --git a/server/datastore/mysql/apple_mdm_test.go b/server/datastore/mysql/apple_mdm_test.go index c17f94cb87..5f2e162a30 100644 --- a/server/datastore/mysql/apple_mdm_test.go +++ b/server/datastore/mysql/apple_mdm_test.go @@ -9539,18 +9539,20 @@ func TestGetMDMAppleOSUpdatesSettingsByHostSerial(t *testing.T) { Platform: "macos", HardwareSerial: "non-dep-serial", }) + require.NoError(t, err) - // non-DEP host should return not found + // non-DEP host should return a not-found error (so callers can skip the + // OS updates check and allow enrollment to proceed) _, _, err = ds.GetMDMAppleOSUpdatesSettingsByHostSerial(context.Background(), "non-dep-serial") - require.ErrorIs(t, err, sql.ErrNoRows) + require.True(t, fleet.IsNotFound(err), "expected not found error, got %v", err) - // deleted DEP host should return not found + // deleted DEP host should return a not-found error ExecAdhocSQL(t, ds, func(q sqlx.ExtContext) error { _, err := q.ExecContext(context.Background(), "UPDATE host_dep_assignments SET deleted_at = NOW() WHERE host_id = ?", hostIDsByKey["macos"]) return err }) _, _, err = ds.GetMDMAppleOSUpdatesSettingsByHostSerial(context.Background(), devicesByKey["macos"].SerialNumber) - require.ErrorIs(t, err, sql.ErrNoRows) + require.True(t, fleet.IsNotFound(err), "expected not found error, got %v", err) } func testMDMManagedSCEPCertificates(t *testing.T, ds *Datastore) {