Skip to content

Commit

Permalink
[CDN/Maps] Update transformRequest to create absolute URLs (#179154)
Browse files Browse the repository at this point in the history
## Summary

Fix #179058

Update the `transformRequest` function to create URLs that are callable
from inside workers that don't share the same domain.

Proposal is to to convert absolute path values to absolute URLs, e.g.
`/my/path` => `https://my.origin/my/path`.

- [x] Blocked by #178669

## Test locally with CDN

Easiest way to test is via the observability project built on this PR
(see buildkite output), otherwise:

- Build CDN assets (`node ./scripts/build.js --skip-docker-ubi
--skip-docker-ubuntu --skip-docker-fips --skip-os-packages`)
- Unpack the CDN assets
`target/kibana-8.14.0-SNAPSHOT-cdn-assets.tar.gz` and serve them `npx
http-server -p 1772 --cors --gzip --brotli`, remember to change the hash
dir name to `XXXXXXXXXXXX`
- Serve via different domain eg:
```
## My test
127.0.0.1 my.cdn.test
```
- Configure Kibana to load assets `server.cdn.url: http://my.cdn.test`

---------

Co-authored-by: Eyo Okon Eyo <[email protected]>
Co-authored-by: kibanamachine <[email protected]>
  • Loading branch information
3 people authored Mar 22, 2024
1 parent 15fde36 commit f8b3d70
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { transformRequest } from './transform_request';

describe('transformRequest', () => {
let location: Location;

beforeEach(() => {
location = global.window.location;
// @ts-expect-error we need to set our own location value
delete global.window.location;
global.window.location = {
origin: 'https://transform.test.local',
} as unknown as Location;
});

afterEach(() => {
Object.defineProperty(global.window, 'location', { value: location });
});

it.each([
['/internal/maps/fake/path', 'https://transform.test.local/internal/maps/fake/path'],
['/', 'https://transform.test.local/'],
[' /with/space', 'https://transform.test.local/with/space'],
])('converts %s to absolute URL', (input, output) => {
const { url } = transformRequest(input, 'Foo');
expect(url).toBe(output);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,27 @@ import {
MVT_GETTILE_API_PATH,
MVT_GETGRIDTILE_API_PATH,
} from '../../../common/constants';
import { getHttp } from '../../kibana_services';

const FONTS = getHttp().basePath.prepend(FONTS_API_PATH);
const GETTILE = getHttp().basePath.prepend(MVT_GETTILE_API_PATH);
const GETGRIDTILE = getHttp().basePath.prepend(MVT_GETGRIDTILE_API_PATH);
/**
* This URL could be used from inside a Worker which may have a different base
* URL. This function takes a string that may be a path and converts it to an
* absolute URL.
*/
function prepareAbsoluteUrl(pathOrUrl: string): string {
pathOrUrl = pathOrUrl.trim();
if (pathOrUrl.startsWith('/')) {
return new URL(pathOrUrl, window.location.origin).toString();
}
return pathOrUrl;
}

export function transformRequest(url: string, resourceType: string | undefined) {
if (resourceType === 'Glyphs' && url.startsWith(FONTS)) {
/**
* @param pathOrUrl - Assumed to be a full URL or a path starting with "/"
* @param resourceType - Indicator of what type of resource is being requested
*/
export function transformRequest(pathOrUrl: string, resourceType: string | undefined) {
const url = prepareAbsoluteUrl(pathOrUrl);
if (resourceType === 'Glyphs' && url.includes(FONTS_API_PATH)) {
return {
url,
method: 'GET' as 'GET',
Expand All @@ -32,7 +45,7 @@ export function transformRequest(url: string, resourceType: string | undefined)
};
}

if (resourceType === 'Tile' && url.startsWith(GETTILE)) {
if (resourceType === 'Tile' && url.includes(MVT_GETTILE_API_PATH)) {
return {
url,
method: 'GET' as 'GET',
Expand All @@ -43,7 +56,7 @@ export function transformRequest(url: string, resourceType: string | undefined)
};
}

if (resourceType === 'Tile' && url.startsWith(GETGRIDTILE)) {
if (resourceType === 'Tile' && url.includes(MVT_GETGRIDTILE_API_PATH)) {
return {
url,
method: 'GET' as 'GET',
Expand Down

0 comments on commit f8b3d70

Please sign in to comment.