Skip to content

Commit

Permalink
[Osquery] Fix packs migration script (#118453)
Browse files Browse the repository at this point in the history
# Conflicts:
#	x-pack/plugins/osquery/public/components/app.tsx
  • Loading branch information
patrykkopycinski committed Nov 13, 2021
1 parent f356797 commit 136d899
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 45 deletions.
32 changes: 30 additions & 2 deletions x-pack/plugins/osquery/public/components/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -24,7 +34,25 @@ const OsqueryAppComponent = () => {
const section = useMemo(() => location.pathname.split('/')[1] ?? 'overview', [location.pathname]);
const { data: osqueryIntegration, isFetched } = useOsqueryIntegrationStatus();

if (isFetched && osqueryIntegration.install_status !== 'installed') {
if (!isFetched) {
return (
<EuiPage paddingSize="none">
<EuiPageBody>
<EuiPageContent
verticalPosition="center"
horizontalPosition="center"
paddingSize="none"
color="subdued"
hasShadow={false}
>
<EuiLoadingElastic size="xxl" />
</EuiPageContent>
</EuiPageBody>
</EuiPage>
);
}

if (isFetched && osqueryIntegration?.install_status !== 'installed') {
return <OsqueryAppEmptyState />;
}

Expand Down
87 changes: 44 additions & 43 deletions x-pack/plugins/osquery/server/routes/status/create_status_route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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) {}
}
Expand Down

0 comments on commit 136d899

Please sign in to comment.