From c286c96be0a502cd38e0208cfdbc56f9ca662e6e Mon Sep 17 00:00:00 2001 From: Aaron Caldwell Date: Mon, 20 Sep 2021 16:32:13 -0600 Subject: [PATCH 01/18] Add inject and extract methods to alert type --- .../alert_types/geo_containment/alert_type.ts | 60 ++++++++++++++++++- 1 file changed, 58 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/alert_type.ts b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/alert_type.ts index 111fda3bdaca8..193ee9038e1a5 100644 --- a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/alert_type.ts +++ b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/alert_type.ts @@ -7,7 +7,7 @@ import { i18n } from '@kbn/i18n'; import { schema } from '@kbn/config-schema'; -import { Logger } from 'src/core/server'; +import { Logger, SavedObjectReference } from 'src/core/server'; import { STACK_ALERTS_FEATURE_ID } from '../../../common'; import { getGeoContainmentExecutor } from './geo_containment'; import { @@ -16,6 +16,7 @@ import { AlertInstanceState, AlertInstanceContext, AlertTypeParams, + RuleParamsAndRefs, } from '../../../../alerting/server'; import { Query } from '../../../../../../src/plugins/data/common/query'; @@ -117,6 +118,12 @@ export interface GeoContainmentParams extends AlertTypeParams { indexQuery?: Query; boundaryIndexQuery?: Query; } +interface GeoContainmentExtractedParams + extends Omit { + boundaryIndexQuery?: Query; + indexRef: string; + boundaryIndexRef: string; +} export interface GeoContainmentState extends AlertTypeState { shapesFilters: Record; shapesIdsNamesMap: Record; @@ -140,7 +147,7 @@ export interface GeoContainmentInstanceContext extends AlertInstanceContext { export type GeoContainmentAlertType = AlertType< GeoContainmentParams, - never, // Only use if defining useSavedObjectReferences hook + GeoContainmentExtractedParams, GeoContainmentState, GeoContainmentInstanceState, GeoContainmentInstanceContext, @@ -179,5 +186,54 @@ export function getAlertType(logger: Logger): GeoContainmentAlertType { actionVariables, minimumLicenseRequired: 'gold', isExportable: true, + useSavedObjectReferences: { + extractReferences: ( + params: GeoContainmentParams + ): RuleParamsAndRefs => { + const { indexId, boundaryIndexId, ...otherParams } = params; + + const references = [ + { + name: `tracked_index_${indexId}`, + type: 'index-pattern', + id: indexId as string, + }, + { + name: `boundary_index_${boundaryIndexId}`, + type: 'index-pattern', + id: boundaryIndexId as string, + }, + ]; + return { + params: { + ...otherParams, + indexRef: `tracked_index_${indexId}`, + boundaryIndexRef: `boundary_index_${boundaryIndexId}`, + }, + references, + }; + }, + injectReferences: ( + params: GeoContainmentExtractedParams, + references: SavedObjectReference[] + ) => { + const { indexRef, boundaryIndexRef, ...otherParams } = params; + const indexReference = references.find((ref) => ref.name === indexRef); + const boundaryIndexReference = references.find((ref) => ref.name === boundaryIndexRef); + if (!indexReference) { + throw new Error(`Index reference "${indexRef}" not found in references array`); + } + if (!boundaryIndexReference) { + throw new Error( + `Boundary index reference "${boundaryIndexReference}" not found in references array` + ); + } + return { + ...otherParams, + indexId: indexReference.id, + boundaryIndexId: boundaryIndexReference.id, + } as GeoContainmentParams; + }, + }, }; } From 616eae3e042e53d1a54756125e8d6366f097377a Mon Sep 17 00:00:00 2001 From: Aaron Caldwell Date: Mon, 20 Sep 2021 17:06:01 -0600 Subject: [PATCH 02/18] Minor clean up --- .../alert_types/geo_containment/alert_type.ts | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/alert_type.ts b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/alert_type.ts index 193ee9038e1a5..e2363ae96c5bb 100644 --- a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/alert_type.ts +++ b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/alert_type.ts @@ -218,20 +218,19 @@ export function getAlertType(logger: Logger): GeoContainmentAlertType { references: SavedObjectReference[] ) => { const { indexRef, boundaryIndexRef, ...otherParams } = params; - const indexReference = references.find((ref) => ref.name === indexRef); - const boundaryIndexReference = references.find((ref) => ref.name === boundaryIndexRef); - if (!indexReference) { - throw new Error(`Index reference "${indexRef}" not found in references array`); + const { id: indexId = null } = references.find((ref) => ref.name === indexRef) || {}; + const { id: boundaryIndexId = null } = + references.find((ref) => ref.name === boundaryIndexRef) || {}; + if (!indexId) { + throw new Error(`Index "${indexId}" not found in references array`); } - if (!boundaryIndexReference) { - throw new Error( - `Boundary index reference "${boundaryIndexReference}" not found in references array` - ); + if (!boundaryIndexId) { + throw new Error(`Boundary index "${boundaryIndexId}" not found in references array`); } return { ...otherParams, - indexId: indexReference.id, - boundaryIndexId: boundaryIndexReference.id, + indexId, + boundaryIndexId, } as GeoContainmentParams; }, }, From 107554869f963026204e877c9cf22d07583e2064 Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Mon, 11 Oct 2021 15:05:41 -0400 Subject: [PATCH 03/18] tmp --- .../server/alert_types/geo_containment/alert_type.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/alert_type.ts b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/alert_type.ts index e2363ae96c5bb..5ebe8f32428d3 100644 --- a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/alert_type.ts +++ b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/alert_type.ts @@ -192,6 +192,9 @@ export function getAlertType(logger: Logger): GeoContainmentAlertType { ): RuleParamsAndRefs => { const { indexId, boundaryIndexId, ...otherParams } = params; + console.log('-------------extract recerences'); + console.log(indexId, boundaryIndexId, otherParams, params); + const references = [ { name: `tracked_index_${indexId}`, @@ -217,6 +220,9 @@ export function getAlertType(logger: Logger): GeoContainmentAlertType { params: GeoContainmentExtractedParams, references: SavedObjectReference[] ) => { + console.log('-------------inejct recerences'); + console.log(params); + const { indexRef, boundaryIndexRef, ...otherParams } = params; const { id: indexId = null } = references.find((ref) => ref.name === indexRef) || {}; const { id: boundaryIndexId = null } = From 4d3bcff09f7e55a75de9f50ef9ead808b4d4593b Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Mon, 11 Oct 2021 16:02:06 -0400 Subject: [PATCH 04/18] remove debug --- .../server/alert_types/geo_containment/alert_type.ts | 7 ------- 1 file changed, 7 deletions(-) diff --git a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/alert_type.ts b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/alert_type.ts index 5ebe8f32428d3..10d7a88eb281f 100644 --- a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/alert_type.ts +++ b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/alert_type.ts @@ -191,10 +191,6 @@ export function getAlertType(logger: Logger): GeoContainmentAlertType { params: GeoContainmentParams ): RuleParamsAndRefs => { const { indexId, boundaryIndexId, ...otherParams } = params; - - console.log('-------------extract recerences'); - console.log(indexId, boundaryIndexId, otherParams, params); - const references = [ { name: `tracked_index_${indexId}`, @@ -220,9 +216,6 @@ export function getAlertType(logger: Logger): GeoContainmentAlertType { params: GeoContainmentExtractedParams, references: SavedObjectReference[] ) => { - console.log('-------------inejct recerences'); - console.log(params); - const { indexRef, boundaryIndexRef, ...otherParams } = params; const { id: indexId = null } = references.find((ref) => ref.name === indexRef) || {}; const { id: boundaryIndexId = null } = From 0869d18b32f39b240f0852791e327b1e94e956b2 Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Mon, 11 Oct 2021 17:32:33 -0400 Subject: [PATCH 05/18] add migration --- x-pack/plugins/alerting/server/index.ts | 8 ++ .../geo_containment/migrations.ts | 112 ++++++++++++++++++ .../server/saved_objects/migrations.ts | 4 +- .../alert_types/geo_containment/alert_type.ts | 66 ++--------- 4 files changed, 130 insertions(+), 60 deletions(-) create mode 100644 x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.ts diff --git a/x-pack/plugins/alerting/server/index.ts b/x-pack/plugins/alerting/server/index.ts index 162ee06216304..b8dd8b608da66 100644 --- a/x-pack/plugins/alerting/server/index.ts +++ b/x-pack/plugins/alerting/server/index.ts @@ -43,6 +43,14 @@ export { AlertingAuthorizationEntity, } from './authorization'; +export { + extractEntityAndBoundaryReferences, + injectEntityAndBoundaryIds, + GeoContainmentParams, + GeoContainmentExtractedParams, + GEO_CONTAINMENT_ID, +} from './saved_objects/geo_containment/migrations'; + export const plugin = (initContext: PluginInitializerContext) => new AlertingPlugin(initContext); export const config: PluginConfigDescriptor = { diff --git a/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.ts b/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.ts new file mode 100644 index 0000000000000..aa2a7a3a5cc2c --- /dev/null +++ b/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.ts @@ -0,0 +1,112 @@ +/* + * 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 { + SavedObjectAttributes, + SavedObjectReference, + SavedObjectUnsanitizedDoc, +} from 'kibana/server'; +import { AlertTypeParams } from '../../index'; +import { Query } from '../../../../../../src/plugins/data/common/query'; +import { RawAlert } from '../../types'; + +export const GEO_CONTAINMENT_ID = '.geo-containment'; + +export interface GeoContainmentParams extends AlertTypeParams { + index: string; + indexId: string; + geoField: string; + entity: string; + dateField: string; + boundaryType: string; + boundaryIndexTitle: string; + boundaryIndexId: string; + boundaryGeoField: string; + boundaryNameField?: string; + indexQuery?: Query; + boundaryIndexQuery?: Query; +} + +export type GeoContainmentExtractedParams = Omit< + GeoContainmentParams, + 'indexId' | 'boundaryIndexId' +> & { + indexRef: string; + boundaryIndexRef: string; +}; + +export function extractEntityAndBoundaryReferences(params: GeoContainmentParams): { + params: GeoContainmentExtractedParams; + references: SavedObjectReference[]; +} { + const { indexId, boundaryIndexId, ...otherParams } = params; + const references = [ + { + name: `tracked_index_${indexId}`, + type: 'index-pattern', + id: indexId as string, + }, + { + name: `boundary_index_${boundaryIndexId}`, + type: 'index-pattern', + id: boundaryIndexId as string, + }, + ]; + return { + params: { + ...otherParams, + indexRef: `tracked_index_${indexId}`, + boundaryIndexRef: `boundary_index_${boundaryIndexId}`, + }, + references, + }; +} + +export function injectEntityAndBoundaryIds( + params: GeoContainmentExtractedParams, + references: SavedObjectReference[] +): GeoContainmentParams { + const { indexRef, boundaryIndexRef, ...otherParams } = params; + const { id: indexId = null } = references.find((ref) => ref.name === indexRef) || {}; + const { id: boundaryIndexId = null } = + references.find((ref) => ref.name === boundaryIndexRef) || {}; + if (!indexId) { + throw new Error(`Index "${indexId}" not found in references array`); + } + if (!boundaryIndexId) { + throw new Error(`Boundary index "${boundaryIndexId}" not found in references array`); + } + return { + ...otherParams, + indexId, + boundaryIndexId, + } as GeoContainmentParams; +} + +export function extractIndexAndBoundaryRefsFromGeoContainmentStackAlert( + doc: SavedObjectUnsanitizedDoc +): SavedObjectUnsanitizedDoc { + if (doc.attributes.alertTypeId === GEO_CONTAINMENT_ID) { + return doc; + } + + const { + attributes: { params }, + } = doc; + + const { params: newParams, references } = extractEntityAndBoundaryReferences( + params as GeoContainmentParams + ); + return { + ...doc, + attributes: { + ...doc.attributes, + params: newParams as SavedObjectAttributes, + }, + references, + }; +} diff --git a/x-pack/plugins/alerting/server/saved_objects/migrations.ts b/x-pack/plugins/alerting/server/saved_objects/migrations.ts index 9dcca54285279..6dfc7dfbbe2a8 100644 --- a/x-pack/plugins/alerting/server/saved_objects/migrations.ts +++ b/x-pack/plugins/alerting/server/saved_objects/migrations.ts @@ -19,6 +19,7 @@ import { import { RawAlert, RawAlertAction } from '../types'; import { EncryptedSavedObjectsPluginSetup } from '../../../encrypted_saved_objects/server'; import type { IsMigrationNeededPredicate } from '../../../encrypted_saved_objects/server'; +import { extractIndexAndBoundaryRefsFromGeoContainmentStackAlert } from './geo_containment/migrations'; const SIEM_APP_ID = 'securitySolution'; const SIEM_SERVER_APP_ID = 'siem'; @@ -117,7 +118,8 @@ export function getMigrations( pipeMigrations( setLegacyId, getRemovePreconfiguredConnectorsFromReferencesFn(isPreconfigured), - addRuleIdsToLegacyNotificationReferences + addRuleIdsToLegacyNotificationReferences, + extractIndexAndBoundaryRefsFromGeoContainmentStackAlert ) ); diff --git a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/alert_type.ts b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/alert_type.ts index 10d7a88eb281f..2104b43926ecd 100644 --- a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/alert_type.ts +++ b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/alert_type.ts @@ -15,12 +15,14 @@ import { AlertTypeState, AlertInstanceState, AlertInstanceContext, - AlertTypeParams, RuleParamsAndRefs, + extractEntityAndBoundaryReferences, + injectEntityAndBoundaryIds, + GeoContainmentExtractedParams, + GeoContainmentParams, + GEO_CONTAINMENT_ID, } from '../../../../alerting/server'; -import { Query } from '../../../../../../src/plugins/data/common/query'; -export const GEO_CONTAINMENT_ID = '.geo-containment'; export const ActionGroupId = 'Tracked entity contained'; export const RecoveryActionGroupId = 'notGeoContained'; @@ -104,26 +106,6 @@ export const ParamsSchema = schema.object({ boundaryIndexQuery: schema.maybe(schema.any({})), }); -export interface GeoContainmentParams extends AlertTypeParams { - index: string; - indexId: string; - geoField: string; - entity: string; - dateField: string; - boundaryType: string; - boundaryIndexTitle: string; - boundaryIndexId: string; - boundaryGeoField: string; - boundaryNameField?: string; - indexQuery?: Query; - boundaryIndexQuery?: Query; -} -interface GeoContainmentExtractedParams - extends Omit { - boundaryIndexQuery?: Query; - indexRef: string; - boundaryIndexRef: string; -} export interface GeoContainmentState extends AlertTypeState { shapesFilters: Record; shapesIdsNamesMap: Record; @@ -190,47 +172,13 @@ export function getAlertType(logger: Logger): GeoContainmentAlertType { extractReferences: ( params: GeoContainmentParams ): RuleParamsAndRefs => { - const { indexId, boundaryIndexId, ...otherParams } = params; - const references = [ - { - name: `tracked_index_${indexId}`, - type: 'index-pattern', - id: indexId as string, - }, - { - name: `boundary_index_${boundaryIndexId}`, - type: 'index-pattern', - id: boundaryIndexId as string, - }, - ]; - return { - params: { - ...otherParams, - indexRef: `tracked_index_${indexId}`, - boundaryIndexRef: `boundary_index_${boundaryIndexId}`, - }, - references, - }; + return extractEntityAndBoundaryReferences(params); }, injectReferences: ( params: GeoContainmentExtractedParams, references: SavedObjectReference[] ) => { - const { indexRef, boundaryIndexRef, ...otherParams } = params; - const { id: indexId = null } = references.find((ref) => ref.name === indexRef) || {}; - const { id: boundaryIndexId = null } = - references.find((ref) => ref.name === boundaryIndexRef) || {}; - if (!indexId) { - throw new Error(`Index "${indexId}" not found in references array`); - } - if (!boundaryIndexId) { - throw new Error(`Boundary index "${boundaryIndexId}" not found in references array`); - } - return { - ...otherParams, - indexId, - boundaryIndexId, - } as GeoContainmentParams; + return injectEntityAndBoundaryIds(params, references); }, }, }; From 39c887563e9d85c808de90e2d682ca419a3ee446 Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Mon, 11 Oct 2021 17:40:48 -0400 Subject: [PATCH 06/18] fix imports --- .../server/alert_types/geo_containment/geo_containment.ts | 3 ++- x-pack/plugins/stack_alerts/server/feature.ts | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/geo_containment.ts b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/geo_containment.ts index 21a536dd474ba..3598daca7f72c 100644 --- a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/geo_containment.ts +++ b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/geo_containment.ts @@ -12,13 +12,14 @@ import { executeEsQueryFactory, getShapesFilters, OTHER_CATEGORY } from './es_qu import { AlertServices } from '../../../../alerting/server'; import { ActionGroupId, - GEO_CONTAINMENT_ID, GeoContainmentInstanceState, GeoContainmentAlertType, GeoContainmentInstanceContext, GeoContainmentState, } from './alert_type'; +import { GEO_CONTAINMENT_ID } from '../../../../alerting/server'; + export type LatestEntityLocation = GeoContainmentInstanceState; // Flatten agg results and get latest locations for each entity diff --git a/x-pack/plugins/stack_alerts/server/feature.ts b/x-pack/plugins/stack_alerts/server/feature.ts index 39ea41374df7b..37e00286ea90a 100644 --- a/x-pack/plugins/stack_alerts/server/feature.ts +++ b/x-pack/plugins/stack_alerts/server/feature.ts @@ -8,7 +8,7 @@ import { i18n } from '@kbn/i18n'; import { KibanaFeatureConfig } from '../../../plugins/features/common'; import { ID as IndexThreshold } from './alert_types/index_threshold/alert_type'; -import { GEO_CONTAINMENT_ID as GeoContainment } from './alert_types/geo_containment/alert_type'; +import { GEO_CONTAINMENT_ID as GeoContainment } from '../../alerting/server'; import { ES_QUERY_ID as ElasticsearchQuery } from './alert_types/es_query/alert_type'; import { STACK_ALERTS_FEATURE_ID } from '../common'; import { DEFAULT_APP_CATEGORIES } from '../../../../src/core/server'; From 4220ebc4f6cd79790471c67047d55b42988a1a3f Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Mon, 11 Oct 2021 18:14:55 -0400 Subject: [PATCH 07/18] type fixes --- .../alert_types/geo_containment/tests/alert_type.test.ts | 3 ++- .../geo_containment/tests/geo_containment.test.ts | 7 ++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/tests/alert_type.test.ts b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/tests/alert_type.test.ts index e8f699eb06161..ba53c3210da5b 100644 --- a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/tests/alert_type.test.ts +++ b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/tests/alert_type.test.ts @@ -6,7 +6,8 @@ */ import { loggingSystemMock } from '../../../../../../../src/core/server/mocks'; -import { getAlertType, GeoContainmentParams } from '../alert_type'; +import { getAlertType } from '../alert_type'; +import type { GeoContainmentParams } from '../../../../../alerting/server'; describe('alertType', () => { const logger = loggingSystemMock.create().get(); diff --git a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/tests/geo_containment.test.ts b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/tests/geo_containment.test.ts index 364c484a02080..bbf610babe20b 100644 --- a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/tests/geo_containment.test.ts +++ b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/tests/geo_containment.test.ts @@ -17,11 +17,8 @@ import { getGeoContainmentExecutor, } from '../geo_containment'; import { OTHER_CATEGORY } from '../es_query_builder'; -import { - GeoContainmentInstanceContext, - GeoContainmentInstanceState, - GeoContainmentParams, -} from '../alert_type'; +import { GeoContainmentInstanceContext, GeoContainmentInstanceState } from '../alert_type'; +import type { GeoContainmentParams } from '../../../../../alerting/server'; const alertInstanceFactory = (contextKeys: unknown[], testAlertActionArr: unknown[]) => (instanceId: string) => { From 58e36312e13025b1468e08b90352127f3738e4cf Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Tue, 12 Oct 2021 09:17:54 -0400 Subject: [PATCH 08/18] fix bug --- .../alerting/server/saved_objects/geo_containment/migrations.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.ts b/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.ts index aa2a7a3a5cc2c..a739c527ec667 100644 --- a/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.ts +++ b/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.ts @@ -90,7 +90,7 @@ export function injectEntityAndBoundaryIds( export function extractIndexAndBoundaryRefsFromGeoContainmentStackAlert( doc: SavedObjectUnsanitizedDoc ): SavedObjectUnsanitizedDoc { - if (doc.attributes.alertTypeId === GEO_CONTAINMENT_ID) { + if (doc.attributes.alertTypeId !== GEO_CONTAINMENT_ID) { return doc; } From 3618b6a0135caec986b17da4196a8770ca1307d7 Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Tue, 12 Oct 2021 09:47:00 -0400 Subject: [PATCH 09/18] type fixes --- .../stack_alerts/server/alert_types/geo_containment/index.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/index.ts b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/index.ts index 023ea168a77d2..08569f7f24e5f 100644 --- a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/index.ts +++ b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/index.ts @@ -8,7 +8,6 @@ import { Logger } from 'src/core/server'; import { AlertingSetup } from '../../types'; import { - GeoContainmentParams, GeoContainmentState, GeoContainmentInstanceState, GeoContainmentInstanceContext, @@ -17,6 +16,8 @@ import { RecoveryActionGroupId, } from './alert_type'; +import { GeoContainmentExtractedParams, GeoContainmentParams } from '../../../../alerting/server'; + interface RegisterParams { logger: Logger; alerting: AlertingSetup; @@ -26,7 +27,7 @@ export function register(params: RegisterParams) { const { logger, alerting } = params; alerting.registerType< GeoContainmentParams, - never, // Only use if defining useSavedObjectReferences hook + GeoContainmentExtractedParams, GeoContainmentState, GeoContainmentInstanceState, GeoContainmentInstanceContext, From 695f42dd504b092ad9cb4e565a475c46cefd19ed Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Tue, 12 Oct 2021 16:47:40 -0400 Subject: [PATCH 10/18] add unit tests --- .../geo_containment/migrations.test.ts | 90 +++++++++++++++++++ .../server/saved_objects/migrations.test.ts | 28 ++++++ 2 files changed, 118 insertions(+) create mode 100644 x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.test.ts diff --git a/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.test.ts b/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.test.ts new file mode 100644 index 0000000000000..fd2449f5c33d0 --- /dev/null +++ b/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.test.ts @@ -0,0 +1,90 @@ +/* + * 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 { extractEntityAndBoundaryReferences, injectEntityAndBoundaryIds } from './migrations'; + +describe('geo_containment migration utilities', () => { + test('extractEntityAndBoundaryReferences', () => { + expect( + extractEntityAndBoundaryReferences({ + index: 'foo*', + indexId: 'foobar', + geoField: 'geometry', + entity: 'vehicle_id', + dateField: '@timestamp', + boundaryType: 'entireIndex', + boundaryIndexTitle: 'boundary*', + boundaryIndexId: 'boundaryid', + boundaryGeoField: 'geometry', + }) + ).toEqual({ + params: { + boundaryGeoField: 'geometry', + boundaryIndexRef: 'boundary_index_boundaryid', + boundaryIndexTitle: 'boundary*', + boundaryType: 'entireIndex', + dateField: '@timestamp', + entity: 'vehicle_id', + geoField: 'geometry', + index: 'foo*', + indexRef: 'tracked_index_foobar', + }, + references: [ + { + id: 'foobar', + name: 'tracked_index_foobar', + type: 'index-pattern', + }, + { + id: 'boundaryid', + name: 'boundary_index_boundaryid', + type: 'index-pattern', + }, + ], + }); + }); + + test('injectEntityAndBoundaryIds', () => { + expect( + injectEntityAndBoundaryIds( + { + boundaryGeoField: 'geometry', + boundaryIndexRef: 'boundary_index_boundaryid', + boundaryIndexTitle: 'boundary*', + boundaryType: 'entireIndex', + dateField: '@timestamp', + entity: 'vehicle_id', + geoField: 'geometry', + index: 'foo*', + indexRef: 'tracked_index_foobar', + }, + [ + { + id: 'foobar', + name: 'tracked_index_foobar', + type: 'index-pattern', + }, + { + id: 'boundaryid', + name: 'boundary_index_boundaryid', + type: 'index-pattern', + }, + ] + ) + ).toEqual({ + index: 'foo*', + indexId: 'foobar', + geoField: 'geometry', + entity: 'vehicle_id', + dateField: '@timestamp', + boundaryType: 'entireIndex', + boundaryIndexTitle: 'boundary*', + boundaryIndexId: 'boundaryid', + boundaryGeoField: 'geometry', + }); + }); +}); diff --git a/x-pack/plugins/alerting/server/saved_objects/migrations.test.ts b/x-pack/plugins/alerting/server/saved_objects/migrations.test.ts index 3f7cdecf4affd..3d4938c29323d 100644 --- a/x-pack/plugins/alerting/server/saved_objects/migrations.test.ts +++ b/x-pack/plugins/alerting/server/saved_objects/migrations.test.ts @@ -1913,6 +1913,34 @@ describe('successful migrations', () => { ], }); }); + + test('extracts references from geo-containment alerts', () => { + const migration7160 = getMigrations(encryptedSavedObjectsSetup, isPreconfigured)['7.16.0']; + const alert = { + ...getMockData({ + alertTypeId: '.geo-containment', + params: { + indexId: 'foo', + boundaryIndexId: 'bar', + }, + }), + }; + + const migratedAlert = migration7160(alert, migrationContext); + + expect(migratedAlert.references).toEqual([ + { id: 'foo', name: 'tracked_index_foo', type: 'index-pattern' }, + { id: 'bar', name: 'boundary_index_bar', type: 'index-pattern' }, + ]); + + expect(migratedAlert.attributes.params).toEqual({ + boundaryIndexRef: 'boundary_index_bar', + indexRef: 'tracked_index_foo', + }); + + expect(migratedAlert.attributes.params.indexId).toEqual(undefined); + expect(migratedAlert.attributes.params.boundaryIndexId).toEqual(undefined); + }); }); describe('8.0.0', () => { From 621e7c77b63e7bc5bd6f9cd4afef04024d3ad6ce Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Wed, 13 Oct 2021 11:42:52 -0400 Subject: [PATCH 11/18] renames --- .../geo_containment/migrations.test.ts | 4 ++-- .../saved_objects/geo_containment/migrations.ts | 16 ++++++++-------- .../alerting/server/saved_objects/migrations.ts | 4 ++-- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.test.ts b/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.test.ts index fd2449f5c33d0..aef19a3e1f402 100644 --- a/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.test.ts +++ b/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.test.ts @@ -53,14 +53,14 @@ describe('geo_containment migration utilities', () => { injectEntityAndBoundaryIds( { boundaryGeoField: 'geometry', - boundaryIndexRef: 'boundary_index_boundaryid', + boundaryIndexRefName: 'boundary_index_boundaryid', boundaryIndexTitle: 'boundary*', boundaryType: 'entireIndex', dateField: '@timestamp', entity: 'vehicle_id', geoField: 'geometry', index: 'foo*', - indexRef: 'tracked_index_foobar', + indexRefName: 'tracked_index_foobar', }, [ { diff --git a/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.ts b/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.ts index a739c527ec667..a783ee13b48ff 100644 --- a/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.ts +++ b/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.ts @@ -35,8 +35,8 @@ export type GeoContainmentExtractedParams = Omit< GeoContainmentParams, 'indexId' | 'boundaryIndexId' > & { - indexRef: string; - boundaryIndexRef: string; + indexRefName: string; + boundaryIndexRefName: string; }; export function extractEntityAndBoundaryReferences(params: GeoContainmentParams): { @@ -59,8 +59,8 @@ export function extractEntityAndBoundaryReferences(params: GeoContainmentParams) return { params: { ...otherParams, - indexRef: `tracked_index_${indexId}`, - boundaryIndexRef: `boundary_index_${boundaryIndexId}`, + indexRefName: `tracked_index_${indexId}`, + boundaryIndexRefName: `boundary_index_${boundaryIndexId}`, }, references, }; @@ -70,10 +70,10 @@ export function injectEntityAndBoundaryIds( params: GeoContainmentExtractedParams, references: SavedObjectReference[] ): GeoContainmentParams { - const { indexRef, boundaryIndexRef, ...otherParams } = params; - const { id: indexId = null } = references.find((ref) => ref.name === indexRef) || {}; + const { indexRefName, boundaryIndexRefName, ...otherParams } = params; + const { id: indexId = null } = references.find((ref) => ref.name === indexRefName) || {}; const { id: boundaryIndexId = null } = - references.find((ref) => ref.name === boundaryIndexRef) || {}; + references.find((ref) => ref.name === boundaryIndexRefName) || {}; if (!indexId) { throw new Error(`Index "${indexId}" not found in references array`); } @@ -87,7 +87,7 @@ export function injectEntityAndBoundaryIds( } as GeoContainmentParams; } -export function extractIndexAndBoundaryRefsFromGeoContainmentStackAlert( +export function extractRefsFromGeoContainmentAlert( doc: SavedObjectUnsanitizedDoc ): SavedObjectUnsanitizedDoc { if (doc.attributes.alertTypeId !== GEO_CONTAINMENT_ID) { diff --git a/x-pack/plugins/alerting/server/saved_objects/migrations.ts b/x-pack/plugins/alerting/server/saved_objects/migrations.ts index 6dfc7dfbbe2a8..0a1d7bfc8a9d7 100644 --- a/x-pack/plugins/alerting/server/saved_objects/migrations.ts +++ b/x-pack/plugins/alerting/server/saved_objects/migrations.ts @@ -19,7 +19,7 @@ import { import { RawAlert, RawAlertAction } from '../types'; import { EncryptedSavedObjectsPluginSetup } from '../../../encrypted_saved_objects/server'; import type { IsMigrationNeededPredicate } from '../../../encrypted_saved_objects/server'; -import { extractIndexAndBoundaryRefsFromGeoContainmentStackAlert } from './geo_containment/migrations'; +import { extractRefsFromGeoContainmentAlert } from './geo_containment/migrations'; const SIEM_APP_ID = 'securitySolution'; const SIEM_SERVER_APP_ID = 'siem'; @@ -119,7 +119,7 @@ export function getMigrations( setLegacyId, getRemovePreconfiguredConnectorsFromReferencesFn(isPreconfigured), addRuleIdsToLegacyNotificationReferences, - extractIndexAndBoundaryRefsFromGeoContainmentStackAlert + extractRefsFromGeoContainmentAlert ) ); From 3a546aef6edf001d5a490403978c9389f83d60cf Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Wed, 13 Oct 2021 11:57:22 -0400 Subject: [PATCH 12/18] move types --- x-pack/plugins/alerting/server/index.ts | 8 +-- .../geo_containment/migrations.test.ts | 42 +--------------- .../geo_containment/migrations.ts | 34 ++----------- .../alert_types/geo_containment/alert_type.ts | 50 +++++++++++++++++-- .../alert_types/geo_containment/index.ts | 2 +- .../geo_containment/tests/alert_type.test.ts | 43 +++++++++++++++- .../tests/geo_containment.test.ts | 2 +- 7 files changed, 96 insertions(+), 85 deletions(-) diff --git a/x-pack/plugins/alerting/server/index.ts b/x-pack/plugins/alerting/server/index.ts index b8dd8b608da66..1c54761dce1a6 100644 --- a/x-pack/plugins/alerting/server/index.ts +++ b/x-pack/plugins/alerting/server/index.ts @@ -43,13 +43,7 @@ export { AlertingAuthorizationEntity, } from './authorization'; -export { - extractEntityAndBoundaryReferences, - injectEntityAndBoundaryIds, - GeoContainmentParams, - GeoContainmentExtractedParams, - GEO_CONTAINMENT_ID, -} from './saved_objects/geo_containment/migrations'; +export { extractEntityAndBoundaryReferences } from './saved_objects/geo_containment/migrations'; export const plugin = (initContext: PluginInitializerContext) => new AlertingPlugin(initContext); diff --git a/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.test.ts b/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.test.ts index aef19a3e1f402..8012ec98baf37 100644 --- a/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.test.ts +++ b/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.test.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { extractEntityAndBoundaryReferences, injectEntityAndBoundaryIds } from './migrations'; +import { extractEntityAndBoundaryReferences } from './migrations'; describe('geo_containment migration utilities', () => { test('extractEntityAndBoundaryReferences', () => { @@ -47,44 +47,4 @@ describe('geo_containment migration utilities', () => { ], }); }); - - test('injectEntityAndBoundaryIds', () => { - expect( - injectEntityAndBoundaryIds( - { - boundaryGeoField: 'geometry', - boundaryIndexRefName: 'boundary_index_boundaryid', - boundaryIndexTitle: 'boundary*', - boundaryType: 'entireIndex', - dateField: '@timestamp', - entity: 'vehicle_id', - geoField: 'geometry', - index: 'foo*', - indexRefName: 'tracked_index_foobar', - }, - [ - { - id: 'foobar', - name: 'tracked_index_foobar', - type: 'index-pattern', - }, - { - id: 'boundaryid', - name: 'boundary_index_boundaryid', - type: 'index-pattern', - }, - ] - ) - ).toEqual({ - index: 'foo*', - indexId: 'foobar', - geoField: 'geometry', - entity: 'vehicle_id', - dateField: '@timestamp', - boundaryType: 'entireIndex', - boundaryIndexTitle: 'boundary*', - boundaryIndexId: 'boundaryid', - boundaryGeoField: 'geometry', - }); - }); }); diff --git a/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.ts b/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.ts index a783ee13b48ff..9107552cf7eee 100644 --- a/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.ts +++ b/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.ts @@ -14,9 +14,10 @@ import { AlertTypeParams } from '../../index'; import { Query } from '../../../../../../src/plugins/data/common/query'; import { RawAlert } from '../../types'; -export const GEO_CONTAINMENT_ID = '.geo-containment'; - -export interface GeoContainmentParams extends AlertTypeParams { +// These definitions are dupes of the SO-types in stack_alerts/geo_containment +// There are not exported to avoid deep imports from stack_alerts plugins into here +const GEO_CONTAINMENT_ID = '.geo-containment'; +interface GeoContainmentParams extends AlertTypeParams { index: string; indexId: string; geoField: string; @@ -30,11 +31,7 @@ export interface GeoContainmentParams extends AlertTypeParams { indexQuery?: Query; boundaryIndexQuery?: Query; } - -export type GeoContainmentExtractedParams = Omit< - GeoContainmentParams, - 'indexId' | 'boundaryIndexId' -> & { +type GeoContainmentExtractedParams = Omit & { indexRefName: string; boundaryIndexRefName: string; }; @@ -66,27 +63,6 @@ export function extractEntityAndBoundaryReferences(params: GeoContainmentParams) }; } -export function injectEntityAndBoundaryIds( - params: GeoContainmentExtractedParams, - references: SavedObjectReference[] -): GeoContainmentParams { - const { indexRefName, boundaryIndexRefName, ...otherParams } = params; - const { id: indexId = null } = references.find((ref) => ref.name === indexRefName) || {}; - const { id: boundaryIndexId = null } = - references.find((ref) => ref.name === boundaryIndexRefName) || {}; - if (!indexId) { - throw new Error(`Index "${indexId}" not found in references array`); - } - if (!boundaryIndexId) { - throw new Error(`Boundary index "${boundaryIndexId}" not found in references array`); - } - return { - ...otherParams, - indexId, - boundaryIndexId, - } as GeoContainmentParams; -} - export function extractRefsFromGeoContainmentAlert( doc: SavedObjectUnsanitizedDoc ): SavedObjectUnsanitizedDoc { diff --git a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/alert_type.ts b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/alert_type.ts index 2104b43926ecd..fd87890176fe2 100644 --- a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/alert_type.ts +++ b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/alert_type.ts @@ -17,15 +17,36 @@ import { AlertInstanceContext, RuleParamsAndRefs, extractEntityAndBoundaryReferences, - injectEntityAndBoundaryIds, - GeoContainmentExtractedParams, - GeoContainmentParams, - GEO_CONTAINMENT_ID, + AlertTypeParams, } from '../../../../alerting/server'; +import { Query } from '../../../../../../src/plugins/data/common/query'; export const ActionGroupId = 'Tracked entity contained'; export const RecoveryActionGroupId = 'notGeoContained'; +export const GEO_CONTAINMENT_ID = '.geo-containment'; +export interface GeoContainmentParams extends AlertTypeParams { + index: string; + indexId: string; + geoField: string; + entity: string; + dateField: string; + boundaryType: string; + boundaryIndexTitle: string; + boundaryIndexId: string; + boundaryGeoField: string; + boundaryNameField?: string; + indexQuery?: Query; + boundaryIndexQuery?: Query; +} +export type GeoContainmentExtractedParams = Omit< + GeoContainmentParams, + 'indexId' | 'boundaryIndexId' +> & { + indexRefName: string; + boundaryIndexRefName: string; +}; + const actionVariableContextEntityIdLabel = i18n.translate( 'xpack.stackAlerts.geoContainment.actionVariableContextEntityIdLabel', { @@ -137,6 +158,27 @@ export type GeoContainmentAlertType = AlertType< typeof RecoveryActionGroupId >; +export function injectEntityAndBoundaryIds( + params: GeoContainmentExtractedParams, + references: SavedObjectReference[] +): GeoContainmentParams { + const { indexRefName, boundaryIndexRefName, ...otherParams } = params; + const { id: indexId = null } = references.find((ref) => ref.name === indexRefName) || {}; + const { id: boundaryIndexId = null } = + references.find((ref) => ref.name === boundaryIndexRefName) || {}; + if (!indexId) { + throw new Error(`Index "${indexId}" not found in references array`); + } + if (!boundaryIndexId) { + throw new Error(`Boundary index "${boundaryIndexId}" not found in references array`); + } + return { + ...otherParams, + indexId, + boundaryIndexId, + } as GeoContainmentParams; +} + export function getAlertType(logger: Logger): GeoContainmentAlertType { const alertTypeName = i18n.translate('xpack.stackAlerts.geoContainment.alertTypeTitle', { defaultMessage: 'Tracking containment', diff --git a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/index.ts b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/index.ts index 08569f7f24e5f..195ffb97bd81f 100644 --- a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/index.ts +++ b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/index.ts @@ -16,7 +16,7 @@ import { RecoveryActionGroupId, } from './alert_type'; -import { GeoContainmentExtractedParams, GeoContainmentParams } from '../../../../alerting/server'; +import { GeoContainmentExtractedParams, GeoContainmentParams } from './alert_type'; interface RegisterParams { logger: Logger; diff --git a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/tests/alert_type.test.ts b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/tests/alert_type.test.ts index ba53c3210da5b..c86b74c751e48 100644 --- a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/tests/alert_type.test.ts +++ b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/tests/alert_type.test.ts @@ -6,8 +6,7 @@ */ import { loggingSystemMock } from '../../../../../../../src/core/server/mocks'; -import { getAlertType } from '../alert_type'; -import type { GeoContainmentParams } from '../../../../../alerting/server'; +import { getAlertType, injectEntityAndBoundaryIds, GeoContainmentParams } from '../alert_type'; describe('alertType', () => { const logger = loggingSystemMock.create().get(); @@ -44,4 +43,44 @@ describe('alertType', () => { expect(alertType.validate?.params?.validate(params)).toBeTruthy(); }); + + test('injectEntityAndBoundaryIds', () => { + expect( + injectEntityAndBoundaryIds( + { + boundaryGeoField: 'geometry', + boundaryIndexRefName: 'boundary_index_boundaryid', + boundaryIndexTitle: 'boundary*', + boundaryType: 'entireIndex', + dateField: '@timestamp', + entity: 'vehicle_id', + geoField: 'geometry', + index: 'foo*', + indexRefName: 'tracked_index_foobar', + }, + [ + { + id: 'foobar', + name: 'tracked_index_foobar', + type: 'index-pattern', + }, + { + id: 'boundaryid', + name: 'boundary_index_boundaryid', + type: 'index-pattern', + }, + ] + ) + ).toEqual({ + index: 'foo*', + indexId: 'foobar', + geoField: 'geometry', + entity: 'vehicle_id', + dateField: '@timestamp', + boundaryType: 'entireIndex', + boundaryIndexTitle: 'boundary*', + boundaryIndexId: 'boundaryid', + boundaryGeoField: 'geometry', + }); + }); }); diff --git a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/tests/geo_containment.test.ts b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/tests/geo_containment.test.ts index bbf610babe20b..8b78441d174b2 100644 --- a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/tests/geo_containment.test.ts +++ b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/tests/geo_containment.test.ts @@ -18,7 +18,7 @@ import { } from '../geo_containment'; import { OTHER_CATEGORY } from '../es_query_builder'; import { GeoContainmentInstanceContext, GeoContainmentInstanceState } from '../alert_type'; -import type { GeoContainmentParams } from '../../../../../alerting/server'; +import type { GeoContainmentParams } from '../alert_type'; const alertInstanceFactory = (contextKeys: unknown[], testAlertActionArr: unknown[]) => (instanceId: string) => { From 814e5f1dca952f5ac82c668f8b1b14101a87f060 Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Wed, 13 Oct 2021 13:10:45 -0400 Subject: [PATCH 13/18] type fixes --- .../server/alert_types/geo_containment/geo_containment.ts | 2 +- x-pack/plugins/stack_alerts/server/feature.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/geo_containment.ts b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/geo_containment.ts index 3598daca7f72c..f227ae4fc23cc 100644 --- a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/geo_containment.ts +++ b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/geo_containment.ts @@ -18,7 +18,7 @@ import { GeoContainmentState, } from './alert_type'; -import { GEO_CONTAINMENT_ID } from '../../../../alerting/server'; +import { GEO_CONTAINMENT_ID } from './alert_type'; export type LatestEntityLocation = GeoContainmentInstanceState; diff --git a/x-pack/plugins/stack_alerts/server/feature.ts b/x-pack/plugins/stack_alerts/server/feature.ts index 37e00286ea90a..39ea41374df7b 100644 --- a/x-pack/plugins/stack_alerts/server/feature.ts +++ b/x-pack/plugins/stack_alerts/server/feature.ts @@ -8,7 +8,7 @@ import { i18n } from '@kbn/i18n'; import { KibanaFeatureConfig } from '../../../plugins/features/common'; import { ID as IndexThreshold } from './alert_types/index_threshold/alert_type'; -import { GEO_CONTAINMENT_ID as GeoContainment } from '../../alerting/server'; +import { GEO_CONTAINMENT_ID as GeoContainment } from './alert_types/geo_containment/alert_type'; import { ES_QUERY_ID as ElasticsearchQuery } from './alert_types/es_query/alert_type'; import { STACK_ALERTS_FEATURE_ID } from '../common'; import { DEFAULT_APP_CATEGORIES } from '../../../../src/core/server'; From d04774577ed7104de3e398a3a5ebc4bef01dc3f8 Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Wed, 13 Oct 2021 15:42:58 -0400 Subject: [PATCH 14/18] fix refs --- .../server/saved_objects/geo_containment/migrations.test.ts | 4 ++-- .../plugins/alerting/server/saved_objects/migrations.test.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.test.ts b/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.test.ts index 8012ec98baf37..fe12da4f7d697 100644 --- a/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.test.ts +++ b/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.test.ts @@ -24,14 +24,14 @@ describe('geo_containment migration utilities', () => { ).toEqual({ params: { boundaryGeoField: 'geometry', - boundaryIndexRef: 'boundary_index_boundaryid', + boundaryIndexRefName: 'boundary_index_boundaryid', boundaryIndexTitle: 'boundary*', boundaryType: 'entireIndex', dateField: '@timestamp', entity: 'vehicle_id', geoField: 'geometry', index: 'foo*', - indexRef: 'tracked_index_foobar', + indexRefName: 'tracked_index_foobar', }, references: [ { diff --git a/x-pack/plugins/alerting/server/saved_objects/migrations.test.ts b/x-pack/plugins/alerting/server/saved_objects/migrations.test.ts index 3d4938c29323d..d8c8e3fd3a443 100644 --- a/x-pack/plugins/alerting/server/saved_objects/migrations.test.ts +++ b/x-pack/plugins/alerting/server/saved_objects/migrations.test.ts @@ -1934,8 +1934,8 @@ describe('successful migrations', () => { ]); expect(migratedAlert.attributes.params).toEqual({ - boundaryIndexRef: 'boundary_index_bar', - indexRef: 'tracked_index_foo', + boundaryIndexRefName: 'boundary_index_bar', + indexRefName: 'tracked_index_foo', }); expect(migratedAlert.attributes.params.indexId).toEqual(undefined); From 85ac258190cbad87452a891a53637c113a3e2c8e Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Wed, 13 Oct 2021 17:27:42 -0400 Subject: [PATCH 15/18] improve test coverage --- .../geo_containment/migrations.ts | 2 +- .../server/saved_objects/migrations.test.ts | 64 ++++++++++++++++++- .../geo_containment/tests/alert_type.test.ts | 10 +++ 3 files changed, 74 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.ts b/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.ts index 9107552cf7eee..1c4437905e022 100644 --- a/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.ts +++ b/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.ts @@ -83,6 +83,6 @@ export function extractRefsFromGeoContainmentAlert( ...doc.attributes, params: newParams as SavedObjectAttributes, }, - references, + references: [...(doc.references || []), ...references], }; } diff --git a/x-pack/plugins/alerting/server/saved_objects/migrations.test.ts b/x-pack/plugins/alerting/server/saved_objects/migrations.test.ts index d8c8e3fd3a443..2f4aff9f7d4bb 100644 --- a/x-pack/plugins/alerting/server/saved_objects/migrations.test.ts +++ b/x-pack/plugins/alerting/server/saved_objects/migrations.test.ts @@ -1914,7 +1914,7 @@ describe('successful migrations', () => { }); }); - test('extracts references from geo-containment alerts', () => { + test('geo-containment alert migration extracts boundary and index references', () => { const migration7160 = getMigrations(encryptedSavedObjectsSetup, isPreconfigured)['7.16.0']; const alert = { ...getMockData({ @@ -1941,6 +1941,68 @@ describe('successful migrations', () => { expect(migratedAlert.attributes.params.indexId).toEqual(undefined); expect(migratedAlert.attributes.params.boundaryIndexId).toEqual(undefined); }); + + test('geo-containment alert migration should preserve foreign references', () => { + const migration7160 = getMigrations(encryptedSavedObjectsSetup, isPreconfigured)['7.16.0']; + const alert = { + ...getMockData({ + alertTypeId: '.geo-containment', + params: { + indexId: 'foo', + boundaryIndexId: 'bar', + }, + }), + references: [ + { + name: 'foreign-name', + id: '999', + type: 'foreign-name', + }, + ], + }; + + const migratedAlert = migration7160(alert, migrationContext); + + expect(migratedAlert.references).toEqual([ + { + name: 'foreign-name', + id: '999', + type: 'foreign-name', + }, + { id: 'foo', name: 'tracked_index_foo', type: 'index-pattern' }, + { id: 'bar', name: 'boundary_index_bar', type: 'index-pattern' }, + ]); + + expect(migratedAlert.attributes.params).toEqual({ + boundaryIndexRefName: 'boundary_index_bar', + indexRefName: 'tracked_index_foo', + }); + + expect(migratedAlert.attributes.params.indexId).toEqual(undefined); + expect(migratedAlert.attributes.params.boundaryIndexId).toEqual(undefined); + }); + + test('geo-containment alert migration ignores other alert-types', () => { + const migration7160 = getMigrations(encryptedSavedObjectsSetup, isPreconfigured)['7.16.0']; + const alert = { + ...getMockData({ + alertTypeId: '.foo', + references: [ + { + name: 'foreign-name', + id: '999', + type: 'foreign-name', + }, + ], + }), + }; + + const migratedAlert = migration7160(alert, migrationContext); + + expect(typeof migratedAlert.attributes.legacyId).toEqual('string'); // introduced by setLegacyId migration + delete migratedAlert.attributes.legacyId; + expect(migratedAlert).toEqual(alert); + }); }); describe('8.0.0', () => { diff --git a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/tests/alert_type.test.ts b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/tests/alert_type.test.ts index c86b74c751e48..2f5e5d778b838 100644 --- a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/tests/alert_type.test.ts +++ b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/tests/alert_type.test.ts @@ -59,11 +59,21 @@ describe('alertType', () => { indexRefName: 'tracked_index_foobar', }, [ + { + id: 'foreign', + name: 'foobar', + type: 'foreign', + }, { id: 'foobar', name: 'tracked_index_foobar', type: 'index-pattern', }, + { + id: 'foreignToo', + name: 'boundary_index_shouldbeignored', + type: 'index-pattern', + }, { id: 'boundaryid', name: 'boundary_index_boundaryid', From 9daaa1f6c6e8be3030b5293dbcb5c09558a940bf Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Thu, 14 Oct 2021 16:24:46 -0400 Subject: [PATCH 16/18] use param prefix --- x-pack/plugins/alerting/server/index.ts | 2 -- .../geo_containment/migrations.test.ts | 4 +-- .../geo_containment/migrations.ts | 15 ++++++---- .../server/saved_objects/migrations.test.ts | 8 ++--- .../alert_types/geo_containment/alert_type.ts | 29 +++++++++++++++++++ 5 files changed, 45 insertions(+), 13 deletions(-) diff --git a/x-pack/plugins/alerting/server/index.ts b/x-pack/plugins/alerting/server/index.ts index 1c54761dce1a6..162ee06216304 100644 --- a/x-pack/plugins/alerting/server/index.ts +++ b/x-pack/plugins/alerting/server/index.ts @@ -43,8 +43,6 @@ export { AlertingAuthorizationEntity, } from './authorization'; -export { extractEntityAndBoundaryReferences } from './saved_objects/geo_containment/migrations'; - export const plugin = (initContext: PluginInitializerContext) => new AlertingPlugin(initContext); export const config: PluginConfigDescriptor = { diff --git a/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.test.ts b/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.test.ts index fe12da4f7d697..779e201635495 100644 --- a/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.test.ts +++ b/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.test.ts @@ -36,12 +36,12 @@ describe('geo_containment migration utilities', () => { references: [ { id: 'foobar', - name: 'tracked_index_foobar', + name: 'param:tracked_index_foobar', type: 'index-pattern', }, { id: 'boundaryid', - name: 'boundary_index_boundaryid', + name: 'param:boundary_index_boundaryid', type: 'index-pattern', }, ], diff --git a/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.ts b/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.ts index 1c4437905e022..113b4cf796d2f 100644 --- a/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.ts +++ b/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.ts @@ -41,14 +41,19 @@ export function extractEntityAndBoundaryReferences(params: GeoContainmentParams) references: SavedObjectReference[]; } { const { indexId, boundaryIndexId, ...otherParams } = params; + + const indexRefNamePrefix = 'tracked_index_'; + const boundaryRefNamePrefix = 'boundary_index_'; + + // Since these are stack-alerts, we need to prefix with the `param:`-namespace const references = [ { - name: `tracked_index_${indexId}`, - type: 'index-pattern', + name: `param:${indexRefNamePrefix}${indexId}`, + type: `index-pattern`, id: indexId as string, }, { - name: `boundary_index_${boundaryIndexId}`, + name: `param:${boundaryRefNamePrefix}${boundaryIndexId}`, type: 'index-pattern', id: boundaryIndexId as string, }, @@ -56,8 +61,8 @@ export function extractEntityAndBoundaryReferences(params: GeoContainmentParams) return { params: { ...otherParams, - indexRefName: `tracked_index_${indexId}`, - boundaryIndexRefName: `boundary_index_${boundaryIndexId}`, + indexRefName: `${indexRefNamePrefix}${indexId}`, + boundaryIndexRefName: `${boundaryRefNamePrefix}${boundaryIndexId}`, }, references, }; diff --git a/x-pack/plugins/alerting/server/saved_objects/migrations.test.ts b/x-pack/plugins/alerting/server/saved_objects/migrations.test.ts index 2f4aff9f7d4bb..3822334579137 100644 --- a/x-pack/plugins/alerting/server/saved_objects/migrations.test.ts +++ b/x-pack/plugins/alerting/server/saved_objects/migrations.test.ts @@ -1929,8 +1929,8 @@ describe('successful migrations', () => { const migratedAlert = migration7160(alert, migrationContext); expect(migratedAlert.references).toEqual([ - { id: 'foo', name: 'tracked_index_foo', type: 'index-pattern' }, - { id: 'bar', name: 'boundary_index_bar', type: 'index-pattern' }, + { id: 'foo', name: 'param:tracked_index_foo', type: 'index-pattern' }, + { id: 'bar', name: 'param:boundary_index_bar', type: 'index-pattern' }, ]); expect(migratedAlert.attributes.params).toEqual({ @@ -1969,8 +1969,8 @@ describe('successful migrations', () => { id: '999', type: 'foreign-name', }, - { id: 'foo', name: 'tracked_index_foo', type: 'index-pattern' }, - { id: 'bar', name: 'boundary_index_bar', type: 'index-pattern' }, + { id: 'foo', name: 'param:tracked_index_foo', type: 'index-pattern' }, + { id: 'bar', name: 'param:boundary_index_bar', type: 'index-pattern' }, ]); expect(migratedAlert.attributes.params).toEqual({ diff --git a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/alert_type.ts b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/alert_type.ts index fd87890176fe2..eb8da0699ceba 100644 --- a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/alert_type.ts +++ b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/alert_type.ts @@ -158,6 +158,35 @@ export type GeoContainmentAlertType = AlertType< typeof RecoveryActionGroupId >; +export function extractEntityAndBoundaryReferences(params: GeoContainmentParams): { + params: GeoContainmentExtractedParams; + references: SavedObjectReference[]; +} { + const { indexId, boundaryIndexId, ...otherParams } = params; + + // Reference names omit the `param:`-prefix. This is handled by the alerting framework already + const references = [ + { + name: `tracked_index_${indexId}`, + type: 'index-pattern', + id: indexId as string, + }, + { + name: `boundary_index_${boundaryIndexId}`, + type: 'index-pattern', + id: boundaryIndexId as string, + }, + ]; + return { + params: { + ...otherParams, + indexRefName: `tracked_index_${indexId}`, + boundaryIndexRefName: `boundary_index_${boundaryIndexId}`, + }, + references, + }; +} + export function injectEntityAndBoundaryIds( params: GeoContainmentExtractedParams, references: SavedObjectReference[] From 54338744a48b9eb4a6db52364f96133f5ab2f6eb Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Thu, 14 Oct 2021 16:35:30 -0400 Subject: [PATCH 17/18] remove broken import --- .../server/alert_types/geo_containment/alert_type.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/alert_type.ts b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/alert_type.ts index eb8da0699ceba..2a98a4670f2b5 100644 --- a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/alert_type.ts +++ b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/alert_type.ts @@ -16,7 +16,6 @@ import { AlertInstanceState, AlertInstanceContext, RuleParamsAndRefs, - extractEntityAndBoundaryReferences, AlertTypeParams, } from '../../../../alerting/server'; import { Query } from '../../../../../../src/plugins/data/common/query'; From d42a15b6d860fa8553eca5e792a54f6673ad5207 Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Fri, 15 Oct 2021 11:15:42 -0400 Subject: [PATCH 18/18] add unit test --- .../geo_containment/tests/alert_type.test.ts | 47 ++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/tests/alert_type.test.ts b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/tests/alert_type.test.ts index 2f5e5d778b838..9fc382240d0be 100644 --- a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/tests/alert_type.test.ts +++ b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/tests/alert_type.test.ts @@ -6,7 +6,12 @@ */ import { loggingSystemMock } from '../../../../../../../src/core/server/mocks'; -import { getAlertType, injectEntityAndBoundaryIds, GeoContainmentParams } from '../alert_type'; +import { + getAlertType, + injectEntityAndBoundaryIds, + GeoContainmentParams, + extractEntityAndBoundaryReferences, +} from '../alert_type'; describe('alertType', () => { const logger = loggingSystemMock.create().get(); @@ -93,4 +98,44 @@ describe('alertType', () => { boundaryGeoField: 'geometry', }); }); + + test('extractEntityAndBoundaryReferences', () => { + expect( + extractEntityAndBoundaryReferences({ + index: 'foo*', + indexId: 'foobar', + geoField: 'geometry', + entity: 'vehicle_id', + dateField: '@timestamp', + boundaryType: 'entireIndex', + boundaryIndexTitle: 'boundary*', + boundaryIndexId: 'boundaryid', + boundaryGeoField: 'geometry', + }) + ).toEqual({ + params: { + boundaryGeoField: 'geometry', + boundaryIndexRefName: 'boundary_index_boundaryid', + boundaryIndexTitle: 'boundary*', + boundaryType: 'entireIndex', + dateField: '@timestamp', + entity: 'vehicle_id', + geoField: 'geometry', + index: 'foo*', + indexRefName: 'tracked_index_foobar', + }, + references: [ + { + id: 'foobar', + name: 'tracked_index_foobar', + type: 'index-pattern', + }, + { + id: 'boundaryid', + name: 'boundary_index_boundaryid', + type: 'index-pattern', + }, + ], + }); + }); });