Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Security Solution] Refactor resolver children _source #77343

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Finishing type conversions for backend
  • Loading branch information
jonathan-buttner committed Sep 8, 2020
commit 304335aa85260e15665d91099435138e895914f1
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@
import { Client } from '@elastic/elasticsearch';
import seedrandom from 'seedrandom';
import { EndpointDocGenerator, TreeOptions, Event } from './generate_data';
import { firstNonNullValue } from './models/ecs_safety_helpers';

export async function indexHostsAndAlerts(
client: Client,
@@ -86,7 +87,7 @@ async function indexAlerts(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(array: Array<Record<string, any>>, doc) => {
let index = eventIndex;
if (doc.event?.kind === 'alert') {
if (firstNonNullValue(doc.event?.kind) === 'alert') {
index = alertIndex;
}
array.push({ create: { _index: index } }, doc);
Original file line number Diff line number Diff line change
@@ -67,11 +67,7 @@ export function timestampAsDateSafeVersion(event: SafeResolverEvent): Date | und
}

export function eventTimestamp(event: ResolverEvent): string | undefined | number {
if (isLegacyEvent(event)) {
return event.endgame.timestamp_utc;
} else {
return event['@timestamp'];
}
return event['@timestamp'];
}

export function eventName(event: ResolverEvent): string {
Original file line number Diff line number Diff line change
@@ -7,7 +7,6 @@
import {
SafeResolverAncestry,
SafeResolverTree,
ResolverChildNode,
ResolverRelatedAlerts,
SafeResolverChildren,
SafeResolverLifecycleNode,
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ import {
SafeResolverEvent,
SafeResolverRelatedEvents,
} from '../../../../../common/endpoint/types';
import { entityId, entityIDSafeVersion } from '../../../../../common/endpoint/models/event';
import { entityIDSafeVersion } from '../../../../../common/endpoint/models/event';

describe('Tree', () => {
const generator = new EndpointDocGenerator();
Original file line number Diff line number Diff line change
@@ -5,21 +5,25 @@
*/
import expect from '@kbn/expect';
import { SearchResponse } from 'elasticsearch';
import { entityId } from '../../../../plugins/security_solution/common/endpoint/models/event';
import {
entityIDSafeVersion,
timestampSafeVersion,
} from '../../../../plugins/security_solution/common/endpoint/models/event';
import { eventsIndexPattern } from '../../../../plugins/security_solution/common/endpoint/constants';
import { ChildrenPaginationBuilder } from '../../../../plugins/security_solution/server/endpoint/routes/resolver/utils/children_pagination';
import { ChildrenQuery } from '../../../../plugins/security_solution/server/endpoint/routes/resolver/queries/children';
import {
ResolverTree,
ResolverEvent,
ResolverChildren,
SafeResolverTree,
SafeResolverEvent,
SafeResolverChildren,
} from '../../../../plugins/security_solution/common/endpoint/types';
import { FtrProviderContext } from '../../ftr_provider_context';
import {
Event,
EndpointDocGenerator,
} from '../../../../plugins/security_solution/common/endpoint/generate_data';
import { InsertedEvents } from '../../services/resolver';
import { createAncestryArray } from './common';

export default function resolverAPIIntegrationTests({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
@@ -40,20 +44,20 @@ export default function resolverAPIIntegrationTests({ getService }: FtrProviderC
// Origin -> infoEvent -> startEvent -> execEvent
origin = generator.generateEvent();
infoEvent = generator.generateEvent({
parentEntityID: origin.process.entity_id,
ancestry: [origin.process.entity_id],
parentEntityID: entityIDSafeVersion(origin),
ancestry: createAncestryArray([origin]),
eventType: ['info'],
});

startEvent = generator.generateEvent({
parentEntityID: infoEvent.process.entity_id,
ancestry: [infoEvent.process.entity_id, origin.process.entity_id],
parentEntityID: entityIDSafeVersion(infoEvent),
ancestry: createAncestryArray([infoEvent, origin]),
eventType: ['start'],
});

execEvent = generator.generateEvent({
parentEntityID: startEvent.process.entity_id,
ancestry: [startEvent.process.entity_id, infoEvent.process.entity_id],
parentEntityID: entityIDSafeVersion(startEvent),
ancestry: createAncestryArray([startEvent, infoEvent]),
eventType: ['change'],
});
genData = await resolver.insertEvents([origin, infoEvent, startEvent, execEvent]);
@@ -64,13 +68,13 @@ export default function resolverAPIIntegrationTests({ getService }: FtrProviderC
});

it('finds all the children of the origin', async () => {
const { body }: { body: ResolverTree } = await supertest
.get(`/api/endpoint/resolver/${origin.process.entity_id}?children=100`)
const { body }: { body: SafeResolverTree } = await supertest
.get(`/api/endpoint/resolver/${origin.process?.entity_id}?children=100`)
.expect(200);
expect(body.children.childNodes.length).to.be(3);
expect(body.children.childNodes[0].entityID).to.be(infoEvent.process.entity_id);
expect(body.children.childNodes[1].entityID).to.be(startEvent.process.entity_id);
expect(body.children.childNodes[2].entityID).to.be(execEvent.process.entity_id);
expect(body.children.childNodes[0].entityID).to.be(infoEvent.process?.entity_id);
expect(body.children.childNodes[1].entityID).to.be(startEvent.process?.entity_id);
expect(body.children.childNodes[2].entityID).to.be(execEvent.process?.entity_id);
});
});

@@ -86,23 +90,23 @@ export default function resolverAPIIntegrationTests({ getService }: FtrProviderC
// Origin -> (infoEvent, startEvent, execEvent are all for the same node)
origin = generator.generateEvent();
startEvent = generator.generateEvent({
parentEntityID: origin.process.entity_id,
ancestry: [origin.process.entity_id],
parentEntityID: entityIDSafeVersion(origin),
ancestry: createAncestryArray([origin]),
eventType: ['start'],
});

infoEvent = generator.generateEvent({
parentEntityID: origin.process.entity_id,
ancestry: [origin.process.entity_id],
entityID: startEvent.process.entity_id,
parentEntityID: entityIDSafeVersion(origin),
ancestry: createAncestryArray([origin]),
entityID: entityIDSafeVersion(startEvent),
eventType: ['info'],
});

execEvent = generator.generateEvent({
parentEntityID: origin.process.entity_id,
ancestry: [origin.process.entity_id],
parentEntityID: entityIDSafeVersion(origin),
ancestry: createAncestryArray([origin]),
eventType: ['change'],
entityID: startEvent.process.entity_id,
entityID: entityIDSafeVersion(startEvent),
});
genData = await resolver.insertEvents([origin, infoEvent, startEvent, execEvent]);
});
@@ -117,12 +121,12 @@ export default function resolverAPIIntegrationTests({ getService }: FtrProviderC
eventsIndexPattern
);
// [1] here gets the body portion of the array
const [, query] = childrenQuery.buildMSearch(origin.process.entity_id);
const { body } = await es.search<SearchResponse<ResolverEvent>>({ body: query });
const [, query] = childrenQuery.buildMSearch(entityIDSafeVersion(origin) ?? '');
const { body } = await es.search<SearchResponse<SafeResolverEvent>>({ body: query });
expect(body.hits.hits.length).to.be(1);

const event = body.hits.hits[0]._source;
expect(entityId(event)).to.be(startEvent.process.entity_id);
expect(entityIDSafeVersion(event)).to.be(startEvent.process?.entity_id);
expect(event.event?.type).to.eql(['start']);
});
});
@@ -139,25 +143,25 @@ export default function resolverAPIIntegrationTests({ getService }: FtrProviderC
// Origin -> (infoEvent, startEvent, execEvent are all for the same node)
origin = generator.generateEvent();
startEvent = generator.generateEvent({
parentEntityID: origin.process.entity_id,
ancestry: [origin.process.entity_id],
parentEntityID: entityIDSafeVersion(origin),
ancestry: createAncestryArray([origin]),
eventType: ['start'],
});

infoEvent = generator.generateEvent({
timestamp: startEvent['@timestamp'] + 100,
parentEntityID: origin.process.entity_id,
ancestry: [origin.process.entity_id],
entityID: startEvent.process.entity_id,
timestamp: (timestampSafeVersion(startEvent) ?? 0) + 100,
parentEntityID: entityIDSafeVersion(origin),
ancestry: createAncestryArray([origin]),
entityID: entityIDSafeVersion(startEvent),
eventType: ['info'],
});

execEvent = generator.generateEvent({
timestamp: infoEvent['@timestamp'] + 100,
parentEntityID: origin.process.entity_id,
ancestry: [origin.process.entity_id],
timestamp: (timestampSafeVersion(infoEvent) ?? 0) + 100,
parentEntityID: entityIDSafeVersion(origin),
ancestry: createAncestryArray([origin]),
eventType: ['change'],
entityID: startEvent.process.entity_id,
entityID: entityIDSafeVersion(startEvent),
});
genData = await resolver.insertEvents([origin, infoEvent, startEvent, execEvent]);
});
@@ -167,37 +171,37 @@ export default function resolverAPIIntegrationTests({ getService }: FtrProviderC
});

it('retrieves the same node three times', async () => {
let { body }: { body: ResolverChildren } = await supertest
.get(`/api/endpoint/resolver/${origin.process.entity_id}/children?children=1`)
let { body }: { body: SafeResolverChildren } = await supertest
.get(`/api/endpoint/resolver/${origin.process?.entity_id}/children?children=1`)
.expect(200);
expect(body.childNodes.length).to.be(1);
expect(body.nextChild).to.not.be(null);
expect(body.childNodes[0].entityID).to.be(startEvent.process.entity_id);
expect(body.childNodes[0].lifecycle[0].event?.type).to.eql(startEvent.event.type);
expect(body.childNodes[0].entityID).to.be(startEvent.process?.entity_id);
expect(body.childNodes[0].lifecycle[0].event?.type).to.eql(startEvent.event?.type);

({ body } = await supertest
.get(
`/api/endpoint/resolver/${origin.process.entity_id}/children?children=1&afterChild=${body.nextChild}`
`/api/endpoint/resolver/${origin.process?.entity_id}/children?children=1&afterChild=${body.nextChild}`
)
.expect(200));
expect(body.childNodes.length).to.be(1);
expect(body.nextChild).to.not.be(null);
expect(body.childNodes[0].entityID).to.be(infoEvent.process.entity_id);
expect(body.childNodes[0].lifecycle[1].event?.type).to.eql(infoEvent.event.type);
expect(body.childNodes[0].entityID).to.be(infoEvent.process?.entity_id);
expect(body.childNodes[0].lifecycle[1].event?.type).to.eql(infoEvent.event?.type);

({ body } = await supertest
.get(
`/api/endpoint/resolver/${origin.process.entity_id}/children?children=1&afterChild=${body.nextChild}`
`/api/endpoint/resolver/${origin.process?.entity_id}/children?children=1&afterChild=${body.nextChild}`
)
.expect(200));
expect(body.childNodes.length).to.be(1);
expect(body.nextChild).to.not.be(null);
expect(body.childNodes[0].entityID).to.be(infoEvent.process.entity_id);
expect(body.childNodes[0].lifecycle[2].event?.type).to.eql(execEvent.event.type);
expect(body.childNodes[0].entityID).to.be(infoEvent.process?.entity_id);
expect(body.childNodes[0].lifecycle[2].event?.type).to.eql(execEvent.event?.type);

({ body } = await supertest
.get(
`/api/endpoint/resolver/${origin.process.entity_id}/children?children=1&afterChild=${body.nextChild}`
`/api/endpoint/resolver/${origin.process?.entity_id}/children?children=1&afterChild=${body.nextChild}`
)
.expect(200));
expect(body.childNodes.length).to.be(0);
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@ import {
} from '../../../../plugins/security_solution/common/endpoint/types';
import {
parentEntityIDSafeVersion,
entityIDSafeVersion,
eventIDSafeVersion,
} from '../../../../plugins/security_solution/common/endpoint/models/event';
import {
@@ -23,6 +24,23 @@ import {
categoryMapping,
} from '../../../../plugins/security_solution/common/endpoint/generate_data';

/**
* Creates the ancestry array based on an array of events. The order of the ancestry array will match the order
* of the events passed in.
*
* @param events an array of generated events
*/
export const createAncestryArray = (events: Event[]) => {
const ancestry: string[] = [];
for (const event of events) {
const entityID = entityIDSafeVersion(event);
if (entityID) {
ancestry.push(entityID);
}
}
return ancestry;
};

/**
* Check that the given lifecycle is in the resolver tree's corresponding map
*
Loading