-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[Fleet] moving action batching to async #138870
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
Changes from all commits
c23db52
6cdbf4e
d9da608
d8e968f
e7fd443
8e5667a
3c94459
8a31e6c
8d705a6
8d692d2
8da6894
1afb694
1137591
6de094e
07f3283
83fe4b6
ec22cbc
ddca370
0fb109c
a117ba3
dab606f
f4653e2
e38afd0
edd2ab5
0bf645a
88a6362
a59df36
92db446
53c55c4
dc10593
76a87fd
abea983
32579ff
6d73425
6c81a64
6978057
f02b268
5525dc1
97b1edd
951566c
f1eb373
be50c8f
c0b02d7
f26a1f5
9e3c01c
fa9bb47
7e95311
1d7643b
921e96c
c303407
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 |
|---|---|---|
|
|
@@ -47,6 +47,7 @@ export interface NewAgentAction { | |
| start_time?: string; | ||
| minimum_execution_duration?: number; | ||
| source_uri?: string; | ||
| total?: number; | ||
| } | ||
|
|
||
| export interface AgentAction extends NewAgentAction { | ||
|
|
@@ -104,6 +105,22 @@ export interface CurrentUpgrade { | |
| startTime?: string; | ||
| } | ||
|
|
||
| export interface ActionStatus { | ||
| actionId: string; | ||
| // how many agents are successfully included in action documents | ||
| nbAgentsActionCreated: number; | ||
|
Contributor
Author
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.
|
||
| // how many agents acknowledged the action sucessfully (completed) | ||
| nbAgentsAck: number; | ||
| version: string; | ||
| startTime?: string; | ||
| type?: string; | ||
| // how many agents were actioned by the user | ||
| nbAgentsActioned: number; | ||
| status: 'complete' | 'expired' | 'cancelled' | 'failed' | 'in progress'; | ||
| errorMessage?: string; | ||
| } | ||
|
|
||
| // Generated from FleetServer schema.json | ||
| interface FleetServerAgentComponentUnit { | ||
| id: string; | ||
| type: 'input' | 'output'; | ||
|
|
@@ -122,8 +139,6 @@ interface FleetServerAgentComponent { | |
| units: FleetServerAgentComponentUnit[]; | ||
| } | ||
|
|
||
| // Initially generated from FleetServer schema.json | ||
|
|
||
| /** | ||
| * An Elastic Agent that has enrolled into Fleet | ||
| */ | ||
|
|
@@ -309,5 +324,7 @@ export interface FleetServerAgentAction { | |
| data?: { | ||
| [k: string]: unknown; | ||
| }; | ||
|
|
||
| total?: number; | ||
| [k: string]: unknown; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| /* | ||
|
Member
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. Thanks for adding this script to help with testing and local dev setup. |
||
| * 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 fetch from 'node-fetch'; | ||
| import { ToolingLog } from '@kbn/tooling-log'; | ||
| import uuid from 'uuid/v4'; | ||
|
|
||
| const KIBANA_URL = 'http://localhost:5601'; | ||
| const KIBANA_USERNAME = 'elastic'; | ||
| const KIBANA_PASSWORD = 'changeme'; | ||
|
|
||
| const ES_URL = 'http://localhost:9200'; | ||
| const ES_SUPERUSER = 'fleet_superuser'; | ||
| const ES_PASSWORD = 'password'; | ||
|
|
||
| async function createAgentDocsBulk(policyId: string, count: number) { | ||
| const auth = 'Basic ' + Buffer.from(ES_SUPERUSER + ':' + ES_PASSWORD).toString('base64'); | ||
| const body = ( | ||
| '{ "index":{ } }\n' + | ||
| JSON.stringify({ | ||
| access_api_key_id: 'api-key-1', | ||
| active: true, | ||
| policy_id: policyId, | ||
| type: 'PERMANENT', | ||
| local_metadata: { | ||
| elastic: { | ||
| agent: { | ||
| snapshot: false, | ||
| upgradeable: true, | ||
| version: '8.2.0', | ||
| }, | ||
| }, | ||
| host: { hostname: uuid() }, | ||
| }, | ||
| user_provided_metadata: {}, | ||
| enrolled_at: new Date().toISOString(), | ||
| last_checkin: new Date().toISOString(), | ||
| tags: ['script_create_agents'], | ||
| }) + | ||
| '\n' | ||
| ).repeat(count); | ||
| const res = await fetch(`${ES_URL}/.fleet-agents/_bulk`, { | ||
| method: 'post', | ||
| body, | ||
| headers: { | ||
| Authorization: auth, | ||
| 'Content-Type': 'application/x-ndjson', | ||
| }, | ||
| }); | ||
| const data = await res.json(); | ||
| return data; | ||
| } | ||
|
|
||
| async function createSuperUser() { | ||
| const auth = 'Basic ' + Buffer.from(KIBANA_USERNAME + ':' + KIBANA_PASSWORD).toString('base64'); | ||
| const roleRes = await fetch(`${ES_URL}/_security/role/${ES_SUPERUSER}`, { | ||
| method: 'post', | ||
| body: JSON.stringify({ | ||
| indices: [ | ||
| { | ||
| names: ['.fleet*'], | ||
| privileges: ['all'], | ||
| allow_restricted_indices: true, | ||
| }, | ||
| ], | ||
| }), | ||
| headers: { | ||
| Authorization: auth, | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| }); | ||
| const role = await roleRes.json(); | ||
| const userRes = await fetch(`${ES_URL}/_security/user/${ES_SUPERUSER}`, { | ||
| method: 'post', | ||
| body: JSON.stringify({ | ||
| password: ES_PASSWORD, | ||
| roles: ['superuser', ES_SUPERUSER], | ||
| }), | ||
| headers: { | ||
| Authorization: auth, | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| }); | ||
| const user = await userRes.json(); | ||
| return { role, user }; | ||
| } | ||
|
|
||
| async function createAgentPolicy(id: string) { | ||
| const auth = 'Basic ' + Buffer.from(KIBANA_USERNAME + ':' + KIBANA_PASSWORD).toString('base64'); | ||
| const res = await fetch(`${KIBANA_URL}/api/fleet/agent_policies`, { | ||
| method: 'post', | ||
| body: JSON.stringify({ | ||
| id, | ||
| name: id, | ||
| namespace: 'default', | ||
| description: '', | ||
| monitoring_enabled: ['logs'], | ||
| data_output_id: 'fleet-default-output', | ||
| monitoring_output_id: 'fleet-default-output', | ||
| }), | ||
| headers: { | ||
| Authorization: auth, | ||
| 'Content-Type': 'application/json', | ||
| 'kbn-xsrf': 'kibana', | ||
| 'x-elastic-product-origin': 'fleet', | ||
| }, | ||
| }); | ||
| const data = await res.json(); | ||
| return data; | ||
| } | ||
|
|
||
| /** | ||
| * Script to create large number of agent documents at once. | ||
| * This is helpful for testing agent bulk actions locally as the kibana async logic kicks in for >10k agents. | ||
| */ | ||
| export async function run() { | ||
| const logger = new ToolingLog({ | ||
| level: 'info', | ||
| writeTo: process.stdout, | ||
| }); | ||
|
|
||
| logger.info('Creating agent policy'); | ||
|
|
||
| const agentPolicyId = uuid(); | ||
| const agentPolicy = await createAgentPolicy(agentPolicyId); | ||
| logger.info(`Created agent policy ${agentPolicy.item.id}`); | ||
|
|
||
| logger.info('Creating fleet superuser'); | ||
| const { role, user } = await createSuperUser(); | ||
| logger.info(`Created role ${ES_SUPERUSER}, created: ${role.role.created}`); | ||
| logger.info(`Created user ${ES_SUPERUSER}, created: ${user.created}`); | ||
|
|
||
| logger.info('Creating agent documents'); | ||
| const count = 50000; | ||
| const agents = await createAgentDocsBulk(agentPolicyId, count); | ||
| logger.info( | ||
| `Created ${agents.items.length} agent docs, took ${agents.took}, errors: ${agents.errors}` | ||
|
Contributor
Author
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. |
||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| require('../../../../../src/setup_node_env'); | ||
| require('./create_agents').run(); | ||
|
|
||
| /* | ||
| Usage: | ||
|
|
||
| cd x-pack/plugins/fleet | ||
| node scripts/create_agents/index.js | ||
|
|
||
| */ |

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.
Added the
totalcount that represents how many agents were actioned (clicked by user), this helps with status reporting in case something went wrong while creating the action documents in batches.