forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OSquery fix issue with document rejection by upgrading osquery_manage…
…r package and rolling over indices on upgrade (elastic#148991) (cherry picked from commit 192c739) # Conflicts: # x-pack/plugins/osquery/server/plugin.ts # x-pack/plugins/osquery/tsconfig.json
- Loading branch information
Showing
2 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
x-pack/plugins/osquery/server/utils/upgrade_integration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { satisfies } from 'semver'; | ||
import { installPackage } from '@kbn/fleet-plugin/server/services/epm/packages'; | ||
import { pkgToPkgKey } from '@kbn/fleet-plugin/server/services/epm/registry'; | ||
import { DEFAULT_SPACE_ID } from '@kbn/spaces-plugin/common/constants'; | ||
import { asyncForEach } from '@kbn/std'; | ||
import { orderBy } from 'lodash'; | ||
import type { Installation } from '@kbn/fleet-plugin/common'; | ||
import type { ElasticsearchClient } from '@kbn/core-elasticsearch-server'; | ||
import type { SavedObjectsClientContract } from '@kbn/core-saved-objects-api-server'; | ||
import type { Logger } from '@kbn/logging'; | ||
import { OSQUERY_INTEGRATION_NAME } from '../../common'; | ||
|
||
interface UpgradeIntegrationOptions { | ||
packageInfo?: Installation; | ||
client: SavedObjectsClientContract; | ||
esClient: ElasticsearchClient; | ||
logger: Logger; | ||
} | ||
|
||
// Conditionally upgrade osquery integration in order to fix 8.6.0 agent issue | ||
export const upgradeIntegration = async ({ | ||
packageInfo, | ||
client, | ||
esClient, | ||
logger, | ||
}: UpgradeIntegrationOptions) => { | ||
let updatedPackageResult; | ||
|
||
if (packageInfo && satisfies(packageInfo?.version ?? '', '<1.6.0')) { | ||
try { | ||
logger.info('Updating osquery_manager integration'); | ||
updatedPackageResult = await installPackage({ | ||
installSource: 'registry', | ||
savedObjectsClient: client, | ||
pkgkey: pkgToPkgKey({ | ||
name: packageInfo.name, | ||
version: '1.6.0', // This package upgrade is specific to a bug fix, so keeping the upgrade focused on 1.6.0 | ||
}), | ||
esClient, | ||
spaceId: packageInfo.installed_kibana_space_id || DEFAULT_SPACE_ID, | ||
// Force install the package will update the index template and the datastream write indices | ||
force: true, | ||
}); | ||
logger.info('osquery_manager integration updated'); | ||
} catch (e) { | ||
logger.error(e); | ||
} | ||
} | ||
|
||
// Check to see if the package has already been updated to at least 1.6.0 | ||
if ( | ||
satisfies(packageInfo?.version ?? '', '>=1.6.0') || | ||
updatedPackageResult?.status === 'installed' | ||
) { | ||
try { | ||
// First get all datastreams matching the pattern. | ||
const dataStreams = await esClient.indices.getDataStream({ | ||
name: `logs-${OSQUERY_INTEGRATION_NAME}.result-*`, | ||
}); | ||
|
||
// Then for each of those datastreams, we need to see if they need to rollover. | ||
await asyncForEach(dataStreams.data_streams, async (dataStream) => { | ||
const mapping = await esClient.indices.getMapping({ | ||
index: dataStream.name, | ||
}); | ||
|
||
const valuesToSort = Object.entries(mapping).map(([key, value]) => ({ | ||
index: key, | ||
mapping: value, | ||
})); | ||
|
||
// Sort by index name to get the latest index for detecting if we need to rollover | ||
const dataStreamMapping = orderBy(valuesToSort, ['index'], 'desc'); | ||
|
||
if ( | ||
dataStreamMapping && | ||
// @ts-expect-error 'properties' does not exist on type 'MappingMatchOnlyTextProperty' | ||
dataStreamMapping[0]?.mapping?.mappings?.properties?.data_stream?.properties?.dataset | ||
?.value === 'generic' | ||
) { | ||
logger.info('Rolling over index: ' + dataStream.name); | ||
await esClient.indices.rollover({ | ||
alias: dataStream.name, | ||
}); | ||
} | ||
}); | ||
} catch (e) { | ||
logger.error(e); | ||
} | ||
} | ||
}; |