From 7cf7365987853369b3265b3d88f64d6756d56295 Mon Sep 17 00:00:00 2001 From: Tim Sullivan Date: Fri, 28 Jun 2024 09:26:29 -0700 Subject: [PATCH] [OSQuery Plugin] Migrate usage og authc.getCurrentUser to coreContext.security (#187014) Part of https://github.com/elastic/kibana/issues/186574 ## Summary This PR migrates the OSQuery Plugin's route handlers that consume `authc.getCurrentUser` to use coreContext.security Background: This PR serves as an example of a plugin migrating away from depending on the Security plugin, which is a high priority effort for the last release before 9.0. ### Checklist Delete any items that are not applicable to this PR. - [ ] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios --- .../osquery/server/routes/asset/update_assets_route.ts | 7 ++++--- .../server/routes/live_query/create_live_query_route.ts | 5 +++-- .../osquery/server/routes/pack/create_pack_route.ts | 2 +- .../osquery/server/routes/pack/update_pack_route.ts | 2 +- .../server/routes/saved_query/create_saved_query_route.ts | 2 +- .../server/routes/saved_query/update_saved_query_route.ts | 2 +- 6 files changed, 11 insertions(+), 9 deletions(-) diff --git a/x-pack/plugins/osquery/server/routes/asset/update_assets_route.ts b/x-pack/plugins/osquery/server/routes/asset/update_assets_route.ts index 57800729d6435..690ea4206b84a 100644 --- a/x-pack/plugins/osquery/server/routes/asset/update_assets_route.ts +++ b/x-pack/plugins/osquery/server/routes/asset/update_assets_route.ts @@ -42,9 +42,10 @@ export const updateAssetsRoute = (router: IRouter, osqueryContext: OsqueryAppCon }, }, }, - async (context, request, response) => { - const savedObjectsClient = (await context.core).savedObjects.client; - const currentUser = await osqueryContext.security.authc.getCurrentUser(request)?.username; + async (context, _request, response) => { + const coreContext = await context.core; + const savedObjectsClient = coreContext.savedObjects.client; + const currentUser = coreContext.security.authc.getCurrentUser()?.username; let installation; diff --git a/x-pack/plugins/osquery/server/routes/live_query/create_live_query_route.ts b/x-pack/plugins/osquery/server/routes/live_query/create_live_query_route.ts index c93b58cf5af73..6372a48c89b6e 100644 --- a/x-pack/plugins/osquery/server/routes/live_query/create_live_query_route.ts +++ b/x-pack/plugins/osquery/server/routes/live_query/create_live_query_route.ts @@ -42,7 +42,8 @@ export const createLiveQueryRoute = (router: IRouter, osqueryContext: OsqueryApp }, async (context, request, response) => { const [coreStartServices] = await osqueryContext.getStartServices(); - const soClient = (await context.core).savedObjects.client; + const coreContext = await context.core; + const soClient = coreContext.savedObjects.client; const { osquery: { writeLiveQueries, runSavedQueries }, @@ -106,7 +107,7 @@ export const createLiveQueryRoute = (router: IRouter, osqueryContext: OsqueryApp } try { - const currentUser = await osqueryContext.security.authc.getCurrentUser(request)?.username; + const currentUser = coreContext.security.authc.getCurrentUser()?.username; const { response: osqueryAction, fleetActionsCount } = await createActionHandler( osqueryContext, request.body, diff --git a/x-pack/plugins/osquery/server/routes/pack/create_pack_route.ts b/x-pack/plugins/osquery/server/routes/pack/create_pack_route.ts index d69182762c8e2..31f9395d2174e 100644 --- a/x-pack/plugins/osquery/server/routes/pack/create_pack_route.ts +++ b/x-pack/plugins/osquery/server/routes/pack/create_pack_route.ts @@ -64,7 +64,7 @@ export const createPackRoute = (router: IRouter, osqueryContext: OsqueryAppConte const agentPolicyService = osqueryContext.service.getAgentPolicyService(); const packagePolicyService = osqueryContext.service.getPackagePolicyService(); - const currentUser = await osqueryContext.security.authc.getCurrentUser(request)?.username; + const currentUser = coreContext.security.authc.getCurrentUser()?.username; // eslint-disable-next-line @typescript-eslint/naming-convention const { name, description, queries, enabled, policy_ids, shards = {} } = request.body; diff --git a/x-pack/plugins/osquery/server/routes/pack/update_pack_route.ts b/x-pack/plugins/osquery/server/routes/pack/update_pack_route.ts index 5715806a19b5b..0109270f539d9 100644 --- a/x-pack/plugins/osquery/server/routes/pack/update_pack_route.ts +++ b/x-pack/plugins/osquery/server/routes/pack/update_pack_route.ts @@ -71,7 +71,7 @@ export const updatePackRoute = (router: IRouter, osqueryContext: OsqueryAppConte ); const agentPolicyService = osqueryContext.service.getAgentPolicyService(); const packagePolicyService = osqueryContext.service.getPackagePolicyService(); - const currentUser = await osqueryContext.security.authc.getCurrentUser(request)?.username; + const currentUser = coreContext.security.authc.getCurrentUser()?.username; // eslint-disable-next-line @typescript-eslint/naming-convention const { name, description, queries, enabled, policy_ids, shards = {} } = request.body; diff --git a/x-pack/plugins/osquery/server/routes/saved_query/create_saved_query_route.ts b/x-pack/plugins/osquery/server/routes/saved_query/create_saved_query_route.ts index d59f1c5ba0f8d..1be4cb24a2ea3 100644 --- a/x-pack/plugins/osquery/server/routes/saved_query/create_saved_query_route.ts +++ b/x-pack/plugins/osquery/server/routes/saved_query/create_saved_query_route.ts @@ -55,7 +55,7 @@ export const createSavedQueryRoute = (router: IRouter, osqueryContext: OsqueryAp ecs_mapping, } = request.body; - const currentUser = await osqueryContext.security.authc.getCurrentUser(request)?.username; + const currentUser = coreContext.security.authc.getCurrentUser()?.username; const conflictingEntries = await savedObjectsClient.find({ type: savedQuerySavedObjectType, diff --git a/x-pack/plugins/osquery/server/routes/saved_query/update_saved_query_route.ts b/x-pack/plugins/osquery/server/routes/saved_query/update_saved_query_route.ts index f6bcad9d4bed4..4225f0f223cd3 100644 --- a/x-pack/plugins/osquery/server/routes/saved_query/update_saved_query_route.ts +++ b/x-pack/plugins/osquery/server/routes/saved_query/update_saved_query_route.ts @@ -51,7 +51,7 @@ export const updateSavedQueryRoute = (router: IRouter, osqueryContext: OsqueryAp async (context, request, response) => { const coreContext = await context.core; const savedObjectsClient = coreContext.savedObjects.client; - const currentUser = await osqueryContext.security.authc.getCurrentUser(request)?.username; + const currentUser = coreContext.security.authc.getCurrentUser()?.username; const { id,