Skip to content

Fix deprecated Kibana APIs #2236

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

Merged
merged 2 commits into from
Nov 20, 2024
Merged
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
11 changes: 6 additions & 5 deletions internal/agentdeployer/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
22 changes: 18 additions & 4 deletions internal/kibana/agents.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down