-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[HTTP] Add support for configuring a CDN (part I) (#169408)
- Loading branch information
1 parent
af84131
commit 8727c68
Showing
24 changed files
with
467 additions
and
56 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
packages/core/http/core-http-server-internal/src/__snapshots__/http_config.test.ts.snap
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
57 changes: 57 additions & 0 deletions
57
packages/core/http/core-http-server-internal/src/cdn.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { CdnConfig } from './cdn'; | ||
|
||
describe('CdnConfig', () => { | ||
it.each([ | ||
['https://cdn.elastic.co', 'cdn.elastic.co'], | ||
['https://foo.bar', 'foo.bar'], | ||
['http://foo.bar', 'foo.bar'], | ||
['https://cdn.elastic.co:9999', 'cdn.elastic.co:9999'], | ||
['https://cdn.elastic.co:9999/with-a-path', 'cdn.elastic.co:9999'], | ||
])('host as expected for %p', (url, expected) => { | ||
expect(CdnConfig.from({ url }).host).toEqual(expected); | ||
}); | ||
|
||
it.each([ | ||
['https://cdn.elastic.co', 'https://cdn.elastic.co'], | ||
['https://foo.bar', 'https://foo.bar'], | ||
['http://foo.bar', 'http://foo.bar'], | ||
['https://cdn.elastic.co:9999', 'https://cdn.elastic.co:9999'], | ||
['https://cdn.elastic.co:9999/with-a-path', 'https://cdn.elastic.co:9999/with-a-path'], | ||
])('base HREF as expected for %p', (url, expected) => { | ||
expect(CdnConfig.from({ url }).baseHref).toEqual(expected); | ||
}); | ||
|
||
it.each([['foo'], ['#!']])('throws for invalid URLs (%p)', (url) => { | ||
expect(() => CdnConfig.from({ url })).toThrow(/Invalid URL/); | ||
}); | ||
|
||
it('handles empty urls', () => { | ||
expect(CdnConfig.from({ url: '' }).baseHref).toBeUndefined(); | ||
expect(CdnConfig.from({ url: '' }).host).toBeUndefined(); | ||
}); | ||
|
||
it('generates the expected CSP additions', () => { | ||
const cdnConfig = CdnConfig.from({ url: 'https://foo.bar:9999' }); | ||
expect(cdnConfig.getCspConfig()).toEqual({ | ||
connect_src: ['foo.bar:9999'], | ||
font_src: ['foo.bar:9999'], | ||
img_src: ['foo.bar:9999'], | ||
script_src: ['foo.bar:9999'], | ||
style_src: ['foo.bar:9999'], | ||
worker_src: ['foo.bar:9999'], | ||
}); | ||
}); | ||
|
||
it('generates the expected CSP additions when no URL is provided', () => { | ||
const cdnConfig = CdnConfig.from({ url: '' }); | ||
expect(cdnConfig.getCspConfig()).toEqual({}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { URL, format } from 'node:url'; | ||
import type { CspAdditionalConfig } from './csp'; | ||
|
||
export interface Input { | ||
url?: string; | ||
} | ||
|
||
export class CdnConfig { | ||
private url: undefined | URL; | ||
constructor(url?: string) { | ||
if (url) { | ||
this.url = new URL(url); // This will throw for invalid URLs | ||
} | ||
} | ||
|
||
public get host(): undefined | string { | ||
return this.url?.host ?? undefined; | ||
} | ||
|
||
public get baseHref(): undefined | string { | ||
if (this.url) { | ||
return this.url.pathname === '/' ? this.url.origin : format(this.url); | ||
} | ||
} | ||
|
||
public getCspConfig(): CspAdditionalConfig { | ||
const host = this.host; | ||
if (!host) return {}; | ||
return { | ||
font_src: [host], | ||
img_src: [host], | ||
script_src: [host], | ||
style_src: [host], | ||
worker_src: [host], | ||
connect_src: [host], | ||
}; | ||
} | ||
|
||
public static from(input: Input = {}) { | ||
return new CdnConfig(input.url); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
packages/core/http/core-http-server-internal/src/static_assets.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { StaticAssets } from './static_assets'; | ||
import { BasePath } from './base_path_service'; | ||
import { CdnConfig } from './cdn'; | ||
|
||
describe('StaticAssets', () => { | ||
let basePath: BasePath; | ||
let cdnConfig: CdnConfig; | ||
let staticAssets: StaticAssets; | ||
beforeEach(() => { | ||
basePath = new BasePath('/test'); | ||
cdnConfig = CdnConfig.from(); | ||
staticAssets = new StaticAssets(basePath, cdnConfig); | ||
}); | ||
it('provides fallsback to server base path', () => { | ||
expect(staticAssets.getHrefBase()).toEqual('/test'); | ||
}); | ||
|
||
it('provides the correct HREF given a CDN is configured', () => { | ||
cdnConfig = CdnConfig.from({ url: 'https://cdn.example.com/test' }); | ||
staticAssets = new StaticAssets(basePath, cdnConfig); | ||
expect(staticAssets.getHrefBase()).toEqual('https://cdn.example.com/test'); | ||
}); | ||
}); |
28 changes: 28 additions & 0 deletions
28
packages/core/http/core-http-server-internal/src/static_assets.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import type { BasePath } from './base_path_service'; | ||
import { CdnConfig } from './cdn'; | ||
|
||
export interface IStaticAssets { | ||
getHrefBase(): string; | ||
} | ||
|
||
export class StaticAssets implements IStaticAssets { | ||
constructor(private readonly basePath: BasePath, private readonly cdnConfig: CdnConfig) {} | ||
/** | ||
* Returns a href (hypertext reference) intended to be used as the base for constructing | ||
* other hrefs to static assets. | ||
*/ | ||
getHrefBase(): string { | ||
if (this.cdnConfig.baseHref) { | ||
return this.cdnConfig.baseHref; | ||
} | ||
return this.basePath.serverBasePath; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.