Skip to content

Commit

Permalink
update metadata post validation param
Browse files Browse the repository at this point in the history
  • Loading branch information
Bamieh committed Nov 10, 2024
1 parent 687164e commit 003a5b1
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@ import type {
ApiDeprecationDetails,
DomainDeprecationDetails,
} from '@kbn/core-deprecations-common';
import { CoreKibanaRequest } from '@kbn/core-http-router-server-internal';

import type { PostValidationMetadata } from '@kbn/core-http-server';
import type { BuildApiDeprecationDetailsParams } from '../types';
import {
getApiDeprecationMessage,
getApiDeprecationsManualSteps,
getApiDeprecationTitle,
} from './i18n_texts';

export const getIsAccessApiDeprecation = (req: CoreKibanaRequest): boolean => {
const isNotPublicAccess = req.route.options.access !== 'public';
const isNotInternalRequest = !req.isInternalApiRequest;
export const getIsAccessApiDeprecation = ({
isInternalApiRequest,
isPublicAccess,
}: PostValidationMetadata): boolean => {
const isNotPublicAccess = !isPublicAccess;
const isNotInternalRequest = !isInternalApiRequest;

return !!(isNotPublicAccess && isNotInternalRequest);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,21 @@ import type {
ApiDeprecationDetails,
DomainDeprecationDetails,
} from '@kbn/core-deprecations-common';

import { CoreKibanaRequest } from '@kbn/core-http-router-server-internal';
import _ from 'lodash';
import { RouteDeprecationInfo } from '@kbn/core-http-server';
import { PostValidationMetadata } from '@kbn/core-http-server';
import {
getApiDeprecationMessage,
getApiDeprecationsManualSteps,
getApiDeprecationTitle,
} from './i18n_texts';
import type { BuildApiDeprecationDetailsParams } from '../types';

export const getIsRouteApiDeprecation = (
req: CoreKibanaRequest,
deprecated?: RouteDeprecationInfo
): boolean => {
export const getIsRouteApiDeprecation = ({
isInternalApiRequest,
deprecated,
}: PostValidationMetadata): boolean => {
const hasDeprecatedObject = deprecated && _.isObject(deprecated);
const isNotInternalRequest = !req.isInternalApiRequest;
const isNotInternalRequest = !isInternalApiRequest;

return !!(hasDeprecatedObject && isNotInternalRequest);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import type { InternalCoreUsageDataSetup } from '@kbn/core-usage-data-server-internal';
import type { CoreKibanaRequest } from '@kbn/core-http-router-server-internal';
import type { InternalHttpServiceSetup } from '@kbn/core-http-server-internal';
import { RouteDeprecationInfo } from '@kbn/core-http-server';
import type { PostValidationMetadata } from '@kbn/core-http-server';
import { buildApiDeprecationId } from '../deprecations';
import { getIsRouteApiDeprecation, getIsAccessApiDeprecation } from '../deprecations';

Expand All @@ -35,9 +35,9 @@ export function createRouteDeprecationsHandler({
}: {
coreUsageData: InternalCoreUsageDataSetup;
}) {
return (req: CoreKibanaRequest, { deprecated }: { deprecated?: RouteDeprecationInfo }) => {
const hasRouteDeprecation = getIsRouteApiDeprecation(req, deprecated);
const hasAccessDeprecation = getIsAccessApiDeprecation(req);
return (req: CoreKibanaRequest, metadata: PostValidationMetadata) => {
const hasRouteDeprecation = getIsRouteApiDeprecation(metadata);
const hasAccessDeprecation = getIsAccessApiDeprecation(metadata);

const isApiDeprecation = hasAccessDeprecation || hasRouteDeprecation;
if (isApiDeprecation && req.route.routePath) {
Expand Down
23 changes: 17 additions & 6 deletions packages/core/http/core-http-router-server-internal/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { isZod } from '@kbn/zod';
import { validBodyOutput, getRequestValidation } from '@kbn/core-http-server';
import type { RouteSecurityGetter } from '@kbn/core-http-server';
import type { DeepPartial } from '@kbn/utility-types';
import { RouteDeprecationInfo } from '@kbn/core-http-server/src/router/route';
import type { PostValidationMetadata } from '@kbn/core-http-server-internal';
import { RouteValidator } from './validator';
import { ALLOWED_PUBLIC_VERSION, CoreVersionedRouter } from './versioned_router';
import { CoreKibanaRequest } from './request';
Expand Down Expand Up @@ -287,10 +287,13 @@ export class Router<Context extends RequestHandlerContextBase = RequestHandlerCo
/** Should be private, just exposed for convenience for the versioned router */
public emitPostValidate = (
request: KibanaRequest,
routeOptions: { deprecated?: RouteDeprecationInfo } = {}
postValidateConext: PostValidationMetadata = {
isInternalApiRequest: true,
isPublicAccess: false,
}
) => {
const postValidate: RouterEvents = 'onPostValidate';
Router.ee.emit(postValidate, request, routeOptions);
Router.ee.emit(postValidate, request, postValidateConext);
};

private async handle<P, Q, B>({
Expand All @@ -304,7 +307,7 @@ export class Router<Context extends RequestHandlerContextBase = RequestHandlerCo
request: Request;
responseToolkit: ResponseToolkit;
emit?: {
onPostValidation: (req: KibanaRequest, reqOptions: any) => void;
onPostValidation: (req: KibanaRequest, metadata: PostValidationMetadata) => void;
};
isPublicUnversionedRoute: boolean;
handler: RequestHandlerEnhanced<
Expand Down Expand Up @@ -337,11 +340,19 @@ export class Router<Context extends RequestHandlerContextBase = RequestHandlerCo

// Emit onPostValidation even if validation fails.
const req = CoreKibanaRequest.from(request);
emit?.onPostValidation(req, req.route.options);
emit?.onPostValidation(req, {
deprecated: req.route.options.deprecated,
isInternalApiRequest: req.isInternalApiRequest,
isPublicAccess: isPublicUnversionedRoute,
});
return response;
}

emit?.onPostValidation(kibanaRequest, kibanaRequest.route.options);
emit?.onPostValidation(kibanaRequest, {
deprecated: kibanaRequest.route.options.deprecated,
isInternalApiRequest: kibanaRequest.isInternalApiRequest,
isPublicAccess: isPublicUnversionedRoute,
});

try {
const kibanaResponse = await handler(kibanaRequest, kibanaResponseFactory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import type {
VersionedRouterRoute,
} from '@kbn/core-http-server';
import type { Mutable } from 'utility-types';
import { PostValidationMetadata } from '@kbn/core-http-server-internal';
import type { HandlerResolutionStrategy, Method, Options } from './types';

import { validate } from './validate';
Expand Down Expand Up @@ -210,6 +211,12 @@ export class CoreVersionedRoute implements VersionedRoute {
});
}
const validation = extractValidationSchemaFromHandler(handler);
const postValidateMetadata: PostValidationMetadata = {
deprecated: handler.options.options?.deprecated,
isInternalApiRequest: req.isInternalApiRequest,
isPublicAccess: this.isPublic,
};

if (
validation?.request &&
Boolean(validation.request.body || validation.request.params || validation.request.query)
Expand All @@ -221,7 +228,8 @@ export class CoreVersionedRoute implements VersionedRoute {
req.query = query;
} catch (e) {
// Emit onPostValidation even if validation fails.
this.router.emitPostValidate(req, handler.options.options);

this.router.emitPostValidate(req, postValidateMetadata);
return res.badRequest({ body: e.message, headers: getVersionHeader(version) });
}
} else {
Expand All @@ -231,7 +239,7 @@ export class CoreVersionedRoute implements VersionedRoute {
req.query = {};
}

this.router.emitPostValidate(req, handler.options.options);
this.router.emitPostValidate(req, postValidateMetadata);

const response = await handler.fn(ctx, req, res);

Expand Down
6 changes: 3 additions & 3 deletions packages/core/http/core-http-server-internal/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import type {
HttpServiceStart,
RouterDeprecatedApiDetails,
} from '@kbn/core-http-server';
import { CoreKibanaRequest } from '@kbn/core-http-router-server-internal';
import { RouteDeprecationInfo } from '@kbn/core-http-server/src/router/route';
import type { CoreKibanaRequest } from '@kbn/core-http-router-server-internal';
import type { PostValidationMetadata } from '@kbn/core-http-server';
import type { HttpServerSetup } from './http_server';
import type { ExternalUrlConfig } from './external_url';
import type { InternalStaticAssets } from './static_assets';
Expand Down Expand Up @@ -58,7 +58,7 @@ export interface InternalHttpServiceSetup
plugin?: PluginOpaqueId
) => IRouter<Context>;
registerOnPostValidation(
cb: (req: CoreKibanaRequest, metadata: { deprecated: RouteDeprecationInfo }) => void
cb: (req: CoreKibanaRequest, metadata: PostValidationMetadata) => void
): void;
registerRouterAfterListening: (router: IRouter) => void;
registerStaticDir: (path: string, dirPath: string) => void;
Expand Down
1 change: 1 addition & 0 deletions packages/core/http/core-http-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export type {
RouteSecurityGetter,
InternalRouteSecurity,
RouteDeprecationInfo,
PostValidationMetadata,
} from './src/router';
export {
validBodyOutput,
Expand Down
1 change: 1 addition & 0 deletions packages/core/http/core-http-server/src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export type {
Privilege,
PrivilegeSet,
RouteDeprecationInfo,
PostValidationMetadata,
} from './route';

export { validBodyOutput, ReservedPrivilegesSet } from './route';
Expand Down
9 changes: 9 additions & 0 deletions packages/core/http/core-http-server/src/router/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -525,3 +525,12 @@ export interface RouteConfig<P, Q, B, Method extends RouteMethod> {
*/
options?: RouteConfigOptions<Method>;
}

/**
* Post Validation Route emitter metadata.
*/
export interface PostValidationMetadata {
deprecated?: RouteDeprecationInfo;
isInternalApiRequest: boolean;
isPublicAccess: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ export interface CoreUsageStats {
'savedObjectsRepository.resolvedOutcome.conflict'?: number;
'savedObjectsRepository.resolvedOutcome.notFound'?: number;
'savedObjectsRepository.resolvedOutcome.total'?: number;
// API Deprecations counters
'deprecated_api_calls_resolved.total'?: number;
'deprecated_api_calls.total'?: number;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1183,6 +1183,18 @@ export function getCoreUsageCollector(
'How many times a saved object has resolved with any of the four possible outcomes.',
},
},
'deprecated_api_calls_resolved.total': {
type: 'integer',
_meta: {
description: 'How many times deprecated APIs has been marked as resolved',
},
},
'deprecated_api_calls.total': {
type: 'integer',
_meta: {
description: 'How many times deprecated APIs has been called.',
},
},
},
fetch() {
return getCoreUsageDataService().getCoreUsageData();
Expand Down
12 changes: 6 additions & 6 deletions src/plugins/telemetry/schema/oss_plugins.json
Original file line number Diff line number Diff line change
Expand Up @@ -10905,12 +10905,6 @@
"description": "Non-default value of setting."
}
},
"observability:newLogsOverview": {
"type": "boolean",
"_meta": {
"description": "Enable the new logs overview component."
}
},
"observability:searchExcludedDataTiers": {
"type": "array",
"items": {
Expand All @@ -10919,6 +10913,12 @@
"description": "Non-default value of setting."
}
}
},
"observability:newLogsOverview": {
"type": "boolean",
"_meta": {
"description": "Enable the new logs overview component."
}
}
}
},
Expand Down

0 comments on commit 003a5b1

Please sign in to comment.