Skip to content

Commit

Permalink
Restrict custom registry URL (#64374)
Browse files Browse the repository at this point in the history
  • Loading branch information
jen-huang authored Apr 24, 2020
1 parent 18d1af2 commit a00051f
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 13 deletions.
1 change: 1 addition & 0 deletions x-pack/plugins/ingest_manager/common/constants/epm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@

export const PACKAGES_SAVED_OBJECT_TYPE = 'epm-package';
export const INDEX_PATTERN_SAVED_OBJECT_TYPE = 'index-pattern';
export const DEFAULT_REGISTRY_URL = 'https://epr.elastic.co';
2 changes: 1 addition & 1 deletion x-pack/plugins/ingest_manager/common/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface IngestManagerConfigType {
enabled: boolean;
epm: {
enabled: boolean;
registryUrl: string;
registryUrl?: string;
};
fleet: {
enabled: boolean;
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/ingest_manager/server/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ export {
// Defaults
DEFAULT_AGENT_CONFIG,
DEFAULT_OUTPUT,
DEFAULT_REGISTRY_URL,
} from '../../common';
2 changes: 1 addition & 1 deletion x-pack/plugins/ingest_manager/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const config = {
enabled: schema.boolean({ defaultValue: false }),
epm: schema.object({
enabled: schema.boolean({ defaultValue: true }),
registryUrl: schema.uri({ defaultValue: 'https://epr-staging.elastic.co' }),
registryUrl: schema.maybe(schema.uri()),
}),
fleet: schema.object({
enabled: schema.boolean({ defaultValue: true }),
Expand Down
15 changes: 12 additions & 3 deletions x-pack/plugins/ingest_manager/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
PluginInitializerContext,
SavedObjectsServiceStart,
} from 'kibana/server';
import { LicensingPluginSetup } from '../../licensing/server';
import { LicensingPluginSetup, ILicense } from '../../licensing/server';
import {
EncryptedSavedObjectsPluginStart,
EncryptedSavedObjectsPluginSetup,
Expand Down Expand Up @@ -42,8 +42,13 @@ import {
} from './routes';

import { IngestManagerConfigType } from '../common';
import { appContextService, ESIndexPatternSavedObjectService } from './services';
import { ESIndexPatternService, AgentService } from './services';
import {
appContextService,
licenseService,
ESIndexPatternSavedObjectService,
ESIndexPatternService,
AgentService,
} from './services';
import { getAgentStatusById } from './services/agents';

export interface IngestManagerSetupDeps {
Expand Down Expand Up @@ -90,6 +95,7 @@ export class IngestManagerPlugin
IngestManagerSetupDeps,
IngestManagerStartDeps
> {
private licensing$!: Observable<ILicense>;
private config$: Observable<IngestManagerConfigType>;
private security: SecurityPluginSetup | undefined;

Expand All @@ -98,6 +104,7 @@ export class IngestManagerPlugin
}

public async setup(core: CoreSetup, deps: IngestManagerSetupDeps) {
this.licensing$ = deps.licensing.license$;
if (deps.security) {
this.security = deps.security;
}
Expand Down Expand Up @@ -173,6 +180,7 @@ export class IngestManagerPlugin
config$: this.config$,
savedObjects: core.savedObjects,
});
licenseService.start(this.licensing$);
return {
esIndexPatternService: new ESIndexPatternSavedObjectService(),
agentService: {
Expand All @@ -183,5 +191,6 @@ export class IngestManagerPlugin

public async stop() {
appContextService.stop();
licenseService.stop();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import {
RegistrySearchResults,
RegistrySearchResult,
} from '../../../types';
import { appContextService } from '../../';
import { cacheGet, cacheSet } from './cache';
import { ArchiveEntry, untarBuffer } from './extract';
import { fetchUrl, getResponse, getResponseStream } from './requests';
import { streamToBuffer } from './streams';
import { getRegistryUrl } from './registry_url';

export { ArchiveEntry } from './extract';

Expand All @@ -32,7 +32,7 @@ export const pkgToPkgKey = ({ name, version }: { name: string; version: string }
`${name}-${version}`;

export async function fetchList(params?: SearchParams): Promise<RegistrySearchResults> {
const registryUrl = appContextService.getConfig()?.epm.registryUrl;
const registryUrl = getRegistryUrl();
const url = new URL(`${registryUrl}/search`);
if (params && params.category) {
url.searchParams.set('category', params.category);
Expand All @@ -45,7 +45,7 @@ export async function fetchFindLatestPackage(
packageName: string,
internal: boolean = true
): Promise<RegistrySearchResult> {
const registryUrl = appContextService.getConfig()?.epm.registryUrl;
const registryUrl = getRegistryUrl();
const url = new URL(`${registryUrl}/search?package=${packageName}&internal=${internal}`);
const res = await fetchUrl(url.toString());
const searchResults = JSON.parse(res);
Expand All @@ -57,17 +57,17 @@ export async function fetchFindLatestPackage(
}

export async function fetchInfo(pkgName: string, pkgVersion: string): Promise<RegistryPackage> {
const registryUrl = appContextService.getConfig()?.epm.registryUrl;
const registryUrl = getRegistryUrl();
return fetchUrl(`${registryUrl}/package/${pkgName}/${pkgVersion}`).then(JSON.parse);
}

export async function fetchFile(filePath: string): Promise<Response> {
const registryUrl = appContextService.getConfig()?.epm.registryUrl;
const registryUrl = getRegistryUrl();
return getResponse(`${registryUrl}${filePath}`);
}

export async function fetchCategories(): Promise<CategorySummaryList> {
const registryUrl = appContextService.getConfig()?.epm.registryUrl;
const registryUrl = getRegistryUrl();
return fetchUrl(`${registryUrl}/categories`).then(JSON.parse);
}

Expand Down Expand Up @@ -151,7 +151,7 @@ async function getOrFetchArchiveBuffer(pkgName: string, pkgVersion: string): Pro

async function fetchArchiveBuffer(pkgName: string, pkgVersion: string): Promise<Buffer> {
const { download: archivePath } = await fetchInfo(pkgName, pkgVersion);
const registryUrl = appContextService.getConfig()?.epm.registryUrl;
const registryUrl = getRegistryUrl();
return getResponseStream(`${registryUrl}${archivePath}`).then(streamToBuffer);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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 { DEFAULT_REGISTRY_URL } from '../../../constants';
import { appContextService, licenseService } from '../../';

export const getRegistryUrl = (): string => {
const license = licenseService.getLicenseInformation();
const customUrl = appContextService.getConfig()?.epm.registryUrl;

if (
customUrl &&
license &&
license.isAvailable &&
license.hasAtLeast('gold') &&
license.isActive
) {
return customUrl;
}

return DEFAULT_REGISTRY_URL;
};
5 changes: 4 additions & 1 deletion x-pack/plugins/ingest_manager/server/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import { SavedObjectsClientContract } from 'kibana/server';
import { AgentStatus } from '../../common/types/models';

export { appContextService } from './app_context';
export { ESIndexPatternSavedObjectService } from './es_index_pattern';

/**
Expand Down Expand Up @@ -36,3 +35,7 @@ export interface AgentService {
export { datasourceService } from './datasource';
export { agentConfigService } from './agent_config';
export { outputService } from './output';

// Plugin services
export { appContextService } from './app_context';
export { licenseService } from './license';

0 comments on commit a00051f

Please sign in to comment.