-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[Fleet] Tagging of integration assets #137184
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
Merged
juliaElastic
merged 18 commits into
elastic:main
from
juliaElastic:feat/saved-object-tagging
Jul 29, 2022
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
90e46c9
WIP saved object tagging
juliaElastic cfe84a8
fixing plugin usage
juliaElastic 77ec521
added logic to create tags before assigning
juliaElastic d7cbcf4
moved constants out
juliaElastic c4373f9
fixed tests, added span
juliaElastic c7512b2
added unit test to tagKibanaAssets
juliaElastic 7706aab
fix test
juliaElastic d00478b
fix types
juliaElastic a5fa279
fixed tests
juliaElastic 3f92ebe
fixed tests
juliaElastic 4382894
fix types
juliaElastic 766ee1a
fix sot tests by loading empty kibana archive
juliaElastic f74e91d
added tag checking to api integration test
juliaElastic a430602
Merge branch 'main' into feat/saved-object-tagging
kibanamachine df7b186
Merge branch 'main' into feat/saved-object-tagging
kibanamachine 7660613
added refresh option to speed up tagging assets
juliaElastic d288690
fixed tests
juliaElastic 0c9f93e
workaround to prevent installing fleet packages in SOT functional tests
juliaElastic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
127 changes: 127 additions & 0 deletions
127
x-pack/plugins/fleet/server/services/epm/kibana/assets/tag_assets.test.ts
This file contains hidden or 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,127 @@ | ||
| /* | ||
| * 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 { tagKibanaAssets } from './tag_assets'; | ||
|
|
||
| describe('tagKibanaAssets', () => { | ||
| const savedObjectTagAssignmentService = { | ||
| updateTagAssignments: jest.fn(), | ||
| } as any; | ||
| const savedObjectTagClient = { | ||
| getAll: jest.fn(), | ||
| create: jest.fn(), | ||
| } as any; | ||
|
|
||
| beforeEach(() => { | ||
| savedObjectTagAssignmentService.updateTagAssignments.mockReset(); | ||
| savedObjectTagClient.getAll.mockReset(); | ||
| savedObjectTagClient.create.mockReset(); | ||
| }); | ||
|
|
||
| it('should create Managed and System tags when tagKibanaAssets with System package', async () => { | ||
| savedObjectTagClient.getAll.mockResolvedValue([]); | ||
| savedObjectTagClient.create.mockImplementation(({ name }: { name: string }) => | ||
| Promise.resolve({ id: name.toLowerCase(), name }) | ||
| ); | ||
| const kibanaAssets = { dashboard: [{ id: 'dashboard1', type: 'dashboard' }] } as any; | ||
|
|
||
| await tagKibanaAssets({ | ||
| savedObjectTagAssignmentService, | ||
| savedObjectTagClient, | ||
| kibanaAssets, | ||
| pkgTitle: 'System', | ||
| pkgName: 'system', | ||
| }); | ||
|
|
||
| expect(savedObjectTagClient.create).toHaveBeenCalledWith( | ||
| { | ||
| name: 'Managed', | ||
| description: '', | ||
| color: '#FFFFFF', | ||
| }, | ||
| { id: 'managed', overwrite: true, refresh: false } | ||
| ); | ||
| expect(savedObjectTagClient.create).toHaveBeenCalledWith( | ||
| { | ||
| name: 'System', | ||
| description: '', | ||
| color: '#FFFFFF', | ||
| }, | ||
| { id: 'system', overwrite: true, refresh: false } | ||
| ); | ||
| expect(savedObjectTagAssignmentService.updateTagAssignments).toHaveBeenCalledWith({ | ||
| tags: ['managed', 'system'], | ||
| assign: kibanaAssets.dashboard, | ||
| unassign: [], | ||
| refresh: false, | ||
| }); | ||
| }); | ||
|
|
||
| it('should only assign Managed and System tags when tags already exist', async () => { | ||
| savedObjectTagClient.getAll.mockResolvedValue([ | ||
| { id: 'managed', name: 'Managed' }, | ||
| { id: 'system', name: 'System' }, | ||
| ]); | ||
| const kibanaAssets = { dashboard: [{ id: 'dashboard1', type: 'dashboard' }] } as any; | ||
|
|
||
| await tagKibanaAssets({ | ||
| savedObjectTagAssignmentService, | ||
| savedObjectTagClient, | ||
| kibanaAssets, | ||
| pkgTitle: 'System', | ||
| pkgName: 'system', | ||
| }); | ||
|
|
||
| expect(savedObjectTagClient.create).not.toHaveBeenCalled(); | ||
| expect(savedObjectTagAssignmentService.updateTagAssignments).toHaveBeenCalledWith({ | ||
| tags: ['managed', 'system'], | ||
| assign: kibanaAssets.dashboard, | ||
| unassign: [], | ||
| refresh: false, | ||
| }); | ||
| }); | ||
|
|
||
| it('should skip non taggable asset types', async () => { | ||
| savedObjectTagClient.getAll.mockResolvedValue([]); | ||
| savedObjectTagClient.create.mockImplementation(({ name }: { name: string }) => | ||
| Promise.resolve({ id: name.toLowerCase(), name }) | ||
| ); | ||
| const kibanaAssets = { | ||
| dashboard: [{ id: 'dashboard1', type: 'dashboard' }], | ||
| search: [{ id: 's1', type: 'search' }], | ||
| visualization: [{ id: 'v1', type: 'visualization' }], | ||
| } as any; | ||
|
|
||
| await tagKibanaAssets({ | ||
| savedObjectTagAssignmentService, | ||
| savedObjectTagClient, | ||
| kibanaAssets, | ||
| pkgTitle: 'System', | ||
| pkgName: 'system', | ||
| }); | ||
|
|
||
| expect(savedObjectTagAssignmentService.updateTagAssignments).toHaveBeenCalledWith({ | ||
| tags: ['managed', 'system'], | ||
| assign: [...kibanaAssets.dashboard, ...kibanaAssets.visualization], | ||
| unassign: [], | ||
| refresh: false, | ||
| }); | ||
| }); | ||
|
|
||
| it('should do nothing if no taggable assets', async () => { | ||
| const kibanaAssets = { search: [{ id: 's1', type: 'search' }] } as any; | ||
|
|
||
| await tagKibanaAssets({ | ||
| savedObjectTagAssignmentService, | ||
| savedObjectTagClient, | ||
| kibanaAssets, | ||
| pkgTitle: 'System', | ||
| pkgName: 'system', | ||
| }); | ||
|
|
||
| expect(savedObjectTagAssignmentService.updateTagAssignments).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
80 changes: 80 additions & 0 deletions
80
x-pack/plugins/fleet/server/services/epm/kibana/assets/tag_assets.ts
This file contains hidden or 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,80 @@ | ||
| /* | ||
| * 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 { taggableTypes } from '@kbn/saved-objects-tagging-plugin/common/constants'; | ||
| import type { IAssignmentService, ITagsClient } from '@kbn/saved-objects-tagging-plugin/server'; | ||
|
|
||
| import type { KibanaAssetType } from '../../../../../common'; | ||
|
|
||
| import type { ArchiveAsset } from './install'; | ||
|
|
||
| const TAG_COLOR = '#FFFFFF'; | ||
| const MANAGED_TAG_NAME = 'Managed'; | ||
| const MANAGED_TAG_ID = 'managed'; | ||
|
|
||
| export async function tagKibanaAssets({ | ||
| savedObjectTagAssignmentService, | ||
| savedObjectTagClient, | ||
| kibanaAssets, | ||
| pkgTitle, | ||
| pkgName, | ||
| }: { | ||
| savedObjectTagAssignmentService: IAssignmentService; | ||
| savedObjectTagClient: ITagsClient; | ||
| kibanaAssets: Record<KibanaAssetType, ArchiveAsset[]>; | ||
| pkgTitle: string; | ||
| pkgName: string; | ||
| }) { | ||
| const taggableAssets = Object.entries(kibanaAssets).flatMap(([assetType, assets]) => { | ||
| if (!taggableTypes.includes(assetType as KibanaAssetType)) { | ||
| return []; | ||
| } | ||
|
|
||
| if (!assets.length) { | ||
| return []; | ||
| } | ||
|
|
||
| return assets; | ||
| }); | ||
|
|
||
| // no assets to tag | ||
| if (taggableAssets.length === 0) { | ||
| return; | ||
| } | ||
|
|
||
| const allTags = await savedObjectTagClient.getAll(); | ||
| let managedTag = allTags.find((tag) => tag.name === MANAGED_TAG_NAME); | ||
| if (!managedTag) { | ||
| managedTag = await savedObjectTagClient.create( | ||
| { | ||
| name: MANAGED_TAG_NAME, | ||
| description: '', | ||
| color: TAG_COLOR, | ||
| }, | ||
| { id: MANAGED_TAG_ID, overwrite: true, refresh: false } | ||
| ); | ||
| } | ||
|
|
||
| let packageTag = allTags.find((tag) => tag.name === pkgTitle); | ||
| if (!packageTag) { | ||
| packageTag = await savedObjectTagClient.create( | ||
| { | ||
| name: pkgTitle, | ||
| description: '', | ||
| color: TAG_COLOR, | ||
| }, | ||
| { id: pkgName, overwrite: true, refresh: false } | ||
| ); | ||
| } | ||
|
|
||
| await savedObjectTagAssignmentService.updateTagAssignments({ | ||
| tags: [managedTag.id, packageTag.id], | ||
| assign: taggableAssets, | ||
| unassign: [], | ||
| refresh: false, | ||
| }); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.