-
Notifications
You must be signed in to change notification settings - Fork 8.1k
/
resolver_generator_script.ts
501 lines (462 loc) · 15.2 KB
/
resolver_generator_script.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/*
* 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.
*/
/* eslint-disable no-console,max-classes-per-file */
import yargs from 'yargs';
import fs from 'fs';
import { Client, errors } from '@elastic/elasticsearch';
import type { ClientOptions } from '@elastic/elasticsearch/lib/client';
import { CA_CERT_PATH } from '@kbn/dev-utils';
import { ToolingLog } from '@kbn/tooling-log';
import type { KbnClientOptions } from '@kbn/test';
import { KbnClient } from '@kbn/test';
import type { Role } from '@kbn/security-plugin/common';
import { METADATA_DATASTREAM } from '../../common/endpoint/constants';
import { EndpointMetadataGenerator } from '../../common/endpoint/data_generators/endpoint_metadata_generator';
import { indexHostsAndAlerts } from '../../common/endpoint/index_data';
import { ANCESTRY_LIMIT, EndpointDocGenerator } from '../../common/endpoint/generate_data';
import { fetchStackVersion, isServerlessKibanaFlavor } from './common/stack_services';
import { ENDPOINT_ALERTS_INDEX, ENDPOINT_EVENTS_INDEX } from './common/constants';
import { getWithResponseActionsRole } from './common/roles_users/with_response_actions_role';
import { getNoResponseActionsRole } from './common/roles_users/without_response_actions_role';
import { getT1Analyst } from './common/roles_users/t1_analyst';
import { getT2Analyst } from './common/roles_users/t2_analyst';
import { getEndpointOperationsAnalyst } from './common/roles_users/endpoint_operations_analyst';
import { getEndpointSecurityPolicyManager } from './common/roles_users/endpoint_security_policy_manager';
import { getHunter } from './common/roles_users/hunter';
import { getPlatformEngineer } from './common/roles_users/platform_engineer';
import { getSocManager } from './common/roles_users/soc_manager';
import { getThreatIntelligenceAnalyst } from './common/roles_users/threat_intelligence_analyst';
const rolesMapping: { [id: string]: Omit<Role, 'name'> } = {
t1Analyst: getT1Analyst(),
t2Analyst: getT2Analyst(),
hunter: getHunter(),
threatIntelligenceAnalyst: getThreatIntelligenceAnalyst(),
socManager: getSocManager(),
platformEngineer: getPlatformEngineer(),
endpointOperationsAnalyst: getEndpointOperationsAnalyst(),
endpointSecurityPolicyManager: getEndpointSecurityPolicyManager(),
withResponseActionsRole: getWithResponseActionsRole(),
noResponseActionsRole: getNoResponseActionsRole(),
};
main();
function handleErr(err: unknown) {
if (err instanceof errors.ResponseError && err.statusCode !== 404) {
console.log(JSON.stringify(err, null, 2));
// eslint-disable-next-line no-process-exit
process.exit(1);
}
}
async function deleteIndices(indices: string[], client: Client) {
for (const index of indices) {
try {
// The index could be a data stream so let's try deleting that first
// The ES client in Kibana doesn't support data streams yet so we need to make a raw request to the ES route
await client.transport.request({ method: 'DELETE', path: `_data_stream/${index}` });
} catch (err) {
handleErr(err);
}
}
}
async function addRole(kbnClient: KbnClient, role: Role): Promise<string | undefined> {
if (!role) {
console.log('No role data given');
return;
}
const { name, ...permissions } = role;
const path = `/api/security/role/${name}?createOnly=true`;
// add role if doesn't exist already
try {
console.log(`Adding ${name} role`);
await kbnClient.request({
method: 'PUT',
path,
body: permissions,
});
return name;
} catch (error) {
console.log(error);
handleErr(error);
}
}
interface UserInfo {
username: string;
password: string;
full_name?: string;
roles?: string[];
}
async function addUser(esClient: Client, user?: UserInfo): Promise<UserInfo | undefined> {
if (!user) {
return;
}
const superuserRole = ['superuser', 'kibana_system'];
const path = `_security/user/${user.username}`;
// add user if doesn't exist already
try {
console.log(`Adding ${user.username}...`);
const addedUser = await esClient.transport.request<Promise<{ created: boolean }>>({
method: 'POST',
path,
body: {
password: user.password,
roles: user.roles ?? superuserRole,
full_name: user.full_name ?? user.username,
username: user.username,
},
});
if (addedUser.created) {
console.log(`User ${user.username} added successfully!`);
} else {
console.log(`User ${user.username} already exists!`);
}
return {
username: user.username,
password: user.password,
};
} catch (error) {
handleErr(error);
}
}
async function deleteUser(esClient: Client, username: string): Promise<{ found: boolean }> {
return esClient.transport.request({
method: 'DELETE',
path: `_security/user/${username}`,
});
}
function updateURL({
url,
user,
protocol,
}: {
url: string;
user?: { username: string; password: string };
protocol?: string;
}): string {
const urlObject = new URL(url);
if (user) {
urlObject.username = user.username;
urlObject.password = user.password;
}
if (protocol) {
urlObject.protocol = protocol;
}
return urlObject.href;
}
async function main() {
const startTime = new Date().getTime();
const argv = yargs.help().options({
seed: {
alias: 's',
describe: 'random seed to use for document generator',
type: 'string',
},
node: {
alias: 'n',
describe: 'elasticsearch node url',
default: 'http://elastic:changeme@127.0.0.1:9200',
type: 'string',
},
kibana: {
alias: 'k',
describe: 'kibana url',
default: 'http://elastic:changeme@127.0.0.1:5601',
type: 'string',
},
eventIndex: {
alias: 'ei',
describe: 'index to store events in',
default: ENDPOINT_EVENTS_INDEX,
type: 'string',
},
alertIndex: {
alias: 'ai',
describe: 'index to store alerts in',
default: ENDPOINT_ALERTS_INDEX,
type: 'string',
},
metadataIndex: {
alias: 'mi',
describe: 'index to store host metadata in',
default: METADATA_DATASTREAM,
type: 'string',
},
policyIndex: {
alias: 'pi',
describe: 'index to store host policy in',
default: 'metrics-endpoint.policy-default',
type: 'string',
},
ancestors: {
alias: 'anc',
describe: 'number of ancestors of origin to create',
type: 'number',
default: 3,
},
ancestryArraySize: {
alias: 'ancSize',
describe: 'the upper bound size of the ancestry array, 0 will mark the field as undefined',
type: 'number',
default: ANCESTRY_LIMIT,
},
generations: {
alias: 'gen',
describe: 'number of child generations to create',
type: 'number',
default: 3,
},
children: {
alias: 'ch',
describe: 'maximum number of children per node',
type: 'number',
default: 3,
},
relatedEvents: {
alias: 'related',
describe: 'number of related events to create for each process event',
type: 'number',
default: 5,
},
relatedAlerts: {
alias: 'relAlerts',
describe: 'number of related alerts to create for each process event',
type: 'number',
default: 5,
},
percentWithRelated: {
alias: 'pr',
describe: 'percent of process events to add related events and related alerts to',
type: 'number',
default: 30,
},
percentTerminated: {
alias: 'pt',
describe: 'percent of process events to add termination event for',
type: 'number',
default: 30,
},
maxChildrenPerNode: {
alias: 'maxCh',
describe:
'always generate the max number of children per node instead of it being random up to the max children',
type: 'boolean',
default: false,
},
numHosts: {
alias: 'ne',
describe: 'number of different hosts to generate alerts for',
type: 'number',
default: 1,
},
numDocs: {
alias: 'nd',
describe: 'number of metadata and policy response doc to generate per host',
type: 'number',
default: 5,
},
alertsPerHost: {
alias: 'ape',
describe: 'number of resolver trees to make for each host',
type: 'number',
default: 1,
},
delete: {
alias: 'd',
describe: 'delete indices and remake them',
type: 'boolean',
default: false,
},
fleet: {
alias: 'f',
describe: 'enroll fleet agents for hosts',
type: 'boolean',
default: false,
},
ssl: {
alias: 'ssl',
describe: 'Use https for elasticsearch and kbn clients',
type: 'boolean',
default: false,
},
withNewUser: {
alias: 'wnu',
describe:
'If the --fleet flag is enabled, using `--withNewUser=username:password` would add a new user with \
the given username, password and `superuser`, `kibana_system` roles. Adding a new user would also write \
to indices in the generator as this user with the new roles.',
type: 'string',
default: '',
},
randomVersions: {
describe:
'By default, the data generated (that contains a stack version - ex: `agent.version`) will have a ' +
'version number set to be the same as the version of the running stack. Using this flag (`--randomVersions=true`) ' +
'will result in random version being generated',
default: false,
},
rbacUser: {
alias: 'rbac',
describe:
'Creates a set of roles and users, password=changeme, with RBAC privileges enabled/disabled. Neither have the superuser role. ',
type: 'boolean',
default: false,
},
}).argv;
let ca: Buffer;
let clientOptions: ClientOptions;
let url: string;
let node: string;
const logger = new ToolingLog({
level: 'info',
writeTo: process.stdout,
});
const toolingLogOptions = { log: logger };
let kbnClientOptions: KbnClientOptions = {
...toolingLogOptions,
url: argv.kibana,
};
if (argv.ssl) {
ca = fs.readFileSync(CA_CERT_PATH);
url = updateURL({ url: argv.kibana, protocol: 'https:' });
node = updateURL({ url: argv.node, protocol: 'https:' });
kbnClientOptions = {
...kbnClientOptions,
url,
certificateAuthorities: [ca],
};
clientOptions = { node, tls: { ca: [ca] } };
} else {
clientOptions = { node: argv.node };
}
let client = new Client(clientOptions);
let kbnClient = new KbnClient({ ...kbnClientOptions });
let user: UserInfo | undefined;
const isServerless = await isServerlessKibanaFlavor(kbnClient);
logger.info(`Build flavor: ${isServerless ? 'serverless' : 'non-serverless'}`);
if (argv.fleet && !argv.withNewUser && !isServerless) {
// warn and exit when using fleet flag
logger.error(
'Please use the --withNewUser=username:password flag to add a custom user with required roles when --fleet is enabled!'
);
// eslint-disable-next-line no-process-exit
process.exit(0);
}
// if fleet flag is used
if (argv.fleet) {
if (!isServerless) {
// add endpoint user if --withNewUser flag has values as username:password
const newUserCreds =
argv.withNewUser.indexOf(':') !== -1 ? argv.withNewUser.split(':') : undefined;
user = await addUser(
client,
newUserCreds
? {
username: newUserCreds[0],
password: newUserCreds[1],
}
: undefined
);
// update client and kibana options before instantiating
if (user) {
// use endpoint user for Es and Kibana URLs
url = updateURL({ url: argv.kibana, user });
node = updateURL({ url: argv.node, user });
kbnClientOptions = {
...kbnClientOptions,
url,
};
client = new Client({ ...clientOptions, node });
kbnClient = new KbnClient({ ...kbnClientOptions });
logger.verbose(`ES/KBN clients updated to login using: ${JSON.stringify(user)}`);
}
} else {
logger.warning(
'Option `--withNewUser` not supported in serverless.\n' +
'Ensure that `--kibana` and `--node` options are defined with username/password of ' +
'`system_indices_superuser:changeme`'
);
}
}
if (argv.delete) {
await deleteIndices(
[argv.eventIndex, argv.metadataIndex, argv.policyIndex, argv.alertIndex],
client
);
}
if (argv.rbacUser) {
if (isServerless) {
// FIXME:PT create users in serverless when that capability is available
throw new Error(`Can not use '--rbacUser' option against serverless deployment`);
}
// Add roles and users with response actions kibana privileges
for (const role of Object.keys(rolesMapping)) {
const addedRole = await addRole(kbnClient, {
name: role,
...rolesMapping[role],
});
if (addedRole) {
logger.info(`Successfully added ${role} role`);
await addUser(client, { username: role, password: 'changeme', roles: [role] });
} else {
logger.warning(`Failed to add role, ${role}`);
}
}
}
const seed = argv.seed || Math.random().toString();
let DocGenerator: typeof EndpointDocGenerator = EndpointDocGenerator;
// If `--randomVersions` is NOT set, then use custom generator that ensures all data generated
// has a stack version number that matches that of the running stack
if (!argv.randomVersions) {
const stackVersion = await fetchStackVersion(kbnClient);
// Document Generator override that uses a custom Endpoint Metadata generator and sets the
// `agent.version` to the current version
DocGenerator = class extends EndpointDocGenerator {
constructor(...args: ConstructorParameters<typeof EndpointDocGenerator>) {
const MetadataGenerator = class extends EndpointMetadataGenerator {
protected randomVersion(): string {
return stackVersion;
}
};
super(args[0], MetadataGenerator);
}
};
}
logger.info('Indexing host and alerts...');
await indexHostsAndAlerts(
client,
kbnClient,
seed,
argv.numHosts,
argv.numDocs,
argv.metadataIndex,
argv.policyIndex,
argv.eventIndex,
argv.alertIndex,
argv.alertsPerHost,
argv.fleet,
{
ancestors: argv.ancestors,
generations: argv.generations,
children: argv.children,
relatedEvents: argv.relatedEvents,
relatedAlerts: argv.relatedAlerts,
percentWithRelated: argv.percentWithRelated,
percentTerminated: argv.percentTerminated,
alwaysGenMaxChildrenPerNode: argv.maxChildrenPerNode,
ancestryArraySize: argv.ancestryArraySize,
eventsDataStream: EndpointDocGenerator.createDataStreamFromIndex(argv.eventIndex),
alertsDataStream: EndpointDocGenerator.createDataStreamFromIndex(argv.alertIndex),
},
DocGenerator
);
// delete endpoint_user after
if (user && !isServerless) {
const deleted = await deleteUser(client, user.username);
if (deleted.found) {
logger.info(`User ${user.username} deleted successfully!`);
}
}
logger.info(`Creating and indexing documents took: ${new Date().getTime() - startTime}ms`);
}