Skip to content

Commit

Permalink
[Security Solution] [Platform] optionally include legacy id when reso…
Browse files Browse the repository at this point in the history
…lving a rule (#118946)

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
2 people authored and dmlemeshko committed Nov 29, 2021
1 parent 80dfd6f commit 300f919
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
5 changes: 4 additions & 1 deletion x-pack/plugins/alerting/server/rules_client/rules_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,10 @@ export class RulesClient {

public async resolve<Params extends AlertTypeParams = never>({
id,
includeLegacyId,
}: {
id: string;
includeLegacyId?: boolean;
}): Promise<ResolvedSanitizedRule<Params>> {
const { saved_object: result, ...resolveResponse } =
await this.unsecuredSavedObjectsClient.resolve<RawAlert>('alert', id);
Expand Down Expand Up @@ -483,7 +485,8 @@ export class RulesClient {
result.id,
result.attributes.alertTypeId,
result.attributes,
result.references
result.references,
includeLegacyId
);

return {
Expand Down
74 changes: 74 additions & 0 deletions x-pack/plugins/alerting/server/rules_client/tests/resolve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,80 @@ describe('resolve()', () => {
`);
});

test('calls saved objects client with id and includeLegacyId params', async () => {
const rulesClient = new RulesClient(rulesClientParams);
unsecuredSavedObjectsClient.resolve.mockResolvedValueOnce({
saved_object: {
id: '1',
type: 'alert',
attributes: {
legacyId: 'some-legacy-id',
alertTypeId: '123',
schedule: { interval: '10s' },
params: {
bar: true,
},
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
actions: [
{
group: 'default',
actionRef: 'action_0',
params: {
foo: true,
},
},
],
notifyWhen: 'onActiveAlert',
},
references: [
{
name: 'action_0',
type: 'action',
id: '1',
},
],
},
outcome: 'aliasMatch',
alias_target_id: '2',
});
const result = await rulesClient.resolve({ id: '1', includeLegacyId: true });
expect(result).toMatchInlineSnapshot(`
Object {
"actions": Array [
Object {
"group": "default",
"id": "1",
"params": Object {
"foo": true,
},
},
],
"alertTypeId": "123",
"alias_target_id": "2",
"createdAt": 2019-02-12T21:01:22.479Z,
"id": "1",
"legacyId": "some-legacy-id",
"notifyWhen": "onActiveAlert",
"outcome": "aliasMatch",
"params": Object {
"bar": true,
},
"schedule": Object {
"interval": "10s",
},
"updatedAt": 2019-02-12T21:01:22.479Z,
}
`);
expect(unsecuredSavedObjectsClient.resolve).toHaveBeenCalledTimes(1);
expect(unsecuredSavedObjectsClient.resolve.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"alert",
"1",
]
`);
});

test('should call useSavedObjectReferences.injectReferences if defined for rule type', async () => {
const injectReferencesFn = jest.fn().mockReturnValue({
bar: true,
Expand Down

0 comments on commit 300f919

Please sign in to comment.