Skip to content

Commit 08eae71

Browse files
committed
Add prettier eslint config
1 parent 64da5aa commit 08eae71

20 files changed

+1024
-619
lines changed

eslint.config.mjs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import tseslint from '@typescript-eslint/eslint-plugin';
22
import tsparser from '@typescript-eslint/parser';
3+
import eslintPluginPrettierRecommended from 'eslint-plugin-prettier/recommended';
4+
35
import js from '@eslint/js';
46
import globals from 'globals';
57
export default [
@@ -12,13 +14,14 @@ export default [
1214
},
1315
plugins: {
1416
'@typescript-eslint': tseslint,
17+
...eslintPluginPrettierRecommended.plugins,
1518
},
1619
rules: {
1720
...js.configs.recommended.rules,
1821
...tseslint.configs['eslint-recommended'].rules,
1922
...tseslint.configs.recommended.rules,
20-
indent: ['error', 2],
21-
quotes: ['error', 'single'],
23+
...eslintPluginPrettierRecommended.rules,
24+
'prettier/prettier': ['error', { singleQuote: true }]
2225
},
2326
files: ['src/**/*.ts'],
2427
},

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"devDependencies": {
4141
"@types/cli-progress": "3.11.6",
4242
"@types/inquirer": "9.0.7",
43+
"@types/lodash-es": "^4.17.12",
4344
"@types/uuid": "10.0.0",
4445
"@typescript-eslint/eslint-plugin": "8.23.0",
4546
"@typescript-eslint/parser": "8.23.0",
@@ -48,6 +49,9 @@
4849
"eslint": "9.20.0",
4950
"eslint-config-airbnb": "19.0.4",
5051
"eslint-config-airbnb-typescript": "18.0.0",
52+
"eslint-config-prettier": "^10.0.1",
53+
"eslint-plugin-prettier": "^5.2.3",
54+
"prettier": "3.5.0",
5155
"typescript": "5.7.3"
5256
}
5357
}

src/commands/asset_criticality.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
import { faker } from '@faker-js/faker';
22
import { generateNewSeed } from '../constants';
3-
import { assignAssetCriticalityToEntities, createRandomHost, createRandomUser } from './entity-store';
4-
3+
import {
4+
assignAssetCriticalityToEntities,
5+
createRandomHost,
6+
createRandomUser,
7+
} from './entity-store';
58

69
/**
710
* Generate asset criticality
811
*/
9-
export const generateAssetCriticality = async ({ users, hosts, seed = generateNewSeed() }: { users: number; hosts: number; seed?: number}) => {
12+
export const generateAssetCriticality = async ({
13+
users,
14+
hosts,
15+
seed = generateNewSeed(),
16+
}: {
17+
users: number;
18+
hosts: number;
19+
seed?: number;
20+
}) => {
1021
faker.seed(seed);
1122

1223
try {
@@ -17,12 +28,12 @@ export const generateAssetCriticality = async ({ users, hosts, seed = generateNe
1728
const generatedHosts = faker.helpers.multiple(createRandomHost, {
1829
count: hosts,
1930
});
20-
31+
2132
await assignAssetCriticalityToEntities(generatedUsers, 'user.name');
2233
console.log(`Assigned asset criticality to ${generatedUsers.length} users`);
2334
await assignAssetCriticalityToEntities(generatedHosts, 'host.name');
2435
console.log(`Assigned asset criticality to ${generatedHosts.length} hosts`);
25-
36+
2637
console.log('Finished generating asset criticality');
2738
} catch (error) {
2839
console.log('Error: ', error);

src/commands/documents.ts

Lines changed: 93 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
1-
21
import createAlerts, { BaseCreateAlertsReturnType } from '../createAlerts';
32
import createEvents from '../createEvents';
43
import eventMappings from '../mappings/eventMappings.json' assert { type: 'json' };
54
import { getEsClient, indexCheck } from './utils/index';
65
import { 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';
810
import pMap from 'p-map';
911
import { chunk } from 'lodash-es';
1012
import cliProgress from 'cli-progress';
1113
import { faker } from '@faker-js/faker';
1214
import { getAlertIndex } from '../utils';
1315

1416
const 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

5363
interface 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?
141181
export 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

241293
export 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

Comments
 (0)