-
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 agents write APIs space aware #188507
Merged
jillguyonnet
merged 13 commits into
elastic:main
from
jillguyonnet:fleet/185040-rbac-agents-write-api
Jul 25, 2024
Merged
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
55389b2
[Fleet] RBAC - Make agents write APIs space aware
jillguyonnet 828a0af
Add bulk update agent tags endpoint
jillguyonnet 95cd473
Add API integration test for bulk update tags
jillguyonnet 6c11b42
Add unit tests for agent namespace helpers
jillguyonnet c4b034f
Merge branch 'main' into fleet/185040-rbac-agents-write-api
elasticmachine 7c00dde
Merge branch 'main' into fleet/185040-rbac-agents-write-api
elasticmachine 5d00614
Add namespace property to actions and action results
jillguyonnet 6a87d09
Change namespace to namespaces
jillguyonnet 82d8158
Fix integration tests
jillguyonnet ff98d7a
Put behind feature flag
jillguyonnet 80ff19f
Merge remote-tracking branch 'origin/main' into fleet/185040-rbac-age…
jillguyonnet 2e3e2cb
Fix unit tests
jillguyonnet 616d3bf
Add small utility function
jillguyonnet 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
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
72 changes: 72 additions & 0 deletions
72
x-pack/plugins/fleet/server/services/agents/namespace.test.ts
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import type { Agent } from '../../types'; | ||
|
||
import { agentsKueryNamespaceFilter, isAgentInNamespace } from './namespace'; | ||
|
||
describe('isAgentInNamespace', () => { | ||
describe('when the namespace is defined', () => { | ||
it('returns true if the agent namespaces include the namespace', () => { | ||
const agent = { id: '123', namespaces: ['default', 'space1'] } as Agent; | ||
expect(isAgentInNamespace(agent, 'space1')).toEqual(true); | ||
}); | ||
|
||
it('returns false if the agent namespaces do not include the namespace', () => { | ||
const agent = { id: '123', namespaces: ['default', 'space1'] } as Agent; | ||
expect(isAgentInNamespace(agent, 'space2')).toEqual(false); | ||
}); | ||
|
||
it('returns false if the agent has zero length namespaces', () => { | ||
const agent = { id: '123', namespaces: [] as string[] } as Agent; | ||
expect(isAgentInNamespace(agent, 'space1')).toEqual(false); | ||
}); | ||
|
||
it('returns false if the agent does not have namespaces', () => { | ||
const agent = { id: '123' } as Agent; | ||
expect(isAgentInNamespace(agent, 'space1')).toEqual(false); | ||
}); | ||
}); | ||
|
||
describe('when the namespace is undefined', () => { | ||
it('returns true if the agent does not have namespaces', () => { | ||
const agent = { id: '123' } as Agent; | ||
expect(isAgentInNamespace(agent)).toEqual(true); | ||
}); | ||
|
||
it('returns true if the agent has zero length namespaces', () => { | ||
const agent = { id: '123', namespaces: [] as string[] } as Agent; | ||
expect(isAgentInNamespace(agent)).toEqual(true); | ||
}); | ||
|
||
it('returns true if the agent namespaces include the default one', () => { | ||
const agent = { id: '123', namespaces: ['default'] } as Agent; | ||
expect(isAgentInNamespace(agent)).toEqual(true); | ||
}); | ||
|
||
it('returns false if the agent namespaces include the default one', () => { | ||
const agent = { id: '123', namespaces: ['space1'] } as Agent; | ||
expect(isAgentInNamespace(agent)).toEqual(false); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('agentsKueryNamespaceFilter', () => { | ||
it('returns undefined if the namespace is undefined', () => { | ||
expect(agentsKueryNamespaceFilter()).toBeUndefined(); | ||
}); | ||
|
||
it('returns a kuery for the default space', () => { | ||
expect(agentsKueryNamespaceFilter('default')).toEqual( | ||
'namespaces:(default) or not namespaces:*' | ||
); | ||
}); | ||
|
||
it('returns a kuery for custom spaces', () => { | ||
expect(agentsKueryNamespaceFilter('space1')).toEqual('namespaces:(space1)'); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { DEFAULT_NAMESPACE_STRING } from '@kbn/core-saved-objects-utils-server'; | ||
|
||
import type { Agent } from '../../types'; | ||
|
||
export function isAgentInNamespace(agent: Agent, namespace?: string) { | ||
jillguyonnet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return ( | ||
(namespace && agent.namespaces?.includes(namespace)) || | ||
(!namespace && | ||
(!agent.namespaces || | ||
agent.namespaces.length === 0 || | ||
agent.namespaces?.includes(DEFAULT_NAMESPACE_STRING))) | ||
); | ||
} | ||
|
||
export function agentsKueryNamespaceFilter(namespace?: string) { | ||
if (!namespace) { | ||
return; | ||
} | ||
return namespace === DEFAULT_NAMESPACE_STRING | ||
? `namespaces:(${DEFAULT_NAMESPACE_STRING}) or not namespaces:*` | ||
: `namespaces:(${namespace})`; | ||
} |
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.
nit: I would be tempted to move these three line to a separate function, something like
findAgentInNamespace
, since it's repeated three times in the same file. What do you think?