Skip to content

Commit

Permalink
[Security Solution] [Endpoint] Creates generic policy tab artifact co…
Browse files Browse the repository at this point in the history
…mponent to be used for all of our artifacts (#126685)

* Initial commit adding a generic component for policy artifact tabs

* Adds labels and translations. Also fixes loading state using isRefetching

* Fix checks and wrong count update when removing/adding items

* Fixes translations and adds extra privileges checks

* Use hook to retrieve url params instead of redux

* Fixes wrong loading state in flyout

* Extracts conditional artifact logic from generic components and adds an artifact_layout unit test

* Include new changes in policy tabs component

* Adds policy artifact flyout unit test

* Adds policy artifacts list unit test

* Adds policy artifacts delete modal unit test

* Adds external privileges checks on unit tests

* Uses FormattedMessage to include EuiLink inside the translation

* Uses FormattedMessage

* Generate new ExceptionsListApiClient instance when http changes

* Removes existing policy tab artifacts code in favor of generic component

* Update translation files

* Include pr suggestions

* Uses useUrlPagination hook for pagination

* Fix checks

* Fixes uni test

* Reorder use_list_artifact hook params and move custom getInstance functions inside a useCallback

* Fix typos and added pr feedback

* Fix typo in searchableFields props and vars

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
dasansol92 and kibanamachine authored Mar 10, 2022
1 parent aef0731 commit 776fe46
Show file tree
Hide file tree
Showing 83 changed files with 2,289 additions and 7,343 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('List artifact hook', () => {

result = await renderQuery(
() =>
useListArtifact(instance, searchableFields, options, {
useListArtifact(instance, options, searchableFields, {
onSuccess: onSuccessMock,
retry: false,
}),
Expand Down Expand Up @@ -92,7 +92,7 @@ describe('List artifact hook', () => {

result = await renderQuery(
() =>
useListArtifact(instance, searchableFields, options, {
useListArtifact(instance, options, searchableFields, {
onError: onErrorMock,
retry: false,
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,45 @@
import { FoundExceptionListItemSchema } from '@kbn/securitysolution-io-ts-list-types';
import { HttpFetchError } from 'kibana/public';
import { QueryObserverResult, useQuery, UseQueryOptions } from 'react-query';
import { DEFAULT_EXCEPTION_LIST_ITEM_SEARCHABLE_FIELDS } from '../../../../common/endpoint/service/artifacts/constants';
import { MaybeImmutable } from '../../../../common/endpoint/types';
import { MANAGEMENT_DEFAULT_PAGE, MANAGEMENT_DEFAULT_PAGE_SIZE } from '../../common/constants';
import { parsePoliciesAndFilterToKql, parseQueryFilterToKQL } from '../../common/utils';
import { ExceptionsListApiClient } from '../../services/exceptions_list/exceptions_list_api_client';

const DEFAULT_OPTIONS = Object.freeze({});

export function useListArtifact(
exceptionListApiClient: ExceptionsListApiClient,
searcheableFields: string[],
options: {
filter: string;
page: number;
perPage: number;
policies: string[];
} = {
options: Partial<{
filter?: string;
page?: number;
perPage?: number;
policies?: string[];
excludedPolicies?: string[];
}> = {
filter: '',
page: MANAGEMENT_DEFAULT_PAGE,
page: MANAGEMENT_DEFAULT_PAGE + 1,
perPage: MANAGEMENT_DEFAULT_PAGE_SIZE,
policies: [],
excludedPolicies: [],
},
customQueryOptions: UseQueryOptions<FoundExceptionListItemSchema, HttpFetchError>
searchableFields: MaybeImmutable<string[]> = DEFAULT_EXCEPTION_LIST_ITEM_SEARCHABLE_FIELDS,
customQueryOptions: Partial<
UseQueryOptions<FoundExceptionListItemSchema, HttpFetchError>
> = DEFAULT_OPTIONS,
customQueryIds: string[] = []
): QueryObserverResult<FoundExceptionListItemSchema, HttpFetchError> {
const { filter, page, perPage, policies } = options;
const { filter, page, perPage, policies, excludedPolicies } = options;

return useQuery<FoundExceptionListItemSchema, HttpFetchError>(
['list', exceptionListApiClient, options],
[...customQueryIds, 'list', exceptionListApiClient, options],
() => {
return exceptionListApiClient.find({
filter: parsePoliciesAndFilterToKql({
policies,
kuery: parseQueryFilterToKQL(filter, searcheableFields),
excludedPolicies,
kuery: parseQueryFilterToKQL(filter, searchableFields),
}),
perPage,
page,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@ import { QueryObserverResult, useQuery, UseQueryOptions } from 'react-query';
import { parsePoliciesAndFilterToKql, parseQueryFilterToKQL } from '../../common/utils';
import { ExceptionsListApiClient } from '../../services/exceptions_list/exceptions_list_api_client';

const DEFAULT_OPTIONS = Object.freeze({});

export function useSummaryArtifact(
exceptionListApiClient: ExceptionsListApiClient,
searcheableFields: string[],
searchableFields: string[],
options: {
filter: string;
policies: string[];
} = {
filter: '',
policies: [],
},
customQueryOptions: UseQueryOptions<ExceptionListSummarySchema, HttpFetchError>
customQueryOptions: UseQueryOptions<ExceptionListSummarySchema, HttpFetchError> = DEFAULT_OPTIONS
): QueryObserverResult<ExceptionListSummarySchema, HttpFetchError> {
const { filter, policies } = options;

Expand All @@ -30,7 +32,7 @@ export function useSummaryArtifact(
return exceptionListApiClient.summary(
parsePoliciesAndFilterToKql({
policies,
kuery: parseQueryFilterToKQL(filter, searcheableFields),
kuery: parseQueryFilterToKQL(filter, searchableFields),
})
);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ import {
ENDPOINT_HOST_ISOLATION_EXCEPTIONS_LIST_NAME,
} from '@kbn/securitysolution-list-constants';

export const SEARCHABLE_FIELDS: Readonly<string[]> = [
`item_id`,
`name`,
`description`,
`entries.value`,
];

export const HOST_ISOLATION_EXCEPTIONS_LIST_TYPE: ExceptionListType =
ExceptionListTypeEnum.ENDPOINT_HOST_ISOLATION_EXCEPTIONS;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
} from '../../../common/constants';
import { getHostIsolationExceptionsListPath } from '../../../common/routing';
import { parsePoliciesAndFilterToKql, parseQueryFilterToKQL } from '../../../common/utils';
import { SEARCHABLE_FIELDS } from '../constants';
import {
getHostIsolationExceptionItems,
getHostIsolationExceptionSummary,
Expand Down Expand Up @@ -85,8 +86,6 @@ export function useCanSeeHostIsolationExceptionsMenu(): boolean {
return canSeeMenu;
}

const SEARCHABLE_FIELDS: Readonly<string[]> = [`item_id`, `name`, `description`, `entries.value`];

export function useFetchHostIsolationExceptionsList({
filter,
page,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@
*/

import { PolicySettingsAction } from './policy_settings_action';
import { PolicyTrustedAppsAction } from './policy_trusted_apps_action';

export type PolicyDetailsAction = PolicySettingsAction | PolicyTrustedAppsAction;
export type PolicyDetailsAction = PolicySettingsAction;

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,18 @@

import { ImmutableMiddlewareFactory } from '../../../../../../common/store';
import { MiddlewareRunnerContext, PolicyDetailsState } from '../../../types';
import { policyTrustedAppsMiddlewareRunner } from './policy_trusted_apps_middleware';
import { policySettingsMiddlewareRunner } from './policy_settings_middleware';
import { TrustedAppsHttpService } from '../../../../trusted_apps/service';

export const policyDetailsMiddlewareFactory: ImmutableMiddlewareFactory<PolicyDetailsState> = (
coreStart
) => {
// Initialize services needed by Policy middleware
const trustedAppsService = new TrustedAppsHttpService(coreStart.http);
const middlewareContext: MiddlewareRunnerContext = {
coreStart,
trustedAppsService,
};

return (store) => (next) => async (action) => {
next(action);

policySettingsMiddlewareRunner(middlewareContext, store, action);
policyTrustedAppsMiddlewareRunner(middlewareContext, store, action);
};
};
Loading

0 comments on commit 776fe46

Please sign in to comment.