You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Update our API Key handling logic for updatable permission and bulk operation on the api endpoint #1581
The current implementation of the API keys come with two problematic for Fleet-Server:
The API key permission are immutable, when an API key is created it will be stuck with the same permissions. When a new integration is added to an Agent policy it meant that all the Elastic Agent API need to be invalidated and recreated.
The API key endpoint in Elasticsearch doesn't support bulk operation.
Tasks
Fleet server will need to be adjusted to support the following feature.
Elasticsearch will allow API Key permission to be updated in place
Elasticsearch will support Bulk Operation to Update API Key.
Verifies what happen when we migrate from non updatable to updatable api keys.
changed the title [-]Use our API Key handling logic for updatable API Key and bulk operation[/-][+]Update our API Key handling logic for updatable permission and bulk operation on the api endpoint[/+]on Jun 22, 2022
@ph, sorry, I'm a bit unsure about what you meant when you said: "Verifies what happen when we migrate from non updatable to updatable api keys.". Is it to measure the performance before and after the change?
@AndersonQ let me clarify, I was more curious about upgrade in the stack.
8.4 Api keys permissions are not updatable.
8.5 Api Keys permissions are updatable.
If I remember correctly from the ES proposal, you cannot update permission on keys created on 8.4, they are immutable.
So you will have to create new keys in 8.5, after that point the new keys would be updatable.
there's no flag required to make new/old keys become updatable
Considering this I was looking into how API keys works now in the code for quick summary:(if i missed something please correct me)
User changes ES policy, change is reflected in .fleet_policies
fleet-server is notified about the change comparing to last checkpoints
fleet-server loads the policy, populate roles definition based on policy content and updates it internally
on CHECK-IN``fleet-server prepares ES output meaning it will
a. handle whatever ES needs to accept new items
b. create a new API Key for agent to use with the current policy
once agent handles config it will ACK the action
fleet-server when handling ACK action invalidates OLD api key based on agent history
To make use of Bulk and updatable API Keys i was thinking about 3 scenarios of process modifications:
Option 1: Bulk Create/Update on policy change
This approach gets rid of steps 4.b and 6 instead of these it introduces a Bulk action into step 3
Updated API Key contains newly created roles
Pros:
Bulk of everything at once (or split to conform http.max_content_length config option
Maybe better memory footprint as we wont need to store roles in a structure
Cons:
Depending on number of agents it may take long time for policy to be rolled out, this means that it may take long time when agent+processes are trying to write to indices where they don't longer have access to (api key is updated already)
Option 2: Bulk Create/Update on policy change with merged roles
Similarly to previous approach we're getting rid of steps 4.b and 6 instead of these it introduces a Bulk action into step 3.
Difference is that new API Key now contains roles from OLD and NEW configuration merged to overcome access denied failures introduced in previous approach.
The problem is that we cannot exist with this set forever.
We should fix API Keys back to proper, secure role set when agents are ready.
When agents are ready is really the question.
I see two options of fixing time:
On ACK
Timer based
First one will fix permissions when agent ACKs policy change. This seems nice but gives us not benefit of bulking because we're ending up with n+1 updates. While we're getting rid of invalidated api key issues we're not helping scale
Second one sets a timer for some predefined time and schedules an API Key update for that time. This means we're at 2 updates per batch. While we fixed permission issue for most of agents, slow updaters will still face this issue.
Pros:
Bulk of everything at once (or split to conform http.max_content_length config option
Cons:
Either no scale benefits or just delayed/reduced problem from option 1
Option 3: Modify bulker
Fleet server uses bulker for communicating with ES.
Option comes in mind when we introduce new kind of action update_api_key
In this case we dont' need to change much.
On step 4.b we just call Bulker as usuall
We skip invalidating on step 6
Inside bulker we would group updates with same roles into a single req with multiple agent ids.
Bulker executes when interval passes (5s) or expected request size is close to limit.
Number of agents included in a bulk is dependent on a checkin interval. In agent we use 1s checking frequency by default which means most agents should be handled within few calls making significant savings in term of request count.
Slow updaters won't run into situation in Option 2 as their API Keys will get updated on individual basis upon Checkin
We could also consider updates on ACK but in case ACK is lost this would mean even worse permission issue behavior.
Pros:
Significantly less request to update api keys
Handles slow updaters
Easy to implement
Cons:
number of API Key update request is not 1 (best case is really low though).
Worst case (though not probable) scenario is not saving any Update/Create request (just getting rid of invalidations)
Still may see permission issues during 5s window
thinking about these I like option 3, please if you see any issues with the approach or some step I did not take into account let me know
API Keys before change will be updatable
there's no flag required to make new/old keys become updatable
Good news, we don't have to deal with that special upgrade case.
First one will fix permissions when agent ACKs policy change. This seems nice but gives us not benefit of bulking because we're ending up with n+1 updates.
I agree you would lose some of the benefit of bulking but you can also have a flush timeout with quantity flush. you would still bulk, but instead of bulking on the every agent associated to the policy you may bulk on a subset, which would still be better that n request that we have today.
I do like option 3, I've hit submit before reading option 3, just to clarify you will still need to have an expended permission set for this option no?
if we're ok with up to 5 second skew then i don't need to extend and we will save a lot of requests.
otherwise we can do it in 2phase fashion: option 3 with extending but cleaning up on ACK which will reduce requests compared to today. but not to the minimal value, in case of ACK failure api key will exist with extended set of roles.
if this is considered security risk we should maybe aim for 5s skew
just point for me: for this option we need to make sure we dont have a race when ack update is applied before initial bulk update
So there could some ordering needed in that case ?
Let's say that the user is really going wild on the policy changes, no sure if there is some kind of debounce in fleet.
+1 on option 3. The main benefit of this change is being able to avoid creating and hashing new keys. The bulk API is an additional optimization that will matter at scale, but less impactful than the change from create -> update. I like that option 3 allows us to use the same bulking pattern that we use for all other ES interactions (.fleet-agents updates and searches).
Worst case (though not probable) scenario is not saving any Update/Create request (just getting rid of invalidations)
I think we will not do any creates, correct? It will still only be updates which are much cheaper, just that it will take multiple requests to do it. Since action/policy delivery is quite fast, I do expect the typical case to maximize usage of the bulk API quite fairly well.
I do think with option 3 we may still need to consider working around http.max_content_size, though it'd be good to do some back-of-the-envelope math on how many API keys you'd need to have in a single bulk update request to hit this limit.
otherwise we can do it in 2phase fashion: option 3 with extending but cleaning up on ACK which will reduce requests compared to today. but not to the minimal value, in case of ACK failure api key will exist with extended set of roles.
I think we should still use the extended/union privileges + second update to reduce privileges in order to avoid any ingestion problems if there's an availability problem with Fleet Server. I think this is important especially for the agent running Fleet Server itself. If an integration is removed from that policy, the agent should have an opportunity to flush any remaining data before it's access is revoked.
Truly slow updaters should get kicked out of the Fleet / disabled by the unenrollment timeout.
Reacted by Michal Pristas and Pier-Hugues Pellerin
Activity
[-]Use our API Key handling logic[/-][+]Use our API Key handling logic for updatable API Key and bulk operation[/+][-]Use our API Key handling logic for updatable API Key and bulk operation[/-][+]Update our API Key handling logic for updatable permission and bulk operation on the api endpoint[/+]ph commentedon Jun 23, 2022
Single operation route PR elastic/elasticsearch#87870
AndersonQ commentedon Aug 1, 2022
@ph, sorry, I'm a bit unsure about what you meant when you said: "Verifies what happen when we migrate from non updatable to updatable api keys.". Is it to measure the performance before and after the change?
ph commentedon Aug 3, 2022
@AndersonQ let me clarify, I was more curious about upgrade in the stack.
If I remember correctly from the ES proposal, you cannot update permission on keys created on 8.4, they are immutable.
So you will have to create new keys in 8.5, after that point the new keys would be updatable.
Is this more clear?
michalpristas commentedon Aug 11, 2022
checked that
Considering this I was looking into how API keys works now in the code for quick summary:(if i missed something please correct me)
.fleet_policiesfleet-serveris notified about the change comparing to last checkpointsfleet-serverloads the policy, populaterolesdefinition based on policy content and updates it internallyCHECK-IN``fleet-serverprepares ES output meaning it willa. handle whatever ES needs to accept new items
b. create a
newAPI Key for agent to use with the current policyagenthandles config it willACKthe actionfleet-serverwhen handlingACKaction invalidates OLD api key based on agent historyTo make use of Bulk and updatable API Keys i was thinking about 3 scenarios of process modifications:
Option 1: Bulk Create/Update on policy change
This approach gets rid of steps
4.band6instead of these it introduces a Bulk action into step3Updated API Key contains newly created roles
Pros:
http.max_content_lengthconfig optionCons:
Option 2: Bulk Create/Update on policy change with merged roles
Similarly to previous approach we're getting rid of steps
4.band6instead of these it introduces a Bulk action into step3.Difference is that new API Key now contains roles from OLD and NEW configuration merged to overcome
access deniedfailures introduced in previous approach.The problem is that we cannot exist with this set forever.
We should fix API Keys back to proper, secure role set when agents are ready.
When agents are ready is really the question.
I see two options of
fixingtime:First one will fix permissions when agent ACKs policy change. This seems nice but gives us not benefit of bulking because we're ending up with n+1 updates. While we're getting rid of
invalidated api keyissues we're not helping scaleSecond one sets a timer for some predefined time and schedules an API Key update for that time. This means we're at 2 updates per batch. While we fixed permission issue for most of agents, slow updaters will still face this issue.
Pros:
http.max_content_lengthconfig optionCons:
Option 3: Modify bulker
Fleet server uses bulker for communicating with ES.
Option comes in mind when we introduce new kind of action
update_api_keyIn this case we dont' need to change much.
On step
4.bwe just call Bulker as usuallWe skip invalidating on step
6Inside bulker we would group updates with same roles into a single req with multiple agent ids.
Bulker executes when interval passes (5s) or expected request size is close to limit.
Number of agents included in a bulk is dependent on a checkin interval. In agent we use 1s checking frequency by default which means most agents should be handled within few calls making significant savings in term of request count.
Slow updaters won't run into situation in
Option 2as their API Keys will get updated on individual basis upon CheckinWe could also consider updates on ACK but in case ACK is lost this would mean even worse permission issue behavior.
Pros:
Cons:
thinking about these I like option 3, please if you see any issues with the approach or some step I did not take into account let me know
cc @ph
ph commentedon Aug 11, 2022
Good news, we don't have to deal with that special upgrade case.
I agree you would lose some of the benefit of bulking but you can also have a flush timeout with quantity flush. you would still bulk, but instead of bulking on the every agent associated to the policy you may bulk on a subset, which would still be better that n request that we have today.
ph commentedon Aug 11, 2022
I do like option 3, I've hit submit before reading option 3, just to clarify you will still need to have an expended permission set for this option no?
cc @cmacknz @joshdover
michalpristas commentedon Aug 11, 2022
if we're ok with up to 5 second skew then i don't need to extend and we will save a lot of requests.
otherwise we can do it in 2phase fashion: option 3 with extending but cleaning up on ACK which will reduce requests compared to today. but not to the minimal value, in case of ACK failure api key will exist with extended set of roles.
if this is considered security risk we should maybe aim for 5s skew
ph commentedon Aug 11, 2022
I don't think it's different as today when we have two api key are valid, and can be valid for an extended period of time.
michalpristas commentedon Aug 11, 2022
just point for me: for this option we need to make sure we dont have a race when ack update is applied before initial bulk update
ph commentedon Aug 11, 2022
So there could some ordering needed in that case ?
Let's say that the user is really going wild on the policy changes, no sure if there is some kind of debounce in fleet.
michalpristas commentedon Aug 11, 2022
there can be but if we take it as a queue/journal and compose on flush it should be fine, but it's something to keep in mind
aleksmaus commentedon Aug 11, 2022
👍 for option 3
joshdover commentedon Aug 11, 2022
+1 on option 3. The main benefit of this change is being able to avoid creating and hashing new keys. The bulk API is an additional optimization that will matter at scale, but less impactful than the change from create -> update. I like that option 3 allows us to use the same bulking pattern that we use for all other ES interactions (
.fleet-agentsupdates and searches).I think we will not do any creates, correct? It will still only be updates which are much cheaper, just that it will take multiple requests to do it. Since action/policy delivery is quite fast, I do expect the typical case to maximize usage of the bulk API quite fairly well.
I do think with option 3 we may still need to consider working around
http.max_content_size, though it'd be good to do some back-of-the-envelope math on how many API keys you'd need to have in a single bulk update request to hit this limit.I think we should still use the extended/union privileges + second update to reduce privileges in order to avoid any ingestion problems if there's an availability problem with Fleet Server. I think this is important especially for the agent running Fleet Server itself. If an integration is removed from that policy, the agent should have an opportunity to flush any remaining data before it's access is revoked.
Truly slow updaters should get kicked out of the Fleet / disabled by the unenrollment timeout.
AndersonQ commentedon Aug 11, 2022
👍 for option 3