Skip to content

Commit

Permalink
Backport puppeteer update to Kibana 7.17 (#172440)
Browse files Browse the repository at this point in the history
## Summary

This is a manual back port of #172332 to Kibana 7.17. It's worth
pointing out in this PR, for validating archive and binary checksums we
now use the sha256 hashing algorithm.

<!-- ### Checklist

Delete any items that are not applicable to this PR.

- [ ] Any text added follows [EUI's writing
guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses
sentence case text and includes [i18n
support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)
- [ ]
[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)
was added for features that require explanation or tutorials
- [ ] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
- [ ] [Flaky Test
Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was
used on any tests changed
- [ ] Any UI touched in this PR is usable by keyboard only (learn more
about [keyboard accessibility](https://webaim.org/techniques/keyboard/))
- [ ] Any UI touched in this PR does not create any new axe failures
(run axe in browser:
[FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/),
[Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US))
- [ ] If a plugin configuration key changed, check if it needs to be
allowlisted in the cloud and added to the [docker
list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)
- [ ] This renders correctly on smaller devices using a responsive
layout. (You can test this [in your
browser](https://www.browserstack.com/guide/responsive-testing-on-local-server))
- [ ] This was checked for [cross-browser
compatibility](https://www.elastic.co/support/matrix#matrix_browsers)


### Risk Matrix

Delete this section if it is not applicable to this PR.

Before closing this PR, invite QA, stakeholders, and other developers to
identify risks that should be tested prior to the change/feature
release.

When forming the risk matrix, consider some of the following examples
and how they may potentially impact the change:

| Risk | Probability | Severity | Mitigation/Notes |

|---------------------------|-------------|----------|-------------------------|
| Multiple Spaces&mdash;unexpected behavior in non-default Kibana Space.
| Low | High | Integration tests will verify that all features are still
supported in non-default Kibana Space and when user switches between
spaces. |
| Multiple nodes&mdash;Elasticsearch polling might have race conditions
when multiple Kibana nodes are polling for the same tasks. | High | Low
| Tasks are idempotent, so executing them multiple times will not result
in logical error, but will degrade performance. To test for this case we
add plenty of unit tests around this logic and document manual testing
procedure. |
| Code should gracefully handle cases when feature X or plugin Y are
disabled. | Medium | High | Unit tests will verify that any feature flag
or plugin combination still results in our service operational. |
| [See more potential risk
examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx) |


### For maintainers

- [ ] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)
-->
  • Loading branch information
eokoneyo authored Dec 7, 2023
1 parent 4a70c53 commit b0ccd31
Show file tree
Hide file tree
Showing 14 changed files with 110 additions and 179 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@
"**/remark-parse/trim": "1.0.1",
"**/typescript": "4.7.4",
"**/underscore": "^1.13.1",
"globby/fast-glob": "^3.2.11"
"globby/fast-glob": "^3.2.11",
"wrap-ansi": "7.0.0"
},
"dependencies": {
"@babel/runtime": "^7.21.0",
Expand Down Expand Up @@ -321,7 +322,7 @@
"prop-types": "^15.7.2",
"proxy-from-env": "1.0.0",
"puid": "1.0.7",
"puppeteer": "21.3.6",
"puppeteer": "21.5.2",
"query-string": "^6.13.2",
"random-word-slugs": "^0.0.5",
"raw-loader": "^3.1.0",
Expand Down
12 changes: 6 additions & 6 deletions x-pack/build_chromium/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from build_util import (
runcmd,
runcmdsilent,
md5_file,
sha256_file,
)

# This file builds Chromium headless on Mac and Linux.
Expand Down Expand Up @@ -99,7 +99,7 @@
# chromium-4747cc2-linux_x64.zip
base_filename = 'out/headless/chromium-' + base_version + '-' + platform.system().lower() + '_' + arch_name
zip_filename = base_filename + '.zip'
md5_filename = base_filename + '.md5'
sha256_filename = base_filename + '.sha256'

print('Creating ' + path.join(src_path, zip_filename))
archive = zipfile.ZipFile(zip_filename, mode='w', compression=zipfile.ZIP_DEFLATED)
Expand All @@ -126,9 +126,9 @@ def archive_file(name):

archive.close()

print('Creating ' + path.join(src_path, md5_filename))
with open (md5_filename, 'w') as f:
f.write(md5_file(zip_filename))
print('Creating ' + path.join(src_path, sha256_filename))
with open (sha256_filename, 'w') as f:
f.write(sha256_file(zip_filename))

runcmd('gsutil cp ' + path.join(src_path, zip_filename) + ' gs://headless_shell_staging')
runcmd('gsutil cp ' + path.join(src_path, md5_filename) + ' gs://headless_shell_staging')
runcmd('gsutil cp ' + path.join(src_path, sha256_filename) + ' gs://headless_shell_staging')
12 changes: 6 additions & 6 deletions x-pack/build_chromium/build_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ def mkdir(dir):
if not os.path.exists(dir):
return os.makedirs(dir)

def md5_file(filename):
"""Builds a hex md5 hash of the given file"""
md5 = hashlib.md5()
def sha256_file(filename):
"""Builds a hex sha256 hash of the given file"""
sha256 = hashlib.sha256()
with open(filename, 'rb') as f:
for chunk in iter(lambda: f.read(128 * md5.block_size), b''):
md5.update(chunk)
return md5.hexdigest()
for chunk in iter(lambda: f.read(128 * sha256.block_size), b''):
sha256.update(chunk)
return sha256.hexdigest()
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import { safeChildProcess } from '../../safe_child_process';
import { HeadlessChromiumDriver } from '../driver';
import { args } from './args';
import { getMetrics, Metrics } from './metrics';
// @ts-ignore
import { launch } from './puppeteer_launcher';

interface CreatePageOptions {
Expand Down Expand Up @@ -99,15 +98,15 @@ export class HeadlessChromiumDriverFactory {
let startMetrics: Metrics | undefined;

try {
browser = await launch(
this.browserConfig,
this.userDataDir,
this.binaryPath,
browser = await launch({
browserConfig: this.browserConfig,
userDataDir: this.userDataDir,
binaryPath: this.binaryPath,
chromiumArgs,
viewport,
browserTimezone,
this.protocolTimeout
);
protocolTimeout: this.protocolTimeout,
});

page = await browser.newPage();
devTools = await page.target().createCDPSession();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,30 @@
* 2.0.
*/

import puppeteer from 'puppeteer';
import puppeteer, { type PuppeteerLaunchOptions } from 'puppeteer';

/**
* This function exists as a JS file in order to prevent the v4.1.3 TypeScript compiler from interpreting types
* in the Puppeteer node module.
*/
import { CaptureConfig } from '../../../../server/types';

type LaunchOptions = Pick<PuppeteerLaunchOptions, 'userDataDir' | 'protocolTimeout'> & {
browserConfig: CaptureConfig['browser']['chromium'];
binaryPath: PuppeteerLaunchOptions['executablePath'];
chromiumArgs: PuppeteerLaunchOptions['args'];
viewport: PuppeteerLaunchOptions['defaultViewport'];
browserTimezone?: string;
};

export async function launch(
export async function launch({
browserConfig,
userDataDir,
binaryPath,
chromiumArgs,
viewport,
browserTimezone,
protocolTimeout
) {
protocolTimeout,
}: LaunchOptions) {
return await puppeteer.launch({
pipe: !browserConfig.inspect,
userDataDir: userDataDir,
userDataDir,
executablePath: binaryPath,
ignoreHTTPSErrors: true,
handleSIGHUP: false,
Expand Down
34 changes: 17 additions & 17 deletions x-pack/plugins/reporting/server/browsers/chromium/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ export class ChromiumArchivePaths {
platform: 'darwin',
architecture: 'x64',
archiveFilename: 'chrome-mac.zip',
archiveChecksum: '086ffb9d1e248f41f1e385aaea1bb568',
binaryChecksum: '58ed6d2bba7773b85aaec1d78b9c1a7b',
archiveChecksum: '35261c7a88f1797d27646c340eeaf7d7d70727f0c4ae884e8400240ed66d7192',
binaryChecksum: 'ca90fe7573ddb0723d633fe526acf0fdefdda570a549f35e15c111d10f3ffc0d',
binaryRelativePath: 'chrome-mac/Chromium.app/Contents/MacOS/Chromium',
revision: 1181205,
revision: 1204244, // 1204232 is not available for Mac Intel
location: 'common',
archivePath: 'Mac',
isPreInstalled: false,
Expand All @@ -56,44 +56,44 @@ export class ChromiumArchivePaths {
platform: 'darwin',
architecture: 'arm64',
archiveFilename: 'chrome-mac.zip',
archiveChecksum: 'f80b2cb14025e283a740836aa66e46d4',
binaryChecksum: '361f7cbac5bcac1d9974a43e29bf4bf5',
archiveChecksum: '1ed375086a9505ee6bc9bc1373bebd79e87e5b27af5a93258ea25ffb6f71f03c',
binaryChecksum: 'a8556ed7ac2a669fa81f752f7d18a9d1e9b99b05d3504f6bbc08e3e0b02ff71e',
binaryRelativePath: 'chrome-mac/Chromium.app/Contents/MacOS/Chromium',
revision: 1181286, // 1181205 is not available for Mac_Arm
revision: 1204255, // 1204232 is not available for Mac_Arm
location: 'common',
archivePath: 'Mac_Arm',
isPreInstalled: false,
},
{
platform: 'linux',
architecture: 'x64',
archiveFilename: 'chromium-67649b1-locales-linux_x64.zip',
archiveChecksum: '21bd8a1e06f236fa405c74d92a7ccd63',
binaryChecksum: 'b75d45d3044cc320bb09ce7356003d24',
archiveFilename: 'chromium-38c7255-locales-linux_x64.zip',
archiveChecksum: 'bf07734366ece771a85b2452fd63e5981b1abc234ef0ed1c7d0774b8a7b5c6a9',
binaryChecksum: '87a991c412ad333549a58524b6be23f2a1ff56af61bb1a1b10c1f4a0206edc2a',
binaryRelativePath: 'headless_shell-linux_x64/headless_shell',
revision: 1181205,
revision: 1204232,
location: 'custom',
isPreInstalled: true,
},
{
platform: 'linux',
architecture: 'arm64',
archiveFilename: 'chromium-67649b1-locales-linux_arm64.zip',
archiveChecksum: '0c3b42ada934258b4596f3e984d011e3',
binaryChecksum: 'ac521fbc52fb1589416a214ce7b299ee',
archiveFilename: 'chromium-38c7255-locales-linux_arm64.zip',
archiveChecksum: '11c1cd2398ae3b57a72e7746e1f1cbbd2c2d18d1b83dec949dc81a3c690688f0',
binaryChecksum: '4d914034d466b97c438283dbc914230e087217c25028f403dfa3c933ea755e94',
binaryRelativePath: 'headless_shell-linux_arm64/headless_shell',
revision: 1181205,
revision: 1204232,
location: 'custom',
isPreInstalled: true,
},
{
platform: 'win32',
architecture: 'x64',
archiveFilename: 'chrome-win.zip',
archiveChecksum: '08186d7494e75c2cca03270d9a4ff589',
binaryChecksum: '1623fed921c9acee7221b2de98abe54e',
archiveChecksum: 'd6f5a21973867115435814c2c46d49edd9a0a2ad6da14b4724746374cad80e47',
binaryChecksum: '9c0d2404004bd7c4ada649049422de6958460ecf6cec53460a478c6d8c33e444',
binaryRelativePath: path.join('chrome-win', 'chrome.exe'),
revision: 1181280, // 1181205 is not available for win
revision: 1204234, // 1204232 is not available for win
location: 'common',
archivePath: 'Win',
isPreInstalled: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ jest.mock('fs');

import { createReadStream, ReadStream } from 'fs';
import { Readable } from 'stream';
import { md5 } from './checksum';
import { sha256 } from './checksum';

describe('md5', () => {
describe('sha256', () => {
let stream: ReadStream;

beforeEach(() => {
Expand All @@ -25,14 +25,16 @@ describe('md5', () => {
(createReadStream as jest.MockedFunction<typeof createReadStream>).mockReturnValue(stream);
});

it('should return an md5 hash', async () => {
await expect(md5('path')).resolves.toBe('437b930db84b8079c2dd804a71936b5f');
it('should return an sha256 hash', async () => {
expect(await sha256('path')).toMatchInlineSnapshot(
`"3fc9b689459d738f8c88a3a48aa9e33542016b7a4052e001aaa536fca74813cb"`
);
});

it('should reject on stream error', async () => {
const error = new Error('Some error');
stream.destroy(error);

await expect(md5('path')).rejects.toEqual(error);
await expect(sha256('path')).rejects.toEqual(error);
});
});
4 changes: 2 additions & 2 deletions x-pack/plugins/reporting/server/browsers/download/checksum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ function readableEnd(stream: Readable) {
});
}

export async function md5(path: string) {
const hash = createHash('md5');
export async function sha256(path: string) {
const hash = createHash('sha256');
await readableEnd(createReadStream(path).on('data', (chunk) => hash.update(chunk)));
return hash.digest('hex');
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ test('downloads the url to the path', async () => {
expect(readFileSync(TEMP_FILE, 'utf8')).toEqual(BODY);
});

test('returns the md5 hex hash of the http body', async () => {
test('returns the sha256 hex hash of the http body', async () => {
const BODY = 'foobar';
const HASH = createHash('md5').update(BODY).digest('hex');
const HASH = createHash('sha256').update(BODY).digest('hex');
request.mockImplementationOnce(async () => {
return {
data: new ReadableOf(BODY),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export async function download(
): Promise<string> {
logger.info(`Downloading ${url} to ${path}`);

const hash = createHash('md5');
const hash = createHash('sha256');

mkdirSync(dirname(path), { recursive: true });
const handle = openSync(path, 'w');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import mockFs from 'mock-fs';
import { existsSync, readdirSync } from 'fs';
import { chromium } from '../chromium';
import { download } from './download';
import { md5 } from './checksum';
import { sha256 } from './checksum';
import { ensureBrowserDownloaded } from './ensure_downloaded';
import { LevelLogger } from '../../lib';

Expand All @@ -28,18 +28,18 @@ describe.skip('ensureBrowserDownloaded', () => {
warning: jest.fn(),
} as unknown as typeof logger;

(md5 as jest.MockedFunction<typeof md5>).mockImplementation(
(sha256 as jest.MockedFunction<typeof sha256>).mockImplementation(
async (packagePath) =>
chromium.paths.packages.find(
(packageInfo) => chromium.paths.resolvePath(packageInfo) === packagePath
)?.archiveChecksum ?? 'some-md5'
)?.archiveChecksum ?? 'some-sha256'
);

(download as jest.MockedFunction<typeof download>).mockImplementation(
async (_url, packagePath) =>
chromium.paths.packages.find(
(packageInfo) => chromium.paths.resolvePath(packageInfo) === packagePath
)?.archiveChecksum ?? 'some-md5'
)?.archiveChecksum ?? 'some-sha256'
);

mockFs();
Expand Down Expand Up @@ -73,8 +73,8 @@ describe.skip('ensureBrowserDownloaded', () => {
await expect(ensureBrowserDownloaded(logger)).rejects.toBeInstanceOf(Error);
});

it('should reject when downloaded md5 hash is different', async () => {
(download as jest.MockedFunction<typeof download>).mockResolvedValue('random-md5');
it('should reject when downloaded sha256 hash is different', async () => {
(download as jest.MockedFunction<typeof download>).mockResolvedValue('random-sha256');

await expect(ensureBrowserDownloaded(logger)).rejects.toBeInstanceOf(Error);
});
Expand Down Expand Up @@ -110,8 +110,8 @@ describe.skip('ensureBrowserDownloaded', () => {
]);
});

it('should download again if md5 hash different', async () => {
(md5 as jest.MockedFunction<typeof md5>).mockResolvedValueOnce('random-md5');
it('should download again if sha256 hash different', async () => {
(sha256 as jest.MockedFunction<typeof sha256>).mockResolvedValueOnce('random-sha256');
await ensureBrowserDownloaded(logger);

expect(download).toHaveBeenCalledTimes(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { existsSync } from 'fs';
import del from 'del';
import { BrowserDownload, chromium } from '../';
import { GenericLevelLogger } from '../../lib/level_logger';
import { md5 } from './checksum';
import { sha256 } from './checksum';
import { download } from './download';

/**
Expand Down Expand Up @@ -48,7 +48,7 @@ async function ensureDownloaded(browsers: BrowserDownload[], logger: GenericLeve

let foundChecksum: string;
try {
foundChecksum = await md5(path).catch();
foundChecksum = await sha256(path).catch();
} catch {
foundChecksum = 'MISSING';
}
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/reporting/server/browsers/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import * as Rx from 'rxjs';
import { GenericLevelLogger } from '../lib/level_logger';
import { ChromiumArchivePaths } from './chromium';
import { ensureBrowserDownloaded } from './download';
import { md5 } from './download/checksum';
import { sha256 } from './download/checksum';
import { extract } from './extract';

/**
Expand All @@ -36,7 +36,7 @@ export function installBrowser(

const backgroundInstall = async () => {
const binaryPath = paths.getBinaryPath(pkg);
const binaryChecksum = await md5(binaryPath).catch(() => '');
const binaryChecksum = await sha256(binaryPath).catch(() => '');

if (binaryChecksum !== pkg.binaryChecksum) {
logger.warning(
Expand Down
Loading

0 comments on commit b0ccd31

Please sign in to comment.