import { FtrProviderContext } from '../../ftr_provider_context' ; import { PACKAGE_POLICY_SAVED_OBJECT_TYPE , DeletePackagePoliciesRequest , GetPackagePoliciesResponse , GetFullAgentPolicyResponse , GetPackagesResponse , GetAgentPoliciesResponse , } from '../../../../plugins/fleet/common' ; const INGEST_API_ROOT = '/api/fleet' ; const INGEST_API_AGENT_POLICIES = `${ INGEST_API_ROOT }/agent_policies` ; const INGEST_API_PACKAGE_POLICIES = `${ INGEST_API_ROOT }/package_policies` ; const INGEST_API_PACKAGE_POLICIES_DELETE = `${ INGEST_API_PACKAGE_POLICIES }/delete` ; const INGEST_API_EPM_PACKAGES = `${ INGEST_API_ROOT }/epm/packages` ; export function SyntheticsPackageProvider ( { getService } ) { const supertest = getService ( 'supertest' ) ; const log = getService ( 'log' ) ; const retry = getService ( 'retry' ) ; const logSupertestApiErrorAndThrow = ( message , error ) : never => { const responseBody = error ?. response ?. body ; const responseText = error ?. response ?. text ; log . error ( `Error occurred at ${ Date . now ( ) } | ${ new Date ( ) . toISOString ( ) }` ) ; log . error ( JSON . stringify ( responseBody || responseText , null , 2 ) ) ; log . error ( error ) ; throw new Error ( message ) ; } ; const retrieveSyntheticsPackageInfo = ( ( ) => { let apiRequest ; return ( ) => { if ( ! apiRequest ) { log . info ( `Setting up call to retrieve Synthetics package` ) ; apiRequest = retry . try ( ( ) => { return supertest . get ( INGEST_API_EPM_PACKAGES ) . set ( 'kbn-xsrf' , 'xxx' ) . expect ( 200 ) . catch ( ( error ) => { return logSupertestApiErrorAndThrow ( `Unable to retrieve packages via Ingest!` , error ) ; } ) . then ( ( response ) => { const { body } = response ; const syntheticsPackageInfo = body . items . find ( ( epmPackage ) => epmPackage . name === 'synthetics' ) ; if ( ! syntheticsPackageInfo ) { throw new Error ( `Synthetics package was not in response from ${ INGEST_API_EPM_PACKAGES }` ) ; } return Promise . resolve ( syntheticsPackageInfo ) ; } ) ; } ) ; } else { log . info ( 'Using cached retrieval of synthetics package' ) ; } return apiRequest ; } ; } ) ( ) ; return { /**
         * Returns the synthetics package version for the currently installed package. This version can then
         * be used to build URLs for Fleet pages or APIs
         */ async getSyntheticsPackageVersion ( ) { const syntheticsPackage = await retrieveSyntheticsPackageInfo ( ) ! ; return syntheticsPackage ?. version ; } , /**
         * Retrieves the full Agent policy by id, which mirrors what the Elastic Agent would get
         * once they checkin.
         */ async getFullAgentPolicy ( agentPolicyId ) { let fullAgentPolicy ; try { const apiResponse = await supertest . get ( `${ INGEST_API_AGENT_POLICIES }/${ agentPolicyId }/full` ) . expect ( 200 ) ; fullAgentPolicy = apiResponse . body . item ; } catch ( error ) { return logSupertestApiErrorAndThrow ( 'Unable to get full Agent policy' , error ) ; } return fullAgentPolicy ! ; } , /**
         * Retrieves all the agent policies.
         */ async getAgentPolicyList ( ) { let agentPolicyList ; try { const apiResponse = await supertest . get ( INGEST_API_AGENT_POLICIES ) . expect ( 200 ) ; agentPolicyList = apiResponse . body . items ; } catch ( error ) { return logSupertestApiErrorAndThrow ( 'Unable to get full Agent policy list' , error ) ; } return agentPolicyList ! ; } , param name async deletePolicyByName ( name ) { const id = await this . getPackagePolicyIdByName ( name ) ; if ( id ) { try { const deletePackagePolicyData = { packagePolicyIds : [ id ] , } ; await supertest . post ( INGEST_API_PACKAGE_POLICIES_DELETE ) . set ( 'kbn-xsrf' , 'xxx' ) . send ( deletePackagePolicyData ) . expect ( 200 ) ; } catch ( error ) { logSupertestApiErrorAndThrow ( `Unable to delete Package Policy via Ingest! ${ name }` , error ) ; } } } , param name async getPackagePolicyIdByName ( name ) { const { body : packagePoliciesResponse } = await supertest . get ( INGEST_API_PACKAGE_POLICIES ) . set ( 'kbn-xsrf' , 'xxx' ) . query ( { kuery : `${ PACKAGE_POLICY_SAVED_OBJECT_TYPE }.name: ${ name }` } ) . send ( ) . expect ( 200 ) ; const packagePolicyList = packagePoliciesResponse . items ; if ( packagePolicyList . length > 1 ) { throw new Error ( `Found ${ packagePolicyList . length } Policies - was expecting only one!` ) ; } if ( packagePolicyList . length ) { return packagePolicyList [ 0 ] . id ; } } , } ; }