import { Space , User } from '../types' ; import { ObjectRemover } from './object_remover' ; import { getUrlPrefix } from './space_test_utils' ; import { ES_TEST_INDEX_NAME } from './es_test_index_tool' ; import { getTestAlertData } from './get_test_alert_data' ; export interface AlertUtilsOpts { user ? : User ; space : Space ; supertestWithoutAuth : any ; indexRecordActionId ? : string ; objectRemover ? : ObjectRemover ; } export interface CreateAlertWithActionOpts { indexRecordActionId ? : string ; objectRemover ? : ObjectRemover ; overwrites ? : Record < string , any > ; reference : string ; } export interface CreateNoopAlertOpts { objectRemover ? : ObjectRemover ; overwrites ? : Record < string , any > ; } interface UpdateAlwaysFiringAction { alertId : string ; actionId : string | undefined ; reference : string ; user : User ; overwrites : Record < string , any > ; } export class AlertUtils { private referenceCounter = 1 ; private readonly user ? : User ; private readonly space : Space ; private readonly supertestWithoutAuth : any ; private readonly indexRecordActionId ? : string ; private readonly objectRemover ? : ObjectRemover ; constructor ( { indexRecordActionId , objectRemover , space , supertestWithoutAuth , user , } ) { this . user = user ; this . space = space ; this . objectRemover = objectRemover ; this . indexRecordActionId = indexRecordActionId ; this . supertestWithoutAuth = supertestWithoutAuth ; } public generateReference ( ) { return [ 'alert-utils-ref' , this . referenceCounter ++ , this . user ? this . user . username : '' ] . join ( ':' ) ; } public getEnableRequest ( alertId ) { const request = this . supertestWithoutAuth . post ( `${ getUrlPrefix ( this . space . id ) }/api/alerting/rule/${ alertId }/_enable` ) . set ( 'kbn-xsrf' , 'foo' ) ; if ( this . user ) { return request . auth ( this . user . username , this . user . password ) ; } return request ; } public getDisableRequest ( alertId ) { const request = this . supertestWithoutAuth . post ( `${ getUrlPrefix ( this . space . id ) }/api/alerting/rule/${ alertId }/_disable` ) . set ( 'kbn-xsrf' , 'foo' ) ; if ( this . user ) { return request . auth ( this . user . username , this . user . password ) ; } return request ; } public getMuteAllRequest ( alertId ) { const request = this . supertestWithoutAuth . post ( `${ getUrlPrefix ( this . space . id ) }/api/alerting/rule/${ alertId }/_mute_all` ) . set ( 'kbn-xsrf' , 'foo' ) ; if ( this . user ) { return request . auth ( this . user . username , this . user . password ) ; } return request ; } public getUnmuteAllRequest ( alertId ) { const request = this . supertestWithoutAuth . post ( `${ getUrlPrefix ( this . space . id ) }/api/alerting/rule/${ alertId }/_unmute_all` ) . set ( 'kbn-xsrf' , 'foo' ) ; if ( this . user ) { return request . auth ( this . user . username , this . user . password ) ; } return request ; } public getMuteInstanceRequest ( alertId , instanceId ) { const request = this . supertestWithoutAuth . post ( `${ getUrlPrefix ( this . space . id ) }/api/alerting/rule/${ alertId }/alert/${ instanceId }/_mute` ) . set ( 'kbn-xsrf' , 'foo' ) ; if ( this . user ) { return request . auth ( this . user . username , this . user . password ) ; } return request ; } public getUnmuteInstanceRequest ( alertId , instanceId ) { const request = this . supertestWithoutAuth . post ( `${ getUrlPrefix ( this . space . id ) }/api/alerting/rule/${ alertId }/alert/${ instanceId }/_unmute` ) . set ( 'kbn-xsrf' , 'foo' ) ; if ( this . user ) { return request . auth ( this . user . username , this . user . password ) ; } return request ; } public getUpdateApiKeyRequest ( alertId ) { const request = this . supertestWithoutAuth . post ( `${ getUrlPrefix ( this . space . id ) }/internal/alerting/rule/${ alertId }/_update_api_key` ) . set ( 'kbn-xsrf' , 'foo' ) ; if ( this . user ) { return request . auth ( this . user . username , this . user . password ) ; } return request ; } public async enable ( alertId ) { await this . getEnableRequest ( alertId ) . expect ( 204 , '' ) ; } public async disable ( alertId ) { await this . getDisableRequest ( alertId ) . expect ( 204 , '' ) ; } public async muteAll ( alertId ) { await this . getMuteAllRequest ( alertId ) . expect ( 204 , '' ) ; } public async unmuteAll ( alertId ) { await this . getUnmuteAllRequest ( alertId ) . expect ( 204 , '' ) ; } public async muteInstance ( alertId , instanceId ) { await this . getMuteInstanceRequest ( alertId , instanceId ) . expect ( 204 , '' ) ; } public async unmuteInstance ( alertId , instanceId ) { await this . getUnmuteInstanceRequest ( alertId , instanceId ) . expect ( 204 , '' ) ; } public async updateApiKey ( alertId ) { await this . getUpdateApiKeyRequest ( alertId ) . expect ( 204 , '' ) ; } public async createAlwaysFiringAction ( { objectRemover , overwrites = { } , indexRecordActionId , reference , } ) { const objRemover = objectRemover || this . objectRemover ; const actionId = indexRecordActionId || this . indexRecordActionId ; if ( ! objRemover ) { throw new Error ( 'objectRemover is required' ) ; } if ( ! actionId ) { throw new Error ( 'indexRecordActionId is required ' ) ; } let request = this . supertestWithoutAuth . post ( `${ getUrlPrefix ( this . space . id ) }/api/alerting/rule` ) . set ( 'kbn-xsrf' , 'foo' ) ; if ( this . user ) { request = request . auth ( this . user . username , this . user . password ) ; } const alertBody = getDefaultAlwaysFiringAlertData ( reference , actionId ) ; const response = await request . send ( { ... alertBody , ... overwrites } ) ; if ( response . statusCode === 200 ) { objRemover . add ( this . space . id , response . body . id , 'rule' , 'alerting' ) ; } return response ; } public async updateAlwaysFiringAction ( { alertId , actionId , reference , user , overwrites = { } , } ) { actionId = actionId || this . indexRecordActionId ; if ( ! actionId ) { throw new Error ( 'actionId is required ' ) ; } const request = this . supertestWithoutAuth . put ( `${ getUrlPrefix ( this . space . id ) }/api/alerting/rule/${ alertId }` ) . set ( 'kbn-xsrf' , 'foo' ) . auth ( user . username , user . password ) ; const { rule_type_id : alertTypeId , enabled , consumer , ... alertBody } = getDefaultAlwaysFiringAlertData ( reference , actionId ) ; const response = await request . send ( { ... alertBody , ... overwrites } ) ; return response ; } public async createAlwaysFailingAction ( { objectRemover , overwrites = { } , indexRecordActionId , reference , } ) { const objRemover = objectRemover || this . objectRemover ; const actionId = indexRecordActionId || this . indexRecordActionId ; if ( ! objRemover ) { throw new Error ( 'objectRemover is required' ) ; } if ( ! actionId ) { throw new Error ( 'indexRecordActionId is required ' ) ; } let request = this . supertestWithoutAuth . post ( `${ getUrlPrefix ( this . space . id ) }/api/alerting/rule` ) . set ( 'kbn-xsrf' , 'foo' ) ; if ( this . user ) { request = request . auth ( this . user . username , this . user . password ) ; } const response = await request . send ( { enabled : true , name : 'fail' , schedule : { interval : '30s' } , throttle : '30s' , tags : [ ] , rule_type_id : 'test.failing' , consumer : 'alertsFixture' , params : { index : ES_TEST_INDEX_NAME , reference , } , notify_when : 'onActiveAlert' , actions : [ ] , ... overwrites , } ) ; if ( response . statusCode === 200 ) { objRemover . add ( this . space . id , response . body . id , 'rule' , 'alerting' ) ; } return response ; } public replaceApiKeys ( id ) { let request = this . supertestWithoutAuth . put ( `/api/alerts_fixture/${ id }/replace_api_key` ) . set ( 'kbn-xsrf' , 'foo' ) ; if ( this . user ) { request = request . auth ( this . user . username , this . user . password ) ; } return request . send ( { spaceId : this . space . id } ) ; } public async createNoopAlert ( { objectRemover , overwrites = { } } ) { const objRemover = objectRemover || this . objectRemover ; if ( ! objRemover ) { throw new Error ( 'objectRemover is required' ) ; } let request = this . supertestWithoutAuth . post ( `${ getUrlPrefix ( this . space . id ) }/api/alerting/rule` ) . set ( 'kbn-xsrf' , 'foo' ) ; if ( this . user ) { request = request . auth ( this . user . username , this . user . password ) ; } const response = await request . send ( { ... getTestAlertData ( ) , ... overwrites , } ) ; if ( response . statusCode === 200 ) { objRemover . add ( this . space . id , response . body . id , 'rule' , 'alerting' ) ; } return response ; } } export function getConsumerUnauthorizedErrorMessage ( operation , alertType , consumer ) { return `Unauthorized to ${ operation } a "${ alertType }" rule for "${ consumer }"` ; } export function getProducerUnauthorizedErrorMessage ( operation , alertType , producer ) { return `Unauthorized to ${ operation } a "${ alertType }" rule by "${ producer }"` ; } function getDefaultAlwaysFiringAlertData ( reference , actionId ) { const messageTemplate = `
alertId: {{alertId}},
alertName: {{alertName}},
spaceId: {{spaceId}},
tags: {{tags}},
alertInstanceId: {{alertInstanceId}},
alertActionGroup: {{alertActionGroup}},
instanceContextValue: {{context.instanceContextValue}},
instanceStateValue: {{state.instanceStateValue}}
` . trim ( ) ; return { enabled : true , name : 'abc' , schedule : { interval : '1m' } , throttle : '1m' , tags : [ 'tag-A' , 'tag-B' ] , rule_type_id : 'test.always-firing' , consumer : 'alertsFixture' , params : { index : ES_TEST_INDEX_NAME , reference , } , notify_when : 'onActiveAlert' , actions : [ { group : 'default' , id : actionId , params : { index : ES_TEST_INDEX_NAME , reference , message : messageTemplate , } , } , ] , } ; }