[SIEM] Fix and consolidate handling of error responses in the client (#59438)
* Convert our manual throwing of TypeError to a custom Error
Throwing a TypeError meant that our manual errors were indistinguishable
from, say, trying to invoke a method on undefined. This adds a custom
error, BadRequestError, that disambiguates that situation.
* Present API Error messages to the user
With Core's new HTTP client, an unsuccessful API call will raise an
error containing the body of the response it received. In the case of
SIEM endpoints, this will include a useful error message with
potentially more specificity than e.g. 'Internal Server Error'.
This adds a type predicate to check for such errors, and adds a handling
case in our errorToToaster handler.
If the error does not contain our SIEM-specific message, it will fall
through as normal and the general error.message will be displayed in the
toaster.
* Remove unnecessary use of throwIfNotOk in our client API calls
The new HTTP client raises an error on a 4xx or 5xx response, so there
should not be a case where throwIfNotOk is actually going to throw an
error.
The established pattern on the frontend is to catch errors at the call
site and handle them appropriately, so I'm mainly just verifying that
these are caught where they're used, now.
* Move errorToToaster and ToasterError to general location
These were living in ML since that's where they originated. However, we
have need of it (and already use it) elsewhere.
The basic pattern for error handling on the frontend is:
1) API call throws error
2) caller catches error and dispatches a toast
throwIfNotOk is meant to convert the error into a useful message in 1).
We currently use both errorToToaster and displayErrorToast to display
that in a toaster in 2)
Now that errorToToaster handles a few different types of errors, and
throwIfNotOk is going to be bypassed due to the new client behavior of
throwing on error, we're going to start consolidating on:
1) Api call throws error
2) caller catches error and passes it to errorToToaster
* Refactor Rules API functions to not use throwIfNotOk
* Ensures that all callers of these methods properly catch errors
* Updates error toasterification to use errorToToaster
* Simplifies tests now that we mainly just invoke the http client and
return the result.
throwIfNotOk is not being used in the majority of cases, as the client
raises an error and bypasses that call.
The few cases this might break are where we return a 200 but have errors
within the response. Whether throwIfNotOk handled this or not, I'll need
a simpler helper to accomplish the same behavior.
* Define a type for our BulkRule responses
These can be an array of errors OR rules; typing it as such forces
downstream to deal with both. enableRules was being handled correctly
with the bucketing helper, and TS has confirmed the rest are as well.
This obviates the need to raise from our API calls, as bulk errors are
recoverable and we want to both a) continue on with any successful rules
and b) handle the errors as necessary. This is highly dependent on the
caller and so we can't/shouldn't handle it here.
* Address case where bulk rules errors were not handled
I'm not sure that we're ever using this non-dispatch version, but it was
throwing a type error. Will bring it up in review.
* Remove more throwIfNotOk uses from API calls
These are unneeded as an error response will already throw an error to
be handled at the call site.
* Display an error toaster on newsfeed fetch failure
* Remove dead code
This was left over as a result of #56261
* Remove throwIfNotOk from case API calls
Again, not needed because the client already throws.
* Update use_get_tags for NP
* Gets rid of throwIfNotOK usage
* uses core http fetch
* Remove throwIfNotOk from signals API
* Remove throwIfNotOk
This served the same purpose as errorToToaster, but in a less robust
way. All usages have been replaced, so now we say goodbye.
* Remove custom errors in favor of KibanaApiError and isApiError type predicate
There was no functional difference between these two code paths, and
removing these custom errors allowed us to delete a bunch of associated
code as well..
* Fix test failures
These were mainly related to my swapping any remaining fetch calls with
the core router as good kibana denizens should :salute:
* Replace use of core mocks with our simpler local ones
This is enough to get our tests to pass. We can't use the core mocks for
now since there are circular dependencies there, which breaks our build.
* add signal api unit tests
* privilege unit test api
* Add unit tests on the signals container
* Refactor signals API tests to use core mocks
* Simplifies our mocking verbosity by leveraging core mocks
* Simplifies test setup by isolating a reference to our fetch mock
* Abstracts response structure to pure helper functions
The try/catch tests had some false positives in that nothing would be
asserted if the code did not throw an error. These proved to be masking
a gap in coverage for our get/create signal index requests, which do not
leverage `throwIfNotOk` but instead rely on the fetch to throw an error;
once that behavior is verified we can update those tests to have our
fetchMock throw errors, and we should be all set.
* Simplify signals API tests now that the subjects do less
We no longer re-throw errors, or parse the response, we just return the
result of the client call. Simple!
* Simplify API functions to use implict returns
When possible. Also adds missing error-throwing documentation where
necessary.
* Revert "Display an error toaster on newsfeed fetch failure"
This reverts commit 64213221f5
.
* Error property is readonly
* Pull uuid generation into default argument value
* Fix type predicate isApiError
Uses has to properly inspect our errorish object. Turns out we have a
'message' property, not an 'error' property.
* Fix test setup following modification of type predicate
We need a message (via new Error), a body.message, and a
body.status_code to satisfy isApiError.
Co-authored-by: Xavier Mouligneau <189600+XavierM@users.noreply.github.com>
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
This commit is contained in:
parent
eb533c8211
commit
b56cd41412
|
@ -10,8 +10,7 @@ import { InfluencerInput, Anomalies, CriteriaFields } from '../types';
|
|||
import { hasMlUserPermissions } from '../permissions/has_ml_user_permissions';
|
||||
import { MlCapabilitiesContext } from '../permissions/ml_capabilities_provider';
|
||||
import { useSiemJobs } from '../../ml_popover/hooks/use_siem_jobs';
|
||||
import { useStateToaster } from '../../toasters';
|
||||
import { errorToToaster } from '../api/error_to_toaster';
|
||||
import { useStateToaster, errorToToaster } from '../../toasters';
|
||||
|
||||
import * as i18n from './translations';
|
||||
import { useTimeZone, useUiSetting$ } from '../../../lib/kibana';
|
||||
|
|
|
@ -5,7 +5,6 @@
|
|||
*/
|
||||
|
||||
import { Anomalies, InfluencerInput, CriteriaFields } from '../types';
|
||||
import { throwIfNotOk } from '../../../hooks/api/api';
|
||||
import { KibanaServices } from '../../../lib/kibana';
|
||||
|
||||
export interface Body {
|
||||
|
@ -22,17 +21,10 @@ export interface Body {
|
|||
}
|
||||
|
||||
export const anomaliesTableData = async (body: Body, signal: AbortSignal): Promise<Anomalies> => {
|
||||
const response = await KibanaServices.get().http.fetch<Anomalies>(
|
||||
'/api/ml/results/anomalies_table_data',
|
||||
{
|
||||
method: 'POST',
|
||||
body: JSON.stringify(body),
|
||||
asResponse: true,
|
||||
asSystemRequest: true,
|
||||
signal,
|
||||
}
|
||||
);
|
||||
|
||||
await throwIfNotOk(response.response);
|
||||
return response.body!;
|
||||
return KibanaServices.get().http.fetch<Anomalies>('/api/ml/results/anomalies_table_data', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(body),
|
||||
asSystemRequest: true,
|
||||
signal,
|
||||
});
|
||||
};
|
||||
|
|
|
@ -1,67 +0,0 @@
|
|||
/*
|
||||
* 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 { isError } from 'lodash/fp';
|
||||
import uuid from 'uuid';
|
||||
import { ActionToaster, AppToast } from '../../toasters';
|
||||
import { ToasterErrorsType, ToasterErrors } from '../../../hooks/api/throw_if_not_ok';
|
||||
|
||||
export type ErrorToToasterArgs = Partial<AppToast> & {
|
||||
error: unknown;
|
||||
dispatchToaster: React.Dispatch<ActionToaster>;
|
||||
};
|
||||
|
||||
export const errorToToaster = ({
|
||||
id = uuid.v4(),
|
||||
title,
|
||||
error,
|
||||
color = 'danger',
|
||||
iconType = 'alert',
|
||||
dispatchToaster,
|
||||
}: ErrorToToasterArgs) => {
|
||||
if (isToasterError(error)) {
|
||||
const toast: AppToast = {
|
||||
id,
|
||||
title,
|
||||
color,
|
||||
iconType,
|
||||
errors: error.messages,
|
||||
};
|
||||
dispatchToaster({
|
||||
type: 'addToaster',
|
||||
toast,
|
||||
});
|
||||
} else if (isAnError(error)) {
|
||||
const toast: AppToast = {
|
||||
id,
|
||||
title,
|
||||
color,
|
||||
iconType,
|
||||
errors: [error.message],
|
||||
};
|
||||
dispatchToaster({
|
||||
type: 'addToaster',
|
||||
toast,
|
||||
});
|
||||
} else {
|
||||
const toast: AppToast = {
|
||||
id,
|
||||
title,
|
||||
color,
|
||||
iconType,
|
||||
errors: ['Network Error'],
|
||||
};
|
||||
dispatchToaster({
|
||||
type: 'addToaster',
|
||||
toast,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const isAnError = (error: unknown): error is Error => isError(error);
|
||||
|
||||
export const isToasterError = (error: unknown): error is ToasterErrorsType =>
|
||||
error instanceof ToasterErrors;
|
|
@ -0,0 +1,20 @@
|
|||
/*
|
||||
* 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 { has } from 'lodash/fp';
|
||||
|
||||
import { MlError } from '../types';
|
||||
|
||||
export interface MlStartJobError {
|
||||
error: MlError;
|
||||
started: boolean;
|
||||
}
|
||||
|
||||
// use the "in operator" and regular type guards to do a narrow once this issue is fixed below:
|
||||
// https://github.com/microsoft/TypeScript/issues/21732
|
||||
// Otherwise for now, has will work ok even though it casts 'unknown' to 'any'
|
||||
export const isMlStartJobError = (value: unknown): value is MlStartJobError =>
|
||||
has('error.msg', value) && has('error.response', value) && has('error.statusCode', value);
|
|
@ -5,7 +5,6 @@
|
|||
*/
|
||||
|
||||
import { InfluencerInput, MlCapabilities } from '../types';
|
||||
import { throwIfNotOk } from '../../../hooks/api/api';
|
||||
import { KibanaServices } from '../../../lib/kibana';
|
||||
|
||||
export interface Body {
|
||||
|
@ -22,16 +21,9 @@ export interface Body {
|
|||
}
|
||||
|
||||
export const getMlCapabilities = async (signal: AbortSignal): Promise<MlCapabilities> => {
|
||||
const response = await KibanaServices.get().http.fetch<MlCapabilities>(
|
||||
'/api/ml/ml_capabilities',
|
||||
{
|
||||
method: 'GET',
|
||||
asResponse: true,
|
||||
asSystemRequest: true,
|
||||
signal,
|
||||
}
|
||||
);
|
||||
|
||||
await throwIfNotOk(response.response);
|
||||
return response.body!;
|
||||
return KibanaServices.get().http.fetch<MlCapabilities>('/api/ml/ml_capabilities', {
|
||||
method: 'GET',
|
||||
asSystemRequest: true,
|
||||
signal,
|
||||
});
|
||||
};
|
||||
|
|
|
@ -5,48 +5,21 @@
|
|||
*/
|
||||
|
||||
import fetchMock from 'fetch-mock';
|
||||
|
||||
import { ToasterError } from '../../toasters';
|
||||
import { SetupMlResponse } from '../../ml_popover/types';
|
||||
import { isMlStartJobError } from './errors';
|
||||
import {
|
||||
isMlStartJobError,
|
||||
MessageBody,
|
||||
parseJsonFromBody,
|
||||
throwIfErrorAttached,
|
||||
throwIfErrorAttachedToSetup,
|
||||
ToasterErrors,
|
||||
tryParseResponse,
|
||||
} from './throw_if_not_ok';
|
||||
import { SetupMlResponse } from '../../components/ml_popover/types';
|
||||
|
||||
describe('throw_if_not_ok', () => {
|
||||
afterEach(() => {
|
||||
fetchMock.reset();
|
||||
});
|
||||
|
||||
describe('#parseJsonFromBody', () => {
|
||||
test('parses a json from the body correctly', async () => {
|
||||
fetchMock.mock('http://example.com', {
|
||||
status: 500,
|
||||
body: {
|
||||
error: 'some error',
|
||||
statusCode: 500,
|
||||
message: 'I am a custom message',
|
||||
},
|
||||
});
|
||||
const response = await fetch('http://example.com');
|
||||
const expected: MessageBody = {
|
||||
error: 'some error',
|
||||
statusCode: 500,
|
||||
message: 'I am a custom message',
|
||||
};
|
||||
await expect(parseJsonFromBody(response)).resolves.toEqual(expected);
|
||||
});
|
||||
|
||||
test('returns null if the body does not exist', async () => {
|
||||
fetchMock.mock('http://example.com', { status: 500, body: 'some text' });
|
||||
const response = await fetch('http://example.com');
|
||||
await expect(parseJsonFromBody(response)).resolves.toEqual(null);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#tryParseResponse', () => {
|
||||
test('It formats a JSON object', () => {
|
||||
const parsed = tryParseResponse(JSON.stringify({ hello: 'how are you?' }));
|
||||
|
@ -119,7 +92,7 @@ describe('throw_if_not_ok', () => {
|
|||
},
|
||||
};
|
||||
expect(() => throwIfErrorAttached(json, ['some-id'])).toThrow(
|
||||
new ToasterErrors(['some message'])
|
||||
new ToasterError(['some message'])
|
||||
);
|
||||
});
|
||||
|
|
@ -4,32 +4,10 @@
|
|||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
|
||||
import { has } from 'lodash/fp';
|
||||
|
||||
import * as i18n from '../../components/ml/api/translations';
|
||||
import { MlError } from '../../components/ml/types';
|
||||
import { SetupMlResponse } from '../../components/ml_popover/types';
|
||||
|
||||
export { MessageBody, parseJsonFromBody } from '../../utils/api';
|
||||
|
||||
export interface MlStartJobError {
|
||||
error: MlError;
|
||||
started: boolean;
|
||||
}
|
||||
|
||||
export type ToasterErrorsType = Error & {
|
||||
messages: string[];
|
||||
};
|
||||
|
||||
export class ToasterErrors extends Error implements ToasterErrorsType {
|
||||
public messages: string[];
|
||||
|
||||
constructor(messages: string[]) {
|
||||
super(messages[0]);
|
||||
this.name = 'ToasterErrors';
|
||||
this.messages = messages;
|
||||
}
|
||||
}
|
||||
import * as i18n from './translations';
|
||||
import { ToasterError } from '../../toasters';
|
||||
import { SetupMlResponse } from '../../ml_popover/types';
|
||||
import { isMlStartJobError } from './errors';
|
||||
|
||||
export const tryParseResponse = (response: string): string => {
|
||||
try {
|
||||
|
@ -71,7 +49,7 @@ export const throwIfErrorAttachedToSetup = (
|
|||
|
||||
const errors = [...jobErrors, ...dataFeedErrors];
|
||||
if (errors.length > 0) {
|
||||
throw new ToasterErrors(errors);
|
||||
throw new ToasterError(errors);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -93,12 +71,6 @@ export const throwIfErrorAttached = (
|
|||
}
|
||||
}, []);
|
||||
if (errors.length > 0) {
|
||||
throw new ToasterErrors(errors);
|
||||
throw new ToasterError(errors);
|
||||
}
|
||||
};
|
||||
|
||||
// use the "in operator" and regular type guards to do a narrow once this issue is fixed below:
|
||||
// https://github.com/microsoft/TypeScript/issues/21732
|
||||
// Otherwise for now, has will work ok even though it casts 'unknown' to 'any'
|
||||
export const isMlStartJobError = (value: unknown): value is MlStartJobError =>
|
||||
has('error.msg', value) && has('error.response', value) && has('error.statusCode', value);
|
|
@ -9,8 +9,7 @@ import React, { useState, useEffect } from 'react';
|
|||
import { MlCapabilities } from '../types';
|
||||
import { getMlCapabilities } from '../api/get_ml_capabilities';
|
||||
import { emptyMlCapabilities } from '../empty_ml_capabilities';
|
||||
import { errorToToaster } from '../api/error_to_toaster';
|
||||
import { useStateToaster } from '../../toasters';
|
||||
import { errorToToaster, useStateToaster } from '../../toasters';
|
||||
|
||||
import * as i18n from './translations';
|
||||
|
||||
|
|
|
@ -17,8 +17,7 @@ import {
|
|||
StartDatafeedResponse,
|
||||
StopDatafeedResponse,
|
||||
} from './types';
|
||||
import { throwIfErrorAttached, throwIfErrorAttachedToSetup } from '../../hooks/api/throw_if_not_ok';
|
||||
import { throwIfNotOk } from '../../hooks/api/api';
|
||||
import { throwIfErrorAttached, throwIfErrorAttachedToSetup } from '../ml/api/throw_if_not_ok';
|
||||
import { KibanaServices } from '../../lib/kibana';
|
||||
|
||||
/**
|
||||
|
@ -26,45 +25,36 @@ import { KibanaServices } from '../../lib/kibana';
|
|||
*
|
||||
* @param indexPatternName ES index pattern to check for compatible modules
|
||||
* @param signal to cancel request
|
||||
*
|
||||
* @throws An error if response is not OK
|
||||
*/
|
||||
export const checkRecognizer = async ({
|
||||
indexPatternName,
|
||||
signal,
|
||||
}: CheckRecognizerProps): Promise<RecognizerModule[]> => {
|
||||
const response = await KibanaServices.get().http.fetch<RecognizerModule[]>(
|
||||
}: CheckRecognizerProps): Promise<RecognizerModule[]> =>
|
||||
KibanaServices.get().http.fetch<RecognizerModule[]>(
|
||||
`/api/ml/modules/recognize/${indexPatternName}`,
|
||||
{
|
||||
method: 'GET',
|
||||
asResponse: true,
|
||||
asSystemRequest: true,
|
||||
signal,
|
||||
}
|
||||
);
|
||||
|
||||
await throwIfNotOk(response.response);
|
||||
return response.body!;
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns ML Module for given moduleId. Returns all modules if no moduleId specified
|
||||
*
|
||||
* @param moduleId id of the module to retrieve
|
||||
* @param signal to cancel request
|
||||
*
|
||||
* @throws An error if response is not OK
|
||||
*/
|
||||
export const getModules = async ({ moduleId = '', signal }: GetModulesProps): Promise<Module[]> => {
|
||||
const response = await KibanaServices.get().http.fetch<Module[]>(
|
||||
`/api/ml/modules/get_module/${moduleId}`,
|
||||
{
|
||||
method: 'GET',
|
||||
asResponse: true,
|
||||
asSystemRequest: true,
|
||||
signal,
|
||||
}
|
||||
);
|
||||
|
||||
await throwIfNotOk(response.response);
|
||||
return response.body!;
|
||||
};
|
||||
export const getModules = async ({ moduleId = '', signal }: GetModulesProps): Promise<Module[]> =>
|
||||
KibanaServices.get().http.fetch<Module[]>(`/api/ml/modules/get_module/${moduleId}`, {
|
||||
method: 'GET',
|
||||
asSystemRequest: true,
|
||||
signal,
|
||||
});
|
||||
|
||||
/**
|
||||
* Creates ML Jobs + Datafeeds for the given configTemplate + indexPatternName
|
||||
|
@ -74,6 +64,8 @@ export const getModules = async ({ moduleId = '', signal }: GetModulesProps): Pr
|
|||
* @param jobIdErrorFilter - if provided, filters all errors except for given jobIds
|
||||
* @param groups - list of groups to add to jobs being installed
|
||||
* @param prefix - prefix to be added to job name
|
||||
*
|
||||
* @throws An error if response is not OK
|
||||
*/
|
||||
export const setupMlJob = async ({
|
||||
configTemplate,
|
||||
|
@ -93,16 +85,12 @@ export const setupMlJob = async ({
|
|||
startDatafeed: false,
|
||||
useDedicatedIndex: true,
|
||||
}),
|
||||
asResponse: true,
|
||||
asSystemRequest: true,
|
||||
}
|
||||
);
|
||||
|
||||
await throwIfNotOk(response.response);
|
||||
const json = response.body!;
|
||||
throwIfErrorAttachedToSetup(json, jobIdErrorFilter);
|
||||
|
||||
return json;
|
||||
throwIfErrorAttachedToSetup(response, jobIdErrorFilter);
|
||||
return response;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -110,6 +98,8 @@ export const setupMlJob = async ({
|
|||
*
|
||||
* @param datafeedIds
|
||||
* @param start
|
||||
*
|
||||
* @throws An error if response is not OK
|
||||
*/
|
||||
export const startDatafeeds = async ({
|
||||
datafeedIds,
|
||||
|
@ -126,22 +116,20 @@ export const startDatafeeds = async ({
|
|||
datafeedIds,
|
||||
...(start !== 0 && { start }),
|
||||
}),
|
||||
asResponse: true,
|
||||
asSystemRequest: true,
|
||||
}
|
||||
);
|
||||
|
||||
await throwIfNotOk(response.response);
|
||||
const json = response.body!;
|
||||
throwIfErrorAttached(json, datafeedIds);
|
||||
|
||||
return json;
|
||||
throwIfErrorAttached(response, datafeedIds);
|
||||
return response;
|
||||
};
|
||||
|
||||
/**
|
||||
* Stops the given dataFeedIds and sets the corresponding Job's jobState to closed
|
||||
*
|
||||
* @param datafeedIds
|
||||
*
|
||||
* @throws An error if response is not OK
|
||||
*/
|
||||
export const stopDatafeeds = async ({
|
||||
datafeedIds,
|
||||
|
@ -155,14 +143,10 @@ export const stopDatafeeds = async ({
|
|||
body: JSON.stringify({
|
||||
datafeedIds,
|
||||
}),
|
||||
asResponse: true,
|
||||
asSystemRequest: true,
|
||||
}
|
||||
);
|
||||
|
||||
await throwIfNotOk(stopDatafeedsResponse.response);
|
||||
const stopDatafeedsResponseJson = stopDatafeedsResponse.body!;
|
||||
|
||||
const datafeedPrefix = 'datafeed-';
|
||||
const closeJobsResponse = await KibanaServices.get().http.fetch<CloseJobsResponse>(
|
||||
'/api/ml/jobs/close_jobs',
|
||||
|
@ -175,13 +159,11 @@ export const stopDatafeeds = async ({
|
|||
: dataFeedId
|
||||
),
|
||||
}),
|
||||
asResponse: true,
|
||||
asSystemRequest: true,
|
||||
}
|
||||
);
|
||||
|
||||
await throwIfNotOk(closeJobsResponse.response);
|
||||
return [stopDatafeedsResponseJson, closeJobsResponse.body!];
|
||||
return [stopDatafeedsResponse, closeJobsResponse];
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -191,19 +173,13 @@ export const stopDatafeeds = async ({
|
|||
* return a 500
|
||||
*
|
||||
* @param signal to cancel request
|
||||
*
|
||||
* @throws An error if response is not OK
|
||||
*/
|
||||
export const getJobsSummary = async (signal: AbortSignal): Promise<JobSummary[]> => {
|
||||
const response = await KibanaServices.get().http.fetch<JobSummary[]>(
|
||||
'/api/ml/jobs/jobs_summary',
|
||||
{
|
||||
method: 'POST',
|
||||
body: JSON.stringify({}),
|
||||
asResponse: true,
|
||||
asSystemRequest: true,
|
||||
signal,
|
||||
}
|
||||
);
|
||||
|
||||
await throwIfNotOk(response.response);
|
||||
return response.body!;
|
||||
};
|
||||
export const getJobsSummary = async (signal: AbortSignal): Promise<JobSummary[]> =>
|
||||
KibanaServices.get().http.fetch<JobSummary[]>('/api/ml/jobs/jobs_summary', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({}),
|
||||
asSystemRequest: true,
|
||||
signal,
|
||||
});
|
||||
|
|
|
@ -10,8 +10,7 @@ import { checkRecognizer, getJobsSummary, getModules } from '../api';
|
|||
import { SiemJob } from '../types';
|
||||
import { hasMlUserPermissions } from '../../ml/permissions/has_ml_user_permissions';
|
||||
import { MlCapabilitiesContext } from '../../ml/permissions/ml_capabilities_provider';
|
||||
import { useStateToaster } from '../../toasters';
|
||||
import { errorToToaster } from '../../ml/api/error_to_toaster';
|
||||
import { errorToToaster, useStateToaster } from '../../toasters';
|
||||
import { useUiSetting$ } from '../../../lib/kibana';
|
||||
import { DEFAULT_INDEX_KEY } from '../../../../common/constants';
|
||||
|
||||
|
|
|
@ -12,10 +12,9 @@ import styled from 'styled-components';
|
|||
|
||||
import { useKibana } from '../../lib/kibana';
|
||||
import { METRIC_TYPE, TELEMETRY_EVENT, track } from '../../lib/telemetry';
|
||||
import { errorToToaster } from '../ml/api/error_to_toaster';
|
||||
import { hasMlAdminPermissions } from '../ml/permissions/has_ml_admin_permissions';
|
||||
import { MlCapabilitiesContext } from '../ml/permissions/ml_capabilities_provider';
|
||||
import { useStateToaster } from '../toasters';
|
||||
import { errorToToaster, useStateToaster } from '../toasters';
|
||||
import { setupMlJob, startDatafeeds, stopDatafeeds } from './api';
|
||||
import { filterJobs } from './helpers';
|
||||
import { useSiemJobs } from './hooks/use_siem_jobs';
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
|
||||
import { KibanaServices } from '../../lib/kibana';
|
||||
import { NEWS_FEED_URL_SETTING_DEFAULT } from '../../../common/constants';
|
||||
import { rawNewsApiResponse } from '../../mock/news';
|
||||
import { rawNewsJSON } from '../../mock/raw_news';
|
||||
|
@ -18,7 +19,7 @@ import {
|
|||
} from './helpers';
|
||||
import { NewsItem, RawNewsApiResponse } from './types';
|
||||
|
||||
type GlobalWithFetch = NodeJS.Global & { fetch: jest.Mock };
|
||||
jest.mock('../../lib/kibana');
|
||||
|
||||
describe('helpers', () => {
|
||||
describe('removeSnapshotFromVersion', () => {
|
||||
|
@ -390,37 +391,19 @@ describe('helpers', () => {
|
|||
});
|
||||
|
||||
describe('fetchNews', () => {
|
||||
const newsFeedUrl = 'https://feeds.elastic.co/security-solution/v8.0.0.json';
|
||||
const mockKibanaServices = KibanaServices.get as jest.Mock;
|
||||
const fetchMock = jest.fn();
|
||||
mockKibanaServices.mockReturnValue({ http: { fetch: fetchMock } });
|
||||
|
||||
afterAll(() => {
|
||||
delete (global as GlobalWithFetch).fetch;
|
||||
beforeEach(() => {
|
||||
fetchMock.mockClear();
|
||||
fetchMock.mockResolvedValue(rawNewsApiResponse);
|
||||
});
|
||||
|
||||
test('it returns the raw API response from the news feed', async () => {
|
||||
(global as GlobalWithFetch).fetch = jest.fn().mockImplementationOnce(() =>
|
||||
Promise.resolve({
|
||||
ok: true,
|
||||
json: () => {
|
||||
return rawNewsApiResponse;
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
const newsFeedUrl = 'https://feeds.elastic.co/security-solution/v8.0.0.json';
|
||||
expect(await fetchNews({ newsFeedUrl })).toEqual(rawNewsApiResponse);
|
||||
});
|
||||
|
||||
test('it throws if the response from the news feed is not ok', async () => {
|
||||
(global as GlobalWithFetch).fetch = jest.fn().mockImplementationOnce(() =>
|
||||
Promise.resolve({
|
||||
ok: false,
|
||||
json: () => {
|
||||
return rawNewsApiResponse;
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
await expect(fetchNews({ newsFeedUrl })).rejects.toThrow('Network Error: undefined');
|
||||
});
|
||||
});
|
||||
|
||||
describe('showNewsItem', () => {
|
||||
|
|
|
@ -9,7 +9,7 @@ import moment from 'moment';
|
|||
import uuid from 'uuid';
|
||||
|
||||
import { NewsItem, RawNewsApiItem, RawNewsApiResponse } from './types';
|
||||
import { throwIfNotOk } from '../../hooks/api/api';
|
||||
import { KibanaServices } from '../../lib/kibana';
|
||||
|
||||
/**
|
||||
* Removes the `-SNAPSHOT` that is sometimes appended to the Kibana version,
|
||||
|
@ -90,15 +90,11 @@ export const fetchNews = async ({
|
|||
}: {
|
||||
newsFeedUrl: string;
|
||||
}): Promise<RawNewsApiResponse> => {
|
||||
const response = await fetch(newsFeedUrl, {
|
||||
credentials: 'omit',
|
||||
return KibanaServices.get().http.fetch<RawNewsApiResponse>(newsFeedUrl, {
|
||||
method: 'GET',
|
||||
credentials: 'omit',
|
||||
mode: 'cors',
|
||||
});
|
||||
|
||||
await throwIfNotOk(response);
|
||||
|
||||
return response.json();
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,20 +0,0 @@
|
|||
/*
|
||||
* 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 { throwIfNotOk } from '../../hooks/api/api';
|
||||
import { KibanaServices } from '../../lib/kibana';
|
||||
import { MeApiResponse } from './recent_timelines';
|
||||
|
||||
export const fetchUsername = async () => {
|
||||
const response = await KibanaServices.get().http.fetch<MeApiResponse>('/internal/security/me', {
|
||||
method: 'GET',
|
||||
credentials: 'same-origin',
|
||||
asResponse: true,
|
||||
});
|
||||
|
||||
await throwIfNotOk(response.response);
|
||||
return response.body!.username;
|
||||
};
|
|
@ -22,10 +22,6 @@ import { RecentTimelines } from './recent_timelines';
|
|||
import * as i18n from './translations';
|
||||
import { FilterMode } from './types';
|
||||
|
||||
export interface MeApiResponse {
|
||||
username: string;
|
||||
}
|
||||
|
||||
interface OwnProps {
|
||||
apolloClient: ApolloClient<{}>;
|
||||
filterBy: FilterMode;
|
||||
|
|
|
@ -21,10 +21,6 @@ import { WithHoverActions } from '../with_hover_actions';
|
|||
import { RecentTimelineCounts } from './counts';
|
||||
import * as i18n from './translations';
|
||||
|
||||
export interface MeApiResponse {
|
||||
username: string;
|
||||
}
|
||||
|
||||
export const RecentTimelines = React.memo<{
|
||||
noTimelinesMessage: string;
|
||||
onOpenTimeline: OnOpenTimeline;
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
export class ToasterError extends Error {
|
||||
public readonly messages: string[];
|
||||
|
||||
constructor(messages: string[]) {
|
||||
super(messages[0]);
|
||||
this.name = 'ToasterError';
|
||||
this.messages = messages;
|
||||
}
|
||||
}
|
||||
|
||||
export const isToasterError = (error: unknown): error is ToasterError =>
|
||||
error instanceof ToasterError;
|
|
@ -8,11 +8,13 @@ import { EuiButton, EuiGlobalToastList, EuiGlobalToastListToast as Toast } from
|
|||
import { noop } from 'lodash/fp';
|
||||
import React, { createContext, Dispatch, useContext, useReducer, useState } from 'react';
|
||||
import styled from 'styled-components';
|
||||
import uuid from 'uuid';
|
||||
|
||||
import { ModalAllErrors } from './modal_all_errors';
|
||||
import * as i18n from './translations';
|
||||
|
||||
export * from './utils';
|
||||
export * from './errors';
|
||||
|
||||
export interface AppToast extends Toast {
|
||||
errors?: string[];
|
||||
}
|
||||
|
@ -131,50 +133,3 @@ const ErrorToastContainer = styled.div`
|
|||
`;
|
||||
|
||||
ErrorToastContainer.displayName = 'ErrorToastContainer';
|
||||
|
||||
/**
|
||||
* Displays an error toast for the provided title and message
|
||||
*
|
||||
* @param errorTitle Title of error to display in toaster and modal
|
||||
* @param errorMessages Message to display in error modal when clicked
|
||||
* @param dispatchToaster provided by useStateToaster()
|
||||
*/
|
||||
export const displayErrorToast = (
|
||||
errorTitle: string,
|
||||
errorMessages: string[],
|
||||
dispatchToaster: React.Dispatch<ActionToaster>
|
||||
): void => {
|
||||
const toast: AppToast = {
|
||||
id: uuid.v4(),
|
||||
title: errorTitle,
|
||||
color: 'danger',
|
||||
iconType: 'alert',
|
||||
errors: errorMessages,
|
||||
};
|
||||
dispatchToaster({
|
||||
type: 'addToaster',
|
||||
toast,
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Displays a success toast for the provided title and message
|
||||
*
|
||||
* @param title success message to display in toaster and modal
|
||||
* @param dispatchToaster provided by useStateToaster()
|
||||
*/
|
||||
export const displaySuccessToast = (
|
||||
title: string,
|
||||
dispatchToaster: React.Dispatch<ActionToaster>
|
||||
): void => {
|
||||
const toast: AppToast = {
|
||||
id: uuid.v4(),
|
||||
title,
|
||||
color: 'success',
|
||||
iconType: 'check',
|
||||
};
|
||||
dispatchToaster({
|
||||
type: 'addToaster',
|
||||
toast,
|
||||
});
|
||||
};
|
||||
|
|
|
@ -4,8 +4,12 @@
|
|||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
|
||||
import { isAnError, isToasterError, errorToToaster } from './error_to_toaster';
|
||||
import { ToasterErrors } from '../../../hooks/api/throw_if_not_ok';
|
||||
import { errorToToaster } from './utils';
|
||||
import { ToasterError } from './errors';
|
||||
|
||||
const ApiError = class extends Error {
|
||||
public body: {} = {};
|
||||
};
|
||||
|
||||
describe('error_to_toaster', () => {
|
||||
let dispatchToaster = jest.fn();
|
||||
|
@ -15,8 +19,8 @@ describe('error_to_toaster', () => {
|
|||
});
|
||||
|
||||
describe('#errorToToaster', () => {
|
||||
test('adds a ToastError given multiple toaster errors', () => {
|
||||
const error = new ToasterErrors(['some error 1', 'some error 2']);
|
||||
test('dispatches an error toast given a ToasterError with multiple error messages', () => {
|
||||
const error = new ToasterError(['some error 1', 'some error 2']);
|
||||
errorToToaster({ id: 'some-made-up-id', title: 'some title', error, dispatchToaster });
|
||||
expect(dispatchToaster.mock.calls[0]).toEqual([
|
||||
{
|
||||
|
@ -32,8 +36,8 @@ describe('error_to_toaster', () => {
|
|||
]);
|
||||
});
|
||||
|
||||
test('adds a ToastError given a single toaster errors', () => {
|
||||
const error = new ToasterErrors(['some error 1']);
|
||||
test('dispatches an error toast given a ToasterError with a single error message', () => {
|
||||
const error = new ToasterError(['some error 1']);
|
||||
errorToToaster({ id: 'some-made-up-id', title: 'some title', error, dispatchToaster });
|
||||
expect(dispatchToaster.mock.calls[0]).toEqual([
|
||||
{
|
||||
|
@ -49,7 +53,44 @@ describe('error_to_toaster', () => {
|
|||
]);
|
||||
});
|
||||
|
||||
test('adds a regular Error given a single error', () => {
|
||||
test('dispatches an error toast given an ApiError with a message', () => {
|
||||
const error = new ApiError('Internal Server Error');
|
||||
error.body = { message: 'something bad happened', status_code: 500 };
|
||||
|
||||
errorToToaster({ id: 'some-made-up-id', title: 'some title', error, dispatchToaster });
|
||||
expect(dispatchToaster.mock.calls[0]).toEqual([
|
||||
{
|
||||
toast: {
|
||||
color: 'danger',
|
||||
errors: ['something bad happened'],
|
||||
iconType: 'alert',
|
||||
id: 'some-made-up-id',
|
||||
title: 'some title',
|
||||
},
|
||||
type: 'addToaster',
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
test('dispatches an error toast given an ApiError with no message', () => {
|
||||
const error = new ApiError('Internal Server Error');
|
||||
|
||||
errorToToaster({ id: 'some-made-up-id', title: 'some title', error, dispatchToaster });
|
||||
expect(dispatchToaster.mock.calls[0]).toEqual([
|
||||
{
|
||||
toast: {
|
||||
color: 'danger',
|
||||
errors: ['Internal Server Error'],
|
||||
iconType: 'alert',
|
||||
id: 'some-made-up-id',
|
||||
title: 'some title',
|
||||
},
|
||||
type: 'addToaster',
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
test('dispatches an error toast given a standard Error', () => {
|
||||
const error = new Error('some error 1');
|
||||
errorToToaster({ id: 'some-made-up-id', title: 'some title', error, dispatchToaster });
|
||||
expect(dispatchToaster.mock.calls[0]).toEqual([
|
||||
|
@ -83,44 +124,4 @@ describe('error_to_toaster', () => {
|
|||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#isAnError', () => {
|
||||
test('returns true if given an error object', () => {
|
||||
const error = new Error('some error');
|
||||
expect(isAnError(error)).toEqual(true);
|
||||
});
|
||||
|
||||
test('returns false if given a regular object', () => {
|
||||
expect(isAnError({})).toEqual(false);
|
||||
});
|
||||
|
||||
test('returns false if given a string', () => {
|
||||
expect(isAnError('som string')).toEqual(false);
|
||||
});
|
||||
|
||||
test('returns true if given a toaster error', () => {
|
||||
const error = new ToasterErrors(['some error']);
|
||||
expect(isAnError(error)).toEqual(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#isToasterError', () => {
|
||||
test('returns true if given a ToasterErrors object', () => {
|
||||
const error = new ToasterErrors(['some error']);
|
||||
expect(isToasterError(error)).toEqual(true);
|
||||
});
|
||||
|
||||
test('returns false if given a regular object', () => {
|
||||
expect(isToasterError({})).toEqual(false);
|
||||
});
|
||||
|
||||
test('returns false if given a string', () => {
|
||||
expect(isToasterError('som string')).toEqual(false);
|
||||
});
|
||||
|
||||
test('returns false if given a regular error', () => {
|
||||
const error = new Error('some error');
|
||||
expect(isToasterError(error)).toEqual(false);
|
||||
});
|
||||
});
|
||||
});
|
123
x-pack/legacy/plugins/siem/public/components/toasters/utils.ts
Normal file
123
x-pack/legacy/plugins/siem/public/components/toasters/utils.ts
Normal file
|
@ -0,0 +1,123 @@
|
|||
/*
|
||||
* 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 uuid from 'uuid';
|
||||
import { isError } from 'lodash/fp';
|
||||
|
||||
import { AppToast, ActionToaster } from './';
|
||||
import { isToasterError } from './errors';
|
||||
import { isApiError } from '../../utils/api';
|
||||
|
||||
/**
|
||||
* Displays an error toast for the provided title and message
|
||||
*
|
||||
* @param errorTitle Title of error to display in toaster and modal
|
||||
* @param errorMessages Message to display in error modal when clicked
|
||||
* @param dispatchToaster provided by useStateToaster()
|
||||
*/
|
||||
export const displayErrorToast = (
|
||||
errorTitle: string,
|
||||
errorMessages: string[],
|
||||
dispatchToaster: React.Dispatch<ActionToaster>,
|
||||
id: string = uuid.v4()
|
||||
): void => {
|
||||
const toast: AppToast = {
|
||||
id,
|
||||
title: errorTitle,
|
||||
color: 'danger',
|
||||
iconType: 'alert',
|
||||
errors: errorMessages,
|
||||
};
|
||||
dispatchToaster({
|
||||
type: 'addToaster',
|
||||
toast,
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Displays a success toast for the provided title and message
|
||||
*
|
||||
* @param title success message to display in toaster and modal
|
||||
* @param dispatchToaster provided by useStateToaster()
|
||||
*/
|
||||
export const displaySuccessToast = (
|
||||
title: string,
|
||||
dispatchToaster: React.Dispatch<ActionToaster>,
|
||||
id: string = uuid.v4()
|
||||
): void => {
|
||||
const toast: AppToast = {
|
||||
id,
|
||||
title,
|
||||
color: 'success',
|
||||
iconType: 'check',
|
||||
};
|
||||
dispatchToaster({
|
||||
type: 'addToaster',
|
||||
toast,
|
||||
});
|
||||
};
|
||||
|
||||
export type ErrorToToasterArgs = Partial<AppToast> & {
|
||||
error: unknown;
|
||||
dispatchToaster: React.Dispatch<ActionToaster>;
|
||||
};
|
||||
|
||||
/**
|
||||
* Displays an error toast with messages parsed from the error
|
||||
*
|
||||
* @param title error message to display in toaster and modal
|
||||
* @param error the error from which messages will be parsed
|
||||
* @param dispatchToaster provided by useStateToaster()
|
||||
*/
|
||||
export const errorToToaster = ({
|
||||
id = uuid.v4(),
|
||||
title,
|
||||
error,
|
||||
color = 'danger',
|
||||
iconType = 'alert',
|
||||
dispatchToaster,
|
||||
}: ErrorToToasterArgs) => {
|
||||
let toast: AppToast;
|
||||
|
||||
if (isToasterError(error)) {
|
||||
toast = {
|
||||
id,
|
||||
title,
|
||||
color,
|
||||
iconType,
|
||||
errors: error.messages,
|
||||
};
|
||||
} else if (isApiError(error)) {
|
||||
toast = {
|
||||
id,
|
||||
title,
|
||||
color,
|
||||
iconType,
|
||||
errors: [error.body.message],
|
||||
};
|
||||
} else if (isError(error)) {
|
||||
toast = {
|
||||
id,
|
||||
title,
|
||||
color,
|
||||
iconType,
|
||||
errors: [error.message],
|
||||
};
|
||||
} else {
|
||||
toast = {
|
||||
id,
|
||||
title,
|
||||
color,
|
||||
iconType,
|
||||
errors: ['Network Error'],
|
||||
};
|
||||
}
|
||||
|
||||
dispatchToaster({
|
||||
type: 'addToaster',
|
||||
toast,
|
||||
});
|
||||
};
|
|
@ -13,7 +13,6 @@ import {
|
|||
} from '../../../../../../plugins/case/common/api';
|
||||
import { KibanaServices } from '../../lib/kibana';
|
||||
import { AllCases, Case, Comment, FetchCasesProps, SortFieldCase } from './types';
|
||||
import { throwIfNotOk } from '../../hooks/api/api';
|
||||
import { CASES_URL } from './constants';
|
||||
import {
|
||||
convertToCamelCase,
|
||||
|
@ -28,22 +27,18 @@ const CaseSavedObjectType = 'cases';
|
|||
export const getCase = async (caseId: string, includeComments: boolean = true): Promise<Case> => {
|
||||
const response = await KibanaServices.get().http.fetch<CaseResponse>(`${CASES_URL}/${caseId}`, {
|
||||
method: 'GET',
|
||||
asResponse: true,
|
||||
query: {
|
||||
includeComments,
|
||||
},
|
||||
});
|
||||
await throwIfNotOk(response.response);
|
||||
return convertToCamelCase<CaseResponse, Case>(decodeCaseResponse(response.body));
|
||||
return convertToCamelCase<CaseResponse, Case>(decodeCaseResponse(response));
|
||||
};
|
||||
|
||||
export const getTags = async (): Promise<string[]> => {
|
||||
const response = await KibanaServices.get().http.fetch<string[]>(`${CASES_URL}/tags`, {
|
||||
method: 'GET',
|
||||
asResponse: true,
|
||||
});
|
||||
await throwIfNotOk(response.response);
|
||||
return response.body ?? [];
|
||||
return response ?? [];
|
||||
};
|
||||
|
||||
export const getCases = async ({
|
||||
|
@ -74,20 +69,16 @@ export const getCases = async ({
|
|||
const response = await KibanaServices.get().http.fetch<CasesResponse>(`${CASES_URL}/_find`, {
|
||||
method: 'GET',
|
||||
query,
|
||||
asResponse: true,
|
||||
});
|
||||
await throwIfNotOk(response.response);
|
||||
return convertAllCasesToCamel(decodeCasesResponse(response.body));
|
||||
return convertAllCasesToCamel(decodeCasesResponse(response));
|
||||
};
|
||||
|
||||
export const postCase = async (newCase: CaseRequest): Promise<Case> => {
|
||||
const response = await KibanaServices.get().http.fetch<CaseResponse>(`${CASES_URL}`, {
|
||||
method: 'POST',
|
||||
asResponse: true,
|
||||
body: JSON.stringify(newCase),
|
||||
});
|
||||
await throwIfNotOk(response.response);
|
||||
return convertToCamelCase<CaseResponse, Case>(decodeCaseResponse(response.body));
|
||||
return convertToCamelCase<CaseResponse, Case>(decodeCaseResponse(response));
|
||||
};
|
||||
|
||||
export const patchCase = async (
|
||||
|
@ -97,11 +88,9 @@ export const patchCase = async (
|
|||
): Promise<Case> => {
|
||||
const response = await KibanaServices.get().http.fetch(`${CASES_URL}`, {
|
||||
method: 'PATCH',
|
||||
asResponse: true,
|
||||
body: JSON.stringify({ ...updatedCase, id: caseId, version }),
|
||||
});
|
||||
await throwIfNotOk(response.response);
|
||||
return convertToCamelCase<CaseResponse, Case>(decodeCaseResponse(response.body));
|
||||
return convertToCamelCase<CaseResponse, Case>(decodeCaseResponse(response));
|
||||
};
|
||||
|
||||
export const postComment = async (newComment: CommentRequest, caseId: string): Promise<Comment> => {
|
||||
|
@ -109,12 +98,10 @@ export const postComment = async (newComment: CommentRequest, caseId: string): P
|
|||
`${CASES_URL}/${caseId}/comments`,
|
||||
{
|
||||
method: 'POST',
|
||||
asResponse: true,
|
||||
body: JSON.stringify(newComment),
|
||||
}
|
||||
);
|
||||
await throwIfNotOk(response.response);
|
||||
return convertToCamelCase<CommentResponse, Comment>(decodeCommentResponse(response.body));
|
||||
return convertToCamelCase<CommentResponse, Comment>(decodeCommentResponse(response));
|
||||
};
|
||||
|
||||
export const patchComment = async (
|
||||
|
@ -124,9 +111,7 @@ export const patchComment = async (
|
|||
): Promise<Partial<Comment>> => {
|
||||
const response = await KibanaServices.get().http.fetch<CommentResponse>(`${CASES_URL}/comments`, {
|
||||
method: 'PATCH',
|
||||
asResponse: true,
|
||||
body: JSON.stringify({ comment: commentUpdate, id: commentId, version }),
|
||||
});
|
||||
await throwIfNotOk(response.response);
|
||||
return convertToCamelCase<CommentResponse, Comment>(decodeCommentResponse(response.body));
|
||||
return convertToCamelCase<CommentResponse, Comment>(decodeCommentResponse(response));
|
||||
};
|
||||
|
|
|
@ -9,9 +9,8 @@ import { useEffect, useReducer } from 'react';
|
|||
import { Case } from './types';
|
||||
import { FETCH_INIT, FETCH_FAILURE, FETCH_SUCCESS } from './constants';
|
||||
import { getTypedPayload } from './utils';
|
||||
import { errorToToaster } from '../../components/ml/api/error_to_toaster';
|
||||
import * as i18n from './translations';
|
||||
import { useStateToaster } from '../../components/toasters';
|
||||
import { errorToToaster, useStateToaster } from '../../components/toasters';
|
||||
import { getCase } from './api';
|
||||
|
||||
interface CaseState {
|
||||
|
|
|
@ -7,8 +7,7 @@
|
|||
import { useCallback, useEffect, useReducer } from 'react';
|
||||
import { DEFAULT_TABLE_ACTIVE_PAGE, DEFAULT_TABLE_LIMIT } from './constants';
|
||||
import { AllCases, SortFieldCase, FilterOptions, QueryParams, Case } from './types';
|
||||
import { errorToToaster } from '../../components/ml/api/error_to_toaster';
|
||||
import { useStateToaster } from '../../components/toasters';
|
||||
import { errorToToaster, useStateToaster } from '../../components/toasters';
|
||||
import * as i18n from './translations';
|
||||
import { UpdateByKey } from './use_update_case';
|
||||
import { getCases, patchCase } from './api';
|
||||
|
|
|
@ -5,9 +5,8 @@
|
|||
*/
|
||||
|
||||
import { useEffect, useReducer } from 'react';
|
||||
import { useStateToaster } from '../../components/toasters';
|
||||
import { errorToToaster } from '../../components/ml/api/error_to_toaster';
|
||||
|
||||
import { errorToToaster, useStateToaster } from '../../components/toasters';
|
||||
import { getTags } from './api';
|
||||
import { FETCH_FAILURE, FETCH_INIT, FETCH_SUCCESS } from './constants';
|
||||
import * as i18n from './translations';
|
||||
|
|
|
@ -7,9 +7,7 @@
|
|||
import { useReducer, useCallback } from 'react';
|
||||
|
||||
import { CaseRequest } from '../../../../../../plugins/case/common/api';
|
||||
import { useStateToaster } from '../../components/toasters';
|
||||
import { errorToToaster } from '../../components/ml/api/error_to_toaster';
|
||||
|
||||
import { errorToToaster, useStateToaster } from '../../components/toasters';
|
||||
import { postCase } from './api';
|
||||
import { FETCH_FAILURE, FETCH_INIT, FETCH_SUCCESS } from './constants';
|
||||
import * as i18n from './translations';
|
||||
|
|
|
@ -7,8 +7,7 @@
|
|||
import { useReducer, useCallback } from 'react';
|
||||
|
||||
import { CommentRequest } from '../../../../../../plugins/case/common/api';
|
||||
import { useStateToaster } from '../../components/toasters';
|
||||
import { errorToToaster } from '../../components/ml/api/error_to_toaster';
|
||||
import { errorToToaster, useStateToaster } from '../../components/toasters';
|
||||
|
||||
import { postComment } from './api';
|
||||
import { FETCH_FAILURE, FETCH_INIT, FETCH_SUCCESS } from './constants';
|
||||
|
|
|
@ -7,8 +7,7 @@
|
|||
import { useReducer, useCallback } from 'react';
|
||||
|
||||
import { CaseRequest } from '../../../../../../plugins/case/common/api';
|
||||
import { useStateToaster } from '../../components/toasters';
|
||||
import { errorToToaster } from '../../components/ml/api/error_to_toaster';
|
||||
import { errorToToaster, useStateToaster } from '../../components/toasters';
|
||||
|
||||
import { patchCase } from './api';
|
||||
import { FETCH_FAILURE, FETCH_INIT, FETCH_SUCCESS } from './constants';
|
||||
|
|
|
@ -6,8 +6,7 @@
|
|||
|
||||
import { useReducer, useCallback } from 'react';
|
||||
|
||||
import { useStateToaster } from '../../components/toasters';
|
||||
import { errorToToaster } from '../../components/ml/api/error_to_toaster';
|
||||
import { errorToToaster, useStateToaster } from '../../components/toasters';
|
||||
|
||||
import { patchComment } from './api';
|
||||
import { FETCH_FAILURE, FETCH_INIT, FETCH_SUCCESS } from './constants';
|
||||
|
|
|
@ -18,7 +18,7 @@ import {
|
|||
CommentResponse,
|
||||
CommentResponseRt,
|
||||
} from '../../../../../../plugins/case/common/api';
|
||||
import { ToasterErrors } from '../../hooks/api/throw_if_not_ok';
|
||||
import { ToasterError } from '../../components/toasters';
|
||||
import { AllCases, Case } from './types';
|
||||
|
||||
export const getTypedPayload = <T>(a: unknown): T => a as T;
|
||||
|
@ -53,7 +53,7 @@ export const convertAllCasesToCamel = (snakeCases: CasesResponse): AllCases => (
|
|||
total: snakeCases.total,
|
||||
});
|
||||
|
||||
export const createToasterPlainError = (message: string) => new ToasterErrors([message]);
|
||||
export const createToasterPlainError = (message: string) => new ToasterError([message]);
|
||||
|
||||
export const decodeCaseResponse = (respCase?: CaseResponse) =>
|
||||
pipe(CaseResponseRt.decode(respCase), fold(throwErrors(createToasterPlainError), identity));
|
||||
|
|
|
@ -20,115 +20,46 @@ import {
|
|||
getPrePackagedRulesStatus,
|
||||
} from './api';
|
||||
import { ruleMock, rulesMock } from './mock';
|
||||
import { ToasterErrors } from '../../../hooks/api/throw_if_not_ok';
|
||||
|
||||
const abortCtrl = new AbortController();
|
||||
const mockKibanaServices = KibanaServices.get as jest.Mock;
|
||||
jest.mock('../../../lib/kibana');
|
||||
|
||||
const mockfetchSuccess = (body: unknown, fetchMock?: jest.Mock) => {
|
||||
if (fetchMock) {
|
||||
mockKibanaServices.mockImplementation(() => ({
|
||||
http: {
|
||||
fetch: fetchMock,
|
||||
},
|
||||
}));
|
||||
} else {
|
||||
mockKibanaServices.mockImplementation(() => ({
|
||||
http: {
|
||||
fetch: () => ({
|
||||
response: {
|
||||
ok: true,
|
||||
message: 'success',
|
||||
text: 'success',
|
||||
},
|
||||
body,
|
||||
}),
|
||||
},
|
||||
}));
|
||||
}
|
||||
};
|
||||
|
||||
const mockfetchError = () => {
|
||||
mockKibanaServices.mockImplementation(() => ({
|
||||
http: {
|
||||
fetch: () => ({
|
||||
response: {
|
||||
ok: false,
|
||||
text: () =>
|
||||
JSON.stringify({
|
||||
message: 'super mega error, it is not that bad',
|
||||
}),
|
||||
},
|
||||
body: null,
|
||||
}),
|
||||
},
|
||||
}));
|
||||
};
|
||||
const fetchMock = jest.fn();
|
||||
mockKibanaServices.mockReturnValue({ http: { fetch: fetchMock } });
|
||||
|
||||
describe('Detections Rules API', () => {
|
||||
const fetchMock = jest.fn();
|
||||
describe('addRule', () => {
|
||||
beforeEach(() => {
|
||||
mockKibanaServices.mockClear();
|
||||
fetchMock.mockClear();
|
||||
fetchMock.mockImplementation(() => ({
|
||||
response: {
|
||||
ok: true,
|
||||
message: 'success',
|
||||
text: 'success',
|
||||
},
|
||||
body: ruleMock,
|
||||
}));
|
||||
fetchMock.mockResolvedValue(ruleMock);
|
||||
});
|
||||
test('check parameter url, body', async () => {
|
||||
mockfetchSuccess(null, fetchMock);
|
||||
|
||||
test('check parameter url, body', async () => {
|
||||
await addRule({ rule: ruleMock, signal: abortCtrl.signal });
|
||||
expect(fetchMock).toHaveBeenCalledWith('/api/detection_engine/rules', {
|
||||
asResponse: true,
|
||||
body:
|
||||
'{"description":"some desc","enabled":true,"false_positives":[],"filters":[],"from":"now-360s","index":["apm-*-transaction*","auditbeat-*","endgame-*","filebeat-*","packetbeat-*","winlogbeat-*"],"interval":"5m","rule_id":"bbd3106e-b4b5-4d7c-a1a2-47531d6a2baf","language":"kuery","risk_score":75,"name":"Test rule","query":"user.email: \'root@elastic.co\'","references":[],"severity":"high","tags":["APM"],"to":"now","type":"query","threat":[]}',
|
||||
method: 'POST',
|
||||
signal: abortCtrl.signal,
|
||||
});
|
||||
});
|
||||
|
||||
test('happy path', async () => {
|
||||
mockfetchSuccess(ruleMock);
|
||||
const ruleResp = await addRule({ rule: ruleMock, signal: abortCtrl.signal });
|
||||
expect(ruleResp).toEqual(ruleMock);
|
||||
});
|
||||
test('unhappy path', async () => {
|
||||
mockfetchError();
|
||||
try {
|
||||
await addRule({ rule: ruleMock, signal: abortCtrl.signal });
|
||||
} catch (exp) {
|
||||
expect(exp).toBeInstanceOf(ToasterErrors);
|
||||
expect(exp.message).toEqual('super mega error, it is not that bad');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('fetchRules', () => {
|
||||
beforeEach(() => {
|
||||
mockKibanaServices.mockClear();
|
||||
fetchMock.mockClear();
|
||||
fetchMock.mockImplementation(() => ({
|
||||
response: {
|
||||
ok: true,
|
||||
message: 'success',
|
||||
text: 'success',
|
||||
},
|
||||
body: rulesMock,
|
||||
}));
|
||||
fetchMock.mockResolvedValue(rulesMock);
|
||||
});
|
||||
|
||||
test('check parameter url, query without any options', async () => {
|
||||
mockfetchSuccess(null, fetchMock);
|
||||
|
||||
await fetchRules({ signal: abortCtrl.signal });
|
||||
expect(fetchMock).toHaveBeenCalledWith('/api/detection_engine/rules/_find', {
|
||||
asResponse: true,
|
||||
method: 'GET',
|
||||
query: {
|
||||
page: 1,
|
||||
|
@ -141,8 +72,6 @@ describe('Detections Rules API', () => {
|
|||
});
|
||||
|
||||
test('check parameter url, query with a filter', async () => {
|
||||
mockfetchSuccess(null, fetchMock);
|
||||
|
||||
await fetchRules({
|
||||
filterOptions: {
|
||||
filter: 'hello world',
|
||||
|
@ -154,8 +83,8 @@ describe('Detections Rules API', () => {
|
|||
},
|
||||
signal: abortCtrl.signal,
|
||||
});
|
||||
|
||||
expect(fetchMock).toHaveBeenCalledWith('/api/detection_engine/rules/_find', {
|
||||
asResponse: true,
|
||||
method: 'GET',
|
||||
query: {
|
||||
filter: 'alert.attributes.name: hello world',
|
||||
|
@ -169,8 +98,6 @@ describe('Detections Rules API', () => {
|
|||
});
|
||||
|
||||
test('check parameter url, query with showCustomRules', async () => {
|
||||
mockfetchSuccess(null, fetchMock);
|
||||
|
||||
await fetchRules({
|
||||
filterOptions: {
|
||||
filter: '',
|
||||
|
@ -182,8 +109,8 @@ describe('Detections Rules API', () => {
|
|||
},
|
||||
signal: abortCtrl.signal,
|
||||
});
|
||||
|
||||
expect(fetchMock).toHaveBeenCalledWith('/api/detection_engine/rules/_find', {
|
||||
asResponse: true,
|
||||
method: 'GET',
|
||||
query: {
|
||||
filter: 'alert.attributes.tags: "__internal_immutable:false"',
|
||||
|
@ -197,8 +124,6 @@ describe('Detections Rules API', () => {
|
|||
});
|
||||
|
||||
test('check parameter url, query with showElasticRules', async () => {
|
||||
mockfetchSuccess(null, fetchMock);
|
||||
|
||||
await fetchRules({
|
||||
filterOptions: {
|
||||
filter: '',
|
||||
|
@ -210,8 +135,8 @@ describe('Detections Rules API', () => {
|
|||
},
|
||||
signal: abortCtrl.signal,
|
||||
});
|
||||
|
||||
expect(fetchMock).toHaveBeenCalledWith('/api/detection_engine/rules/_find', {
|
||||
asResponse: true,
|
||||
method: 'GET',
|
||||
query: {
|
||||
filter: 'alert.attributes.tags: "__internal_immutable:true"',
|
||||
|
@ -225,8 +150,6 @@ describe('Detections Rules API', () => {
|
|||
});
|
||||
|
||||
test('check parameter url, query with tags', async () => {
|
||||
mockfetchSuccess(null, fetchMock);
|
||||
|
||||
await fetchRules({
|
||||
filterOptions: {
|
||||
filter: '',
|
||||
|
@ -238,8 +161,8 @@ describe('Detections Rules API', () => {
|
|||
},
|
||||
signal: abortCtrl.signal,
|
||||
});
|
||||
|
||||
expect(fetchMock).toHaveBeenCalledWith('/api/detection_engine/rules/_find', {
|
||||
asResponse: true,
|
||||
method: 'GET',
|
||||
query: {
|
||||
filter: 'alert.attributes.tags: hello AND alert.attributes.tags: world',
|
||||
|
@ -253,8 +176,6 @@ describe('Detections Rules API', () => {
|
|||
});
|
||||
|
||||
test('check parameter url, query with all options', async () => {
|
||||
mockfetchSuccess(null, fetchMock);
|
||||
|
||||
await fetchRules({
|
||||
filterOptions: {
|
||||
filter: 'ruleName',
|
||||
|
@ -267,7 +188,6 @@ describe('Detections Rules API', () => {
|
|||
signal: abortCtrl.signal,
|
||||
});
|
||||
expect(fetchMock).toHaveBeenCalledWith('/api/detection_engine/rules/_find', {
|
||||
asResponse: true,
|
||||
method: 'GET',
|
||||
query: {
|
||||
filter:
|
||||
|
@ -282,41 +202,20 @@ describe('Detections Rules API', () => {
|
|||
});
|
||||
|
||||
test('happy path', async () => {
|
||||
mockfetchSuccess(rulesMock);
|
||||
|
||||
const rulesResp = await fetchRules({ signal: abortCtrl.signal });
|
||||
expect(rulesResp).toEqual(rulesMock);
|
||||
});
|
||||
test('unhappy path', async () => {
|
||||
mockfetchError();
|
||||
try {
|
||||
await fetchRules({ signal: abortCtrl.signal });
|
||||
} catch (exp) {
|
||||
expect(exp).toBeInstanceOf(ToasterErrors);
|
||||
expect(exp.message).toEqual('super mega error, it is not that bad');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('fetchRuleById', () => {
|
||||
beforeEach(() => {
|
||||
mockKibanaServices.mockClear();
|
||||
fetchMock.mockClear();
|
||||
fetchMock.mockImplementation(() => ({
|
||||
response: {
|
||||
ok: true,
|
||||
message: 'success',
|
||||
text: 'success',
|
||||
},
|
||||
body: ruleMock,
|
||||
}));
|
||||
fetchMock.mockResolvedValue(ruleMock);
|
||||
});
|
||||
test('check parameter url, query', async () => {
|
||||
mockfetchSuccess(null, fetchMock);
|
||||
|
||||
test('check parameter url, query', async () => {
|
||||
await fetchRuleById({ id: 'mySuperRuleId', signal: abortCtrl.signal });
|
||||
expect(fetchMock).toHaveBeenCalledWith('/api/detection_engine/rules', {
|
||||
asResponse: true,
|
||||
query: {
|
||||
id: 'mySuperRuleId',
|
||||
},
|
||||
|
@ -324,192 +223,102 @@ describe('Detections Rules API', () => {
|
|||
signal: abortCtrl.signal,
|
||||
});
|
||||
});
|
||||
|
||||
test('happy path', async () => {
|
||||
mockfetchSuccess(ruleMock);
|
||||
const ruleResp = await fetchRuleById({ id: 'mySuperRuleId', signal: abortCtrl.signal });
|
||||
expect(ruleResp).toEqual(ruleMock);
|
||||
});
|
||||
test('unhappy path', async () => {
|
||||
mockfetchError();
|
||||
try {
|
||||
await fetchRuleById({ id: 'mySuperRuleId', signal: abortCtrl.signal });
|
||||
} catch (exp) {
|
||||
expect(exp).toBeInstanceOf(ToasterErrors);
|
||||
expect(exp.message).toEqual('super mega error, it is not that bad');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('enableRules', () => {
|
||||
beforeEach(() => {
|
||||
mockKibanaServices.mockClear();
|
||||
fetchMock.mockClear();
|
||||
fetchMock.mockImplementation(() => ({
|
||||
response: {
|
||||
ok: true,
|
||||
message: 'success',
|
||||
text: 'success',
|
||||
},
|
||||
body: ruleMock,
|
||||
}));
|
||||
fetchMock.mockResolvedValue(rulesMock);
|
||||
});
|
||||
test('check parameter url, body when enabling rules', async () => {
|
||||
mockfetchSuccess(null, fetchMock);
|
||||
|
||||
test('check parameter url, body when enabling rules', async () => {
|
||||
await enableRules({ ids: ['mySuperRuleId', 'mySuperRuleId_II'], enabled: true });
|
||||
expect(fetchMock).toHaveBeenCalledWith('/api/detection_engine/rules/_bulk_update', {
|
||||
asResponse: true,
|
||||
body: '[{"id":"mySuperRuleId","enabled":true},{"id":"mySuperRuleId_II","enabled":true}]',
|
||||
method: 'PATCH',
|
||||
});
|
||||
});
|
||||
test('check parameter url, body when disabling rules', async () => {
|
||||
mockfetchSuccess(null, fetchMock);
|
||||
|
||||
await enableRules({ ids: ['mySuperRuleId', 'mySuperRuleId_II'], enabled: false });
|
||||
expect(fetchMock).toHaveBeenCalledWith('/api/detection_engine/rules/_bulk_update', {
|
||||
asResponse: true,
|
||||
body: '[{"id":"mySuperRuleId","enabled":false},{"id":"mySuperRuleId_II","enabled":false}]',
|
||||
method: 'PATCH',
|
||||
});
|
||||
});
|
||||
test('happy path', async () => {
|
||||
mockfetchSuccess(rulesMock.data);
|
||||
const ruleResp = await enableRules({
|
||||
ids: ['mySuperRuleId', 'mySuperRuleId_II'],
|
||||
enabled: true,
|
||||
});
|
||||
expect(ruleResp).toEqual(rulesMock.data);
|
||||
});
|
||||
test('unhappy path', async () => {
|
||||
mockfetchError();
|
||||
try {
|
||||
await enableRules({ ids: ['mySuperRuleId', 'mySuperRuleId_II'], enabled: true });
|
||||
} catch (exp) {
|
||||
expect(exp).toBeInstanceOf(ToasterErrors);
|
||||
expect(exp.message).toEqual('super mega error, it is not that bad');
|
||||
}
|
||||
expect(ruleResp).toEqual(rulesMock);
|
||||
});
|
||||
});
|
||||
|
||||
describe('deleteRules', () => {
|
||||
beforeEach(() => {
|
||||
mockKibanaServices.mockClear();
|
||||
fetchMock.mockClear();
|
||||
fetchMock.mockImplementation(() => ({
|
||||
response: {
|
||||
ok: true,
|
||||
message: 'success',
|
||||
text: 'success',
|
||||
},
|
||||
body: ruleMock,
|
||||
}));
|
||||
fetchMock.mockResolvedValue(rulesMock);
|
||||
});
|
||||
test('check parameter url, body when deleting rules', async () => {
|
||||
mockfetchSuccess(null, fetchMock);
|
||||
|
||||
test('check parameter url, body when deleting rules', async () => {
|
||||
await deleteRules({ ids: ['mySuperRuleId', 'mySuperRuleId_II'] });
|
||||
expect(fetchMock).toHaveBeenCalledWith('/api/detection_engine/rules/_bulk_delete', {
|
||||
asResponse: true,
|
||||
body: '[{"id":"mySuperRuleId"},{"id":"mySuperRuleId_II"}]',
|
||||
method: 'DELETE',
|
||||
});
|
||||
});
|
||||
|
||||
test('happy path', async () => {
|
||||
mockfetchSuccess(ruleMock);
|
||||
const ruleResp = await deleteRules({
|
||||
ids: ['mySuperRuleId', 'mySuperRuleId_II'],
|
||||
});
|
||||
expect(ruleResp).toEqual(ruleMock);
|
||||
});
|
||||
test('unhappy path', async () => {
|
||||
mockfetchError();
|
||||
try {
|
||||
await deleteRules({ ids: ['mySuperRuleId', 'mySuperRuleId_II'] });
|
||||
} catch (exp) {
|
||||
expect(exp).toBeInstanceOf(ToasterErrors);
|
||||
expect(exp.message).toEqual('super mega error, it is not that bad');
|
||||
}
|
||||
expect(ruleResp).toEqual(rulesMock);
|
||||
});
|
||||
});
|
||||
|
||||
describe('duplicateRules', () => {
|
||||
beforeEach(() => {
|
||||
mockKibanaServices.mockClear();
|
||||
fetchMock.mockClear();
|
||||
fetchMock.mockImplementation(() => ({
|
||||
response: {
|
||||
ok: true,
|
||||
message: 'success',
|
||||
text: 'success',
|
||||
},
|
||||
body: ruleMock,
|
||||
}));
|
||||
fetchMock.mockResolvedValue(rulesMock);
|
||||
});
|
||||
test('check parameter url, body when duplicating rules', async () => {
|
||||
mockfetchSuccess(null, fetchMock);
|
||||
|
||||
test('check parameter url, body when duplicating rules', async () => {
|
||||
await duplicateRules({ rules: rulesMock.data });
|
||||
expect(fetchMock).toHaveBeenCalledWith('/api/detection_engine/rules/_bulk_create', {
|
||||
asResponse: true,
|
||||
body:
|
||||
'[{"description":"Elastic Endpoint detected Credential Dumping. Click the Elastic Endpoint icon in the event.module column or the link in the rule.reference column in the External Alerts tab of the SIEM Detections page for additional information.","enabled":false,"false_positives":[],"from":"now-660s","index":["endgame-*"],"interval":"10m","language":"kuery","output_index":".siem-signals-default","max_signals":100,"risk_score":73,"name":"Credential Dumping - Detected - Elastic Endpoint [Duplicate]","query":"event.kind:alert and event.module:endgame and event.action:cred_theft_event and endgame.metadata.type:detection","filters":[],"references":[],"severity":"high","tags":["Elastic","Endpoint"],"to":"now","type":"query","threat":[],"version":1},{"description":"Elastic Endpoint detected an Adversary Behavior. Click the Elastic Endpoint icon in the event.module column or the link in the rule.reference column in the External Alerts tab of the SIEM Detections page for additional information.","enabled":false,"false_positives":[],"from":"now-660s","index":["endgame-*"],"interval":"10m","language":"kuery","output_index":".siem-signals-default","max_signals":100,"risk_score":47,"name":"Adversary Behavior - Detected - Elastic Endpoint [Duplicate]","query":"event.kind:alert and event.module:endgame and event.action:rules_engine_event","filters":[],"references":[],"severity":"medium","tags":["Elastic","Endpoint"],"to":"now","type":"query","threat":[],"version":1}]',
|
||||
method: 'POST',
|
||||
});
|
||||
});
|
||||
|
||||
test('happy path', async () => {
|
||||
mockfetchSuccess(rulesMock.data);
|
||||
const ruleResp = await duplicateRules({ rules: rulesMock.data });
|
||||
expect(ruleResp).toEqual(rulesMock.data);
|
||||
});
|
||||
test('unhappy path', async () => {
|
||||
mockfetchError();
|
||||
try {
|
||||
await duplicateRules({ rules: rulesMock.data });
|
||||
} catch (exp) {
|
||||
expect(exp).toBeInstanceOf(ToasterErrors);
|
||||
expect(exp.message).toEqual('super mega error, it is not that bad');
|
||||
}
|
||||
expect(ruleResp).toEqual(rulesMock);
|
||||
});
|
||||
});
|
||||
|
||||
describe('createPrepackagedRules', () => {
|
||||
beforeEach(() => {
|
||||
mockKibanaServices.mockClear();
|
||||
fetchMock.mockClear();
|
||||
fetchMock.mockImplementation(() => ({
|
||||
response: {
|
||||
ok: true,
|
||||
message: 'success',
|
||||
text: 'success',
|
||||
},
|
||||
body: ruleMock,
|
||||
}));
|
||||
fetchMock.mockResolvedValue('unknown');
|
||||
});
|
||||
test('check parameter url when creating pre-packaged rules', async () => {
|
||||
mockfetchSuccess(null, fetchMock);
|
||||
|
||||
test('check parameter url when creating pre-packaged rules', async () => {
|
||||
await createPrepackagedRules({ signal: abortCtrl.signal });
|
||||
expect(fetchMock).toHaveBeenCalledWith('/api/detection_engine/rules/prepackaged', {
|
||||
asResponse: true,
|
||||
signal: abortCtrl.signal,
|
||||
method: 'PUT',
|
||||
});
|
||||
});
|
||||
test('happy path', async () => {
|
||||
mockfetchSuccess(true);
|
||||
const resp = await createPrepackagedRules({ signal: abortCtrl.signal });
|
||||
expect(resp).toEqual(true);
|
||||
});
|
||||
test('unhappy path', async () => {
|
||||
mockfetchError();
|
||||
try {
|
||||
await createPrepackagedRules({ signal: abortCtrl.signal });
|
||||
} catch (exp) {
|
||||
expect(exp).toBeInstanceOf(ToasterErrors);
|
||||
expect(exp.message).toEqual('super mega error, it is not that bad');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('importRules', () => {
|
||||
|
@ -525,23 +334,15 @@ describe('Detections Rules API', () => {
|
|||
} as File;
|
||||
const formData = new FormData();
|
||||
formData.append('file', fileToImport);
|
||||
|
||||
beforeEach(() => {
|
||||
mockKibanaServices.mockClear();
|
||||
fetchMock.mockClear();
|
||||
fetchMock.mockImplementation(() => ({
|
||||
response: {
|
||||
ok: true,
|
||||
message: 'success',
|
||||
text: 'success',
|
||||
},
|
||||
body: ruleMock,
|
||||
}));
|
||||
fetchMock.mockResolvedValue('unknown');
|
||||
});
|
||||
|
||||
test('check parameter url, body and query when importing rules', async () => {
|
||||
mockfetchSuccess(null, fetchMock);
|
||||
await importRules({ fileToImport, signal: abortCtrl.signal });
|
||||
expect(fetchMock).toHaveBeenCalledWith('/api/detection_engine/rules/_import', {
|
||||
asResponse: true,
|
||||
signal: abortCtrl.signal,
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
|
@ -555,11 +356,8 @@ describe('Detections Rules API', () => {
|
|||
});
|
||||
|
||||
test('check parameter url, body and query when importing rules with overwrite', async () => {
|
||||
mockfetchSuccess(null, fetchMock);
|
||||
|
||||
await importRules({ fileToImport, overwrite: true, signal: abortCtrl.signal });
|
||||
expect(fetchMock).toHaveBeenCalledWith('/api/detection_engine/rules/_import', {
|
||||
asResponse: true,
|
||||
signal: abortCtrl.signal,
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
|
@ -573,7 +371,7 @@ describe('Detections Rules API', () => {
|
|||
});
|
||||
|
||||
test('happy path', async () => {
|
||||
mockfetchSuccess({
|
||||
fetchMock.mockResolvedValue({
|
||||
success: true,
|
||||
success_count: 33,
|
||||
errors: [],
|
||||
|
@ -585,40 +383,29 @@ describe('Detections Rules API', () => {
|
|||
errors: [],
|
||||
});
|
||||
});
|
||||
|
||||
test('unhappy path', async () => {
|
||||
mockfetchError();
|
||||
try {
|
||||
await importRules({ fileToImport, signal: abortCtrl.signal });
|
||||
} catch (exp) {
|
||||
expect(exp).toBeInstanceOf(ToasterErrors);
|
||||
expect(exp.message).toEqual('super mega error, it is not that bad');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('exportRules', () => {
|
||||
const blob: Blob = {
|
||||
size: 89,
|
||||
type: 'json',
|
||||
arrayBuffer: jest.fn(),
|
||||
slice: jest.fn(),
|
||||
stream: jest.fn(),
|
||||
text: jest.fn(),
|
||||
} as Blob;
|
||||
|
||||
beforeEach(() => {
|
||||
mockKibanaServices.mockClear();
|
||||
fetchMock.mockClear();
|
||||
fetchMock.mockImplementation(() => ({
|
||||
response: {
|
||||
ok: true,
|
||||
message: 'success',
|
||||
text: 'success',
|
||||
},
|
||||
body: ruleMock,
|
||||
}));
|
||||
fetchMock.mockResolvedValue(blob);
|
||||
});
|
||||
|
||||
test('check parameter url, body and query when exporting rules', async () => {
|
||||
mockfetchSuccess(null, fetchMock);
|
||||
await exportRules({
|
||||
ruleIds: ['mySuperRuleId', 'mySuperRuleId_II'],
|
||||
signal: abortCtrl.signal,
|
||||
});
|
||||
expect(fetchMock).toHaveBeenCalledWith('/api/detection_engine/rules/_export', {
|
||||
asResponse: true,
|
||||
signal: abortCtrl.signal,
|
||||
method: 'POST',
|
||||
body: '{"objects":[{"rule_id":"mySuperRuleId"},{"rule_id":"mySuperRuleId_II"}]}',
|
||||
|
@ -630,14 +417,12 @@ describe('Detections Rules API', () => {
|
|||
});
|
||||
|
||||
test('check parameter url, body and query when exporting rules with excludeExportDetails', async () => {
|
||||
mockfetchSuccess(null, fetchMock);
|
||||
await exportRules({
|
||||
excludeExportDetails: true,
|
||||
ruleIds: ['mySuperRuleId', 'mySuperRuleId_II'],
|
||||
signal: abortCtrl.signal,
|
||||
});
|
||||
expect(fetchMock).toHaveBeenCalledWith('/api/detection_engine/rules/_export', {
|
||||
asResponse: true,
|
||||
signal: abortCtrl.signal,
|
||||
method: 'POST',
|
||||
body: '{"objects":[{"rule_id":"mySuperRuleId"},{"rule_id":"mySuperRuleId_II"}]}',
|
||||
|
@ -649,14 +434,12 @@ describe('Detections Rules API', () => {
|
|||
});
|
||||
|
||||
test('check parameter url, body and query when exporting rules with fileName', async () => {
|
||||
mockfetchSuccess(null, fetchMock);
|
||||
await exportRules({
|
||||
filename: 'myFileName.ndjson',
|
||||
ruleIds: ['mySuperRuleId', 'mySuperRuleId_II'],
|
||||
signal: abortCtrl.signal,
|
||||
});
|
||||
expect(fetchMock).toHaveBeenCalledWith('/api/detection_engine/rules/_export', {
|
||||
asResponse: true,
|
||||
signal: abortCtrl.signal,
|
||||
method: 'POST',
|
||||
body: '{"objects":[{"rule_id":"mySuperRuleId"},{"rule_id":"mySuperRuleId_II"}]}',
|
||||
|
@ -668,7 +451,6 @@ describe('Detections Rules API', () => {
|
|||
});
|
||||
|
||||
test('check parameter url, body and query when exporting rules with all options', async () => {
|
||||
mockfetchSuccess(null, fetchMock);
|
||||
await exportRules({
|
||||
excludeExportDetails: true,
|
||||
filename: 'myFileName.ndjson',
|
||||
|
@ -676,7 +458,6 @@ describe('Detections Rules API', () => {
|
|||
signal: abortCtrl.signal,
|
||||
});
|
||||
expect(fetchMock).toHaveBeenCalledWith('/api/detection_engine/rules/_export', {
|
||||
asResponse: true,
|
||||
signal: abortCtrl.signal,
|
||||
method: 'POST',
|
||||
body: '{"objects":[{"rule_id":"mySuperRuleId"},{"rule_id":"mySuperRuleId_II"}]}',
|
||||
|
@ -688,55 +469,38 @@ describe('Detections Rules API', () => {
|
|||
});
|
||||
|
||||
test('happy path', async () => {
|
||||
const blob: Blob = {
|
||||
size: 89,
|
||||
type: 'json',
|
||||
arrayBuffer: jest.fn(),
|
||||
slice: jest.fn(),
|
||||
stream: jest.fn(),
|
||||
text: jest.fn(),
|
||||
} as Blob;
|
||||
mockfetchSuccess(blob);
|
||||
const resp = await exportRules({
|
||||
ruleIds: ['mySuperRuleId', 'mySuperRuleId_II'],
|
||||
signal: abortCtrl.signal,
|
||||
});
|
||||
expect(resp).toEqual(blob);
|
||||
});
|
||||
|
||||
test('unhappy path', async () => {
|
||||
mockfetchError();
|
||||
try {
|
||||
await exportRules({
|
||||
ruleIds: ['mySuperRuleId', 'mySuperRuleId_II'],
|
||||
signal: abortCtrl.signal,
|
||||
});
|
||||
} catch (exp) {
|
||||
expect(exp).toBeInstanceOf(ToasterErrors);
|
||||
expect(exp.message).toEqual('super mega error, it is not that bad');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('getRuleStatusById', () => {
|
||||
beforeEach(() => {
|
||||
mockKibanaServices.mockClear();
|
||||
fetchMock.mockClear();
|
||||
fetchMock.mockImplementation(() => ({
|
||||
response: {
|
||||
ok: true,
|
||||
message: 'success',
|
||||
text: 'success',
|
||||
const statusMock = {
|
||||
myRule: {
|
||||
current_status: {
|
||||
alert_id: 'alertId',
|
||||
status_date: 'mm/dd/yyyyTHH:MM:sssz',
|
||||
status: 'succeeded',
|
||||
last_failure_at: null,
|
||||
last_success_at: 'mm/dd/yyyyTHH:MM:sssz',
|
||||
last_failure_message: null,
|
||||
last_success_message: 'it is a success',
|
||||
},
|
||||
body: ruleMock,
|
||||
}));
|
||||
});
|
||||
test('check parameter url, query', async () => {
|
||||
mockfetchSuccess(null, fetchMock);
|
||||
failures: [],
|
||||
},
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
fetchMock.mockClear();
|
||||
fetchMock.mockResolvedValue(statusMock);
|
||||
});
|
||||
|
||||
test('check parameter url, query', async () => {
|
||||
await getRuleStatusById({ id: 'mySuperRuleId', signal: abortCtrl.signal });
|
||||
expect(fetchMock).toHaveBeenCalledWith('/api/detection_engine/rules/_find_statuses', {
|
||||
asResponse: true,
|
||||
query: {
|
||||
ids: '["mySuperRuleId"]',
|
||||
},
|
||||
|
@ -744,117 +508,54 @@ describe('Detections Rules API', () => {
|
|||
signal: abortCtrl.signal,
|
||||
});
|
||||
});
|
||||
|
||||
test('happy path', async () => {
|
||||
const statusMock = {
|
||||
myRule: {
|
||||
current_status: {
|
||||
alert_id: 'alertId',
|
||||
status_date: 'mm/dd/yyyyTHH:MM:sssz',
|
||||
status: 'succeeded',
|
||||
last_failure_at: null,
|
||||
last_success_at: 'mm/dd/yyyyTHH:MM:sssz',
|
||||
last_failure_message: null,
|
||||
last_success_message: 'it is a success',
|
||||
},
|
||||
failures: [],
|
||||
},
|
||||
};
|
||||
mockfetchSuccess(statusMock);
|
||||
const ruleResp = await getRuleStatusById({ id: 'mySuperRuleId', signal: abortCtrl.signal });
|
||||
expect(ruleResp).toEqual(statusMock);
|
||||
});
|
||||
test('unhappy path', async () => {
|
||||
mockfetchError();
|
||||
try {
|
||||
await getRuleStatusById({ id: 'mySuperRuleId', signal: abortCtrl.signal });
|
||||
} catch (exp) {
|
||||
expect(exp).toBeInstanceOf(ToasterErrors);
|
||||
expect(exp.message).toEqual('super mega error, it is not that bad');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('fetchTags', () => {
|
||||
beforeEach(() => {
|
||||
mockKibanaServices.mockClear();
|
||||
fetchMock.mockClear();
|
||||
fetchMock.mockImplementation(() => ({
|
||||
response: {
|
||||
ok: true,
|
||||
message: 'success',
|
||||
text: 'success',
|
||||
},
|
||||
body: ruleMock,
|
||||
}));
|
||||
fetchMock.mockResolvedValue(['some', 'tags']);
|
||||
});
|
||||
test('check parameter url when fetching tags', async () => {
|
||||
mockfetchSuccess(null, fetchMock);
|
||||
|
||||
test('check parameter url when fetching tags', async () => {
|
||||
await fetchTags({ signal: abortCtrl.signal });
|
||||
expect(fetchMock).toHaveBeenCalledWith('/api/detection_engine/tags', {
|
||||
asResponse: true,
|
||||
signal: abortCtrl.signal,
|
||||
method: 'GET',
|
||||
});
|
||||
});
|
||||
|
||||
test('happy path', async () => {
|
||||
mockfetchSuccess(['hello', 'tags']);
|
||||
const resp = await fetchTags({ signal: abortCtrl.signal });
|
||||
expect(resp).toEqual(['hello', 'tags']);
|
||||
});
|
||||
test('unhappy path', async () => {
|
||||
mockfetchError();
|
||||
try {
|
||||
await fetchTags({ signal: abortCtrl.signal });
|
||||
} catch (exp) {
|
||||
expect(exp).toBeInstanceOf(ToasterErrors);
|
||||
expect(exp.message).toEqual('super mega error, it is not that bad');
|
||||
}
|
||||
expect(resp).toEqual(['some', 'tags']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getPrePackagedRulesStatus', () => {
|
||||
const prePackagedRulesStatus = {
|
||||
rules_custom_installed: 33,
|
||||
rules_installed: 12,
|
||||
rules_not_installed: 0,
|
||||
rules_not_updated: 2,
|
||||
};
|
||||
beforeEach(() => {
|
||||
mockKibanaServices.mockClear();
|
||||
fetchMock.mockClear();
|
||||
fetchMock.mockImplementation(() => ({
|
||||
response: {
|
||||
ok: true,
|
||||
message: 'success',
|
||||
text: 'success',
|
||||
},
|
||||
body: ruleMock,
|
||||
}));
|
||||
fetchMock.mockResolvedValue(prePackagedRulesStatus);
|
||||
});
|
||||
test('check parameter url when fetching tags', async () => {
|
||||
mockfetchSuccess(null, fetchMock);
|
||||
|
||||
await getPrePackagedRulesStatus({ signal: abortCtrl.signal });
|
||||
expect(fetchMock).toHaveBeenCalledWith('/api/detection_engine/rules/prepackaged/_status', {
|
||||
asResponse: true,
|
||||
signal: abortCtrl.signal,
|
||||
method: 'GET',
|
||||
});
|
||||
});
|
||||
test('happy path', async () => {
|
||||
const prePackagesRulesStatus = {
|
||||
rules_custom_installed: 33,
|
||||
rules_installed: 12,
|
||||
rules_not_installed: 0,
|
||||
rules_not_updated: 2,
|
||||
};
|
||||
mockfetchSuccess(prePackagesRulesStatus);
|
||||
const resp = await getPrePackagedRulesStatus({ signal: abortCtrl.signal });
|
||||
expect(resp).toEqual(prePackagesRulesStatus);
|
||||
});
|
||||
test('unhappy path', async () => {
|
||||
mockfetchError();
|
||||
try {
|
||||
await getPrePackagedRulesStatus({ signal: abortCtrl.signal });
|
||||
} catch (exp) {
|
||||
expect(exp).toBeInstanceOf(ToasterErrors);
|
||||
expect(exp.message).toEqual('super mega error, it is not that bad');
|
||||
}
|
||||
expect(resp).toEqual(prePackagedRulesStatus);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -17,13 +17,12 @@ import {
|
|||
BasicFetchProps,
|
||||
ImportRulesProps,
|
||||
ExportRulesProps,
|
||||
RuleError,
|
||||
RuleStatusResponse,
|
||||
ImportRulesResponse,
|
||||
PrePackagedRulesStatusResponse,
|
||||
BulkRuleResponse,
|
||||
} from './types';
|
||||
import { KibanaServices } from '../../../lib/kibana';
|
||||
import { throwIfNotOk } from '../../../hooks/api/api';
|
||||
import {
|
||||
DETECTION_ENGINE_RULES_URL,
|
||||
DETECTION_ENGINE_PREPACKAGED_URL,
|
||||
|
@ -38,19 +37,16 @@ import * as i18n from '../../../pages/detection_engine/rules/translations';
|
|||
*
|
||||
* @param rule to add
|
||||
* @param signal to cancel request
|
||||
*
|
||||
* @throws An error if response is not OK
|
||||
*/
|
||||
export const addRule = async ({ rule, signal }: AddRulesProps): Promise<NewRule> => {
|
||||
const response = await KibanaServices.get().http.fetch<NewRule>(DETECTION_ENGINE_RULES_URL, {
|
||||
export const addRule = async ({ rule, signal }: AddRulesProps): Promise<NewRule> =>
|
||||
KibanaServices.get().http.fetch<NewRule>(DETECTION_ENGINE_RULES_URL, {
|
||||
method: rule.id != null ? 'PUT' : 'POST',
|
||||
body: JSON.stringify(rule),
|
||||
asResponse: true,
|
||||
signal,
|
||||
});
|
||||
|
||||
await throwIfNotOk(response.response);
|
||||
return response.body!;
|
||||
};
|
||||
|
||||
/**
|
||||
* Fetches all rules from the Detection Engine API
|
||||
*
|
||||
|
@ -58,6 +54,7 @@ export const addRule = async ({ rule, signal }: AddRulesProps): Promise<NewRule>
|
|||
* @param pagination desired pagination options (e.g. page/perPage)
|
||||
* @param signal to cancel request
|
||||
*
|
||||
* @throws An error if response is not OK
|
||||
*/
|
||||
export const fetchRules = async ({
|
||||
filterOptions = {
|
||||
|
@ -94,18 +91,14 @@ export const fetchRules = async ({
|
|||
...(filters.length ? { filter: filters.join(' AND ') } : {}),
|
||||
};
|
||||
|
||||
const response = await KibanaServices.get().http.fetch<FetchRulesResponse>(
|
||||
return KibanaServices.get().http.fetch<FetchRulesResponse>(
|
||||
`${DETECTION_ENGINE_RULES_URL}/_find`,
|
||||
{
|
||||
method: 'GET',
|
||||
query,
|
||||
signal,
|
||||
asResponse: true,
|
||||
}
|
||||
);
|
||||
|
||||
await throwIfNotOk(response.response);
|
||||
return response.body!;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -114,19 +107,15 @@ export const fetchRules = async ({
|
|||
* @param id Rule ID's (not rule_id)
|
||||
* @param signal to cancel request
|
||||
*
|
||||
* @throws An error if response is not OK
|
||||
*/
|
||||
export const fetchRuleById = async ({ id, signal }: FetchRuleProps): Promise<Rule> => {
|
||||
const response = await KibanaServices.get().http.fetch<Rule>(DETECTION_ENGINE_RULES_URL, {
|
||||
export const fetchRuleById = async ({ id, signal }: FetchRuleProps): Promise<Rule> =>
|
||||
KibanaServices.get().http.fetch<Rule>(DETECTION_ENGINE_RULES_URL, {
|
||||
method: 'GET',
|
||||
query: { id },
|
||||
asResponse: true,
|
||||
signal,
|
||||
});
|
||||
|
||||
await throwIfNotOk(response.response);
|
||||
return response.body!;
|
||||
};
|
||||
|
||||
/**
|
||||
* Enables/Disables provided Rule ID's
|
||||
*
|
||||
|
@ -135,19 +124,11 @@ export const fetchRuleById = async ({ id, signal }: FetchRuleProps): Promise<Rul
|
|||
*
|
||||
* @throws An error if response is not OK
|
||||
*/
|
||||
export const enableRules = async ({ ids, enabled }: EnableRulesProps): Promise<Rule[]> => {
|
||||
const response = await KibanaServices.get().http.fetch<Rule[]>(
|
||||
`${DETECTION_ENGINE_RULES_URL}/_bulk_update`,
|
||||
{
|
||||
method: 'PATCH',
|
||||
body: JSON.stringify(ids.map(id => ({ id, enabled }))),
|
||||
asResponse: true,
|
||||
}
|
||||
);
|
||||
|
||||
await throwIfNotOk(response.response);
|
||||
return response.body!;
|
||||
};
|
||||
export const enableRules = async ({ ids, enabled }: EnableRulesProps): Promise<BulkRuleResponse> =>
|
||||
KibanaServices.get().http.fetch<BulkRuleResponse>(`${DETECTION_ENGINE_RULES_URL}/_bulk_update`, {
|
||||
method: 'PATCH',
|
||||
body: JSON.stringify(ids.map(id => ({ id, enabled }))),
|
||||
});
|
||||
|
||||
/**
|
||||
* Deletes provided Rule ID's
|
||||
|
@ -156,74 +137,57 @@ export const enableRules = async ({ ids, enabled }: EnableRulesProps): Promise<R
|
|||
*
|
||||
* @throws An error if response is not OK
|
||||
*/
|
||||
export const deleteRules = async ({ ids }: DeleteRulesProps): Promise<Array<Rule | RuleError>> => {
|
||||
const response = await KibanaServices.get().http.fetch<Rule[]>(
|
||||
`${DETECTION_ENGINE_RULES_URL}/_bulk_delete`,
|
||||
{
|
||||
method: 'DELETE',
|
||||
body: JSON.stringify(ids.map(id => ({ id }))),
|
||||
asResponse: true,
|
||||
}
|
||||
);
|
||||
|
||||
await throwIfNotOk(response.response);
|
||||
return response.body!;
|
||||
};
|
||||
export const deleteRules = async ({ ids }: DeleteRulesProps): Promise<BulkRuleResponse> =>
|
||||
KibanaServices.get().http.fetch<Rule[]>(`${DETECTION_ENGINE_RULES_URL}/_bulk_delete`, {
|
||||
method: 'DELETE',
|
||||
body: JSON.stringify(ids.map(id => ({ id }))),
|
||||
});
|
||||
|
||||
/**
|
||||
* Duplicates provided Rules
|
||||
*
|
||||
* @param rules to duplicate
|
||||
*
|
||||
* @throws An error if response is not OK
|
||||
*/
|
||||
export const duplicateRules = async ({ rules }: DuplicateRulesProps): Promise<Rule[]> => {
|
||||
const response = await KibanaServices.get().http.fetch<Rule[]>(
|
||||
`${DETECTION_ENGINE_RULES_URL}/_bulk_create`,
|
||||
{
|
||||
method: 'POST',
|
||||
body: JSON.stringify(
|
||||
rules.map(rule => ({
|
||||
...rule,
|
||||
name: `${rule.name} [${i18n.DUPLICATE}]`,
|
||||
created_at: undefined,
|
||||
created_by: undefined,
|
||||
id: undefined,
|
||||
rule_id: undefined,
|
||||
updated_at: undefined,
|
||||
updated_by: undefined,
|
||||
enabled: rule.enabled,
|
||||
immutable: undefined,
|
||||
last_success_at: undefined,
|
||||
last_success_message: undefined,
|
||||
last_failure_at: undefined,
|
||||
last_failure_message: undefined,
|
||||
status: undefined,
|
||||
status_date: undefined,
|
||||
}))
|
||||
),
|
||||
asResponse: true,
|
||||
}
|
||||
);
|
||||
|
||||
await throwIfNotOk(response.response);
|
||||
return response.body!;
|
||||
};
|
||||
export const duplicateRules = async ({ rules }: DuplicateRulesProps): Promise<BulkRuleResponse> =>
|
||||
KibanaServices.get().http.fetch<Rule[]>(`${DETECTION_ENGINE_RULES_URL}/_bulk_create`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(
|
||||
rules.map(rule => ({
|
||||
...rule,
|
||||
name: `${rule.name} [${i18n.DUPLICATE}]`,
|
||||
created_at: undefined,
|
||||
created_by: undefined,
|
||||
id: undefined,
|
||||
rule_id: undefined,
|
||||
updated_at: undefined,
|
||||
updated_by: undefined,
|
||||
enabled: rule.enabled,
|
||||
immutable: undefined,
|
||||
last_success_at: undefined,
|
||||
last_success_message: undefined,
|
||||
last_failure_at: undefined,
|
||||
last_failure_message: undefined,
|
||||
status: undefined,
|
||||
status_date: undefined,
|
||||
}))
|
||||
),
|
||||
});
|
||||
|
||||
/**
|
||||
* Create Prepackaged Rules
|
||||
*
|
||||
* @param signal AbortSignal for cancelling request
|
||||
*
|
||||
* @throws An error if response is not OK
|
||||
*/
|
||||
export const createPrepackagedRules = async ({ signal }: BasicFetchProps): Promise<boolean> => {
|
||||
const response = await KibanaServices.get().http.fetch<unknown>(
|
||||
DETECTION_ENGINE_PREPACKAGED_URL,
|
||||
{
|
||||
method: 'PUT',
|
||||
signal,
|
||||
asResponse: true,
|
||||
}
|
||||
);
|
||||
await KibanaServices.get().http.fetch<unknown>(DETECTION_ENGINE_PREPACKAGED_URL, {
|
||||
method: 'PUT',
|
||||
signal,
|
||||
});
|
||||
|
||||
await throwIfNotOk(response.response);
|
||||
return true;
|
||||
};
|
||||
|
||||
|
@ -244,20 +208,16 @@ export const importRules = async ({
|
|||
const formData = new FormData();
|
||||
formData.append('file', fileToImport);
|
||||
|
||||
const response = await KibanaServices.get().http.fetch<ImportRulesResponse>(
|
||||
return KibanaServices.get().http.fetch<ImportRulesResponse>(
|
||||
`${DETECTION_ENGINE_RULES_URL}/_import`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': undefined },
|
||||
query: { overwrite },
|
||||
body: formData,
|
||||
asResponse: true,
|
||||
signal,
|
||||
}
|
||||
);
|
||||
|
||||
await throwIfNotOk(response.response);
|
||||
return response.body!;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -281,22 +241,15 @@ export const exportRules = async ({
|
|||
? JSON.stringify({ objects: ruleIds.map(rule => ({ rule_id: rule })) })
|
||||
: undefined;
|
||||
|
||||
const response = await KibanaServices.get().http.fetch<Blob>(
|
||||
`${DETECTION_ENGINE_RULES_URL}/_export`,
|
||||
{
|
||||
method: 'POST',
|
||||
body,
|
||||
query: {
|
||||
exclude_export_details: excludeExportDetails,
|
||||
file_name: filename,
|
||||
},
|
||||
signal,
|
||||
asResponse: true,
|
||||
}
|
||||
);
|
||||
|
||||
await throwIfNotOk(response.response);
|
||||
return response.body!;
|
||||
return KibanaServices.get().http.fetch<Blob>(`${DETECTION_ENGINE_RULES_URL}/_export`, {
|
||||
method: 'POST',
|
||||
body,
|
||||
query: {
|
||||
exclude_export_details: excludeExportDetails,
|
||||
file_name: filename,
|
||||
},
|
||||
signal,
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -313,38 +266,26 @@ export const getRuleStatusById = async ({
|
|||
}: {
|
||||
id: string;
|
||||
signal: AbortSignal;
|
||||
}): Promise<RuleStatusResponse> => {
|
||||
const response = await KibanaServices.get().http.fetch<RuleStatusResponse>(
|
||||
DETECTION_ENGINE_RULES_STATUS_URL,
|
||||
{
|
||||
method: 'GET',
|
||||
query: { ids: JSON.stringify([id]) },
|
||||
signal,
|
||||
asResponse: true,
|
||||
}
|
||||
);
|
||||
|
||||
await throwIfNotOk(response.response);
|
||||
return response.body!;
|
||||
};
|
||||
}): Promise<RuleStatusResponse> =>
|
||||
KibanaServices.get().http.fetch<RuleStatusResponse>(DETECTION_ENGINE_RULES_STATUS_URL, {
|
||||
method: 'GET',
|
||||
query: { ids: JSON.stringify([id]) },
|
||||
signal,
|
||||
});
|
||||
|
||||
/**
|
||||
* Fetch all unique Tags used by Rules
|
||||
*
|
||||
* @param signal to cancel request
|
||||
*
|
||||
* @throws An error if response is not OK
|
||||
*/
|
||||
export const fetchTags = async ({ signal }: { signal: AbortSignal }): Promise<string[]> => {
|
||||
const response = await KibanaServices.get().http.fetch<string[]>(DETECTION_ENGINE_TAGS_URL, {
|
||||
export const fetchTags = async ({ signal }: { signal: AbortSignal }): Promise<string[]> =>
|
||||
KibanaServices.get().http.fetch<string[]>(DETECTION_ENGINE_TAGS_URL, {
|
||||
method: 'GET',
|
||||
signal,
|
||||
asResponse: true,
|
||||
});
|
||||
|
||||
await throwIfNotOk(response.response);
|
||||
return response.body!;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get pre packaged rules Status
|
||||
*
|
||||
|
@ -356,16 +297,11 @@ export const getPrePackagedRulesStatus = async ({
|
|||
signal,
|
||||
}: {
|
||||
signal: AbortSignal;
|
||||
}): Promise<PrePackagedRulesStatusResponse> => {
|
||||
const response = await KibanaServices.get().http.fetch<PrePackagedRulesStatusResponse>(
|
||||
}): Promise<PrePackagedRulesStatusResponse> =>
|
||||
KibanaServices.get().http.fetch<PrePackagedRulesStatusResponse>(
|
||||
DETECTION_ENGINE_PREPACKAGED_RULES_STATUS_URL,
|
||||
{
|
||||
method: 'GET',
|
||||
signal,
|
||||
asResponse: true,
|
||||
}
|
||||
);
|
||||
|
||||
await throwIfNotOk(response.response);
|
||||
return response.body!;
|
||||
};
|
||||
|
|
|
@ -15,8 +15,7 @@ import {
|
|||
getIndexFields,
|
||||
sourceQuery,
|
||||
} from '../../../containers/source';
|
||||
import { useStateToaster } from '../../../components/toasters';
|
||||
import { errorToToaster } from '../../../components/ml/api/error_to_toaster';
|
||||
import { errorToToaster, useStateToaster } from '../../../components/toasters';
|
||||
import { SourceQuery } from '../../../graphql/types';
|
||||
import { useApolloClient } from '../../../utils/apollo_context';
|
||||
|
||||
|
|
|
@ -6,8 +6,7 @@
|
|||
|
||||
import { useEffect, useState, Dispatch } from 'react';
|
||||
|
||||
import { useStateToaster } from '../../../components/toasters';
|
||||
import { errorToToaster } from '../../../components/ml/api/error_to_toaster';
|
||||
import { errorToToaster, useStateToaster } from '../../../components/toasters';
|
||||
|
||||
import { addRule as persistRule } from './api';
|
||||
import * as i18n from './translations';
|
||||
|
|
|
@ -96,10 +96,13 @@ export type Rule = t.TypeOf<typeof RuleSchema>;
|
|||
export type Rules = t.TypeOf<typeof RulesSchema>;
|
||||
|
||||
export interface RuleError {
|
||||
rule_id: string;
|
||||
id?: string;
|
||||
rule_id?: string;
|
||||
error: { status_code: number; message: string };
|
||||
}
|
||||
|
||||
export type BulkRuleResponse = Array<Rule | RuleError>;
|
||||
|
||||
export interface RuleResponseBuckets {
|
||||
rules: Rule[];
|
||||
errors: RuleError[];
|
||||
|
|
|
@ -6,8 +6,7 @@
|
|||
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
import { useStateToaster, displaySuccessToast } from '../../../components/toasters';
|
||||
import { errorToToaster } from '../../../components/ml/api/error_to_toaster';
|
||||
import { errorToToaster, useStateToaster, displaySuccessToast } from '../../../components/toasters';
|
||||
import { getPrePackagedRulesStatus, createPrepackagedRules } from './api';
|
||||
import * as i18n from './translations';
|
||||
|
||||
|
|
|
@ -6,8 +6,7 @@
|
|||
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
import { useStateToaster } from '../../../components/toasters';
|
||||
import { errorToToaster } from '../../../components/ml/api/error_to_toaster';
|
||||
import { errorToToaster, useStateToaster } from '../../../components/toasters';
|
||||
import { fetchRuleById } from './api';
|
||||
import * as i18n from './translations';
|
||||
import { Rule } from './types';
|
||||
|
|
|
@ -6,8 +6,7 @@
|
|||
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
|
||||
import { useStateToaster } from '../../../components/toasters';
|
||||
import { errorToToaster } from '../../../components/ml/api/error_to_toaster';
|
||||
import { errorToToaster, useStateToaster } from '../../../components/toasters';
|
||||
import { getRuleStatusById } from './api';
|
||||
import * as i18n from './translations';
|
||||
import { RuleStatus } from './types';
|
||||
|
|
|
@ -8,9 +8,8 @@ import { noop } from 'lodash/fp';
|
|||
import { useEffect, useState, useRef } from 'react';
|
||||
|
||||
import { FetchRulesResponse, FilterOptions, PaginationOptions, Rule } from './types';
|
||||
import { useStateToaster } from '../../../components/toasters';
|
||||
import { errorToToaster, useStateToaster } from '../../../components/toasters';
|
||||
import { fetchRules } from './api';
|
||||
import { errorToToaster } from '../../../components/ml/api/error_to_toaster';
|
||||
import * as i18n from './translations';
|
||||
|
||||
export type ReturnRules = [
|
||||
|
|
|
@ -12,7 +12,7 @@ jest.mock('./api');
|
|||
describe('useTags', () => {
|
||||
test('init', async () => {
|
||||
await act(async () => {
|
||||
const { result, waitForNextUpdate } = renderHook<unknown, ReturnTags>(() => useTags());
|
||||
const { result, waitForNextUpdate } = renderHook<void, ReturnTags>(() => useTags());
|
||||
await waitForNextUpdate();
|
||||
expect(result.current).toEqual([true, [], result.current[2]]);
|
||||
});
|
||||
|
@ -20,7 +20,7 @@ describe('useTags', () => {
|
|||
|
||||
test('fetch tags', async () => {
|
||||
await act(async () => {
|
||||
const { result, waitForNextUpdate } = renderHook<unknown, ReturnTags>(() => useTags());
|
||||
const { result, waitForNextUpdate } = renderHook<void, ReturnTags>(() => useTags());
|
||||
await waitForNextUpdate();
|
||||
await waitForNextUpdate();
|
||||
expect(result.current).toEqual([
|
||||
|
|
|
@ -6,9 +6,8 @@
|
|||
|
||||
import { noop } from 'lodash/fp';
|
||||
import { useEffect, useState, useRef } from 'react';
|
||||
import { useStateToaster } from '../../../components/toasters';
|
||||
import { errorToToaster, useStateToaster } from '../../../components/toasters';
|
||||
import { fetchTags } from './api';
|
||||
import { errorToToaster } from '../../../components/ml/api/error_to_toaster';
|
||||
import * as i18n from './translations';
|
||||
|
||||
export type ReturnTags = [boolean, string[], () => void];
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* 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 {
|
||||
QuerySignals,
|
||||
SignalSearchResponse,
|
||||
BasicSignals,
|
||||
SignalsIndex,
|
||||
Privilege,
|
||||
} from '../types';
|
||||
import { signalsMock, mockSignalIndex, mockUserPrivilege } from '../mock';
|
||||
|
||||
export const fetchQuerySignals = async <Hit, Aggregations>({
|
||||
query,
|
||||
signal,
|
||||
}: QuerySignals): Promise<SignalSearchResponse<Hit, Aggregations>> =>
|
||||
Promise.resolve(signalsMock as SignalSearchResponse<Hit, Aggregations>);
|
||||
|
||||
export const getSignalIndex = async ({ signal }: BasicSignals): Promise<SignalsIndex> =>
|
||||
Promise.resolve(mockSignalIndex);
|
||||
|
||||
export const getUserPrivilege = async ({ signal }: BasicSignals): Promise<Privilege> =>
|
||||
Promise.resolve(mockUserPrivilege);
|
||||
|
||||
export const createSignalIndex = async ({ signal }: BasicSignals): Promise<SignalsIndex> =>
|
||||
Promise.resolve(mockSignalIndex);
|
|
@ -0,0 +1,165 @@
|
|||
/*
|
||||
* 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 { KibanaServices } from '../../../lib/kibana';
|
||||
import {
|
||||
signalsMock,
|
||||
mockSignalsQuery,
|
||||
mockStatusSignalQuery,
|
||||
mockSignalIndex,
|
||||
mockUserPrivilege,
|
||||
} from './mock';
|
||||
import {
|
||||
fetchQuerySignals,
|
||||
updateSignalStatus,
|
||||
getSignalIndex,
|
||||
getUserPrivilege,
|
||||
createSignalIndex,
|
||||
} from './api';
|
||||
|
||||
const abortCtrl = new AbortController();
|
||||
const mockKibanaServices = KibanaServices.get as jest.Mock;
|
||||
jest.mock('../../../lib/kibana');
|
||||
|
||||
const fetchMock = jest.fn();
|
||||
mockKibanaServices.mockReturnValue({ http: { fetch: fetchMock } });
|
||||
|
||||
describe('Detections Signals API', () => {
|
||||
describe('fetchQuerySignals', () => {
|
||||
beforeEach(() => {
|
||||
fetchMock.mockClear();
|
||||
fetchMock.mockResolvedValue(signalsMock);
|
||||
});
|
||||
|
||||
test('check parameter url, body', async () => {
|
||||
await fetchQuerySignals({ query: mockSignalsQuery, signal: abortCtrl.signal });
|
||||
expect(fetchMock).toHaveBeenCalledWith('/api/detection_engine/signals/search', {
|
||||
body:
|
||||
'{"aggs":{"signalsByGrouping":{"terms":{"field":"signal.rule.risk_score","missing":"All others","order":{"_count":"desc"},"size":10},"aggs":{"signals":{"date_histogram":{"field":"@timestamp","fixed_interval":"81000000ms","min_doc_count":0,"extended_bounds":{"min":1579644343954,"max":1582236343955}}}}}},"query":{"bool":{"filter":[{"bool":{"must":[],"filter":[{"match_all":{}}],"should":[],"must_not":[]}},{"range":{"@timestamp":{"gte":1579644343954,"lte":1582236343955}}}]}}}',
|
||||
method: 'POST',
|
||||
signal: abortCtrl.signal,
|
||||
});
|
||||
});
|
||||
|
||||
test('happy path', async () => {
|
||||
const signalsResp = await fetchQuerySignals({
|
||||
query: mockSignalsQuery,
|
||||
signal: abortCtrl.signal,
|
||||
});
|
||||
expect(signalsResp).toEqual(signalsMock);
|
||||
});
|
||||
});
|
||||
|
||||
describe('updateSignalStatus', () => {
|
||||
beforeEach(() => {
|
||||
fetchMock.mockClear();
|
||||
fetchMock.mockResolvedValue({});
|
||||
});
|
||||
|
||||
test('check parameter url, body when closing a signal', async () => {
|
||||
await updateSignalStatus({
|
||||
query: mockStatusSignalQuery,
|
||||
signal: abortCtrl.signal,
|
||||
status: 'closed',
|
||||
});
|
||||
expect(fetchMock).toHaveBeenCalledWith('/api/detection_engine/signals/status', {
|
||||
body:
|
||||
'{"status":"closed","bool":{"filter":{"terms":{"_id":["b4ee5c32e3a321057edcc953ca17228c6fdfe5ba43fdbbdaffa8cefa11605cc5"]}}}}',
|
||||
method: 'POST',
|
||||
signal: abortCtrl.signal,
|
||||
});
|
||||
});
|
||||
|
||||
test('check parameter url, body when opening a signal', async () => {
|
||||
await updateSignalStatus({
|
||||
query: mockStatusSignalQuery,
|
||||
signal: abortCtrl.signal,
|
||||
status: 'open',
|
||||
});
|
||||
expect(fetchMock).toHaveBeenCalledWith('/api/detection_engine/signals/status', {
|
||||
body:
|
||||
'{"status":"open","bool":{"filter":{"terms":{"_id":["b4ee5c32e3a321057edcc953ca17228c6fdfe5ba43fdbbdaffa8cefa11605cc5"]}}}}',
|
||||
method: 'POST',
|
||||
signal: abortCtrl.signal,
|
||||
});
|
||||
});
|
||||
|
||||
test('happy path', async () => {
|
||||
const signalsResp = await updateSignalStatus({
|
||||
query: mockStatusSignalQuery,
|
||||
signal: abortCtrl.signal,
|
||||
status: 'open',
|
||||
});
|
||||
expect(signalsResp).toEqual({});
|
||||
});
|
||||
});
|
||||
|
||||
describe('getSignalIndex', () => {
|
||||
beforeEach(() => {
|
||||
fetchMock.mockClear();
|
||||
fetchMock.mockResolvedValue(mockSignalIndex);
|
||||
});
|
||||
|
||||
test('check parameter url', async () => {
|
||||
await getSignalIndex({ signal: abortCtrl.signal });
|
||||
expect(fetchMock).toHaveBeenCalledWith('/api/detection_engine/index', {
|
||||
method: 'GET',
|
||||
signal: abortCtrl.signal,
|
||||
});
|
||||
});
|
||||
|
||||
test('happy path', async () => {
|
||||
const signalsResp = await getSignalIndex({
|
||||
signal: abortCtrl.signal,
|
||||
});
|
||||
expect(signalsResp).toEqual(mockSignalIndex);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getUserPrivilege', () => {
|
||||
beforeEach(() => {
|
||||
fetchMock.mockClear();
|
||||
fetchMock.mockResolvedValue(mockUserPrivilege);
|
||||
});
|
||||
|
||||
test('check parameter url', async () => {
|
||||
await getUserPrivilege({ signal: abortCtrl.signal });
|
||||
expect(fetchMock).toHaveBeenCalledWith('/api/detection_engine/privileges', {
|
||||
method: 'GET',
|
||||
signal: abortCtrl.signal,
|
||||
});
|
||||
});
|
||||
|
||||
test('happy path', async () => {
|
||||
const signalsResp = await getUserPrivilege({
|
||||
signal: abortCtrl.signal,
|
||||
});
|
||||
expect(signalsResp).toEqual(mockUserPrivilege);
|
||||
});
|
||||
});
|
||||
|
||||
describe('createSignalIndex', () => {
|
||||
beforeEach(() => {
|
||||
fetchMock.mockClear();
|
||||
fetchMock.mockResolvedValue(mockSignalIndex);
|
||||
});
|
||||
|
||||
test('check parameter url', async () => {
|
||||
await createSignalIndex({ signal: abortCtrl.signal });
|
||||
expect(fetchMock).toHaveBeenCalledWith('/api/detection_engine/index', {
|
||||
method: 'POST',
|
||||
signal: abortCtrl.signal,
|
||||
});
|
||||
});
|
||||
|
||||
test('happy path', async () => {
|
||||
const signalsResp = await createSignalIndex({
|
||||
signal: abortCtrl.signal,
|
||||
});
|
||||
expect(signalsResp).toEqual(mockSignalIndex);
|
||||
});
|
||||
});
|
||||
});
|
|
@ -5,7 +5,6 @@
|
|||
*/
|
||||
|
||||
import { KibanaServices } from '../../../lib/kibana';
|
||||
import { throwIfNotOk } from '../../../hooks/api/api';
|
||||
import {
|
||||
DETECTION_ENGINE_QUERY_SIGNALS_URL,
|
||||
DETECTION_ENGINE_SIGNALS_STATUS_URL,
|
||||
|
@ -14,10 +13,8 @@ import {
|
|||
} from '../../../../common/constants';
|
||||
import {
|
||||
BasicSignals,
|
||||
PostSignalError,
|
||||
Privilege,
|
||||
QuerySignals,
|
||||
SignalIndexError,
|
||||
SignalSearchResponse,
|
||||
SignalsIndex,
|
||||
UpdateSignalStatusProps,
|
||||
|
@ -27,101 +24,78 @@ import {
|
|||
* Fetch Signals by providing a query
|
||||
*
|
||||
* @param query String to match a dsl
|
||||
* @param signal to cancel request
|
||||
*
|
||||
* @throws An error if response is not OK
|
||||
*/
|
||||
export const fetchQuerySignals = async <Hit, Aggregations>({
|
||||
query,
|
||||
signal,
|
||||
}: QuerySignals): Promise<SignalSearchResponse<Hit, Aggregations>> => {
|
||||
const response = await KibanaServices.get().http.fetch<SignalSearchResponse<Hit, Aggregations>>(
|
||||
}: QuerySignals): Promise<SignalSearchResponse<Hit, Aggregations>> =>
|
||||
KibanaServices.get().http.fetch<SignalSearchResponse<Hit, Aggregations>>(
|
||||
DETECTION_ENGINE_QUERY_SIGNALS_URL,
|
||||
{
|
||||
method: 'POST',
|
||||
body: JSON.stringify(query),
|
||||
asResponse: true,
|
||||
signal,
|
||||
}
|
||||
);
|
||||
|
||||
await throwIfNotOk(response.response);
|
||||
return response.body!;
|
||||
};
|
||||
|
||||
/**
|
||||
* Update signal status by query
|
||||
*
|
||||
* @param query of signals to update
|
||||
* @param status to update to('open' / 'closed')
|
||||
* @param signal AbortSignal for cancelling request
|
||||
*
|
||||
* @throws An error if response is not OK
|
||||
*/
|
||||
export const updateSignalStatus = async ({
|
||||
query,
|
||||
status,
|
||||
signal,
|
||||
}: UpdateSignalStatusProps): Promise<unknown> => {
|
||||
const response = await KibanaServices.get().http.fetch(DETECTION_ENGINE_SIGNALS_STATUS_URL, {
|
||||
}: UpdateSignalStatusProps): Promise<unknown> =>
|
||||
KibanaServices.get().http.fetch(DETECTION_ENGINE_SIGNALS_STATUS_URL, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ status, ...query }),
|
||||
asResponse: true,
|
||||
signal,
|
||||
});
|
||||
|
||||
await throwIfNotOk(response.response);
|
||||
return response.body!;
|
||||
};
|
||||
|
||||
/**
|
||||
* Fetch Signal Index
|
||||
*
|
||||
* @param signal AbortSignal for cancelling request
|
||||
*
|
||||
* @throws An error if response is not OK
|
||||
*/
|
||||
export const getSignalIndex = async ({ signal }: BasicSignals): Promise<SignalsIndex> => {
|
||||
try {
|
||||
return await KibanaServices.get().http.fetch<SignalsIndex>(DETECTION_ENGINE_INDEX_URL, {
|
||||
method: 'GET',
|
||||
signal,
|
||||
});
|
||||
} catch (e) {
|
||||
if (e.body) {
|
||||
throw new SignalIndexError(e.body);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
export const getSignalIndex = async ({ signal }: BasicSignals): Promise<SignalsIndex> =>
|
||||
KibanaServices.get().http.fetch<SignalsIndex>(DETECTION_ENGINE_INDEX_URL, {
|
||||
method: 'GET',
|
||||
signal,
|
||||
});
|
||||
|
||||
/**
|
||||
* Get User Privileges
|
||||
*
|
||||
* @param signal AbortSignal for cancelling request
|
||||
*
|
||||
* @throws An error if response is not OK
|
||||
*/
|
||||
export const getUserPrivilege = async ({ signal }: BasicSignals): Promise<Privilege> => {
|
||||
const response = await KibanaServices.get().http.fetch<Privilege>(
|
||||
DETECTION_ENGINE_PRIVILEGES_URL,
|
||||
{
|
||||
method: 'GET',
|
||||
signal,
|
||||
asResponse: true,
|
||||
}
|
||||
);
|
||||
|
||||
await throwIfNotOk(response.response);
|
||||
return response.body!;
|
||||
};
|
||||
export const getUserPrivilege = async ({ signal }: BasicSignals): Promise<Privilege> =>
|
||||
KibanaServices.get().http.fetch<Privilege>(DETECTION_ENGINE_PRIVILEGES_URL, {
|
||||
method: 'GET',
|
||||
signal,
|
||||
});
|
||||
|
||||
/**
|
||||
* Create Signal Index if needed it
|
||||
*
|
||||
* @param signal AbortSignal for cancelling request
|
||||
*
|
||||
* @throws An error if response is not OK
|
||||
*/
|
||||
export const createSignalIndex = async ({ signal }: BasicSignals): Promise<SignalsIndex> => {
|
||||
try {
|
||||
return await KibanaServices.get().http.fetch<SignalsIndex>(DETECTION_ENGINE_INDEX_URL, {
|
||||
method: 'POST',
|
||||
signal,
|
||||
});
|
||||
} catch (e) {
|
||||
if (e.body) {
|
||||
throw new PostSignalError(e.body);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
export const createSignalIndex = async ({ signal }: BasicSignals): Promise<SignalsIndex> =>
|
||||
KibanaServices.get().http.fetch<SignalsIndex>(DETECTION_ENGINE_INDEX_URL, {
|
||||
method: 'POST',
|
||||
signal,
|
||||
});
|
||||
|
|
|
@ -1,24 +0,0 @@
|
|||
/*
|
||||
* 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 { MessageBody } from '../../../../hooks/api/throw_if_not_ok';
|
||||
|
||||
export class SignalIndexError extends Error {
|
||||
message: string = '';
|
||||
status_code: number = -1;
|
||||
error: string = '';
|
||||
|
||||
constructor(errObj: MessageBody) {
|
||||
super(errObj.message);
|
||||
this.message = errObj.message ?? '';
|
||||
this.status_code = errObj.status_code ?? -1;
|
||||
this.error = errObj.error ?? '';
|
||||
this.name = 'SignalIndexError';
|
||||
|
||||
// Set the prototype explicitly.
|
||||
Object.setPrototypeOf(this, SignalIndexError.prototype);
|
||||
}
|
||||
}
|
|
@ -1,24 +0,0 @@
|
|||
/*
|
||||
* 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 { MessageBody } from '../../../../hooks/api/throw_if_not_ok';
|
||||
|
||||
export class PostSignalError extends Error {
|
||||
message: string = '';
|
||||
statusCode: number = -1;
|
||||
error: string = '';
|
||||
|
||||
constructor(errObj: MessageBody) {
|
||||
super(errObj.message);
|
||||
this.message = errObj.message ?? '';
|
||||
this.statusCode = errObj.statusCode ?? -1;
|
||||
this.error = errObj.error ?? '';
|
||||
this.name = 'PostSignalError';
|
||||
|
||||
// Set the prototype explicitly.
|
||||
Object.setPrototypeOf(this, PostSignalError.prototype);
|
||||
}
|
||||
}
|
|
@ -1,24 +0,0 @@
|
|||
/*
|
||||
* 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 { MessageBody } from '../../../../hooks/api/throw_if_not_ok';
|
||||
|
||||
export class PrivilegeUserError extends Error {
|
||||
message: string = '';
|
||||
statusCode: number = -1;
|
||||
error: string = '';
|
||||
|
||||
constructor(errObj: MessageBody) {
|
||||
super(errObj.message);
|
||||
this.message = errObj.message ?? '';
|
||||
this.statusCode = errObj.statusCode ?? -1;
|
||||
this.error = errObj.error ?? '';
|
||||
this.name = 'PrivilegeUserError';
|
||||
|
||||
// Set the prototype explicitly.
|
||||
Object.setPrototypeOf(this, PrivilegeUserError.prototype);
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load diff
|
@ -4,8 +4,6 @@
|
|||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
|
||||
export * from './errors_types';
|
||||
|
||||
export interface BasicSignals {
|
||||
signal: AbortSignal;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,70 @@
|
|||
/*
|
||||
* 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 { renderHook, act } from '@testing-library/react-hooks';
|
||||
import { usePrivilegeUser, ReturnPrivilegeUser } from './use_privilege_user';
|
||||
import * as api from './api';
|
||||
|
||||
jest.mock('./api');
|
||||
|
||||
describe('usePrivilegeUser', () => {
|
||||
test('init', async () => {
|
||||
await act(async () => {
|
||||
const { result, waitForNextUpdate } = renderHook<void, ReturnPrivilegeUser>(() =>
|
||||
usePrivilegeUser()
|
||||
);
|
||||
await waitForNextUpdate();
|
||||
expect(result.current).toEqual({
|
||||
hasEncryptionKey: null,
|
||||
hasIndexManage: null,
|
||||
hasIndexWrite: null,
|
||||
hasManageApiKey: null,
|
||||
isAuthenticated: null,
|
||||
loading: true,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
test('fetch user privilege', async () => {
|
||||
await act(async () => {
|
||||
const { result, waitForNextUpdate } = renderHook<void, ReturnPrivilegeUser>(() =>
|
||||
usePrivilegeUser()
|
||||
);
|
||||
await waitForNextUpdate();
|
||||
await waitForNextUpdate();
|
||||
expect(result.current).toEqual({
|
||||
hasEncryptionKey: true,
|
||||
hasIndexManage: true,
|
||||
hasIndexWrite: true,
|
||||
hasManageApiKey: true,
|
||||
isAuthenticated: true,
|
||||
loading: false,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
test('if there is an error when fetching user privilege, we should get back false for every properties', async () => {
|
||||
const spyOnGetUserPrivilege = jest.spyOn(api, 'getUserPrivilege');
|
||||
spyOnGetUserPrivilege.mockImplementation(() => {
|
||||
throw new Error('Something went wrong, let see what happen');
|
||||
});
|
||||
await act(async () => {
|
||||
const { result, waitForNextUpdate } = renderHook<void, ReturnPrivilegeUser>(() =>
|
||||
usePrivilegeUser()
|
||||
);
|
||||
await waitForNextUpdate();
|
||||
await waitForNextUpdate();
|
||||
expect(result.current).toEqual({
|
||||
hasEncryptionKey: false,
|
||||
hasIndexManage: false,
|
||||
hasIndexWrite: false,
|
||||
hasManageApiKey: false,
|
||||
isAuthenticated: false,
|
||||
loading: false,
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
|
@ -6,12 +6,11 @@
|
|||
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
import { errorToToaster } from '../../../components/ml/api/error_to_toaster';
|
||||
import { useStateToaster } from '../../../components/toasters';
|
||||
import { errorToToaster, useStateToaster } from '../../../components/toasters';
|
||||
import { getUserPrivilege } from './api';
|
||||
import * as i18n from './translations';
|
||||
|
||||
interface Return {
|
||||
export interface ReturnPrivilegeUser {
|
||||
loading: boolean;
|
||||
isAuthenticated: boolean | null;
|
||||
hasEncryptionKey: boolean | null;
|
||||
|
@ -23,11 +22,11 @@ interface Return {
|
|||
* Hook to get user privilege from
|
||||
*
|
||||
*/
|
||||
export const usePrivilegeUser = (): Return => {
|
||||
export const usePrivilegeUser = (): ReturnPrivilegeUser => {
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [privilegeUser, setPrivilegeUser] = useState<
|
||||
Pick<
|
||||
Return,
|
||||
ReturnPrivilegeUser,
|
||||
| 'isAuthenticated'
|
||||
| 'hasEncryptionKey'
|
||||
| 'hasIndexManage'
|
||||
|
|
|
@ -0,0 +1,130 @@
|
|||
/*
|
||||
* 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 { renderHook, act } from '@testing-library/react-hooks';
|
||||
import { useQuerySignals, ReturnQuerySignals } from './use_query';
|
||||
import * as api from './api';
|
||||
import { mockSignalsQuery, signalsMock } from './mock';
|
||||
|
||||
jest.mock('./api');
|
||||
|
||||
describe('useQuerySignals', () => {
|
||||
const indexName = 'mock-index-name';
|
||||
beforeEach(() => {
|
||||
jest.resetAllMocks();
|
||||
});
|
||||
test('init', async () => {
|
||||
await act(async () => {
|
||||
const { result, waitForNextUpdate } = renderHook<
|
||||
[object, string],
|
||||
ReturnQuerySignals<unknown, unknown>
|
||||
>(() => useQuerySignals<unknown, unknown>(mockSignalsQuery, indexName));
|
||||
await waitForNextUpdate();
|
||||
expect(result.current).toEqual({
|
||||
loading: true,
|
||||
data: null,
|
||||
response: '',
|
||||
request: '',
|
||||
setQuery: result.current.setQuery,
|
||||
refetch: null,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
test('fetch signals data', async () => {
|
||||
await act(async () => {
|
||||
const { result, waitForNextUpdate } = renderHook<
|
||||
[object, string],
|
||||
ReturnQuerySignals<unknown, unknown>
|
||||
>(() => useQuerySignals<unknown, unknown>(mockSignalsQuery, indexName));
|
||||
await waitForNextUpdate();
|
||||
await waitForNextUpdate();
|
||||
expect(result.current).toEqual({
|
||||
loading: false,
|
||||
data: signalsMock,
|
||||
response: JSON.stringify(signalsMock, null, 2),
|
||||
request: JSON.stringify({ index: [indexName] ?? [''], body: mockSignalsQuery }, null, 2),
|
||||
setQuery: result.current.setQuery,
|
||||
refetch: result.current.refetch,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
test('re-fetch signals data', async () => {
|
||||
const spyOnfetchQuerySignals = jest.spyOn(api, 'fetchQuerySignals');
|
||||
await act(async () => {
|
||||
const { result, waitForNextUpdate } = renderHook<
|
||||
[object, string],
|
||||
ReturnQuerySignals<unknown, unknown>
|
||||
>(() => useQuerySignals<unknown, unknown>(mockSignalsQuery, indexName));
|
||||
await waitForNextUpdate();
|
||||
await waitForNextUpdate();
|
||||
if (result.current.refetch) {
|
||||
result.current.refetch();
|
||||
}
|
||||
await waitForNextUpdate();
|
||||
expect(spyOnfetchQuerySignals).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
});
|
||||
|
||||
test('fetch signal when index name changed', async () => {
|
||||
const spyOnfetchRules = jest.spyOn(api, 'fetchQuerySignals');
|
||||
await act(async () => {
|
||||
const { rerender, waitForNextUpdate } = renderHook<
|
||||
[object, string],
|
||||
ReturnQuerySignals<unknown, unknown>
|
||||
>(args => useQuerySignals(args[0], args[1]), {
|
||||
initialProps: [mockSignalsQuery, indexName],
|
||||
});
|
||||
await waitForNextUpdate();
|
||||
await waitForNextUpdate();
|
||||
rerender([mockSignalsQuery, 'new-mock-index-name']);
|
||||
await waitForNextUpdate();
|
||||
expect(spyOnfetchRules).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
});
|
||||
|
||||
test('fetch signal when query object changed', async () => {
|
||||
const spyOnfetchRules = jest.spyOn(api, 'fetchQuerySignals');
|
||||
await act(async () => {
|
||||
const { result, waitForNextUpdate } = renderHook<
|
||||
[object, string],
|
||||
ReturnQuerySignals<unknown, unknown>
|
||||
>(args => useQuerySignals(args[0], args[1]), {
|
||||
initialProps: [mockSignalsQuery, indexName],
|
||||
});
|
||||
await waitForNextUpdate();
|
||||
await waitForNextUpdate();
|
||||
if (result.current.setQuery) {
|
||||
result.current.setQuery({ ...mockSignalsQuery });
|
||||
}
|
||||
await waitForNextUpdate();
|
||||
expect(spyOnfetchRules).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
});
|
||||
|
||||
test('if there is an error when fetching data, we should get back the init value for every properties', async () => {
|
||||
const spyOnGetUserPrivilege = jest.spyOn(api, 'fetchQuerySignals');
|
||||
spyOnGetUserPrivilege.mockImplementation(() => {
|
||||
throw new Error('Something went wrong, let see what happen');
|
||||
});
|
||||
await act(async () => {
|
||||
const { result, waitForNextUpdate } = renderHook<void, ReturnQuerySignals<unknown, unknown>>(
|
||||
() => useQuerySignals<unknown, unknown>(mockSignalsQuery, 'mock-index-name')
|
||||
);
|
||||
await waitForNextUpdate();
|
||||
await waitForNextUpdate();
|
||||
expect(result.current).toEqual({
|
||||
loading: false,
|
||||
data: null,
|
||||
response: '',
|
||||
request: '',
|
||||
setQuery: result.current.setQuery,
|
||||
refetch: result.current.refetch,
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
|
@ -11,7 +11,7 @@ import { SignalSearchResponse } from './types';
|
|||
|
||||
type Func = () => void;
|
||||
|
||||
interface Return<Hit, Aggs> {
|
||||
export interface ReturnQuerySignals<Hit, Aggs> {
|
||||
loading: boolean;
|
||||
data: SignalSearchResponse<Hit, Aggs> | null;
|
||||
setQuery: React.Dispatch<SetStateAction<object>>;
|
||||
|
@ -29,10 +29,10 @@ interface Return<Hit, Aggs> {
|
|||
export const useQuerySignals = <Hit, Aggs>(
|
||||
initialQuery: object,
|
||||
indexName?: string | null
|
||||
): Return<Hit, Aggs> => {
|
||||
): ReturnQuerySignals<Hit, Aggs> => {
|
||||
const [query, setQuery] = useState(initialQuery);
|
||||
const [signals, setSignals] = useState<
|
||||
Pick<Return<Hit, Aggs>, 'data' | 'setQuery' | 'response' | 'request' | 'refetch'>
|
||||
Pick<ReturnQuerySignals<Hit, Aggs>, 'data' | 'setQuery' | 'response' | 'request' | 'refetch'>
|
||||
>({
|
||||
data: null,
|
||||
response: '',
|
||||
|
|
|
@ -0,0 +1,127 @@
|
|||
/*
|
||||
* 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 { renderHook, act } from '@testing-library/react-hooks';
|
||||
import { useSignalIndex, ReturnSignalIndex } from './use_signal_index';
|
||||
import * as api from './api';
|
||||
|
||||
jest.mock('./api');
|
||||
|
||||
describe('useSignalIndex', () => {
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
jest.resetAllMocks();
|
||||
});
|
||||
test('init', async () => {
|
||||
await act(async () => {
|
||||
const { result, waitForNextUpdate } = renderHook<void, ReturnSignalIndex>(() =>
|
||||
useSignalIndex()
|
||||
);
|
||||
await waitForNextUpdate();
|
||||
expect(result.current).toEqual({
|
||||
createDeSignalIndex: null,
|
||||
loading: true,
|
||||
signalIndexExists: null,
|
||||
signalIndexName: null,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
test('fetch signals info', async () => {
|
||||
await act(async () => {
|
||||
const { result, waitForNextUpdate } = renderHook<void, ReturnSignalIndex>(() =>
|
||||
useSignalIndex()
|
||||
);
|
||||
await waitForNextUpdate();
|
||||
await waitForNextUpdate();
|
||||
expect(result.current).toEqual({
|
||||
createDeSignalIndex: result.current.createDeSignalIndex,
|
||||
loading: false,
|
||||
signalIndexExists: true,
|
||||
signalIndexName: 'mock-signal-index',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
test('make sure that createSignalIndex is giving back the signal info', async () => {
|
||||
await act(async () => {
|
||||
const { result, waitForNextUpdate } = renderHook<void, ReturnSignalIndex>(() =>
|
||||
useSignalIndex()
|
||||
);
|
||||
await waitForNextUpdate();
|
||||
await waitForNextUpdate();
|
||||
if (result.current.createDeSignalIndex != null) {
|
||||
await result.current.createDeSignalIndex();
|
||||
}
|
||||
await waitForNextUpdate();
|
||||
expect(result.current).toEqual({
|
||||
createDeSignalIndex: result.current.createDeSignalIndex,
|
||||
loading: false,
|
||||
signalIndexExists: true,
|
||||
signalIndexName: 'mock-signal-index',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
test('make sure that createSignalIndex have been called when trying to create signal index', async () => {
|
||||
const spyOnCreateSignalIndex = jest.spyOn(api, 'createSignalIndex');
|
||||
await act(async () => {
|
||||
const { result, waitForNextUpdate } = renderHook<void, ReturnSignalIndex>(() =>
|
||||
useSignalIndex()
|
||||
);
|
||||
await waitForNextUpdate();
|
||||
await waitForNextUpdate();
|
||||
if (result.current.createDeSignalIndex != null) {
|
||||
await result.current.createDeSignalIndex();
|
||||
}
|
||||
await waitForNextUpdate();
|
||||
expect(spyOnCreateSignalIndex).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
test('if there is an error during createSignalIndex, we should get back signalIndexExists === false && signalIndexName == null', async () => {
|
||||
const spyOnCreateSignalIndex = jest.spyOn(api, 'createSignalIndex');
|
||||
spyOnCreateSignalIndex.mockImplementation(() => {
|
||||
throw new Error('Something went wrong, let see what happen');
|
||||
});
|
||||
await act(async () => {
|
||||
const { result, waitForNextUpdate } = renderHook<void, ReturnSignalIndex>(() =>
|
||||
useSignalIndex()
|
||||
);
|
||||
await waitForNextUpdate();
|
||||
await waitForNextUpdate();
|
||||
if (result.current.createDeSignalIndex != null) {
|
||||
await result.current.createDeSignalIndex();
|
||||
}
|
||||
expect(result.current).toEqual({
|
||||
createDeSignalIndex: result.current.createDeSignalIndex,
|
||||
loading: false,
|
||||
signalIndexExists: false,
|
||||
signalIndexName: null,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
test('if there is an error when fetching signals info, signalIndexExists === false && signalIndexName == null', async () => {
|
||||
const spyOnGetSignalIndex = jest.spyOn(api, 'getSignalIndex');
|
||||
spyOnGetSignalIndex.mockImplementation(() => {
|
||||
throw new Error('Something went wrong, let see what happen');
|
||||
});
|
||||
await act(async () => {
|
||||
const { result, waitForNextUpdate } = renderHook<void, ReturnSignalIndex>(() =>
|
||||
useSignalIndex()
|
||||
);
|
||||
await waitForNextUpdate();
|
||||
await waitForNextUpdate();
|
||||
expect(result.current).toEqual({
|
||||
createDeSignalIndex: result.current.createDeSignalIndex,
|
||||
loading: false,
|
||||
signalIndexExists: false,
|
||||
signalIndexName: null,
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
|
@ -6,15 +6,14 @@
|
|||
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
import { errorToToaster } from '../../../components/ml/api/error_to_toaster';
|
||||
import { useStateToaster } from '../../../components/toasters';
|
||||
import { errorToToaster, useStateToaster } from '../../../components/toasters';
|
||||
import { createSignalIndex, getSignalIndex } from './api';
|
||||
import * as i18n from './translations';
|
||||
import { PostSignalError, SignalIndexError } from './types';
|
||||
import { isApiError } from '../../../utils/api';
|
||||
|
||||
type Func = () => void;
|
||||
|
||||
interface Return {
|
||||
export interface ReturnSignalIndex {
|
||||
loading: boolean;
|
||||
signalIndexExists: boolean | null;
|
||||
signalIndexName: string | null;
|
||||
|
@ -26,10 +25,10 @@ interface Return {
|
|||
*
|
||||
*
|
||||
*/
|
||||
export const useSignalIndex = (): Return => {
|
||||
export const useSignalIndex = (): ReturnSignalIndex => {
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [signalIndex, setSignalIndex] = useState<
|
||||
Pick<Return, 'signalIndexExists' | 'signalIndexName' | 'createDeSignalIndex'>
|
||||
Pick<ReturnSignalIndex, 'signalIndexExists' | 'signalIndexName' | 'createDeSignalIndex'>
|
||||
>({
|
||||
signalIndexExists: null,
|
||||
signalIndexName: null,
|
||||
|
@ -60,7 +59,7 @@ export const useSignalIndex = (): Return => {
|
|||
signalIndexName: null,
|
||||
createDeSignalIndex: createIndex,
|
||||
});
|
||||
if (error instanceof SignalIndexError && error.status_code !== 404) {
|
||||
if (isApiError(error) && error.body.status_code !== 404) {
|
||||
errorToToaster({ title: i18n.SIGNAL_GET_NAME_FAILURE, error, dispatchToaster });
|
||||
}
|
||||
}
|
||||
|
@ -82,7 +81,7 @@ export const useSignalIndex = (): Return => {
|
|||
}
|
||||
} catch (error) {
|
||||
if (isSubscribed) {
|
||||
if (error instanceof PostSignalError && error.statusCode === 409) {
|
||||
if (isApiError(error) && error.body.status_code === 409) {
|
||||
fetchData();
|
||||
} else {
|
||||
setSignalIndex({
|
||||
|
|
|
@ -8,7 +8,7 @@ import { useQuery } from '.';
|
|||
import { mount } from 'enzyme';
|
||||
import React from 'react';
|
||||
import { useApolloClient } from '../../utils/apollo_context';
|
||||
import { errorToToaster } from '../../components/ml/api/error_to_toaster';
|
||||
import { errorToToaster } from '../../components/toasters';
|
||||
import { MatrixOverTimeHistogramData, HistogramType } from '../../graphql/types';
|
||||
import { InspectQuery, Refetch } from '../../store/inputs/model';
|
||||
|
||||
|
@ -41,7 +41,10 @@ jest.mock('./index.gql_query', () => {
|
|||
};
|
||||
});
|
||||
|
||||
jest.mock('../../components/ml/api/error_to_toaster');
|
||||
jest.mock('../../components/toasters/', () => ({
|
||||
useStateToaster: () => [jest.fn(), jest.fn()],
|
||||
errorToToaster: jest.fn(),
|
||||
}));
|
||||
|
||||
describe('useQuery', () => {
|
||||
let result: {
|
||||
|
|
|
@ -6,8 +6,7 @@
|
|||
import { useEffect, useState, useRef } from 'react';
|
||||
import { MatrixHistogramQueryProps } from '../../components/matrix_histogram/types';
|
||||
import { DEFAULT_INDEX_KEY } from '../../../common/constants';
|
||||
import { useStateToaster } from '../../components/toasters';
|
||||
import { errorToToaster } from '../../components/ml/api/error_to_toaster';
|
||||
import { errorToToaster, useStateToaster } from '../../components/toasters';
|
||||
import { useUiSetting$ } from '../../lib/kibana';
|
||||
import { createFilter } from '../helpers';
|
||||
import { useApolloClient } from '../../utils/apollo_context';
|
||||
|
|
|
@ -1,44 +0,0 @@
|
|||
/*
|
||||
* 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 fetchMock from 'fetch-mock';
|
||||
import { throwIfNotOk } from './api';
|
||||
|
||||
describe('api', () => {
|
||||
afterEach(() => {
|
||||
fetchMock.reset();
|
||||
});
|
||||
|
||||
describe('#throwIfNotOk', () => {
|
||||
test('throws a network error if there is no response', async () => {
|
||||
await expect(throwIfNotOk()).rejects.toThrow('Network Error');
|
||||
});
|
||||
|
||||
test('does a throw if it is given response that is not ok and the body is not parsable', async () => {
|
||||
fetchMock.mock('http://example.com', 500);
|
||||
const response = await fetch('http://example.com');
|
||||
await expect(throwIfNotOk(response)).rejects.toThrow('Network Error: Internal Server Error');
|
||||
});
|
||||
|
||||
test('does a throw and returns a body if it is parsable', async () => {
|
||||
fetchMock.mock('http://example.com', {
|
||||
status: 500,
|
||||
body: {
|
||||
statusCode: 500,
|
||||
message: 'I am a custom message',
|
||||
},
|
||||
});
|
||||
const response = await fetch('http://example.com');
|
||||
await expect(throwIfNotOk(response)).rejects.toThrow('I am a custom message');
|
||||
});
|
||||
|
||||
test('does NOT do a throw if it is given response is not ok', async () => {
|
||||
fetchMock.mock('http://example.com', 200);
|
||||
const response = await fetch('http://example.com');
|
||||
await expect(throwIfNotOk(response)).resolves.toEqual(undefined);
|
||||
});
|
||||
});
|
||||
});
|
|
@ -4,9 +4,7 @@
|
|||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
|
||||
import * as i18n from '../translations';
|
||||
import { StartServices } from '../../plugin';
|
||||
import { parseJsonFromBody, ToasterErrors } from './throw_if_not_ok';
|
||||
import { IndexPatternSavedObject, IndexPatternSavedObjectAttributes } from '../types';
|
||||
|
||||
/**
|
||||
|
@ -25,24 +23,3 @@ export const getIndexPatterns = async (
|
|||
|
||||
return response.savedObjects;
|
||||
};
|
||||
|
||||
export const throwIfNotOk = async (response?: Response): Promise<void> => {
|
||||
if (!response) {
|
||||
throw new ToasterErrors([i18n.NETWORK_ERROR]);
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
const body = await parseJsonFromBody(response);
|
||||
if (body != null && body.message) {
|
||||
if (body.statusCode != null) {
|
||||
throw new ToasterErrors([body.message, `${i18n.STATUS_CODE} ${body.statusCode}`]);
|
||||
} else if (body.status_code != null) {
|
||||
throw new ToasterErrors([body.message, `${i18n.STATUS_CODE} ${body.status_code}`]);
|
||||
} else {
|
||||
throw new ToasterErrors([body.message]);
|
||||
}
|
||||
} else {
|
||||
throw new ToasterErrors([`${i18n.NETWORK_ERROR} ${response.statusText}`]);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
@ -7,8 +7,7 @@
|
|||
import { useEffect, useState } from 'react';
|
||||
|
||||
import { useKibana } from '../lib/kibana';
|
||||
import { useStateToaster } from '../components/toasters';
|
||||
import { errorToToaster } from '../components/ml/api/error_to_toaster';
|
||||
import { errorToToaster, useStateToaster } from '../components/toasters';
|
||||
|
||||
import * as i18n from './translations';
|
||||
import { IndexPatternSavedObject } from './types';
|
||||
|
|
|
@ -20,6 +20,7 @@ import {
|
|||
ActionToaster,
|
||||
displayErrorToast,
|
||||
displaySuccessToast,
|
||||
errorToToaster,
|
||||
} from '../../../../components/toasters';
|
||||
import { track, METRIC_TYPE, TELEMETRY_EVENT } from '../../../../lib/telemetry';
|
||||
|
||||
|
@ -50,9 +51,9 @@ export const duplicateRulesAction = async (
|
|||
displaySuccessToast(i18n.SUCCESSFULLY_DUPLICATED_RULES(ruleIds.length), dispatchToaster);
|
||||
}
|
||||
dispatch({ type: 'loadingRuleIds', ids: [], actionType: null });
|
||||
} catch (e) {
|
||||
} catch (error) {
|
||||
dispatch({ type: 'loadingRuleIds', ids: [], actionType: null });
|
||||
displayErrorToast(i18n.DUPLICATE_RULE_ERROR, [e.message], dispatchToaster);
|
||||
errorToToaster({ title: i18n.DUPLICATE_RULE_ERROR, error, dispatchToaster });
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -80,13 +81,13 @@ export const deleteRulesAction = async (
|
|||
} else if (onRuleDeleted) {
|
||||
onRuleDeleted();
|
||||
}
|
||||
} catch (e) {
|
||||
} catch (error) {
|
||||
dispatch({ type: 'loadingRuleIds', ids: [], actionType: null });
|
||||
displayErrorToast(
|
||||
i18n.BATCH_ACTION_DELETE_SELECTED_ERROR(ruleIds.length),
|
||||
[e.message],
|
||||
dispatchToaster
|
||||
);
|
||||
errorToToaster({
|
||||
title: i18n.BATCH_ACTION_DELETE_SELECTED_ERROR(ruleIds.length),
|
||||
error,
|
||||
dispatchToaster,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -5,17 +5,16 @@
|
|||
*/
|
||||
|
||||
import {
|
||||
Rule,
|
||||
RuleError,
|
||||
BulkRuleResponse,
|
||||
RuleResponseBuckets,
|
||||
} from '../../../../containers/detection_engine/rules';
|
||||
|
||||
/**
|
||||
* Separates rules/errors from bulk rules API response (create/update/delete)
|
||||
*
|
||||
* @param response Array<Rule | RuleError> from bulk rules API
|
||||
* @param response BulkRuleResponse from bulk rules API
|
||||
*/
|
||||
export const bucketRulesResponse = (response: Array<Rule | RuleError>) =>
|
||||
export const bucketRulesResponse = (response: BulkRuleResponse) =>
|
||||
response.reduce<RuleResponseBuckets>(
|
||||
(acc, cv): RuleResponseBuckets => {
|
||||
return 'error' in cv
|
||||
|
|
|
@ -26,6 +26,7 @@ import {
|
|||
displayErrorToast,
|
||||
displaySuccessToast,
|
||||
useStateToaster,
|
||||
errorToToaster,
|
||||
} from '../../../../../components/toasters';
|
||||
import * as i18n from './translations';
|
||||
|
||||
|
@ -83,9 +84,9 @@ export const ImportRuleModalComponent = ({
|
|||
|
||||
importComplete();
|
||||
cleanupAndCloseModal();
|
||||
} catch (e) {
|
||||
} catch (error) {
|
||||
cleanupAndCloseModal();
|
||||
displayErrorToast(i18n.IMPORT_FAILED, [e.message], dispatchToaster);
|
||||
errorToToaster({ title: i18n.IMPORT_FAILED, error, dispatchToaster });
|
||||
}
|
||||
}
|
||||
}, [selectedFiles, overwrite]);
|
||||
|
|
|
@ -8,7 +8,7 @@ import React, { useEffect, useRef } from 'react';
|
|||
import styled from 'styled-components';
|
||||
import { isFunction } from 'lodash/fp';
|
||||
import { exportRules } from '../../../../../containers/detection_engine/rules';
|
||||
import { displayErrorToast, useStateToaster } from '../../../../../components/toasters';
|
||||
import { useStateToaster, errorToToaster } from '../../../../../components/toasters';
|
||||
import * as i18n from './translations';
|
||||
|
||||
const InvisibleAnchor = styled.a`
|
||||
|
@ -65,7 +65,7 @@ export const RuleDownloaderComponent = ({
|
|||
}
|
||||
} catch (error) {
|
||||
if (isSubscribed) {
|
||||
displayErrorToast(i18n.EXPORT_FAILURE, [error.message], dispatchToaster);
|
||||
errorToToaster({ title: i18n.EXPORT_FAILURE, error, dispatchToaster });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,10 +15,12 @@ import { isEmpty } from 'lodash/fp';
|
|||
import styled from 'styled-components';
|
||||
import React, { useCallback, useState, useEffect } from 'react';
|
||||
|
||||
import * as i18n from '../../translations';
|
||||
import { enableRules } from '../../../../../containers/detection_engine/rules';
|
||||
import { enableRulesAction } from '../../all/actions';
|
||||
import { Action } from '../../all/reducer';
|
||||
import { useStateToaster } from '../../../../../components/toasters';
|
||||
import { useStateToaster, displayErrorToast } from '../../../../../components/toasters';
|
||||
import { bucketRulesResponse } from '../../all/helpers';
|
||||
|
||||
const StaticSwitch = styled(EuiSwitch)`
|
||||
.euiSwitch__thumb,
|
||||
|
@ -62,13 +64,29 @@ export const RuleSwitchComponent = ({
|
|||
await enableRulesAction([id], event.target.checked!, dispatch, dispatchToaster);
|
||||
} else {
|
||||
try {
|
||||
const updatedRules = await enableRules({
|
||||
const enabling = event.target.checked!;
|
||||
const response = await enableRules({
|
||||
ids: [id],
|
||||
enabled: event.target.checked!,
|
||||
enabled: enabling,
|
||||
});
|
||||
setMyEnabled(updatedRules[0].enabled);
|
||||
if (onChange != null) {
|
||||
onChange(updatedRules[0].enabled);
|
||||
const { rules, errors } = bucketRulesResponse(response);
|
||||
|
||||
if (errors.length > 0) {
|
||||
setMyIsLoading(false);
|
||||
const title = enabling
|
||||
? i18n.BATCH_ACTION_ACTIVATE_SELECTED_ERROR(1)
|
||||
: i18n.BATCH_ACTION_DEACTIVATE_SELECTED_ERROR(1);
|
||||
displayErrorToast(
|
||||
title,
|
||||
errors.map(e => e.error.message),
|
||||
dispatchToaster
|
||||
);
|
||||
} else {
|
||||
const [rule] = rules;
|
||||
setMyEnabled(rule.enabled);
|
||||
if (onChange != null) {
|
||||
onChange(rule.enabled);
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
setMyIsLoading(false);
|
||||
|
|
|
@ -4,18 +4,15 @@
|
|||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
|
||||
export interface MessageBody {
|
||||
error?: string;
|
||||
message?: string;
|
||||
statusCode?: number;
|
||||
status_code?: number;
|
||||
import { has } from 'lodash/fp';
|
||||
|
||||
export interface KibanaApiError {
|
||||
message: string;
|
||||
body: {
|
||||
message: string;
|
||||
status_code: number;
|
||||
};
|
||||
}
|
||||
|
||||
export const parseJsonFromBody = async (response: Response): Promise<MessageBody | null> => {
|
||||
try {
|
||||
const text = await response.text();
|
||||
return JSON.parse(text);
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
export const isApiError = (error: unknown): error is KibanaApiError =>
|
||||
has('message', error) && has('body.message', error) && has('body.status_code', error);
|
||||
|
|
|
@ -4,6 +4,4 @@
|
|||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
|
||||
export * from './get_index_error';
|
||||
export * from './post_index_error';
|
||||
export * from './privilege_user_error';
|
||||
export class BadRequestError extends Error {}
|
|
@ -8,6 +8,7 @@ import Boom from 'boom';
|
|||
|
||||
import { SavedObjectsFindResponse } from 'kibana/server';
|
||||
import { IRuleSavedAttributesSavedObjectAttributes, IRuleStatusAttributes } from '../rules/types';
|
||||
import { BadRequestError } from '../errors/bad_request_error';
|
||||
import {
|
||||
transformError,
|
||||
transformBulkError,
|
||||
|
@ -70,8 +71,8 @@ describe('utils', () => {
|
|||
});
|
||||
});
|
||||
|
||||
test('it detects a TypeError and returns a status code of 400 from that particular error type', () => {
|
||||
const error: TypeError = new TypeError('I have a type error');
|
||||
test('it detects a BadRequestError and returns a status code of 400 from that particular error type', () => {
|
||||
const error: BadRequestError = new BadRequestError('I have a type error');
|
||||
const transformed = transformError(error);
|
||||
expect(transformed).toEqual({
|
||||
message: 'I have a type error',
|
||||
|
@ -79,8 +80,8 @@ describe('utils', () => {
|
|||
});
|
||||
});
|
||||
|
||||
test('it detects a TypeError and returns a Boom status of 400', () => {
|
||||
const error: TypeError = new TypeError('I have a type error');
|
||||
test('it detects a BadRequestError and returns a Boom status of 400', () => {
|
||||
const error: BadRequestError = new BadRequestError('I have a type error');
|
||||
const transformed = transformError(error);
|
||||
expect(transformed).toEqual({
|
||||
message: 'I have a type error',
|
||||
|
@ -127,8 +128,8 @@ describe('utils', () => {
|
|||
expect(transformed).toEqual(expected);
|
||||
});
|
||||
|
||||
test('it detects a TypeError and returns a Boom status of 400', () => {
|
||||
const error: TypeError = new TypeError('I have a type error');
|
||||
test('it detects a BadRequestError and returns a Boom status of 400', () => {
|
||||
const error: BadRequestError = new BadRequestError('I have a type error');
|
||||
const transformed = transformBulkError('rule-1', error);
|
||||
const expected: BulkError = {
|
||||
rule_id: 'rule-1',
|
||||
|
@ -279,8 +280,8 @@ describe('utils', () => {
|
|||
expect(transformed).toEqual(expected);
|
||||
});
|
||||
|
||||
test('it detects a TypeError and returns a Boom status of 400', () => {
|
||||
const error: TypeError = new TypeError('I have a type error');
|
||||
test('it detects a BadRequestError and returns a Boom status of 400', () => {
|
||||
const error: BadRequestError = new BadRequestError('I have a type error');
|
||||
const transformed = transformImportError('rule-1', error, {
|
||||
success_count: 1,
|
||||
success: false,
|
||||
|
|
|
@ -13,6 +13,7 @@ import {
|
|||
KibanaResponseFactory,
|
||||
CustomHttpResponseOptions,
|
||||
} from '../../../../../../../../src/core/server';
|
||||
import { BadRequestError } from '../errors/bad_request_error';
|
||||
|
||||
export interface OutputError {
|
||||
message: string;
|
||||
|
@ -31,9 +32,8 @@ export const transformError = (err: Error & { statusCode?: number }): OutputErro
|
|||
message: err.message,
|
||||
statusCode: err.statusCode,
|
||||
};
|
||||
} else if (err instanceof TypeError) {
|
||||
// allows us to throw type errors instead of booms in some conditions
|
||||
// where we don't want to mingle Boom with the rest of the code
|
||||
} else if (err instanceof BadRequestError) {
|
||||
// allows us to throw request validation errors in the absence of Boom
|
||||
return {
|
||||
message: err.message,
|
||||
statusCode: 400,
|
||||
|
@ -178,7 +178,7 @@ export const transformImportError = (
|
|||
message: err.message,
|
||||
existingImportSuccessError,
|
||||
});
|
||||
} else if (err instanceof TypeError) {
|
||||
} else if (err instanceof BadRequestError) {
|
||||
return createImportErrorObject({
|
||||
ruleId,
|
||||
statusCode: 400,
|
||||
|
@ -205,7 +205,7 @@ export const transformBulkError = (
|
|||
statusCode: err.output.statusCode,
|
||||
message: err.message,
|
||||
});
|
||||
} else if (err instanceof TypeError) {
|
||||
} else if (err instanceof BadRequestError) {
|
||||
return createBulkErrorObject({
|
||||
ruleId,
|
||||
statusCode: 400,
|
||||
|
|
|
@ -7,6 +7,7 @@ import { Readable } from 'stream';
|
|||
import { createRulesStreamFromNdJson } from './create_rules_stream_from_ndjson';
|
||||
import { createPromiseFromStreams } from 'src/legacy/utils/streams';
|
||||
import { ImportRuleAlertRest } from '../types';
|
||||
import { BadRequestError } from '../errors/bad_request_error';
|
||||
|
||||
type PromiseFromStreams = ImportRuleAlertRest | Error;
|
||||
|
||||
|
@ -331,7 +332,7 @@ describe('create_rules_stream_from_ndjson', () => {
|
|||
ndJsonStream,
|
||||
...rulesObjectsStream,
|
||||
]);
|
||||
const resultOrError = result as TypeError[];
|
||||
const resultOrError = result as BadRequestError[];
|
||||
expect(resultOrError[0]).toEqual({
|
||||
rule_id: 'rule-1',
|
||||
output_index: '.siem-signals',
|
||||
|
@ -383,7 +384,7 @@ describe('create_rules_stream_from_ndjson', () => {
|
|||
});
|
||||
});
|
||||
|
||||
test('non validated data is an instanceof TypeError', async () => {
|
||||
test('non validated data is an instanceof BadRequestError', async () => {
|
||||
const sample1 = getOutputSample();
|
||||
const sample2 = getOutputSample();
|
||||
sample2.rule_id = 'rule-2';
|
||||
|
@ -400,8 +401,8 @@ describe('create_rules_stream_from_ndjson', () => {
|
|||
ndJsonStream,
|
||||
...rulesObjectsStream,
|
||||
]);
|
||||
const resultOrError = result as TypeError[];
|
||||
expect(resultOrError[1] instanceof TypeError).toEqual(true);
|
||||
const resultOrError = result as BadRequestError[];
|
||||
expect(resultOrError[1] instanceof BadRequestError).toEqual(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -13,6 +13,7 @@ import {
|
|||
createConcatStream,
|
||||
} from '../../../../../../../../src/legacy/utils/streams';
|
||||
import { importRulesSchema } from '../routes/schemas/import_rules_schema';
|
||||
import { BadRequestError } from '../errors/bad_request_error';
|
||||
|
||||
export interface RulesObjectsExportResultDetails {
|
||||
/** number of successfully exported objects */
|
||||
|
@ -42,7 +43,7 @@ export const validateRules = (): Transform => {
|
|||
if (!(obj instanceof Error)) {
|
||||
const validated = importRulesSchema.validate(obj);
|
||||
if (validated.error != null) {
|
||||
return new TypeError(validated.error.message);
|
||||
return new BadRequestError(validated.error.message);
|
||||
} else {
|
||||
return validated.value;
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
|
||||
import { PrepackagedRules } from '../types';
|
||||
import { addPrepackagedRulesSchema } from '../routes/schemas/add_prepackaged_rules_schema';
|
||||
import { BadRequestError } from '../errors/bad_request_error';
|
||||
import { rawRules } from './prepackaged_rules';
|
||||
|
||||
/**
|
||||
|
@ -19,7 +20,7 @@ export const validateAllPrepackagedRules = (rules: PrepackagedRules[]): Prepacka
|
|||
if (validatedRule.error != null) {
|
||||
const ruleName = rule.name ? rule.name : '(rule name unknown)';
|
||||
const ruleId = rule.rule_id ? rule.rule_id : '(rule rule_id unknown)';
|
||||
throw new TypeError(
|
||||
throw new BadRequestError(
|
||||
`name: "${ruleName}", rule_id: "${ruleId}" within the folder rules/prepackaged_rules ` +
|
||||
`is not a valid detection engine rule. Expect the system ` +
|
||||
`to not work with pre-packaged rules until this rule is fixed ` +
|
||||
|
|
|
@ -14,6 +14,7 @@ import {
|
|||
IIndexPattern,
|
||||
} from '../../../../../../../../src/plugins/data/server';
|
||||
import { PartialFilter, RuleAlertParams } from '../types';
|
||||
import { BadRequestError } from '../errors/bad_request_error';
|
||||
|
||||
export const getQueryFilter = (
|
||||
query: string,
|
||||
|
@ -74,7 +75,7 @@ export const getFilter = async ({
|
|||
if (query != null && language != null && index != null) {
|
||||
return getQueryFilter(query, language, filters || [], index);
|
||||
} else {
|
||||
throw new TypeError('query, filters, and index parameter should be defined');
|
||||
throw new BadRequestError('query, filters, and index parameter should be defined');
|
||||
}
|
||||
}
|
||||
case 'saved_query': {
|
||||
|
@ -103,7 +104,7 @@ export const getFilter = async ({
|
|||
}
|
||||
}
|
||||
} else {
|
||||
throw new TypeError('savedId parameter should be defined');
|
||||
throw new BadRequestError('savedId parameter should be defined');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue