diff --git a/x-pack/legacy/plugins/infra/server/lib/adapters/framework/adapter_types.ts b/x-pack/legacy/plugins/infra/server/lib/adapters/framework/adapter_types.ts index 883a11c02461bf8..5b8d0a8bfd0246d 100644 --- a/x-pack/legacy/plugins/infra/server/lib/adapters/framework/adapter_types.ts +++ b/x-pack/legacy/plugins/infra/server/lib/adapters/framework/adapter_types.ts @@ -22,7 +22,7 @@ export interface InfraServerPluginDeps { } /* eslint-disable @typescript-eslint/unified-signatures */ -export interface InfraBackendFrameworkAdapter { +export interface InfraBackendFrameworkAdapter { registerGraphQLEndpoint(routePath: string, schema: GraphQLSchema): void; registerRoute( route: R @@ -66,10 +66,10 @@ export interface InfraBackendFrameworkAdapter { // NP_TODO: using Promise here until new platform callAsCurrentUser can return types // callWithRequest(req: KibanaRequest, method: string, options?: object): Promise; - getIndexPatternsService(req: InfraFrameworkRequest): Legacy.IndexPatternsService; - getSpaceId(request: InfraFrameworkRequest): string; + getIndexPatternsService(req: KibanaRequest): Legacy.IndexPatternsService; + getSpaceId(request: KibanaRequest): string; makeTSVBRequest( - req: InfraFrameworkRequest, + req: KibanaRequest, model: InfraMetricModel, timerange: { min: number; max: number }, filters: JsonObject[] diff --git a/x-pack/legacy/plugins/infra/server/lib/adapters/framework/kibana_framework_adapter.ts b/x-pack/legacy/plugins/infra/server/lib/adapters/framework/kibana_framework_adapter.ts index f0bf7bf969c47a6..206e067a145f03a 100644 --- a/x-pack/legacy/plugins/infra/server/lib/adapters/framework/kibana_framework_adapter.ts +++ b/x-pack/legacy/plugins/infra/server/lib/adapters/framework/kibana_framework_adapter.ts @@ -75,9 +75,24 @@ export class InfraKibanaBackendFrameworkAdapter const body = schema.object({ operationName: schema.string(), query: schema.string(), - variables: schema.object({ - sourceId: schema.string(), - }), + // NP_TODO: we short circuit validation here, need to understand the best way forward + // whether it's using io-ts and skipping ALL config-schema validation or figuring out + // why the nested maybe stuff inside variables didn't work well here + variables: schema.object( + { + // sourceId: schema.string(), + // countBefore: schema.maybe(schema.number()), + // countAfter: schema.maybe(schema.number()), + // filterQuery: schema.maybe(schema.string()), + // timeKey: schema.maybe( + // schema.object({ + // time: schema.maybe(schema.number()), + // tiebreaker: schema.maybe(schema.number()), + // }) + // ), + }, + { allowUnknowns: true } + ), }); type Body = TypeOf; @@ -101,7 +116,7 @@ export class InfraKibanaBackendFrameworkAdapter const gqlResponse = await runHttpQuery([request], { method: request.route.method.toUpperCase(), options: (req: Legacy.Request) => ({ - context: { req: wrapRequest(req) }, + context: { req }, schema: gqlSchema, }), query, @@ -114,7 +129,9 @@ export class InfraKibanaBackendFrameworkAdapter }, }); } catch (error) { - return response.badRequest({ body: error }); + return response.internalError({ + body: { ...error, temporary: 'this is just a temporary error catch' }, + }); // NP_TODO handle errors! (see below for previously handled error cases) } @@ -238,19 +255,24 @@ export class InfraKibanaBackendFrameworkAdapter { ...params, ...frozenIndicesParams, + // NP_TODO not sure how to use config auth automatically?? + headers: { + Authorization: 'Basic ZWxhc3RpYzpjaGFuZ2VtZQ==', // 8.0 shared 'Basic YWRtaW46dkw0MVpiREpoNWtuUUE=', + }, }, - ...rest + // NP_TODO: not sure we need this? + { + wrap401Errors: true, + } ); return fields as Promise>; } - public getIndexPatternsService( - request: InfraFrameworkRequest - ): Legacy.IndexPatternsService { + public getIndexPatternsService(request: KibanaRequest): Legacy.IndexPatternsService { return this.plugins.indexPatterns.indexPatternsServiceFactory({ callCluster: async (method: string, args: [GenericParams], ...rest: any[]) => { const fieldCaps = await this.callWithRequest( - request[internalInfraFrameworkRequest], + request, method, { ...args, allowNoIndices: true } as GenericParams, ...rest @@ -260,63 +282,60 @@ export class InfraKibanaBackendFrameworkAdapter }); } - public getSpaceId(request: InfraFrameworkRequest): string { + public getSpaceId(request: KibanaRequest): string { const spacesPlugin = this.plugins.spaces; if (spacesPlugin && typeof spacesPlugin.getSpaceId === 'function') { - return spacesPlugin.getSpaceId(request[internalInfraFrameworkRequest]); + return spacesPlugin.getSpaceId(request); } else { return 'default'; } } + // NP_TODO: this function still needs NP migration for the metrics plugin + // and for the getBasePath public async makeTSVBRequest( - req: InfraFrameworkRequest, + request: KibanaRequest, model: InfraMetricModel, timerange: { min: number; max: number }, filters: any[] ) { - const internalRequest = req[internalInfraFrameworkRequest]; - const server = internalRequest.server; + const server = request.server; const getVisData = get(server, 'plugins.metrics.getVisData'); if (typeof getVisData !== 'function') { throw new Error('TSVB is not available'); } // getBasePath returns randomized base path AND spaces path - const basePath = internalRequest.getBasePath(); + const basePath = request.getBasePath(); const url = `${basePath}/api/metrics/vis/data`; // For the following request we need a copy of the instnace of the internal request // but modified for our TSVB request. This will ensure all the instance methods // are available along with our overriden values - const request = Object.assign( - Object.create(Object.getPrototypeOf(internalRequest)), - internalRequest, - { - url, - method: 'POST', - payload: { - timerange, - panels: [model], - filters, - }, - } - ); - const result = await getVisData(request); + const requestCopy = Object.assign(Object.create(Object.getPrototypeOf(request)), request, { + url, + method: 'POST', + payload: { + timerange, + panels: [model], + filters, + }, + }); + const result = await getVisData(requestCopy); return result as InfraTSVBResponse; } } -export function wrapRequest( - req: InternalRequest -): InfraFrameworkRequest { - const { params, payload, query } = req; - - return { - [internalInfraFrameworkRequest]: req, - params, - payload, - query, - }; -} +// export function wrapRequest( +// req: InternalRequest +// ): InfraFrameworkRequest { +// const { params, payload, query } = req; + +// return { +// [internalInfraFrameworkRequest]: req, +// params, +// payload, +// query, +// }; +// } diff --git a/x-pack/legacy/plugins/infra/server/lib/adapters/log_entries/kibana_log_entries_adapter.ts b/x-pack/legacy/plugins/infra/server/lib/adapters/log_entries/kibana_log_entries_adapter.ts index 547e74eecb67c90..bb42a49b2978c82 100644 --- a/x-pack/legacy/plugins/infra/server/lib/adapters/log_entries/kibana_log_entries_adapter.ts +++ b/x-pack/legacy/plugins/infra/server/lib/adapters/log_entries/kibana_log_entries_adapter.ts @@ -15,6 +15,7 @@ import zip from 'lodash/fp/zip'; import { pipe } from 'fp-ts/lib/pipeable'; import { map, fold } from 'fp-ts/lib/Either'; import { identity, constant } from 'fp-ts/lib/function'; +import { KibanaRequest } from 'src/core/server'; import { compareTimeKeys, isTimeKey, TimeKey } from '../../../../common/time'; import { JsonObject } from '../../../../common/typed_json'; import { @@ -212,7 +213,7 @@ export class InfraKibanaLogEntriesAdapter implements LogEntriesAdapter { } private async getLogEntryDocumentsBetween( - request: InfraFrameworkRequest, + request: KibanaRequest, sourceConfiguration: InfraSourceConfiguration, fields: string[], start: number, diff --git a/x-pack/legacy/plugins/infra/server/lib/log_analysis/log_analysis.ts b/x-pack/legacy/plugins/infra/server/lib/log_analysis/log_analysis.ts index 9c057d66b90f381..6be91ebfbf8ab59 100644 --- a/x-pack/legacy/plugins/infra/server/lib/log_analysis/log_analysis.ts +++ b/x-pack/legacy/plugins/infra/server/lib/log_analysis/log_analysis.ts @@ -9,7 +9,7 @@ import { map, fold } from 'fp-ts/lib/Either'; import { identity } from 'fp-ts/lib/function'; import { getJobId } from '../../../common/log_analysis'; import { throwErrors, createPlainError } from '../../../common/runtime_types'; -import { InfraBackendFrameworkAdapter, InfraFrameworkRequest } from '../adapters/framework'; +import { InfraBackendFrameworkAdapter } from '../adapters/framework'; import { NoLogRateResultsIndexError } from './errors'; import { logRateModelPlotResponseRT, diff --git a/x-pack/legacy/plugins/infra/server/lib/sources/sources.ts b/x-pack/legacy/plugins/infra/server/lib/sources/sources.ts index fb721201c2a713b..a54774cc832dcb6 100644 --- a/x-pack/legacy/plugins/infra/server/lib/sources/sources.ts +++ b/x-pack/legacy/plugins/infra/server/lib/sources/sources.ts @@ -108,7 +108,7 @@ export class InfraSources { const createdSourceConfiguration = convertSavedObjectToSavedSourceConfiguration( await this.libs.savedObjects - .getScopedSavedObjectsClient(request[internalInfraFrameworkRequest]) + .getScopedSavedObjectsClient(request) .create( infraSourceConfigurationSavedObjectType, pickSavedSourceConfiguration(newSourceConfiguration) as any, @@ -127,7 +127,7 @@ export class InfraSources { public async deleteSourceConfiguration(request: InfraFrameworkRequest, sourceId: string) { await this.libs.savedObjects - .getScopedSavedObjectsClient(request[internalInfraFrameworkRequest]) + .getScopedSavedObjectsClient(request) .delete(infraSourceConfigurationSavedObjectType, sourceId); } @@ -147,7 +147,7 @@ export class InfraSources { const updatedSourceConfiguration = convertSavedObjectToSavedSourceConfiguration( await this.libs.savedObjects - .getScopedSavedObjectsClient(request[internalInfraFrameworkRequest]) + .getScopedSavedObjectsClient(request) .update( infraSourceConfigurationSavedObjectType, sourceId, @@ -203,9 +203,7 @@ export class InfraSources { } private async getSavedSourceConfiguration(request: InfraFrameworkRequest, sourceId: string) { - const savedObjectsClient = this.libs.savedObjects.getScopedSavedObjectsClient( - request[internalInfraFrameworkRequest] - ); + const savedObjectsClient = this.libs.savedObjects.getScopedSavedObjectsClient(request); const savedObject = await savedObjectsClient.get( infraSourceConfigurationSavedObjectType, @@ -216,9 +214,7 @@ export class InfraSources { } private async getAllSavedSourceConfigurations(request: InfraFrameworkRequest) { - const savedObjectsClient = this.libs.savedObjects.getScopedSavedObjectsClient( - request[internalInfraFrameworkRequest] - ); + const savedObjectsClient = this.libs.savedObjects.getScopedSavedObjectsClient(request); const savedObjects = await savedObjectsClient.find({ type: infraSourceConfigurationSavedObjectType,