Skip to content

Commit

Permalink
fix unenroll offline agents (#2092)
Browse files Browse the repository at this point in the history
* fix unenroll offline agents

* added changelog

* added unit test

* changed ErrNotFound error to an info log

* fixed error logic

(cherry picked from commit 3b22855)
  • Loading branch information
juliaElastic authored and mergify[bot] committed Nov 23, 2022
1 parent d136c36 commit f9ff2ee
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions internal/pkg/coordinator/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
8 changes: 6 additions & 2 deletions internal/pkg/model/ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand Down
25 changes: 25 additions & 0 deletions internal/pkg/model/ext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit f9ff2ee

Please sign in to comment.