Skip to content

Commit

Permalink
Merge branch '8.11' into backport/8.11/pr-170499
Browse files Browse the repository at this point in the history
  • Loading branch information
ashokaditya authored Nov 8, 2023
2 parents c884617 + f083d2d commit 727ba64
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 21 deletions.
20 changes: 12 additions & 8 deletions x-pack/plugins/fleet/common/openapi/bundled.json
Original file line number Diff line number Diff line change
Expand Up @@ -2740,14 +2740,6 @@
"schema": {
"type": "object",
"properties": {
"revoke": {
"type": "boolean",
"description": "Revokes API keys of agents"
},
"force": {
"type": "boolean",
"description": "Unenroll hosted agents too"
},
"agents": {
"oneOf": [
{
Expand All @@ -2762,6 +2754,18 @@
"description": "list of agent IDs"
}
]
},
"revoke": {
"type": "boolean",
"description": "Revokes API keys of agents"
},
"force": {
"type": "boolean",
"description": "Unenrolls hosted agents too"
},
"includeInactive": {
"type": "boolean",
"description": "When passing agents by KQL query, unenrolls inactive agents too"
}
},
"required": [
Expand Down
17 changes: 11 additions & 6 deletions x-pack/plugins/fleet/common/openapi/bundled.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1719,12 +1719,6 @@ paths:
schema:
type: object
properties:
revoke:
type: boolean
description: Revokes API keys of agents
force:
type: boolean
description: Unenroll hosted agents too
agents:
oneOf:
- type: string
Expand All @@ -1733,6 +1727,17 @@ paths:
items:
type: string
description: list of agent IDs
revoke:
type: boolean
description: Revokes API keys of agents
force:
type: boolean
description: Unenrolls hosted agents too
includeInactive:
type: boolean
description: >-
When passing agents by KQL query, unenrolls inactive agents
too
required:
- agents
example:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ post:
schema:
type: object
properties:
revoke:
type: boolean
description: Revokes API keys of agents
force:
type: boolean
description: Unenroll hosted agents too
agents:
oneOf:
- type: string
Expand All @@ -37,6 +31,15 @@ post:
items:
type: string
description: list of agent IDs
revoke:
type: boolean
description: Revokes API keys of agents
force:
type: boolean
description: Unenrolls hosted agents too
includeInactive:
type: boolean
description: When passing agents by KQL query, unenrolls inactive agents too
required:
- agents
example:
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/fleet/common/types/rest_spec/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export interface PostBulkAgentUnenrollRequest {
agents: string[] | string;
force?: boolean;
revoke?: boolean;
includeInactive?: boolean;
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export const AgentUnenrollAgentModal: React.FunctionComponent<Props> = ({
: await sendPostBulkAgentUnenroll({
agents: Array.isArray(agents) ? agents.map((agent) => agent.id) : agents,
revoke: forceUnenroll,
// includeInactive is only used when the agents are selected by query, it's ignored in the case of agent ids
includeInactive: true,
});
if (error) {
throw error;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export const postBulkAgentsUnenrollHandler: RequestHandler<
revoke: request.body?.revoke,
force: request.body?.force,
batchSize: request.body?.batchSize,
showInactive: request.body?.includeInactive,
});

return response.ok({ body: { actionId: results.actionId } });
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/fleet/server/services/agents/unenroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export async function unenrollAgents(
force?: boolean;
revoke?: boolean;
batchSize?: number;
showInactive?: boolean;
}
): Promise<{ actionId: string }> {
if ('agentIds' in options) {
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/fleet/server/types/rest_spec/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export const PostBulkAgentUnenrollRequestSchema = {
force: schema.maybe(schema.boolean()),
revoke: schema.maybe(schema.boolean()),
batchSize: schema.maybe(schema.number()),
includeInactive: schema.maybe(schema.boolean()),
}),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ export async function getVisTypeFactory(lens: LensPublicStart) {
export async function isCompatibleVisualizationType(chartInfo: ChartInfo) {
return (
chartInfo.visualizationType === COMPATIBLE_VISUALIZATION &&
// @ts-expect-error esql is missing in the type
chartInfo.query.esql === undefined &&
chartInfo.layers.some((l) => l.layerType === layerTypes.DATA && l.dataView !== undefined)
);
}
Expand Down
75 changes: 74 additions & 1 deletion x-pack/test/fleet_api_integration/apis/agents/unenroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import expect from '@kbn/expect';
import { v4 as uuidv4 } from 'uuid';

import { AGENTS_INDEX } from '@kbn/fleet-plugin/common';
import { FtrProviderContext } from '../../../api_integration/ftr_provider_context';
import { setupFleetAndAgents } from './services';
import { skipIfNoDockerRegistry } from '../../helpers';
Expand Down Expand Up @@ -149,7 +150,7 @@ export default function (providerContext: FtrProviderContext) {
expect(actionStatus.nbAgentsFailed).to.eql(2);
});

it('/agents/bulk_unenroll should allow to unenroll multiple agents by id from an regular agent policy', async () => {
it('/agents/bulk_unenroll should allow to unenroll multiple agents by id from a regular agent policy', async () => {
// set policy to regular
await supertest
.put(`/api/fleet/agent_policies/policy1`)
Expand Down Expand Up @@ -188,6 +189,78 @@ export default function (providerContext: FtrProviderContext) {
expect(body.total).to.eql(0);
});

it('/agents/bulk_unenroll should allow to unenroll active and inactive agents by kuery with includeInactive', async () => {
// Agent inactive
await esClient.update({
id: 'agent4',
refresh: 'wait_for',
index: AGENTS_INDEX,
body: {
doc: {
policy_id: 'policy1',
policy_revision_idx: 1,
last_checkin: new Date(Date.now() - 1000 * 60).toISOString(), // policy timeout 1 min
},
},
});
// unenroll all agents that had last checkin before "now"
await supertest
.post(`/api/fleet/agents/bulk_unenroll`)
.set('kbn-xsrf', 'xxx')
.send({
agents: `last_checkin<="${new Date(Date.now()).toISOString()}"`,
revoke: true,
includeInactive: true,
})
.expect(200);

const { body } = await supertest.get(`/api/fleet/agents`);
expect(body.total).to.eql(0);
});
it('/agents/bulk_unenroll should allow to unenroll inactive agents that never had last checkin by kuery with includeInactive', async () => {
// Agent inactive
await esClient.update({
id: 'agent4',
refresh: 'wait_for',
index: AGENTS_INDEX,
body: {
doc: {
policy_id: 'policy1',
policy_revision_idx: 1,
last_checkin: new Date(Date.now() - 1000 * 60).toISOString(), // policy timeout 1 min
},
},
});
// agent inactive through enrolled_at as no last_checkin
await esClient.create({
id: 'agent5',
refresh: 'wait_for',
index: AGENTS_INDEX,
document: {
active: true,
access_api_key_id: 'api-key-4',
policy_id: 'policy1',
type: 'PERMANENT',
local_metadata: { host: { hostname: 'host6' } },
user_provided_metadata: {},
enrolled_at: new Date(Date.now() - 1000 * 60).toISOString(), // policy timeout 1 min
},
});
// unenroll all agents
await supertest
.post(`/api/fleet/agents/bulk_unenroll`)
.set('kbn-xsrf', 'xxx')
.send({
agents: 'active: true',
revoke: true,
includeInactive: true,
})
.expect(200);

const { body } = await supertest.get(`/api/fleet/agents`);
expect(body.total).to.eql(0);
});

it('/agents/bulk_unenroll should allow to unenroll multiple agents by kuery in batches async', async () => {
const { body } = await supertest
.post(`/api/fleet/agents/bulk_unenroll`)
Expand Down

0 comments on commit 727ba64

Please sign in to comment.