Skip to content

Commit

Permalink
Merge branch 'system_actions_mvp' of github.com:elastic/kibana into s…
Browse files Browse the repository at this point in the history
…ystem_actions_mvp
  • Loading branch information
guskovaue committed Oct 26, 2023
2 parents 861697c + 8dedac8 commit 645e45c
Show file tree
Hide file tree
Showing 58 changed files with 2,458 additions and 535 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ export class ActionsClient {
/**
* Get all system connectors
*/
public async getAllSystemConnectors(): Promise<FindConnectorResult[]> {
public async getAllSystemConnectors(): Promise<ConnectorWithExtraFindData[]> {
return getAllSystemConnectors({ context: this.context });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export async function getAllSystemConnectors({
context,
}: {
context: GetAllParams['context'];
}): Promise<FindConnectorResult[]> {
}): Promise<ConnectorWithExtraFindData[]> {
try {
await context.authorization.ensureAuthorized({ operation: 'get' });
} catch (error) {
Expand Down
4 changes: 3 additions & 1 deletion x-pack/plugins/actions/server/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ import {
} from '@kbn/core/server/mocks';
import { encryptedSavedObjectsMock } from '@kbn/encrypted-saved-objects-plugin/server/mocks';
import { Logger } from '@kbn/core/server';
import { actionsClientMock } from './actions_client/actions_client.mock';
import { actionsClientMock, ActionsClientMock } from './actions_client/actions_client.mock';
import { PluginSetupContract, PluginStartContract, renderActionParameterTemplates } from './plugin';
import { Services } from './types';
import { actionsAuthorizationMock } from './authorization/actions_authorization.mock';
import { ConnectorTokenClient } from './lib/connector_token_client';
import { unsecuredActionsClientMock } from './unsecured_actions_client/unsecured_actions_client.mock';
export { actionsAuthorizationMock };
export { actionsClientMock };
export type { ActionsClientMock };

const logger = loggingSystemMock.create().get() as jest.Mocked<Logger>;

const createSetupMock = () => {
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/alerting/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export * from './iso_weekdays';
export * from './saved_objects/rules/mappings';
export * from './rule_circuit_breaker_error_message';

export { isSystemAction } from './system_actions/is_system_action';

export type {
MaintenanceWindowModificationMetadata,
DateRange,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const actionAlertsFilterSchema = schema.object({

export const actionSchema = schema.object({
uuid: schema.maybe(schema.string()),
group: schema.string(),
group: schema.maybe(schema.string()),
id: schema.string(),
actionTypeId: schema.maybe(schema.string()),
params: schema.recordOf(schema.string(), schema.maybe(schema.any()), { defaultValue: {} }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const actionAlertsFilterSchema = schema.object({

const actionSchema = schema.object({
uuid: schema.maybe(schema.string()),
group: schema.string(),
group: schema.maybe(schema.string()),
id: schema.string(),
connector_type_id: schema.string(),
params: actionParamsSchema,
Expand Down
24 changes: 18 additions & 6 deletions x-pack/plugins/alerting/common/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ export interface RuleExecutionStatus {
export type RuleActionParams = SavedObjectAttributes;
export type RuleActionParam = SavedObjectAttribute;

export interface RuleActionFrequency extends SavedObjectAttributes {
export interface RuleActionFrequency {
summary: boolean;
notifyWhen: RuleNotifyWhenType;
throttle: string | null;
}

export interface AlertsFilterTimeframe extends SavedObjectAttributes {
export interface AlertsFilterTimeframe {
days: IsoWeekday[];
timezone: string;
hours: {
Expand All @@ -97,7 +97,7 @@ export interface AlertsFilterTimeframe extends SavedObjectAttributes {
};
}

export interface AlertsFilter extends SavedObjectAttributes {
export interface AlertsFilter {
query?: {
kql: string;
filters: Filter[];
Expand All @@ -121,17 +121,27 @@ export const RuleActionTypes = {

export type RuleActionTypes = typeof RuleActionTypes[keyof typeof RuleActionTypes];

export interface RuleAction {
export interface RuleDefaultAction {
uuid?: string;
group: string;
id: string;
actionTypeId: string;
params: RuleActionParams;
frequency?: RuleActionFrequency;
alertsFilter?: AlertsFilter;
type?: typeof RuleActionTypes.DEFAULT;
type: typeof RuleActionTypes.DEFAULT;
}

export interface RuleSystemAction {
uuid?: string;
id: string;
actionTypeId: string;
params: RuleActionParams;
type: typeof RuleActionTypes.SYSTEM;
}

export type RuleAction = RuleDefaultAction | RuleSystemAction;

export interface RuleLastRun {
outcome: RuleLastRunOutcomes;
outcomeOrder?: number;
Expand Down Expand Up @@ -195,10 +205,12 @@ export interface SanitizedAlertsFilter extends AlertsFilter {
timeframe?: AlertsFilterTimeframe;
}

export type SanitizedRuleAction = Omit<RuleAction, 'alertsFilter'> & {
export type SanitizedDefaultRuleAction = Omit<RuleDefaultAction, 'alertsFilter'> & {
alertsFilter?: SanitizedAlertsFilter;
};

export type SanitizedRuleAction = SanitizedDefaultRuleAction | RuleSystemAction;

export type SanitizedRule<Params extends RuleTypeParams = never> = Omit<
Rule<Params>,
'apiKey' | 'actions'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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 { RuleSystemAction, RuleActionTypes, RuleDefaultAction } from '../rule';
import { isSystemAction } from './is_system_action';

describe('isSystemAction', () => {
const defaultAction: RuleDefaultAction = {
actionTypeId: '.test',
uuid: '111',
group: 'default',
id: '1',
params: {},
type: RuleActionTypes.DEFAULT,
};

const systemAction: RuleSystemAction = {
id: '1',
uuid: '123',
params: { 'not-exist': 'test' },
actionTypeId: '.test',
type: RuleActionTypes.SYSTEM,
};

it('returns true if it is a system action', () => {
expect(isSystemAction(systemAction)).toBe(true);
});

it('returns false if it is not a system action', () => {
expect(isSystemAction(defaultAction)).toBe(false);
});
});
17 changes: 17 additions & 0 deletions x-pack/plugins/alerting/common/system_actions/is_system_action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* 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 { AsApiContract } from '@kbn/actions-plugin/common';
import { RuleAction, RuleSystemAction, RuleActionTypes } from '../rule';

type GetSystemActionType<T> = T extends RuleAction
? RuleSystemAction
: AsApiContract<RuleSystemAction>;

export const isSystemAction = (
action: RuleAction | AsApiContract<RuleAction>
): action is GetSystemActionType<typeof action> => action.type === RuleActionTypes.SYSTEM;
Loading

0 comments on commit 645e45c

Please sign in to comment.