-
Notifications
You must be signed in to change notification settings - Fork 483
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cherry pick for
last_enrolled_at
fix (#20375)
Cherry pick for #20173.
- Loading branch information
Showing
7 changed files
with
195 additions
and
21 deletions.
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
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.