diff --git a/x-pack/plugins/fleet/server/plugin.ts b/x-pack/plugins/fleet/server/plugin.ts index 269e220e58da7..cd8a4d0a5275e 100644 --- a/x-pack/plugins/fleet/server/plugin.ts +++ b/x-pack/plugins/fleet/server/plugin.ts @@ -115,7 +115,6 @@ import type { PackagePolicyService } from './services/package_policy_service'; import { PackagePolicyServiceImpl } from './services/package_policy'; import { registerFleetUsageLogger, startFleetUsageLogger } from './services/fleet_usage_logger'; import { CheckDeletedFilesTask } from './tasks/check_deleted_files_task'; -import { getRequestStore } from './services/request_store'; export interface FleetSetupDeps { security: SecurityPluginSetup; @@ -368,42 +367,38 @@ export class FleetPlugin .getSavedObjects() .getScopedClient(request, { excludedExtensions: [SECURITY_EXTENSION_ID] }); - const requestStore = getRequestStore(); + return { + get agentClient() { + const agentService = plugin.setupAgentService(esClient.asInternalUser, soClient); - return requestStore.run(request, () => { - return { - get agentClient() { - const agentService = plugin.setupAgentService(esClient.asInternalUser, soClient); - - return { - asCurrentUser: agentService.asScoped(request), - asInternalUser: agentService.asInternalUser, - }; - }, - get packagePolicyService() { - const service = plugin.setupPackagePolicyService(); + return { + asCurrentUser: agentService.asScoped(request), + asInternalUser: agentService.asInternalUser, + }; + }, + get packagePolicyService() { + const service = plugin.setupPackagePolicyService(); - return { - asCurrentUser: service.asScoped(request), - asInternalUser: service.asInternalUser, - }; - }, - authz, - get internalSoClient() { - // Use a lazy getter to avoid constructing this client when not used by a request handler - return getInternalSoClient(); - }, - get spaceId() { - return deps.spaces?.spacesService?.getSpaceId(request) ?? DEFAULT_SPACE_ID; - }, + return { + asCurrentUser: service.asScoped(request), + asInternalUser: service.asInternalUser, + }; + }, + authz, + get internalSoClient() { + // Use a lazy getter to avoid constructing this client when not used by a request handler + return getInternalSoClient(); + }, + get spaceId() { + return deps.spaces?.spacesService?.getSpaceId(request) ?? DEFAULT_SPACE_ID; + }, - get limitedToPackages() { - if (routeAuthz && routeAuthz.granted) { - return routeAuthz.scopeDataToPackages; - } - }, - }; - }); + get limitedToPackages() { + if (routeAuthz && routeAuthz.granted) { + return routeAuthz.scopeDataToPackages; + } + }, + }; } ); diff --git a/x-pack/plugins/fleet/server/services/request_store.ts b/x-pack/plugins/fleet/server/services/request_store.ts index bed1bc174e764..f038a0f031388 100644 --- a/x-pack/plugins/fleet/server/services/request_store.ts +++ b/x-pack/plugins/fleet/server/services/request_store.ts @@ -9,8 +9,8 @@ import { AsyncLocalStorage } from 'async_hooks'; import type { KibanaRequest } from '@kbn/core-http-server'; -export function getRequestStore() { - const requestStore = new AsyncLocalStorage(); +const requestStore = new AsyncLocalStorage(); +export function getRequestStore() { return requestStore; } diff --git a/x-pack/plugins/fleet/server/services/security/fleet_router.ts b/x-pack/plugins/fleet/server/services/security/fleet_router.ts index 1b1d84d3aca40..17f78c901ab9d 100644 --- a/x-pack/plugins/fleet/server/services/security/fleet_router.ts +++ b/x-pack/plugins/fleet/server/services/security/fleet_router.ts @@ -17,6 +17,8 @@ import type { import type { FleetRequestHandlerContext } from '../..'; +import { getRequestStore } from '../request_store'; + import type { FleetAuthzRouteConfig, FleetAuthzRouter } from './types'; import { checkSecurityEnabled, @@ -60,30 +62,72 @@ export function makeRouterWithFleetAuthz({ + context, + request, + response, + handler, + }: { + context: TContext; + request: KibanaRequest; + response: KibanaResponseFactory; + handler: RequestHandler; + }): Promise> => { + return getRequestStore().run(request, () => handler(context, request, response)); + }; + + const fleetHandlerWrapper = async ({ + context, + request, + response, + handler, + hasRequiredAuthz, + }: { + context: TContext; + request: KibanaRequest; + response: KibanaResponseFactory; + handler: RequestHandler; + hasRequiredAuthz?: FleetAuthzRouteConfig['fleetAuthz']; + }): Promise> => { + return requestContextWrapper({ + context, + request, + response, + handler: (handlerContext, handlerRequest, handlerResponse) => + routerAuthzWrapper({ + context: handlerContext, + request: handlerRequest, + response: handlerResponse, + handler, + hasRequiredAuthz, + }), + }); + }; + const fleetAuthzRouter: FleetAuthzRouter = { get: ({ fleetAuthz: hasRequiredAuthz, ...options }, handler) => { - router.get(options, async (context, request, response) => - routerAuthzWrapper({ context, request, response, handler, hasRequiredAuthz }) + router.get(options, (context, request, response) => + fleetHandlerWrapper({ context, request, response, handler, hasRequiredAuthz }) ); }, delete: ({ fleetAuthz: hasRequiredAuthz, ...options }, handler) => { - router.delete(options, async (context, request, response) => - routerAuthzWrapper({ context, request, response, handler, hasRequiredAuthz }) + router.delete(options, (context, request, response) => + fleetHandlerWrapper({ context, request, response, handler, hasRequiredAuthz }) ); }, post: ({ fleetAuthz: hasRequiredAuthz, ...options }, handler) => { - router.post(options, async (context, request, response) => - routerAuthzWrapper({ context, request, response, handler, hasRequiredAuthz }) + router.post(options, (context, request, response) => + fleetHandlerWrapper({ context, request, response, handler, hasRequiredAuthz }) ); }, put: ({ fleetAuthz: hasRequiredAuthz, ...options }, handler) => { - router.put(options, async (context, request, response) => - routerAuthzWrapper({ context, request, response, handler, hasRequiredAuthz }) + router.put(options, (context, request, response) => + fleetHandlerWrapper({ context, request, response, handler, hasRequiredAuthz }) ); }, patch: ({ fleetAuthz: hasRequiredAuthz, ...options }, handler) => { - router.patch(options, async (context, request, response) => - routerAuthzWrapper({ context, request, response, handler, hasRequiredAuthz }) + router.patch(options, (context, request, response) => + fleetHandlerWrapper({ context, request, response, handler, hasRequiredAuthz }) ); }, handleLegacyErrors: (handler) => router.handleLegacyErrors(handler),