-
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
Collapse two unenroll functions into one #94848
Changes from 1 commit
5564f25
1db2dd9
3d7b0f5
669525f
9a6ee18
c71ed57
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,11 +56,12 @@ export async function unenrollAgent( | |
export async function unenrollAgents( | ||
soClient: SavedObjectsClientContract, | ||
esClient: ElasticsearchClient, | ||
options: GetAgentsOptions | ||
options: GetAgentsOptions & { force?: boolean } | ||
) { | ||
// start with all agents specified | ||
const agents = await getAgents(esClient, options); | ||
|
||
// Filter to agents that are not already unenrolled, or unenrolling | ||
// Filter to those not already unenrolled, or unenrolling | ||
const agentsEnrolled = agents.filter( | ||
(agent) => !agent.unenrollment_started_at && !agent.unenrolled_at | ||
); | ||
|
@@ -71,30 +72,59 @@ export async function unenrollAgents( | |
) | ||
); | ||
const agentsToUpdate = agentsEnrolled.filter((_, index) => settled[index].status === 'fulfilled'); | ||
|
||
const now = new Date().toISOString(); | ||
|
||
// Create unenroll action for each agent | ||
await bulkCreateAgentActions( | ||
soClient, | ||
esClient, | ||
agentsToUpdate.map((agent) => ({ | ||
agent_id: agent.id, | ||
created_at: now, | ||
type: 'UNENROLL', | ||
})) | ||
); | ||
if (options.force) { | ||
// Get all API keys that need to be invalidated | ||
const apiKeys = agentsToUpdate.reduce<string[]>((keys, agent) => { | ||
if (agent.access_api_key_id) { | ||
keys.push(agent.access_api_key_id); | ||
} | ||
if (agent.default_api_key_id) { | ||
keys.push(agent.default_api_key_id); | ||
} | ||
|
||
return keys; | ||
}, []); | ||
|
||
// Invalidate all API keys | ||
if (apiKeys.length) { | ||
await APIKeyService.invalidateAPIKeys(soClient, apiKeys); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice, I think the removal of |
||
} | ||
// Update the necessary agents | ||
return bulkUpdateAgents( | ||
esClient, | ||
agentsToUpdate.map((agent) => ({ | ||
agentId: agent.id, | ||
data: { | ||
active: false, | ||
unenrolled_at: now, | ||
}, | ||
})) | ||
); | ||
} else { | ||
// Create unenroll action for each agent | ||
await bulkCreateAgentActions( | ||
soClient, | ||
esClient, | ||
agentsToUpdate.map((agent) => ({ | ||
agent_id: agent.id, | ||
created_at: now, | ||
type: 'UNENROLL', | ||
})) | ||
); | ||
|
||
// Update the necessary agents | ||
return bulkUpdateAgents( | ||
esClient, | ||
agentsToUpdate.map((agent) => ({ | ||
agentId: agent.id, | ||
data: { | ||
unenrollment_started_at: now, | ||
}, | ||
})) | ||
); | ||
// Update the necessary agents | ||
return bulkUpdateAgents( | ||
esClient, | ||
agentsToUpdate.map((agent) => ({ | ||
agentId: agent.id, | ||
data: { | ||
unenrollment_started_at: now, | ||
}, | ||
})) | ||
); | ||
} | ||
} | ||
|
||
export async function forceUnenrollAgent( | ||
|
@@ -118,41 +148,3 @@ export async function forceUnenrollAgent( | |
unenrolled_at: new Date().toISOString(), | ||
}); | ||
} | ||
|
||
export async function forceUnenrollAgents( | ||
soClient: SavedObjectsClientContract, | ||
esClient: ElasticsearchClient, | ||
options: GetAgentsOptions | ||
) { | ||
// Filter to agents that are not already unenrolled | ||
const agents = await getAgents(esClient, options); | ||
const agentsToUpdate = agents.filter((agent) => !agent.unenrolled_at); | ||
const now = new Date().toISOString(); | ||
const apiKeys: string[] = []; | ||
|
||
// Get all API keys that need to be invalidated | ||
agentsToUpdate.forEach((agent) => { | ||
if (agent.access_api_key_id) { | ||
apiKeys.push(agent.access_api_key_id); | ||
} | ||
if (agent.default_api_key_id) { | ||
apiKeys.push(agent.default_api_key_id); | ||
} | ||
}); | ||
|
||
// Invalidate all API keys | ||
if (apiKeys.length) { | ||
APIKeyService.invalidateAPIKeys(soClient, apiKeys); | ||
} | ||
// Update the necessary agents | ||
return bulkUpdateAgents( | ||
esClient, | ||
agentsToUpdate.map((agent) => ({ | ||
agentId: agent.id, | ||
data: { | ||
active: false, | ||
unenrolled_at: now, | ||
}, | ||
})) | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,11 +22,10 @@ export default function (providerContext: FtrProviderContext) { | |
skipIfNoDockerRegistry(providerContext); | ||
let accessAPIKeyId: string; | ||
let outputAPIKeyId: string; | ||
before(async () => { | ||
await esArchiver.load('fleet/agents'); | ||
}); | ||
setupFleetAndAgents(providerContext); | ||
beforeEach(async () => { | ||
await esArchiver.load('fleet/agents'); | ||
|
||
const { body: accessAPIKeyBody } = await esClient.security.createApiKey({ | ||
body: { | ||
name: `test access api key: ${uuid.v4()}`, | ||
|
@@ -61,7 +60,7 @@ export default function (providerContext: FtrProviderContext) { | |
}, | ||
}); | ||
}); | ||
after(async () => { | ||
afterEach(async () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved the cleanup to each There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You may still have to have a before and after that load empty fleet server fixtures as the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can add them both back, but do you know of any way for us to test/confirm? If they pass CI should we leave them out or would you still prefer to restore them? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if CI pass leave them out :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While developing, this test was failing it wouldn't remove 2 agents which were enrolled in a managed policy earlier. That's why I added the reset between tests. However, CI kept failing in the before each for that test with a I reverted the changes and CI passed 🤷🏻 🎉 |
||
await esArchiver.unload('fleet/agents'); | ||
}); | ||
|
||
|
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.
There was one difference between the filtering logic of the two functions.
force*
only testedunenrolled_at
vs this function which also checksunenrollment_started_at
I believe this is the correct logic, but let me know if it should be different if
force: true
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 think the difference was correct, as you could have to force unenroll an agent that started the enrollment before but never finished.