-
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
Bulk API Keys update #1779
Merged
michalpristas
merged 8 commits into
elastic:main
from
michalpristas:feat/bulk-api-update
Sep 20, 2022
Merged
Bulk API Keys update #1779
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a4c29f0
bulk api update
michalpristas a536f62
lint
michalpristas af77ec6
integration build
michalpristas f15ff5d
use max key size
michalpristas 607ab12
Merge branch 'main' of github.com:elastic/fleet-server into feat/bulk…
michalpristas 2863300
review
michalpristas 70fac96
Merge branch 'main' of github.com:elastic/fleet-server into feat/bulk…
michalpristas cc21850
fixed find key fn
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 |
---|---|---|
|
@@ -24,6 +24,7 @@ import ( | |
"github.com/elastic/fleet-server/v7/internal/pkg/logger" | ||
"github.com/elastic/fleet-server/v7/internal/pkg/model" | ||
"github.com/elastic/fleet-server/v7/internal/pkg/policy" | ||
"github.com/elastic/fleet-server/v7/internal/pkg/smap" | ||
"github.com/pkg/errors" | ||
|
||
"github.com/julienschmidt/httprouter" | ||
|
@@ -349,10 +350,44 @@ func (ack *AckT) handlePolicyChange(ctx context.Context, zlog zerolog.Logger, ag | |
return nil | ||
} | ||
|
||
if agent.DefaultAPIKeyID != "" { | ||
res, err := ack.bulk.APIKeyRead(ctx, agent.DefaultAPIKeyID, true) | ||
if err != nil { | ||
zlog.Info(). | ||
Err(err). | ||
Str("id", agent.DefaultAPIKeyID). | ||
Msg("Failed to read API Key roles") | ||
} else { | ||
clean, removedCount, err := cleanRoles(res.RoleDescriptors) | ||
if err != nil { | ||
zlog.Info(). | ||
Err(err). | ||
RawJSON("roles", res.RoleDescriptors). | ||
Str("id", agent.DefaultAPIKeyID). | ||
Msg("Failed to cleanup roles") | ||
} else if removedCount > 0 { | ||
if err := ack.bulk.APIKeyUpdate(ctx, agent.DefaultAPIKeyID, agent.PolicyOutputPermissionsHash, clean); err != nil { | ||
zlog.Info().Err(err).RawJSON("roles", clean).Str("id", agent.DefaultAPIKeyID).Msg("Failed to refresh API Key") | ||
} else { | ||
zlog.Debug(). | ||
Str("hash.sha256", agent.PolicyOutputPermissionsHash). | ||
Str("apikey", agent.DefaultAPIKeyID). | ||
RawJSON("roles", clean). | ||
Int("removed", removedCount). | ||
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. [Suggestion] |
||
Msg("Updating agent record to pick up reduced roles.") | ||
} | ||
} | ||
} | ||
} | ||
|
||
sz := len(agent.DefaultAPIKeyHistory) | ||
if sz > 0 { | ||
ids := make([]string, sz) | ||
for i := 0; i < sz; i++ { | ||
if agent.DefaultAPIKeyHistory[i].ID == agent.DefaultAPIKeyID { | ||
// already updated | ||
continue | ||
} | ||
ids[i] = agent.DefaultAPIKeyHistory[i].ID | ||
} | ||
log.Info().Strs("ids", ids).Msg("Invalidate old API keys") | ||
|
@@ -385,6 +420,32 @@ func (ack *AckT) handlePolicyChange(ctx context.Context, zlog zerolog.Logger, ag | |
return errors.Wrap(err, "handlePolicyChange update") | ||
} | ||
|
||
func cleanRoles(roles json.RawMessage) (json.RawMessage, int, error) { | ||
rr := smap.Map{} | ||
if err := json.Unmarshal(roles, &rr); err != nil { | ||
return nil, 0, err | ||
} | ||
|
||
keys := make([]string, 0, len(rr)) | ||
for k := range rr { | ||
if strings.HasSuffix(k, "-rdstale") { | ||
keys = append(keys, k) | ||
} | ||
|
||
} | ||
|
||
if len(keys) == 0 { | ||
return roles, 0, nil | ||
} | ||
|
||
for _, k := range keys { | ||
delete(rr, k) | ||
} | ||
|
||
r, err := json.Marshal(rr) | ||
return r, len(keys), err | ||
} | ||
|
||
func (ack *AckT) handleUnenroll(ctx context.Context, zlog zerolog.Logger, agent *model.Agent) error { | ||
apiKeys := _getAPIKeyIDs(agent) | ||
if len(apiKeys) > 0 { | ||
|
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
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
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
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
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
Oops, something went wrong.
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.
[Suggestion]
Is
removedCount
the count of removed roles? Then perhapsremovedRoles
orremovedRolesCount
would make it easier to understand