Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Osquery] Fix packs migration script #118453

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion 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,6 +34,24 @@ const OsqueryAppComponent = () => {
const section = useMemo(() => location.pathname.split('/')[1] ?? 'overview', [location.pathname]);
const { data: osqueryIntegration, isFetched } = useOsqueryIntegrationStatus();

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
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