-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Cloud Security] Clicking on Contextual Flyout popout Icon now opens page in new tab #196763
Changes from 13 commits
82ff137
f09dce8
5dd8c78
ac0df66
9d2632e
e313319
35cc693
c63540f
19135a8
cdb9f8c
3303859
bf7e3ee
d32c49b
3a98256
41c2c90
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* 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 { CoreStart } from '@kbn/core-lifecycle-browser'; | ||
import { useKibana } from '@kbn/kibana-react-plugin/public'; | ||
import { useCallback } from 'react'; | ||
import { CspClientPluginStartDeps } from '../types'; | ||
import { NavFilter, encodeQueryUrl, queryFilters } from '../utils/query_utils'; | ||
|
||
export const useGetNavigationUrlParams = () => { | ||
const { services } = useKibana<CoreStart & CspClientPluginStartDeps>(); | ||
|
||
const getNavUrlParams = useCallback( | ||
( | ||
filterParams: NavFilter = {}, | ||
findingsType?: 'configurations' | 'vulnerabilities', | ||
groupBy?: string[] | ||
) => { | ||
const filters = queryFilters(filterParams); | ||
|
||
const searchParams = new URLSearchParams(encodeQueryUrl(services.data, filters, groupBy)); | ||
|
||
return `${findingsType ? findingsType : ''}?${searchParams.toString()}`; | ||
}, | ||
[services] | ||
); | ||
|
||
return getNavUrlParams; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,6 @@ | |
|
||
import { useCallback } from 'react'; | ||
import { useHistory } from 'react-router-dom'; | ||
import { Filter } from '@kbn/es-query'; | ||
import { | ||
SECURITY_DEFAULT_DATA_VIEW_ID, | ||
CDR_MISCONFIGURATIONS_DATA_VIEW_ID_PREFIX, | ||
|
@@ -17,64 +16,23 @@ import { useKibana } from '@kbn/kibana-react-plugin/public'; | |
import { findingsNavigation } from '../constants/navigation'; | ||
import { useDataView } from './use_data_view'; | ||
import { CspClientPluginStartDeps } from '../..'; | ||
import { encodeQuery } from '../utils/query_utils'; | ||
import { NavFilter, encodeQueryUrl, queryFilters } from '../utils/query_utils'; | ||
|
||
interface NegatedValue { | ||
value: string | number; | ||
negate: boolean; | ||
} | ||
|
||
type FilterValue = string | number | NegatedValue; | ||
|
||
export type NavFilter = Record<string, FilterValue>; | ||
|
||
const createFilter = (key: string, filterValue: FilterValue, dataViewId: string): Filter => { | ||
let negate = false; | ||
let value = filterValue; | ||
if (typeof filterValue === 'object') { | ||
negate = filterValue.negate; | ||
value = filterValue.value; | ||
} | ||
// If the value is '*', we want to create an exists filter | ||
if (value === '*') { | ||
return { | ||
query: { exists: { field: key } }, | ||
meta: { type: 'exists', index: dataViewId }, | ||
}; | ||
} | ||
return { | ||
meta: { | ||
alias: null, | ||
negate, | ||
disabled: false, | ||
type: 'phrase', | ||
key, | ||
index: dataViewId, | ||
}, | ||
query: { match_phrase: { [key]: value } }, | ||
}; | ||
}; | ||
// dataViewId is used to prevent FilterManager from falling back to the default in the sorcerer (logs-*) | ||
const useNavigate = (pathname: string, dataViewId = SECURITY_DEFAULT_DATA_VIEW_ID) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we don't need to fallback to |
||
const history = useHistory(); | ||
const { services } = useKibana<CoreStart & CspClientPluginStartDeps>(); | ||
|
||
const { services } = useKibana<CoreStart & CspClientPluginStartDeps>(); | ||
return useCallback( | ||
(filterParams: NavFilter = {}, groupBy?: string[]) => { | ||
const filters = Object.entries(filterParams).map(([key, filterValue]) => | ||
createFilter(key, filterValue, dataViewId) | ||
); | ||
const filters = queryFilters(filterParams, dataViewId); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I think the code is more readable when a function has some verb in the name, eg. |
||
|
||
history.push({ | ||
pathname, | ||
search: encodeQuery({ | ||
// Set query language from user's preference | ||
query: services.data.query.queryString.getDefaultQuery(), | ||
filters, | ||
...(groupBy && { groupBy }), | ||
}), | ||
search: encodeQueryUrl(services.data, filters, groupBy), | ||
}); | ||
}, | ||
[history, pathname, services.data.query.queryString, dataViewId] | ||
[dataViewId, history, pathname, services.data] | ||
); | ||
}; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* 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 { encodeQueryUrl, queryFilters } from './query_utils'; | ||
import { dataPluginMock } from '@kbn/data-plugin/public/mocks'; | ||
|
||
const DEFAULT_DATA_VIEW_ID = 'security-solution-default'; | ||
|
||
describe('queryFilters', () => { | ||
it('Should return correct filters given some filterParams', () => { | ||
const testFilterParams = { | ||
test_field: 'test_value', | ||
}; | ||
const testResult = [ | ||
{ | ||
meta: { | ||
alias: null, | ||
negate: false, | ||
disabled: false, | ||
type: 'phrase', | ||
key: 'test_field', | ||
index: DEFAULT_DATA_VIEW_ID, | ||
}, | ||
query: { match_phrase: { test_field: 'test_value' } }, | ||
}, | ||
]; | ||
expect(queryFilters(testFilterParams)).toEqual(testResult); | ||
}); | ||
|
||
it('Should return empty filters given empty filterParams', () => { | ||
expect(queryFilters({})).toEqual([]); | ||
}); | ||
|
||
it('Should return correct filters given some filterParams and dataviewId', () => { | ||
const testFilterParams = { | ||
test_field: 'test_value', | ||
}; | ||
const testResult = [ | ||
{ | ||
meta: { | ||
alias: null, | ||
negate: false, | ||
disabled: false, | ||
type: 'phrase', | ||
key: 'test_field', | ||
index: 'test-data-view', | ||
}, | ||
query: { match_phrase: { test_field: 'test_value' } }, | ||
}, | ||
]; | ||
expect(queryFilters(testFilterParams, 'test-data-view')).toEqual(testResult); | ||
}); | ||
}); | ||
|
||
describe('encodeQueryUrl', () => { | ||
const getServicesMock = () => ({ | ||
data: dataPluginMock.createStartContract(), | ||
}); | ||
|
||
it('Should return correct URL given empty filters', () => { | ||
const result = 'cspq=(filters:!())'; | ||
expect(encodeQueryUrl(getServicesMock().data, [])).toEqual(result); | ||
}); | ||
|
||
it('should return correct URL given filters', () => { | ||
const filter = [ | ||
{ | ||
meta: { | ||
alias: null, | ||
negate: false, | ||
disabled: false, | ||
type: 'phrase', | ||
key: 'test_field', | ||
index: DEFAULT_DATA_VIEW_ID, | ||
}, | ||
query: { match_phrase: { test_field: 'test_value' } }, | ||
}, | ||
]; | ||
const result = | ||
'cspq=(filters:!((meta:(alias:!n,disabled:!f,index:security-solution-default,key:test_field,negate:!f,type:phrase),query:(match_phrase:(test_field:test_value)))))'; | ||
expect(encodeQueryUrl(getServicesMock().data, filter)).toEqual(result); | ||
}); | ||
|
||
it('should return correct URL given filters and group by', () => { | ||
const filter = [ | ||
{ | ||
meta: { | ||
alias: null, | ||
negate: false, | ||
disabled: false, | ||
type: 'phrase', | ||
key: 'test_field', | ||
index: DEFAULT_DATA_VIEW_ID, | ||
}, | ||
query: { match_phrase: { test_field: 'test_value' } }, | ||
}, | ||
]; | ||
const groupByFilter = ['filterA']; | ||
const result = | ||
'cspq=(filters:!((meta:(alias:!n,disabled:!f,index:security-solution-default,key:test_field,negate:!f,type:phrase),query:(match_phrase:(test_field:test_value)))),groupBy:!(filterA))'; | ||
expect(encodeQueryUrl(getServicesMock().data, filter, groupByFilter)).toEqual(result); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,21 @@ | |
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { encode, decode } from '@kbn/rison'; | ||
import type { LocationDescriptorObject } from 'history'; | ||
import { Filter } from '@kbn/es-query'; | ||
import { SECURITY_DEFAULT_DATA_VIEW_ID } from '@kbn/cloud-security-posture-common'; | ||
import { DataPublicPluginStart } from '@kbn/data-plugin/public'; | ||
|
||
interface NegatedValue { | ||
value: string | number; | ||
negate: boolean; | ||
} | ||
|
||
type FilterValue = string | number | NegatedValue; | ||
|
||
export type NavFilter = Record<string, FilterValue>; | ||
|
||
const encodeRison = (v: any): string | undefined => { | ||
try { | ||
|
@@ -38,3 +51,51 @@ export const decodeQuery = <T extends unknown>(search?: string): Partial<T> | un | |
if (!risonQuery) return; | ||
return decodeRison<T>(risonQuery); | ||
}; | ||
|
||
export const encodeQueryUrl = ( | ||
maxcold marked this conversation as resolved.
Show resolved
Hide resolved
|
||
servicesStart: DataPublicPluginStart, | ||
filters: Filter[], | ||
groupBy?: string[] | ||
): any => { | ||
return encodeQuery({ | ||
query: servicesStart.query.queryString.getDefaultQuery(), | ||
filters, | ||
...(groupBy && { groupBy }), | ||
}); | ||
}; | ||
|
||
export const queryFilters = ( | ||
filterParams: NavFilter = {}, | ||
dataViewId = SECURITY_DEFAULT_DATA_VIEW_ID | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand the use of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey that was added on this PR If we don't specify the DataView on the filter when navigating to the findings page, the FilterManager will fallback to the default in the sorcerer ( So the fix was to specify the Dataview for each filter, and then the edit filter works: Should probably have added it as a comment, which I suggest @animehart to add so we can recall when going over it in the future. Another solution could be to update the navigation to set the sorcerer index pattern instead of setting it per filter, but that also involves setting a Dataview in the navigation logic. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the detailed clarification, makes sense! Agree that we need to add a comment as this Data View logic pieces are quite complicated to grasp without all the context. I think this logic should also be added to the vulnerability navigation logic, right now it looks like it is not part of it, unless I missed that in the code There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @animehart we still don't pass vulnerabiltiy dataview id to the navigation, while for findings we do. Should we? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @maxcold There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as we plan to backport this to 8.16 let's not add this new logic, just to be on the safe side. We can do it separately for 8.17 |
||
): Filter[] => { | ||
return Object.entries(filterParams).map(([key, filterValue]) => | ||
createFilter(key, filterValue, dataViewId) | ||
); | ||
}; | ||
|
||
export const createFilter = (key: string, filterValue: FilterValue, dataViewId: string): Filter => { | ||
let negate = false; | ||
let value = filterValue; | ||
if (typeof filterValue === 'object') { | ||
negate = filterValue.negate; | ||
value = filterValue.value; | ||
} | ||
// If the value is '*', we want to create an exists filter | ||
if (value === '*') { | ||
return { | ||
query: { exists: { field: key } }, | ||
meta: { type: 'exists', index: dataViewId }, | ||
}; | ||
} | ||
return { | ||
meta: { | ||
alias: null, | ||
negate, | ||
disabled: false, | ||
type: 'phrase', | ||
key, | ||
index: dataViewId, | ||
}, | ||
query: { match_phrase: { [key]: value } }, | ||
}; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,5 +35,6 @@ | |
"@kbn/ui-theme", | ||
"@kbn/i18n-react", | ||
"@kbn/rison", | ||
"@kbn/core-lifecycle-browser", | ||
] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can be replaced with
services.data
as we only rely on data service