Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set correct default webhook batch size #25529

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion server/webhooks/failing_policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ func SendFailingPoliciesBatchedPOSTs(
return hosts[i].ID < hosts[j].ID
})

// Default to "no batching", i.e. send one webhook request at a time.
if hostBatchSize == 0 {
hostBatchSize = len(hosts)
hostBatchSize = 1
}
for i := 0; i < len(hosts); i += hostBatchSize {
end := i + hostBatchSize
Expand Down
52 changes: 41 additions & 11 deletions server/webhooks/failing_policies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import (
func TestTriggerFailingPoliciesWebhookBasic(t *testing.T) {
ds := new(mock.Store)

requestBody := ""
requestBody := make([]string, 2)
numRequests := 0

policyID1 := uint(1)
ds.PolicyFunc = func(ctx context.Context, id uint) (*fleet.Policy, error) {
Expand Down Expand Up @@ -55,7 +56,8 @@ func TestTriggerFailingPoliciesWebhookBasic(t *testing.T) {
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
requestBody = string(requestBodyBytes)
requestBody[numRequests] = string(requestBodyBytes)
numRequests++
}))
t.Cleanup(func() {
ts.Close()
Expand Down Expand Up @@ -105,6 +107,7 @@ func TestTriggerFailingPoliciesWebhookBasic(t *testing.T) {
timestamp, err := mockClock.MarshalJSON()
require.NoError(t, err)
// Request body as defined in #2756.
require.Equal(t, numRequests, 2)
require.JSONEq(
t, fmt.Sprintf(`{
"timestamp": %s,
Expand Down Expand Up @@ -133,21 +136,47 @@ func TestTriggerFailingPoliciesWebhookBasic(t *testing.T) {
"hostname": "host1.example",
"display_name": "display1",
"url": "https://fleet.example.com/hosts/1"
},
}
]
}`, timestamp), requestBody[0])

require.JSONEq(
t, fmt.Sprintf(`{
"timestamp": %s,
"policy": {
"id": 1,
"name": "policy1",
"query": "select 42",
"description": "policy1 description",
"author_id": 1,
"author_name": "Alice",
"author_email": "[email protected]",
"team_id": null,
"resolution": "policy1 resolution",
"platform": "darwin",
"created_at": "0001-01-01T00:00:00Z",
"updated_at": "0001-01-01T00:00:00Z",
"passing_host_count": 0,
"failing_host_count": 2,
"host_count_updated_at": null,
"critical": true,
"calendar_events_enabled": false
},
"hosts": [
{
"id": 2,
"hostname": "host2.example",
"display_name": "display2",
"url": "https://fleet.example.com/hosts/2"
}
}
]
}`, timestamp), requestBody)

}`, timestamp), requestBody[1])
hosts, err := failingPolicySet.ListHosts(policyID1)
require.NoError(t, err)
assert.Empty(t, hosts)

requestBody = ""
requestBody = make([]string, 0)
numRequests = 0

err = policies.TriggerFailingPoliciesAutomation(context.Background(), ds, kitlog.NewNopLogger(), failingPolicySet, func(pol *fleet.Policy, cfg policies.FailingPolicyAutomationConfig) error {
serverURL, err := url.Parse(ac.ServerSettings.ServerURL)
Expand All @@ -158,7 +187,8 @@ func TestTriggerFailingPoliciesWebhookBasic(t *testing.T) {
context.Background(), pol, failingPolicySet, cfg.HostBatchSize, serverURL, cfg.WebhookURL, mockClock, kitlog.NewNopLogger())
})
require.NoError(t, err)
assert.Empty(t, requestBody)
assert.Equal(t, len(requestBody), 0)
assert.Equal(t, numRequests, 0)
}

func TestTriggerFailingPoliciesWebhookTeam(t *testing.T) {
Expand Down Expand Up @@ -403,7 +433,7 @@ func TestSendBatchedPOSTs(t *testing.T) {
name: "no-batching",
hostCount: 10,
batchSize: 0,
expRequestCount: 1,
expRequestCount: 10,
},
{
name: "one-host-no-batching",
Expand Down Expand Up @@ -436,9 +466,9 @@ func TestSendBatchedPOSTs(t *testing.T) {
expRequestCount: 1,
},
{
name: "100k-hosts-no-batching",
name: "100k-hosts-all-in-one-batch",
hostCount: 100000,
batchSize: 0,
batchSize: 100000,
expRequestCount: 1,
},
} {
Expand Down
Loading