Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
95260d5
renaming route to configurations
opauloh Mar 1, 2023
500ea44
moving findings to configurations
opauloh Mar 1, 2023
b59284f
adding tabs to findings page
opauloh Mar 1, 2023
57ebe16
adding vulnerabilities tab content placeholder
opauloh Mar 1, 2023
3365ef7
fix i18n
opauloh Mar 1, 2023
01bb8fe
remove unused translation
opauloh Mar 1, 2023
191c44a
remove unused translation
opauloh Mar 1, 2023
5e2385c
WIP: vulnerabilities table
opauloh Mar 7, 2023
4857900
fixing test subj placement
opauloh Mar 7, 2023
bffd4a6
Merge branch 'main' into vuln/split-findings-page
opauloh Mar 8, 2023
b5f2b9b
Merge branch 'vuln/split-findings-page' into vuln/table
opauloh Mar 9, 2023
1638246
Merge branch 'main' into vuln/table
opauloh Mar 21, 2023
2495264
WIP: fix update index safe
opauloh Mar 24, 2023
70491f5
Merge branch 'main' into vuln/table
opauloh Mar 28, 2023
091e91f
wip moving common files
opauloh Apr 3, 2023
0abf0cb
updating index pattern
opauloh Apr 3, 2023
a594a3e
wip: moving common files
opauloh Apr 3, 2023
b8ef0b0
wip: vulnerability table
opauloh Apr 3, 2023
f44a72c
Merge branch 'main' into vuln/table
opauloh Apr 3, 2023
87f7165
data view index pattern
opauloh Apr 4, 2023
f6fc8a0
add new helper hook
opauloh Apr 4, 2023
befe2c3
add posture table helper hook
opauloh Apr 4, 2023
c1a0de4
moving files
opauloh Apr 4, 2023
b9fb3ec
updating latest findings table to new hook
opauloh Apr 4, 2023
6b0f54f
moving files
opauloh Apr 4, 2023
87623e8
vulnerabilities
opauloh Apr 4, 2023
c286e9d
add no results components
opauloh Apr 4, 2023
4f575b3
adding method description
opauloh Apr 4, 2023
f0c06bc
adding wrapper and removing null
opauloh Apr 4, 2023
9c4c3cc
fix casing
opauloh Apr 4, 2023
7d7ed66
add badges
opauloh Apr 4, 2023
7b816e3
Merge branch 'main' into vuln/table
opauloh Apr 5, 2023
4d873cc
Merge branch 'main' into vuln/table
opauloh Apr 10, 2023
89d20cc
update vulnerability schema
opauloh Apr 11, 2023
016f8c8
remove message
opauloh Apr 11, 2023
070a839
updating vulnerabilities utils
opauloh Apr 11, 2023
1b7afe8
add translate and handle plural
opauloh Apr 11, 2023
6c6e89f
Merge branch 'main' into vuln/table
opauloh Apr 12, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion x-pack/plugins/cloud_security_posture/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ export const VULNERABILITIES_INDEX_DEFAULT_NS =
export const LATEST_VULNERABILITIES_INDEX_TEMPLATE_NAME =
'logs-cloud_security_posture.vulnerabilities_latest';
export const LATEST_VULNERABILITIES_INDEX_PATTERN =
'logs-cloud_security_posture.vulnerabilities_latest-*';
'logs-cloud_security_posture.vulnerabilities_latest*';
Comment thread
opauloh marked this conversation as resolved.
export const LATEST_VULNERABILITIES_INDEX_DEFAULT_NS =
'logs-cloud_security_posture.vulnerabilities_latest-default';
export const DATA_VIEW_INDEX_PATTERN = 'logs-*';

export const CSP_INGEST_TIMESTAMP_PIPELINE = 'cloud_security_posture_add_ingest_timestamp_pipeline';
export const CSP_LATEST_FINDINGS_INGEST_TIMESTAMP_PIPELINE =
Expand Down
4 changes: 0 additions & 4 deletions x-pack/plugins/cloud_security_posture/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,3 @@ export type BenchmarkName = CspRuleTemplateMetadata['benchmark']['name'];
export type PostureInput = typeof SUPPORTED_CLOUDBEAT_INPUTS[number];
export type CloudSecurityPolicyTemplate = typeof SUPPORTED_POLICY_TEMPLATES[number];
export type PosturePolicyTemplate = Extract<CloudSecurityPolicyTemplate, 'kspm' | 'cspm'>;

// Vulnerability Integration Types
export type CVSSVersion = '2.0' | '3.0';
export type SeverityStatus = 'Low' | 'Medium' | 'High' | 'Critical';
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { useQuery } from '@tanstack/react-query';
import { useKibana } from '@kbn/kibana-react-plugin/public';
import type { DataView } from '@kbn/data-plugin/common';
import { DATA_VIEW_INDEX_PATTERN } from '../../../common/constants';
import { CspClientPluginStartDeps } from '../../types';

/**
* Returns the common logs-* data view with fields filtered by
* fields present in the given index pattern
*/
export const useFilteredDataView = (indexPattern: string) => {
const {
data: { dataViews },
} = useKibana<CspClientPluginStartDeps>().services;

const findDataView = async (): Promise<DataView> => {
const dataView = (await dataViews.find(DATA_VIEW_INDEX_PATTERN))?.[0];
if (!dataView) {
throw new Error('Findings data view not found');
}

const indexPatternFields = await dataViews.getFieldsForWildcard({
pattern: indexPattern,
});

if (!indexPatternFields) {
throw new Error('Error fetching fields for the index pattern');
}

dataView.fields = dataView.fields.filter((field) =>
indexPatternFields.some((indexPatternField) => indexPatternField.name === field.name)
) as DataView['fields'];

return dataView;
};

return useQuery(['latest_findings_data_view', indexPattern], findDataView);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

export * from './use_cloud_posture_table';
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { useCallback } from 'react';
import { type DataView } from '@kbn/data-views-plugin/common';
import { useUrlQuery } from '../use_url_query';
import { usePageSize } from '../use_page_size';
import { getDefaultQuery, useBaseEsQuery, usePersistedQuery } from './utils';

/*
Hook for managing common table state and methods for Cloud Posture
*/
export const useCloudPostureTable = ({
defaultQuery = getDefaultQuery,
dataView,
paginationLocalStorageKey,
}: {
defaultQuery?: (params: any) => any;
dataView: DataView;
paginationLocalStorageKey: string;
}) => {
const getPersistedDefaultQuery = usePersistedQuery(defaultQuery);
const { urlQuery, setUrlQuery } = useUrlQuery(getPersistedDefaultQuery);
const { pageSize, setPageSize } = usePageSize(paginationLocalStorageKey);

const onChangeItemsPerPage = useCallback(
(newPageSize) => {
setPageSize(newPageSize);
setUrlQuery({
pageIndex: 0,
});
},
[setUrlQuery, setPageSize]
);

const onChangePage = useCallback(
(newPageIndex) => {
setUrlQuery({
pageIndex: newPageIndex,
});
},
[setUrlQuery]
);

const onSort = useCallback(
(sort) => {
setUrlQuery({
sort,
});
},
[setUrlQuery]
);

const setTableOptions = useCallback(
({ page, sort }) => {
setPageSize(page.size);
setUrlQuery({
sort,
pageIndex: page.index,
});
},
[setUrlQuery, setPageSize]
);

/**
* Page URL query to ES query
*/
const baseEsQuery = useBaseEsQuery({
dataView,
filters: urlQuery.filters,
query: urlQuery.query,
});

const handleUpdateQuery = useCallback(
(query) => {
setUrlQuery({ ...query, pageIndex: 0 });
},
[setUrlQuery]
);

return {
setUrlQuery,
sort: urlQuery.sort,
filters: urlQuery.filters,
query: baseEsQuery.query,
queryError: baseEsQuery.error,
pageIndex: urlQuery.pageIndex,
urlQuery,
setTableOptions,
handleUpdateQuery,
pageSize,
setPageSize,
onChangeItemsPerPage,
onChangePage,
onSort,
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { useEffect, useCallback, useMemo } from 'react';
import { buildEsQuery } from '@kbn/es-query';
import type { EuiBasicTableProps, Pagination } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { type Query } from '@kbn/es-query';
import { useKibana } from '../use_kibana';
import type { FindingsBaseProps, FindingsBaseURLQuery } from '../../types';

const getBaseQuery = ({ dataView, query, filters }: FindingsBaseURLQuery & FindingsBaseProps) => {
try {
return {
query: buildEsQuery(dataView, query, filters), // will throw for malformed query
};
} catch (error) {
return {
query: undefined,
error: error instanceof Error ? error : new Error('Unknown Error'),
};
}
};

type TablePagination = NonNullable<EuiBasicTableProps<unknown>['pagination']>;

export const getPaginationTableParams = (
params: TablePagination & Pick<Required<TablePagination>, 'pageIndex' | 'pageSize'>,
pageSizeOptions = [10, 25, 100],
showPerPageOptions = true
): Required<TablePagination> => ({
...params,
pageSizeOptions,
showPerPageOptions,
});

export const getPaginationQuery = ({
pageIndex,
pageSize,
}: Pick<Pagination, 'pageIndex' | 'pageSize'>) => ({
from: pageIndex * pageSize,
size: pageSize,
});

export const useBaseEsQuery = ({
dataView,
filters,
query,
}: FindingsBaseURLQuery & FindingsBaseProps) => {
const {
notifications: { toasts },
data: {
query: { filterManager, queryString },
},
} = useKibana().services;

const baseEsQuery = useMemo(
() => getBaseQuery({ dataView, filters, query }),
[dataView, filters, query]
);

/**
* Sync filters with the URL query
*/
useEffect(() => {
filterManager.setAppFilters(filters);
queryString.setQuery(query);
}, [filters, filterManager, queryString, query]);

const handleMalformedQueryError = () => {
const error = baseEsQuery.error;
if (error) {
toasts.addError(error, {
title: i18n.translate('xpack.csp.findings.search.queryErrorToastMessage', {
defaultMessage: 'Query Error',
}),
toastLifeTimeMs: 1000 * 5,
});
}
};

useEffect(handleMalformedQueryError, [baseEsQuery.error, toasts]);

return baseEsQuery;
};

export const usePersistedQuery = <T>(getter: ({ filters, query }: FindingsBaseURLQuery) => T) => {
const {
data: {
query: { filterManager, queryString },
},
} = useKibana().services;

return useCallback(
() =>
getter({
filters: filterManager.getAppFilters(),
query: queryString.getQuery() as Query,
}),
[getter, filterManager, queryString]
);
};

export const getDefaultQuery = ({ query, filters }: any): any => ({
query,
filters,
sort: { field: '@timestamp', direction: 'desc' },
pageIndex: 0,
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import type { Criteria } from '@elastic/eui';
import type { DataView } from '@kbn/data-views-plugin/common';
import type { BoolQuery, Filter, Query } from '@kbn/es-query';
import { CspFinding } from '../../../common/schemas/csp_finding';
import { CspFinding } from '../../common/schemas/csp_finding';

export type FindingsGroupByKind = 'default' | 'resource';

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

import { useMemo } from 'react';
import { MAX_FINDINGS_TO_LOAD } from '../../../common/constants';
import { MAX_FINDINGS_TO_LOAD } from '../constants';

export const getLimitProperties = (
totalItems: number,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import type { CoreStart } from '@kbn/core/public';
import { i18n } from '@kbn/i18n';
import { extractErrorMessage } from '../../../common/utils/helpers';

const SEARCH_FAILED_TEXT = i18n.translate(
'xpack.csp.findings.findingsErrorToast.searchFailedTitle',
{ defaultMessage: 'Search failed' }
);

export const showErrorToast = (
toasts: CoreStart['notifications']['toasts'],
error: unknown
): void => {
if (error instanceof Error) toasts.addError(error, { title: SEARCH_FAILED_TEXT });
else toasts.addDanger(extractErrorMessage(error, SEARCH_FAILED_TEXT));
};
Loading