Skip to content

Commit f45ddad

Browse files
committed
[Event Log] add rel=primary to saved objects for query targets (#64615)
resolves #62668 Adds a property named `rel` to the nested saved objects in the event documents, whose value should not be set, or set to `primary`. The query by saved object function changes to only match event documents with that saved objects if it has the `rel: primary` value. This is used to limit searching alerting's executeAction event document with only the alert saved object, and not the action saved object (this document has an alert and action saved object). The alert saved object has the `rel: primary` field set, and the action does not. Previously, those documents were returned with a query of the action saved object.
1 parent eb05f6e commit f45ddad

File tree

17 files changed

+135
-12
lines changed

17 files changed

+135
-12
lines changed

x-pack/plugins/actions/server/lib/action_executor.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
import { EncryptedSavedObjectsPluginStart } from '../../../encrypted_saved_objects/server';
1818
import { SpacesServiceSetup } from '../../../spaces/server';
1919
import { EVENT_LOG_ACTIONS } from '../plugin';
20-
import { IEvent, IEventLogger } from '../../../event_log/server';
20+
import { IEvent, IEventLogger, SAVED_OBJECT_REL_PRIMARY } from '../../../event_log/server';
2121

2222
export interface ActionExecutorContext {
2323
logger: Logger;
@@ -110,7 +110,16 @@ export class ActionExecutor {
110110
const actionLabel = `${actionTypeId}:${actionId}: ${name}`;
111111
const event: IEvent = {
112112
event: { action: EVENT_LOG_ACTIONS.execute },
113-
kibana: { saved_objects: [{ type: 'action', id: actionId, ...namespace }] },
113+
kibana: {
114+
saved_objects: [
115+
{
116+
rel: SAVED_OBJECT_REL_PRIMARY,
117+
type: 'action',
118+
id: actionId,
119+
...namespace,
120+
},
121+
],
122+
},
114123
};
115124

116125
eventLogger.startTiming(event);

x-pack/plugins/alerting/server/task_runner/create_execution_handler.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ test('calls actionsPlugin.execute per selected action', async () => {
9595
"saved_objects": Array [
9696
Object {
9797
"id": "1",
98+
"rel": "primary",
9899
"type": "alert",
99100
},
100101
Object {

x-pack/plugins/alerting/server/task_runner/create_execution_handler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { AlertAction, State, Context, AlertType } from '../types';
99
import { Logger } from '../../../../../src/core/server';
1010
import { transformActionParams } from './transform_action_params';
1111
import { PluginStartContract as ActionsPluginStartContract } from '../../../../plugins/actions/server';
12-
import { IEventLogger, IEvent } from '../../../event_log/server';
12+
import { IEventLogger, IEvent, SAVED_OBJECT_REL_PRIMARY } from '../../../event_log/server';
1313
import { EVENT_LOG_ACTIONS } from '../plugin';
1414

1515
interface CreateExecutionHandlerOptions {
@@ -96,7 +96,7 @@ export function createExecutionHandler({
9696
instance_id: alertInstanceId,
9797
},
9898
saved_objects: [
99-
{ type: 'alert', id: alertId, ...namespace },
99+
{ rel: SAVED_OBJECT_REL_PRIMARY, type: 'alert', id: alertId, ...namespace },
100100
{ type: 'action', id: action.id, ...namespace },
101101
],
102102
},

x-pack/plugins/alerting/server/task_runner/task_runner.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ describe('Task Runner', () => {
172172
Object {
173173
"id": "1",
174174
"namespace": undefined,
175+
"rel": "primary",
175176
"type": "alert",
176177
},
177178
],
@@ -234,6 +235,7 @@ describe('Task Runner', () => {
234235
Object {
235236
"id": "1",
236237
"namespace": undefined,
238+
"rel": "primary",
237239
"type": "alert",
238240
},
239241
],
@@ -254,6 +256,7 @@ describe('Task Runner', () => {
254256
Object {
255257
"id": "1",
256258
"namespace": undefined,
259+
"rel": "primary",
257260
"type": "alert",
258261
},
259262
],
@@ -274,6 +277,7 @@ describe('Task Runner', () => {
274277
Object {
275278
"id": "1",
276279
"namespace": undefined,
280+
"rel": "primary",
277281
"type": "alert",
278282
},
279283
Object {
@@ -351,6 +355,7 @@ describe('Task Runner', () => {
351355
Object {
352356
"id": "1",
353357
"namespace": undefined,
358+
"rel": "primary",
354359
"type": "alert",
355360
},
356361
],
@@ -371,6 +376,7 @@ describe('Task Runner', () => {
371376
Object {
372377
"id": "1",
373378
"namespace": undefined,
379+
"rel": "primary",
374380
"type": "alert",
375381
},
376382
],
@@ -568,6 +574,7 @@ describe('Task Runner', () => {
568574
Object {
569575
"id": "1",
570576
"namespace": undefined,
577+
"rel": "primary",
571578
"type": "alert",
572579
},
573580
],

x-pack/plugins/alerting/server/task_runner/task_runner.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { promiseResult, map, Resultable, asOk, asErr, resolveErr } from '../lib/
2525
import { taskInstanceToAlertTaskInstance } from './alert_task_instance';
2626
import { AlertInstances } from '../alert_instance/alert_instance';
2727
import { EVENT_LOG_ACTIONS } from '../plugin';
28-
import { IEvent, IEventLogger } from '../../../event_log/server';
28+
import { IEvent, IEventLogger, SAVED_OBJECT_REL_PRIMARY } from '../../../event_log/server';
2929
import { isAlertSavedObjectNotFoundError } from '../lib/is_alert_not_found_error';
3030

3131
const FALLBACK_RETRY_INTERVAL: IntervalSchedule = { interval: '5m' };
@@ -174,7 +174,16 @@ export class TaskRunner {
174174
const alertLabel = `${this.alertType.id}:${alertId}: '${name}'`;
175175
const event: IEvent = {
176176
event: { action: EVENT_LOG_ACTIONS.execute },
177-
kibana: { saved_objects: [{ type: 'alert', id: alertId, namespace }] },
177+
kibana: {
178+
saved_objects: [
179+
{
180+
rel: SAVED_OBJECT_REL_PRIMARY,
181+
type: 'alert',
182+
id: alertId,
183+
namespace,
184+
},
185+
],
186+
},
178187
};
179188
eventLogger.startTiming(event);
180189

@@ -393,7 +402,14 @@ function generateNewAndResolvedInstanceEvents(params: GenerateNewAndResolvedInst
393402
alerting: {
394403
instance_id: id,
395404
},
396-
saved_objects: [{ type: 'alert', id: params.alertId, namespace: params.namespace }],
405+
saved_objects: [
406+
{
407+
rel: SAVED_OBJECT_REL_PRIMARY,
408+
type: 'alert',
409+
id: params.alertId,
410+
namespace: params.namespace,
411+
},
412+
],
397413
},
398414
message,
399415
};

x-pack/plugins/event_log/generated/mappings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@
8686
},
8787
"saved_objects": {
8888
"properties": {
89+
"rel": {
90+
"type": "keyword",
91+
"ignore_above": 1024
92+
},
8993
"namespace": {
9094
"type": "keyword",
9195
"ignore_above": 1024

x-pack/plugins/event_log/generated/schemas.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export const EventSchema = schema.maybe(
6565
saved_objects: schema.maybe(
6666
schema.arrayOf(
6767
schema.object({
68+
rel: ecsString(),
6869
namespace: ecsString(),
6970
id: ecsString(),
7071
type: ecsString(),

x-pack/plugins/event_log/scripts/mappings.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ exports.EcsKibanaExtensionsMappings = {
2424
saved_objects: {
2525
type: 'nested',
2626
properties: {
27+
// relation; currently only supports "primary" or not set
28+
rel: {
29+
type: 'keyword',
30+
ignore_above: 1024,
31+
},
2732
// relevant kibana space
2833
namespace: {
2934
type: 'keyword',
@@ -58,6 +63,7 @@ exports.EcsEventLogProperties = [
5863
'user.name',
5964
'kibana.server_uuid',
6065
'kibana.alerting.instance_id',
66+
'kibana.saved_objects.rel',
6167
'kibana.saved_objects.namespace',
6268
'kibana.saved_objects.id',
6369
'kibana.saved_objects.name',

x-pack/plugins/event_log/server/es/cluster_client_adapter.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,13 @@ describe('queryEventsBySavedObject', () => {
236236
query: {
237237
bool: {
238238
must: [
239+
{
240+
term: {
241+
'kibana.saved_objects.rel': {
242+
value: 'primary',
243+
},
244+
},
245+
},
239246
{
240247
term: {
241248
'kibana.saved_objects.type': {
@@ -319,6 +326,13 @@ describe('queryEventsBySavedObject', () => {
319326
query: {
320327
bool: {
321328
must: [
329+
{
330+
term: {
331+
'kibana.saved_objects.rel': {
332+
value: 'primary',
333+
},
334+
},
335+
},
322336
{
323337
term: {
324338
'kibana.saved_objects.type': {
@@ -388,6 +402,13 @@ describe('queryEventsBySavedObject', () => {
388402
query: {
389403
bool: {
390404
must: [
405+
{
406+
term: {
407+
'kibana.saved_objects.rel': {
408+
value: 'primary',
409+
},
410+
},
411+
},
391412
{
392413
term: {
393414
'kibana.saved_objects.type': {

x-pack/plugins/event_log/server/es/cluster_client_adapter.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import { reject, isUndefined } from 'lodash';
88
import { SearchResponse, Client } from 'elasticsearch';
99
import { Logger, ClusterClient } from '../../../../../src/core/server';
10-
import { IEvent } from '../types';
10+
import { IEvent, SAVED_OBJECT_REL_PRIMARY } from '../types';
1111
import { FindOptionsType } from '../event_log_client';
1212

1313
export type EsClusterClient = Pick<ClusterClient, 'callAsInternalUser' | 'asScoped'>;
@@ -155,6 +155,13 @@ export class ClusterClientAdapter {
155155
query: {
156156
bool: {
157157
must: [
158+
{
159+
term: {
160+
'kibana.saved_objects.rel': {
161+
value: SAVED_OBJECT_REL_PRIMARY,
162+
},
163+
},
164+
},
158165
{
159166
term: {
160167
'kibana.saved_objects.type': {

0 commit comments

Comments
 (0)