Skip to content

Commit

Permalink
Merge branch 'main' into tsvb_discacrded_searches
Browse files Browse the repository at this point in the history
  • Loading branch information
kibanamachine authored Feb 28, 2022
2 parents 22b723e + e7e9be9 commit 7f27d29
Show file tree
Hide file tree
Showing 20 changed files with 708 additions and 24 deletions.
3 changes: 2 additions & 1 deletion test/functional/apps/console/_autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) {
const log = getService('log');
const PageObjects = getPageObjects(['common', 'console']);

describe('console autocomplete feature', function describeIndexTests() {
// Failing: See https://github.com/elastic/kibana/issues/126421
describe.skip('console autocomplete feature', function describeIndexTests() {
this.tags('includeFirefox');
before(async () => {
log.debug('navigateTo console');
Expand Down
15 changes: 15 additions & 0 deletions test/functional/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,21 @@ export default async function ({ readConfigFile }) {
kibana: [],
},

version_test: {
elasticsearch: {
cluster: [],
indices: [
{
names: ['version-test'],
privileges: ['read', 'view_index_metadata', 'manage', 'create_index', 'index'],
field_security: { grant: ['*'], except: [] },
},
],
run_as: [],
},
kibana: [],
},

kibana_sample_read: {
elasticsearch: {
cluster: [],
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/rule_registry/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
*/

export const BASE_RAC_ALERTS_API_PATH = '/internal/rac/alerts';
export const MAX_ALERT_SEARCH_SIZE = 1000;
4 changes: 3 additions & 1 deletion x-pack/plugins/rule_registry/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
export { parseTechnicalFields } from './parse_technical_fields';
export { parseTechnicalFields, type ParsedTechnicalFields } from './parse_technical_fields';
export type { RuleRegistrySearchRequest, RuleRegistrySearchResponse } from './search_strategy';
export { BASE_RAC_ALERTS_API_PATH } from './constants';
58 changes: 58 additions & 0 deletions x-pack/plugins/rule_registry/common/search_strategy/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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 { ValidFeatureId } from '@kbn/rule-data-utils';
import { Ecs } from 'kibana/server';
import * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { IEsSearchRequest, IEsSearchResponse } from 'src/plugins/data/common';

export type RuleRegistrySearchRequest = IEsSearchRequest & {
featureIds: ValidFeatureId[];
query?: { bool: estypes.QueryDslBoolQuery };
};

type Prev = [
never,
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
...Array<0>
];

type Join<K, P> = K extends string | number
? P extends string | number
? `${K}${'' extends P ? '' : '.'}${P}`
: never
: never;

type DotNestedKeys<T, D extends number = 10> = [D] extends [never]
? never
: T extends object
? { [K in keyof T]-?: Join<K, DotNestedKeys<T[K], Prev[D]>> }[keyof T]
: '';

type EcsFieldsResponse = {
[Property in DotNestedKeys<Ecs>]: string[];
};
export type RuleRegistrySearchResponse = IEsSearchResponse<EcsFieldsResponse>;
2 changes: 1 addition & 1 deletion x-pack/plugins/rule_registry/kibana.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"kibanaVersion": "kibana",
"configPath": ["xpack", "ruleRegistry"],
"requiredPlugins": ["alerting", "data", "triggersActionsUi"],
"optionalPlugins": ["security"],
"optionalPlugins": ["security", "spaces"],
"server": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
InlineScript,
QueryDslQueryContainer,
} from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { AlertTypeParams, AlertingAuthorizationFilterType } from '../../../alerting/server';
import { AlertTypeParams } from '../../../alerting/server';
import {
ReadOperations,
AlertingAuthorization,
Expand All @@ -39,6 +39,7 @@ import {
} from '../../common/technical_rule_data_field_names';
import { ParsedTechnicalFields } from '../../common/parse_technical_fields';
import { Dataset, IRuleDataService } from '../rule_data_plugin_service';
import { getAuthzFilter, getSpacesFilter } from '../lib';

// TODO: Fix typings https://github.com/elastic/kibana/issues/101776
type NonNullableProps<Obj extends {}, Props extends keyof Obj> = Omit<Obj, Props> & {
Expand Down Expand Up @@ -369,14 +370,8 @@ export class AlertsClient {
config: EsQueryConfig
) {
try {
const { filter: authzFilter } = await this.authorization.getAuthorizationFilter(
AlertingAuthorizationEntity.Alert,
{
type: AlertingAuthorizationFilterType.ESDSL,
fieldNames: { consumer: ALERT_RULE_CONSUMER, ruleTypeId: ALERT_RULE_TYPE_ID },
},
operation
);
const authzFilter = (await getAuthzFilter(this.authorization, operation)) as Filter;
const spacesFilter = getSpacesFilter(alertSpaceId) as unknown as Filter;
let esQuery;
if (id != null) {
esQuery = { query: `_id:${id}`, language: 'kuery' };
Expand All @@ -388,10 +383,7 @@ export class AlertsClient {
const builtQuery = buildEsQuery(
undefined,
esQuery == null ? { query: ``, language: 'kuery' } : esQuery,
[
authzFilter as unknown as Filter,
{ query: { term: { [SPACE_IDS]: alertSpaceId } } } as unknown as Filter,
],
[authzFilter, spacesFilter],
config
);
if (query != null && typeof query === 'object') {
Expand Down
20 changes: 20 additions & 0 deletions x-pack/plugins/rule_registry/server/lib/get_authz_filter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* 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 { alertingAuthorizationMock } from '../../../alerting/server/authorization/alerting_authorization.mock';
import { ReadOperations } from '../../../alerting/server';
import { getAuthzFilter } from './get_authz_filter';

describe('getAuthzFilter()', () => {
it('should call `getAuthorizationFilter`', async () => {
const authorization = alertingAuthorizationMock.create();
authorization.getAuthorizationFilter.mockImplementationOnce(async () => {
return { filter: { test: true }, ensureRuleTypeIsAuthorized: () => {} };
});
const filter = await getAuthzFilter(authorization, ReadOperations.Find);
expect(filter).toStrictEqual({ test: true });
});
});
33 changes: 33 additions & 0 deletions x-pack/plugins/rule_registry/server/lib/get_authz_filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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 { PublicMethodsOf } from '@kbn/utility-types';
import {
ReadOperations,
WriteOperations,
AlertingAuthorization,
AlertingAuthorizationEntity,
AlertingAuthorizationFilterType,
} from '../../../alerting/server';
import {
ALERT_RULE_CONSUMER,
ALERT_RULE_TYPE_ID,
} from '../../common/technical_rule_data_field_names';

export async function getAuthzFilter(
authorization: PublicMethodsOf<AlertingAuthorization>,
operation: WriteOperations.Update | ReadOperations.Get | ReadOperations.Find
) {
const { filter } = await authorization.getAuthorizationFilter(
AlertingAuthorizationEntity.Alert,
{
type: AlertingAuthorizationFilterType.ESDSL,
fieldNames: { consumer: ALERT_RULE_CONSUMER, ruleTypeId: ALERT_RULE_TYPE_ID },
},
operation
);
return filter;
}
20 changes: 20 additions & 0 deletions x-pack/plugins/rule_registry/server/lib/get_spaces_filter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* 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 { getSpacesFilter } from '.';
describe('getSpacesFilter()', () => {
it('should return a spaces filter', () => {
expect(getSpacesFilter('1')).toStrictEqual({
term: {
'kibana.space_ids': '1',
},
});
});

it('should return undefined if no space id is provided', () => {
expect(getSpacesFilter()).toBeUndefined();
});
});
11 changes: 11 additions & 0 deletions x-pack/plugins/rule_registry/server/lib/get_spaces_filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* 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 { SPACE_IDS } from '../../common/technical_rule_data_field_names';

export function getSpacesFilter(spaceId?: string) {
return spaceId ? { term: { [SPACE_IDS]: spaceId } } : undefined;
}
8 changes: 8 additions & 0 deletions x-pack/plugins/rule_registry/server/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* 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.
*/
export { getAuthzFilter } from './get_authz_filter';
export { getSpacesFilter } from './get_spaces_filter';
25 changes: 25 additions & 0 deletions x-pack/plugins/rule_registry/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,29 @@ import {

import { PluginStartContract as AlertingStart } from '../../alerting/server';
import { SecurityPluginSetup } from '../../security/server';
import { SpacesPluginStart } from '../../spaces/server';
import {
PluginStart as DataPluginStart,
PluginSetup as DataPluginSetup,
} from '../../../../src/plugins/data/server';

import { RuleRegistryPluginConfig } from './config';
import { IRuleDataService, RuleDataService } from './rule_data_plugin_service';
import { AlertsClientFactory } from './alert_data_client/alerts_client_factory';
import { AlertsClient } from './alert_data_client/alerts_client';
import { RacApiRequestHandlerContext, RacRequestHandlerContext } from './types';
import { defineRoutes } from './routes';
import { ruleRegistrySearchStrategyProvider } from './search_strategy';

export interface RuleRegistryPluginSetupDependencies {
security?: SecurityPluginSetup;
data: DataPluginSetup;
}

export interface RuleRegistryPluginStartDependencies {
alerting: AlertingStart;
data: DataPluginStart;
spaces?: SpacesPluginStart;
}

export interface RuleRegistryPluginSetupContract {
Expand Down Expand Up @@ -95,6 +104,22 @@ export class RuleRegistryPlugin

this.ruleDataService.initializeService();

core.getStartServices().then(([_, depsStart]) => {
const ruleRegistrySearchStrategy = ruleRegistrySearchStrategyProvider(
depsStart.data,
this.ruleDataService!,
depsStart.alerting,
logger,
plugins.security,
depsStart.spaces
);

plugins.data.search.registerSearchStrategy(
'ruleRegistryAlertsSearchStrategy',
ruleRegistrySearchStrategy
);
});

// ALERTS ROUTES
const router = core.http.createRouter<RacRequestHandlerContext>();
core.http.registerRouteHandlerContext<RacRequestHandlerContext, 'rac'>(
Expand Down
8 changes: 8 additions & 0 deletions x-pack/plugins/rule_registry/server/search_strategy/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* 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.
*/

export { ruleRegistrySearchStrategyProvider } from './search_strategy';
Loading

0 comments on commit 7f27d29

Please sign in to comment.