diff --git a/internal/kibana/agents.go b/internal/kibana/agents.go index 7e8c92dea0..3ce0c52ffd 100644 --- a/internal/kibana/agents.go +++ b/internal/kibana/agents.go @@ -15,6 +15,8 @@ import ( "github.com/elastic/elastic-package/internal/signal" ) +var waitForPolicyAssignedTimeout = 10 * time.Minute + // Agent represents an Elastic Agent enrolled with fleet. type Agent struct { ID string `json:"id"` @@ -27,6 +29,15 @@ type Agent struct { } `json:"local_metadata"` } +// String method returns string representation of an agent. +func (a *Agent) String() string { + b, err := json.Marshal(a) + if err != nil { + return err.Error() + } + return string(b) +} + // ListAgents returns the list of agents enrolled with Fleet. func (c *Client) ListAgents() ([]Agent, error) { statusCode, respBody, err := c.get(fmt.Sprintf("%s/agents", FleetAPI)) @@ -71,7 +82,12 @@ func (c *Client) AssignPolicyToAgent(a Agent, p Policy) error { } func (c *Client) waitUntilPolicyAssigned(a Agent, p Policy) error { + timeout := time.Now().Add(waitForPolicyAssignedTimeout) for { + if time.Now().After(timeout) { + return errors.New("timeout: policy hasn't been assigned in time") + } + if signal.SIGINT() { return errors.New("SIGINT: cancel waiting for policy assigned") } @@ -80,6 +96,7 @@ func (c *Client) waitUntilPolicyAssigned(a Agent, p Policy) error { if err != nil { return errors.Wrap(err, "can't get the agent") } + logger.Debugf("Agent data: %s", agent.String()) if agent.PolicyID == p.ID && agent.PolicyRevision == p.Revision { logger.Debugf("Policy revision assigned to the agent (ID: %s)...", a.ID)