[8.x] [Infra] Check ML permissions before requesting ML data (#218069) (#218229)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[Infra] Check ML permissions before requesting ML data
(#218069)](https://github.com/elastic/kibana/pull/218069)

<!--- Backport version: 9.6.6 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sorenlouv/backport)

<!--BACKPORT [{"author":{"name":"Carlos
Crespo","email":"crespocarlos@users.noreply.github.com"},"sourceCommit":{"committedDate":"2025-04-15T10:02:51Z","message":"[Infra]
Check ML permissions before requesting ML data (#218069)\n\nfixes
[#189213](https://github.com/elastic/kibana/issues/189213)\n\n##
Summary\n\nChecks whether the user has permission to ML before
triggering requests\nto fetch ML data\n\n\n### How to test\n\n- Create a
user whose role doesn't have permission to ML, but has\npermission to
O11y apps\n- Run `node scripts/synthtrace infra_hosts_with_apm_hosts
--live\n--clean`\n - Navigate to Inventory Infrastructure / Hosts
View\n\n---------\n\nCo-authored-by: Elastic Machine
<elasticmachine@users.noreply.github.com>","sha":"5423655975924c5e2840f684132fc0447e4bcec3","branchLabelMapping":{"^v9.1.0$":"main","^v8.19.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","backport:prev-minor","backport:prev-major","Team:obs-ux-infra_services","v9.1.0"],"title":"[Infra]
Check ML permissions before requesting ML
data","number":218069,"url":"https://github.com/elastic/kibana/pull/218069","mergeCommit":{"message":"[Infra]
Check ML permissions before requesting ML data (#218069)\n\nfixes
[#189213](https://github.com/elastic/kibana/issues/189213)\n\n##
Summary\n\nChecks whether the user has permission to ML before
triggering requests\nto fetch ML data\n\n\n### How to test\n\n- Create a
user whose role doesn't have permission to ML, but has\npermission to
O11y apps\n- Run `node scripts/synthtrace infra_hosts_with_apm_hosts
--live\n--clean`\n - Navigate to Inventory Infrastructure / Hosts
View\n\n---------\n\nCo-authored-by: Elastic Machine
<elasticmachine@users.noreply.github.com>","sha":"5423655975924c5e2840f684132fc0447e4bcec3"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.1.0","branchLabelMappingKey":"^v9.1.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/218069","number":218069,"mergeCommit":{"message":"[Infra]
Check ML permissions before requesting ML data (#218069)\n\nfixes
[#189213](https://github.com/elastic/kibana/issues/189213)\n\n##
Summary\n\nChecks whether the user has permission to ML before
triggering requests\nto fetch ML data\n\n\n### How to test\n\n- Create a
user whose role doesn't have permission to ML, but has\npermission to
O11y apps\n- Run `node scripts/synthtrace infra_hosts_with_apm_hosts
--live\n--clean`\n - Navigate to Inventory Infrastructure / Hosts
View\n\n---------\n\nCo-authored-by: Elastic Machine
<elasticmachine@users.noreply.github.com>","sha":"5423655975924c5e2840f684132fc0447e4bcec3"}}]}]
BACKPORT-->

Co-authored-by: Carlos Crespo <crespocarlos@users.noreply.github.com>
This commit is contained in:
Kibana Machine 2025-04-15 14:02:53 +02:00 committed by GitHub
parent e3826dec77
commit 7397472f90
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$,