1-
21import createAlerts , { BaseCreateAlertsReturnType } from '../createAlerts' ;
32import createEvents from '../createEvents' ;
43import eventMappings from '../mappings/eventMappings.json' assert { type : 'json' } ;
54import { getEsClient , indexCheck } from './utils/index' ;
65import { getConfig } from '../get_config' ;
7- import { MappingTypeMapping , BulkOperationContainer } from '@elastic/elasticsearch/lib/api/types' ;
6+ import {
7+ MappingTypeMapping ,
8+ BulkOperationContainer ,
9+ } from '@elastic/elasticsearch/lib/api/types' ;
810import pMap from 'p-map' ;
911import { chunk } from 'lodash-es' ;
1012import cliProgress from 'cli-progress' ;
1113import { faker } from '@faker-js/faker' ;
1214import { getAlertIndex } from '../utils' ;
1315
1416const config = getConfig ( ) ;
15- const client = getEsClient ( ) ;
16-
17- const generateDocs = async ( { createDocs, amount, index } : { createDocs : DocumentCreator ; amount : number ; index : string } ) => {
17+ const client = getEsClient ( ) ;
18+
19+ const generateDocs = async ( {
20+ createDocs,
21+ amount,
22+ index,
23+ } : {
24+ createDocs : DocumentCreator ;
25+ amount : number ;
26+ index : string ;
27+ } ) => {
1828 if ( ! client ) {
1929 throw new Error ( 'failed to create ES client' ) ;
2030 }
@@ -26,7 +36,7 @@ const generateDocs = async ({ createDocs, amount, index }: {createDocs: Document
2636 Math . min ( limit , amount ) ,
2737 generated ,
2838 createDocs ,
29- index
39+ index ,
3040 ) ;
3141 try {
3242 const result = await bulkUpsert ( docs ) ;
@@ -51,18 +61,25 @@ const bulkUpsert = async (docs: unknown[]) => {
5161} ;
5262
5363interface DocumentCreator {
54- ( descriptor : { id_field : string , id_value : string } ) : object ;
64+ ( descriptor : { id_field : string ; id_value : string } ) : object ;
5565}
5666
57- const alertToBatchOps = ( alert : BaseCreateAlertsReturnType , index : string ) : unknown [ ] => {
67+ const alertToBatchOps = (
68+ alert : BaseCreateAlertsReturnType ,
69+ index : string ,
70+ ) : unknown [ ] => {
5871 return [
5972 { index : { _index : index , _id : alert [ 'kibana.alert.uuid' ] } } ,
6073 { ...alert } ,
6174 ] ;
75+ } ;
6276
63- }
64-
65- const createDocuments = ( n : number , generated : number , createDoc : DocumentCreator , index : string ) : unknown [ ] => {
77+ const createDocuments = (
78+ n : number ,
79+ generated : number ,
80+ createDoc : DocumentCreator ,
81+ index : string ,
82+ ) : unknown [ ] => {
6683 return Array ( n )
6784 . fill ( null )
6885 . reduce ( ( acc , _ , i ) => {
@@ -82,9 +99,12 @@ const createDocuments = (n: number, generated: number, createDoc: DocumentCreato
8299 } , [ ] ) ;
83100} ;
84101
85-
86- export const generateAlerts = async ( alertCount : number , hostCount : number , userCount : number , space : string ) => {
87-
102+ export const generateAlerts = async (
103+ alertCount : number ,
104+ hostCount : number ,
105+ userCount : number ,
106+ space : string ,
107+ ) => {
88108 if ( userCount > alertCount ) {
89109 console . log ( 'User count should be less than alert count' ) ;
90110 process . exit ( 1 ) ;
@@ -95,33 +115,53 @@ export const generateAlerts = async (alertCount: number, hostCount: number, user
95115 process . exit ( 1 ) ;
96116 }
97117
98- console . log ( `Generating ${ alertCount } alerts containing ${ hostCount } hosts and ${ userCount } users in space ${ space } ` ) ;
118+ console . log (
119+ `Generating ${ alertCount } alerts containing ${ hostCount } hosts and ${ userCount } users in space ${ space } ` ,
120+ ) ;
99121 const concurrency = 10 ; // how many batches to send in parallel
100122 const batchSize = 2500 ; // number of alerts in a batch
101123 const no_overrides = { } ;
102124
103- const batchOpForIndex = ( { userName, hostName } : { userName : string , hostName : string } ) => alertToBatchOps ( createAlerts ( no_overrides , { userName, hostName, space } ) , getAlertIndex ( space ) ) ;
104-
125+ const batchOpForIndex = ( {
126+ userName,
127+ hostName,
128+ } : {
129+ userName : string ;
130+ hostName : string ;
131+ } ) =>
132+ alertToBatchOps (
133+ createAlerts ( no_overrides , { userName, hostName, space } ) ,
134+ getAlertIndex ( space ) ,
135+ ) ;
105136
106137 console . log ( 'Generating entity names...' ) ;
107- const userNames = Array . from ( { length : userCount } , ( ) => faker . internet . userName ( ) ) ;
108- const hostNames = Array . from ( { length : hostCount } , ( ) => faker . internet . domainName ( ) ) ;
138+ const userNames = Array . from ( { length : userCount } , ( ) =>
139+ faker . internet . userName ( ) ,
140+ ) ;
141+ const hostNames = Array . from ( { length : hostCount } , ( ) =>
142+ faker . internet . domainName ( ) ,
143+ ) ;
109144
110- console . log ( 'Assigning entity names...' )
145+ console . log ( 'Assigning entity names...' ) ;
111146 const alertEntityNames = Array . from ( { length : alertCount } , ( _ , i ) => ( {
112147 userName : userNames [ i % userCount ] ,
113148 hostName : hostNames [ i % hostCount ] ,
114149 } ) ) ;
115-
150+
116151 console . log ( 'Entity names assigned. Batching...' ) ;
117- const operationBatches = chunk ( alertEntityNames , batchSize ) . map ( ( batch ) =>
118- batch . flatMap ( batchOpForIndex )
152+ const operationBatches = chunk ( alertEntityNames , batchSize ) . map ( ( batch ) =>
153+ batch . flatMap ( batchOpForIndex ) ,
119154 ) ;
120155
121156 console . log ( 'Batching complete. Sending to ES...' ) ;
122157
123- console . log ( `Sending in ${ operationBatches . length } batches of ${ batchSize } alerts, with up to ${ concurrency } batches in parallel\n\n` ) ;
124- const progress = new cliProgress . SingleBar ( { } , cliProgress . Presets . shades_classic ) ;
158+ console . log (
159+ `Sending in ${ operationBatches . length } batches of ${ batchSize } alerts, with up to ${ concurrency } batches in parallel\n\n` ,
160+ ) ;
161+ const progress = new cliProgress . SingleBar (
162+ { } ,
163+ cliProgress . Presets . shades_classic ,
164+ ) ;
125165
126166 progress . start ( operationBatches . length , 0 ) ;
127167
@@ -130,16 +170,18 @@ export const generateAlerts = async (alertCount: number, hostCount: number, user
130170 async ( operations ) => {
131171 await bulkUpsert ( operations ) ;
132172 progress . increment ( ) ;
133- } ,
134- { concurrency }
173+ } ,
174+ { concurrency } ,
135175 ) ;
136176
137177 progress . stop ( ) ;
138178} ;
139179
140- // this creates asset criticality not events?
180+ // this creates asset criticality not events?
141181export const generateEvents = async ( n : number ) => {
142- if ( ! config . eventIndex ) { throw new Error ( 'eventIndex not defined in config' ) ; }
182+ if ( ! config . eventIndex ) {
183+ throw new Error ( 'eventIndex not defined in config' ) ;
184+ }
143185 await indexCheck ( config . eventIndex , eventMappings as MappingTypeMapping ) ;
144186
145187 console . log ( 'Generating events...' ) ;
@@ -157,15 +199,17 @@ export const generateGraph = async ({ users = 100, maxHosts = 3 }) => {
157199 //await alertIndexCheck(); TODO
158200 console . log ( 'Generating alerts graph...' ) ;
159201
160- type AlertOverride = { host : { name : string } ; user : { name : string } } ;
202+ type AlertOverride = { host : { name : string } ; user : { name : string } } ;
161203
162- const clusters : ( ReturnType < typeof createAlerts > & AlertOverride ) [ ] [ ] = [ ] ;
204+ const clusters : ( ReturnType < typeof createAlerts > & AlertOverride ) [ ] [ ] = [ ] ;
163205
164206 /**
165207 * The type you can pass to the bulk API, if you're working with Fake Alerts.
166208 * This accepts partial docs, full docs, and other docs that indicate _index, _id, and such
167209 */
168- type FakeAlertBulkOperations = BulkOperationContainer | Partial < AlertOverride > ;
210+ type FakeAlertBulkOperations =
211+ | BulkOperationContainer
212+ | Partial < AlertOverride > ;
169213
170214 const alerts : FakeAlertBulkOperations [ ] = [ ] ;
171215 for ( let i = 0 ; i < users ; i ++ ) {
@@ -184,7 +228,9 @@ export const generateGraph = async ({ users = 100, maxHosts = 3 }) => {
184228 clusters . push ( userCluster ) ;
185229 }
186230
187- let lastAlertFromCluster : ( ReturnType < typeof createAlerts > & AlertOverride ) | null = null ;
231+ let lastAlertFromCluster :
232+ | ( ReturnType < typeof createAlerts > & AlertOverride )
233+ | null = null ;
188234 clusters . forEach ( ( cluster ) => {
189235 if ( lastAlertFromCluster ) {
190236 const alert = createAlerts ( {
@@ -196,21 +242,27 @@ export const generateGraph = async ({ users = 100, maxHosts = 3 }) => {
196242 } ,
197243 } ) ;
198244 alerts . push ( {
199- index : { _index : getAlertIndex ( 'default' ) , _id : alert [ 'kibana.alert.uuid' ] } ,
245+ index : {
246+ _index : getAlertIndex ( 'default' ) ,
247+ _id : alert [ 'kibana.alert.uuid' ] ,
248+ } ,
200249 } ) ;
201250 alerts . push ( alert ) ;
202251 }
203252 cluster . forEach ( ( alert ) => {
204253 alerts . push ( {
205- index : { _index : getAlertIndex ( 'default' ) , _id : alert [ 'kibana.alert.uuid' ] } ,
254+ index : {
255+ _index : getAlertIndex ( 'default' ) ,
256+ _id : alert [ 'kibana.alert.uuid' ] ,
257+ } ,
206258 } ) ;
207259 alerts . push ( alert ) ;
208260 lastAlertFromCluster = alert ;
209261 } ) ;
210262 } ) ;
211263
212264 try {
213- if ( ! client ) throw new Error ;
265+ if ( ! client ) throw new Error ( ) ;
214266 const result = await client . bulk ( { body : alerts , refresh : true } ) ;
215267 console . log ( `${ result . items . length } alerts created` ) ;
216268 } catch ( err ) {
@@ -222,7 +274,7 @@ export const deleteAllAlerts = async () => {
222274 console . log ( 'Deleting all alerts...' ) ;
223275 try {
224276 console . log ( 'Deleted all alerts' ) ;
225- if ( ! client ) throw new Error ;
277+ if ( ! client ) throw new Error ( ) ;
226278 await client . deleteByQuery ( {
227279 index : '.alerts-security.alerts-*' ,
228280 refresh : true ,
@@ -240,10 +292,12 @@ export const deleteAllAlerts = async () => {
240292
241293export const deleteAllEvents = async ( ) => {
242294 console . log ( 'Deleting all events...' ) ;
243- if ( ! config . eventIndex ) { throw new Error ( 'eventIndex not defined in config' ) ; }
295+ if ( ! config . eventIndex ) {
296+ throw new Error ( 'eventIndex not defined in config' ) ;
297+ }
244298 try {
245299 console . log ( 'Deleted all events' ) ;
246- if ( ! client ) throw new Error ;
300+ if ( ! client ) throw new Error ( ) ;
247301 await client . deleteByQuery ( {
248302 index : config . eventIndex ,
249303 refresh : true ,
0 commit comments