-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Cloud Posture] fix negated filter in findings (#137021)
- Loading branch information
Showing
6 changed files
with
129 additions
and
41 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
x-pack/plugins/cloud_security_posture/public/pages/findings/get_filters.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* 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 { CSP_LATEST_FINDINGS_DATA_VIEW } from '../../../common/constants'; | ||
import { createStubDataView } from '@kbn/data-views-plugin/common/stubs'; | ||
import { DataView } from '@kbn/data-views-plugin/common'; | ||
import { getFilters } from './get_filters'; | ||
|
||
describe('Get Filters', () => { | ||
let dataViewMock: DataView; | ||
const fieldKey = 'some_field_name'; | ||
|
||
beforeEach(() => { | ||
dataViewMock = createStubDataView({ | ||
spec: { | ||
id: CSP_LATEST_FINDINGS_DATA_VIEW, | ||
fields: { | ||
a: { | ||
searchable: false, | ||
aggregatable: false, | ||
name: fieldKey, | ||
type: 'type', | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it('negate an existing filter', () => { | ||
const fields = { | ||
dataView: dataViewMock, | ||
field: fieldKey, | ||
value: 'b', | ||
}; | ||
const initialFilters = getFilters({ | ||
...fields, | ||
filters: [], | ||
negate: false, | ||
}); | ||
|
||
expect(initialFilters.length).toBe(1); | ||
expect(initialFilters[0].meta.negate).toBe(false); | ||
|
||
const nextFilters = getFilters({ | ||
...fields, | ||
filters: initialFilters, | ||
negate: true, | ||
}); | ||
|
||
expect(nextFilters.length).toBe(1); | ||
expect(nextFilters[0].meta.negate).toBe(true); | ||
}); | ||
}); |
65 changes: 65 additions & 0 deletions
65
x-pack/plugins/cloud_security_posture/public/pages/findings/get_filters.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* 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 Filter, | ||
buildFilter, | ||
FILTERS, | ||
FilterStateStore, | ||
compareFilters, | ||
FilterCompareOptions, | ||
} from '@kbn/es-query'; | ||
import type { Serializable } from '@kbn/utility-types'; | ||
import type { FindingsBaseProps } from './types'; | ||
|
||
const compareOptions: FilterCompareOptions = { | ||
negate: false, | ||
}; | ||
|
||
/** | ||
* adds a new filter to a new filters array | ||
* removes existing filter if negated filter is added | ||
* | ||
* @returns {Filter[]} a new array of filters to be added back to filterManager | ||
*/ | ||
export const getFilters = ({ | ||
filters: existingFilters, | ||
dataView, | ||
field, | ||
value, | ||
negate, | ||
}: { | ||
filters: Filter[]; | ||
dataView: FindingsBaseProps['dataView']; | ||
field: string; | ||
value: Serializable; | ||
negate: boolean; | ||
}): Filter[] => { | ||
const dataViewField = dataView.getFieldByName(field); | ||
if (!dataViewField) return existingFilters; | ||
|
||
const phraseFilter = buildFilter( | ||
dataView, | ||
dataViewField, | ||
FILTERS.PHRASE, | ||
negate, | ||
false, | ||
value, | ||
null, | ||
FilterStateStore.APP_STATE | ||
); | ||
|
||
const nextFilters = [ | ||
...existingFilters.filter( | ||
// Exclude existing filters that match the newly added 'phraseFilter' | ||
(filter) => !compareFilters(filter, phraseFilter, compareOptions) | ||
), | ||
phraseFilter, | ||
]; | ||
|
||
return nextFilters; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters