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][Resolver] Allow a configurable entity_id field #81679

Merged
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
58c6ada
Trying to flesh out new tree route
jonathan-buttner Oct 21, 2020
3139b65
Working on the descendants query
jonathan-buttner Oct 22, 2020
b561c3d
Almost working descendants
jonathan-buttner Oct 22, 2020
8506512
Possible solution for aggs
jonathan-buttner Oct 22, 2020
cf548c6
Working aggregations extraction
jonathan-buttner Oct 26, 2020
da4af52
Working on the ancestry array for descendants
jonathan-buttner Oct 26, 2020
a9c87d0
Making changes to the unique id for ancestr
jonathan-buttner Oct 26, 2020
023f119
Implementing ancestry funcitonality
jonathan-buttner Oct 27, 2020
dd04e44
Deleting the multiple edges
jonathan-buttner Oct 28, 2020
109c09f
Fleshing out the descendants loop for levels
jonathan-buttner Oct 30, 2020
43423a0
Writing tests for ancestors and descendants
jonathan-buttner Nov 2, 2020
0d45d15
Fixing type errors and writing more tests
jonathan-buttner Nov 3, 2020
7e2ac9e
Renaming validation variable and deprecating old tree routes
jonathan-buttner Nov 3, 2020
c05c795
Renaming tree integration test file
jonathan-buttner Nov 3, 2020
893eb46
Merge branch 'master' of github.com:elastic/kibana into resolver-mult…
jonathan-buttner Nov 5, 2020
cf2d772
Adding some integration tests
jonathan-buttner Nov 5, 2020
1663428
Fixing ancestry to handle multiple nodes in the request and writing m…
jonathan-buttner Nov 6, 2020
dd4e0dd
Adding more tests
jonathan-buttner Nov 9, 2020
a4d3ba3
Merge branch 'master' of github.com:elastic/kibana into resolver-mult…
jonathan-buttner Nov 9, 2020
837e164
Renaming new tree to handler file
jonathan-buttner Nov 9, 2020
bf113ac
Renaming new tree directory
jonathan-buttner Nov 9, 2020
34cf33e
Adding more unit tests
jonathan-buttner Nov 9, 2020
63dadd3
Using doc value fields and working on types
jonathan-buttner Nov 9, 2020
ea5d217
Adding comments and more tests
jonathan-buttner Nov 9, 2020
549b920
Fixing timestamp test issue
jonathan-buttner Nov 10, 2020
76f8644
Adding more comments
jonathan-buttner Nov 10, 2020
b4fd18e
Merge branch 'master' of github.com:elastic/kibana into resolver-mult…
jonathan-buttner Nov 10, 2020
400331c
Fixing timestamp test issue take 2
jonathan-buttner Nov 10, 2020
e8ec0a0
Merge branch 'master' of github.com:elastic/kibana into resolver-mult…
jonathan-buttner Nov 12, 2020
619088c
Adding id, parent, and name fields to the top level response
jonathan-buttner Nov 12, 2020
0660d81
Merge branch 'master' into resolver-multi-fields
kibanamachine Nov 16, 2020
e13bf88
Merge branch 'master' into resolver-multi-fields
kibanamachine Nov 18, 2020
d62acac
Merge branch 'master' of github.com:elastic/kibana into resolver-mult…
jonathan-buttner Nov 18, 2020
7da1579
Merge branch 'resolver-multi-fields' of github.com:jonathan-buttner/k…
jonathan-buttner Nov 18, 2020
9e9abf6
Fixing generator start and end time generation
jonathan-buttner Nov 18, 2020
aaad8c4
Adding more comments
jonathan-buttner Nov 18, 2020
1610a48
Revert "Fixing generator start and end time generation"
jonathan-buttner Nov 18, 2020
0694636
Adding test for time
jonathan-buttner Nov 18, 2020
a6aa4b1
Merge branch 'master' into resolver-multi-fields
kibanamachine Nov 23, 2020
d182ca3
Merge branch 'master' into resolver-multi-fields
kibanamachine Nov 24, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,77 @@ export const validateTree = {
}),
};

// {
// id: ["host.id", "process.pid"]
// parent: ["host.id", "process.parent.pid"]
// }

// {
// id_definition: [
// {
// entity_id_field: "name",
// parent_id_field: "name_parent",
// },
// {
// entity_id_field: "age",
// parent_id_field: "age_parent",
// }
// ],
// id: [
// {
// "name": "Jon",
// "age": "50",
// },
// {
// "name": "Mike",
// "age": "~30",
// }
// ],
// levels: 10,
// limit: 50,
// }

// toID(): string

/**
* Used to validate GET requests for a complete resolver tree.
*/
export const validateTree2 = {
body: schema.object({
// optional
// if the ancestry field below is specified ignore these fields
levels: schema.maybe(
schema.object({
ancestors: schema.number({ min: 0, max: 1000 }),
descendants: schema.number({ min: 0, max: 1000 }),
})
),
// levels supersedes limit if it is defined
descendants: schema.number({ defaultValue: 1000, min: 0, max: 10000 }),
ancestors: schema.number({ defaultValue: 1000, min: 0, max: 10000 }),
timerange: schema.object({
start: schema.string(),
end: schema.string(),
}),
schema: schema.object({
// the ancestry field is optional
ancestry: schema.maybe(schema.string()),
// TODO set a limit on the array size
edges: schema.arrayOf(
schema.object({
id: schema.string(),
parentID: schema.string(),
}),
{ minSize: 1 }
),
}),
// TODO would be great if we could enforce that the keys are the same as those defined in the userFieldsDef.relationship
// somehow
nodes: schema.arrayOf(schema.mapOf(schema.string(), schema.any()), { minSize: 1 }),
indexPatterns: schema.arrayOf(schema.string(), { minSize: 1 }),
}),
};

/**
* Used to validate POST requests for `/resolver/events` api.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,28 @@ import {
validateAncestry,
validateAlerts,
validateEntities,
validateTree2,
} from '../../../common/endpoint/schema/resolver';
import { handleChildren } from './resolver/children';
import { handleAncestry } from './resolver/ancestry';
import { handleTree } from './resolver/tree';
import { handleTree as handleTree2 } from './resolver/new_tree/tree_route';
import { handleAlerts } from './resolver/alerts';
import { handleEntities } from './resolver/entity';
import { handleEvents } from './resolver/events';

export function registerResolverRoutes(router: IRouter, endpointAppContext: EndpointAppContext) {
const log = endpointAppContext.logFactory.get('resolver');

router.post(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once we transition the front end over to this api we'll be able to remove all the deprecated routes and their code.

{
path: '/api/endpoint/resolver/tree',
validate: validateTree2,
options: { authRequired: true },
},
handleTree2(log)
);

router.post(
{
path: '/api/endpoint/resolver/events',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { SearchResponse } from 'elasticsearch';
import { ApiResponse } from '@elastic/elasticsearch';
import { IScopedClusterClient } from 'src/core/server';
import { JsonObject, JsonValue } from '../../../../../../../../../src/plugins/kibana_utils/common';
import { UniqueID } from './unique_id';

interface Timerange {
start: string;
end: string;
}

type Nodes = Array<Map<string, string>>;

interface DescendantsParams {
uniqueID: UniqueID;
indexPatterns: string | string[];
timerange: Timerange;
size: number;
}

/**
* Builds a query for retrieving descendants of a node.
*/
export class DescendantsQuery {
private readonly uniqueID: UniqueID;
private readonly indexPatterns: string | string[];
private readonly timerange: Timerange;
private readonly size: number;
constructor({ uniqueID, indexPatterns, timerange, size }: DescendantsParams) {
this.uniqueID = uniqueID;
this.indexPatterns = indexPatterns;
this.timerange = timerange;
this.size = size;
}

private query(shouldClauses: JsonValue[]): JsonObject {
return {
// TODO look into switching this to doc_values
_source: this.uniqueID.sourceFilter,
size: 0,
query: {
bool: {
filter: [
{
range: {
'@timestamp': {
gte: this.timerange.start,
lte: this.timerange.end,
// TODO this is what the search_strategy uses, need to double check
format: 'strict_date_optional_time',
},
},
},
{
bool: {
should: shouldClauses,
},
},
...this.uniqueID.buildConstraints(),
{
term: { 'event.category': 'process' },
},
{
term: { 'event.kind': 'event' },
},
],
},
},
aggs: this.uniqueID.buildAggregations(this.size),
};
}

private queryWithAncestryArray(nodes: Nodes): JsonObject {
return {
// TODO look into switching this to doc_values
_source: this.uniqueID.sourceFilter,
size: 0,
query: {
bool: {
filter: [
{
range: {
'@timestamp': {
gte: this.timerange.start,
lte: this.timerange.end,
// TODO this is what the search_strategy uses, need to double check
format: 'strict_date_optional_time',
},
},
},
...this.uniqueID.buildDescendantsAncestryQueryFilters(nodes),
...this.uniqueID.buildConstraints(),
{
term: { 'event.category': 'process' },
},
{
term: { 'event.kind': 'event' },
},
],
},
},
aggs: this.uniqueID.buildAncestryAggregations(this.size),
};
}

private async searchChunked(client: IScopedClusterClient, nodes: Nodes): Promise<unknown> {
const filters = this.uniqueID.buildDescendantsQueryFilters(nodes);
const searches = [];
// this is the max number of bool clauses an elasticsearch query can contain is 1024 so let's
// make sure we're less than that
const chunkSize = 1000;
for (let i = 0; i < filters.length; i += chunkSize) {
const chunkedFilters = filters.slice(i, i + chunkSize);
searches.push(
client.asCurrentUser.search<SearchResponse<unknown>>({
body: this.query(chunkedFilters),
index: this.indexPatterns,
})
);
}
const results: Array<ApiResponse<SearchResponse<unknown>>> = await Promise.all(searches);
return results.reduce((allResults: unknown[], resultChunk) => {
allResults.push(...this.uniqueID.getNodesFromAggs(resultChunk));
return allResults;
}, []);
}

private async searchWithAncestryArray(
client: IScopedClusterClient,
nodes: Nodes
): Promise<unknown> {}

/**
* TODO get rid of the unknowns
*/
async search(client: IScopedClusterClient, nodes: Nodes): Promise<unknown> {
if (this.uniqueID.idSchema.ancestry) {
return this.searchWithAncestryArray(client, nodes);
}
return this.searchChunked(client, nodes);
}
}
Loading