/* * SPDX-License-Identifier: Apache-2.0 * * The OpenSearch Contributors require contributions made to * this file be licensed under the Apache-2.0 license or a * compatible open source license. * * Any modifications Copyright OpenSearch Contributors. See * GitHub history for details. */ /* * Licensed to Elasticsearch B.V. under one or more contributor * license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright * ownership. Elasticsearch B.V. licenses this file to you under * the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ import React from 'react'; import { renderToStaticMarkup } from 'react-dom/server'; import { first, take } from 'rxjs/operators'; import { i18n } from '@osd/i18n'; import { Agent as HttpsAgent } from 'https'; import Axios from 'axios'; // @ts-expect-error untyped internal module used to prevent axios from using xhr adapter in tests import AxiosHttpAdapter from 'axios/lib/adapters/http'; import { UiPlugins } from '../plugins'; import { CoreContext } from '../core_context'; import { Template } from './views'; import { IRenderOptions, RenderingSetupDeps, InternalRenderingServiceSetup, RenderingMetadata, BrandingValidation, BrandingAssignment, } from './types'; import { OpenSearchDashboardsConfigType } from '../opensearch_dashboards_config'; import { HttpConfigType } from '../http/http_config'; import { SslConfig } from '../http/ssl_config'; import { LoggerFactory } from '../logging'; const DEFAULT_TITLE = 'OpenSearch Dashboards'; /** @internal */ export class RenderingService { constructor(private readonly coreContext: CoreContext) { this.logger = this.coreContext.logger; } private logger: LoggerFactory; private httpsAgent?: HttpsAgent; public async setup({ http, status, uiPlugins, }: RenderingSetupDeps): Promise { const [opensearchDashboardsConfig, serverConfig] = await Promise.all([ this.coreContext.configService .atPath('opensearchDashboards') .pipe(first()) .toPromise(), this.coreContext.configService.atPath('server').pipe(first()).toPromise(), ]); this.setupHttpAgent(serverConfig as HttpConfigType); return { render: async ( request, uiSettings, { includeUserSettings = true, vars }: IRenderOptions = {} ) => { const env = { mode: this.coreContext.env.mode, packageInfo: this.coreContext.env.packageInfo, }; const basePath = http.basePath.get(request); const uiPublicUrl = `${basePath}/ui`; const serverBasePath = http.basePath.serverBasePath; const settings = { defaults: uiSettings.getRegistered(), user: includeUserSettings ? await uiSettings.getUserProvided() : {}, }; // Cannot use `uiSettings.get()` since a user might not be authenticated const darkMode = (settings.user?.['theme:darkMode']?.userValue ?? uiSettings.getOverrideOrDefault('theme:darkMode')) || false; // At the very least, the schema should define a default theme; the '' will be unreachable const themeVersion = (settings.user?.['theme:version']?.userValue ?? uiSettings.getOverrideOrDefault('theme:version')) || ''; const brandingAssignment = await this.assignBrandingConfig( darkMode, opensearchDashboardsConfig as OpenSearchDashboardsConfigType ); const metadata: RenderingMetadata = { strictCsp: http.csp.strict, uiPublicUrl, bootstrapScriptUrl: `${basePath}/bootstrap.js`, i18n: i18n.translate, locale: i18n.getLocale(), darkMode, themeVersion, injectedMetadata: { version: env.packageInfo.version, buildNumber: env.packageInfo.buildNum, branch: env.packageInfo.branch, basePath, serverBasePath, env, anonymousStatusPage: status.isStatusPageAnonymous(), i18n: { translationsUrl: `${basePath}/translations/${i18n.getLocale()}.json`, }, csp: { warnLegacyBrowsers: http.csp.warnLegacyBrowsers }, vars: vars ?? {}, uiPlugins: await Promise.all( [...uiPlugins.public].map(async ([id, plugin]) => ({ id, plugin, config: await this.getUiConfig(uiPlugins, id), })) ), legacyMetadata: { uiSettings: settings, }, branding: { darkMode, assetFolderUrl: `${uiPublicUrl}/default_branding`, logo: { defaultUrl: brandingAssignment.logoDefault, darkModeUrl: brandingAssignment.logoDarkmode, }, mark: { defaultUrl: brandingAssignment.markDefault, darkModeUrl: brandingAssignment.markDarkmode, }, loadingLogo: { defaultUrl: brandingAssignment.loadingLogoDefault, darkModeUrl: brandingAssignment.loadingLogoDarkmode, }, faviconUrl: brandingAssignment.favicon, applicationTitle: brandingAssignment.applicationTitle, useExpandedHeader: brandingAssignment.useExpandedHeader, }, survey: opensearchDashboardsConfig.survey.url, }, }; return `${renderToStaticMarkup(