diff --git a/x-pack/plugins/security_solution/public/entity_analytics/api/api.ts b/x-pack/plugins/security_solution/public/entity_analytics/api/api.ts index 13408e349007d..73cb1cbd57ff0 100644 --- a/x-pack/plugins/security_solution/public/entity_analytics/api/api.ts +++ b/x-pack/plugins/security_solution/public/entity_analytics/api/api.ts @@ -15,7 +15,6 @@ import { ASSET_CRITICALITY_PRIVILEGES_URL, } from '../../../common/constants'; -import { KibanaServices } from '../../common/lib/kibana'; import type { CalculateScoresResponse, EnableRiskEngineResponse, @@ -25,89 +24,90 @@ import type { } from '../../../server/lib/entity_analytics/types'; import type { RiskScorePreviewRequestSchema } from '../../../common/entity_analytics/risk_engine/risk_score_preview/request_schema'; import type { EntityAnalyticsPrivileges } from '../../../common/api/entity_analytics/common'; +import { useKibana } from '../../common/lib/kibana/kibana_react'; -/** - * Fetches preview risks scores - */ -export const fetchRiskScorePreview = async ({ - signal, - params, -}: { - signal?: AbortSignal; - params: RiskScorePreviewRequestSchema; -}): Promise => { - return KibanaServices.get().http.fetch(RISK_SCORE_PREVIEW_URL, { - version: '1', - method: 'POST', - body: JSON.stringify(params), - signal, - }); -}; +export const useEntityAnalyticsRoutes = () => { + const http = useKibana().services.http; -/** - * Fetches risks engine status - */ -export const fetchRiskEngineStatus = async ({ - signal, -}: { - signal?: AbortSignal; -}): Promise => { - return KibanaServices.get().http.fetch(RISK_ENGINE_STATUS_URL, { - version: '1', - method: 'GET', + /** + * Fetches preview risks scores + */ + const fetchRiskScorePreview = ({ signal, - }); -}; + params, + }: { + signal?: AbortSignal; + params: RiskScorePreviewRequestSchema; + }) => + http.fetch(RISK_SCORE_PREVIEW_URL, { + version: '1', + method: 'POST', + body: JSON.stringify(params), + signal, + }); -/** - * Init risk score engine - */ -export const initRiskEngine = async (): Promise => { - return KibanaServices.get().http.fetch(RISK_ENGINE_INIT_URL, { - version: '1', - method: 'POST', - }); -}; + /** + * Fetches risks engine status + */ + const fetchRiskEngineStatus = ({ signal }: { signal?: AbortSignal }) => + http.fetch(RISK_ENGINE_STATUS_URL, { + version: '1', + method: 'GET', + signal, + }); -/** - * Enable risk score engine - */ -export const enableRiskEngine = async (): Promise => { - return KibanaServices.get().http.fetch(RISK_ENGINE_ENABLE_URL, { - version: '1', - method: 'POST', - }); -}; + /** + * Init risk score engine + */ + const initRiskEngine = () => + http.fetch(RISK_ENGINE_INIT_URL, { + version: '1', + method: 'POST', + }); -/** - * Disable risk score engine - */ -export const disableRiskEngine = async (): Promise => { - return KibanaServices.get().http.fetch(RISK_ENGINE_DISABLE_URL, { - version: '1', - method: 'POST', - }); -}; + /** + * Enable risk score engine + */ + const enableRiskEngine = () => + http.fetch(RISK_ENGINE_ENABLE_URL, { + version: '1', + method: 'POST', + }); -/** - * Get risk engine privileges - */ -export const fetchRiskEnginePrivileges = async (): Promise => { - return KibanaServices.get().http.fetch(RISK_ENGINE_PRIVILEGES_URL, { - version: '1', - method: 'GET', - }); -}; + /** + * Disable risk score engine + */ + const disableRiskEngine = () => + http.fetch(RISK_ENGINE_DISABLE_URL, { + version: '1', + method: 'POST', + }); -/** - * Get asset criticality privileges - */ -export const fetchAssetCriticalityPrivileges = async (): Promise => { - return KibanaServices.get().http.fetch( - ASSET_CRITICALITY_PRIVILEGES_URL, - { + /** + * Get risk engine privileges + */ + const fetchRiskEnginePrivileges = () => + http.fetch(RISK_ENGINE_PRIVILEGES_URL, { + version: '1', + method: 'GET', + }); + + /** + * Get asset criticality privileges + */ + const fetchAssetCriticalityPrivileges = () => + http.fetch(ASSET_CRITICALITY_PRIVILEGES_URL, { version: '1', method: 'GET', - } - ); + }); + + return { + fetchRiskScorePreview, + fetchRiskEngineStatus, + initRiskEngine, + enableRiskEngine, + disableRiskEngine, + fetchRiskEnginePrivileges, + fetchAssetCriticalityPrivileges, + }; }; diff --git a/x-pack/plugins/security_solution/public/entity_analytics/api/hooks/use_asset_criticality_privileges.ts b/x-pack/plugins/security_solution/public/entity_analytics/api/hooks/use_asset_criticality_privileges.ts index 7314dcc6147a4..afed24ac49e63 100644 --- a/x-pack/plugins/security_solution/public/entity_analytics/api/hooks/use_asset_criticality_privileges.ts +++ b/x-pack/plugins/security_solution/public/entity_analytics/api/hooks/use_asset_criticality_privileges.ts @@ -5,8 +5,9 @@ * 2.0. */ import { useQuery } from '@tanstack/react-query'; -import { fetchAssetCriticalityPrivileges } from '../api'; +import { useEntityAnalyticsRoutes } from '../api'; export const useAssetCriticalityPrivileges = () => { + const { fetchAssetCriticalityPrivileges } = useEntityAnalyticsRoutes(); return useQuery(['GET', 'FETCH_ASSET_CRITICALITY_PRIVILEGES'], fetchAssetCriticalityPrivileges); }; diff --git a/x-pack/plugins/security_solution/public/entity_analytics/api/hooks/use_disable_risk_engine_mutation.ts b/x-pack/plugins/security_solution/public/entity_analytics/api/hooks/use_disable_risk_engine_mutation.ts index 8b3ca2d0ac2ad..9f601d191d440 100644 --- a/x-pack/plugins/security_solution/public/entity_analytics/api/hooks/use_disable_risk_engine_mutation.ts +++ b/x-pack/plugins/security_solution/public/entity_analytics/api/hooks/use_disable_risk_engine_mutation.ts @@ -6,7 +6,7 @@ */ import type { UseMutationOptions } from '@tanstack/react-query'; import { useMutation } from '@tanstack/react-query'; -import { disableRiskEngine } from '../api'; +import { useEntityAnalyticsRoutes } from '../api'; import { useInvalidateRiskEngineStatusQuery } from './use_risk_engine_status'; import type { EnableRiskEngineResponse, @@ -17,6 +17,7 @@ export const DISABLE_RISK_ENGINE_MUTATION_KEY = ['POST', 'DISABLE_RISK_ENGINE']; export const useDisableRiskEngineMutation = (options?: UseMutationOptions<{}>) => { const invalidateRiskEngineStatusQuery = useInvalidateRiskEngineStatusQuery(); + const { disableRiskEngine } = useEntityAnalyticsRoutes(); return useMutation( () => disableRiskEngine(), diff --git a/x-pack/plugins/security_solution/public/entity_analytics/api/hooks/use_enable_risk_engine_mutation.ts b/x-pack/plugins/security_solution/public/entity_analytics/api/hooks/use_enable_risk_engine_mutation.ts index c4d5070014fc2..0f1a5995b7656 100644 --- a/x-pack/plugins/security_solution/public/entity_analytics/api/hooks/use_enable_risk_engine_mutation.ts +++ b/x-pack/plugins/security_solution/public/entity_analytics/api/hooks/use_enable_risk_engine_mutation.ts @@ -6,7 +6,7 @@ */ import type { UseMutationOptions } from '@tanstack/react-query'; import { useMutation } from '@tanstack/react-query'; -import { enableRiskEngine } from '../api'; +import { useEntityAnalyticsRoutes } from '../api'; import { useInvalidateRiskEngineStatusQuery } from './use_risk_engine_status'; import type { EnableRiskEngineResponse, @@ -16,9 +16,9 @@ export const ENABLE_RISK_ENGINE_MUTATION_KEY = ['POST', 'ENABLE_RISK_ENGINE']; export const useEnableRiskEngineMutation = (options?: UseMutationOptions<{}>) => { const invalidateRiskEngineStatusQuery = useInvalidateRiskEngineStatusQuery(); - + const { enableRiskEngine } = useEntityAnalyticsRoutes(); return useMutation( - () => enableRiskEngine(), + enableRiskEngine, { ...options, mutationKey: ENABLE_RISK_ENGINE_MUTATION_KEY, diff --git a/x-pack/plugins/security_solution/public/entity_analytics/api/hooks/use_init_risk_engine_mutation.ts b/x-pack/plugins/security_solution/public/entity_analytics/api/hooks/use_init_risk_engine_mutation.ts index 1e2a695652edf..aa8a19c4a7a6a 100644 --- a/x-pack/plugins/security_solution/public/entity_analytics/api/hooks/use_init_risk_engine_mutation.ts +++ b/x-pack/plugins/security_solution/public/entity_analytics/api/hooks/use_init_risk_engine_mutation.ts @@ -6,7 +6,7 @@ */ import type { UseMutationOptions } from '@tanstack/react-query'; import { useMutation } from '@tanstack/react-query'; -import { initRiskEngine } from '../api'; +import { useEntityAnalyticsRoutes } from '../api'; import { useInvalidateRiskEngineStatusQuery } from './use_risk_engine_status'; import type { InitRiskEngineResponse, @@ -17,7 +17,7 @@ export const INIT_RISK_ENGINE_STATUS_KEY = ['POST', 'INIT_RISK_ENGINE']; export const useInitRiskEngineMutation = (options?: UseMutationOptions<{}>) => { const invalidateRiskEngineStatusQuery = useInvalidateRiskEngineStatusQuery(); - + const { initRiskEngine } = useEntityAnalyticsRoutes(); return useMutation(() => initRiskEngine(), { ...options, mutationKey: INIT_RISK_ENGINE_STATUS_KEY, diff --git a/x-pack/plugins/security_solution/public/entity_analytics/api/hooks/use_preview_risk_scores.ts b/x-pack/plugins/security_solution/public/entity_analytics/api/hooks/use_preview_risk_scores.ts index 84360372ff876..01e9c6ac8dfea 100644 --- a/x-pack/plugins/security_solution/public/entity_analytics/api/hooks/use_preview_risk_scores.ts +++ b/x-pack/plugins/security_solution/public/entity_analytics/api/hooks/use_preview_risk_scores.ts @@ -6,7 +6,7 @@ */ import { useQuery } from '@tanstack/react-query'; import dateMath from '@kbn/datemath'; -import { fetchRiskScorePreview } from '../api'; +import { useEntityAnalyticsRoutes } from '../api'; import type { RiskScorePreviewRequestSchema } from '../../../../common/entity_analytics/risk_engine/risk_score_preview/request_schema'; export const useRiskScorePreview = ({ @@ -14,9 +14,10 @@ export const useRiskScorePreview = ({ range, filter, }: RiskScorePreviewRequestSchema) => { + const { fetchRiskScorePreview } = useEntityAnalyticsRoutes(); + return useQuery(['POST', 'FETCH_PREVIEW_RISK_SCORE', range, filter], async ({ signal }) => { const params: RiskScorePreviewRequestSchema = { data_view_id: dataViewId }; - if (range) { const startTime = dateMath.parse(range.start)?.utc().toISOString(); const endTime = dateMath diff --git a/x-pack/plugins/security_solution/public/entity_analytics/api/hooks/use_risk_engine_privileges.ts b/x-pack/plugins/security_solution/public/entity_analytics/api/hooks/use_risk_engine_privileges.ts index 2a3ffa40856cb..5278b537048ff 100644 --- a/x-pack/plugins/security_solution/public/entity_analytics/api/hooks/use_risk_engine_privileges.ts +++ b/x-pack/plugins/security_solution/public/entity_analytics/api/hooks/use_risk_engine_privileges.ts @@ -5,8 +5,9 @@ * 2.0. */ import { useQuery } from '@tanstack/react-query'; -import { fetchRiskEnginePrivileges } from '../api'; +import { useEntityAnalyticsRoutes } from '../api'; export const useRiskEnginePrivileges = () => { + const { fetchRiskEnginePrivileges } = useEntityAnalyticsRoutes(); return useQuery(['GET', 'FETCH_RISK_ENGINE_PRIVILEGES'], fetchRiskEnginePrivileges); }; diff --git a/x-pack/plugins/security_solution/public/entity_analytics/api/hooks/use_risk_engine_status.ts b/x-pack/plugins/security_solution/public/entity_analytics/api/hooks/use_risk_engine_status.ts index ed591213b2dcf..07ef32f0a3e41 100644 --- a/x-pack/plugins/security_solution/public/entity_analytics/api/hooks/use_risk_engine_status.ts +++ b/x-pack/plugins/security_solution/public/entity_analytics/api/hooks/use_risk_engine_status.ts @@ -6,7 +6,7 @@ */ import { useQuery, useQueryClient } from '@tanstack/react-query'; import { useCallback } from 'react'; -import { fetchRiskEngineStatus } from '../api'; +import { useEntityAnalyticsRoutes } from '../api'; import { RiskEngineStatus } from '../../../../common/entity_analytics/risk_engine/types'; import { useIsExperimentalFeatureEnabled } from '../../../common/hooks/use_experimental_features'; const FETCH_RISK_ENGINE_STATUS = ['GET', 'FETCH_RISK_ENGINE_STATUS']; @@ -29,7 +29,7 @@ export const useIsNewRiskScoreModuleInstalled = () => { export const useRiskEngineStatus = () => { const isNewRiskScoreModuleAvailable = useIsExperimentalFeatureEnabled('riskScoringRoutesEnabled'); - + const { fetchRiskEngineStatus } = useEntityAnalyticsRoutes(); return useQuery(FETCH_RISK_ENGINE_STATUS, async ({ signal }) => { if (!isNewRiskScoreModuleAvailable) { return {