Skip to content

Commit

Permalink
[RAM][HTTP Versioning] Version Internal and Public Find Rules Route (e…
Browse files Browse the repository at this point in the history
…lastic#180654)

## Summary

Issue: elastic#180551
Parent Issue: elastic#157883

Versions the internal and public find API routes. Adds Input validation
as well.

### Checklist
- [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

---------

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
2 people authored and yuliacech committed May 3, 2024
1 parent daa18fc commit 3f8df6c
Show file tree
Hide file tree
Showing 32 changed files with 587 additions and 194 deletions.
15 changes: 15 additions & 0 deletions x-pack/plugins/alerting/common/routes/rule/apis/find/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 { findRulesRequestQuerySchema } from './schemas/latest';
export type { FindRulesRequestQuery, FindRulesResponse } from './types/latest';

export { findRulesRequestQuerySchema as findRulesRequestQuerySchemaV1 } from './schemas/v1';
export type {
FindRulesRequestQuery as FindRulesRequestQueryV1,
FindRulesResponse as FindRulesResponseV1,
} from './types/latest';
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';
33 changes: 33 additions & 0 deletions x-pack/plugins/alerting/common/routes/rule/apis/find/schemas/v1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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 findRulesRequestQuerySchema = schema.object({
per_page: schema.number({ defaultValue: 10, min: 0 }),
page: schema.number({ defaultValue: 1, min: 1 }),
search: schema.maybe(schema.string()),
default_search_operator: schema.oneOf([schema.literal('OR'), schema.literal('AND')], {
defaultValue: 'OR',
}),
search_fields: schema.maybe(schema.oneOf([schema.arrayOf(schema.string()), schema.string()])),
sort_field: schema.maybe(schema.string()),
sort_order: schema.maybe(schema.oneOf([schema.literal('asc'), schema.literal('desc')])),
has_reference: schema.maybe(
// use nullable as maybe is currently broken
// in config-schema
schema.nullable(
schema.object({
type: schema.string(),
id: schema.string(),
})
)
),
fields: schema.maybe(schema.arrayOf(schema.string())),
filter: schema.maybe(schema.string()),
filter_consumers: schema.maybe(schema.arrayOf(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';
21 changes: 21 additions & 0 deletions x-pack/plugins/alerting/common/routes/rule/apis/find/types/v1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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 { findRulesRequestQuerySchemaV1 } from '..';

export type FindRulesRequestQuery = TypeOf<typeof findRulesRequestQuerySchemaV1>;

export interface FindRulesResponse<Params extends RuleParamsV1 = never> {
body: {
page: number;
per_page: number;
total: number;
data: Array<Partial<RuleResponseV1<Params>>>;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,38 @@
* 2.0.
*/

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 { nodeTypes, fromKueryExpression } from '@kbn/es-query';
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 { RegistryRuleType } from '../../rule_type_registry';
import { getBeforeSetup, setGlobalDate } from '../../../../rules_client/tests/lib';
import { RecoveredActionGroup } from '../../../../../common';
import { RegistryRuleType } from '../../../../rule_type_registry';
import { schema } from '@kbn/config-schema';
import { enabledRule1, enabledRule2, siemRule1, siemRule2 } from './test_helpers';
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 {
enabledRule1,
enabledRule2,
siemRule1,
siemRule2,
} from '../../../../rules_client/tests/test_helpers';
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 @@ -82,7 +87,7 @@ beforeEach(() => {

setGlobalDate();

jest.mock('../common/map_sort_field', () => ({
jest.mock('../../../../rules_client/common/map_sort_field', () => ({
mapSortField: jest.fn(),
}));

Expand Down Expand Up @@ -127,6 +132,10 @@ describe('find()', () => {
},
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
executionStatus: {
status: 'pending',
lastExecutionDate: new Date('2019-02-12T21:01:22.479Z'),
},
notifyWhen: 'onActiveAlert',
actions: [
{
Expand Down Expand Up @@ -197,6 +206,10 @@ describe('find()', () => {
],
"alertTypeId": "myType",
"createdAt": 2019-02-12T21:01:22.479Z,
"executionStatus": Object {
"lastExecutionDate": 2019-02-12T21:01:22.000Z,
"status": "pending",
},
"id": "1",
"notifyWhen": "onActiveAlert",
"params": Object {
Expand Down Expand Up @@ -244,6 +257,10 @@ describe('find()', () => {
params: {
bar: true,
},
executionStatus: {
status: 'pending',
lastExecutionDate: new Date('2019-02-12T21:01:22.479Z'),
},
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
notifyWhen: 'onActiveAlert',
Expand Down Expand Up @@ -303,6 +320,10 @@ describe('find()', () => {
],
"alertTypeId": "myType",
"createdAt": 2019-02-12T21:01:22.479Z,
"executionStatus": Object {
"lastExecutionDate": 2019-02-12T21:01:22.000Z,
"status": "pending",
},
"id": "1",
"notifyWhen": "onActiveAlert",
"params": Object {
Expand Down Expand Up @@ -350,6 +371,10 @@ describe('find()', () => {
params: {
bar: true,
},
executionStatus: {
status: 'pending',
lastExecutionDate: new Date('2019-02-12T21:01:22.479Z'),
},
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
notifyWhen: 'onActiveAlert',
Expand Down Expand Up @@ -400,6 +425,10 @@ describe('find()', () => {
],
"alertTypeId": "myType",
"createdAt": 2019-02-12T21:01:22.479Z,
"executionStatus": Object {
"lastExecutionDate": 2019-02-12T21:01:22.000Z,
"status": "pending",
},
"id": "1",
"notifyWhen": "onActiveAlert",
"params": Object {
Expand Down Expand Up @@ -441,7 +470,9 @@ describe('find()', () => {
test('calls mapSortField', async () => {
const rulesClient = new RulesClient(rulesClientParams);
await rulesClient.find({ options: { sortField: 'name' } });
expect(jest.requireMock('../common/map_sort_field').mapSortField).toHaveBeenCalledWith('name');
expect(
jest.requireMock('../../../../rules_client/common/map_sort_field').mapSortField
).toHaveBeenCalledWith('name');
});

test('should translate filter/sort/search on params to mapped_params', async () => {
Expand Down Expand Up @@ -559,6 +590,10 @@ describe('find()', () => {
params: {
bar: true,
},
executionStatus: {
status: 'pending',
lastExecutionDate: new Date('2019-02-12T21:01:22.479Z'),
},
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
notifyWhen: 'onActiveAlert',
Expand Down Expand Up @@ -591,6 +626,10 @@ describe('find()', () => {
bar: true,
parameterThatIsSavedObjectRef: 'soRef_0',
},
executionStatus: {
status: 'pending',
lastExecutionDate: new Date('2019-02-12T21:01:22.479Z'),
},
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
notifyWhen: 'onActiveAlert',
Expand Down Expand Up @@ -649,6 +688,10 @@ describe('find()', () => {
],
"alertTypeId": "myType",
"createdAt": 2019-02-12T21:01:22.479Z,
"executionStatus": Object {
"lastExecutionDate": 2019-02-12T21:01:22.000Z,
"status": "pending",
},
"id": "1",
"notifyWhen": "onActiveAlert",
"params": Object {
Expand All @@ -675,6 +718,10 @@ describe('find()', () => {
],
"alertTypeId": "123",
"createdAt": 2019-02-12T21:01:22.479Z,
"executionStatus": Object {
"lastExecutionDate": 2019-02-12T21:01:22.000Z,
"status": "pending",
},
"id": "2",
"notifyWhen": "onActiveAlert",
"params": Object {
Expand Down Expand Up @@ -779,6 +826,10 @@ describe('find()', () => {
params: {
bar: true,
},
executionStatus: {
status: 'pending',
lastExecutionDate: new Date('2019-02-12T21:01:22.479Z'),
},
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
notifyWhen: 'onActiveAlert',
Expand Down Expand Up @@ -811,6 +862,10 @@ describe('find()', () => {
bar: true,
parameterThatIsSavedObjectRef: 'soRef_0',
},
executionStatus: {
status: 'pending',
lastExecutionDate: new Date('2019-02-12T21:01:22.479Z'),
},
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
notifyWhen: 'onActiveAlert',
Expand Down Expand Up @@ -891,6 +946,10 @@ describe('find()', () => {
type: RULE_SAVED_OBJECT_TYPE,
attributes: {
actions: [],
executionStatus: {
status: 'pending',
lastExecutionDate: new Date('2019-02-12T21:01:22.479Z'),
},
alertTypeId: 'myType',
consumer: 'myApp',
tags: ['myTag'],
Expand Down
Loading

0 comments on commit 3f8df6c

Please sign in to comment.