Skip to content

Commit

Permalink
[RAM][HTTP Versioning] Version GET Rule Route (#181304)
Browse files Browse the repository at this point in the history
## Summary

Parent Issue: #157883
Issue: #181263

Versions the GET rule endpoint with added input and output validation.

```
GET /internal/alerting/rule/{id}
GET /api/alerting/rule/{id}
```
### Checklist

Delete any items that are not applicable to this PR.
- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
  • Loading branch information
JiaweiWu authored Apr 30, 2024
1 parent 5814f2e commit a1ada76
Show file tree
Hide file tree
Showing 39 changed files with 707 additions and 299 deletions.
15 changes: 15 additions & 0 deletions x-pack/plugins/alerting/common/routes/rule/apis/get/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* 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.
*/

export { getRuleRequestParamsSchema } from './schemas/latest';
export type { GetRuleRequestParams, GetRuleResponse } from './types/latest';

export { getRuleRequestParamsSchema as getRuleRequestParamsSchemaV1 } from './schemas/v1';
export type {
GetRuleRequestParams as GetRuleRequestParamsV1,
GetRuleResponse as GetRuleResponseV1,
} from './types/v1';
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* 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.
*/

export * from './v1';
12 changes: 12 additions & 0 deletions x-pack/plugins/alerting/common/routes/rule/apis/get/schemas/v1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* 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 } from '@kbn/config-schema';

export const getRuleRequestParamsSchema = schema.object({
id: schema.string(),
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* 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.
*/

export * from './v1';
16 changes: 16 additions & 0 deletions x-pack/plugins/alerting/common/routes/rule/apis/get/types/v1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* 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 type { TypeOf } from '@kbn/config-schema';
import { RuleParamsV1, RuleResponseV1 } from '../../../response';
import { getRuleRequestParamsSchemaV1 } from '..';

export type GetRuleRequestParams = TypeOf<typeof getRuleRequestParamsSchemaV1>;

export interface GetRuleResponse<Params extends RuleParamsV1 = never> {
body: RuleResponseV1<Params>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export interface RuleResponse<Params extends RuleParams = never> {
mute_all: RuleResponseSchemaType['mute_all'];
notify_when?: RuleResponseSchemaType['notify_when'];
muted_alert_ids: RuleResponseSchemaType['muted_alert_ids'];
execution_status: RuleResponseSchemaType['execution_status'];
execution_status?: RuleResponseSchemaType['execution_status'];
monitoring?: RuleResponseSchemaType['monitoring'];
snooze_schedule?: RuleResponseSchemaType['snooze_schedule'];
active_snoozes?: RuleResponseSchemaType['active_snoozes'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ describe('scheduleBackfill()', () => {
rules: [
{
id: existingDecryptedRule1.id,
legacyId: null,
actions: existingDecryptedRule1.attributes.actions,
alertTypeId: existingDecryptedRule1.attributes.alertTypeId,
apiKey: existingDecryptedRule1.attributes.apiKey,
Expand Down Expand Up @@ -430,6 +431,7 @@ describe('scheduleBackfill()', () => {
},
{
id: existingDecryptedRule2.id,
legacyId: null,
actions: existingDecryptedRule2.attributes.actions,
alertTypeId: existingDecryptedRule2.attributes.alertTypeId,
apiKey: existingDecryptedRule2.attributes.apiKey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,29 @@
*/
import { AlertConsumers } from '@kbn/rule-data-utils';

import { RulesClient, ConstructorOptions } from '../rules_client';
import { RulesClient, ConstructorOptions } from '../../../../rules_client/rules_client';
import {
savedObjectsClientMock,
loggingSystemMock,
savedObjectsRepositoryMock,
uiSettingsServiceMock,
} from '@kbn/core/server/mocks';
import { taskManagerMock } from '@kbn/task-manager-plugin/server/mocks';
import { ruleTypeRegistryMock } from '../../rule_type_registry.mock';
import { alertingAuthorizationMock } from '../../authorization/alerting_authorization.mock';
import { ruleTypeRegistryMock } from '../../../../rule_type_registry.mock';
import { alertingAuthorizationMock } from '../../../../authorization/alerting_authorization.mock';
import { encryptedSavedObjectsMock } from '@kbn/encrypted-saved-objects-plugin/server/mocks';
import { actionsAuthorizationMock } from '@kbn/actions-plugin/server/mocks';
import { AlertingAuthorization } from '../../authorization/alerting_authorization';
import { AlertingAuthorization } from '../../../../authorization/alerting_authorization';
import { ActionsAuthorization } from '@kbn/actions-plugin/server';
import { auditLoggerMock } from '@kbn/security-plugin/server/audit/mocks';
import { getBeforeSetup, setGlobalDate } from './lib';
import { RecoveredActionGroup } from '../../../common';
import { formatLegacyActions } from '../lib';
import { ConnectorAdapterRegistry } from '../../connector_adapters/connector_adapter_registry';
import { RULE_SAVED_OBJECT_TYPE } from '../../saved_objects';
import { backfillClientMock } from '../../backfill_client/backfill_client.mock';
import { getBeforeSetup, setGlobalDate } from '../../../../rules_client/tests/lib';
import { RecoveredActionGroup } from '../../../../../common';
import { formatLegacyActions } from '../../../../rules_client/lib';
import { ConnectorAdapterRegistry } from '../../../../connector_adapters/connector_adapter_registry';
import { RULE_SAVED_OBJECT_TYPE } from '../../../../saved_objects';
import { backfillClientMock } from '../../../../backfill_client/backfill_client.mock';

jest.mock('../lib/siem_legacy_actions/format_legacy_actions', () => {
jest.mock('../../../../rules_client/lib/siem_legacy_actions/format_legacy_actions', () => {
return {
formatLegacyActions: jest.fn(),
};
Expand Down Expand Up @@ -91,6 +91,10 @@ describe('get()', () => {
params: {
bar: true,
},
executionStatus: {
status: 'unknown',
lastExecutionDate: new Date('2020-08-20T19:23:38Z'),
},
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
actions: [
Expand Down Expand Up @@ -128,6 +132,10 @@ describe('get()', () => {
],
"alertTypeId": "123",
"createdAt": 2019-02-12T21:01:22.479Z,
"executionStatus": Object {
"lastExecutionDate": 2020-08-20T19:23:38.000Z,
"status": "unknown",
},
"id": "1",
"notifyWhen": "onActiveAlert",
"params": Object {
Expand All @@ -143,11 +151,12 @@ describe('get()', () => {
`);
expect(unsecuredSavedObjectsClient.get).toHaveBeenCalledTimes(1);
expect(unsecuredSavedObjectsClient.get.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"alert",
"1",
]
`);
Array [
"alert",
"1",
undefined,
]
`);
});

test('gets rule with actions using preconfigured connectors', async () => {
Expand All @@ -161,6 +170,10 @@ describe('get()', () => {
params: {
bar: true,
},
executionStatus: {
status: 'unknown',
lastExecutionDate: new Date('2020-08-20T19:23:38Z'),
},
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
actions: [
Expand Down Expand Up @@ -214,6 +227,10 @@ describe('get()', () => {
],
"alertTypeId": "123",
"createdAt": 2019-02-12T21:01:22.479Z,
"executionStatus": Object {
"lastExecutionDate": 2020-08-20T19:23:38.000Z,
"status": "unknown",
},
"id": "1",
"notifyWhen": "onActiveAlert",
"params": Object {
Expand All @@ -229,11 +246,12 @@ describe('get()', () => {
`);
expect(unsecuredSavedObjectsClient.get).toHaveBeenCalledTimes(1);
expect(unsecuredSavedObjectsClient.get.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"alert",
"1",
]
`);
Array [
"alert",
"1",
undefined,
]
`);
});

test('gets rule with actions using system connectors', async () => {
Expand All @@ -247,6 +265,10 @@ describe('get()', () => {
params: {
bar: true,
},
executionStatus: {
status: 'unknown',
lastExecutionDate: new Date('2020-08-20T19:23:38Z'),
},
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
actions: [
Expand Down Expand Up @@ -289,6 +311,10 @@ describe('get()', () => {
],
"alertTypeId": "123",
"createdAt": 2019-02-12T21:01:22.479Z,
"executionStatus": Object {
"lastExecutionDate": 2020-08-20T19:23:38.000Z,
"status": "unknown",
},
"id": "1",
"notifyWhen": "onActiveAlert",
"params": Object {
Expand All @@ -311,11 +337,12 @@ describe('get()', () => {
`);
expect(unsecuredSavedObjectsClient.get).toHaveBeenCalledTimes(1);
expect(unsecuredSavedObjectsClient.get.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"alert",
"1",
]
`);
Array [
"alert",
"1",
undefined,
]
`);
});

test('should call useSavedObjectReferences.injectReferences if defined for rule type', async () => {
Expand Down Expand Up @@ -356,6 +383,10 @@ describe('get()', () => {
bar: true,
parameterThatIsSavedObjectRef: 'soRef_0',
},
executionStatus: {
status: 'unknown',
lastExecutionDate: new Date('2020-08-20T19:23:38Z'),
},
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
actions: [
Expand Down Expand Up @@ -406,6 +437,10 @@ describe('get()', () => {
],
"alertTypeId": "123",
"createdAt": 2019-02-12T21:01:22.479Z,
"executionStatus": Object {
"lastExecutionDate": 2020-08-20T19:23:38.000Z,
"status": "unknown",
},
"id": "1",
"notifyWhen": "onActiveAlert",
"params": Object {
Expand Down Expand Up @@ -487,6 +522,10 @@ describe('get()', () => {
bar: true,
parameterThatIsSavedObjectRef: 'soRef_0',
},
executionStatus: {
status: 'unknown',
lastExecutionDate: new Date('2020-08-20T19:23:38Z'),
},
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
actions: [
Expand Down Expand Up @@ -530,6 +569,10 @@ describe('get()', () => {
params: {
bar: true,
},
executionStatus: {
status: 'unknown',
lastExecutionDate: new Date('2020-08-20T19:23:38Z'),
},
actions: [
{
group: 'default',
Expand Down Expand Up @@ -592,6 +635,10 @@ describe('get()', () => {
params: {
bar: true,
},
executionStatus: {
status: 'unknown',
lastExecutionDate: new Date('2020-08-20T19:23:38Z'),
},
actions: [],
},
references: [],
Expand Down Expand Up @@ -648,6 +695,10 @@ describe('get()', () => {
params: {
bar: true,
},
executionStatus: {
status: 'unknown',
lastExecutionDate: new Date('2020-08-20T19:23:38Z'),
},
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
actions: [
Expand Down
Loading

0 comments on commit a1ada76

Please sign in to comment.