import supertest from 'supertest' ; import { Client , HttpConnection } from '@elastic/elasticsearch' ; import { format as formatUrl } from 'url' ; import { FtrProviderContext } from '../../../api_integration/ftr_provider_context' ; export function getSupertestWithoutAuth ( { getService } ) { const config = getService ( 'config' ) ; const kibanaUrl = config . get ( 'servers.kibana' ) ; kibanaUrl . auth = null ; kibanaUrl . password = null ; return supertest ( formatUrl ( kibanaUrl ) ) ; } export function getEsClientForAPIKey ( { getService } , esApiKey ) { const config = getService ( 'config' ) ; const url = formatUrl ( { ... config . get ( 'servers.elasticsearch' ) , auth : false } ) ; return new Client ( { nodes : [ url ] , auth : { apiKey : esApiKey , } , requestTimeout : config . get ( 'timeouts.esRequestTimeout' ) , Connection : HttpConnection , } ) ; } export function setupFleetAndAgents ( providerContext ) { before ( async ( ) => { const es = providerContext . getService ( 'es' ) ; const { token } = await es . security . createServiceToken ( { namespace : 'elastic' , service : 'fleet-server' , } ) ; const supetestWithoutAuth = getSupertestWithoutAuth ( providerContext ) ; await supetestWithoutAuth . post ( `/api/fleet/setup` ) . set ( 'kbn-xsrf' , 'xxx' ) . set ( 'Authorization' , `Bearer ${ token . value }` ) . send ( ) . expect ( 200 ) ; await supetestWithoutAuth . post ( `/api/fleet/agents/setup` ) . set ( 'kbn-xsrf' , 'xxx' ) . set ( 'Authorization' , `Bearer ${ token . value }` ) . send ( { forceRecreate : true } ) . expect ( 200 ) ; } ) ; }