diff --git a/commands/entity-store.mjs b/commands/entity-store.mjs index 418b020..4f238ad 100644 --- a/commands/entity-store.mjs +++ b/commands/entity-store.mjs @@ -9,7 +9,7 @@ import { ENTITY_STORE_OPTIONS, generateNewSeed } from "../constants.mjs"; let client = getEsClient(); let EVENT_INDEX_NAME = "auditbeat-8.12.0-2024.01.18-000001"; -const offset = () => Math.random() * 1000; +const offset = () => faker.number.int({ max: 1000 }) const ASSET_CRITICALITY = [ "very_important", @@ -33,6 +33,8 @@ export const createRandomHost = () => { }; }; + + export const createFactoryRandomEventForHost = (name) => () => { return { "@timestamp": moment().subtract(offset(), "h").format("yyyy-MM-DDTHH:mm:ss.SSSSSSZ"), @@ -138,11 +140,14 @@ export const generateEntityStore = async ({ users = 10, hosts = 10, seed = gener createFactoryRandomEventForHost ); - await ingestEvents(eventsForUsers); + const relational = matchUsersAndHosts(eventsForUsers, eventsForHosts) + + await ingestEvents(relational.users); console.log("Users events ingested"); - await ingestEvents(eventsForHosts); + await ingestEvents(relational.hosts); console.log("Hosts events ingested"); + if (options.includes(ENTITY_STORE_OPTIONS.criticality)) { await assignAssetCriticalityToEntities(generatedUsers, "user.name"); console.log("Assigned asset criticality to users"); @@ -197,3 +202,26 @@ export const cleanEntityStore = async () => { console.log(error); } }; + + +const matchUsersAndHosts = (users, hosts) => { + const splitIndex = faker.number.int({ max: users.length - 1 }); + + return { + users: users + .slice(0, splitIndex) + .map(user => { + const index = faker.number.int({ max: hosts.length - 1 }); + return { ...user, host: hosts[index].host } + }) + .concat(users.slice(splitIndex)), + + hosts: hosts. + slice(0, splitIndex) + .map(host => { + const index = faker.number.int({ max: users.length - 1 }); + return { ...host, user: users[index].user } + }) + .concat(hosts.slice(splitIndex)) + }; +}