Skip to content

Commit

Permalink
[Fleet] Make asset tags space aware (#144066)
Browse files Browse the repository at this point in the history
* fix: use space ID in managed tag SO ID

* Add SO migration

* add integration test for installing pkg in 2 spaces

* Revert "Add SO migration"

This reverts commit 4aeeea658c79d30cfb7ad96090b87418a3b72ad2.

* use legacy tags if they exist

* add tags integration test

* test working in isolation

* neaten tests

* remove test pkg

* revert test file

* tidy for PR

* fix type errors
  • Loading branch information
hop-dev authored Oct 27, 2022
1 parent 30f585b commit 2a8e933
Show file tree
Hide file tree
Showing 6 changed files with 380 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ export async function installKibanaAssetsAndReferences({
pkgTitle,
paths,
installedPkg,
spaceId,
}: {
savedObjectsClient: SavedObjectsClientContract;
savedObjectsImporter: Pick<ISavedObjectsImporter, 'import' | 'resolveImportErrors'>;
Expand All @@ -151,6 +152,7 @@ export async function installKibanaAssetsAndReferences({
pkgTitle: string;
paths: string[];
installedPkg?: SavedObject<Installation>;
spaceId: string;
}) {
const kibanaAssets = await getKibanaAssets(paths);
if (installedPkg) await deleteKibanaSavedObjectsAssets({ savedObjectsClient, installedPkg });
Expand All @@ -167,14 +169,14 @@ export async function installKibanaAssetsAndReferences({
pkgName,
kibanaAssets,
});

await withPackageSpan('Create and assign package tags', () =>
tagKibanaAssets({
savedObjectTagAssignmentService,
savedObjectTagClient,
kibanaAssets,
pkgTitle,
pkgName,
spaceId,
})
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ describe('tagKibanaAssets', () => {
updateTagAssignments: jest.fn(),
} as any;
const savedObjectTagClient = {
getAll: jest.fn(),
get: jest.fn(),
create: jest.fn(),
} as any;

beforeEach(() => {
savedObjectTagAssignmentService.updateTagAssignments.mockReset();
savedObjectTagClient.getAll.mockReset();
savedObjectTagClient.get.mockReset();
savedObjectTagClient.create.mockReset();
});

it('should create Managed and System tags when tagKibanaAssets with System package', async () => {
savedObjectTagClient.getAll.mockResolvedValue([]);
it('should create Managed and System tags when tagKibanaAssets with System package when no tags exist', async () => {
savedObjectTagClient.get.mockRejectedValue(new Error('not found'));
savedObjectTagClient.create.mockImplementation(({ name }: { name: string }) =>
Promise.resolve({ id: name.toLowerCase(), name })
);
Expand All @@ -34,6 +34,7 @@ describe('tagKibanaAssets', () => {
kibanaAssets,
pkgTitle: 'System',
pkgName: 'system',
spaceId: 'default',
});

expect(savedObjectTagClient.create).toHaveBeenCalledWith(
Expand All @@ -42,29 +43,26 @@ describe('tagKibanaAssets', () => {
description: '',
color: '#FFFFFF',
},
{ id: 'managed', overwrite: true, refresh: false }
{ id: 'fleet-managed-default', overwrite: true, refresh: false }
);
expect(savedObjectTagClient.create).toHaveBeenCalledWith(
{
name: 'System',
description: '',
color: '#FFFFFF',
},
{ id: 'system', overwrite: true, refresh: false }
{ id: 'fleet-pkg-system-default', overwrite: true, refresh: false }
);
expect(savedObjectTagAssignmentService.updateTagAssignments).toHaveBeenCalledWith({
tags: ['managed', 'system'],
tags: ['fleet-managed-default', 'fleet-pkg-system-default'],
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' },
]);
savedObjectTagClient.get.mockResolvedValue({ name: '', color: '', description: '' });
const kibanaAssets = { dashboard: [{ id: 'dashboard1', type: 'dashboard' }] } as any;

await tagKibanaAssets({
Expand All @@ -73,19 +71,20 @@ describe('tagKibanaAssets', () => {
kibanaAssets,
pkgTitle: 'System',
pkgName: 'system',
spaceId: 'default',
});

expect(savedObjectTagClient.create).not.toHaveBeenCalled();
expect(savedObjectTagAssignmentService.updateTagAssignments).toHaveBeenCalledWith({
tags: ['managed', 'system'],
tags: ['fleet-managed-default', 'fleet-pkg-system-default'],
assign: kibanaAssets.dashboard,
unassign: [],
refresh: false,
});
});

it('should skip non taggable asset types', async () => {
savedObjectTagClient.getAll.mockResolvedValue([]);
savedObjectTagClient.get.mockRejectedValue(new Error('tag not found'));
savedObjectTagClient.create.mockImplementation(({ name }: { name: string }) =>
Promise.resolve({ id: name.toLowerCase(), name })
);
Expand All @@ -104,10 +103,11 @@ describe('tagKibanaAssets', () => {
kibanaAssets,
pkgTitle: 'System',
pkgName: 'system',
spaceId: 'default',
});

expect(savedObjectTagAssignmentService.updateTagAssignments).toHaveBeenCalledWith({
tags: ['managed', 'system'],
tags: ['fleet-managed-default', 'fleet-pkg-system-default'],
assign: [
...kibanaAssets.dashboard,
...kibanaAssets.search,
Expand All @@ -129,8 +129,132 @@ describe('tagKibanaAssets', () => {
kibanaAssets,
pkgTitle: 'System',
pkgName: 'system',
spaceId: 'default',
});

expect(savedObjectTagAssignmentService.updateTagAssignments).not.toHaveBeenCalled();
});

it('should use legacy managed tag if it exists', async () => {
savedObjectTagClient.get.mockImplementation(async (id: string) => {
if (id === 'managed') return { name: 'managed', description: '', color: '' };

throw new Error('not found');
});

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',
spaceId: 'default',
});

expect(savedObjectTagClient.create).not.toHaveBeenCalledWith(
{
name: 'Managed',
description: '',
color: '#FFFFFF',
},
{ id: 'fleet-managed-default', overwrite: true, refresh: false }
);

expect(savedObjectTagClient.create).toHaveBeenCalledWith(
{
name: 'System',
description: '',
color: '#FFFFFF',
},
{ id: 'fleet-pkg-system-default', overwrite: true, refresh: false }
);
expect(savedObjectTagAssignmentService.updateTagAssignments).toHaveBeenCalledWith({
tags: ['managed', 'fleet-pkg-system-default'],
assign: kibanaAssets.dashboard,
unassign: [],
refresh: false,
});
});

it('should use legacy package tag if it exists', async () => {
savedObjectTagClient.get.mockImplementation(async (id: string) => {
if (id === 'system') return { name: 'system', description: '', color: '' };

throw new Error('not found');
});

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',
spaceId: 'default',
});

expect(savedObjectTagClient.create).toHaveBeenCalledWith(
{
name: 'Managed',
description: '',
color: '#FFFFFF',
},
{ id: 'fleet-managed-default', overwrite: true, refresh: false }
);

expect(savedObjectTagClient.create).not.toHaveBeenCalledWith(
{
name: 'System',
description: '',
color: '#FFFFFF',
},
{ id: 'system', overwrite: true, refresh: false }
);
expect(savedObjectTagAssignmentService.updateTagAssignments).toHaveBeenCalledWith({
tags: ['fleet-managed-default', 'system'],
assign: kibanaAssets.dashboard,
unassign: [],
refresh: false,
});
});

it('should use both legacy tags if they exist', async () => {
savedObjectTagClient.get.mockImplementation(async (id: string) => {
if (id === 'managed') return { name: 'managed', description: '', color: '' };
if (id === 'system') return { name: 'system', description: '', color: '' };

throw new Error('not found');
});

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',
spaceId: 'default',
});

expect(savedObjectTagClient.create).not.toHaveBeenCalled();
expect(savedObjectTagAssignmentService.updateTagAssignments).toHaveBeenCalledWith({
tags: ['managed', 'system'],
assign: kibanaAssets.dashboard,
unassign: [],
refresh: false,
});
});
});
Loading

0 comments on commit 2a8e933

Please sign in to comment.