Skip to content

Commit a8439e8

Browse files
committed
[EPM] restrict package install endpoint from installing/updating to old packages (elastic#64932)
* restrict installing or updating to out-of-date package * throw bad requests in remove handler * remove accidental commit * remove space
1 parent 042f3f0 commit a8439e8

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

x-pack/plugins/ingest_manager/server/routes/epm/handlers.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ export const installPackageHandler: RequestHandler<TypeOf<
136136
};
137137
return response.ok({ body });
138138
} catch (e) {
139+
if (e.isBoom) {
140+
return response.customError({
141+
statusCode: e.output.statusCode,
142+
body: { message: e.output.payload.message },
143+
});
144+
}
139145
return response.customError({
140146
statusCode: 500,
141147
body: { message: e.message },
@@ -157,6 +163,12 @@ export const deletePackageHandler: RequestHandler<TypeOf<
157163
};
158164
return response.ok({ body });
159165
} catch (e) {
166+
if (e.isBoom) {
167+
return response.customError({
168+
statusCode: e.output.statusCode,
169+
body: { message: e.output.payload.message },
170+
});
171+
}
160172
return response.customError({
161173
statusCode: 500,
162174
body: { message: e.message },

x-pack/plugins/ingest_manager/server/services/epm/packages/install.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
import { SavedObject, SavedObjectsClientContract } from 'src/core/server';
8+
import Boom from 'boom';
89
import { PACKAGES_SAVED_OBJECT_TYPE } from '../../../constants';
910
import {
1011
AssetReference,
@@ -93,11 +94,18 @@ export async function installPackage(options: {
9394
const { savedObjectsClient, pkgkey, callCluster } = options;
9495
// TODO: change epm API to /packageName/version so we don't need to do this
9596
const [pkgName, pkgVersion] = pkgkey.split('-');
97+
9698
// see if some version of this package is already installed
99+
// TODO: calls to getInstallationObject, Registry.fetchInfo, and Registry.fetchFindLatestPackge
100+
// and be replaced by getPackageInfo after adjusting for it to not group/use archive assets
97101
const installedPkg = await getInstallationObject({ savedObjectsClient, pkgName });
98-
const reinstall = pkgVersion === installedPkg?.attributes.version;
99-
100102
const registryPackageInfo = await Registry.fetchInfo(pkgName, pkgVersion);
103+
const latestPackage = await Registry.fetchFindLatestPackage(pkgName);
104+
105+
if (pkgVersion < latestPackage.version)
106+
throw Boom.badRequest('Cannot install or update to an out-of-date package');
107+
108+
const reinstall = pkgVersion === installedPkg?.attributes.version;
101109
const { internal = false, removable = true } = registryPackageInfo;
102110

103111
// delete the previous version's installation's SO kibana assets before installing new ones

x-pack/plugins/ingest_manager/server/services/epm/packages/remove.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
import { SavedObjectsClientContract } from 'src/core/server';
8+
import Boom from 'boom';
89
import { PACKAGES_SAVED_OBJECT_TYPE } from '../../../constants';
910
import { AssetReference, AssetType, ElasticsearchAssetType } from '../../../types';
1011
import { CallESAsCurrentUser } from '../../../types';
@@ -20,9 +21,9 @@ export async function removeInstallation(options: {
2021
// TODO: the epm api should change to /name/version so we don't need to do this
2122
const [pkgName] = pkgkey.split('-');
2223
const installation = await getInstallation({ savedObjectsClient, pkgName });
23-
if (!installation) throw new Error('integration does not exist');
24+
if (!installation) throw Boom.badRequest(`${pkgName} is not installed`);
2425
if (installation.removable === false)
25-
throw new Error(`The ${pkgName} integration is installed by default and cannot be removed`);
26+
throw Boom.badRequest(`${pkgName} is installed by default and cannot be removed`);
2627
const installedObjects = installation.installed || [];
2728

2829
// Delete the manager saved object with references to the asset objects

0 commit comments

Comments
 (0)