Skip to content

Commit

Permalink
[Ingest Manager] Add route for package installation by upload (#77044) (
Browse files Browse the repository at this point in the history
#77438)

* Add route to accept a direct package upload.

* Only accept application/zip or application/gzip

* Use better name for response containing only a message.

* Add integration test for direct package upload.
  • Loading branch information
skh committed Sep 15, 2020
1 parent 9065242 commit 8bfb248
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 12 deletions.
3 changes: 2 additions & 1 deletion x-pack/plugins/ingest_manager/common/constants/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export const EPM_API_ROUTES = {
LIST_PATTERN: EPM_PACKAGES_MANY,
LIMITED_LIST_PATTERN: `${EPM_PACKAGES_MANY}/limited`,
INFO_PATTERN: EPM_PACKAGES_ONE,
INSTALL_PATTERN: EPM_PACKAGES_ONE,
INSTALL_FROM_REGISTRY_PATTERN: EPM_PACKAGES_ONE,
INSTALL_BY_UPLOAD_PATTERN: EPM_PACKAGES_MANY,
DELETE_PATTERN: EPM_PACKAGES_ONE,
FILEPATH_PATTERN: `${EPM_PACKAGES_FILE}/{filePath*}`,
CATEGORIES_PATTERN: `${EPM_API_ROOT}/categories`,
Expand Down
5 changes: 4 additions & 1 deletion x-pack/plugins/ingest_manager/common/services/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ export const epmRouteService = {
},

getInstallPath: (pkgkey: string) => {
return EPM_API_ROUTES.INSTALL_PATTERN.replace('{pkgkey}', pkgkey).replace(/\/$/, ''); // trim trailing slash
return EPM_API_ROUTES.INSTALL_FROM_REGISTRY_PATTERN.replace('{pkgkey}', pkgkey).replace(
/\/$/,
''
); // trim trailing slash
},

getRemovePath: (pkgkey: string) => {
Expand Down
4 changes: 4 additions & 0 deletions x-pack/plugins/ingest_manager/common/types/rest_spec/epm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ export interface InstallPackageResponse {
response: AssetReference[];
}

export interface MessageResponse {
response: string;
}

export interface DeletePackageRequest {
params: {
pkgkey: string;
Expand Down
21 changes: 17 additions & 4 deletions x-pack/plugins/ingest_manager/server/routes/epm/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { appContextService } from '../../services';
import {
GetInfoResponse,
InstallPackageResponse,
MessageResponse,
DeletePackageResponse,
GetCategoriesResponse,
GetPackagesResponse,
Expand All @@ -19,7 +20,8 @@ import {
GetPackagesRequestSchema,
GetFileRequestSchema,
GetInfoRequestSchema,
InstallPackageRequestSchema,
InstallPackageFromRegistryRequestSchema,
InstallPackageByUploadRequestSchema,
DeletePackageRequestSchema,
} from '../../types';
import {
Expand Down Expand Up @@ -129,10 +131,10 @@ export const getInfoHandler: RequestHandler<TypeOf<typeof GetInfoRequestSchema.p
}
};

export const installPackageHandler: RequestHandler<
TypeOf<typeof InstallPackageRequestSchema.params>,
export const installPackageFromRegistryHandler: RequestHandler<
TypeOf<typeof InstallPackageFromRegistryRequestSchema.params>,
undefined,
TypeOf<typeof InstallPackageRequestSchema.body>
TypeOf<typeof InstallPackageFromRegistryRequestSchema.body>
> = async (context, request, response) => {
const logger = appContextService.getLogger();
const savedObjectsClient = context.core.savedObjects.client;
Expand Down Expand Up @@ -183,6 +185,17 @@ export const installPackageHandler: RequestHandler<
}
};

export const installPackageByUploadHandler: RequestHandler<
undefined,
undefined,
TypeOf<typeof InstallPackageByUploadRequestSchema.body>
> = async (context, request, response) => {
const body: MessageResponse = {
response: 'package upload was received ok, but not installed (not implemented yet)',
};
return response.ok({ body });
};

export const deletePackageHandler: RequestHandler<TypeOf<
typeof DeletePackageRequestSchema.params
>> = async (context, request, response) => {
Expand Down
30 changes: 25 additions & 5 deletions x-pack/plugins/ingest_manager/server/routes/epm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,22 @@ import {
getLimitedListHandler,
getFileHandler,
getInfoHandler,
installPackageHandler,
installPackageFromRegistryHandler,
installPackageByUploadHandler,
deletePackageHandler,
} from './handlers';
import {
GetCategoriesRequestSchema,
GetPackagesRequestSchema,
GetFileRequestSchema,
GetInfoRequestSchema,
InstallPackageRequestSchema,
InstallPackageFromRegistryRequestSchema,
InstallPackageByUploadRequestSchema,
DeletePackageRequestSchema,
} from '../../types';

const MAX_FILE_SIZE_BYTES = 104857600; // 100MB

export const registerRoutes = (router: IRouter) => {
router.get(
{
Expand Down Expand Up @@ -71,11 +75,27 @@ export const registerRoutes = (router: IRouter) => {

router.post(
{
path: EPM_API_ROUTES.INSTALL_PATTERN,
validate: InstallPackageRequestSchema,
path: EPM_API_ROUTES.INSTALL_FROM_REGISTRY_PATTERN,
validate: InstallPackageFromRegistryRequestSchema,
options: { tags: [`access:${PLUGIN_ID}-all`] },
},
installPackageHandler
installPackageFromRegistryHandler
);

router.post(
{
path: EPM_API_ROUTES.INSTALL_BY_UPLOAD_PATTERN,
validate: InstallPackageByUploadRequestSchema,
options: {
tags: [`access:${PLUGIN_ID}-all`],
body: {
accepts: ['application/gzip', 'application/zip'],
parse: false,
maxBytes: MAX_FILE_SIZE_BYTES,
},
},
},
installPackageByUploadHandler
);

router.delete(
Expand Down
6 changes: 5 additions & 1 deletion x-pack/plugins/ingest_manager/server/types/rest_spec/epm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const GetInfoRequestSchema = {
}),
};

export const InstallPackageRequestSchema = {
export const InstallPackageFromRegistryRequestSchema = {
params: schema.object({
pkgkey: schema.string(),
}),
Expand All @@ -43,6 +43,10 @@ export const InstallPackageRequestSchema = {
),
};

export const InstallPackageByUploadRequestSchema = {
body: schema.buffer(),
};

export const DeletePackageRequestSchema = {
params: schema.object({
pkgkey: schema.string(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default function loadTests({ loadTestFile }) {
loadTestFile(require.resolve('./file'));
//loadTestFile(require.resolve('./template'));
loadTestFile(require.resolve('./ilm'));
loadTestFile(require.resolve('./install_by_upload'));
loadTestFile(require.resolve('./install_overrides'));
loadTestFile(require.resolve('./install_prerelease'));
loadTestFile(require.resolve('./install_remove_assets'));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import fs from 'fs';
import path from 'path';
import expect from '@kbn/expect';

import { FtrProviderContext } from '../../../api_integration/ftr_provider_context';
import { warnAndSkipTest } from '../../helpers';

export default function ({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const dockerServers = getService('dockerServers');
const log = getService('log');

const testPkgArchiveTgz = path.join(
path.dirname(__filename),
'../fixtures/direct_upload_packages/apache_0.1.4.tar.gz'
);
const testPkgKey = 'apache-0.14';
const server = dockerServers.get('registry');

const deletePackage = async (pkgkey: string) => {
await supertest.delete(`/api/ingest_manager/epm/packages/${pkgkey}`).set('kbn-xsrf', 'xxxx');
};

describe('installs packages from direct upload', async () => {
after(async () => {
if (server.enabled) {
// remove the package just in case it being installed will affect other tests
await deletePackage(testPkgKey);
}
});

it('should install a tar archive correctly', async function () {
if (server.enabled) {
const buf = fs.readFileSync(testPkgArchiveTgz);
const res = await supertest
.post(`/api/ingest_manager/epm/packages`)
.set('kbn-xsrf', 'xxxx')
.type('application/gzip')
.send(buf)
.expect(200);
expect(res.body.response).to.equal(
'package upload was received ok, but not installed (not implemented yet)'
);
} else {
warnAndSkipTest(this, log);
}
});
});
}
Binary file not shown.

0 comments on commit 8bfb248

Please sign in to comment.