Skip to content

Commit

Permalink
Fix deprecated Kibana APIs (#2236)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mrodm authored Nov 20, 2024
1 parent 6ef2def commit b6f5d5d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
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

0 comments on commit b6f5d5d

Please sign in to comment.