Skip to content

Commit eb79c95

Browse files
Remove extra search when preparing agent policies (#5283)
1 parent 106bdc7 commit eb79c95

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Kind can be one of:
2+
# - breaking-change: a change to previously-documented behavior
3+
# - deprecation: functionality that is being removed in a later release
4+
# - bug-fix: fixes a problem in a previous version
5+
# - enhancement: extends functionality but does not break or fix existing behavior
6+
# - feature: new functionality
7+
# - known-issue: problems that we are aware of in a given version
8+
# - security: impacts on the security of a product or a user’s deployment.
9+
# - upgrade: important information for someone upgrading from a prior version
10+
# - other: does not fit into any of the other categories
11+
kind: bug-fix
12+
13+
# Change summary; a 80ish characters long description of the change.
14+
summary: Remove extra ES search when preparing agent policy
15+
16+
# Long description; in case the summary is not enough to describe the change
17+
# this field accommodate a description without length limits.
18+
# NOTE: This field will be rendered only for breaking-change and known-issue kinds at the moment.
19+
#description:
20+
21+
# Affected component; usually one of "elastic-agent", "fleet-server", "filebeat", "metricbeat", "auditbeat", "all", etc.
22+
component: fleet-server
23+
24+
# PR URL; optional; the PR number that added the changeset.
25+
# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added.
26+
# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number.
27+
# Please provide it if you are adding a fragment for a different PR.
28+
pr: https://github.com/elastic/fleet-server/pull/5283
29+
30+
# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of).
31+
# If not present is automatically filled by the tooling with the issue linked to the PR number.
32+
#issue: https://github.com/owner/repo/1234

internal/pkg/api/handleCheckin.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ func (ct *CheckinT) ProcessRequest(zlog zerolog.Logger, w http.ResponseWriter, r
371371
actions = append(actions, acs...)
372372
break LOOP
373373
case policy := <-sub.Output():
374-
actionResp, err := processPolicy(ctx, zlog, ct.bulker, agent.Id, policy)
374+
actionResp, err := processPolicy(ctx, zlog, ct.bulker, agent, policy)
375375
if err != nil {
376376
span.End()
377377
return fmt.Errorf("processPolicy: %w", err)
@@ -841,7 +841,7 @@ func convertActions(zlog zerolog.Logger, agentID string, actions []model.Action)
841841
// A new policy exists for this agent. Perform the following:
842842
// - Generate and update default ApiKey if roles have changed.
843843
// - Rewrite the policy for delivery to the agent injecting the key material.
844-
func processPolicy(ctx context.Context, zlog zerolog.Logger, bulker bulk.Bulk, agentID string, pp *policy.ParsedPolicy) (*Action, error) {
844+
func processPolicy(ctx context.Context, zlog zerolog.Logger, bulker bulk.Bulk, agent *model.Agent, pp *policy.ParsedPolicy) (*Action, error) {
845845
var links []apm.SpanLink = nil // set to a nil array to preserve default behaviour if no policy links are found
846846
if err := pp.Links.Trace.Validate(); err == nil {
847847
links = []apm.SpanLink{pp.Links}
@@ -854,23 +854,14 @@ func processPolicy(ctx context.Context, zlog zerolog.Logger, bulker bulk.Bulk, a
854854
Str(LogPolicyID, pp.Policy.PolicyID).
855855
Logger()
856856

857-
// Repull and decode the agent object. Do not trust the cache.
858-
bSpan, bCtx := apm.StartSpan(ctx, "findAgent", "search")
859-
agent, err := dl.FindAgent(bCtx, bulker, dl.QueryAgentByID, dl.FieldID, agentID)
860-
bSpan.End()
861-
if err != nil {
862-
zlog.Error().Err(err).Msg("fail find agent record")
863-
return nil, err
864-
}
865-
866857
if len(pp.Policy.Data.Outputs) == 0 {
867858
return nil, ErrNoPolicyOutput
868859
}
869860

870861
data := model.ClonePolicyData(pp.Policy.Data)
871862
for policyName, policyOutput := range data.Outputs {
872863
// NOTE: Not sure if output secret keys collected here include new entries, but they are collected for completeness
873-
ks, err := policy.ProcessOutputSecret(ctx, policyOutput, bulker)
864+
ks, err := policy.ProcessOutputSecret(ctx, policyOutput, bulker) // makes a bulk request to get secret values
874865
if err != nil {
875866
return nil, fmt.Errorf("failed to process output secrets %q: %w",
876867
policyName, err)
@@ -879,8 +870,7 @@ func processPolicy(ctx context.Context, zlog zerolog.Logger, bulker bulk.Bulk, a
879870
}
880871
// Iterate through the policy outputs and prepare them
881872
for _, policyOutput := range pp.Outputs {
882-
err = policyOutput.Prepare(ctx, zlog, bulker, &agent, data.Outputs)
883-
if err != nil {
873+
if err := policyOutput.Prepare(ctx, zlog, bulker, agent, data.Outputs); err != nil {
884874
return nil, fmt.Errorf("failed to prepare output %q: %w",
885875
policyOutput.Name, err)
886876
}

0 commit comments

Comments
 (0)