Skip to content

Commit

Permalink
[8.15] [Screenshotting] Server package for stateless code (elastic#18…
Browse files Browse the repository at this point in the history
…8390) (elastic#193231)

# Backport

This will backport the following commits from `main` to `8.15`:
- [[Screenshotting] Server package for stateless code
(elastic#188390)](elastic#188390)

<!--- Backport version: 8.9.8 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Tim
Sullivan","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-08-22T18:03:03Z","message":"[Screenshotting]
Server package for stateless code (elastic#188390)\n\n## Summary\r\n\r\nThis PR
moves code that is usually impacted by a Puppeteer/Chromium\r\nupgrade
to a package. The same work is happening to a lesser degree in\r\nthe
7.17 branch. Together, these changes will ease the manual labor
of\r\nbackporting the upgrades from main to
7.17.\r\n\r\n---------\r\n\r\nCo-authored-by: kibanamachine
<[email protected]>","sha":"bf673d9c565c288223483c4c622f37f745ce70e6","branchLabelMapping":{"^v8.16.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v8.14.0","v8.15.0","v8.16.0","backport:version"],"number":188390,"url":"https://github.com/elastic/kibana/pull/188390","mergeCommit":{"message":"[Screenshotting]
Server package for stateless code (elastic#188390)\n\n## Summary\r\n\r\nThis PR
moves code that is usually impacted by a Puppeteer/Chromium\r\nupgrade
to a package. The same work is happening to a lesser degree in\r\nthe
7.17 branch. Together, these changes will ease the manual labor
of\r\nbackporting the upgrades from main to
7.17.\r\n\r\n---------\r\n\r\nCo-authored-by: kibanamachine
<[email protected]>","sha":"bf673d9c565c288223483c4c622f37f745ce70e6"}},"sourceBranch":"main","suggestedTargetBranches":["8.14","8.15"],"targetPullRequestStates":[{"branch":"8.14","label":"v8.14.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.15","label":"v8.15.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.16.0","labelRegex":"^v8.16.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/188390","number":188390,"mergeCommit":{"message":"[Screenshotting]
Server package for stateless code (elastic#188390)\n\n## Summary\r\n\r\nThis PR
moves code that is usually impacted by a Puppeteer/Chromium\r\nupgrade
to a package. The same work is happening to a lesser degree in\r\nthe
7.17 branch. Together, these changes will ease the manual labor
of\r\nbackporting the upgrades from main to
7.17.\r\n\r\n---------\r\n\r\nCo-authored-by: kibanamachine
<[email protected]>","sha":"bf673d9c565c288223483c4c622f37f745ce70e6"}}]}]
BACKPORT-->

---------

Co-authored-by: Eyo O. Eyo <[email protected]>
  • Loading branch information
tsullivan and eokoneyo authored Sep 18, 2024
1 parent 82cee56 commit 5a522bf
Show file tree
Hide file tree
Showing 47 changed files with 211 additions and 86 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,7 @@
"@kbn/screenshot-mode-plugin": "link:src/plugins/screenshot_mode",
"@kbn/screenshotting-example-plugin": "link:x-pack/examples/screenshotting_example",
"@kbn/screenshotting-plugin": "link:x-pack/plugins/screenshotting",
"@kbn/screenshotting-server": "link:packages/kbn-screenshotting-server",
"@kbn/search-api-panels": "link:packages/kbn-search-api-panels",
"@kbn/search-connectors": "link:packages/kbn-search-connectors",
"@kbn/search-connectors-plugin": "link:x-pack/plugins/search_connectors",
Expand Down
3 changes: 3 additions & 0 deletions packages/kbn-screenshotting-server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @kbn/screenshotting-server

Stateless code pertaining to the capture of screenshots for Kibana Reporting
13 changes: 13 additions & 0 deletions packages/kbn-screenshotting-server/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* 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.
*/

export { args } from './src/args';
export { ChromiumArchivePaths, type PackageInfo } from './src/paths';
export { getChromiumPackage } from './src/get_chromium_package';
export { type ConfigType, createConfig, config, durationToNumber } from './src/config';
export { ConfigSchema } from './src/config/schema';
13 changes: 13 additions & 0 deletions packages/kbn-screenshotting-server/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* 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.
*/

module.exports = {
preset: '@kbn/test/jest_node',
rootDir: '../..',
roots: ['<rootDir>/packages/kbn-screenshotting-server'],
};
5 changes: 5 additions & 0 deletions packages/kbn-screenshotting-server/kibana.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "shared-server",
"id": "@kbn/screenshotting-server",
"owner": "@elastic/appex-sharedux"
}
6 changes: 6 additions & 0 deletions packages/kbn-screenshotting-server/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "@kbn/screenshotting-server",
"private": true,
"version": "1.0.0",
"license": "SSPL-1.0 OR Elastic License 2.0"
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/*
* 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.
* 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 os from 'os';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/*
* 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.
* 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 os from 'os';
import type { ConfigType } from '../../../config';
import type { ConfigType } from './config';

interface WindowSize {
height: number;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/*
* 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.
* 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 { Logger } from '@kbn/core/server';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/*
* 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.
* 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 { set } from '@kbn/safer-lodash-set';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/*
* 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.
* 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.
*/

jest.mock('getos', () => jest.fn());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/*
* 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.
* 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 getOsSync from 'getos';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/*
* 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.
* 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 { PluginConfigDescriptor } from '@kbn/core/server';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/*
* 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.
* 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 { schema, TypeOf, offeringBasedSchema } from '@kbn/config-schema';
Expand Down
23 changes: 23 additions & 0 deletions packages/kbn-screenshotting-server/src/get_chromium_package.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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 os from 'os';
import { ChromiumArchivePaths } from './paths';

const paths = new ChromiumArchivePaths();

export const getChromiumPackage = () => {
const platform = process.platform;
const architecture = os.arch();

const chromiumPackageInfo = paths.find(process.platform, architecture);
if (!chromiumPackageInfo) {
throw new Error(`Unsupported platform: ${platform}-${architecture}`);
}
return chromiumPackageInfo;
};
18 changes: 18 additions & 0 deletions packages/kbn-screenshotting-server/src/paths.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* 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 { ChromiumArchivePaths } from './paths';

describe('ChromiumArchivePaths', () => {
it('should download chromium archive to a specific path', () => {
const ChromiumArchivePathsInstance = new ChromiumArchivePaths();
expect(ChromiumArchivePathsInstance.archivesPath).toEqual(
expect.stringContaining('kibana/.chromium')
);
});
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/*
* 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.
* 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 path from 'path';
Expand Down Expand Up @@ -101,7 +102,7 @@ export class ChromiumArchivePaths {
];

// zip files get downloaded to a .chromium directory in the kibana root
public readonly archivesPath = path.resolve(__dirname, '../../../../../../.chromium');
public readonly archivesPath = path.resolve(__dirname, '../../../.chromium');

public find(platform: string, architecture: string, packages: PackageInfo[] = this.packages) {
return packages.find((p) => p.platform === platform && p.architecture === architecture);
Expand Down
21 changes: 21 additions & 0 deletions packages/kbn-screenshotting-server/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "target/types",
"types": [
"jest",
"node"
]
},
"include": [
"**/*.ts"
],
"exclude": [
"target/**/*"
],
"kbn_references": [
"@kbn/core",
"@kbn/safer-lodash-set",
"@kbn/config-schema"
]
}
2 changes: 2 additions & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -1422,6 +1422,8 @@
"@kbn/screenshotting-example-plugin/*": ["x-pack/examples/screenshotting_example/*"],
"@kbn/screenshotting-plugin": ["x-pack/plugins/screenshotting"],
"@kbn/screenshotting-plugin/*": ["x-pack/plugins/screenshotting/*"],
"@kbn/screenshotting-server": ["packages/kbn-screenshotting-server"],
"@kbn/screenshotting-server/*": ["packages/kbn-screenshotting-server/*"],
"@kbn/search-api-panels": ["packages/kbn-search-api-panels"],
"@kbn/search-api-panels/*": ["packages/kbn-search-api-panels/*"],
"@kbn/search-connectors": ["packages/kbn-search-connectors"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

import type { Logger } from '@kbn/logging';
import { ScreenshotModePluginSetup } from '@kbn/screenshot-mode-plugin/server';
import { ConfigType } from '@kbn/screenshotting-server';
import * as puppeteer from 'puppeteer';
import { Size } from '../../../common/layout';
import { ConfigType } from '../../config';
import { PreserveLayout } from '../../layouts/preserve_layout';
import { HeadlessChromiumDriver } from './driver';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import {
KBN_SCREENSHOT_MODE_HEADER,
ScreenshotModePluginSetup,
} from '@kbn/screenshot-mode-plugin/server';
import { ConfigType } from '@kbn/screenshotting-server';
import { truncate } from 'lodash';
import open from 'opn';
import { ElementHandle, Page, EvaluateFunc, HTTPResponse } from 'puppeteer';
import { ElementHandle, EvaluateFunc, HTTPResponse, Page } from 'puppeteer';
import { Subject } from 'rxjs';
import { parse as parseUrl } from 'url';
import { getDisallowedOutgoingUrlError } from '.';
import { ConfigType } from '../../config';
import { Layout } from '../../layouts';
import { getPrintLayoutSelectors } from '../../layouts/print_layout';
import { allowRequest } from '../network_policy';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import type { Logger } from '@kbn/core/server';
import { loggerMock } from '@kbn/logging-mocks';
import type { ScreenshotModePluginSetup } from '@kbn/screenshot-mode-plugin/server';
import { ConfigType } from '@kbn/screenshotting-server';
import * as puppeteer from 'puppeteer';
import * as Rx from 'rxjs';
import { mergeMap, take } from 'rxjs';
import { DEFAULT_VIEWPORT, HeadlessChromiumDriverFactory } from '.';
import { ConfigType } from '../../../config';

jest.mock('puppeteer');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,22 @@

import type { Logger } from '@kbn/core/server';
import type { ScreenshotModePluginSetup } from '@kbn/screenshot-mode-plugin/server';
import { ConfigType, args } from '@kbn/screenshotting-server';
import { getDataPath } from '@kbn/utils';
import { spawn } from 'child_process';
import del from 'del';
import fs from 'fs';
import { uniq } from 'lodash';
import path from 'path';
import puppeteer, { Browser, ConsoleMessage, Page, Viewport, PageEvents } from 'puppeteer';
import puppeteer, { Browser, ConsoleMessage, Page, PageEvents, Viewport } from 'puppeteer';
import { createInterface } from 'readline';
import * as Rx from 'rxjs';
import { catchError, concatMap, ignoreElements, mergeMap, map, reduce, takeUntil, tap } from 'rxjs';
import { PerformanceMetrics } from '../../../../common/types';
import { catchError, concatMap, ignoreElements, map, mergeMap, reduce, takeUntil, tap } from 'rxjs';
import { getChromiumDisconnectedError } from '..';
import { errors } from '../../../../common';
import { ConfigType } from '../../../config';
import { PerformanceMetrics } from '../../../../common/types';
import { safeChildProcess } from '../../safe_child_process';
import { HeadlessChromiumDriver } from '../driver';
import { args } from './args';
import { getMetrics } from './metrics';

interface CreatePageOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,3 @@ export const getDisallowedOutgoingUrlError = (interceptedUrl: string) =>
export { HeadlessChromiumDriver } from './driver';
export type { Context } from './driver';
export { DEFAULT_VIEWPORT, HeadlessChromiumDriverFactory } from './driver_factory';
export { ChromiumArchivePaths } from './paths';
export type { PackageInfo } from './paths';
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
*/

import { loggingSystemMock } from '@kbn/core-logging-server-mocks';
import { PackageInfo } from '@kbn/screenshotting-server';
import assert from 'assert';
import axios from 'axios';
import path from 'path';
import { PackageInfo } from '..';
import { paths as chromiumArchivePaths } from '../../../utils';
import { download } from '../../download';
import { install } from '../../install';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
* 2.0.
*/

import path from 'path';
import mockFs from 'mock-fs';
import { ChromiumArchivePaths, PackageInfo } from '@kbn/screenshotting-server';
import { access, readdir } from 'fs/promises';
import { ChromiumArchivePaths, PackageInfo } from '../chromium';
import { fetch } from './fetch';
import { sha256 } from './checksum';
import mockFs from 'mock-fs';
import path from 'path';
import { download } from '.';
import { sha256 } from './checksum';
import { fetch } from './fetch';

jest.mock('./checksum');
jest.mock('./fetch');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
* 2.0.
*/

import { access } from 'fs/promises';
import del from 'del';
import type { Logger } from '@kbn/core/server';
import type { ChromiumArchivePaths, PackageInfo } from '../chromium';
import type { ChromiumArchivePaths, PackageInfo } from '@kbn/screenshotting-server';
import del from 'del';
import { access } from 'fs/promises';
import { sha256 } from './checksum';
import { fetch } from './fetch';

Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/screenshotting/server/browsers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export { install } from './install';
export type { Context } from './chromium';
export {
getChromiumDisconnectedError,
ChromiumArchivePaths,
DEFAULT_VIEWPORT,
HeadlessChromiumDriver,
HeadlessChromiumDriverFactory,
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/screenshotting/server/browsers/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
* 2.0.
*/

import type { Logger } from '@kbn/core/server';
import { ChromiumArchivePaths, PackageInfo } from '@kbn/screenshotting-server';
import del from 'del';
import path from 'path';
import type { Logger } from '@kbn/core/server';
import { ChromiumArchivePaths, PackageInfo } from './chromium';
import { download } from './download';
import { sha256 } from './download/checksum';
import { extract } from './extract';
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/screenshotting/server/config/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { ConfigSchema } from './schema';
import { ConfigSchema } from '@kbn/screenshotting-server';

describe('ConfigSchema', () => {
it(`should produce correct config for context {"dev": false,"dist": false}`, () => {
Expand Down
Loading

0 comments on commit 5a522bf

Please sign in to comment.