mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 17:59:23 -04:00
* [ML] Adding capabilities checks to shared functions * small refactor * disabling capabilities checks for functions called by SIEM alerting * testing git * removing comment * using constant for ml app id * tiny type clean up * removing check in ml_capabilities * fixing types * removing capabilities checks from ml_capabilities endpoint * updating types * better error handling * improving capabilities check * adding custom errors Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
121 lines
3.6 KiB
TypeScript
121 lines
3.6 KiB
TypeScript
/*
|
|
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
|
* or more contributor license agreements. Licensed under the Elastic License;
|
|
* you may not use this file except in compliance with the Elastic License.
|
|
*/
|
|
|
|
import moment from 'moment';
|
|
import { KibanaRequest } from '../../../../../../src/core/server';
|
|
import { IIndexPattern } from '../../../../../../src/plugins/data/common';
|
|
import { APMConfig } from '../..';
|
|
import {
|
|
getApmIndices,
|
|
ApmIndicesConfig,
|
|
} from '../settings/apm_indices/get_apm_indices';
|
|
import { ESFilter } from '../../../typings/elasticsearch';
|
|
import { ESClient } from './es_client';
|
|
import { getUiFiltersES } from './convert_ui_filters/get_ui_filters_es';
|
|
import { APMRequestHandlerContext } from '../../routes/typings';
|
|
import { getESClient } from './es_client';
|
|
import { ProcessorEvent } from '../../../common/processor_event';
|
|
import { getDynamicIndexPattern } from '../index_pattern/get_dynamic_index_pattern';
|
|
|
|
function decodeUiFilters(
|
|
indexPattern: IIndexPattern | undefined,
|
|
uiFiltersEncoded?: string
|
|
) {
|
|
if (!uiFiltersEncoded || !indexPattern) {
|
|
return [];
|
|
}
|
|
const uiFilters = JSON.parse(uiFiltersEncoded);
|
|
return getUiFiltersES(indexPattern, uiFilters);
|
|
}
|
|
// Explicitly type Setup to prevent TS initialization errors
|
|
// https://github.com/microsoft/TypeScript/issues/34933
|
|
|
|
export interface Setup {
|
|
client: ESClient;
|
|
internalClient: ESClient;
|
|
ml?: ReturnType<typeof getMlSetup>;
|
|
config: APMConfig;
|
|
indices: ApmIndicesConfig;
|
|
dynamicIndexPattern?: IIndexPattern;
|
|
}
|
|
|
|
export interface SetupTimeRange {
|
|
start: number;
|
|
end: number;
|
|
}
|
|
|
|
export interface SetupUIFilters {
|
|
uiFiltersES: ESFilter[];
|
|
}
|
|
|
|
interface SetupRequestParams {
|
|
query?: {
|
|
_debug?: boolean;
|
|
start?: string;
|
|
end?: string;
|
|
uiFilters?: string;
|
|
processorEvent?: ProcessorEvent;
|
|
};
|
|
}
|
|
|
|
type InferSetup<TParams extends SetupRequestParams> = Setup &
|
|
(TParams extends { query: { start: string } } ? { start: number } : {}) &
|
|
(TParams extends { query: { end: string } } ? { end: number } : {}) &
|
|
(TParams extends { query: { uiFilters: string } }
|
|
? { uiFiltersES: ESFilter[] }
|
|
: {});
|
|
|
|
export async function setupRequest<TParams extends SetupRequestParams>(
|
|
context: APMRequestHandlerContext<TParams>,
|
|
request: KibanaRequest
|
|
): Promise<InferSetup<TParams>> {
|
|
const { config } = context;
|
|
const { query } = context.params;
|
|
|
|
const indices = await getApmIndices({
|
|
savedObjectsClient: context.core.savedObjects.client,
|
|
config,
|
|
});
|
|
|
|
const dynamicIndexPattern = await getDynamicIndexPattern({
|
|
context,
|
|
indices,
|
|
processorEvent: query.processorEvent,
|
|
});
|
|
|
|
const uiFiltersES = decodeUiFilters(dynamicIndexPattern, query.uiFilters);
|
|
|
|
const coreSetupRequest = {
|
|
indices,
|
|
client: getESClient(context, request, { clientAsInternalUser: false }),
|
|
internalClient: getESClient(context, request, {
|
|
clientAsInternalUser: true,
|
|
}),
|
|
ml: getMlSetup(context, request),
|
|
config,
|
|
dynamicIndexPattern,
|
|
};
|
|
|
|
return {
|
|
...('start' in query ? { start: moment.utc(query.start).valueOf() } : {}),
|
|
...('end' in query ? { end: moment.utc(query.end).valueOf() } : {}),
|
|
...('uiFilters' in query ? { uiFiltersES } : {}),
|
|
...coreSetupRequest,
|
|
} as InferSetup<TParams>;
|
|
}
|
|
|
|
function getMlSetup(context: APMRequestHandlerContext, request: KibanaRequest) {
|
|
if (!context.plugins.ml) {
|
|
return;
|
|
}
|
|
const ml = context.plugins.ml;
|
|
const mlClient = ml.mlClient.asScoped(request).callAsCurrentUser;
|
|
return {
|
|
mlSystem: ml.mlSystemProvider(mlClient, request),
|
|
anomalyDetectors: ml.anomalyDetectorsProvider(mlClient, request),
|
|
mlClient,
|
|
};
|
|
}
|