Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RAM] Get field caps from kibana event logs #127896

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions x-pack/plugins/alerting/server/routes/get_event_logs_field_caps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { schema } from '@kbn/config-schema';
import { IRouter } from 'kibana/server';
import { ILicenseState } from '../lib';
import { verifyAccessAndContext } from './lib';
import { AlertingRequestHandlerContext, INTERNAL_BASE_ALERTING_API_PATH } from '../types';

const paramSchema = schema.object({
id: schema.string(),
});

interface BuildGetRulesRouteParams {
licenseState: ILicenseState;
path: string;
router: IRouter<AlertingRequestHandlerContext>;
excludeFromPublicApi?: boolean;
}
const buildGetEventLogsFieldCapsRoute = ({
licenseState,
path,
router,
excludeFromPublicApi = false,
}: BuildGetRulesRouteParams) => {
router.get(
{
path,
validate: {
params: paramSchema,
},
},
router.handleLegacyErrors(
verifyAccessAndContext(licenseState, async function (context, req, res) {
const rulesClient = context.alerting.getRulesClient();
const { id } = req.params;
const eventLogsFieldCaps = await rulesClient.getEventLogsFieldCaps(id);
return res.ok({
body: eventLogsFieldCaps,
});
})
)
);
};

export const getInternalGetEventLogsFieldCapsRoute = (
router: IRouter<AlertingRequestHandlerContext>,
licenseState: ILicenseState
) =>
buildGetEventLogsFieldCapsRoute({
excludeFromPublicApi: false,
licenseState,
path: `${INTERNAL_BASE_ALERTING_API_PATH}/rule/{id}/logs/_field_caps`,
router,
});
2 changes: 2 additions & 0 deletions x-pack/plugins/alerting/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { muteAlertRoute } from './mute_alert';
import { unmuteAllRuleRoute } from './unmute_all_rule';
import { unmuteAlertRoute } from './unmute_alert';
import { updateRuleApiKeyRoute } from './update_rule_api_key';
import { getInternalGetEventLogsFieldCapsRoute } from './get_event_logs_field_caps';

export interface RouteOptions {
router: IRouter<AlertingRequestHandlerContext>;
Expand All @@ -52,6 +53,7 @@ export function defineRoutes(opts: RouteOptions) {
enableRuleRoute(router, licenseState);
findRulesRoute(router, licenseState, usageCounter);
findInternalRulesRoute(router, licenseState, usageCounter);
getInternalGetEventLogsFieldCapsRoute(router, licenseState);
getRuleAlertSummaryRoute(router, licenseState);
getRuleStateRoute(router, licenseState);
healthRoute(router, licenseState, encryptedSavedObjects);
Expand Down
5 changes: 5 additions & 0 deletions x-pack/plugins/alerting/server/rules_client/rules_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,11 @@ export class RulesClient {
});
}

public async getEventLogsFieldCaps(id: string) {
const eventLogClient = await this.getEventLogClient();
return await eventLogClient.getEventLogsFieldCaps('alert', [id]);
}

public async find<Params extends RuleTypeParams = never>({
options: { fields, ...options } = {},
excludeFromPublicApi = false,
Expand Down
9 changes: 9 additions & 0 deletions x-pack/plugins/event_log/server/es/cluster_client_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Logger, ElasticsearchClient } from 'src/core/server';
import util from 'util';
import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { fromKueryExpression, toElasticsearchQuery } from '@kbn/es-query';
import { FieldDescriptor, IndexPatternsFetcher } from '../../../../../src/plugins/data/server';
import { IEvent, IValidatedEvent, SAVED_OBJECT_REL_PRIMARY } from '../types';
import { AggregateOptionsType, FindOptionsType, QueryOptionsType } from '../event_log_client';
import { ParsedIndexAlias } from './init';
Expand Down Expand Up @@ -415,6 +416,14 @@ export class ClusterClientAdapter<TDoc extends { body: AliasAny; index: string }
);
}
}

public async getEventLogsFieldCaps(index: string): Promise<FieldDescriptor[]> {
const esClient = await this.elasticsearchClientPromise;
const indexPatternsFetcher = new IndexPatternsFetcher(esClient);
return await indexPatternsFetcher.getFieldsForWildcard({
pattern: index,
});
}
}

function getNamespaceQuery(namespace?: string) {
Expand Down
12 changes: 12 additions & 0 deletions x-pack/plugins/event_log/server/event_log_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { EsContext } from './es';
import { IEventLogClient } from './types';
import { QueryEventsBySavedObjectResult } from './es/cluster_client_adapter';
import { SavedObjectBulkGetterResult } from './saved_object_provider_registry';
import { FieldDescriptor } from '../../../../src/plugins/data_views/server';
export type PluginClusterClient = Pick<IClusterClient, 'asInternalUser'>;
export type AdminClusterClient$ = Observable<PluginClusterClient>;

Expand Down Expand Up @@ -88,6 +89,9 @@ export class EventLogClient implements IEventLogClient {
this.spacesService = spacesService;
this.request = request;
}
getEventsFieldCaps(type: string, ids: string[]): Promise<FieldDescriptor[]> {
throw new Error('Method not implemented.');
}

public async findEventsBySavedObjectIds(
type: string,
Expand Down Expand Up @@ -137,6 +141,14 @@ export class EventLogClient implements IEventLogClient {
});
}

public async getEventLogsFieldCaps(type: string, ids: string[]) {
// verify the user has the required permissions to view this saved object
await this.savedObjectGetter(type, ids);
return await this.esContext.esAdapter.getEventLogsFieldCaps(
this.esContext.esNames.indexPattern
);
}

private async getNamespace() {
const space = await this.spacesService?.getActiveSpace(this.request);
return space && this.spacesService?.spaceIdToNamespace(space.id);
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/event_log/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type {
AggregateEventsBySavedObjectResult,
} from './es/cluster_client_adapter';
import { SavedObjectProvider } from './saved_object_provider_registry';
import { FieldDescriptor } from '../../../../src/plugins/data_views/server';

export const SAVED_OBJECT_REL_PRIMARY = 'primary';

Expand Down Expand Up @@ -61,6 +62,7 @@ export interface IEventLogClient {
options?: Partial<AggregateOptionsType>,
legacyIds?: string[]
): Promise<AggregateEventsBySavedObjectResult>;
getEventLogsFieldCaps(type: string, ids: string[]): Promise<FieldDescriptor[]>;
}

export interface IEventLogger {
Expand Down