Skip to content

Commit

Permalink
[HTTP] Add server/browser side http.staticAssets service (#171003)
Browse files Browse the repository at this point in the history
## Summary

Part of #170421

### 1. Introduce the `http.staticAssets` service

Which can be used to generate hrefs to Kibana's static assets in a
CDN-friendly way (based on the CDN url if defined in the config, and the
Kibana's basePath otherwise)

The service is exposed both on the browser and server-side.

For now a single API is exposed: `getPluginAssetHref`

```ts
// returns "/plugins/{pluginId}/assets/some_folder/asset.png" when CDN isn't configured
core.http.statisAssets.getPluginAssetHref('some_folder/asset.png');
```

### 2. Plug it on some of the `home` plugin assets

Adapt the sample data sets and tutorial schemas to use the service for
links to the associated assets

## How to test

#### 1. Edit`/etc/hosts`

add a line `127.0.0.1       local.cdn`

#### 2. Edit `kibana.yaml`

Add `server.cdn.url: "http://local.cdn:5601"`

#### 3. Boot kibana and navigate to sample data set installation

(if started in dev mode, use `--no-base-path`)

Confirm that the sample data set presentation images are pointing to the
CDN url and properly displayed:

<img width="1565" alt="Screenshot 2023-11-13 at 09 28 51"
src="https://github.com/elastic/kibana/assets/1532934/23a887af-00cb-400c-9ab1-511ba463495f">

---------

Co-authored-by: kibanamachine <[email protected]>
  • Loading branch information
pgayvallet and kibanamachine authored Nov 16, 2023
1 parent 82d0503 commit c713b91
Show file tree
Hide file tree
Showing 163 changed files with 672 additions and 286 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { createBrowserHistory, History } from 'history';

import type { PluginOpaqueId } from '@kbn/core-base-common';
import type { ThemeServiceStart } from '@kbn/core-theme-browser';
import type { HttpSetup, HttpStart } from '@kbn/core-http-browser';
import type { InternalHttpSetup, InternalHttpStart } from '@kbn/core-http-browser-internal';
import type { Capabilities } from '@kbn/core-capabilities-common';
import type { MountPoint } from '@kbn/core-mount-utils-browser';
import type { OverlayStart } from '@kbn/core-overlays-browser';
Expand Down Expand Up @@ -46,15 +46,15 @@ import {
import { registerAnalyticsContextProvider } from './register_analytics_context_provider';

export interface SetupDeps {
http: HttpSetup;
http: InternalHttpSetup;
analytics: AnalyticsServiceSetup;
history?: History<any>;
/** Used to redirect to external urls */
redirectTo?: (path: string) => void;
}

export interface StartDeps {
http: HttpStart;
http: InternalHttpStart;
analytics: AnalyticsServiceStart;
theme: ThemeServiceStart;
overlays: OverlayStart;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('parseAppUrl', () => {

beforeEach(() => {
apps = new Map();
basePath = new BasePath('/base-path');
basePath = new BasePath({ basePath: '/base-path' });

createApp({
id: 'foo',
Expand Down
6 changes: 3 additions & 3 deletions packages/core/apps/core-apps-browser-internal/src/core_app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { UnregisterCallback } from 'history';
import type { CoreContext } from '@kbn/core-base-browser-internal';
import type { InternalInjectedMetadataSetup } from '@kbn/core-injected-metadata-browser-internal';
import type { DocLinksStart } from '@kbn/core-doc-links-browser';
import type { HttpSetup, HttpStart } from '@kbn/core-http-browser';
import type { InternalHttpSetup, InternalHttpStart } from '@kbn/core-http-browser-internal';
import type { IUiSettingsClient } from '@kbn/core-ui-settings-browser';
import type { NotificationsSetup, NotificationsStart } from '@kbn/core-notifications-browser';
import { AppNavLinkStatus, type AppMountParameters } from '@kbn/core-application-browser';
Expand All @@ -27,15 +27,15 @@ import { renderApp as renderStatusApp } from './status';

export interface CoreAppsServiceSetupDeps {
application: InternalApplicationSetup;
http: HttpSetup;
http: InternalHttpSetup;
injectedMetadata: InternalInjectedMetadataSetup;
notifications: NotificationsSetup;
}

export interface CoreAppsServiceStartDeps {
application: InternalApplicationStart;
docLinks: DocLinksStart;
http: HttpStart;
http: InternalHttpStart;
notifications: NotificationsStart;
uiSettings: IUiSettingsClient;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('renderApp', () => {
let unmount: () => void;

beforeEach(() => {
basePath = new BasePath();
basePath = new BasePath({ basePath: '' });
element = document.createElement('div');
history = createMemoryHistory();
unmount = renderApp(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n-react';

import type { DocLinksStart } from '@kbn/core-doc-links-browser';
import type { HttpStart } from '@kbn/core-http-browser';
import type { InternalHttpStart } from '@kbn/core-http-browser-internal';
import type { NotificationsStart } from '@kbn/core-notifications-browser';
import { mountReactNode } from '@kbn/core-mount-utils-browser-internal';

Expand All @@ -21,7 +21,7 @@ export const MISSING_CONFIG_STORAGE_KEY = `core.warnings.publicBaseUrlMissingDis

interface Deps {
docLinks: DocLinksStart;
http: HttpStart;
http: InternalHttpStart;
notifications: NotificationsStart;
// Exposed for easier testing
storage?: Storage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('url overflow detection', () => {
let unlisten: any;

beforeEach(() => {
basePath = new BasePath('/test-123');
basePath = new BasePath({ basePath: '/test-123' });
history = createMemoryHistory();
toasts = notificationServiceMock.createStartContract().toasts;
uiSettings = uiSettingsServiceMock.createStartContract();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export async function loadStatus({
http,
notifications,
}: {
http: HttpSetup;
http: Pick<HttpSetup, 'get'>;
notifications: NotificationsSetup;
}) {
let response: StatusResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import React from 'react';
import ReactDOM from 'react-dom';
import { I18nProvider } from '@kbn/i18n-react';
import { CoreThemeProvider } from '@kbn/core-theme-browser-internal';
import type { HttpSetup } from '@kbn/core-http-browser';
import type { InternalHttpSetup } from '@kbn/core-http-browser-internal';
import type { NotificationsSetup } from '@kbn/core-notifications-browser';
import type { AppMountParameters } from '@kbn/core-application-browser';
import { StatusApp } from './status_app';

interface Deps {
http: HttpSetup;
http: InternalHttpSetup;
notifications: NotificationsSetup;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import React, { Component } from 'react';
import { EuiLoadingSpinner, EuiText, EuiPage, EuiPageBody, EuiSpacer } from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n-react';
import { i18n } from '@kbn/i18n';
import type { HttpSetup } from '@kbn/core-http-browser';
import type { InternalHttpSetup } from '@kbn/core-http-browser-internal';
import type { NotificationsSetup } from '@kbn/core-notifications-browser';
import { loadStatus, type ProcessedServerResponse } from './lib';
import { MetricTiles, ServerStatus, StatusSection, VersionHeader } from './components';

interface StatusAppProps {
http: HttpSetup;
http: InternalHttpSetup;
notifications: NotificationsSetup;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

import type { RecursiveReadonly } from '@kbn/utility-types';
import { deepFreeze } from '@kbn/std';
import type { HttpStart } from '@kbn/core-http-browser';
import type { InternalHttpStart } from '@kbn/core-http-browser-internal';
import type { Capabilities } from '@kbn/core-capabilities-common';

interface StartDeps {
appIds: string[];
http: HttpStart;
http: InternalHttpStart;
}

/** @internal */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
"kbn_references": [
"@kbn/utility-types",
"@kbn/std",
"@kbn/core-http-browser",
"@kbn/core-http-browser-mocks",
"@kbn/core-capabilities-common"
"@kbn/core-capabilities-common",
"@kbn/core-http-browser-internal"
],
"exclude": [
"target/**/*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import useObservable from 'react-use/lib/useObservable';
import type { InternalInjectedMetadataStart } from '@kbn/core-injected-metadata-browser-internal';
import type { AnalyticsServiceSetup } from '@kbn/core-analytics-browser';
import { type DocLinksStart } from '@kbn/core-doc-links-browser';
import type { HttpStart } from '@kbn/core-http-browser';
import type { InternalHttpStart } from '@kbn/core-http-browser-internal';
import { mountReactNode } from '@kbn/core-mount-utils-browser-internal';
import type { NotificationsStart } from '@kbn/core-notifications-browser';
import type { InternalApplicationStart } from '@kbn/core-application-browser-internal';
Expand Down Expand Up @@ -63,7 +63,7 @@ export interface SetupDeps {
export interface StartDeps {
application: InternalApplicationStart;
docLinks: DocLinksStart;
http: HttpStart;
http: InternalHttpStart;
injectedMetadata: InternalInjectedMetadataStart;
notifications: NotificationsStart;
customBranding: CustomBrandingStart;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import { sortBy } from 'lodash';
import { BehaviorSubject, ReplaySubject } from 'rxjs';
import { map, takeUntil } from 'rxjs/operators';
import type { HttpStart, IBasePath } from '@kbn/core-http-browser';
import type { IBasePath } from '@kbn/core-http-browser';
import type { InternalHttpStart } from '@kbn/core-http-browser-internal';
import type { PublicAppDeepLinkInfo, PublicAppInfo } from '@kbn/core-application-browser';
import type { InternalApplicationStart } from '@kbn/core-application-browser-internal';
import type { ChromeNavLinks } from '@kbn/core-chrome-browser';
Expand All @@ -18,7 +19,7 @@ import { toNavLink } from './to_nav_link';

interface StartDeps {
application: InternalApplicationStart;
http: HttpStart;
http: InternalHttpStart;
}

export class NavLinksService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
ChromeSetProjectBreadcrumbsParams,
ChromeProjectNavigationNode,
} from '@kbn/core-chrome-browser';
import type { HttpStart } from '@kbn/core-http-browser';
import type { InternalHttpStart } from '@kbn/core-http-browser-internal';
import {
BehaviorSubject,
Observable,
Expand All @@ -37,7 +37,7 @@ import { buildBreadcrumbs } from './breadcrumbs';
interface StartDeps {
application: InternalApplicationStart;
navLinks: ChromeNavLinks;
http: HttpStart;
http: InternalHttpStart;
chromeBreadcrumbs$: Observable<ChromeBreadcrumb[]>;
}

Expand All @@ -59,7 +59,7 @@ export class ProjectNavigationService {
}>({ breadcrumbs: [], params: { absolute: false } });
private readonly stop$ = new ReplaySubject<void>(1);
private application?: InternalApplicationStart;
private http?: HttpStart;
private http?: InternalHttpStart;
private unlistenHistory?: () => void;

public start({ application, navLinks, http, chromeBreadcrumbs$ }: StartDeps) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

import type { HttpSetup } from '@kbn/core-http-browser';
import type { InternalHttpStart } from '@kbn/core-http-browser-internal';
import type {
ChromeRecentlyAccessed,
ChromeRecentlyAccessedHistoryItem,
Expand All @@ -15,7 +15,7 @@ import { PersistedLog } from './persisted_log';
import { createLogKey } from './create_log_key';

interface StartDeps {
http: HttpSetup;
http: InternalHttpStart;
}

/** @internal */
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"@kbn/core-analytics-browser",
"@kbn/shared-ux-router",
"@kbn/shared-ux-link-redirect-app",
"@kbn/core-http-browser-internal",
],
"exclude": [
"target/**/*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

import type { CoreService } from '@kbn/core-base-browser-internal';
import type { DeprecationsServiceStart } from '@kbn/core-deprecations-browser';
import type { HttpStart } from '@kbn/core-http-browser';
import type { InternalHttpStart } from '@kbn/core-http-browser-internal';
import { DeprecationsClient } from './deprecations_client';

export class DeprecationsService implements CoreService<void, DeprecationsServiceStart> {
public setup(): void {}

public start({ http }: { http: HttpStart }): DeprecationsServiceStart {
public start({ http }: { http: InternalHttpStart }): DeprecationsServiceStart {
const deprecationsClient = new DeprecationsClient({ http });

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"@kbn/core-http-browser",
"@kbn/core-http-browser-mocks",
"@kbn/core-deprecations-common",
"@kbn/core-deprecations-browser"
"@kbn/core-deprecations-browser",
"@kbn/core-http-browser-internal"
],
"exclude": [
"target/**/*",
Expand Down
1 change: 1 addition & 0 deletions packages/core/http/core-http-browser-internal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@

export { BasePath } from './src/base_path';
export { HttpService } from './src/http_service';
export type { InternalHttpSetup, InternalHttpStart } from './src/types';
Loading

0 comments on commit c713b91

Please sign in to comment.