Skip to content
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

fix unenroll offline agents #2092

Merged
merged 7 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
6 changes: 6 additions & 0 deletions internal/pkg/coordinator/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,12 @@ func runUnenrollerWork(ctx context.Context, bulker bulk.Bulk, policyID string, u
return err
}

if len(agents) == 0 {
zlog.Info().
Msg("no agents to unenroll")
return nil
}

juliaElastic marked this conversation as resolved.
Show resolved Hide resolved
zlog = zlog.With().Dur("timeout", unenrollTimeout).Logger()

agentIds := make([]string, len(agents))
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/dl/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func FindOfflineAgents(ctx context.Context, bulker bulk.Bulk, policyID string, u
}

if len(res.Hits) == 0 {
return nil, ErrNotFound
return nil, nil
juliaElastic marked this conversation as resolved.
Show resolved Hide resolved
}

agents := make([]model.Agent, len(res.Hits))
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