-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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 issue with document rejection by upgrading osquery_manager package and rolling over indices on upgrade #148991
Changes from all commits
190dd47
0d108bf
788a1c0
6a6641d
7915504
7d9cd2b
02d3c3f
6df1c42
86e6998
197d573
3a12288
1bec2c6
2574f24
b3794fd
67e44ac
a2971b3
79900fb
2ab7c4f
3b36267
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You seem to be pinning the version to The reason this looks strange to me is because this PR is being merged to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank @paul-tavares! Thanks for raising the issue :) I haven’t really think about it, but the goal is to force upgrade older version than 1.6.0 to 1.6.0 which fixed the issue. The rest - update to any other versions are not crucial to us. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @paul-tavares - osquery doesn't normally upgrade it's package automatically. We wanted to make an exception in this case since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
}), | ||
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) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't need to be changed, but if you would like to avoid for (const dataStream of dataStreams.data_streams) {
[....]
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this be done in parallel? Wondering if you could use |
||
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); | ||
} | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: as the upgrade is conditional, could this be renamed to something like
upgradeIntegrationIfNeeded
orcheckAndUpgradeIntegrationIfNeeded