Skip to content

Commit

Permalink
[Fleet] Fix package license check to use new constraints.elastic.subs… (
Browse files Browse the repository at this point in the history
#154831)

closes #154822

Uses the new `conditions.elastic.subscription` field to check elastic
subscription.

Error message when `conditions.elastic.subscription` is set to
**enterprise** but ES is under **basic** license.
<img width="832" alt="Screenshot 2023-04-12 at 1 31 21 PM"
src="https://user-images.githubusercontent.com/55978943/231538154-fd3c54fd-228a-45f1-b159-6c46ce622c25.png">
  • Loading branch information
cauemarcondes authored Apr 12, 2023
1 parent e2d790d commit 263af61
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,11 @@ describe('install', () => {
jest
.spyOn(Registry, 'fetchFindLatestPackageOrThrow')
.mockImplementation(() => Promise.resolve({ version: '1.3.0' } as any));
jest
.spyOn(Registry, 'getPackage')
.mockImplementation(() => Promise.resolve({ packageInfo: { license: 'basic' } } as any));
jest.spyOn(Registry, 'getPackage').mockImplementation(() =>
Promise.resolve({
packageInfo: { license: 'basic', conditions: { elastic: { subscription: 'basic' } } },
} as any)
);

mockGetBundledPackages.mockReset();
(install._installPackage as jest.Mock).mockClear();
Expand Down
14 changes: 11 additions & 3 deletions x-pack/plugins/fleet/server/services/epm/packages/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import pRetry from 'p-retry';

import { uniqBy } from 'lodash';

import type { LicenseType } from '@kbn/licensing-plugin/server';

import { isPackagePrerelease, getNormalizedDataStreams } from '../../../../common/services';

import { FLEET_INSTALL_FORMAT_VERSION } from '../../../constants/fleet_es_assets';
Expand Down Expand Up @@ -378,6 +380,12 @@ async function installPackageFromRegistry({
}
}

function getElasticSubscription(packageInfo: ArchivePackage) {
const subscription = packageInfo.conditions?.elastic?.subscription as LicenseType | undefined;
// Keep packageInfo.license for backward compatibility
return subscription || packageInfo.license || 'basic';
}

async function installPackageCommon(options: {
pkgName: string;
pkgVersion: string;
Expand Down Expand Up @@ -450,9 +458,9 @@ async function installPackageCommon(options: {
};
}
}

if (!licenseService.hasAtLeast(packageInfo.license || 'basic')) {
const err = new Error(`Requires ${packageInfo.license} license`);
const elasticSubscription = getElasticSubscription(packageInfo);
if (!licenseService.hasAtLeast(elasticSubscription)) {
const err = new Error(`Requires ${elasticSubscription} license`);
sendEvent({
...telemetryEvent,
errorMessage: err.message,
Expand Down

0 comments on commit 263af61

Please sign in to comment.