[Infra] Check ML permissions before requesting ML data (#218069)

fixes [#189213](https://github.com/elastic/kibana/issues/189213)

## Summary

Checks whether the user has permission to ML before triggering requests
to fetch ML data


### How to test

- Create a user whose role doesn't have permission to ML, but has
permission to O11y apps
- Run `node scripts/synthtrace infra_hosts_with_apm_hosts --live
--clean`
 - Navigate to Inventory Infrastructure / Hosts View

---------

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
This commit is contained in:
Carlos Crespo 2025-04-15 12:02:51 +02:00 committed by GitHub
parent b1ffcf3060
commit 5423655975
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 28 additions and 2 deletions

View file

@ -83,8 +83,9 @@ class LosslessHistogram {
record(value: number) {
const countForValue = this.trackedValues.get(value);
if (
this.backingHistogram ||
(countForValue === undefined && this.trackedValues.size >= MAX_VALUES_TO_TRACK_LOSSLESS)
this.backingHistogram &&
countForValue === undefined &&
this.trackedValues.size >= MAX_VALUES_TO_TRACK_LOSSLESS
) {
this.getBackingHistogram().record(value);
return;

View file

@ -8,6 +8,7 @@
import { useState, useCallback, useEffect, useReducer } from 'react';
import type { BehaviorSubject } from 'rxjs';
import { decodeOrThrow } from '@kbn/io-ts-utils';
import { useKibanaContextForPlugin } from '../../../../hooks/use_kibana';
import { isPending, isFailure, useFetcher } from '../../../../hooks/use_fetcher';
import type {
Sort,
@ -185,6 +186,9 @@ export const useMetricsHostsAnomaliesResults = (
active = true,
}: { request$?: BehaviorSubject<(() => Promise<unknown>) | undefined>; active?: boolean }
) => {
const {
services: { application },
} = useKibanaContextForPlugin();
const [reducerState, dispatch] = useReducer(
stateReducer,
STATE_DEFAULTS,
@ -199,12 +203,19 @@ export const useMetricsHostsAnomaliesResults = (
const [metricsHostsAnomalies, setMetricsHostsAnomalies] = useState<MetricsHostsAnomalies>([]);
const mlCapabilities = application.capabilities.ml as { canGetJobs: boolean } | undefined;
const canGetAnomalies = mlCapabilities?.canGetJobs;
const {
data: response,
status,
refetch,
} = useFetcher(
async (callApi) => {
if (!canGetAnomalies) {
return;
}
const apiResponse = await callApi(INFA_ML_GET_METRICS_HOSTS_ANOMALIES_PATH, {
method: 'POST',
body: JSON.stringify(
@ -242,6 +253,7 @@ export const useMetricsHostsAnomaliesResults = (
reducerState.timeRange.start,
search,
sourceId,
canGetAnomalies,
],
{
requestObservable$: request$,

View file

@ -8,6 +8,7 @@
import { useState, useCallback, useEffect, useReducer } from 'react';
import type { BehaviorSubject } from 'rxjs';
import { decodeOrThrow } from '@kbn/io-ts-utils';
import { useKibanaContextForPlugin } from '../../../../hooks/use_kibana';
import { isFailure, isPending, useFetcher } from '../../../../hooks/use_fetcher';
import type {
Sort,
@ -162,6 +163,10 @@ export const useMetricsK8sAnomaliesResults = (
active = true,
}: { request$?: BehaviorSubject<(() => Promise<unknown>) | undefined>; active?: boolean }
) => {
const {
services: { application },
} = useKibanaContextForPlugin();
const [metricsK8sAnomalies, setMetricsK8sAnomalies] = useState<MetricsK8sAnomalies>([]);
const initStateReducer = (stateDefaults: ReducerStateDefaults): ReducerState => {
return {
@ -177,12 +182,19 @@ export const useMetricsK8sAnomaliesResults = (
};
const [reducerState, dispatch] = useReducer(stateReducer, STATE_DEFAULTS, initStateReducer);
const mlCapabilities = application.capabilities.ml as { canGetJobs: boolean } | undefined;
const canGetAnomalies = mlCapabilities?.canGetJobs;
const {
data: response,
status,
refetch,
} = useFetcher(
async (callApi) => {
if (!canGetAnomalies) {
return;
}
const apiResponse = await callApi(INFA_ML_GET_METRICS_K8S_ANOMALIES_PATH, {
method: 'POST',
body: JSON.stringify(
@ -218,6 +230,7 @@ export const useMetricsK8sAnomaliesResults = (
reducerState.timeRange.start,
search,
sourceId,
canGetAnomalies,
],
{
requestObservable$: request$,