diff --git a/.gitignore b/.gitignore index 02b20da297fc67..e7391a5c292d0e 100644 --- a/.gitignore +++ b/.gitignore @@ -29,6 +29,7 @@ disabledPlugins webpackstats.json /config/* !/config/kibana.yml +!/config/apm.js coverage selenium .babel_register_cache.json diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index e2a8459c2b01ab..17d2ca85a54ba5 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -24,6 +24,7 @@ A high level overview of our contributing guidelines. - [Internationalization](#internationalization) - [Testing and Building](#testing-and-building) - [Debugging server code](#debugging-server-code) + - [Instrumenting with Elastic APM](#instrumenting-with-elastic-apm) - [Debugging Unit Tests](#debugging-unit-tests) - [Unit Testing Plugins](#unit-testing-plugins) - [Cross-browser compatibility](#cross-browser-compatibility) @@ -374,6 +375,21 @@ macOS users on a machine with a discrete graphics card may see significant speed ### Debugging Server Code `yarn debug` will start the server with Node's inspect flag. Kibana's development mode will start three processes on ports `9229`, `9230`, and `9231`. Chrome's developer tools need to be configured to connect to all three connections. Add `localhost:` for each Kibana process in Chrome's developer tools connection tab. +### Instrumenting with Elastic APM +Kibana ships with the [Elastic APM Node.js Agent](https://github.com/elastic/apm-agent-nodejs) built-in for debugging purposes. + +Its default configuration is meant to be used by core Kibana developers only, but it can easily be re-configured to your needs. +In its default configuration it's disabled and will, once enabled, send APM data to a centrally managed Elasticsearch cluster accessible only to Elastic employees. + +To change the location where data is sent, use the [`serverUrl`](https://www.elastic.co/guide/en/apm/agent/nodejs/current/configuration.html#server-url) APM config option. +To activate the APM agent, use the [`active`](https://www.elastic.co/guide/en/apm/agent/nodejs/current/configuration.html#active) APM config option. + +All config options can be set either via environment variables, or by creating an appropriate config file under `config/apm.dev.js`. +For more information about configuring the APM agent, please refer to [the documentation](https://www.elastic.co/guide/en/apm/agent/nodejs/current/configuring-the-agent.html). + +Once the agent is active, it will trace all incoming HTTP requests to Kibana, monitor for errors, and collect process-level metrics. +The collected data will be sent to the APM Server and is viewable in the APM UI in Kibana. + ### Unit testing frameworks Kibana is migrating unit testing from Mocha to Jest. Legacy unit tests still exist in Mocha but all new unit tests should be written in Jest. Mocha tests @@ -389,7 +405,7 @@ The following table outlines possible test file locations and how to invoke them | Jest | `src/**/*.test.js`
`src/**/*.test.ts` | `node scripts/jest -t regexp [test path]` | | Jest (integration) | `**/integration_tests/**/*.test.js` | `node scripts/jest_integration -t regexp [test path]` | | Mocha | `src/**/__tests__/**/*.js`
`!src/**/public/__tests__/*.js`
`packages/kbn-datemath/test/**/*.js`
`packages/kbn-dev-utils/src/**/__tests__/**/*.js`
`tasks/**/__tests__/**/*.js` | `node scripts/mocha --grep=regexp [test path]` | -| Functional | `test/*integration/**/config.js`
`test/*functional/**/config.js` | `node scripts/functional_tests_server --config test/[directory]/config.js`
`node scripts/functional_test_runner --config test/[directory]/config.js --grep=regexp` | +| Functional | `test/*integration/**/config.js`
`test/*functional/**/config.js`
`test/accessibility/config.js` | `node scripts/functional_tests_server --config test/[directory]/config.js`
`node scripts/functional_test_runner --config test/[directory]/config.js --grep=regexp` | | Karma | `src/**/public/__tests__/*.js` | `npm run test:dev` | For X-Pack tests located in `x-pack/` see [X-Pack Testing](x-pack/README.md#testing) diff --git a/config/apm.js b/config/apm.js new file mode 100644 index 00000000000000..8efbbf87487e36 --- /dev/null +++ b/config/apm.js @@ -0,0 +1,81 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * DO NOT EDIT THIS FILE! + * + * This file contains the configuration for the Elastic APM instrumentaion of + * Kibana itself and is only intented to be used during development of Kibana. + * + * Instrumentation is turned off by default. Once activated it will send APM + * data to an Elasticsearch cluster accessible by Elastic employees. + * + * To modify the configuration, either use environment variables, or create a + * file named `config/apm.dev.js`, which exports a config object as described + * in the docs. + * + * For an overview over the available configuration files, see: + * https://www.elastic.co/guide/en/apm/agent/nodejs/current/configuration.html + * + * For general information about Elastic APM, see: + * https://www.elastic.co/guide/en/apm/get-started/current/index.html + */ + +const { readFileSync } = require('fs'); +const { join } = require('path'); +const { execSync } = require('child_process'); +const merge = require('lodash.merge'); + +module.exports = merge({ + active: false, + serverUrl: 'https://f1542b814f674090afd914960583265f.apm.us-central1.gcp.cloud.es.io:443', + // The secretToken below is intended to be hardcoded in this file even though + // it makes it public. This is not a security/privacy issue. Normally we'd + // instead disable the need for a secretToken in the APM Server config where + // the data is transmitted to, but due to how it's being hosted, it's easier, + // for now, to simply leave it in. + secretToken: 'R0Gjg46pE9K9wGestd', + globalLabels: {}, + centralConfig: false, + logUncaughtExceptions: true +}, devConfig()); + +const rev = gitRev(); +if (rev !== null) module.exports.globalLabels.git_rev = rev; + +try { + const filename = join(__dirname, '..', 'data', 'uuid'); + module.exports.globalLabels.kibana_uuid = readFileSync(filename, 'utf-8'); +} catch (e) {} // eslint-disable-line no-empty + +function gitRev() { + try { + return execSync('git rev-parse --short HEAD', { encoding: 'utf-8' }).trim(); + } catch (e) { + return null; + } +} + +function devConfig() { + try { + return require('./apm.dev'); // eslint-disable-line import/no-unresolved + } catch (e) { + return {}; + } +} diff --git a/docs/developer/core/development-functional-tests.asciidoc b/docs/developer/core/development-functional-tests.asciidoc index 6d2c5a72f0532b..350a3c2a997cf9 100644 --- a/docs/developer/core/development-functional-tests.asciidoc +++ b/docs/developer/core/development-functional-tests.asciidoc @@ -98,6 +98,7 @@ When run without any arguments the `FunctionalTestRunner` automatically loads th * `--config test/functional/config.js` starts Elasticsearch and Kibana servers with the WebDriver tests configured to run in Chrome. * `--config test/functional/config.firefox.js` starts Elasticsearch and Kibana servers with the WebDriver tests configured to run in Firefox. * `--config test/api_integration/config.js` starts Elasticsearch and Kibana servers with the api integration tests configuration. +* `--config test/accessibility/config.ts` starts Elasticsearch and Kibana servers with the WebDriver tests configured to run an accessibility audit using https://www.deque.com/axe/[axe]. There are also command line flags for `--bail` and `--grep`, which behave just like their mocha counterparts. For instance, use `--grep=foo` to run only tests that match a regular expression. @@ -362,7 +363,7 @@ Full list of services that are used in functional tests can be found here: {blob ** Source: {blob}test/functional/services/remote/remote.ts[test/functional/services/remote/remote.ts] ** Instance of https://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/index_exports_WebDriver.html[WebDriver] class ** Responsible for all communication with the browser -** To perform browser actions, use `remote` service +** To perform browser actions, use `remote` service ** For searching and manipulating with DOM elements, use `testSubjects` and `find` services ** See the https://seleniumhq.github.io/selenium/docs/api/javascript/[selenium-webdriver docs] for the full API. diff --git a/package.json b/package.json index ea6276496e84da..1ff42384f95d51 100644 --- a/package.json +++ b/package.json @@ -53,7 +53,7 @@ "typespec": "typings-tester --config x-pack/legacy/plugins/canvas/public/lib/aeroelastic/tsconfig.json x-pack/legacy/plugins/canvas/public/lib/aeroelastic/__fixtures__/typescript/typespec_tests.ts", "checkLicenses": "node scripts/check_licenses --dev", "build": "node scripts/build --all-platforms", - "start": "node --trace-warnings --trace-deprecation scripts/kibana --dev ", + "start": "node --trace-warnings --throw-deprecation scripts/kibana --dev", "debug": "node --nolazy --inspect scripts/kibana --dev", "debug-break": "node --nolazy --inspect-brk scripts/kibana --dev", "karma": "karma start", @@ -159,6 +159,7 @@ "d3-cloud": "1.2.5", "deepmerge": "^4.2.2", "del": "^5.1.0", + "elastic-apm-node": "^3.2.0", "elasticsearch": "^16.5.0", "elasticsearch-browser": "^16.5.0", "encode-uri-query": "1.0.1", @@ -203,6 +204,7 @@ "minimatch": "^3.0.4", "moment": "^2.24.0", "moment-timezone": "^0.5.27", + "monaco-editor": "~0.17.0", "mustache": "2.3.2", "ngreact": "0.5.1", "node-fetch": "1.7.3", @@ -221,7 +223,9 @@ "react-grid-layout": "^0.16.2", "react-input-range": "^1.3.0", "react-markdown": "^3.4.1", + "react-monaco-editor": "~0.27.0", "react-redux": "^5.1.2", + "react-resize-detector": "^4.2.0", "react-router-dom": "^4.3.1", "react-sizeme": "^2.3.6", "reactcss": "1.2.3", @@ -334,6 +338,7 @@ "@types/react": "^16.9.11", "@types/react-dom": "^16.9.4", "@types/react-redux": "^6.0.6", + "@types/react-resize-detector": "^4.0.1", "@types/react-router-dom": "^4.3.1", "@types/react-virtualized": "^9.18.7", "@types/redux": "^3.6.31", @@ -405,7 +410,6 @@ "gulp-sourcemaps": "2.6.5", "has-ansi": "^3.0.0", "iedriver": "^3.14.1", - "image-diff": "1.6.3", "intl-messageformat-parser": "^1.4.0", "is-path-inside": "^2.1.0", "istanbul-instrumenter-loader": "3.0.1", @@ -433,7 +437,7 @@ "node-sass": "^4.9.4", "normalize-path": "^3.0.0", "nyc": "^14.1.1", - "pixelmatch": "4.0.2", + "pixelmatch": "^5.1.0", "pkg-up": "^2.0.0", "pngjs": "^3.4.0", "postcss": "^7.0.5", diff --git a/packages/kbn-babel-code-parser/src/strategies.js b/packages/kbn-babel-code-parser/src/strategies.js index 89621bc53bd534..f116abde9e0e6d 100644 --- a/packages/kbn-babel-code-parser/src/strategies.js +++ b/packages/kbn-babel-code-parser/src/strategies.js @@ -20,6 +20,7 @@ import { canRequire } from './can_require'; import { dependenciesVisitorsGenerator } from './visitors'; import { dirname, isAbsolute, resolve } from 'path'; +import { builtinModules } from 'module'; export function _calculateTopLevelDependency(inputDep, outputDep = '') { // The path separator will be always the forward slash @@ -48,14 +49,18 @@ export function _calculateTopLevelDependency(inputDep, outputDep = '') { return _calculateTopLevelDependency(depSplitPaths.join(pathSeparator), outputDep); } -export async function dependenciesParseStrategy(cwd, parseSingleFile, mainEntry, wasParsed, results) { - // Retrieve native nodeJS modules - const natives = process.binding('natives'); - +export async function dependenciesParseStrategy( + cwd, + parseSingleFile, + mainEntry, + wasParsed, + results +) { // Get dependencies from a single file and filter // out node native modules from the result - const dependencies = (await parseSingleFile(mainEntry, dependenciesVisitorsGenerator)) - .filter(dep => !natives[dep]); + const dependencies = (await parseSingleFile(mainEntry, dependenciesVisitorsGenerator)).filter( + dep => !builtinModules.includes(dep) + ); // Return the list of all the new entries found into // the current mainEntry that we could use to look for diff --git a/packages/kbn-plugin-helpers/tasks/build/rewrite_package_json.js b/packages/kbn-plugin-helpers/tasks/build/rewrite_package_json.js index db33b209951ebd..64656baee6fd2b 100644 --- a/packages/kbn-plugin-helpers/tasks/build/rewrite_package_json.js +++ b/packages/kbn-plugin-helpers/tasks/build/rewrite_package_json.js @@ -41,19 +41,9 @@ module.exports = function rewritePackage(buildSource, buildVersion, kibanaVersio delete pkg.scripts; delete pkg.devDependencies; - file.contents = toBuffer(JSON.stringify(pkg, null, 2)); + file.contents = Buffer.from(JSON.stringify(pkg, null, 2)); } return file; }); }; - -function toBuffer(string) { - if (typeof Buffer.from === 'function') { - return Buffer.from(string, 'utf8'); - } else { - // this was deprecated in node v5 in favor - // of Buffer.from(string, encoding) - return new Buffer(string, 'utf8'); - } -} diff --git a/scripts/kibana.js b/scripts/kibana.js index b1b470a37535fc..f5a63e6c07dd60 100644 --- a/scripts/kibana.js +++ b/scripts/kibana.js @@ -17,5 +17,6 @@ * under the License. */ +require('../src/apm')(process.env.ELASTIC_APM_PROXY_SERVICE_NAME || 'kibana-proxy'); require('../src/setup_node_env'); require('../src/cli/cli'); diff --git a/src/apm.js b/src/apm.js new file mode 100644 index 00000000000000..04a70ee71c53ec --- /dev/null +++ b/src/apm.js @@ -0,0 +1,39 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +const { existsSync } = require('fs'); +const { join } = require('path'); +const { name, version } = require('../package.json'); + +module.exports = function (serviceName = name) { + if (process.env.kbnWorkerType === 'optmzr') return; + + const conf = { + serviceName: `${serviceName}-${version.replace(/\./g, '_')}` + }; + + if (configFileExists()) conf.configFile = 'config/apm.js'; + else conf.active = false; + + require('elastic-apm-node').start(conf); +}; + +function configFileExists() { + return existsSync(join(__dirname, '..', 'config', 'apm.js')); +} diff --git a/src/cli/index.js b/src/cli/index.js index 4af5e3c68423cd..45f88eaf82a5b8 100644 --- a/src/cli/index.js +++ b/src/cli/index.js @@ -17,5 +17,6 @@ * under the License. */ +require('../apm')(); require('../setup_node_env'); require('./cli'); diff --git a/src/core/CONVENTIONS.md b/src/core/CONVENTIONS.md index fbe2740b961088..786409614e6d94 100644 --- a/src/core/CONVENTIONS.md +++ b/src/core/CONVENTIONS.md @@ -23,6 +23,8 @@ my_plugin/ └── server ├── routes │ └── index.ts + ├── collectors + │ └── register.ts    ├── services    │   ├── my_service    │   │ └── index.ts @@ -30,7 +32,6 @@ my_plugin/    ├── index.ts    └── plugin.ts ``` - - Both `server` and `public` should have an `index.ts` and a `plugin.ts` file: - `index.ts` should only contain: - The `plugin` export @@ -44,8 +45,9 @@ my_plugin/ - If there is only a single application, this directory can be called `application` that exports the `renderApp` function. - Services provided to other plugins as APIs should live inside the `services` subdirectory. - Services should model the plugin lifecycle (more details below). -- HTTP routes should be contained inside the `routes` directory. +- HTTP routes should be contained inside the `server/routes` directory. - More should be fleshed out here... +- Usage collectors for Telemetry should be defined in a separate `server/collectors/` directory. ### The PluginInitializer @@ -213,7 +215,7 @@ export class Plugin { ### Usage Collection -For creating and registering a Usage Collector. Collectors would be defined in a separate directory `server/collectors/register.ts`. You can read more about usage collectors on `src/plugins/usage_collection/README.md`. +For creating and registering a Usage Collector. Collectors should be defined in a separate directory `server/collectors/`. You can read more about usage collectors on `src/plugins/usage_collection/README.md`. ```ts // server/collectors/register.ts @@ -247,3 +249,8 @@ export function registerMyPluginUsageCollector(usageCollection?: UsageCollection usageCollection.registerCollector(myCollector); } ``` + +### Naming conventions + +Export start and setup contracts as `MyPluginStart` and `MyPluginSetup`. +This avoids naming clashes, if everyone exported them simply as `Start` and `Setup`. diff --git a/src/core/MIGRATION.md b/src/core/MIGRATION.md index 7c1489a345e55e..6cb66b17c2e03d 100644 --- a/src/core/MIGRATION.md +++ b/src/core/MIGRATION.md @@ -93,6 +93,7 @@ src/plugins "ui": true } ``` +More details about[manifest file format](/docs/development/core/server/kibana-plugin-server.pluginmanifest.md) Note that `package.json` files are irrelevant to and ignored by the new platform. @@ -1140,7 +1141,6 @@ import { npStart: { core } } from 'ui/new_platform'; | `chrome.getUiSettingsClient` | [`core.uiSettings`](/docs/development/core/public/kibana-plugin-public.uisettingsclient.md) | | | `chrome.helpExtension.set` | [`core.chrome.setHelpExtension`](/docs/development/core/public/kibana-plugin-public.chromestart.sethelpextension.md) | | | `chrome.setVisible` | [`core.chrome.setIsVisible`](/docs/development/core/public/kibana-plugin-public.chromestart.setisvisible.md) | | -| `chrome.getInjected` | -- | Not implemented yet, see [#41990](https://github.com/elastic/kibana/issues/41990) | | `chrome.setRootTemplate` / `chrome.setRootController` | -- | Use application mounting via `core.application.register` (not available to legacy plugins at this time). | | `import { recentlyAccessed } from 'ui/persisted_log'` | [`core.chrome.recentlyAccessed`](/docs/development/core/public/kibana-plugin-public.chromerecentlyaccessed.md) | | | `ui/capabilities` | [`core.application.capabilities`](/docs/development/core/public/kibana-plugin-public.capabilities.md) | | @@ -1150,7 +1150,7 @@ import { npStart: { core } } from 'ui/new_platform'; | `ui/routes` | -- | There is no global routing mechanism. Each app [configures its own routing](/rfcs/text/0004_application_service_mounting.md#complete-example). | | `ui/saved_objects` | [`core.savedObjects`](/docs/development/core/public/kibana-plugin-public.savedobjectsstart.md) | Client API is the same | | `ui/doc_title` | [`core.chrome.docTitle`](/docs/development/core/public/kibana-plugin-public.chromedoctitle.md) | | -| `uiExports/injectedVars` | [Configure plugin](#configure-plugin) and [`PluginConfigDescriptor.exposeToBrowser`](/docs/development/core/server/kibana-plugin-server.pluginconfigdescriptor.exposetobrowser.md) | Can only be used to expose configuration properties | +| `uiExports/injectedVars` / `chrome.getInjected` | [Configure plugin](#configure-plugin) and [`PluginConfigDescriptor.exposeToBrowser`](/docs/development/core/server/kibana-plugin-server.pluginconfigdescriptor.exposetobrowser.md) | Can only be used to expose configuration properties | _See also: [Public's CoreStart API Docs](/docs/development/core/public/kibana-plugin-public.corestart.md)_ @@ -1352,6 +1352,13 @@ export class Plugin implements Plugin { } ``` +All plugins are considered enabled by default. If you want to disable your plugin by default, you could declare the `enabled` flag in plugin config. This is a special Kibana platform key. The platform reads its value and won't create a plugin instance if `enabled: false`. +```js +export const config = { + schema: schema.object({ enabled: schema.boolean({ defaultValue: false }) }), +}; +``` + ### Mock new platform services in tests #### Writing mocks for your plugin diff --git a/src/core/server/http/base_path_proxy_server.ts b/src/core/server/http/base_path_proxy_server.ts index cde35f3cbe995a..276e3955a4678b 100644 --- a/src/core/server/http/base_path_proxy_server.ts +++ b/src/core/server/http/base_path_proxy_server.ts @@ -17,6 +17,8 @@ * under the License. */ +import apm from 'elastic-apm-node'; + import { ByteSizeValue } from '@kbn/config-schema'; import { Server, Request } from 'hapi'; import Url from 'url'; @@ -139,6 +141,7 @@ export class BasePathProxyServer { // Before we proxy request to a target port we may want to wait until some // condition is met (e.g. until target listener is ready). async (request, responseToolkit) => { + apm.setTransactionName(`${request.method.toUpperCase()} /{basePath}/{kbnPath*}`); await blockUntil(); return responseToolkit.continue; }, diff --git a/src/core/server/http/integration_tests/router.test.ts b/src/core/server/http/integration_tests/router.test.ts index 7d95110b98a129..6117190c57ba89 100644 --- a/src/core/server/http/integration_tests/router.test.ts +++ b/src/core/server/http/integration_tests/router.test.ts @@ -329,7 +329,7 @@ describe('Response factory', () => { const router = createRouter('/'); router.get({ path: '/', validate: false }, (context, req, res) => { - const buffer = new Buffer('abc'); + const buffer = Buffer.from('abc'); return res.ok({ body: buffer, diff --git a/src/core/server/saved_objects/service/lib/filter_utils.test.ts b/src/core/server/saved_objects/service/lib/filter_utils.test.ts index 9ae4b32202823d..4d9bcdda3c8ae9 100644 --- a/src/core/server/saved_objects/service/lib/filter_utils.test.ts +++ b/src/core/server/saved_objects/service/lib/filter_utils.test.ts @@ -49,6 +49,28 @@ const mockMappings = { }, }, }, + alert: { + properties: { + actions: { + type: 'nested', + properties: { + group: { + type: 'keyword', + }, + actionRef: { + type: 'keyword', + }, + actionTypeId: { + type: 'keyword', + }, + params: { + enabled: false, + type: 'object', + }, + }, + }, + }, + }, hiddenType: { properties: { description: { @@ -108,6 +130,16 @@ describe('Filter Utils', () => { ); }); + test('Assemble filter with a nested filter', () => { + expect( + validateConvertFilterToKueryNode( + ['alert'], + 'alert.attributes.actions:{ actionTypeId: ".server-log" }', + mockMappings + ) + ).toEqual(esKuery.fromKueryExpression('alert.actions:{ actionTypeId: ".server-log" }')); + }); + test('Lets make sure that we are throwing an exception if we get an error', () => { expect(() => { validateConvertFilterToKueryNode( @@ -129,13 +161,13 @@ describe('Filter Utils', () => { describe('#validateFilterKueryNode', () => { test('Validate filter query through KueryNode - happy path', () => { - const validationObject = validateFilterKueryNode( - esKuery.fromKueryExpression( + const validationObject = validateFilterKueryNode({ + astFilter: esKuery.fromKueryExpression( 'foo.updatedAt: 5678654567 and foo.attributes.bytes > 1000 and foo.attributes.bytes < 8000 and foo.attributes.title: "best" and (foo.attributes.description: t* or foo.attributes.description :*)' ), - ['foo'], - mockMappings - ); + types: ['foo'], + indexMapping: mockMappings, + }); expect(validationObject).toEqual([ { @@ -183,14 +215,34 @@ describe('Filter Utils', () => { ]); }); + test('Validate nested filter query through KueryNode - happy path', () => { + const validationObject = validateFilterKueryNode({ + astFilter: esKuery.fromKueryExpression( + 'alert.attributes.actions:{ actionTypeId: ".server-log" }' + ), + types: ['alert'], + indexMapping: mockMappings, + hasNestedKey: true, + }); + expect(validationObject).toEqual([ + { + astPath: 'arguments.1', + error: null, + isSavedObjectAttr: false, + key: 'alert.attributes.actions.actionTypeId', + type: 'alert', + }, + ]); + }); + test('Return Error if key is not wrapper by a saved object type', () => { - const validationObject = validateFilterKueryNode( - esKuery.fromKueryExpression( + const validationObject = validateFilterKueryNode({ + astFilter: esKuery.fromKueryExpression( 'updatedAt: 5678654567 and foo.attributes.bytes > 1000 and foo.attributes.bytes < 8000 and foo.attributes.title: "best" and (foo.attributes.description: t* or foo.attributes.description :*)' ), - ['foo'], - mockMappings - ); + types: ['foo'], + indexMapping: mockMappings, + }); expect(validationObject).toEqual([ { @@ -239,13 +291,13 @@ describe('Filter Utils', () => { }); test('Return Error if key of a saved object type is not wrapped with attributes', () => { - const validationObject = validateFilterKueryNode( - esKuery.fromKueryExpression( + const validationObject = validateFilterKueryNode({ + astFilter: esKuery.fromKueryExpression( 'foo.updatedAt: 5678654567 and foo.attributes.bytes > 1000 and foo.bytes < 8000 and foo.attributes.title: "best" and (foo.attributes.description: t* or foo.description :*)' ), - ['foo'], - mockMappings - ); + types: ['foo'], + indexMapping: mockMappings, + }); expect(validationObject).toEqual([ { @@ -296,13 +348,13 @@ describe('Filter Utils', () => { }); test('Return Error if filter is not using an allowed type', () => { - const validationObject = validateFilterKueryNode( - esKuery.fromKueryExpression( + const validationObject = validateFilterKueryNode({ + astFilter: esKuery.fromKueryExpression( 'bar.updatedAt: 5678654567 and foo.attributes.bytes > 1000 and foo.attributes.bytes < 8000 and foo.attributes.title: "best" and (foo.attributes.description: t* or foo.attributes.description :*)' ), - ['foo'], - mockMappings - ); + types: ['foo'], + indexMapping: mockMappings, + }); expect(validationObject).toEqual([ { @@ -351,13 +403,13 @@ describe('Filter Utils', () => { }); test('Return Error if filter is using an non-existing key in the index patterns of the saved object type', () => { - const validationObject = validateFilterKueryNode( - esKuery.fromKueryExpression( + const validationObject = validateFilterKueryNode({ + astFilter: esKuery.fromKueryExpression( 'foo.updatedAt33: 5678654567 and foo.attributes.bytes > 1000 and foo.attributes.bytes < 8000 and foo.attributes.header: "best" and (foo.attributes.description: t* or foo.attributes.description :*)' ), - ['foo'], - mockMappings - ); + types: ['foo'], + indexMapping: mockMappings, + }); expect(validationObject).toEqual([ { @@ -407,11 +459,12 @@ describe('Filter Utils', () => { }); test('Return Error if filter is using an non-existing key null key', () => { - const validationObject = validateFilterKueryNode( - esKuery.fromKueryExpression('foo.attributes.description: hello AND bye'), - ['foo'], - mockMappings - ); + const validationObject = validateFilterKueryNode({ + astFilter: esKuery.fromKueryExpression('foo.attributes.description: hello AND bye'), + types: ['foo'], + indexMapping: mockMappings, + }); + expect(validationObject).toEqual([ { astPath: 'arguments.0', diff --git a/src/core/server/saved_objects/service/lib/filter_utils.ts b/src/core/server/saved_objects/service/lib/filter_utils.ts index e7509933a38aa7..9d796c279a7701 100644 --- a/src/core/server/saved_objects/service/lib/filter_utils.ts +++ b/src/core/server/saved_objects/service/lib/filter_utils.ts @@ -23,6 +23,8 @@ import { IndexMapping } from '../../mappings'; // eslint-disable-next-line @kbn/eslint/no-restricted-paths import { esKuery } from '../../../../../plugins/data/server'; +const astFunctionType = ['is', 'range', 'nested']; + export const validateConvertFilterToKueryNode = ( allowedTypes: string[], filter: string, @@ -31,12 +33,14 @@ export const validateConvertFilterToKueryNode = ( if (filter && filter.length > 0 && indexMapping) { const filterKueryNode = esKuery.fromKueryExpression(filter); - const validationFilterKuery = validateFilterKueryNode( - filterKueryNode, - allowedTypes, + const validationFilterKuery = validateFilterKueryNode({ + astFilter: filterKueryNode, + types: allowedTypes, indexMapping, - filterKueryNode.type === 'function' && ['is', 'range'].includes(filterKueryNode.function) - ); + storeValue: + filterKueryNode.type === 'function' && astFunctionType.includes(filterKueryNode.function), + hasNestedKey: filterKueryNode.type === 'function' && filterKueryNode.function === 'nested', + }); if (validationFilterKuery.length === 0) { throw SavedObjectsErrorHelpers.createBadRequestError( @@ -90,26 +94,44 @@ interface ValidateFilterKueryNode { type: string | null; } -export const validateFilterKueryNode = ( - astFilter: esKuery.KueryNode, - types: string[], - indexMapping: IndexMapping, - storeValue: boolean = false, - path: string = 'arguments' -): ValidateFilterKueryNode[] => { +interface ValidateFilterKueryNodeParams { + astFilter: esKuery.KueryNode; + types: string[]; + indexMapping: IndexMapping; + hasNestedKey?: boolean; + nestedKeys?: string; + storeValue?: boolean; + path?: string; +} + +export const validateFilterKueryNode = ({ + astFilter, + types, + indexMapping, + hasNestedKey = false, + nestedKeys, + storeValue = false, + path = 'arguments', +}: ValidateFilterKueryNodeParams): ValidateFilterKueryNode[] => { + let localNestedKeys: string | undefined; return astFilter.arguments.reduce( (kueryNode: string[], ast: esKuery.KueryNode, index: number) => { + if (hasNestedKey && ast.type === 'literal' && ast.value != null) { + localNestedKeys = ast.value; + } if (ast.arguments) { const myPath = `${path}.${index}`; return [ ...kueryNode, - ...validateFilterKueryNode( - ast, + ...validateFilterKueryNode({ + astFilter: ast, types, indexMapping, - ast.type === 'function' && ['is', 'range'].includes(ast.function), - `${myPath}.arguments` - ), + storeValue: ast.type === 'function' && astFunctionType.includes(ast.function), + path: `${myPath}.arguments`, + hasNestedKey: ast.type === 'function' && ast.function === 'nested', + nestedKeys: localNestedKeys, + }), ]; } if (storeValue && index === 0) { @@ -118,10 +140,17 @@ export const validateFilterKueryNode = ( ...kueryNode, { astPath: splitPath.slice(0, splitPath.length - 1).join('.'), - error: hasFilterKeyError(ast.value, types, indexMapping), - isSavedObjectAttr: isSavedObjectAttr(ast.value, indexMapping), - key: ast.value, - type: getType(ast.value), + error: hasFilterKeyError( + nestedKeys != null ? `${nestedKeys}.${ast.value}` : ast.value, + types, + indexMapping + ), + isSavedObjectAttr: isSavedObjectAttr( + nestedKeys != null ? `${nestedKeys}.${ast.value}` : ast.value, + indexMapping + ), + key: nestedKeys != null ? `${nestedKeys}.${ast.value}` : ast.value, + type: getType(nestedKeys != null ? `${nestedKeys}.${ast.value}` : ast.value), }, ]; } @@ -164,7 +193,6 @@ export const hasFilterKeyError = ( return `This key '${key}' need to be wrapped by a saved object type like ${types.join()}`; } else if (key.includes('.')) { const keySplit = key.split('.'); - if (keySplit.length <= 1 || !types.includes(keySplit[0])) { return `This type ${keySplit[0]} is not allowed`; } @@ -177,7 +205,10 @@ export const hasFilterKeyError = ( if ( (keySplit.length === 2 && !fieldDefined(indexMapping, keySplit[1])) || (keySplit.length > 2 && - !fieldDefined(indexMapping, keySplit[0] + '.' + keySplit.slice(2, keySplit.length))) + !fieldDefined( + indexMapping, + `${keySplit[0]}.${keySplit.slice(2, keySplit.length).join('.')}` + )) ) { return `This key '${key}' does NOT exist in ${types.join()} saved object index patterns`; } diff --git a/src/dev/build/tasks/copy_source_task.js b/src/dev/build/tasks/copy_source_task.js index e487ac0567f766..a693c6ce8a8a6f 100644 --- a/src/dev/build/tasks/copy_source_task.js +++ b/src/dev/build/tasks/copy_source_task.js @@ -46,6 +46,7 @@ export const CopySourceTask = { 'typings/**', 'webpackShims/**', 'config/kibana.yml', + 'config/apm.js', 'tsconfig*.json', '.i18nrc.json', 'kibana.d.ts' diff --git a/src/dev/ci_setup/setup_env.sh b/src/dev/ci_setup/setup_env.sh index 6cfcaca5843b3e..e5edf5bd9c2609 100644 --- a/src/dev/ci_setup/setup_env.sh +++ b/src/dev/ci_setup/setup_env.sh @@ -14,6 +14,8 @@ cacheDir="$HOME/.kibana" RED='\033[0;31m' C_RESET='\033[0m' # Reset color +export NODE_OPTIONS="$NODE_OPTIONS --throw-deprecation" + ### ### Since the Jenkins logging output collector doesn't look like a TTY ### Node/Chalk and other color libs disable their color output. But Jenkins diff --git a/src/dev/jest/config.js b/src/dev/jest/config.js index f5c20da89dcfaf..23bba78e052729 100644 --- a/src/dev/jest/config.js +++ b/src/dev/jest/config.js @@ -98,8 +98,9 @@ export default { '^.+\\.html?$': 'jest-raw-loader', }, transformIgnorePatterns: [ - // ignore all node_modules except @elastic/eui which requires babel transforms to handle dynamic import() - '[/\\\\]node_modules(?![\\/\\\\]@elastic[\\/\\\\]eui)[/\\\\].+\\.js$', + // ignore all node_modules except @elastic/eui and monaco-editor which both require babel transforms to handle dynamic import() + // since ESM modules are not natively supported in Jest yet (https://github.com/facebook/jest/issues/4842) + '[/\\\\]node_modules(?![\\/\\\\]@elastic[\\/\\\\]eui)(?![\\/\\\\]monaco-editor)[/\\\\].+\\.js$', 'packages/kbn-pm/dist/index.js' ], snapshotSerializers: [ diff --git a/src/legacy/core_plugins/console/np_ready/public/application/containers/editor/legacy/console_editor/editor.tsx b/src/legacy/core_plugins/console/np_ready/public/application/containers/editor/legacy/console_editor/editor.tsx index 0fa0ec732c7705..b99249b2b0016b 100644 --- a/src/legacy/core_plugins/console/np_ready/public/application/containers/editor/legacy/console_editor/editor.tsx +++ b/src/legacy/core_plugins/console/np_ready/public/application/containers/editor/legacy/console_editor/editor.tsx @@ -193,12 +193,20 @@ function EditorUI({ previousStateLocation = 'stored' }: EditorProps) { /> -
+ + {/* Axe complains about Ace's textarea element missing a label, which interferes with our + automated a11y tests per #52136. This wrapper does nothing to address a11y but it does + satisfy Axe. */} + + {/* eslint-disable-next-line jsx-a11y/label-has-associated-control */} +
); diff --git a/src/legacy/core_plugins/console/np_ready/public/application/containers/editor/legacy/console_editor/editor_output.tsx b/src/legacy/core_plugins/console/np_ready/public/application/containers/editor/legacy/console_editor/editor_output.tsx index c167155bd18a91..3690ea61d56842 100644 --- a/src/legacy/core_plugins/console/np_ready/public/application/containers/editor/legacy/console_editor/editor_output.tsx +++ b/src/legacy/core_plugins/console/np_ready/public/application/containers/editor/legacy/console_editor/editor_output.tsx @@ -87,7 +87,14 @@ function EditorOutputUI() { return (
-
+ {/* Axe complains about Ace's textarea element missing a label, which interferes with our + automated a11y tests per #52136. This wrapper does nothing to address a11y but it does + satisfy Axe. */} + + {/* eslint-disable-next-line jsx-a11y/label-has-associated-control */} +