Skip to content

Commit

Permalink
Results for queries in 2017-packs should always log (#15486)
Browse files Browse the repository at this point in the history
#15168

- [X] Added/updated tests
- [X] Manual QA for all new/changed functionality
  • Loading branch information
lucasmrod authored and lukeheath committed Dec 7, 2023
1 parent bb1e297 commit 0e52a3e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
11 changes: 3 additions & 8 deletions server/service/osquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -1691,14 +1691,9 @@ func getQueryNameAndTeamIDFromResult(path string) (*uint, string, error) {
return &teamNumber, parts[2], nil
}

// For pattern: pack/PackName/Query (legacy pack)
if strings.HasPrefix(path, "pack/") {
parts := strings.SplitN(path, "/", 3)
if len(parts) != 3 {
return nil, "", fmt.Errorf("unknown format: %q", path)
}
return nil, parts[2], nil
}
// 2017/legacy packs with the format "pack/<Pack name>/<Query name> are
// considered unknown format (they are not considered global or team
// scheduled queries).

// If none of the above patterns match, return error
return nil, "", fmt.Errorf("unknown format: %q", path)
Expand Down
12 changes: 10 additions & 2 deletions server/service/osquery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,11 @@ func TestSubmitResultLogs(t *testing.T) {
// Fleet doesn't know of this query, so this result should be streamed as is (This is to support streaming results for osquery nodes that are configured outside of Fleet, e.g. `--config_plugin=filesystem`).
`{"snapshot":[{"hour":"20","minutes":"8"}],"action":"snapshot","name":"pack/Global/doesntexist","hostIdentifier":"1379f59d98f4","calendarTime":"Tue Jan 10 20:08:51 2017 UTC","unixTime":1484078931,"decorations":{"host_uuid":"EB714C9D-C1F8-A436-B6DA-3F853C5502EA"}}`,

// If a global query belongs to a 2017/legacy pack, it should be automated even if the global query has automations turned off.
`{"snapshot":[{"hour":"20","minutes":"8"}],"action":"snapshot","name":"pack/Some Pack Name/query_not_automated","hostIdentifier":"1379f59d98f4","calendarTime":"Tue Jan 10 20:08:51 2017 UTC","unixTime":1484078931,"decorations":{"host_uuid":"EB714C9D-C1F8-A436-B6DA-3F853C5502EA"}}`,

// The "name" field has invalid format, so this result will be streamed as is (This is to support streaming results for osquery nodes that are configured outside of Fleet, e.g. `--config_plugin=filesystem`).
`{"name":"com.foo.bar","hostIdentifier":"52eb420a-2085-438a-abf0-5670e97588e2","calendarTime":"Thu Dec 7 15:15:20 2023 UTC","unixTime":1701962120,"epoch":0,"counter":0,"numerics":false,"columns":{"foo": "bar"},"action":"snapshot"}`,
`{"snapshot":[{"hour":"20","minutes":"8"}],"action":"snapshot","name":"some_name","hostIdentifier":"1379f59d98f4","calendarTime":"Tue Jan 10 20:08:51 2017 UTC","unixTime":1484078931,"decorations":{"host_uuid":"EB714C9D-C1F8-A436-B6DA-3F853C5502EA"}}`,
`{"snapshot":[{"hour":"20","minutes":"8"}],"action":"snapshot","name":"pack/team-foo/bar","hostIdentifier":"1379f59d98f4","calendarTime":"Tue Jan 10 20:08:51 2017 UTC","unixTime":1484078931,"decorations":{"host_uuid":"EB714C9D-C1F8-A436-B6DA-3F853C5502EA"}}`,
`{"snapshot":[{"hour":"20","minutes":"8"}],"action":"snapshot","name":"pack/team-","hostIdentifier":"1379f59d98f4","calendarTime":"Tue Jan 10 20:08:51 2017 UTC","unixTime":1484078931,"decorations":{"host_uuid":"EB714C9D-C1F8-A436-B6DA-3F853C5502EA"}}`,
Expand Down Expand Up @@ -756,13 +760,17 @@ func TestGetQueryNameAndTeamIDFromResult(t *testing.T) {
{"pack/Global/Query Name", nil, "Query Name", false},
{"pack/team-1/Query Name", ptr.Uint(1), "Query Name", false},
{"pack/team-12345/Another Query", ptr.Uint(12345), "Another Query", false},
{"pack/PackName/Query", nil, "Query", false}, // Legacy Pack support
{"pack/team-foo/Query", nil, "", true},
{"pack/Global/QueryWith/Slash", nil, "QueryWith/Slash", false},
{"pack/team-1/QueryWith/Slash", ptr.Uint(1), "QueryWith/Slash", false},
{"pack/PackName/QueryWith/Slash", nil, "QueryWith/Slash", false}, // Legacy Pack support

{"InvalidString", nil, "", true},
{"Invalid/Query", nil, "", true},

// Legacy 2017 packs should fail the parsing as they are separate
// from global or team queries.
{"pack/PackName/Query", nil, "", true},
{"pack/PackName/QueryWith/Slash", nil, "", true},
}

for _, tt := range tests {
Expand Down

0 comments on commit 0e52a3e

Please sign in to comment.