-
Notifications
You must be signed in to change notification settings - Fork 483
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix last_enrolled_at
for macOS devices when re-enrolling to MDM
#20173
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8927af9
fix last_enrolled_at for macOS devices when re-enrolling to MDM
lucasmrod 56ace10
Fix tests
lucasmrod b2ab3fd
Merge branch 'main' into 20059-fix-last_enrolled_at
lucasmrod 63db14c
Add migration to fix last_enrolled_at issue
lucasmrod d9e5e7c
Merge branch 'main' into 20059-fix-last_enrolled_at
lucasmrod 1e4a825
Fix error string
lucasmrod 28df767
Merge branch 'main' into 20059-fix-last_enrolled_at
lucasmrod File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* Fixed bug that set `Added to Fleet` to `Never` after macOS hosts re-enrolled to Fleet via MDM. | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -784,30 +784,37 @@ func updateMDMAppleHostDB( | |
) error { | ||
refetchRequested, lastEnrolledAt := mdmHostEnrollFields(mdmHost) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if it would make things easier to maintain to split up this abstraction and calculate these two values separately when needed? |
||
|
||
updateStmt := ` | ||
UPDATE hosts SET | ||
hardware_serial = ?, | ||
uuid = ?, | ||
hardware_model = ?, | ||
platform = ?, | ||
refetch_requested = ?, | ||
last_enrolled_at = ?, | ||
osquery_host_id = COALESCE(NULLIF(osquery_host_id, ''), ?) | ||
WHERE id = ?` | ||
|
||
if _, err := tx.ExecContext( | ||
ctx, | ||
updateStmt, | ||
args := []interface{}{ | ||
mdmHost.HardwareSerial, | ||
mdmHost.UUID, | ||
mdmHost.HardwareModel, | ||
mdmHost.Platform, | ||
refetchRequested, | ||
lastEnrolledAt, | ||
// Set osquery_host_id to the device UUID only if it is not already set. | ||
mdmHost.UUID, | ||
hostID, | ||
); err != nil { | ||
} | ||
|
||
// Only update last_enrolled_at if this is a iOS/iPadOS device. | ||
// macOS should not update last_enrolled_at as it is set when osquery enrolls. | ||
lastEnrolledAtColumn := "" | ||
if mdmHost.Platform == "ios" || mdmHost.Platform == "ipados" { | ||
lastEnrolledAtColumn = "last_enrolled_at = ?," | ||
args = append([]interface{}{lastEnrolledAt}, args...) | ||
} | ||
|
||
updateStmt := fmt.Sprintf(` | ||
UPDATE hosts SET | ||
%s | ||
hardware_serial = ?, | ||
uuid = ?, | ||
hardware_model = ?, | ||
platform = ?, | ||
refetch_requested = ?, | ||
osquery_host_id = COALESCE(NULLIF(osquery_host_id, ''), ?) | ||
WHERE id = ?`, lastEnrolledAtColumn) | ||
|
||
if _, err := tx.ExecContext(ctx, updateStmt, args...); err != nil { | ||
return ctxerr.Wrap(ctx, err, "update mdm apple host") | ||
} | ||
|
||
|
36 changes: 36 additions & 0 deletions
36
server/datastore/mysql/migrations/tables/20240710155623_FixResetLastEnrolledAt.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package tables | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
) | ||
|
||
func init() { | ||
MigrationClient.AddMigration(Up_20240710155623, Down_20240710155623) | ||
} | ||
|
||
func Up_20240710155623(tx *sql.Tx) error { | ||
// `hosts.last_enrolled_at` contains the date the osquery agent enrolled. | ||
// | ||
// A bug in v4.51.0 caused the `last_enrolled_at` column to be set to | ||
// '2000-01-01 00:00:00' (aka the "Never" date) when macOS hosts perform a | ||
// MDM re-enrollment (see https://github.com/fleetdm/fleet/issues/20059 | ||
// for more details). | ||
// | ||
// We cannot restore the exact date of the osquery enrollment but | ||
// `host_disks.created_at` is a good approximation. | ||
if _, err := tx.Exec(` | ||
UPDATE hosts h | ||
JOIN host_disks hd ON h.id=hd.host_id | ||
SET h.last_enrolled_at = hd.created_at, h.updated_at = h.updated_at | ||
WHERE h.platform = 'darwin' AND h.last_enrolled_at = '2000-01-01 00:00:00';`, | ||
); err != nil { | ||
return fmt.Errorf("failed to update hosts.last_enrolled_at: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func Down_20240710155623(tx *sql.Tx) error { | ||
return nil | ||
} |
94 changes: 94 additions & 0 deletions
94
server/datastore/mysql/migrations/tables/20240710155623_FixResetLastEnrolledAt_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package tables | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestUp_20240710155623(t *testing.T) { | ||
db := applyUpToPrev(t) | ||
|
||
i := uint(1) | ||
newHost := func(platform, lastEnrolledAt string, hostDisk bool) uint { | ||
id := fmt.Sprintf("%d", i) | ||
i++ | ||
hostID := uint(execNoErrLastID(t, db, | ||
`INSERT INTO hosts (osquery_host_id, node_key, uuid, platform, last_enrolled_at) VALUES (?, ?, ?, ?, ?);`, | ||
id, id, id, platform, lastEnrolledAt, | ||
)) | ||
if hostDisk { | ||
execNoErr(t, db, | ||
`INSERT INTO host_disks (host_id) VALUES (?);`, | ||
hostID, | ||
) | ||
} | ||
return hostID | ||
} | ||
neverDate := "2000-01-01 00:00:00" | ||
ubuntuHostID := newHost("ubuntu", neverDate, true) // non-darwin hosts should not be updated. | ||
validMacOSHostID := newHost("darwin", "2024-07-08 18:00:53", true) // host without the issue, should not be updated. | ||
pendingMacOSDEPHostID := newHost("darwin", neverDate, false) // host without the issue (e.g. DEP pending, not enrolled), should not be updated. | ||
invalidMacOSHostID := newHost("darwin", neverDate, true) // host with the issue, should be updated. | ||
|
||
const getColumnsQuery = ` | ||
SELECT h.last_enrolled_at, h.updated_at, hd.created_at AS host_disks_created_at | ||
FROM hosts h LEFT JOIN host_disks hd ON h.id=hd.host_id WHERE h.id = ?;` | ||
type hostTimestamps struct { | ||
LastEnrolledAt time.Time `db:"last_enrolled_at"` | ||
UpdatedAt time.Time `db:"updated_at"` | ||
HostDisksCreatedAt *time.Time `db:"host_disks_created_at"` | ||
} | ||
var ubuntuTimestampsBefore hostTimestamps | ||
err := db.Get(&ubuntuTimestampsBefore, getColumnsQuery, ubuntuHostID) | ||
require.NoError(t, err) | ||
require.NotZero(t, ubuntuTimestampsBefore.UpdatedAt) | ||
require.Equal(t, ubuntuTimestampsBefore.LastEnrolledAt.Format("2006-01-02 15:04:05"), neverDate) | ||
require.NotNil(t, ubuntuTimestampsBefore.HostDisksCreatedAt) | ||
require.NotZero(t, *ubuntuTimestampsBefore.HostDisksCreatedAt) | ||
var validMacOSTimestampsBefore hostTimestamps | ||
err = db.Get(&validMacOSTimestampsBefore, getColumnsQuery, validMacOSHostID) | ||
require.NoError(t, err) | ||
require.NotZero(t, validMacOSTimestampsBefore.UpdatedAt) | ||
require.Equal(t, validMacOSTimestampsBefore.LastEnrolledAt.Format("2006-01-02 15:04:05"), "2024-07-08 18:00:53") | ||
require.NotNil(t, validMacOSTimestampsBefore.HostDisksCreatedAt) | ||
require.NotZero(t, *validMacOSTimestampsBefore.HostDisksCreatedAt) | ||
var pendingMacOSDEPTimestampsBefore hostTimestamps | ||
err = db.Get(&pendingMacOSDEPTimestampsBefore, getColumnsQuery, pendingMacOSDEPHostID) | ||
require.NoError(t, err) | ||
require.NotZero(t, pendingMacOSDEPTimestampsBefore.UpdatedAt) | ||
require.Equal(t, pendingMacOSDEPTimestampsBefore.LastEnrolledAt.Format("2006-01-02 15:04:05"), neverDate) | ||
require.Nil(t, pendingMacOSDEPTimestampsBefore.HostDisksCreatedAt) | ||
var invalidMacOSTimestampsBefore hostTimestamps | ||
err = db.Get(&invalidMacOSTimestampsBefore, getColumnsQuery, invalidMacOSHostID) | ||
require.NoError(t, err) | ||
require.NotZero(t, invalidMacOSTimestampsBefore.UpdatedAt) | ||
require.Equal(t, invalidMacOSTimestampsBefore.LastEnrolledAt.Format("2006-01-02 15:04:05"), neverDate) | ||
require.NotNil(t, invalidMacOSTimestampsBefore.HostDisksCreatedAt) | ||
require.NotZero(t, *invalidMacOSTimestampsBefore.HostDisksCreatedAt) | ||
|
||
// Apply current migration. | ||
applyNext(t, db) | ||
|
||
var ubuntuTimestampsAfter hostTimestamps | ||
err = db.Get(&ubuntuTimestampsAfter, getColumnsQuery, ubuntuHostID) | ||
require.NoError(t, err) | ||
require.Equal(t, ubuntuTimestampsBefore, ubuntuTimestampsAfter) | ||
var validMacOSTimestampsAfter hostTimestamps | ||
err = db.Get(&validMacOSTimestampsAfter, getColumnsQuery, validMacOSHostID) | ||
require.NoError(t, err) | ||
require.Equal(t, validMacOSTimestampsBefore, validMacOSTimestampsAfter) | ||
var pendingMacOSDEPTimestampsAfter hostTimestamps | ||
err = db.Get(&pendingMacOSDEPTimestampsAfter, getColumnsQuery, pendingMacOSDEPHostID) | ||
require.NoError(t, err) | ||
require.Equal(t, pendingMacOSDEPTimestampsBefore.UpdatedAt, pendingMacOSDEPTimestampsAfter.UpdatedAt) // updated_at is unmodified | ||
require.Equal(t, neverDate, pendingMacOSDEPTimestampsAfter.LastEnrolledAt.Format("2006-01-02 15:04:05")) // last_enrolled_at was not modified | ||
var invalidMacOSTimestampsAfter hostTimestamps | ||
err = db.Get(&invalidMacOSTimestampsAfter, getColumnsQuery, invalidMacOSHostID) | ||
require.NoError(t, err) | ||
require.Equal(t, invalidMacOSTimestampsBefore.UpdatedAt, invalidMacOSTimestampsAfter.UpdatedAt) // updated_at is unmodified | ||
require.NotNil(t, invalidMacOSTimestampsAfter.HostDisksCreatedAt) | ||
require.Equal(t, *invalidMacOSTimestampsAfter.HostDisksCreatedAt, invalidMacOSTimestampsAfter.LastEnrolledAt) // last_enrolled_at was updated to host_disks date | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ℹ️ info: Documented the fix for the
last_enrolled_at
field issue for macOS re-enrollment.