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

[Event log][7.x] Updated event log client to search across legacy IDs #109365

Merged
merged 32 commits into from
Sep 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
9d8f520
[Event log][7.x] Updated event log client to search across legacy IDs
YulNaumenko Aug 20, 2021
e7a6ec0
fixed tests
YulNaumenko Aug 22, 2021
0eb9ade
Merge branch 'master' into event-log-search-legacy-id
kibanamachine Aug 22, 2021
a55c323
extended kibana null version check
YulNaumenko Aug 24, 2021
1f2209a
added logic to alerting plugin
YulNaumenko Aug 24, 2021
541704f
fixed typechecks
YulNaumenko Aug 24, 2021
041243a
Merge remote-tracking branch upstream/master
YulNaumenko Aug 24, 2021
6f6770f
fixed typechecks
YulNaumenko Aug 24, 2021
15edeed
Revert "fixed typechecks"
YulNaumenko Aug 25, 2021
1f9639c
removed legacyId for routes
YulNaumenko Aug 25, 2021
8d87f2e
fixed typechecks
YulNaumenko Aug 25, 2021
f367f5f
fixed position
YulNaumenko Aug 25, 2021
af5a284
fixed query
YulNaumenko Aug 25, 2021
a77504e
fixed query
YulNaumenko Aug 25, 2021
df93a0c
fixed tests
YulNaumenko Aug 25, 2021
0c6cb12
fixed types place
YulNaumenko Aug 25, 2021
01dfef3
Merge remote-tracking branch upstream
YulNaumenko Aug 30, 2021
62c518e
fixed due to comments
YulNaumenko Aug 30, 2021
0322be8
fixed due to comments
YulNaumenko Aug 31, 2021
4e55c43
fixed eslint
YulNaumenko Aug 31, 2021
27bd9fb
Merge branch 'master' into event-log-search-legacy-id
kibanamachine Aug 31, 2021
bc92b00
fixed due to comments
YulNaumenko Sep 1, 2021
31863cf
splitted test data
YulNaumenko Sep 1, 2021
bd4dc4c
fixed test data
YulNaumenko Sep 1, 2021
06a7db8
increased the delay time to await the search
YulNaumenko Sep 1, 2021
4318296
removed version for 7.9 docs
YulNaumenko Sep 2, 2021
2dab339
Update x-pack/plugins/event_log/server/es/cluster_client_adapter.ts
YulNaumenko Sep 2, 2021
fd118c6
Merge remote-tracking branch 'upstream/master' into event-log-search-…
YulNaumenko Sep 2, 2021
c034a4c
fixed unit test
YulNaumenko Sep 2, 2021
64348a7
Merge branch 'master' into event-log-search-legacy-id
kibanamachine Sep 2, 2021
5e34301
Merge branch 'event-log-search-legacy-id' of https://github.com/YulNa…
YulNaumenko Sep 2, 2021
d644304
fixed test data
YulNaumenko Sep 3, 2021
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
63 changes: 46 additions & 17 deletions x-pack/plugins/alerting/server/rules_client/rules_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ import {
AlertNotifyWhenType,
AlertTypeParams,
ResolvedSanitizedRule,
AlertWithLegacyId,
SanitizedAlertWithLegacyId,
PartialAlertWithLegacyId,
} from '../types';
import {
validateAlertTypeParams,
Expand Down Expand Up @@ -383,9 +386,11 @@ export class RulesClient {

public async get<Params extends AlertTypeParams = never>({
id,
includeLegacyId = false,
}: {
id: string;
}): Promise<SanitizedAlert<Params>> {
includeLegacyId?: boolean;
}): Promise<SanitizedAlert<Params> | SanitizedAlertWithLegacyId<Params>> {
const result = await this.unsecuredSavedObjectsClient.get<RawAlert>('alert', id);
try {
await this.authorization.ensureAuthorized({
Expand Down Expand Up @@ -414,7 +419,8 @@ export class RulesClient {
result.id,
result.attributes.alertTypeId,
result.attributes,
result.references
result.references,
includeLegacyId
);
}

Expand Down Expand Up @@ -486,7 +492,8 @@ export class RulesClient {
dateStart,
}: GetAlertInstanceSummaryParams): Promise<AlertInstanceSummary> {
this.logger.debug(`getAlertInstanceSummary(): getting alert ${id}`);
const alert = await this.get({ id });
const alert = (await this.get({ id, includeLegacyId: true })) as SanitizedAlertWithLegacyId;

await this.authorization.ensureAuthorized({
ruleTypeId: alert.alertTypeId,
consumer: alert.consumer,
Expand All @@ -505,13 +512,18 @@ export class RulesClient {
this.logger.debug(`getAlertInstanceSummary(): search the event log for alert ${id}`);
let events: IEvent[];
try {
const queryResults = await eventLogClient.findEventsBySavedObjectIds('alert', [id], {
page: 1,
per_page: 10000,
start: parsedDateStart.toISOString(),
end: dateNow.toISOString(),
sort_order: 'desc',
});
const queryResults = await eventLogClient.findEventsBySavedObjectIds(
'alert',
[id],
{
page: 1,
per_page: 10000,
start: parsedDateStart.toISOString(),
end: dateNow.toISOString(),
sort_order: 'desc',
},
alert.legacyId !== null ? [alert.legacyId] : undefined
);
events = queryResults.data;
} catch (err) {
this.logger.debug(
Expand Down Expand Up @@ -1533,13 +1545,26 @@ export class RulesClient {
id: string,
ruleTypeId: string,
rawAlert: RawAlert,
references: SavedObjectReference[] | undefined
): Alert {
references: SavedObjectReference[] | undefined,
includeLegacyId: boolean = false
): Alert | AlertWithLegacyId {
const ruleType = this.ruleTypeRegistry.get(ruleTypeId);
// In order to support the partial update API of Saved Objects we have to support
// partial updates of an Alert, but when we receive an actual RawAlert, it is safe
// to cast the result to an Alert
return this.getPartialAlertFromRaw<Params>(id, ruleType, rawAlert, references) as Alert;
const res = this.getPartialAlertFromRaw<Params>(
id,
ruleType,
rawAlert,
references,
includeLegacyId
);
// include to result because it is for internal rules client usage
if (includeLegacyId) {
return res as AlertWithLegacyId;
}
// exclude from result because it is an internal variable
return omit(res, ['legacyId']) as Alert;
}

private getPartialAlertFromRaw<Params extends AlertTypeParams>(
Expand All @@ -1550,17 +1575,18 @@ export class RulesClient {
updatedAt,
meta,
notifyWhen,
legacyId,
scheduledTaskId,
params,
legacyId, // exclude from result because it is an internal variable
YulNaumenko marked this conversation as resolved.
Show resolved Hide resolved
executionStatus,
schedule,
actions,
...partialRawAlert
}: Partial<RawAlert>,
references: SavedObjectReference[] | undefined
): PartialAlert<Params> {
return {
references: SavedObjectReference[] | undefined,
includeLegacyId: boolean = false
): PartialAlert<Params> | PartialAlertWithLegacyId<Params> {
const rule = {
id,
notifyWhen,
...partialRawAlert,
Expand All @@ -1576,6 +1602,9 @@ export class RulesClient {
? { executionStatus: alertExecutionStatusFromRaw(this.logger, id, executionStatus) }
: {}),
};
return includeLegacyId
? ({ ...rule, legacyId } as PartialAlertWithLegacyId<Params>)
: (rule as PartialAlert<Params>);
}

private async validateActions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ describe('getAlertInstanceSummary()', () => {
"sort_order": "desc",
"start": "2019-02-12T21:00:22.479Z",
},
undefined,
]
`);
// calculate the expected start/end date for one test
Expand All @@ -225,6 +226,38 @@ describe('getAlertInstanceSummary()', () => {
expect(endMillis - startMillis).toBeLessThan(expectedDuration + 2);
});

test('calls event log client with legacy ids param', async () => {
unsecuredSavedObjectsClient.get.mockResolvedValueOnce(
getAlertInstanceSummarySavedObject({ legacyId: '99999' })
);
eventLogClient.findEventsBySavedObjectIds.mockResolvedValueOnce(
AlertInstanceSummaryFindEventsResult
);

await rulesClient.getAlertInstanceSummary({ id: '1' });

expect(unsecuredSavedObjectsClient.get).toHaveBeenCalledTimes(1);
expect(eventLogClient.findEventsBySavedObjectIds).toHaveBeenCalledTimes(1);
expect(eventLogClient.findEventsBySavedObjectIds.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"alert",
Array [
"1",
],
Object {
"end": "2019-02-12T21:01:22.479Z",
"page": 1,
"per_page": 10000,
"sort_order": "desc",
"start": "2019-02-12T21:00:22.479Z",
},
Array [
"99999",
],
]
`);
});

test('calls event log client with start date', async () => {
unsecuredSavedObjectsClient.get.mockResolvedValueOnce(getAlertInstanceSummarySavedObject());
eventLogClient.findEventsBySavedObjectIds.mockResolvedValueOnce(
Expand Down
15 changes: 15 additions & 0 deletions x-pack/plugins/alerting/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,21 @@ export interface RawAlertExecutionStatus extends SavedObjectAttributes {
export type PartialAlert<Params extends AlertTypeParams = never> = Pick<Alert<Params>, 'id'> &
Partial<Omit<Alert<Params>, 'id'>>;

export interface AlertWithLegacyId<Params extends AlertTypeParams = never> extends Alert<Params> {
legacyId: string | null;
}

export type SanitizedAlertWithLegacyId<Params extends AlertTypeParams = never> = Omit<
AlertWithLegacyId<Params>,
'apiKey'
>;

export type PartialAlertWithLegacyId<Params extends AlertTypeParams = never> = Pick<
AlertWithLegacyId<Params>,
'id'
> &
Partial<Omit<AlertWithLegacyId<Params>, 'id'>>;

export interface RawAlert extends SavedObjectAttributes {
enabled: boolean;
name: string;
Expand Down
7 changes: 5 additions & 2 deletions x-pack/plugins/event_log/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ Request Body:
|Property|Description|Type|
|---|---|---|
|ids|The array ids of the saved object.|string array|
|legacyIds|The array legacy ids of the saved object. This filter applies to the rules creted in Kibana versions before 8.0.0.|string array|

Response body:

Expand All @@ -284,7 +285,8 @@ interface EventLogClient {
findEventsBySavedObjectIds(
type: string,
ids: string[],
options?: Partial<FindOptionsType>
options?: Partial<FindOptionsType>,
legacyIds?: string[]
): Promise<QueryEventsBySavedObjectResult>;
}

Expand Down Expand Up @@ -404,7 +406,8 @@ export interface IEventLogClient {
findEventsBySavedObjectIds(
type: string,
ids: string[],
options?: Partial<FindOptionsType>
options?: Partial<FindOptionsType>,
legacyIds?: string[]
): Promise<QueryEventsBySavedObjectResult>;
}
```
Expand Down
Loading