From 92ba8e2ea06f26444b8321070eede9a43564a900 Mon Sep 17 00:00:00 2001 From: John Schulz Date: Mon, 3 Aug 2020 19:30:18 -0400 Subject: [PATCH] [Ingest Management] main branch uses epr-snapshot. Others production (#73555) (#74122) * Same behavior as now. Just refactored. * main branch uses epr-snapshot. Others use prod * Link some types vs repeating them * replace DEFAULT_REGISTRY_URL with getRegistryUrl in Endpoint tests * Make an Endpoint test helper name more clear * try/catch around getKibanaBranch * Use branch & version from package.json as fallback * No guards b/c kibana{Branch,Version} have defaults Co-authored-by: Elastic Machine # Conflicts: # x-pack/plugins/ingest_manager/common/constants/epm.ts --- .../ingest_manager/common/constants/epm.ts | 1 - .../ingest_manager/server/constants/index.ts | 1 - x-pack/plugins/ingest_manager/server/index.ts | 2 +- .../plugins/ingest_manager/server/plugin.ts | 12 +++---- .../server/services/app_context.ts | 13 +++---- .../services/epm/registry/registry_url.ts | 35 ++++++++++++++----- .../ingest_manager/server/services/index.ts | 2 ++ .../apps/endpoint/index.ts | 6 ++-- .../apis/index.ts | 6 ++-- .../registry.ts | 6 ++-- 10 files changed, 48 insertions(+), 36 deletions(-) diff --git a/x-pack/plugins/ingest_manager/common/constants/epm.ts b/x-pack/plugins/ingest_manager/common/constants/epm.ts index 97b5cca3692980..73cd8463bb6aab 100644 --- a/x-pack/plugins/ingest_manager/common/constants/epm.ts +++ b/x-pack/plugins/ingest_manager/common/constants/epm.ts @@ -6,5 +6,4 @@ export const PACKAGES_SAVED_OBJECT_TYPE = 'epm-packages'; export const INDEX_PATTERN_SAVED_OBJECT_TYPE = 'index-pattern'; -export const DEFAULT_REGISTRY_URL = 'https://epr.elastic.co'; export const INDEX_PATTERN_PLACEHOLDER_SUFFIX = '-index_pattern_placeholder'; diff --git a/x-pack/plugins/ingest_manager/server/constants/index.ts b/x-pack/plugins/ingest_manager/server/constants/index.ts index ce81736f2e84f1..1ec13bd80f0fb9 100644 --- a/x-pack/plugins/ingest_manager/server/constants/index.ts +++ b/x-pack/plugins/ingest_manager/server/constants/index.ts @@ -43,5 +43,4 @@ export { // Defaults DEFAULT_AGENT_CONFIG, DEFAULT_OUTPUT, - DEFAULT_REGISTRY_URL, } from '../../common'; diff --git a/x-pack/plugins/ingest_manager/server/index.ts b/x-pack/plugins/ingest_manager/server/index.ts index b4752f167e2315..e2f659f54d6254 100644 --- a/x-pack/plugins/ingest_manager/server/index.ts +++ b/x-pack/plugins/ingest_manager/server/index.ts @@ -6,7 +6,7 @@ import { schema, TypeOf } from '@kbn/config-schema'; import { PluginInitializerContext } from 'src/core/server'; import { IngestManagerPlugin } from './plugin'; -export { AgentService, ESIndexPatternService } from './services'; +export { AgentService, ESIndexPatternService, getRegistryUrl } from './services'; export { IngestManagerSetupContract, IngestManagerSetupDeps, diff --git a/x-pack/plugins/ingest_manager/server/plugin.ts b/x-pack/plugins/ingest_manager/server/plugin.ts index e7495df254a090..e5e1194d59ecb9 100644 --- a/x-pack/plugins/ingest_manager/server/plugin.ts +++ b/x-pack/plugins/ingest_manager/server/plugin.ts @@ -83,9 +83,9 @@ export interface IngestManagerAppContext { security?: SecurityPluginSetup; config$?: Observable; savedObjects: SavedObjectsServiceStart; - isProductionMode: boolean; - kibanaVersion: string; - kibanaBranch: string; + isProductionMode: PluginInitializerContext['env']['mode']['prod']; + kibanaVersion: PluginInitializerContext['env']['packageInfo']['version']; + kibanaBranch: PluginInitializerContext['env']['packageInfo']['branch']; cloud?: CloudSetup; logger?: Logger; httpSetup?: HttpServiceSetup; @@ -144,9 +144,9 @@ export class IngestManagerPlugin private cloud: CloudSetup | undefined; private logger: Logger | undefined; - private isProductionMode: boolean; - private kibanaVersion: string; - private kibanaBranch: string; + private isProductionMode: IngestManagerAppContext['isProductionMode']; + private kibanaVersion: IngestManagerAppContext['kibanaVersion']; + private kibanaBranch: IngestManagerAppContext['kibanaBranch']; private httpSetup: HttpServiceSetup | undefined; private encryptedSavedObjectsSetup: EncryptedSavedObjectsPluginSetup | undefined; diff --git a/x-pack/plugins/ingest_manager/server/services/app_context.ts b/x-pack/plugins/ingest_manager/server/services/app_context.ts index bdc7a443ba6dd0..7f82670a4d02c2 100644 --- a/x-pack/plugins/ingest_manager/server/services/app_context.ts +++ b/x-pack/plugins/ingest_manager/server/services/app_context.ts @@ -10,6 +10,7 @@ import { EncryptedSavedObjectsClient, EncryptedSavedObjectsPluginSetup, } from '../../../encrypted_saved_objects/server'; +import packageJSON from '../../../../../package.json'; import { SecurityPluginSetup } from '../../../security/server'; import { IngestManagerConfigType } from '../../common'; import { ExternalCallback, ExternalCallbacksStorage, IngestManagerAppContext } from '../plugin'; @@ -22,9 +23,9 @@ class AppContextService { private config$?: Observable; private configSubject$?: BehaviorSubject; private savedObjects: SavedObjectsServiceStart | undefined; - private isProductionMode: boolean = false; - private kibanaVersion: string | undefined; - private kibanaBranch: string | undefined; + private isProductionMode: IngestManagerAppContext['isProductionMode'] = false; + private kibanaVersion: IngestManagerAppContext['kibanaVersion'] = packageJSON.version; + private kibanaBranch: IngestManagerAppContext['kibanaBranch'] = packageJSON.branch; private cloud?: CloudSetup; private logger: Logger | undefined; private httpSetup?: HttpServiceSetup; @@ -121,16 +122,10 @@ class AppContextService { } public getKibanaVersion() { - if (!this.kibanaVersion) { - throw new Error('Kibana version is not set.'); - } return this.kibanaVersion; } public getKibanaBranch() { - if (!this.kibanaBranch) { - throw new Error('Kibana branch is not set.'); - } return this.kibanaBranch; } diff --git a/x-pack/plugins/ingest_manager/server/services/epm/registry/registry_url.ts b/x-pack/plugins/ingest_manager/server/services/epm/registry/registry_url.ts index 47c91218089883..b788d1bcbb4a92 100644 --- a/x-pack/plugins/ingest_manager/server/services/epm/registry/registry_url.ts +++ b/x-pack/plugins/ingest_manager/server/services/epm/registry/registry_url.ts @@ -3,20 +3,37 @@ * 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 '../../'; +// from https://github.com/elastic/package-registry#docker (maybe from OpenAPI one day) +// the unused variables cause a TS warning about unused values +// chose to comment them out vs @ts-ignore or @ts-expect-error on each line + +const PRODUCTION_REGISTRY_URL_CDN = 'https://epr.elastic.co'; +// const STAGING_REGISTRY_URL_CDN = 'https://epr-staging.elastic.co'; +// const EXPERIMENTAL_REGISTRY_URL_CDN = 'https://epr-experimental.elastic.co/'; +const SNAPSHOT_REGISTRY_URL_CDN = 'https://epr-snapshot.elastic.co'; + +// const PRODUCTION_REGISTRY_URL_NO_CDN = 'https://epr.ea-web.elastic.dev'; +// const STAGING_REGISTRY_URL_NO_CDN = 'https://epr-staging.ea-web.elastic.dev'; +// const EXPERIMENTAL_REGISTRY_URL_NO_CDN = 'https://epr-experimental.ea-web.elastic.dev/'; +// const SNAPSHOT_REGISTRY_URL_NO_CDN = 'https://epr-snapshot.ea-web.elastic.dev'; + +const getDefaultRegistryUrl = (): string => { + const branch = appContextService.getKibanaBranch(); + if (branch === 'master') { + return SNAPSHOT_REGISTRY_URL_CDN; + } else { + return PRODUCTION_REGISTRY_URL_CDN; + } +}; + export const getRegistryUrl = (): string => { const license = licenseService.getLicenseInformation(); const customUrl = appContextService.getConfig()?.registryUrl; + const isGoldPlus = license?.isAvailable && license?.isActive && license?.hasAtLeast('gold'); - if ( - customUrl && - license && - license.isAvailable && - license.hasAtLeast('gold') && - license.isActive - ) { + if (customUrl && isGoldPlus) { return customUrl; } @@ -24,5 +41,5 @@ export const getRegistryUrl = (): string => { appContextService.getLogger().warn('Gold license is required to use a custom registry url.'); } - return DEFAULT_REGISTRY_URL; + return getDefaultRegistryUrl(); }; diff --git a/x-pack/plugins/ingest_manager/server/services/index.ts b/x-pack/plugins/ingest_manager/server/services/index.ts index 9e198debbb9046..c5f080386f20f5 100644 --- a/x-pack/plugins/ingest_manager/server/services/index.ts +++ b/x-pack/plugins/ingest_manager/server/services/index.ts @@ -10,6 +10,8 @@ import * as settingsService from './settings'; export { ESIndexPatternSavedObjectService } from './es_index_pattern'; +export { getRegistryUrl } from './epm/registry/registry_url'; + /** * Service to return the index pattern of EPM packages */ diff --git a/x-pack/test/security_solution_endpoint/apps/endpoint/index.ts b/x-pack/test/security_solution_endpoint/apps/endpoint/index.ts index 7962ec60ff57e8..ad1980cd7218b8 100644 --- a/x-pack/test/security_solution_endpoint/apps/endpoint/index.ts +++ b/x-pack/test/security_solution_endpoint/apps/endpoint/index.ts @@ -3,11 +3,11 @@ * 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 '../../../../plugins/ingest_manager/common'; +import { getRegistryUrl as getRegistryUrlFromIngest } from '../../../../plugins/ingest_manager/server'; import { FtrProviderContext } from '../../ftr_provider_context'; import { isRegistryEnabled, - getRegistryUrl, + getRegistryUrlFromTestEnv, } from '../../../security_solution_endpoint_api_int/registry'; export default function (providerContext: FtrProviderContext) { @@ -22,7 +22,7 @@ export default function (providerContext: FtrProviderContext) { log.warning('These tests are being run with an external package registry'); } - const registryUrl = getRegistryUrl() ?? DEFAULT_REGISTRY_URL; + const registryUrl = getRegistryUrlFromTestEnv() ?? getRegistryUrlFromIngest(); log.info(`Package registry URL for tests: ${registryUrl}`); before(async () => { diff --git a/x-pack/test/security_solution_endpoint_api_int/apis/index.ts b/x-pack/test/security_solution_endpoint_api_int/apis/index.ts index b1317c2d9f1c14..9cdef1c938890c 100644 --- a/x-pack/test/security_solution_endpoint_api_int/apis/index.ts +++ b/x-pack/test/security_solution_endpoint_api_int/apis/index.ts @@ -4,8 +4,8 @@ * you may not use this file except in compliance with the Elastic License. */ import { FtrProviderContext } from '../ftr_provider_context'; -import { isRegistryEnabled, getRegistryUrl } from '../registry'; -import { DEFAULT_REGISTRY_URL } from '../../../plugins/ingest_manager/common'; +import { isRegistryEnabled, getRegistryUrlFromTestEnv } from '../registry'; +import { getRegistryUrl as getRegistryUrlFromIngest } from '../../../plugins/ingest_manager/server'; export default function endpointAPIIntegrationTests(providerContext: FtrProviderContext) { const { loadTestFile, getService } = providerContext; @@ -20,7 +20,7 @@ export default function endpointAPIIntegrationTests(providerContext: FtrProvider log.warning('These tests are being run with an external package registry'); } - const registryUrl = getRegistryUrl() ?? DEFAULT_REGISTRY_URL; + const registryUrl = getRegistryUrlFromTestEnv() ?? getRegistryUrlFromIngest(); log.info(`Package registry URL for tests: ${registryUrl}`); before(async () => { diff --git a/x-pack/test/security_solution_endpoint_api_int/registry.ts b/x-pack/test/security_solution_endpoint_api_int/registry.ts index cc474cbf29aaf9..9a9d184b9c2971 100644 --- a/x-pack/test/security_solution_endpoint_api_int/registry.ts +++ b/x-pack/test/security_solution_endpoint_api_int/registry.ts @@ -57,7 +57,7 @@ export function createEndpointDockerConfig( }); } -export function getRegistryUrl(): string | undefined { +export function getRegistryUrlFromTestEnv(): string | undefined { let registryUrl: string | undefined; if (dockerRegistryPort !== undefined) { registryUrl = `--xpack.ingestManager.registryUrl=http://localhost:${dockerRegistryPort}`; @@ -68,10 +68,10 @@ export function getRegistryUrl(): string | undefined { } export function getRegistryUrlAsArray(): string[] { - const registryUrl: string | undefined = getRegistryUrl(); + const registryUrl: string | undefined = getRegistryUrlFromTestEnv(); return registryUrl !== undefined ? [registryUrl] : []; } export function isRegistryEnabled() { - return getRegistryUrl() !== undefined; + return getRegistryUrlFromTestEnv() !== undefined; }