import httpProxy from 'http-proxy' ; import expect from '@kbn/expect' ; import { getHttpProxyServer } from '../../../../common/lib/get_proxy_server' ; import { FtrProviderContext } from '../../../../common/ftr_provider_context' ; import { getExternalServiceSimulatorPath , ExternalServiceSimulator , } from '../../../../common/fixtures/plugins/actions_simulators/server/plugin' ; export default function resilientTest ( { getService } ) { const supertest = getService ( 'supertest' ) ; const kibanaServer = getService ( 'kibanaServer' ) ; const configService = getService ( 'config' ) ; const mockResilient = { config : { apiUrl : 'www.resilientisinkibanaactions.com' , orgId : '201' , } , secrets : { apiKeyId : 'key' , apiKeySecret : 'secret' , } , params : { subAction : 'pushToService' , subActionParams : { incident : { name : 'a title' , description : 'a description' , incidentTypes : [ 1001 ] , severityCode : 6 , externalId : null , } , comments : [ { comment : 'first comment' , commentId : '456' , } , ] , } , } , } ; let resilientSimulatorURL = '<could not determine kibana url>' ; describe ( 'IBM Resilient' , ( ) => { before ( ( ) => { resilientSimulatorURL = kibanaServer . resolveUrl ( getExternalServiceSimulatorPath ( ExternalServiceSimulator . RESILIENT ) ) ; } ) ; describe ( 'IBM Resilient - Action Creation' , ( ) => { it ( 'should return 200 when creating a ibm resilient action successfully' , async ( ) => { const { body : createdAction } = await supertest . post ( '/api/actions/connector' ) . set ( 'kbn-xsrf' , 'foo' ) . send ( { name : 'An IBM Resilient action' , connector_type_id : '.resilient' , config : { ... mockResilient . config , apiUrl : resilientSimulatorURL , } , secrets : mockResilient . secrets , } ) . expect ( 200 ) ; expect ( createdAction ) . to . eql ( { id : createdAction . id , is_preconfigured : false , name : 'An IBM Resilient action' , connector_type_id : '.resilient' , is_missing_secrets : false , config : { apiUrl : resilientSimulatorURL , orgId : mockResilient . config . orgId , } , } ) ; const { body : fetchedAction } = await supertest . get ( `/api/actions/connector/${ createdAction . id }` ) . expect ( 200 ) ; expect ( fetchedAction ) . to . eql ( { id : fetchedAction . id , is_preconfigured : false , name : 'An IBM Resilient action' , connector_type_id : '.resilient' , is_missing_secrets : false , config : { apiUrl : resilientSimulatorURL , orgId : mockResilient . config . orgId , } , } ) ; } ) ; it ( 'should respond with a 400 Bad Request when creating a ibm resilient action with no apiUrl' , async ( ) => { await supertest . post ( '/api/actions/connector' ) . set ( 'kbn-xsrf' , 'foo' ) . send ( { name : 'An IBM Resilient' , connector_type_id : '.resilient' , config : { orgId : '201' } , } ) . expect ( 400 ) . then ( ( resp ) => { expect ( resp . body ) . to . eql ( { statusCode : 400 , error : 'Bad Request' , message : 'error validating action type config: [apiUrl]: expected value of type [string] but got [undefined]' , } ) ; } ) ; } ) ; it ( 'should respond with a 400 Bad Request when creating a ibm resilient action with no orgId' , async ( ) => { await supertest . post ( '/api/actions/connector' ) . set ( 'kbn-xsrf' , 'foo' ) . send ( { name : 'An IBM Resilient' , connector_type_id : '.resilient' , config : { apiUrl : resilientSimulatorURL } , } ) . expect ( 400 ) . then ( ( resp ) => { expect ( resp . body ) . to . eql ( { statusCode : 400 , error : 'Bad Request' , message : 'error validating action type config: [orgId]: expected value of type [string] but got [undefined]' , } ) ; } ) ; } ) ; it ( 'should respond with a 400 Bad Request when creating a ibm resilient action with a not present in allowedHosts apiUrl' , async ( ) => { await supertest . post ( '/api/actions/connector' ) . set ( 'kbn-xsrf' , 'foo' ) . send ( { name : 'An IBM Resilient' , connector_type_id : '.resilient' , config : { apiUrl : 'http://resilient.mynonexistent.com' , orgId : mockResilient . config . orgId , } , secrets : mockResilient . secrets , } ) . expect ( 400 ) . then ( ( resp ) => { expect ( resp . body ) . to . eql ( { statusCode : 400 , error : 'Bad Request' , message : 'error validating action type config: error configuring connector action: target url "http://resilient.mynonexistent.com" is not added to the Kibana config xpack.actions.allowedHosts' , } ) ; } ) ; } ) ; it ( 'should respond with a 400 Bad Request when creating a ibm resilient action without secrets' , async ( ) => { await supertest . post ( '/api/actions/connector' ) . set ( 'kbn-xsrf' , 'foo' ) . send ( { name : 'An IBM Resilient' , connector_type_id : '.resilient' , config : { apiUrl : resilientSimulatorURL , orgId : mockResilient . config . orgId , } , } ) . expect ( 400 ) . then ( ( resp ) => { expect ( resp . body ) . to . eql ( { statusCode : 400 , error : 'Bad Request' , message : 'error validating action type secrets: [apiKeyId]: expected value of type [string] but got [undefined]' , } ) ; } ) ; } ) ; } ) ; describe ( 'IBM Resilient - Executor' , ( ) => { let simulatedActionId ; let proxyServer ; let proxyHaveBeenCalled = false ; before ( async ( ) => { const { body } = await supertest . post ( '/api/actions/connector' ) . set ( 'kbn-xsrf' , 'foo' ) . send ( { name : 'A ibm resilient simulator' , connector_type_id : '.resilient' , config : { apiUrl : resilientSimulatorURL , orgId : mockResilient . config . orgId , } , secrets : mockResilient . secrets , } ) ; simulatedActionId = body . id ; proxyServer = await getHttpProxyServer ( kibanaServer . resolveUrl ( '/' ) , configService . get ( 'kbnTestServer.serverArgs' ) , ( ) => { proxyHaveBeenCalled = true ; } ) ; } ) ; describe ( 'Validation' , ( ) => { it ( 'should handle failing with a simulated success without action' , async ( ) => { await supertest . post ( `/api/actions/connector/${ simulatedActionId }/_execute` ) . set ( 'kbn-xsrf' , 'foo' ) . send ( { params : { } , } ) . then ( ( resp ) => { expect ( Object . keys ( resp . body ) ) . to . eql ( [ 'status' , 'message' , 'retry' , 'connector_id' ] ) ; expect ( resp . body . connector_id ) . to . eql ( simulatedActionId ) ; expect ( resp . body . status ) . to . eql ( 'error' ) ; } ) ; } ) ; it ( 'should handle failing with a simulated success without unsupported action' , async ( ) => { await supertest . post ( `/api/actions/connector/${ simulatedActionId }/_execute` ) . set ( 'kbn-xsrf' , 'foo' ) . send ( { params : { subAction : 'non-supported' } , } ) . then ( ( resp ) => { expect ( resp . body ) . to . eql ( { connector_id : simulatedActionId , status : 'error' , retry : false , message : 'error validating action params: types that failed validation:\n- [0.subAction]: expected value to equal [getFields]\n- [1.subAction]: expected value to equal [getIncident]\n- [2.subAction]: expected value to equal [handshake]\n- [3.subAction]: expected value to equal [pushToService]\n- [4.subAction]: expected value to equal [incidentTypes]\n- [5.subAction]: expected value to equal [severity]' , } ) ; } ) ; } ) ; it ( 'should handle failing with a simulated success without subActionParams' , async ( ) => { await supertest . post ( `/api/actions/connector/${ simulatedActionId }/_execute` ) . set ( 'kbn-xsrf' , 'foo' ) . send ( { params : { subAction : 'pushToService' } , } ) . then ( ( resp ) => { expect ( resp . body ) . to . eql ( { connector_id : simulatedActionId , status : 'error' , retry : false , message : 'error validating action params: types that failed validation:\n- [0.subAction]: expected value to equal [getFields]\n- [1.subAction]: expected value to equal [getIncident]\n- [2.subAction]: expected value to equal [handshake]\n- [3.subActionParams.incident.name]: expected value of type [string] but got [undefined]\n- [4.subAction]: expected value to equal [incidentTypes]\n- [5.subAction]: expected value to equal [severity]' , } ) ; } ) ; } ) ; it ( 'should handle failing with a simulated success without title' , async ( ) => { await supertest . post ( `/api/actions/connector/${ simulatedActionId }/_execute` ) . set ( 'kbn-xsrf' , 'foo' ) . send ( { params : { ... mockResilient . params , subActionParams : { incident : { description : 'success' , } , comments : [ ] , } , } , } ) . then ( ( resp ) => { expect ( resp . body ) . to . eql ( { connector_id : simulatedActionId , status : 'error' , retry : false , message : 'error validating action params: types that failed validation:\n- [0.subAction]: expected value to equal [getFields]\n- [1.subAction]: expected value to equal [getIncident]\n- [2.subAction]: expected value to equal [handshake]\n- [3.subActionParams.incident.name]: expected value of type [string] but got [undefined]\n- [4.subAction]: expected value to equal [incidentTypes]\n- [5.subAction]: expected value to equal [severity]' , } ) ; } ) ; } ) ; it ( 'should handle failing with a simulated success without commentId' , async ( ) => { await supertest . post ( `/api/actions/connector/${ simulatedActionId }/_execute` ) . set ( 'kbn-xsrf' , 'foo' ) . send ( { params : { ... mockResilient . params , subActionParams : { incident : { ... mockResilient . params . subActionParams . incident , name : 'success' , } , comments : [ { comment : 'comment' } ] , } , } , } ) . then ( ( resp ) => { expect ( resp . body ) . to . eql ( { connector_id : simulatedActionId , status : 'error' , retry : false , message : 'error validating action params: types that failed validation:\n- [0.subAction]: expected value to equal [getFields]\n- [1.subAction]: expected value to equal [getIncident]\n- [2.subAction]: expected value to equal [handshake]\n- [3.subActionParams.comments]: types that failed validation:\n - [subActionParams.comments.0.0.commentId]: expected value of type [string] but got [undefined]\n - [subActionParams.comments.1]: expected value to equal [null]\n- [4.subAction]: expected value to equal [incidentTypes]\n- [5.subAction]: expected value to equal [severity]' , } ) ; } ) ; } ) ; it ( 'should handle failing with a simulated success without comment message' , async ( ) => { await supertest . post ( `/api/actions/connector/${ simulatedActionId }/_execute` ) . set ( 'kbn-xsrf' , 'foo' ) . send ( { params : { ... mockResilient . params , subActionParams : { incident : { ... mockResilient . params . subActionParams . incident , name : 'success' , } , comments : [ { commentId : 'success' } ] , } , } , } ) . then ( ( resp ) => { expect ( resp . body ) . to . eql ( { connector_id : simulatedActionId , status : 'error' , retry : false , message : 'error validating action params: types that failed validation:\n- [0.subAction]: expected value to equal [getFields]\n- [1.subAction]: expected value to equal [getIncident]\n- [2.subAction]: expected value to equal [handshake]\n- [3.subActionParams.comments]: types that failed validation:\n - [subActionParams.comments.0.0.comment]: expected value of type [string] but got [undefined]\n - [subActionParams.comments.1]: expected value to equal [null]\n- [4.subAction]: expected value to equal [incidentTypes]\n- [5.subAction]: expected value to equal [severity]' , } ) ; } ) ; } ) ; } ) ; describe ( 'Execution' , ( ) => { it ( 'should handle creating an incident without comments' , async ( ) => { const { body } = await supertest . post ( `/api/actions/connector/${ simulatedActionId }/_execute` ) . set ( 'kbn-xsrf' , 'foo' ) . send ( { params : { ... mockResilient . params , subActionParams : { ... mockResilient . params . subActionParams , comments : null , } , } , } ) . expect ( 200 ) ; expect ( proxyHaveBeenCalled ) . to . equal ( true ) ; expect ( body ) . to . eql ( { status : 'ok' , connector_id : simulatedActionId , data : { id : '123' , title : '123' , pushedDate : '2020-05-13T17:44:34.472Z' , url : `${ resilientSimulatorURL }/#incidents/123` , } , } ) ; } ) ; } ) ; after ( ( ) => { if ( proxyServer ) { proxyServer . close ( ) ; } } ) ; } ) ; } ) ; }