From 4a67aec6a70f68ab4112a15e2ccf165d47d4bf76 Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Mon, 7 Nov 2022 18:49:46 +0800 Subject: [PATCH 01/47] update test/server_integration/http/ssl_redirect/config.js Url to formatUrl Signed-off-by: Lin Wang --- test/server_integration/http/ssl_redirect/config.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/server_integration/http/ssl_redirect/config.js b/test/server_integration/http/ssl_redirect/config.js index 1bd6000fc62d..626734ed67ee 100644 --- a/test/server_integration/http/ssl_redirect/config.js +++ b/test/server_integration/http/ssl_redirect/config.js @@ -28,7 +28,7 @@ * under the License. */ -import Url from 'url'; +import { format as formatUrl } from 'url'; import { readFileSync } from 'fs'; import { CA_CERT_PATH, OSD_CERT_PATH, OSD_KEY_PATH } from '@osd/dev-utils'; @@ -46,7 +46,7 @@ export default async function ({ readConfigFile }) { ...httpConfig.get('services'), supertest: createOpenSearchDashboardsSupertestProvider({ certificateAuthorities, - opensearchDashboardsUrl: Url.format({ + opensearchDashboardsUrl: formatUrl({ ...httpConfig.get('servers.opensearchDashboards'), port: redirectPort, // test with non ssl protocol From c37c80ddc2a2c8a8a26ebcf2122280e6f9882541 Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Mon, 7 Nov 2022 19:04:16 +0800 Subject: [PATCH 02/47] update for examples/url_generators_examples/public/url_generator.ts Signed-off-by: Lin Wang --- .../public/url_generator.ts | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/examples/url_generators_examples/public/url_generator.ts b/examples/url_generators_examples/public/url_generator.ts index dba057eb6de4..b7ba8f16b558 100644 --- a/examples/url_generators_examples/public/url_generator.ts +++ b/examples/url_generators_examples/public/url_generator.ts @@ -28,7 +28,6 @@ * under the License. */ -import url from 'url'; import { UrlGeneratorState, UrlGeneratorsDefinition } from '../../../src/plugins/share/public'; /** @@ -53,16 +52,11 @@ export const createHelloPageLinkGenerator = ( createUrl: async (state) => { const startServices = await getStartServices(); const appBasePath = startServices.appBasePath; - const parsedUrl = url.parse(window.location.href); - return url.format({ - protocol: parsedUrl.protocol, - host: parsedUrl.host, - pathname: `${appBasePath}/hello`, - query: { - ...state, - }, - }); + const url = new URL(`${appBasePath}/hello`, window.location.origin); + url.search = new URLSearchParams((state as unknown) as Record).toString(); + + return url.toString(); }, }); From c2511a820499b88df4f0ed2a42beb4b92086d27a Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Mon, 7 Nov 2022 19:34:20 +0800 Subject: [PATCH 03/47] update packages/osd-dev-utils/src/osd_client/osd_client_requester.ts Signed-off-by: Lin Wang --- .../osd-dev-utils/src/osd_client/osd_client_requester.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/osd-dev-utils/src/osd_client/osd_client_requester.ts b/packages/osd-dev-utils/src/osd_client/osd_client_requester.ts index effa5da6dd7e..d8ec7c43ba08 100644 --- a/packages/osd-dev-utils/src/osd_client/osd_client_requester.ts +++ b/packages/osd-dev-utils/src/osd_client/osd_client_requester.ts @@ -28,7 +28,7 @@ * under the License. */ -import Url from 'url'; +import { URL } from 'url'; import Https from 'https'; import Axios, { AxiosResponse } from 'axios'; @@ -93,7 +93,7 @@ export class OsdClientRequester { constructor(private readonly log: ToolingLog, options: Options) { this.url = options.url; this.httpsAgent = - Url.parse(options.url).protocol === 'https:' + new URL(options.url).protocol === 'https:' ? new Https.Agent({ ca: options.certificateAuthorities, }) @@ -105,11 +105,11 @@ export class OsdClientRequester { } public resolveUrl(relativeUrl: string = '/') { - return Url.resolve(this.pickUrl(), relativeUrl); + return new URL(relativeUrl, this.pickUrl()).toString(); } async request(options: ReqOptions): Promise> { - const url = Url.resolve(this.pickUrl(), options.path); + const url = new URL(options.path, this.pickUrl()).toString(); const description = options.description || `${options.method} ${url}`; let attempt = 0; const maxAttempts = options.retries ?? DEFAULT_MAX_ATTEMPTS; From 2921fdae879c3ccec6c2ce32b7212a487d74d372 Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Mon, 7 Nov 2022 20:09:02 +0800 Subject: [PATCH 04/47] update src/cli_plugin/install/download.js Signed-off-by: Lin Wang --- src/cli_plugin/install/download.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/cli_plugin/install/download.js b/src/cli_plugin/install/download.js index d614b7ac230b..b058c3386224 100644 --- a/src/cli_plugin/install/download.js +++ b/src/cli_plugin/install/download.js @@ -28,8 +28,6 @@ * under the License. */ -import { parse } from 'url'; - import { UnsupportedProtocolError } from '../lib/errors'; import { downloadHttpFile } from './downloaders/http'; import { downloadLocalFile } from './downloaders/file'; @@ -56,14 +54,19 @@ export function _checkFilePathDeprecation(sourceUrl, logger) { } export function _downloadSingle(settings, logger, sourceUrl) { - const urlInfo = parse(sourceUrl); + let urlInfo; + try { + urlInfo = new URL(sourceUrl); + } catch (e) { + return Promise.reject(new UnsupportedProtocolError()); + } let downloadPromise; if (/^file/.test(urlInfo.protocol)) { _checkFilePathDeprecation(sourceUrl, logger); downloadPromise = downloadLocalFile( logger, - _getFilePath(urlInfo.path, sourceUrl), + _getFilePath(urlInfo.pathname, sourceUrl), settings.tempArchiveFile ); } else if (/^https?/.test(urlInfo.protocol)) { From 3247937573f23c5e32ade238ed918d2d25264c25 Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Mon, 7 Nov 2022 20:09:44 +0800 Subject: [PATCH 05/47] update packages/osd-test/src/failed_tests_reporter/github_api.ts Signed-off-by: Lin Wang --- .../osd-test/src/failed_tests_reporter/github_api.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/osd-test/src/failed_tests_reporter/github_api.ts b/packages/osd-test/src/failed_tests_reporter/github_api.ts index c384d56eb104..cca4b3a960bd 100644 --- a/packages/osd-test/src/failed_tests_reporter/github_api.ts +++ b/packages/osd-test/src/failed_tests_reporter/github_api.ts @@ -28,7 +28,7 @@ * under the License. */ -import Url from 'url'; +import { URL } from 'url'; import Axios, { AxiosRequestConfig, AxiosInstance } from 'axios'; import parseLinkHeader from 'parse-link-header'; @@ -98,7 +98,7 @@ export class GithubApi { nextRequest: { safeForDryRun: true, method: 'GET', - url: Url.resolve(BASE_URL, 'issues'), + url: new URL('issues', BASE_URL).toString(), params: { state: 'all', per_page: '100', @@ -158,7 +158,7 @@ export class GithubApi { await this.request( { method: 'PATCH', - url: Url.resolve(BASE_URL, `issues/${encodeURIComponent(issueNumber)}`), + url: new URL(`issues/${encodeURIComponent(issueNumber)}`, BASE_URL).toString(), data: { state: 'open', // Reopen issue if it was closed. body: newBody, @@ -172,7 +172,7 @@ export class GithubApi { await this.request( { method: 'POST', - url: Url.resolve(BASE_URL, `issues/${encodeURIComponent(issueNumber)}/comments`), + url: new URL(`issues/${encodeURIComponent(issueNumber)}/comments`, BASE_URL).toString(), data: { body: commentBody, }, @@ -185,7 +185,7 @@ export class GithubApi { const resp = await this.request( { method: 'POST', - url: Url.resolve(BASE_URL, 'issues'), + url: new URL('issues', BASE_URL).toString(), data: { title, body, From defe504aff2b707d6f32d474d7149cfd836928fa Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Mon, 7 Nov 2022 20:10:27 +0800 Subject: [PATCH 06/47] update packages/osd-test/src/functional_test_runner/lib/docker_servers/docker_servers_service.ts Signed-off-by: Lin Wang --- .../lib/docker_servers/docker_servers_service.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/packages/osd-test/src/functional_test_runner/lib/docker_servers/docker_servers_service.ts b/packages/osd-test/src/functional_test_runner/lib/docker_servers/docker_servers_service.ts index 954740ca24ff..94c5b073ece5 100644 --- a/packages/osd-test/src/functional_test_runner/lib/docker_servers/docker_servers_service.ts +++ b/packages/osd-test/src/functional_test_runner/lib/docker_servers/docker_servers_service.ts @@ -28,7 +28,7 @@ * under the License. */ -import Url from 'url'; +import { URL } from 'url'; import execa from 'execa'; import * as Rx from 'rxjs'; import { filter, take, map } from 'rxjs/operators'; @@ -54,11 +54,7 @@ export class DockerServersService { this.servers = Object.entries(configs).map(([name, config]) => ({ ...config, name, - url: Url.format({ - protocol: 'http:', - hostname: 'localhost', - port: config.port, - }), + url: new URL(`http://localhost:${config.port}`).toString(), })); this.lifecycle.beforeTests.add(async () => { From 12cc53f94bf9e8a60580e35a1c8ae2949f2fe5c1 Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Mon, 7 Nov 2022 20:11:07 +0800 Subject: [PATCH 07/47] update src/cli/serve/serve.js Signed-off-by: Lin Wang --- src/cli/serve/serve.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cli/serve/serve.js b/src/cli/serve/serve.js index 34c03c2547bc..8d6f080e8a7f 100644 --- a/src/cli/serve/serve.js +++ b/src/cli/serve/serve.js @@ -32,7 +32,7 @@ import { set as lodashSet } from '@elastic/safer-lodash-set'; import _ from 'lodash'; import { statSync } from 'fs'; import { resolve } from 'path'; -import url from 'url'; +import { URL } from 'url'; import { getConfigPath } from '@osd/utils'; import { IS_OPENSEARCH_DASHBOARDS_DISTRIBUTABLE } from '../../legacy/utils'; @@ -110,7 +110,7 @@ function applyConfigOverrides(rawConfig, opts, extraCliOptions) { const opensearchHosts = ( (customOpenSearchHosts.length > 0 && customOpenSearchHosts) || ['https://localhost:9200'] ).map((hostUrl) => { - const parsedUrl = url.parse(hostUrl); + const parsedUrl = new URL(hostUrl); if (parsedUrl.hostname !== 'localhost') { throw new Error( `Hostname "${parsedUrl.hostname}" can't be used with --ssl. Must be "localhost" to work with certificates.` From 2218ede35bf8145db7f50499a7ca716504798068 Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Mon, 7 Nov 2022 20:25:04 +0800 Subject: [PATCH 08/47] update src/core/public/application/utils/parse_app_url.ts Signed-off-by: Lin Wang --- src/core/public/application/utils/parse_app_url.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/core/public/application/utils/parse_app_url.ts b/src/core/public/application/utils/parse_app_url.ts index 433e1a877bc4..fc19be75ec8a 100644 --- a/src/core/public/application/utils/parse_app_url.ts +++ b/src/core/public/application/utils/parse_app_url.ts @@ -29,7 +29,7 @@ */ import { getUrlOrigin } from '@osd/std'; -import { resolve } from 'url'; +import { URL } from 'url'; import { IBasePath } from '../../http'; import { App, ParsedAppUrl } from '../types'; @@ -57,7 +57,6 @@ export const parseAppUrl = ( if (!currentOrigin) { throw new Error('when manually provided, currentUrl must be valid url with an origin'); } - const currentPath = currentUrl.substring(currentOrigin.length); // remove the origin from the given url if (url.startsWith(currentOrigin)) { @@ -66,7 +65,7 @@ export const parseAppUrl = ( // if the path is relative (i.e `../../to/somewhere`), we convert it to absolute if (!url.startsWith('/')) { - url = resolve(currentPath, url); + url = new URL(url, currentUrl).toString().substring(currentOrigin.length); } // if using a basePath and the absolute path does not starts with it, it can't be a match From 3fc62a309a1039c74ac78c0d16c76e021d82eeaf Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Mon, 7 Nov 2022 20:28:45 +0800 Subject: [PATCH 09/47] update src/core/public/chrome/chrome_service.tsx Signed-off-by: Lin Wang --- src/core/public/chrome/chrome_service.tsx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/core/public/chrome/chrome_service.tsx b/src/core/public/chrome/chrome_service.tsx index da5cbda1c92a..a78232f4e336 100644 --- a/src/core/public/chrome/chrome_service.tsx +++ b/src/core/public/chrome/chrome_service.tsx @@ -33,7 +33,6 @@ import React from 'react'; import { FormattedMessage } from '@osd/i18n/react'; import { BehaviorSubject, combineLatest, merge, Observable, of, ReplaySubject } from 'rxjs'; import { flatMap, map, takeUntil } from 'rxjs/operators'; -import { parse } from 'url'; import { EuiLink } from '@elastic/eui'; import { mountReactNode } from '../utils/mount'; import { InternalApplicationStart } from '../application'; @@ -123,7 +122,7 @@ export class ChromeService { */ private initVisibility(application: StartDeps['application']) { // Start off the chrome service hidden if "embed" is in the hash query string. - const isEmbedded = 'embed' in parse(location.hash.slice(1), true).query; + const isEmbedded = new URL(location.hash.slice(1), location.origin).searchParams.has('embed'); this.isForceHidden$ = new BehaviorSubject(isEmbedded); const appHidden$ = merge( From d1cc130a1ef5b495694ae75ca126cbf8cb6094de Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Mon, 7 Nov 2022 20:37:26 +0800 Subject: [PATCH 10/47] update src/core/public/chrome/ui/header/header_logo.tsx Signed-off-by: Lin Wang --- src/core/public/chrome/ui/header/header_logo.tsx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/core/public/chrome/ui/header/header_logo.tsx b/src/core/public/chrome/ui/header/header_logo.tsx index abc32daabdf9..2ecca30ed574 100644 --- a/src/core/public/chrome/ui/header/header_logo.tsx +++ b/src/core/public/chrome/ui/header/header_logo.tsx @@ -33,7 +33,6 @@ import { i18n } from '@osd/i18n'; import React from 'react'; import useObservable from 'react-use/lib/useObservable'; import { Observable } from 'rxjs'; -import Url from 'url'; import { ChromeNavLink } from '../..'; import { ChromeBranding } from '../../chrome_service'; @@ -74,11 +73,12 @@ function onClick( } if (forceNavigation) { - const toParsed = Url.parse(anchor.href); - const fromParsed = Url.parse(document.location.href); + const toParsed = new URL(anchor.href); + const fromParsed = new URL(document.location.href); const sameProto = toParsed.protocol === fromParsed.protocol; const sameHost = toParsed.host === fromParsed.host; - const samePath = toParsed.path === fromParsed.path; + const samePath = + toParsed.pathname === fromParsed.pathname && toParsed.search === fromParsed.search; if (sameProto && sameHost && samePath) { if (toParsed.hash) { From 14b5497541b19b42ab15c7f0d546ff3b1fd1eec5 Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Mon, 7 Nov 2022 23:26:38 +0800 Subject: [PATCH 11/47] update src/core/server/http/base_path_proxy_server.ts Signed-off-by: Lin Wang --- .../server/http/base_path_proxy_server.ts | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/core/server/http/base_path_proxy_server.ts b/src/core/server/http/base_path_proxy_server.ts index 7df5a1e6d301..c8397aa596a0 100644 --- a/src/core/server/http/base_path_proxy_server.ts +++ b/src/core/server/http/base_path_proxy_server.ts @@ -28,7 +28,7 @@ * under the License. */ -import Url from 'url'; +import { URL } from 'url'; import { Agent as HttpsAgent, ServerOptions as TlsOptions } from 'https'; import apm from 'elastic-apm-node'; @@ -173,16 +173,18 @@ export class BasePathProxyServer { agent: this.httpsAgent, passThrough: true, xforward: true, - mapUri: async (request: Request) => ({ - uri: Url.format({ - hostname: request.server.info.host, - port: this.devConfig.basePathProxyTargetPort, - protocol: request.server.info.protocol, - pathname: `${this.httpConfig.basePath}/${request.params.osdPath}`, - query: request.query, - }), - headers: request.headers, - }), + mapUri: async (request: Request) => { + const uri = new URL( + `${this.httpConfig.basePath}/${request.params.osdPath}`, + `${request.server.info.protocol}://${request.server.info.host}:${this.devConfig.basePathProxyTargetPort}` + ); + uri.search = new URLSearchParams(request.query).toString(); + + return { + uri: uri.toString(), + headers: request.headers, + }; + }, }, }, method: '*', From a76fd601d09e6b3e718cffdb8dc358d2d4a4caaa Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Mon, 7 Nov 2022 23:30:22 +0800 Subject: [PATCH 12/47] update src/core/server/http/http_server.mocks.ts Signed-off-by: Lin Wang --- src/core/server/http/http_server.mocks.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/core/server/http/http_server.mocks.ts b/src/core/server/http/http_server.mocks.ts index 8e50e331200d..224e62907537 100644 --- a/src/core/server/http/http_server.mocks.ts +++ b/src/core/server/http/http_server.mocks.ts @@ -28,7 +28,7 @@ * under the License. */ -import { URL, format as formatUrl } from 'url'; +import { URL } from 'url'; import { Request } from '@hapi/hapi'; import { merge } from 'lodash'; import { Socket } from 'net'; @@ -140,10 +140,7 @@ type DeepPartialObject = { [P in keyof T]+?: DeepPartial }; function createRawRequestMock(customization: DeepPartial = {}) { const pathname = customization.url?.pathname || '/'; const path = `${pathname}${customization.url?.search || ''}`; - const url = new URL( - formatUrl(Object.assign({ pathname, path, href: path }, customization.url)), - 'http://localhost' - ); + const url = new URL(path, 'http://localhost'); return merge( {}, From 592301bfda7345a6662d6b7e353cbac2123d50ec Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Mon, 7 Nov 2022 23:34:11 +0800 Subject: [PATCH 13/47] update src/core/server/http/http_server.ts Signed-off-by: Lin Wang --- src/core/server/http/http_server.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/server/http/http_server.ts b/src/core/server/http/http_server.ts index dc3727662c77..62d4200452b1 100644 --- a/src/core/server/http/http_server.ts +++ b/src/core/server/http/http_server.ts @@ -30,7 +30,7 @@ import { Server } from '@hapi/hapi'; import HapiStaticFiles from '@hapi/inert'; -import url from 'url'; +import { URL } from 'url'; import uuid from 'uuid'; import { Logger, LoggerFactory } from '../logging'; @@ -294,7 +294,7 @@ export class HttpServer { this.server.ext('onRequest', (request, h) => { const { referrer } = request.info; if (referrer !== '') { - const { hostname } = url.parse(referrer); + const { hostname } = new URL(referrer); if (!hostname || !list.includes(hostname)) { request.info.acceptEncoding = ''; } From 5fabfd313e4e51e0febe1e70c491fd5db76ee030 Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Mon, 7 Nov 2022 23:37:33 +0800 Subject: [PATCH 14/47] update src/core/server/http/https_redirect_server.ts Signed-off-by: Lin Wang --- src/core/server/http/https_redirect_server.ts | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/core/server/http/https_redirect_server.ts b/src/core/server/http/https_redirect_server.ts index e0747466a389..109c1e0dd0fd 100644 --- a/src/core/server/http/https_redirect_server.ts +++ b/src/core/server/http/https_redirect_server.ts @@ -29,7 +29,7 @@ */ import { Request, ResponseToolkit, Server } from '@hapi/hapi'; -import { format as formatUrl } from 'url'; +import { URL } from 'url'; import { Logger } from '../logging'; import { HttpConfig } from './http_config'; @@ -62,17 +62,9 @@ export class HttpsRedirectServer { ); this.server.ext('onRequest', (request: Request, responseToolkit: ResponseToolkit) => { - return responseToolkit - .redirect( - formatUrl({ - hostname: config.host, - pathname: request.url.pathname, - port: config.port, - protocol: 'https', - search: request.url.search, - }) - ) - .takeover(); + const redirectUrl = new URL(request.url.pathname, `https://${config.host}:${config.port}`); + redirectUrl.search = request.url.search; + return responseToolkit.redirect(redirectUrl.toString()).takeover(); }); try { From 7c5e0141e17b86d65e257f23c95594eda7296853 Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Mon, 7 Nov 2022 23:59:10 +0800 Subject: [PATCH 15/47] update src/core/server/opensearch/legacy/opensearch_client_config.ts Signed-off-by: Lin Wang --- .../server/opensearch/legacy/opensearch_client_config.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/core/server/opensearch/legacy/opensearch_client_config.ts b/src/core/server/opensearch/legacy/opensearch_client_config.ts index 3aac7fcc48e8..5237d4e47161 100644 --- a/src/core/server/opensearch/legacy/opensearch_client_config.ts +++ b/src/core/server/opensearch/legacy/opensearch_client_config.ts @@ -32,7 +32,7 @@ import { ConfigOptions } from 'elasticsearch'; import { cloneDeep } from 'lodash'; import { Duration } from 'moment'; import { checkServerIdentity } from 'tls'; -import url from 'url'; +import { URL } from 'url'; import { pick } from '@osd/std'; import { Logger } from '../../logging'; import { OpenSearchConfig } from '../opensearch_config'; @@ -131,17 +131,18 @@ export function parseOpenSearchClientConfig( if (Array.isArray(config.hosts)) { const needsAuth = auth !== false && config.username && config.password; opensearchClientConfig.hosts = config.hosts.map((nodeUrl: string) => { - const uri = url.parse(nodeUrl); + const uri = new URL(nodeUrl); const httpsURI = uri.protocol === 'https:'; const httpURI = uri.protocol === 'http:'; + const query = uri.searchParams.toString(); const host: Record = { host: uri.hostname, port: uri.port || (httpsURI && '443') || (httpURI && '80'), protocol: uri.protocol, path: uri.pathname, - query: uri.query, + query: query === '' ? null : query, headers: { ...DEFAULT_HEADERS, ...config.customHeaders, From 1517b5a3e4a3dff08d78672929f71c33b074d2ec Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Tue, 8 Nov 2022 10:54:01 +0800 Subject: [PATCH 16/47] update src/plugins/console/server/lib/opensearch_proxy_config.ts Signed-off-by: Lin Wang --- src/plugins/console/server/lib/opensearch_proxy_config.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/plugins/console/server/lib/opensearch_proxy_config.ts b/src/plugins/console/server/lib/opensearch_proxy_config.ts index b2b108b20e90..9cec803ae0a8 100644 --- a/src/plugins/console/server/lib/opensearch_proxy_config.ts +++ b/src/plugins/console/server/lib/opensearch_proxy_config.ts @@ -31,12 +31,12 @@ import _ from 'lodash'; import http from 'http'; import https from 'https'; -import url from 'url'; +import { URL } from 'url'; import { OpenSearchConfigForProxy } from '../types'; const createAgent = (legacyConfig: OpenSearchConfigForProxy): http.Agent | https.Agent => { - const target = url.parse(_.head(legacyConfig.hosts) as any); + const target = new URL(_.head(legacyConfig.hosts)!); if (!/^https/.test(target.protocol || '')) return new http.Agent(); const agentOptions: https.AgentOptions = {}; From 7248b3f02e4e5e72bbf4c2a6068c15bc7dd7aa72 Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Tue, 8 Nov 2022 11:01:37 +0800 Subject: [PATCH 17/47] update src/plugins/console/server/lib/proxy_config.ts Signed-off-by: Lin Wang --- src/plugins/console/server/lib/proxy_config.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/plugins/console/server/lib/proxy_config.ts b/src/plugins/console/server/lib/proxy_config.ts index c762462f6de2..26ef3c0a258e 100644 --- a/src/plugins/console/server/lib/proxy_config.ts +++ b/src/plugins/console/server/lib/proxy_config.ts @@ -29,7 +29,7 @@ */ import { values } from 'lodash'; -import { format as formatUrl } from 'url'; +import { URL } from 'url'; import { Agent as HttpsAgent, AgentOptions } from 'https'; import { WildcardMatcher } from './wildcard_matcher'; @@ -62,13 +62,14 @@ export class ProxyConfig { const rawMatches = { ...config.match, }; - this.id = - formatUrl({ - protocol: rawMatches.protocol, - hostname: rawMatches.host, - port: rawMatches.port, - pathname: rawMatches.path, - }) || '*'; + try { + this.id = new URL( + rawMatches.path, + `${rawMatches.protocol}://${rawMatches.host}:${rawMatches.port}` + ).toString(); + } catch (e) { + this.id = '*'; + } this.matchers = { protocol: new WildcardMatcher(rawMatches.protocol), From fea59fe06acfe4f15696274bc3c931c406383445 Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Tue, 8 Nov 2022 11:03:52 +0800 Subject: [PATCH 18/47] update src/plugins/console/server/routes/api/console/proxy/create_handler.ts Signed-off-by: Lin Wang --- .../console/server/routes/api/console/proxy/create_handler.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/plugins/console/server/routes/api/console/proxy/create_handler.ts b/src/plugins/console/server/routes/api/console/proxy/create_handler.ts index 9f564d9c8b81..c5efc2a84be4 100644 --- a/src/plugins/console/server/routes/api/console/proxy/create_handler.ts +++ b/src/plugins/console/server/routes/api/console/proxy/create_handler.ts @@ -29,7 +29,7 @@ */ import { Agent, IncomingMessage } from 'http'; -import * as url from 'url'; +import { URL } from 'url'; import { pick, trimStart, trimEnd } from 'lodash'; import { OpenSearchDashboardsRequest, RequestHandler } from 'opensearch-dashboards/server'; @@ -52,7 +52,7 @@ import { RouteDependencies } from '../../../'; import { Body, Query } from './validation_config'; function toURL(base: string, path: string) { - const urlResult = new url.URL(`${trimEnd(base, '/')}/${trimStart(path, '/')}`); + const urlResult = new URL(`${trimEnd(base, '/')}/${trimStart(path, '/')}`); // Appending pretty here to have OpenSearch do the JSON formatting, as doing // in JS can lead to data loss (7.0 will get munged into 7, thus losing indication of // measurement precision) From 73a0cb4c0b8153ee23ce10bb52465f9dccebb331 Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Tue, 8 Nov 2022 11:21:15 +0800 Subject: [PATCH 19/47] update src/plugins/maps_legacy/public/map/service_settings.test.js Signed-off-by: Lin Wang --- .../public/map/service_settings.test.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/plugins/maps_legacy/public/map/service_settings.test.js b/src/plugins/maps_legacy/public/map/service_settings.test.js index 89d7cfeec6e7..3aa60690f7c4 100644 --- a/src/plugins/maps_legacy/public/map/service_settings.test.js +++ b/src/plugins/maps_legacy/public/map/service_settings.test.js @@ -34,7 +34,7 @@ jest.mock('../opensearch_dashboards_services', () => ({ }, })); -import url from 'url'; +import { URL } from 'url'; import EMS_FILES from '../__tests__/map/ems_mocks/sample_files.json'; import EMS_TILES from '../__tests__/map/ems_mocks/sample_tiles.json'; @@ -121,11 +121,11 @@ describe('service_settings (FKA tile_map test)', function () { 'OpenStreetMap contributors | OpenMapTiles | MapTiler | <iframe id=\'iframe\' style=\'position:fixed;height: 40%;width: 100%;top: 60%;left: 5%;right:5%;border: 0px;background:white;\' src=\'http://256.256.256.256\'></iframe>' ); - const urlObject = url.parse(attrs.url, true); + const urlObject = new URL(attrs.url); expect(urlObject.hostname).toEqual('tiles.foobar'); - expect(urlObject.query.my_app_name).toEqual('opensearchDashboards'); - expect(urlObject.query.opensearch_tile_service_tos).toEqual('agree'); - expect(typeof urlObject.query.my_app_version).toEqual('string'); + expect(urlObject.searchParams.get('my_app_name')).toEqual('opensearchDashboards'); + expect(urlObject.searchParams.get('opensearch_tile_service_tos')).toEqual('agree'); + expect(typeof urlObject.searchParams.get('my_app_version')).toEqual('string'); }); it('should get options', async function () { @@ -143,9 +143,9 @@ describe('service_settings (FKA tile_map test)', function () { let serviceSettings; async function assertQuery(expected) { const attrs = await serviceSettings.getAttributesForTMSLayer(tilemapServices[0]); - const urlObject = url.parse(attrs.url, true); + const urlObject = new URL(attrs.url); Object.keys(expected).forEach((key) => { - expect(urlObject.query[key]).toEqual(expected[key]); + expect(urlObject.searchParams.get(key)).toEqual(expected[key]); }); } @@ -317,9 +317,9 @@ describe('service_settings (FKA tile_map test)', function () { const assertions = fileLayers.map(async function (fileLayer) { expect(fileLayer.origin).toEqual(ORIGIN.EMS); const fileUrl = await serviceSettings.getUrlForRegionLayer(fileLayer); - const urlObject = url.parse(fileUrl, true); + const urlObject = new URL(fileUrl); Object.keys({ foo: 'bar', opensearch_tile_service_tos: 'agree' }).forEach((key) => { - expect(typeof urlObject.query[key]).toEqual('string'); + expect(typeof urlObject.searchParams.get(key)).toEqual('string'); }); }); From 0a293d1bd260a3a9a540066a1fc19db8d9a26e50 Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Tue, 8 Nov 2022 11:43:16 +0800 Subject: [PATCH 20/47] update src/plugins/share/public/lib/url_shortener.ts Signed-off-by: Lin Wang --- src/plugins/share/public/lib/url_shortener.ts | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/plugins/share/public/lib/url_shortener.ts b/src/plugins/share/public/lib/url_shortener.ts index 81fb7d7ff651..3a98afa7631e 100644 --- a/src/plugins/share/public/lib/url_shortener.ts +++ b/src/plugins/share/public/lib/url_shortener.ts @@ -28,7 +28,6 @@ * under the License. */ -import url from 'url'; import { HttpStart } from 'opensearch-dashboards/public'; import { CREATE_PATH, getGotoPath } from '../../common/short_url_routes'; @@ -36,20 +35,15 @@ export async function shortenUrl( absoluteUrl: string, { basePath, post }: { basePath: string; post: HttpStart['post'] } ) { - const parsedUrl = url.parse(absoluteUrl); - if (!parsedUrl || !parsedUrl.path) { + const parsedUrl = new URL(absoluteUrl); + if (!parsedUrl || !parsedUrl.pathname) { return; } - const path = parsedUrl.path.replace(basePath, ''); - const hash = parsedUrl.hash ? parsedUrl.hash : ''; - const relativeUrl = path + hash; + parsedUrl.pathname = parsedUrl.pathname.replace(basePath, ''); + const relativeUrl = parsedUrl.toString().substring(parsedUrl.origin.length); const body = JSON.stringify({ url: relativeUrl }); const resp = await post(CREATE_PATH, { body }); - return url.format({ - protocol: parsedUrl.protocol, - host: parsedUrl.host, - pathname: `${basePath}${getGotoPath(resp.urlId)}`, - }); + return new URL(`${basePath}${getGotoPath(resp.urlId)}`, parsedUrl.origin).toString(); } From 7792efa667a3690affb07c4b9903766c3c48c372 Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Tue, 8 Nov 2022 12:12:51 +0800 Subject: [PATCH 21/47] update test/plugin_functional/test_suites/application_links/redirect_app_links.ts Signed-off-by: Lin Wang --- .../test_suites/application_links/redirect_app_links.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/plugin_functional/test_suites/application_links/redirect_app_links.ts b/test/plugin_functional/test_suites/application_links/redirect_app_links.ts index 135cdb125ae5..4d19a90f08ad 100644 --- a/test/plugin_functional/test_suites/application_links/redirect_app_links.ts +++ b/test/plugin_functional/test_suites/application_links/redirect_app_links.ts @@ -28,7 +28,7 @@ * under the License. */ -import url from 'url'; +import { URL } from 'url'; import expect from '@osd/expect'; import { PluginFunctionalProviderContext } from '../../services'; import '../../plugins/core_app_status/public/types'; @@ -40,8 +40,8 @@ declare global { } const getPathWithHash = (absoluteUrl: string) => { - const parsed = url.parse(absoluteUrl); - return `${parsed.path}${parsed.hash ?? ''}`; + const parsed = new URL(absoluteUrl); + return parsed.toString().substring(parsed.origin.length); }; export default function ({ getService, getPageObjects }: PluginFunctionalProviderContext) { From 652a81ce85a5d472ca1ef51fee483466d42265c4 Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Tue, 8 Nov 2022 12:19:39 +0800 Subject: [PATCH 22/47] update getOpenSearchDashboardsUrl method in funtional tests Signed-off-by: Lin Wang --- .../core_plugins/application_leave_confirm.ts | 23 ++++++++++-------- .../core_plugins/application_status.ts | 24 +++++++++++-------- .../test_suites/core_plugins/applications.ts | 22 ++++++++++------- 3 files changed, 40 insertions(+), 29 deletions(-) diff --git a/test/plugin_functional/test_suites/core_plugins/application_leave_confirm.ts b/test/plugin_functional/test_suites/core_plugins/application_leave_confirm.ts index b0d04e77954c..8da02e7ba441 100644 --- a/test/plugin_functional/test_suites/core_plugins/application_leave_confirm.ts +++ b/test/plugin_functional/test_suites/core_plugins/application_leave_confirm.ts @@ -28,19 +28,22 @@ * under the License. */ -import url from 'url'; +import { URL } from 'url'; import expect from '@osd/expect'; import { PluginFunctionalProviderContext } from '../../services'; -const getOpenSearchDashboardsUrl = (pathname?: string, search?: string) => - url.format({ - protocol: 'http:', - hostname: process.env.TEST_OPENSEARCH_DASHBOARDS_HOST || 'localhost', - port: process.env.TEST_OPENSEARCH_DASHBOARDS_PORT || '5620', - pathname, - search, - }); - +const getOpenSearchDashboardsUrl = (pathname?: string, search?: string) => { + const url = new URL( + pathname ?? '', + `http://${process.env.TEST_OPENSEARCH_DASHBOARDS_HOST || 'localhost'}:${ + process.env.TEST_OPENSEARCH_DASHBOARDS_PORT || '5620' + }` + ); + if (search) { + url.search = search; + } + return url.toString(); +}; export default function ({ getService, getPageObjects }: PluginFunctionalProviderContext) { const PageObjects = getPageObjects(['common']); const browser = getService('browser'); diff --git a/test/plugin_functional/test_suites/core_plugins/application_status.ts b/test/plugin_functional/test_suites/core_plugins/application_status.ts index b2cb48a50485..ec62afbc2d7e 100644 --- a/test/plugin_functional/test_suites/core_plugins/application_status.ts +++ b/test/plugin_functional/test_suites/core_plugins/application_status.ts @@ -28,7 +28,7 @@ * under the License. */ -import Url from 'url'; +import { URL } from 'url'; import expect from '@osd/expect'; import { AppNavLinkStatus, @@ -38,14 +38,18 @@ import { import { PluginFunctionalProviderContext } from '../../services'; import '../../plugins/core_app_status/public/types'; -const getOpenSearchDashboardsUrl = (pathname?: string, search?: string) => - Url.format({ - protocol: 'http:', - hostname: process.env.TEST_OPENSEARCH_DASHBOARDS_HOST || 'localhost', - port: process.env.TEST_OPENSEARCH_DASHBOARDS_PORT || '5620', - pathname, - search, - }); +const getOpenSearchDashboardsUrl = (pathname?: string, search?: string) => { + const url = new URL( + pathname ?? '', + `http://${process.env.TEST_OPENSEARCH_DASHBOARDS_HOST || 'localhost'}:${ + process.env.TEST_OPENSEARCH_DASHBOARDS_PORT || '5620' + }` + ); + if (search) { + url.search = search; + } + return url.toString(); +}; export default function ({ getService, getPageObjects }: PluginFunctionalProviderContext) { const PageObjects = getPageObjects(['common']); @@ -126,7 +130,7 @@ export default function ({ getService, getPageObjects }: PluginFunctionalProvide await navigateToApp('app_status'); expect(await testSubjects.exists('appStatusApp')).to.eql(true); const currentUrl = await browser.getCurrentUrl(); - expect(Url.parse(currentUrl).pathname).to.eql('/app/app_status/arbitrary/path'); + expect(new URL(currentUrl).pathname).to.eql('/app/app_status/arbitrary/path'); }); it('can change the state of the currently mounted app', async () => { diff --git a/test/plugin_functional/test_suites/core_plugins/applications.ts b/test/plugin_functional/test_suites/core_plugins/applications.ts index 95c37f99748c..7ac1ccd42a19 100644 --- a/test/plugin_functional/test_suites/core_plugins/applications.ts +++ b/test/plugin_functional/test_suites/core_plugins/applications.ts @@ -28,7 +28,7 @@ * under the License. */ -import url from 'url'; +import { URL } from 'url'; import expect from '@osd/expect'; import { PluginFunctionalProviderContext } from '../../services'; @@ -50,14 +50,18 @@ export default function ({ getService, getPageObjects }: PluginFunctionalProvide return (await wrapper.getSize()).height; }; - const getOpenSearchDashboardsUrl = (pathname?: string, search?: string) => - url.format({ - protocol: 'http:', - hostname: process.env.TEST_OPENSEARCH_DASHBOARDS_HOST || 'localhost', - port: process.env.TEST_OPENSEARCH_DASHBOARDS_PORT || '5620', - pathname, - search, - }); + const getOpenSearchDashboardsUrl = (pathname?: string, search?: string) => { + const url = new URL( + pathname ?? '', + `http://${process.env.TEST_OPENSEARCH_DASHBOARDS_HOST || 'localhost'}:${ + process.env.TEST_OPENSEARCH_DASHBOARDS_PORT || '5620' + }` + ); + if (search) { + url.search = search; + } + return url.toString(); + }; /** Use retry logic to make URL assertions less flaky */ const waitForUrlToBe = (pathname?: string, search?: string) => { From 7b0a2fcbcf57829809989ebc6ae8e2b28cd6352d Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Tue, 8 Nov 2022 19:06:28 +0800 Subject: [PATCH 23/47] change config servers.opensearch and servers.opensearchDashboards to URL Signed-off-by: Lin Wang --- packages/osd-opensearch-archiver/src/cli.ts | 5 +- .../lib/config/config.ts | 7 +++ .../lib/config/schema.ts | 19 +------- .../functional_tests/lib/run_opensearch.js | 4 +- .../legacy_opensearch_test_cluster.js | 7 ++- .../opensearch_test_config.js | 34 ++++---------- packages/osd-test/src/osd/osd_test_config.ts | 47 +++++-------------- src/test_utils/get_url.js | 26 +++++----- src/test_utils/get_url.test.ts | 39 ++++----------- test/api_integration/services/supertest.ts | 5 +- test/common/config.js | 3 +- test/common/services/deployment.ts | 8 ++-- test/common/services/legacy_opensearch.ts | 4 +- test/common/services/opensearch.ts | 5 +- test/common/services/opensearch_archiver.ts | 3 +- .../opensearch_dashboards_server.ts | 4 +- test/common/services/security/test_user.ts | 11 ++--- test/functional/page_objects/common_page.ts | 4 +- test/functional/services/supertest.ts | 6 ++- .../http/platform/headers.ts | 8 +--- .../http/ssl_redirect/config.js | 13 ++--- test/server_integration/services/supertest.js | 18 +++---- 22 files changed, 97 insertions(+), 183 deletions(-) diff --git a/packages/osd-opensearch-archiver/src/cli.ts b/packages/osd-opensearch-archiver/src/cli.ts index 39dbbb264cd7..767fd6d2f444 100644 --- a/packages/osd-opensearch-archiver/src/cli.ts +++ b/packages/osd-opensearch-archiver/src/cli.ts @@ -35,7 +35,6 @@ *************************************************************/ import Path from 'path'; -import Url from 'url'; import readline from 'readline'; import { RunWithCommands, createFlagError } from '@osd/dev-utils'; @@ -72,7 +71,7 @@ export function runCli() { throw createFlagError('--opensearch-url must be a string'); } if (!opensearchUrl && config) { - opensearchUrl = Url.format(config.get('servers.opensearch')); + opensearchUrl = config.get('servers.opensearch').toString(); } if (!opensearchUrl) { throw createFlagError('--opensearch-url or --config must be defined'); @@ -83,7 +82,7 @@ export function runCli() { throw createFlagError('--opensearch-dashboards-url must be a string'); } if (!opensearchDashboardsUrl && config) { - opensearchDashboardsUrl = Url.format(config.get('servers.opensearchDashboards')); + opensearchDashboardsUrl = config.get('servers.opensearchDashboards').toString() as string; } if (!opensearchDashboardsUrl) { throw createFlagError('---url or --config must be defined'); diff --git a/packages/osd-test/src/functional_test_runner/lib/config/config.ts b/packages/osd-test/src/functional_test_runner/lib/config/config.ts index 3fd162bef29b..ac36960f333b 100644 --- a/packages/osd-test/src/functional_test_runner/lib/config/config.ts +++ b/packages/osd-test/src/functional_test_runner/lib/config/config.ts @@ -30,6 +30,7 @@ import { Schema } from 'joi'; import { cloneDeepWith, get, has, toPath } from 'lodash'; +import { URL } from 'url'; import { schema } from './schema'; @@ -126,6 +127,9 @@ export class Config { if (typeof v === 'function') { return v; } + if (v instanceof URL) { + return new URL(v.toString()); + } }); } @@ -134,6 +138,9 @@ export class Config { if (typeof v === 'function') { return v; } + if (v instanceof URL) { + return new URL(v.toString()); + } }); } } diff --git a/packages/osd-test/src/functional_test_runner/lib/config/schema.ts b/packages/osd-test/src/functional_test_runner/lib/config/schema.ts index 447db1e97874..dc6e0c1d1590 100644 --- a/packages/osd-test/src/functional_test_runner/lib/config/schema.ts +++ b/packages/osd-test/src/functional_test_runner/lib/config/schema.ts @@ -38,21 +38,6 @@ const ID_PATTERN = /^[a-zA-Z0-9_]+$/; const INSPECTING = process.execArgv.includes('--inspect') || process.execArgv.includes('--inspect-brk'); -const urlPartsSchema = () => - Joi.object() - .keys({ - protocol: Joi.string().valid('http', 'https').default('http'), - hostname: Joi.string().hostname().default('localhost'), - port: Joi.number(), - auth: Joi.string().regex(/^[^:]+:.+$/, 'username and password separated by a colon'), - username: Joi.string(), - password: Joi.string(), - pathname: Joi.string().regex(/^\//, 'start with a /'), - hash: Joi.string().regex(/^\//, 'start with a /'), - certificateAuthorities: Joi.array().items(Joi.binary()).optional(), - }) - .default(); - const appUrlPartsSchema = () => Joi.object() .keys({ @@ -184,8 +169,8 @@ export const schema = Joi.object() servers: Joi.object() .keys({ - opensearchDashboards: urlPartsSchema(), - opensearch: urlPartsSchema(), + opensearchDashboards: Joi.object().type(URL), + opensearch: Joi.object().type(URL), }) .default(), diff --git a/packages/osd-test/src/functional_tests/lib/run_opensearch.js b/packages/osd-test/src/functional_tests/lib/run_opensearch.js index 925cebcf55bf..d0e6ff7e9f90 100644 --- a/packages/osd-test/src/functional_tests/lib/run_opensearch.js +++ b/packages/osd-test/src/functional_tests/lib/run_opensearch.js @@ -41,8 +41,8 @@ export async function runOpenSearch({ config, options }) { const isSecurityEnabled = opensearchArgs.includes('xpack.security.enabled=true'); const cluster = createLegacyOpenSearchTestCluster({ - port: config.get('servers.opensearch.port'), - password: isSecurityEnabled ? 'changethispassword' : config.get('servers.opensearch.password'), + port: config.get('servers.opensearch').port, + password: isSecurityEnabled ? 'changethispassword' : config.get('servers.opensearch').password, license, log, basePath: resolve(OPENSEARCH_DASHBOARDS_ROOT, '.opensearch'), diff --git a/packages/osd-test/src/legacy_opensearch/legacy_opensearch_test_cluster.js b/packages/osd-test/src/legacy_opensearch/legacy_opensearch_test_cluster.js index 72506099dfe4..85930f0d892e 100644 --- a/packages/osd-test/src/legacy_opensearch/legacy_opensearch_test_cluster.js +++ b/packages/osd-test/src/legacy_opensearch/legacy_opensearch_test_cluster.js @@ -29,7 +29,6 @@ */ import { resolve } from 'path'; -import { format } from 'url'; import { get, toPath } from 'lodash'; import { Cluster } from '@osd/opensearch'; import { CI_PARALLEL_PROCESS_PREFIX } from '../ci_parallel_process_prefix'; @@ -135,10 +134,10 @@ export function createLegacyOpenSearchTestCluster(options = {}) { } getUrl() { - const parts = opensearchTestConfig.getUrlParts(); - parts.port = port; + const url = opensearchTestConfig.getUrlParts(); + url.port = port; - return format(parts); + return url.toString(); } })(); } diff --git a/packages/osd-test/src/legacy_opensearch/opensearch_test_config.js b/packages/osd-test/src/legacy_opensearch/opensearch_test_config.js index 7befa4226afd..bf985f083f6a 100644 --- a/packages/osd-test/src/legacy_opensearch/opensearch_test_config.js +++ b/packages/osd-test/src/legacy_opensearch/opensearch_test_config.js @@ -28,7 +28,7 @@ * under the License. */ -import url, { format as formatUrl } from 'url'; +import { URL } from 'url'; import pkg from '../../../../package.json'; import { adminTestUser } from '../osd'; @@ -42,7 +42,7 @@ export const opensearchTestConfig = new (class OpenSearchTestConfig { } getUrl() { - return formatUrl(this.getUrlParts()); + return this.getUrlParts().toString(); } getBuildFrom() { @@ -56,30 +56,16 @@ export const opensearchTestConfig = new (class OpenSearchTestConfig { getUrlParts() { // Allow setting one complete TEST_OPENSEARCH_URL for opensearch like https://opensearch:changeme@example.com:9200 if (process.env.TEST_OPENSEARCH_URL) { - const testOpenSearchUrl = url.parse(process.env.TEST_OPENSEARCH_URL); - return { - // have to remove the ":" off protocol - protocol: testOpenSearchUrl.protocol.slice(0, -1), - hostname: testOpenSearchUrl.hostname, - port: parseInt(testOpenSearchUrl.port, 10), - username: testOpenSearchUrl.auth.split(':')[0], - password: testOpenSearchUrl.auth.split(':')[1], - auth: testOpenSearchUrl.auth, - }; + return new URL(process.env.TEST_OPENSEARCH_URL); } - const username = process.env.TEST_OPENSEARCH_USERNAME || adminTestUser.username; const password = process.env.TEST_OPENSEARCH_PASSWORD || adminTestUser.password; - - return { - // Allow setting any individual component(s) of the URL, - // or use default values (username and password from ../osd/users.js) - protocol: process.env.TEST_OPENSEARCH_PROTOCOL || 'http', - hostname: process.env.TEST_OPENSEARCH_HOSTNAME || 'localhost', - port: parseInt(process.env.TEST_OPENSEARCH_PORT, 10) || 9220, - auth: `${username}:${password}`, - username: username, - password: password, - }; + const protocol = process.env.TEST_OPENSEARCH_PROTOCOL || 'http'; + const hostname = process.env.TEST_OPENSEARCH_HOSTNAME || 'localhost'; + const port = parseInt(process.env.TEST_OPENSEARCH_PORT, 10) || 9220; + const url = new URL(`${protocol}://${hostname}:${port}`); + url.username = username; + url.password = password; + return url; } })(); diff --git a/packages/osd-test/src/osd/osd_test_config.ts b/packages/osd-test/src/osd/osd_test_config.ts index 6c3ef573511b..4e9396f964e4 100644 --- a/packages/osd-test/src/osd/osd_test_config.ts +++ b/packages/osd-test/src/osd/osd_test_config.ts @@ -28,52 +28,31 @@ * under the License. */ -import url from 'url'; +import { URL } from 'url'; import { opensearchDashboardsTestUser } from './users'; -interface UrlParts { - protocol?: string; - hostname?: string; - port?: number; - auth?: string; - username?: string; - password?: string; -} - export const osdTestConfig = new (class OsdTestConfig { getPort() { - return this.getUrlParts().port; + return parseInt(this.getUrlParts().port, 10); } - getUrlParts(): UrlParts { + getUrlParts() { // allow setting one complete TEST_OPENSEARCH_DASHBOARDS_URL for opensearch like https://opensearch:changeme@example.com:9200 if (process.env.TEST_OPENSEARCH_DASHBOARDS_URL) { - const testOpenSearchDashboardsUrl = url.parse(process.env.TEST_OPENSEARCH_DASHBOARDS_URL); - return { - protocol: testOpenSearchDashboardsUrl.protocol?.slice(0, -1), - hostname: testOpenSearchDashboardsUrl.hostname ?? undefined, - port: testOpenSearchDashboardsUrl.port - ? parseInt(testOpenSearchDashboardsUrl.port, 10) - : undefined, - auth: testOpenSearchDashboardsUrl.auth ?? undefined, - username: testOpenSearchDashboardsUrl.auth?.split(':')[0], - password: testOpenSearchDashboardsUrl.auth?.split(':')[1], - }; + return new URL(process.env.TEST_OPENSEARCH_DASHBOARDS_URL); } - const username = process.env.TEST_OPENSEARCH_DASHBOARDS_USERNAME || opensearchDashboardsTestUser.username; const password = process.env.TEST_OPENSEARCH_DASHBOARDS_PASSWORD || opensearchDashboardsTestUser.password; - return { - protocol: process.env.TEST_OPENSEARCH_DASHBOARDS_PROTOCOL || 'http', - hostname: process.env.TEST_OPENSEARCH_DASHBOARDS_HOSTNAME || 'localhost', - port: process.env.TEST_OPENSEARCH_DASHBOARDS_PORT - ? parseInt(process.env.TEST_OPENSEARCH_DASHBOARDS_PORT, 10) - : 5620, - auth: `${username}:${password}`, - username, - password, - }; + const protocol = process.env.TEST_OPENSEARCH_DASHBOARDS_PROTOCOL || 'http'; + const hostname = process.env.TEST_OPENSEARCH_DASHBOARDS_HOSTNAME || 'localhost'; + const port = process.env.TEST_OPENSEARCH_DASHBOARDS_PORT + ? parseInt(process.env.TEST_OPENSEARCH_DASHBOARDS_PORT, 10) + : 5620; + const url = new URL(`${protocol}://${hostname}:${port}`); + url.username = username; + url.password = password; + return url; } })(); diff --git a/src/test_utils/get_url.js b/src/test_utils/get_url.js index 3b25d2bc626b..2787e467b7a9 100644 --- a/src/test_utils/get_url.js +++ b/src/test_utils/get_url.js @@ -28,9 +28,6 @@ * under the License. */ -import _ from 'lodash'; -import url from 'url'; - /** * Converts a config and a pathname to a url * @param {object} config A url config @@ -50,17 +47,22 @@ import url from 'url'; * @return {string} */ -export default function getUrl(config, app) { - return url.format(_.assign({}, config, app)); +export default function getUrl(url, app) { + if (app.pathname) { + url.pathname = app.pathname; + } + if (app.hash) { + url.hash = app.hash; + } + return url.toString(); } -getUrl.noAuth = function getUrlNoAuth(config, app) { - config = _.pickBy(config, function (val, param) { - return param !== 'auth'; - }); - return getUrl(config, app); +getUrl.noAuth = function getUrlNoAuth(url, app) { + url.username = ''; + url.password = ''; + return getUrl(url, app); }; -getUrl.baseUrl = function getBaseUrl(config) { - return url.format(_.pick(config, 'protocol', 'hostname', 'port')); +getUrl.baseUrl = function getBaseUrl(url) { + return url.origin; }; diff --git a/src/test_utils/get_url.test.ts b/src/test_utils/get_url.test.ts index c784c9c60b95..59cf2c3b22e1 100644 --- a/src/test_utils/get_url.test.ts +++ b/src/test_utils/get_url.test.ts @@ -33,46 +33,27 @@ import getUrl from './get_url'; describe('getUrl', function () { it('should convert to a url', function () { - const url = getUrl( - { - protocol: 'http', - hostname: 'localhost', - }, - { - pathname: 'foo', - } - ); + const url = getUrl(new URL('http://localhost'), { + pathname: 'foo', + }); expect(url).to.be('http://localhost/foo'); }); it('should convert to a url with port', function () { - const url = getUrl( - { - protocol: 'http', - hostname: 'localhost', - port: 9220, - }, - { - pathname: 'foo', - } - ); + const url = getUrl(new URL('http://localhost:9220'), { + pathname: 'foo', + }); expect(url).to.be('http://localhost:9220/foo'); }); it('should convert to a secure hashed url', function () { expect( - getUrl( - { - protocol: 'https', - hostname: 'localhost', - }, - { - pathname: 'foo', - hash: 'bar', - } - ) + getUrl(new URL('https://localhost'), { + pathname: 'foo', + hash: 'bar', + }) ).to.be('https://localhost/foo#bar'); }); }); diff --git a/test/api_integration/services/supertest.ts b/test/api_integration/services/supertest.ts index 369c25902b11..eec1ca3a9b40 100644 --- a/test/api_integration/services/supertest.ts +++ b/test/api_integration/services/supertest.ts @@ -29,18 +29,17 @@ */ import { FtrProviderContext } from 'test/functional/ftr_provider_context'; -import { format as formatUrl } from 'url'; import supertestAsPromised from 'supertest-as-promised'; export function OpenSearchDashboardsSupertestProvider({ getService }: FtrProviderContext) { const config = getService('config'); - const opensearchDashboardsServerUrl = formatUrl(config.get('servers.opensearchDashboards')); + const opensearchDashboardsServerUrl = config.get('servers.opensearchDashboards').toString(); return supertestAsPromised(opensearchDashboardsServerUrl); } export function OpenSearchSupertestProvider({ getService }: FtrProviderContext) { const config = getService('config'); - const elasticSearchServerUrl = formatUrl(config.get('servers.opensearch')); + const elasticSearchServerUrl = config.get('servers.opensearch').toString(); return supertestAsPromised(elasticSearchServerUrl); } diff --git a/test/common/config.js b/test/common/config.js index 5db5748087a3..f7328649b3c9 100644 --- a/test/common/config.js +++ b/test/common/config.js @@ -29,7 +29,6 @@ */ //import path from 'path'; -import { format as formatUrl } from 'url'; import { opensearchTestConfig, osdTestConfig, opensearchDashboardsServerTestUser } from '@osd/test'; import { services } from './services'; @@ -55,7 +54,7 @@ export default function () { '--logging.json=false', `--server.port=${osdTestConfig.getPort()}`, '--status.allowAnonymous=true', - `--opensearch.hosts=${formatUrl(servers.opensearch)}`, + `--opensearch.hosts=${servers.opensearch.toString()}`, `--opensearch.username=${opensearchDashboardsServerTestUser.username}`, `--opensearch.password=${opensearchDashboardsServerTestUser.password}`, `--home.disableWelcomeScreen=false`, diff --git a/test/common/services/deployment.ts b/test/common/services/deployment.ts index d28ed55a2bb3..0ef1eb64bc13 100644 --- a/test/common/services/deployment.ts +++ b/test/common/services/deployment.ts @@ -60,8 +60,8 @@ export function DeploymentProvider({ getService }: FtrProviderContext) { */ async isOss() { const baseUrl = this.getOpenSearchHostPort(); - const username = config.get('servers.opensearch.username'); - const password = config.get('servers.opensearch.password'); + const username = config.get('servers.opensearch').username; + const password = config.get('servers.opensearch').password; const response = await fetch(baseUrl + '/_xpack', { method: 'get', headers: { @@ -74,8 +74,8 @@ export function DeploymentProvider({ getService }: FtrProviderContext) { async isCloud(): Promise { const baseUrl = this.getHostPort(); - const username = config.get('servers.opensearchDashboards.username'); - const password = config.get('servers.opensearchDashboards.password'); + const username = config.get('servers.opensearchDashboards').username; + const password = config.get('servers.opensearchDashboards').password; const response = await fetch(baseUrl + '/api/stats?extended', { method: 'get', headers: { diff --git a/test/common/services/legacy_opensearch.ts b/test/common/services/legacy_opensearch.ts index 597a55a79f3b..3c2dac8ea4f1 100644 --- a/test/common/services/legacy_opensearch.ts +++ b/test/common/services/legacy_opensearch.ts @@ -28,8 +28,6 @@ * under the License. */ -import { format as formatUrl } from 'url'; - import * as legacyOpenSearch from 'elasticsearch'; import { DEFAULT_API_VERSION } from '../../../src/core/server/opensearch/opensearch_config'; @@ -42,7 +40,7 @@ export function LegacyOpenSearchProvider({ return new legacyOpenSearch.Client({ apiVersion: DEFAULT_API_VERSION, - host: formatUrl(config.get('servers.opensearch')), + host: config.get('servers.opensearch').toString(), requestTimeout: config.get('timeouts.opensearchRequestTimeout'), }); } diff --git a/test/common/services/opensearch.ts b/test/common/services/opensearch.ts index b162ee961f6d..340eaeff0c0b 100644 --- a/test/common/services/opensearch.ts +++ b/test/common/services/opensearch.ts @@ -28,7 +28,6 @@ * under the License. */ -import { format as formatUrl } from 'url'; import fs from 'fs'; import { Client } from '@opensearch-project/opensearch'; import { CA_CERT_PATH } from '@osd/dev-utils'; @@ -40,7 +39,7 @@ export function OpenSearchProvider({ getService }: FtrProviderContext) { if (process.env.TEST_CLOUD) { return new Client({ - nodes: [formatUrl(config.get('servers.opensearch'))], + nodes: [config.get('servers.opensearch').toString()], requestTimeout: config.get('timeouts.opensearchRequestTimeout'), }); } else { @@ -48,7 +47,7 @@ export function OpenSearchProvider({ getService }: FtrProviderContext) { ssl: { ca: fs.readFileSync(CA_CERT_PATH, 'utf-8'), }, - nodes: [formatUrl(config.get('servers.opensearch'))], + nodes: [config.get('servers.opensearch').toString()], requestTimeout: config.get('timeouts.opensearchRequestTimeout'), }); } diff --git a/test/common/services/opensearch_archiver.ts b/test/common/services/opensearch_archiver.ts index eb36c9e5a1d5..bb6ab3225c1b 100644 --- a/test/common/services/opensearch_archiver.ts +++ b/test/common/services/opensearch_archiver.ts @@ -28,7 +28,6 @@ * under the License. */ -import { format as formatUrl } from 'url'; import { OpenSearchArchiver } from '@osd/opensearch-archiver'; import { FtrProviderContext } from '../ftr_provider_context'; @@ -55,7 +54,7 @@ export function OpenSearchArchiverProvider({ client, dataDir, log, - opensearchDashboardsUrl: formatUrl(config.get('servers.opensearchDashboards')), + opensearchDashboardsUrl: config.get('servers.opensearchDashboards').toString(), }); if (hasService('opensearchDashboardsServer')) { diff --git a/test/common/services/opensearch_dashboards_server/opensearch_dashboards_server.ts b/test/common/services/opensearch_dashboards_server/opensearch_dashboards_server.ts index c989bbff9caa..b76bf25c852a 100644 --- a/test/common/services/opensearch_dashboards_server/opensearch_dashboards_server.ts +++ b/test/common/services/opensearch_dashboards_server/opensearch_dashboards_server.ts @@ -28,7 +28,6 @@ * under the License. */ -import Url from 'url'; import { OsdClient } from '@osd/dev-utils'; import { FtrProviderContext } from '../../ftr_provider_context'; @@ -37,12 +36,11 @@ export function OpenSearchDashboardsServerProvider({ getService }: FtrProviderCo const log = getService('log'); const config = getService('config'); const lifecycle = getService('lifecycle'); - const url = Url.format(config.get('servers.opensearchDashboards')); + const url = config.get('servers.opensearchDashboards').toString(); const defaults = config.get('uiSettings.defaults'); const osd = new OsdClient({ log, url, - certificateAuthorities: config.get('servers.opensearchDashboards.certificateAuthorities'), uiSettingDefaults: defaults, }); diff --git a/test/common/services/security/test_user.ts b/test/common/services/security/test_user.ts index 8cb7b14af24e..129177b9d4f2 100644 --- a/test/common/services/security/test_user.ts +++ b/test/common/services/security/test_user.ts @@ -28,7 +28,6 @@ * under the License. */ -import { format as formatUrl } from 'url'; import supertestAsPromised from 'supertest-as-promised'; import { Role } from './role'; @@ -115,11 +114,9 @@ export async function createTestUserService( export function TestUserSupertestProvider({ getService }: FtrProviderContext) { const config = getService('config'); const opensearchDashboardsServerConfig = config.get('servers.opensearchDashboards'); + const url = new URL(opensearchDashboardsServerConfig); + url.username = TEST_USER_NAME; + url.password = TEST_USER_PASSWORD; - return supertestAsPromised( - formatUrl({ - ...opensearchDashboardsServerConfig, - auth: `${TEST_USER_NAME}:${TEST_USER_PASSWORD}`, - }) - ); + return supertestAsPromised(url.toString()); } diff --git a/test/functional/page_objects/common_page.ts b/test/functional/page_objects/common_page.ts index 1a303dab14dc..a580a4f40d50 100644 --- a/test/functional/page_objects/common_page.ts +++ b/test/functional/page_objects/common_page.ts @@ -79,8 +79,8 @@ export function CommonPageProvider({ getService, getPageObjects }: FtrProviderCo log.debug('Found login page'); if (config.get('security.disableTestUser')) { await PageObjects.login.login( - config.get('servers.opensearchDashboards.username'), - config.get('servers.opensearchDashboards.password') + config.get('servers.opensearchDashboards').username, + config.get('servers.opensearchDashboards').password ); } else { await PageObjects.login.login('test_user', 'changeme'); diff --git a/test/functional/services/supertest.ts b/test/functional/services/supertest.ts index cc33c618be52..0c4aebbef4d9 100644 --- a/test/functional/services/supertest.ts +++ b/test/functional/services/supertest.ts @@ -29,12 +29,14 @@ */ import { FtrProviderContext } from 'test/functional/ftr_provider_context'; -import { format as formatUrl } from 'url'; import supertestAsPromised from 'supertest-as-promised'; export function OpenSearchDashboardsSupertestProvider({ getService }: FtrProviderContext) { const config = getService('config'); - const opensearchDashboardsServerUrl = formatUrl(config.get('servers.opensearchDashboards')); + const opensearchDashboardsServerUrl = config + .get('servers.opensearchDashboards') + .toString() + .slice(0, -1); return supertestAsPromised(opensearchDashboardsServerUrl); } diff --git a/test/server_integration/http/platform/headers.ts b/test/server_integration/http/platform/headers.ts index dadabe1e6058..0b460e4572d0 100644 --- a/test/server_integration/http/platform/headers.ts +++ b/test/server_integration/http/platform/headers.ts @@ -29,12 +29,8 @@ */ import Http from 'http'; -import Url from 'url'; import { FtrProviderContext } from '../../services/types'; -// @ts-ignore -import getUrl from '../../../../src/test_utils/get_url'; - const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); const oneSec = 1_000; @@ -47,9 +43,7 @@ export default function ({ getService }: FtrProviderContext) { const agent = new Http.Agent({ keepAlive: true, }); - const { protocol, hostname, port } = Url.parse( - getUrl.baseUrl(config.get('servers.opensearchDashboards')) - ); + const { protocol, hostname, port } = config.get('servers.opensearchDashboards'); function performRequest() { return new Promise((resolve, reject) => { diff --git a/test/server_integration/http/ssl_redirect/config.js b/test/server_integration/http/ssl_redirect/config.js index 626734ed67ee..1e6c24ca1811 100644 --- a/test/server_integration/http/ssl_redirect/config.js +++ b/test/server_integration/http/ssl_redirect/config.js @@ -28,7 +28,6 @@ * under the License. */ -import { format as formatUrl } from 'url'; import { readFileSync } from 'fs'; import { CA_CERT_PATH, OSD_CERT_PATH, OSD_KEY_PATH } from '@osd/dev-utils'; @@ -38,7 +37,10 @@ export default async function ({ readConfigFile }) { const httpConfig = await readConfigFile(require.resolve('../../config')); const certificateAuthorities = [readFileSync(CA_CERT_PATH)]; - const redirectPort = httpConfig.get('servers.opensearchDashboards.port') + 1234; + const opensearchDashboardsUrl = new URL(httpConfig.get('servers.opensearchDashboards')); + const redirectPort = opensearchDashboardsUrl.port + 1234; + opensearchDashboardsUrl.port = redirectPort; + opensearchDashboardsUrl.protocol = 'http'; return { testFiles: [require.resolve('./')], @@ -46,12 +48,7 @@ export default async function ({ readConfigFile }) { ...httpConfig.get('services'), supertest: createOpenSearchDashboardsSupertestProvider({ certificateAuthorities, - opensearchDashboardsUrl: formatUrl({ - ...httpConfig.get('servers.opensearchDashboards'), - port: redirectPort, - // test with non ssl protocol - protocol: 'http', - }), + opensearchDashboardsUrl: opensearchDashboardsUrl.toString(), }), }, servers: { diff --git a/test/server_integration/services/supertest.js b/test/server_integration/services/supertest.js index 0667f1fb1a82..0fc453103f44 100644 --- a/test/server_integration/services/supertest.js +++ b/test/server_integration/services/supertest.js @@ -28,8 +28,6 @@ * under the License. */ -import { format as formatUrl } from 'url'; - import supertestAsPromised from 'supertest-as-promised'; export function createOpenSearchDashboardsSupertestProvider({ @@ -39,7 +37,7 @@ export function createOpenSearchDashboardsSupertestProvider({ return function ({ getService }) { const config = getService('config'); opensearchDashboardsUrl = - opensearchDashboardsUrl ?? formatUrl(config.get('servers.opensearchDashboards')); + opensearchDashboardsUrl ?? config.get('servers.opensearchDashboards').toString(); return certificateAuthorities ? supertestAsPromised.agent(opensearchDashboardsUrl, { ca: certificateAuthorities }) @@ -49,18 +47,14 @@ export function createOpenSearchDashboardsSupertestProvider({ export function OpenSearchDashboardsSupertestWithoutAuthProvider({ getService }) { const config = getService('config'); - const opensearchDashboardsServerConfig = config.get('servers.opensearchDashboards'); - - return supertestAsPromised( - formatUrl({ - ...opensearchDashboardsServerConfig, - auth: false, - }) - ); + const opensearchDashboardsServerUrl = config.get('servers.opensearchDashboards'); + opensearchDashboardsServerUrl.username = ''; + opensearchDashboardsServerUrl.password = ''; + return supertestAsPromised(opensearchDashboardsServerUrl.toString()); } export function OpenSearchSupertestProvider({ getService }) { const config = getService('config'); - const elasticSearchServerUrl = formatUrl(config.get('servers.opensearch')); + const elasticSearchServerUrl = config.get('servers.opensearch').toString(); return supertestAsPromised(elasticSearchServerUrl); } From 90e29a14cb0b06b1e28968c685c83bdb2cb2a566 Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Thu, 10 Nov 2022 23:04:29 +0800 Subject: [PATCH 24/47] update src/plugins/share/public/components/url_panel_content.tsx Signed-off-by: Lin Wang --- .../components/url_panel_content.test.tsx | 4 +-- .../public/components/url_panel_content.tsx | 31 ++++++------------- 2 files changed, 12 insertions(+), 23 deletions(-) diff --git a/src/plugins/share/public/components/url_panel_content.test.tsx b/src/plugins/share/public/components/url_panel_content.test.tsx index 32b59cdf2b5f..902425d82650 100644 --- a/src/plugins/share/public/components/url_panel_content.test.tsx +++ b/src/plugins/share/public/components/url_panel_content.test.tsx @@ -77,7 +77,7 @@ describe('share url panel content', () => { component.find(EuiRadioGroup).prop('onChange')!(ExportUrlAsType.EXPORT_URL_AS_SAVED_OBJECT); }); expect(component.find(EuiCopy).prop('textToCopy')).toEqual( - 'http://localhost:5601/app/myapp#/?_g=()' + 'http://localhost:5601/app/myapp#/?_g=%28%29' ); }); @@ -168,7 +168,7 @@ describe('share url panel content', () => { component.find(EuiRadioGroup).prop('onChange')!(ExportUrlAsType.EXPORT_URL_AS_SAVED_OBJECT); }); expect(component.find(EuiCopy).prop('textToCopy')).toEqual( - asIframe('http://localhost:5601/app/myapp#/?embed=true&_g=()') + asIframe('http://localhost:5601/app/myapp#/?embed=true&_g=%28%29') ); }); diff --git a/src/plugins/share/public/components/url_panel_content.tsx b/src/plugins/share/public/components/url_panel_content.tsx index 9e97def0e41f..6f042f4d77f4 100644 --- a/src/plugins/share/public/components/url_panel_content.tsx +++ b/src/plugins/share/public/components/url_panel_content.tsx @@ -45,8 +45,6 @@ import { EuiSwitchEvent, } from '@elastic/eui'; -import { format as formatUrl, parse as parseUrl } from 'url'; - import { FormattedMessage, I18nProvider } from '@osd/i18n/react'; import { HttpStart } from 'opensearch-dashboards/public'; import { i18n } from '@osd/i18n'; @@ -185,30 +183,21 @@ export class UrlPanelContent extends Component { const url = this.getSnapshotUrl(); - const parsedUrl = parseUrl(url); + const parsedUrl = new URL(url); if (!parsedUrl || !parsedUrl.hash) { return; } // Get the application route, after the hash, and remove the #. - const parsedAppUrl = parseUrl(parsedUrl.hash.slice(1), true); - - const formattedUrl = formatUrl({ - protocol: parsedUrl.protocol, - auth: parsedUrl.auth, - host: parsedUrl.host, - pathname: parsedUrl.pathname, - hash: formatUrl({ - pathname: parsedAppUrl.pathname, - query: { - // Add global state to the URL so that the iframe doesn't just show the time range - // default. - _g: parsedAppUrl.query._g, - }, - }), - }); - - return this.updateUrlParams(formattedUrl); + const parsedAppUrl = new URL(parsedUrl.hash.slice(1), window.location.origin); + const formattedAppUrl = new URL(parsedAppUrl.pathname, parsedAppUrl.origin); + const formattedUrl = new URL(parsedUrl.pathname, parsedUrl.origin); + formattedAppUrl.search = new URLSearchParams({ + _g: parsedAppUrl.searchParams.get('_g') ?? '', + }).toString(); + formattedUrl.hash = formattedAppUrl.toString().substring(formattedAppUrl.origin.length); + + return this.updateUrlParams(formattedUrl.toString()); }; private getSnapshotUrl = () => { From 9e8818c85ae699f42910a6689254ee6c77bf4ad9 Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Tue, 22 Nov 2022 18:42:03 +0800 Subject: [PATCH 25/47] update expected url due to parentheses and single quotation mark encoded Signed-off-by: Lin Wang --- test/functional/apps/discover/_shared_links.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/functional/apps/discover/_shared_links.js b/test/functional/apps/discover/_shared_links.js index 96b7f705c73a..ecae25d8d4d3 100644 --- a/test/functional/apps/discover/_shared_links.js +++ b/test/functional/apps/discover/_shared_links.js @@ -123,9 +123,9 @@ export default function ({ getService, getPageObjects }) { baseUrl + '/app/discover#' + '/view/ab12e3c0-f231-11e6-9486-733b1ac9221a' + - '?_g=(filters%3A!()%2CrefreshInterval%3A(pause%3A!t%2Cvalue%3A0)' + - "%2Ctime%3A(from%3A'2015-09-19T06%3A31%3A44.000Z'%2C" + - "to%3A'2015-09-23T18%3A31%3A44.000Z'))"; + '?_g=%28filters%3A%21%28%29%2CrefreshInterval%3A%28pause%3A%21t%2Cvalue%3A0%29%' + + '2Ctime%3A%28from%3A%272015-09-19T06%3A31%3A44.000Z%27%2C' + + 'to%3A%272015-09-23T18%3A31%3A44.000Z%27%29%29'; await PageObjects.discover.loadSavedSearch('A Saved Search'); await PageObjects.share.clickShareTopNavButton(); await PageObjects.share.exportAsSavedObject(); From f683ba278e2205712f796b32e7b56670d5c70a64 Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Tue, 13 Dec 2022 11:37:40 +0800 Subject: [PATCH 26/47] remove unnecessary imported URL Signed-off-by: Lin Wang --- packages/osd-dev-utils/src/osd_client/osd_client_requester.ts | 1 - packages/osd-test/src/failed_tests_reporter/github_api.ts | 2 -- .../osd-test/src/functional_test_runner/lib/config/config.ts | 1 - .../lib/docker_servers/docker_servers_service.ts | 1 - .../osd-test/src/legacy_opensearch/opensearch_test_config.js | 1 - packages/osd-test/src/osd/osd_test_config.ts | 1 - src/cli/serve/serve.js | 1 - src/core/public/application/utils/parse_app_url.ts | 1 - src/core/server/http/base_path_proxy_server.ts | 1 - src/core/server/http/http_server.mocks.ts | 1 - src/core/server/http/http_server.ts | 1 - src/core/server/http/https_redirect_server.ts | 1 - src/core/server/opensearch/legacy/opensearch_client_config.ts | 1 - src/plugins/console/server/lib/opensearch_proxy_config.ts | 1 - src/plugins/console/server/lib/proxy_config.ts | 1 - .../console/server/routes/api/console/proxy/create_handler.ts | 1 - src/plugins/maps_legacy/public/map/service_settings.test.js | 2 -- .../test_suites/application_links/redirect_app_links.ts | 1 - .../test_suites/core_plugins/application_leave_confirm.ts | 1 - .../test_suites/core_plugins/application_status.ts | 1 - test/plugin_functional/test_suites/core_plugins/applications.ts | 1 - 21 files changed, 23 deletions(-) diff --git a/packages/osd-dev-utils/src/osd_client/osd_client_requester.ts b/packages/osd-dev-utils/src/osd_client/osd_client_requester.ts index d8ec7c43ba08..807ec64b0e5a 100644 --- a/packages/osd-dev-utils/src/osd_client/osd_client_requester.ts +++ b/packages/osd-dev-utils/src/osd_client/osd_client_requester.ts @@ -28,7 +28,6 @@ * under the License. */ -import { URL } from 'url'; import Https from 'https'; import Axios, { AxiosResponse } from 'axios'; diff --git a/packages/osd-test/src/failed_tests_reporter/github_api.ts b/packages/osd-test/src/failed_tests_reporter/github_api.ts index cca4b3a960bd..c9df43472585 100644 --- a/packages/osd-test/src/failed_tests_reporter/github_api.ts +++ b/packages/osd-test/src/failed_tests_reporter/github_api.ts @@ -28,8 +28,6 @@ * under the License. */ -import { URL } from 'url'; - import Axios, { AxiosRequestConfig, AxiosInstance } from 'axios'; import parseLinkHeader from 'parse-link-header'; import { ToolingLog, isAxiosResponseError, isAxiosRequestError } from '@osd/dev-utils'; diff --git a/packages/osd-test/src/functional_test_runner/lib/config/config.ts b/packages/osd-test/src/functional_test_runner/lib/config/config.ts index ac36960f333b..7396a92e6441 100644 --- a/packages/osd-test/src/functional_test_runner/lib/config/config.ts +++ b/packages/osd-test/src/functional_test_runner/lib/config/config.ts @@ -30,7 +30,6 @@ import { Schema } from 'joi'; import { cloneDeepWith, get, has, toPath } from 'lodash'; -import { URL } from 'url'; import { schema } from './schema'; diff --git a/packages/osd-test/src/functional_test_runner/lib/docker_servers/docker_servers_service.ts b/packages/osd-test/src/functional_test_runner/lib/docker_servers/docker_servers_service.ts index 94c5b073ece5..c34b5c659dd5 100644 --- a/packages/osd-test/src/functional_test_runner/lib/docker_servers/docker_servers_service.ts +++ b/packages/osd-test/src/functional_test_runner/lib/docker_servers/docker_servers_service.ts @@ -28,7 +28,6 @@ * under the License. */ -import { URL } from 'url'; import execa from 'execa'; import * as Rx from 'rxjs'; import { filter, take, map } from 'rxjs/operators'; diff --git a/packages/osd-test/src/legacy_opensearch/opensearch_test_config.js b/packages/osd-test/src/legacy_opensearch/opensearch_test_config.js index bf985f083f6a..e92906071847 100644 --- a/packages/osd-test/src/legacy_opensearch/opensearch_test_config.js +++ b/packages/osd-test/src/legacy_opensearch/opensearch_test_config.js @@ -28,7 +28,6 @@ * under the License. */ -import { URL } from 'url'; import pkg from '../../../../package.json'; import { adminTestUser } from '../osd'; diff --git a/packages/osd-test/src/osd/osd_test_config.ts b/packages/osd-test/src/osd/osd_test_config.ts index 4e9396f964e4..bba2f81bfe1d 100644 --- a/packages/osd-test/src/osd/osd_test_config.ts +++ b/packages/osd-test/src/osd/osd_test_config.ts @@ -28,7 +28,6 @@ * under the License. */ -import { URL } from 'url'; import { opensearchDashboardsTestUser } from './users'; export const osdTestConfig = new (class OsdTestConfig { diff --git a/src/cli/serve/serve.js b/src/cli/serve/serve.js index 8d6f080e8a7f..061e511f5917 100644 --- a/src/cli/serve/serve.js +++ b/src/cli/serve/serve.js @@ -32,7 +32,6 @@ import { set as lodashSet } from '@elastic/safer-lodash-set'; import _ from 'lodash'; import { statSync } from 'fs'; import { resolve } from 'path'; -import { URL } from 'url'; import { getConfigPath } from '@osd/utils'; import { IS_OPENSEARCH_DASHBOARDS_DISTRIBUTABLE } from '../../legacy/utils'; diff --git a/src/core/public/application/utils/parse_app_url.ts b/src/core/public/application/utils/parse_app_url.ts index fc19be75ec8a..1ad496613608 100644 --- a/src/core/public/application/utils/parse_app_url.ts +++ b/src/core/public/application/utils/parse_app_url.ts @@ -29,7 +29,6 @@ */ import { getUrlOrigin } from '@osd/std'; -import { URL } from 'url'; import { IBasePath } from '../../http'; import { App, ParsedAppUrl } from '../types'; diff --git a/src/core/server/http/base_path_proxy_server.ts b/src/core/server/http/base_path_proxy_server.ts index c8397aa596a0..f82e5a94d34a 100644 --- a/src/core/server/http/base_path_proxy_server.ts +++ b/src/core/server/http/base_path_proxy_server.ts @@ -28,7 +28,6 @@ * under the License. */ -import { URL } from 'url'; import { Agent as HttpsAgent, ServerOptions as TlsOptions } from 'https'; import apm from 'elastic-apm-node'; diff --git a/src/core/server/http/http_server.mocks.ts b/src/core/server/http/http_server.mocks.ts index 224e62907537..01fda75aab78 100644 --- a/src/core/server/http/http_server.mocks.ts +++ b/src/core/server/http/http_server.mocks.ts @@ -28,7 +28,6 @@ * under the License. */ -import { URL } from 'url'; import { Request } from '@hapi/hapi'; import { merge } from 'lodash'; import { Socket } from 'net'; diff --git a/src/core/server/http/http_server.ts b/src/core/server/http/http_server.ts index 62d4200452b1..af348fd5d961 100644 --- a/src/core/server/http/http_server.ts +++ b/src/core/server/http/http_server.ts @@ -30,7 +30,6 @@ import { Server } from '@hapi/hapi'; import HapiStaticFiles from '@hapi/inert'; -import { URL } from 'url'; import uuid from 'uuid'; import { Logger, LoggerFactory } from '../logging'; diff --git a/src/core/server/http/https_redirect_server.ts b/src/core/server/http/https_redirect_server.ts index 109c1e0dd0fd..ea7adc652583 100644 --- a/src/core/server/http/https_redirect_server.ts +++ b/src/core/server/http/https_redirect_server.ts @@ -29,7 +29,6 @@ */ import { Request, ResponseToolkit, Server } from '@hapi/hapi'; -import { URL } from 'url'; import { Logger } from '../logging'; import { HttpConfig } from './http_config'; diff --git a/src/core/server/opensearch/legacy/opensearch_client_config.ts b/src/core/server/opensearch/legacy/opensearch_client_config.ts index 5237d4e47161..12d333682b94 100644 --- a/src/core/server/opensearch/legacy/opensearch_client_config.ts +++ b/src/core/server/opensearch/legacy/opensearch_client_config.ts @@ -32,7 +32,6 @@ import { ConfigOptions } from 'elasticsearch'; import { cloneDeep } from 'lodash'; import { Duration } from 'moment'; import { checkServerIdentity } from 'tls'; -import { URL } from 'url'; import { pick } from '@osd/std'; import { Logger } from '../../logging'; import { OpenSearchConfig } from '../opensearch_config'; diff --git a/src/plugins/console/server/lib/opensearch_proxy_config.ts b/src/plugins/console/server/lib/opensearch_proxy_config.ts index 9cec803ae0a8..9cc2fd629a5e 100644 --- a/src/plugins/console/server/lib/opensearch_proxy_config.ts +++ b/src/plugins/console/server/lib/opensearch_proxy_config.ts @@ -31,7 +31,6 @@ import _ from 'lodash'; import http from 'http'; import https from 'https'; -import { URL } from 'url'; import { OpenSearchConfigForProxy } from '../types'; diff --git a/src/plugins/console/server/lib/proxy_config.ts b/src/plugins/console/server/lib/proxy_config.ts index 26ef3c0a258e..8ccb0c6de6a5 100644 --- a/src/plugins/console/server/lib/proxy_config.ts +++ b/src/plugins/console/server/lib/proxy_config.ts @@ -29,7 +29,6 @@ */ import { values } from 'lodash'; -import { URL } from 'url'; import { Agent as HttpsAgent, AgentOptions } from 'https'; import { WildcardMatcher } from './wildcard_matcher'; diff --git a/src/plugins/console/server/routes/api/console/proxy/create_handler.ts b/src/plugins/console/server/routes/api/console/proxy/create_handler.ts index c5efc2a84be4..45fc8ba79194 100644 --- a/src/plugins/console/server/routes/api/console/proxy/create_handler.ts +++ b/src/plugins/console/server/routes/api/console/proxy/create_handler.ts @@ -29,7 +29,6 @@ */ import { Agent, IncomingMessage } from 'http'; -import { URL } from 'url'; import { pick, trimStart, trimEnd } from 'lodash'; import { OpenSearchDashboardsRequest, RequestHandler } from 'opensearch-dashboards/server'; diff --git a/src/plugins/maps_legacy/public/map/service_settings.test.js b/src/plugins/maps_legacy/public/map/service_settings.test.js index 3aa60690f7c4..7bfd779e663e 100644 --- a/src/plugins/maps_legacy/public/map/service_settings.test.js +++ b/src/plugins/maps_legacy/public/map/service_settings.test.js @@ -34,8 +34,6 @@ jest.mock('../opensearch_dashboards_services', () => ({ }, })); -import { URL } from 'url'; - import EMS_FILES from '../__tests__/map/ems_mocks/sample_files.json'; import EMS_TILES from '../__tests__/map/ems_mocks/sample_tiles.json'; import EMS_STYLE_ROAD_MAP_BRIGHT from '../__tests__/map/ems_mocks/sample_style_bright'; diff --git a/test/plugin_functional/test_suites/application_links/redirect_app_links.ts b/test/plugin_functional/test_suites/application_links/redirect_app_links.ts index 4d19a90f08ad..6b0b52469509 100644 --- a/test/plugin_functional/test_suites/application_links/redirect_app_links.ts +++ b/test/plugin_functional/test_suites/application_links/redirect_app_links.ts @@ -28,7 +28,6 @@ * under the License. */ -import { URL } from 'url'; import expect from '@osd/expect'; import { PluginFunctionalProviderContext } from '../../services'; import '../../plugins/core_app_status/public/types'; diff --git a/test/plugin_functional/test_suites/core_plugins/application_leave_confirm.ts b/test/plugin_functional/test_suites/core_plugins/application_leave_confirm.ts index 8da02e7ba441..56eb0509a00a 100644 --- a/test/plugin_functional/test_suites/core_plugins/application_leave_confirm.ts +++ b/test/plugin_functional/test_suites/core_plugins/application_leave_confirm.ts @@ -28,7 +28,6 @@ * under the License. */ -import { URL } from 'url'; import expect from '@osd/expect'; import { PluginFunctionalProviderContext } from '../../services'; diff --git a/test/plugin_functional/test_suites/core_plugins/application_status.ts b/test/plugin_functional/test_suites/core_plugins/application_status.ts index ec62afbc2d7e..7334e036486b 100644 --- a/test/plugin_functional/test_suites/core_plugins/application_status.ts +++ b/test/plugin_functional/test_suites/core_plugins/application_status.ts @@ -28,7 +28,6 @@ * under the License. */ -import { URL } from 'url'; import expect from '@osd/expect'; import { AppNavLinkStatus, diff --git a/test/plugin_functional/test_suites/core_plugins/applications.ts b/test/plugin_functional/test_suites/core_plugins/applications.ts index 7ac1ccd42a19..18e780a68568 100644 --- a/test/plugin_functional/test_suites/core_plugins/applications.ts +++ b/test/plugin_functional/test_suites/core_plugins/applications.ts @@ -28,7 +28,6 @@ * under the License. */ -import { URL } from 'url'; import expect from '@osd/expect'; import { PluginFunctionalProviderContext } from '../../services'; From b53ee934d3a58e8e3e24de8af1098d8f943c4867 Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Tue, 13 Dec 2022 17:18:35 +0800 Subject: [PATCH 27/47] add force validate new URL create Signed-off-by: Lin Wang --- packages/osd-opensearch-archiver/src/cli.ts | 2 +- .../src/legacy_opensearch/opensearch_test_config.js | 2 +- packages/osd-test/src/osd/osd_test_config.ts | 2 +- src/cli/serve/serve.js | 2 +- src/cli_plugin/install/download.js | 2 +- src/core/server/http/http_server.ts | 2 +- .../server/opensearch/legacy/opensearch_client_config.ts | 2 +- src/plugins/console/server/lib/opensearch_proxy_config.ts | 2 +- src/plugins/maps_legacy/public/map/service_settings.test.js | 6 +++--- src/plugins/share/public/components/url_panel_content.tsx | 2 +- src/plugins/share/public/lib/url_shortener.ts | 2 +- test/common/services/security/test_user.ts | 2 +- .../test_suites/application_links/redirect_app_links.ts | 2 +- .../test_suites/core_plugins/application_status.ts | 2 +- test/server_integration/http/ssl_redirect/config.js | 2 +- 15 files changed, 17 insertions(+), 17 deletions(-) diff --git a/packages/osd-opensearch-archiver/src/cli.ts b/packages/osd-opensearch-archiver/src/cli.ts index 767fd6d2f444..2a1912343003 100644 --- a/packages/osd-opensearch-archiver/src/cli.ts +++ b/packages/osd-opensearch-archiver/src/cli.ts @@ -71,7 +71,7 @@ export function runCli() { throw createFlagError('--opensearch-url must be a string'); } if (!opensearchUrl && config) { - opensearchUrl = config.get('servers.opensearch').toString(); + opensearchUrl = new URL('', config.get('servers.opensearch')).toString(); } if (!opensearchUrl) { throw createFlagError('--opensearch-url or --config must be defined'); diff --git a/packages/osd-test/src/legacy_opensearch/opensearch_test_config.js b/packages/osd-test/src/legacy_opensearch/opensearch_test_config.js index e92906071847..8c2695fc7808 100644 --- a/packages/osd-test/src/legacy_opensearch/opensearch_test_config.js +++ b/packages/osd-test/src/legacy_opensearch/opensearch_test_config.js @@ -55,7 +55,7 @@ export const opensearchTestConfig = new (class OpenSearchTestConfig { getUrlParts() { // Allow setting one complete TEST_OPENSEARCH_URL for opensearch like https://opensearch:changeme@example.com:9200 if (process.env.TEST_OPENSEARCH_URL) { - return new URL(process.env.TEST_OPENSEARCH_URL); + return new URL('', process.env.TEST_OPENSEARCH_URL); } const username = process.env.TEST_OPENSEARCH_USERNAME || adminTestUser.username; const password = process.env.TEST_OPENSEARCH_PASSWORD || adminTestUser.password; diff --git a/packages/osd-test/src/osd/osd_test_config.ts b/packages/osd-test/src/osd/osd_test_config.ts index bba2f81bfe1d..5021cdaba3bb 100644 --- a/packages/osd-test/src/osd/osd_test_config.ts +++ b/packages/osd-test/src/osd/osd_test_config.ts @@ -38,7 +38,7 @@ export const osdTestConfig = new (class OsdTestConfig { getUrlParts() { // allow setting one complete TEST_OPENSEARCH_DASHBOARDS_URL for opensearch like https://opensearch:changeme@example.com:9200 if (process.env.TEST_OPENSEARCH_DASHBOARDS_URL) { - return new URL(process.env.TEST_OPENSEARCH_DASHBOARDS_URL); + return new URL('', process.env.TEST_OPENSEARCH_DASHBOARDS_URL); } const username = process.env.TEST_OPENSEARCH_DASHBOARDS_USERNAME || opensearchDashboardsTestUser.username; diff --git a/src/cli/serve/serve.js b/src/cli/serve/serve.js index 061e511f5917..6f89911ed089 100644 --- a/src/cli/serve/serve.js +++ b/src/cli/serve/serve.js @@ -109,7 +109,7 @@ function applyConfigOverrides(rawConfig, opts, extraCliOptions) { const opensearchHosts = ( (customOpenSearchHosts.length > 0 && customOpenSearchHosts) || ['https://localhost:9200'] ).map((hostUrl) => { - const parsedUrl = new URL(hostUrl); + const parsedUrl = new URL('', hostUrl); if (parsedUrl.hostname !== 'localhost') { throw new Error( `Hostname "${parsedUrl.hostname}" can't be used with --ssl. Must be "localhost" to work with certificates.` diff --git a/src/cli_plugin/install/download.js b/src/cli_plugin/install/download.js index b058c3386224..892fc7296969 100644 --- a/src/cli_plugin/install/download.js +++ b/src/cli_plugin/install/download.js @@ -56,7 +56,7 @@ export function _checkFilePathDeprecation(sourceUrl, logger) { export function _downloadSingle(settings, logger, sourceUrl) { let urlInfo; try { - urlInfo = new URL(sourceUrl); + urlInfo = new URL('', sourceUrl); } catch (e) { return Promise.reject(new UnsupportedProtocolError()); } diff --git a/src/core/server/http/http_server.ts b/src/core/server/http/http_server.ts index af348fd5d961..c0d2caaab727 100644 --- a/src/core/server/http/http_server.ts +++ b/src/core/server/http/http_server.ts @@ -293,7 +293,7 @@ export class HttpServer { this.server.ext('onRequest', (request, h) => { const { referrer } = request.info; if (referrer !== '') { - const { hostname } = new URL(referrer); + const { hostname } = new URL('', referrer); if (!hostname || !list.includes(hostname)) { request.info.acceptEncoding = ''; } diff --git a/src/core/server/opensearch/legacy/opensearch_client_config.ts b/src/core/server/opensearch/legacy/opensearch_client_config.ts index 12d333682b94..76d077131eab 100644 --- a/src/core/server/opensearch/legacy/opensearch_client_config.ts +++ b/src/core/server/opensearch/legacy/opensearch_client_config.ts @@ -130,7 +130,7 @@ export function parseOpenSearchClientConfig( if (Array.isArray(config.hosts)) { const needsAuth = auth !== false && config.username && config.password; opensearchClientConfig.hosts = config.hosts.map((nodeUrl: string) => { - const uri = new URL(nodeUrl); + const uri = new URL('', nodeUrl); const httpsURI = uri.protocol === 'https:'; const httpURI = uri.protocol === 'http:'; diff --git a/src/plugins/console/server/lib/opensearch_proxy_config.ts b/src/plugins/console/server/lib/opensearch_proxy_config.ts index 9cc2fd629a5e..3824e8c43c01 100644 --- a/src/plugins/console/server/lib/opensearch_proxy_config.ts +++ b/src/plugins/console/server/lib/opensearch_proxy_config.ts @@ -35,7 +35,7 @@ import https from 'https'; import { OpenSearchConfigForProxy } from '../types'; const createAgent = (legacyConfig: OpenSearchConfigForProxy): http.Agent | https.Agent => { - const target = new URL(_.head(legacyConfig.hosts)!); + const target = new URL('', _.head(legacyConfig.hosts)!); if (!/^https/.test(target.protocol || '')) return new http.Agent(); const agentOptions: https.AgentOptions = {}; diff --git a/src/plugins/maps_legacy/public/map/service_settings.test.js b/src/plugins/maps_legacy/public/map/service_settings.test.js index 7bfd779e663e..56671b849210 100644 --- a/src/plugins/maps_legacy/public/map/service_settings.test.js +++ b/src/plugins/maps_legacy/public/map/service_settings.test.js @@ -119,7 +119,7 @@ describe('service_settings (FKA tile_map test)', function () { 'OpenStreetMap contributors | OpenMapTiles | MapTiler | <iframe id=\'iframe\' style=\'position:fixed;height: 40%;width: 100%;top: 60%;left: 5%;right:5%;border: 0px;background:white;\' src=\'http://256.256.256.256\'></iframe>' ); - const urlObject = new URL(attrs.url); + const urlObject = new URL('', attrs.url); expect(urlObject.hostname).toEqual('tiles.foobar'); expect(urlObject.searchParams.get('my_app_name')).toEqual('opensearchDashboards'); expect(urlObject.searchParams.get('opensearch_tile_service_tos')).toEqual('agree'); @@ -141,7 +141,7 @@ describe('service_settings (FKA tile_map test)', function () { let serviceSettings; async function assertQuery(expected) { const attrs = await serviceSettings.getAttributesForTMSLayer(tilemapServices[0]); - const urlObject = new URL(attrs.url); + const urlObject = new URL('', attrs.url); Object.keys(expected).forEach((key) => { expect(urlObject.searchParams.get(key)).toEqual(expected[key]); }); @@ -315,7 +315,7 @@ describe('service_settings (FKA tile_map test)', function () { const assertions = fileLayers.map(async function (fileLayer) { expect(fileLayer.origin).toEqual(ORIGIN.EMS); const fileUrl = await serviceSettings.getUrlForRegionLayer(fileLayer); - const urlObject = new URL(fileUrl); + const urlObject = new URL('', fileUrl); Object.keys({ foo: 'bar', opensearch_tile_service_tos: 'agree' }).forEach((key) => { expect(typeof urlObject.searchParams.get(key)).toEqual('string'); }); diff --git a/src/plugins/share/public/components/url_panel_content.tsx b/src/plugins/share/public/components/url_panel_content.tsx index 6f042f4d77f4..9b4631cb58f0 100644 --- a/src/plugins/share/public/components/url_panel_content.tsx +++ b/src/plugins/share/public/components/url_panel_content.tsx @@ -183,7 +183,7 @@ export class UrlPanelContent extends Component { const url = this.getSnapshotUrl(); - const parsedUrl = new URL(url); + const parsedUrl = new URL('', url); if (!parsedUrl || !parsedUrl.hash) { return; } diff --git a/src/plugins/share/public/lib/url_shortener.ts b/src/plugins/share/public/lib/url_shortener.ts index 3a98afa7631e..353d067aec80 100644 --- a/src/plugins/share/public/lib/url_shortener.ts +++ b/src/plugins/share/public/lib/url_shortener.ts @@ -35,7 +35,7 @@ export async function shortenUrl( absoluteUrl: string, { basePath, post }: { basePath: string; post: HttpStart['post'] } ) { - const parsedUrl = new URL(absoluteUrl); + const parsedUrl = new URL('', absoluteUrl); if (!parsedUrl || !parsedUrl.pathname) { return; } diff --git a/test/common/services/security/test_user.ts b/test/common/services/security/test_user.ts index 129177b9d4f2..7de6894d0eeb 100644 --- a/test/common/services/security/test_user.ts +++ b/test/common/services/security/test_user.ts @@ -114,7 +114,7 @@ export async function createTestUserService( export function TestUserSupertestProvider({ getService }: FtrProviderContext) { const config = getService('config'); const opensearchDashboardsServerConfig = config.get('servers.opensearchDashboards'); - const url = new URL(opensearchDashboardsServerConfig); + const url = new URL('', opensearchDashboardsServerConfig); url.username = TEST_USER_NAME; url.password = TEST_USER_PASSWORD; diff --git a/test/plugin_functional/test_suites/application_links/redirect_app_links.ts b/test/plugin_functional/test_suites/application_links/redirect_app_links.ts index 6b0b52469509..8df9df61ab5e 100644 --- a/test/plugin_functional/test_suites/application_links/redirect_app_links.ts +++ b/test/plugin_functional/test_suites/application_links/redirect_app_links.ts @@ -39,7 +39,7 @@ declare global { } const getPathWithHash = (absoluteUrl: string) => { - const parsed = new URL(absoluteUrl); + const parsed = new URL('', absoluteUrl); return parsed.toString().substring(parsed.origin.length); }; diff --git a/test/plugin_functional/test_suites/core_plugins/application_status.ts b/test/plugin_functional/test_suites/core_plugins/application_status.ts index 7334e036486b..3685be38f0ed 100644 --- a/test/plugin_functional/test_suites/core_plugins/application_status.ts +++ b/test/plugin_functional/test_suites/core_plugins/application_status.ts @@ -129,7 +129,7 @@ export default function ({ getService, getPageObjects }: PluginFunctionalProvide await navigateToApp('app_status'); expect(await testSubjects.exists('appStatusApp')).to.eql(true); const currentUrl = await browser.getCurrentUrl(); - expect(new URL(currentUrl).pathname).to.eql('/app/app_status/arbitrary/path'); + expect(new URL('', currentUrl).pathname).to.eql('/app/app_status/arbitrary/path'); }); it('can change the state of the currently mounted app', async () => { diff --git a/test/server_integration/http/ssl_redirect/config.js b/test/server_integration/http/ssl_redirect/config.js index 1e6c24ca1811..7eee53b238db 100644 --- a/test/server_integration/http/ssl_redirect/config.js +++ b/test/server_integration/http/ssl_redirect/config.js @@ -37,7 +37,7 @@ export default async function ({ readConfigFile }) { const httpConfig = await readConfigFile(require.resolve('../../config')); const certificateAuthorities = [readFileSync(CA_CERT_PATH)]; - const opensearchDashboardsUrl = new URL(httpConfig.get('servers.opensearchDashboards')); + const opensearchDashboardsUrl = new URL('', httpConfig.get('servers.opensearchDashboards')); const redirectPort = opensearchDashboardsUrl.port + 1234; opensearchDashboardsUrl.port = redirectPort; opensearchDashboardsUrl.protocol = 'http'; From c910deaf45d5370db20459f6aa15543968610e26 Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Tue, 13 Dec 2022 17:24:38 +0800 Subject: [PATCH 28/47] add changelog Signed-off-by: Lin Wang --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 37d21f377bdc..514360e6d392 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -69,6 +69,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - Temporary workaround for task-kill exceptions on Windows when it is passed a pid for a process that is already dead ([#2842](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2842)) - [Vis Builder] Fix empty workspace animation does not work in firefox ([#2853](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2853)) - Bumped `del` version to fix MacOS race condition ([#2847](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2873)) +- [Chore] Update deprecated url methods (url.parse(), url.format()) ([#1561](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/1561)) ### 🚞 Infrastructure From b73ac85a8ce842787211af8e6ca69be571c4f6e7 Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Fri, 16 Dec 2022 14:47:41 +0800 Subject: [PATCH 29/47] update to use object spread construct URLSearchParams Signed-off-by: Lin Wang --- examples/url_generators_examples/public/url_generator.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/url_generators_examples/public/url_generator.ts b/examples/url_generators_examples/public/url_generator.ts index b7ba8f16b558..512dc191782a 100644 --- a/examples/url_generators_examples/public/url_generator.ts +++ b/examples/url_generators_examples/public/url_generator.ts @@ -54,7 +54,7 @@ export const createHelloPageLinkGenerator = ( const appBasePath = startServices.appBasePath; const url = new URL(`${appBasePath}/hello`, window.location.origin); - url.search = new URLSearchParams((state as unknown) as Record).toString(); + url.search = new URLSearchParams({ ...state }).toString(); return url.toString(); }, From 507c3a8f899bcd86c60b7f19c39af7faeded6eee Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Fri, 16 Dec 2022 14:51:00 +0800 Subject: [PATCH 30/47] renaming getUrlParts to getURL Signed-off-by: Lin Wang --- .../src/legacy_opensearch/opensearch_test_config.js | 6 +++--- packages/osd-test/src/osd/osd_test_config.ts | 4 ++-- test/common/config.js | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/osd-test/src/legacy_opensearch/opensearch_test_config.js b/packages/osd-test/src/legacy_opensearch/opensearch_test_config.js index 8c2695fc7808..2af51e77a975 100644 --- a/packages/osd-test/src/legacy_opensearch/opensearch_test_config.js +++ b/packages/osd-test/src/legacy_opensearch/opensearch_test_config.js @@ -37,11 +37,11 @@ export const opensearchTestConfig = new (class OpenSearchTestConfig { } getPort() { - return this.getUrlParts().port; + return this.getURL().port; } getUrl() { - return this.getUrlParts().toString(); + return this.getURL().toString(); } getBuildFrom() { @@ -52,7 +52,7 @@ export const opensearchTestConfig = new (class OpenSearchTestConfig { return process.env.TEST_OPENSEARCH_TRANSPORT_PORT || '9300-9400'; } - getUrlParts() { + getURL() { // Allow setting one complete TEST_OPENSEARCH_URL for opensearch like https://opensearch:changeme@example.com:9200 if (process.env.TEST_OPENSEARCH_URL) { return new URL('', process.env.TEST_OPENSEARCH_URL); diff --git a/packages/osd-test/src/osd/osd_test_config.ts b/packages/osd-test/src/osd/osd_test_config.ts index 5021cdaba3bb..44473ac9bb47 100644 --- a/packages/osd-test/src/osd/osd_test_config.ts +++ b/packages/osd-test/src/osd/osd_test_config.ts @@ -32,10 +32,10 @@ import { opensearchDashboardsTestUser } from './users'; export const osdTestConfig = new (class OsdTestConfig { getPort() { - return parseInt(this.getUrlParts().port, 10); + return parseInt(this.getURL().port, 10); } - getUrlParts() { + getURL() { // allow setting one complete TEST_OPENSEARCH_DASHBOARDS_URL for opensearch like https://opensearch:changeme@example.com:9200 if (process.env.TEST_OPENSEARCH_DASHBOARDS_URL) { return new URL('', process.env.TEST_OPENSEARCH_DASHBOARDS_URL); diff --git a/test/common/config.js b/test/common/config.js index f7328649b3c9..f99baac32aaa 100644 --- a/test/common/config.js +++ b/test/common/config.js @@ -34,8 +34,8 @@ import { services } from './services'; export default function () { const servers = { - opensearchDashboards: osdTestConfig.getUrlParts(), - opensearch: opensearchTestConfig.getUrlParts(), + opensearchDashboards: osdTestConfig.getURL(), + opensearch: opensearchTestConfig.getURL(), }; return { From f5a13f046e6878be3733ed8f70d3adc22d00c656 Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Thu, 29 Dec 2022 12:10:16 +0800 Subject: [PATCH 31/47] fix force url validate remove hash part Signed-off-by: Lin Wang --- src/plugins/share/public/components/url_panel_content.tsx | 2 +- src/plugins/share/public/lib/url_shortener.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/plugins/share/public/components/url_panel_content.tsx b/src/plugins/share/public/components/url_panel_content.tsx index 9b4631cb58f0..6f042f4d77f4 100644 --- a/src/plugins/share/public/components/url_panel_content.tsx +++ b/src/plugins/share/public/components/url_panel_content.tsx @@ -183,7 +183,7 @@ export class UrlPanelContent extends Component { const url = this.getSnapshotUrl(); - const parsedUrl = new URL('', url); + const parsedUrl = new URL(url); if (!parsedUrl || !parsedUrl.hash) { return; } diff --git a/src/plugins/share/public/lib/url_shortener.ts b/src/plugins/share/public/lib/url_shortener.ts index 353d067aec80..3a98afa7631e 100644 --- a/src/plugins/share/public/lib/url_shortener.ts +++ b/src/plugins/share/public/lib/url_shortener.ts @@ -35,7 +35,7 @@ export async function shortenUrl( absoluteUrl: string, { basePath, post }: { basePath: string; post: HttpStart['post'] } ) { - const parsedUrl = new URL('', absoluteUrl); + const parsedUrl = new URL(absoluteUrl); if (!parsedUrl || !parsedUrl.pathname) { return; } From 1731fc0f9b287ebd3fae1ca3a4e1bb2051e4aea7 Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Thu, 29 Dec 2022 13:52:21 +0800 Subject: [PATCH 32/47] update opensearch config getUrlParts to getURL Signed-off-by: Lin Wang --- .../src/legacy_opensearch/legacy_opensearch_test_cluster.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/osd-test/src/legacy_opensearch/legacy_opensearch_test_cluster.js b/packages/osd-test/src/legacy_opensearch/legacy_opensearch_test_cluster.js index 85930f0d892e..e17099132680 100644 --- a/packages/osd-test/src/legacy_opensearch/legacy_opensearch_test_cluster.js +++ b/packages/osd-test/src/legacy_opensearch/legacy_opensearch_test_cluster.js @@ -134,7 +134,7 @@ export function createLegacyOpenSearchTestCluster(options = {}) { } getUrl() { - const url = opensearchTestConfig.getUrlParts(); + const url = opensearchTestConfig.getURL(); url.port = port; return url.toString(); From 2d007f09aa66682f4212251d1708c452674d42c5 Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Fri, 30 Dec 2022 17:30:28 +0800 Subject: [PATCH 33/47] remove search string compare in header_logo Signed-off-by: Lin Wang --- src/core/public/chrome/ui/header/header_logo.tsx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/core/public/chrome/ui/header/header_logo.tsx b/src/core/public/chrome/ui/header/header_logo.tsx index 2ecca30ed574..26b1783e132b 100644 --- a/src/core/public/chrome/ui/header/header_logo.tsx +++ b/src/core/public/chrome/ui/header/header_logo.tsx @@ -77,8 +77,7 @@ function onClick( const fromParsed = new URL(document.location.href); const sameProto = toParsed.protocol === fromParsed.protocol; const sameHost = toParsed.host === fromParsed.host; - const samePath = - toParsed.pathname === fromParsed.pathname && toParsed.search === fromParsed.search; + const samePath = toParsed.pathname === fromParsed.pathname; if (sameProto && sameHost && samePath) { if (toParsed.hash) { From c2feeb45fed95b5aefb7e85a9b49b714c7caec53 Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Thu, 12 Jan 2023 19:40:34 +0800 Subject: [PATCH 34/47] fix: url was truncated incorrectly Signed-off-by: Lin Wang --- test/functional/services/supertest.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/functional/services/supertest.ts b/test/functional/services/supertest.ts index 0c4aebbef4d9..60430423b353 100644 --- a/test/functional/services/supertest.ts +++ b/test/functional/services/supertest.ts @@ -34,9 +34,9 @@ import supertestAsPromised from 'supertest-as-promised'; export function OpenSearchDashboardsSupertestProvider({ getService }: FtrProviderContext) { const config = getService('config'); - const opensearchDashboardsServerUrl = config - .get('servers.opensearchDashboards') - .toString() - .slice(0, -1); + const originOSDServerUrl = config.get('servers.opensearchDashboards').toString(); + const opensearchDashboardsServerUrl = originOSDServerUrl.endsWith('/') + ? originOSDServerUrl.slice(0, -1) + : originOSDServerUrl; return supertestAsPromised(opensearchDashboardsServerUrl); } From 610ca3012224a003151c12076b7e9215a9ef611a Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Thu, 12 Jan 2023 19:53:00 +0800 Subject: [PATCH 35/47] feat: parse URL string port to number port Signed-off-by: Lin Wang --- test/server_integration/http/ssl_redirect/config.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/server_integration/http/ssl_redirect/config.js b/test/server_integration/http/ssl_redirect/config.js index 7eee53b238db..97f6c04c9bf5 100644 --- a/test/server_integration/http/ssl_redirect/config.js +++ b/test/server_integration/http/ssl_redirect/config.js @@ -37,8 +37,8 @@ export default async function ({ readConfigFile }) { const httpConfig = await readConfigFile(require.resolve('../../config')); const certificateAuthorities = [readFileSync(CA_CERT_PATH)]; - const opensearchDashboardsUrl = new URL('', httpConfig.get('servers.opensearchDashboards')); - const redirectPort = opensearchDashboardsUrl.port + 1234; + const opensearchDashboardsUrl = new URL(httpConfig.get('servers.opensearchDashboards')); + const redirectPort = parseInt(opensearchDashboardsUrl.port, 10) + 1234; opensearchDashboardsUrl.port = redirectPort; opensearchDashboardsUrl.protocol = 'http'; From 91c36b9a8b992178f7bc202eb2b8fbae22561b05 Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Sat, 28 Jan 2023 22:57:13 +0800 Subject: [PATCH 36/47] Revert "change config servers.opensearch and servers.opensearchDashboards to URL" This reverts commit 7b0a2fcbcf57829809989ebc6ae8e2b28cd6352d. Signed-off-by: Lin Wang --- packages/osd-opensearch-archiver/src/cli.ts | 5 +- .../lib/config/config.ts | 6 --- .../lib/config/schema.ts | 19 +++++++- .../functional_tests/lib/run_opensearch.js | 4 +- .../legacy_opensearch_test_cluster.js | 7 +-- .../opensearch_test_config.js | 37 ++++++++++----- packages/osd-test/src/osd/osd_test_config.ts | 46 ++++++++++++++----- src/test_utils/get_url.js | 26 +++++------ src/test_utils/get_url.test.ts | 39 ++++++++++++---- test/api_integration/services/supertest.ts | 5 +- test/common/config.js | 7 +-- test/common/services/deployment.ts | 8 ++-- test/common/services/legacy_opensearch.ts | 4 +- test/common/services/opensearch.ts | 5 +- test/common/services/opensearch_archiver.ts | 3 +- .../opensearch_dashboards_server.ts | 4 +- test/common/services/security/test_user.ts | 11 +++-- test/functional/page_objects/common_page.ts | 4 +- test/functional/services/supertest.ts | 6 +-- .../http/platform/headers.ts | 8 +++- .../http/ssl_redirect/config.js | 13 ++++-- test/server_integration/services/supertest.js | 18 +++++--- 22 files changed, 187 insertions(+), 98 deletions(-) diff --git a/packages/osd-opensearch-archiver/src/cli.ts b/packages/osd-opensearch-archiver/src/cli.ts index 2a1912343003..39dbbb264cd7 100644 --- a/packages/osd-opensearch-archiver/src/cli.ts +++ b/packages/osd-opensearch-archiver/src/cli.ts @@ -35,6 +35,7 @@ *************************************************************/ import Path from 'path'; +import Url from 'url'; import readline from 'readline'; import { RunWithCommands, createFlagError } from '@osd/dev-utils'; @@ -71,7 +72,7 @@ export function runCli() { throw createFlagError('--opensearch-url must be a string'); } if (!opensearchUrl && config) { - opensearchUrl = new URL('', config.get('servers.opensearch')).toString(); + opensearchUrl = Url.format(config.get('servers.opensearch')); } if (!opensearchUrl) { throw createFlagError('--opensearch-url or --config must be defined'); @@ -82,7 +83,7 @@ export function runCli() { throw createFlagError('--opensearch-dashboards-url must be a string'); } if (!opensearchDashboardsUrl && config) { - opensearchDashboardsUrl = config.get('servers.opensearchDashboards').toString() as string; + opensearchDashboardsUrl = Url.format(config.get('servers.opensearchDashboards')); } if (!opensearchDashboardsUrl) { throw createFlagError('---url or --config must be defined'); diff --git a/packages/osd-test/src/functional_test_runner/lib/config/config.ts b/packages/osd-test/src/functional_test_runner/lib/config/config.ts index 7396a92e6441..3fd162bef29b 100644 --- a/packages/osd-test/src/functional_test_runner/lib/config/config.ts +++ b/packages/osd-test/src/functional_test_runner/lib/config/config.ts @@ -126,9 +126,6 @@ export class Config { if (typeof v === 'function') { return v; } - if (v instanceof URL) { - return new URL(v.toString()); - } }); } @@ -137,9 +134,6 @@ export class Config { if (typeof v === 'function') { return v; } - if (v instanceof URL) { - return new URL(v.toString()); - } }); } } diff --git a/packages/osd-test/src/functional_test_runner/lib/config/schema.ts b/packages/osd-test/src/functional_test_runner/lib/config/schema.ts index dc6e0c1d1590..447db1e97874 100644 --- a/packages/osd-test/src/functional_test_runner/lib/config/schema.ts +++ b/packages/osd-test/src/functional_test_runner/lib/config/schema.ts @@ -38,6 +38,21 @@ const ID_PATTERN = /^[a-zA-Z0-9_]+$/; const INSPECTING = process.execArgv.includes('--inspect') || process.execArgv.includes('--inspect-brk'); +const urlPartsSchema = () => + Joi.object() + .keys({ + protocol: Joi.string().valid('http', 'https').default('http'), + hostname: Joi.string().hostname().default('localhost'), + port: Joi.number(), + auth: Joi.string().regex(/^[^:]+:.+$/, 'username and password separated by a colon'), + username: Joi.string(), + password: Joi.string(), + pathname: Joi.string().regex(/^\//, 'start with a /'), + hash: Joi.string().regex(/^\//, 'start with a /'), + certificateAuthorities: Joi.array().items(Joi.binary()).optional(), + }) + .default(); + const appUrlPartsSchema = () => Joi.object() .keys({ @@ -169,8 +184,8 @@ export const schema = Joi.object() servers: Joi.object() .keys({ - opensearchDashboards: Joi.object().type(URL), - opensearch: Joi.object().type(URL), + opensearchDashboards: urlPartsSchema(), + opensearch: urlPartsSchema(), }) .default(), diff --git a/packages/osd-test/src/functional_tests/lib/run_opensearch.js b/packages/osd-test/src/functional_tests/lib/run_opensearch.js index d0e6ff7e9f90..925cebcf55bf 100644 --- a/packages/osd-test/src/functional_tests/lib/run_opensearch.js +++ b/packages/osd-test/src/functional_tests/lib/run_opensearch.js @@ -41,8 +41,8 @@ export async function runOpenSearch({ config, options }) { const isSecurityEnabled = opensearchArgs.includes('xpack.security.enabled=true'); const cluster = createLegacyOpenSearchTestCluster({ - port: config.get('servers.opensearch').port, - password: isSecurityEnabled ? 'changethispassword' : config.get('servers.opensearch').password, + port: config.get('servers.opensearch.port'), + password: isSecurityEnabled ? 'changethispassword' : config.get('servers.opensearch.password'), license, log, basePath: resolve(OPENSEARCH_DASHBOARDS_ROOT, '.opensearch'), diff --git a/packages/osd-test/src/legacy_opensearch/legacy_opensearch_test_cluster.js b/packages/osd-test/src/legacy_opensearch/legacy_opensearch_test_cluster.js index e17099132680..72506099dfe4 100644 --- a/packages/osd-test/src/legacy_opensearch/legacy_opensearch_test_cluster.js +++ b/packages/osd-test/src/legacy_opensearch/legacy_opensearch_test_cluster.js @@ -29,6 +29,7 @@ */ import { resolve } from 'path'; +import { format } from 'url'; import { get, toPath } from 'lodash'; import { Cluster } from '@osd/opensearch'; import { CI_PARALLEL_PROCESS_PREFIX } from '../ci_parallel_process_prefix'; @@ -134,10 +135,10 @@ export function createLegacyOpenSearchTestCluster(options = {}) { } getUrl() { - const url = opensearchTestConfig.getURL(); - url.port = port; + const parts = opensearchTestConfig.getUrlParts(); + parts.port = port; - return url.toString(); + return format(parts); } })(); } diff --git a/packages/osd-test/src/legacy_opensearch/opensearch_test_config.js b/packages/osd-test/src/legacy_opensearch/opensearch_test_config.js index 2af51e77a975..7befa4226afd 100644 --- a/packages/osd-test/src/legacy_opensearch/opensearch_test_config.js +++ b/packages/osd-test/src/legacy_opensearch/opensearch_test_config.js @@ -28,6 +28,7 @@ * under the License. */ +import url, { format as formatUrl } from 'url'; import pkg from '../../../../package.json'; import { adminTestUser } from '../osd'; @@ -37,11 +38,11 @@ export const opensearchTestConfig = new (class OpenSearchTestConfig { } getPort() { - return this.getURL().port; + return this.getUrlParts().port; } getUrl() { - return this.getURL().toString(); + return formatUrl(this.getUrlParts()); } getBuildFrom() { @@ -52,19 +53,33 @@ export const opensearchTestConfig = new (class OpenSearchTestConfig { return process.env.TEST_OPENSEARCH_TRANSPORT_PORT || '9300-9400'; } - getURL() { + getUrlParts() { // Allow setting one complete TEST_OPENSEARCH_URL for opensearch like https://opensearch:changeme@example.com:9200 if (process.env.TEST_OPENSEARCH_URL) { - return new URL('', process.env.TEST_OPENSEARCH_URL); + const testOpenSearchUrl = url.parse(process.env.TEST_OPENSEARCH_URL); + return { + // have to remove the ":" off protocol + protocol: testOpenSearchUrl.protocol.slice(0, -1), + hostname: testOpenSearchUrl.hostname, + port: parseInt(testOpenSearchUrl.port, 10), + username: testOpenSearchUrl.auth.split(':')[0], + password: testOpenSearchUrl.auth.split(':')[1], + auth: testOpenSearchUrl.auth, + }; } + const username = process.env.TEST_OPENSEARCH_USERNAME || adminTestUser.username; const password = process.env.TEST_OPENSEARCH_PASSWORD || adminTestUser.password; - const protocol = process.env.TEST_OPENSEARCH_PROTOCOL || 'http'; - const hostname = process.env.TEST_OPENSEARCH_HOSTNAME || 'localhost'; - const port = parseInt(process.env.TEST_OPENSEARCH_PORT, 10) || 9220; - const url = new URL(`${protocol}://${hostname}:${port}`); - url.username = username; - url.password = password; - return url; + + return { + // Allow setting any individual component(s) of the URL, + // or use default values (username and password from ../osd/users.js) + protocol: process.env.TEST_OPENSEARCH_PROTOCOL || 'http', + hostname: process.env.TEST_OPENSEARCH_HOSTNAME || 'localhost', + port: parseInt(process.env.TEST_OPENSEARCH_PORT, 10) || 9220, + auth: `${username}:${password}`, + username: username, + password: password, + }; } })(); diff --git a/packages/osd-test/src/osd/osd_test_config.ts b/packages/osd-test/src/osd/osd_test_config.ts index 44473ac9bb47..6c3ef573511b 100644 --- a/packages/osd-test/src/osd/osd_test_config.ts +++ b/packages/osd-test/src/osd/osd_test_config.ts @@ -28,30 +28,52 @@ * under the License. */ +import url from 'url'; import { opensearchDashboardsTestUser } from './users'; +interface UrlParts { + protocol?: string; + hostname?: string; + port?: number; + auth?: string; + username?: string; + password?: string; +} + export const osdTestConfig = new (class OsdTestConfig { getPort() { - return parseInt(this.getURL().port, 10); + return this.getUrlParts().port; } - getURL() { + getUrlParts(): UrlParts { // allow setting one complete TEST_OPENSEARCH_DASHBOARDS_URL for opensearch like https://opensearch:changeme@example.com:9200 if (process.env.TEST_OPENSEARCH_DASHBOARDS_URL) { - return new URL('', process.env.TEST_OPENSEARCH_DASHBOARDS_URL); + const testOpenSearchDashboardsUrl = url.parse(process.env.TEST_OPENSEARCH_DASHBOARDS_URL); + return { + protocol: testOpenSearchDashboardsUrl.protocol?.slice(0, -1), + hostname: testOpenSearchDashboardsUrl.hostname ?? undefined, + port: testOpenSearchDashboardsUrl.port + ? parseInt(testOpenSearchDashboardsUrl.port, 10) + : undefined, + auth: testOpenSearchDashboardsUrl.auth ?? undefined, + username: testOpenSearchDashboardsUrl.auth?.split(':')[0], + password: testOpenSearchDashboardsUrl.auth?.split(':')[1], + }; } + const username = process.env.TEST_OPENSEARCH_DASHBOARDS_USERNAME || opensearchDashboardsTestUser.username; const password = process.env.TEST_OPENSEARCH_DASHBOARDS_PASSWORD || opensearchDashboardsTestUser.password; - const protocol = process.env.TEST_OPENSEARCH_DASHBOARDS_PROTOCOL || 'http'; - const hostname = process.env.TEST_OPENSEARCH_DASHBOARDS_HOSTNAME || 'localhost'; - const port = process.env.TEST_OPENSEARCH_DASHBOARDS_PORT - ? parseInt(process.env.TEST_OPENSEARCH_DASHBOARDS_PORT, 10) - : 5620; - const url = new URL(`${protocol}://${hostname}:${port}`); - url.username = username; - url.password = password; - return url; + return { + protocol: process.env.TEST_OPENSEARCH_DASHBOARDS_PROTOCOL || 'http', + hostname: process.env.TEST_OPENSEARCH_DASHBOARDS_HOSTNAME || 'localhost', + port: process.env.TEST_OPENSEARCH_DASHBOARDS_PORT + ? parseInt(process.env.TEST_OPENSEARCH_DASHBOARDS_PORT, 10) + : 5620, + auth: `${username}:${password}`, + username, + password, + }; } })(); diff --git a/src/test_utils/get_url.js b/src/test_utils/get_url.js index 2787e467b7a9..3b25d2bc626b 100644 --- a/src/test_utils/get_url.js +++ b/src/test_utils/get_url.js @@ -28,6 +28,9 @@ * under the License. */ +import _ from 'lodash'; +import url from 'url'; + /** * Converts a config and a pathname to a url * @param {object} config A url config @@ -47,22 +50,17 @@ * @return {string} */ -export default function getUrl(url, app) { - if (app.pathname) { - url.pathname = app.pathname; - } - if (app.hash) { - url.hash = app.hash; - } - return url.toString(); +export default function getUrl(config, app) { + return url.format(_.assign({}, config, app)); } -getUrl.noAuth = function getUrlNoAuth(url, app) { - url.username = ''; - url.password = ''; - return getUrl(url, app); +getUrl.noAuth = function getUrlNoAuth(config, app) { + config = _.pickBy(config, function (val, param) { + return param !== 'auth'; + }); + return getUrl(config, app); }; -getUrl.baseUrl = function getBaseUrl(url) { - return url.origin; +getUrl.baseUrl = function getBaseUrl(config) { + return url.format(_.pick(config, 'protocol', 'hostname', 'port')); }; diff --git a/src/test_utils/get_url.test.ts b/src/test_utils/get_url.test.ts index 59cf2c3b22e1..c784c9c60b95 100644 --- a/src/test_utils/get_url.test.ts +++ b/src/test_utils/get_url.test.ts @@ -33,27 +33,46 @@ import getUrl from './get_url'; describe('getUrl', function () { it('should convert to a url', function () { - const url = getUrl(new URL('http://localhost'), { - pathname: 'foo', - }); + const url = getUrl( + { + protocol: 'http', + hostname: 'localhost', + }, + { + pathname: 'foo', + } + ); expect(url).to.be('http://localhost/foo'); }); it('should convert to a url with port', function () { - const url = getUrl(new URL('http://localhost:9220'), { - pathname: 'foo', - }); + const url = getUrl( + { + protocol: 'http', + hostname: 'localhost', + port: 9220, + }, + { + pathname: 'foo', + } + ); expect(url).to.be('http://localhost:9220/foo'); }); it('should convert to a secure hashed url', function () { expect( - getUrl(new URL('https://localhost'), { - pathname: 'foo', - hash: 'bar', - }) + getUrl( + { + protocol: 'https', + hostname: 'localhost', + }, + { + pathname: 'foo', + hash: 'bar', + } + ) ).to.be('https://localhost/foo#bar'); }); }); diff --git a/test/api_integration/services/supertest.ts b/test/api_integration/services/supertest.ts index eec1ca3a9b40..369c25902b11 100644 --- a/test/api_integration/services/supertest.ts +++ b/test/api_integration/services/supertest.ts @@ -29,17 +29,18 @@ */ import { FtrProviderContext } from 'test/functional/ftr_provider_context'; +import { format as formatUrl } from 'url'; import supertestAsPromised from 'supertest-as-promised'; export function OpenSearchDashboardsSupertestProvider({ getService }: FtrProviderContext) { const config = getService('config'); - const opensearchDashboardsServerUrl = config.get('servers.opensearchDashboards').toString(); + const opensearchDashboardsServerUrl = formatUrl(config.get('servers.opensearchDashboards')); return supertestAsPromised(opensearchDashboardsServerUrl); } export function OpenSearchSupertestProvider({ getService }: FtrProviderContext) { const config = getService('config'); - const elasticSearchServerUrl = config.get('servers.opensearch').toString(); + const elasticSearchServerUrl = formatUrl(config.get('servers.opensearch')); return supertestAsPromised(elasticSearchServerUrl); } diff --git a/test/common/config.js b/test/common/config.js index f99baac32aaa..3d5ea8852751 100644 --- a/test/common/config.js +++ b/test/common/config.js @@ -29,13 +29,14 @@ */ //import path from 'path'; +import { format as formatUrl } from 'url'; import { opensearchTestConfig, osdTestConfig, opensearchDashboardsServerTestUser } from '@osd/test'; import { services } from './services'; export default function () { const servers = { - opensearchDashboards: osdTestConfig.getURL(), - opensearch: opensearchTestConfig.getURL(), + opensearchDashboards: osdTestConfig.getURLParts(), + opensearch: opensearchTestConfig.getURLParts(), }; return { @@ -54,7 +55,7 @@ export default function () { '--logging.json=false', `--server.port=${osdTestConfig.getPort()}`, '--status.allowAnonymous=true', - `--opensearch.hosts=${servers.opensearch.toString()}`, + `--opensearch.hosts=${formatUrl(servers.opensearch)}`, `--opensearch.username=${opensearchDashboardsServerTestUser.username}`, `--opensearch.password=${opensearchDashboardsServerTestUser.password}`, `--home.disableWelcomeScreen=false`, diff --git a/test/common/services/deployment.ts b/test/common/services/deployment.ts index 0ef1eb64bc13..d28ed55a2bb3 100644 --- a/test/common/services/deployment.ts +++ b/test/common/services/deployment.ts @@ -60,8 +60,8 @@ export function DeploymentProvider({ getService }: FtrProviderContext) { */ async isOss() { const baseUrl = this.getOpenSearchHostPort(); - const username = config.get('servers.opensearch').username; - const password = config.get('servers.opensearch').password; + const username = config.get('servers.opensearch.username'); + const password = config.get('servers.opensearch.password'); const response = await fetch(baseUrl + '/_xpack', { method: 'get', headers: { @@ -74,8 +74,8 @@ export function DeploymentProvider({ getService }: FtrProviderContext) { async isCloud(): Promise { const baseUrl = this.getHostPort(); - const username = config.get('servers.opensearchDashboards').username; - const password = config.get('servers.opensearchDashboards').password; + const username = config.get('servers.opensearchDashboards.username'); + const password = config.get('servers.opensearchDashboards.password'); const response = await fetch(baseUrl + '/api/stats?extended', { method: 'get', headers: { diff --git a/test/common/services/legacy_opensearch.ts b/test/common/services/legacy_opensearch.ts index 3c2dac8ea4f1..597a55a79f3b 100644 --- a/test/common/services/legacy_opensearch.ts +++ b/test/common/services/legacy_opensearch.ts @@ -28,6 +28,8 @@ * under the License. */ +import { format as formatUrl } from 'url'; + import * as legacyOpenSearch from 'elasticsearch'; import { DEFAULT_API_VERSION } from '../../../src/core/server/opensearch/opensearch_config'; @@ -40,7 +42,7 @@ export function LegacyOpenSearchProvider({ return new legacyOpenSearch.Client({ apiVersion: DEFAULT_API_VERSION, - host: config.get('servers.opensearch').toString(), + host: formatUrl(config.get('servers.opensearch')), requestTimeout: config.get('timeouts.opensearchRequestTimeout'), }); } diff --git a/test/common/services/opensearch.ts b/test/common/services/opensearch.ts index 340eaeff0c0b..b162ee961f6d 100644 --- a/test/common/services/opensearch.ts +++ b/test/common/services/opensearch.ts @@ -28,6 +28,7 @@ * under the License. */ +import { format as formatUrl } from 'url'; import fs from 'fs'; import { Client } from '@opensearch-project/opensearch'; import { CA_CERT_PATH } from '@osd/dev-utils'; @@ -39,7 +40,7 @@ export function OpenSearchProvider({ getService }: FtrProviderContext) { if (process.env.TEST_CLOUD) { return new Client({ - nodes: [config.get('servers.opensearch').toString()], + nodes: [formatUrl(config.get('servers.opensearch'))], requestTimeout: config.get('timeouts.opensearchRequestTimeout'), }); } else { @@ -47,7 +48,7 @@ export function OpenSearchProvider({ getService }: FtrProviderContext) { ssl: { ca: fs.readFileSync(CA_CERT_PATH, 'utf-8'), }, - nodes: [config.get('servers.opensearch').toString()], + nodes: [formatUrl(config.get('servers.opensearch'))], requestTimeout: config.get('timeouts.opensearchRequestTimeout'), }); } diff --git a/test/common/services/opensearch_archiver.ts b/test/common/services/opensearch_archiver.ts index bb6ab3225c1b..eb36c9e5a1d5 100644 --- a/test/common/services/opensearch_archiver.ts +++ b/test/common/services/opensearch_archiver.ts @@ -28,6 +28,7 @@ * under the License. */ +import { format as formatUrl } from 'url'; import { OpenSearchArchiver } from '@osd/opensearch-archiver'; import { FtrProviderContext } from '../ftr_provider_context'; @@ -54,7 +55,7 @@ export function OpenSearchArchiverProvider({ client, dataDir, log, - opensearchDashboardsUrl: config.get('servers.opensearchDashboards').toString(), + opensearchDashboardsUrl: formatUrl(config.get('servers.opensearchDashboards')), }); if (hasService('opensearchDashboardsServer')) { diff --git a/test/common/services/opensearch_dashboards_server/opensearch_dashboards_server.ts b/test/common/services/opensearch_dashboards_server/opensearch_dashboards_server.ts index b76bf25c852a..c989bbff9caa 100644 --- a/test/common/services/opensearch_dashboards_server/opensearch_dashboards_server.ts +++ b/test/common/services/opensearch_dashboards_server/opensearch_dashboards_server.ts @@ -28,6 +28,7 @@ * under the License. */ +import Url from 'url'; import { OsdClient } from '@osd/dev-utils'; import { FtrProviderContext } from '../../ftr_provider_context'; @@ -36,11 +37,12 @@ export function OpenSearchDashboardsServerProvider({ getService }: FtrProviderCo const log = getService('log'); const config = getService('config'); const lifecycle = getService('lifecycle'); - const url = config.get('servers.opensearchDashboards').toString(); + const url = Url.format(config.get('servers.opensearchDashboards')); const defaults = config.get('uiSettings.defaults'); const osd = new OsdClient({ log, url, + certificateAuthorities: config.get('servers.opensearchDashboards.certificateAuthorities'), uiSettingDefaults: defaults, }); diff --git a/test/common/services/security/test_user.ts b/test/common/services/security/test_user.ts index 7de6894d0eeb..8cb7b14af24e 100644 --- a/test/common/services/security/test_user.ts +++ b/test/common/services/security/test_user.ts @@ -28,6 +28,7 @@ * under the License. */ +import { format as formatUrl } from 'url'; import supertestAsPromised from 'supertest-as-promised'; import { Role } from './role'; @@ -114,9 +115,11 @@ export async function createTestUserService( export function TestUserSupertestProvider({ getService }: FtrProviderContext) { const config = getService('config'); const opensearchDashboardsServerConfig = config.get('servers.opensearchDashboards'); - const url = new URL('', opensearchDashboardsServerConfig); - url.username = TEST_USER_NAME; - url.password = TEST_USER_PASSWORD; - return supertestAsPromised(url.toString()); + return supertestAsPromised( + formatUrl({ + ...opensearchDashboardsServerConfig, + auth: `${TEST_USER_NAME}:${TEST_USER_PASSWORD}`, + }) + ); } diff --git a/test/functional/page_objects/common_page.ts b/test/functional/page_objects/common_page.ts index a580a4f40d50..1a303dab14dc 100644 --- a/test/functional/page_objects/common_page.ts +++ b/test/functional/page_objects/common_page.ts @@ -79,8 +79,8 @@ export function CommonPageProvider({ getService, getPageObjects }: FtrProviderCo log.debug('Found login page'); if (config.get('security.disableTestUser')) { await PageObjects.login.login( - config.get('servers.opensearchDashboards').username, - config.get('servers.opensearchDashboards').password + config.get('servers.opensearchDashboards.username'), + config.get('servers.opensearchDashboards.password') ); } else { await PageObjects.login.login('test_user', 'changeme'); diff --git a/test/functional/services/supertest.ts b/test/functional/services/supertest.ts index 60430423b353..cc33c618be52 100644 --- a/test/functional/services/supertest.ts +++ b/test/functional/services/supertest.ts @@ -29,14 +29,12 @@ */ import { FtrProviderContext } from 'test/functional/ftr_provider_context'; +import { format as formatUrl } from 'url'; import supertestAsPromised from 'supertest-as-promised'; export function OpenSearchDashboardsSupertestProvider({ getService }: FtrProviderContext) { const config = getService('config'); - const originOSDServerUrl = config.get('servers.opensearchDashboards').toString(); - const opensearchDashboardsServerUrl = originOSDServerUrl.endsWith('/') - ? originOSDServerUrl.slice(0, -1) - : originOSDServerUrl; + const opensearchDashboardsServerUrl = formatUrl(config.get('servers.opensearchDashboards')); return supertestAsPromised(opensearchDashboardsServerUrl); } diff --git a/test/server_integration/http/platform/headers.ts b/test/server_integration/http/platform/headers.ts index 0b460e4572d0..dadabe1e6058 100644 --- a/test/server_integration/http/platform/headers.ts +++ b/test/server_integration/http/platform/headers.ts @@ -29,8 +29,12 @@ */ import Http from 'http'; +import Url from 'url'; import { FtrProviderContext } from '../../services/types'; +// @ts-ignore +import getUrl from '../../../../src/test_utils/get_url'; + const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); const oneSec = 1_000; @@ -43,7 +47,9 @@ export default function ({ getService }: FtrProviderContext) { const agent = new Http.Agent({ keepAlive: true, }); - const { protocol, hostname, port } = config.get('servers.opensearchDashboards'); + const { protocol, hostname, port } = Url.parse( + getUrl.baseUrl(config.get('servers.opensearchDashboards')) + ); function performRequest() { return new Promise((resolve, reject) => { diff --git a/test/server_integration/http/ssl_redirect/config.js b/test/server_integration/http/ssl_redirect/config.js index 97f6c04c9bf5..626734ed67ee 100644 --- a/test/server_integration/http/ssl_redirect/config.js +++ b/test/server_integration/http/ssl_redirect/config.js @@ -28,6 +28,7 @@ * under the License. */ +import { format as formatUrl } from 'url'; import { readFileSync } from 'fs'; import { CA_CERT_PATH, OSD_CERT_PATH, OSD_KEY_PATH } from '@osd/dev-utils'; @@ -37,10 +38,7 @@ export default async function ({ readConfigFile }) { const httpConfig = await readConfigFile(require.resolve('../../config')); const certificateAuthorities = [readFileSync(CA_CERT_PATH)]; - const opensearchDashboardsUrl = new URL(httpConfig.get('servers.opensearchDashboards')); - const redirectPort = parseInt(opensearchDashboardsUrl.port, 10) + 1234; - opensearchDashboardsUrl.port = redirectPort; - opensearchDashboardsUrl.protocol = 'http'; + const redirectPort = httpConfig.get('servers.opensearchDashboards.port') + 1234; return { testFiles: [require.resolve('./')], @@ -48,7 +46,12 @@ export default async function ({ readConfigFile }) { ...httpConfig.get('services'), supertest: createOpenSearchDashboardsSupertestProvider({ certificateAuthorities, - opensearchDashboardsUrl: opensearchDashboardsUrl.toString(), + opensearchDashboardsUrl: formatUrl({ + ...httpConfig.get('servers.opensearchDashboards'), + port: redirectPort, + // test with non ssl protocol + protocol: 'http', + }), }), }, servers: { diff --git a/test/server_integration/services/supertest.js b/test/server_integration/services/supertest.js index 0fc453103f44..0667f1fb1a82 100644 --- a/test/server_integration/services/supertest.js +++ b/test/server_integration/services/supertest.js @@ -28,6 +28,8 @@ * under the License. */ +import { format as formatUrl } from 'url'; + import supertestAsPromised from 'supertest-as-promised'; export function createOpenSearchDashboardsSupertestProvider({ @@ -37,7 +39,7 @@ export function createOpenSearchDashboardsSupertestProvider({ return function ({ getService }) { const config = getService('config'); opensearchDashboardsUrl = - opensearchDashboardsUrl ?? config.get('servers.opensearchDashboards').toString(); + opensearchDashboardsUrl ?? formatUrl(config.get('servers.opensearchDashboards')); return certificateAuthorities ? supertestAsPromised.agent(opensearchDashboardsUrl, { ca: certificateAuthorities }) @@ -47,14 +49,18 @@ export function createOpenSearchDashboardsSupertestProvider({ export function OpenSearchDashboardsSupertestWithoutAuthProvider({ getService }) { const config = getService('config'); - const opensearchDashboardsServerUrl = config.get('servers.opensearchDashboards'); - opensearchDashboardsServerUrl.username = ''; - opensearchDashboardsServerUrl.password = ''; - return supertestAsPromised(opensearchDashboardsServerUrl.toString()); + const opensearchDashboardsServerConfig = config.get('servers.opensearchDashboards'); + + return supertestAsPromised( + formatUrl({ + ...opensearchDashboardsServerConfig, + auth: false, + }) + ); } export function OpenSearchSupertestProvider({ getService }) { const config = getService('config'); - const elasticSearchServerUrl = config.get('servers.opensearch').toString(); + const elasticSearchServerUrl = formatUrl(config.get('servers.opensearch')); return supertestAsPromised(elasticSearchServerUrl); } From a2d71088bbe2d34fe90f9329aaa6367bab3de85a Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Thu, 9 Feb 2023 20:12:14 +0800 Subject: [PATCH 37/47] feat: add fullURL to urlPartsSchema and use fullURL generate url string Signed-off-by: Lin Wang --- packages/osd-opensearch-archiver/src/cli.ts | 7 ++-- .../lib/config/config.ts | 8 ++++ .../lib/config/schema.ts | 1 + .../legacy_opensearch_test_cluster.js | 7 ++-- .../opensearch_test_config.js | 23 ++++++----- packages/osd-test/src/osd/osd_test_config.ts | 30 +++++++++----- src/test_utils/get_url.js | 28 +++++++------ src/test_utils/get_url.test.ts | 39 +++++-------------- test/api_integration/services/supertest.ts | 7 ++-- test/common/config.js | 3 +- test/common/services/deployment.ts | 4 +- test/common/services/legacy_opensearch.ts | 4 +- test/common/services/opensearch.ts | 5 +-- test/common/services/opensearch_archiver.ts | 3 +- .../opensearch_dashboards_server.ts | 3 +- test/common/services/security/test_user.ts | 14 +++---- test/functional/page_objects/common_page.ts | 6 +-- test/functional/services/supertest.ts | 5 ++- .../http/platform/headers.ts | 5 +-- .../http/ssl_redirect/config.js | 11 ++---- test/server_integration/services/supertest.js | 19 ++++----- 21 files changed, 112 insertions(+), 120 deletions(-) diff --git a/packages/osd-opensearch-archiver/src/cli.ts b/packages/osd-opensearch-archiver/src/cli.ts index 39dbbb264cd7..a1d7ecb50e5d 100644 --- a/packages/osd-opensearch-archiver/src/cli.ts +++ b/packages/osd-opensearch-archiver/src/cli.ts @@ -35,7 +35,6 @@ *************************************************************/ import Path from 'path'; -import Url from 'url'; import readline from 'readline'; import { RunWithCommands, createFlagError } from '@osd/dev-utils'; @@ -72,7 +71,7 @@ export function runCli() { throw createFlagError('--opensearch-url must be a string'); } if (!opensearchUrl && config) { - opensearchUrl = Url.format(config.get('servers.opensearch')); + opensearchUrl = config.get('servers.opensearch').fullURL.toString(); } if (!opensearchUrl) { throw createFlagError('--opensearch-url or --config must be defined'); @@ -83,7 +82,9 @@ export function runCli() { throw createFlagError('--opensearch-dashboards-url must be a string'); } if (!opensearchDashboardsUrl && config) { - opensearchDashboardsUrl = Url.format(config.get('servers.opensearchDashboards')); + opensearchDashboardsUrl = config + .get('servers.opensearchDashboards') + .fullURL.toString() as string; } if (!opensearchDashboardsUrl) { throw createFlagError('---url or --config must be defined'); diff --git a/packages/osd-test/src/functional_test_runner/lib/config/config.ts b/packages/osd-test/src/functional_test_runner/lib/config/config.ts index 3fd162bef29b..6aba4b29f7b4 100644 --- a/packages/osd-test/src/functional_test_runner/lib/config/config.ts +++ b/packages/osd-test/src/functional_test_runner/lib/config/config.ts @@ -126,6 +126,10 @@ export class Config { if (typeof v === 'function') { return v; } + + if (v instanceof URL) { + return new URL(v.toString()); + } }); } @@ -134,6 +138,10 @@ export class Config { if (typeof v === 'function') { return v; } + + if (v instanceof URL) { + return new URL(v.toString()); + } }); } } diff --git a/packages/osd-test/src/functional_test_runner/lib/config/schema.ts b/packages/osd-test/src/functional_test_runner/lib/config/schema.ts index 447db1e97874..e127edca95f7 100644 --- a/packages/osd-test/src/functional_test_runner/lib/config/schema.ts +++ b/packages/osd-test/src/functional_test_runner/lib/config/schema.ts @@ -50,6 +50,7 @@ const urlPartsSchema = () => pathname: Joi.string().regex(/^\//, 'start with a /'), hash: Joi.string().regex(/^\//, 'start with a /'), certificateAuthorities: Joi.array().items(Joi.binary()).optional(), + fullURL: Joi.object().type(URL), }) .default(); diff --git a/packages/osd-test/src/legacy_opensearch/legacy_opensearch_test_cluster.js b/packages/osd-test/src/legacy_opensearch/legacy_opensearch_test_cluster.js index 72506099dfe4..9af9b4645879 100644 --- a/packages/osd-test/src/legacy_opensearch/legacy_opensearch_test_cluster.js +++ b/packages/osd-test/src/legacy_opensearch/legacy_opensearch_test_cluster.js @@ -29,7 +29,6 @@ */ import { resolve } from 'path'; -import { format } from 'url'; import { get, toPath } from 'lodash'; import { Cluster } from '@osd/opensearch'; import { CI_PARALLEL_PROCESS_PREFIX } from '../ci_parallel_process_prefix'; @@ -135,10 +134,10 @@ export function createLegacyOpenSearchTestCluster(options = {}) { } getUrl() { - const parts = opensearchTestConfig.getUrlParts(); - parts.port = port; + const url = new URL(opensearchTestConfig.getUrlParts().fullURL); + url.port = port; - return format(parts); + return url.toString(); } })(); } diff --git a/packages/osd-test/src/legacy_opensearch/opensearch_test_config.js b/packages/osd-test/src/legacy_opensearch/opensearch_test_config.js index 7befa4226afd..a8dd1a69fd53 100644 --- a/packages/osd-test/src/legacy_opensearch/opensearch_test_config.js +++ b/packages/osd-test/src/legacy_opensearch/opensearch_test_config.js @@ -28,7 +28,6 @@ * under the License. */ -import url, { format as formatUrl } from 'url'; import pkg from '../../../../package.json'; import { adminTestUser } from '../osd'; @@ -42,7 +41,7 @@ export const opensearchTestConfig = new (class OpenSearchTestConfig { } getUrl() { - return formatUrl(this.getUrlParts()); + return this.getUrlParts().fullURL.toString(); } getBuildFrom() { @@ -56,30 +55,36 @@ export const opensearchTestConfig = new (class OpenSearchTestConfig { getUrlParts() { // Allow setting one complete TEST_OPENSEARCH_URL for opensearch like https://opensearch:changeme@example.com:9200 if (process.env.TEST_OPENSEARCH_URL) { - const testOpenSearchUrl = url.parse(process.env.TEST_OPENSEARCH_URL); + const testOpenSearchUrl = new URL('', process.env.TEST_OPENSEARCH_URL); return { // have to remove the ":" off protocol protocol: testOpenSearchUrl.protocol.slice(0, -1), hostname: testOpenSearchUrl.hostname, port: parseInt(testOpenSearchUrl.port, 10), - username: testOpenSearchUrl.auth.split(':')[0], - password: testOpenSearchUrl.auth.split(':')[1], - auth: testOpenSearchUrl.auth, + username: testOpenSearchUrl.username, + password: testOpenSearchUrl.password, + auth: `${testOpenSearchUrl.username}:${testOpenSearchUrl.password}`, + fullURL: testOpenSearchUrl, }; } const username = process.env.TEST_OPENSEARCH_USERNAME || adminTestUser.username; const password = process.env.TEST_OPENSEARCH_PASSWORD || adminTestUser.password; + const protocol = process.env.TEST_OPENSEARCH_PROTOCOL || 'http'; + const hostname = process.env.TEST_OPENSEARCH_HOSTNAME || 'localhost'; + const port = parseInt(process.env.TEST_OPENSEARCH_PORT, 10) || 9220; + const fullURL = new URL('', `${protocol}://${username}:${password}@${hostname}:${port}`); return { // Allow setting any individual component(s) of the URL, // or use default values (username and password from ../osd/users.js) - protocol: process.env.TEST_OPENSEARCH_PROTOCOL || 'http', - hostname: process.env.TEST_OPENSEARCH_HOSTNAME || 'localhost', - port: parseInt(process.env.TEST_OPENSEARCH_PORT, 10) || 9220, + protocol, + hostname, + port, auth: `${username}:${password}`, username: username, password: password, + fullURL, }; } })(); diff --git a/packages/osd-test/src/osd/osd_test_config.ts b/packages/osd-test/src/osd/osd_test_config.ts index 6c3ef573511b..6a25bb675940 100644 --- a/packages/osd-test/src/osd/osd_test_config.ts +++ b/packages/osd-test/src/osd/osd_test_config.ts @@ -28,7 +28,6 @@ * under the License. */ -import url from 'url'; import { opensearchDashboardsTestUser } from './users'; interface UrlParts { @@ -38,6 +37,7 @@ interface UrlParts { auth?: string; username?: string; password?: string; + fullURL: URL; } export const osdTestConfig = new (class OsdTestConfig { @@ -48,16 +48,20 @@ export const osdTestConfig = new (class OsdTestConfig { getUrlParts(): UrlParts { // allow setting one complete TEST_OPENSEARCH_DASHBOARDS_URL for opensearch like https://opensearch:changeme@example.com:9200 if (process.env.TEST_OPENSEARCH_DASHBOARDS_URL) { - const testOpenSearchDashboardsUrl = url.parse(process.env.TEST_OPENSEARCH_DASHBOARDS_URL); + const testOpenSearchDashboardsUrl = new URL('', process.env.TEST_OPENSEARCH_DASHBOARDS_URL); return { protocol: testOpenSearchDashboardsUrl.protocol?.slice(0, -1), hostname: testOpenSearchDashboardsUrl.hostname ?? undefined, port: testOpenSearchDashboardsUrl.port ? parseInt(testOpenSearchDashboardsUrl.port, 10) : undefined, - auth: testOpenSearchDashboardsUrl.auth ?? undefined, - username: testOpenSearchDashboardsUrl.auth?.split(':')[0], - password: testOpenSearchDashboardsUrl.auth?.split(':')[1], + auth: + testOpenSearchDashboardsUrl.username && testOpenSearchDashboardsUrl.password + ? `${testOpenSearchDashboardsUrl.username}:${testOpenSearchDashboardsUrl.password}` + : undefined, + username: testOpenSearchDashboardsUrl.username ?? undefined, + password: testOpenSearchDashboardsUrl.password ?? undefined, + fullURL: testOpenSearchDashboardsUrl, }; } @@ -65,15 +69,21 @@ export const osdTestConfig = new (class OsdTestConfig { process.env.TEST_OPENSEARCH_DASHBOARDS_USERNAME || opensearchDashboardsTestUser.username; const password = process.env.TEST_OPENSEARCH_DASHBOARDS_PASSWORD || opensearchDashboardsTestUser.password; + const protocol = process.env.TEST_OPENSEARCH_DASHBOARDS_PROTOCOL || 'http'; + const hostname = process.env.TEST_OPENSEARCH_DASHBOARDS_HOSTNAME || 'localhost'; + const port = process.env.TEST_OPENSEARCH_DASHBOARDS_PORT + ? parseInt(process.env.TEST_OPENSEARCH_DASHBOARDS_PORT, 10) + : 5620; + const fullURL = new URL(`${protocol}://${username}:${password}@${hostname}:${port}`); + return { - protocol: process.env.TEST_OPENSEARCH_DASHBOARDS_PROTOCOL || 'http', - hostname: process.env.TEST_OPENSEARCH_DASHBOARDS_HOSTNAME || 'localhost', - port: process.env.TEST_OPENSEARCH_DASHBOARDS_PORT - ? parseInt(process.env.TEST_OPENSEARCH_DASHBOARDS_PORT, 10) - : 5620, + protocol, + hostname, + port, auth: `${username}:${password}`, username, password, + fullURL, }; } })(); diff --git a/src/test_utils/get_url.js b/src/test_utils/get_url.js index 3b25d2bc626b..5168bdb08171 100644 --- a/src/test_utils/get_url.js +++ b/src/test_utils/get_url.js @@ -28,9 +28,6 @@ * under the License. */ -import _ from 'lodash'; -import url from 'url'; - /** * Converts a config and a pathname to a url * @param {object} config A url config @@ -50,17 +47,24 @@ import url from 'url'; * @return {string} */ -export default function getUrl(config, app) { - return url.format(_.assign({}, config, app)); +export default function getUrl(url, app) { + url = new URL(url); + if (app.pathname) { + url.pathname = app.pathname; + } + if (app.hash) { + url.hash = app.hash; + } + return url.toString(); } -getUrl.noAuth = function getUrlNoAuth(config, app) { - config = _.pickBy(config, function (val, param) { - return param !== 'auth'; - }); - return getUrl(config, app); +getUrl.noAuth = function getUrlNoAuth(url, app) { + url = new URL(url); + url.username = ''; + url.password = ''; + return getUrl(url, app); }; -getUrl.baseUrl = function getBaseUrl(config) { - return url.format(_.pick(config, 'protocol', 'hostname', 'port')); +getUrl.baseUrl = function getBaseUrl(url) { + return url.origin; }; diff --git a/src/test_utils/get_url.test.ts b/src/test_utils/get_url.test.ts index c784c9c60b95..59cf2c3b22e1 100644 --- a/src/test_utils/get_url.test.ts +++ b/src/test_utils/get_url.test.ts @@ -33,46 +33,27 @@ import getUrl from './get_url'; describe('getUrl', function () { it('should convert to a url', function () { - const url = getUrl( - { - protocol: 'http', - hostname: 'localhost', - }, - { - pathname: 'foo', - } - ); + const url = getUrl(new URL('http://localhost'), { + pathname: 'foo', + }); expect(url).to.be('http://localhost/foo'); }); it('should convert to a url with port', function () { - const url = getUrl( - { - protocol: 'http', - hostname: 'localhost', - port: 9220, - }, - { - pathname: 'foo', - } - ); + const url = getUrl(new URL('http://localhost:9220'), { + pathname: 'foo', + }); expect(url).to.be('http://localhost:9220/foo'); }); it('should convert to a secure hashed url', function () { expect( - getUrl( - { - protocol: 'https', - hostname: 'localhost', - }, - { - pathname: 'foo', - hash: 'bar', - } - ) + getUrl(new URL('https://localhost'), { + pathname: 'foo', + hash: 'bar', + }) ).to.be('https://localhost/foo#bar'); }); }); diff --git a/test/api_integration/services/supertest.ts b/test/api_integration/services/supertest.ts index 369c25902b11..19fdd68571a6 100644 --- a/test/api_integration/services/supertest.ts +++ b/test/api_integration/services/supertest.ts @@ -29,18 +29,19 @@ */ import { FtrProviderContext } from 'test/functional/ftr_provider_context'; -import { format as formatUrl } from 'url'; import supertestAsPromised from 'supertest-as-promised'; export function OpenSearchDashboardsSupertestProvider({ getService }: FtrProviderContext) { const config = getService('config'); - const opensearchDashboardsServerUrl = formatUrl(config.get('servers.opensearchDashboards')); + const opensearchDashboardsServerUrl = config + .get('servers.opensearchDashboards') + .fullURL.toString(); return supertestAsPromised(opensearchDashboardsServerUrl); } export function OpenSearchSupertestProvider({ getService }: FtrProviderContext) { const config = getService('config'); - const elasticSearchServerUrl = formatUrl(config.get('servers.opensearch')); + const elasticSearchServerUrl = config.get('servers.opensearch').fullURL.toString(); return supertestAsPromised(elasticSearchServerUrl); } diff --git a/test/common/config.js b/test/common/config.js index 3d5ea8852751..e441b29f0234 100644 --- a/test/common/config.js +++ b/test/common/config.js @@ -29,7 +29,6 @@ */ //import path from 'path'; -import { format as formatUrl } from 'url'; import { opensearchTestConfig, osdTestConfig, opensearchDashboardsServerTestUser } from '@osd/test'; import { services } from './services'; @@ -55,7 +54,7 @@ export default function () { '--logging.json=false', `--server.port=${osdTestConfig.getPort()}`, '--status.allowAnonymous=true', - `--opensearch.hosts=${formatUrl(servers.opensearch)}`, + `--opensearch.hosts=${servers.opensearch.fullURL.toString()}`, `--opensearch.username=${opensearchDashboardsServerTestUser.username}`, `--opensearch.password=${opensearchDashboardsServerTestUser.password}`, `--home.disableWelcomeScreen=false`, diff --git a/test/common/services/deployment.ts b/test/common/services/deployment.ts index d28ed55a2bb3..ebf94108f21c 100644 --- a/test/common/services/deployment.ts +++ b/test/common/services/deployment.ts @@ -44,14 +44,14 @@ export function DeploymentProvider({ getService }: FtrProviderContext) { * Returns OpenSearch Dashboards host URL */ getHostPort() { - return getUrl.baseUrl(config.get('servers.opensearchDashboards')); + return getUrl.baseUrl(config.get('servers.opensearchDashboards.fullURL')); }, /** * Returns OpenSearch host URL */ getOpenSearchHostPort() { - return getUrl.baseUrl(config.get('servers.opensearch')); + return getUrl.baseUrl(config.get('servers.opensearch.fullURL')); }, /** diff --git a/test/common/services/legacy_opensearch.ts b/test/common/services/legacy_opensearch.ts index 597a55a79f3b..9368119abd73 100644 --- a/test/common/services/legacy_opensearch.ts +++ b/test/common/services/legacy_opensearch.ts @@ -28,8 +28,6 @@ * under the License. */ -import { format as formatUrl } from 'url'; - import * as legacyOpenSearch from 'elasticsearch'; import { DEFAULT_API_VERSION } from '../../../src/core/server/opensearch/opensearch_config'; @@ -42,7 +40,7 @@ export function LegacyOpenSearchProvider({ return new legacyOpenSearch.Client({ apiVersion: DEFAULT_API_VERSION, - host: formatUrl(config.get('servers.opensearch')), + host: config.get('servers.opensearch.fullURL').toString(), requestTimeout: config.get('timeouts.opensearchRequestTimeout'), }); } diff --git a/test/common/services/opensearch.ts b/test/common/services/opensearch.ts index b162ee961f6d..956cd31e7834 100644 --- a/test/common/services/opensearch.ts +++ b/test/common/services/opensearch.ts @@ -28,7 +28,6 @@ * under the License. */ -import { format as formatUrl } from 'url'; import fs from 'fs'; import { Client } from '@opensearch-project/opensearch'; import { CA_CERT_PATH } from '@osd/dev-utils'; @@ -40,7 +39,7 @@ export function OpenSearchProvider({ getService }: FtrProviderContext) { if (process.env.TEST_CLOUD) { return new Client({ - nodes: [formatUrl(config.get('servers.opensearch'))], + nodes: [config.get('servers.opensearch.fullURL').toString()], requestTimeout: config.get('timeouts.opensearchRequestTimeout'), }); } else { @@ -48,7 +47,7 @@ export function OpenSearchProvider({ getService }: FtrProviderContext) { ssl: { ca: fs.readFileSync(CA_CERT_PATH, 'utf-8'), }, - nodes: [formatUrl(config.get('servers.opensearch'))], + nodes: [config.get('servers.opensearch.fullURL').toString()], requestTimeout: config.get('timeouts.opensearchRequestTimeout'), }); } diff --git a/test/common/services/opensearch_archiver.ts b/test/common/services/opensearch_archiver.ts index eb36c9e5a1d5..4c2454dc3b6c 100644 --- a/test/common/services/opensearch_archiver.ts +++ b/test/common/services/opensearch_archiver.ts @@ -28,7 +28,6 @@ * under the License. */ -import { format as formatUrl } from 'url'; import { OpenSearchArchiver } from '@osd/opensearch-archiver'; import { FtrProviderContext } from '../ftr_provider_context'; @@ -55,7 +54,7 @@ export function OpenSearchArchiverProvider({ client, dataDir, log, - opensearchDashboardsUrl: formatUrl(config.get('servers.opensearchDashboards')), + opensearchDashboardsUrl: config.get('servers.opensearchDashboards.fullURL').toString(), }); if (hasService('opensearchDashboardsServer')) { diff --git a/test/common/services/opensearch_dashboards_server/opensearch_dashboards_server.ts b/test/common/services/opensearch_dashboards_server/opensearch_dashboards_server.ts index c989bbff9caa..bffeab6afa07 100644 --- a/test/common/services/opensearch_dashboards_server/opensearch_dashboards_server.ts +++ b/test/common/services/opensearch_dashboards_server/opensearch_dashboards_server.ts @@ -28,7 +28,6 @@ * under the License. */ -import Url from 'url'; import { OsdClient } from '@osd/dev-utils'; import { FtrProviderContext } from '../../ftr_provider_context'; @@ -37,7 +36,7 @@ export function OpenSearchDashboardsServerProvider({ getService }: FtrProviderCo const log = getService('log'); const config = getService('config'); const lifecycle = getService('lifecycle'); - const url = Url.format(config.get('servers.opensearchDashboards')); + const url = config.get('servers.opensearchDashboards.fullURL').toString(); const defaults = config.get('uiSettings.defaults'); const osd = new OsdClient({ log, diff --git a/test/common/services/security/test_user.ts b/test/common/services/security/test_user.ts index 8cb7b14af24e..21a3b0076641 100644 --- a/test/common/services/security/test_user.ts +++ b/test/common/services/security/test_user.ts @@ -28,7 +28,6 @@ * under the License. */ -import { format as formatUrl } from 'url'; import supertestAsPromised from 'supertest-as-promised'; import { Role } from './role'; @@ -114,12 +113,11 @@ export async function createTestUserService( export function TestUserSupertestProvider({ getService }: FtrProviderContext) { const config = getService('config'); - const opensearchDashboardsServerConfig = config.get('servers.opensearchDashboards'); - - return supertestAsPromised( - formatUrl({ - ...opensearchDashboardsServerConfig, - auth: `${TEST_USER_NAME}:${TEST_USER_PASSWORD}`, - }) + const opensearchDashboardsServerConfig = new URL( + config.get('servers.opensearchDashboards.fullURL') ); + opensearchDashboardsServerConfig.username = TEST_USER_NAME; + opensearchDashboardsServerConfig.password = TEST_USER_PASSWORD; + + return supertestAsPromised(opensearchDashboardsServerConfig.toString()); } diff --git a/test/functional/page_objects/common_page.ts b/test/functional/page_objects/common_page.ts index 1a303dab14dc..b770ca0de7f0 100644 --- a/test/functional/page_objects/common_page.ts +++ b/test/functional/page_objects/common_page.ts @@ -105,7 +105,7 @@ export function CommonPageProvider({ getService, getPageObjects }: FtrProviderCo useActualUrl, insertTimestamp, } = navigateProps; - const appUrl = getUrl.noAuth(config.get('servers.opensearchDashboards'), appConfig); + const appUrl = getUrl.noAuth(config.get('servers.opensearchDashboards.fullURL'), appConfig); await retry.try(async () => { if (useActualUrl) { @@ -234,12 +234,12 @@ export function CommonPageProvider({ getService, getPageObjects }: FtrProviderCo if (config.has(['apps', appName])) { // Legacy applications const appConfig = config.get(['apps', appName]); - appUrl = getUrl.noAuth(config.get('servers.opensearchDashboards'), { + appUrl = getUrl.noAuth(config.get('servers.opensearchDashboards.fullURL'), { pathname: `${basePath}${appConfig.pathname}`, hash: hash || appConfig.hash, }); } else { - appUrl = getUrl.noAuth(config.get('servers.opensearchDashboards'), { + appUrl = getUrl.noAuth(config.get('servers.opensearchDashboards.fullURL'), { pathname: `${basePath}/app/${appName}`, hash, }); diff --git a/test/functional/services/supertest.ts b/test/functional/services/supertest.ts index cc33c618be52..a11623c5f810 100644 --- a/test/functional/services/supertest.ts +++ b/test/functional/services/supertest.ts @@ -29,12 +29,13 @@ */ import { FtrProviderContext } from 'test/functional/ftr_provider_context'; -import { format as formatUrl } from 'url'; import supertestAsPromised from 'supertest-as-promised'; export function OpenSearchDashboardsSupertestProvider({ getService }: FtrProviderContext) { const config = getService('config'); - const opensearchDashboardsServerUrl = formatUrl(config.get('servers.opensearchDashboards')); + const opensearchDashboardsServerUrl = config + .get('servers.opensearchDashboards.fullURL') + .toString(); return supertestAsPromised(opensearchDashboardsServerUrl); } diff --git a/test/server_integration/http/platform/headers.ts b/test/server_integration/http/platform/headers.ts index dadabe1e6058..2383621bfa0c 100644 --- a/test/server_integration/http/platform/headers.ts +++ b/test/server_integration/http/platform/headers.ts @@ -29,7 +29,6 @@ */ import Http from 'http'; -import Url from 'url'; import { FtrProviderContext } from '../../services/types'; // @ts-ignore @@ -47,9 +46,7 @@ export default function ({ getService }: FtrProviderContext) { const agent = new Http.Agent({ keepAlive: true, }); - const { protocol, hostname, port } = Url.parse( - getUrl.baseUrl(config.get('servers.opensearchDashboards')) - ); + const { protocol, hostname, port } = config.get('servers.opensearchDashboards'); function performRequest() { return new Promise((resolve, reject) => { diff --git a/test/server_integration/http/ssl_redirect/config.js b/test/server_integration/http/ssl_redirect/config.js index 626734ed67ee..1dc80c3e4abd 100644 --- a/test/server_integration/http/ssl_redirect/config.js +++ b/test/server_integration/http/ssl_redirect/config.js @@ -28,7 +28,6 @@ * under the License. */ -import { format as formatUrl } from 'url'; import { readFileSync } from 'fs'; import { CA_CERT_PATH, OSD_CERT_PATH, OSD_KEY_PATH } from '@osd/dev-utils'; @@ -39,6 +38,9 @@ export default async function ({ readConfigFile }) { const certificateAuthorities = [readFileSync(CA_CERT_PATH)]; const redirectPort = httpConfig.get('servers.opensearchDashboards.port') + 1234; + const opensearchDashboardsUrl = new URL(httpConfig.get('servers.opensearchDashboards.fullURL')); + opensearchDashboardsUrl.port = redirectPort; + opensearchDashboardsUrl.protocol = 'http'; return { testFiles: [require.resolve('./')], @@ -46,12 +48,7 @@ export default async function ({ readConfigFile }) { ...httpConfig.get('services'), supertest: createOpenSearchDashboardsSupertestProvider({ certificateAuthorities, - opensearchDashboardsUrl: formatUrl({ - ...httpConfig.get('servers.opensearchDashboards'), - port: redirectPort, - // test with non ssl protocol - protocol: 'http', - }), + opensearchDashboardsUrl: opensearchDashboardsUrl.toString(), }), }, servers: { diff --git a/test/server_integration/services/supertest.js b/test/server_integration/services/supertest.js index 0667f1fb1a82..1855d18a2e01 100644 --- a/test/server_integration/services/supertest.js +++ b/test/server_integration/services/supertest.js @@ -28,8 +28,6 @@ * under the License. */ -import { format as formatUrl } from 'url'; - import supertestAsPromised from 'supertest-as-promised'; export function createOpenSearchDashboardsSupertestProvider({ @@ -39,7 +37,7 @@ export function createOpenSearchDashboardsSupertestProvider({ return function ({ getService }) { const config = getService('config'); opensearchDashboardsUrl = - opensearchDashboardsUrl ?? formatUrl(config.get('servers.opensearchDashboards')); + opensearchDashboardsUrl ?? config.get('servers.opensearchDashboards.fullURL').toString(); return certificateAuthorities ? supertestAsPromised.agent(opensearchDashboardsUrl, { ca: certificateAuthorities }) @@ -49,18 +47,15 @@ export function createOpenSearchDashboardsSupertestProvider({ export function OpenSearchDashboardsSupertestWithoutAuthProvider({ getService }) { const config = getService('config'); - const opensearchDashboardsServerConfig = config.get('servers.opensearchDashboards'); + const opensearchDashboardsServerURL = config.get('servers.opensearchDashboards.fullURL'); + opensearchDashboardsServerURL.username = ''; + opensearchDashboardsServerURL.password = ''; - return supertestAsPromised( - formatUrl({ - ...opensearchDashboardsServerConfig, - auth: false, - }) - ); + return supertestAsPromised(opensearchDashboardsServerURL.toString()); } export function OpenSearchSupertestProvider({ getService }) { const config = getService('config'); - const elasticSearchServerUrl = formatUrl(config.get('servers.opensearch')); - return supertestAsPromised(elasticSearchServerUrl); + const opensearchServerURL = config.get('servers.opensearch.fullURL'); + return supertestAsPromised(opensearchServerURL.toString()); } From 32ec422955eac6fcacabf4488b251ea20a58367e Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Thu, 9 Feb 2023 21:07:50 +0800 Subject: [PATCH 38/47] fix: change getURLParts to getUrlParts Signed-off-by: Lin Wang --- test/common/config.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/common/config.js b/test/common/config.js index e441b29f0234..1538480e704f 100644 --- a/test/common/config.js +++ b/test/common/config.js @@ -34,8 +34,8 @@ import { services } from './services'; export default function () { const servers = { - opensearchDashboards: osdTestConfig.getURLParts(), - opensearch: opensearchTestConfig.getURLParts(), + opensearchDashboards: osdTestConfig.getUrlParts(), + opensearch: opensearchTestConfig.getUrlParts(), }; return { From 27529b14eabe59b5d037a7ef3a6d309ee6b3071d Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Thu, 9 Feb 2023 22:38:15 +0800 Subject: [PATCH 39/47] fix: url parts pass to getUrl.noAuth in test/functional/page_objects/context_page.ts Signed-off-by: Lin Wang --- test/functional/page_objects/context_page.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/functional/page_objects/context_page.ts b/test/functional/page_objects/context_page.ts index ea6a56eb18f4..30f14fba2387 100644 --- a/test/functional/page_objects/context_page.ts +++ b/test/functional/page_objects/context_page.ts @@ -51,7 +51,7 @@ export function ContextPageProvider({ getService, getPageObjects }: FtrProviderC ...DEFAULT_INITIAL_STATE, ...overrideInitialState, }); - const appUrl = getUrl.noAuth(config.get('servers.opensearchDashboards'), { + const appUrl = getUrl.noAuth(config.get('servers.opensearchDashboards.fullURL'), { ...config.get('apps.context'), hash: `${config.get('apps.context.hash')}/${indexPattern}/${anchorId}?_a=${initialState}`, }); From 803c96a70d56d47b534a441edf3de5b445b8885a Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Fri, 10 Feb 2023 12:09:01 +0800 Subject: [PATCH 40/47] fix: colon suffix missed in protocol Signed-off-by: Lin Wang --- test/server_integration/http/platform/headers.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/server_integration/http/platform/headers.ts b/test/server_integration/http/platform/headers.ts index 2383621bfa0c..a714624e6bb8 100644 --- a/test/server_integration/http/platform/headers.ts +++ b/test/server_integration/http/platform/headers.ts @@ -52,7 +52,7 @@ export default function ({ getService }: FtrProviderContext) { return new Promise((resolve, reject) => { const req = Http.request( { - protocol, + protocol: `${protocol}:`, hostname, port, path: '/', From 63a8e87b93eb1bcf33369f0a8011a16c6a2cf0ea Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Fri, 10 Feb 2023 12:12:02 +0800 Subject: [PATCH 41/47] fix: remove all slash suffix for fullURL toString Signed-off-by: Lin Wang --- packages/osd-opensearch-archiver/src/cli.ts | 5 +++-- .../legacy_opensearch/legacy_opensearch_test_cluster.js | 2 +- .../src/legacy_opensearch/opensearch_test_config.js | 3 ++- packages/osd-test/src/osd/osd_test_config.ts | 1 + test/api_integration/services/supertest.ts | 5 +++-- test/common/config.js | 2 +- test/common/services/legacy_opensearch.ts | 5 ++++- test/common/services/opensearch.ts | 4 ++-- test/common/services/opensearch_archiver.ts | 5 ++++- .../opensearch_dashboards_server.ts | 2 +- test/common/services/security/test_user.ts | 2 +- test/functional/services/supertest.ts | 3 ++- test/server_integration/http/ssl_redirect/config.js | 2 +- test/server_integration/services/supertest.js | 7 ++++--- 14 files changed, 30 insertions(+), 18 deletions(-) diff --git a/packages/osd-opensearch-archiver/src/cli.ts b/packages/osd-opensearch-archiver/src/cli.ts index a1d7ecb50e5d..6a021015410e 100644 --- a/packages/osd-opensearch-archiver/src/cli.ts +++ b/packages/osd-opensearch-archiver/src/cli.ts @@ -71,7 +71,7 @@ export function runCli() { throw createFlagError('--opensearch-url must be a string'); } if (!opensearchUrl && config) { - opensearchUrl = config.get('servers.opensearch').fullURL.toString(); + opensearchUrl = config.get('servers.opensearch').fullURL.toString().slice(0, -1); } if (!opensearchUrl) { throw createFlagError('--opensearch-url or --config must be defined'); @@ -84,7 +84,8 @@ export function runCli() { if (!opensearchDashboardsUrl && config) { opensearchDashboardsUrl = config .get('servers.opensearchDashboards') - .fullURL.toString() as string; + .fullURL.toString() + .slice(0, -1) as string; } if (!opensearchDashboardsUrl) { throw createFlagError('---url or --config must be defined'); diff --git a/packages/osd-test/src/legacy_opensearch/legacy_opensearch_test_cluster.js b/packages/osd-test/src/legacy_opensearch/legacy_opensearch_test_cluster.js index 9af9b4645879..07fbd4cdb1de 100644 --- a/packages/osd-test/src/legacy_opensearch/legacy_opensearch_test_cluster.js +++ b/packages/osd-test/src/legacy_opensearch/legacy_opensearch_test_cluster.js @@ -137,7 +137,7 @@ export function createLegacyOpenSearchTestCluster(options = {}) { const url = new URL(opensearchTestConfig.getUrlParts().fullURL); url.port = port; - return url.toString(); + return url.toString().slice(0, -1); } })(); } diff --git a/packages/osd-test/src/legacy_opensearch/opensearch_test_config.js b/packages/osd-test/src/legacy_opensearch/opensearch_test_config.js index a8dd1a69fd53..97c4a6654e83 100644 --- a/packages/osd-test/src/legacy_opensearch/opensearch_test_config.js +++ b/packages/osd-test/src/legacy_opensearch/opensearch_test_config.js @@ -41,7 +41,7 @@ export const opensearchTestConfig = new (class OpenSearchTestConfig { } getUrl() { - return this.getUrlParts().fullURL.toString(); + return this.getUrlParts().fullURL.toString().slice(0, -1); } getBuildFrom() { @@ -56,6 +56,7 @@ export const opensearchTestConfig = new (class OpenSearchTestConfig { // Allow setting one complete TEST_OPENSEARCH_URL for opensearch like https://opensearch:changeme@example.com:9200 if (process.env.TEST_OPENSEARCH_URL) { const testOpenSearchUrl = new URL('', process.env.TEST_OPENSEARCH_URL); + testOpenSearchUrl.pathname = ''; return { // have to remove the ":" off protocol protocol: testOpenSearchUrl.protocol.slice(0, -1), diff --git a/packages/osd-test/src/osd/osd_test_config.ts b/packages/osd-test/src/osd/osd_test_config.ts index 6a25bb675940..6f1512cb5a77 100644 --- a/packages/osd-test/src/osd/osd_test_config.ts +++ b/packages/osd-test/src/osd/osd_test_config.ts @@ -49,6 +49,7 @@ export const osdTestConfig = new (class OsdTestConfig { // allow setting one complete TEST_OPENSEARCH_DASHBOARDS_URL for opensearch like https://opensearch:changeme@example.com:9200 if (process.env.TEST_OPENSEARCH_DASHBOARDS_URL) { const testOpenSearchDashboardsUrl = new URL('', process.env.TEST_OPENSEARCH_DASHBOARDS_URL); + testOpenSearchDashboardsUrl.pathname = ''; return { protocol: testOpenSearchDashboardsUrl.protocol?.slice(0, -1), hostname: testOpenSearchDashboardsUrl.hostname ?? undefined, diff --git a/test/api_integration/services/supertest.ts b/test/api_integration/services/supertest.ts index 19fdd68571a6..f9adb00b447e 100644 --- a/test/api_integration/services/supertest.ts +++ b/test/api_integration/services/supertest.ts @@ -36,12 +36,13 @@ export function OpenSearchDashboardsSupertestProvider({ getService }: FtrProvide const config = getService('config'); const opensearchDashboardsServerUrl = config .get('servers.opensearchDashboards') - .fullURL.toString(); + .fullURL.toString() + .slice(0, -1); return supertestAsPromised(opensearchDashboardsServerUrl); } export function OpenSearchSupertestProvider({ getService }: FtrProviderContext) { const config = getService('config'); - const elasticSearchServerUrl = config.get('servers.opensearch').fullURL.toString(); + const elasticSearchServerUrl = config.get('servers.opensearch').fullURL.toString().slice(0, -1); return supertestAsPromised(elasticSearchServerUrl); } diff --git a/test/common/config.js b/test/common/config.js index 1538480e704f..68c3119fa6e7 100644 --- a/test/common/config.js +++ b/test/common/config.js @@ -54,7 +54,7 @@ export default function () { '--logging.json=false', `--server.port=${osdTestConfig.getPort()}`, '--status.allowAnonymous=true', - `--opensearch.hosts=${servers.opensearch.fullURL.toString()}`, + `--opensearch.hosts=${servers.opensearch.fullURL.toString().slice(0, -1)}`, `--opensearch.username=${opensearchDashboardsServerTestUser.username}`, `--opensearch.password=${opensearchDashboardsServerTestUser.password}`, `--home.disableWelcomeScreen=false`, diff --git a/test/common/services/legacy_opensearch.ts b/test/common/services/legacy_opensearch.ts index 9368119abd73..0094a5d51a6a 100644 --- a/test/common/services/legacy_opensearch.ts +++ b/test/common/services/legacy_opensearch.ts @@ -40,7 +40,10 @@ export function LegacyOpenSearchProvider({ return new legacyOpenSearch.Client({ apiVersion: DEFAULT_API_VERSION, - host: config.get('servers.opensearch.fullURL').toString(), + host: config + .get('servers.opensearch.fullURL') + .toString() + .slice(0 - 1), requestTimeout: config.get('timeouts.opensearchRequestTimeout'), }); } diff --git a/test/common/services/opensearch.ts b/test/common/services/opensearch.ts index 956cd31e7834..d1ab8b1d2fab 100644 --- a/test/common/services/opensearch.ts +++ b/test/common/services/opensearch.ts @@ -39,7 +39,7 @@ export function OpenSearchProvider({ getService }: FtrProviderContext) { if (process.env.TEST_CLOUD) { return new Client({ - nodes: [config.get('servers.opensearch.fullURL').toString()], + nodes: [config.get('servers.opensearch.fullURL').toString().slice(0, -1)], requestTimeout: config.get('timeouts.opensearchRequestTimeout'), }); } else { @@ -47,7 +47,7 @@ export function OpenSearchProvider({ getService }: FtrProviderContext) { ssl: { ca: fs.readFileSync(CA_CERT_PATH, 'utf-8'), }, - nodes: [config.get('servers.opensearch.fullURL').toString()], + nodes: [config.get('servers.opensearch.fullURL').toString().slice(0, -1)], requestTimeout: config.get('timeouts.opensearchRequestTimeout'), }); } diff --git a/test/common/services/opensearch_archiver.ts b/test/common/services/opensearch_archiver.ts index 4c2454dc3b6c..7c4cc0d918e2 100644 --- a/test/common/services/opensearch_archiver.ts +++ b/test/common/services/opensearch_archiver.ts @@ -54,7 +54,10 @@ export function OpenSearchArchiverProvider({ client, dataDir, log, - opensearchDashboardsUrl: config.get('servers.opensearchDashboards.fullURL').toString(), + opensearchDashboardsUrl: config + .get('servers.opensearchDashboards.fullURL') + .toString() + .slice(0, 1), }); if (hasService('opensearchDashboardsServer')) { diff --git a/test/common/services/opensearch_dashboards_server/opensearch_dashboards_server.ts b/test/common/services/opensearch_dashboards_server/opensearch_dashboards_server.ts index bffeab6afa07..33344404b5c7 100644 --- a/test/common/services/opensearch_dashboards_server/opensearch_dashboards_server.ts +++ b/test/common/services/opensearch_dashboards_server/opensearch_dashboards_server.ts @@ -36,7 +36,7 @@ export function OpenSearchDashboardsServerProvider({ getService }: FtrProviderCo const log = getService('log'); const config = getService('config'); const lifecycle = getService('lifecycle'); - const url = config.get('servers.opensearchDashboards.fullURL').toString(); + const url = config.get('servers.opensearchDashboards.fullURL').toString().slice(0, -1); const defaults = config.get('uiSettings.defaults'); const osd = new OsdClient({ log, diff --git a/test/common/services/security/test_user.ts b/test/common/services/security/test_user.ts index 21a3b0076641..fa361705b2d9 100644 --- a/test/common/services/security/test_user.ts +++ b/test/common/services/security/test_user.ts @@ -119,5 +119,5 @@ export function TestUserSupertestProvider({ getService }: FtrProviderContext) { opensearchDashboardsServerConfig.username = TEST_USER_NAME; opensearchDashboardsServerConfig.password = TEST_USER_PASSWORD; - return supertestAsPromised(opensearchDashboardsServerConfig.toString()); + return supertestAsPromised(opensearchDashboardsServerConfig.toString().slice(0, -1)); } diff --git a/test/functional/services/supertest.ts b/test/functional/services/supertest.ts index a11623c5f810..778e94c1212a 100644 --- a/test/functional/services/supertest.ts +++ b/test/functional/services/supertest.ts @@ -36,6 +36,7 @@ export function OpenSearchDashboardsSupertestProvider({ getService }: FtrProvide const config = getService('config'); const opensearchDashboardsServerUrl = config .get('servers.opensearchDashboards.fullURL') - .toString(); + .toString() + .slice(0, -1); return supertestAsPromised(opensearchDashboardsServerUrl); } diff --git a/test/server_integration/http/ssl_redirect/config.js b/test/server_integration/http/ssl_redirect/config.js index 1dc80c3e4abd..ae24f8de654a 100644 --- a/test/server_integration/http/ssl_redirect/config.js +++ b/test/server_integration/http/ssl_redirect/config.js @@ -48,7 +48,7 @@ export default async function ({ readConfigFile }) { ...httpConfig.get('services'), supertest: createOpenSearchDashboardsSupertestProvider({ certificateAuthorities, - opensearchDashboardsUrl: opensearchDashboardsUrl.toString(), + opensearchDashboardsUrl: opensearchDashboardsUrl.toString().slice(0, -1), }), }, servers: { diff --git a/test/server_integration/services/supertest.js b/test/server_integration/services/supertest.js index 1855d18a2e01..d1c8a8b8265b 100644 --- a/test/server_integration/services/supertest.js +++ b/test/server_integration/services/supertest.js @@ -37,7 +37,8 @@ export function createOpenSearchDashboardsSupertestProvider({ return function ({ getService }) { const config = getService('config'); opensearchDashboardsUrl = - opensearchDashboardsUrl ?? config.get('servers.opensearchDashboards.fullURL').toString(); + opensearchDashboardsUrl ?? + config.get('servers.opensearchDashboards.fullURL').toString().slice(0, -1); return certificateAuthorities ? supertestAsPromised.agent(opensearchDashboardsUrl, { ca: certificateAuthorities }) @@ -51,11 +52,11 @@ export function OpenSearchDashboardsSupertestWithoutAuthProvider({ getService }) opensearchDashboardsServerURL.username = ''; opensearchDashboardsServerURL.password = ''; - return supertestAsPromised(opensearchDashboardsServerURL.toString()); + return supertestAsPromised(opensearchDashboardsServerURL.toString().slice(0, -1)); } export function OpenSearchSupertestProvider({ getService }) { const config = getService('config'); const opensearchServerURL = config.get('servers.opensearch.fullURL'); - return supertestAsPromised(opensearchServerURL.toString()); + return supertestAsPromised(opensearchServerURL.toString().slice(0, -1)); } From 12d2e78eaa5298cb60f0ebf561209ce1687a065e Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Fri, 10 Feb 2023 12:58:27 +0800 Subject: [PATCH 42/47] fix: slice(0, 1) cause invalid URL Signed-off-by: Lin Wang --- test/common/services/opensearch_archiver.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/common/services/opensearch_archiver.ts b/test/common/services/opensearch_archiver.ts index 7c4cc0d918e2..939ca094fb52 100644 --- a/test/common/services/opensearch_archiver.ts +++ b/test/common/services/opensearch_archiver.ts @@ -57,7 +57,7 @@ export function OpenSearchArchiverProvider({ opensearchDashboardsUrl: config .get('servers.opensearchDashboards.fullURL') .toString() - .slice(0, 1), + .slice(0, -1), }); if (hasService('opensearchDashboardsServer')) { From ec8ce6ad1db3bcba34d4587888fa9d98cca26905 Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Fri, 10 Feb 2023 15:11:55 +0800 Subject: [PATCH 43/47] fix: use pared protocol, hostname and port Signed-off-by: Lin Wang --- test/server_integration/http/platform/headers.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/server_integration/http/platform/headers.ts b/test/server_integration/http/platform/headers.ts index a714624e6bb8..d5eb2dc8d547 100644 --- a/test/server_integration/http/platform/headers.ts +++ b/test/server_integration/http/platform/headers.ts @@ -46,15 +46,15 @@ export default function ({ getService }: FtrProviderContext) { const agent = new Http.Agent({ keepAlive: true, }); - const { protocol, hostname, port } = config.get('servers.opensearchDashboards'); + const { protocol, hostname, port } = config.get('servers.opensearchDashboards.fullURL'); function performRequest() { return new Promise((resolve, reject) => { const req = Http.request( { - protocol: `${protocol}:`, + protocol, hostname, - port, + port: parseInt(port, 10), path: '/', method: 'GET', agent, From d8ad24d2cdc941b77582bf491f32178b38e1bf61 Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Fri, 10 Feb 2023 15:12:42 +0800 Subject: [PATCH 44/47] fix: clone new URL and modify username and password Signed-off-by: Lin Wang --- test/server_integration/services/supertest.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/server_integration/services/supertest.js b/test/server_integration/services/supertest.js index d1c8a8b8265b..b75620c93e56 100644 --- a/test/server_integration/services/supertest.js +++ b/test/server_integration/services/supertest.js @@ -48,7 +48,7 @@ export function createOpenSearchDashboardsSupertestProvider({ export function OpenSearchDashboardsSupertestWithoutAuthProvider({ getService }) { const config = getService('config'); - const opensearchDashboardsServerURL = config.get('servers.opensearchDashboards.fullURL'); + const opensearchDashboardsServerURL = new URL(config.get('servers.opensearchDashboards.fullURL')); opensearchDashboardsServerURL.username = ''; opensearchDashboardsServerURL.password = ''; From c74698b1fe71335d401100f690e9895c2622dbdc Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Fri, 10 Feb 2023 16:47:36 +0800 Subject: [PATCH 45/47] fix: slice(0 - 1) cause invalid opensearch host Signed-off-by: Lin Wang --- test/common/services/legacy_opensearch.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/test/common/services/legacy_opensearch.ts b/test/common/services/legacy_opensearch.ts index 0094a5d51a6a..23a31fd15a8c 100644 --- a/test/common/services/legacy_opensearch.ts +++ b/test/common/services/legacy_opensearch.ts @@ -40,10 +40,7 @@ export function LegacyOpenSearchProvider({ return new legacyOpenSearch.Client({ apiVersion: DEFAULT_API_VERSION, - host: config - .get('servers.opensearch.fullURL') - .toString() - .slice(0 - 1), + host: config.get('servers.opensearch.fullURL').toString().slice(0, -1), requestTimeout: config.get('timeouts.opensearchRequestTimeout'), }); } From 6ebdd202ad5a3cfc3ee42f9721c35b9e5ffb9913 Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Tue, 14 Feb 2023 17:19:05 +0800 Subject: [PATCH 46/47] update src/core/public/chrome/ui/header/home_loader.tsx Signed-off-by: Lin Wang --- src/core/public/chrome/ui/header/home_loader.tsx | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/core/public/chrome/ui/header/home_loader.tsx b/src/core/public/chrome/ui/header/home_loader.tsx index 4cd231bdf83e..0083df43ff8b 100644 --- a/src/core/public/chrome/ui/header/home_loader.tsx +++ b/src/core/public/chrome/ui/header/home_loader.tsx @@ -34,7 +34,6 @@ import { i18n } from '@osd/i18n'; import React from 'react'; import useObservable from 'react-use/lib/useObservable'; import { Observable } from 'rxjs'; -import Url from 'url'; import { EuiHeaderSectionItemButton } from '@elastic/eui'; import { ChromeNavLink } from '../..'; import { ChromeBranding } from '../../chrome_service'; @@ -78,11 +77,11 @@ function onClick( } if (forceNavigation) { - const toParsed = Url.parse(anchor.href); - const fromParsed = Url.parse(document.location.href); + const toParsed = new URL(anchor.href); + const fromParsed = new URL(document.location.href); const sameProto = toParsed.protocol === fromParsed.protocol; const sameHost = toParsed.host === fromParsed.host; - const samePath = toParsed.path === fromParsed.path; + const samePath = toParsed.pathname === fromParsed.pathname; if (sameProto && sameHost && samePath) { if (toParsed.hash) { From a846ba14238a071812b5083e47fc2e8cd54574f6 Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Tue, 21 Mar 2023 18:20:10 +0800 Subject: [PATCH 47/47] Add serverUrl property and refactor slice(0, -1) to it Signed-off-by: Lin Wang --- packages/osd-opensearch-archiver/src/cli.ts | 7 ++----- .../src/functional_test_runner/lib/config/schema.ts | 1 + .../src/legacy_opensearch/opensearch_test_config.js | 4 +++- packages/osd-test/src/osd/osd_test_config.ts | 3 +++ test/api_integration/services/supertest.ts | 7 ++----- test/common/config.js | 2 +- test/common/services/legacy_opensearch.ts | 2 +- test/common/services/opensearch.ts | 4 ++-- test/common/services/opensearch_archiver.ts | 5 +---- .../opensearch_dashboards_server.ts | 2 +- test/functional/services/supertest.ts | 5 +---- test/server_integration/services/supertest.js | 6 ++---- 12 files changed, 20 insertions(+), 28 deletions(-) diff --git a/packages/osd-opensearch-archiver/src/cli.ts b/packages/osd-opensearch-archiver/src/cli.ts index 6a021015410e..002e3e287587 100644 --- a/packages/osd-opensearch-archiver/src/cli.ts +++ b/packages/osd-opensearch-archiver/src/cli.ts @@ -71,7 +71,7 @@ export function runCli() { throw createFlagError('--opensearch-url must be a string'); } if (!opensearchUrl && config) { - opensearchUrl = config.get('servers.opensearch').fullURL.toString().slice(0, -1); + opensearchUrl = config.get('servers.opensearch.serverUrl'); } if (!opensearchUrl) { throw createFlagError('--opensearch-url or --config must be defined'); @@ -82,10 +82,7 @@ export function runCli() { throw createFlagError('--opensearch-dashboards-url must be a string'); } if (!opensearchDashboardsUrl && config) { - opensearchDashboardsUrl = config - .get('servers.opensearchDashboards') - .fullURL.toString() - .slice(0, -1) as string; + opensearchDashboardsUrl = config.get('servers.opensearchDashboards.serverUrl') as string; } if (!opensearchDashboardsUrl) { throw createFlagError('---url or --config must be defined'); diff --git a/packages/osd-test/src/functional_test_runner/lib/config/schema.ts b/packages/osd-test/src/functional_test_runner/lib/config/schema.ts index e127edca95f7..720bd0940f87 100644 --- a/packages/osd-test/src/functional_test_runner/lib/config/schema.ts +++ b/packages/osd-test/src/functional_test_runner/lib/config/schema.ts @@ -51,6 +51,7 @@ const urlPartsSchema = () => hash: Joi.string().regex(/^\//, 'start with a /'), certificateAuthorities: Joi.array().items(Joi.binary()).optional(), fullURL: Joi.object().type(URL), + serverUrl: Joi.string(), }) .default(); diff --git a/packages/osd-test/src/legacy_opensearch/opensearch_test_config.js b/packages/osd-test/src/legacy_opensearch/opensearch_test_config.js index 97c4a6654e83..22bb858411fd 100644 --- a/packages/osd-test/src/legacy_opensearch/opensearch_test_config.js +++ b/packages/osd-test/src/legacy_opensearch/opensearch_test_config.js @@ -41,7 +41,7 @@ export const opensearchTestConfig = new (class OpenSearchTestConfig { } getUrl() { - return this.getUrlParts().fullURL.toString().slice(0, -1); + return this.getUrlParts().serverUrl; } getBuildFrom() { @@ -66,6 +66,7 @@ export const opensearchTestConfig = new (class OpenSearchTestConfig { password: testOpenSearchUrl.password, auth: `${testOpenSearchUrl.username}:${testOpenSearchUrl.password}`, fullURL: testOpenSearchUrl, + serverUrl: testOpenSearchUrl.toString().slice(0, -1), }; } @@ -86,6 +87,7 @@ export const opensearchTestConfig = new (class OpenSearchTestConfig { username: username, password: password, fullURL, + serverUrl: fullURL.toString().slice(0, -1), }; } })(); diff --git a/packages/osd-test/src/osd/osd_test_config.ts b/packages/osd-test/src/osd/osd_test_config.ts index 6f1512cb5a77..b5fc179b7a97 100644 --- a/packages/osd-test/src/osd/osd_test_config.ts +++ b/packages/osd-test/src/osd/osd_test_config.ts @@ -38,6 +38,7 @@ interface UrlParts { username?: string; password?: string; fullURL: URL; + serverUrl: string; } export const osdTestConfig = new (class OsdTestConfig { @@ -63,6 +64,7 @@ export const osdTestConfig = new (class OsdTestConfig { username: testOpenSearchDashboardsUrl.username ?? undefined, password: testOpenSearchDashboardsUrl.password ?? undefined, fullURL: testOpenSearchDashboardsUrl, + serverUrl: testOpenSearchDashboardsUrl.toString().slice(0, -1), }; } @@ -85,6 +87,7 @@ export const osdTestConfig = new (class OsdTestConfig { username, password, fullURL, + serverUrl: fullURL.toString().slice(0, -1), }; } })(); diff --git a/test/api_integration/services/supertest.ts b/test/api_integration/services/supertest.ts index f9adb00b447e..4eaf2a2531da 100644 --- a/test/api_integration/services/supertest.ts +++ b/test/api_integration/services/supertest.ts @@ -34,15 +34,12 @@ import supertestAsPromised from 'supertest-as-promised'; export function OpenSearchDashboardsSupertestProvider({ getService }: FtrProviderContext) { const config = getService('config'); - const opensearchDashboardsServerUrl = config - .get('servers.opensearchDashboards') - .fullURL.toString() - .slice(0, -1); + const opensearchDashboardsServerUrl = config.get('servers.opensearchDashboards.serverUrl'); return supertestAsPromised(opensearchDashboardsServerUrl); } export function OpenSearchSupertestProvider({ getService }: FtrProviderContext) { const config = getService('config'); - const elasticSearchServerUrl = config.get('servers.opensearch').fullURL.toString().slice(0, -1); + const elasticSearchServerUrl = config.get('servers.opensearch.serverUrl'); return supertestAsPromised(elasticSearchServerUrl); } diff --git a/test/common/config.js b/test/common/config.js index 68c3119fa6e7..cff91302899d 100644 --- a/test/common/config.js +++ b/test/common/config.js @@ -54,7 +54,7 @@ export default function () { '--logging.json=false', `--server.port=${osdTestConfig.getPort()}`, '--status.allowAnonymous=true', - `--opensearch.hosts=${servers.opensearch.fullURL.toString().slice(0, -1)}`, + `--opensearch.hosts=${servers.opensearch.serverUrl}`, `--opensearch.username=${opensearchDashboardsServerTestUser.username}`, `--opensearch.password=${opensearchDashboardsServerTestUser.password}`, `--home.disableWelcomeScreen=false`, diff --git a/test/common/services/legacy_opensearch.ts b/test/common/services/legacy_opensearch.ts index 23a31fd15a8c..1293171a28ff 100644 --- a/test/common/services/legacy_opensearch.ts +++ b/test/common/services/legacy_opensearch.ts @@ -40,7 +40,7 @@ export function LegacyOpenSearchProvider({ return new legacyOpenSearch.Client({ apiVersion: DEFAULT_API_VERSION, - host: config.get('servers.opensearch.fullURL').toString().slice(0, -1), + host: config.get('servers.opensearch.serverUrl'), requestTimeout: config.get('timeouts.opensearchRequestTimeout'), }); } diff --git a/test/common/services/opensearch.ts b/test/common/services/opensearch.ts index d1ab8b1d2fab..1c1543174c43 100644 --- a/test/common/services/opensearch.ts +++ b/test/common/services/opensearch.ts @@ -39,7 +39,7 @@ export function OpenSearchProvider({ getService }: FtrProviderContext) { if (process.env.TEST_CLOUD) { return new Client({ - nodes: [config.get('servers.opensearch.fullURL').toString().slice(0, -1)], + nodes: [config.get('servers.opensearch.serverUrl')], requestTimeout: config.get('timeouts.opensearchRequestTimeout'), }); } else { @@ -47,7 +47,7 @@ export function OpenSearchProvider({ getService }: FtrProviderContext) { ssl: { ca: fs.readFileSync(CA_CERT_PATH, 'utf-8'), }, - nodes: [config.get('servers.opensearch.fullURL').toString().slice(0, -1)], + nodes: [config.get('servers.opensearch.serverUrl')], requestTimeout: config.get('timeouts.opensearchRequestTimeout'), }); } diff --git a/test/common/services/opensearch_archiver.ts b/test/common/services/opensearch_archiver.ts index 939ca094fb52..3985c7aaf466 100644 --- a/test/common/services/opensearch_archiver.ts +++ b/test/common/services/opensearch_archiver.ts @@ -54,10 +54,7 @@ export function OpenSearchArchiverProvider({ client, dataDir, log, - opensearchDashboardsUrl: config - .get('servers.opensearchDashboards.fullURL') - .toString() - .slice(0, -1), + opensearchDashboardsUrl: config.get('servers.opensearchDashboards.serverUrl'), }); if (hasService('opensearchDashboardsServer')) { diff --git a/test/common/services/opensearch_dashboards_server/opensearch_dashboards_server.ts b/test/common/services/opensearch_dashboards_server/opensearch_dashboards_server.ts index 33344404b5c7..31e75db89a01 100644 --- a/test/common/services/opensearch_dashboards_server/opensearch_dashboards_server.ts +++ b/test/common/services/opensearch_dashboards_server/opensearch_dashboards_server.ts @@ -36,7 +36,7 @@ export function OpenSearchDashboardsServerProvider({ getService }: FtrProviderCo const log = getService('log'); const config = getService('config'); const lifecycle = getService('lifecycle'); - const url = config.get('servers.opensearchDashboards.fullURL').toString().slice(0, -1); + const url = config.get('servers.opensearchDashboards.serverUrl'); const defaults = config.get('uiSettings.defaults'); const osd = new OsdClient({ log, diff --git a/test/functional/services/supertest.ts b/test/functional/services/supertest.ts index 778e94c1212a..c84ff6c0a8fd 100644 --- a/test/functional/services/supertest.ts +++ b/test/functional/services/supertest.ts @@ -34,9 +34,6 @@ import supertestAsPromised from 'supertest-as-promised'; export function OpenSearchDashboardsSupertestProvider({ getService }: FtrProviderContext) { const config = getService('config'); - const opensearchDashboardsServerUrl = config - .get('servers.opensearchDashboards.fullURL') - .toString() - .slice(0, -1); + const opensearchDashboardsServerUrl = config.get('servers.opensearchDashboards.serverUrl'); return supertestAsPromised(opensearchDashboardsServerUrl); } diff --git a/test/server_integration/services/supertest.js b/test/server_integration/services/supertest.js index b75620c93e56..91ab7bcdc54f 100644 --- a/test/server_integration/services/supertest.js +++ b/test/server_integration/services/supertest.js @@ -37,8 +37,7 @@ export function createOpenSearchDashboardsSupertestProvider({ return function ({ getService }) { const config = getService('config'); opensearchDashboardsUrl = - opensearchDashboardsUrl ?? - config.get('servers.opensearchDashboards.fullURL').toString().slice(0, -1); + opensearchDashboardsUrl ?? config.get('servers.opensearchDashboards.serverUrl'); return certificateAuthorities ? supertestAsPromised.agent(opensearchDashboardsUrl, { ca: certificateAuthorities }) @@ -57,6 +56,5 @@ export function OpenSearchDashboardsSupertestWithoutAuthProvider({ getService }) export function OpenSearchSupertestProvider({ getService }) { const config = getService('config'); - const opensearchServerURL = config.get('servers.opensearch.fullURL'); - return supertestAsPromised(opensearchServerURL.toString().slice(0, -1)); + return supertestAsPromised(config.get('servers.opensearch.serverUrl')); }