-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[actions] add rule saved object reference to action execution event l…
…og doc (#101526) resolves #99225 Prior to this PR, when an alerting connection action was executed, the event log document generated did not contain a reference to the originating rule. This makes it difficult to diagnose problems with connector errors, since the error is often in the parameters specified in the actions in the alert. In this PR, a reference to the alerting rule is added to the saved_objects field in the event document for these events.
- Loading branch information
Showing
18 changed files
with
442 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
x-pack/plugins/actions/server/lib/related_saved_objects.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* 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 { validatedRelatedSavedObjects } from './related_saved_objects'; | ||
import { loggingSystemMock } from '../../../../../src/core/server/mocks'; | ||
import { Logger } from '../../../../../src/core/server'; | ||
|
||
const loggerMock = loggingSystemMock.createLogger(); | ||
|
||
describe('related_saved_objects', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('validates valid objects', () => { | ||
ensureValid(loggerMock, undefined); | ||
ensureValid(loggerMock, []); | ||
ensureValid(loggerMock, [ | ||
{ | ||
id: 'some-id', | ||
type: 'some-type', | ||
}, | ||
]); | ||
ensureValid(loggerMock, [ | ||
{ | ||
id: 'some-id', | ||
type: 'some-type', | ||
typeId: 'some-type-id', | ||
}, | ||
]); | ||
ensureValid(loggerMock, [ | ||
{ | ||
id: 'some-id', | ||
type: 'some-type', | ||
namespace: 'some-namespace', | ||
}, | ||
]); | ||
ensureValid(loggerMock, [ | ||
{ | ||
id: 'some-id', | ||
type: 'some-type', | ||
typeId: 'some-type-id', | ||
namespace: 'some-namespace', | ||
}, | ||
]); | ||
ensureValid(loggerMock, [ | ||
{ | ||
id: 'some-id', | ||
type: 'some-type', | ||
}, | ||
{ | ||
id: 'some-id-2', | ||
type: 'some-type-2', | ||
}, | ||
]); | ||
}); | ||
}); | ||
|
||
it('handles invalid objects', () => { | ||
ensureInvalid(loggerMock, 42); | ||
ensureInvalid(loggerMock, {}); | ||
ensureInvalid(loggerMock, [{}]); | ||
ensureInvalid(loggerMock, [{ id: 'some-id' }]); | ||
ensureInvalid(loggerMock, [{ id: 42 }]); | ||
ensureInvalid(loggerMock, [{ id: 'some-id', type: 'some-type', x: 42 }]); | ||
}); | ||
|
||
function ensureValid(logger: Logger, savedObjects: unknown) { | ||
const result = validatedRelatedSavedObjects(logger, savedObjects); | ||
expect(result).toEqual(savedObjects === undefined ? [] : savedObjects); | ||
expect(loggerMock.warn).not.toHaveBeenCalled(); | ||
} | ||
|
||
function ensureInvalid(logger: Logger, savedObjects: unknown) { | ||
const result = validatedRelatedSavedObjects(logger, savedObjects); | ||
expect(result).toEqual([]); | ||
|
||
const message = loggerMock.warn.mock.calls[0][0]; | ||
expect(message).toMatch( | ||
/ignoring invalid related saved objects: expected value of type \[array\] but got/ | ||
); | ||
} |
31 changes: 31 additions & 0 deletions
31
x-pack/plugins/actions/server/lib/related_saved_objects.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* 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, TypeOf } from '@kbn/config-schema'; | ||
import { Logger } from '../../../../../src/core/server'; | ||
|
||
export type RelatedSavedObjects = TypeOf<typeof RelatedSavedObjectsSchema>; | ||
|
||
const RelatedSavedObjectsSchema = schema.arrayOf( | ||
schema.object({ | ||
namespace: schema.maybe(schema.string({ minLength: 1 })), | ||
id: schema.string({ minLength: 1 }), | ||
type: schema.string({ minLength: 1 }), | ||
// optional; for SO types like action/alert that have type id's | ||
typeId: schema.maybe(schema.string({ minLength: 1 })), | ||
}), | ||
{ defaultValue: [] } | ||
); | ||
|
||
export function validatedRelatedSavedObjects(logger: Logger, data: unknown): RelatedSavedObjects { | ||
try { | ||
return RelatedSavedObjectsSchema.validate(data); | ||
} catch (err) { | ||
logger.warn(`ignoring invalid related saved objects: ${err.message}`); | ||
return []; | ||
} | ||
} |
Oops, something went wrong.