66 */
77import { useQuery } from '@tanstack/react-query' ;
88import { lastValueFrom } from 'rxjs' ;
9- import type { IKibanaSearchResponse , IKibanaSearchRequest } from '@kbn/search-types' ;
10- import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey' ;
11- import {
12- CDR_MISCONFIGURATIONS_INDEX_PATTERN ,
13- LATEST_FINDINGS_RETENTION_POLICY ,
14- CspFinding ,
15- } from '@kbn/cloud-security-posture-common' ;
16- import type { CspBenchmarkRulesStates } from '@kbn/cloud-security-posture-common/schema/rules/latest' ;
17- import { buildMutedRulesFilter } from '@kbn/cloud-security-posture-common' ;
189import { useKibana } from '@kbn/kibana-react-plugin/public' ;
1910import type { CoreStart } from '@kbn/core/public' ;
2011import { showErrorToast } from '../..' ;
21- import type { CspClientPluginStartDeps } from '../../type' ;
12+ import type {
13+ CspClientPluginStartDeps ,
14+ LatestFindingsRequest ,
15+ LatestFindingsResponse ,
16+ UseMisconfigurationOptions ,
17+ } from '../../type' ;
2218import { useGetCspBenchmarkRulesStatesApi } from './use_get_benchmark_rules_state_api' ;
19+ import {
20+ buildMisconfigurationsFindingsQuery ,
21+ getMisconfigurationAggregationCount ,
22+ } from '../utils/hooks_utils' ;
2323
24- interface MisconfigurationPreviewBaseEsQuery {
25- query ?: {
26- bool : {
27- filter : estypes . QueryDslQueryContainer [ ] ;
28- } ;
29- } ;
30- }
31-
32- interface UseMisconfigurationPreviewOptions extends MisconfigurationPreviewBaseEsQuery {
33- sort : string [ ] [ ] ;
34- enabled : boolean ;
35- pageSize : number ;
36- }
37-
38- type LatestFindingsRequest = IKibanaSearchRequest < estypes . SearchRequest > ;
39- type LatestFindingsResponse = IKibanaSearchResponse <
40- estypes . SearchResponse < CspFinding , FindingsAggs >
41- > ;
42-
43- interface FindingsAggs {
44- count : estypes . AggregationsMultiBucketAggregateBase < estypes . AggregationsStringRareTermsBucketKeys > ;
45- }
46-
47- const RESULT_EVALUATION = {
48- PASSED : 'passed' ,
49- FAILED : 'failed' ,
50- UNKNOWN : 'unknown' ,
51- } ;
52-
53- export const getFindingsCountAggQueryMisconfigurationPreview = ( ) => ( {
54- count : {
55- filters : {
56- other_bucket_key : RESULT_EVALUATION . UNKNOWN ,
57- filters : {
58- [ RESULT_EVALUATION . PASSED ] : { match : { 'result.evaluation' : RESULT_EVALUATION . PASSED } } ,
59- [ RESULT_EVALUATION . FAILED ] : { match : { 'result.evaluation' : RESULT_EVALUATION . FAILED } } ,
60- } ,
61- } ,
62- } ,
63- } ) ;
64-
65- export const getMisconfigurationAggregationCount = (
66- buckets : estypes . AggregationsBuckets < estypes . AggregationsStringRareTermsBucketKeys >
67- ) => {
68- return Object . entries ( buckets ) . reduce (
69- ( evaluation , [ key , value ] ) => {
70- evaluation [ key ] = ( evaluation [ key ] || 0 ) + ( value . doc_count || 0 ) ;
71- return evaluation ;
72- } ,
73- {
74- [ RESULT_EVALUATION . PASSED ] : 0 ,
75- [ RESULT_EVALUATION . FAILED ] : 0 ,
76- [ RESULT_EVALUATION . UNKNOWN ] : 0 ,
77- }
78- ) ;
79- } ;
80-
81- export const buildMisconfigurationsFindingsQuery = (
82- { query } : UseMisconfigurationPreviewOptions ,
83- rulesStates : CspBenchmarkRulesStates
84- ) => {
85- const mutedRulesFilterQuery = buildMutedRulesFilter ( rulesStates ) ;
86-
87- return {
88- index : CDR_MISCONFIGURATIONS_INDEX_PATTERN ,
89- size : 0 ,
90- aggs : getFindingsCountAggQueryMisconfigurationPreview ( ) ,
91- ignore_unavailable : false ,
92- query : buildMisconfigurationsFindingsQueryWithFilters ( query , mutedRulesFilterQuery ) ,
93- } ;
94- } ;
95-
96- const buildMisconfigurationsFindingsQueryWithFilters = (
97- query : UseMisconfigurationPreviewOptions [ 'query' ] ,
98- mutedRulesFilterQuery : estypes . QueryDslQueryContainer [ ]
99- ) => {
100- return {
101- ...query ,
102- bool : {
103- ...query ?. bool ,
104- filter : [
105- ...( query ?. bool ?. filter ?? [ ] ) ,
106- {
107- range : {
108- '@timestamp' : {
109- gte : `now-${ LATEST_FINDINGS_RETENTION_POLICY } ` ,
110- lte : 'now' ,
111- } ,
112- } ,
113- } ,
114- ] ,
115- must_not : [ ...mutedRulesFilterQuery ] ,
116- } ,
117- } ;
118- } ;
119-
120- export const useMisconfigurationPreview = ( options : UseMisconfigurationPreviewOptions ) => {
24+ export const useMisconfigurationPreview = ( options : UseMisconfigurationOptions ) => {
12125 const {
12226 data,
12327 notifications : { toasts } ,
@@ -134,10 +38,10 @@ export const useMisconfigurationPreview = (options: UseMisconfigurationPreviewOp
13438 params : buildMisconfigurationsFindingsQuery ( options , rulesStates ! ) ,
13539 } )
13640 ) ;
137- if ( ! aggregations ) throw new Error ( 'expected aggregations to be defined' ) ;
138-
41+ if ( ! aggregations && ! options . ignore_unavailable )
42+ throw new Error ( 'expected aggregations to be defined' ) ;
13943 return {
140- count : getMisconfigurationAggregationCount ( aggregations . count . buckets ) ,
44+ count : getMisconfigurationAggregationCount ( aggregations ? .count ? .buckets ) ,
14145 } ;
14246 } ,
14347 {
0 commit comments