From f9ff2eee73617bf7df0867900cefabbeff8d9d02 Mon Sep 17 00:00:00 2001 From: Julia Bardi <90178898+juliaElastic@users.noreply.github.com> Date: Wed, 23 Nov 2022 09:14:48 +0100 Subject: [PATCH] fix unenroll offline agents (#2092) * fix unenroll offline agents * added changelog * added unit test * changed ErrNotFound error to an info log * fixed error logic (cherry picked from commit 3b22855135d10ba8b54632733767fc9359f042e0) --- CHANGELOG.next.asciidoc | 1 + internal/pkg/coordinator/monitor.go | 4 ++++ internal/pkg/model/ext.go | 8 ++++++-- internal/pkg/model/ext_test.go | 25 +++++++++++++++++++++++++ 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index 67546b1da..900a3d1d2 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -17,6 +17,7 @@ - Fix fleet.migration.total log key overlap {pull}1951[1951] - Remove POLICY_CHANGE actions from list retrieved from actions index before sending actions to agent on Checkin. {issue}1773[1773] {pull}1963[1963] - Add "active: true" filter to enrollemnent key queries. {issue}2029[2029] {pull}2044[2044] +- Fixed unenroll offline agents logic {issue}2091[2091] {pull}2092[2092] ==== New Features diff --git a/internal/pkg/coordinator/monitor.go b/internal/pkg/coordinator/monitor.go index f203ab45a..0eae06e2b 100644 --- a/internal/pkg/coordinator/monitor.go +++ b/internal/pkg/coordinator/monitor.go @@ -509,6 +509,10 @@ func runUnenroller(ctx context.Context, bulker bulk.Bulk, policyID string, unenr func runUnenrollerWork(ctx context.Context, bulker bulk.Bulk, policyID string, unenrollTimeout time.Duration, zlog zerolog.Logger, agentsIndex string) error { agents, err := dl.FindOfflineAgents(ctx, bulker, policyID, unenrollTimeout, dl.WithIndexName(agentsIndex)) if err != nil { + if errors.Is(err, dl.ErrNotFound) { + zlog.Info().Msg("no agents to unenroll") + return nil + } return err } diff --git a/internal/pkg/model/ext.go b/internal/pkg/model/ext.go index 4a11bbe08..798f9d5b1 100644 --- a/internal/pkg/model/ext.go +++ b/internal/pkg/model/ext.go @@ -51,9 +51,13 @@ func (a *Agent) APIKeyIDs() []string { } for _, output := range a.Outputs { - keys = append(keys, output.APIKeyID) + if output.APIKeyID != "" { + keys = append(keys, output.APIKeyID) + } for _, key := range output.ToRetireAPIKeyIds { - keys = append(keys, key.ID) + if key.ID != "" { + keys = append(keys, key.ID) + } } } diff --git a/internal/pkg/model/ext_test.go b/internal/pkg/model/ext_test.go index 527570270..74a472ae0 100644 --- a/internal/pkg/model/ext_test.go +++ b/internal/pkg/model/ext_test.go @@ -122,6 +122,31 @@ func TestAgentAPIKeyIDs(t *testing.T) { "access_api_key_id", "p1_api_key_id", "p2_api_key_id", "p1_to_retire_key", "p2_to_retire_key"}, }, + { + name: "API key empty", + agent: Agent{ + AccessAPIKeyID: "access_api_key_id", + Outputs: map[string]*PolicyOutput{ + "p1": {APIKeyID: ""}, + }, + }, + want: []string{"access_api_key_id"}, + }, + { + name: "retired API key empty", + agent: Agent{ + AccessAPIKeyID: "access_api_key_id", + Outputs: map[string]*PolicyOutput{ + "p1": { + APIKeyID: "p1_api_key_id", + ToRetireAPIKeyIds: []ToRetireAPIKeyIdsItems{{ + ID: "", + }}}, + }, + }, + want: []string{ + "access_api_key_id", "p1_api_key_id"}, + }, } for _, tc := range tcs {