Skip to content

Commit

Permalink
Merge branch 'master' into feat/ssource-fields
Browse files Browse the repository at this point in the history
  • Loading branch information
kibanamachine authored Dec 3, 2020
2 parents 6953288 + 65cbe4c commit d6b47ad
Show file tree
Hide file tree
Showing 8 changed files with 269 additions and 113 deletions.
61 changes: 47 additions & 14 deletions x-pack/plugins/fleet/server/routes/epm/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* you may not use this file except in compliance with the Elastic License.
*/
import { TypeOf } from '@kbn/config-schema';
import mime from 'mime-types';
import path from 'path';
import { RequestHandler, ResponseHeaders, KnownHeaders } from 'src/core/server';
import {
GetInfoResponse,
Expand Down Expand Up @@ -43,6 +45,8 @@ import {
import { defaultIngestErrorHandler, ingestErrorToResponseOptions } from '../../errors';
import { splitPkgKey } from '../../services/epm/registry';
import { licenseService } from '../../services';
import { getArchiveEntry } from '../../services/epm/archive/cache';
import { bufferToStream } from '../../services/epm/streams';

export const getCategoriesHandler: RequestHandler<
undefined,
Expand Down Expand Up @@ -102,22 +106,51 @@ export const getFileHandler: RequestHandler<TypeOf<typeof GetFileRequestSchema.p
) => {
try {
const { pkgName, pkgVersion, filePath } = request.params;
const registryResponse = await getFile(`/package/${pkgName}/${pkgVersion}/${filePath}`);

const headersToProxy: KnownHeaders[] = ['content-type', 'cache-control'];
const proxiedHeaders = headersToProxy.reduce((headers, knownHeader) => {
const value = registryResponse.headers.get(knownHeader);
if (value !== null) {
headers[knownHeader] = value;
const savedObjectsClient = context.core.savedObjects.client;
const savedObject = await getInstallationObject({ savedObjectsClient, pkgName });
const pkgInstallSource = savedObject?.attributes.install_source;
// TODO: when package storage is available, remove installSource check and check cache and storage, remove registry call
if (pkgInstallSource === 'upload' && pkgVersion === savedObject?.attributes.version) {
const headerContentType = mime.contentType(path.extname(filePath));
if (!headerContentType) {
return response.custom({
body: `unknown content type for file: ${filePath}`,
statusCode: 400,
});
}
return headers;
}, {} as ResponseHeaders);
const archiveFile = getArchiveEntry(`${pkgName}-${pkgVersion}/${filePath}`);
if (!archiveFile) {
return response.custom({
body: `uploaded package file not found: ${filePath}`,
statusCode: 404,
});
}
const headers: ResponseHeaders = {
'cache-control': 'max-age=10, public',
'content-type': headerContentType,
};
return response.custom({
body: bufferToStream(archiveFile),
statusCode: 200,
headers,
});
} else {
const registryResponse = await getFile(`/package/${pkgName}/${pkgVersion}/${filePath}`);
const headersToProxy: KnownHeaders[] = ['content-type', 'cache-control'];
const proxiedHeaders = headersToProxy.reduce((headers, knownHeader) => {
const value = registryResponse.headers.get(knownHeader);
if (value !== null) {
headers[knownHeader] = value;
}
return headers;
}, {} as ResponseHeaders);

return response.custom({
body: registryResponse.body,
statusCode: registryResponse.status,
headers: proxiedHeaders,
});
return response.custom({
body: registryResponse.body,
statusCode: registryResponse.status,
headers: proxiedHeaders,
});
}
} catch (error) {
return defaultIngestErrorHandler({ error, response });
}
Expand Down
5 changes: 4 additions & 1 deletion x-pack/plugins/fleet/server/services/epm/packages/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ export async function getPackageInfo(options: {
const getPackageRes = await getPackageFromSource({
pkgName,
pkgVersion,
pkgInstallSource: savedObject?.attributes.install_source,
pkgInstallSource:
savedObject?.attributes.version === pkgVersion
? savedObject?.attributes.install_source
: 'registry',
});
const paths = getPackageRes.paths;
const packageInfo = getPackageRes.packageInfo;
Expand Down
24 changes: 10 additions & 14 deletions x-pack/plugins/maps/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,27 +147,23 @@ export class MapsPlugin implements Plugin {
return;
}

let routesInitialized = false;
let isEnterprisePlus = false;
let lastLicenseId: string | undefined;
const emsSettings = new EMSSettings(mapsLegacyConfig, () => isEnterprisePlus);
licensing.license$.subscribe((license: ILicense) => {
const basic = license.check(APP_ID, 'basic');

const enterprise = license.check(APP_ID, 'enterprise');
isEnterprisePlus = enterprise.state === 'valid';

if (basic.state === 'valid' && !routesInitialized) {
routesInitialized = true;
initRoutes(
core.http.createRouter(),
license.uid,
emsSettings,
this.kibanaVersion,
this._logger
);
}
lastLicenseId = license.uid;
});

initRoutes(
core.http.createRouter(),
() => lastLicenseId,
emsSettings,
this.kibanaVersion,
this._logger
);

this._initHomeData(home, core.http.basePath.prepend, emsSettings);

features.registerKibanaFeature({
Expand Down
40 changes: 23 additions & 17 deletions x-pack/plugins/maps/server/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,32 @@ const EMPTY_EMS_CLIENT = {
addQueryParams() {},
};

export function initRoutes(router, licenseUid, emsSettings, kbnVersion, logger) {
export function initRoutes(router, getLicenseId, emsSettings, kbnVersion, logger) {
let emsClient;

if (emsSettings.isIncludeElasticMapsService()) {
emsClient = new EMSClient({
language: i18n.getLocale(),
appVersion: kbnVersion,
appName: EMS_APP_NAME,
fileApiUrl: emsSettings.getEMSFileApiUrl(),
tileApiUrl: emsSettings.getEMSTileApiUrl(),
landingPageUrl: emsSettings.getEMSLandingPageUrl(),
fetchFunction: fetch,
});
emsClient.addQueryParams({ license: licenseUid });
} else {
emsClient = EMPTY_EMS_CLIENT;
}
let lastLicenseId;

function getEMSClient() {
return emsSettings.isEMSEnabled() ? emsClient : EMPTY_EMS_CLIENT;
const currentLicenseId = getLicenseId();
if (emsClient && emsSettings.isEMSEnabled() && lastLicenseId === currentLicenseId) {
return emsClient;
}

lastLicenseId = currentLicenseId;
if (emsSettings.isIncludeElasticMapsService()) {
emsClient = new EMSClient({
language: i18n.getLocale(),
appVersion: kbnVersion,
appName: EMS_APP_NAME,
fileApiUrl: emsSettings.getEMSFileApiUrl(),
tileApiUrl: emsSettings.getEMSTileApiUrl(),
landingPageUrl: emsSettings.getEMSLandingPageUrl(),
fetchFunction: fetch,
});
emsClient.addQueryParams({ license: currentLicenseId });
return emsClient;
} else {
return EMPTY_EMS_CLIENT;
}
}

router.get(
Expand Down
Loading

0 comments on commit d6b47ad

Please sign in to comment.