import path from 'path' ; import getPort from 'get-port' ; import fs from 'fs' ; import { CA_CERT_PATH } from '@kbn/dev-utils' ; import { FtrConfigProviderContext } from '@kbn/test' ; import { services } from './services' ; import { getAllExternalServiceSimulatorPaths } from './fixtures/plugins/actions_simulators/server/plugin' ; import { getTlsWebhookServerUrls } from './lib/get_tls_webhook_servers' ; interface CreateTestConfigOptions { license : string ; disabledPlugins ? : string [ ] ; ssl ? : boolean ; enableActionsProxy : boolean ; verificationMode ? : 'full' | 'none' | 'certificate' ; publicBaseUrl ? : boolean ; preconfiguredAlertHistoryEsIndex ? : boolean ; customizeLocalHostSsl ? : boolean ; rejectUnauthorized ? : boolean ; } const enabledActionTypes = [ '.email' , '.index' , '.pagerduty' , '.swimlane' , '.server-log' , '.servicenow' , '.servicenow-sir' , '.servicenow-itom' , '.jira' , '.resilient' , '.slack' , '.webhook' , 'test.authorization' , 'test.failing' , 'test.index-record' , 'test.noop' , 'test.delayed' , 'test.rate-limit' , 'test.no-attempts-rate-limit' , 'test.throw' , 'test.excluded' , ] ; export function createTestConfig ( name , options ) { const { license = 'trial' , disabledPlugins = [ ] , ssl = false , verificationMode = 'full' , preconfiguredAlertHistoryEsIndex = false , customizeLocalHostSsl = false , rejectUnauthorized = true , } = options ; return async ( { readConfigFile } ) => { const xPackApiIntegrationTestsConfig = await readConfigFile ( require . resolve ( '../../api_integration/config.ts' ) ) ; const servers = { ... xPackApiIntegrationTestsConfig . get ( 'servers' ) , elasticsearch : { ... xPackApiIntegrationTestsConfig . get ( 'servers.elasticsearch' ) , protocol : ssl ? 'https' : 'http' , } , } ; const allFiles = fs . readdirSync ( path . resolve ( __dirname , 'fixtures' , 'plugins' ) ) ; const plugins = allFiles . filter ( ( file ) => fs . statSync ( path . resolve ( __dirname , 'fixtures' , 'plugins' , file ) ) . isDirectory ( ) ) ; const proxyPort = process . env . ALERTING_PROXY_PORT ?? ( await getPort ( { port : getPort . makeRange ( 6200 , 6299 ) } ) ) ; const tlsWebhookServers = await getTlsWebhookServerUrls ( 6300 , 6399 ) ; const proxyHosts = [ 'localhost' , 'some.non.existent.com' ] ; const actionsProxyUrl = options . enableActionsProxy ? [ `--xpack.actions.proxyUrl=http://localhost:${ proxyPort }` , `--xpack.actions.proxyOnlyHosts=${ JSON . stringify ( proxyHosts ) }` , '--xpack.actions.proxyRejectUnauthorizedCertificates=false' , ] : [ `--xpack.actions.proxyUrl=http://elastic.co` , `--xpack.actions.proxyBypassHosts=${ JSON . stringify ( proxyHosts ) }` , ] ; const customHostSettingsValue = [ { url : tlsWebhookServers . rejectUnauthorizedFalse , ssl : { verificationMode : 'none' , } , } , { url : tlsWebhookServers . rejectUnauthorizedTrue , ssl : { verificationMode : 'full' , } , } , { url : tlsWebhookServers . caFile , ssl : { verificationMode : 'certificate' , certificateAuthoritiesFiles : [ CA_CERT_PATH ] , } , } , ] ; const customHostSettings = customizeLocalHostSsl ? [ `--xpack.actions.customHostSettings=${ JSON . stringify ( customHostSettingsValue ) }` ] : [ ] ; return { testFiles : [ require . resolve ( `../${ name }/tests/` ) ] , servers , services , junit : { reportName : 'X-Pack Alerting API Integration Tests' , } , esTestCluster : { ... xPackApiIntegrationTestsConfig . get ( 'esTestCluster' ) , license , ssl , serverArgs : [ `xpack.license.self_generated.type=${ license }` , `xpack.security.enabled=${ ! disabledPlugins . includes ( 'security' ) && [ 'trial' , 'basic' ] . includes ( license ) }` , ] , } , kbnTestServer : { ... xPackApiIntegrationTestsConfig . get ( 'kbnTestServer' ) , serverArgs : [ ... xPackApiIntegrationTestsConfig . get ( 'kbnTestServer.serverArgs' ) , ... ( options . publicBaseUrl ? [ '--server.publicBaseUrl=https://localhost:5601' ] : [ ] ) , `--xpack.actions.allowedHosts=${ JSON . stringify ( [ 'localhost' , 'some.non.existent.com' , 'smtp.live.com' , ] ) }` , '--xpack.encryptedSavedObjects.encryptionKey="wuGNaIhoMpk5sO4UBxgr3NyW1sFcLgIf"' , '--xpack.alerting.invalidateApiKeysTask.interval="15s"' , '--xpack.alerting.healthCheck.interval="1s"' , `--xpack.actions.enabledActionTypes=${ JSON . stringify ( enabledActionTypes ) }` , `--xpack.actions.rejectUnauthorized=${ rejectUnauthorized }` , `--xpack.actions.microsoftGraphApiUrl=${ servers . kibana . protocol }://${ servers . kibana . hostname }:${ servers . kibana . port }/api/_actions-FTS-external-service-simulators/exchange/users/test@/sendMail` , `--xpack.actions.ssl.verificationMode=${ verificationMode }` , ... actionsProxyUrl , ... customHostSettings , '--xpack.eventLog.logEntries=true' , '--xpack.task_manager.ephemeral_tasks.enabled=false' , `--xpack.task_manager.unsafe.exclude_task_types=${ JSON . stringify ( [ 'actions:test.excluded' , ] ) }` , `--xpack.actions.preconfiguredAlertHistoryEsIndex=${ preconfiguredAlertHistoryEsIndex }` , `--xpack.actions.preconfigured=${ JSON . stringify ( { 'my-slack1' : { actionTypeId : '.slack' , name : 'Slack#xyz' , secrets : { webhookUrl : 'https://hooks.slack.com/services/abcd/efgh/ijklmnopqrstuvwxyz' , } , } , 'custom-system-abc-connector' : { actionTypeId : 'system-abc-action-type' , name : 'SystemABC' , config : { xyzConfig1 : 'value1' , xyzConfig2 : 'value2' , listOfThings : [ 'a' , 'b' , 'c' , 'd' ] , } , secrets : { xyzSecret1 : 'credential1' , xyzSecret2 : 'credential2' , } , } , 'preconfigured-es-index-action' : { actionTypeId : '.index' , name : 'preconfigured_es_index_action' , config : { index : 'functional-test-actions-index-preconfigured' , refresh : true , executionTimeField : 'timestamp' , } , } , 'preconfigured.test.index-record' : { actionTypeId : 'test.index-record' , name : 'Test:_Preconfigured_Index_Record' , config : { unencrypted : 'ignored-but-required' , } , secrets : { encrypted : 'this-is-also-ignored-and-also-required' , } , } , 'custom.ssl.noCustom' : { actionTypeId : '.webhook' , name : `${ tlsWebhookServers . noCustom }` , config : { url : tlsWebhookServers . noCustom , } , } , 'custom.ssl.rejectUnauthorizedFalse' : { actionTypeId : '.webhook' , name : `${ tlsWebhookServers . rejectUnauthorizedFalse }` , config : { url : tlsWebhookServers . rejectUnauthorizedFalse , } , } , 'custom.ssl.rejectUnauthorizedTrue' : { actionTypeId : '.webhook' , name : `${ tlsWebhookServers . rejectUnauthorizedTrue }` , config : { url : tlsWebhookServers . rejectUnauthorizedTrue , } , } , 'custom.ssl.caFile' : { actionTypeId : '.webhook' , name : `${ tlsWebhookServers . caFile }` , config : { url : tlsWebhookServers . caFile , } , } , } ) }` , ... disabledPlugins . filter ( ( k ) => k !== 'security' ) . map ( ( key ) => `--xpack.${ key }.enabled=false` ) , ... plugins . map ( ( pluginDir ) => `--plugin-path=${ path . resolve ( __dirname , 'fixtures' , 'plugins' , pluginDir ) }` ) , `--server.xsrf.allowlist=${ JSON . stringify ( getAllExternalServiceSimulatorPaths ( ) ) }` , ... ( ssl ? [ `--elasticsearch.hosts=${ servers . elasticsearch . protocol }://${ servers . elasticsearch . hostname }:${ servers . elasticsearch . port }` , `--elasticsearch.ssl.certificateAuthorities=${ CA_CERT_PATH }` , ] : [ ] ) , ] , } , } ; } ; }