From b6f5d5da929001ec18bc62366d389368a6c09938 Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Molins Date: Wed, 20 Nov 2024 16:56:22 +0100 Subject: [PATCH] Fix deprecated Kibana APIs (#2236) Fixed API to get the list of Elastic Agents available and the API to reassign one policy to an Elastic Agent. Fixed policy name to be used in Kubernetes for 9.x stack --- internal/agentdeployer/kubernetes.go | 11 ++++++----- internal/kibana/agents.go | 22 ++++++++++++++++++---- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/internal/agentdeployer/kubernetes.go b/internal/agentdeployer/kubernetes.go index f29f816f24..e7f20e6604 100644 --- a/internal/agentdeployer/kubernetes.go +++ b/internal/agentdeployer/kubernetes.go @@ -221,11 +221,12 @@ func readCACertBase64(profile *profile.Profile) (string, error) { return base64.StdEncoding.EncodeToString(d), nil } -// getTokenPolicyName function returns the policy name for the 8.x Elastic stack. The agent's policy -// is predefined in the Kibana configuration file. The logic is not present in older stacks. +// getTokenPolicyName function returns the policy name for the >= 8.x Elastic stacks. The agent's policy +// is predefined in the Kibana configuration file. The logic is not present in older stacks and it uses +// the default policy in Kibana (empty string). func getTokenPolicyName(stackVersion, policyName string) string { - if strings.HasPrefix(stackVersion, "8.") { - return policyName + if strings.HasPrefix(stackVersion, "7.") { + return "" } - return "" + return policyName } diff --git a/internal/kibana/agents.go b/internal/kibana/agents.go index 041d27e163..853a2520ee 100644 --- a/internal/kibana/agents.go +++ b/internal/kibana/agents.go @@ -59,22 +59,36 @@ func (c *Client) ListAgents(ctx context.Context) ([]Agent, error) { } var resp struct { - List []Agent `json:"list"` + List []Agent `json:"list"` + Items []Agent `json:"items"` } if err := json.Unmarshal(respBody, &resp); err != nil { return nil, fmt.Errorf("could not convert list agents (response) to JSON: %w", err) } - return resp.List, nil + switch { + case c.semver.Major() < 9: + return resp.List, nil + default: + return resp.Items, nil + } } // AssignPolicyToAgent assigns the given Policy to the given Agent. func (c *Client) AssignPolicyToAgent(ctx context.Context, a Agent, p Policy) error { reqBody := `{ "policy_id": "` + p.ID + `" }` - path := fmt.Sprintf("%s/agents/%s/reassign", FleetAPI, a.ID) - statusCode, respBody, err := c.put(ctx, path, []byte(reqBody)) + + var statusCode int + var err error + var respBody []byte + switch { + case c.semver.Major() < 9: + statusCode, respBody, err = c.put(ctx, path, []byte(reqBody)) + default: + statusCode, respBody, err = c.post(ctx, path, []byte(reqBody)) + } if err != nil { return fmt.Errorf("could not assign policy to agent: %w", err) }