Skip to content

Commit f8206db

Browse files
lucasmrodlukeheath
authored andcommitted
Adding support for codesign table on osquery-perf (#23855)
I missed to add support for load testing in: #23766.
1 parent f2fb3e1 commit f8206db

File tree

1 file changed

+50
-4
lines changed

1 file changed

+50
-4
lines changed

cmd/osquery-perf/agent.go

+50-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"net/http"
1919
_ "net/http/pprof"
2020
"os"
21+
"sort"
2122
"strconv"
2223
"strings"
2324
"sync"
@@ -2097,8 +2098,9 @@ func (a *agent) runLiveQuery(query string) (results []map[string]string, status
20972098
}
20982099
}
20992100

2100-
func (a *agent) processQuery(name, query string) (
2101-
handled bool, results []map[string]string, status *fleet.OsqueryStatus, message *string, stats *fleet.Stats,
2101+
func (a *agent) processQuery(name, query string, cachedResults *cachedResults) (
2102+
handled bool, results []map[string]string,
2103+
status *fleet.OsqueryStatus, message *string, stats *fleet.Stats,
21022104
) {
21032105
const (
21042106
hostPolicyQueryPrefix = "fleet_policy_query_"
@@ -2164,6 +2166,33 @@ func (a *agent) processQuery(name, query string) (
21642166
}
21652167
if ss == fleet.StatusOK {
21662168
results = a.softwareMacOS()
2169+
cachedResults.software = results
2170+
}
2171+
return true, results, &ss, nil, nil
2172+
case name == hostDetailQueryPrefix+"software_macos_codesign":
2173+
// Given queries run in lexicographic order software_macos already run and
2174+
// cachedResults.software should have its results.
2175+
ss := fleet.StatusOK
2176+
if a.softwareQueryFailureProb > 0.0 && rand.Float64() <= a.softwareQueryFailureProb {
2177+
ss = fleet.OsqueryStatus(1)
2178+
}
2179+
if ss == fleet.StatusOK {
2180+
if len(cachedResults.software) > 0 {
2181+
for _, s := range cachedResults.software {
2182+
if s["source"] != "apps" {
2183+
continue
2184+
}
2185+
installedPath := s["installed_path"]
2186+
teamIdentifier := s["name"] // use name to be fixed (more realistic than changing often).
2187+
if len(teamIdentifier) > 10 {
2188+
teamIdentifier = teamIdentifier[:10]
2189+
}
2190+
results = append(results, map[string]string{
2191+
"path": installedPath,
2192+
"team_identifier": teamIdentifier,
2193+
})
2194+
}
2195+
}
21672196
}
21682197
return true, results, &ss, nil, nil
21692198
case name == hostDetailQueryPrefix+"software_windows":
@@ -2254,6 +2283,10 @@ func (a *agent) processQuery(name, query string) (
22542283
}
22552284
}
22562285

2286+
type cachedResults struct {
2287+
software []map[string]string
2288+
}
2289+
22572290
func (a *agent) DistributedWrite(queries map[string]string) error {
22582291
r := service.SubmitDistributedQueryResultsRequest{
22592292
Results: make(fleet.OsqueryDistributedQueryResults),
@@ -2262,8 +2295,21 @@ func (a *agent) DistributedWrite(queries map[string]string) error {
22622295
Stats: make(map[string]*fleet.Stats),
22632296
}
22642297
r.NodeKey = a.nodeKey
2265-
for name, query := range queries {
2266-
handled, results, status, message, stats := a.processQuery(name, query)
2298+
2299+
cachedResults := cachedResults{}
2300+
2301+
// Sort queries to be executed by lexicographic name order (for result processing
2302+
// to be more predictable). This aligns to how osquery executes the queries.
2303+
queryNames := make([]string, 0, len(queries))
2304+
for name := range queries {
2305+
queryNames = append(queryNames, name)
2306+
}
2307+
sort.Strings(queryNames)
2308+
2309+
for _, name := range queryNames {
2310+
query := queries[name]
2311+
2312+
handled, results, status, message, stats := a.processQuery(name, query, &cachedResults)
22672313
if !handled {
22682314
// If osquery-perf does not handle the incoming query,
22692315
// always return status OK and the default query result.

0 commit comments

Comments
 (0)