-
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] Managed Agent Policy #88688
Merged
Merged
[Fleet] Managed Agent Policy #88688
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
dcaf8da
Add is_managed (default false) to SO
42d4848
is_managed SO prop defaults false. +some ftr tests
6187b12
Add is_managed in some tests to fix type errors
73414f9
Block unenrolling from a managed agent policy
a4c39c5
Cannot reassign agent enrolled in managed policy
d676705
Merge branch 'master' into 76843-managed-agent-policy
kibanamachine bb3923f
Add tests for AgentPolicyService create & update
ac10a47
API tests for agents (bulk_)unenroll endpoints
988b700
Add a method to get the agent policy for an agent
acb08cd
Merge branch 'master' into 76843-managed-agent-policy
a01e524
Add unsaved/missing esClient arguments after merging main
448f12d
Merge branch 'master' into 76843-managed-agent-policy
kibanamachine 9936144
Don't throw if there's no associated agent policy
bbefa04
typo
8cddf6d
Tests for reassign
e19b0d6
Tests for reassign
445e746
Add migration for managed agent policy
e8554de
Merge branch '76843-managed-agent-policy' of github.com:jfsiii/kibana…
c77c851
Fix broken code in reassignment permissions check
f46437e
Merge branch 'master' into 76843-managed-agent-policy
kibanamachine 92f888b
Fix broken test. Pass correct value
bd6b286
Merge branch '76843-managed-agent-policy' of github.com:jfsiii/kibana…
4a0d342
Merge branch 'master' into 76843-managed-agent-policy
kibanamachine 86d6958
Add IngestManagerError and tests
a15a0d0
Merge branch 'master' into 76843-managed-agent-policy
kibanamachine b6b8ce0
Merge branch 'master' into 76843-managed-agent-policy
13994c7
Fix license header
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
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
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
132 changes: 132 additions & 0 deletions
132
x-pack/plugins/fleet/server/services/agents/reassign.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,132 @@ | ||
/* | ||
* 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 { elasticsearchServiceMock, savedObjectsClientMock } from 'src/core/server/mocks'; | ||
import type { SavedObject } from 'kibana/server'; | ||
import type { Agent, AgentPolicy } from '../../types'; | ||
import { AgentReassignmentError } from '../../errors'; | ||
import { reassignAgent, reassignAgents } from './reassign'; | ||
|
||
const agentInManagedSO = { | ||
id: 'agent-in-managed-policy', | ||
attributes: { policy_id: 'managed-agent-policy' }, | ||
} as SavedObject<Agent>; | ||
const agentInManagedSO2 = { | ||
id: 'agent-in-managed-policy2', | ||
attributes: { policy_id: 'managed-agent-policy' }, | ||
} as SavedObject<Agent>; | ||
const agentInUnmanagedSO = { | ||
id: 'agent-in-unmanaged-policy', | ||
attributes: { policy_id: 'unmanaged-agent-policy' }, | ||
} as SavedObject<Agent>; | ||
const agentInUnmanagedSO2 = { | ||
id: 'agent-in-unmanaged-policy2', | ||
attributes: { policy_id: 'unmanaged-agent-policy' }, | ||
} as SavedObject<Agent>; | ||
const unmanagedAgentPolicySO = { | ||
id: 'unmanaged-agent-policy', | ||
attributes: { is_managed: false }, | ||
} as SavedObject<AgentPolicy>; | ||
const managedAgentPolicySO = { | ||
id: 'managed-agent-policy', | ||
attributes: { is_managed: true }, | ||
} as SavedObject<AgentPolicy>; | ||
|
||
describe('reassignAgent (singular)', () => { | ||
it('can reassign from unmanaged policy to unmanaged', async () => { | ||
const soClient = createClientMock(); | ||
const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser; | ||
await reassignAgent(soClient, esClient, agentInUnmanagedSO.id, agentInUnmanagedSO2.id); | ||
|
||
// calls ES update with correct values | ||
expect(soClient.update).toBeCalledTimes(1); | ||
const calledWith = soClient.update.mock.calls[0]; | ||
expect(calledWith[1]).toBe(agentInUnmanagedSO.id); | ||
expect(calledWith[2]).toHaveProperty('policy_id', agentInUnmanagedSO2.id); | ||
}); | ||
|
||
it('cannot reassign from unmanaged policy to managed', async () => { | ||
const soClient = createClientMock(); | ||
const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser; | ||
await expect( | ||
reassignAgent( | ||
soClient, | ||
esClient, | ||
agentInUnmanagedSO.id, | ||
agentInManagedSO.attributes.policy_id! | ||
) | ||
).rejects.toThrowError(AgentReassignmentError); | ||
|
||
// does not call ES update | ||
expect(soClient.update).toBeCalledTimes(0); | ||
}); | ||
|
||
it('cannot reassign from managed policy', async () => { | ||
const soClient = createClientMock(); | ||
const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser; | ||
await expect( | ||
reassignAgent(soClient, esClient, agentInManagedSO.id, agentInManagedSO2.id) | ||
).rejects.toThrowError(AgentReassignmentError); | ||
// does not call ES update | ||
expect(soClient.update).toBeCalledTimes(0); | ||
|
||
await expect( | ||
reassignAgent(soClient, esClient, agentInManagedSO.id, agentInUnmanagedSO.id) | ||
).rejects.toThrowError(AgentReassignmentError); | ||
// does not call ES update | ||
expect(soClient.update).toBeCalledTimes(0); | ||
}); | ||
}); | ||
|
||
describe('reassignAgents (plural)', () => { | ||
it('agents in managed policies are not updated', async () => { | ||
const soClient = createClientMock(); | ||
const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser; | ||
const idsToReassign = [agentInUnmanagedSO.id, agentInManagedSO.id, agentInUnmanagedSO.id]; | ||
await reassignAgents(soClient, esClient, { agentIds: idsToReassign }, agentInUnmanagedSO.id); | ||
|
||
// calls ES update with correct values | ||
const calledWith = soClient.bulkUpdate.mock.calls[0][0]; | ||
const expectedResults = [agentInUnmanagedSO.id, agentInUnmanagedSO.id]; | ||
expect(calledWith.length).toBe(expectedResults.length); // only 2 are unmanaged | ||
expect(calledWith.map(({ id }) => id)).toEqual(expectedResults); | ||
}); | ||
}); | ||
|
||
function createClientMock() { | ||
const soClientMock = savedObjectsClientMock.create(); | ||
|
||
// need to mock .create & bulkCreate due to (bulk)createAgentAction(s) in reassignAgent(s) | ||
soClientMock.create.mockResolvedValue(agentInUnmanagedSO); | ||
soClientMock.bulkCreate.mockImplementation(async ([{ type, attributes }]) => { | ||
return { | ||
saved_objects: [await soClientMock.create(type, attributes)], | ||
}; | ||
}); | ||
|
||
soClientMock.get.mockImplementation(async (_, id) => { | ||
switch (id) { | ||
case unmanagedAgentPolicySO.id: | ||
return unmanagedAgentPolicySO; | ||
case managedAgentPolicySO.id: | ||
return managedAgentPolicySO; | ||
case agentInManagedSO.id: | ||
return agentInManagedSO; | ||
case agentInUnmanagedSO.id: | ||
default: | ||
return agentInUnmanagedSO; | ||
} | ||
}); | ||
|
||
soClientMock.bulkGet.mockImplementation(async (options) => { | ||
return { | ||
saved_objects: await Promise.all(options!.map(({ type, id }) => soClientMock.get(type, id))), | ||
}; | ||
}); | ||
|
||
return soClientMock; | ||
} |
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.
Do we need the
isV12
here? the migration will only run when it's migrated one time right?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.
Right. It's left over from when I was debug logging; it also felt like a long
if
. I guess it also behasManagedAttr
or not a problem to inline it.