From dc089215f26d9998a9e6f320f188a11ad9eee717 Mon Sep 17 00:00:00 2001 From: pgayvallet Date: Mon, 12 Jun 2023 08:58:03 +0200 Subject: [PATCH 01/14] add urls to cloud browser contracts --- .../cloud/common/decode_cloud_id.test.ts | 102 ++++++++++++++++++ .../plugins/cloud/common/decode_cloud_id.ts | 65 +++++++++++ x-pack/plugins/cloud/public/index.ts | 3 +- x-pack/plugins/cloud/public/plugin.tsx | 92 +++------------- x-pack/plugins/cloud/public/types.ts | 99 +++++++++++++++++ 5 files changed, 284 insertions(+), 77 deletions(-) create mode 100644 x-pack/plugins/cloud/common/decode_cloud_id.test.ts create mode 100644 x-pack/plugins/cloud/common/decode_cloud_id.ts create mode 100644 x-pack/plugins/cloud/public/types.ts diff --git a/x-pack/plugins/cloud/common/decode_cloud_id.test.ts b/x-pack/plugins/cloud/common/decode_cloud_id.test.ts new file mode 100644 index 00000000000000..5ec709b1c8ccdd --- /dev/null +++ b/x-pack/plugins/cloud/common/decode_cloud_id.test.ts @@ -0,0 +1,102 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { decodeCloudId } from './decode_cloud_id'; + +describe('Fleet - decodeCloudId', () => { + it('parses various CloudID formats', () => { + const tests = [ + { + cloudID: + 'staging:dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyRjZWM2ZjI2MWE3NGJmMjRjZTMzYmI4ODExYjg0Mjk0ZiRjNmMyY2E2ZDA0MjI0OWFmMGNjN2Q3YTllOTYyNTc0Mw==', + expectedEsURL: 'https://cec6f261a74bf24ce33bb8811b84294f.us-east-1.aws.found.io:443', + expectedKibanaURL: 'https://c6c2ca6d042249af0cc7d7a9e9625743.us-east-1.aws.found.io:443', + }, + { + cloudID: + 'dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyRjZWM2ZjI2MWE3NGJmMjRjZTMzYmI4ODExYjg0Mjk0ZiRjNmMyY2E2ZDA0MjI0OWFmMGNjN2Q3YTllOTYyNTc0Mw==', + expectedEsURL: 'https://cec6f261a74bf24ce33bb8811b84294f.us-east-1.aws.found.io:443', + expectedKibanaURL: 'https://c6c2ca6d042249af0cc7d7a9e9625743.us-east-1.aws.found.io:443', + }, + { + cloudID: + ':dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyRjZWM2ZjI2MWE3NGJmMjRjZTMzYmI4ODExYjg0Mjk0ZiRjNmMyY2E2ZDA0MjI0OWFmMGNjN2Q3YTllOTYyNTc0Mw==', + expectedEsURL: 'https://cec6f261a74bf24ce33bb8811b84294f.us-east-1.aws.found.io:443', + expectedKibanaURL: 'https://c6c2ca6d042249af0cc7d7a9e9625743.us-east-1.aws.found.io:443', + }, + { + cloudID: + 'gcp-cluster:dXMtY2VudHJhbDEuZ2NwLmNsb3VkLmVzLmlvJDhhMDI4M2FmMDQxZjE5NWY3NzI5YmMwNGM2NmEwZmNlJDBjZDVjZDU2OGVlYmU1M2M4OWViN2NhZTViYWM4YjM3', + expectedEsURL: 'https://8a0283af041f195f7729bc04c66a0fce.us-central1.gcp.cloud.es.io:443', + expectedKibanaURL: + 'https://0cd5cd568eebe53c89eb7cae5bac8b37.us-central1.gcp.cloud.es.io:443', + }, + { + cloudID: + 'custom-port:dXMtY2VudHJhbDEuZ2NwLmNsb3VkLmVzLmlvOjkyNDMkYWMzMWViYjkwMjQxNzczMTU3MDQzYzM0ZmQyNmZkNDYkYTRjMDYyMzBlNDhjOGZjZTdiZTg4YTA3NGEzYmIzZTA=', + expectedEsURL: 'https://ac31ebb90241773157043c34fd26fd46.us-central1.gcp.cloud.es.io:9243', + expectedKibanaURL: + 'https://a4c06230e48c8fce7be88a074a3bb3e0.us-central1.gcp.cloud.es.io:9243', + }, + { + cloudID: + 'different-es-kb-port:dXMtY2VudHJhbDEuZ2NwLmNsb3VkLmVzLmlvJGFjMzFlYmI5MDI0MTc3MzE1NzA0M2MzNGZkMjZmZDQ2OjkyNDMkYTRjMDYyMzBlNDhjOGZjZTdiZTg4YTA3NGEzYmIzZTA6OTI0NA==', + expectedEsURL: 'https://ac31ebb90241773157043c34fd26fd46.us-central1.gcp.cloud.es.io:9243', + expectedKibanaURL: + 'https://a4c06230e48c8fce7be88a074a3bb3e0.us-central1.gcp.cloud.es.io:9244', + }, + { + cloudID: + 'only-kb-set:dXMtY2VudHJhbDEuZ2NwLmNsb3VkLmVzLmlvJGFjMzFlYmI5MDI0MTc3MzE1NzA0M2MzNGZkMjZmZDQ2JGE0YzA2MjMwZTQ4YzhmY2U3YmU4OGEwNzRhM2JiM2UwOjkyNDQ=', + expectedEsURL: 'https://ac31ebb90241773157043c34fd26fd46.us-central1.gcp.cloud.es.io:443', + expectedKibanaURL: + 'https://a4c06230e48c8fce7be88a074a3bb3e0.us-central1.gcp.cloud.es.io:9244', + }, + { + cloudID: + 'host-and-kb-set:dXMtY2VudHJhbDEuZ2NwLmNsb3VkLmVzLmlvOjkyNDMkYWMzMWViYjkwMjQxNzczMTU3MDQzYzM0ZmQyNmZkNDYkYTRjMDYyMzBlNDhjOGZjZTdiZTg4YTA3NGEzYmIzZTA6OTI0NA==', + expectedEsURL: 'https://ac31ebb90241773157043c34fd26fd46.us-central1.gcp.cloud.es.io:9243', + expectedKibanaURL: + 'https://a4c06230e48c8fce7be88a074a3bb3e0.us-central1.gcp.cloud.es.io:9244', + }, + { + cloudID: + 'extra-items:dXMtY2VudHJhbDEuZ2NwLmNsb3VkLmVzLmlvJGFjMzFlYmI5MDI0MTc3MzE1NzA0M2MzNGZkMjZmZDQ2JGE0YzA2MjMwZTQ4YzhmY2U3YmU4OGEwNzRhM2JiM2UwJGFub3RoZXJpZCRhbmRhbm90aGVy', + expectedEsURL: 'https://ac31ebb90241773157043c34fd26fd46.us-central1.gcp.cloud.es.io:443', + expectedKibanaURL: + 'https://a4c06230e48c8fce7be88a074a3bb3e0.us-central1.gcp.cloud.es.io:443', + }, + ]; + + for (const test of tests) { + const decoded = decodeCloudId(test.cloudID); + expect(decoded).toBeTruthy(); + expect(decoded?.elasticsearchUrl === test.expectedEsURL).toBe(true); + expect(decoded?.kibanaUrl === test.expectedKibanaURL).toBe(true); + } + }); + + it('returns undefined for invalid formats', () => { + const tests = [ + { + cloudID: + 'staging:garbagedXMtZWFzdC0xLmF3cy5mb3VuZC5pbyRjZWM2ZjI2MWE3NGJmMjRjZTMzYmI4ODExYjg0Mjk0ZiRjNmMyY2E2ZDA0MjI0OWFmMGNjN2Q3YTllOTYyNTc0Mw==', + errorMsg: 'base64 decoding failed', + }, + { + cloudID: 'dXMtY2VudHJhbDEuZ2NwLmNsb3VkLmVzLmlvJDhhMDI4M2FmMDQxZjE5NWY3NzI5YmMwNGM2NmEwZg==', + errorMsg: 'Expected at least 3 parts', + }, + ]; + + for (const test of tests) { + const decoded = decodeCloudId(test.cloudID); + expect(decoded).toBe(undefined); + // decodeCloudId currently only logs; not throws errors + } + }); +}); diff --git a/x-pack/plugins/cloud/common/decode_cloud_id.ts b/x-pack/plugins/cloud/common/decode_cloud_id.ts new file mode 100644 index 00000000000000..2f27b80060ba17 --- /dev/null +++ b/x-pack/plugins/cloud/common/decode_cloud_id.ts @@ -0,0 +1,65 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +export interface DecodedCloudId { + host: string; + defaultPort: string; + elasticsearchUrl: string; + kibanaUrl: string; +} + +// decodeCloudId decodes the c.id into c.esURL and c.kibURL +export function decodeCloudId(cid: string): DecodedCloudId | undefined { + // 1. Ignore anything before `:`. + const id = cid.split(':').pop(); + if (!id) { + // throw new Error(`Unable to decode ${id}`); + // eslint-disable-next-line no-console + console.debug(`Unable to decode ${id}`); + return; + } + + // 2. base64 decode + let decoded: string | undefined; + try { + decoded = Buffer.from(id, 'base64').toString('utf8'); + } catch { + // throw new Error(`base64 decoding failed on ${id}`); + // eslint-disable-next-line no-console + console.debug(`base64 decoding failed on ${id}`); + return; + } + + // 3. separate based on `$` + const words = decoded.split('$'); + if (words.length < 3) { + // throw new Error(`Expected at least 3 parts in ${decoded}`); + // eslint-disable-next-line no-console + console.debug(`Expected at least 3 parts in ${decoded}`); + return; + } + // 4. extract port from the ES and Kibana host + const [host, defaultPort] = extractPortFromName(words[0]); + const [esId, esPort] = extractPortFromName(words[1], defaultPort); + const [kbId, kbPort] = extractPortFromName(words[2], defaultPort); + // 5. form the URLs + const esUrl = `https://${esId}.${host}:${esPort}`; + const kbUrl = `https://${kbId}.${host}:${kbPort}`; + return { + host, + defaultPort, + elasticsearchUrl: esUrl, + kibanaUrl: kbUrl, + }; +} + +// extractPortFromName takes a string in the form `id:port` and returns the +// Id and the port. If there's no `:`, the default port is returned +function extractPortFromName(word: string, defaultPort = '443') { + const [host, port = defaultPort] = word.split(':'); + return [host, port]; +} diff --git a/x-pack/plugins/cloud/public/index.ts b/x-pack/plugins/cloud/public/index.ts index ee37f85dfb6a7e..8c2c1791dc0d1b 100644 --- a/x-pack/plugins/cloud/public/index.ts +++ b/x-pack/plugins/cloud/public/index.ts @@ -8,7 +8,8 @@ import { PluginInitializerContext } from '@kbn/core/public'; import { CloudPlugin } from './plugin'; -export type { CloudSetup, CloudConfigType, CloudStart } from './plugin'; +export type { CloudSetup, CloudStart } from './types'; +export type { CloudConfigType } from './plugin'; export function plugin(initializerContext: PluginInitializerContext) { return new CloudPlugin(initializerContext); diff --git a/x-pack/plugins/cloud/public/plugin.tsx b/x-pack/plugins/cloud/public/plugin.tsx index ded7924b50631a..5896622b0ad0e5 100644 --- a/x-pack/plugins/cloud/public/plugin.tsx +++ b/x-pack/plugins/cloud/public/plugin.tsx @@ -7,10 +7,11 @@ import React, { FC } from 'react'; import type { CoreSetup, CoreStart, Plugin, PluginInitializerContext } from '@kbn/core/public'; - import { registerCloudDeploymentMetadataAnalyticsContext } from '../common/register_cloud_deployment_id_analytics_context'; import { getIsCloudEnabled } from '../common/is_cloud_enabled'; import { ELASTIC_SUPPORT_LINK, CLOUD_SNAPSHOTS_PATH } from '../common/constants'; +import { decodeCloudId, type DecodedCloudId } from '../common/decode_cloud_id'; +import type { CloudSetup, CloudStart } from './types'; import { getFullCloudUrl } from './utils'; export interface CloudConfigType { @@ -24,81 +25,6 @@ export interface CloudConfigType { is_elastic_staff_owned?: boolean; } -export interface CloudStart { - /** - * A React component that provides a pre-wired `React.Context` which connects components to Cloud services. - */ - CloudContextProvider: FC<{}>; - /** - * `true` when Kibana is running on Elastic Cloud. - */ - isCloudEnabled: boolean; - /** - * Cloud ID. Undefined if not running on Cloud. - */ - cloudId?: string; - /** - * The full URL to the deployment management page on Elastic Cloud. Undefined if not running on Cloud. - */ - deploymentUrl?: string; - /** - * The full URL to the user profile page on Elastic Cloud. Undefined if not running on Cloud. - */ - profileUrl?: string; - /** - * The full URL to the organization management page on Elastic Cloud. Undefined if not running on Cloud. - */ - organizationUrl?: string; -} - -export interface CloudSetup { - /** - * Cloud ID. Undefined if not running on Cloud. - */ - cloudId?: string; - /** - * This value is the same as `baseUrl` on ESS but can be customized on ECE. - */ - cname?: string; - /** - * This is the URL of the Cloud interface. - */ - baseUrl?: string; - /** - * The full URL to the deployment management page on Elastic Cloud. Undefined if not running on Cloud. - */ - deploymentUrl?: string; - /** - * The full URL to the user profile page on Elastic Cloud. Undefined if not running on Cloud. - */ - profileUrl?: string; - /** - * The full URL to the organization management page on Elastic Cloud. Undefined if not running on Cloud. - */ - organizationUrl?: string; - /** - * This is the path to the Snapshots page for the deployment to which the Kibana instance belongs. The value is already prepended with `deploymentUrl`. - */ - snapshotsUrl?: string; - /** - * `true` when Kibana is running on Elastic Cloud. - */ - isCloudEnabled: boolean; - /** - * When the Cloud Trial ends/ended for the organization that owns this deployment. Only available when running on Elastic Cloud. - */ - trialEndDate?: Date; - /** - * `true` if the Elastic Cloud organization that owns this deployment is owned by an Elastician. Only available when running on Elastic Cloud. - */ - isElasticStaffOwned?: boolean; - /** - * Registers CloudServiceProviders so start's `CloudContextProvider` hooks them. - * @param contextProvider The React component from the Service Provider. - */ - registerCloudService: (contextProvider: FC) => void; -} - interface CloudUrls { deploymentUrl?: string; profileUrl?: string; @@ -127,11 +53,18 @@ export class CloudPlugin implements Plugin { is_elastic_staff_owned: isElasticStaffOwned, } = this.config; + let decodedId: DecodedCloudId | undefined; + if (id) { + decodedId = decodeCloudId(id); + } + return { cloudId: id, cname, baseUrl, ...this.getCloudUrls(), + elasticsearchUrl: decodedId?.elasticsearchUrl, + kibanaUrl: decodedId?.kibanaUrl, trialEndDate: trialEndDate ? new Date(trialEndDate) : undefined, isElasticStaffOwned, isCloudEnabled: this.isCloudEnabled, @@ -161,6 +94,11 @@ export class CloudPlugin implements Plugin { const { deploymentUrl, profileUrl, organizationUrl } = this.getCloudUrls(); + let decodedId: DecodedCloudId | undefined; + if (this.config.id) { + decodedId = decodeCloudId(this.config.id); + } + return { CloudContextProvider, isCloudEnabled: this.isCloudEnabled, @@ -168,6 +106,8 @@ export class CloudPlugin implements Plugin { deploymentUrl, profileUrl, organizationUrl, + elasticsearchUrl: decodedId?.elasticsearchUrl, + kibanaUrl: decodedId?.kibanaUrl, }; } diff --git a/x-pack/plugins/cloud/public/types.ts b/x-pack/plugins/cloud/public/types.ts new file mode 100644 index 00000000000000..9c2f5030498736 --- /dev/null +++ b/x-pack/plugins/cloud/public/types.ts @@ -0,0 +1,99 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import type { FC } from 'react'; + +export interface CloudStart { + /** + * A React component that provides a pre-wired `React.Context` which connects components to Cloud services. + */ + CloudContextProvider: FC<{}>; + /** + * `true` when Kibana is running on Elastic Cloud. + */ + isCloudEnabled: boolean; + /** + * Cloud ID. Undefined if not running on Cloud. + */ + cloudId?: string; + /** + * The full URL to the deployment management page on Elastic Cloud. Undefined if not running on Cloud. + */ + deploymentUrl?: string; + /** + * The full URL to the user profile page on Elastic Cloud. Undefined if not running on Cloud. + */ + profileUrl?: string; + /** + * The full URL to the organization management page on Elastic Cloud. Undefined if not running on Cloud. + */ + organizationUrl?: string; + /** + * The full URL to the elasticsearch cluster. + */ + elasticsearchUrl?: string; + /** + * The full URL to the Kibana deployment. + */ + kibanaUrl?: string; +} + +export interface CloudSetup { + /** + * Cloud ID. Undefined if not running on Cloud. + */ + cloudId?: string; + /** + * This value is the same as `baseUrl` on ESS but can be customized on ECE. + */ + cname?: string; + /** + * This is the URL of the Cloud interface. + */ + baseUrl?: string; + /** + * The full URL to the deployment management page on Elastic Cloud. Undefined if not running on Cloud. + */ + deploymentUrl?: string; + /** + * The full URL to the user profile page on Elastic Cloud. Undefined if not running on Cloud. + */ + profileUrl?: string; + /** + * The full URL to the organization management page on Elastic Cloud. Undefined if not running on Cloud. + */ + organizationUrl?: string; + /** + * This is the path to the Snapshots page for the deployment to which the Kibana instance belongs. The value is already prepended with `deploymentUrl`. + */ + snapshotsUrl?: string; + /** + * The full URL to the elasticsearch cluster. + */ + elasticsearchUrl?: string; + /** + * The full URL to the Kibana deployment. + */ + kibanaUrl?: string; + /** + * `true` when Kibana is running on Elastic Cloud. + */ + isCloudEnabled: boolean; + /** + * When the Cloud Trial ends/ended for the organization that owns this deployment. Only available when running on Elastic Cloud. + */ + trialEndDate?: Date; + /** + * `true` if the Elastic Cloud organization that owns this deployment is owned by an Elastician. Only available when running on Elastic Cloud. + */ + isElasticStaffOwned?: boolean; + /** + * Registers CloudServiceProviders so start's `CloudContextProvider` hooks them. + * @param contextProvider The React component from the Service Provider. + */ + registerCloudService: (contextProvider: FC) => void; +} From 607fb45a6ee5b7304850bb280068857e65b6e18b Mon Sep 17 00:00:00 2001 From: pgayvallet Date: Mon, 12 Jun 2023 09:13:24 +0200 Subject: [PATCH 02/14] adapt fleet usages --- x-pack/plugins/cloud/server/plugin.ts | 26 +++++ .../common/services/decode_cloud_id.test.ts | 102 ------------------ .../fleet/common/services/decode_cloud_id.ts | 64 ----------- .../plugins/fleet/server/services/output.ts | 5 +- .../preconfiguration/fleet_server_host.ts | 13 +-- .../routes/custom_logs/get_cloud_urls.ts | 18 ---- .../server/routes/custom_logs/route.ts | 6 +- .../server/routes/elastic_agent/route.ts | 7 +- 8 files changed, 37 insertions(+), 204 deletions(-) delete mode 100644 x-pack/plugins/fleet/common/services/decode_cloud_id.test.ts delete mode 100644 x-pack/plugins/fleet/common/services/decode_cloud_id.ts delete mode 100644 x-pack/plugins/observability_onboarding/server/routes/custom_logs/get_cloud_urls.ts diff --git a/x-pack/plugins/cloud/server/plugin.ts b/x-pack/plugins/cloud/server/plugin.ts index 7d33ac99629b8b..44ba979a069699 100644 --- a/x-pack/plugins/cloud/server/plugin.ts +++ b/x-pack/plugins/cloud/server/plugin.ts @@ -12,6 +12,7 @@ import type { CloudConfigType } from './config'; import { registerCloudUsageCollector } from './collectors'; import { getIsCloudEnabled } from '../common/is_cloud_enabled'; import { parseDeploymentIdFromDeploymentUrl } from '../common/parse_deployment_id_from_deployment_url'; +import { decodeCloudId, DecodedCloudId } from '../common/decode_cloud_id'; import { readInstanceSizeMb } from './env'; interface PluginsSetup { @@ -30,6 +31,22 @@ export interface CloudSetup { * The deployment's ID. Only available when running on Elastic Cloud. */ deploymentId?: string; + /** + * The full URL to the elasticsearch cluster. + */ + elasticsearchUrl?: string; + /** + * The full URL to the Kibana deployment. + */ + kibanaUrl?: string; + /** + * {host} from the deployment url https://.. + */ + cloudHost?: string; + /** + * {port} from the deployment url https://.. + */ + cloudDefaultPort?: string; /** * `true` when running on Elastic Cloud. */ @@ -81,10 +98,19 @@ export class CloudPlugin implements Plugin { isElasticStaffOwned: this.config.is_elastic_staff_owned, }); + let decodedId: DecodedCloudId | undefined; + if (this.config.id) { + decodedId = decodeCloudId(this.config.id); + } + return { cloudId: this.config.id, instanceSizeMb: readInstanceSizeMb(), deploymentId: parseDeploymentIdFromDeploymentUrl(this.config.deployment_url), + elasticsearchUrl: decodedId?.elasticsearchUrl, + kibanaUrl: decodedId?.kibanaUrl, + cloudHost: decodedId?.host, + cloudDefaultPort: decodedId?.defaultPort, isCloudEnabled, trialEndDate: this.config.trial_end_date ? new Date(this.config.trial_end_date) : undefined, isElasticStaffOwned: this.config.is_elastic_staff_owned, diff --git a/x-pack/plugins/fleet/common/services/decode_cloud_id.test.ts b/x-pack/plugins/fleet/common/services/decode_cloud_id.test.ts deleted file mode 100644 index 5ec709b1c8ccdd..00000000000000 --- a/x-pack/plugins/fleet/common/services/decode_cloud_id.test.ts +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -import { decodeCloudId } from './decode_cloud_id'; - -describe('Fleet - decodeCloudId', () => { - it('parses various CloudID formats', () => { - const tests = [ - { - cloudID: - 'staging:dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyRjZWM2ZjI2MWE3NGJmMjRjZTMzYmI4ODExYjg0Mjk0ZiRjNmMyY2E2ZDA0MjI0OWFmMGNjN2Q3YTllOTYyNTc0Mw==', - expectedEsURL: 'https://cec6f261a74bf24ce33bb8811b84294f.us-east-1.aws.found.io:443', - expectedKibanaURL: 'https://c6c2ca6d042249af0cc7d7a9e9625743.us-east-1.aws.found.io:443', - }, - { - cloudID: - 'dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyRjZWM2ZjI2MWE3NGJmMjRjZTMzYmI4ODExYjg0Mjk0ZiRjNmMyY2E2ZDA0MjI0OWFmMGNjN2Q3YTllOTYyNTc0Mw==', - expectedEsURL: 'https://cec6f261a74bf24ce33bb8811b84294f.us-east-1.aws.found.io:443', - expectedKibanaURL: 'https://c6c2ca6d042249af0cc7d7a9e9625743.us-east-1.aws.found.io:443', - }, - { - cloudID: - ':dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyRjZWM2ZjI2MWE3NGJmMjRjZTMzYmI4ODExYjg0Mjk0ZiRjNmMyY2E2ZDA0MjI0OWFmMGNjN2Q3YTllOTYyNTc0Mw==', - expectedEsURL: 'https://cec6f261a74bf24ce33bb8811b84294f.us-east-1.aws.found.io:443', - expectedKibanaURL: 'https://c6c2ca6d042249af0cc7d7a9e9625743.us-east-1.aws.found.io:443', - }, - { - cloudID: - 'gcp-cluster:dXMtY2VudHJhbDEuZ2NwLmNsb3VkLmVzLmlvJDhhMDI4M2FmMDQxZjE5NWY3NzI5YmMwNGM2NmEwZmNlJDBjZDVjZDU2OGVlYmU1M2M4OWViN2NhZTViYWM4YjM3', - expectedEsURL: 'https://8a0283af041f195f7729bc04c66a0fce.us-central1.gcp.cloud.es.io:443', - expectedKibanaURL: - 'https://0cd5cd568eebe53c89eb7cae5bac8b37.us-central1.gcp.cloud.es.io:443', - }, - { - cloudID: - 'custom-port:dXMtY2VudHJhbDEuZ2NwLmNsb3VkLmVzLmlvOjkyNDMkYWMzMWViYjkwMjQxNzczMTU3MDQzYzM0ZmQyNmZkNDYkYTRjMDYyMzBlNDhjOGZjZTdiZTg4YTA3NGEzYmIzZTA=', - expectedEsURL: 'https://ac31ebb90241773157043c34fd26fd46.us-central1.gcp.cloud.es.io:9243', - expectedKibanaURL: - 'https://a4c06230e48c8fce7be88a074a3bb3e0.us-central1.gcp.cloud.es.io:9243', - }, - { - cloudID: - 'different-es-kb-port:dXMtY2VudHJhbDEuZ2NwLmNsb3VkLmVzLmlvJGFjMzFlYmI5MDI0MTc3MzE1NzA0M2MzNGZkMjZmZDQ2OjkyNDMkYTRjMDYyMzBlNDhjOGZjZTdiZTg4YTA3NGEzYmIzZTA6OTI0NA==', - expectedEsURL: 'https://ac31ebb90241773157043c34fd26fd46.us-central1.gcp.cloud.es.io:9243', - expectedKibanaURL: - 'https://a4c06230e48c8fce7be88a074a3bb3e0.us-central1.gcp.cloud.es.io:9244', - }, - { - cloudID: - 'only-kb-set:dXMtY2VudHJhbDEuZ2NwLmNsb3VkLmVzLmlvJGFjMzFlYmI5MDI0MTc3MzE1NzA0M2MzNGZkMjZmZDQ2JGE0YzA2MjMwZTQ4YzhmY2U3YmU4OGEwNzRhM2JiM2UwOjkyNDQ=', - expectedEsURL: 'https://ac31ebb90241773157043c34fd26fd46.us-central1.gcp.cloud.es.io:443', - expectedKibanaURL: - 'https://a4c06230e48c8fce7be88a074a3bb3e0.us-central1.gcp.cloud.es.io:9244', - }, - { - cloudID: - 'host-and-kb-set:dXMtY2VudHJhbDEuZ2NwLmNsb3VkLmVzLmlvOjkyNDMkYWMzMWViYjkwMjQxNzczMTU3MDQzYzM0ZmQyNmZkNDYkYTRjMDYyMzBlNDhjOGZjZTdiZTg4YTA3NGEzYmIzZTA6OTI0NA==', - expectedEsURL: 'https://ac31ebb90241773157043c34fd26fd46.us-central1.gcp.cloud.es.io:9243', - expectedKibanaURL: - 'https://a4c06230e48c8fce7be88a074a3bb3e0.us-central1.gcp.cloud.es.io:9244', - }, - { - cloudID: - 'extra-items:dXMtY2VudHJhbDEuZ2NwLmNsb3VkLmVzLmlvJGFjMzFlYmI5MDI0MTc3MzE1NzA0M2MzNGZkMjZmZDQ2JGE0YzA2MjMwZTQ4YzhmY2U3YmU4OGEwNzRhM2JiM2UwJGFub3RoZXJpZCRhbmRhbm90aGVy', - expectedEsURL: 'https://ac31ebb90241773157043c34fd26fd46.us-central1.gcp.cloud.es.io:443', - expectedKibanaURL: - 'https://a4c06230e48c8fce7be88a074a3bb3e0.us-central1.gcp.cloud.es.io:443', - }, - ]; - - for (const test of tests) { - const decoded = decodeCloudId(test.cloudID); - expect(decoded).toBeTruthy(); - expect(decoded?.elasticsearchUrl === test.expectedEsURL).toBe(true); - expect(decoded?.kibanaUrl === test.expectedKibanaURL).toBe(true); - } - }); - - it('returns undefined for invalid formats', () => { - const tests = [ - { - cloudID: - 'staging:garbagedXMtZWFzdC0xLmF3cy5mb3VuZC5pbyRjZWM2ZjI2MWE3NGJmMjRjZTMzYmI4ODExYjg0Mjk0ZiRjNmMyY2E2ZDA0MjI0OWFmMGNjN2Q3YTllOTYyNTc0Mw==', - errorMsg: 'base64 decoding failed', - }, - { - cloudID: 'dXMtY2VudHJhbDEuZ2NwLmNsb3VkLmVzLmlvJDhhMDI4M2FmMDQxZjE5NWY3NzI5YmMwNGM2NmEwZg==', - errorMsg: 'Expected at least 3 parts', - }, - ]; - - for (const test of tests) { - const decoded = decodeCloudId(test.cloudID); - expect(decoded).toBe(undefined); - // decodeCloudId currently only logs; not throws errors - } - }); -}); diff --git a/x-pack/plugins/fleet/common/services/decode_cloud_id.ts b/x-pack/plugins/fleet/common/services/decode_cloud_id.ts deleted file mode 100644 index 2afc15e8f71b5e..00000000000000 --- a/x-pack/plugins/fleet/common/services/decode_cloud_id.ts +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -// decodeCloudId decodes the c.id into c.esURL and c.kibURL -export function decodeCloudId(cid: string): - | { - host: string; - defaultPort: string; - elasticsearchUrl: string; - kibanaUrl: string; - } - | undefined { - // 1. Ignore anything before `:`. - const id = cid.split(':').pop(); - if (!id) { - // throw new Error(`Unable to decode ${id}`); - // eslint-disable-next-line no-console - console.debug(`Unable to decode ${id}`); - return; - } - - // 2. base64 decode - let decoded: string | undefined; - try { - decoded = Buffer.from(id, 'base64').toString('utf8'); - } catch { - // throw new Error(`base64 decoding failed on ${id}`); - // eslint-disable-next-line no-console - console.debug(`base64 decoding failed on ${id}`); - return; - } - - // 3. separate based on `$` - const words = decoded.split('$'); - if (words.length < 3) { - // throw new Error(`Expected at least 3 parts in ${decoded}`); - // eslint-disable-next-line no-console - console.debug(`Expected at least 3 parts in ${decoded}`); - return; - } - // 4. extract port from the ES and Kibana host - const [host, defaultPort] = extractPortFromName(words[0]); - const [esId, esPort] = extractPortFromName(words[1], defaultPort); - const [kbId, kbPort] = extractPortFromName(words[2], defaultPort); - // 5. form the URLs - const esUrl = `https://${esId}.${host}:${esPort}`; - const kbUrl = `https://${kbId}.${host}:${kbPort}`; - return { - host, - defaultPort, - elasticsearchUrl: esUrl, - kibanaUrl: kbUrl, - }; -} -// extractPortFromName takes a string in the form `id:port` and returns the -// Id and the port. If there's no `:`, the default port is returned -function extractPortFromName(word: string, defaultPort = '443') { - const [host, port = defaultPort] = word.split(':'); - return [host, port]; -} diff --git a/x-pack/plugins/fleet/server/services/output.ts b/x-pack/plugins/fleet/server/services/output.ts index 8666330f6640f5..50fb7b54a53461 100644 --- a/x-pack/plugins/fleet/server/services/output.ts +++ b/x-pack/plugins/fleet/server/services/output.ts @@ -25,7 +25,7 @@ import { AGENT_POLICY_SAVED_OBJECT_TYPE, } from '../constants'; import { SO_SEARCH_LIMIT, outputType } from '../../common/constants'; -import { decodeCloudId, normalizeHostsForAgents } from '../../common/services'; +import { normalizeHostsForAgents } from '../../common/services'; import { OutputUnauthorizedError, OutputInvalidError, @@ -289,8 +289,7 @@ class OutputService { public getDefaultESHosts(): string[] { const cloud = appContextService.getCloud(); - const cloudId = cloud?.isCloudEnabled && cloud.cloudId; - const cloudUrl = cloudId && decodeCloudId(cloudId)?.elasticsearchUrl; + const cloudUrl = cloud?.elasticsearchUrl; const cloudHosts = cloudUrl ? [cloudUrl] : undefined; const flagHosts = appContextService.getConfig()!.agents?.elasticsearch?.hosts && diff --git a/x-pack/plugins/fleet/server/services/preconfiguration/fleet_server_host.ts b/x-pack/plugins/fleet/server/services/preconfiguration/fleet_server_host.ts index 3dc25332a848bb..65543ee01f883d 100644 --- a/x-pack/plugins/fleet/server/services/preconfiguration/fleet_server_host.ts +++ b/x-pack/plugins/fleet/server/services/preconfiguration/fleet_server_host.ts @@ -7,7 +7,7 @@ import type { ElasticsearchClient, SavedObjectsClientContract } from '@kbn/core/server'; -import { decodeCloudId, normalizeHostsForAgents } from '../../../common/services'; +import { normalizeHostsForAgents } from '../../../common/services'; import type { FleetConfigType } from '../../config'; import { DEFAULT_FLEET_SERVER_HOST_ID } from '../../constants'; @@ -27,16 +27,11 @@ import { isDifferent } from './utils'; export function getCloudFleetServersHosts() { const cloudSetup = appContextService.getCloud(); - if (cloudSetup && cloudSetup.isCloudEnabled && cloudSetup.cloudId && cloudSetup.deploymentId) { - const res = decodeCloudId(cloudSetup.cloudId); - if (!res) { - return; - } - + if (cloudSetup && cloudSetup.isCloudEnabled && cloudSetup.cloudHost) { // Fleet Server url are formed like this `https://.fleet. return [ - `https://${cloudSetup.deploymentId}.fleet.${res.host}${ - res.defaultPort !== '443' ? `:${res.defaultPort}` : '' + `https://${cloudSetup.deploymentId}.fleet.${cloudSetup.cloudHost}${ + cloudSetup.cloudDefaultPort !== '443' ? `:${cloudSetup.cloudDefaultPort}` : '' }`, ]; } diff --git a/x-pack/plugins/observability_onboarding/server/routes/custom_logs/get_cloud_urls.ts b/x-pack/plugins/observability_onboarding/server/routes/custom_logs/get_cloud_urls.ts deleted file mode 100644 index 62e6e5d84fcb94..00000000000000 --- a/x-pack/plugins/observability_onboarding/server/routes/custom_logs/get_cloud_urls.ts +++ /dev/null @@ -1,18 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -import { decodeCloudId } from '@kbn/fleet-plugin/common'; - -export function getCloudUrls(cloudId: string) { - const decodedCloudId = decodeCloudId(cloudId); - if (decodedCloudId) { - return { - elasticsearchUrl: decodedCloudId.elasticsearchUrl, - kibanaUrl: decodedCloudId.kibanaUrl, - }; - } -} diff --git a/x-pack/plugins/observability_onboarding/server/routes/custom_logs/route.ts b/x-pack/plugins/observability_onboarding/server/routes/custom_logs/route.ts index d9498011d03744..583e79ef54cfcb 100644 --- a/x-pack/plugins/observability_onboarding/server/routes/custom_logs/route.ts +++ b/x-pack/plugins/observability_onboarding/server/routes/custom_logs/route.ts @@ -11,7 +11,6 @@ import { ObservabilityOnboardingState } from '../../saved_objects/observability_ import { createObservabilityOnboardingServerRoute } from '../create_observability_onboarding_server_route'; import { createShipperApiKey } from './api_key/create_shipper_api_key'; import { hasLogMonitoringPrivileges } from './api_key/has_log_monitoring_privileges'; -import { getCloudUrls } from './get_cloud_urls'; import { getFallbackUrls } from './get_fallback_urls'; import { getHasLogs } from './get_has_logs'; import { getObservabilityOnboardingState } from './get_observability_onboarding_state'; @@ -50,9 +49,8 @@ const installShipperSetupRoute = createObservabilityOnboardingServerRoute({ const { core, plugins } = resources; const coreStart = await core.start(); - const cloudId = plugins.cloud.setup.cloudId; - const { kibanaUrl } = - (cloudId && getCloudUrls(cloudId)) || getFallbackUrls(coreStart); + const kibanaUrl = + plugins.cloud.setup.kibanaUrl ?? getFallbackUrls(coreStart).kibanaUrl; const scriptDownloadUrl = `${kibanaUrl}/plugins/observabilityOnboarding/assets/standalone_agent_setup.sh`; const apiEndpoint = `${kibanaUrl}/api/observability_onboarding`; diff --git a/x-pack/plugins/observability_onboarding/server/routes/elastic_agent/route.ts b/x-pack/plugins/observability_onboarding/server/routes/elastic_agent/route.ts index 7d5505b79c9b9d..27f874ee82e594 100644 --- a/x-pack/plugins/observability_onboarding/server/routes/elastic_agent/route.ts +++ b/x-pack/plugins/observability_onboarding/server/routes/elastic_agent/route.ts @@ -9,7 +9,6 @@ import { getAuthenticationAPIKey } from '../../lib/get_authentication_api_key'; import { createObservabilityOnboardingServerRoute } from '../create_observability_onboarding_server_route'; import { getObservabilityOnboardingState } from '../custom_logs/get_observability_onboarding_state'; import { generateYml } from './generate_yml'; -import { getCloudUrls } from '../custom_logs/get_cloud_urls'; import { getFallbackUrls } from '../custom_logs/get_fallback_urls'; const generateConfig = createObservabilityOnboardingServerRoute({ @@ -23,9 +22,9 @@ const generateConfig = createObservabilityOnboardingServerRoute({ const savedObjectsClient = coreStart.savedObjects.createInternalRepository(); - const cloudId = plugins.cloud.setup.cloudId; - const { elasticsearchUrl } = - (cloudId && getCloudUrls(cloudId)) || getFallbackUrls(coreStart); + const elasticsearchUrl = + plugins.cloud.setup.elasticsearchUrl ?? + getFallbackUrls(coreStart).elasticsearchUrl; const savedState = await getObservabilityOnboardingState({ savedObjectsClient, From 80934be5a248955611bfc11aed3ce65245314497 Mon Sep 17 00:00:00 2001 From: pgayvallet Date: Mon, 12 Jun 2023 09:29:06 +0200 Subject: [PATCH 03/14] adapt other usages --- x-pack/plugins/cloud/public/plugin.tsx | 2 + x-pack/plugins/cloud/public/types.ts | 8 ++ .../analytics_collection_integrate_view.tsx | 4 +- .../engine_connect/search_application_api.tsx | 4 +- .../components/curl_request/curl_request.tsx | 3 +- .../shared/cloud_details/cloud_details.ts | 4 + .../decode_cloud_id/decode_cloud_id.test.ts | 104 ------------------ .../shared/decode_cloud_id/decode_cloud_id.ts | 66 ----------- .../get_cloud_enterprise_search_host.ts | 13 +-- x-pack/plugins/fleet/common/index.ts | 2 - x-pack/plugins/fleet/common/services/index.ts | 1 - .../common/services/decode_cloud_id.test.ts | 104 ------------------ .../common/services/decode_cloud_id.ts | 66 ----------- .../application/components/overview.tsx | 8 +- .../server/synthetics_service/get_es_hosts.ts | 6 +- 15 files changed, 26 insertions(+), 369 deletions(-) delete mode 100644 x-pack/plugins/enterprise_search/public/applications/shared/decode_cloud_id/decode_cloud_id.test.ts delete mode 100644 x-pack/plugins/enterprise_search/public/applications/shared/decode_cloud_id/decode_cloud_id.ts delete mode 100644 x-pack/plugins/serverless_search/common/services/decode_cloud_id.test.ts delete mode 100644 x-pack/plugins/serverless_search/common/services/decode_cloud_id.ts diff --git a/x-pack/plugins/cloud/public/plugin.tsx b/x-pack/plugins/cloud/public/plugin.tsx index 5896622b0ad0e5..2165ec7ad52fc3 100644 --- a/x-pack/plugins/cloud/public/plugin.tsx +++ b/x-pack/plugins/cloud/public/plugin.tsx @@ -65,6 +65,8 @@ export class CloudPlugin implements Plugin { ...this.getCloudUrls(), elasticsearchUrl: decodedId?.elasticsearchUrl, kibanaUrl: decodedId?.kibanaUrl, + cloudHost: decodedId?.host, + cloudDefaultPort: decodedId?.defaultPort, trialEndDate: trialEndDate ? new Date(trialEndDate) : undefined, isElasticStaffOwned, isCloudEnabled: this.isCloudEnabled, diff --git a/x-pack/plugins/cloud/public/types.ts b/x-pack/plugins/cloud/public/types.ts index 9c2f5030498736..d7bdf319deb673 100644 --- a/x-pack/plugins/cloud/public/types.ts +++ b/x-pack/plugins/cloud/public/types.ts @@ -79,6 +79,14 @@ export interface CloudSetup { * The full URL to the Kibana deployment. */ kibanaUrl?: string; + /** + * {host} from the deployment url https://.. + */ + cloudHost?: string; + /** + * {port} from the deployment url https://.. + */ + cloudDefaultPort?: string; /** * `true` when Kibana is running on Elastic Cloud. */ diff --git a/x-pack/plugins/enterprise_search/public/applications/analytics/components/analytics_collection_view/analytics_collection_integrate/analytics_collection_integrate_view.tsx b/x-pack/plugins/enterprise_search/public/applications/analytics/components/analytics_collection_view/analytics_collection_integrate/analytics_collection_integrate_view.tsx index 903486e025adf6..56b575f0aef396 100644 --- a/x-pack/plugins/enterprise_search/public/applications/analytics/components/analytics_collection_view/analytics_collection_integrate/analytics_collection_integrate_view.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/analytics/components/analytics_collection_view/analytics_collection_integrate/analytics_collection_integrate_view.tsx @@ -28,7 +28,6 @@ import { i18n } from '@kbn/i18n'; import { AnalyticsCollection } from '../../../../../../common/types/analytics'; import { useCloudDetails } from '../../../../shared/cloud_details/cloud_details'; -import { decodeCloudId } from '../../../../shared/decode_cloud_id/decode_cloud_id'; import { docLinks } from '../../../../shared/doc_links'; import { KibanaLogic } from '../../../../shared/kibana'; @@ -196,8 +195,7 @@ export const AnalyticsCollectionIntegrateView: React.FC { const defaultUrl = 'https://localhost:9200'; - const url = - (cloudContext.cloudId && decodeCloudId(cloudContext.cloudId)?.elasticsearchUrl) || defaultUrl; + const url = cloudContext.elasticsearchUrl || defaultUrl; return url; }; diff --git a/x-pack/plugins/enterprise_search/public/applications/enterprise_search_content/components/search_index/components/curl_request/curl_request.tsx b/x-pack/plugins/enterprise_search/public/applications/enterprise_search_content/components/search_index/components/curl_request/curl_request.tsx index 6dfbf1c5083611..094f1568f7a6f6 100644 --- a/x-pack/plugins/enterprise_search/public/applications/enterprise_search_content/components/search_index/components/curl_request/curl_request.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/enterprise_search_content/components/search_index/components/curl_request/curl_request.tsx @@ -30,8 +30,7 @@ export const CurlRequest: React.FC = ({ const cloudContext = useCloudDetails(); const DEFAULT_URL = 'https://localhost:9200'; - const baseUrl = - (cloudContext.cloudId && decodeCloudId(cloudContext.cloudId)?.elasticsearchUrl) || DEFAULT_URL; + const baseUrl = cloudContext.elasticsearchUrl || DEFAULT_URL; const apiKeyExample = apiKey || ''; const { name: pipelineName, ...pipelineParams } = pipeline ?? {}; // We have to prefix the parameters with an underscore because that's what the actual pipeline looks for diff --git a/x-pack/plugins/enterprise_search/public/applications/shared/cloud_details/cloud_details.ts b/x-pack/plugins/enterprise_search/public/applications/shared/cloud_details/cloud_details.ts index b46a7f9c130fb7..33a5ab1d114966 100644 --- a/x-pack/plugins/enterprise_search/public/applications/shared/cloud_details/cloud_details.ts +++ b/x-pack/plugins/enterprise_search/public/applications/shared/cloud_details/cloud_details.ts @@ -12,6 +12,8 @@ import { KibanaLogic } from '../kibana'; export interface CloudDetails { cloudId: string | undefined; deploymentUrl: string | undefined; + elasticsearchUrl: string | undefined; + kibanaUrl: string | undefined; } export const useCloudDetails = (): CloudDetails => { @@ -19,5 +21,7 @@ export const useCloudDetails = (): CloudDetails => { return { cloudId: cloud?.cloudId, deploymentUrl: cloud?.deploymentUrl, + elasticsearchUrl: cloud?.elasticsearchUrl, + kibanaUrl: cloud?.kibanaUrl, }; }; diff --git a/x-pack/plugins/enterprise_search/public/applications/shared/decode_cloud_id/decode_cloud_id.test.ts b/x-pack/plugins/enterprise_search/public/applications/shared/decode_cloud_id/decode_cloud_id.test.ts deleted file mode 100644 index 2886d5f5b91414..00000000000000 --- a/x-pack/plugins/enterprise_search/public/applications/shared/decode_cloud_id/decode_cloud_id.test.ts +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -import { decodeCloudId } from './decode_cloud_id'; - -// Copied from Fleet's solution -// x-pack/fleet/common/services/decode_cloud_id.test.ts -describe('Enterprise Search - decodeCloudId', () => { - it('parses various CloudID formats', () => { - const tests = [ - { - cloudID: - 'staging:dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyRjZWM2ZjI2MWE3NGJmMjRjZTMzYmI4ODExYjg0Mjk0ZiRjNmMyY2E2ZDA0MjI0OWFmMGNjN2Q3YTllOTYyNTc0Mw==', - expectedEsURL: 'https://cec6f261a74bf24ce33bb8811b84294f.us-east-1.aws.found.io:443', - expectedKibanaURL: 'https://c6c2ca6d042249af0cc7d7a9e9625743.us-east-1.aws.found.io:443', - }, - { - cloudID: - 'dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyRjZWM2ZjI2MWE3NGJmMjRjZTMzYmI4ODExYjg0Mjk0ZiRjNmMyY2E2ZDA0MjI0OWFmMGNjN2Q3YTllOTYyNTc0Mw==', - expectedEsURL: 'https://cec6f261a74bf24ce33bb8811b84294f.us-east-1.aws.found.io:443', - expectedKibanaURL: 'https://c6c2ca6d042249af0cc7d7a9e9625743.us-east-1.aws.found.io:443', - }, - { - cloudID: - ':dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyRjZWM2ZjI2MWE3NGJmMjRjZTMzYmI4ODExYjg0Mjk0ZiRjNmMyY2E2ZDA0MjI0OWFmMGNjN2Q3YTllOTYyNTc0Mw==', - expectedEsURL: 'https://cec6f261a74bf24ce33bb8811b84294f.us-east-1.aws.found.io:443', - expectedKibanaURL: 'https://c6c2ca6d042249af0cc7d7a9e9625743.us-east-1.aws.found.io:443', - }, - { - cloudID: - 'gcp-cluster:dXMtY2VudHJhbDEuZ2NwLmNsb3VkLmVzLmlvJDhhMDI4M2FmMDQxZjE5NWY3NzI5YmMwNGM2NmEwZmNlJDBjZDVjZDU2OGVlYmU1M2M4OWViN2NhZTViYWM4YjM3', - expectedEsURL: 'https://8a0283af041f195f7729bc04c66a0fce.us-central1.gcp.cloud.es.io:443', - expectedKibanaURL: - 'https://0cd5cd568eebe53c89eb7cae5bac8b37.us-central1.gcp.cloud.es.io:443', - }, - { - cloudID: - 'custom-port:dXMtY2VudHJhbDEuZ2NwLmNsb3VkLmVzLmlvOjkyNDMkYWMzMWViYjkwMjQxNzczMTU3MDQzYzM0ZmQyNmZkNDYkYTRjMDYyMzBlNDhjOGZjZTdiZTg4YTA3NGEzYmIzZTA=', - expectedEsURL: 'https://ac31ebb90241773157043c34fd26fd46.us-central1.gcp.cloud.es.io:9243', - expectedKibanaURL: - 'https://a4c06230e48c8fce7be88a074a3bb3e0.us-central1.gcp.cloud.es.io:9243', - }, - { - cloudID: - 'different-es-kb-port:dXMtY2VudHJhbDEuZ2NwLmNsb3VkLmVzLmlvJGFjMzFlYmI5MDI0MTc3MzE1NzA0M2MzNGZkMjZmZDQ2OjkyNDMkYTRjMDYyMzBlNDhjOGZjZTdiZTg4YTA3NGEzYmIzZTA6OTI0NA==', - expectedEsURL: 'https://ac31ebb90241773157043c34fd26fd46.us-central1.gcp.cloud.es.io:9243', - expectedKibanaURL: - 'https://a4c06230e48c8fce7be88a074a3bb3e0.us-central1.gcp.cloud.es.io:9244', - }, - { - cloudID: - 'only-kb-set:dXMtY2VudHJhbDEuZ2NwLmNsb3VkLmVzLmlvJGFjMzFlYmI5MDI0MTc3MzE1NzA0M2MzNGZkMjZmZDQ2JGE0YzA2MjMwZTQ4YzhmY2U3YmU4OGEwNzRhM2JiM2UwOjkyNDQ=', - expectedEsURL: 'https://ac31ebb90241773157043c34fd26fd46.us-central1.gcp.cloud.es.io:443', - expectedKibanaURL: - 'https://a4c06230e48c8fce7be88a074a3bb3e0.us-central1.gcp.cloud.es.io:9244', - }, - { - cloudID: - 'host-and-kb-set:dXMtY2VudHJhbDEuZ2NwLmNsb3VkLmVzLmlvOjkyNDMkYWMzMWViYjkwMjQxNzczMTU3MDQzYzM0ZmQyNmZkNDYkYTRjMDYyMzBlNDhjOGZjZTdiZTg4YTA3NGEzYmIzZTA6OTI0NA==', - expectedEsURL: 'https://ac31ebb90241773157043c34fd26fd46.us-central1.gcp.cloud.es.io:9243', - expectedKibanaURL: - 'https://a4c06230e48c8fce7be88a074a3bb3e0.us-central1.gcp.cloud.es.io:9244', - }, - { - cloudID: - 'extra-items:dXMtY2VudHJhbDEuZ2NwLmNsb3VkLmVzLmlvJGFjMzFlYmI5MDI0MTc3MzE1NzA0M2MzNGZkMjZmZDQ2JGE0YzA2MjMwZTQ4YzhmY2U3YmU4OGEwNzRhM2JiM2UwJGFub3RoZXJpZCRhbmRhbm90aGVy', - expectedEsURL: 'https://ac31ebb90241773157043c34fd26fd46.us-central1.gcp.cloud.es.io:443', - expectedKibanaURL: - 'https://a4c06230e48c8fce7be88a074a3bb3e0.us-central1.gcp.cloud.es.io:443', - }, - ]; - - for (const test of tests) { - const decoded = decodeCloudId(test.cloudID); - expect(decoded).toBeTruthy(); - expect(decoded?.elasticsearchUrl === test.expectedEsURL).toBe(true); - expect(decoded?.kibanaUrl === test.expectedKibanaURL).toBe(true); - } - }); - - it('returns undefined for invalid formats', () => { - const tests = [ - { - cloudID: - 'staging:garbagedXMtZWFzdC0xLmF3cy5mb3VuZC5pbyRjZWM2ZjI2MWE3NGJmMjRjZTMzYmI4ODExYjg0Mjk0ZiRjNmMyY2E2ZDA0MjI0OWFmMGNjN2Q3YTllOTYyNTc0Mw==', - errorMsg: 'base64 decoding failed', - }, - { - cloudID: 'dXMtY2VudHJhbDEuZ2NwLmNsb3VkLmVzLmlvJDhhMDI4M2FmMDQxZjE5NWY3NzI5YmMwNGM2NmEwZg==', - errorMsg: 'Expected at least 3 parts', - }, - ]; - - for (const test of tests) { - const decoded = decodeCloudId(test.cloudID); - expect(decoded).toBe(undefined); - // decodeCloudId currently only logs; not throws errors - } - }); -}); diff --git a/x-pack/plugins/enterprise_search/public/applications/shared/decode_cloud_id/decode_cloud_id.ts b/x-pack/plugins/enterprise_search/public/applications/shared/decode_cloud_id/decode_cloud_id.ts deleted file mode 100644 index f0059495ef17e1..00000000000000 --- a/x-pack/plugins/enterprise_search/public/applications/shared/decode_cloud_id/decode_cloud_id.ts +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -// copied this solution from fleet due to time constraints -// x-pack/fleet/common/services/decode_cloud_id.ts -// decodeCloudId decodes the c.id into c.esURL and c.kibURL -export function decodeCloudId(cid: string): - | { - defaultPort: string; - elasticsearchUrl: string; - host: string; - kibanaUrl: string; - } - | undefined { - // 1. Ignore anything before `:`. - const id = cid.split(':').pop(); - if (!id) { - // throw new Error(`Unable to decode ${id}`); - // eslint-disable-next-line no-console - console.debug(`Unable to decode ${id}`); - return; - } - - // 2. base64 decode - let decoded: string | undefined; - try { - decoded = Buffer.from(id, 'base64').toString('utf8'); - } catch { - // throw new Error(`base64 decoding failed on ${id}`); - // eslint-disable-next-line no-console - console.debug(`base64 decoding failed on ${id}`); - return; - } - - // 3. separate based on `$` - const words = decoded.split('$'); - if (words.length < 3) { - // throw new Error(`Expected at least 3 parts in ${decoded}`); - // eslint-disable-next-line no-console - console.debug(`Expected at least 3 parts in ${decoded}`); - return; - } - // 4. extract port from the ES and Kibana host - const [host, defaultPort] = extractPortFromName(words[0]); - const [esId, esPort] = extractPortFromName(words[1], defaultPort); - const [kbId, kbPort] = extractPortFromName(words[2], defaultPort); - // 5. form the URLs - const esUrl = `https://${esId}.${host}:${esPort}`; - const kbUrl = `https://${kbId}.${host}:${kbPort}`; - return { - defaultPort, - elasticsearchUrl: esUrl, - host, - kibanaUrl: kbUrl, - }; -} -// extractPortFromName takes a string in the form `id:port` and returns the -// Id and the port. If there's no `:`, the default port is returned -function extractPortFromName(word: string, defaultPort = '443') { - const [host, port = defaultPort] = word.split(':'); - return [host, port]; -} diff --git a/x-pack/plugins/enterprise_search/public/applications/shared/get_cloud_enterprise_search_host/get_cloud_enterprise_search_host.ts b/x-pack/plugins/enterprise_search/public/applications/shared/get_cloud_enterprise_search_host/get_cloud_enterprise_search_host.ts index e7a61113b51eac..eba9696e0b4b29 100644 --- a/x-pack/plugins/enterprise_search/public/applications/shared/get_cloud_enterprise_search_host/get_cloud_enterprise_search_host.ts +++ b/x-pack/plugins/enterprise_search/public/applications/shared/get_cloud_enterprise_search_host/get_cloud_enterprise_search_host.ts @@ -5,21 +5,18 @@ * 2.0. */ -import { CloudSetup } from '@kbn/cloud-plugin/public'; - -import { decodeCloudId } from '../decode_cloud_id/decode_cloud_id'; +import type { CloudSetup } from '@kbn/cloud-plugin/public'; export function getCloudEnterpriseSearchHost(cloud: CloudSetup | undefined): string | undefined { - if (cloud && cloud.isCloudEnabled && cloud.cloudId) { + if (cloud && cloud.isCloudEnabled && cloud.cloudId && cloud.cloudHost) { const deploymentId = getDeploymentId(cloud.cloudId); - const res = decodeCloudId(cloud.cloudId); - if (!(deploymentId && res)) { + if (!deploymentId) { return; } // Enterprise Search Server url are formed like this `https://.ent. - return `https://${deploymentId}.ent.${res.host}${ - res.defaultPort !== '443' ? `:${res.defaultPort}` : '' + return `https://${deploymentId}.ent.${cloud.cloudHost}${ + cloud.cloudDefaultPort !== '443' ? `:${cloud.cloudDefaultPort}` : '' }`; } } diff --git a/x-pack/plugins/fleet/common/index.ts b/x-pack/plugins/fleet/common/index.ts index 21b66ed14a0ff7..6d89be74ada25d 100644 --- a/x-pack/plugins/fleet/common/index.ts +++ b/x-pack/plugins/fleet/common/index.ts @@ -71,8 +71,6 @@ export { // Package policy helpers isValidNamespace, INVALID_NAMESPACE_CHARACTERS, - // TODO Should probably not be exposed by Fleet - decodeCloudId, getFileMetadataIndexName, getFileDataIndexName, } from './services'; diff --git a/x-pack/plugins/fleet/common/services/index.ts b/x-pack/plugins/fleet/common/services/index.ts index 6f799c4a01b0e0..671f416f0c972d 100644 --- a/x-pack/plugins/fleet/common/services/index.ts +++ b/x-pack/plugins/fleet/common/services/index.ts @@ -15,7 +15,6 @@ export { } from './package_to_package_policy'; export { fullAgentPolicyToYaml } from './full_agent_policy_to_yaml'; export { isPackageLimited, doesAgentPolicyAlreadyIncludePackage } from './limited_package'; -export { decodeCloudId } from './decode_cloud_id'; export { isValidNamespace, INVALID_NAMESPACE_CHARACTERS } from './is_valid_namespace'; export { isDiffPathProtocol } from './is_diff_path_protocol'; export { LicenseService } from './license'; diff --git a/x-pack/plugins/serverless_search/common/services/decode_cloud_id.test.ts b/x-pack/plugins/serverless_search/common/services/decode_cloud_id.test.ts deleted file mode 100644 index 2886d5f5b91414..00000000000000 --- a/x-pack/plugins/serverless_search/common/services/decode_cloud_id.test.ts +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -import { decodeCloudId } from './decode_cloud_id'; - -// Copied from Fleet's solution -// x-pack/fleet/common/services/decode_cloud_id.test.ts -describe('Enterprise Search - decodeCloudId', () => { - it('parses various CloudID formats', () => { - const tests = [ - { - cloudID: - 'staging:dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyRjZWM2ZjI2MWE3NGJmMjRjZTMzYmI4ODExYjg0Mjk0ZiRjNmMyY2E2ZDA0MjI0OWFmMGNjN2Q3YTllOTYyNTc0Mw==', - expectedEsURL: 'https://cec6f261a74bf24ce33bb8811b84294f.us-east-1.aws.found.io:443', - expectedKibanaURL: 'https://c6c2ca6d042249af0cc7d7a9e9625743.us-east-1.aws.found.io:443', - }, - { - cloudID: - 'dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyRjZWM2ZjI2MWE3NGJmMjRjZTMzYmI4ODExYjg0Mjk0ZiRjNmMyY2E2ZDA0MjI0OWFmMGNjN2Q3YTllOTYyNTc0Mw==', - expectedEsURL: 'https://cec6f261a74bf24ce33bb8811b84294f.us-east-1.aws.found.io:443', - expectedKibanaURL: 'https://c6c2ca6d042249af0cc7d7a9e9625743.us-east-1.aws.found.io:443', - }, - { - cloudID: - ':dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyRjZWM2ZjI2MWE3NGJmMjRjZTMzYmI4ODExYjg0Mjk0ZiRjNmMyY2E2ZDA0MjI0OWFmMGNjN2Q3YTllOTYyNTc0Mw==', - expectedEsURL: 'https://cec6f261a74bf24ce33bb8811b84294f.us-east-1.aws.found.io:443', - expectedKibanaURL: 'https://c6c2ca6d042249af0cc7d7a9e9625743.us-east-1.aws.found.io:443', - }, - { - cloudID: - 'gcp-cluster:dXMtY2VudHJhbDEuZ2NwLmNsb3VkLmVzLmlvJDhhMDI4M2FmMDQxZjE5NWY3NzI5YmMwNGM2NmEwZmNlJDBjZDVjZDU2OGVlYmU1M2M4OWViN2NhZTViYWM4YjM3', - expectedEsURL: 'https://8a0283af041f195f7729bc04c66a0fce.us-central1.gcp.cloud.es.io:443', - expectedKibanaURL: - 'https://0cd5cd568eebe53c89eb7cae5bac8b37.us-central1.gcp.cloud.es.io:443', - }, - { - cloudID: - 'custom-port:dXMtY2VudHJhbDEuZ2NwLmNsb3VkLmVzLmlvOjkyNDMkYWMzMWViYjkwMjQxNzczMTU3MDQzYzM0ZmQyNmZkNDYkYTRjMDYyMzBlNDhjOGZjZTdiZTg4YTA3NGEzYmIzZTA=', - expectedEsURL: 'https://ac31ebb90241773157043c34fd26fd46.us-central1.gcp.cloud.es.io:9243', - expectedKibanaURL: - 'https://a4c06230e48c8fce7be88a074a3bb3e0.us-central1.gcp.cloud.es.io:9243', - }, - { - cloudID: - 'different-es-kb-port:dXMtY2VudHJhbDEuZ2NwLmNsb3VkLmVzLmlvJGFjMzFlYmI5MDI0MTc3MzE1NzA0M2MzNGZkMjZmZDQ2OjkyNDMkYTRjMDYyMzBlNDhjOGZjZTdiZTg4YTA3NGEzYmIzZTA6OTI0NA==', - expectedEsURL: 'https://ac31ebb90241773157043c34fd26fd46.us-central1.gcp.cloud.es.io:9243', - expectedKibanaURL: - 'https://a4c06230e48c8fce7be88a074a3bb3e0.us-central1.gcp.cloud.es.io:9244', - }, - { - cloudID: - 'only-kb-set:dXMtY2VudHJhbDEuZ2NwLmNsb3VkLmVzLmlvJGFjMzFlYmI5MDI0MTc3MzE1NzA0M2MzNGZkMjZmZDQ2JGE0YzA2MjMwZTQ4YzhmY2U3YmU4OGEwNzRhM2JiM2UwOjkyNDQ=', - expectedEsURL: 'https://ac31ebb90241773157043c34fd26fd46.us-central1.gcp.cloud.es.io:443', - expectedKibanaURL: - 'https://a4c06230e48c8fce7be88a074a3bb3e0.us-central1.gcp.cloud.es.io:9244', - }, - { - cloudID: - 'host-and-kb-set:dXMtY2VudHJhbDEuZ2NwLmNsb3VkLmVzLmlvOjkyNDMkYWMzMWViYjkwMjQxNzczMTU3MDQzYzM0ZmQyNmZkNDYkYTRjMDYyMzBlNDhjOGZjZTdiZTg4YTA3NGEzYmIzZTA6OTI0NA==', - expectedEsURL: 'https://ac31ebb90241773157043c34fd26fd46.us-central1.gcp.cloud.es.io:9243', - expectedKibanaURL: - 'https://a4c06230e48c8fce7be88a074a3bb3e0.us-central1.gcp.cloud.es.io:9244', - }, - { - cloudID: - 'extra-items:dXMtY2VudHJhbDEuZ2NwLmNsb3VkLmVzLmlvJGFjMzFlYmI5MDI0MTc3MzE1NzA0M2MzNGZkMjZmZDQ2JGE0YzA2MjMwZTQ4YzhmY2U3YmU4OGEwNzRhM2JiM2UwJGFub3RoZXJpZCRhbmRhbm90aGVy', - expectedEsURL: 'https://ac31ebb90241773157043c34fd26fd46.us-central1.gcp.cloud.es.io:443', - expectedKibanaURL: - 'https://a4c06230e48c8fce7be88a074a3bb3e0.us-central1.gcp.cloud.es.io:443', - }, - ]; - - for (const test of tests) { - const decoded = decodeCloudId(test.cloudID); - expect(decoded).toBeTruthy(); - expect(decoded?.elasticsearchUrl === test.expectedEsURL).toBe(true); - expect(decoded?.kibanaUrl === test.expectedKibanaURL).toBe(true); - } - }); - - it('returns undefined for invalid formats', () => { - const tests = [ - { - cloudID: - 'staging:garbagedXMtZWFzdC0xLmF3cy5mb3VuZC5pbyRjZWM2ZjI2MWE3NGJmMjRjZTMzYmI4ODExYjg0Mjk0ZiRjNmMyY2E2ZDA0MjI0OWFmMGNjN2Q3YTllOTYyNTc0Mw==', - errorMsg: 'base64 decoding failed', - }, - { - cloudID: 'dXMtY2VudHJhbDEuZ2NwLmNsb3VkLmVzLmlvJDhhMDI4M2FmMDQxZjE5NWY3NzI5YmMwNGM2NmEwZg==', - errorMsg: 'Expected at least 3 parts', - }, - ]; - - for (const test of tests) { - const decoded = decodeCloudId(test.cloudID); - expect(decoded).toBe(undefined); - // decodeCloudId currently only logs; not throws errors - } - }); -}); diff --git a/x-pack/plugins/serverless_search/common/services/decode_cloud_id.ts b/x-pack/plugins/serverless_search/common/services/decode_cloud_id.ts deleted file mode 100644 index f0059495ef17e1..00000000000000 --- a/x-pack/plugins/serverless_search/common/services/decode_cloud_id.ts +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -// copied this solution from fleet due to time constraints -// x-pack/fleet/common/services/decode_cloud_id.ts -// decodeCloudId decodes the c.id into c.esURL and c.kibURL -export function decodeCloudId(cid: string): - | { - defaultPort: string; - elasticsearchUrl: string; - host: string; - kibanaUrl: string; - } - | undefined { - // 1. Ignore anything before `:`. - const id = cid.split(':').pop(); - if (!id) { - // throw new Error(`Unable to decode ${id}`); - // eslint-disable-next-line no-console - console.debug(`Unable to decode ${id}`); - return; - } - - // 2. base64 decode - let decoded: string | undefined; - try { - decoded = Buffer.from(id, 'base64').toString('utf8'); - } catch { - // throw new Error(`base64 decoding failed on ${id}`); - // eslint-disable-next-line no-console - console.debug(`base64 decoding failed on ${id}`); - return; - } - - // 3. separate based on `$` - const words = decoded.split('$'); - if (words.length < 3) { - // throw new Error(`Expected at least 3 parts in ${decoded}`); - // eslint-disable-next-line no-console - console.debug(`Expected at least 3 parts in ${decoded}`); - return; - } - // 4. extract port from the ES and Kibana host - const [host, defaultPort] = extractPortFromName(words[0]); - const [esId, esPort] = extractPortFromName(words[1], defaultPort); - const [kbId, kbPort] = extractPortFromName(words[2], defaultPort); - // 5. form the URLs - const esUrl = `https://${esId}.${host}:${esPort}`; - const kbUrl = `https://${kbId}.${host}:${kbPort}`; - return { - defaultPort, - elasticsearchUrl: esUrl, - host, - kibanaUrl: kbUrl, - }; -} -// extractPortFromName takes a string in the form `id:port` and returns the -// Id and the port. If there's no `:`, the default port is returned -function extractPortFromName(word: string, defaultPort = '443') { - const [host, port = defaultPort] = word.split(':'); - return [host, port]; -} diff --git a/x-pack/plugins/serverless_search/public/application/components/overview.tsx b/x-pack/plugins/serverless_search/public/application/components/overview.tsx index 52ba85870c29f8..59c0a0f2eca882 100644 --- a/x-pack/plugins/serverless_search/public/application/components/overview.tsx +++ b/x-pack/plugins/serverless_search/public/application/components/overview.tsx @@ -22,7 +22,6 @@ import { i18n } from '@kbn/i18n'; import React, { useMemo, useState } from 'react'; import { docLinks } from '../../../common/doc_links'; import { PLUGIN_ID } from '../../../common'; -import { decodeCloudId } from '../../../common/services/decode_cloud_id'; import { useKibanaServices } from '../hooks/use_kibana'; import { CodeBox } from './code_box'; import { javascriptDefinition } from './languages/javascript'; @@ -49,12 +48,9 @@ export const ElasticsearchOverview = () => { http, userProfile, } = useKibanaServices(); - const cloudId = cloud.cloudId ?? ''; const elasticsearchURL = useMemo(() => { - if (cloudId.length === 0) return ELASTICSEARCH_URL_PLACEHOLDER; - const decodedCloudId = decodeCloudId(cloudId); - return decodedCloudId?.elasticsearchUrl ?? ELASTICSEARCH_URL_PLACEHOLDER; - }, [cloudId]); + return cloud?.elasticsearchUrl ?? ELASTICSEARCH_URL_PLACEHOLDER; + }, [cloud]); const assetBasePath = http.basePath.prepend(`/plugins/${PLUGIN_ID}/assets/`); const codeSnippetArguments: LanguageDefinitionSnippetArguments = { url: elasticsearchURL, diff --git a/x-pack/plugins/synthetics/server/synthetics_service/get_es_hosts.ts b/x-pack/plugins/synthetics/server/synthetics_service/get_es_hosts.ts index 646ec52c2fb300..23a3f5aaf27ce3 100644 --- a/x-pack/plugins/synthetics/server/synthetics_service/get_es_hosts.ts +++ b/x-pack/plugins/synthetics/server/synthetics_service/get_es_hosts.ts @@ -12,8 +12,7 @@ * 2.0. */ -import { CloudSetup } from '@kbn/cloud-plugin/server'; -import { decodeCloudId } from '@kbn/fleet-plugin/common'; +import type { CloudSetup } from '@kbn/cloud-plugin/server'; import { ServiceConfig } from '../../common/config'; export function getEsHosts({ @@ -23,8 +22,7 @@ export function getEsHosts({ cloud?: CloudSetup; config: ServiceConfig; }): string[] { - const cloudId = cloud?.isCloudEnabled && cloud.cloudId; - const cloudUrl = cloudId && decodeCloudId(cloudId)?.elasticsearchUrl; + const cloudUrl = cloud?.isCloudEnabled && cloud?.elasticsearchUrl; const cloudHosts = cloudUrl ? [cloudUrl] : undefined; if (cloudHosts && cloudHosts.length > 0) { return cloudHosts; From d08de8c2e6e7ea438ce04697594e051cb603ea81 Mon Sep 17 00:00:00 2001 From: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Date: Mon, 12 Jun 2023 07:35:59 +0000 Subject: [PATCH 04/14] [CI] Auto-commit changed files from 'node scripts/precommit_hook.js --ref HEAD~1..HEAD --fix' --- .../search_index/components/curl_request/curl_request.tsx | 2 -- 1 file changed, 2 deletions(-) diff --git a/x-pack/plugins/enterprise_search/public/applications/enterprise_search_content/components/search_index/components/curl_request/curl_request.tsx b/x-pack/plugins/enterprise_search/public/applications/enterprise_search_content/components/search_index/components/curl_request/curl_request.tsx index 094f1568f7a6f6..a645a60fb67fba 100644 --- a/x-pack/plugins/enterprise_search/public/applications/enterprise_search_content/components/search_index/components/curl_request/curl_request.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/enterprise_search_content/components/search_index/components/curl_request/curl_request.tsx @@ -12,8 +12,6 @@ import { EuiCodeBlock } from '@elastic/eui'; import { IngestPipelineParams } from '../../../../../../../common/types/connectors'; import { useCloudDetails } from '../../../../../shared/cloud_details/cloud_details'; -import { decodeCloudId } from '../../../../../shared/decode_cloud_id/decode_cloud_id'; - interface CurlRequestParams { apiKey?: string; document?: Record; From 6e23426707e6112a3cfff7658372c986638c3d6b Mon Sep 17 00:00:00 2001 From: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Date: Mon, 12 Jun 2023 07:41:57 +0000 Subject: [PATCH 05/14] [CI] Auto-commit changed files from 'node scripts/lint_ts_projects --fix' --- x-pack/plugins/observability_onboarding/tsconfig.json | 1 - 1 file changed, 1 deletion(-) diff --git a/x-pack/plugins/observability_onboarding/tsconfig.json b/x-pack/plugins/observability_onboarding/tsconfig.json index 3452fd941d51f4..8b2e56775c57bb 100644 --- a/x-pack/plugins/observability_onboarding/tsconfig.json +++ b/x-pack/plugins/observability_onboarding/tsconfig.json @@ -24,7 +24,6 @@ "@kbn/shared-ux-router", "@kbn/i18n-react", "@kbn/cloud-plugin", - "@kbn/fleet-plugin", "@kbn/usage-collection-plugin", "@kbn/observability-shared-plugin", "@kbn/core-http-server", From 65057b97006b58e4a96bda59d162b4466dc7cf37 Mon Sep 17 00:00:00 2001 From: pgayvallet Date: Mon, 12 Jun 2023 10:01:52 +0200 Subject: [PATCH 06/14] adapt unit tests --- .../get_cloud_enterprise_search_host.test.ts | 4 ++++ .../get_cloud_enterprise_search_host.ts | 2 +- x-pack/plugins/fleet/server/services/output.test.ts | 3 ++- .../services/preconfiguration/fleet_server_host.test.ts | 4 ++++ .../server/services/preconfiguration/fleet_server_host.ts | 4 +++- .../server/routes/custom_logs/route.ts | 2 +- .../server/routes/elastic_agent/route.ts | 2 +- .../synthetics/server/synthetics_service/get_es_hosts.test.ts | 1 + 8 files changed, 17 insertions(+), 5 deletions(-) diff --git a/x-pack/plugins/enterprise_search/public/applications/shared/get_cloud_enterprise_search_host/get_cloud_enterprise_search_host.test.ts b/x-pack/plugins/enterprise_search/public/applications/shared/get_cloud_enterprise_search_host/get_cloud_enterprise_search_host.test.ts index 7744c5d96a400f..7a8296b12882da 100644 --- a/x-pack/plugins/enterprise_search/public/applications/shared/get_cloud_enterprise_search_host/get_cloud_enterprise_search_host.test.ts +++ b/x-pack/plugins/enterprise_search/public/applications/shared/get_cloud_enterprise_search_host/get_cloud_enterprise_search_host.test.ts @@ -11,6 +11,8 @@ const defaultPortCloud = { cloudId: 'gcp-cluster:dXMtY2VudHJhbDEuZ2NwLmNsb3VkLmVzLmlvJDhhMDI4M2FmMDQxZjE5NWY3NzI5YmMwNGM2NmEwZmNlJDBjZDVjZDU2OGVlYmU1M2M4OWViN2NhZTViYWM4YjM3', isCloudEnabled: true, + cloudHost: 'us-central1.gcp.cloud.es.io', + cloudDefaultPort: '443', registerCloudService: jest.fn(), }; // 9243 @@ -18,6 +20,8 @@ const customPortCloud = { cloudId: 'custom-port:dXMtY2VudHJhbDEuZ2NwLmNsb3VkLmVzLmlvOjkyNDMkYWMzMWViYjkwMjQxNzczMTU3MDQzYzM0ZmQyNmZkNDYkYTRjMDYyMzBlNDhjOGZjZTdiZTg4YTA3NGEzYmIzZTA=', isCloudEnabled: true, + cloudHost: 'us-central1.gcp.cloud.es.io', + cloudDefaultPort: '9243', registerCloudService: jest.fn(), }; const missingDeploymentIdCloud = { diff --git a/x-pack/plugins/enterprise_search/public/applications/shared/get_cloud_enterprise_search_host/get_cloud_enterprise_search_host.ts b/x-pack/plugins/enterprise_search/public/applications/shared/get_cloud_enterprise_search_host/get_cloud_enterprise_search_host.ts index eba9696e0b4b29..2e01307fe28fac 100644 --- a/x-pack/plugins/enterprise_search/public/applications/shared/get_cloud_enterprise_search_host/get_cloud_enterprise_search_host.ts +++ b/x-pack/plugins/enterprise_search/public/applications/shared/get_cloud_enterprise_search_host/get_cloud_enterprise_search_host.ts @@ -16,7 +16,7 @@ export function getCloudEnterpriseSearchHost(cloud: CloudSetup | undefined): str // Enterprise Search Server url are formed like this `https://.ent. return `https://${deploymentId}.ent.${cloud.cloudHost}${ - cloud.cloudDefaultPort !== '443' ? `:${cloud.cloudDefaultPort}` : '' + cloud.cloudDefaultPort && cloud.cloudDefaultPort !== '443' ? `:${cloud.cloudDefaultPort}` : '' }`; } } diff --git a/x-pack/plugins/fleet/server/services/output.test.ts b/x-pack/plugins/fleet/server/services/output.test.ts index 630dda81b76446..16a27abb0b3302 100644 --- a/x-pack/plugins/fleet/server/services/output.test.ts +++ b/x-pack/plugins/fleet/server/services/output.test.ts @@ -1050,11 +1050,12 @@ describe('Output Service', () => { mockedAppContextService.getConfig.mockReset(); mockedAppContextService.getConfig.mockReset(); }); - it('Should use cloud ID as the source of truth for ES hosts', () => { + it('Should use cloud plugin as the source of truth for ES hosts', () => { // @ts-expect-error mockedAppContextService.getCloud.mockReturnValue({ isCloudEnabled: true, cloudId: CLOUD_ID, + elasticsearchUrl: 'https://cec6f261a74bf24ce33bb8811b84294f.us-east-1.aws.found.io:443', }); mockedAppContextService.getConfig.mockReturnValue(CONFIG_WITH_ES_HOSTS); diff --git a/x-pack/plugins/fleet/server/services/preconfiguration/fleet_server_host.test.ts b/x-pack/plugins/fleet/server/services/preconfiguration/fleet_server_host.test.ts index aec64226ca5eca..f7a1d2b8611acc 100644 --- a/x-pack/plugins/fleet/server/services/preconfiguration/fleet_server_host.test.ts +++ b/x-pack/plugins/fleet/server/services/preconfiguration/fleet_server_host.test.ts @@ -118,6 +118,7 @@ describe('getCloudFleetServersHosts', () => { 'dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyRjZWM2ZjI2MWE3NGJmMjRjZTMzYmI4ODExYjg0Mjk0ZiRjNmMyY2E2ZDA0MjI0OWFmMGNjN2Q3YTllOTYyNTc0Mw==', isCloudEnabled: true, deploymentId: 'deployment-id-1', + cloudHost: 'us-east-1.aws.found.io', apm: {}, }); @@ -134,6 +135,8 @@ describe('getCloudFleetServersHosts', () => { 'test:dGVzdC5mcjo5MjQzJGRhM2I2YjNkYWY5ZDRjODE4ZjI4ZmEzNDdjMzgzODViJDgxMmY4NWMxZjNjZTQ2YTliYjgxZjFjMWIxMzRjNmRl', isCloudEnabled: true, deploymentId: 'deployment-id-1', + cloudHost: 'test.fr', + cloudDefaultPort: '9243', apm: {}, }); @@ -185,6 +188,7 @@ describe('createCloudFleetServerHostIfNeeded', () => { 'dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyRjZWM2ZjI2MWE3NGJmMjRjZTMzYmI4ODExYjg0Mjk0ZiRjNmMyY2E2ZDA0MjI0OWFmMGNjN2Q3YTllOTYyNTc0Mw==', isCloudEnabled: true, deploymentId: 'deployment-id-1', + cloudHost: 'us-east-1.aws.found.io', apm: {}, }); mockedGetDefaultFleetServerHost.mockResolvedValue(null); diff --git a/x-pack/plugins/fleet/server/services/preconfiguration/fleet_server_host.ts b/x-pack/plugins/fleet/server/services/preconfiguration/fleet_server_host.ts index 65543ee01f883d..d0a2dac7b641e2 100644 --- a/x-pack/plugins/fleet/server/services/preconfiguration/fleet_server_host.ts +++ b/x-pack/plugins/fleet/server/services/preconfiguration/fleet_server_host.ts @@ -31,7 +31,9 @@ export function getCloudFleetServersHosts() { // Fleet Server url are formed like this `https://.fleet. return [ `https://${cloudSetup.deploymentId}.fleet.${cloudSetup.cloudHost}${ - cloudSetup.cloudDefaultPort !== '443' ? `:${cloudSetup.cloudDefaultPort}` : '' + cloudSetup.cloudDefaultPort && cloudSetup.cloudDefaultPort !== '443' + ? `:${cloudSetup.cloudDefaultPort}` + : '' }`, ]; } diff --git a/x-pack/plugins/observability_onboarding/server/routes/custom_logs/route.ts b/x-pack/plugins/observability_onboarding/server/routes/custom_logs/route.ts index 583e79ef54cfcb..52cc979a27e7a9 100644 --- a/x-pack/plugins/observability_onboarding/server/routes/custom_logs/route.ts +++ b/x-pack/plugins/observability_onboarding/server/routes/custom_logs/route.ts @@ -50,7 +50,7 @@ const installShipperSetupRoute = createObservabilityOnboardingServerRoute({ const coreStart = await core.start(); const kibanaUrl = - plugins.cloud.setup.kibanaUrl ?? getFallbackUrls(coreStart).kibanaUrl; + plugins.cloud?.setup?.kibanaUrl ?? getFallbackUrls(coreStart).kibanaUrl; const scriptDownloadUrl = `${kibanaUrl}/plugins/observabilityOnboarding/assets/standalone_agent_setup.sh`; const apiEndpoint = `${kibanaUrl}/api/observability_onboarding`; diff --git a/x-pack/plugins/observability_onboarding/server/routes/elastic_agent/route.ts b/x-pack/plugins/observability_onboarding/server/routes/elastic_agent/route.ts index 27f874ee82e594..320547410bd91a 100644 --- a/x-pack/plugins/observability_onboarding/server/routes/elastic_agent/route.ts +++ b/x-pack/plugins/observability_onboarding/server/routes/elastic_agent/route.ts @@ -23,7 +23,7 @@ const generateConfig = createObservabilityOnboardingServerRoute({ coreStart.savedObjects.createInternalRepository(); const elasticsearchUrl = - plugins.cloud.setup.elasticsearchUrl ?? + plugins.cloud?.setup?.elasticsearchUrl ?? getFallbackUrls(coreStart).elasticsearchUrl; const savedState = await getObservabilityOnboardingState({ diff --git a/x-pack/plugins/synthetics/server/synthetics_service/get_es_hosts.test.ts b/x-pack/plugins/synthetics/server/synthetics_service/get_es_hosts.test.ts index 0e198b000e6ea0..05e50f07f437b8 100644 --- a/x-pack/plugins/synthetics/server/synthetics_service/get_es_hosts.test.ts +++ b/x-pack/plugins/synthetics/server/synthetics_service/get_es_hosts.test.ts @@ -20,6 +20,7 @@ describe('getEsHostsTest', () => { cloudId: 'TLS_Test:dXMtY2VudHJhbDEuZ2NwLmNsb3VkLmVzLmlvJDI0ZDYwY2NjYmZjODRhZmZhNGRjYTQ3M2M2YjFlZDgwJGUxMjkyY2YzMTczZTRkNTViZDViM2NlNzYyZDg1NzY3', isCloudEnabled: true, + elasticsearchUrl: 'https://24d60cccbfc84affa4dca473c6b1ed80.us-central1.gcp.cloud.es.io:443', } as CloudSetup; it('should return expected host in cloud', function () { From f2b91d5079a345d965187c83df2af656177a09dc Mon Sep 17 00:00:00 2001 From: pgayvallet Date: Mon, 12 Jun 2023 10:05:23 +0200 Subject: [PATCH 07/14] add deploymentId to public contract --- x-pack/plugins/cloud/public/plugin.tsx | 2 ++ x-pack/plugins/cloud/public/types.ts | 4 ++++ .../get_cloud_enterprise_search_host.test.ts | 2 ++ .../get_cloud_enterprise_search_host.ts | 17 ++--------------- 4 files changed, 10 insertions(+), 15 deletions(-) diff --git a/x-pack/plugins/cloud/public/plugin.tsx b/x-pack/plugins/cloud/public/plugin.tsx index 2165ec7ad52fc3..e976fa34159ab3 100644 --- a/x-pack/plugins/cloud/public/plugin.tsx +++ b/x-pack/plugins/cloud/public/plugin.tsx @@ -9,6 +9,7 @@ import React, { FC } from 'react'; import type { CoreSetup, CoreStart, Plugin, PluginInitializerContext } from '@kbn/core/public'; import { registerCloudDeploymentMetadataAnalyticsContext } from '../common/register_cloud_deployment_id_analytics_context'; import { getIsCloudEnabled } from '../common/is_cloud_enabled'; +import { parseDeploymentIdFromDeploymentUrl } from '../common/parse_deployment_id_from_deployment_url'; import { ELASTIC_SUPPORT_LINK, CLOUD_SNAPSHOTS_PATH } from '../common/constants'; import { decodeCloudId, type DecodedCloudId } from '../common/decode_cloud_id'; import type { CloudSetup, CloudStart } from './types'; @@ -60,6 +61,7 @@ export class CloudPlugin implements Plugin { return { cloudId: id, + deploymentId: parseDeploymentIdFromDeploymentUrl(this.config.deployment_url), cname, baseUrl, ...this.getCloudUrls(), diff --git a/x-pack/plugins/cloud/public/types.ts b/x-pack/plugins/cloud/public/types.ts index d7bdf319deb673..ae862a981cd551 100644 --- a/x-pack/plugins/cloud/public/types.ts +++ b/x-pack/plugins/cloud/public/types.ts @@ -47,6 +47,10 @@ export interface CloudSetup { * Cloud ID. Undefined if not running on Cloud. */ cloudId?: string; + /** + * The deployment's ID. Only available when running on Elastic Cloud. + */ + deploymentId?: string; /** * This value is the same as `baseUrl` on ESS but can be customized on ECE. */ diff --git a/x-pack/plugins/enterprise_search/public/applications/shared/get_cloud_enterprise_search_host/get_cloud_enterprise_search_host.test.ts b/x-pack/plugins/enterprise_search/public/applications/shared/get_cloud_enterprise_search_host/get_cloud_enterprise_search_host.test.ts index 7a8296b12882da..a784bf910ee632 100644 --- a/x-pack/plugins/enterprise_search/public/applications/shared/get_cloud_enterprise_search_host/get_cloud_enterprise_search_host.test.ts +++ b/x-pack/plugins/enterprise_search/public/applications/shared/get_cloud_enterprise_search_host/get_cloud_enterprise_search_host.test.ts @@ -10,6 +10,7 @@ import { getCloudEnterpriseSearchHost } from './get_cloud_enterprise_search_host const defaultPortCloud = { cloudId: 'gcp-cluster:dXMtY2VudHJhbDEuZ2NwLmNsb3VkLmVzLmlvJDhhMDI4M2FmMDQxZjE5NWY3NzI5YmMwNGM2NmEwZmNlJDBjZDVjZDU2OGVlYmU1M2M4OWViN2NhZTViYWM4YjM3', + deploymentId: 'gcp-cluster', isCloudEnabled: true, cloudHost: 'us-central1.gcp.cloud.es.io', cloudDefaultPort: '443', @@ -19,6 +20,7 @@ const defaultPortCloud = { const customPortCloud = { cloudId: 'custom-port:dXMtY2VudHJhbDEuZ2NwLmNsb3VkLmVzLmlvOjkyNDMkYWMzMWViYjkwMjQxNzczMTU3MDQzYzM0ZmQyNmZkNDYkYTRjMDYyMzBlNDhjOGZjZTdiZTg4YTA3NGEzYmIzZTA=', + deploymentId: 'custom-port', isCloudEnabled: true, cloudHost: 'us-central1.gcp.cloud.es.io', cloudDefaultPort: '9243', diff --git a/x-pack/plugins/enterprise_search/public/applications/shared/get_cloud_enterprise_search_host/get_cloud_enterprise_search_host.ts b/x-pack/plugins/enterprise_search/public/applications/shared/get_cloud_enterprise_search_host/get_cloud_enterprise_search_host.ts index 2e01307fe28fac..01e8ae84be1944 100644 --- a/x-pack/plugins/enterprise_search/public/applications/shared/get_cloud_enterprise_search_host/get_cloud_enterprise_search_host.ts +++ b/x-pack/plugins/enterprise_search/public/applications/shared/get_cloud_enterprise_search_host/get_cloud_enterprise_search_host.ts @@ -8,23 +8,10 @@ import type { CloudSetup } from '@kbn/cloud-plugin/public'; export function getCloudEnterpriseSearchHost(cloud: CloudSetup | undefined): string | undefined { - if (cloud && cloud.isCloudEnabled && cloud.cloudId && cloud.cloudHost) { - const deploymentId = getDeploymentId(cloud.cloudId); - if (!deploymentId) { - return; - } - + if (cloud && cloud.isCloudEnabled && cloud.cloudId && cloud.deploymentId && cloud.cloudHost) { // Enterprise Search Server url are formed like this `https://.ent. - return `https://${deploymentId}.ent.${cloud.cloudHost}${ + return `https://${cloud.deploymentId}.ent.${cloud.cloudHost}${ cloud.cloudDefaultPort && cloud.cloudDefaultPort !== '443' ? `:${cloud.cloudDefaultPort}` : '' }`; } } - -function getDeploymentId(cloudId: string): string | undefined { - const [deploymentId, rest] = cloudId.split(':'); - - if (deploymentId && rest) { - return deploymentId; - } -} From e0dc1cc7c0b6d0a0e5a9e239164a63fa0bbf5608 Mon Sep 17 00:00:00 2001 From: pgayvallet Date: Mon, 12 Jun 2023 10:08:36 +0200 Subject: [PATCH 08/14] update the mocks --- x-pack/plugins/cloud/public/mocks.tsx | 9 +++++++-- x-pack/plugins/cloud/server/mocks.ts | 8 ++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/x-pack/plugins/cloud/public/mocks.tsx b/x-pack/plugins/cloud/public/mocks.tsx index fb1b66adcec984..38c63ecb5cfb84 100644 --- a/x-pack/plugins/cloud/public/mocks.tsx +++ b/x-pack/plugins/cloud/public/mocks.tsx @@ -7,17 +7,22 @@ import React from 'react'; -import { CloudStart } from '.'; +import type { CloudSetup, CloudStart } from './types'; -function createSetupMock() { +function createSetupMock(): jest.Mocked { return { cloudId: 'mock-cloud-id', + deploymentId: 'mock-deployment-id', isCloudEnabled: true, cname: 'cname', baseUrl: 'base-url', deploymentUrl: 'deployment-url', profileUrl: 'profile-url', organizationUrl: 'organization-url', + elasticsearchUrl: 'elasticsearch-url', + kibanaUrl: 'kibana-url', + cloudHost: 'cloud-host', + cloudDefaultPort: '443', isElasticStaffOwned: true, trialEndDate: new Date('2020-10-01T14:13:12Z'), registerCloudService: jest.fn(), diff --git a/x-pack/plugins/cloud/server/mocks.ts b/x-pack/plugins/cloud/server/mocks.ts index ad647689514502..10423bd3e4cc9f 100644 --- a/x-pack/plugins/cloud/server/mocks.ts +++ b/x-pack/plugins/cloud/server/mocks.ts @@ -5,13 +5,17 @@ * 2.0. */ -import { CloudSetup } from '.'; +import type { CloudSetup } from './plugin'; function createSetupMock(): jest.Mocked { return { cloudId: 'mock-cloud-id', - instanceSizeMb: 1234, deploymentId: 'deployment-id', + elasticsearchUrl: 'elasticsearch-url', + kibanaUrl: 'kibana-url', + cloudHost: 'cloud-host', + cloudDefaultPort: '443', + instanceSizeMb: 1234, isCloudEnabled: true, isElasticStaffOwned: true, trialEndDate: new Date('2020-10-01T14:13:12Z'), From 296beda5df8df38bc4ed8d421bd3b6353b5b5790 Mon Sep 17 00:00:00 2001 From: pgayvallet Date: Mon, 12 Jun 2023 10:31:31 +0200 Subject: [PATCH 09/14] add unit tests for plugin --- .../plugins/cloud/public/plugin.test.mocks.ts | 22 +++++++++++ x-pack/plugins/cloud/public/plugin.test.ts | 38 ++++++++++++++++++ .../plugins/cloud/server/plugin.test.mocks.ts | 22 +++++++++++ x-pack/plugins/cloud/server/plugin.test.ts | 39 +++++++++++++++++-- 4 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 x-pack/plugins/cloud/public/plugin.test.mocks.ts create mode 100644 x-pack/plugins/cloud/server/plugin.test.mocks.ts diff --git a/x-pack/plugins/cloud/public/plugin.test.mocks.ts b/x-pack/plugins/cloud/public/plugin.test.mocks.ts new file mode 100644 index 00000000000000..f3015fa81bba43 --- /dev/null +++ b/x-pack/plugins/cloud/public/plugin.test.mocks.ts @@ -0,0 +1,22 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +export const parseDeploymentIdFromDeploymentUrlMock = jest.fn(); + +jest.doMock('../common/parse_deployment_id_from_deployment_url', () => { + return { + parseDeploymentIdFromDeploymentUrl: parseDeploymentIdFromDeploymentUrlMock, + }; +}); + +export const decodeCloudIdMock = jest.fn(); + +jest.doMock('../common/decode_cloud_id', () => { + return { + decodeCloudId: decodeCloudIdMock, + }; +}); diff --git a/x-pack/plugins/cloud/public/plugin.test.ts b/x-pack/plugins/cloud/public/plugin.test.ts index efb566761e22a5..18b2a2683d1e27 100644 --- a/x-pack/plugins/cloud/public/plugin.test.ts +++ b/x-pack/plugins/cloud/public/plugin.test.ts @@ -5,8 +5,10 @@ * 2.0. */ +import { decodeCloudIdMock, parseDeploymentIdFromDeploymentUrlMock } from './plugin.test.mocks'; import { coreMock } from '@kbn/core/public/mocks'; import { CloudPlugin } from './plugin'; +import type { DecodedCloudId } from '../common/decode_cloud_id'; const baseConfig = { base_url: 'https://cloud.elastic.co', @@ -16,6 +18,11 @@ const baseConfig = { }; describe('Cloud Plugin', () => { + beforeEach(() => { + parseDeploymentIdFromDeploymentUrlMock.mockReset().mockReturnValue('deployment-id'); + decodeCloudIdMock.mockReset().mockReturnValue({}); + }); + describe('#setup', () => { describe('interface', () => { const setupPlugin = () => { @@ -76,6 +83,37 @@ describe('Cloud Plugin', () => { const { setup } = setupPlugin(); expect(setup.registerCloudService).toBeDefined(); }); + + it('exposes deploymentId', () => { + parseDeploymentIdFromDeploymentUrlMock.mockReturnValue('some-deployment-id'); + const { setup } = setupPlugin(); + expect(setup.deploymentId).toBe('some-deployment-id'); + expect(parseDeploymentIdFromDeploymentUrlMock).toHaveBeenCalledTimes(2); // called when registering the analytic context too + expect(parseDeploymentIdFromDeploymentUrlMock).toHaveBeenCalledWith( + baseConfig.deployment_url + ); + }); + + it('exposes components decoded from the cloudId', () => { + const decodedId: DecodedCloudId = { + defaultPort: '9000', + host: 'host', + elasticsearchUrl: 'elasticsearch-url', + kibanaUrl: 'kibana-url', + }; + decodeCloudIdMock.mockReturnValue(decodedId); + const { setup } = setupPlugin(); + expect(setup).toEqual( + expect.objectContaining({ + cloudDefaultPort: '9000', + cloudHost: 'host', + elasticsearchUrl: 'elasticsearch-url', + kibanaUrl: 'kibana-url', + }) + ); + expect(decodeCloudIdMock).toHaveBeenCalledTimes(1); + expect(decodeCloudIdMock).toHaveBeenCalledWith('cloudId'); + }); }); }); diff --git a/x-pack/plugins/cloud/server/plugin.test.mocks.ts b/x-pack/plugins/cloud/server/plugin.test.mocks.ts new file mode 100644 index 00000000000000..f3015fa81bba43 --- /dev/null +++ b/x-pack/plugins/cloud/server/plugin.test.mocks.ts @@ -0,0 +1,22 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +export const parseDeploymentIdFromDeploymentUrlMock = jest.fn(); + +jest.doMock('../common/parse_deployment_id_from_deployment_url', () => { + return { + parseDeploymentIdFromDeploymentUrl: parseDeploymentIdFromDeploymentUrlMock, + }; +}); + +export const decodeCloudIdMock = jest.fn(); + +jest.doMock('../common/decode_cloud_id', () => { + return { + decodeCloudId: decodeCloudIdMock, + }; +}); diff --git a/x-pack/plugins/cloud/server/plugin.test.ts b/x-pack/plugins/cloud/server/plugin.test.ts index e3095e97e6a4cc..9f38cb7e2fb8ef 100644 --- a/x-pack/plugins/cloud/server/plugin.test.ts +++ b/x-pack/plugins/cloud/server/plugin.test.ts @@ -5,8 +5,10 @@ * 2.0. */ +import { decodeCloudIdMock, parseDeploymentIdFromDeploymentUrlMock } from './plugin.test.mocks'; import { coreMock } from '@kbn/core/server/mocks'; import { CloudPlugin } from './plugin'; +import type { DecodedCloudId } from '../common/decode_cloud_id'; const baseConfig = { base_url: 'https://cloud.elastic.co', @@ -16,6 +18,11 @@ const baseConfig = { }; describe('Cloud Plugin', () => { + beforeEach(() => { + parseDeploymentIdFromDeploymentUrlMock.mockReset().mockReturnValue('deployment-id'); + decodeCloudIdMock.mockReset().mockReturnValue({}); + }); + const setupPlugin = () => { const initContext = coreMock.createPluginInitializerContext({ ...baseConfig, @@ -48,14 +55,40 @@ describe('Cloud Plugin', () => { expect(setup.instanceSizeMb).toBeUndefined(); }); + it('exposes apm', () => { + const { setup } = setupPlugin(); + expect(setup.apm).toStrictEqual({ url: undefined, secretToken: undefined }); + }); + it('exposes deploymentId', () => { + parseDeploymentIdFromDeploymentUrlMock.mockReturnValue('some-deployment-id'); const { setup } = setupPlugin(); - expect(setup.deploymentId).toBe('abc123'); + expect(setup.deploymentId).toBe('some-deployment-id'); + expect(parseDeploymentIdFromDeploymentUrlMock).toHaveBeenCalledTimes(2); // called when registering the analytic context too + expect(parseDeploymentIdFromDeploymentUrlMock).toHaveBeenCalledWith( + baseConfig.deployment_url + ); }); - it('exposes apm', () => { + it('exposes components decoded from the cloudId', () => { + const decodedId: DecodedCloudId = { + defaultPort: '9000', + host: 'host', + elasticsearchUrl: 'elasticsearch-url', + kibanaUrl: 'kibana-url', + }; + decodeCloudIdMock.mockReturnValue(decodedId); const { setup } = setupPlugin(); - expect(setup.apm).toStrictEqual({ url: undefined, secretToken: undefined }); + expect(setup).toEqual( + expect.objectContaining({ + cloudDefaultPort: '9000', + cloudHost: 'host', + elasticsearchUrl: 'elasticsearch-url', + kibanaUrl: 'kibana-url', + }) + ); + expect(decodeCloudIdMock).toHaveBeenCalledTimes(1); + expect(decodeCloudIdMock).toHaveBeenCalledWith('cloudId'); }); }); }); From 2485859574fb24d2108475ccb6a4159ada957034 Mon Sep 17 00:00:00 2001 From: pgayvallet Date: Mon, 12 Jun 2023 10:41:19 +0200 Subject: [PATCH 10/14] remove from start contract given unused for now --- x-pack/plugins/cloud/public/plugin.tsx | 7 ------- 1 file changed, 7 deletions(-) diff --git a/x-pack/plugins/cloud/public/plugin.tsx b/x-pack/plugins/cloud/public/plugin.tsx index e976fa34159ab3..363a8a0cedd306 100644 --- a/x-pack/plugins/cloud/public/plugin.tsx +++ b/x-pack/plugins/cloud/public/plugin.tsx @@ -98,11 +98,6 @@ export class CloudPlugin implements Plugin { const { deploymentUrl, profileUrl, organizationUrl } = this.getCloudUrls(); - let decodedId: DecodedCloudId | undefined; - if (this.config.id) { - decodedId = decodeCloudId(this.config.id); - } - return { CloudContextProvider, isCloudEnabled: this.isCloudEnabled, @@ -110,8 +105,6 @@ export class CloudPlugin implements Plugin { deploymentUrl, profileUrl, organizationUrl, - elasticsearchUrl: decodedId?.elasticsearchUrl, - kibanaUrl: decodedId?.kibanaUrl, }; } From fdc5d6cf46484dd63f03b9e6224b2f183e2b99bd Mon Sep 17 00:00:00 2001 From: pgayvallet Date: Mon, 12 Jun 2023 12:48:33 +0200 Subject: [PATCH 11/14] fix snapshot --- .../analytics_collection_integrate.test.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugins/enterprise_search/public/applications/analytics/components/analytics_collection_view/analytics_collection_integrate/analytics_collection_integrate.test.tsx b/x-pack/plugins/enterprise_search/public/applications/analytics/components/analytics_collection_view/analytics_collection_integrate/analytics_collection_integrate.test.tsx index df2dbade8c8521..9bb23b677f743c 100644 --- a/x-pack/plugins/enterprise_search/public/applications/analytics/components/analytics_collection_view/analytics_collection_integrate/analytics_collection_integrate.test.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/analytics/components/analytics_collection_view/analytics_collection_integrate/analytics_collection_integrate.test.tsx @@ -55,7 +55,7 @@ describe('AnalyticsCollectionIntegrate', () => { .toMatchInlineSnapshot(` "