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

[Response Ops][Rules] Version Get Rule Types API #195361

Merged
merged 17 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* 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 { typesRulesResponseSchema, typesRulesResponseBodySchema } from './schemas/latest';
export type { TypesRulesResponse, TypesRulesResponseBody } from './types/latest';

export {
typesRulesResponseSchema as typesRulesResponseSchemaV1,
typesRulesResponseBodySchema as typesRulesResponseBodySchemaV1,
} from './schemas/v1';
export type {
TypesRulesResponse as TypesRulesResponseV1,
TypesRulesResponseBody as TypesRulesResponseBodyV1,
} 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';
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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';

const actionVariableSchema = schema.object({
name: schema.string(),
description: schema.string(),
usesPublicBaseUrl: schema.maybe(schema.boolean()),
});

const actionGroupSchema = schema.object({
id: schema.string(),
name: schema.string(),
});

export const typesRulesResponseBodySchema = schema.arrayOf(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the schema is missing default_schedule_interval.

schema.object({
action_groups: schema.maybe(schema.arrayOf(actionGroupSchema)),
action_variables: schema.maybe(
schema.object({
context: schema.maybe(schema.arrayOf(actionVariableSchema)),
state: schema.maybe(schema.arrayOf(actionVariableSchema)),
params: schema.maybe(schema.arrayOf(actionVariableSchema)),
})
),
alerts: schema.maybe(
schema.object({
context: schema.string(),
mappings: schema.maybe(
schema.object({
dynamic: schema.maybe(schema.oneOf([schema.literal(false), schema.literal('strict')])),
fieldMap: schema.recordOf(schema.string(), schema.any()),
shouldWrite: schema.maybe(schema.boolean()),
useEcs: schema.maybe(schema.boolean()),
})
),
})
),
authorized_consumers: schema.recordOf(
schema.string(),
schema.object({ read: schema.boolean(), all: schema.boolean() })
),
category: schema.string(),
default_action_group_id: schema.string(),
default_schedule_interval: schema.maybe(schema.string()),
does_set_recovery_context: schema.maybe(schema.boolean()),
enabled_in_license: schema.boolean(),
fieldsForAAD: schema.maybe(schema.arrayOf(schema.string())),
has_alerts_mappings: schema.boolean(),
has_fields_for_a_a_d: schema.boolean(),
id: schema.string(),
is_exportable: schema.boolean(),
minimum_license_required: schema.oneOf([
schema.literal('basic'),
schema.literal('gold'),
schema.literal('platinum'),
schema.literal('standard'),
schema.literal('enterprise'),
schema.literal('trial'),
]),
name: schema.string(),
producer: schema.string(),
recovery_action_group: actionGroupSchema,
rule_task_timeout: schema.maybe(schema.string()),
})
);

export const typesRulesResponseSchema = schema.object({
body: typesRulesResponseBodySchema,
});
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';
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* 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 { typesRulesResponseSchemaV1, typesRulesResponseBodySchemaV1 } from '..';

export type TypesRulesResponse = TypeOf<typeof typesRulesResponseSchemaV1>;
export type TypesRulesResponseBody = TypeOf<typeof typesRulesResponseBodySchemaV1>;
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
* 2.0.
*/

import { WriteOperations, ReadOperations, AlertingAuthorizationEntity } from '../../authorization';
import { RulesClientContext } from '../types';
import {
WriteOperations,
ReadOperations,
AlertingAuthorizationEntity,
} from '../../../../authorization';
import { RulesClientContext } from '../../../../rules_client/types';

export async function listRuleTypes(context: RulesClientContext) {
return await context.authorization.filterByRuleTypeAuthorization(
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/alerting/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { getRuleExecutionKPIRoute } from './get_rule_execution_kpi';
import { getRuleStateRoute } from './get_rule_state';
import { healthRoute } from './health';
import { resolveRuleRoute } from './rule/apis/resolve';
import { ruleTypesRoute } from './rule_types';
import { ruleTypesRoute } from './rule/apis/list_types/rule_types';
import { muteAllRuleRoute } from './rule/apis/mute_all/mute_all_rule';
import { muteAlertRoute } from './rule/apis/mute_alert/mute_alert';
import { unmuteAllRuleRoute } from './unmute_all_rule';
Expand Down
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 { ruleTypesRoute } from './rule_types';
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@

import { ruleTypesRoute } from './rule_types';
import { httpServiceMock } from '@kbn/core/server/mocks';
import { licenseStateMock } from '../lib/license_state.mock';
import { verifyApiAccess } from '../lib/license_api_access';
import { mockHandlerArguments } from './_mock_handler_arguments';
import { rulesClientMock } from '../rules_client.mock';
import { RecoveredActionGroup } from '../../common';
import { RegistryAlertTypeWithAuth } from '../authorization';
import { AsApiContract } from './lib';
import { licenseStateMock } from '../../../../lib/license_state.mock';
import { verifyApiAccess } from '../../../../lib/license_api_access';
import { mockHandlerArguments } from '../../../_mock_handler_arguments';
import { rulesClientMock } from '../../../../rules_client.mock';
import { RecoveredActionGroup } from '../../../../../common';
import { RegistryAlertTypeWithAuth } from '../../../../authorization';
import { AsApiContract } from '../../../lib';

const rulesClient = rulesClientMock.create();

jest.mock('../lib/license_api_access', () => ({
jest.mock('../../../../lib/license_api_access', () => ({
verifyApiAccess: jest.fn(),
}));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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 { IRouter } from '@kbn/core/server';
import { TypesRulesResponseBodyV1 } from '../../../../../common/routes/rule/apis/list_types';
import { ILicenseState } from '../../../../lib';
import { verifyAccessAndContext } from '../../../lib';
import { AlertingRequestHandlerContext, BASE_ALERTING_API_PATH } from '../../../../types';
import { transformRuleTypesResponseV1 } from './transforms';

export const ruleTypesRoute = (
router: IRouter<AlertingRequestHandlerContext>,
licenseState: ILicenseState
) => {
router.get(
{
path: `${BASE_ALERTING_API_PATH}/rule_types`,
options: {
access: 'public',
summary: `Get the rule types`,
tags: ['oas-tag:alerting'],
},
validate: {},
},
router.handleLegacyErrors(
verifyAccessAndContext(licenseState, async function (context, req, res) {
const rulesClient = (await context.alerting).getRulesClient();
const ruleTypes = await rulesClient.listRuleTypes();

const responseBody: TypesRulesResponseBodyV1 = transformRuleTypesResponseV1(ruleTypes);

return res.ok({
body: responseBody,
});
})
)
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* 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 { transformRuleTypesResponse } from './transform_rule_types_response/latest';
export { transformRuleTypesResponse as transformRuleTypesResponseV1 } from './transform_rule_types_response/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';
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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 { isBoolean } from 'lodash/fp';
import { RegistryAlertTypeWithAuth } from '../../../../../../authorization';
import type { TypesRulesResponseBodyV1 } from '../../../../../../../common/routes/rule/apis/list_types';

export const transformRuleTypesResponse = (
ruleTypes: Set<RegistryAlertTypeWithAuth>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I talked with @JiaweiWu about RegistryAlertTypeWithAuth Ideally it would be moved to be an application type but it's a complete mess. As long as it's not been used anywhere else, we should be fine. Wdyt?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that it should be an application type and the rule client returns a more suitable structure. I would say it is out of the scope of this PR.

): TypesRulesResponseBodyV1 => {
return Array.from(ruleTypes).map((ruleType: RegistryAlertTypeWithAuth) => {
return {
...(ruleType.actionGroups ? { action_groups: ruleType.actionGroups } : {}),
...(ruleType.actionVariables ? { action_variables: ruleType.actionVariables } : {}),
...(ruleType.alerts ? { alerts: ruleType.alerts } : {}),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one throws an error: Property 'alerts' does not exist on type 'RegistryAlertTypeWithAuth'. I'll add it to the type (see x-pack/plugins/alerting/server/rule_type_registry.ts changes), just let me know if you have more info/know a better option

authorized_consumers: ruleType.authorizedConsumers,
category: ruleType.category,
default_action_group_id: ruleType.defaultActionGroupId,
...(ruleType.defaultScheduleInterval
? { default_schedule_interval: ruleType.defaultScheduleInterval }
: {}),
...(isBoolean(ruleType.doesSetRecoveryContext)
? { does_set_recovery_context: ruleType.doesSetRecoveryContext }
: {}),
enabled_in_license: ruleType.enabledInLicense,
...(ruleType.fieldsForAAD ? { fieldsForAAD: ruleType.fieldsForAAD } : {}),
has_alerts_mappings: ruleType.hasAlertsMappings,
has_fields_for_a_a_d: ruleType.hasFieldsForAAD,
id: ruleType.id,
is_exportable: ruleType.isExportable,
minimum_license_required: ruleType.minimumLicenseRequired,
name: ruleType.name,
producer: ruleType.producer,
recovery_action_group: ruleType.recoveryActionGroup,
...(ruleType.ruleTaskTimeout ? { rule_task_timeout: ruleType.ruleTaskTimeout } : {}),
};
});
};
75 changes: 0 additions & 75 deletions x-pack/plugins/alerting/server/routes/rule_types.ts

This file was deleted.

1 change: 1 addition & 0 deletions x-pack/plugins/alerting/server/rule_type_registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export interface RegistryRuleType
| 'defaultScheduleInterval'
| 'doesSetRecoveryContext'
| 'fieldsForAAD'
| 'alerts'
> {
id: string;
enabledInLicense: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ import { muteAll } from '../application/rule/methods/mute_all';
import { unmuteAll } from './methods/unmute_all';
import { unmuteInstance } from '../application/rule/methods/unmute_alert/unmute_instance';
import { runSoon } from './methods/run_soon';
import { listRuleTypes } from './methods/list_rule_types';
import { listRuleTypes } from '../application/rule/methods/rule_types/rule_types';
import { getScheduleFrequency } from '../application/rule/methods/get_schedule_frequency/get_schedule_frequency';
import {
bulkUntrackAlerts,
Expand Down