From bb5e183619ebe555d4e40e412aad84fba4385006 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patryk=20Kopyci=C5=84ski?= Date: Sat, 13 Nov 2021 16:34:00 +0100 Subject: [PATCH] [Osquery] Fix packs migration script (#118453) --- .../plugins/osquery/public/components/app.tsx | 30 ++++++- .../routes/status/create_status_route.ts | 87 ++++++++++--------- 2 files changed, 73 insertions(+), 44 deletions(-) diff --git a/x-pack/plugins/osquery/public/components/app.tsx b/x-pack/plugins/osquery/public/components/app.tsx index f4c805d375351..ef249d5b8c7aa 100644 --- a/x-pack/plugins/osquery/public/components/app.tsx +++ b/x-pack/plugins/osquery/public/components/app.tsx @@ -9,7 +9,17 @@ import React, { useMemo } from 'react'; import { FormattedMessage } from '@kbn/i18n/react'; -import { EuiButtonEmpty, EuiFlexGroup, EuiFlexItem, EuiTabs, EuiTab } from '@elastic/eui'; +import { + EuiButtonEmpty, + EuiFlexGroup, + EuiFlexItem, + EuiTabs, + EuiTab, + EuiLoadingElastic, + EuiPage, + EuiPageBody, + EuiPageContent, +} from '@elastic/eui'; import { useLocation } from 'react-router-dom'; import { Container, Nav, Wrapper } from './layouts'; @@ -24,6 +34,24 @@ const OsqueryAppComponent = () => { const section = useMemo(() => location.pathname.split('/')[1] ?? 'overview', [location.pathname]); const { data: osqueryIntegration, isFetched } = useOsqueryIntegrationStatus(); + if (!isFetched) { + return ( + + + + + + + + ); + } + if (isFetched && osqueryIntegration?.install_status !== 'installed') { return ; } diff --git a/x-pack/plugins/osquery/server/routes/status/create_status_route.ts b/x-pack/plugins/osquery/server/routes/status/create_status_route.ts index 630ec8b3743c8..ae79ef851bed9 100644 --- a/x-pack/plugins/osquery/server/routes/status/create_status_route.ts +++ b/x-pack/plugins/osquery/server/routes/status/create_status_route.ts @@ -107,6 +107,50 @@ export const createStatusRoute = (router: IRouter, osqueryContext: OsqueryAppCon pkgName: OSQUERY_INTEGRATION_NAME, }); + const agentPolicyIds = uniq(map(policyPackages?.items, 'policy_id')); + const agentPolicies = mapKeys( + await agentPolicyService?.getByIds(internalSavedObjectsClient, agentPolicyIds), + 'id' + ); + + await Promise.all( + map(migrationObject.packs, async (packObject) => { + await internalSavedObjectsClient.create( + packSavedObjectType, + { + // @ts-expect-error update types + name: packObject.name, + // @ts-expect-error update types + description: packObject.description, + // @ts-expect-error update types + queries: convertPackQueriesToSO(packObject.queries), + // @ts-expect-error update types + enabled: packObject.enabled, + created_at: new Date().toISOString(), + created_by: 'system', + updated_at: new Date().toISOString(), + updated_by: 'system', + }, + { + // @ts-expect-error update types + references: packObject.policy_ids.map((policyId: string) => ({ + id: policyId, + name: agentPolicies[policyId].name, + type: AGENT_POLICY_SAVED_OBJECT_TYPE, + })), + refresh: 'wait_for', + } + ); + }) + ); + + // delete unnecessary package policies + await packagePolicyService?.delete( + internalSavedObjectsClient, + esClient, + migrationObject.packagePoliciesToDelete + ); + // updatePackagePolicies await Promise.all( map(migrationObject.agentPolicyToPackage, async (value, key) => { @@ -151,49 +195,6 @@ export const createStatusRoute = (router: IRouter, osqueryContext: OsqueryAppCon } }) ); - - const agentPolicyIds = uniq(map(policyPackages?.items, 'policy_id')); - const agentPolicies = mapKeys( - await agentPolicyService?.getByIds(internalSavedObjectsClient, agentPolicyIds), - 'id' - ); - - await Promise.all( - map(migrationObject.packs, async (packObject) => { - await internalSavedObjectsClient.create( - packSavedObjectType, - { - // @ts-expect-error update types - name: packObject.name, - // @ts-expect-error update types - description: packObject.description, - // @ts-expect-error update types - queries: convertPackQueriesToSO(packObject.queries), - // @ts-expect-error update types - enabled: packObject.enabled, - created_at: new Date().toISOString(), - created_by: 'system', - updated_at: new Date().toISOString(), - updated_by: 'system', - }, - { - // @ts-expect-error update types - references: packObject.policy_ids.map((policyId: string) => ({ - id: policyId, - name: agentPolicies[policyId].name, - type: AGENT_POLICY_SAVED_OBJECT_TYPE, - })), - refresh: 'wait_for', - } - ); - }) - ); - - await packagePolicyService?.delete( - internalSavedObjectsClient, - esClient, - migrationObject.packagePoliciesToDelete - ); // eslint-disable-next-line no-empty } catch (e) {} }