Skip to content

Commit

Permalink
Add probability of policy pass flag to osquery-perf (#3014)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmrod authored Nov 19, 2021
1 parent f885522 commit d1dcc0e
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions cmd/osquery-perf/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,16 @@ type agent struct {
Stats *Stats
SoftwareCount int
NodeKeyManager *nodeKeyManager
policyPassProb float64

strings map[string]string
}

func newAgent(serverAddress, enrollSecret string, templates *template.Template, configInterval, queryInterval time.Duration, softwareCount int) *agent {
func newAgent(
serverAddress, enrollSecret string, templates *template.Template,
configInterval, queryInterval time.Duration, softwareCount int,
policyPassProb float64,
) *agent {
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
transport.DisableCompression = true
Expand All @@ -146,8 +151,9 @@ func newAgent(serverAddress, enrollSecret string, templates *template.Template,
FastClient: fasthttp.Client{
TLSConfig: &tls.Config{InsecureSkipVerify: true},
},
SoftwareCount: softwareCount,
strings: make(map[string]string),
SoftwareCount: softwareCount,
strings: make(map[string]string),
policyPassProb: policyPassProb,
}
}

Expand Down Expand Up @@ -336,15 +342,29 @@ var defaultQueryResult = []map[string]string{
{"foo": "bar"},
}

func (a *agent) runPolicy(query string) []map[string]string {
if rand.Float64() <= a.policyPassProb {
return []map[string]string{
{"1": "1"},
}
}
return nil
}

func (a *agent) DistributedWrite(queries map[string]string) {
r := service.SubmitDistributedQueryResultsRequest{
Results: make(fleet.OsqueryDistributedQueryResults),
Statuses: make(map[string]fleet.OsqueryStatus),
}
r.NodeKey = a.NodeKey
const hostPolicyQueryPrefix = "fleet_policy_query_"
for name := range queries {
r.Results[name] = defaultQueryResult
r.Statuses[name] = fleet.StatusOK
if strings.HasPrefix(name, hostPolicyQueryPrefix) {
r.Results[name] = a.runPolicy(queries[name])
continue
}
if t := a.Templates.Lookup(name); t == nil {
continue
}
Expand Down Expand Up @@ -393,6 +413,7 @@ func main() {
onlyAlreadyEnrolled := flag.Bool("only_already_enrolled", false, "Only start agents that are already enrolled")
nodeKeyFile := flag.String("node_key_file", "", "File with node keys to use")
softwareCount := flag.Int("software_count", 10, "Number of installed applications reported to fleet")
policyPassProb := flag.Float64("policy_pass_prob", 1.0, "Probability of policies to pass [0, 1].")

flag.Parse()

Expand All @@ -417,7 +438,7 @@ func main() {
}

for i := 0; i < *hostCount; i++ {
a := newAgent(*serverURL, *enrollSecret, tmpl, *configInterval, *queryInterval, *softwareCount)
a := newAgent(*serverURL, *enrollSecret, tmpl, *configInterval, *queryInterval, *softwareCount, *policyPassProb)
a.Stats = stats
a.NodeKeyManager = nodeKeyManager
go a.runLoop(i, onlyAlreadyEnrolled != nil && *onlyAlreadyEnrolled)
Expand Down

1 comment on commit d1dcc0e

@vercel
Copy link

@vercel vercel bot commented on d1dcc0e Nov 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.