From 38ebaac5276fe522b4940c7c5d0cdd9aa122155e Mon Sep 17 00:00:00 2001 From: Tiago Vila Verde Date: Mon, 5 Feb 2024 13:57:51 +0100 Subject: [PATCH 1/2] matching users to hosts --- commands/entity-store.mjs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/commands/entity-store.mjs b/commands/entity-store.mjs index 418b020..4343ae8 100644 --- a/commands/entity-store.mjs +++ b/commands/entity-store.mjs @@ -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,15 @@ export const generateEntityStore = async ({ users = 10, hosts = 10, seed = gener createFactoryRandomEventForHost ); - await ingestEvents(eventsForUsers); + const relational = matchUsersAndHosts(eventsForUsers, eventsForHosts) + console.log("Matched users and hosts", JSON.stringify(relational)) + + 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 +203,18 @@ export const cleanEntityStore = async () => { console.log(error); } }; + + +const matchUsersAndHosts = (users, hosts) => { + + let half = Math.floor(users.length / 2); + console.log("Half", half) + + const [_users, remainingUsers] = chunk(users, half); + const [_hosts, remainingHosts] = chunk(hosts, half); + + return { + users: _users.map((user, index) => ({ ...user, host: hosts[index].host })).concat(remainingUsers), + hosts: _hosts.map((host, index) => ({ ...host, user: users[index].user })).concat(remainingHosts) + }; +} From 5e12de9beca293081c5f71fff1f4c365dc29c91e Mon Sep 17 00:00:00 2001 From: Tiago Vila Verde Date: Tue, 6 Feb 2024 10:37:25 +0100 Subject: [PATCH 2/2] randomness --- commands/entity-store.mjs | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/commands/entity-store.mjs b/commands/entity-store.mjs index 4343ae8..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", @@ -141,7 +141,6 @@ export const generateEntityStore = async ({ users = 10, hosts = 10, seed = gener ); const relational = matchUsersAndHosts(eventsForUsers, eventsForHosts) - console.log("Matched users and hosts", JSON.stringify(relational)) await ingestEvents(relational.users); console.log("Users events ingested"); @@ -206,15 +205,23 @@ export const cleanEntityStore = async () => { const matchUsersAndHosts = (users, hosts) => { - - let half = Math.floor(users.length / 2); - console.log("Half", half) - - const [_users, remainingUsers] = chunk(users, half); - const [_hosts, remainingHosts] = chunk(hosts, half); + const splitIndex = faker.number.int({ max: users.length - 1 }); return { - users: _users.map((user, index) => ({ ...user, host: hosts[index].host })).concat(remainingUsers), - hosts: _hosts.map((host, index) => ({ ...host, user: users[index].user })).concat(remainingHosts) + 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)) }; }