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

[RAC] Build a consolidated implementation in Rule Registry (Draft) #101453

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
1 change: 1 addition & 0 deletions x-pack/plugins/rule_registry/common/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const APP_ID = 'ruleRegistry';
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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 { merge } from 'lodash';
import { ComponentTemplateOptions } from '../definition';
import { commonEcsMappings } from './schema/common_ecs_fields';
import { technicalFieldMappings } from './schema/technical_fields';

/**
* Based on these options the Event Log mechanism will create and maintain
* `.alerts-settings` component template.
*/
export const commonSettingsTemplate: ComponentTemplateOptions = {
version: 1,
settings: {
number_of_shards: 1,
auto_expand_replicas: '0-1',
'mapping.total_fields.limit': 10000,
'sort.field': '@timestamp',
'sort.order': 'desc',
},
};

/**
* Based on these options the Event Log mechanism will create and maintain
* `.alerts-mappings` component template.
*/
export const commonMappingsTemplate: ComponentTemplateOptions = {
version: 1,
mappings: merge({}, commonEcsMappings, technicalFieldMappings),
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* 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 '../definition';
import { commonEcsSchema } from './schema/common_ecs_fields';
import { technicalFieldSchema } from './schema/technical_fields';

export const commonSchema = Schema.combine(commonEcsSchema, technicalFieldSchema);

export type CommonFields = typeof commonSchema.event;
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,26 @@
* 2.0.
*/

import { estypes } from '@elastic/elasticsearch';

export interface IlmPolicy {
policy: estypes.IlmPolicy;
}
import { IlmPolicy } from '../definition';

export const defaultIlmPolicy: IlmPolicy = {
policy: {
phases: {
hot: {
min_age: '0ms',
actions: {
rollover: {
max_age: '90d',
max_size: '50gb',
},
phases: {
hot: {
min_age: '0ms',
actions: {
rollover: {
max_age: '30d',
max_primary_shard_size: '50gb',
},
},
delete: {
actions: {
delete: {},
set_priority: {
priority: 100,
},
},
},
delete: {
actions: {
delete: {},
},
},
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
* 2.0.
*/

export interface IndexMappings {
dynamic: 'strict' | boolean;
properties: Record<string, { type: string } | IndexMappings>;
_meta?: Record<string, unknown>;
}
export * from './common_component_templates';
export * from './common_schema';
export * from './default_ilm_policy';
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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 { pickWithPatterns } from '../../../../common/pick_with_patterns';
import {
TIMESTAMP,
TAGS,
EVENT_KIND,
EVENT_ACTION,
RULE_UUID,
RULE_ID,
RULE_NAME,
RULE_CATEGORY,
} from '../../../../common/technical_rule_data_field_names';
import { ecsFieldMap } from '../../../assets/field_maps/ecs_field_map';
import { mappingFromFieldMap } from '../../../mapping_from_field_map';
import { runtimeTypeFromFieldMap } from '../../../field_map/runtime_type_from_fieldmap';
import { Schema } from '../../definition';

export const commonEcsFieldMap = {
...pickWithPatterns(
ecsFieldMap,
TIMESTAMP,
TAGS,
EVENT_KIND,
EVENT_ACTION,
RULE_UUID,
RULE_ID,
RULE_NAME,
RULE_CATEGORY
),
} as const;

export const commonEcsMappings = mappingFromFieldMap(commonEcsFieldMap);
export const commonEcsSchema = Schema.create(runtimeTypeFromFieldMap(commonEcsFieldMap));
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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 {
ALERT_DURATION,
ALERT_END,
ALERT_EVALUATION_THRESHOLD,
ALERT_EVALUATION_VALUE,
ALERT_ID,
ALERT_SEVERITY_LEVEL,
ALERT_SEVERITY_VALUE,
ALERT_START,
ALERT_STATUS,
ALERT_UUID,
PRODUCER,
} from '../../../../common/technical_rule_data_field_names';
import { mappingFromFieldMap } from '../../../mapping_from_field_map';
import { runtimeTypeFromFieldMap } from '../../../field_map/runtime_type_from_fieldmap';
import { Schema } from '../../definition';

export const technicalFieldMap = {
[PRODUCER]: { type: 'keyword' },
[ALERT_UUID]: { type: 'keyword' },
[ALERT_ID]: { type: 'keyword' },
[ALERT_START]: { type: 'date' },
[ALERT_END]: { type: 'date' },
[ALERT_DURATION]: { type: 'long' },
[ALERT_SEVERITY_LEVEL]: { type: 'keyword' },
[ALERT_SEVERITY_VALUE]: { type: 'long' },
[ALERT_STATUS]: { type: 'keyword' },
[ALERT_EVALUATION_THRESHOLD]: { type: 'scaled_float', scaling_factor: 100 },
[ALERT_EVALUATION_VALUE]: { type: 'scaled_float', scaling_factor: 100 },
} as const;

export const technicalFieldMappings = mappingFromFieldMap(technicalFieldMap);
export const technicalFieldSchema = Schema.create(runtimeTypeFromFieldMap(technicalFieldMap));
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* 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 { EventSchema } from './event_schema';
import { IlmPolicy } from './ilm_policy';
import { Templates } from './index_template';

export interface EventLogDefinition<TEvent> {
logName: string;
schema: EventSchema<TEvent>;
templates: Templates;
ilmPolicy?: IlmPolicy;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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 * as t from 'io-ts';

export interface EventSchema<TEvent> {
eventType: t.Type<TEvent>;
event: TEvent;
}

export abstract class Schema {
public static create<TEvent>(eventType: t.Type<TEvent>): EventSchema<TEvent> {
return {
eventType,
event: {} as t.TypeOf<typeof eventType>,
};
}

public static combine<T1, T2>(s1: EventSchema<T1>, s2: EventSchema<T2>): EventSchema<T1 & T2> {
const combinedType = t.intersection([s1.eventType, s2.eventType]);
return this.create(combinedType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
* 2.0.
*/

export * from './schema_types';
export * from './schema';
import { estypes } from '@elastic/elasticsearch';

export type IlmPolicy = estypes.IlmPolicy;
12 changes: 12 additions & 0 deletions x-pack/plugins/rule_registry/common/event_log/definition/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* 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 * from './event_log_definition';
export * from './event_schema';
export * from './ilm_policy';
export * from './index_names';
export * from './index_template';
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,31 @@ export interface IndexNames extends IndexParams {
/** @example '.alerts-security.alerts-default-*' */
indexAliasPattern: string;

/** @example '.alerts-security.alerts-default-000001' */
indexInitialName: string;

/** @example '.alerts-security.alerts-default-policy' */
indexIlmPolicyName: string;

/** @example '.alerts-security.alerts-default-template' */
indexTemplateName: string;

/** @example '.alerts-security.alerts-default-000001' */
indexInitialName: string;
componentTemplates: {
/** @example '.alerts-mappings' */
commonMappingsTemplateName: string;

/** @example '.alerts-settings' */
commonSettingsTemplateName: string;

/** @example '.alerts-security.alerts-app' */
applicationDefinedTemplateName: string;

/** @example '.alerts-security.alerts-user' */
userDefinedTemplateName: string;

/** @example '.alerts-security.alerts-user-default' */
userDefinedSpaceAwareTemplateName: string;
};
}

export abstract class IndexNames {
Expand All @@ -49,9 +66,19 @@ export abstract class IndexNames {
const indexBasePattern = joinWithDash(indexPrefix, logName, '*');
const indexAliasName = joinWithDash(indexPrefix, logName, kibanaSpaceId);
const indexAliasPattern = joinWithDash(indexPrefix, logName, kibanaSpaceId, '*');
const indexInitialName = joinWithDash(indexPrefix, logName, kibanaSpaceId, '000001');
const indexIlmPolicyName = joinWithDash(indexPrefix, logName, kibanaSpaceId, 'policy');
const indexTemplateName = joinWithDash(indexPrefix, logName, kibanaSpaceId, 'template');
const indexInitialName = joinWithDash(indexPrefix, logName, kibanaSpaceId, '000001');
const commonMappingsTemplateName = joinWithDash(indexPrefix, 'mappings');
const commonSettingsTemplateName = joinWithDash(indexPrefix, 'settings');
const applicationDefinedTemplateName = joinWithDash(indexPrefix, logName, 'app');
const userDefinedTemplateName = joinWithDash(indexPrefix, logName, 'user');
const userDefinedSpaceAwareTemplateName = joinWithDash(
indexPrefix,
logName,
'user',
kibanaSpaceId
);

return {
indexPrefix,
Expand All @@ -61,9 +88,16 @@ export abstract class IndexNames {
indexBasePattern,
indexAliasName,
indexAliasPattern,
indexInitialName,
indexIlmPolicyName,
indexTemplateName,
indexInitialName,
componentTemplates: {
commonMappingsTemplateName,
commonSettingsTemplateName,
applicationDefinedTemplateName,
userDefinedTemplateName,
userDefinedSpaceAwareTemplateName,
},
};
}

Expand Down
Loading