Skip to content
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

[Fleet] Tagging of integration assets #137184

Merged
merged 18 commits into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion x-pack/plugins/fleet/kibana.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"server": true,
"ui": true,
"configPath": ["xpack", "fleet"],
"requiredPlugins": ["licensing", "data", "encryptedSavedObjects", "navigation", "customIntegrations", "share", "spaces", "security", "unifiedSearch"],
"requiredPlugins": ["licensing", "data", "encryptedSavedObjects", "navigation", "customIntegrations", "share", "spaces", "security", "unifiedSearch", "savedObjectsTagging"],
"optionalPlugins": ["features", "cloud", "usageCollection", "home", "globalSearch", "telemetry", "discover", "ingestPipelines"],
"extraPublicDirs": ["common"],
"requiredBundles": ["kibanaReact", "cloud", "esUiShared", "infra", "kibanaUtils", "usageCollection", "unifiedSearch"]
Expand Down
5 changes: 5 additions & 0 deletions x-pack/plugins/fleet/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ import type { CloudSetup } from '@kbn/cloud-plugin/server';

import type { SpacesPluginStart } from '@kbn/spaces-plugin/server';

import type { SavedObjectTaggingStart } from '@kbn/saved-objects-tagging-plugin/server';

import type { FleetConfigType } from '../common/types';
import type { FleetAuthz } from '../common';
import type { ExperimentalFeatures } from '../common/experimental_features';
Expand Down Expand Up @@ -115,6 +117,7 @@ export interface FleetStartDeps {
encryptedSavedObjects: EncryptedSavedObjectsPluginStart;
security: SecurityPluginStart;
telemetry?: TelemetryPluginStart;
savedObjectsTagging: SavedObjectTaggingStart;
}

export interface FleetAppContext {
Expand All @@ -128,6 +131,7 @@ export interface FleetAppContext {
configInitialValue: FleetConfigType;
experimentalFeatures: ExperimentalFeatures;
savedObjects: SavedObjectsServiceStart;
savedObjectsTagging?: SavedObjectTaggingStart;
isProductionMode: PluginInitializerContext['env']['mode']['prod'];
kibanaVersion: PluginInitializerContext['env']['packageInfo']['version'];
kibanaBranch: PluginInitializerContext['env']['packageInfo']['branch'];
Expand Down Expand Up @@ -400,6 +404,7 @@ export class FleetPlugin
this.configInitialValue.enableExperimental || []
),
savedObjects: core.savedObjects,
savedObjectsTagging: plugins.savedObjectsTagging,
isProductionMode: this.isProductionMode,
kibanaVersion: this.kibanaVersion,
kibanaBranch: this.kibanaBranch,
Expand Down
11 changes: 11 additions & 0 deletions x-pack/plugins/fleet/server/services/app_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import type { SecurityPluginStart, SecurityPluginSetup } from '@kbn/security-plu

import type { CloudSetup } from '@kbn/cloud-plugin/server';

import type { SavedObjectTaggingStart } from '@kbn/saved-objects-tagging-plugin/server';

import type { FleetConfigType } from '../../common/types';
import type { ExperimentalFeatures } from '../../common/experimental_features';
import type {
Expand Down Expand Up @@ -58,6 +60,7 @@ class AppContextService {
private httpSetup?: HttpServiceSetup;
private externalCallbacks: ExternalCallbacksStorage = new Map();
private telemetryEventsSender: TelemetryEventsSender | undefined;
private savedObjectsTagging: SavedObjectTaggingStart | undefined;

public start(appContext: FleetAppContext) {
this.data = appContext.data;
Expand All @@ -75,6 +78,7 @@ class AppContextService {
this.kibanaBranch = appContext.kibanaBranch;
this.httpSetup = appContext.httpSetup;
this.telemetryEventsSender = appContext.telemetryEventsSender;
this.savedObjectsTagging = appContext.savedObjectsTagging;

if (appContext.config$) {
this.config$ = appContext.config$;
Expand Down Expand Up @@ -143,6 +147,13 @@ class AppContextService {
return this.savedObjects;
}

public getSavedObjectsTagging() {
if (!this.savedObjectsTagging) {
throw new Error('Saved object tagging start service not set.');
}
return this.savedObjectsTagging;
}

public getInternalUserSOClient(request: KibanaRequest) {
// soClient as kibana internal users, be careful on how you use it, security is not enabled
return appContextService.getSavedObjects().getScopedClient(request, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import type { SavedObjectsImportSuccess, SavedObjectsImportFailure } from '@kbn/
import { createListStream } from '@kbn/utils';
import { partition } from 'lodash';

import type { IAssignmentService, ITagsClient } from '@kbn/saved-objects-tagging-plugin/server';

import { taggableTypes } from '@kbn/saved-objects-tagging-plugin/common/constants';

import { PACKAGES_SAVED_OBJECT_TYPE } from '../../../../../common';
import { getAsset, getPathParts } from '../../archive';
import { KibanaAssetType, KibanaSavedObjectType } from '../../../../types';
Expand Down Expand Up @@ -128,15 +132,21 @@ export async function installKibanaAssets(options: {
export async function installKibanaAssetsAndReferences({
savedObjectsClient,
savedObjectsImporter,
savedObjectTagAssignmentService,
savedObjectTagClient,
logger,
pkgName,
pkgTitle,
paths,
installedPkg,
}: {
savedObjectsClient: SavedObjectsClientContract;
savedObjectsImporter: Pick<SavedObjectsImporter, 'import' | 'resolveImportErrors'>;
savedObjectTagAssignmentService: IAssignmentService;
savedObjectTagClient: ITagsClient;
logger: Logger;
pkgName: string;
pkgTitle: string;
paths: string[];
installedPkg?: SavedObject<Installation>;
}) {
Expand All @@ -156,9 +166,67 @@ export async function installKibanaAssetsAndReferences({
kibanaAssets,
});

await tagKibanaAssets({
savedObjectTagAssignmentService,
savedObjectTagClient,
kibanaAssets,
pkgTitle,
});

return installedKibanaAssetsRefs;
}

export async function tagKibanaAssets({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it'd be possible to wrap this in an APM span and track how it impacts installation performance for packages?

savedObjectTagAssignmentService,
savedObjectTagClient,
kibanaAssets,
pkgTitle,
}: {
savedObjectTagAssignmentService: IAssignmentService;
savedObjectTagClient: ITagsClient;
kibanaAssets: Record<KibanaAssetType, ArchiveAsset[]>;
pkgTitle: string;
}) {
const taggableAssets = Object.entries(kibanaAssets).flatMap(([assetType, assets]) => {
if (!taggableTypes.includes(assetType as KibanaAssetType)) {
return [];
}

if (!assets.length) {
return [];
}

return assets;
});

const managedTagName = 'Managed';
const tagColor = '#FFFFFF';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: it will be nice to move this to a constant

const allTags = await savedObjectTagClient.getAll();
let managedTag = allTags.find((tag) => tag.name === managedTagName);
if (!managedTag) {
managedTag = await savedObjectTagClient.create({
juliaElastic marked this conversation as resolved.
Show resolved Hide resolved
name: managedTagName,
description: '',
color: tagColor,
});
}

let packageTag = allTags.find((tag) => tag.name === pkgTitle);
if (!packageTag) {
packageTag = await savedObjectTagClient.create({
name: pkgTitle,
description: '',
color: tagColor,
});
}

savedObjectTagAssignmentService.updateTagAssignments({
tags: [managedTag.id, packageTag.id],
assign: taggableAssets,
unassign: [],
});
}

export const deleteKibanaInstalledRefs = async (
savedObjectsClient: SavedObjectsClientContract,
pkgName: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import type {
} from '@kbn/core/server';
import { SavedObjectsErrorHelpers } from '@kbn/core/server';

import type { IAssignmentService, ITagsClient } from '@kbn/saved-objects-tagging-plugin/server';

import {
MAX_TIME_COMPLETE_INSTALL,
ASSETS_SAVED_OBJECT_TYPE,
Expand Down Expand Up @@ -56,6 +58,8 @@ import { withPackageSpan } from './utils';
export async function _installPackage({
savedObjectsClient,
savedObjectsImporter,
savedObjectTagAssignmentService,
savedObjectTagClient,
esClient,
logger,
installedPkg,
Expand All @@ -68,6 +72,8 @@ export async function _installPackage({
}: {
savedObjectsClient: SavedObjectsClientContract;
savedObjectsImporter: Pick<SavedObjectsImporter, 'import' | 'resolveImportErrors'>;
savedObjectTagAssignmentService: IAssignmentService;
savedObjectTagClient: ITagsClient;
esClient: ElasticsearchClient;
logger: Logger;
installedPkg?: SavedObject<Installation>;
Expand All @@ -78,7 +84,7 @@ export async function _installPackage({
spaceId: string;
verificationResult?: PackageVerificationResult;
}): Promise<AssetReference[]> {
const { name: pkgName, version: pkgVersion } = packageInfo;
const { name: pkgName, version: pkgVersion, title: pkgTitle } = packageInfo;

try {
// if some installation already exists
Expand Down Expand Up @@ -120,7 +126,10 @@ export async function _installPackage({
installKibanaAssetsAndReferences({
savedObjectsClient,
savedObjectsImporter,
savedObjectTagAssignmentService,
savedObjectTagClient,
pkgName,
pkgTitle,
paths,
installedPkg,
logger,
Expand Down
10 changes: 10 additions & 0 deletions x-pack/plugins/fleet/server/services/epm/packages/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,11 +362,21 @@ async function installPackageFromRegistry({
.getSavedObjects()
.createImporter(savedObjectsClient);

const savedObjectTagAssignmentService = appContextService
.getSavedObjectsTagging()
.createInternalAssignmentService({ client: savedObjectsClient });

const savedObjectTagClient = appContextService
.getSavedObjectsTagging()
.createTagClient({ client: savedObjectsClient });

// try installing the package, if there was an error, call error handler and rethrow
// @ts-expect-error status is string instead of InstallResult.status 'installed' | 'already_installed'
return await _installPackage({
savedObjectsClient,
savedObjectsImporter,
savedObjectTagAssignmentService,
savedObjectTagClient,
esClient,
logger,
installedPkg,
Expand Down