diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 2e71f23309f81..2371e7d9f7a51 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -727,6 +727,7 @@ packages/kbn-resizable-layout @elastic/kibana-data-discovery examples/resizable_layout_examples @elastic/kibana-data-discovery x-pack/test/plugin_functional/plugins/resolver_test @elastic/security-solution packages/response-ops/feature_flag_service @elastic/response-ops +packages/response-ops/rule_params @elastic/response-ops examples/response_stream @elastic/ml-ui packages/kbn-rison @elastic/kibana-operations x-pack/packages/rollup @elastic/kibana-management diff --git a/package.json b/package.json index d0778dba1fb2c..643a3ffe1a4c8 100644 --- a/package.json +++ b/package.json @@ -746,6 +746,7 @@ "@kbn/resizable-layout-examples-plugin": "link:examples/resizable_layout_examples", "@kbn/resolver-test-plugin": "link:x-pack/test/plugin_functional/plugins/resolver_test", "@kbn/response-ops-feature-flag-service": "link:packages/response-ops/feature_flag_service", + "@kbn/response-ops-rule-params": "link:packages/response-ops/rule_params", "@kbn/response-stream-plugin": "link:examples/response_stream", "@kbn/rison": "link:packages/kbn-rison", "@kbn/rollup": "link:x-pack/packages/rollup", diff --git a/packages/response-ops/rule_params/README.md b/packages/response-ops/rule_params/README.md new file mode 100644 index 0000000000000..8cc747bf38864 --- /dev/null +++ b/packages/response-ops/rule_params/README.md @@ -0,0 +1,3 @@ +# @kbn/response-ops-rule-params + +The package is responsible for the parameters' schema of all rule types. The alerting plugin uses this package to generate OAS documentation for the `params` property in the rule in requests and responses. diff --git a/packages/response-ops/rule_params/index.ts b/packages/response-ops/rule_params/index.ts new file mode 100644 index 0000000000000..a5ce640a4c5d4 --- /dev/null +++ b/packages/response-ops/rule_params/index.ts @@ -0,0 +1,23 @@ +/* + * 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", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +export { ruleParamsSchema, ruleParamsSchemaWithDefaultValue } from './latest'; + +export { + ruleParamsSchema as ruleParamsSchemaV1, + ruleParamsSchemaWithDefaultValue as ruleParamsSchemaWithDefaultValueV1, +} from './v1'; + +export type { RuleParams } from './latest'; +export type { RuleParamsWithDefaultValue } from './latest'; + +export type { + RuleParams as RuleParamsV1, + RuleParamsWithDefaultValue as RuleParamsWithDefaultValueV1, +} from './v1'; diff --git a/packages/response-ops/rule_params/jest.config.js b/packages/response-ops/rule_params/jest.config.js new file mode 100644 index 0000000000000..ee60f7ea42272 --- /dev/null +++ b/packages/response-ops/rule_params/jest.config.js @@ -0,0 +1,14 @@ +/* + * 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", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +module.exports = { + preset: '@kbn/test/jest_node', + rootDir: '../../..', + roots: ['/packages/response-ops/rule_params'], +}; diff --git a/packages/response-ops/rule_params/kibana.jsonc b/packages/response-ops/rule_params/kibana.jsonc new file mode 100644 index 0000000000000..6a6744a58c4a1 --- /dev/null +++ b/packages/response-ops/rule_params/kibana.jsonc @@ -0,0 +1,5 @@ +{ + "type": "shared-common", + "id": "@kbn/response-ops-rule-params", + "owner": "@elastic/response-ops" +} diff --git a/packages/response-ops/rule_params/latest.ts b/packages/response-ops/rule_params/latest.ts new file mode 100644 index 0000000000000..f278309c22b03 --- /dev/null +++ b/packages/response-ops/rule_params/latest.ts @@ -0,0 +1,10 @@ +/* + * 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", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +export * from './v1'; diff --git a/packages/response-ops/rule_params/package.json b/packages/response-ops/rule_params/package.json new file mode 100644 index 0000000000000..43145b8d5da4c --- /dev/null +++ b/packages/response-ops/rule_params/package.json @@ -0,0 +1,6 @@ +{ + "name": "@kbn/response-ops-rule-params", + "private": true, + "version": "1.0.0", + "license": "Elastic License 2.0 OR AGPL-3.0-only OR SSPL-1.0" +} \ No newline at end of file diff --git a/packages/response-ops/rule_params/tsconfig.json b/packages/response-ops/rule_params/tsconfig.json new file mode 100644 index 0000000000000..3df73f778fdc1 --- /dev/null +++ b/packages/response-ops/rule_params/tsconfig.json @@ -0,0 +1,19 @@ +{ + "extends": "../../../tsconfig.base.json", + "compilerOptions": { + "outDir": "target/types", + "types": [ + "jest", + "node" + ] + }, + "include": [ + "**/*.ts", + ], + "exclude": [ + "target/**/*" + ], + "kbn_references": [ + "@kbn/config-schema", + ] +} diff --git a/packages/response-ops/rule_params/v1.ts b/packages/response-ops/rule_params/v1.ts new file mode 100644 index 0000000000000..a083f67f10c8c --- /dev/null +++ b/packages/response-ops/rule_params/v1.ts @@ -0,0 +1,26 @@ +/* + * 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", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +import { TypeOf, schema } from '@kbn/config-schema'; + +export const ruleParamsSchema = schema.recordOf(schema.string(), schema.maybe(schema.any()), { + meta: { description: 'The parameters for the rule.' }, +}); + +export const ruleParamsSchemaWithDefaultValue = schema.recordOf( + schema.string(), + schema.maybe(schema.any()), + { + defaultValue: {}, + meta: { description: 'The parameters for the rule.' }, + } +); + +export type RuleParams = TypeOf; +export type RuleParamsWithDefaultValue = TypeOf; diff --git a/tsconfig.base.json b/tsconfig.base.json index 43b35e31ea905..b68b2e8703138 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -1448,6 +1448,8 @@ "@kbn/resolver-test-plugin/*": ["x-pack/test/plugin_functional/plugins/resolver_test/*"], "@kbn/response-ops-feature-flag-service": ["packages/response-ops/feature_flag_service"], "@kbn/response-ops-feature-flag-service/*": ["packages/response-ops/feature_flag_service/*"], + "@kbn/response-ops-rule-params": ["packages/response-ops/rule_params"], + "@kbn/response-ops-rule-params/*": ["packages/response-ops/rule_params/*"], "@kbn/response-stream-plugin": ["examples/response_stream"], "@kbn/response-stream-plugin/*": ["examples/response_stream/*"], "@kbn/rison": ["packages/kbn-rison"], diff --git a/x-pack/plugins/alerting/common/routes/backfill/response/schemas/v1.ts b/x-pack/plugins/alerting/common/routes/backfill/response/schemas/v1.ts index 268ef7f5e90d1..5da51d53dddbb 100644 --- a/x-pack/plugins/alerting/common/routes/backfill/response/schemas/v1.ts +++ b/x-pack/plugins/alerting/common/routes/backfill/response/schemas/v1.ts @@ -6,6 +6,7 @@ */ import { schema } from '@kbn/config-schema'; +import { ruleParamsSchemaV1 } from '@kbn/response-ops-rule-params'; import { adHocRunStatus } from '../../../../constants'; export const statusSchema = schema.oneOf([ @@ -26,7 +27,7 @@ export const backfillResponseSchema = schema.object({ name: schema.string(), tags: schema.arrayOf(schema.string()), rule_type_id: schema.string(), - params: schema.recordOf(schema.string(), schema.maybe(schema.any())), + params: ruleParamsSchemaV1, api_key_owner: schema.nullable(schema.string()), api_key_created_by_user: schema.maybe(schema.nullable(schema.boolean())), consumer: schema.string(), diff --git a/x-pack/plugins/alerting/common/routes/rule/apis/create/schemas/v1.ts b/x-pack/plugins/alerting/common/routes/rule/apis/create/schemas/v1.ts index d9157850bfd8d..e70df7f9dc73f 100644 --- a/x-pack/plugins/alerting/common/routes/rule/apis/create/schemas/v1.ts +++ b/x-pack/plugins/alerting/common/routes/rule/apis/create/schemas/v1.ts @@ -6,6 +6,7 @@ */ import { schema } from '@kbn/config-schema'; +import { ruleParamsSchemaWithDefaultValueV1 } from '@kbn/response-ops-rule-params'; import { validateDurationV1, validateHoursV1, validateTimezoneV1 } from '../../../validation'; import { notifyWhenSchemaV1, alertDelaySchemaV1 } from '../../../response'; import { alertsFilterQuerySchemaV1 } from '../../../../alerts_filter_query'; @@ -166,10 +167,7 @@ export const createBodySchema = schema.object({ }) ) ), - params: schema.recordOf(schema.string(), schema.maybe(schema.any()), { - defaultValue: {}, - meta: { description: 'The parameters for the rule.' }, - }), + params: ruleParamsSchemaWithDefaultValueV1, schedule: schema.object( { interval: schema.string({ diff --git a/x-pack/plugins/alerting/common/routes/rule/apis/update/schemas/v1.ts b/x-pack/plugins/alerting/common/routes/rule/apis/update/schemas/v1.ts index e83e26f119595..b838d21e5cc03 100644 --- a/x-pack/plugins/alerting/common/routes/rule/apis/update/schemas/v1.ts +++ b/x-pack/plugins/alerting/common/routes/rule/apis/update/schemas/v1.ts @@ -6,6 +6,7 @@ */ import { schema } from '@kbn/config-schema'; +import { ruleParamsSchemaWithDefaultValueV1 } from '@kbn/response-ops-rule-params'; import { validateDurationV1, validateHoursV1, validateTimezoneV1 } from '../../../validation'; import { notifyWhenSchemaV1, alertDelaySchemaV1 } from '../../../response'; import { alertsFilterQuerySchemaV1 } from '../../../../alerts_filter_query'; @@ -152,10 +153,7 @@ export const updateBodySchema = schema.object({ }) ) ), - params: schema.recordOf(schema.string(), schema.any(), { - defaultValue: {}, - meta: { description: 'The parameters for the rule.' }, - }), + params: ruleParamsSchemaWithDefaultValueV1, actions: schema.arrayOf(actionSchema, { defaultValue: [] }), notify_when: schema.maybe(schema.nullable(notifyWhenSchemaV1)), alert_delay: schema.maybe(alertDelaySchemaV1), diff --git a/x-pack/plugins/alerting/common/routes/rule/response/index.ts b/x-pack/plugins/alerting/common/routes/rule/response/index.ts index 8c784e744d473..1c7632ad28988 100644 --- a/x-pack/plugins/alerting/common/routes/rule/response/index.ts +++ b/x-pack/plugins/alerting/common/routes/rule/response/index.ts @@ -6,7 +6,6 @@ */ export { - ruleParamsSchema, actionParamsSchema, mappedParamsSchema, ruleExecutionStatusSchema, @@ -18,16 +17,9 @@ export { scheduleIdsSchema, } from './schemas/latest'; -export type { - RuleParams, - RuleResponse, - RuleSnoozeSchedule, - RuleLastRun, - Monitoring, -} from './types/latest'; +export type { RuleResponse, RuleSnoozeSchedule, RuleLastRun, Monitoring } from './types/latest'; export { - ruleParamsSchema as ruleParamsSchemaV1, actionParamsSchema as actionParamsSchemaV1, mappedParamsSchema as mappedParamsSchemaV1, ruleExecutionStatusSchema as ruleExecutionStatusSchemaV1, @@ -41,9 +33,14 @@ export { } from './schemas/v1'; export type { - RuleParams as RuleParamsV1, RuleResponse as RuleResponseV1, RuleSnoozeSchedule as RuleSnoozeScheduleV1, RuleLastRun as RuleLastRunV1, Monitoring as MonitoringV1, } from './types/v1'; + +export { ruleParamsSchemaV1 } from '@kbn/response-ops-rule-params'; +export { ruleParamsSchema } from '@kbn/response-ops-rule-params'; + +export type { RuleParamsV1 } from '@kbn/response-ops-rule-params'; +export type { RuleParams } from '@kbn/response-ops-rule-params'; diff --git a/x-pack/plugins/alerting/common/routes/rule/response/schemas/v1.ts b/x-pack/plugins/alerting/common/routes/rule/response/schemas/v1.ts index 29488b98d6ca8..069aca001d14f 100644 --- a/x-pack/plugins/alerting/common/routes/rule/response/schemas/v1.ts +++ b/x-pack/plugins/alerting/common/routes/rule/response/schemas/v1.ts @@ -6,6 +6,7 @@ */ import { schema } from '@kbn/config-schema'; +import { ruleParamsSchemaV1 } from '@kbn/response-ops-rule-params'; import { rRuleResponseSchemaV1 } from '../../../r_rule'; import { alertsFilterQuerySchemaV1 } from '../../../alerts_filter_query'; import { @@ -18,9 +19,6 @@ import { import { validateNotifyWhenV1 } from '../../validation'; import { flappingSchemaV1 } from '../../common'; -export const ruleParamsSchema = schema.recordOf(schema.string(), schema.maybe(schema.any()), { - meta: { description: 'The parameters for the rule.' }, -}); export const actionParamsSchema = schema.recordOf(schema.string(), schema.maybe(schema.any()), { meta: { description: @@ -497,7 +495,7 @@ export const ruleResponseSchema = schema.object({ }), schedule: intervalScheduleSchema, actions: schema.arrayOf(actionSchema), - params: ruleParamsSchema, + params: ruleParamsSchemaV1, mapped_params: schema.maybe(mappedParamsSchema), scheduled_task_id: schema.maybe( schema.string({ diff --git a/x-pack/plugins/alerting/common/routes/rule/response/types/v1.ts b/x-pack/plugins/alerting/common/routes/rule/response/types/v1.ts index e9a37eea1fe72..e32a56a302e63 100644 --- a/x-pack/plugins/alerting/common/routes/rule/response/types/v1.ts +++ b/x-pack/plugins/alerting/common/routes/rule/response/types/v1.ts @@ -6,22 +6,21 @@ */ import type { TypeOf } from '@kbn/config-schema'; +import { RuleParamsV1 } from '@kbn/response-ops-rule-params'; import { - ruleParamsSchemaV1, ruleResponseSchemaV1, ruleSnoozeScheduleSchemaV1, ruleLastRunSchemaV1, monitoringSchemaV1, } from '..'; -export type RuleParams = TypeOf; export type RuleSnoozeSchedule = TypeOf; export type RuleLastRun = TypeOf; export type Monitoring = TypeOf; type RuleResponseSchemaType = TypeOf; -export interface RuleResponse { +export interface RuleResponse { id: RuleResponseSchemaType['id']; enabled: RuleResponseSchemaType['enabled']; name: RuleResponseSchemaType['name']; diff --git a/x-pack/plugins/alerting/server/application/backfill/result/schemas/index.ts b/x-pack/plugins/alerting/server/application/backfill/result/schemas/index.ts index de3cc5926a4ae..b454d41dd40ca 100644 --- a/x-pack/plugins/alerting/server/application/backfill/result/schemas/index.ts +++ b/x-pack/plugins/alerting/server/application/backfill/result/schemas/index.ts @@ -6,6 +6,7 @@ */ import { schema } from '@kbn/config-schema'; +import { ruleParamsSchema } from '@kbn/response-ops-rule-params'; import { adHocRunStatus } from '../../../../../common/constants'; export const statusSchema = schema.oneOf([ @@ -32,7 +33,7 @@ export const backfillSchema = schema.object({ name: schema.string(), tags: schema.arrayOf(schema.string()), alertTypeId: schema.string(), - params: schema.recordOf(schema.string(), schema.maybe(schema.any())), + params: ruleParamsSchema, apiKeyOwner: schema.nullable(schema.string()), apiKeyCreatedByUser: schema.maybe(schema.nullable(schema.boolean())), consumer: schema.string(), diff --git a/x-pack/plugins/alerting/server/application/rule/methods/create/schemas/create_rule_data_schema.ts b/x-pack/plugins/alerting/server/application/rule/methods/create/schemas/create_rule_data_schema.ts index 0672d7929fdb2..e2cf0da359b0a 100644 --- a/x-pack/plugins/alerting/server/application/rule/methods/create/schemas/create_rule_data_schema.ts +++ b/x-pack/plugins/alerting/server/application/rule/methods/create/schemas/create_rule_data_schema.ts @@ -6,6 +6,7 @@ */ import { schema } from '@kbn/config-schema'; +import { ruleParamsSchemaWithDefaultValue } from '@kbn/response-ops-rule-params'; import { validateDuration } from '../../../validation'; import { notifyWhenSchema, @@ -23,7 +24,7 @@ export const createRuleDataSchema = schema.object( consumer: schema.string(), tags: schema.arrayOf(schema.string(), { defaultValue: [] }), throttle: schema.maybe(schema.nullable(schema.string({ validate: validateDuration }))), - params: schema.recordOf(schema.string(), schema.maybe(schema.any()), { defaultValue: {} }), + params: ruleParamsSchemaWithDefaultValue, schedule: schema.object({ interval: schema.string({ validate: validateDuration }), }), diff --git a/x-pack/plugins/alerting/server/application/rule/methods/update/schemas/update_rule_data_schema.ts b/x-pack/plugins/alerting/server/application/rule/methods/update/schemas/update_rule_data_schema.ts index 0c4a1df45d44e..9c0bf1666f846 100644 --- a/x-pack/plugins/alerting/server/application/rule/methods/update/schemas/update_rule_data_schema.ts +++ b/x-pack/plugins/alerting/server/application/rule/methods/update/schemas/update_rule_data_schema.ts @@ -6,6 +6,7 @@ */ import { schema } from '@kbn/config-schema'; +import { ruleParamsSchemaWithDefaultValue } from '@kbn/response-ops-rule-params'; import { validateDuration } from '../../../validation'; import { notifyWhenSchema, @@ -23,7 +24,7 @@ export const updateRuleDataSchema = schema.object( interval: schema.string({ validate: validateDuration }), }), throttle: schema.maybe(schema.nullable(schema.string({ validate: validateDuration }))), - params: schema.recordOf(schema.string(), schema.maybe(schema.any()), { defaultValue: {} }), + params: ruleParamsSchemaWithDefaultValue, actions: schema.arrayOf(actionRequestSchema, { defaultValue: [] }), systemActions: schema.maybe(schema.arrayOf(systemActionRequestSchema, { defaultValue: [] })), notifyWhen: schema.maybe(schema.nullable(notifyWhenSchema)), diff --git a/x-pack/plugins/alerting/server/application/rule/schemas/rule_schemas.ts b/x-pack/plugins/alerting/server/application/rule/schemas/rule_schemas.ts index 7f9fcb1bd5377..da91ceb727d2c 100644 --- a/x-pack/plugins/alerting/server/application/rule/schemas/rule_schemas.ts +++ b/x-pack/plugins/alerting/server/application/rule/schemas/rule_schemas.ts @@ -6,6 +6,7 @@ */ import { schema } from '@kbn/config-schema'; +import { ruleParamsSchema } from '@kbn/response-ops-rule-params'; import { ruleLastRunOutcomeValues, ruleExecutionStatusValues, @@ -18,7 +19,6 @@ import { notifyWhenSchema } from './notify_when_schema'; import { actionSchema, systemActionSchema } from './action_schemas'; import { flappingSchema } from './flapping_schema'; -export const ruleParamsSchema = schema.recordOf(schema.string(), schema.maybe(schema.any())); export const mappedParamsSchema = schema.recordOf(schema.string(), schema.maybe(schema.any())); export const intervalScheduleSchema = schema.object({ diff --git a/x-pack/plugins/alerting/server/application/rule/types/rule.ts b/x-pack/plugins/alerting/server/application/rule/types/rule.ts index 0b1177d31e1f7..2e5cad45cf92f 100644 --- a/x-pack/plugins/alerting/server/application/rule/types/rule.ts +++ b/x-pack/plugins/alerting/server/application/rule/types/rule.ts @@ -6,6 +6,7 @@ */ import { TypeOf } from '@kbn/config-schema'; +import { ruleParamsSchema } from '@kbn/response-ops-rule-params'; import { ruleNotifyWhen, ruleLastRunOutcomeValues, @@ -14,7 +15,6 @@ import { ruleExecutionStatusWarningReason, } from '../constants'; import { - ruleParamsSchema, snoozeScheduleSchema, ruleExecutionStatusSchema, ruleLastRunSchema, diff --git a/x-pack/plugins/alerting/tsconfig.json b/x-pack/plugins/alerting/tsconfig.json index c0951663a8489..eefc1999b26d5 100644 --- a/x-pack/plugins/alerting/tsconfig.json +++ b/x-pack/plugins/alerting/tsconfig.json @@ -73,7 +73,8 @@ "@kbn/core-security-server", "@kbn/core-http-server", "@kbn/zod", - "@kbn/core-saved-objects-base-server-internal" + "@kbn/core-saved-objects-base-server-internal", + "@kbn/response-ops-rule-params" ], "exclude": [ "target/**/*" diff --git a/yarn.lock b/yarn.lock index 6139daa0f206c..54e60a9b8543d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6191,6 +6191,10 @@ version "0.0.0" uid "" +"@kbn/response-ops-rule-params@link:packages/response-ops/rule_params": + version "0.0.0" + uid "" + "@kbn/response-stream-plugin@link:examples/response_stream": version "0.0.0" uid ""