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 retrieve entity id of documents without field mapped #76562

Merged
merged 10 commits into from
Sep 4, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
ResolverTree,
ResolverEntityIndex,
} from '../../../common/endpoint/types';
import { DEFAULT_INDEX_KEY as defaultIndexKey } from '../../../common/constants';

/**
* The data access layer for resolver. All communication with the Kibana server is done through this object. This object is provided to Resolver. In tests, a mock data access layer can be used instead.
Expand All @@ -38,13 +37,6 @@ export function dataAccessLayerFactory(
});
},

/**
Copy link
Contributor

Choose a reason for hiding this comment

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

The indices to use are now passed in to Resolver via props from the Security Solution app.

* Used to get the default index pattern from the SIEM application.
*/
indexPatterns(): string[] {
return context.services.uiSettings.get(defaultIndexKey);
},

/**
* Used to get the entity_id for an _id.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import { mockTreeWithNoProcessEvents } from '../../mocks/resolver_tree';
import { DataAccessLayer } from '../../types';

type EmptiableRequests = 'relatedEvents' | 'resolverTree' | 'entities' | 'indexPatterns';
type EmptiableRequests = 'relatedEvents' | 'resolverTree' | 'entities';

interface Metadata<T> {
/**
Expand Down Expand Up @@ -66,15 +66,6 @@ export function emptifyMock<T>(
: dataAccessLayer.resolverTree(...args);
},

/**
* Get an array of index patterns that contain events.
*/
indexPatterns(...args): string[] {
return dataShouldBeEmpty.includes('indexPatterns')
? []
: dataAccessLayer.indexPatterns(...args);
},

/**
* Get entities matching a document.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,6 @@ export function noAncestorsTwoChildren(): { dataAccessLayer: DataAccessLayer; me
);
},

/**
* Get an array of index patterns that contain events.
*/
indexPatterns(): string[] {
return ['index pattern'];
},

/**
* Get entities matching a document.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,6 @@ export function noAncestorsTwoChildrenWithRelatedEventsOnOrigin(): {
return Promise.resolve(tree);
},

/**
* Get an array of index patterns that contain events.
*/
indexPatterns(): string[] {
return ['index pattern'];
},

/**
* Get entities matching a document.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* 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 {
ResolverRelatedEvents,
ResolverTree,
ResolverEntityIndex,
} from '../../../../common/endpoint/types';
import { mockEndpointEvent } from '../../mocks/endpoint_event';
import { mockTreeWithNoAncestorsAnd2Children } from '../../mocks/resolver_tree';
import { DataAccessLayer } from '../../types';

interface Metadata {
/**
* The `_id` of the document being analyzed.
*/
databaseDocumentID: string;
/**
* A record of entityIDs to be used in tests assertions.
*/
entityIDs: {
/**
* The entityID of the node related to the document being analyzed.
*/
origin: 'origin';
/**
* The entityID of the first child of the origin.
*/
firstChild: 'firstChild';
/**
* The entityID of the second child of the origin.
*/
secondChild: 'secondChild';
};
}

/**
* A simple mock dataAccessLayer possible that returns a tree with 0 ancestors and 2 direct children. 1 related event is returned. The parameter to `entities` is ignored.
*/
export function handleNoIndices(): { dataAccessLayer: DataAccessLayer; metadata: Metadata } {
const metadata: Metadata = {
databaseDocumentID: '_id',
entityIDs: { origin: 'origin', firstChild: 'firstChild', secondChild: 'secondChild' },
};
return {
metadata,
dataAccessLayer: {
/**
* Fetch related events for an entity ID
*/
relatedEvents(entityID: string): Promise<ResolverRelatedEvents> {
return Promise.resolve({
entityID,
events: [
mockEndpointEvent({
entityID,
name: 'event',
timestamp: 0,
}),
],
nextEvent: null,
});
},

/**
* Fetch a ResolverTree for a entityID
*/
resolverTree(): Promise<ResolverTree> {
return Promise.resolve(
mockTreeWithNoAncestorsAnd2Children({
originID: metadata.entityIDs.origin,
firstChildID: metadata.entityIDs.firstChild,
secondChildID: metadata.entityIDs.secondChild,
})
);
},

/**
* Get entities matching a document.
*/
entities({ indices }): Promise<ResolverEntityIndex> {
if (indices.length === 1 && indices[0] === 'awesome_index') {
return Promise.resolve([{ entity_id: metadata.entityIDs.origin }]);
}
return Promise.resolve([]);
},
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,6 @@ export function pausifyMock<T>({
return dataAccessLayer.resolverTree(...args);
},

/**
* Get an array of index patterns that contain events.
*/
indexPatterns(...args): string[] {
return dataAccessLayer.indexPatterns(...args);
},

/**
* Get entities matching a document.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* 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 _ from 'lodash';
import { DatabaseParameters } from '../types';

export function equal(param1: DatabaseParameters, param2?: DatabaseParameters) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why add this extra abstraction layer? For the type checking?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think since timeline now sends resolver the ID and the indices we figured it'd be a good idea to combine them in a single object. Since indices is a string array we need some way to compare all the strings easily so Rob and I came up with this method. We might switch out the lodash equal though.

Copy link
Contributor

Choose a reason for hiding this comment

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

I was specifically talking about the abstraction of equal over isEqual. I understand the combined type, that makes sense. It's really more, if this equality check is going to be focused solely on checking the equality of DatabaseParameters, we know the shape, so might as well explicitly check the equality of the individual fields?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh you mean like, why not just use _.isEqual instead of our equal function? Or are you suggesting doing the comparisons directly in the places we're calling equal? I think @oatkiller was considering removing the lodash use so we'd have to implement an array comparison in that case. We'd probably want the array comparison at a minimum to be in a helper function because we do the DatabaseParameters check in like 3 different places I think.

I guess another option is we make DatabaseParameters into a class and move equal as a method of that class?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yea, that was my thought, but since it's going to be something different from isEqual, then I would just leave it. Depending on what it is, it could still be more generalized into a util, but no need to do that here anyways

return _.isEqual(param1, param2);
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ interface AppReceivedNewExternalProperties {
/**
* the `_id` of an ES document. This defines the origin of the Resolver graph.
*/
databaseDocumentID?: string;
databaseDocumentID: string;
Copy link
Contributor

Choose a reason for hiding this comment

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

This is required by ResolverProps now should it will always be present and can always be passed to this event.

/**
* An ID that uniquely identifies this Resolver instance from other concurrent Resolvers.
*/
Expand All @@ -125,6 +125,11 @@ interface AppReceivedNewExternalProperties {
* The `search` part of the URL of this page.
*/
locationSearch: string;

/**
* Indices that the backend will use to find the document.
*/
indices: string[];
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import { ResolverRelatedEvents, ResolverTree } from '../../../../common/endpoint/types';
import { DatabaseParameters } from '../../types';

interface ServerReturnedResolverData {
readonly type: 'serverReturnedResolverData';
Expand All @@ -14,9 +15,9 @@ interface ServerReturnedResolverData {
*/
result: ResolverTree;
/**
* The database document ID that was used to fetch the resolver tree
* The database parameters that was used to fetch the resolver tree
*/
databaseDocumentID: string;
parameters: DatabaseParameters;
};
}

Expand All @@ -25,31 +26,31 @@ interface AppRequestedResolverData {
/**
* entity ID used to make the request.
*/
readonly payload: string;
readonly payload: DatabaseParameters;
}

interface ServerFailedToReturnResolverData {
readonly type: 'serverFailedToReturnResolverData';
/**
* entity ID used to make the failed request
*/
readonly payload: string;
readonly payload: DatabaseParameters;
}

interface AppAbortedResolverDataRequest {
readonly type: 'appAbortedResolverDataRequest';
/**
* entity ID used to make the aborted request
*/
readonly payload: string;
readonly payload: DatabaseParameters;
}

/**
* Will occur when a request for related event data is unsuccessful.
*/
interface ServerFailedToReturnRelatedEventData {
readonly type: 'serverFailedToReturnRelatedEventData';
readonly payload: string;
readonly payload: DatabaseParameters;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { Reducer } from 'redux';
import { DataState } from '../../types';
import { ResolverAction } from '../actions';
import * as databaseParameters from '../../models/database_parameters';

const initialState: DataState = {
relatedEvents: new Map(),
Expand All @@ -18,22 +19,28 @@ export const dataReducer: Reducer<DataState, ResolverAction> = (state = initialS
if (action.type === 'appReceivedNewExternalProperties') {
const nextState: DataState = {
...state,
databaseDocumentID: action.payload.databaseDocumentID,
currentParameters: {
databaseDocumentID: action.payload.databaseDocumentID,
indices: action.payload.indices,
},
resolverComponentInstanceID: action.payload.resolverComponentInstanceID,
};
return nextState;
} else if (action.type === 'appRequestedResolverData') {
// keep track of what we're requesting, this way we know when to request and when not to.
return {
...state,
pendingRequestDatabaseDocumentID: action.payload,
pendingRequestParameters: {
databaseDocumentID: action.payload.databaseDocumentID,
indices: action.payload.indices,
},
};
} else if (action.type === 'appAbortedResolverDataRequest') {
if (action.payload === state.pendingRequestDatabaseDocumentID) {
if (databaseParameters.equal(action.payload, state.pendingRequestParameters)) {
// the request we were awaiting was aborted
return {
...state,
pendingRequestDatabaseDocumentID: undefined,
pendingRequestParameters: undefined,
};
} else {
return state;
Expand All @@ -48,23 +55,23 @@ export const dataReducer: Reducer<DataState, ResolverAction> = (state = initialS
*/
lastResponse: {
result: action.payload.result,
databaseDocumentID: action.payload.databaseDocumentID,
parameters: action.payload.parameters,
successful: true,
},

// This assumes that if we just received something, there is no longer a pending request.
// This cannot model multiple in-flight requests
pendingRequestDatabaseDocumentID: undefined,
pendingRequestParameters: undefined,
};
return nextState;
} else if (action.type === 'serverFailedToReturnResolverData') {
/** Only handle this if we are expecting a response */
if (state.pendingRequestDatabaseDocumentID !== undefined) {
if (state.pendingRequestParameters !== undefined) {
const nextState: DataState = {
...state,
pendingRequestDatabaseDocumentID: undefined,
pendingRequestParameters: undefined,
lastResponse: {
databaseDocumentID: state.pendingRequestDatabaseDocumentID,
parameters: state.pendingRequestParameters,
successful: false,
},
};
Expand Down
Loading