Skip to content

Commit

Permalink
load prepacked timelines by default
Browse files Browse the repository at this point in the history
  • Loading branch information
angorayc committed Jul 13, 2020
1 parent b7f294e commit 812756b
Show file tree
Hide file tree
Showing 26 changed files with 1,420 additions and 284 deletions.
19 changes: 19 additions & 0 deletions x-pack/plugins/security_solution/common/types/timeline/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ import * as runtimeTypes from 'io-ts';
import { stringEnum, unionWithNullType } from '../../utility_types';
import { NoteSavedObject, NoteSavedObjectToReturnRuntimeType } from './note';
import { PinnedEventToReturnSavedObjectRuntimeType, PinnedEventSavedObject } from './pinned_event';
import {
success,
// eslint-disable-next-line @typescript-eslint/camelcase
success_count,
} from '../../detection_engine/schemas/common/schemas';
import { PositiveInteger } from '../../detection_engine/schemas/types';
import { errorSchema } from '../../detection_engine/schemas/response/error_schema';

/*
* ColumnHeader Types
Expand Down Expand Up @@ -379,3 +386,15 @@ export type NotesAndPinnedEventsByTimelineId = Record<
string,
{ notes: NoteSavedObject[]; pinnedEvents: PinnedEventSavedObject[] }
>;

export const importTimelineResultSchema = runtimeTypes.exact(
runtimeTypes.type({
success,
success_count,
timelines_installed: PositiveInteger,
timelines_updated: PositiveInteger,
errors: runtimeTypes.array(errorSchema),
})
);

export type ImportTimelineResultSchema = runtimeTypes.TypeOf<typeof importTimelineResultSchema>;
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,12 @@ export const StatefulOpenTimelineComponent = React.memo<OpenTimelineOwnProps>(
defaultTimelineCount,
templateTimelineCount,
});
const { timelineStatus, templateTimelineType, templateTimelineFilter } = useTimelineStatus({
const {
timelineStatus,
templateTimelineType,
templateTimelineFilter,
installPrepackagedTimelines,
} = useTimelineStatus({
timelineType,
customTemplateTimelineCount,
elasticTemplateTimelineCount,
Expand Down Expand Up @@ -287,7 +292,13 @@ export const StatefulOpenTimelineComponent = React.memo<OpenTimelineOwnProps>(
focusInput();
}, []);

useEffect(() => refetch(), [refetch]);
useEffect(() => {
const fetchData = async () => {
await installPrepackagedTimelines();
refetch();
};
fetchData();
}, [refetch, installPrepackagedTimelines]);

return !isModal ? (
<OpenTimeline
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {

import * as i18n from './translations';
import { TemplateTimelineFilter } from './types';
import { installPrepackedTimelines } from '../../containers/api';

export const useTimelineStatus = ({
timelineType,
Expand All @@ -30,6 +31,7 @@ export const useTimelineStatus = ({
timelineStatus: TimelineStatusLiteralWithNull;
templateTimelineType: TemplateTimelineTypeLiteralWithNull;
templateTimelineFilter: JSX.Element[] | null;
installPrepackagedTimelines: () => void;
} => {
const [selectedTab, setSelectedTab] = useState<TemplateTimelineTypeLiteralWithNull>(
TemplateTimelineType.elastic
Expand Down Expand Up @@ -101,9 +103,16 @@ export const useTimelineStatus = ({
: null;
}, [templateTimelineType, filters, isTemplateFilterEnabled, onFilterClicked]);

const installPrepackagedTimelines = useCallback(async () => {
if (templateTimelineType === TemplateTimelineType.elastic) {
await installPrepackedTimelines();
}
}, [templateTimelineType]);

return {
timelineStatus,
templateTimelineType,
templateTimelineFilter,
installPrepackagedTimelines,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ import {
TimelineStatus,
TimelineErrorResponseType,
TimelineErrorResponse,
ImportTimelineResultSchema,
importTimelineResultSchema,
} from '../../../common/types/timeline';
import { TimelineInput, TimelineType } from '../../graphql/types';
import {
TIMELINE_URL,
TIMELINE_DRAFT_URL,
TIMELINE_IMPORT_URL,
TIMELINE_EXPORT_URL,
TIMELINE_PREPACKAGED_URL,
} from '../../../common/constants';

import { KibanaServices } from '../../common/lib/kibana';
Expand Down Expand Up @@ -56,6 +59,12 @@ const decodeTimelineErrorResponse = (respTimeline?: TimelineErrorResponse) =>
fold(throwErrors(createToasterPlainError), identity)
);

const decodePrepackedTimelineResponse = (respTimeline?: ImportTimelineResultSchema) =>
pipe(
importTimelineResultSchema.decode(respTimeline),
fold(throwErrors(createToasterPlainError), identity)
);

const postTimeline = async ({ timeline }: RequestPostTimeline): Promise<TimelineResponse> => {
const response = await KibanaServices.get().http.post<TimelineResponse>(TIMELINE_URL, {
method: 'POST',
Expand Down Expand Up @@ -200,3 +209,12 @@ export const cleanDraftTimeline = async ({

return decodeTimelineResponse(response);
};

export const installPrepackedTimelines = async (): Promise<ImportTimelineResultSchema> => {
const response = await KibanaServices.get().http.post<ImportTimelineResultSchema>(
TIMELINE_PREPACKAGED_URL,
{}
);

return decodePrepackedTimelineResponse(response);
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,30 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { IRouter } from '../../../../../../../../src/core/server';

import { validate } from '../../../../../common/validate';
import {
PrePackagedRulesAndTimelinesSchema,
prePackagedRulesAndTimelinesSchema,
} from '../../../../../common/detection_engine/schemas/response/prepackaged_rules_schema';
import { IRouter } from '../../../../../../../../src/core/server';
import { importTimelineResultSchema } from '../../../../../common/types/timeline';
import { DETECTION_ENGINE_PREPACKAGED_URL } from '../../../../../common/constants';

import { ConfigType } from '../../../../config';
import { SetupPlugins } from '../../../../plugin';
import { buildFrameworkRequest } from '../../../timeline/routes/utils/common';
import { installPrepackagedTimelines } from '../../../timeline/routes/utils/install_prepacked_timelines';

import { getIndexExists } from '../../index/get_index_exists';
import { transformError, buildSiemResponse } from '../utils';
import { getPrepackagedRules } from '../../rules/get_prepackaged_rules';
import { installPrepackagedRules } from '../../rules/install_prepacked_rules';
import { updatePrepackagedRules } from '../../rules/update_prepacked_rules';
import { getRulesToInstall } from '../../rules/get_rules_to_install';
import { getRulesToUpdate } from '../../rules/get_rules_to_update';
import { getExistingPrepackagedRules } from '../../rules/get_existing_prepackaged_rules';
import { ConfigType } from '../../../../config';
import { SetupPlugins } from '../../../../plugin';
import { buildFrameworkRequest } from '../../../timeline/routes/utils/common';
import { importTimelineResultSchema } from '../../../timeline/routes/schemas/import_timelines_schema';
import { installPrepackagedTimelines } from '../../../timeline/routes/utils/install_prepacked_timelines';

import { transformError, buildSiemResponse } from '../utils';

export const addPrepackedRulesRoute = (
router: IRouter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ import { getExistingPrepackagedRules } from '../../rules/get_existing_prepackage
import { buildFrameworkRequest } from '../../../timeline/routes/utils/common';
import { ConfigType } from '../../../../config';
import { SetupPlugins } from '../../../../plugin';
import {
checkTimelinesStatus,
checkTimelineStatusRt,
} from '../../../timeline/routes/utils/install_prepacked_timelines';
import { checkTimelinesStatus } from '../../../timeline/routes/utils/check_timelines_status';
import { checkTimelineStatusRt } from '../../../timeline/routes/schemas/check_timelines_status_schema';

export const getPrepackagedRulesStatusRoute = (
router: IRouter,
Expand Down
Loading

0 comments on commit 812756b

Please sign in to comment.