-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[Fleet] RBAC - Make read agent actions space aware #189519
[Fleet] RBAC - Make read agent actions space aware #189519
Conversation
🤖 GitHub commentsExpand to view the GitHub comments
Just comment with:
|
/ci |
a1264cd
to
3e64318
Compare
Pinging @elastic/fleet (Team:Fleet) |
@@ -39,6 +40,7 @@ export const postNewAgentActionHandlerBuilder = function ( | |||
created_at: new Date().toISOString(), | |||
...newAgentAction, | |||
agents: [agent.id], |
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.
Should we validate the agent is in the current namespace?
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.
I believe the call to actionsService.getAgent
already handles that. I've added an integration test to cover that case.
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.
Just checked what is done in actionsService.getAgent
and it seems there is no namespace validation there.
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.
Please correct me if I'm wrong :)
actionsService.getAgent
is defined here- the
postNewAgentActionHandlerBuilder
maps it toAgentService.getAgentById
here - and
getAgentById
contains namespace validation here
I've tried it manually as well and I'm seeing 404 for agent ids in other spaces. Am I missing anything?
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.
Looks like I missed that change in getAgentById
👍
💛 Build succeeded, but was flaky
Failed CI StepsTest Failures
Metrics [docs]
History
To update your PR or re-run it, just comment with: |
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.
LGTM 🚀
## Summary Closes #185040 Followup to: #188507 #189519 #190069 This PR contains the last required changed for making Fleet agents write APIs space aware: * Implement space awareness in the following endpoints: * `POST /agents/{agentId}/unenroll` * `POST /agents/{agentId}/request_diagnostics` * `POST /agents/bulk_unenroll` * `POST /agents/bulk_request_diagnostics` * Fix a bug in `POST /agents/bulk_update_agent_tags` where the space id was not passed. * Simply the setting of the `namespaces` property when creating agent actions when the space id is known. * Rename `currentNamespace` to `currentSpaceId` where appropriate (see comment below). * Add API integration tests and consolidate existing ones. ~⚠️ At the time of writing, I would like there to be more tests covering bulk query processing in batches, which are currently lacking. I have experienced difficulties getting those tests to pass consistently.~ Filed [followup issue](#191643) for those. ### A note on terminology As pointed out in #191083 (comment), it seems that the terms "namespace" and "space id" are occasionally used interchangeably in some parts of the codebase to refer to a Kibana space. For instance, documents in Fleet indices (agents, agent policies, agent actions...) [possess a `namespaces` property](elastic/elasticsearch#108363) to track the spaces they belong to. The current space id is also returned using the Saved Object client's `getCurrentNamespace` function. However, "namespace" is also a datastream property. In the Agent policy settings UI, the "Spaces" property (which will be linked to the saved object's `namespaces` property) is above the "Default namespace" property, which relates to the integration's data streams: <img width="1916" alt="Screenshot 2024-08-26 at 14 51 10" src="https://github.com/user-attachments/assets/fe2a0948-3387-4a93-96dc-90fc5cf1a683"> This should not be a source of major issues, but is best clarified for future reference. In this PR, I've replaced some occurrences of `namespace` with `spaceId` where appropriate to try to maximise the use of the latter. ### Testing * This PR should be put through the Flaky Test Runner prior to merging (I will kick the job). * Manual testing should also be performed for the new endpoints mentioned above. ### Checklist - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios - [x] [Flaky Test Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was used on any tests changed --------- Co-authored-by: Elastic Machine <[email protected]>
Summary
Relates to #185040
This PR makes the following Fleet agents API space aware (behind
useSpaceAwareness
feature flag):GET /agents/action_status
POST /agents/{agentId}/actions
I have already started work on
POST /agents/{agentId}/actions/{actionId}/cancel
but I think it would best grouped withPOST /agents/{agentId}/upgrade
andPOST /agents/bulk_upgrade
in a separate PR.Details
GET /agents/action_status
useSpaceAwareness
feature flag is disabled, there is no change.namespaces: ['default']
and those with nonamespaces
property are returned.namespaces: ['spaceName']
are returned.This is to ensure older actions with no
namespaces
property are not lost. Feedback on this approach would be awesome.NB: only tag update agent actions and agent policy update actions have a
namespaces
property at the moment.POST /agents/{agentId}/actions
useSpaceAwareness
feature flag is enabled, the action fails if the agent is not in the current space.namespaces
property is populated when the action is created.Other
This PR also fixes an issue with setting
namespaces
in agent actions for tags update in the default space (this is because I didn't realisesoClient.getCurrentNamespace()
returnsundefined
in the default space).isAgentInNamespace
helper to returntrue
in the default space for agents with no explicitly set namespaces.Finally, this PR introduces the following helpers:
getCurrentNamespace(soClient)
: this helper returns the stringdefault
instead ofundefined
in the default space, which seems to be the behaviour we want most of the time.addNamespaceFilteringToQuery
: this helper extends the ES queries used in the action status service to conditionally add filtering by namespace as described above. It should be reusable for other endpoints as well.isAgentInNamespace
andagentsKueryNamespaceFilter
were moved into thefleet/server/services/spaces
folder where other space-related helpers live.Testing
GET /agents/action_status
, the best would be to have a custom space and create a mix of agent and agent policy actions across the default and the custom spaces, for instance:namespaces
)namespaces
property)Then check the output of
GET /agents/action_status
from the Dev Tools and the UI (agent activity flyout):POST /agents/{agentId}/actions
from the Dev Tools with any action type, e.g."UNENROLL"
:namespaces
property.Checklist