-
Notifications
You must be signed in to change notification settings - Fork 82
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
Conditional log level for api key read #1946
Merged
michalpristas
merged 12 commits into
elastic:main
from
michalpristas:fix/api-key-not-found
Oct 6, 2022
Merged
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4b441a5
Conditional log level for api key read
michalpristas b4c976a
use agent id to query agent
michalpristas a2b6926
use agent id to query agent
michalpristas 0eb699d
stop processing once agent is detected inactive
michalpristas 284e7e0
remove unnecessary args
michalpristas 097a241
lint
michalpristas f2ea7aa
check result instead of modifying passed structure
michalpristas dddf039
propagate info to client, decrease log level on api handler
michalpristas 7d53804
Merge branch 'main' of github.com:elastic/fleet-server into fix/api-k…
michalpristas a522ac1
Merge branch 'main' into fix/api-key-not-found
michalpristas 4956bd6
error as error
michalpristas 2b105fb
Merge branch 'fix/api-key-not-found' of github.com:michalpristas/flee…
michalpristas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -335,15 +335,19 @@ func (ack *AckT) handlePolicyChange(ctx context.Context, zlog zerolog.Logger, ag | |
} | ||
|
||
for _, output := range agent.Outputs { | ||
if !agent.Active { | ||
// stop processing if agent is inactive | ||
break | ||
} | ||
|
||
if output.Type != policy.OutputTypeElasticsearch { | ||
continue | ||
} | ||
|
||
err := ack.updateAPIKey(ctx, | ||
zlog, | ||
agent.Id, | ||
agent, | ||
currRev, currCoord, | ||
agent.PolicyID, | ||
output.APIKeyID, output.PermissionsHash, output.ToRetireAPIKeyIds) | ||
if err != nil { | ||
return err | ||
|
@@ -356,18 +360,29 @@ func (ack *AckT) handlePolicyChange(ctx context.Context, zlog zerolog.Logger, ag | |
|
||
func (ack *AckT) updateAPIKey(ctx context.Context, | ||
zlog zerolog.Logger, | ||
agentID string, | ||
agent *model.Agent, | ||
currRev, currCoord int64, | ||
policyID, apiKeyID, permissionHash string, | ||
apiKeyID, permissionHash string, | ||
toRetireAPIKeyIDs []model.ToRetireAPIKeyIdsItems) error { | ||
|
||
if apiKeyID != "" { | ||
res, err := ack.bulk.APIKeyRead(ctx, apiKeyID, true) | ||
if err != nil { | ||
zlog.Error(). | ||
Err(err). | ||
Str(LogAPIKeyID, apiKeyID). | ||
Msg("Failed to read API Key roles") | ||
if isAgentActive(ctx, zlog, ack.bulk, agent.Id) { | ||
zlog.Error(). | ||
Err(err). | ||
Str(LogAPIKeyID, apiKeyID). | ||
Msg("Failed to read API Key roles") | ||
} else { | ||
// not an error, race when API key was invalidated before acking | ||
zlog.Info(). | ||
Err(err). | ||
Str(LogAPIKeyID, apiKeyID). | ||
Msg("Failed to read invalidated API Key roles") | ||
|
||
// update to inactive for future checks | ||
agent.Active = false | ||
} | ||
} else { | ||
clean, removedRolesCount, err := cleanRoles(res.RoleDescriptors) | ||
if err != nil { | ||
|
@@ -393,22 +408,22 @@ func (ack *AckT) updateAPIKey(ctx context.Context, | |
} | ||
|
||
body := makeUpdatePolicyBody( | ||
policyID, | ||
agent.PolicyID, | ||
currRev, | ||
currCoord, | ||
) | ||
|
||
err := ack.bulk.Update( | ||
ctx, | ||
dl.FleetAgents, | ||
agentID, | ||
agent.Id, | ||
body, | ||
bulk.WithRefresh(), | ||
bulk.WithRetryOnConflict(3), | ||
) | ||
|
||
zlog.Err(err). | ||
Str(LogPolicyID, policyID). | ||
Str(LogPolicyID, agent.PolicyID). | ||
Int64("policyRevision", currRev). | ||
Int64("policyCoordinator", currCoord). | ||
Msg("ack policy") | ||
|
@@ -513,6 +528,18 @@ func (ack *AckT) handleUpgrade(ctx context.Context, zlog zerolog.Logger, agent * | |
return nil | ||
} | ||
|
||
func isAgentActive(ctx context.Context, zlog zerolog.Logger, bulk bulk.Bulk, agentID string) bool { | ||
agent, err := dl.FindAgent(ctx, bulk, dl.QueryAgentByID, dl.FieldID, agentID) | ||
scunningham marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
zlog.Warn(). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why just warn? isn't it a real error? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes we can take it like an error |
||
Err(err). | ||
Msg("failed to find agent by ID") | ||
return true | ||
} | ||
|
||
return agent.Active // it is a valid error in case agent is active (was not invalidated) | ||
} | ||
|
||
// Generate an update script that validates that the policy_id | ||
// has not changed underneath us by an upstream process (Kibana or otherwise). | ||
// We have a race condition where a user could have assigned a new policy to | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is still an error. If the ApiKey is gone, the ACK should fail. We should still output an error, but it should be an info level log message. Ie. this is an error, but it is expected.
It is also unexpected to have this call produce a side effect of changing the agent record.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
discussed with @scunningham offline, approach agreed on is: returning error and propagating it to the client while decreasing log level on the server side inside handler