Skip to content

Commit

Permalink
Rename force param to revoke for agent unenroll. Fix tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
John Schulz committed Apr 13, 2021
1 parent d9167b5 commit fcbc9d9
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions x-pack/plugins/fleet/server/routes/agent/unenroll_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const postAgentUnenrollHandler: RequestHandler<
const soClient = context.core.savedObjects.client;
const esClient = context.core.elasticsearch.client.asInternalUser;
try {
if (request.body?.force === true) {
if (request.body?.revoke === true) {
await AgentService.forceUnenrollAgent(soClient, esClient, request.params.agentId);
} else {
await AgentService.unenrollAgent(soClient, esClient, request.params.agentId);
Expand Down Expand Up @@ -62,7 +62,7 @@ export const postBulkAgentsUnenrollHandler: RequestHandler<
try {
const results = await AgentService.unenrollAgents(soClient, esClient, {
...agentOptions,
force: request.body?.force,
revoke: request.body?.revoke,
});
const body = results.items.reduce<PostBulkAgentUnenrollResponse>((acc, so) => {
acc[so.id] = {
Expand Down
8 changes: 4 additions & 4 deletions x-pack/plugins/fleet/server/services/agents/unenroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ export async function unenrollAgent(
export async function unenrollAgents(
soClient: SavedObjectsClientContract,
esClient: ElasticsearchClient,
options: GetAgentsOptions & { force?: boolean }
options: GetAgentsOptions & { revoke?: boolean }
): Promise<{ items: BulkActionResult[] }> {
// start with all agents specified
const givenAgents = await getAgents(esClient, options);

// Filter to those not already unenrolled, or unenrolling
const agentsEnrolled = givenAgents.filter((agent) => {
if (options.force) {
if (options.revoke) {
return !agent.unenrolled_at;
}
return !agent.unenrollment_started_at && !agent.unenrolled_at;
Expand All @@ -87,7 +87,7 @@ export async function unenrollAgents(
}, []);

const now = new Date().toISOString();
if (options.force) {
if (options.revoke) {
// Get all API keys that need to be invalidated
await invalidateAPIKeysForAgents(agentsToUpdate);
} else {
Expand All @@ -104,7 +104,7 @@ export async function unenrollAgents(
}

// Update the necessary agents
const updateData = options.force
const updateData = options.revoke
? { unenrolled_at: now, active: false }
: { unenrollment_started_at: now };

Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/fleet/server/types/rest_spec/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,15 @@ export const PostAgentUnenrollRequestSchema = {
}),
body: schema.nullable(
schema.object({
force: schema.boolean(),
revoke: schema.boolean(),
})
),
};

export const PostBulkAgentUnenrollRequestSchema = {
body: schema.object({
agents: schema.oneOf([schema.arrayOf(schema.string()), schema.string()]),
force: schema.maybe(schema.boolean()),
revoke: schema.maybe(schema.boolean()),
}),
};

Expand Down
2 changes: 1 addition & 1 deletion x-pack/test/fleet_api_integration/apis/agents/reassign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export default function (providerContext: FtrProviderContext) {
.post(`/api/fleet/agents/bulk_reassign`)
.set('kbn-xsrf', 'xxx')
.send({
agents: 'fleet-agents.active: true',
agents: 'active: true',
policy_id: 'policy2',
})
.expect(200);
Expand Down
8 changes: 4 additions & 4 deletions x-pack/test/fleet_api_integration/apis/agents/unenroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ export default function (providerContext: FtrProviderContext) {
await supertest.post(`/api/fleet/agents/agent1/unenroll`).set('kbn-xsrf', 'xxx').expect(200);
});

it('/agents/{agent_id}/unenroll { force: true } should invalidate related API keys', async () => {
it('/agents/{agent_id}/unenroll { revoke: true } should invalidate related API keys', async () => {
await supertest
.post(`/api/fleet/agents/agent1/unenroll`)
.set('kbn-xsrf', 'xxx')
.send({
force: true,
revoke: true,
})
.expect(200);

Expand Down Expand Up @@ -192,8 +192,8 @@ export default function (providerContext: FtrProviderContext) {
.post(`/api/fleet/agents/bulk_unenroll`)
.set('kbn-xsrf', 'xxx')
.send({
agents: 'fleet-agents.active: true',
force: true,
agents: 'active: true',
revoke: true,
})
.expect(200);

Expand Down

0 comments on commit fcbc9d9

Please sign in to comment.