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] Tag assets based on definitions in integration tag.yml #162643

Merged
merged 15 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions x-pack/plugins/fleet/common/types/models/package_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ export interface PackageSpecManifest {
RegistryElasticsearch,
'index_template.settings' | 'index_template.mappings' | 'index_template.data_stream'
>;
asset_tags?: PackageSpecTags[];
}
export interface PackageSpecTags {
text: string;
asset_types?: string[];
asset_ids?: string[];
}

export type PackageSpecPackageType = 'integration' | 'input';
Expand Down
6 changes: 6 additions & 0 deletions x-pack/plugins/fleet/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ import {
import { FleetActionsClient, type FleetActionsClientInterface } from './services/actions';
import type { FilesClientFactory } from './services/files/types';
import { PolicyWatcher } from './services/agent_policy_watch';
import { getPackageSpecTagId } from './services/epm/kibana/assets/tag_assets';

export interface FleetSetupDeps {
security: SecurityPluginSetup;
Expand Down Expand Up @@ -232,6 +233,10 @@ export interface FleetStartContract {
messageSigningService: MessageSigningServiceInterface;
uninstallTokenService: UninstallTokenServiceInterface;
createFleetActionsClient: (packageName: string) => FleetActionsClientInterface;
/*
Function exported to allow creating unique ids for saved object tags
*/
getPackageSpecTagId: (spaceId: string, pkgName: string, tagName: string) => string;
}

export class FleetPlugin
Expand Down Expand Up @@ -591,6 +596,7 @@ export class FleetPlugin
createFleetActionsClient(packageName: string) {
return new FleetActionsClient(core.elasticsearch.client.asInternalUser, packageName);
},
getPackageSpecTagId,
};
}

Expand Down
29 changes: 21 additions & 8 deletions x-pack/plugins/fleet/server/services/epm/archive/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import type {
PackageSpecManifest,
RegistryDataStreamRoutingRules,
RegistryDataStreamLifecycle,
PackageSpecTags,
} from '../../../../common/types';
import {
RegistryInputKeys,
Expand All @@ -45,6 +46,9 @@ export const DATASTREAM_MANIFEST_NAME = 'manifest.yml';
export const DATASTREAM_ROUTING_RULES_NAME = 'routing_rules.yml';
export const DATASTREAM_LIFECYCLE_NAME = 'lifecycle.yml';

export const KIBANA_FOLDER_NAME = 'kibana';
export const TAGS_NAME = 'tags.yml';

const DEFAULT_RELEASE_VALUE = 'ga';

// Ingest pipelines are specified in a `data_stream/<name>/elasticsearch/ingest_pipeline/` directory where a `default`
Expand Down Expand Up @@ -135,6 +139,7 @@ const PARSE_AND_VERIFY_ASSETS_NAME = [
MANIFEST_NAME,
DATASTREAM_ROUTING_RULES_NAME,
DATASTREAM_LIFECYCLE_NAME,
TAGS_NAME,
];
/**
* Filter assets needed for the parse and verify archive function
Expand All @@ -146,14 +151,6 @@ export function filterAssetPathForParseAndVerifyArchive(assetPath: string): bool
/*
This function generates a package info object (see type `ArchivePackage`) by parsing and verifying the `manifest.yml` file as well
as the directory structure for the given package archive and other files adhering to the package spec: https://github.com/elastic/package-spec.

Currently, this process is duplicative of logic that's already implemented in the Package Registry codebase,
e.g. https://github.com/elastic/package-registry/blob/main/packages/package.go. Because of this duplication, it's likely for our parsing/verification
logic to fall out of sync with the registry codebase's implementation.

This should be addressed in https://github.com/elastic/kibana/issues/115032
where we'll no longer use the package registry endpoint as a source of truth for package info objects, and instead Fleet will _always_ generate
them in the manner implemented below.
*/
export async function generatePackageInfoFromArchiveBuffer(
archiveBuffer: Buffer,
Expand Down Expand Up @@ -289,6 +286,22 @@ export function parseAndVerifyArchive(
parsed.vars = parseAndVerifyVars(manifest.vars, 'manifest.yml');
}

// check that kibana/tags.yml file exists and add its content to ArchivePackage
const tagsFile = path.posix.join(toplevelDir, KIBANA_FOLDER_NAME, TAGS_NAME);
const tagsBuffer = assetsMap[tagsFile];

if (paths.includes(tagsFile) || tagsBuffer) {
let tags: PackageSpecTags[];
try {
tags = yaml.safeLoad(tagsBuffer.toString());
if (tags.length) {
parsed.asset_tags = tags;
}
} catch (error) {
throw new PackageInvalidArchiveError(`Could not parse tags file kibana/tags.yml: ${error}.`);
}
}

return parsed;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ import type { IAssignmentService, ITagsClient } from '@kbn/saved-objects-tagging
import { PACKAGES_SAVED_OBJECT_TYPE } from '../../../../../common';
import { getAsset, getPathParts } from '../../archive';
import { KibanaAssetType, KibanaSavedObjectType } from '../../../../types';
import type { AssetType, AssetReference, AssetParts, Installation } from '../../../../types';
import type {
AssetType,
AssetReference,
AssetParts,
Installation,
PackageSpecTags,
} from '../../../../types';
import { savedObjectTypes } from '../../packages';
import { indexPatternTypes, getIndexPatternSavedObjects } from '../index_pattern/install';
import { saveKibanaAssetsRefs } from '../../packages/install';
Expand Down Expand Up @@ -159,6 +165,7 @@ export async function installKibanaAssetsAndReferences({
paths,
installedPkg,
spaceId,
assetTags,
}: {
savedObjectsClient: SavedObjectsClientContract;
savedObjectsImporter: Pick<ISavedObjectsImporter, 'import' | 'resolveImportErrors'>;
Expand All @@ -170,6 +177,7 @@ export async function installKibanaAssetsAndReferences({
paths: string[];
installedPkg?: SavedObject<Installation>;
spaceId: string;
assetTags?: PackageSpecTags[];
}) {
const kibanaAssets = await getKibanaAssets(paths);
if (installedPkg) await deleteKibanaSavedObjectsAssets({ savedObjectsClient, installedPkg });
Expand All @@ -195,6 +203,7 @@ export async function installKibanaAssetsAndReferences({
pkgName,
spaceId,
importedAssets,
assetTags,
})
);

Expand Down
Loading