import { schema } from '@kbn/config-schema' ; import rison from 'rison-node' ; import { ReportingCore } from '../..' ; import { API_BASE_URL } from '../../../common/constants' ; import { LevelLogger } from '../../lib' ; import { BaseParams } from '../../types' ; import { authorizedUserPreRouting } from '../lib/authorized_user_pre_routing' ; import { RequestHandler } from '../lib/request_handler' ; const BASE_GENERATE = `${ API_BASE_URL }/generate` ; export function registerJobGenerationRoutes ( reporting , logger ) { const setupDeps = reporting . getPluginSetupDeps ( ) ; const { router } = setupDeps ; const useKibanaAccessControl = reporting . getDeprecatedAllowedRoles ( ) === false ; const kibanaAccessControlTags = useKibanaAccessControl ? [ 'access:generateReport' ] : [ ] ; router . post ( { path : `${ BASE_GENERATE }/{exportType}` , validate : { params : schema . object ( { exportType : schema . string ( { minLength : 2 } ) } ) , body : schema . nullable ( schema . object ( { jobParams : schema . maybe ( schema . string ( ) ) } ) ) , query : schema . nullable ( schema . object ( { jobParams : schema . string ( { defaultValue : '' } ) } ) ) , } , options : { tags : kibanaAccessControlTags } , } , authorizedUserPreRouting ( reporting , async ( user , context , req , res ) => { let jobParamsRison = null ; if ( req . body ) { const { jobParams : jobParamsPayload } = req . body ; jobParamsRison = jobParamsPayload ? jobParamsPayload : null ; } else if ( req . query ?. jobParams ) { const { jobParams : queryJobParams } = req . query ; if ( queryJobParams ) { jobParamsRison = queryJobParams ; } else { jobParamsRison = null ; } } if ( ! jobParamsRison ) { return res . customError ( { statusCode : 400 , body : 'A jobParams RISON string is required in the querystring or POST body' , } ) ; } let jobParams ; try { jobParams = rison . decode ( jobParamsRison ) as BaseParams | null ; if ( ! jobParams ) { return res . customError ( { statusCode : 400 , body : 'Missing jobParams!' , } ) ; } } catch ( err ) { return res . customError ( { statusCode : 400 , body : `invalid rison: ${ jobParamsRison }` , } ) ; } const requestHandler = new RequestHandler ( reporting , user , context , req , res , logger ) ; try { return await requestHandler . handleGenerateRequest ( req . params . exportType , jobParams ) ; } catch ( err ) { return requestHandler . handleError ( err ) ; } } ) ) ; router . get ( { path : `${ BASE_GENERATE }/{p*}` , validate : false , } , ( _context , _req , res ) => { return res . customError ( { statusCode : 405 , body : 'GET is not allowed' } ) ; } ) ; }