import { i18n } from '@kbn/i18n' ; import { ReportingCore } from '../..' ; import { APP_WRAPPER_CLASS } from '../../../../../../src/core/server' ; import { API_DIAGNOSE_URL } from '../../../common/constants' ; import { omitBlockedHeaders , generatePngObservable } from '../../export_types/common' ; import { getAbsoluteUrlFactory } from '../../export_types/common/get_absolute_url' ; import { LevelLogger as Logger } from '../../lib' ; import { authorizedUserPreRouting } from '../lib/authorized_user_pre_routing' ; import { DiagnosticResponse } from './' ; export const registerDiagnoseScreenshot = ( reporting , logger ) => { const setupDeps = reporting . getPluginSetupDeps ( ) ; const { router } = setupDeps ; router . post ( { path : `${ API_DIAGNOSE_URL }/screenshot` , validate : { } , } , authorizedUserPreRouting ( reporting , async ( _user , _context , req , res ) => { const config = reporting . getConfig ( ) ; const decryptedHeaders = req . headers as Record < string , string > ; const [ basePath , protocol , hostname , port ] = [ config . kbnConfig . get ( 'server' , 'basePath' ) , config . get ( 'kibanaServer' , 'protocol' ) , config . get ( 'kibanaServer' , 'hostname' ) , config . get ( 'kibanaServer' , 'port' ) , ] as string [ ] ; const getAbsoluteUrl = getAbsoluteUrlFactory ( { basePath , protocol , hostname , port } ) ; const hashUrl = getAbsoluteUrl ( { path : '/' , hash : '' , search : '' } ) ; const layout = { dimensions : { width : 1440 , height : 2024 , } , selectors : { screenshot : `.${ APP_WRAPPER_CLASS }` , renderComplete : `.${ APP_WRAPPER_CLASS }` , itemsCountAttribute : 'data-test-subj="kibanaChrome"' , timefilterDurationAttribute : 'data-test-subj="kibanaChrome"' , } , } ; const conditionalHeaders = { headers : omitBlockedHeaders ( decryptedHeaders ) , conditions : { hostname , port : + port , basePath , protocol , } , } ; return generatePngObservable ( reporting , logger , { conditionalHeaders , layout , browserTimezone : 'America/Los_Angeles' , urls : [ hashUrl ] , } ) . pipe ( ) . toPromise ( ) . then ( ( screenshot ) => { if ( screenshot . warnings . length ) { return res . ok ( { body : { success : false , help : [ ] , logs : screenshot . warnings , } , } ) ; } return res . ok ( { body : { success : true , help : [ ] , logs : '' , } as DiagnosticResponse , } ) ; } ) . catch ( ( error ) => res . ok ( { body : { success : false , help : [ i18n . translate ( 'xpack.reporting.diagnostic.screenshotFailureMessage' , { defaultMessage : `We couldn't screenshot your Kibana install.` , } ) , ] , logs : error . message , } as DiagnosticResponse , } ) ) ; } ) ) ; } ;