[8.6] [Fleet] added a logic to use destinationId when tagging imported SOs (#147439) (#147528)

# Backport

This will backport the following commits from `main` to `8.6`:
- [[Fleet] added a logic to use `destinationId` when tagging imported
SOs (#147439)](https://github.com/elastic/kibana/pull/147439)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Julia
Bardi","email":"90178898+juliaElastic@users.noreply.github.com"},"sourceCommit":{"committedDate":"2022-12-14T14:09:23Z","message":"[Fleet]
added a logic to use `destinationId` when tagging imported SOs
(#147439)\n\n## Summary\r\n\r\nFixes
https://github.com/elastic/kibana/issues/147364\r\n\r\nWhen installing
an integration, and some assets already exist in Kibana,\r\nthe imported
SOs will recieve a newly generated ID. This caused a\r\nproblem
previously when the tagging logic didn't find the SOs with
their\r\noriginal ID.\r\nThe fix takes the new `destinationId` of those
SOs, so the tagging can\r\ncomplete successfully.\r\n\r\n**Steps to
verify**\r\n1. Add system integration in default space\r\n2. Enroll an
agent and wait for system metrics to be ingested\r\n3. Create an Admin
space\r\n4. Remove the system package policy, and uninstall the system
package\r\nfrom Default space\r\n5. Go to Admin space and add system
integration again\r\n6. Verify that the integration is added
successfully, assets are created\r\nin Admin space and assets are tagged
with Managed and System tags.\r\n\r\n\r\n### Checklist\r\n\r\n- [x]
[Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios\r\n\r\nCo-authored-by: Kibana Machine
<42973632+kibanamachine@users.noreply.github.com>","sha":"7cee9fff787f1d647142d711f9f2618dd9dbc266","branchLabelMapping":{"^v8.7.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Team:Fleet","v8.6.0","v8.7.0","v8.5.4"],"number":147439,"url":"https://github.com/elastic/kibana/pull/147439","mergeCommit":{"message":"[Fleet]
added a logic to use `destinationId` when tagging imported SOs
(#147439)\n\n## Summary\r\n\r\nFixes
https://github.com/elastic/kibana/issues/147364\r\n\r\nWhen installing
an integration, and some assets already exist in Kibana,\r\nthe imported
SOs will recieve a newly generated ID. This caused a\r\nproblem
previously when the tagging logic didn't find the SOs with
their\r\noriginal ID.\r\nThe fix takes the new `destinationId` of those
SOs, so the tagging can\r\ncomplete successfully.\r\n\r\n**Steps to
verify**\r\n1. Add system integration in default space\r\n2. Enroll an
agent and wait for system metrics to be ingested\r\n3. Create an Admin
space\r\n4. Remove the system package policy, and uninstall the system
package\r\nfrom Default space\r\n5. Go to Admin space and add system
integration again\r\n6. Verify that the integration is added
successfully, assets are created\r\nin Admin space and assets are tagged
with Managed and System tags.\r\n\r\n\r\n### Checklist\r\n\r\n- [x]
[Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios\r\n\r\nCo-authored-by: Kibana Machine
<42973632+kibanamachine@users.noreply.github.com>","sha":"7cee9fff787f1d647142d711f9f2618dd9dbc266"}},"sourceBranch":"main","suggestedTargetBranches":["8.6","8.5"],"targetPullRequestStates":[{"branch":"8.6","label":"v8.6.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.7.0","labelRegex":"^v8.7.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/147439","number":147439,"mergeCommit":{"message":"[Fleet]
added a logic to use `destinationId` when tagging imported SOs
(#147439)\n\n## Summary\r\n\r\nFixes
https://github.com/elastic/kibana/issues/147364\r\n\r\nWhen installing
an integration, and some assets already exist in Kibana,\r\nthe imported
SOs will recieve a newly generated ID. This caused a\r\nproblem
previously when the tagging logic didn't find the SOs with
their\r\noriginal ID.\r\nThe fix takes the new `destinationId` of those
SOs, so the tagging can\r\ncomplete successfully.\r\n\r\n**Steps to
verify**\r\n1. Add system integration in default space\r\n2. Enroll an
agent and wait for system metrics to be ingested\r\n3. Create an Admin
space\r\n4. Remove the system package policy, and uninstall the system
package\r\nfrom Default space\r\n5. Go to Admin space and add system
integration again\r\n6. Verify that the integration is added
successfully, assets are created\r\nin Admin space and assets are tagged
with Managed and System tags.\r\n\r\n\r\n### Checklist\r\n\r\n- [x]
[Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios\r\n\r\nCo-authored-by: Kibana Machine
<42973632+kibanamachine@users.noreply.github.com>","sha":"7cee9fff787f1d647142d711f9f2618dd9dbc266"}},{"branch":"8.5","label":"v8.5.4","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Julia Bardi <90178898+juliaElastic@users.noreply.github.com>
This commit is contained in:
Kibana Machine 2022-12-14 10:14:20 -05:00 committed by GitHub
parent c9ca8486c0
commit 690e4a15b3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 4 deletions

View file

@ -163,7 +163,7 @@ export async function installKibanaAssetsAndReferences({
kibanaAssets
);
await installKibanaAssets({
const importedAssets = await installKibanaAssets({
logger,
savedObjectsImporter,
pkgName,
@ -177,6 +177,7 @@ export async function installKibanaAssetsAndReferences({
pkgTitle,
pkgName,
spaceId,
importedAssets,
})
);
@ -292,7 +293,6 @@ export async function installKibanaSavedObjects({
overwrite: true,
readStream: createListStream(toBeSavedObjects),
createNewCopies: false,
refresh: false,
})
);

View file

@ -35,6 +35,7 @@ describe('tagKibanaAssets', () => {
pkgTitle: 'System',
pkgName: 'system',
spaceId: 'default',
importedAssets: [],
});
expect(savedObjectTagClient.create).toHaveBeenCalledWith(
@ -72,6 +73,7 @@ describe('tagKibanaAssets', () => {
pkgTitle: 'System',
pkgName: 'system',
spaceId: 'default',
importedAssets: [],
});
expect(savedObjectTagClient.create).not.toHaveBeenCalled();
@ -83,6 +85,29 @@ describe('tagKibanaAssets', () => {
});
});
it('should use destinationId instead of original SO id if imported asset has it', async () => {
savedObjectTagClient.get.mockResolvedValue({ name: '', color: '', description: '' });
const kibanaAssets = { dashboard: [{ id: 'dashboard1', type: 'dashboard' }] } as any;
await tagKibanaAssets({
savedObjectTagAssignmentService,
savedObjectTagClient,
kibanaAssets,
pkgTitle: 'System',
pkgName: 'system',
spaceId: 'default',
importedAssets: [{ id: 'dashboard1', destinationId: 'destination1' } as any],
});
expect(savedObjectTagClient.create).not.toHaveBeenCalled();
expect(savedObjectTagAssignmentService.updateTagAssignments).toHaveBeenCalledWith({
tags: ['fleet-managed-default', 'fleet-pkg-system-default'],
assign: [{ id: 'destination1', type: 'dashboard' }],
unassign: [],
refresh: false,
});
});
it('should skip non taggable asset types', async () => {
savedObjectTagClient.get.mockRejectedValue(new Error('tag not found'));
savedObjectTagClient.create.mockImplementation(({ name }: { name: string }) =>
@ -104,6 +129,7 @@ describe('tagKibanaAssets', () => {
pkgTitle: 'System',
pkgName: 'system',
spaceId: 'default',
importedAssets: [],
});
expect(savedObjectTagAssignmentService.updateTagAssignments).toHaveBeenCalledWith({
@ -130,6 +156,7 @@ describe('tagKibanaAssets', () => {
pkgTitle: 'System',
pkgName: 'system',
spaceId: 'default',
importedAssets: [],
});
expect(savedObjectTagAssignmentService.updateTagAssignments).not.toHaveBeenCalled();
@ -154,6 +181,7 @@ describe('tagKibanaAssets', () => {
pkgTitle: 'System',
pkgName: 'system',
spaceId: 'default',
importedAssets: [],
});
expect(savedObjectTagClient.create).not.toHaveBeenCalledWith(
@ -200,6 +228,7 @@ describe('tagKibanaAssets', () => {
pkgTitle: 'System',
pkgName: 'system',
spaceId: 'default',
importedAssets: [],
});
expect(savedObjectTagClient.create).toHaveBeenCalledWith(
@ -247,6 +276,7 @@ describe('tagKibanaAssets', () => {
pkgTitle: 'System',
pkgName: 'system',
spaceId: 'default',
importedAssets: [],
});
expect(savedObjectTagClient.create).not.toHaveBeenCalled();

View file

@ -5,6 +5,7 @@
* 2.0.
*/
import type { SavedObjectsImportSuccess } from '@kbn/core-saved-objects-common';
import { taggableTypes } from '@kbn/saved-objects-tagging-plugin/common/constants';
import type { IAssignmentService, ITagsClient } from '@kbn/saved-objects-tagging-plugin/server';
@ -29,11 +30,17 @@ interface TagAssetsParams {
pkgTitle: string;
pkgName: string;
spaceId: string;
importedAssets: SavedObjectsImportSuccess[];
}
export async function tagKibanaAssets(opts: TagAssetsParams) {
const { savedObjectTagAssignmentService, kibanaAssets } = opts;
const taggableAssets = getTaggableAssets(kibanaAssets);
const { savedObjectTagAssignmentService, kibanaAssets, importedAssets } = opts;
const getNewId = (assetId: string) =>
importedAssets.find((imported) => imported.id === assetId)?.destinationId ?? assetId;
const taggableAssets = getTaggableAssets(kibanaAssets).map((asset) => ({
...asset,
id: getNewId(asset.id),
}));
// no assets to tag
if (taggableAssets.length === 0) {