Skip to content

Update our API Key handling logic for updatable permission and bulk operation on the api endpoint #1581

Closed

Description

@ph

Situation

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.

Depends on

Activity

  1. added
    Team:Elastic-Agent-Control-Plane
    on Jun 22, 2022
  2. changed the title [-]Use our API Key handling logic[/-] [+]Use our API Key handling logic for updatable API Key and bulk operation[/+] on Jun 22, 2022
  3. 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
  4. ph commented on Jun 23, 2022

    @ph
    ContributorAuthor

    Single operation route PR elastic/elasticsearch#87870

  5. AndersonQ commented on Aug 1, 2022

    @AndersonQ
    Contributor

    @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?

  6. ph commented on Aug 3, 2022

    @ph
    ContributorAuthor

    @AndersonQ let me clarify, I was more curious about upgrade in the stack.

    1. 8.4 Api keys permissions are not updatable.
    2. 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.

    Is this more clear?

  7. michalpristas commented on Aug 11, 2022

    @michalpristas
    Contributor

    checked that

    • API Keys before change will 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)

    1. User changes ES policy, change is reflected in .fleet_policies
    2. fleet-server is notified about the change comparing to last checkpoints
    3. fleet-server loads the policy, populate roles definition based on policy content and updates it internally
    4. 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
    5. once agent handles config it will ACK the action
    6. 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

    cc @ph

  8. ph commented on Aug 11, 2022

    @ph
    ContributorAuthor

    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.

  9. ph commented on Aug 11, 2022

    @ph
    ContributorAuthor

    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

  10. michalpristas commented on Aug 11, 2022

    @michalpristas
    Contributor

    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

  11. ph commented on Aug 11, 2022

    @ph
    ContributorAuthor

    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.

  12. michalpristas commented on Aug 11, 2022

    @michalpristas
    Contributor

    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

  13. ph commented on Aug 11, 2022

    @ph
    ContributorAuthor

    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.

  14. michalpristas commented on Aug 11, 2022

    @michalpristas
    Contributor

    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

  15. aleksmaus commented on Aug 11, 2022

    @aleksmaus
    Contributor

    👍 for option 3

  16. joshdover commented on Aug 11, 2022

    @joshdover
    Contributor

    +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.

  17. AndersonQ commented on Aug 11, 2022

    @AndersonQ
    Contributor

    👍 for option 3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Team:Elastic-Agent-Control-Planev8.5.0

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions