Skip to content

Commit

Permalink
get back index names in o11y
Browse files Browse the repository at this point in the history
  • Loading branch information
XavierM committed Aug 19, 2021
1 parent 8d1ebea commit bc3527a
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ export function AlertsTableTGrid(props: AlertsTableTGridProps) {
defaultCellActions: getDefaultCellActions({ addToQuery }),
end: rangeTo,
filters: [],
indexNames: [indexName],
indexNames: indexName.split(','),
itemsPerPage: 10,
itemsPerPageOptions: [10, 25, 50],
loadingText: i18n.translate('xpack.observability.alertsTable.loadingTextLabel', {
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/observability/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export class ObservabilityPlugin implements Plugin<ObservabilityPluginSetup> {
logger: this.initContext.logger.get(),
repository: getGlobalObservabilityServerRouteRepository(),
ruleDataClient,
ruleDataService,
});

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import { CoreSetup, CoreStart, Logger, RouteRegistrar } from 'kibana/server';
import Boom from '@hapi/boom';
import { RequestAbortedError } from '@elastic/elasticsearch/lib/errors';
import { IRuleDataClient } from '../../../rule_registry/server';
import { IRuleDataClient, RuleDataPluginService } from '../../../rule_registry/server';
import { ObservabilityRequestHandlerContext } from '../types';
import { AbstractObservabilityServerRouteRepository } from './types';

Expand All @@ -22,6 +22,7 @@ export function registerRoutes({
core,
logger,
ruleDataClient,
ruleDataService,
}: {
core: {
setup: CoreSetup;
Expand All @@ -30,6 +31,7 @@ export function registerRoutes({
repository: AbstractObservabilityServerRouteRepository;
logger: Logger;
ruleDataClient: IRuleDataClient;
ruleDataService: RuleDataPluginService;
}) {
const routes = repository.getRoutes();

Expand Down Expand Up @@ -63,6 +65,7 @@ export function registerRoutes({
logger,
params: decodedParams,
ruleDataClient,
ruleDataService,
})) as any;

return response.ok({ body: data });
Expand Down
18 changes: 15 additions & 3 deletions x-pack/plugins/observability/server/routes/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { observabilityFeatureId } from '../../common';
import * as t from 'io-ts';
import { createObservabilityServerRoute } from './create_observability_server_route';
import { createObservabilityServerRouteRepository } from './create_observability_server_route_repository';

Expand All @@ -14,8 +14,20 @@ const alertsDynamicIndexPatternRoute = createObservabilityServerRoute({
options: {
tags: [],
},
handler: async ({ ruleDataClient }) => {
const reader = ruleDataClient.getReader({ namespace: observabilityFeatureId });
params: t.type({
query: t.type({
registrationContexts: t.array(t.string),
}),
}),
handler: async ({ ruleDataService, ruleDataClient, params }) => {
const { registrationContexts } = params.query;

const indexNames = registrationContexts.map((registrationContext) =>
ruleDataService.getBaseNameByRegistrationContext(registrationContext)
);
const reader = ruleDataClient.getReader({
indexNames: indexNames.filter<string>((item: string | undefined): item is string => !!item),
});

return reader.getDynamicIndexPattern();
},
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/observability/server/routes/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {
ServerRouteRepository,
} from '@kbn/server-route-repository';
import { CoreSetup, CoreStart, KibanaRequest, Logger } from 'kibana/server';
import { IRuleDataClient } from '../../../rule_registry/server';
import { IRuleDataClient, RuleDataPluginService } from '../../../rule_registry/server';

import { ObservabilityServerRouteRepository } from './get_global_observability_server_route_repository';
import { ObservabilityRequestHandlerContext } from '../types';
Expand All @@ -25,6 +25,7 @@ export interface ObservabilityRouteHandlerResources {
setup: CoreSetup;
};
ruleDataClient: IRuleDataClient;
ruleDataService: RuleDataPluginService;
request: KibanaRequest;
context: ObservabilityRequestHandlerContext;
logger: Logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,8 @@ export class AlertsClient {

public async getAuthorizedAlertsIndices(featureIds: string[]): Promise<string[] | undefined> {
try {
// ATTENTION FUTURE DEVELOPER when you are a super user the augmentedRuleTypes.authorizedRuleTypes will
// return all of the features that you can access and does not care about your featureIds
const augmentedRuleTypes = await this.authorization.getAugmentedRuleTypesWithAuthorization(
featureIds,
[ReadOperations.Find, ReadOperations.Get, WriteOperations.Update],
Expand All @@ -665,7 +667,7 @@ export class AlertsClient {
}

const toReturn = Array.from(authorizedFeatures).flatMap((feature) => {
if (isValidFeatureId(feature)) {
if (featureIds.includes(feature) && isValidFeatureId(feature)) {
if (feature === 'siem') {
return `${mapConsumerToIndexName[feature]}-${this.spaceId}`;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ export class RuleDataClient implements IRuleDataClient {
return this.options.isWriteEnabled;
}

public getReader(options: { namespace?: string } = {}): IRuleDataReader {
public getReader(options: { namespace?: string; indexNames?: string[] } = {}): IRuleDataReader {
const { indexInfo } = this.options;
const indexPattern = indexInfo.getPatternForReading(options.namespace);
const { namespace, indexNames } = options;
let indexPattern = indexInfo.getPatternForReading(namespace);
if (indexNames && indexNames.length > 0) {
indexPattern = indexNames.join(',');
}

const waitUntilReady = async () => {
const result = await this.options.waitUntilReadyForReading;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { TechnicalRuleDataFieldName } from '../../common/technical_rule_data_fie
export interface IRuleDataClient {
indexName: string;
isWriteEnabled(): boolean;
getReader(options?: { namespace?: string }): IRuleDataReader;
getReader(options?: { namespace?: string; indexNames?: string[] }): IRuleDataReader;
getWriter(options?: { namespace?: string }): IRuleDataWriter;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class RuleDataPluginService {
private readonly resourceInstaller: ResourceInstaller;
private installCommonResources: Promise<Either<Error, 'ok'>>;
private isInitialized: boolean;
private registrationContextWithBaseName: Map<string, string>;

constructor(private readonly options: ConstructorOptions) {
this.resourceInstaller = new ResourceInstaller({
Expand All @@ -40,6 +41,7 @@ export class RuleDataPluginService {

this.installCommonResources = Promise.resolve(right('ok'));
this.isInitialized = false;
this.registrationContextWithBaseName = new Map();
}

/**
Expand Down Expand Up @@ -105,6 +107,8 @@ export class RuleDataPluginService {
indexOptions,
});

this.registrationContextWithBaseName.set(indexOptions.registrationContext, indexInfo.baseName);

const waitUntilClusterClientAvailable = async (): Promise<WaitResult> => {
try {
const clusterClient = await this.options.getClusterClient();
Expand Down Expand Up @@ -148,4 +152,13 @@ export class RuleDataPluginService {
waitUntilReadyForWriting,
});
}

/**
* Initializes alerts-as-data index and starts index bootstrapping right away.
* @param indexOptions Index parameters: names and resources.
* @returns Client for reading and writing data to this index.
*/
public getBaseNameByRegistrationContext(registrationContext: string): string | undefined {
return this.registrationContextWithBaseName.get(registrationContext);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,7 @@ const timelineAlertsSearchStrategy = <T extends TimelineFactoryQueryTypes>({
}) => {
// Based on what solution alerts you want to see, figures out what corresponding
// index to query (ex: siem --> .alerts-security.alerts)
const indices = alertConsumers.flatMap((consumer) => {
if (consumer === CONSUMERS.SIEM) {
return request.defaultIndex ?? request.indexType;
}

return `${mapConsumerToIndexName[consumer]}`;
});
const indices = request.defaultIndex ?? request.indexType;
const requestWithAlertsIndices = { ...request, defaultIndex: indices, indexName: indices };

// Note: Alerts RBAC are built off of the alerting's authorization class, which
Expand Down

0 comments on commit bc3527a

Please sign in to comment.