-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[7.x] [Security_Solution][Resolver] Resolver loading and error state (#…
- Loading branch information
1 parent
787929f
commit 20c6efe
Showing
5 changed files
with
387 additions
and
5 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
x-pack/plugins/security_solution/public/resolver/data_access_layer/mocks/emptify_mock.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* 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 { mockTreeWithNoProcessEvents } from '../../mocks/resolver_tree'; | ||
import { DataAccessLayer } from '../../types'; | ||
|
||
type EmptiableRequests = 'relatedEvents' | 'resolverTree' | 'entities' | 'indexPatterns'; | ||
|
||
interface Metadata<T> { | ||
/** | ||
* The `_id` of the document being analyzed. | ||
*/ | ||
databaseDocumentID: string; | ||
/** | ||
* A record of entityIDs to be used in tests assertions. | ||
*/ | ||
entityIDs: T; | ||
} | ||
|
||
/** | ||
* A simple mock dataAccessLayer that allows you to control whether a request comes back with data or empty. | ||
*/ | ||
export function emptifyMock<T>( | ||
{ | ||
metadata, | ||
dataAccessLayer, | ||
}: { | ||
dataAccessLayer: DataAccessLayer; | ||
metadata: Metadata<T>; | ||
}, | ||
dataShouldBeEmpty: EmptiableRequests[] | ||
): { | ||
dataAccessLayer: DataAccessLayer; | ||
metadata: Metadata<T>; | ||
} { | ||
return { | ||
metadata, | ||
dataAccessLayer: { | ||
/** | ||
* Fetch related events for an entity ID | ||
*/ | ||
async relatedEvents(...args): Promise<ResolverRelatedEvents> { | ||
return dataShouldBeEmpty.includes('relatedEvents') | ||
? Promise.resolve({ | ||
entityID: args[0], | ||
events: [], | ||
nextEvent: null, | ||
}) | ||
: dataAccessLayer.relatedEvents(...args); | ||
}, | ||
|
||
/** | ||
* Fetch a ResolverTree for a entityID | ||
*/ | ||
async resolverTree(...args): Promise<ResolverTree> { | ||
return dataShouldBeEmpty.includes('resolverTree') | ||
? Promise.resolve(mockTreeWithNoProcessEvents()) | ||
: 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. | ||
*/ | ||
async entities(...args): Promise<ResolverEntityIndex> { | ||
return dataShouldBeEmpty.includes('entities') | ||
? Promise.resolve([]) | ||
: dataAccessLayer.entities(...args); | ||
}, | ||
}, | ||
}; | ||
} |
124 changes: 124 additions & 0 deletions
124
x-pack/plugins/security_solution/public/resolver/data_access_layer/mocks/pausify_mock.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* 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 { DataAccessLayer } from '../../types'; | ||
|
||
type PausableRequests = 'relatedEvents' | 'resolverTree' | 'entities'; | ||
|
||
interface Metadata<T> { | ||
/** | ||
* The `_id` of the document being analyzed. | ||
*/ | ||
databaseDocumentID: string; | ||
/** | ||
* A record of entityIDs to be used in tests assertions. | ||
*/ | ||
entityIDs: T; | ||
} | ||
|
||
/** | ||
* A simple mock dataAccessLayer that allows you to manually pause and resume a request. | ||
*/ | ||
export function pausifyMock<T>({ | ||
metadata, | ||
dataAccessLayer, | ||
}: { | ||
dataAccessLayer: DataAccessLayer; | ||
metadata: Metadata<T>; | ||
}): { | ||
dataAccessLayer: DataAccessLayer; | ||
metadata: Metadata<T>; | ||
pause: (pausableRequests: PausableRequests[]) => void; | ||
resume: (pausableRequests: PausableRequests[]) => void; | ||
} { | ||
let relatedEventsPromise = Promise.resolve(); | ||
let resolverTreePromise = Promise.resolve(); | ||
let entitiesPromise = Promise.resolve(); | ||
|
||
let relatedEventsResolver: (() => void) | null; | ||
let resolverTreeResolver: (() => void) | null; | ||
let entitiesResolver: (() => void) | null; | ||
|
||
return { | ||
metadata, | ||
pause: (pausableRequests: PausableRequests[]) => { | ||
const pauseRelatedEventsRequest = pausableRequests.includes('relatedEvents'); | ||
const pauseResolverTreeRequest = pausableRequests.includes('resolverTree'); | ||
const pauseEntitiesRequest = pausableRequests.includes('entities'); | ||
|
||
if (pauseRelatedEventsRequest && !relatedEventsResolver) { | ||
relatedEventsPromise = new Promise((resolve) => { | ||
relatedEventsResolver = resolve; | ||
}); | ||
} | ||
if (pauseResolverTreeRequest && !resolverTreeResolver) { | ||
resolverTreePromise = new Promise((resolve) => { | ||
resolverTreeResolver = resolve; | ||
}); | ||
} | ||
if (pauseEntitiesRequest && !entitiesResolver) { | ||
entitiesPromise = new Promise((resolve) => { | ||
entitiesResolver = resolve; | ||
}); | ||
} | ||
}, | ||
resume: (pausableRequests: PausableRequests[]) => { | ||
const resumeEntitiesRequest = pausableRequests.includes('entities'); | ||
const resumeResolverTreeRequest = pausableRequests.includes('resolverTree'); | ||
const resumeRelatedEventsRequest = pausableRequests.includes('relatedEvents'); | ||
|
||
if (resumeEntitiesRequest && entitiesResolver) { | ||
entitiesResolver(); | ||
entitiesResolver = null; | ||
} | ||
if (resumeResolverTreeRequest && resolverTreeResolver) { | ||
resolverTreeResolver(); | ||
resolverTreeResolver = null; | ||
} | ||
if (resumeRelatedEventsRequest && relatedEventsResolver) { | ||
relatedEventsResolver(); | ||
relatedEventsResolver = null; | ||
} | ||
}, | ||
dataAccessLayer: { | ||
/** | ||
* Fetch related events for an entity ID | ||
*/ | ||
async relatedEvents(...args): Promise<ResolverRelatedEvents> { | ||
await relatedEventsPromise; | ||
return dataAccessLayer.relatedEvents(...args); | ||
}, | ||
|
||
/** | ||
* Fetch a ResolverTree for a entityID | ||
*/ | ||
async resolverTree(...args): Promise<ResolverTree> { | ||
await resolverTreePromise; | ||
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. | ||
*/ | ||
async entities(...args): Promise<ResolverEntityIndex> { | ||
await entitiesPromise; | ||
return dataAccessLayer.entities(...args); | ||
}, | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.