-
Notifications
You must be signed in to change notification settings - Fork 427
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleanup query results after host is transferred to another team (#18712)
#18079 - [X] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://fleetdm.com/docs/contributing/committing-changes#changes-files) for more information. - [X] Input data is properly validated, `SELECT *` is avoided, SQL injection is prevented (using placeholders for values in statements) - ~[ ] Added support on fleet's osquery simulator `cmd/osquery-perf` for new osquery data ingestion features.~ - [X] Added/updated tests - [X] If database migrations are included, checked table schema to confirm autoupdate - For database migrations: - ~[ ] Checked schema for all modified table for columns that will auto-update timestamps during migration.~ - ~[ ] Confirmed that updating the timestamps is acceptable, and will not cause unwanted side effects.~ - ~[ ] Ensured the correct collation is explicitly set for character columns (`COLLATE utf8mb4_unicode_ci`).~ - [X] Manual QA for all new/changed functionality - ~For Orbit and Fleet Desktop changes:~ - ~[ ] Manual QA must be performed in the three main OSs, macOS, Windows and Linux.~ - ~[ ] Auto-update manual QA, from released version of component to new version (see [tools/tuf/test](../tools/tuf/test/README.md)).~
- Loading branch information
Showing
9 changed files
with
403 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* Fixed bug where hosts query results were not cleared after transferring the host to other teams. |
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
30 changes: 30 additions & 0 deletions
30
server/datastore/mysql/migrations/tables/20240430111727_CleanupQueryResults.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,30 @@ | ||
package tables | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
) | ||
|
||
func init() { | ||
MigrationClient.AddMigration(Up_20240430111727, Down_20240430111727) | ||
} | ||
|
||
func Up_20240430111727(tx *sql.Tx) error { | ||
// This cleanup correspond to the following bug: https://github.com/fleetdm/fleet/issues/18079. | ||
// The following deletes "team query results" that do not match the host's team. | ||
_, err := tx.Exec(` | ||
DELETE qr | ||
FROM query_results qr | ||
JOIN queries q ON (q.id=qr.query_id) | ||
JOIN hosts h ON (h.id=qr.host_id) | ||
WHERE q.team_id IS NOT NULL AND q.team_id != COALESCE(h.team_id, 0); | ||
`) | ||
if err != nil { | ||
return fmt.Errorf("failed to delete query_results %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
func Down_20240430111727(tx *sql.Tx) error { | ||
return nil | ||
} |
98 changes: 98 additions & 0 deletions
98
server/datastore/mysql/migrations/tables/20240430111727_CleanupQueryResults_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,98 @@ | ||
package tables | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestUp_20240430111727(t *testing.T) { | ||
db := applyUpToPrev(t) | ||
|
||
hostID := 1 | ||
newTeam := func(name string) uint { | ||
return uint(execNoErrLastID(t, db, | ||
`INSERT INTO teams (name) VALUES (?);`, | ||
name, | ||
)) | ||
} | ||
newHost := func(teamID *uint) uint { | ||
id := fmt.Sprintf("%d", hostID) | ||
hostID++ | ||
return uint(execNoErrLastID(t, db, | ||
`INSERT INTO hosts (osquery_host_id, node_key, team_id) VALUES (?, ?, ?);`, | ||
id, id, teamID, | ||
)) | ||
} | ||
newQuery := func(name string, teamID *uint) uint { | ||
return uint(execNoErrLastID(t, db, | ||
`INSERT INTO queries (name, description, logging_type, team_id, query, saved) VALUES (?, '', 'snapshot', ?, 'SELECT 1;', 1);`, | ||
name, teamID, | ||
)) | ||
} | ||
newQueryResults := func(queryID, hostID uint, resultCount int) { | ||
var args []interface{} | ||
for i := 0; i < resultCount; i++ { | ||
args = append(args, queryID, hostID, fmt.Sprintf(`{"foo": "bar%d"}`, i)) | ||
} | ||
values := strings.TrimSuffix(strings.Repeat("(?, ?, ?, NOW()),", resultCount), ",") | ||
_, err := db.Exec(fmt.Sprintf(`INSERT INTO query_results (query_id, host_id, data, last_fetched) VALUES %s;`, values), | ||
args..., | ||
) | ||
require.NoError(t, err) | ||
} | ||
|
||
team1ID := newTeam("team1") | ||
team2ID := newTeam("team2") | ||
host1GlobalID := newHost(nil) | ||
host2Team1ID := newHost(&team1ID) | ||
host3Team2ID := newHost(&team2ID) | ||
query1GlobalID := newQuery("query1Global", nil) | ||
query2Team1ID := newQuery("query2Team1", &team1ID) | ||
query3Team2ID := newQuery("query3Team2", &team2ID) | ||
|
||
newQueryResults(query1GlobalID, host1GlobalID, 1) | ||
newQueryResults(query1GlobalID, host2Team1ID, 2) | ||
newQueryResults(query1GlobalID, host3Team2ID, 3) | ||
|
||
newQueryResults(query2Team1ID, host1GlobalID, 4) | ||
newQueryResults(query2Team1ID, host2Team1ID, 5) | ||
newQueryResults(query2Team1ID, host3Team2ID, 6) | ||
|
||
newQueryResults(query3Team2ID, host1GlobalID, 7) | ||
newQueryResults(query3Team2ID, host2Team1ID, 8) | ||
newQueryResults(query3Team2ID, host3Team2ID, 9) | ||
|
||
// Apply current migration. | ||
applyNext(t, db) | ||
|
||
getQueryResultsCount := func(queryID, hostID uint) int { | ||
var count int | ||
err := db.Get(&count, `SELECT COUNT(*) FROM query_results WHERE query_id = ? AND host_id = ?`, queryID, hostID) | ||
require.NoError(t, err) | ||
return count | ||
} | ||
|
||
count := getQueryResultsCount(query1GlobalID, host1GlobalID) | ||
require.Equal(t, 1, count) // result for global queries are not deleted. | ||
count = getQueryResultsCount(query1GlobalID, host2Team1ID) | ||
require.Equal(t, 2, count) // result for global queries are not deleted. | ||
count = getQueryResultsCount(query1GlobalID, host3Team2ID) | ||
require.Equal(t, 3, count) // result for global queries are not deleted. | ||
|
||
count = getQueryResultsCount(query2Team1ID, host1GlobalID) | ||
require.Equal(t, 0, count) // query results of a team query different than the host's team are deleted. | ||
count = getQueryResultsCount(query2Team1ID, host2Team1ID) | ||
require.Equal(t, 5, count) // team query results of the host's team are not deleted. | ||
count = getQueryResultsCount(query2Team1ID, host3Team2ID) | ||
require.Equal(t, 0, count) // query results of a team query different than the host's team are deleted. | ||
|
||
count = getQueryResultsCount(query3Team2ID, host1GlobalID) | ||
require.Equal(t, 0, count) // query results of a team query different than the host's team are deleted. | ||
count = getQueryResultsCount(query3Team2ID, host2Team1ID) | ||
require.Equal(t, 0, count) // query results of a team query different than the host's team are deleted. | ||
count = getQueryResultsCount(query3Team2ID, host3Team2ID) | ||
require.Equal(t, 9, count) // team query results of the host's team are not deleted. | ||
} |
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
Oops, something went wrong.