diff --git a/.github/workflows/pr-project-assigner.yml b/.github/workflows/pr-project-assigner.yml index 517aefb36e8d65..4058fcaadee5da 100644 --- a/.github/workflows/pr-project-assigner.yml +++ b/.github/workflows/pr-project-assigner.yml @@ -13,8 +13,9 @@ jobs: with: issue-mappings: | [ - { "label": "Team:AppArch", "projectName": "kibana-app-arch", "columnId": 6173897 }, - { "label": "Feature:Lens", "projectName": "Lens", "columnId": 6219362 }, - { "label": "Team:Canvas", "projectName": "canvas", "columnId": 6187580 } ] ghToken: ${{ secrets.PROJECT_ASSIGNER_TOKEN }} + +# { "label": "Team:AppArch", "projectName": "kibana-app-arch", "columnId": 6173897 }, +# { "label": "Feature:Lens", "projectName": "Lens", "columnId": 6219362 }, +# { "label": "Team:Canvas", "projectName": "canvas", "columnId": 6187580 } diff --git a/docs/development/core/server/kibana-plugin-server.authnothandled.md b/docs/development/core/server/kibana-plugin-server.authnothandled.md new file mode 100644 index 00000000000000..01e465c266319b --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.authnothandled.md @@ -0,0 +1,19 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [AuthNotHandled](./kibana-plugin-server.authnothandled.md) + +## AuthNotHandled interface + + +Signature: + +```typescript +export interface AuthNotHandled +``` + +## Properties + +| Property | Type | Description | +| --- | --- | --- | +| [type](./kibana-plugin-server.authnothandled.type.md) | AuthResultType.notHandled | | + diff --git a/docs/development/core/server/kibana-plugin-server.authnothandled.type.md b/docs/development/core/server/kibana-plugin-server.authnothandled.type.md new file mode 100644 index 00000000000000..81543de0ec61b5 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.authnothandled.type.md @@ -0,0 +1,11 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [AuthNotHandled](./kibana-plugin-server.authnothandled.md) > [type](./kibana-plugin-server.authnothandled.type.md) + +## AuthNotHandled.type property + +Signature: + +```typescript +type: AuthResultType.notHandled; +``` diff --git a/docs/development/core/server/kibana-plugin-server.authredirected.md b/docs/development/core/server/kibana-plugin-server.authredirected.md new file mode 100644 index 00000000000000..3eb88d6c5a2307 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.authredirected.md @@ -0,0 +1,19 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [AuthRedirected](./kibana-plugin-server.authredirected.md) + +## AuthRedirected interface + + +Signature: + +```typescript +export interface AuthRedirected extends AuthRedirectedParams +``` + +## Properties + +| Property | Type | Description | +| --- | --- | --- | +| [type](./kibana-plugin-server.authredirected.type.md) | AuthResultType.redirected | | + diff --git a/docs/development/core/server/kibana-plugin-server.authredirected.type.md b/docs/development/core/server/kibana-plugin-server.authredirected.type.md new file mode 100644 index 00000000000000..866ed358119e7d --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.authredirected.type.md @@ -0,0 +1,11 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [AuthRedirected](./kibana-plugin-server.authredirected.md) > [type](./kibana-plugin-server.authredirected.type.md) + +## AuthRedirected.type property + +Signature: + +```typescript +type: AuthResultType.redirected; +``` diff --git a/docs/development/core/server/kibana-plugin-server.authredirectedparams.headers.md b/docs/development/core/server/kibana-plugin-server.authredirectedparams.headers.md new file mode 100644 index 00000000000000..c1cf8218e75094 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.authredirectedparams.headers.md @@ -0,0 +1,15 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [AuthRedirectedParams](./kibana-plugin-server.authredirectedparams.md) > [headers](./kibana-plugin-server.authredirectedparams.headers.md) + +## AuthRedirectedParams.headers property + +Headers to attach for auth redirect. Must include "location" header + +Signature: + +```typescript +headers: { + location: string; + } & ResponseHeaders; +``` diff --git a/docs/development/core/server/kibana-plugin-server.authredirectedparams.md b/docs/development/core/server/kibana-plugin-server.authredirectedparams.md new file mode 100644 index 00000000000000..3658f88fb64950 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.authredirectedparams.md @@ -0,0 +1,20 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [AuthRedirectedParams](./kibana-plugin-server.authredirectedparams.md) + +## AuthRedirectedParams interface + +Result of auth redirection. + +Signature: + +```typescript +export interface AuthRedirectedParams +``` + +## Properties + +| Property | Type | Description | +| --- | --- | --- | +| [headers](./kibana-plugin-server.authredirectedparams.headers.md) | {
location: string;
} & ResponseHeaders | Headers to attach for auth redirect. Must include "location" header | + diff --git a/docs/development/core/server/kibana-plugin-server.authresult.md b/docs/development/core/server/kibana-plugin-server.authresult.md index 8739c4899bd02a..f540173f34c7ca 100644 --- a/docs/development/core/server/kibana-plugin-server.authresult.md +++ b/docs/development/core/server/kibana-plugin-server.authresult.md @@ -8,5 +8,5 @@ Signature: ```typescript -export declare type AuthResult = Authenticated; +export declare type AuthResult = Authenticated | AuthNotHandled | AuthRedirected; ``` diff --git a/docs/development/core/server/kibana-plugin-server.authresultparams.md b/docs/development/core/server/kibana-plugin-server.authresultparams.md index 55b247f21f5a9e..7a725cb340f5bc 100644 --- a/docs/development/core/server/kibana-plugin-server.authresultparams.md +++ b/docs/development/core/server/kibana-plugin-server.authresultparams.md @@ -4,7 +4,7 @@ ## AuthResultParams interface -Result of an incoming request authentication. +Result of successful authentication. Signature: diff --git a/docs/development/core/server/kibana-plugin-server.authresulttype.md b/docs/development/core/server/kibana-plugin-server.authresulttype.md index 61a98ee5e7b110..48c159a94c23d9 100644 --- a/docs/development/core/server/kibana-plugin-server.authresulttype.md +++ b/docs/development/core/server/kibana-plugin-server.authresulttype.md @@ -16,4 +16,6 @@ export declare enum AuthResultType | Member | Value | Description | | --- | --- | --- | | authenticated | "authenticated" | | +| notHandled | "notHandled" | | +| redirected | "redirected" | | diff --git a/docs/development/core/server/kibana-plugin-server.authtoolkit.md b/docs/development/core/server/kibana-plugin-server.authtoolkit.md index bc7003c5a68f30..a6a30dae894ada 100644 --- a/docs/development/core/server/kibana-plugin-server.authtoolkit.md +++ b/docs/development/core/server/kibana-plugin-server.authtoolkit.md @@ -17,4 +17,6 @@ export interface AuthToolkit | Property | Type | Description | | --- | --- | --- | | [authenticated](./kibana-plugin-server.authtoolkit.authenticated.md) | (data?: AuthResultParams) => AuthResult | Authentication is successful with given credentials, allow request to pass through | +| [notHandled](./kibana-plugin-server.authtoolkit.nothandled.md) | () => AuthResult | User has no credentials. Allows user to access a resource when authRequired: 'optional' Rejects a request when authRequired: true | +| [redirected](./kibana-plugin-server.authtoolkit.redirected.md) | (headers: {
location: string;
} & ResponseHeaders) => AuthResult | Redirect user to IdP when authRequired: true Allows user to access a resource without redirection when authRequired: 'optional' | diff --git a/docs/development/core/server/kibana-plugin-server.authtoolkit.nothandled.md b/docs/development/core/server/kibana-plugin-server.authtoolkit.nothandled.md new file mode 100644 index 00000000000000..7de174b3c7bb68 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.authtoolkit.nothandled.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [AuthToolkit](./kibana-plugin-server.authtoolkit.md) > [notHandled](./kibana-plugin-server.authtoolkit.nothandled.md) + +## AuthToolkit.notHandled property + +User has no credentials. Allows user to access a resource when authRequired: 'optional' Rejects a request when authRequired: true + +Signature: + +```typescript +notHandled: () => AuthResult; +``` diff --git a/docs/development/core/server/kibana-plugin-server.authtoolkit.redirected.md b/docs/development/core/server/kibana-plugin-server.authtoolkit.redirected.md new file mode 100644 index 00000000000000..64d1d04a4abc0c --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.authtoolkit.redirected.md @@ -0,0 +1,15 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [AuthToolkit](./kibana-plugin-server.authtoolkit.md) > [redirected](./kibana-plugin-server.authtoolkit.redirected.md) + +## AuthToolkit.redirected property + +Redirect user to IdP when authRequired: true Allows user to access a resource without redirection when authRequired: 'optional' + +Signature: + +```typescript +redirected: (headers: { + location: string; + } & ResponseHeaders) => AuthResult; +``` diff --git a/docs/development/core/server/kibana-plugin-server.kibanarequest.auth.md b/docs/development/core/server/kibana-plugin-server.kibanarequest.auth.md new file mode 100644 index 00000000000000..536d6bd04d9379 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.kibanarequest.auth.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [KibanaRequest](./kibana-plugin-server.kibanarequest.md) > [auth](./kibana-plugin-server.kibanarequest.auth.md) + +## KibanaRequest.auth property + +Signature: + +```typescript +readonly auth: { + isAuthenticated: boolean; + }; +``` diff --git a/docs/development/core/server/kibana-plugin-server.kibanarequest.md b/docs/development/core/server/kibana-plugin-server.kibanarequest.md index cb6745623e3818..0d520783fd4cf6 100644 --- a/docs/development/core/server/kibana-plugin-server.kibanarequest.md +++ b/docs/development/core/server/kibana-plugin-server.kibanarequest.md @@ -22,6 +22,7 @@ export declare class KibanaRequest{
isAuthenticated: boolean;
} | | | [body](./kibana-plugin-server.kibanarequest.body.md) | | Body | | | [events](./kibana-plugin-server.kibanarequest.events.md) | | KibanaRequestEvents | Request events [KibanaRequestEvents](./kibana-plugin-server.kibanarequestevents.md) | | [headers](./kibana-plugin-server.kibanarequest.headers.md) | | Headers | Readonly copy of incoming request headers. | diff --git a/docs/development/core/server/kibana-plugin-server.md b/docs/development/core/server/kibana-plugin-server.md index e843ffb265b826..c84585bf6cb650 100644 --- a/docs/development/core/server/kibana-plugin-server.md +++ b/docs/development/core/server/kibana-plugin-server.md @@ -53,7 +53,10 @@ The plugin integrates with the core system via lifecycle events: `setup` | [AssistanceAPIResponse](./kibana-plugin-server.assistanceapiresponse.md) | | | [AssistantAPIClientParams](./kibana-plugin-server.assistantapiclientparams.md) | | | [Authenticated](./kibana-plugin-server.authenticated.md) | | -| [AuthResultParams](./kibana-plugin-server.authresultparams.md) | Result of an incoming request authentication. | +| [AuthNotHandled](./kibana-plugin-server.authnothandled.md) | | +| [AuthRedirected](./kibana-plugin-server.authredirected.md) | | +| [AuthRedirectedParams](./kibana-plugin-server.authredirectedparams.md) | Result of auth redirection. | +| [AuthResultParams](./kibana-plugin-server.authresultparams.md) | Result of successful authentication. | | [AuthToolkit](./kibana-plugin-server.authtoolkit.md) | A tool set defining an outcome of Auth interceptor for incoming request. | | [CallAPIOptions](./kibana-plugin-server.callapioptions.md) | The set of options that defines how API call should be made and result be processed. | | [Capabilities](./kibana-plugin-server.capabilities.md) | The read-only set of capabilities available for the current UI session. Capabilities are simple key-value pairs of (string, boolean), where the string denotes the capability ID, and the boolean is a flag indicating if the capability is enabled or disabled. | diff --git a/docs/development/core/server/kibana-plugin-server.routeconfigoptions.authrequired.md b/docs/development/core/server/kibana-plugin-server.routeconfigoptions.authrequired.md index e4cbca9c978108..830abd4dde738c 100644 --- a/docs/development/core/server/kibana-plugin-server.routeconfigoptions.authrequired.md +++ b/docs/development/core/server/kibana-plugin-server.routeconfigoptions.authrequired.md @@ -4,12 +4,12 @@ ## RouteConfigOptions.authRequired property -A flag shows that authentication for a route: `enabled` when true `disabled` when false +Defines authentication mode for a route: - true. A user has to have valid credentials to access a resource - false. A user can access a resource without any credentials. - 'optional'. A user can access a resource if has valid credentials or no credentials at all. Can be useful when we grant access to a resource but want to identify a user if possible. -Enabled by default. +Defaults to `true` if an auth mechanism is registered. Signature: ```typescript -authRequired?: boolean; +authRequired?: boolean | 'optional'; ``` diff --git a/docs/development/core/server/kibana-plugin-server.routeconfigoptions.md b/docs/development/core/server/kibana-plugin-server.routeconfigoptions.md index 7fbab90cc2c8a6..6664a28424a326 100644 --- a/docs/development/core/server/kibana-plugin-server.routeconfigoptions.md +++ b/docs/development/core/server/kibana-plugin-server.routeconfigoptions.md @@ -16,7 +16,7 @@ export interface RouteConfigOptions | Property | Type | Description | | --- | --- | --- | -| [authRequired](./kibana-plugin-server.routeconfigoptions.authrequired.md) | boolean | A flag shows that authentication for a route: enabled when true disabled when falseEnabled by default. | +| [authRequired](./kibana-plugin-server.routeconfigoptions.authrequired.md) | boolean | 'optional' | Defines authentication mode for a route: - true. A user has to have valid credentials to access a resource - false. A user can access a resource without any credentials. - 'optional'. A user can access a resource if has valid credentials or no credentials at all. Can be useful when we grant access to a resource but want to identify a user if possible.Defaults to true if an auth mechanism is registered. | | [body](./kibana-plugin-server.routeconfigoptions.body.md) | Method extends 'get' | 'options' ? undefined : RouteConfigOptionsBody | Additional body options [RouteConfigOptionsBody](./kibana-plugin-server.routeconfigoptionsbody.md). | | [tags](./kibana-plugin-server.routeconfigoptions.tags.md) | readonly string[] | Additional metadata tag strings to attach to the route. | | [xsrfRequired](./kibana-plugin-server.routeconfigoptions.xsrfrequired.md) | Method extends 'get' ? never : boolean | Defines xsrf protection requirements for a route: - true. Requires an incoming POST/PUT/DELETE request to contain kbn-xsrf header. - false. Disables xsrf protection.Set to true by default | diff --git a/packages/kbn-config-schema/src/byte_size_value/__snapshots__/index.test.ts.snap b/packages/kbn-config-schema/src/byte_size_value/__snapshots__/index.test.ts.snap deleted file mode 100644 index 97e9082401b3da..00000000000000 --- a/packages/kbn-config-schema/src/byte_size_value/__snapshots__/index.test.ts.snap +++ /dev/null @@ -1,11 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`#constructor throws if number of bytes is negative 1`] = `"Value in bytes is expected to be a safe positive integer, but provided [-1024]."`; - -exports[`#constructor throws if number of bytes is not safe 1`] = `"Value in bytes is expected to be a safe positive integer, but provided [NaN]."`; - -exports[`#constructor throws if number of bytes is not safe 2`] = `"Value in bytes is expected to be a safe positive integer, but provided [Infinity]."`; - -exports[`#constructor throws if number of bytes is not safe 3`] = `"Value in bytes is expected to be a safe positive integer, but provided [9007199254740992]."`; - -exports[`parsing units throws an error when unsupported unit specified 1`] = `"Failed to parse [1tb] as byte value. Value must be either number of bytes, or follow the format [b|kb|mb|gb] (e.g., '1024kb', '200mb', '1gb'), where the number is a safe positive integer."`; diff --git a/packages/kbn-config-schema/src/byte_size_value/index.test.ts b/packages/kbn-config-schema/src/byte_size_value/index.test.ts index 198d95aa0ab4cc..59960a4567f604 100644 --- a/packages/kbn-config-schema/src/byte_size_value/index.test.ts +++ b/packages/kbn-config-schema/src/byte_size_value/index.test.ts @@ -42,19 +42,29 @@ describe('parsing units', () => { }); test('throws an error when unsupported unit specified', () => { - expect(() => ByteSizeValue.parse('1tb')).toThrowErrorMatchingSnapshot(); + expect(() => ByteSizeValue.parse('1tb')).toThrowErrorMatchingInlineSnapshot( + `"Failed to parse value as byte value. Value must be either number of bytes, or follow the format [b|kb|mb|gb] (e.g., '1024kb', '200mb', '1gb'), where the number is a safe positive integer."` + ); }); }); describe('#constructor', () => { test('throws if number of bytes is negative', () => { - expect(() => new ByteSizeValue(-1024)).toThrowErrorMatchingSnapshot(); + expect(() => new ByteSizeValue(-1024)).toThrowErrorMatchingInlineSnapshot( + `"Value in bytes is expected to be a safe positive integer."` + ); }); test('throws if number of bytes is not safe', () => { - expect(() => new ByteSizeValue(NaN)).toThrowErrorMatchingSnapshot(); - expect(() => new ByteSizeValue(Infinity)).toThrowErrorMatchingSnapshot(); - expect(() => new ByteSizeValue(Math.pow(2, 53))).toThrowErrorMatchingSnapshot(); + expect(() => new ByteSizeValue(NaN)).toThrowErrorMatchingInlineSnapshot( + `"Value in bytes is expected to be a safe positive integer."` + ); + expect(() => new ByteSizeValue(Infinity)).toThrowErrorMatchingInlineSnapshot( + `"Value in bytes is expected to be a safe positive integer."` + ); + expect(() => new ByteSizeValue(Math.pow(2, 53))).toThrowErrorMatchingInlineSnapshot( + `"Value in bytes is expected to be a safe positive integer."` + ); }); test('accepts 0', () => { diff --git a/packages/kbn-config-schema/src/byte_size_value/index.ts b/packages/kbn-config-schema/src/byte_size_value/index.ts index 48862821bb78d9..183a6c30f38390 100644 --- a/packages/kbn-config-schema/src/byte_size_value/index.ts +++ b/packages/kbn-config-schema/src/byte_size_value/index.ts @@ -38,7 +38,7 @@ export class ByteSizeValue { const number = Number(text); if (typeof number !== 'number' || isNaN(number)) { throw new Error( - `Failed to parse [${text}] as byte value. Value must be either number of bytes, or follow the format [b|kb|mb|gb] ` + + `Failed to parse value as byte value. Value must be either number of bytes, or follow the format [b|kb|mb|gb] ` + `(e.g., '1024kb', '200mb', '1gb'), where the number is a safe positive integer.` ); } @@ -53,9 +53,7 @@ export class ByteSizeValue { constructor(private readonly valueInBytes: number) { if (!Number.isSafeInteger(valueInBytes) || valueInBytes < 0) { - throw new Error( - `Value in bytes is expected to be a safe positive integer, but provided [${valueInBytes}].` - ); + throw new Error(`Value in bytes is expected to be a safe positive integer.`); } } diff --git a/packages/kbn-config-schema/src/duration/index.ts b/packages/kbn-config-schema/src/duration/index.ts index b96b5a3687bbb4..282c150e8150ac 100644 --- a/packages/kbn-config-schema/src/duration/index.ts +++ b/packages/kbn-config-schema/src/duration/index.ts @@ -28,7 +28,7 @@ function stringToDuration(text: string) { const number = Number(text); if (typeof number !== 'number' || isNaN(number)) { throw new Error( - `Failed to parse [${text}] as time value. Value must be a duration in milliseconds, or follow the format ` + + `Failed to parse value as time value. Value must be a duration in milliseconds, or follow the format ` + `[ms|s|m|h|d|w|M|Y] (e.g. '70ms', '5s', '3d', '1Y'), where the duration is a safe positive integer.` ); } @@ -43,9 +43,7 @@ function stringToDuration(text: string) { function numberToDuration(numberMs: number) { if (!Number.isSafeInteger(numberMs) || numberMs < 0) { - throw new Error( - `Value in milliseconds is expected to be a safe positive integer, but provided [${numberMs}].` - ); + throw new Error(`Value in milliseconds is expected to be a safe positive integer.`); } return momentDuration(numberMs); diff --git a/packages/kbn-config-schema/src/types/__snapshots__/any_type.test.ts.snap b/packages/kbn-config-schema/src/types/__snapshots__/any_type.test.ts.snap deleted file mode 100644 index 3a40752d52b6ea..00000000000000 --- a/packages/kbn-config-schema/src/types/__snapshots__/any_type.test.ts.snap +++ /dev/null @@ -1,5 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`includes namespace in failure 1`] = `"[foo-namespace]: expected value of type [any] but got [undefined]"`; - -exports[`is required by default 1`] = `"expected value of type [any] but got [undefined]"`; diff --git a/packages/kbn-config-schema/src/types/__snapshots__/boolean_type.test.ts.snap b/packages/kbn-config-schema/src/types/__snapshots__/boolean_type.test.ts.snap deleted file mode 100644 index 0e5f6de2deea8f..00000000000000 --- a/packages/kbn-config-schema/src/types/__snapshots__/boolean_type.test.ts.snap +++ /dev/null @@ -1,15 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`includes namespace in failure 1`] = `"[foo-namespace]: expected value of type [boolean] but got [undefined]"`; - -exports[`is required by default 1`] = `"expected value of type [boolean] but got [undefined]"`; - -exports[`returns error when not boolean 1`] = `"expected value of type [boolean] but got [number]"`; - -exports[`returns error when not boolean 2`] = `"expected value of type [boolean] but got [Array]"`; - -exports[`returns error when not boolean 3`] = `"expected value of type [boolean] but got [string]"`; - -exports[`returns error when not boolean 4`] = `"expected value of type [boolean] but got [number]"`; - -exports[`returns error when not boolean 5`] = `"expected value of type [boolean] but got [string]"`; diff --git a/packages/kbn-config-schema/src/types/__snapshots__/buffer_type.test.ts.snap b/packages/kbn-config-schema/src/types/__snapshots__/buffer_type.test.ts.snap deleted file mode 100644 index 96a7ab34dac26a..00000000000000 --- a/packages/kbn-config-schema/src/types/__snapshots__/buffer_type.test.ts.snap +++ /dev/null @@ -1,11 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`includes namespace in failure 1`] = `"[foo-namespace]: expected value of type [Buffer] but got [undefined]"`; - -exports[`is required by default 1`] = `"expected value of type [Buffer] but got [undefined]"`; - -exports[`returns error when not a buffer 1`] = `"expected value of type [Buffer] but got [number]"`; - -exports[`returns error when not a buffer 2`] = `"expected value of type [Buffer] but got [Array]"`; - -exports[`returns error when not a buffer 3`] = `"expected value of type [Buffer] but got [string]"`; diff --git a/packages/kbn-config-schema/src/types/__snapshots__/byte_size_type.test.ts.snap b/packages/kbn-config-schema/src/types/__snapshots__/byte_size_type.test.ts.snap deleted file mode 100644 index ea2102b1776fbf..00000000000000 --- a/packages/kbn-config-schema/src/types/__snapshots__/byte_size_type.test.ts.snap +++ /dev/null @@ -1,61 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`#defaultValue can be a ByteSizeValue 1`] = ` -ByteSizeValue { - "valueInBytes": 1024, -} -`; - -exports[`#defaultValue can be a number 1`] = ` -ByteSizeValue { - "valueInBytes": 1024, -} -`; - -exports[`#defaultValue can be a string 1`] = ` -ByteSizeValue { - "valueInBytes": 1024, -} -`; - -exports[`#defaultValue can be a string-formatted number 1`] = ` -ByteSizeValue { - "valueInBytes": 1024, -} -`; - -exports[`#max returns error when larger 1`] = `"Value is [1mb] ([1048576b]) but it must be equal to or less than [1kb]"`; - -exports[`#max returns value when smaller 1`] = ` -ByteSizeValue { - "valueInBytes": 1, -} -`; - -exports[`#min returns error when smaller 1`] = `"Value is [1b] ([1b]) but it must be equal to or greater than [1kb]"`; - -exports[`#min returns value when larger 1`] = ` -ByteSizeValue { - "valueInBytes": 1024, -} -`; - -exports[`includes namespace in failure 1`] = `"[foo-namespace]: expected value of type [ByteSize] but got [undefined]"`; - -exports[`is required by default 1`] = `"expected value of type [ByteSize] but got [undefined]"`; - -exports[`returns error when not valid string or positive safe integer 1`] = `"Value in bytes is expected to be a safe positive integer, but provided [-123]."`; - -exports[`returns error when not valid string or positive safe integer 2`] = `"Value in bytes is expected to be a safe positive integer, but provided [NaN]."`; - -exports[`returns error when not valid string or positive safe integer 3`] = `"Value in bytes is expected to be a safe positive integer, but provided [Infinity]."`; - -exports[`returns error when not valid string or positive safe integer 4`] = `"Value in bytes is expected to be a safe positive integer, but provided [9007199254740992]."`; - -exports[`returns error when not valid string or positive safe integer 5`] = `"expected value of type [ByteSize] but got [Array]"`; - -exports[`returns error when not valid string or positive safe integer 6`] = `"expected value of type [ByteSize] but got [RegExp]"`; - -exports[`returns error when not valid string or positive safe integer 7`] = `"Failed to parse [123foo] as byte value. Value must be either number of bytes, or follow the format [b|kb|mb|gb] (e.g., '1024kb', '200mb', '1gb'), where the number is a safe positive integer."`; - -exports[`returns error when not valid string or positive safe integer 8`] = `"Failed to parse [123 456] as byte value. Value must be either number of bytes, or follow the format [b|kb|mb|gb] (e.g., '1024kb', '200mb', '1gb'), where the number is a safe positive integer."`; diff --git a/packages/kbn-config-schema/src/types/__snapshots__/conditional_type.test.ts.snap b/packages/kbn-config-schema/src/types/__snapshots__/conditional_type.test.ts.snap deleted file mode 100644 index b32db114860f5f..00000000000000 --- a/packages/kbn-config-schema/src/types/__snapshots__/conditional_type.test.ts.snap +++ /dev/null @@ -1,45 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`correctly handles missing references 1`] = `"[value]: expected value of type [string] but got [number]"`; - -exports[`includes namespace into failures 1`] = `"[mega-namespace.value]: expected value of type [string] but got [number]"`; - -exports[`includes namespace into failures 2`] = `"[mega-namespace.value]: expected value of type [number] but got [string]"`; - -exports[`properly handles conditionals within objects 1`] = `"[value]: expected value of type [string] but got [number]"`; - -exports[`properly handles conditionals within objects 2`] = `"[value]: expected value of type [number] but got [string]"`; - -exports[`properly handles schemas with incompatible types 1`] = `"expected value of type [string] but got [boolean]"`; - -exports[`properly handles schemas with incompatible types 2`] = `"expected value of type [boolean] but got [string]"`; - -exports[`properly validates types according chosen schema 1`] = `"value is [a] but it must have a minimum length of [2]."`; - -exports[`properly validates types according chosen schema 2`] = `"value is [ab] but it must have a maximum length of [1]."`; - -exports[`properly validates when compares with "null" literal Schema 1`] = `"value is [a] but it must have a minimum length of [2]."`; - -exports[`properly validates when compares with "null" literal Schema 2`] = `"value is [ab] but it must have a minimum length of [3]."`; - -exports[`properly validates when compares with Schema 1`] = `"value is [a] but it must have a minimum length of [2]."`; - -exports[`properly validates when compares with Schema 2`] = `"value is [ab] but it must have a minimum length of [3]."`; - -exports[`required by default 1`] = `"expected value of type [string] but got [undefined]"`; - -exports[`works with both context and sibling references 1`] = `"[value]: expected value of type [string] but got [number]"`; - -exports[`works with both context and sibling references 2`] = `"[value]: expected value of type [number] but got [string]"`; - -exports[`works within \`oneOf\` 1`] = ` -"types that failed validation: -- [0]: expected value of type [string] but got [number] -- [1]: expected value of type [array] but got [number]" -`; - -exports[`works within \`oneOf\` 2`] = ` -"types that failed validation: -- [0]: expected value of type [string] but got [boolean] -- [1]: expected value of type [array] but got [boolean]" -`; diff --git a/packages/kbn-config-schema/src/types/__snapshots__/duration_type.test.ts.snap b/packages/kbn-config-schema/src/types/__snapshots__/duration_type.test.ts.snap deleted file mode 100644 index c4e4ff652a2d7e..00000000000000 --- a/packages/kbn-config-schema/src/types/__snapshots__/duration_type.test.ts.snap +++ /dev/null @@ -1,29 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`#defaultValue can be a moment.Duration 1`] = `"PT1H"`; - -exports[`#defaultValue can be a number 1`] = `"PT0.6S"`; - -exports[`#defaultValue can be a string 1`] = `"PT1H"`; - -exports[`#defaultValue can be a string-formatted number 1`] = `"PT0.6S"`; - -exports[`includes namespace in failure 1`] = `"[foo-namespace]: expected value of type [moment.Duration] but got [undefined]"`; - -exports[`is required by default 1`] = `"expected value of type [moment.Duration] but got [undefined]"`; - -exports[`returns error when not valid string or non-safe positive integer 1`] = `"Value in milliseconds is expected to be a safe positive integer, but provided [-123]."`; - -exports[`returns error when not valid string or non-safe positive integer 2`] = `"Value in milliseconds is expected to be a safe positive integer, but provided [NaN]."`; - -exports[`returns error when not valid string or non-safe positive integer 3`] = `"Value in milliseconds is expected to be a safe positive integer, but provided [Infinity]."`; - -exports[`returns error when not valid string or non-safe positive integer 4`] = `"Value in milliseconds is expected to be a safe positive integer, but provided [9007199254740992]."`; - -exports[`returns error when not valid string or non-safe positive integer 5`] = `"expected value of type [moment.Duration] but got [Array]"`; - -exports[`returns error when not valid string or non-safe positive integer 6`] = `"expected value of type [moment.Duration] but got [RegExp]"`; - -exports[`returns error when not valid string or non-safe positive integer 7`] = `"Failed to parse [123foo] as time value. Value must be a duration in milliseconds, or follow the format [ms|s|m|h|d|w|M|Y] (e.g. '70ms', '5s', '3d', '1Y'), where the duration is a safe positive integer."`; - -exports[`returns error when not valid string or non-safe positive integer 8`] = `"Failed to parse [123 456] as time value. Value must be a duration in milliseconds, or follow the format [ms|s|m|h|d|w|M|Y] (e.g. '70ms', '5s', '3d', '1Y'), where the duration is a safe positive integer."`; diff --git a/packages/kbn-config-schema/src/types/__snapshots__/literal_type.test.ts.snap b/packages/kbn-config-schema/src/types/__snapshots__/literal_type.test.ts.snap deleted file mode 100644 index 14d474b4a516bb..00000000000000 --- a/packages/kbn-config-schema/src/types/__snapshots__/literal_type.test.ts.snap +++ /dev/null @@ -1,13 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`includes namespace in failure 1`] = `"[foo-namespace]: expected value to equal [test] but got [foo]"`; - -exports[`returns error when not correct 1`] = `"expected value to equal [test] but got [foo]"`; - -exports[`returns error when not correct 2`] = `"expected value to equal [true] but got [false]"`; - -exports[`returns error when not correct 3`] = `"expected value to equal [test] but got [1,2,3]"`; - -exports[`returns error when not correct 4`] = `"expected value to equal [123] but got [abc]"`; - -exports[`returns error when not correct 5`] = `"expected value to equal [null] but got [42]"`; diff --git a/packages/kbn-config-schema/src/types/__snapshots__/maybe_type.test.ts.snap b/packages/kbn-config-schema/src/types/__snapshots__/maybe_type.test.ts.snap deleted file mode 100644 index fdb172df356a75..00000000000000 --- a/packages/kbn-config-schema/src/types/__snapshots__/maybe_type.test.ts.snap +++ /dev/null @@ -1,9 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`fails if null 1`] = `"expected value of type [string] but got [null]"`; - -exports[`includes namespace in failure 1`] = `"[foo-namespace]: expected value of type [string] but got [null]"`; - -exports[`validates basic type 1`] = `"expected value of type [string] but got [number]"`; - -exports[`validates contained type 1`] = `"value is [foo] but it must have a maximum length of [1]."`; diff --git a/packages/kbn-config-schema/src/types/__snapshots__/never_type.test.ts.snap b/packages/kbn-config-schema/src/types/__snapshots__/never_type.test.ts.snap deleted file mode 100644 index 6eea2a7cefc728..00000000000000 --- a/packages/kbn-config-schema/src/types/__snapshots__/never_type.test.ts.snap +++ /dev/null @@ -1,13 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`throws on any value set 1`] = `"a value wasn't expected to be present"`; - -exports[`throws on any value set 2`] = `"a value wasn't expected to be present"`; - -exports[`throws on any value set 3`] = `"a value wasn't expected to be present"`; - -exports[`throws on any value set 4`] = `"a value wasn't expected to be present"`; - -exports[`throws on value set as object property 1`] = `"[name]: a value wasn't expected to be present"`; - -exports[`works for conditional types 1`] = `"[name]: a value wasn't expected to be present"`; diff --git a/packages/kbn-config-schema/src/types/__snapshots__/nullable_type.test.ts.snap b/packages/kbn-config-schema/src/types/__snapshots__/nullable_type.test.ts.snap deleted file mode 100644 index 96ab664921fdfb..00000000000000 --- a/packages/kbn-config-schema/src/types/__snapshots__/nullable_type.test.ts.snap +++ /dev/null @@ -1,25 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`includes namespace in failure 1`] = ` -"[foo-namespace]: types that failed validation: -- [foo-namespace.0]: value is [foo] but it must have a maximum length of [1]. -- [foo-namespace.1]: expected value to equal [null] but got [foo]" -`; - -exports[`validates basic type 1`] = ` -"types that failed validation: -- [0]: expected value of type [string] but got [number] -- [1]: expected value to equal [null] but got [666]" -`; - -exports[`validates contained type 1`] = ` -"types that failed validation: -- [0]: value is [foo] but it must have a maximum length of [1]. -- [1]: expected value to equal [null] but got [foo]" -`; - -exports[`validates type errors in object 1`] = ` -"[foo]: types that failed validation: -- [foo.0]: value is [ab] but it must have a maximum length of [1]. -- [foo.1]: expected value to equal [null] but got [ab]" -`; diff --git a/packages/kbn-config-schema/src/types/__snapshots__/number_type.test.ts.snap b/packages/kbn-config-schema/src/types/__snapshots__/number_type.test.ts.snap deleted file mode 100644 index 5d1e5fcf1ef817..00000000000000 --- a/packages/kbn-config-schema/src/types/__snapshots__/number_type.test.ts.snap +++ /dev/null @@ -1,17 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`#max returns error when larger number 1`] = `"Value is [3] but it must be equal to or lower than [2]."`; - -exports[`#min returns error when smaller number 1`] = `"Value is [3] but it must be equal to or greater than [4]."`; - -exports[`fails if number is \`NaN\` 1`] = `"expected value of type [number] but got [number]"`; - -exports[`includes namespace in failure 1`] = `"[foo-namespace]: expected value of type [number] but got [undefined]"`; - -exports[`is required by default 1`] = `"expected value of type [number] but got [undefined]"`; - -exports[`returns error when not number or numeric string 1`] = `"expected value of type [number] but got [string]"`; - -exports[`returns error when not number or numeric string 2`] = `"expected value of type [number] but got [Array]"`; - -exports[`returns error when not number or numeric string 3`] = `"expected value of type [number] but got [RegExp]"`; diff --git a/packages/kbn-config-schema/src/types/__snapshots__/one_of_type.test.ts.snap b/packages/kbn-config-schema/src/types/__snapshots__/one_of_type.test.ts.snap deleted file mode 100644 index 75dfff456ebe7c..00000000000000 --- a/packages/kbn-config-schema/src/types/__snapshots__/one_of_type.test.ts.snap +++ /dev/null @@ -1,32 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`fails if not matching literal 1`] = ` -"types that failed validation: -- [0]: expected value to equal [foo] but got [bar]" -`; - -exports[`fails if not matching multiple types 1`] = ` -"types that failed validation: -- [0]: expected value of type [string] but got [boolean] -- [1]: expected value of type [number] but got [boolean]" -`; - -exports[`fails if not matching type 1`] = ` -"types that failed validation: -- [0]: expected value of type [string] but got [boolean]" -`; - -exports[`fails if not matching type 2`] = ` -"types that failed validation: -- [0]: expected value of type [string] but got [number]" -`; - -exports[`handles object with wrong type 1`] = ` -"types that failed validation: -- [0.age]: expected value of type [number] but got [string]" -`; - -exports[`includes namespace in failure 1`] = ` -"[foo-namespace]: types that failed validation: -- [foo-namespace.0.age]: expected value of type [number] but got [string]" -`; diff --git a/packages/kbn-config-schema/src/types/__snapshots__/stream_type.test.ts.snap b/packages/kbn-config-schema/src/types/__snapshots__/stream_type.test.ts.snap deleted file mode 100644 index e813b4f68a09e1..00000000000000 --- a/packages/kbn-config-schema/src/types/__snapshots__/stream_type.test.ts.snap +++ /dev/null @@ -1,11 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`includes namespace in failure 1`] = `"[foo-namespace]: expected value of type [Stream] but got [undefined]"`; - -exports[`is required by default 1`] = `"expected value of type [Buffer] but got [undefined]"`; - -exports[`returns error when not a stream 1`] = `"expected value of type [Stream] but got [number]"`; - -exports[`returns error when not a stream 2`] = `"expected value of type [Stream] but got [Array]"`; - -exports[`returns error when not a stream 3`] = `"expected value of type [Stream] but got [string]"`; diff --git a/packages/kbn-config-schema/src/types/__snapshots__/string_type.test.ts.snap b/packages/kbn-config-schema/src/types/__snapshots__/string_type.test.ts.snap deleted file mode 100644 index 8e1f63fb66733e..00000000000000 --- a/packages/kbn-config-schema/src/types/__snapshots__/string_type.test.ts.snap +++ /dev/null @@ -1,35 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`#hostname returns error when empty string 1`] = `"any.empty"`; - -exports[`#hostname returns error when value is not a valid hostname 1`] = `"value is [host:name] but it must be a valid hostname (see RFC 1123)."`; - -exports[`#hostname returns error when value is not a valid hostname 2`] = `"value is [localhost:5601] but it must be a valid hostname (see RFC 1123)."`; - -exports[`#hostname returns error when value is not a valid hostname 3`] = `"value is [-] but it must be a valid hostname (see RFC 1123)."`; - -exports[`#hostname returns error when value is not a valid hostname 4`] = `"value is [0:?:0:0:0:0:0:1] but it must be a valid hostname (see RFC 1123)."`; - -exports[`#hostname returns error when value is not a valid hostname 5`] = `"value is [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa] but it must be a valid hostname (see RFC 1123)."`; - -exports[`#hostname supports string validation rules 1`] = `"value is [www.example.com] but it must have a maximum length of [3]."`; - -exports[`#maxLength returns error when longer string 1`] = `"value is [foo] but it must have a maximum length of [2]."`; - -exports[`#minLength returns error when empty string 1`] = `"value is [] but it must have a minimum length of [2]."`; - -exports[`#minLength returns error when shorter string 1`] = `"value is [foo] but it must have a minimum length of [4]."`; - -exports[`#validate throw when empty string 1`] = `"validator failure"`; - -exports[`#validate throws when returns string 1`] = `"validator failure"`; - -exports[`includes namespace in failure 1`] = `"[foo-namespace]: expected value of type [string] but got [undefined]"`; - -exports[`is required by default 1`] = `"expected value of type [string] but got [undefined]"`; - -exports[`returns error when not string 1`] = `"expected value of type [string] but got [number]"`; - -exports[`returns error when not string 2`] = `"expected value of type [string] but got [Array]"`; - -exports[`returns error when not string 3`] = `"expected value of type [string] but got [RegExp]"`; diff --git a/packages/kbn-config-schema/src/types/__snapshots__/uri_type.test.ts.snap b/packages/kbn-config-schema/src/types/__snapshots__/uri_type.test.ts.snap deleted file mode 100644 index 81fafdb4a7b339..00000000000000 --- a/packages/kbn-config-schema/src/types/__snapshots__/uri_type.test.ts.snap +++ /dev/null @@ -1,25 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`#scheme returns error when shorter string 1`] = `"expected URI with scheme [http|https] but got [ftp://elastic.co]."`; - -exports[`#scheme returns error when shorter string 2`] = `"expected URI with scheme [http|https] but got [file:///kibana.log]."`; - -exports[`#validate throws when returns string 1`] = `"validator failure"`; - -exports[`is required by default 1`] = `"expected value of type [string] but got [undefined]."`; - -exports[`returns error when not string 1`] = `"expected value of type [string] but got [number]."`; - -exports[`returns error when not string 2`] = `"expected value of type [string] but got [Array]."`; - -exports[`returns error when not string 3`] = `"expected value of type [string] but got [RegExp]."`; - -exports[`returns error when value is not a URI 1`] = `"value is [3domain.local] but it must be a valid URI (see RFC 3986)."`; - -exports[`returns error when value is not a URI 2`] = `"value is [http://8010:0:0:0:9:500:300C:200A] but it must be a valid URI (see RFC 3986)."`; - -exports[`returns error when value is not a URI 3`] = `"value is [-] but it must be a valid URI (see RFC 3986)."`; - -exports[`returns error when value is not a URI 4`] = `"value is [https://example.com?baz[]=foo&baz[]=bar] but it must be a valid URI (see RFC 3986)."`; - -exports[`returns error when value is not a URI 5`] = `"value is [http://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa] but it must be a valid URI (see RFC 3986)."`; diff --git a/packages/kbn-config-schema/src/types/any_type.test.ts b/packages/kbn-config-schema/src/types/any_type.test.ts index 4d68c860ba13d3..30a9a8b71ea120 100644 --- a/packages/kbn-config-schema/src/types/any_type.test.ts +++ b/packages/kbn-config-schema/src/types/any_type.test.ts @@ -28,13 +28,17 @@ test('works for any value', () => { }); test('is required by default', () => { - expect(() => schema.any().validate(undefined)).toThrowErrorMatchingSnapshot(); + expect(() => schema.any().validate(undefined)).toThrowErrorMatchingInlineSnapshot( + `"expected value of type [any] but got [undefined]"` + ); }); test('includes namespace in failure', () => { expect(() => schema.any().validate(undefined, {}, 'foo-namespace') - ).toThrowErrorMatchingSnapshot(); + ).toThrowErrorMatchingInlineSnapshot( + `"[foo-namespace]: expected value of type [any] but got [undefined]"` + ); }); describe('#defaultValue', () => { diff --git a/packages/kbn-config-schema/src/types/array_type.test.ts b/packages/kbn-config-schema/src/types/array_type.test.ts index 66b72096a593da..9f3370de8c2659 100644 --- a/packages/kbn-config-schema/src/types/array_type.test.ts +++ b/packages/kbn-config-schema/src/types/array_type.test.ts @@ -39,7 +39,7 @@ test('fails if wrong input type', () => { test('fails if string input cannot be parsed', () => { const type = schema.arrayOf(schema.string()); expect(() => type.validate('test')).toThrowErrorMatchingInlineSnapshot( - `"could not parse array value from [test]"` + `"could not parse array value from json input"` ); }); @@ -53,7 +53,7 @@ test('fails with correct type if parsed input is not an array', () => { test('includes namespace in failure when wrong top-level type', () => { const type = schema.arrayOf(schema.string()); expect(() => type.validate('test', {}, 'foo-namespace')).toThrowErrorMatchingInlineSnapshot( - `"[foo-namespace]: could not parse array value from [test]"` + `"[foo-namespace]: could not parse array value from json input"` ); }); diff --git a/packages/kbn-config-schema/src/types/array_type.ts b/packages/kbn-config-schema/src/types/array_type.ts index a0353e8348ddd6..0df0d44a37951f 100644 --- a/packages/kbn-config-schema/src/types/array_type.ts +++ b/packages/kbn-config-schema/src/types/array_type.ts @@ -52,7 +52,7 @@ export class ArrayType extends Type { case 'array.sparse': return `sparse array are not allowed`; case 'array.parse': - return `could not parse array value from [${value}]`; + return `could not parse array value from json input`; case 'array.min': return `array size is [${value.length}], but cannot be smaller than [${limit}]`; case 'array.max': diff --git a/packages/kbn-config-schema/src/types/boolean_type.test.ts b/packages/kbn-config-schema/src/types/boolean_type.test.ts index e94999b5054377..bffa3e18f93bfc 100644 --- a/packages/kbn-config-schema/src/types/boolean_type.test.ts +++ b/packages/kbn-config-schema/src/types/boolean_type.test.ts @@ -35,13 +35,17 @@ test('handles boolean strings', () => { }); test('is required by default', () => { - expect(() => schema.boolean().validate(undefined)).toThrowErrorMatchingSnapshot(); + expect(() => schema.boolean().validate(undefined)).toThrowErrorMatchingInlineSnapshot( + `"expected value of type [boolean] but got [undefined]"` + ); }); test('includes namespace in failure', () => { expect(() => schema.boolean().validate(undefined, {}, 'foo-namespace') - ).toThrowErrorMatchingSnapshot(); + ).toThrowErrorMatchingInlineSnapshot( + `"[foo-namespace]: expected value of type [boolean] but got [undefined]"` + ); }); describe('#defaultValue', () => { @@ -55,13 +59,23 @@ describe('#defaultValue', () => { }); test('returns error when not boolean', () => { - expect(() => schema.boolean().validate(123)).toThrowErrorMatchingSnapshot(); + expect(() => schema.boolean().validate(123)).toThrowErrorMatchingInlineSnapshot( + `"expected value of type [boolean] but got [number]"` + ); - expect(() => schema.boolean().validate([1, 2, 3])).toThrowErrorMatchingSnapshot(); + expect(() => schema.boolean().validate([1, 2, 3])).toThrowErrorMatchingInlineSnapshot( + `"expected value of type [boolean] but got [Array]"` + ); - expect(() => schema.boolean().validate('abc')).toThrowErrorMatchingSnapshot(); + expect(() => schema.boolean().validate('abc')).toThrowErrorMatchingInlineSnapshot( + `"expected value of type [boolean] but got [string]"` + ); - expect(() => schema.boolean().validate(0)).toThrowErrorMatchingSnapshot(); + expect(() => schema.boolean().validate(0)).toThrowErrorMatchingInlineSnapshot( + `"expected value of type [boolean] but got [number]"` + ); - expect(() => schema.boolean().validate('no')).toThrowErrorMatchingSnapshot(); + expect(() => schema.boolean().validate('no')).toThrowErrorMatchingInlineSnapshot( + `"expected value of type [boolean] but got [string]"` + ); }); diff --git a/packages/kbn-config-schema/src/types/buffer_type.test.ts b/packages/kbn-config-schema/src/types/buffer_type.test.ts index 63d59296aec843..a7b589df0c6d10 100644 --- a/packages/kbn-config-schema/src/types/buffer_type.test.ts +++ b/packages/kbn-config-schema/src/types/buffer_type.test.ts @@ -25,13 +25,17 @@ test('returns value by default', () => { }); test('is required by default', () => { - expect(() => schema.buffer().validate(undefined)).toThrowErrorMatchingSnapshot(); + expect(() => schema.buffer().validate(undefined)).toThrowErrorMatchingInlineSnapshot( + `"expected value of type [Buffer] but got [undefined]"` + ); }); test('includes namespace in failure', () => { expect(() => schema.buffer().validate(undefined, {}, 'foo-namespace') - ).toThrowErrorMatchingSnapshot(); + ).toThrowErrorMatchingInlineSnapshot( + `"[foo-namespace]: expected value of type [Buffer] but got [undefined]"` + ); }); describe('#defaultValue', () => { @@ -49,9 +53,15 @@ describe('#defaultValue', () => { }); test('returns error when not a buffer', () => { - expect(() => schema.buffer().validate(123)).toThrowErrorMatchingSnapshot(); + expect(() => schema.buffer().validate(123)).toThrowErrorMatchingInlineSnapshot( + `"expected value of type [Buffer] but got [number]"` + ); - expect(() => schema.buffer().validate([1, 2, 3])).toThrowErrorMatchingSnapshot(); + expect(() => schema.buffer().validate([1, 2, 3])).toThrowErrorMatchingInlineSnapshot( + `"expected value of type [Buffer] but got [Array]"` + ); - expect(() => schema.buffer().validate('abc')).toThrowErrorMatchingSnapshot(); + expect(() => schema.buffer().validate('abc')).toThrowErrorMatchingInlineSnapshot( + `"expected value of type [Buffer] but got [string]"` + ); }); diff --git a/packages/kbn-config-schema/src/types/byte_size_type.test.ts b/packages/kbn-config-schema/src/types/byte_size_type.test.ts index 7c65ec2945b493..a69a7296a6eb82 100644 --- a/packages/kbn-config-schema/src/types/byte_size_type.test.ts +++ b/packages/kbn-config-schema/src/types/byte_size_type.test.ts @@ -35,11 +35,17 @@ test('handles numbers', () => { }); test('is required by default', () => { - expect(() => byteSize().validate(undefined)).toThrowErrorMatchingSnapshot(); + expect(() => byteSize().validate(undefined)).toThrowErrorMatchingInlineSnapshot( + `"expected value of type [ByteSize] but got [undefined]"` + ); }); test('includes namespace in failure', () => { - expect(() => byteSize().validate(undefined, {}, 'foo-namespace')).toThrowErrorMatchingSnapshot(); + expect(() => + byteSize().validate(undefined, {}, 'foo-namespace') + ).toThrowErrorMatchingInlineSnapshot( + `"[foo-namespace]: expected value of type [ByteSize] but got [undefined]"` + ); }); describe('#defaultValue', () => { @@ -48,7 +54,11 @@ describe('#defaultValue', () => { byteSize({ defaultValue: ByteSizeValue.parse('1kb'), }).validate(undefined) - ).toMatchSnapshot(); + ).toMatchInlineSnapshot(` + ByteSizeValue { + "valueInBytes": 1024, + } + `); }); test('can be a string', () => { @@ -56,7 +66,11 @@ describe('#defaultValue', () => { byteSize({ defaultValue: '1kb', }).validate(undefined) - ).toMatchSnapshot(); + ).toMatchInlineSnapshot(` + ByteSizeValue { + "valueInBytes": 1024, + } + `); }); test('can be a string-formatted number', () => { @@ -64,7 +78,11 @@ describe('#defaultValue', () => { byteSize({ defaultValue: '1024', }).validate(undefined) - ).toMatchSnapshot(); + ).toMatchInlineSnapshot(` + ByteSizeValue { + "valueInBytes": 1024, + } + `); }); test('can be a number', () => { @@ -72,7 +90,11 @@ describe('#defaultValue', () => { byteSize({ defaultValue: 1024, }).validate(undefined) - ).toMatchSnapshot(); + ).toMatchInlineSnapshot(` + ByteSizeValue { + "valueInBytes": 1024, + } + `); }); }); @@ -82,7 +104,11 @@ describe('#min', () => { byteSize({ min: '1b', }).validate('1kb') - ).toMatchSnapshot(); + ).toMatchInlineSnapshot(` + ByteSizeValue { + "valueInBytes": 1024, + } + `); }); test('returns error when smaller', () => { @@ -90,34 +116,56 @@ describe('#min', () => { byteSize({ min: '1kb', }).validate('1b') - ).toThrowErrorMatchingSnapshot(); + ).toThrowErrorMatchingInlineSnapshot(`"Value must be equal to or greater than [1kb]"`); }); }); describe('#max', () => { test('returns value when smaller', () => { - expect(byteSize({ max: '1kb' }).validate('1b')).toMatchSnapshot(); + expect(byteSize({ max: '1kb' }).validate('1b')).toMatchInlineSnapshot(` + ByteSizeValue { + "valueInBytes": 1, + } + `); }); test('returns error when larger', () => { - expect(() => byteSize({ max: '1kb' }).validate('1mb')).toThrowErrorMatchingSnapshot(); + expect(() => byteSize({ max: '1kb' }).validate('1mb')).toThrowErrorMatchingInlineSnapshot( + `"Value must be equal to or less than [1kb]"` + ); }); }); test('returns error when not valid string or positive safe integer', () => { - expect(() => byteSize().validate(-123)).toThrowErrorMatchingSnapshot(); + expect(() => byteSize().validate(-123)).toThrowErrorMatchingInlineSnapshot( + `"Value in bytes is expected to be a safe positive integer."` + ); - expect(() => byteSize().validate(NaN)).toThrowErrorMatchingSnapshot(); + expect(() => byteSize().validate(NaN)).toThrowErrorMatchingInlineSnapshot( + `"Value in bytes is expected to be a safe positive integer."` + ); - expect(() => byteSize().validate(Infinity)).toThrowErrorMatchingSnapshot(); + expect(() => byteSize().validate(Infinity)).toThrowErrorMatchingInlineSnapshot( + `"Value in bytes is expected to be a safe positive integer."` + ); - expect(() => byteSize().validate(Math.pow(2, 53))).toThrowErrorMatchingSnapshot(); + expect(() => byteSize().validate(Math.pow(2, 53))).toThrowErrorMatchingInlineSnapshot( + `"Value in bytes is expected to be a safe positive integer."` + ); - expect(() => byteSize().validate([1, 2, 3])).toThrowErrorMatchingSnapshot(); + expect(() => byteSize().validate([1, 2, 3])).toThrowErrorMatchingInlineSnapshot( + `"expected value of type [ByteSize] but got [Array]"` + ); - expect(() => byteSize().validate(/abc/)).toThrowErrorMatchingSnapshot(); + expect(() => byteSize().validate(/abc/)).toThrowErrorMatchingInlineSnapshot( + `"expected value of type [ByteSize] but got [RegExp]"` + ); - expect(() => byteSize().validate('123foo')).toThrowErrorMatchingSnapshot(); + expect(() => byteSize().validate('123foo')).toThrowErrorMatchingInlineSnapshot( + `"Failed to parse value as byte value. Value must be either number of bytes, or follow the format [b|kb|mb|gb] (e.g., '1024kb', '200mb', '1gb'), where the number is a safe positive integer."` + ); - expect(() => byteSize().validate('123 456')).toThrowErrorMatchingSnapshot(); + expect(() => byteSize().validate('123 456')).toThrowErrorMatchingInlineSnapshot( + `"Failed to parse value as byte value. Value must be either number of bytes, or follow the format [b|kb|mb|gb] (e.g., '1024kb', '200mb', '1gb'), where the number is a safe positive integer."` + ); }); diff --git a/packages/kbn-config-schema/src/types/byte_size_type.ts b/packages/kbn-config-schema/src/types/byte_size_type.ts index 4833de7ecf15f9..f7aa12291c7de3 100644 --- a/packages/kbn-config-schema/src/types/byte_size_type.ts +++ b/packages/kbn-config-schema/src/types/byte_size_type.ts @@ -61,13 +61,9 @@ export class ByteSizeType extends Type { case 'bytes.parse': return new SchemaTypeError(message, path); case 'bytes.min': - return `Value is [${value.toString()}] ([${value.toString( - 'b' - )}]) but it must be equal to or greater than [${limit.toString()}]`; + return `Value must be equal to or greater than [${limit.toString()}]`; case 'bytes.max': - return `Value is [${value.toString()}] ([${value.toString( - 'b' - )}]) but it must be equal to or less than [${limit.toString()}]`; + return `Value must be equal to or less than [${limit.toString()}]`; } } } diff --git a/packages/kbn-config-schema/src/types/conditional_type.test.ts b/packages/kbn-config-schema/src/types/conditional_type.test.ts index 354854b864755c..b7ad431318e858 100644 --- a/packages/kbn-config-schema/src/types/conditional_type.test.ts +++ b/packages/kbn-config-schema/src/types/conditional_type.test.ts @@ -32,7 +32,7 @@ test('required by default', () => { context_value_1: 0, context_value_2: 0, }) - ).toThrowErrorMatchingSnapshot(); + ).toThrowErrorMatchingInlineSnapshot(`"expected value of type [string] but got [undefined]"`); }); test('returns default', () => { @@ -90,7 +90,9 @@ test('properly validates types according chosen schema', () => { context_value_1: 0, context_value_2: 0, }) - ).toThrowErrorMatchingSnapshot(); + ).toThrowErrorMatchingInlineSnapshot( + `"value has length [1] but it must have a minimum length of [2]."` + ); expect( type.validate('ab', { @@ -104,7 +106,9 @@ test('properly validates types according chosen schema', () => { context_value_1: 0, context_value_2: 1, }) - ).toThrowErrorMatchingSnapshot(); + ).toThrowErrorMatchingInlineSnapshot( + `"value has length [2] but it must have a maximum length of [1]."` + ); expect( type.validate('a', { @@ -126,7 +130,9 @@ test('properly validates when compares with Schema', () => { type.validate('a', { context_value_1: 0, }) - ).toThrowErrorMatchingSnapshot(); + ).toThrowErrorMatchingInlineSnapshot( + `"value has length [1] but it must have a minimum length of [2]."` + ); expect( type.validate('ab', { @@ -138,7 +144,9 @@ test('properly validates when compares with Schema', () => { type.validate('ab', { context_value_1: 'b', }) - ).toThrowErrorMatchingSnapshot(); + ).toThrowErrorMatchingInlineSnapshot( + `"value has length [2] but it must have a minimum length of [3]."` + ); expect( type.validate('abc', { @@ -159,7 +167,9 @@ test('properly validates when compares with "null" literal Schema', () => { type.validate('a', { context_value_1: null, }) - ).toThrowErrorMatchingSnapshot(); + ).toThrowErrorMatchingInlineSnapshot( + `"value has length [1] but it must have a minimum length of [2]."` + ); expect( type.validate('ab', { @@ -171,7 +181,9 @@ test('properly validates when compares with "null" literal Schema', () => { type.validate('ab', { context_value_1: 'b', }) - ).toThrowErrorMatchingSnapshot(); + ).toThrowErrorMatchingInlineSnapshot( + `"value has length [2] but it must have a minimum length of [3]."` + ); expect( type.validate('abc', { @@ -193,7 +205,7 @@ test('properly handles schemas with incompatible types', () => { context_value_1: 0, context_value_2: 0, }) - ).toThrowErrorMatchingSnapshot(); + ).toThrowErrorMatchingInlineSnapshot(`"expected value of type [string] but got [boolean]"`); expect( type.validate('a', { @@ -207,7 +219,7 @@ test('properly handles schemas with incompatible types', () => { context_value_1: 0, context_value_2: 1, }) - ).toThrowErrorMatchingSnapshot(); + ).toThrowErrorMatchingInlineSnapshot(`"expected value of type [boolean] but got [string]"`); expect( type.validate(true, { @@ -223,14 +235,18 @@ test('properly handles conditionals within objects', () => { value: schema.conditional(schema.siblingRef('key'), 'number', schema.number(), schema.string()), }); - expect(() => type.validate({ key: 'string', value: 1 })).toThrowErrorMatchingSnapshot(); + expect(() => type.validate({ key: 'string', value: 1 })).toThrowErrorMatchingInlineSnapshot( + `"[value]: expected value of type [string] but got [number]"` + ); expect(type.validate({ key: 'string', value: 'a' })).toEqual({ key: 'string', value: 'a', }); - expect(() => type.validate({ key: 'number', value: 'a' })).toThrowErrorMatchingSnapshot(); + expect(() => type.validate({ key: 'number', value: 'a' })).toThrowErrorMatchingInlineSnapshot( + `"[value]: expected value of type [number] but got [string]"` + ); expect(type.validate({ key: 'number', value: 1 })).toEqual({ key: 'number', @@ -269,7 +285,9 @@ test('works with both context and sibling references', () => { expect(() => type.validate({ key: 'string', value: 1 }, { context_key: 'number' }) - ).toThrowErrorMatchingSnapshot(); + ).toThrowErrorMatchingInlineSnapshot( + `"[value]: expected value of type [string] but got [number]"` + ); expect(type.validate({ key: 'string', value: 'a' }, { context_key: 'number' })).toEqual({ key: 'string', @@ -278,7 +296,9 @@ test('works with both context and sibling references', () => { expect(() => type.validate({ key: 'number', value: 'a' }, { context_key: 'number' }) - ).toThrowErrorMatchingSnapshot(); + ).toThrowErrorMatchingInlineSnapshot( + `"[value]: expected value of type [number] but got [string]"` + ); expect(type.validate({ key: 'number', value: 1 }, { context_key: 'number' })).toEqual({ key: 'number', @@ -294,11 +314,15 @@ test('includes namespace into failures', () => { expect(() => type.validate({ key: 'string', value: 1 }, {}, 'mega-namespace') - ).toThrowErrorMatchingSnapshot(); + ).toThrowErrorMatchingInlineSnapshot( + `"[mega-namespace.value]: expected value of type [string] but got [number]"` + ); expect(() => type.validate({ key: 'number', value: 'a' }, {}, 'mega-namespace') - ).toThrowErrorMatchingSnapshot(); + ).toThrowErrorMatchingInlineSnapshot( + `"[mega-namespace.value]: expected value of type [number] but got [string]"` + ); }); test('correctly handles missing references', () => { @@ -311,7 +335,9 @@ test('correctly handles missing references', () => { ), }); - expect(() => type.validate({ value: 1 })).toThrowErrorMatchingSnapshot(); + expect(() => type.validate({ value: 1 })).toThrowErrorMatchingInlineSnapshot( + `"[value]: expected value of type [string] but got [number]"` + ); expect(type.validate({ value: 'a' })).toEqual({ value: 'a' }); }); @@ -332,8 +358,16 @@ test('works within `oneOf`', () => { expect(type.validate(true, { type: 'boolean' })).toEqual(true); expect(type.validate(['a', 'b'], { type: 'array' })).toEqual(['a', 'b']); - expect(() => type.validate(1, { type: 'string' })).toThrowErrorMatchingSnapshot(); - expect(() => type.validate(true, { type: 'string' })).toThrowErrorMatchingSnapshot(); + expect(() => type.validate(1, { type: 'string' })).toThrowErrorMatchingInlineSnapshot(` +"types that failed validation: +- [0]: expected value of type [string] but got [number] +- [1]: expected value of type [array] but got [number]" +`); + expect(() => type.validate(true, { type: 'string' })).toThrowErrorMatchingInlineSnapshot(` +"types that failed validation: +- [0]: expected value of type [string] but got [boolean] +- [1]: expected value of type [array] but got [boolean]" +`); }); describe('#validate', () => { diff --git a/packages/kbn-config-schema/src/types/duration_type.test.ts b/packages/kbn-config-schema/src/types/duration_type.test.ts index 57e917dc99b2b3..2a0458f1419cc5 100644 --- a/packages/kbn-config-schema/src/types/duration_type.test.ts +++ b/packages/kbn-config-schema/src/types/duration_type.test.ts @@ -35,11 +35,17 @@ test('handles number', () => { }); test('is required by default', () => { - expect(() => duration().validate(undefined)).toThrowErrorMatchingSnapshot(); + expect(() => duration().validate(undefined)).toThrowErrorMatchingInlineSnapshot( + `"expected value of type [moment.Duration] but got [undefined]"` + ); }); test('includes namespace in failure', () => { - expect(() => duration().validate(undefined, {}, 'foo-namespace')).toThrowErrorMatchingSnapshot(); + expect(() => + duration().validate(undefined, {}, 'foo-namespace') + ).toThrowErrorMatchingInlineSnapshot( + `"[foo-namespace]: expected value of type [moment.Duration] but got [undefined]"` + ); }); describe('#defaultValue', () => { @@ -48,7 +54,7 @@ describe('#defaultValue', () => { duration({ defaultValue: momentDuration(1, 'hour'), }).validate(undefined) - ).toMatchSnapshot(); + ).toMatchInlineSnapshot(`"PT1H"`); }); test('can be a string', () => { @@ -56,7 +62,7 @@ describe('#defaultValue', () => { duration({ defaultValue: '1h', }).validate(undefined) - ).toMatchSnapshot(); + ).toMatchInlineSnapshot(`"PT1H"`); }); test('can be a string-formatted number', () => { @@ -64,7 +70,7 @@ describe('#defaultValue', () => { duration({ defaultValue: '600', }).validate(undefined) - ).toMatchSnapshot(); + ).toMatchInlineSnapshot(`"PT0.6S"`); }); test('can be a number', () => { @@ -72,7 +78,7 @@ describe('#defaultValue', () => { duration({ defaultValue: 600, }).validate(undefined) - ).toMatchSnapshot(); + ).toMatchInlineSnapshot(`"PT0.6S"`); }); test('can be a function that returns compatible type', () => { @@ -103,12 +109,12 @@ describe('#defaultValue', () => { fromContext: duration({ defaultValue: contextRef('val') }), }).validate({}, { val: momentDuration(700, 'ms') }) ).toMatchInlineSnapshot(` -Object { - "fromContext": "PT0.7S", - "source": "PT0.6S", - "target": "PT0.6S", -} -`); + Object { + "fromContext": "PT0.7S", + "source": "PT0.6S", + "target": "PT0.6S", + } + `); expect( object({ @@ -117,12 +123,12 @@ Object { fromContext: duration({ defaultValue: contextRef('val') }), }).validate({}, { val: momentDuration(2, 'hour') }) ).toMatchInlineSnapshot(` -Object { - "fromContext": "PT2H", - "source": "PT1H", - "target": "PT1H", -} -`); + Object { + "fromContext": "PT2H", + "source": "PT1H", + "target": "PT1H", + } + `); expect( object({ @@ -131,29 +137,45 @@ Object { fromContext: duration({ defaultValue: contextRef('val') }), }).validate({}, { val: momentDuration(2, 'hour') }) ).toMatchInlineSnapshot(` -Object { - "fromContext": "PT2H", - "source": "PT1H", - "target": "PT1H", -} -`); + Object { + "fromContext": "PT2H", + "source": "PT1H", + "target": "PT1H", + } + `); }); }); test('returns error when not valid string or non-safe positive integer', () => { - expect(() => duration().validate(-123)).toThrowErrorMatchingSnapshot(); + expect(() => duration().validate(-123)).toThrowErrorMatchingInlineSnapshot( + `"Value in milliseconds is expected to be a safe positive integer."` + ); - expect(() => duration().validate(NaN)).toThrowErrorMatchingSnapshot(); + expect(() => duration().validate(NaN)).toThrowErrorMatchingInlineSnapshot( + `"Value in milliseconds is expected to be a safe positive integer."` + ); - expect(() => duration().validate(Infinity)).toThrowErrorMatchingSnapshot(); + expect(() => duration().validate(Infinity)).toThrowErrorMatchingInlineSnapshot( + `"Value in milliseconds is expected to be a safe positive integer."` + ); - expect(() => duration().validate(Math.pow(2, 53))).toThrowErrorMatchingSnapshot(); + expect(() => duration().validate(Math.pow(2, 53))).toThrowErrorMatchingInlineSnapshot( + `"Value in milliseconds is expected to be a safe positive integer."` + ); - expect(() => duration().validate([1, 2, 3])).toThrowErrorMatchingSnapshot(); + expect(() => duration().validate([1, 2, 3])).toThrowErrorMatchingInlineSnapshot( + `"expected value of type [moment.Duration] but got [Array]"` + ); - expect(() => duration().validate(/abc/)).toThrowErrorMatchingSnapshot(); + expect(() => duration().validate(/abc/)).toThrowErrorMatchingInlineSnapshot( + `"expected value of type [moment.Duration] but got [RegExp]"` + ); - expect(() => duration().validate('123foo')).toThrowErrorMatchingSnapshot(); + expect(() => duration().validate('123foo')).toThrowErrorMatchingInlineSnapshot( + `"Failed to parse value as time value. Value must be a duration in milliseconds, or follow the format [ms|s|m|h|d|w|M|Y] (e.g. '70ms', '5s', '3d', '1Y'), where the duration is a safe positive integer."` + ); - expect(() => duration().validate('123 456')).toThrowErrorMatchingSnapshot(); + expect(() => duration().validate('123 456')).toThrowErrorMatchingInlineSnapshot( + `"Failed to parse value as time value. Value must be a duration in milliseconds, or follow the format [ms|s|m|h|d|w|M|Y] (e.g. '70ms', '5s', '3d', '1Y'), where the duration is a safe positive integer."` + ); }); diff --git a/packages/kbn-config-schema/src/types/literal_type.test.ts b/packages/kbn-config-schema/src/types/literal_type.test.ts index a5ddff3152368f..abcf5bb2a3b2d8 100644 --- a/packages/kbn-config-schema/src/types/literal_type.test.ts +++ b/packages/kbn-config-schema/src/types/literal_type.test.ts @@ -38,17 +38,29 @@ test('handles null', () => { }); test('returns error when not correct', () => { - expect(() => literal('test').validate('foo')).toThrowErrorMatchingSnapshot(); + expect(() => literal('test').validate('foo')).toThrowErrorMatchingInlineSnapshot( + `"expected value to equal [test]"` + ); - expect(() => literal(true).validate(false)).toThrowErrorMatchingSnapshot(); + expect(() => literal(true).validate(false)).toThrowErrorMatchingInlineSnapshot( + `"expected value to equal [true]"` + ); - expect(() => literal('test').validate([1, 2, 3])).toThrowErrorMatchingSnapshot(); + expect(() => literal('test').validate([1, 2, 3])).toThrowErrorMatchingInlineSnapshot( + `"expected value to equal [test]"` + ); - expect(() => literal(123).validate('abc')).toThrowErrorMatchingSnapshot(); + expect(() => literal(123).validate('abc')).toThrowErrorMatchingInlineSnapshot( + `"expected value to equal [123]"` + ); - expect(() => literal(null).validate(42)).toThrowErrorMatchingSnapshot(); + expect(() => literal(null).validate(42)).toThrowErrorMatchingInlineSnapshot( + `"expected value to equal [null]"` + ); }); test('includes namespace in failure', () => { - expect(() => literal('test').validate('foo', {}, 'foo-namespace')).toThrowErrorMatchingSnapshot(); + expect(() => + literal('test').validate('foo', {}, 'foo-namespace') + ).toThrowErrorMatchingInlineSnapshot(`"[foo-namespace]: expected value to equal [test]"`); }); diff --git a/packages/kbn-config-schema/src/types/literal_type.ts b/packages/kbn-config-schema/src/types/literal_type.ts index b5ddaa2f68d4f6..5ba0b417683bd2 100644 --- a/packages/kbn-config-schema/src/types/literal_type.ts +++ b/packages/kbn-config-schema/src/types/literal_type.ts @@ -29,7 +29,7 @@ export class LiteralType extends Type { switch (type) { case 'any.required': case 'any.allowOnly': - return `expected value to equal [${expectedValue}] but got [${value}]`; + return `expected value to equal [${expectedValue}]`; } } } diff --git a/packages/kbn-config-schema/src/types/map_of_type.test.ts b/packages/kbn-config-schema/src/types/map_of_type.test.ts index 3cb3d2d0b68622..b015f51bdc8ad9 100644 --- a/packages/kbn-config-schema/src/types/map_of_type.test.ts +++ b/packages/kbn-config-schema/src/types/map_of_type.test.ts @@ -40,7 +40,7 @@ test('properly parse the value if input is a string', () => { test('fails if string input cannot be parsed', () => { const type = schema.mapOf(schema.string(), schema.string()); expect(() => type.validate(`invalidjson`)).toThrowErrorMatchingInlineSnapshot( - `"could not parse map value from [invalidjson]"` + `"could not parse map value from json input"` ); }); @@ -169,7 +169,7 @@ test('error preserves full path', () => { expect(() => type.validate({ grandParentKey: { parentKey: { a: 'some-value' } } }) ).toThrowErrorMatchingInlineSnapshot( - `"[grandParentKey.parentKey.key(\\"a\\")]: value is [a] but it must have a minimum length of [2]."` + `"[grandParentKey.parentKey.key(\\"a\\")]: value has length [1] but it must have a minimum length of [2]."` ); expect(() => diff --git a/packages/kbn-config-schema/src/types/map_type.ts b/packages/kbn-config-schema/src/types/map_type.ts index 1c0c473f98ec14..231c3726ae9d57 100644 --- a/packages/kbn-config-schema/src/types/map_type.ts +++ b/packages/kbn-config-schema/src/types/map_type.ts @@ -49,7 +49,7 @@ export class MapOfType extends Type> { case 'map.base': return `expected value of type [Map] or [object] but got [${typeDetect(value)}]`; case 'map.parse': - return `could not parse map value from [${value}]`; + return `could not parse map value from json input`; case 'map.key': case 'map.value': const childPathWithIndex = path.slice(); diff --git a/packages/kbn-config-schema/src/types/maybe_type.test.ts b/packages/kbn-config-schema/src/types/maybe_type.test.ts index c35fa18593520a..2a1278f5e801cf 100644 --- a/packages/kbn-config-schema/src/types/maybe_type.test.ts +++ b/packages/kbn-config-schema/src/types/maybe_type.test.ts @@ -42,23 +42,31 @@ test('returns undefined even if contained type has a default value', () => { test('validates contained type', () => { const type = schema.maybe(schema.string({ maxLength: 1 })); - expect(() => type.validate('foo')).toThrowErrorMatchingSnapshot(); + expect(() => type.validate('foo')).toThrowErrorMatchingInlineSnapshot( + `"value has length [3] but it must have a maximum length of [1]."` + ); }); test('validates basic type', () => { const type = schema.maybe(schema.string()); - expect(() => type.validate(666)).toThrowErrorMatchingSnapshot(); + expect(() => type.validate(666)).toThrowErrorMatchingInlineSnapshot( + `"expected value of type [string] but got [number]"` + ); }); test('fails if null', () => { const type = schema.maybe(schema.string()); - expect(() => type.validate(null)).toThrowErrorMatchingSnapshot(); + expect(() => type.validate(null)).toThrowErrorMatchingInlineSnapshot( + `"expected value of type [string] but got [null]"` + ); }); test('includes namespace in failure', () => { const type = schema.maybe(schema.string()); - expect(() => type.validate(null, {}, 'foo-namespace')).toThrowErrorMatchingSnapshot(); + expect(() => type.validate(null, {}, 'foo-namespace')).toThrowErrorMatchingInlineSnapshot( + `"[foo-namespace]: expected value of type [string] but got [null]"` + ); }); describe('maybe + object', () => { diff --git a/packages/kbn-config-schema/src/types/never_type.test.ts b/packages/kbn-config-schema/src/types/never_type.test.ts index 46f0b47f56ad60..2e5bc0e8c672df 100644 --- a/packages/kbn-config-schema/src/types/never_type.test.ts +++ b/packages/kbn-config-schema/src/types/never_type.test.ts @@ -22,10 +22,18 @@ import { schema } from '..'; test('throws on any value set', () => { const type = schema.never(); - expect(() => type.validate(1)).toThrowErrorMatchingSnapshot(); - expect(() => type.validate('a')).toThrowErrorMatchingSnapshot(); - expect(() => type.validate(null)).toThrowErrorMatchingSnapshot(); - expect(() => type.validate({})).toThrowErrorMatchingSnapshot(); + expect(() => type.validate(1)).toThrowErrorMatchingInlineSnapshot( + `"a value wasn't expected to be present"` + ); + expect(() => type.validate('a')).toThrowErrorMatchingInlineSnapshot( + `"a value wasn't expected to be present"` + ); + expect(() => type.validate(null)).toThrowErrorMatchingInlineSnapshot( + `"a value wasn't expected to be present"` + ); + expect(() => type.validate({})).toThrowErrorMatchingInlineSnapshot( + `"a value wasn't expected to be present"` + ); expect(() => type.validate(undefined)).not.toThrow(); }); @@ -37,7 +45,7 @@ test('throws on value set as object property', () => { expect(() => type.validate({ name: 'name', status: 'in progress' }) - ).toThrowErrorMatchingSnapshot(); + ).toThrowErrorMatchingInlineSnapshot(`"[name]: a value wasn't expected to be present"`); expect(() => type.validate({ status: 'in progress' })).not.toThrow(); expect(() => type.validate({ name: undefined, status: 'in progress' })).not.toThrow(); @@ -71,5 +79,5 @@ test('works for conditional types', () => { context_value_2: 1, } ) - ).toThrowErrorMatchingSnapshot(); + ).toThrowErrorMatchingInlineSnapshot(`"[name]: a value wasn't expected to be present"`); }); diff --git a/packages/kbn-config-schema/src/types/nullable_type.test.ts b/packages/kbn-config-schema/src/types/nullable_type.test.ts index ed04202950a2c9..fb9d544a3eb0eb 100644 --- a/packages/kbn-config-schema/src/types/nullable_type.test.ts +++ b/packages/kbn-config-schema/src/types/nullable_type.test.ts @@ -94,13 +94,21 @@ test('returns null even if contained type has a default value', () => { test('validates contained type', () => { const type = schema.nullable(schema.string({ maxLength: 1 })); - expect(() => type.validate('foo')).toThrowErrorMatchingSnapshot(); + expect(() => type.validate('foo')).toThrowErrorMatchingInlineSnapshot(` +"types that failed validation: +- [0]: value has length [3] but it must have a maximum length of [1]. +- [1]: expected value to equal [null]" +`); }); test('validates basic type', () => { const type = schema.nullable(schema.string()); - expect(() => type.validate(666)).toThrowErrorMatchingSnapshot(); + expect(() => type.validate(666)).toThrowErrorMatchingInlineSnapshot(` +"types that failed validation: +- [0]: expected value of type [string] but got [number] +- [1]: expected value to equal [null]" +`); }); test('validates type in object', () => { @@ -121,11 +129,19 @@ test('validates type errors in object', () => { bar: schema.nullable(schema.boolean()), }); - expect(() => type.validate({ foo: 'ab' })).toThrowErrorMatchingSnapshot(); + expect(() => type.validate({ foo: 'ab' })).toThrowErrorMatchingInlineSnapshot(` +"[foo]: types that failed validation: +- [foo.0]: value has length [2] but it must have a maximum length of [1]. +- [foo.1]: expected value to equal [null]" +`); }); test('includes namespace in failure', () => { const type = schema.nullable(schema.string({ maxLength: 1 })); - expect(() => type.validate('foo', {}, 'foo-namespace')).toThrowErrorMatchingSnapshot(); + expect(() => type.validate('foo', {}, 'foo-namespace')).toThrowErrorMatchingInlineSnapshot(` +"[foo-namespace]: types that failed validation: +- [foo-namespace.0]: value has length [3] but it must have a maximum length of [1]. +- [foo-namespace.1]: expected value to equal [null]" +`); }); diff --git a/packages/kbn-config-schema/src/types/number_type.test.ts b/packages/kbn-config-schema/src/types/number_type.test.ts index b85d5113563eb6..cfcb0e99afbd5c 100644 --- a/packages/kbn-config-schema/src/types/number_type.test.ts +++ b/packages/kbn-config-schema/src/types/number_type.test.ts @@ -32,17 +32,23 @@ test('handles numeric strings with floats', () => { }); test('fails if number is `NaN`', () => { - expect(() => schema.number().validate(NaN)).toThrowErrorMatchingSnapshot(); + expect(() => schema.number().validate(NaN)).toThrowErrorMatchingInlineSnapshot( + `"expected value of type [number] but got [number]"` + ); }); test('is required by default', () => { - expect(() => schema.number().validate(undefined)).toThrowErrorMatchingSnapshot(); + expect(() => schema.number().validate(undefined)).toThrowErrorMatchingInlineSnapshot( + `"expected value of type [number] but got [undefined]"` + ); }); test('includes namespace in failure', () => { expect(() => schema.number().validate(undefined, {}, 'foo-namespace') - ).toThrowErrorMatchingSnapshot(); + ).toThrowErrorMatchingInlineSnapshot( + `"[foo-namespace]: expected value of type [number] but got [undefined]"` + ); }); describe('#min', () => { @@ -51,7 +57,9 @@ describe('#min', () => { }); test('returns error when smaller number', () => { - expect(() => schema.number({ min: 4 }).validate(3)).toThrowErrorMatchingSnapshot(); + expect(() => schema.number({ min: 4 }).validate(3)).toThrowErrorMatchingInlineSnapshot( + `"Value must be equal to or greater than [4]."` + ); }); }); @@ -61,7 +69,9 @@ describe('#max', () => { }); test('returns error when larger number', () => { - expect(() => schema.number({ max: 2 }).validate(3)).toThrowErrorMatchingSnapshot(); + expect(() => schema.number({ max: 2 }).validate(3)).toThrowErrorMatchingInlineSnapshot( + `"Value must be equal to or lower than [2]."` + ); }); }); @@ -76,9 +86,15 @@ describe('#defaultValue', () => { }); test('returns error when not number or numeric string', () => { - expect(() => schema.number().validate('test')).toThrowErrorMatchingSnapshot(); + expect(() => schema.number().validate('test')).toThrowErrorMatchingInlineSnapshot( + `"expected value of type [number] but got [string]"` + ); - expect(() => schema.number().validate([1, 2, 3])).toThrowErrorMatchingSnapshot(); + expect(() => schema.number().validate([1, 2, 3])).toThrowErrorMatchingInlineSnapshot( + `"expected value of type [number] but got [Array]"` + ); - expect(() => schema.number().validate(/abc/)).toThrowErrorMatchingSnapshot(); + expect(() => schema.number().validate(/abc/)).toThrowErrorMatchingInlineSnapshot( + `"expected value of type [number] but got [RegExp]"` + ); }); diff --git a/packages/kbn-config-schema/src/types/number_type.ts b/packages/kbn-config-schema/src/types/number_type.ts index ada4d1909c9178..81ca4449122a3a 100644 --- a/packages/kbn-config-schema/src/types/number_type.ts +++ b/packages/kbn-config-schema/src/types/number_type.ts @@ -46,9 +46,9 @@ export class NumberType extends Type { case 'number.base': return `expected value of type [number] but got [${typeDetect(value)}]`; case 'number.min': - return `Value is [${value}] but it must be equal to or greater than [${limit}].`; + return `Value must be equal to or greater than [${limit}].`; case 'number.max': - return `Value is [${value}] but it must be equal to or lower than [${limit}].`; + return `Value must be equal to or lower than [${limit}].`; } } } diff --git a/packages/kbn-config-schema/src/types/object_type.test.ts b/packages/kbn-config-schema/src/types/object_type.test.ts index 5ddefcf261e24b..47a0f5f7a5491c 100644 --- a/packages/kbn-config-schema/src/types/object_type.test.ts +++ b/packages/kbn-config-schema/src/types/object_type.test.ts @@ -49,7 +49,7 @@ test('fails if string input cannot be parsed', () => { name: schema.string(), }); expect(() => type.validate(`invalidjson`)).toThrowErrorMatchingInlineSnapshot( - `"could not parse object value from [invalidjson]"` + `"could not parse object value from json input"` ); }); @@ -181,7 +181,7 @@ test('called with wrong type', () => { const type = schema.object({}); expect(() => type.validate('foo')).toThrowErrorMatchingInlineSnapshot( - `"could not parse object value from [foo]"` + `"could not parse object value from json input"` ); expect(() => type.validate(123)).toThrowErrorMatchingInlineSnapshot( `"expected a plain object value, but found [number] instead."` diff --git a/packages/kbn-config-schema/src/types/object_type.ts b/packages/kbn-config-schema/src/types/object_type.ts index d30c9921169425..5a50e714a59311 100644 --- a/packages/kbn-config-schema/src/types/object_type.ts +++ b/packages/kbn-config-schema/src/types/object_type.ts @@ -71,7 +71,7 @@ export class ObjectType

extends Type> case 'object.base': return `expected a plain object value, but found [${typeDetect(value)}] instead.`; case 'object.parse': - return `could not parse object value from [${value}]`; + return `could not parse object value from json input`; case 'object.allowUnknown': return `definition for this key is missing`; case 'object.child': diff --git a/packages/kbn-config-schema/src/types/one_of_type.test.ts b/packages/kbn-config-schema/src/types/one_of_type.test.ts index c9da1a6cd8494b..deb87a485cdfe5 100644 --- a/packages/kbn-config-schema/src/types/one_of_type.test.ts +++ b/packages/kbn-config-schema/src/types/one_of_type.test.ts @@ -75,13 +75,20 @@ test('handles object', () => { test('handles object with wrong type', () => { const type = schema.oneOf([schema.object({ age: schema.number() })]); - expect(() => type.validate({ age: 'foo' })).toThrowErrorMatchingSnapshot(); + expect(() => type.validate({ age: 'foo' })).toThrowErrorMatchingInlineSnapshot(` +"types that failed validation: +- [0.age]: expected value of type [number] but got [string]" +`); }); test('includes namespace in failure', () => { const type = schema.oneOf([schema.object({ age: schema.number() })]); - expect(() => type.validate({ age: 'foo' }, {}, 'foo-namespace')).toThrowErrorMatchingSnapshot(); + expect(() => type.validate({ age: 'foo' }, {}, 'foo-namespace')) + .toThrowErrorMatchingInlineSnapshot(` +"[foo-namespace]: types that failed validation: +- [foo-namespace.0.age]: expected value of type [number] but got [string]" +`); }); test('handles multiple objects with same key', () => { @@ -110,20 +117,33 @@ test('handles maybe', () => { test('fails if not matching type', () => { const type = schema.oneOf([schema.string()]); - expect(() => type.validate(false)).toThrowErrorMatchingSnapshot(); - expect(() => type.validate(123)).toThrowErrorMatchingSnapshot(); + expect(() => type.validate(false)).toThrowErrorMatchingInlineSnapshot(` +"types that failed validation: +- [0]: expected value of type [string] but got [boolean]" +`); + expect(() => type.validate(123)).toThrowErrorMatchingInlineSnapshot(` +"types that failed validation: +- [0]: expected value of type [string] but got [number]" +`); }); test('fails if not matching multiple types', () => { const type = schema.oneOf([schema.string(), schema.number()]); - expect(() => type.validate(false)).toThrowErrorMatchingSnapshot(); + expect(() => type.validate(false)).toThrowErrorMatchingInlineSnapshot(` +"types that failed validation: +- [0]: expected value of type [string] but got [boolean] +- [1]: expected value of type [number] but got [boolean]" +`); }); test('fails if not matching literal', () => { const type = schema.oneOf([schema.literal('foo')]); - expect(() => type.validate('bar')).toThrowErrorMatchingSnapshot(); + expect(() => type.validate('bar')).toThrowErrorMatchingInlineSnapshot(` +"types that failed validation: +- [0]: expected value to equal [foo]" +`); }); test('fails if nested union type fail', () => { @@ -138,7 +158,7 @@ test('fails if nested union type fail', () => { - [0]: expected value of type [boolean] but got [string] - [1]: types that failed validation: - [0]: types that failed validation: - - [0]: could not parse object value from [aaa] + - [0]: could not parse object value from json input - [1]: expected value of type [number] but got [string]" `); }); diff --git a/packages/kbn-config-schema/src/types/record_of_type.test.ts b/packages/kbn-config-schema/src/types/record_of_type.test.ts index f3ab1925597b54..ef15e7b0f6ad6b 100644 --- a/packages/kbn-config-schema/src/types/record_of_type.test.ts +++ b/packages/kbn-config-schema/src/types/record_of_type.test.ts @@ -73,8 +73,8 @@ test('fails when not receiving expected key type', () => { expect(() => type.validate(value)).toThrowErrorMatchingInlineSnapshot(` "[key(\\"name\\")]: types that failed validation: -- [0]: expected value to equal [nickName] but got [name] -- [1]: expected value to equal [lastName] but got [name]" +- [0]: expected value to equal [nickName] +- [1]: expected value to equal [lastName]" `); }); @@ -88,8 +88,8 @@ test('fails after parsing when not receiving expected key type', () => { expect(() => type.validate(value)).toThrowErrorMatchingInlineSnapshot(` "[key(\\"name\\")]: types that failed validation: -- [0]: expected value to equal [nickName] but got [name] -- [1]: expected value to equal [lastName] but got [name]" +- [0]: expected value to equal [nickName] +- [1]: expected value to equal [lastName]" `); }); @@ -118,7 +118,7 @@ test('includes namespace in failure when wrong key type', () => { }; expect(() => type.validate(value, {}, 'foo-namespace')).toThrowErrorMatchingInlineSnapshot( - `"[foo-namespace.key(\\"name\\")]: value is [name] but it must have a minimum length of [10]."` + `"[foo-namespace.key(\\"name\\")]: value has length [4] but it must have a minimum length of [10]."` ); }); @@ -169,7 +169,7 @@ test('error preserves full path', () => { expect(() => type.validate({ grandParentKey: { parentKey: { a: 'some-value' } } }) ).toThrowErrorMatchingInlineSnapshot( - `"[grandParentKey.parentKey.key(\\"a\\")]: value is [a] but it must have a minimum length of [2]."` + `"[grandParentKey.parentKey.key(\\"a\\")]: value has length [1] but it must have a minimum length of [2]."` ); expect(() => diff --git a/packages/kbn-config-schema/src/types/record_type.ts b/packages/kbn-config-schema/src/types/record_type.ts index b795c83acdadbf..c6d4b4d71b4f1d 100644 --- a/packages/kbn-config-schema/src/types/record_type.ts +++ b/packages/kbn-config-schema/src/types/record_type.ts @@ -41,7 +41,7 @@ export class RecordOfType extends Type> { case 'record.base': return `expected value of type [object] but got [${typeDetect(value)}]`; case 'record.parse': - return `could not parse record value from [${value}]`; + return `could not parse record value from json input`; case 'record.key': case 'record.value': const childPathWithIndex = path.slice(); diff --git a/packages/kbn-config-schema/src/types/stream_type.test.ts b/packages/kbn-config-schema/src/types/stream_type.test.ts index 011fa6373df335..2e6f31ad09b34f 100644 --- a/packages/kbn-config-schema/src/types/stream_type.test.ts +++ b/packages/kbn-config-schema/src/types/stream_type.test.ts @@ -41,13 +41,17 @@ test('Passthrough is valid', () => { }); test('is required by default', () => { - expect(() => schema.buffer().validate(undefined)).toThrowErrorMatchingSnapshot(); + expect(() => schema.buffer().validate(undefined)).toThrowErrorMatchingInlineSnapshot( + `"expected value of type [Buffer] but got [undefined]"` + ); }); test('includes namespace in failure', () => { expect(() => schema.stream().validate(undefined, {}, 'foo-namespace') - ).toThrowErrorMatchingSnapshot(); + ).toThrowErrorMatchingInlineSnapshot( + `"[foo-namespace]: expected value of type [Stream] but got [undefined]"` + ); }); describe('#defaultValue', () => { @@ -63,9 +67,15 @@ describe('#defaultValue', () => { }); test('returns error when not a stream', () => { - expect(() => schema.stream().validate(123)).toThrowErrorMatchingSnapshot(); + expect(() => schema.stream().validate(123)).toThrowErrorMatchingInlineSnapshot( + `"expected value of type [Stream] but got [number]"` + ); - expect(() => schema.stream().validate([1, 2, 3])).toThrowErrorMatchingSnapshot(); + expect(() => schema.stream().validate([1, 2, 3])).toThrowErrorMatchingInlineSnapshot( + `"expected value of type [Stream] but got [Array]"` + ); - expect(() => schema.stream().validate('abc')).toThrowErrorMatchingSnapshot(); + expect(() => schema.stream().validate('abc')).toThrowErrorMatchingInlineSnapshot( + `"expected value of type [Stream] but got [string]"` + ); }); diff --git a/packages/kbn-config-schema/src/types/string_type.test.ts b/packages/kbn-config-schema/src/types/string_type.test.ts index d599ea65c5ae22..c1d853fe82b82e 100644 --- a/packages/kbn-config-schema/src/types/string_type.test.ts +++ b/packages/kbn-config-schema/src/types/string_type.test.ts @@ -28,13 +28,17 @@ test('allows empty strings', () => { }); test('is required by default', () => { - expect(() => schema.string().validate(undefined)).toThrowErrorMatchingSnapshot(); + expect(() => schema.string().validate(undefined)).toThrowErrorMatchingInlineSnapshot( + `"expected value of type [string] but got [undefined]"` + ); }); test('includes namespace in failure', () => { expect(() => schema.string().validate(undefined, {}, 'foo-namespace') - ).toThrowErrorMatchingSnapshot(); + ).toThrowErrorMatchingInlineSnapshot( + `"[foo-namespace]: expected value of type [string] but got [undefined]"` + ); }); describe('#minLength', () => { @@ -43,11 +47,17 @@ describe('#minLength', () => { }); test('returns error when shorter string', () => { - expect(() => schema.string({ minLength: 4 }).validate('foo')).toThrowErrorMatchingSnapshot(); + expect(() => + schema.string({ minLength: 4 }).validate('foo') + ).toThrowErrorMatchingInlineSnapshot( + `"value has length [3] but it must have a minimum length of [4]."` + ); }); test('returns error when empty string', () => { - expect(() => schema.string({ minLength: 2 }).validate('')).toThrowErrorMatchingSnapshot(); + expect(() => schema.string({ minLength: 2 }).validate('')).toThrowErrorMatchingInlineSnapshot( + `"value has length [0] but it must have a minimum length of [2]."` + ); }); }); @@ -57,7 +67,11 @@ describe('#maxLength', () => { }); test('returns error when longer string', () => { - expect(() => schema.string({ maxLength: 2 }).validate('foo')).toThrowErrorMatchingSnapshot(); + expect(() => + schema.string({ maxLength: 2 }).validate('foo') + ).toThrowErrorMatchingInlineSnapshot( + `"value has length [3] but it must have a maximum length of [2]."` + ); }); }); @@ -84,23 +98,37 @@ describe('#hostname', () => { test('returns error when value is not a valid hostname', () => { const hostNameSchema = schema.string({ hostname: true }); - expect(() => hostNameSchema.validate('host:name')).toThrowErrorMatchingSnapshot(); - expect(() => hostNameSchema.validate('localhost:5601')).toThrowErrorMatchingSnapshot(); - expect(() => hostNameSchema.validate('-')).toThrowErrorMatchingSnapshot(); - expect(() => hostNameSchema.validate('0:?:0:0:0:0:0:1')).toThrowErrorMatchingSnapshot(); + expect(() => hostNameSchema.validate('host:name')).toThrowErrorMatchingInlineSnapshot( + `"value must be a valid hostname (see RFC 1123)."` + ); + expect(() => hostNameSchema.validate('localhost:5601')).toThrowErrorMatchingInlineSnapshot( + `"value must be a valid hostname (see RFC 1123)."` + ); + expect(() => hostNameSchema.validate('-')).toThrowErrorMatchingInlineSnapshot( + `"value must be a valid hostname (see RFC 1123)."` + ); + expect(() => hostNameSchema.validate('0:?:0:0:0:0:0:1')).toThrowErrorMatchingInlineSnapshot( + `"value must be a valid hostname (see RFC 1123)."` + ); const tooLongHostName = 'a'.repeat(256); - expect(() => hostNameSchema.validate(tooLongHostName)).toThrowErrorMatchingSnapshot(); + expect(() => hostNameSchema.validate(tooLongHostName)).toThrowErrorMatchingInlineSnapshot( + `"value must be a valid hostname (see RFC 1123)."` + ); }); test('returns error when empty string', () => { - expect(() => schema.string({ hostname: true }).validate('')).toThrowErrorMatchingSnapshot(); + expect(() => schema.string({ hostname: true }).validate('')).toThrowErrorMatchingInlineSnapshot( + `"any.empty"` + ); }); test('supports string validation rules', () => { expect(() => schema.string({ hostname: true, maxLength: 3 }).validate('www.example.com') - ).toThrowErrorMatchingSnapshot(); + ).toThrowErrorMatchingInlineSnapshot( + `"value has length [15] but it must have a maximum length of [3]."` + ); }); }); @@ -146,20 +174,30 @@ describe('#validate', () => { test('throws when returns string', () => { const validate = () => 'validator failure'; - expect(() => schema.string({ validate }).validate('foo')).toThrowErrorMatchingSnapshot(); + expect(() => schema.string({ validate }).validate('foo')).toThrowErrorMatchingInlineSnapshot( + `"validator failure"` + ); }); test('throw when empty string', () => { const validate = () => 'validator failure'; - expect(() => schema.string({ validate }).validate('')).toThrowErrorMatchingSnapshot(); + expect(() => schema.string({ validate }).validate('')).toThrowErrorMatchingInlineSnapshot( + `"validator failure"` + ); }); }); test('returns error when not string', () => { - expect(() => schema.string().validate(123)).toThrowErrorMatchingSnapshot(); + expect(() => schema.string().validate(123)).toThrowErrorMatchingInlineSnapshot( + `"expected value of type [string] but got [number]"` + ); - expect(() => schema.string().validate([1, 2, 3])).toThrowErrorMatchingSnapshot(); + expect(() => schema.string().validate([1, 2, 3])).toThrowErrorMatchingInlineSnapshot( + `"expected value of type [string] but got [Array]"` + ); - expect(() => schema.string().validate(/abc/)).toThrowErrorMatchingSnapshot(); + expect(() => schema.string().validate(/abc/)).toThrowErrorMatchingInlineSnapshot( + `"expected value of type [string] but got [RegExp]"` + ); }); diff --git a/packages/kbn-config-schema/src/types/string_type.ts b/packages/kbn-config-schema/src/types/string_type.ts index 6d5fa93c0299c6..7f49440b8d7e29 100644 --- a/packages/kbn-config-schema/src/types/string_type.ts +++ b/packages/kbn-config-schema/src/types/string_type.ts @@ -45,7 +45,7 @@ export class StringType extends Type { if (options.minLength !== undefined) { schema = schema.custom(value => { if (value.length < options.minLength!) { - return `value is [${value}] but it must have a minimum length of [${options.minLength}].`; + return `value has length [${value.length}] but it must have a minimum length of [${options.minLength}].`; } }); } @@ -53,7 +53,7 @@ export class StringType extends Type { if (options.maxLength !== undefined) { schema = schema.custom(value => { if (value.length > options.maxLength!) { - return `value is [${value}] but it must have a maximum length of [${options.maxLength}].`; + return `value has length [${value.length}] but it must have a maximum length of [${options.maxLength}].`; } }); } @@ -66,7 +66,7 @@ export class StringType extends Type { case 'any.required': return `expected value of type [string] but got [${typeDetect(value)}]`; case 'string.hostname': - return `value is [${value}] but it must be a valid hostname (see RFC 1123).`; + return `value must be a valid hostname (see RFC 1123).`; } } } diff --git a/packages/kbn-config-schema/src/types/uri_type.test.ts b/packages/kbn-config-schema/src/types/uri_type.test.ts index 1345b47a63c1ff..72e5ca6f7171e6 100644 --- a/packages/kbn-config-schema/src/types/uri_type.test.ts +++ b/packages/kbn-config-schema/src/types/uri_type.test.ts @@ -20,7 +20,9 @@ import { schema } from '..'; test('is required by default', () => { - expect(() => schema.uri().validate(undefined)).toThrowErrorMatchingSnapshot(); + expect(() => schema.uri().validate(undefined)).toThrowErrorMatchingInlineSnapshot( + `"expected value of type [string] but got [undefined]."` + ); }); test('returns value for valid URI as per RFC3986', () => { @@ -54,17 +56,23 @@ test('returns value for valid URI as per RFC3986', () => { test('returns error when value is not a URI', () => { const uriSchema = schema.uri(); - expect(() => uriSchema.validate('3domain.local')).toThrowErrorMatchingSnapshot(); + expect(() => uriSchema.validate('3domain.local')).toThrowErrorMatchingInlineSnapshot( + `"value must be a valid URI (see RFC 3986)."` + ); expect(() => uriSchema.validate('http://8010:0:0:0:9:500:300C:200A') - ).toThrowErrorMatchingSnapshot(); - expect(() => uriSchema.validate('-')).toThrowErrorMatchingSnapshot(); + ).toThrowErrorMatchingInlineSnapshot(`"value must be a valid URI (see RFC 3986)."`); + expect(() => uriSchema.validate('-')).toThrowErrorMatchingInlineSnapshot( + `"value must be a valid URI (see RFC 3986)."` + ); expect(() => uriSchema.validate('https://example.com?baz[]=foo&baz[]=bar') - ).toThrowErrorMatchingSnapshot(); + ).toThrowErrorMatchingInlineSnapshot(`"value must be a valid URI (see RFC 3986)."`); const tooLongUri = `http://${'a'.repeat(256)}`; - expect(() => uriSchema.validate(tooLongUri)).toThrowErrorMatchingSnapshot(); + expect(() => uriSchema.validate(tooLongUri)).toThrowErrorMatchingInlineSnapshot( + `"value must be a valid URI (see RFC 3986)."` + ); }); describe('#scheme', () => { @@ -78,8 +86,12 @@ describe('#scheme', () => { test('returns error when shorter string', () => { const uriSchema = schema.uri({ scheme: ['http', 'https'] }); - expect(() => uriSchema.validate('ftp://elastic.co')).toThrowErrorMatchingSnapshot(); - expect(() => uriSchema.validate('file:///kibana.log')).toThrowErrorMatchingSnapshot(); + expect(() => uriSchema.validate('ftp://elastic.co')).toThrowErrorMatchingInlineSnapshot( + `"expected URI with scheme [http|https]."` + ); + expect(() => uriSchema.validate('file:///kibana.log')).toThrowErrorMatchingInlineSnapshot( + `"expected URI with scheme [http|https]."` + ); }); }); @@ -131,14 +143,20 @@ describe('#validate', () => { expect(() => schema.uri({ validate }).validate('http://kibana.local') - ).toThrowErrorMatchingSnapshot(); + ).toThrowErrorMatchingInlineSnapshot(`"validator failure"`); }); }); test('returns error when not string', () => { - expect(() => schema.uri().validate(123)).toThrowErrorMatchingSnapshot(); + expect(() => schema.uri().validate(123)).toThrowErrorMatchingInlineSnapshot( + `"expected value of type [string] but got [number]."` + ); - expect(() => schema.uri().validate([1, 2, 3])).toThrowErrorMatchingSnapshot(); + expect(() => schema.uri().validate([1, 2, 3])).toThrowErrorMatchingInlineSnapshot( + `"expected value of type [string] but got [Array]."` + ); - expect(() => schema.uri().validate(/abc/)).toThrowErrorMatchingSnapshot(); + expect(() => schema.uri().validate(/abc/)).toThrowErrorMatchingInlineSnapshot( + `"expected value of type [string] but got [RegExp]."` + ); }); diff --git a/packages/kbn-config-schema/src/types/uri_type.ts b/packages/kbn-config-schema/src/types/uri_type.ts index df1ce9e869d3b4..f365ed35e3579a 100644 --- a/packages/kbn-config-schema/src/types/uri_type.ts +++ b/packages/kbn-config-schema/src/types/uri_type.ts @@ -36,9 +36,9 @@ export class URIType extends Type { case 'string.base': return `expected value of type [string] but got [${typeDetect(value)}].`; case 'string.uriCustomScheme': - return `expected URI with scheme [${scheme}] but got [${value}].`; + return `expected URI with scheme [${scheme}].`; case 'string.uri': - return `value is [${value}] but it must be a valid URI (see RFC 3986).`; + return `value must be a valid URI (see RFC 3986).`; } } } diff --git a/packages/kbn-storybook/storybook_config/webpack.config.js b/packages/kbn-storybook/storybook_config/webpack.config.js index 72ff9162ffe6c9..1531c1d22b01bc 100644 --- a/packages/kbn-storybook/storybook_config/webpack.config.js +++ b/packages/kbn-storybook/storybook_config/webpack.config.js @@ -19,6 +19,7 @@ const { resolve } = require('path'); const webpack = require('webpack'); +const { stringifyRequest } = require('loader-utils'); const CopyWebpackPlugin = require('copy-webpack-plugin'); const { REPO_ROOT, DLL_DIST_DIR } = require('../lib/constants'); // eslint-disable-next-line import/no-unresolved @@ -72,6 +73,38 @@ module.exports = async ({ config }) => { ], }); + // Enable SASS + config.module.rules.push({ + test: /\.scss$/, + exclude: /\.module.(s(a|c)ss)$/, + use: [ + { loader: 'style-loader' }, + { loader: 'css-loader', options: { importLoaders: 2 } }, + { + loader: 'postcss-loader', + options: { + config: { + path: resolve(REPO_ROOT, 'src/optimize/'), + }, + }, + }, + { + loader: 'sass-loader', + options: { + prependData(loaderContext) { + return `@import ${stringifyRequest( + loaderContext, + resolve(REPO_ROOT, 'src/legacy/ui/public/styles/_styling_constants.scss') + )};\n`; + }, + sassOptions: { + includePaths: [resolve(REPO_ROOT, 'node_modules')], + }, + }, + }, + ], + }); + // Reference the built DLL file of static(ish) dependencies, which are removed // during kbn:bootstrap and rebuilt if missing. config.plugins.push( @@ -96,7 +129,7 @@ module.exports = async ({ config }) => { ); // Tell Webpack about the ts/x extensions - config.resolve.extensions.push('.ts', '.tsx'); + config.resolve.extensions.push('.ts', '.tsx', '.scss'); // Load custom Webpack config specified by a plugin. if (currentConfig.webpackHook) { diff --git a/src/cli_plugin/install/cleanup.js b/src/cli_plugin/install/cleanup.js index fa4bdcf4f69669..eaa25962ef0e44 100644 --- a/src/cli_plugin/install/cleanup.js +++ b/src/cli_plugin/install/cleanup.js @@ -27,7 +27,7 @@ export function cleanPrevious(settings, logger) { logger.log('Found previous install attempt. Deleting...'); try { - del.sync(settings.workingPath); + del.sync(settings.workingPath, { force: true }); } catch (e) { reject(e); } diff --git a/src/cli_plugin/install/install.js b/src/cli_plugin/install/install.js index 5a341e67dc128a..92be2ac2503202 100644 --- a/src/cli_plugin/install/install.js +++ b/src/cli_plugin/install/install.js @@ -46,7 +46,7 @@ export default async function install(settings, logger) { await extract(settings, logger); - del.sync(settings.tempArchiveFile); + del.sync(settings.tempArchiveFile, { force: true }); existingInstall(settings, logger); diff --git a/src/cli_plugin/remove/remove.js b/src/cli_plugin/remove/remove.js index 8432d0f44836ba..353e592390ff40 100644 --- a/src/cli_plugin/remove/remove.js +++ b/src/cli_plugin/remove/remove.js @@ -37,7 +37,7 @@ export default function remove(settings, logger) { } logger.log(`Removing ${settings.plugin}...`); - del.sync(settings.pluginPath); + del.sync(settings.pluginPath, { force: true }); logger.log('Plugin removal complete'); } catch (err) { logger.error(`Unable to remove plugin because of error: "${err.message}"`); diff --git a/src/core/MIGRATION.md b/src/core/MIGRATION.md index 4dd6bedfa4f0ce..c5e649f7d9d5c8 100644 --- a/src/core/MIGRATION.md +++ b/src/core/MIGRATION.md @@ -1178,10 +1178,10 @@ import { setup, start } from '../core_plugins/visualizations/public/legacy'; | `ui/index_patterns` | `data.indexPatterns` | still in progress | | `ui/registry/field_formats` | `data.fieldFormats` | | | `ui/registry/feature_catalogue` | `home.featureCatalogue.register` | Must add `home` as a dependency in your kibana.json. | -| `ui/registry/vis_types` | `visualizations.types` | -- | -| `ui/vis` | `visualizations.types` | -- | +| `ui/registry/vis_types` | `visualizations` | -- | +| `ui/vis` | `visualizations` | -- | | `ui/share` | `share` | `showShareContextMenu` is now called `toggleShareContextMenu`, `ShareContextMenuExtensionsRegistryProvider` is now called `register` | -| `ui/vis/vis_factory` | `visualizations.types` | -- | +| `ui/vis/vis_factory` | `visualizations` | -- | | `ui/vis/vis_filters` | `visualizations.filters` | -- | | `ui/utils/parse_es_interval` | `import { parseEsInterval } from '../data/public'` | `parseEsInterval`, `ParsedInterval`, `InvalidEsCalendarIntervalError`, `InvalidEsIntervalFormatError` items were moved to the `Data Plugin` as a static code | diff --git a/src/core/public/application/capabilities/capabilities_service.tsx b/src/core/public/application/capabilities/capabilities_service.tsx index 05d718e1073dfa..d602422c146348 100644 --- a/src/core/public/application/capabilities/capabilities_service.tsx +++ b/src/core/public/application/capabilities/capabilities_service.tsx @@ -37,8 +37,7 @@ export interface CapabilitiesStart { */ export class CapabilitiesService { public async start({ appIds, http }: StartDeps): Promise { - const route = http.anonymousPaths.isAnonymous(window.location.pathname) ? '/defaults' : ''; - const capabilities = await http.post(`/api/core/capabilities${route}`, { + const capabilities = await http.post('/api/core/capabilities', { body: JSON.stringify({ applications: appIds }), }); diff --git a/src/core/server/capabilities/capabilities_service.test.ts b/src/core/server/capabilities/capabilities_service.test.ts index aace0b9debf9c0..7d2e7391aa8d4b 100644 --- a/src/core/server/capabilities/capabilities_service.test.ts +++ b/src/core/server/capabilities/capabilities_service.test.ts @@ -41,8 +41,8 @@ describe('CapabilitiesService', () => { }); it('registers the capabilities routes', async () => { - expect(http.createRouter).toHaveBeenCalledWith('/api/core/capabilities'); - expect(router.post).toHaveBeenCalledTimes(2); + expect(http.createRouter).toHaveBeenCalledWith(''); + expect(router.post).toHaveBeenCalledTimes(1); expect(router.post).toHaveBeenCalledWith(expect.any(Object), expect.any(Function)); }); diff --git a/src/core/server/capabilities/routes/index.ts b/src/core/server/capabilities/routes/index.ts index ccaa4621d70035..74c485986a77b2 100644 --- a/src/core/server/capabilities/routes/index.ts +++ b/src/core/server/capabilities/routes/index.ts @@ -22,6 +22,6 @@ import { InternalHttpServiceSetup } from '../../http'; import { registerCapabilitiesRoutes } from './resolve_capabilities'; export function registerRoutes(http: InternalHttpServiceSetup, resolver: CapabilitiesResolver) { - const router = http.createRouter('/api/core/capabilities'); + const router = http.createRouter(''); registerCapabilitiesRoutes(router, resolver); } diff --git a/src/core/server/capabilities/routes/resolve_capabilities.ts b/src/core/server/capabilities/routes/resolve_capabilities.ts index 5e1d49b4b1b7e8..3fb1bb3d13d0bf 100644 --- a/src/core/server/capabilities/routes/resolve_capabilities.ts +++ b/src/core/server/capabilities/routes/resolve_capabilities.ts @@ -22,30 +22,24 @@ import { IRouter } from '../../http'; import { CapabilitiesResolver } from '../resolve_capabilities'; export function registerCapabilitiesRoutes(router: IRouter, resolver: CapabilitiesResolver) { - // Capabilities are fetched on both authenticated and anonymous routes. - // However when `authRequired` is false, authentication is not performed - // and only default capabilities are returned (all disabled), even for authenticated users. - // So we need two endpoints to handle both scenarios. - [true, false].forEach(authRequired => { - router.post( - { - path: authRequired ? '' : '/defaults', - options: { - authRequired, - }, - validate: { - body: schema.object({ - applications: schema.arrayOf(schema.string()), - }), - }, + router.post( + { + path: '/api/core/capabilities', + options: { + authRequired: 'optional', }, - async (ctx, req, res) => { - const { applications } = req.body; - const capabilities = await resolver(req, applications); - return res.ok({ - body: capabilities, - }); - } - ); - }); + validate: { + body: schema.object({ + applications: schema.arrayOf(schema.string()), + }), + }, + }, + async (ctx, req, res) => { + const { applications } = req.body; + const capabilities = await resolver(req, applications); + return res.ok({ + body: capabilities, + }); + } + ); } diff --git a/src/core/server/http/__snapshots__/http_config.test.ts.snap b/src/core/server/http/__snapshots__/http_config.test.ts.snap index 28933a035c8709..07c153a7a8a200 100644 --- a/src/core/server/http/__snapshots__/http_config.test.ts.snap +++ b/src/core/server/http/__snapshots__/http_config.test.ts.snap @@ -87,7 +87,7 @@ exports[`throws if basepath is missing prepended slash 1`] = `"[basePath]: must exports[`throws if basepath is not specified, but rewriteBasePath is set 1`] = `"cannot use [rewriteBasePath] when [basePath] is not specified"`; -exports[`throws if invalid hostname 1`] = `"[host]: value is [asdf$%^] but it must be a valid hostname (see RFC 1123)."`; +exports[`throws if invalid hostname 1`] = `"[host]: value must be a valid hostname (see RFC 1123)."`; exports[`with TLS throws if TLS is enabled but \`redirectHttpFromPort\` is equal to \`port\` 1`] = `"Kibana does not accept http traffic to [port] when ssl is enabled (only https is allowed), so [ssl.redirectHttpFromPort] cannot be configured to the same value. Both are [1234]."`; @@ -100,7 +100,7 @@ Array [ ] `; -exports[`with compression throws if invalid referrer whitelist 1`] = `"[compression.referrerWhitelist.0]: value is [asdf$%^] but it must be a valid hostname (see RFC 1123)."`; +exports[`with compression throws if invalid referrer whitelist 1`] = `"[compression.referrerWhitelist.0]: value must be a valid hostname (see RFC 1123)."`; exports[`with compression throws if invalid referrer whitelist 2`] = `"[compression.referrerWhitelist]: array size is [0], but cannot be smaller than [1]"`; diff --git a/src/core/server/http/http_server.mocks.ts b/src/core/server/http/http_server.mocks.ts index 741c723ca93652..bbef0a105c0896 100644 --- a/src/core/server/http/http_server.mocks.ts +++ b/src/core/server/http/http_server.mocks.ts @@ -36,6 +36,7 @@ import { OnPostAuthToolkit } from './lifecycle/on_post_auth'; import { OnPreAuthToolkit } from './lifecycle/on_pre_auth'; interface RequestFixtureOptions

{ + auth?: { isAuthenticated: boolean }; headers?: Record; params?: Record; body?: Record; @@ -65,11 +66,13 @@ function createKibanaRequestMock

({ routeAuthRequired, validation = {}, kibanaRouteState = { xsrfRequired: true }, + auth = { isAuthenticated: true }, }: RequestFixtureOptions = {}) { const queryString = stringify(query, { sort: false }); return KibanaRequest.from( createRawRequestMock({ + auth, headers, params, query, @@ -113,6 +116,9 @@ function createRawRequestMock(customization: DeepPartial = {}) { {}, { app: { xsrfRequired: true } as any, + auth: { + isAuthenticated: true, + }, headers: {}, path: '/', route: { settings: {} }, diff --git a/src/core/server/http/http_server.ts b/src/core/server/http/http_server.ts index cffdffab0d0cf7..f898ed0ea1a991 100644 --- a/src/core/server/http/http_server.ts +++ b/src/core/server/http/http_server.ts @@ -26,8 +26,7 @@ import { adoptToHapiAuthFormat, AuthenticationHandler } from './lifecycle/auth'; import { adoptToHapiOnPostAuthFormat, OnPostAuthHandler } from './lifecycle/on_post_auth'; import { adoptToHapiOnPreAuthFormat, OnPreAuthHandler } from './lifecycle/on_pre_auth'; import { adoptToHapiOnPreResponseFormat, OnPreResponseHandler } from './lifecycle/on_pre_response'; - -import { IRouter, KibanaRouteState, isSafeMethod } from './router'; +import { IRouter, RouteConfigOptions, KibanaRouteState, isSafeMethod } from './router'; import { SessionStorageCookieOptions, createCookieSessionStorageFactory, @@ -148,7 +147,7 @@ export class HttpServer { this.log.debug(`registering route handler for [${route.path}]`); // Hapi does not allow payload validation to be specified for 'head' or 'get' requests const validate = isSafeMethod(route.method) ? undefined : { payload: true }; - const { authRequired = true, tags, body = {} } = route.options; + const { authRequired, tags, body = {} } = route.options; const { accepts: allow, maxBytes, output, parse } = body; const kibanaRouteState: KibanaRouteState = { @@ -160,8 +159,7 @@ export class HttpServer { method: route.method, path: route.path, options: { - // Enforcing the comparison with true because plugins could overwrite the auth strategy by doing `options: { authRequired: authStrategy as any }` - auth: authRequired === true ? undefined : false, + auth: this.getAuthOption(authRequired), app: kibanaRouteState, tags: tags ? Array.from(tags) : undefined, // TODO: This 'validate' section can be removed once the legacy platform is completely removed. @@ -196,6 +194,22 @@ export class HttpServer { this.server = undefined; } + private getAuthOption( + authRequired: RouteConfigOptions['authRequired'] = true + ): undefined | false | { mode: 'required' | 'optional' } { + if (this.authRegistered === false) return undefined; + + if (authRequired === true) { + return { mode: 'required' }; + } + if (authRequired === 'optional') { + return { mode: 'optional' }; + } + if (authRequired === false) { + return false; + } + } + private setupBasePathRewrite(config: HttpConfig, basePathService: BasePath) { if (config.basePath === undefined || !config.rewriteBasePath) { return; diff --git a/src/core/server/http/http_service.mock.ts b/src/core/server/http/http_service.mock.ts index 30032ff5da7968..442bc93190d86d 100644 --- a/src/core/server/http/http_service.mock.ts +++ b/src/core/server/http/http_service.mock.ts @@ -115,6 +115,8 @@ const createOnPostAuthToolkitMock = (): jest.Mocked => ({ const createAuthToolkitMock = (): jest.Mocked => ({ authenticated: jest.fn(), + notHandled: jest.fn(), + redirected: jest.fn(), }); const createOnPreResponseToolkitMock = (): jest.Mocked => ({ diff --git a/src/core/server/http/index.ts b/src/core/server/http/index.ts index 8f4c02680f8a30..a75eb04fa01204 100644 --- a/src/core/server/http/index.ts +++ b/src/core/server/http/index.ts @@ -67,9 +67,12 @@ export { AuthenticationHandler, AuthHeaders, AuthResultParams, + AuthRedirected, + AuthRedirectedParams, AuthToolkit, AuthResult, Authenticated, + AuthNotHandled, AuthResultType, } from './lifecycle/auth'; export { OnPostAuthHandler, OnPostAuthToolkit } from './lifecycle/on_post_auth'; diff --git a/src/core/server/http/integration_tests/core_services.test.ts b/src/core/server/http/integration_tests/core_services.test.ts index 425d8cac1893ea..7b1630a7de0be0 100644 --- a/src/core/server/http/integration_tests/core_services.test.ts +++ b/src/core/server/http/integration_tests/core_services.test.ts @@ -50,7 +50,7 @@ describe('http service', () => { await root.shutdown(); }); describe('#isAuthenticated()', () => { - it('returns true if has been authorized', async () => { + it('returns true if has been authenticated', async () => { const { http } = await root.setup(); const { registerAuth, createRouter, auth } = http; @@ -65,11 +65,11 @@ describe('http service', () => { await kbnTestServer.request.get(root, '/is-auth').expect(200, { isAuthenticated: true }); }); - it('returns false if has not been authorized', async () => { + it('returns false if has not been authenticated', async () => { const { http } = await root.setup(); const { registerAuth, createRouter, auth } = http; - await registerAuth((req, res, toolkit) => toolkit.authenticated()); + registerAuth((req, res, toolkit) => toolkit.authenticated()); const router = createRouter(''); router.get( @@ -81,7 +81,7 @@ describe('http service', () => { await kbnTestServer.request.get(root, '/is-auth').expect(200, { isAuthenticated: false }); }); - it('returns false if no authorization mechanism has been registered', async () => { + it('returns false if no authentication mechanism has been registered', async () => { const { http } = await root.setup(); const { createRouter, auth } = http; @@ -94,6 +94,37 @@ describe('http service', () => { await root.start(); await kbnTestServer.request.get(root, '/is-auth').expect(200, { isAuthenticated: false }); }); + + it('returns true if authenticated on a route with "optional" auth', async () => { + const { http } = await root.setup(); + const { createRouter, auth, registerAuth } = http; + + registerAuth((req, res, toolkit) => toolkit.authenticated()); + const router = createRouter(''); + router.get( + { path: '/is-auth', validate: false, options: { authRequired: 'optional' } }, + (context, req, res) => res.ok({ body: { isAuthenticated: auth.isAuthenticated(req) } }) + ); + + await root.start(); + await kbnTestServer.request.get(root, '/is-auth').expect(200, { isAuthenticated: true }); + }); + + it('returns false if not authenticated on a route with "optional" auth', async () => { + const { http } = await root.setup(); + const { createRouter, auth, registerAuth } = http; + + registerAuth((req, res, toolkit) => toolkit.notHandled()); + + const router = createRouter(''); + router.get( + { path: '/is-auth', validate: false, options: { authRequired: 'optional' } }, + (context, req, res) => res.ok({ body: { isAuthenticated: auth.isAuthenticated(req) } }) + ); + + await root.start(); + await kbnTestServer.request.get(root, '/is-auth').expect(200, { isAuthenticated: false }); + }); }); describe('#get()', () => { it('returns authenticated status and allow associate auth state with request', async () => { diff --git a/src/core/server/http/integration_tests/lifecycle.test.ts b/src/core/server/http/integration_tests/lifecycle.test.ts index 6dc7ece1359df7..0f0d54e88daca3 100644 --- a/src/core/server/http/integration_tests/lifecycle.test.ts +++ b/src/core/server/http/integration_tests/lifecycle.test.ts @@ -57,7 +57,7 @@ interface StorageData { } describe('OnPreAuth', () => { - it('supports registering request inceptors', async () => { + it('supports registering a request interceptor', async () => { const { registerOnPreAuth, server: innerServer, createRouter } = await server.setup(setupDeps); const router = createRouter('/'); @@ -415,6 +415,23 @@ describe('Auth', () => { .expect(200, { content: 'ok' }); }); + it('blocks access to a resource if credentials are not provided', async () => { + const { registerAuth, server: innerServer, createRouter } = await server.setup(setupDeps); + const router = createRouter('/'); + + router.get({ path: '/', validate: false }, (context, req, res) => + res.ok({ body: { content: 'ok' } }) + ); + registerAuth((req, res, t) => t.notHandled()); + await server.start(); + + const result = await supertest(innerServer.listener) + .get('/') + .expect(401); + + expect(result.body.message).toBe('Unauthorized'); + }); + it('enables auth for a route by default if registerAuth has been called', async () => { const { registerAuth, server: innerServer, createRouter } = await server.setup(setupDeps); const router = createRouter('/'); @@ -492,11 +509,9 @@ describe('Auth', () => { router.get({ path: '/', validate: false }, (context, req, res) => res.ok()); const redirectTo = '/redirect-url'; - registerAuth((req, res) => - res.redirected({ - headers: { - location: redirectTo, - }, + registerAuth((req, res, t) => + t.redirected({ + location: redirectTo, }) ); await server.start(); @@ -507,6 +522,19 @@ describe('Auth', () => { expect(response.header.location).toBe(redirectTo); }); + it('throws if redirection url is not provided', async () => { + const { registerAuth, server: innerServer, createRouter } = await server.setup(setupDeps); + const router = createRouter('/'); + + router.get({ path: '/', validate: false }, (context, req, res) => res.ok()); + registerAuth((req, res, t) => t.redirected({} as any)); + await server.start(); + + await supertest(innerServer.listener) + .get('/') + .expect(500); + }); + it(`doesn't expose internal error details`, async () => { const { registerAuth, server: innerServer, createRouter } = await server.setup(setupDeps); const router = createRouter('/'); @@ -865,7 +893,7 @@ describe('Auth', () => { ] `); }); - // eslint-disable-next-line + it(`doesn't share request object between interceptors`, async () => { const { registerOnPostAuth, server: innerServer, createRouter } = await server.setup(setupDeps); const router = createRouter('/'); diff --git a/src/core/server/http/integration_tests/request.test.ts b/src/core/server/http/integration_tests/request.test.ts index bc1bbc881315ab..85270174fbc048 100644 --- a/src/core/server/http/integration_tests/request.test.ts +++ b/src/core/server/http/integration_tests/request.test.ts @@ -45,6 +45,89 @@ afterEach(async () => { const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)); describe('KibanaRequest', () => { + describe('auth', () => { + describe('isAuthenticated', () => { + it('returns false if no auth interceptor was registered', async () => { + const { server: innerServer, createRouter } = await server.setup(setupDeps); + const router = createRouter('/'); + router.get( + { path: '/', validate: false, options: { authRequired: true } }, + (context, req, res) => res.ok({ body: { isAuthenticated: req.auth.isAuthenticated } }) + ); + await server.start(); + + await supertest(innerServer.listener) + .get('/') + .expect(200, { + isAuthenticated: false, + }); + }); + it('returns false if not authenticated on a route with authRequired: "optional"', async () => { + const { server: innerServer, createRouter, registerAuth } = await server.setup(setupDeps); + const router = createRouter('/'); + registerAuth((req, res, toolkit) => toolkit.notHandled()); + router.get( + { path: '/', validate: false, options: { authRequired: 'optional' } }, + (context, req, res) => res.ok({ body: { isAuthenticated: req.auth.isAuthenticated } }) + ); + await server.start(); + + await supertest(innerServer.listener) + .get('/') + .expect(200, { + isAuthenticated: false, + }); + }); + it('returns false if redirected on a route with authRequired: "optional"', async () => { + const { server: innerServer, createRouter, registerAuth } = await server.setup(setupDeps); + const router = createRouter('/'); + registerAuth((req, res, toolkit) => toolkit.redirected({ location: '/any' })); + router.get( + { path: '/', validate: false, options: { authRequired: 'optional' } }, + (context, req, res) => res.ok({ body: { isAuthenticated: req.auth.isAuthenticated } }) + ); + await server.start(); + + await supertest(innerServer.listener) + .get('/') + .expect(200, { + isAuthenticated: false, + }); + }); + it('returns true if authenticated on a route with authRequired: "optional"', async () => { + const { server: innerServer, createRouter, registerAuth } = await server.setup(setupDeps); + const router = createRouter('/'); + registerAuth((req, res, toolkit) => toolkit.authenticated()); + router.get( + { path: '/', validate: false, options: { authRequired: 'optional' } }, + (context, req, res) => res.ok({ body: { isAuthenticated: req.auth.isAuthenticated } }) + ); + await server.start(); + + await supertest(innerServer.listener) + .get('/') + .expect(200, { + isAuthenticated: true, + }); + }); + it('returns true if authenticated', async () => { + const { server: innerServer, createRouter, registerAuth } = await server.setup(setupDeps); + const router = createRouter('/'); + registerAuth((req, res, toolkit) => toolkit.authenticated()); + router.get( + { path: '/', validate: false, options: { authRequired: true } }, + (context, req, res) => res.ok({ body: { isAuthenticated: req.auth.isAuthenticated } }) + ); + await server.start(); + + await supertest(innerServer.listener) + .get('/') + .expect(200, { + isAuthenticated: true, + }); + }); + }); + }); describe('events', () => { describe('aborted$', () => { it('emits once and completes when request aborted', async done => { diff --git a/src/core/server/http/integration_tests/router.test.ts b/src/core/server/http/integration_tests/router.test.ts index a1523781010d47..ee5b0c50acafb1 100644 --- a/src/core/server/http/integration_tests/router.test.ts +++ b/src/core/server/http/integration_tests/router.test.ts @@ -46,6 +46,286 @@ afterEach(async () => { await server.stop(); }); +describe('Options', () => { + describe('authRequired', () => { + describe('optional', () => { + it('User has access to a route if auth mechanism not registered', async () => { + const { server: innerServer, createRouter, auth } = await server.setup(setupDeps); + const router = createRouter('/'); + + router.get( + { path: '/', validate: false, options: { authRequired: 'optional' } }, + (context, req, res) => + res.ok({ + body: { + httpAuthIsAuthenticated: auth.isAuthenticated(req), + requestIsAuthenticated: req.auth.isAuthenticated, + }, + }) + ); + await server.start(); + + await supertest(innerServer.listener) + .get('/') + .expect(200, { + httpAuthIsAuthenticated: false, + requestIsAuthenticated: false, + }); + }); + + it('Authenticated user has access to a route', async () => { + const { server: innerServer, createRouter, registerAuth, auth } = await server.setup( + setupDeps + ); + const router = createRouter('/'); + + registerAuth((req, res, toolkit) => { + return toolkit.authenticated(); + }); + router.get( + { path: '/', validate: false, options: { authRequired: 'optional' } }, + (context, req, res) => + res.ok({ + body: { + httpAuthIsAuthenticated: auth.isAuthenticated(req), + requestIsAuthenticated: req.auth.isAuthenticated, + }, + }) + ); + await server.start(); + + await supertest(innerServer.listener) + .get('/') + .expect(200, { + httpAuthIsAuthenticated: true, + requestIsAuthenticated: true, + }); + }); + + it('User with no credentials can access a route', async () => { + const { server: innerServer, createRouter, registerAuth, auth } = await server.setup( + setupDeps + ); + const router = createRouter('/'); + + registerAuth((req, res, toolkit) => toolkit.notHandled()); + + router.get( + { path: '/', validate: false, options: { authRequired: 'optional' } }, + (context, req, res) => + res.ok({ + body: { + httpAuthIsAuthenticated: auth.isAuthenticated(req), + requestIsAuthenticated: req.auth.isAuthenticated, + }, + }) + ); + await server.start(); + + await supertest(innerServer.listener) + .get('/') + .expect(200, { + httpAuthIsAuthenticated: false, + requestIsAuthenticated: false, + }); + }); + + it('User with invalid credentials cannot access a route', async () => { + const { server: innerServer, createRouter, registerAuth } = await server.setup(setupDeps); + const router = createRouter('/'); + + registerAuth((req, res, toolkit) => res.unauthorized()); + + router.get( + { path: '/', validate: false, options: { authRequired: 'optional' } }, + (context, req, res) => res.ok({ body: 'ok' }) + ); + await server.start(); + + await supertest(innerServer.listener) + .get('/') + .expect(401); + }); + + it('does not redirect user and allows access to a resource', async () => { + const { server: innerServer, createRouter, registerAuth, auth } = await server.setup( + setupDeps + ); + const router = createRouter('/'); + + registerAuth((req, res, toolkit) => + toolkit.redirected({ + location: '/redirect-to', + }) + ); + + router.get( + { path: '/', validate: false, options: { authRequired: 'optional' } }, + (context, req, res) => + res.ok({ + body: { + httpAuthIsAuthenticated: auth.isAuthenticated(req), + requestIsAuthenticated: req.auth.isAuthenticated, + }, + }) + ); + await server.start(); + + await supertest(innerServer.listener) + .get('/') + .expect(200, { + httpAuthIsAuthenticated: false, + requestIsAuthenticated: false, + }); + }); + }); + + describe('true', () => { + it('User has access to a route if auth interceptor is not registered', async () => { + const { server: innerServer, createRouter, auth } = await server.setup(setupDeps); + const router = createRouter('/'); + + router.get( + { path: '/', validate: false, options: { authRequired: true } }, + (context, req, res) => + res.ok({ + body: { + httpAuthIsAuthenticated: auth.isAuthenticated(req), + requestIsAuthenticated: req.auth.isAuthenticated, + }, + }) + ); + await server.start(); + + await supertest(innerServer.listener) + .get('/') + .expect(200, { + httpAuthIsAuthenticated: false, + requestIsAuthenticated: false, + }); + }); + + it('Authenticated user has access to a route', async () => { + const { server: innerServer, createRouter, registerAuth, auth } = await server.setup( + setupDeps + ); + const router = createRouter('/'); + + registerAuth((req, res, toolkit) => { + return toolkit.authenticated(); + }); + router.get( + { path: '/', validate: false, options: { authRequired: true } }, + (context, req, res) => + res.ok({ + body: { + httpAuthIsAuthenticated: auth.isAuthenticated(req), + requestIsAuthenticated: req.auth.isAuthenticated, + }, + }) + ); + await server.start(); + + await supertest(innerServer.listener) + .get('/') + .expect(200, { + httpAuthIsAuthenticated: true, + requestIsAuthenticated: true, + }); + }); + + it('User with no credentials cannot access a route', async () => { + const { server: innerServer, createRouter, registerAuth } = await server.setup(setupDeps); + const router = createRouter('/'); + + registerAuth((req, res, toolkit) => toolkit.notHandled()); + router.get( + { path: '/', validate: false, options: { authRequired: true } }, + (context, req, res) => res.ok({ body: 'ok' }) + ); + await server.start(); + + await supertest(innerServer.listener) + .get('/') + .expect(401); + }); + + it('User with invalid credentials cannot access a route', async () => { + const { server: innerServer, createRouter, registerAuth } = await server.setup(setupDeps); + const router = createRouter('/'); + + registerAuth((req, res, toolkit) => res.unauthorized()); + + router.get( + { path: '/', validate: false, options: { authRequired: true } }, + (context, req, res) => res.ok({ body: 'ok' }) + ); + await server.start(); + + await supertest(innerServer.listener) + .get('/') + .expect(401); + }); + + it('allows redirecting an user', async () => { + const { server: innerServer, createRouter, registerAuth } = await server.setup(setupDeps); + const router = createRouter('/'); + const redirectUrl = '/redirect-to'; + + registerAuth((req, res, toolkit) => + toolkit.redirected({ + location: redirectUrl, + }) + ); + + router.get( + { path: '/', validate: false, options: { authRequired: true } }, + (context, req, res) => res.ok({ body: 'ok' }) + ); + await server.start(); + + const result = await supertest(innerServer.listener) + .get('/') + .expect(302); + + expect(result.header.location).toBe(redirectUrl); + }); + }); + + describe('false', () => { + it('does not try to authenticate a user', async () => { + const { server: innerServer, createRouter, registerAuth, auth } = await server.setup( + setupDeps + ); + const router = createRouter('/'); + + const authHook = jest.fn(); + registerAuth(authHook); + router.get( + { path: '/', validate: false, options: { authRequired: false } }, + (context, req, res) => + res.ok({ + body: { + httpAuthIsAuthenticated: auth.isAuthenticated(req), + requestIsAuthenticated: req.auth.isAuthenticated, + }, + }) + ); + await server.start(); + + await supertest(innerServer.listener) + .get('/') + .expect(200, { + httpAuthIsAuthenticated: false, + requestIsAuthenticated: false, + }); + + expect(authHook).toHaveBeenCalledTimes(0); + }); + }); + }); +}); + describe('Handler', () => { it("Doesn't expose error details if handler throws", async () => { const { server: innerServer, createRouter } = await server.setup(setupDeps); diff --git a/src/core/server/http/lifecycle/auth.ts b/src/core/server/http/lifecycle/auth.ts index 036ab0211c2ff5..2eaf7e0f6fbfed 100644 --- a/src/core/server/http/lifecycle/auth.ts +++ b/src/core/server/http/lifecycle/auth.ts @@ -25,11 +25,14 @@ import { lifecycleResponseFactory, LifecycleResponseFactory, isKibanaResponse, + ResponseHeaders, } from '../router'; /** @public */ export enum AuthResultType { authenticated = 'authenticated', + notHandled = 'notHandled', + redirected = 'redirected', } /** @public */ @@ -38,10 +41,20 @@ export interface Authenticated extends AuthResultParams { } /** @public */ -export type AuthResult = Authenticated; +export interface AuthNotHandled { + type: AuthResultType.notHandled; +} + +/** @public */ +export interface AuthRedirected extends AuthRedirectedParams { + type: AuthResultType.redirected; +} + +/** @public */ +export type AuthResult = Authenticated | AuthNotHandled | AuthRedirected; const authResult = { - authenticated(data: Partial = {}): AuthResult { + authenticated(data: AuthResultParams = {}): AuthResult { return { type: AuthResultType.authenticated, state: data.state, @@ -49,8 +62,25 @@ const authResult = { responseHeaders: data.responseHeaders, }; }, + notHandled(): AuthResult { + return { + type: AuthResultType.notHandled, + }; + }, + redirected(headers: { location: string } & ResponseHeaders): AuthResult { + return { + type: AuthResultType.redirected, + headers, + }; + }, isAuthenticated(result: AuthResult): result is Authenticated { - return result && result.type === AuthResultType.authenticated; + return result?.type === AuthResultType.authenticated; + }, + isNotHandled(result: AuthResult): result is AuthNotHandled { + return result?.type === AuthResultType.notHandled; + }, + isRedirected(result: AuthResult): result is AuthRedirected { + return result?.type === AuthResultType.redirected; }, }; @@ -62,7 +92,7 @@ const authResult = { export type AuthHeaders = Record; /** - * Result of an incoming request authentication. + * Result of successful authentication. * @public */ export interface AuthResultParams { @@ -82,6 +112,18 @@ export interface AuthResultParams { responseHeaders?: AuthHeaders; } +/** + * Result of auth redirection. + * @public + */ +export interface AuthRedirectedParams { + /** + * Headers to attach for auth redirect. + * Must include "location" header + */ + headers: { location: string } & ResponseHeaders; +} + /** * @public * A tool set defining an outcome of Auth interceptor for incoming request. @@ -89,10 +131,23 @@ export interface AuthResultParams { export interface AuthToolkit { /** Authentication is successful with given credentials, allow request to pass through */ authenticated: (data?: AuthResultParams) => AuthResult; + /** + * User has no credentials. + * Allows user to access a resource when authRequired: 'optional' + * Rejects a request when authRequired: true + * */ + notHandled: () => AuthResult; + /** + * Redirects user to another location to complete authentication when authRequired: true + * Allows user to access a resource without redirection when authRequired: 'optional' + * */ + redirected: (headers: { location: string } & ResponseHeaders) => AuthResult; } const toolkit: AuthToolkit = { authenticated: authResult.authenticated, + notHandled: authResult.notHandled, + redirected: authResult.redirected, }; /** @@ -109,30 +164,51 @@ export type AuthenticationHandler = ( export function adoptToHapiAuthFormat( fn: AuthenticationHandler, log: Logger, - onSuccess: (req: Request, data: AuthResultParams) => void = () => undefined + onAuth: (request: Request, data: AuthResultParams) => void = () => undefined ) { return async function interceptAuth( request: Request, responseToolkit: ResponseToolkit ): Promise { const hapiResponseAdapter = new HapiResponseAdapter(responseToolkit); + const kibanaRequest = KibanaRequest.from(request, undefined, false); + try { - const result = await fn( - KibanaRequest.from(request, undefined, false), - lifecycleResponseFactory, - toolkit - ); + const result = await fn(kibanaRequest, lifecycleResponseFactory, toolkit); + if (isKibanaResponse(result)) { return hapiResponseAdapter.handle(result); } + if (authResult.isAuthenticated(result)) { - onSuccess(request, { + onAuth(request, { state: result.state, requestHeaders: result.requestHeaders, responseHeaders: result.responseHeaders, }); return responseToolkit.authenticated({ credentials: result.state || {} }); } + + if (authResult.isRedirected(result)) { + // we cannot redirect a user when resources with optional auth requested + if (kibanaRequest.route.options.authRequired === 'optional') { + return responseToolkit.continue; + } + + return hapiResponseAdapter.handle( + lifecycleResponseFactory.redirected({ + // hapi doesn't accept string[] as a valid header + headers: result.headers as any, + }) + ); + } + + if (authResult.isNotHandled(result)) { + if (kibanaRequest.route.options.authRequired === 'optional') { + return responseToolkit.continue; + } + return hapiResponseAdapter.handle(lifecycleResponseFactory.unauthorized()); + } throw new Error( `Unexpected result from Authenticate. Expected AuthResult or KibanaResponse, but given: ${result}.` ); diff --git a/src/core/server/http/router/request.test.ts b/src/core/server/http/router/request.test.ts index 032027c2344858..fb999dc60e39c5 100644 --- a/src/core/server/http/router/request.test.ts +++ b/src/core/server/http/router/request.test.ts @@ -16,6 +16,7 @@ * specific language governing permissions and limitations * under the License. */ +import { RouteOptions } from 'hapi'; import { KibanaRequest } from './request'; import { httpServerMock } from '../http_server.mocks'; import { schema } from '@kbn/config-schema'; @@ -117,6 +118,106 @@ describe('KibanaRequest', () => { }); }); + describe('route.options.authRequired property', () => { + it('handles required auth: undefined', () => { + const auth: RouteOptions['auth'] = undefined; + const request = httpServerMock.createRawRequest({ + route: { + settings: { + auth, + }, + }, + }); + const kibanaRequest = KibanaRequest.from(request); + + expect(kibanaRequest.route.options.authRequired).toBe(true); + }); + it('handles required auth: false', () => { + const auth: RouteOptions['auth'] = false; + const request = httpServerMock.createRawRequest({ + route: { + settings: { + auth, + }, + }, + }); + const kibanaRequest = KibanaRequest.from(request); + + expect(kibanaRequest.route.options.authRequired).toBe(false); + }); + it('handles required auth: { mode: "required" }', () => { + const auth: RouteOptions['auth'] = { mode: 'required' }; + const request = httpServerMock.createRawRequest({ + route: { + settings: { + auth, + }, + }, + }); + const kibanaRequest = KibanaRequest.from(request); + + expect(kibanaRequest.route.options.authRequired).toBe(true); + }); + + it('handles required auth: { mode: "optional" }', () => { + const auth: RouteOptions['auth'] = { mode: 'optional' }; + const request = httpServerMock.createRawRequest({ + route: { + settings: { + auth, + }, + }, + }); + const kibanaRequest = KibanaRequest.from(request); + + expect(kibanaRequest.route.options.authRequired).toBe('optional'); + }); + + it('handles required auth: { mode: "try" } as "optional"', () => { + const auth: RouteOptions['auth'] = { mode: 'try' }; + const request = httpServerMock.createRawRequest({ + route: { + settings: { + auth, + }, + }, + }); + const kibanaRequest = KibanaRequest.from(request); + + expect(kibanaRequest.route.options.authRequired).toBe('optional'); + }); + + it('throws on auth: strategy name', () => { + const auth: RouteOptions['auth'] = 'session'; + const request = httpServerMock.createRawRequest({ + route: { + settings: { + auth, + }, + }, + }); + + expect(() => KibanaRequest.from(request)).toThrowErrorMatchingInlineSnapshot( + `"unexpected authentication options: \\"session\\" for route: /"` + ); + }); + + it('throws on auth: { mode: unexpected mode }', () => { + const auth: RouteOptions['auth'] = { mode: undefined }; + const request = httpServerMock.createRawRequest({ + route: { + settings: { + auth, + }, + }, + }); + + expect(() => KibanaRequest.from(request)).toThrowErrorMatchingInlineSnapshot( + `"unexpected authentication options: {} for route: /"` + ); + }); + }); + describe('RouteSchema type inferring', () => { it('should work with config-schema', () => { const body = Buffer.from('body!'); diff --git a/src/core/server/http/router/request.ts b/src/core/server/http/router/request.ts index bb2db6367f701f..f266677c1a1727 100644 --- a/src/core/server/http/router/request.ts +++ b/src/core/server/http/router/request.ts @@ -143,6 +143,10 @@ export class KibanaRequest< public readonly socket: IKibanaSocket; /** Request events {@link KibanaRequestEvents} */ public readonly events: KibanaRequestEvents; + public readonly auth: { + /* true if the request has been successfully authenticated, otherwise false. */ + isAuthenticated: boolean; + }; /** @internal */ protected readonly [requestSymbol]: Request; @@ -172,6 +176,11 @@ export class KibanaRequest< this.route = deepFreeze(this.getRouteInfo(request)); this.socket = new KibanaSocket(request.raw.req.socket); this.events = this.getEvents(request); + + this.auth = { + // missing in fakeRequests, so we cast to false + isAuthenticated: Boolean(request.auth?.isAuthenticated), + }; } private getEvents(request: Request): KibanaRequestEvents { @@ -189,7 +198,7 @@ export class KibanaRequest< const { parse, maxBytes, allow, output } = request.route.settings.payload || {}; const options = ({ - authRequired: request.route.settings.auth !== false, + authRequired: this.getAuthRequired(request), // some places in LP call KibanaRequest.from(request) manually. remove fallback to true before v8 xsrfRequired: (request.route.settings.app as KibanaRouteState)?.xsrfRequired ?? true, tags: request.route.settings.tags || [], @@ -209,6 +218,31 @@ export class KibanaRequest< options, }; } + + private getAuthRequired(request: Request): boolean | 'optional' { + const authOptions = request.route.settings.auth; + if (typeof authOptions === 'object') { + // 'try' is used in the legacy platform + if (authOptions.mode === 'optional' || authOptions.mode === 'try') { + return 'optional'; + } + if (authOptions.mode === 'required') { + return true; + } + } + + // legacy platform routes + if (authOptions === undefined) { + return true; + } + + if (authOptions === false) return false; + throw new Error( + `unexpected authentication options: ${JSON.stringify(authOptions)} for route: ${ + this.url.href + }` + ); + } } /** diff --git a/src/core/server/http/router/route.ts b/src/core/server/http/router/route.ts index ae74d03c72e128..9789d266587afc 100644 --- a/src/core/server/http/router/route.ts +++ b/src/core/server/http/router/route.ts @@ -116,13 +116,15 @@ export interface RouteConfigOptionsBody { */ export interface RouteConfigOptions { /** - * A flag shows that authentication for a route: - * `enabled` when true - * `disabled` when false + * Defines authentication mode for a route: + * - true. A user has to have valid credentials to access a resource + * - false. A user can access a resource without any credentials. + * - 'optional'. A user can access a resource if has valid credentials or no credentials at all. + * Can be useful when we grant access to a resource but want to identify a user if possible. * - * Enabled by default. + * Defaults to `true` if an auth mechanism is registered. */ - authRequired?: boolean; + authRequired?: boolean | 'optional'; /** * Defines xsrf protection requirements for a route: diff --git a/src/core/server/http/ssl_config.test.ts b/src/core/server/http/ssl_config.test.ts index 738f86f7a69eb8..3980b9c247fa33 100644 --- a/src/core/server/http/ssl_config.test.ts +++ b/src/core/server/http/ssl_config.test.ts @@ -293,16 +293,16 @@ describe('#sslSchema', () => { expect(() => sslSchema.validate(singleUnknownProtocol)).toThrowErrorMatchingInlineSnapshot(` "[supportedProtocols.0]: types that failed validation: -- [supportedProtocols.0.0]: expected value to equal [TLSv1] but got [SOMEv100500] -- [supportedProtocols.0.1]: expected value to equal [TLSv1.1] but got [SOMEv100500] -- [supportedProtocols.0.2]: expected value to equal [TLSv1.2] but got [SOMEv100500]" +- [supportedProtocols.0.0]: expected value to equal [TLSv1] +- [supportedProtocols.0.1]: expected value to equal [TLSv1.1] +- [supportedProtocols.0.2]: expected value to equal [TLSv1.2]" `); expect(() => sslSchema.validate(allKnownWithOneUnknownProtocols)) .toThrowErrorMatchingInlineSnapshot(` "[supportedProtocols.3]: types that failed validation: -- [supportedProtocols.3.0]: expected value to equal [TLSv1] but got [SOMEv100500] -- [supportedProtocols.3.1]: expected value to equal [TLSv1.1] but got [SOMEv100500] -- [supportedProtocols.3.2]: expected value to equal [TLSv1.2] but got [SOMEv100500]" +- [supportedProtocols.3.0]: expected value to equal [TLSv1] +- [supportedProtocols.3.1]: expected value to equal [TLSv1.1] +- [supportedProtocols.3.2]: expected value to equal [TLSv1.2]" `); }); }); diff --git a/src/core/server/index.ts b/src/core/server/index.ts index 8e481171116faf..80eabe778ece33 100644 --- a/src/core/server/index.ts +++ b/src/core/server/index.ts @@ -100,9 +100,12 @@ export { AuthResultParams, AuthStatus, AuthToolkit, + AuthRedirected, + AuthRedirectedParams, AuthResult, AuthResultType, Authenticated, + AuthNotHandled, BasePath, IBasePath, CustomHttpResponseOptions, diff --git a/src/legacy/core_plugins/visualizations/public/np_ready/public/legacy/calculate_object_hash.d.ts b/src/core/server/metrics/collectors/mocks.ts similarity index 72% rename from src/legacy/core_plugins/visualizations/public/np_ready/public/legacy/calculate_object_hash.d.ts rename to src/core/server/metrics/collectors/mocks.ts index d2d11c14a3e5fb..d1eb15637779af 100644 --- a/src/legacy/core_plugins/visualizations/public/np_ready/public/legacy/calculate_object_hash.d.ts +++ b/src/core/server/metrics/collectors/mocks.ts @@ -17,4 +17,19 @@ * under the License. */ -export function calculateObjectHash(obj: object): string; +import { MetricsCollector } from './types'; + +const createMock = () => { + const mocked: jest.Mocked> = { + collect: jest.fn(), + reset: jest.fn(), + }; + + mocked.collect.mockResolvedValue({}); + + return mocked; +}; + +export const collectorMock = { + create: createMock, +}; diff --git a/src/core/server/metrics/collectors/os.ts b/src/core/server/metrics/collectors/os.ts index d3d9bb0be86fa2..59bef9d8ddd2b1 100644 --- a/src/core/server/metrics/collectors/os.ts +++ b/src/core/server/metrics/collectors/os.ts @@ -57,4 +57,6 @@ export class OsMetricsCollector implements MetricsCollector { return metrics; } + + public reset() {} } diff --git a/src/core/server/metrics/collectors/process.ts b/src/core/server/metrics/collectors/process.ts index aa68abaf74e41a..a3b59a7cc8b7c2 100644 --- a/src/core/server/metrics/collectors/process.ts +++ b/src/core/server/metrics/collectors/process.ts @@ -40,6 +40,8 @@ export class ProcessMetricsCollector implements MetricsCollector => { diff --git a/src/core/server/metrics/collectors/server.ts b/src/core/server/metrics/collectors/server.ts index e46ac2f653df66..84204d0466ff34 100644 --- a/src/core/server/metrics/collectors/server.ts +++ b/src/core/server/metrics/collectors/server.ts @@ -26,12 +26,12 @@ interface ServerResponseTime { } export class ServerMetricsCollector implements MetricsCollector { - private readonly requests: OpsServerMetrics['requests'] = { + private requests: OpsServerMetrics['requests'] = { disconnects: 0, total: 0, statusCodes: {}, }; - private readonly responseTimes: ServerResponseTime = { + private responseTimes: ServerResponseTime = { count: 0, total: 0, max: 0, @@ -77,4 +77,17 @@ export class ServerMetricsCollector implements MetricsCollector { + /** collect the data currently gathered by the collector */ collect(): Promise; + /** reset the internal state of the collector */ + reset(): void; } /** diff --git a/src/core/server/metrics/integration_tests/server_collector.test.ts b/src/core/server/metrics/integration_tests/server_collector.test.ts index 6baf95894b9b42..dd5c256cf1600d 100644 --- a/src/core/server/metrics/integration_tests/server_collector.test.ts +++ b/src/core/server/metrics/integration_tests/server_collector.test.ts @@ -200,4 +200,80 @@ describe('ServerMetricsCollector', () => { metrics = await collector.collect(); expect(metrics.concurrent_connections).toEqual(0); }); + + describe('#reset', () => { + it('reset the requests state', async () => { + router.get({ path: '/', validate: false }, async (ctx, req, res) => { + return res.ok({ body: '' }); + }); + await server.start(); + + await sendGet('/'); + await sendGet('/'); + await sendGet('/not-found'); + + let metrics = await collector.collect(); + + expect(metrics.requests).toEqual({ + total: 3, + disconnects: 0, + statusCodes: { + '200': 2, + '404': 1, + }, + }); + + collector.reset(); + metrics = await collector.collect(); + + expect(metrics.requests).toEqual({ + total: 0, + disconnects: 0, + statusCodes: {}, + }); + + await sendGet('/'); + await sendGet('/not-found'); + + metrics = await collector.collect(); + + expect(metrics.requests).toEqual({ + total: 2, + disconnects: 0, + statusCodes: { + '200': 1, + '404': 1, + }, + }); + }); + + it('resets the response times', async () => { + router.get({ path: '/no-delay', validate: false }, async (ctx, req, res) => { + return res.ok({ body: '' }); + }); + router.get({ path: '/500-ms', validate: false }, async (ctx, req, res) => { + await delay(500); + return res.ok({ body: '' }); + }); + + await server.start(); + + await Promise.all([sendGet('/no-delay'), sendGet('/500-ms')]); + let metrics = await collector.collect(); + + expect(metrics.response_times.avg_in_millis).toBeGreaterThanOrEqual(250); + expect(metrics.response_times.max_in_millis).toBeGreaterThanOrEqual(500); + + collector.reset(); + metrics = await collector.collect(); + expect(metrics.response_times.avg_in_millis).toBe(0); + expect(metrics.response_times.max_in_millis).toBeGreaterThanOrEqual(0); + + await Promise.all([sendGet('/500-ms'), sendGet('/500-ms')]); + metrics = await collector.collect(); + + expect(metrics.response_times.avg_in_millis).toBeGreaterThanOrEqual(500); + expect(metrics.response_times.max_in_millis).toBeGreaterThanOrEqual(500); + }); + }); }); diff --git a/src/core/server/metrics/metrics_service.test.mocks.ts b/src/core/server/metrics/metrics_service.test.mocks.ts index 8e91775283042b..fe46e5693bf458 100644 --- a/src/core/server/metrics/metrics_service.test.mocks.ts +++ b/src/core/server/metrics/metrics_service.test.mocks.ts @@ -17,9 +17,10 @@ * under the License. */ -export const mockOpsCollector = { - collect: jest.fn(), -}; +import { collectorMock } from './collectors/mocks'; + +export const mockOpsCollector = collectorMock.create(); + jest.doMock('./ops_metrics_collector', () => ({ OpsMetricsCollector: jest.fn().mockImplementation(() => mockOpsCollector), })); diff --git a/src/core/server/metrics/metrics_service.test.ts b/src/core/server/metrics/metrics_service.test.ts index 10d6761adbe7d5..f6334cc5d3c0f7 100644 --- a/src/core/server/metrics/metrics_service.test.ts +++ b/src/core/server/metrics/metrics_service.test.ts @@ -57,37 +57,50 @@ describe('MetricsService', () => { expect(setInterval).toHaveBeenCalledWith(expect.any(Function), testInterval); }); - it('emits the metrics at start', async () => { + it('collects the metrics at every interval', async () => { mockOpsCollector.collect.mockResolvedValue(dummyMetrics); - const { getOpsMetrics$ } = await metricsService.setup({ - http: httpMock, - }); - + await metricsService.setup({ http: httpMock }); await metricsService.start(); expect(mockOpsCollector.collect).toHaveBeenCalledTimes(1); - expect( - await getOpsMetrics$() - .pipe(take(1)) - .toPromise() - ).toEqual(dummyMetrics); + + jest.advanceTimersByTime(testInterval); + expect(mockOpsCollector.collect).toHaveBeenCalledTimes(2); + + jest.advanceTimersByTime(testInterval); + expect(mockOpsCollector.collect).toHaveBeenCalledTimes(3); }); - it('collects the metrics at every interval', async () => { + it('resets the collector after each collection', async () => { mockOpsCollector.collect.mockResolvedValue(dummyMetrics); - await metricsService.setup({ http: httpMock }); - + const { getOpsMetrics$ } = await metricsService.setup({ http: httpMock }); await metricsService.start(); + // `advanceTimersByTime` only ensure the interval handler is executed + // however the `reset` call is executed after the async call to `collect` + // meaning that we are going to miss the call if we don't wait for the + // actual observable emission that is performed after + const waitForNextEmission = () => + getOpsMetrics$() + .pipe(take(1)) + .toPromise(); + expect(mockOpsCollector.collect).toHaveBeenCalledTimes(1); + expect(mockOpsCollector.reset).toHaveBeenCalledTimes(1); + let nextEmission = waitForNextEmission(); jest.advanceTimersByTime(testInterval); + await nextEmission; expect(mockOpsCollector.collect).toHaveBeenCalledTimes(2); + expect(mockOpsCollector.reset).toHaveBeenCalledTimes(2); + nextEmission = waitForNextEmission(); jest.advanceTimersByTime(testInterval); + await nextEmission; expect(mockOpsCollector.collect).toHaveBeenCalledTimes(3); + expect(mockOpsCollector.reset).toHaveBeenCalledTimes(3); }); it('throws when called before setup', async () => { diff --git a/src/core/server/metrics/metrics_service.ts b/src/core/server/metrics/metrics_service.ts index 1aed89a4aad600..0ea9d007926003 100644 --- a/src/core/server/metrics/metrics_service.ts +++ b/src/core/server/metrics/metrics_service.ts @@ -17,8 +17,8 @@ * under the License. */ -import { ReplaySubject } from 'rxjs'; -import { first, shareReplay } from 'rxjs/operators'; +import { Subject } from 'rxjs'; +import { first } from 'rxjs/operators'; import { CoreService } from '../../types'; import { CoreContext } from '../core_context'; import { Logger } from '../logging'; @@ -37,7 +37,7 @@ export class MetricsService private readonly logger: Logger; private metricsCollector?: OpsMetricsCollector; private collectInterval?: NodeJS.Timeout; - private metrics$ = new ReplaySubject(1); + private metrics$ = new Subject(); constructor(private readonly coreContext: CoreContext) { this.logger = coreContext.logger.get('metrics'); @@ -46,7 +46,7 @@ export class MetricsService public async setup({ http }: MetricsServiceSetupDeps): Promise { this.metricsCollector = new OpsMetricsCollector(http.server); - const metricsObservable = this.metrics$.pipe(shareReplay(1)); + const metricsObservable = this.metrics$.asObservable(); return { getOpsMetrics$: () => metricsObservable, @@ -74,6 +74,7 @@ export class MetricsService private async refreshMetrics() { this.logger.debug('Refreshing metrics'); const metrics = await this.metricsCollector!.collect(); + this.metricsCollector!.reset(); this.metrics$.next(metrics); } diff --git a/src/core/server/metrics/ops_metrics_collector.test.mocks.ts b/src/core/server/metrics/ops_metrics_collector.test.mocks.ts index 8265796d57970e..cf51f8a753729f 100644 --- a/src/core/server/metrics/ops_metrics_collector.test.mocks.ts +++ b/src/core/server/metrics/ops_metrics_collector.test.mocks.ts @@ -17,23 +17,19 @@ * under the License. */ -export const mockOsCollector = { - collect: jest.fn(), -}; +import { collectorMock } from './collectors/mocks'; + +export const mockOsCollector = collectorMock.create(); jest.doMock('./collectors/os', () => ({ OsMetricsCollector: jest.fn().mockImplementation(() => mockOsCollector), })); -export const mockProcessCollector = { - collect: jest.fn(), -}; +export const mockProcessCollector = collectorMock.create(); jest.doMock('./collectors/process', () => ({ ProcessMetricsCollector: jest.fn().mockImplementation(() => mockProcessCollector), })); -export const mockServerCollector = { - collect: jest.fn(), -}; +export const mockServerCollector = collectorMock.create(); jest.doMock('./collectors/server', () => ({ ServerMetricsCollector: jest.fn().mockImplementation(() => mockServerCollector), })); diff --git a/src/core/server/metrics/ops_metrics_collector.test.ts b/src/core/server/metrics/ops_metrics_collector.test.ts index 04302a195fb6cd..559588db60a425 100644 --- a/src/core/server/metrics/ops_metrics_collector.test.ts +++ b/src/core/server/metrics/ops_metrics_collector.test.ts @@ -35,25 +35,43 @@ describe('OpsMetricsCollector', () => { mockOsCollector.collect.mockResolvedValue('osMetrics'); }); - it('gathers metrics from the underlying collectors', async () => { - mockOsCollector.collect.mockResolvedValue('osMetrics'); - mockProcessCollector.collect.mockResolvedValue('processMetrics'); - mockServerCollector.collect.mockResolvedValue({ - requests: 'serverRequestsMetrics', - response_times: 'serverTimingMetrics', + describe('#collect', () => { + it('gathers metrics from the underlying collectors', async () => { + mockOsCollector.collect.mockResolvedValue('osMetrics'); + mockProcessCollector.collect.mockResolvedValue('processMetrics'); + mockServerCollector.collect.mockResolvedValue({ + requests: 'serverRequestsMetrics', + response_times: 'serverTimingMetrics', + }); + + const metrics = await collector.collect(); + + expect(mockOsCollector.collect).toHaveBeenCalledTimes(1); + expect(mockProcessCollector.collect).toHaveBeenCalledTimes(1); + expect(mockServerCollector.collect).toHaveBeenCalledTimes(1); + + expect(metrics).toEqual({ + process: 'processMetrics', + os: 'osMetrics', + requests: 'serverRequestsMetrics', + response_times: 'serverTimingMetrics', + }); }); + }); + + describe('#reset', () => { + it('call reset on the underlying collectors', () => { + collector.reset(); - const metrics = await collector.collect(); + expect(mockOsCollector.reset).toHaveBeenCalledTimes(1); + expect(mockProcessCollector.reset).toHaveBeenCalledTimes(1); + expect(mockServerCollector.reset).toHaveBeenCalledTimes(1); - expect(mockOsCollector.collect).toHaveBeenCalledTimes(1); - expect(mockProcessCollector.collect).toHaveBeenCalledTimes(1); - expect(mockServerCollector.collect).toHaveBeenCalledTimes(1); + collector.reset(); - expect(metrics).toEqual({ - process: 'processMetrics', - os: 'osMetrics', - requests: 'serverRequestsMetrics', - response_times: 'serverTimingMetrics', + expect(mockOsCollector.reset).toHaveBeenCalledTimes(2); + expect(mockProcessCollector.reset).toHaveBeenCalledTimes(2); + expect(mockServerCollector.reset).toHaveBeenCalledTimes(2); }); }); }); diff --git a/src/core/server/metrics/ops_metrics_collector.ts b/src/core/server/metrics/ops_metrics_collector.ts index 04344f21f57f72..525515dba14577 100644 --- a/src/core/server/metrics/ops_metrics_collector.ts +++ b/src/core/server/metrics/ops_metrics_collector.ts @@ -49,4 +49,10 @@ export class OpsMetricsCollector implements MetricsCollector { ...server, }; } + + public reset() { + this.processCollector.reset(); + this.osCollector.reset(); + this.serverCollector.reset(); + } } diff --git a/src/core/server/server.api.md b/src/core/server/server.api.md index 30695df33345ad..f7afe7a6a290aa 100644 --- a/src/core/server/server.api.md +++ b/src/core/server/server.api.md @@ -419,7 +419,26 @@ export type AuthenticationHandler = (request: KibanaRequest, response: Lifecycle export type AuthHeaders = Record; // @public (undocumented) -export type AuthResult = Authenticated; +export interface AuthNotHandled { + // (undocumented) + type: AuthResultType.notHandled; +} + +// @public (undocumented) +export interface AuthRedirected extends AuthRedirectedParams { + // (undocumented) + type: AuthResultType.redirected; +} + +// @public +export interface AuthRedirectedParams { + headers: { + location: string; + } & ResponseHeaders; +} + +// @public (undocumented) +export type AuthResult = Authenticated | AuthNotHandled | AuthRedirected; // @public export interface AuthResultParams { @@ -431,7 +450,11 @@ export interface AuthResultParams { // @public (undocumented) export enum AuthResultType { // (undocumented) - authenticated = "authenticated" + authenticated = "authenticated", + // (undocumented) + notHandled = "notHandled", + // (undocumented) + redirected = "redirected" } // @public @@ -444,6 +467,10 @@ export enum AuthStatus { // @public export interface AuthToolkit { authenticated: (data?: AuthResultParams) => AuthResult; + notHandled: () => AuthResult; + redirected: (headers: { + location: string; + } & ResponseHeaders) => AuthResult; } // @public @@ -970,6 +997,10 @@ export class KibanaRequest { // @public export interface RouteConfigOptions { - authRequired?: boolean; + authRequired?: boolean | 'optional'; body?: Method extends 'get' | 'options' ? undefined : RouteConfigOptionsBody; tags?: readonly string[]; xsrfRequired?: Method extends 'get' ? never : boolean; diff --git a/src/dev/storybook/aliases.ts b/src/dev/storybook/aliases.ts index 35ac4e27f9c8b3..8ed64f004c9be3 100644 --- a/src/dev/storybook/aliases.ts +++ b/src/dev/storybook/aliases.ts @@ -25,4 +25,5 @@ export const storybookAliases = { embeddable: 'src/plugins/embeddable/scripts/storybook.js', infra: 'x-pack/legacy/plugins/infra/scripts/storybook.js', siem: 'x-pack/legacy/plugins/siem/scripts/storybook.js', + ui_actions: 'x-pack/plugins/advanced_ui_actions/scripts/storybook.js', }; diff --git a/src/legacy/core_plugins/data/public/search/expressions/esaggs.ts b/src/legacy/core_plugins/data/public/search/expressions/esaggs.ts index 24dd1c4944bfba..bb954cb887ef3f 100644 --- a/src/legacy/core_plugins/data/public/search/expressions/esaggs.ts +++ b/src/legacy/core_plugins/data/public/search/expressions/esaggs.ts @@ -38,7 +38,7 @@ import { } from '../../../../../../plugins/data/public'; import { buildTabularInspectorData } from './build_tabular_inspector_data'; -import { calculateObjectHash } from '../../../../visualizations/public'; +import { calculateObjectHash } from '../../../../../../plugins/kibana_utils/common'; import { tabifyAggResponse } from '../../../../../core_plugins/data/public'; import { PersistedState } from '../../../../../../plugins/visualizations/public'; import { Adapters } from '../../../../../../plugins/inspector/public'; diff --git a/src/legacy/core_plugins/input_control_vis/public/input_control_vis_type.ts b/src/legacy/core_plugins/input_control_vis/public/input_control_vis_type.ts index 1bdff06b3a59f9..dae6c9abb625ef 100644 --- a/src/legacy/core_plugins/input_control_vis/public/input_control_vis_type.ts +++ b/src/legacy/core_plugins/input_control_vis/public/input_control_vis_type.ts @@ -22,8 +22,9 @@ import { i18n } from '@kbn/i18n'; import { createInputControlVisController } from './vis_controller'; import { getControlsTab } from './components/editor/controls_tab'; import { OptionsTab } from './components/editor/options_tab'; -import { Status, defaultFeedbackMessage } from '../../visualizations/public'; +import { Status } from '../../visualizations/public'; import { InputControlVisDependencies } from './plugin'; +import { defaultFeedbackMessage } from '../../../../plugins/kibana_utils/common'; export function createInputControlVisTypeDefinition(deps: InputControlVisDependencies) { const InputControlVisController = createInputControlVisController(deps); diff --git a/src/legacy/core_plugins/input_control_vis/public/plugin.ts b/src/legacy/core_plugins/input_control_vis/public/plugin.ts index e9ffad8b35f213..e85ccd94f9e6a7 100644 --- a/src/legacy/core_plugins/input_control_vis/public/plugin.ts +++ b/src/legacy/core_plugins/input_control_vis/public/plugin.ts @@ -59,7 +59,7 @@ export class InputControlVisPlugin implements Plugin, void> { }; expressions.registerFunction(createInputControlVisFn); - visualizations.types.createBaseVisualization( + visualizations.createBaseVisualization( createInputControlVisTypeDefinition(visualizationDependencies) ); } diff --git a/src/legacy/core_plugins/kibana/public/discover/kibana_services.ts b/src/legacy/core_plugins/kibana/public/discover/kibana_services.ts index 91b5c7f13dc954..7fa5183a4f54bb 100644 --- a/src/legacy/core_plugins/kibana/public/discover/kibana_services.ts +++ b/src/legacy/core_plugins/kibana/public/discover/kibana_services.ts @@ -50,7 +50,6 @@ export function setServices(newServices: any) { // EXPORT legacy static dependencies, should be migrated when available in a new version; export { angular }; export { wrapInI18nContext } from 'ui/i18n'; -export { buildVislibDimensions } from '../../../visualizations/public'; export { getRequestInspectorStats, getResponseInspectorStats } from '../../../data/public'; // @ts-ignore export { intervalOptions } from 'ui/agg_types'; diff --git a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/discover.js b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/discover.js index fb4158a6e3e038..81c10798936f56 100644 --- a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/discover.js +++ b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/discover.js @@ -45,7 +45,6 @@ import { getPainlessError } from './get_painless_error'; import { discoverResponseHandler } from './response_handler'; import { angular, - buildVislibDimensions, getRequestInspectorStats, getResponseInspectorStats, getServices, @@ -76,6 +75,7 @@ const { import { getRootBreadcrumbs, getSavedSearchBreadcrumbs } from '../helpers/breadcrumbs'; import { esFilters, + fieldFormats, indexPatterns as indexPatternsUtils, } from '../../../../../../../plugins/data/public'; import { getIndexPatternId } from '../helpers/get_index_pattern_id'; @@ -812,21 +812,45 @@ function discoverController( $fetchObservable.next(); }; + function getDimensions(aggs, timeRange) { + const [metric, agg] = aggs; + agg.params.timeRange = timeRange; + const bounds = agg.params.timeRange ? timefilter.calculateBounds(agg.params.timeRange) : null; + agg.buckets.setBounds(bounds); + + const { esUnit, esValue } = agg.buckets.getInterval(); + return { + x: { + accessor: 0, + label: agg.makeLabel(), + format: fieldFormats.serialize(agg), + params: { + date: true, + interval: moment.duration(esValue, esUnit), + intervalESValue: esValue, + intervalESUnit: esUnit, + format: agg.buckets.getScaledDateFormat(), + bounds: agg.buckets.getBounds(), + }, + }, + y: { + accessor: 1, + format: fieldFormats.serialize(metric), + label: metric.makeLabel(), + }, + }; + } + function onResults(resp) { logInspectorResponse(resp); if ($scope.opts.timefield) { const tabifiedData = tabifyAggResponse($scope.vis.aggs, resp); $scope.searchSource.rawResponse = resp; - Promise.resolve( - buildVislibDimensions($scope.vis, { - timefilter, - timeRange: $scope.timeRange, - searchSource: $scope.searchSource, - }) - ).then(resp => { - $scope.histogramData = discoverResponseHandler(tabifiedData, resp); - }); + $scope.histogramData = discoverResponseHandler( + tabifiedData, + getDimensions($scope.vis.aggs.aggs, $scope.timeRange) + ); } $scope.hits = resp.hits.total; @@ -993,7 +1017,7 @@ function discoverController( }, }; - $scope.vis = new visualizations.Vis( + $scope.vis = visualizations.createVis( $scope.searchSource.getField('index'), visSavedObject.visState ); diff --git a/src/legacy/core_plugins/kibana/public/discover/np_ready/components/field_chooser/lib/visualize_url_utils.ts b/src/legacy/core_plugins/kibana/public/discover/np_ready/components/field_chooser/lib/visualize_url_utils.ts index 8dbf3cd79ccb16..7ea1863693e0d6 100644 --- a/src/legacy/core_plugins/kibana/public/discover/np_ready/components/field_chooser/lib/visualize_url_utils.ts +++ b/src/legacy/core_plugins/kibana/public/discover/np_ready/components/field_chooser/lib/visualize_url_utils.ts @@ -29,7 +29,7 @@ import { getServices } from '../../../../kibana_services'; function getMapsAppBaseUrl() { const mapsAppVisAlias = getServices() - .visualizations.types.getAliases() + .visualizations.getAliases() .find(({ name }) => { return name === 'maps'; }); @@ -38,7 +38,7 @@ function getMapsAppBaseUrl() { export function isMapsAppRegistered() { return getServices() - .visualizations.types.getAliases() + .visualizations.getAliases() .some(({ name }) => { return name === 'maps'; }); diff --git a/src/legacy/core_plugins/kibana/public/management/saved_object_registry.ts b/src/legacy/core_plugins/kibana/public/management/saved_object_registry.ts index 604575a6e6220e..8e73a09480c41b 100644 --- a/src/legacy/core_plugins/kibana/public/management/saved_object_registry.ts +++ b/src/legacy/core_plugins/kibana/public/management/saved_object_registry.ts @@ -22,7 +22,7 @@ import { i18n } from '@kbn/i18n'; import { npStart } from 'ui/new_platform'; import { SavedObjectLoader } from '../../../../../plugins/saved_objects/public'; import { createSavedDashboardLoader } from '../dashboard'; -import { TypesService, createSavedVisLoader } from '../../../visualizations/public'; +import { start as visualizations } from '../../../visualizations/public/np_ready/public/legacy'; import { createSavedSearchesLoader } from '../../../../../plugins/discover/public'; /** @@ -58,10 +58,7 @@ const services = { savedObjectManagementRegistry.register({ id: 'savedVisualizations', - service: createSavedVisLoader({ - ...services, - ...{ visualizationTypes: new TypesService().start() }, - }), + service: visualizations.savedVisualizationsLoader, title: 'visualizations', }); diff --git a/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/create_index_pattern_wizard/lib/get_indices.test.ts b/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/create_index_pattern_wizard/lib/get_indices.test.ts index cd7c8278adcc7c..5a8460fcb51bae 100644 --- a/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/create_index_pattern_wizard/lib/get_indices.test.ts +++ b/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/create_index_pattern_wizard/lib/get_indices.test.ts @@ -19,7 +19,8 @@ import { getIndices } from './get_indices'; import { IndexPatternCreationConfig } from './../../../../../../../management/public'; -import { LegacyApiCaller } from '../../../../../../../../../plugins/data/public'; +// eslint-disable-next-line @kbn/eslint/no-restricted-paths +import { LegacyApiCaller } from '../../../../../../../../../plugins/data/public/search'; export const successfulResponse = { hits: { diff --git a/src/legacy/core_plugins/kibana/public/visualize/legacy_imports.ts b/src/legacy/core_plugins/kibana/public/visualize/legacy_imports.ts index b8ee7cd378750c..66a7bd6f333737 100644 --- a/src/legacy/core_plugins/kibana/public/visualize/legacy_imports.ts +++ b/src/legacy/core_plugins/kibana/public/visualize/legacy_imports.ts @@ -24,17 +24,8 @@ * directly where they are needed. */ -export { State } from 'ui/state_management/state'; -// @ts-ignore -export { GlobalStateProvider } from 'ui/state_management/global_state'; -// @ts-ignore -export { StateManagementConfigProvider } from 'ui/state_management/config_provider'; - export { subscribeWithScope } from 'ui/utils/subscribe_with_scope'; // @ts-ignore -export { EventsProvider } from 'ui/events'; -export { registerTimefilterWithGlobalStateFactory } from 'ui/timefilter/setup_router'; -// @ts-ignore export { KbnUrlProvider, RedirectWhenMissingProvider } from 'ui/url'; export { absoluteToParsedUrl } from 'ui/url/absolute_to_parsed_url'; export { KibanaParsedUrl } from 'ui/url/kibana_parsed_url'; diff --git a/src/legacy/core_plugins/kibana/public/visualize/np_ready/application.ts b/src/legacy/core_plugins/kibana/public/visualize/np_ready/application.ts index b15d89275eba79..8ef63ec5778e2d 100644 --- a/src/legacy/core_plugins/kibana/public/visualize/np_ready/application.ts +++ b/src/legacy/core_plugins/kibana/public/visualize/np_ready/application.ts @@ -23,13 +23,11 @@ import { i18nDirective, i18nFilter, I18nProvider } from '@kbn/i18n/angular'; import { AppMountContext } from 'kibana/public'; import { configureAppAngularModule, - GlobalStateProvider, KbnUrlProvider, RedirectWhenMissingProvider, IPrivate, PrivateProvider, PromiseServiceCreator, - StateManagementConfigProvider, } from '../legacy_imports'; import { NavigationPublicPluginStart as NavigationStart } from '../../../../../../plugins/navigation/public'; import { @@ -87,35 +85,20 @@ function createLocalAngularModule(core: AppMountContext['core'], navigation: Nav createLocalI18nModule(); createLocalPrivateModule(); createLocalPromiseModule(); - createLocalConfigModule(core); createLocalKbnUrlModule(); - createLocalStateModule(); createLocalTopNavModule(navigation); const visualizeAngularModule: IModule = angular.module(moduleName, [ ...thirdPartyAngularDependencies, - 'app/visualize/Config', 'app/visualize/I18n', 'app/visualize/Private', 'app/visualize/TopNav', - 'app/visualize/State', + 'app/visualize/KbnUrl', + 'app/visualize/Promise', ]); return visualizeAngularModule; } -function createLocalStateModule() { - angular - .module('app/visualize/State', [ - 'app/visualize/Private', - 'app/visualize/Config', - 'app/visualize/KbnUrl', - 'app/visualize/Promise', - ]) - .service('globalState', function(Private: IPrivate) { - return Private(GlobalStateProvider); - }); -} - function createLocalKbnUrlModule() { angular .module('app/visualize/KbnUrl', ['app/visualize/Private', 'ngRoute']) @@ -123,19 +106,6 @@ function createLocalKbnUrlModule() { .service('redirectWhenMissing', (Private: IPrivate) => Private(RedirectWhenMissingProvider)); } -function createLocalConfigModule(core: AppMountContext['core']) { - angular - .module('app/visualize/Config', ['app/visualize/Private']) - .provider('stateManagementConfig', StateManagementConfigProvider) - .provider('config', () => { - return { - $get: () => ({ - get: core.uiSettings.get.bind(core.uiSettings), - }), - }; - }); -} - function createLocalPromiseModule() { angular.module('app/visualize/Promise', []).service('Promise', PromiseServiceCreator); } diff --git a/src/legacy/core_plugins/kibana/public/visualize/np_ready/editor/editor.html b/src/legacy/core_plugins/kibana/public/visualize/np_ready/editor/editor.html index 9dbb05ea95b487..28baf21925cbea 100644 --- a/src/legacy/core_plugins/kibana/public/visualize/np_ready/editor/editor.html +++ b/src/legacy/core_plugins/kibana/public/visualize/np_ready/editor/editor.html @@ -31,8 +31,8 @@ refresh-interval="refreshInterval.value" on-refresh-change="onRefreshChange" show-save-query="showSaveQuery" - on-saved="onQuerySaved" - on-saved-query-updated="onSavedQueryUpdated" + on-saved="updateSavedQuery" + on-saved-query-updated="updateSavedQuery" on-clear-saved-query="onClearSavedQuery" > diff --git a/src/legacy/core_plugins/kibana/public/visualize/np_ready/editor/editor.js b/src/legacy/core_plugins/kibana/public/visualize/np_ready/editor/editor.js index 2d2552b5e2f30c..e1a20e33813310 100644 --- a/src/legacy/core_plugins/kibana/public/visualize/np_ready/editor/editor.js +++ b/src/legacy/core_plugins/kibana/public/visualize/np_ready/editor/editor.js @@ -20,6 +20,7 @@ import angular from 'angular'; import _ from 'lodash'; import { Subscription } from 'rxjs'; +import { map } from 'rxjs/operators'; import { i18n } from '@kbn/i18n'; import React from 'react'; @@ -29,13 +30,17 @@ import { VisualizeConstants } from '../visualize_constants'; import { getEditBreadcrumbs } from '../breadcrumbs'; import { addHelpMenuToAppChrome } from '../help_menu/help_menu_util'; -import { FilterStateManager } from '../../../../../data/public'; import { unhashUrl } from '../../../../../../../plugins/kibana_utils/public'; import { kbnBaseUrl } from '../../../../../../../plugins/kibana_legacy/public'; import { SavedObjectSaveModal, showSaveModal, } from '../../../../../../../plugins/saved_objects/public'; +import { + esFilters, + connectToQueryState, + syncQueryStateWithUrl, +} from '../../../../../../../plugins/data/public'; import { initVisEditorDirective } from './visualization_editor'; import { initVisualizationDirective } from './visualization'; @@ -65,28 +70,21 @@ export function initEditorDirective(app, deps) { function VisualizeAppController( $scope, - $element, $route, $window, $injector, $timeout, kbnUrl, redirectWhenMissing, - Promise, - globalState, - config + kbnUrlStateStorage, + history ) { const { indexPatterns, localStorage, visualizeCapabilities, share, - data: { - query: { - filterManager, - timefilter: { timefilter }, - }, - }, + data: { query: queryService }, toastNotifications, chrome, getBasePath, @@ -97,6 +95,17 @@ function VisualizeAppController( setActiveUrl, } = getServices(); + const { + filterManager, + timefilter: { timefilter }, + } = queryService; + + // starts syncing `_g` portion of url with query services + const { stop: stopSyncingQueryServiceStateWithUrl } = syncQueryStateWithUrl( + queryService, + kbnUrlStateStorage + ); + // Retrieve the resolved SavedVis instance. const savedVis = $route.current.locals.savedVis; const _applyVis = () => { @@ -284,26 +293,24 @@ function VisualizeAppController( linked: !!savedVis.savedSearchId, }; - const useHash = config.get('state:storeInSessionStorage'); const { stateContainer, stopStateSync } = useVisualizeAppState({ - useHash, stateDefaults, + kbnUrlStateStorage, }); - const filterStateManager = new FilterStateManager( - globalState, - () => { - // Temporary AppState replacement - return { - set filters(_filters) { - stateContainer.transitions.set('filters', _filters); - }, - get filters() { - return stateContainer.getState().filters; - }, - }; + // sync initial app filters from state to filterManager + filterManager.setAppFilters(_.cloneDeep(stateContainer.getState().filters)); + // setup syncing of app filters between appState and filterManager + const stopSyncingAppFilters = connectToQueryState( + queryService, + { + set: ({ filters }) => stateContainer.transitions.set('filters', filters), + get: () => ({ filters: stateContainer.getState().filters }), + state$: stateContainer.state$.pipe(map(state => ({ filters: state.filters }))), }, - filterManager + { + filters: esFilters.FilterStateStore.APP_STATE, + } ); // The savedVis is pulled from elasticsearch, but the appState is pulled from the url, with the @@ -335,6 +342,24 @@ function VisualizeAppController( } ); + const updateSavedQueryFromUrl = savedQueryId => { + if (!savedQueryId) { + delete $scope.savedQuery; + + return; + } + + if ($scope.savedQuery && $scope.savedQuery.id === savedQueryId) { + return; + } + + savedQueryService.getSavedQuery(savedQueryId).then(savedQuery => { + $scope.$evalAsync(() => { + $scope.updateSavedQuery(savedQuery); + }); + }); + }; + function init() { if (vis.indexPattern) { $scope.indexPattern = vis.indexPattern; @@ -388,7 +413,6 @@ function VisualizeAppController( }; $scope.timeRange = timefilter.getTime(); - $scope.opts = _.pick($scope, 'savedVis', 'isAddToDashMode'); const unsubscribeStateUpdates = stateContainer.subscribe(state => { const newQuery = migrateLegacyQuery(state.query); @@ -396,6 +420,7 @@ function VisualizeAppController( stateContainer.transitions.set('query', newQuery); } persistOnChange(state); + updateSavedQueryFromUrl(state.savedQuery); // if the browser history was changed manually we need to reflect changes in the editor if (!_.isEqual(vis.getState(), state.vis)) { @@ -413,6 +438,9 @@ function VisualizeAppController( $scope.$broadcast('render'); }; + // update the query if savedQuery is stored + updateSavedQueryFromUrl(initialState.savedQuery); + const subscriptions = new Subscription(); subscriptions.add( @@ -438,7 +466,7 @@ function VisualizeAppController( // update the searchSource when query updates $scope.fetch = function() { - const { query, filters, linked } = stateContainer.getState(); + const { query, linked, filters } = stateContainer.getState(); $scope.query = query; $scope.linked = linked; savedVis.searchSource.setField('query', query); @@ -451,7 +479,6 @@ function VisualizeAppController( subscribeWithScope($scope, filterManager.getUpdates$(), { next: () => { $scope.filters = filterManager.getFilters(); - $scope.globalFilters = filterManager.getGlobalFilters(); }, }) ); @@ -466,13 +493,14 @@ function VisualizeAppController( $scope._handler.destroy(); } savedVis.destroy(); - filterStateManager.destroy(); subscriptions.unsubscribe(); $scope.vis.off('apply', _applyVis); unsubscribePersisted(); unsubscribeStateUpdates(); stopStateSync(); + stopSyncingQueryServiceStateWithUrl(); + stopSyncingAppFilters(); }); $timeout(() => { @@ -501,23 +529,14 @@ function VisualizeAppController( }); }; - $scope.onQuerySaved = savedQuery => { - $scope.savedQuery = savedQuery; - }; - - $scope.onSavedQueryUpdated = savedQuery => { - $scope.savedQuery = { ...savedQuery }; - }; - $scope.onClearSavedQuery = () => { delete $scope.savedQuery; stateContainer.transitions.removeSavedQuery(defaultQuery); filterManager.setFilters(filterManager.getGlobalFilters()); - $scope.fetch(); }; const updateStateFromSavedQuery = savedQuery => { - stateContainer.transitions.set('query', savedQuery.attributes.query); + stateContainer.transitions.updateFromSavedQuery(savedQuery); const savedQueryFilters = savedQuery.attributes.filters || []; const globalFilters = filterManager.getGlobalFilters(); @@ -532,25 +551,12 @@ function VisualizeAppController( timefilter.setRefreshInterval(savedQuery.attributes.timefilter.refreshInterval); } } - - $scope.fetch(); }; - // update the query if savedQuery is stored - if (stateContainer.getState().savedQuery) { - savedQueryService.getSavedQuery(stateContainer.getState().savedQuery).then(savedQuery => { - $scope.$evalAsync(() => { - $scope.savedQuery = savedQuery; - }); - }); - } - - $scope.$watch('savedQuery', newSavedQuery => { - if (!newSavedQuery) return; - stateContainer.transitions.set('savedQuery', newSavedQuery.id); - - updateStateFromSavedQuery(newSavedQuery); - }); + $scope.updateSavedQuery = savedQuery => { + $scope.savedQuery = savedQuery; + updateStateFromSavedQuery(savedQuery); + }; $scope.$watch('linked', linked => { if (linked && !savedVis.savedSearchId) { @@ -626,7 +632,10 @@ function VisualizeAppController( savedVis.vis.title = savedVis.title; savedVis.vis.description = savedVis.description; } else { - kbnUrl.change(`${VisualizeConstants.EDIT_PATH}/{{id}}`, { id: savedVis.id }); + history.replace({ + ...history.location, + pathname: `${VisualizeConstants.EDIT_PATH}/${savedVis.id}`, + }); } } }); diff --git a/src/legacy/core_plugins/kibana/public/visualize/np_ready/editor/lib/visualize_app_state.ts b/src/legacy/core_plugins/kibana/public/visualize/np_ready/editor/lib/visualize_app_state.ts index d8de81193d857f..d3fae3d457b632 100644 --- a/src/legacy/core_plugins/kibana/public/visualize/np_ready/editor/lib/visualize_app_state.ts +++ b/src/legacy/core_plugins/kibana/public/visualize/np_ready/editor/lib/visualize_app_state.ts @@ -17,21 +17,20 @@ * under the License. */ -import { createHashHistory } from 'history'; import { isFunction, omit } from 'lodash'; import { migrateAppState } from './migrate_app_state'; import { - createKbnUrlStateStorage, createStateContainer, syncState, + IKbnUrlStateStorage, } from '../../../../../../../../plugins/kibana_utils/public'; import { PureVisState, VisualizeAppState, VisualizeAppStateTransitions } from '../../types'; const STATE_STORAGE_KEY = '_a'; interface Arguments { - useHash: boolean; + kbnUrlStateStorage: IKbnUrlStateStorage; stateDefaults: VisualizeAppState; } @@ -41,12 +40,7 @@ function toObject(state: PureVisState): PureVisState { }); } -export function useVisualizeAppState({ useHash, stateDefaults }: Arguments) { - const history = createHashHistory(); - const kbnUrlStateStorage = createKbnUrlStateStorage({ - useHash, - history, - }); +export function useVisualizeAppState({ stateDefaults, kbnUrlStateStorage }: Arguments) { const urlState = kbnUrlStateStorage.get(STATE_STORAGE_KEY); const initialState = migrateAppState({ ...stateDefaults, @@ -88,6 +82,11 @@ export function useVisualizeAppState({ useHash, stateDefaults }: Arguments) { linked: false, }), updateVisState: state => newVisState => ({ ...state, vis: toObject(newVisState) }), + updateFromSavedQuery: state => savedQuery => ({ + ...state, + savedQuery: savedQuery.id, + query: savedQuery.attributes.query, + }), } ); diff --git a/src/legacy/core_plugins/kibana/public/visualize/np_ready/global_state_sync.ts b/src/legacy/core_plugins/kibana/public/visualize/np_ready/global_state_sync.ts deleted file mode 100644 index f29fb72a9fbc5a..00000000000000 --- a/src/legacy/core_plugins/kibana/public/visualize/np_ready/global_state_sync.ts +++ /dev/null @@ -1,67 +0,0 @@ -/* - * 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. - */ - -import { State } from '../legacy_imports'; -import { DataPublicPluginStart as DataStart } from '../../../../../../plugins/data/public'; - -/** - * Helper function to sync the global state with the various state providers - * when a local angular application mounts. There are three different ways - * global state can be passed into the application: - * * parameter in the URL hash - e.g. shared link - * * in-memory state in the data plugin exports (timefilter and filterManager) - e.g. default values - * - * This function looks up the three sources (earlier in the list means it takes precedence), - * puts it into the globalState object and syncs it with the url. - * - * Currently the legacy chrome takes care of restoring the global state when navigating from - * one app to another - to migrate away from that it will become necessary to also write the current - * state to local storage - */ -export function syncOnMount( - globalState: State, - { - query: { - filterManager, - timefilter: { timefilter }, - }, - }: DataStart -) { - // pull in global state information from the URL - globalState.fetch(); - // remember whether there were info in the URL - const hasGlobalURLState = Boolean(Object.keys(globalState.toObject()).length); - - // sync kibana platform state with the angular global state - if (!globalState.time) { - globalState.time = timefilter.getTime(); - } - if (!globalState.refreshInterval) { - globalState.refreshInterval = timefilter.getRefreshInterval(); - } - if (!globalState.filters && filterManager.getGlobalFilters().length > 0) { - globalState.filters = filterManager.getGlobalFilters(); - } - // only inject cross app global state if there is none in the url itself (that takes precedence) - if (hasGlobalURLState) { - // set flag the global state is set from the URL - globalState.$inheritedGlobalState = true; - } - globalState.save(); -} diff --git a/src/legacy/core_plugins/kibana/public/visualize/np_ready/legacy_app.js b/src/legacy/core_plugins/kibana/public/visualize/np_ready/legacy_app.js index 24055b9a2d9ed9..b9409445166bc4 100644 --- a/src/legacy/core_plugins/kibana/public/visualize/np_ready/legacy_app.js +++ b/src/legacy/core_plugins/kibana/public/visualize/np_ready/legacy_app.js @@ -19,6 +19,9 @@ import { find } from 'lodash'; import { i18n } from '@kbn/i18n'; +import { createHashHistory } from 'history'; + +import { createKbnUrlStateStorage } from '../../../../../../plugins/kibana_utils/public'; import editorTemplate from './editor/editor.html'; import visualizeListingTemplate from './listing/visualize_listing.html'; @@ -26,11 +29,7 @@ import visualizeListingTemplate from './listing/visualize_listing.html'; import { initVisualizeAppDirective } from './visualize_app'; import { VisualizeConstants } from './visualize_constants'; import { VisualizeListingController } from './listing/visualize_listing'; -import { - ensureDefaultIndexPattern, - registerTimefilterWithGlobalStateFactory, -} from '../legacy_imports'; -import { syncOnMount } from './global_state_sync'; +import { ensureDefaultIndexPattern } from '../legacy_imports'; import { getLandingBreadcrumbs, @@ -42,17 +41,13 @@ import { export function initVisualizeApp(app, deps) { initVisualizeAppDirective(app, deps); - app.run(globalState => { - syncOnMount(globalState, deps.data); - }); - - app.run((globalState, $rootScope) => { - registerTimefilterWithGlobalStateFactory( - deps.data.query.timefilter.timefilter, - globalState, - $rootScope - ); - }); + app.factory('history', () => createHashHistory()); + app.factory('kbnUrlStateStorage', history => + createKbnUrlStateStorage({ + history, + useHash: deps.uiSettings.get('state:storeInSessionStorage'), + }) + ); app.config(function($routeProvider) { const defaults = { @@ -107,7 +102,7 @@ export function initVisualizeApp(app, deps) { resolve: { savedVis: function(redirectWhenMissing, $route, $rootScope, kbnUrl) { const { core, data, savedVisualizations, visualizations } = deps; - const visTypes = visualizations.types.all(); + const visTypes = visualizations.all(); const visType = find(visTypes, { name: $route.current.params.type }); const shouldHaveIndex = visType.requiresSearch && visType.options.showIndexSelection; const hasIndex = diff --git a/src/legacy/core_plugins/kibana/public/visualize/np_ready/listing/visualize_listing.js b/src/legacy/core_plugins/kibana/public/visualize/np_ready/listing/visualize_listing.js index c0cc499b598f02..5a479a491395ae 100644 --- a/src/legacy/core_plugins/kibana/public/visualize/np_ready/listing/visualize_listing.js +++ b/src/legacy/core_plugins/kibana/public/visualize/np_ready/listing/visualize_listing.js @@ -26,23 +26,21 @@ import { i18n } from '@kbn/i18n'; import { getServices } from '../../kibana_services'; import { wrapInI18nContext } from '../../legacy_imports'; +import { syncQueryStateWithUrl } from '../../../../../../../plugins/data/public'; + export function initListingDirective(app) { app.directive('visualizeListingTable', reactDirective => reactDirective(wrapInI18nContext(VisualizeListingTable)) ); } -export function VisualizeListingController($injector, $scope, createNewVis) { +export function VisualizeListingController($injector, $scope, createNewVis, kbnUrlStateStorage) { const { addBasePath, chrome, savedObjectsClient, savedVisualizations, - data: { - query: { - timefilter: { timefilter }, - }, - }, + data: { query }, toastNotifications, uiSettings, visualizations, @@ -50,6 +48,16 @@ export function VisualizeListingController($injector, $scope, createNewVis) { } = getServices(); const kbnUrl = $injector.get('kbnUrl'); + // syncs `_g` portion of url with query services + const { stop: stopSyncingQueryServiceStateWithUrl } = syncQueryStateWithUrl( + query, + kbnUrlStateStorage + ); + + const { + timefilter: { timefilter }, + } = query; + timefilter.disableAutoRefreshSelector(); timefilter.disableTimeRangeSelector(); @@ -124,5 +132,7 @@ export function VisualizeListingController($injector, $scope, createNewVis) { if (this.closeNewVisModal) { this.closeNewVisModal(); } + + stopSyncingQueryServiceStateWithUrl(); }); } diff --git a/src/legacy/core_plugins/kibana/public/visualize/np_ready/types.d.ts b/src/legacy/core_plugins/kibana/public/visualize/np_ready/types.d.ts index 8ca603eb114590..55fccd75361a04 100644 --- a/src/legacy/core_plugins/kibana/public/visualize/np_ready/types.d.ts +++ b/src/legacy/core_plugins/kibana/public/visualize/np_ready/types.d.ts @@ -17,7 +17,13 @@ * under the License. */ -import { TimeRange, Query, Filter, DataPublicPluginStart } from 'src/plugins/data/public'; +import { + TimeRange, + Query, + Filter, + DataPublicPluginStart, + SavedQuery, +} from 'src/plugins/data/public'; import { IEmbeddableStart } from 'src/plugins/embeddable/public'; import { PersistedState } from 'src/plugins/visualizations/public'; import { LegacyCoreStart } from 'kibana/public'; @@ -48,6 +54,7 @@ export interface VisualizeAppStateTransitions { state: VisualizeAppState ) => (query: Query, filters: Filter[]) => VisualizeAppState; updateVisState: (state: VisualizeAppState) => (vis: PureVisState) => VisualizeAppState; + updateFromSavedQuery: (state: VisualizeAppState) => (savedQuery: SavedQuery) => VisualizeAppState; } export interface EditorRenderProps { diff --git a/src/legacy/core_plugins/region_map/public/__tests__/region_map_visualization.js b/src/legacy/core_plugins/region_map/public/__tests__/region_map_visualization.js index f11aab9b9db882..6bdb5d00e67d8f 100644 --- a/src/legacy/core_plugins/region_map/public/__tests__/region_map_visualization.js +++ b/src/legacy/core_plugins/region_map/public/__tests__/region_map_visualization.js @@ -111,9 +111,7 @@ describe('RegionMapsVisualizationTests', function() { if (!visRegComplete) { visRegComplete = true; - visualizationsSetup.types.createBaseVisualization( - createRegionMapTypeDefinition(dependencies) - ); + visualizationsSetup.createBaseVisualization(createRegionMapTypeDefinition(dependencies)); } RegionMapsVisualization = createRegionMapVisualization(dependencies); @@ -160,7 +158,7 @@ describe('RegionMapsVisualizationTests', function() { imageComparator = new ImageComparator(); - vis = new visualizationsStart.Vis(indexPattern, { + vis = visualizationsStart.createVis(indexPattern, { type: 'region_map', }); diff --git a/src/legacy/core_plugins/region_map/public/plugin.ts b/src/legacy/core_plugins/region_map/public/plugin.ts index aaf0a8a308aeae..98fb5604c3d658 100644 --- a/src/legacy/core_plugins/region_map/public/plugin.ts +++ b/src/legacy/core_plugins/region_map/public/plugin.ts @@ -70,7 +70,7 @@ export class RegionMapPlugin implements Plugin, void> { expressions.registerFunction(createRegionMapFn); - visualizations.types.createBaseVisualization( + visualizations.createBaseVisualization( createRegionMapTypeDefinition(visualizationDependencies) ); } diff --git a/src/legacy/core_plugins/tile_map/public/__tests__/coordinate_maps_visualization.js b/src/legacy/core_plugins/tile_map/public/__tests__/coordinate_maps_visualization.js index 27e9459c7e06c1..6a08405b5b6a55 100644 --- a/src/legacy/core_plugins/tile_map/public/__tests__/coordinate_maps_visualization.js +++ b/src/legacy/core_plugins/tile_map/public/__tests__/coordinate_maps_visualization.js @@ -88,9 +88,7 @@ describe('CoordinateMapsVisualizationTest', function() { if (!visRegComplete) { visRegComplete = true; - visualizationsSetup.types.createBaseVisualization( - createTileMapTypeDefinition(dependencies) - ); + visualizationsSetup.createBaseVisualization(createTileMapTypeDefinition(dependencies)); } CoordinateMapsVisualization = createTileMapVisualization(dependencies); @@ -126,7 +124,7 @@ describe('CoordinateMapsVisualizationTest', function() { setupDOM('512px', '512px'); imageComparator = new ImageComparator(); - vis = new visualizationsStart.Vis(indexPattern, { + vis = visualizationsStart.createVis(indexPattern, { type: 'tile_map', }); vis.params = { diff --git a/src/legacy/core_plugins/tile_map/public/plugin.ts b/src/legacy/core_plugins/tile_map/public/plugin.ts index 52acaf51b39b1a..a12c2753cc5251 100644 --- a/src/legacy/core_plugins/tile_map/public/plugin.ts +++ b/src/legacy/core_plugins/tile_map/public/plugin.ts @@ -64,9 +64,7 @@ export class TileMapPlugin implements Plugin, void> { expressions.registerFunction(() => createTileMapFn(visualizationDependencies)); - visualizations.types.createBaseVisualization( - createTileMapTypeDefinition(visualizationDependencies) - ); + visualizations.createBaseVisualization(createTileMapTypeDefinition(visualizationDependencies)); } public start(core: CoreStart) { diff --git a/src/legacy/core_plugins/timelion/public/app.js b/src/legacy/core_plugins/timelion/public/app.js index e4a48c09db8327..a9d678cfea79cf 100644 --- a/src/legacy/core_plugins/timelion/public/app.js +++ b/src/legacy/core_plugins/timelion/public/app.js @@ -42,7 +42,7 @@ import '../../data/public/legacy'; import './services/saved_sheet_register'; import rootTemplate from 'plugins/timelion/index.html'; -import { createSavedVisLoader, TypesService } from '../../visualizations/public'; +import { start as visualizations } from '../../visualizations/public/np_ready/public/legacy'; import { loadKbnTopNavDirectives } from '../../../../plugins/kibana_legacy/public'; loadKbnTopNavDirectives(npStart.plugins.navigation.ui); @@ -127,13 +127,7 @@ app.controller('timelion', function( timefilter.enableAutoRefreshSelector(); timefilter.enableTimeRangeSelector(); - const savedVisualizations = createSavedVisLoader({ - savedObjectsClient: npStart.core.savedObjects.client, - indexPatterns: npStart.plugins.data.indexPatterns, - chrome: npStart.core.chrome, - overlays: npStart.core.overlays, - visualizationTypes: new TypesService().start(), - }); + const savedVisualizations = visualizations.savedVisualizationsLoader; const timezone = Private(timezoneProvider)(); const defaultExpression = '.es(*)'; diff --git a/src/legacy/core_plugins/vis_type_markdown/public/plugin.ts b/src/legacy/core_plugins/vis_type_markdown/public/plugin.ts index f1316647562020..71d6c1c69ef2d9 100644 --- a/src/legacy/core_plugins/vis_type_markdown/public/plugin.ts +++ b/src/legacy/core_plugins/vis_type_markdown/public/plugin.ts @@ -39,7 +39,7 @@ export class MarkdownPlugin implements Plugin { } public setup(core: CoreSetup, { expressions, visualizations }: MarkdownPluginSetupDependencies) { - visualizations.types.createReactVisualization(markdownVisDefinition); + visualizations.createReactVisualization(markdownVisDefinition); expressions.registerFunction(createMarkdownVisFn); } diff --git a/src/legacy/core_plugins/vis_type_metric/public/metric_vis_type.test.ts b/src/legacy/core_plugins/vis_type_metric/public/metric_vis_type.test.ts index 67b5d018f46388..5dbd59f3f1709f 100644 --- a/src/legacy/core_plugins/vis_type_metric/public/metric_vis_type.test.ts +++ b/src/legacy/core_plugins/vis_type_metric/public/metric_vis_type.test.ts @@ -40,7 +40,7 @@ describe('metric_vis - createMetricVisTypeDefinition', () => { let vis: Vis; beforeAll(() => { - visualizationsSetup.types.createReactVisualization(createMetricVisTypeDefinition()); + visualizationsSetup.createReactVisualization(createMetricVisTypeDefinition()); (npStart.plugins.data.fieldFormats.getType as jest.Mock).mockImplementation(() => { return fieldFormats.UrlFormat; }); @@ -59,7 +59,7 @@ describe('metric_vis - createMetricVisTypeDefinition', () => { // TODO: remove when Vis is converted to typescript. Only importing Vis as type // @ts-ignore - vis = new visualizationsStart.Vis(stubIndexPattern, { + vis = visualizationsStart.createVis(stubIndexPattern, { type: 'metric', aggs: [{ id: '1', type: 'top_hits', schema: 'metric', params: { field: 'ip' } }], }); @@ -80,7 +80,7 @@ describe('metric_vis - createMetricVisTypeDefinition', () => { }; const el = document.createElement('div'); - const metricVisType = visualizationsStart.types.get('metric'); + const metricVisType = visualizationsStart.get('metric'); const Controller = metricVisType.visualization; const controller = new Controller(el, vis); const render = (esResponse: any) => { diff --git a/src/legacy/core_plugins/vis_type_metric/public/plugin.ts b/src/legacy/core_plugins/vis_type_metric/public/plugin.ts index 082fab47e573c6..28b435cbc79807 100644 --- a/src/legacy/core_plugins/vis_type_metric/public/plugin.ts +++ b/src/legacy/core_plugins/vis_type_metric/public/plugin.ts @@ -45,7 +45,7 @@ export class MetricVisPlugin implements Plugin { { expressions, visualizations, charts }: MetricVisPluginSetupDependencies ) { expressions.registerFunction(createMetricVisFn); - visualizations.types.createReactVisualization(createMetricVisTypeDefinition()); + visualizations.createReactVisualization(createMetricVisTypeDefinition()); } public start(core: CoreStart) { diff --git a/src/legacy/core_plugins/vis_type_table/public/agg_table/__tests__/agg_table.js b/src/legacy/core_plugins/vis_type_table/public/agg_table/__tests__/agg_table.js index 9fe7920588cd24..91581923b05cb2 100644 --- a/src/legacy/core_plugins/vis_type_table/public/agg_table/__tests__/agg_table.js +++ b/src/legacy/core_plugins/vis_type_table/public/agg_table/__tests__/agg_table.js @@ -47,10 +47,10 @@ describe('Table Vis - AggTable Directive', function() { const tabifiedData = {}; const init = () => { - const vis1 = new visualizationsStart.Vis(indexPattern, 'table'); + const vis1 = visualizationsStart.createVis(indexPattern, 'table'); tabifiedData.metricOnly = tabifyAggResponse(vis1.aggs, metricOnly); - const vis2 = new visualizationsStart.Vis(indexPattern, { + const vis2 = visualizationsStart.createVis(indexPattern, { type: 'table', params: { showMetricsAtAllLevels: true, @@ -69,7 +69,7 @@ describe('Table Vis - AggTable Directive', function() { metricsAtAllLevels: true, }); - const vis3 = new visualizationsStart.Vis(indexPattern, { + const vis3 = visualizationsStart.createVis(indexPattern, { type: 'table', aggs: [ { type: 'avg', schema: 'metric', params: { field: 'bytes' } }, @@ -110,7 +110,7 @@ describe('Table Vis - AggTable Directive', function() { beforeEach(initLocalAngular); ngMock.inject(function() { - visualizationsSetup.types.createBaseVisualization(tableVisTypeDefinition); + visualizationsSetup.createBaseVisualization(tableVisTypeDefinition); }); beforeEach(ngMock.module('kibana/table_vis')); diff --git a/src/legacy/core_plugins/vis_type_table/public/agg_table/__tests__/agg_table_group.js b/src/legacy/core_plugins/vis_type_table/public/agg_table/__tests__/agg_table_group.js index 79d4d7c40d3559..4d62551dcf3961 100644 --- a/src/legacy/core_plugins/vis_type_table/public/agg_table/__tests__/agg_table_group.js +++ b/src/legacy/core_plugins/vis_type_table/public/agg_table/__tests__/agg_table_group.js @@ -35,10 +35,10 @@ describe('Table Vis - AggTableGroup Directive', function() { const tabifiedData = {}; const init = () => { - const vis1 = new visualizationsStart.Vis(indexPattern, 'table'); + const vis1 = visualizationsStart.createVis(indexPattern, 'table'); tabifiedData.metricOnly = tabifyAggResponse(vis1.aggs, metricOnly); - const vis2 = new visualizationsStart.Vis(indexPattern, { + const vis2 = visualizationsStart.createVis(indexPattern, { type: 'pie', aggs: [ { type: 'avg', schema: 'metric', params: { field: 'bytes' } }, diff --git a/src/legacy/core_plugins/vis_type_table/public/plugin.ts b/src/legacy/core_plugins/vis_type_table/public/plugin.ts index 17c50b0567b67d..519a56da23ac9b 100644 --- a/src/legacy/core_plugins/vis_type_table/public/plugin.ts +++ b/src/legacy/core_plugins/vis_type_table/public/plugin.ts @@ -44,7 +44,7 @@ export class TableVisPlugin implements Plugin, void> { ) { expressions.registerFunction(createTableVisFn); - visualizations.types.createBaseVisualization(tableVisTypeDefinition); + visualizations.createBaseVisualization(tableVisTypeDefinition); } public start(core: CoreStart) { diff --git a/src/legacy/core_plugins/vis_type_tagcloud/public/components/__tests__/tag_cloud_visualization.js b/src/legacy/core_plugins/vis_type_tagcloud/public/components/__tests__/tag_cloud_visualization.js index 55ecf98f994d24..3091b3340cd6d2 100644 --- a/src/legacy/core_plugins/vis_type_tagcloud/public/components/__tests__/tag_cloud_visualization.js +++ b/src/legacy/core_plugins/vis_type_tagcloud/public/components/__tests__/tag_cloud_visualization.js @@ -76,7 +76,7 @@ describe('TagCloudVisualizationTest', function() { beforeEach(async function() { setupDOM('512px', '512px'); imageComparator = new ImageComparator(); - vis = new visualizationsStart.Vis(indexPattern, { + vis = visualizationsStart.createVis(indexPattern, { type: 'tagcloud', params: { bucket: { accessor: 0, format: {} }, diff --git a/src/legacy/core_plugins/vis_type_tagcloud/public/plugin.ts b/src/legacy/core_plugins/vis_type_tagcloud/public/plugin.ts index 9e5940eca15983..8244cba38edc32 100644 --- a/src/legacy/core_plugins/vis_type_tagcloud/public/plugin.ts +++ b/src/legacy/core_plugins/vis_type_tagcloud/public/plugin.ts @@ -53,7 +53,7 @@ export class TagCloudPlugin implements Plugin { colors: charts.colors, }; expressions.registerFunction(createTagCloudFn); - visualizations.types.createBaseVisualization( + visualizations.createBaseVisualization( createTagCloudVisTypeDefinition(visualizationDependencies) ); } diff --git a/src/legacy/core_plugins/vis_type_timelion/public/plugin.ts b/src/legacy/core_plugins/vis_type_timelion/public/plugin.ts index 69a2ad3c1351ae..9d69c312b48f44 100644 --- a/src/legacy/core_plugins/vis_type_timelion/public/plugin.ts +++ b/src/legacy/core_plugins/vis_type_timelion/public/plugin.ts @@ -66,7 +66,7 @@ export class TimelionVisPlugin implements Plugin { }; expressions.registerFunction(() => getTimelionVisualizationConfig(dependencies)); - visualizations.types.createReactVisualization(getTimelionVisDefinition(dependencies)); + visualizations.createReactVisualization(getTimelionVisDefinition(dependencies)); } public start(core: CoreStart, plugins: PluginsStart) { diff --git a/src/legacy/core_plugins/vis_type_timeseries/public/metrics_type.ts b/src/legacy/core_plugins/vis_type_timeseries/public/metrics_type.ts index 135cc1e1814320..30c62d778933bf 100644 --- a/src/legacy/core_plugins/vis_type_timeseries/public/metrics_type.ts +++ b/src/legacy/core_plugins/vis_type_timeseries/public/metrics_type.ts @@ -25,7 +25,7 @@ import { metricsRequestHandler } from './request_handler'; import { EditorController } from './editor_controller'; // @ts-ignore import { PANEL_TYPES } from '../../../../plugins/vis_type_timeseries/common/panel_types'; -import { defaultFeedbackMessage } from '../../visualizations/public'; +import { defaultFeedbackMessage } from '../../../../plugins/kibana_utils/common'; export const metricsVisDefinition = { name: 'metrics', diff --git a/src/legacy/core_plugins/vis_type_timeseries/public/plugin.ts b/src/legacy/core_plugins/vis_type_timeseries/public/plugin.ts index 38a9c68487854e..441b1f05ea78cd 100644 --- a/src/legacy/core_plugins/vis_type_timeseries/public/plugin.ts +++ b/src/legacy/core_plugins/vis_type_timeseries/public/plugin.ts @@ -57,7 +57,7 @@ export class MetricsPlugin implements Plugin, void> { ) { expressions.registerFunction(createMetricsFn); setUISettings(core.uiSettings); - visualizations.types.createReactVisualization(metricsVisDefinition); + visualizations.createReactVisualization(metricsVisDefinition); } public start(core: CoreStart, { data }: MetricsPluginStartDependencies) { diff --git a/src/legacy/core_plugins/vis_type_vega/public/__tests__/vega_visualization.js b/src/legacy/core_plugins/vis_type_vega/public/__tests__/vega_visualization.js index 378590af29d3a8..5befc09b245448 100644 --- a/src/legacy/core_plugins/vis_type_vega/public/__tests__/vega_visualization.js +++ b/src/legacy/core_plugins/vis_type_vega/public/__tests__/vega_visualization.js @@ -93,7 +93,7 @@ describe('VegaVisualizations', () => { if (!visRegComplete) { visRegComplete = true; - visualizationsSetup.types.createBaseVisualization( + visualizationsSetup.createBaseVisualization( createVegaTypeDefinition(vegaVisualizationDependencies) ); } @@ -108,7 +108,7 @@ describe('VegaVisualizations', () => { setupDOM('512px', '512px'); imageComparator = new ImageComparator(); - vis = new visualizationsStart.Vis(indexPattern, { type: 'vega' }); + vis = visualizationsStart.createVis(indexPattern, { type: 'vega' }); }); afterEach(function() { diff --git a/src/legacy/core_plugins/vis_type_vega/public/plugin.ts b/src/legacy/core_plugins/vis_type_vega/public/plugin.ts index b354433330caf8..3b01d9ceca5a6b 100644 --- a/src/legacy/core_plugins/vis_type_vega/public/plugin.ts +++ b/src/legacy/core_plugins/vis_type_vega/public/plugin.ts @@ -84,9 +84,7 @@ export class VegaPlugin implements Plugin, void> { expressions.registerFunction(() => createVegaFn(visualizationDependencies)); - visualizations.types.createBaseVisualization( - createVegaTypeDefinition(visualizationDependencies) - ); + visualizations.createBaseVisualization(createVegaTypeDefinition(visualizationDependencies)); } public start(core: CoreStart, { data }: VegaPluginStartDependencies) { diff --git a/src/legacy/core_plugins/vis_type_vega/public/vega_type.ts b/src/legacy/core_plugins/vis_type_vega/public/vega_type.ts index a84948f725e0ab..78f9c170ab62d7 100644 --- a/src/legacy/core_plugins/vis_type_vega/public/vega_type.ts +++ b/src/legacy/core_plugins/vis_type_vega/public/vega_type.ts @@ -19,10 +19,11 @@ import { i18n } from '@kbn/i18n'; // @ts-ignore -import { Status, defaultFeedbackMessage } from '../../visualizations/public'; +import { Status } from '../../visualizations/public'; import { DefaultEditorSize } from '../../vis_default_editor/public'; import { VegaVisualizationDependencies } from './plugin'; import { VegaVisEditor } from './components'; +import { defaultFeedbackMessage } from '../../../../plugins/kibana_utils/common'; import { createVegaRequestHandler } from './vega_request_handler'; // @ts-ignore diff --git a/src/legacy/core_plugins/vis_type_vislib/public/plugin.ts b/src/legacy/core_plugins/vis_type_vislib/public/plugin.ts index 8a7196a61ecec2..a71892cc47b05a 100644 --- a/src/legacy/core_plugins/vis_type_vislib/public/plugin.ts +++ b/src/legacy/core_plugins/vis_type_vislib/public/plugin.ts @@ -97,14 +97,14 @@ export class VisTypeVislibPlugin implements Plugin, void> { // Register legacy vislib types that have been converted convertedFns.forEach(expressions.registerFunction); convertedTypes.forEach(vis => - visualizations.types.createBaseVisualization(vis(visualizationDependencies)) + visualizations.createBaseVisualization(vis(visualizationDependencies)) ); } // Register non-converted types vislibFns.forEach(expressions.registerFunction); vislibTypes.forEach(vis => - visualizations.types.createBaseVisualization(vis(visualizationDependencies)) + visualizations.createBaseVisualization(vis(visualizationDependencies)) ); } diff --git a/src/legacy/core_plugins/vis_type_vislib/public/vislib/__tests__/visualizations/pie_chart.js b/src/legacy/core_plugins/vis_type_vislib/public/vislib/__tests__/visualizations/pie_chart.js index 9c9c5a84f046cc..43e3b987f19628 100644 --- a/src/legacy/core_plugins/vis_type_vislib/public/vislib/__tests__/visualizations/pie_chart.js +++ b/src/legacy/core_plugins/vis_type_vislib/public/vislib/__tests__/visualizations/pie_chart.js @@ -133,7 +133,7 @@ describe('No global chart settings', function() { responseHandler = vislibSlicesResponseHandler; let id1 = 1; - stubVis1 = new visualizationsStart.Vis(indexPattern, { + stubVis1 = visualizationsStart.createVis(indexPattern, { type: 'pie', aggs: rowAgg, }); @@ -222,7 +222,7 @@ describe('Vislib PieChart Class Test Suite', function() { responseHandler = vislibSlicesResponseHandler; let id = 1; - stubVis = new visualizationsStart.Vis(indexPattern, { + stubVis = visualizationsStart.createVis(indexPattern, { type: 'pie', aggs: dataAgg, }); diff --git a/src/legacy/core_plugins/vis_type_xy/public/plugin.ts b/src/legacy/core_plugins/vis_type_xy/public/plugin.ts index 59bb64b337256c..35abb04fd87324 100644 --- a/src/legacy/core_plugins/vis_type_xy/public/plugin.ts +++ b/src/legacy/core_plugins/vis_type_xy/public/plugin.ts @@ -72,7 +72,7 @@ export class VisTypeXyPlugin implements Plugin, void> { visFunctions.forEach((fn: any) => expressions.registerFunction(fn)); visTypeDefinitions.forEach((vis: any) => - visualizations.types.createBaseVisualization(vis(visualizationDependencies)) + visualizations.createBaseVisualization(vis(visualizationDependencies)) ); } diff --git a/src/legacy/core_plugins/visualizations/public/np_ready/public/index.ts b/src/legacy/core_plugins/visualizations/public/np_ready/public/index.ts index 7688a7769cf795..b59eb2277411c5 100644 --- a/src/legacy/core_plugins/visualizations/public/np_ready/public/index.ts +++ b/src/legacy/core_plugins/visualizations/public/np_ready/public/index.ts @@ -43,20 +43,11 @@ export { Vis, VisParams, VisState } from './vis'; import { VisualizeEmbeddableFactory, VisualizeEmbeddable } from './embeddable'; export type VisualizeEmbeddableFactoryContract = PublicContract; export type VisualizeEmbeddableContract = PublicContract; +export { TypesService } from './vis_types/types_service'; +export { Status } from './legacy/update_status'; // should remove +export { VISUALIZE_EMBEDDABLE_TYPE, VisualizeInput } from './embeddable'; +export { SchemaConfig } from './legacy/build_pipeline'; export function plugin(initializerContext: PluginInitializerContext) { return new VisualizationsPlugin(initializerContext); } - -/** @public static code */ -export { TypesService } from './vis_types/types_service'; -export { VISUALIZE_EMBEDDABLE_TYPE, VisualizeInput } from './embeddable'; - -export { Status } from './legacy/update_status'; -export { buildPipeline, buildVislibDimensions, SchemaConfig } from './legacy/build_pipeline'; - -// @ts-ignore -export { updateOldState } from './legacy/vis_update_state'; -export { calculateObjectHash } from './legacy/calculate_object_hash'; -export { createSavedVisLoader } from './saved_visualizations/saved_visualizations'; -export { defaultFeedbackMessage } from './misc/default_feedback_message'; diff --git a/src/legacy/core_plugins/visualizations/public/np_ready/public/legacy/__tests__/_vis.js b/src/legacy/core_plugins/visualizations/public/np_ready/public/legacy/__tests__/_vis.js index 8c75ba24051b00..deb345a77cdb6e 100644 --- a/src/legacy/core_plugins/visualizations/public/np_ready/public/legacy/__tests__/_vis.js +++ b/src/legacy/core_plugins/visualizations/public/np_ready/public/legacy/__tests__/_vis.js @@ -43,12 +43,12 @@ describe('Vis Class', function() { beforeEach( ngMock.inject(function(Private) { indexPattern = Private(FixturesStubbedLogstashIndexPatternProvider); - visTypes = visualizations.types; + visTypes = visualizations; }) ); beforeEach(function() { - vis = new visualizations.Vis(indexPattern, stateFixture); + vis = visualizations.createVis(indexPattern, stateFixture); }); const verifyVis = function(vis) { @@ -84,7 +84,7 @@ describe('Vis Class', function() { describe('setState()', function() { it('should set the state to defaults', function() { - const vis = new visualizations.Vis(indexPattern); + const vis = visualizations.createVis(indexPattern); expect(vis).to.have.property('type'); expect(vis.type).to.eql(visTypes.get('histogram')); expect(vis).to.have.property('aggs'); @@ -100,7 +100,7 @@ describe('Vis Class', function() { expect(vis.isHierarchical()).to.be(true); }); it('should return false for non-hierarchical vis (like histogram)', function() { - const vis = new visualizations.Vis(indexPattern); + const vis = visualizations.createVis(indexPattern); expect(vis.isHierarchical()).to.be(false); }); }); diff --git a/src/legacy/core_plugins/visualizations/public/np_ready/public/legacy/update_status.ts b/src/legacy/core_plugins/visualizations/public/np_ready/public/legacy/update_status.ts index d9af5122eadec6..92a9ce8366f4fe 100644 --- a/src/legacy/core_plugins/visualizations/public/np_ready/public/legacy/update_status.ts +++ b/src/legacy/core_plugins/visualizations/public/np_ready/public/legacy/update_status.ts @@ -18,7 +18,7 @@ */ import { PersistedState } from '../../../../../../../plugins/visualizations/public'; -import { calculateObjectHash } from './calculate_object_hash'; +import { calculateObjectHash } from '../../../../../../../plugins/kibana_utils/common'; import { Vis } from '../vis'; enum Status { diff --git a/src/legacy/core_plugins/visualizations/public/np_ready/public/mocks.ts b/src/legacy/core_plugins/visualizations/public/np_ready/public/mocks.ts index 8d7407b6191d63..9e8eac08c33eac 100644 --- a/src/legacy/core_plugins/visualizations/public/np_ready/public/mocks.ts +++ b/src/legacy/core_plugins/visualizations/public/np_ready/public/mocks.ts @@ -28,23 +28,19 @@ import { usageCollectionPluginMock } from '../../../../../../plugins/usage_colle import { uiActionsPluginMock } from '../../../../../../plugins/ui_actions/public/mocks'; const createSetupContract = (): VisualizationsSetup => ({ - types: { - createBaseVisualization: jest.fn(), - createReactVisualization: jest.fn(), - registerAlias: jest.fn(), - hideTypes: jest.fn(), - }, + createBaseVisualization: jest.fn(), + createReactVisualization: jest.fn(), + registerAlias: jest.fn(), + hideTypes: jest.fn(), }); const createStartContract = (): VisualizationsStart => ({ - types: { - get: jest.fn(), - all: jest.fn(), - getAliases: jest.fn(), - }, + get: jest.fn(), + all: jest.fn(), + getAliases: jest.fn(), savedVisualizationsLoader: {} as any, showNewVisModal: jest.fn(), - Vis: jest.fn(), + createVis: jest.fn(), }); const createInstance = async () => { diff --git a/src/legacy/core_plugins/visualizations/public/np_ready/public/plugin.ts b/src/legacy/core_plugins/visualizations/public/np_ready/public/plugin.ts index 10797a1a04df49..b8db611f30815f 100644 --- a/src/legacy/core_plugins/visualizations/public/np_ready/public/plugin.ts +++ b/src/legacy/core_plugins/visualizations/public/np_ready/public/plugin.ts @@ -48,27 +48,27 @@ import { visualization as visualizationRenderer } from './expressions/visualizat import { DataPublicPluginSetup, DataPublicPluginStart, + IIndexPattern, } from '../../../../../../plugins/data/public'; import { UsageCollectionSetup } from '../../../../../../plugins/usage_collection/public'; import { createSavedVisLoader, SavedVisualizationsLoader } from './saved_visualizations'; -import { VisImpl, VisImplConstructor } from './vis_impl'; +import { VisImpl } from './vis_impl'; import { showNewVisModal } from './wizard'; import { UiActionsStart } from '../../../../../../plugins/ui_actions/public'; import { DataStart as LegacyDataStart } from '../../../../data/public'; +import { VisState } from './types'; /** * Interface for this plugin's returned setup/start contracts. * * @public */ -export interface VisualizationsSetup { - types: TypesSetup; -} -export interface VisualizationsStart { - types: TypesStart; +export type VisualizationsSetup = TypesSetup; + +export interface VisualizationsStart extends TypesStart { savedVisualizationsLoader: SavedVisualizationsLoader; - Vis: VisImplConstructor; + createVis: (indexPattern: IIndexPattern, visState?: VisState) => VisImpl; showNewVisModal: typeof showNewVisModal; } @@ -122,7 +122,7 @@ export class VisualizationsPlugin embeddable.registerEmbeddableFactory(VISUALIZE_EMBEDDABLE_TYPE, embeddableFactory); return { - types: this.types.setup(), + ...this.types.setup(), }; } @@ -152,9 +152,15 @@ export class VisualizationsPlugin setSavedVisualizationsLoader(savedVisualizationsLoader); return { - types, + ...types, showNewVisModal, - Vis: VisImpl, + /** + * creates new instance of Vis + * @param {IIndexPattern} indexPattern - index pattern to use + * @param {VisState} visState - visualization configuration + */ + createVis: (indexPattern: IIndexPattern, visState?: VisState) => + new VisImpl(indexPattern, visState), savedVisualizationsLoader, }; } diff --git a/src/legacy/core_plugins/visualizations/public/np_ready/public/saved_visualizations/_saved_vis.ts b/src/legacy/core_plugins/visualizations/public/np_ready/public/saved_visualizations/_saved_vis.ts index 2458ed5008ddda..e381a01edef8ba 100644 --- a/src/legacy/core_plugins/visualizations/public/np_ready/public/saved_visualizations/_saved_vis.ts +++ b/src/legacy/core_plugins/visualizations/public/np_ready/public/saved_visualizations/_saved_vis.ts @@ -29,7 +29,8 @@ import { SavedObject, SavedObjectKibanaServices, } from '../../../../../../../plugins/saved_objects/public'; -import { updateOldState } from '../../../index'; +// @ts-ignore +import { updateOldState } from '../legacy/vis_update_state'; import { extractReferences, injectReferences } from './saved_visualization_references'; import { IIndexPattern } from '../../../../../../../plugins/data/public'; import { VisSavedObject } from '../types'; diff --git a/src/legacy/core_plugins/visualizations/public/np_ready/public/vis_types/types_service.ts b/src/legacy/core_plugins/visualizations/public/np_ready/public/vis_types/types_service.ts index 0cae83afb78610..6bcaa9a3e1dac7 100644 --- a/src/legacy/core_plugins/visualizations/public/np_ready/public/vis_types/types_service.ts +++ b/src/legacy/core_plugins/visualizations/public/np_ready/public/vis_types/types_service.ts @@ -67,15 +67,32 @@ export class TypesService { this.types[visDefinition.name] = visDefinition; }; return { + /** + * registers a visualization type + * @param {VisType} config - visualization type definition + */ createBaseVisualization: (config: any) => { const vis = new BaseVisType(config); registerVisualization(() => vis); }, + /** + * registers a visualization which uses react for rendering + * @param {VisType} config - visualization type definition + */ createReactVisualization: (config: any) => { const vis = new ReactVisType(config); registerVisualization(() => vis); }, + /** + * registers a visualization alias + * alias is a visualization type without implementation, it just redirects somewhere in kibana + * @param {VisTypeAlias} config - visualization alias definition + */ registerAlias: visTypeAliasRegistry.add, + /** + * allows to hide specific visualization types from create visualization dialog + * @param {string[]} typeNames - list of type ids to hide + */ hideTypes: (typeNames: string[]) => { typeNames.forEach((name: string) => { if (this.types[name]) { @@ -90,12 +107,22 @@ export class TypesService { public start() { return { + /** + * returns specific visualization or undefined if not found + * @param {string} visualization - id of visualization to return + */ get: (visualization: string) => { return this.types[visualization]; }, + /** + * returns all registered visualization types + */ all: () => { return [...Object.values(this.types)]; }, + /** + * returns all registered aliases + */ getAliases: visTypeAliasRegistry.get, }; } diff --git a/src/legacy/core_plugins/visualizations/public/np_ready/public/wizard/show_new_vis.tsx b/src/legacy/core_plugins/visualizations/public/np_ready/public/wizard/show_new_vis.tsx index a79c6ad98edf6f..6b37845f03db12 100644 --- a/src/legacy/core_plugins/visualizations/public/np_ready/public/wizard/show_new_vis.tsx +++ b/src/legacy/core_plugins/visualizations/public/np_ready/public/wizard/show_new_vis.tsx @@ -29,6 +29,11 @@ export interface ShowNewVisModalParams { onClose?: () => void; } +/** + * shows modal dialog that allows you to create new visualization + * @param {string[]} editorParams + * @param {function} onClose - function that will be called when dialog is closed + */ export function showNewVisModal({ editorParams = [], onClose }: ShowNewVisModalParams = {}) { const container = document.createElement('div'); let isClosed = false; diff --git a/src/legacy/ui/public/field_editor/field_editor.js b/src/legacy/ui/public/field_editor/field_editor.js index ee88ad95eeff02..43461c4c689be6 100644 --- a/src/legacy/ui/public/field_editor/field_editor.js +++ b/src/legacy/ui/public/field_editor/field_editor.js @@ -66,7 +66,7 @@ import { ScriptingHelpFlyout } from './components/scripting_help'; import { FieldFormatEditor } from './components/field_format_editor'; import { FIELD_TYPES_BY_LANG, DEFAULT_FIELD_TYPES } from './constants'; -import { copyField, getDefaultFormat, executeScript, isScriptValid } from './lib'; +import { copyField, executeScript, isScriptValid } from './lib'; import { i18n } from '@kbn/i18n'; import { FormattedMessage } from '@kbn/i18n/react'; @@ -76,6 +76,25 @@ import 'brace/mode/groovy'; const getFieldFormats = () => npStart.plugins.data.fieldFormats; +const getFieldTypeFormatsList = (field, defaultFieldFormat) => { + const fieldFormats = getFieldFormats(); + const formatsByType = fieldFormats.getByFieldType(field.type).map(({ id, title }) => ({ + id, + title, + })); + + return [ + { + id: '', + defaultFieldFormat, + title: i18n.translate('common.ui.fieldEditor.defaultFormatDropDown', { + defaultMessage: '- Default -', + }), + }, + ...formatsByType, + ]; +}; + export class FieldEditor extends PureComponent { static propTypes = { indexPattern: PropTypes.object.isRequired, @@ -137,11 +156,7 @@ export class FieldEditor extends PureComponent { field.type = fieldTypes.includes(field.type) ? field.type : fieldTypes[0]; const fieldFormats = getFieldFormats(); - - const fieldTypeFormats = [ - getDefaultFormat(fieldFormats.getDefaultType(field.type, field.esTypes)), - ...fieldFormats.getByFieldType(field.type), - ]; + const DefaultFieldFormat = fieldFormats.getDefaultType(field.type, field.esTypes); this.setState({ isReady: true, @@ -150,14 +165,14 @@ export class FieldEditor extends PureComponent { errors: [], scriptingLangs, fieldTypes, - fieldTypeFormats, + fieldTypeFormats: getFieldTypeFormatsList(field, DefaultFieldFormat), fieldFormatId: get(indexPattern, ['fieldFormatMap', field.name, 'type', 'id']), fieldFormatParams: field.format.params(), }); } onFieldChange = (fieldName, value) => { - const field = this.state.field; + const { field } = this.state; field[fieldName] = value; this.forceUpdate(); }; @@ -169,18 +184,11 @@ export class FieldEditor extends PureComponent { const DefaultFieldFormat = fieldFormats.getDefaultType(type); field.type = type; - - const fieldTypeFormats = [ - getDefaultFormat(DefaultFieldFormat), - ...getFieldFormats().getByFieldType(field.type), - ]; - - const FieldFormat = fieldTypeFormats[0]; - field.format = new FieldFormat(null, getConfig); + field.format = new DefaultFieldFormat(null, getConfig); this.setState({ - fieldTypeFormats, - fieldFormatId: FieldFormat.id, + fieldTypeFormats: getFieldTypeFormatsList(field, DefaultFieldFormat), + fieldFormatId: DefaultFieldFormat.id, fieldFormatParams: field.format.params(), }); }; @@ -197,12 +205,13 @@ export class FieldEditor extends PureComponent { }; onFormatChange = (formatId, params) => { - const { getConfig } = this.props.helpers; + const fieldFormats = getFieldFormats(); const { field, fieldTypeFormats } = this.state; - const FieldFormat = - fieldTypeFormats.find(format => format.id === formatId) || fieldTypeFormats[0]; + const FieldFormat = fieldFormats.getType( + formatId || fieldTypeFormats[0]?.defaultFieldFormat.id + ); - field.format = new FieldFormat(params, getConfig); + field.format = new FieldFormat(params, this.props.helpers.getConfig); this.setState({ fieldFormatId: FieldFormat.id, @@ -416,7 +425,8 @@ export class FieldEditor extends PureComponent { renderFormat() { const { field, fieldTypeFormats, fieldFormatId, fieldFormatParams } = this.state; const { fieldFormatEditors } = this.props.helpers; - const defaultFormat = fieldTypeFormats[0] && fieldTypeFormats[0].resolvedTitle; + const defaultFormat = fieldTypeFormats[0]?.defaultFieldFormat.title; + const label = defaultFormat ? ( { - return '0,0.[000]'; -}; - -describe('getDefaultFormat', () => { - it('should create default format', () => { - const DefaultFormat = getDefaultFormat(fieldFormats.NumberFormat); - const defaultFormatObject = new DefaultFormat(null, getConfig); - const formatObject = new fieldFormats.NumberFormat(null, getConfig); - - expect(DefaultFormat.id).toEqual(''); - expect(DefaultFormat.resolvedTitle).toEqual(fieldFormats.NumberFormat.title); - expect(DefaultFormat.title).toEqual('- Default -'); - expect(JSON.stringify(defaultFormatObject.params())).toEqual( - JSON.stringify(formatObject.params()) - ); - }); -}); diff --git a/src/legacy/ui/public/field_editor/lib/get_default_format.js b/src/legacy/ui/public/field_editor/lib/get_default_format.js deleted file mode 100644 index acb7ab9c6afa53..00000000000000 --- a/src/legacy/ui/public/field_editor/lib/get_default_format.js +++ /dev/null @@ -1,32 +0,0 @@ -/* - * 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. - */ - -import { i18n } from '@kbn/i18n'; - -export const getDefaultFormat = Format => { - class DefaultFormat extends Format { - static id = ''; - static resolvedTitle = Format.title; - static title = i18n.translate('common.ui.fieldEditor.defaultFormatDropDown', { - defaultMessage: '- Default -', - }); - } - - return DefaultFormat; -}; diff --git a/src/legacy/ui/public/field_editor/lib/index.js b/src/legacy/ui/public/field_editor/lib/index.js index 5e12d51763a189..c74bb0cc2ef8ab 100644 --- a/src/legacy/ui/public/field_editor/lib/index.js +++ b/src/legacy/ui/public/field_editor/lib/index.js @@ -18,5 +18,4 @@ */ export { copyField } from './copy_field'; -export { getDefaultFormat } from './get_default_format'; export { executeScript, isScriptValid } from './validate_script'; diff --git a/src/plugins/data/common/es_query/filters/get_display_value.ts b/src/plugins/data/common/es_query/filters/get_display_value.ts index 4bf7e1c9c6ba70..03167f3080419a 100644 --- a/src/plugins/data/common/es_query/filters/get_display_value.ts +++ b/src/plugins/data/common/es_query/filters/get_display_value.ts @@ -18,6 +18,7 @@ */ import { get } from 'lodash'; +import { i18n } from '@kbn/i18n'; import { IIndexPattern, IFieldType } from '../..'; import { getIndexPatternFromFilter } from './get_index_pattern_from_filter'; import { Filter } from '../filters'; @@ -27,7 +28,16 @@ function getValueFormatter(indexPattern?: IIndexPattern, key?: string) { let format = get(indexPattern, ['fields', 'byName', key, 'format']); if (!format && (indexPattern.fields as any).getByName) { // TODO: Why is indexPatterns sometimes a map and sometimes an array? - format = ((indexPattern.fields as any).getByName(key) as IFieldType).format; + const field: IFieldType = (indexPattern.fields as any).getByName(key); + if (!field) { + throw new Error( + i18n.translate('data.filter.filterBar.fieldNotFound', { + defaultMessage: 'Field {key} not found in index pattern {indexPattern}', + values: { key, indexPattern: indexPattern.title }, + }) + ); + } + format = field.format; } return format; } diff --git a/src/plugins/data/public/index.ts b/src/plugins/data/public/index.ts index a5f4ce2ce3c581..86cc0cca85e0b1 100644 --- a/src/plugins/data/public/index.ts +++ b/src/plugins/data/public/index.ts @@ -283,8 +283,39 @@ export { * Search: */ -export { IRequestTypesMap, IResponseTypesMap } from './search'; -export * from './search'; +export { + ES_SEARCH_STRATEGY, + SYNC_SEARCH_STRATEGY, + defaultSearchStrategy, + esSearchStrategyProvider, + getEsPreference, + addSearchStrategy, + hasSearchStategyForIndexPattern, + getSearchErrorType, + ISearchContext, + TSearchStrategyProvider, + ISearchStrategy, + ISearch, + ISearchOptions, + IRequestTypesMap, + IResponseTypesMap, + ISearchGeneric, + IEsSearchResponse, + IEsSearchRequest, + ISyncSearchRequest, + IKibanaSearchResponse, + IKibanaSearchRequest, + SearchRequest, + SearchResponse, + SearchError, + SearchStrategyProvider, + ISearchSource, + SearchSource, + SearchSourceFields, + EsQuerySortValue, + SortDirection, + FetchOptions, +} from './search'; /* * UI components diff --git a/src/plugins/data/public/query/state_sync/sync_state_with_url.ts b/src/plugins/data/public/query/state_sync/sync_state_with_url.ts index cd7058b9f8f1c1..77e5b0ab02dc13 100644 --- a/src/plugins/data/public/query/state_sync/sync_state_with_url.ts +++ b/src/plugins/data/public/query/state_sync/sync_state_with_url.ts @@ -85,7 +85,10 @@ export const syncQueryStateWithUrl = ( stateContainer: { ...globalQueryStateContainer, set: state => { - globalQueryStateContainer.set(state || defaultState); + if (state) { + // syncState utils requires to handle incoming "null" value + globalQueryStateContainer.set(state); + } }, }, storageKey: GLOBAL_STATE_STORAGE_KEY, diff --git a/src/plugins/data/public/search/fetch/types.ts b/src/plugins/data/public/search/fetch/types.ts index 62eb965703c3a4..e8de0576b8a72f 100644 --- a/src/plugins/data/public/search/fetch/types.ts +++ b/src/plugins/data/public/search/fetch/types.ts @@ -17,8 +17,8 @@ * under the License. */ -import { ISearchStart } from 'src/plugins/data/public'; import { IUiSettingsClient } from '../../../../../core/public'; +import { ISearchStart } from '../types'; export interface FetchOptions { abortSignal?: AbortSignal; diff --git a/src/plugins/data/public/ui/filter_bar/_global_filter_item.scss b/src/plugins/data/public/ui/filter_bar/_global_filter_item.scss index 51204e2a611688..24adf0093af952 100644 --- a/src/plugins/data/public/ui/filter_bar/_global_filter_item.scss +++ b/src/plugins/data/public/ui/filter_bar/_global_filter_item.scss @@ -32,6 +32,15 @@ font-style: italic; } +.globalFilterItem-isInvalid { + text-decoration: none; + + .globalFilterLabel__value { + color: $euiColorDanger; + font-weight: $euiFontWeightBold; + } +} + .globalFilterItem-isPinned { position: relative; diff --git a/src/plugins/data/public/ui/filter_bar/filter_editor/lib/filter_label.tsx b/src/plugins/data/public/ui/filter_bar/filter_editor/lib/filter_label.tsx index ee6d178b25c22e..070631354d8b80 100644 --- a/src/plugins/data/public/ui/filter_bar/filter_editor/lib/filter_label.tsx +++ b/src/plugins/data/public/ui/filter_bar/filter_editor/lib/filter_label.tsx @@ -41,6 +41,10 @@ export function FilterLabel({ filter, valueLabel }: Props) { prefixText ); + const getValue = (text?: string) => { + return {text}; + }; + if (filter.meta.alias !== null) { return ( @@ -55,35 +59,35 @@ export function FilterLabel({ filter, valueLabel }: Props) { return ( {prefix} - {filter.meta.key} {existsOperator.message} + {filter.meta.key}: {getValue(`${existsOperator.message}`)} ); case FILTERS.GEO_BOUNDING_BOX: return ( {prefix} - {filter.meta.key}: {valueLabel} + {filter.meta.key}: {getValue(valueLabel)} ); case FILTERS.GEO_POLYGON: return ( {prefix} - {filter.meta.key}: {valueLabel} + {filter.meta.key}: {getValue(valueLabel)} ); case FILTERS.PHRASES: return ( {prefix} - {filter.meta.key} {isOneOfOperator.message} {valueLabel} + {filter.meta.key}: {getValue(`${isOneOfOperator.message} ${valueLabel}`)} ); case FILTERS.QUERY_STRING: return ( {prefix} - {valueLabel} + {getValue(`${valueLabel}`)} ); case FILTERS.PHRASE: @@ -91,14 +95,14 @@ export function FilterLabel({ filter, valueLabel }: Props) { return ( {prefix} - {filter.meta.key}: {valueLabel} + {filter.meta.key}: {getValue(valueLabel)} ); default: return ( {prefix} - {JSON.stringify(filter.query) || filter.meta.value} + {getValue(`${JSON.stringify(filter.query) || filter.meta.value}`)} ); } diff --git a/src/plugins/data/public/ui/filter_bar/filter_item.tsx b/src/plugins/data/public/ui/filter_bar/filter_item.tsx index 0febfe807a946d..6b5fd41dc06eab 100644 --- a/src/plugins/data/public/ui/filter_bar/filter_item.tsx +++ b/src/plugins/data/public/ui/filter_bar/filter_item.tsx @@ -33,6 +33,7 @@ import { toggleFilterPinned, toggleFilterDisabled, } from '../../../common'; +import { getNotifications } from '../../services'; interface Props { id: string; @@ -64,24 +65,41 @@ class FilterItemUI extends Component { public render() { const { filter, id } = this.props; const { negate, disabled } = filter.meta; + let hasError: boolean = false; + + let valueLabel; + try { + valueLabel = getDisplayValueFromFilter(filter, this.props.indexPatterns); + } catch (e) { + getNotifications().toasts.addError(e, { + title: this.props.intl.formatMessage({ + id: 'data.filter.filterBar.labelErrorMessage', + defaultMessage: 'Failed to display filter', + }), + }); + valueLabel = this.props.intl.formatMessage({ + id: 'data.filter.filterBar.labelErrorText', + defaultMessage: 'Error', + }); + hasError = true; + } + const dataTestSubjKey = filter.meta.key ? `filter-key-${filter.meta.key}` : ''; + const dataTestSubjValue = filter.meta.value ? `filter-value-${valueLabel}` : ''; + const dataTestSubjDisabled = `filter-${ + this.props.filter.meta.disabled ? 'disabled' : 'enabled' + }`; const classes = classNames( 'globalFilterItem', { - 'globalFilterItem-isDisabled': disabled, + 'globalFilterItem-isDisabled': disabled || hasError, + 'globalFilterItem-isInvalid': hasError, 'globalFilterItem-isPinned': isFilterPinned(filter), 'globalFilterItem-isExcluded': negate, }, this.props.className ); - const valueLabel = getDisplayValueFromFilter(filter, this.props.indexPatterns); - const dataTestSubjKey = filter.meta.key ? `filter-key-${filter.meta.key}` : ''; - const dataTestSubjValue = filter.meta.value ? `filter-value-${valueLabel}` : ''; - const dataTestSubjDisabled = `filter-${ - this.props.filter.meta.disabled ? 'disabled' : 'enabled' - }`; - const badge = ( { expect(resp.body.message).to.contain( - '[request query.look_back]: Value is [0] but it must be equal to or greater than [1].' + '[request query.look_back]: Value must be equal to or greater than [1].' ); })); }); diff --git a/test/functional/apps/discover/_discover.js b/test/functional/apps/discover/_discover.js index 432e83891aa928..2011b9bc274f58 100644 --- a/test/functional/apps/discover/_discover.js +++ b/test/functional/apps/discover/_discover.js @@ -23,7 +23,6 @@ export default function({ getService, getPageObjects }) { const log = getService('log'); const retry = getService('retry'); const esArchiver = getService('esArchiver'); - const browser = getService('browser'); const kibanaServer = getService('kibanaServer'); const queryBar = getService('queryBar'); const PageObjects = getPageObjects(['common', 'discover', 'header', 'timePicker']); @@ -188,7 +187,7 @@ export default function({ getService, getPageObjects }) { describe('time zone switch', () => { it('should show bars in the correct time zone after switching', async function() { await kibanaServer.uiSettings.replace({ 'dateFormat:tz': 'America/Phoenix' }); - await browser.refresh(); + await PageObjects.common.navigateToApp('discover'); await PageObjects.header.awaitKibanaChrome(); await PageObjects.timePicker.setDefaultAbsoluteRange(); diff --git a/test/plugin_functional/plugins/kbn_tp_custom_visualizations/public/self_changing_vis/self_changing_vis.js b/test/plugin_functional/plugins/kbn_tp_custom_visualizations/public/self_changing_vis/self_changing_vis.js index 2976a6cd98e30a..643d15c9827929 100644 --- a/test/plugin_functional/plugins/kbn_tp_custom_visualizations/public/self_changing_vis/self_changing_vis.js +++ b/test/plugin_functional/plugins/kbn_tp_custom_visualizations/public/self_changing_vis/self_changing_vis.js @@ -22,7 +22,7 @@ import { SelfChangingComponent } from './self_changing_components'; import { setup as visualizations } from '../../../../../../src/legacy/core_plugins/visualizations/public/np_ready/public/legacy'; -visualizations.types.createReactVisualization({ +visualizations.createReactVisualization({ name: 'self_changing_vis', title: 'Self Changing Vis', icon: 'controlsHorizontal', diff --git a/x-pack/.i18nrc.json b/x-pack/.i18nrc.json index f2af61df73d206..53628ea970fb6f 100644 --- a/x-pack/.i18nrc.json +++ b/x-pack/.i18nrc.json @@ -39,7 +39,7 @@ "xpack.snapshotRestore": "plugins/snapshot_restore", "xpack.spaces": ["legacy/plugins/spaces", "plugins/spaces"], "xpack.taskManager": "legacy/plugins/task_manager", - "xpack.transform": ["legacy/plugins/transform", "plugins/transform"], + "xpack.transform": "plugins/transform", "xpack.triggersActionsUI": "plugins/triggers_actions_ui", "xpack.upgradeAssistant": "plugins/upgrade_assistant", "xpack.uptime": "legacy/plugins/uptime", diff --git a/x-pack/legacy/plugins/apm/public/components/app/ServiceMap/EmptyBanner.tsx b/x-pack/legacy/plugins/apm/public/components/app/ServiceMap/EmptyBanner.tsx new file mode 100644 index 00000000000000..418430e37b21e6 --- /dev/null +++ b/x-pack/legacy/plugins/apm/public/components/app/ServiceMap/EmptyBanner.tsx @@ -0,0 +1,43 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { EuiCallOut } from '@elastic/eui'; +import lightTheme from '@elastic/eui/dist/eui_theme_light.json'; +import { i18n } from '@kbn/i18n'; +import React from 'react'; +import styled from 'styled-components'; +import { ElasticDocsLink } from '../../shared/Links/ElasticDocsLink'; + +const EmptyBannerCallOut = styled(EuiCallOut)` + margin: ${lightTheme.gutterTypes.gutterSmall}; + /* Add some extra margin so it displays to the right of the controls. */ + margin-left: calc( + ${lightTheme.gutterTypes.gutterLarge} + + ${lightTheme.gutterTypes.gutterExtraLarge} + ); + position: absolute; + z-index: 1; +`; + +export function EmptyBanner() { + return ( + + {i18n.translate('xpack.apm.serviceMap.emptyBanner.message', { + defaultMessage: + "We will map out connected services and external requests if we can detect them. Please make sure you're running the latest version of the APM agent." + })}{' '} + + {i18n.translate('xpack.apm.serviceMap.emptyBanner.docsLink', { + defaultMessage: 'Learn more in the docs' + })} + + + ); +} diff --git a/x-pack/legacy/plugins/apm/public/components/app/ServiceMap/index.test.tsx b/x-pack/legacy/plugins/apm/public/components/app/ServiceMap/index.test.tsx new file mode 100644 index 00000000000000..926f53954e7c6b --- /dev/null +++ b/x-pack/legacy/plugins/apm/public/components/app/ServiceMap/index.test.tsx @@ -0,0 +1,45 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { render } from '@testing-library/react'; +import React, { FunctionComponent } from 'react'; +import { License } from '../../../../../../../plugins/licensing/common/license'; +import { LicenseContext } from '../../../context/LicenseContext'; +import { MockApmPluginContextWrapper } from '../../../utils/testHelpers'; +import { ServiceMap } from './'; + +const expiredLicense = new License({ + signature: 'test signature', + license: { + expiryDateInMillis: 0, + mode: 'platinum', + status: 'expired', + type: 'platinum', + uid: '1' + } +}); + +const Wrapper: FunctionComponent = ({ children }) => { + return ( + + {children} + + ); +}; + +describe('ServiceMap', () => { + describe('with an inactive license', () => { + it('renders the license banner', async () => { + expect( + ( + await render(, { + wrapper: Wrapper + }).findAllByText(/Platinum/) + ).length + ).toBeGreaterThan(0); + }); + }); +}); diff --git a/x-pack/legacy/plugins/apm/public/components/app/ServiceMap/index.tsx b/x-pack/legacy/plugins/apm/public/components/app/ServiceMap/index.tsx index 9a93c67f08187a..2942ce64729e7d 100644 --- a/x-pack/legacy/plugins/apm/public/components/app/ServiceMap/index.tsx +++ b/x-pack/legacy/plugins/apm/public/components/app/ServiceMap/index.tsx @@ -26,13 +26,14 @@ import { useLicense } from '../../../hooks/useLicense'; import { useLoadingIndicator } from '../../../hooks/useLoadingIndicator'; import { useLocation } from '../../../hooks/useLocation'; import { useUrlParams } from '../../../hooks/useUrlParams'; +import { callApmApi } from '../../../services/rest/createCallApmApi'; import { Controls } from './Controls'; import { Cytoscape } from './Cytoscape'; +import { EmptyBanner } from './EmptyBanner'; import { getCytoscapeElements } from './get_cytoscape_elements'; import { PlatinumLicensePrompt } from './PlatinumLicensePrompt'; import { Popover } from './Popover'; import { useRefDimensions } from './useRefDimensions'; -import { callApmApi } from '../../../services/rest/createCallApmApi'; interface ServiceMapProps { serviceName?: string; @@ -214,6 +215,9 @@ export function ServiceMap({ serviceName }: ServiceMapProps) { style={cytoscapeDivStyle} > + {serviceName && renderedElements.current.length === 1 && ( + + )} diff --git a/x-pack/legacy/plugins/apm/public/components/shared/Links/ElasticDocsLink.tsx b/x-pack/legacy/plugins/apm/public/components/shared/Links/ElasticDocsLink.tsx index 7645162ab26559..0e0c318ad32996 100644 --- a/x-pack/legacy/plugins/apm/public/components/shared/Links/ElasticDocsLink.tsx +++ b/x-pack/legacy/plugins/apm/public/components/shared/Links/ElasticDocsLink.tsx @@ -23,7 +23,7 @@ export function ElasticDocsLink({ section, path, children, ...rest }: Props) { children(href) ) : ( - children + {children} ); } diff --git a/x-pack/legacy/plugins/canvas/canvas_plugin_src/functions/common/saved_map.test.ts b/x-pack/legacy/plugins/canvas/canvas_plugin_src/functions/common/saved_map.test.ts index cf0c76be4580d1..63dbae55790a3e 100644 --- a/x-pack/legacy/plugins/canvas/canvas_plugin_src/functions/common/saved_map.test.ts +++ b/x-pack/legacy/plugins/canvas/canvas_plugin_src/functions/common/saved_map.test.ts @@ -5,7 +5,7 @@ */ jest.mock('ui/new_platform'); import { savedMap } from './saved_map'; -import { getQueryFilters } from '../../../server/lib/build_embeddable_filters'; +import { getQueryFilters } from '../../../public/lib/build_embeddable_filters'; const filterContext = { and: [ diff --git a/x-pack/legacy/plugins/canvas/canvas_plugin_src/functions/common/saved_map.ts b/x-pack/legacy/plugins/canvas/canvas_plugin_src/functions/common/saved_map.ts index bc30ca858bd504..78240eee7ce130 100644 --- a/x-pack/legacy/plugins/canvas/canvas_plugin_src/functions/common/saved_map.ts +++ b/x-pack/legacy/plugins/canvas/canvas_plugin_src/functions/common/saved_map.ts @@ -7,7 +7,7 @@ import { ExpressionFunctionDefinition } from 'src/plugins/expressions/common'; import { TimeRange } from 'src/plugins/data/public'; import { EmbeddableInput } from 'src/legacy/core_plugins/embeddable_api/public/np_ready/public'; -import { getQueryFilters } from '../../../server/lib/build_embeddable_filters'; +import { getQueryFilters } from '../../../public/lib/build_embeddable_filters'; import { Filter, MapCenter, TimeRange as TimeRangeArg } from '../../../types'; import { EmbeddableTypes, diff --git a/x-pack/legacy/plugins/canvas/canvas_plugin_src/functions/common/saved_search.test.ts b/x-pack/legacy/plugins/canvas/canvas_plugin_src/functions/common/saved_search.test.ts index 294d6124c7e339..67356dae5b3e33 100644 --- a/x-pack/legacy/plugins/canvas/canvas_plugin_src/functions/common/saved_search.test.ts +++ b/x-pack/legacy/plugins/canvas/canvas_plugin_src/functions/common/saved_search.test.ts @@ -5,7 +5,7 @@ */ jest.mock('ui/new_platform'); import { savedSearch } from './saved_search'; -import { buildEmbeddableFilters } from '../../../server/lib/build_embeddable_filters'; +import { buildEmbeddableFilters } from '../../../public/lib/build_embeddable_filters'; const filterContext = { and: [ diff --git a/x-pack/legacy/plugins/canvas/canvas_plugin_src/functions/common/saved_search.ts b/x-pack/legacy/plugins/canvas/canvas_plugin_src/functions/common/saved_search.ts index a351bcb46cdd3f..87dc7eb5e814cf 100644 --- a/x-pack/legacy/plugins/canvas/canvas_plugin_src/functions/common/saved_search.ts +++ b/x-pack/legacy/plugins/canvas/canvas_plugin_src/functions/common/saved_search.ts @@ -12,7 +12,7 @@ import { EmbeddableExpression, } from '../../expression_types'; -import { buildEmbeddableFilters } from '../../../server/lib/build_embeddable_filters'; +import { buildEmbeddableFilters } from '../../../public/lib/build_embeddable_filters'; import { Filter } from '../../../types'; import { getFunctionHelp } from '../../../i18n'; diff --git a/x-pack/legacy/plugins/canvas/canvas_plugin_src/functions/common/saved_visualization.test.ts b/x-pack/legacy/plugins/canvas/canvas_plugin_src/functions/common/saved_visualization.test.ts index 49b4b77de763b3..9c3e80bc22af18 100644 --- a/x-pack/legacy/plugins/canvas/canvas_plugin_src/functions/common/saved_visualization.test.ts +++ b/x-pack/legacy/plugins/canvas/canvas_plugin_src/functions/common/saved_visualization.test.ts @@ -5,7 +5,7 @@ */ jest.mock('ui/new_platform'); import { savedVisualization } from './saved_visualization'; -import { buildEmbeddableFilters } from '../../../server/lib/build_embeddable_filters'; +import { buildEmbeddableFilters } from '../../../public/lib/build_embeddable_filters'; const filterContext = { and: [ diff --git a/x-pack/legacy/plugins/canvas/canvas_plugin_src/functions/common/saved_visualization.ts b/x-pack/legacy/plugins/canvas/canvas_plugin_src/functions/common/saved_visualization.ts index 0315a1f480911b..5b612b7cbd6667 100644 --- a/x-pack/legacy/plugins/canvas/canvas_plugin_src/functions/common/saved_visualization.ts +++ b/x-pack/legacy/plugins/canvas/canvas_plugin_src/functions/common/saved_visualization.ts @@ -11,7 +11,7 @@ import { EmbeddableExpressionType, EmbeddableExpression, } from '../../expression_types'; -import { buildEmbeddableFilters } from '../../../server/lib/build_embeddable_filters'; +import { buildEmbeddableFilters } from '../../../public/lib/build_embeddable_filters'; import { Filter } from '../../../types'; import { getFunctionHelp } from '../../../i18n'; diff --git a/x-pack/legacy/plugins/canvas/public/application.tsx b/x-pack/legacy/plugins/canvas/public/application.tsx index ff22d68772efe2..9bdc8e6308e07d 100644 --- a/x-pack/legacy/plugins/canvas/public/application.tsx +++ b/x-pack/legacy/plugins/canvas/public/application.tsx @@ -23,7 +23,7 @@ export const renderApp = ( canvasStore: Store ) => { ReactDOM.render( - + diff --git a/x-pack/legacy/plugins/canvas/public/lib/build_bool_array.js b/x-pack/legacy/plugins/canvas/public/lib/build_bool_array.js new file mode 100644 index 00000000000000..2dc64477535263 --- /dev/null +++ b/x-pack/legacy/plugins/canvas/public/lib/build_bool_array.js @@ -0,0 +1,21 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { getESFilter } from './get_es_filter'; + +const compact = arr => (Array.isArray(arr) ? arr.filter(val => Boolean(val)) : []); + +export function buildBoolArray(canvasQueryFilterArray) { + return compact( + canvasQueryFilterArray.map(clause => { + try { + return getESFilter(clause); + } catch (e) { + return; + } + }) + ); +} diff --git a/x-pack/legacy/plugins/canvas/server/lib/build_embeddable_filters.test.ts b/x-pack/legacy/plugins/canvas/public/lib/build_embeddable_filters.test.ts similarity index 100% rename from x-pack/legacy/plugins/canvas/server/lib/build_embeddable_filters.test.ts rename to x-pack/legacy/plugins/canvas/public/lib/build_embeddable_filters.test.ts diff --git a/x-pack/legacy/plugins/canvas/server/lib/build_embeddable_filters.ts b/x-pack/legacy/plugins/canvas/public/lib/build_embeddable_filters.ts similarity index 73% rename from x-pack/legacy/plugins/canvas/server/lib/build_embeddable_filters.ts rename to x-pack/legacy/plugins/canvas/public/lib/build_embeddable_filters.ts index 05d4c6570bcfbf..1a5d2119a94b67 100644 --- a/x-pack/legacy/plugins/canvas/server/lib/build_embeddable_filters.ts +++ b/x-pack/legacy/plugins/canvas/public/lib/build_embeddable_filters.ts @@ -7,17 +7,11 @@ import { Filter } from '../../types'; // @ts-ignore Untyped Local import { buildBoolArray } from './build_bool_array'; - -// TODO: We should be importing from `data/server` below instead of `data/common`, but -// need to keep `data/common` since the contents of this file are currently imported -// by the browser. This file should probably be refactored so that the pieces required -// on the client live in a `public` directory instead. See kibana/issues/52343 -// eslint-disable-next-line @kbn/eslint/no-restricted-paths import { TimeRange, esFilters, Filter as DataFilter, -} from '../../../../../../src/plugins/data/server'; +} from '../../../../../../src/plugins/data/public'; export interface EmbeddableFilterInput { filters: DataFilter[]; diff --git a/x-pack/legacy/plugins/canvas/public/lib/filters.js b/x-pack/legacy/plugins/canvas/public/lib/filters.js new file mode 100644 index 00000000000000..afa58c7ee30c2d --- /dev/null +++ b/x-pack/legacy/plugins/canvas/public/lib/filters.js @@ -0,0 +1,38 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +/* + TODO: This could be pluggable +*/ + +export function time(filter) { + if (!filter.column) { + throw new Error('column is required for Elasticsearch range filters'); + } + return { + range: { + [filter.column]: { gte: filter.from, lte: filter.to }, + }, + }; +} + +export function luceneQueryString(filter) { + return { + query_string: { + query: filter.query || '*', + }, + }; +} + +export function exactly(filter) { + return { + term: { + [filter.column]: { + value: filter.value, + }, + }, + }; +} diff --git a/x-pack/legacy/plugins/canvas/public/lib/get_es_filter.js b/x-pack/legacy/plugins/canvas/public/lib/get_es_filter.js new file mode 100644 index 00000000000000..e8a4d704118e8b --- /dev/null +++ b/x-pack/legacy/plugins/canvas/public/lib/get_es_filter.js @@ -0,0 +1,26 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +/* + boolArray is the array of bool filter clauses to push filters into. Usually this would be + the value of must, should or must_not. + filter is the abstracted canvas filter. +*/ + +/*eslint import/namespace: ['error', { allowComputed: true }]*/ +import * as filters from './filters'; + +export function getESFilter(filter) { + if (!filters[filter.type]) { + throw new Error(`Unknown filter type: ${filter.type}`); + } + + try { + return filters[filter.type](filter); + } catch (e) { + throw new Error(`Could not create elasticsearch filter from ${filter.type}`); + } +} diff --git a/x-pack/legacy/plugins/lens/public/plugin.tsx b/x-pack/legacy/plugins/lens/public/plugin.tsx index 7f96268fc2e8c6..7afe6d7abedc0e 100644 --- a/x-pack/legacy/plugins/lens/public/plugin.tsx +++ b/x-pack/legacy/plugins/lens/public/plugin.tsx @@ -103,7 +103,7 @@ export class LensPlugin { this.datatableVisualization.setup(core, dependencies); this.metricVisualization.setup(core, dependencies); - visualizations.types.registerAlias(getLensAliasConfig()); + visualizations.registerAlias(getLensAliasConfig()); kibanaLegacy.registerLegacyApp({ id: 'lens', diff --git a/x-pack/legacy/plugins/maps/public/register_vis_type_alias.js b/x-pack/legacy/plugins/maps/public/register_vis_type_alias.js index b0e62d37fbf1cf..4d87b6a0558020 100644 --- a/x-pack/legacy/plugins/maps/public/register_vis_type_alias.js +++ b/x-pack/legacy/plugins/maps/public/register_vis_type_alias.js @@ -23,7 +23,7 @@ The Maps app offers more functionality and is easier to use.`, } ); -visualizationsSetup.types.registerAlias({ +visualizationsSetup.registerAlias({ aliasUrl: MAP_BASE_URL, name: APP_ID, title: i18n.translate('xpack.maps.visTypeAlias.title', { @@ -37,5 +37,5 @@ visualizationsSetup.types.registerAlias({ }); if (!showMapVisualizationTypes) { - visualizationsSetup.types.hideTypes(['region_map', 'tile_map']); + visualizationsSetup.hideTypes(['region_map', 'tile_map']); } diff --git a/x-pack/legacy/plugins/ml/public/application/components/anomalies_table/anomalies_table_columns.js b/x-pack/legacy/plugins/ml/public/application/components/anomalies_table/anomalies_table_columns.js index 23a40d9ecf295a..0c6c959927140a 100644 --- a/x-pack/legacy/plugins/ml/public/application/components/anomalies_table/anomalies_table_columns.js +++ b/x-pack/legacy/plugins/ml/public/application/components/anomalies_table/anomalies_table_columns.js @@ -281,7 +281,7 @@ export function getColumns( defaultMessage: 'actions', }), render: item => { - if (showLinksMenuForItem(item) === true) { + if (showLinksMenuForItem(item, showViewSeriesLink) === true) { return ( { + del(userDataDir, { force: true }).catch(error => { logger.error(`error deleting user data directory at [${userDataDir}]: [${error}]`); }); }); @@ -216,17 +217,35 @@ export class HeadlessChromiumDriverFactory { } getPageExit(browser: Browser, page: Page) { - const pageError$ = Rx.fromEvent(page, 'error').pipe(mergeMap(err => Rx.throwError(err))); + const pageError$ = Rx.fromEvent(page, 'error').pipe( + mergeMap(err => { + return Rx.throwError( + i18n.translate('xpack.reporting.browsers.chromium.errorDetected', { + defaultMessage: 'Reporting detected an error: {err}', + values: { err: err.toString() }, + }) + ); + }) + ); const uncaughtExceptionPageError$ = Rx.fromEvent(page, 'pageerror').pipe( - mergeMap(err => Rx.throwError(err)) + mergeMap(err => { + return Rx.throwError( + i18n.translate('xpack.reporting.browsers.chromium.pageErrorDetected', { + defaultMessage: `Reporting detected an error on the page: {err}`, + values: { err: err.toString() }, + }) + ); + }) ); const browserDisconnect$ = Rx.fromEvent(browser, 'disconnected').pipe( mergeMap(() => Rx.throwError( new Error( - `Puppeteer was disconnected from the Chromium instance! Chromium has closed or crashed.` + i18n.translate('xpack.reporting.browsers.chromium.chromiumClosed', { + defaultMessage: `Reporting detected that Chromium has closed.`, + }) ) ) ) diff --git a/x-pack/legacy/plugins/reporting/server/browsers/download/clean.ts b/x-pack/legacy/plugins/reporting/server/browsers/download/clean.ts index 4355a6a0a17732..a2d1fc7f91a290 100644 --- a/x-pack/legacy/plugins/reporting/server/browsers/download/clean.ts +++ b/x-pack/legacy/plugins/reporting/server/browsers/download/clean.ts @@ -31,7 +31,7 @@ export async function clean(dir: string, expectedPaths: string[]) { const path = resolvePath(dir, filename); if (!expectedPaths.includes(path)) { log(`Deleting unexpected file ${path}`); - await del(path); + await del(path, { force: true }); } }); } diff --git a/x-pack/legacy/plugins/siem/cypress/integration/signal_detection_rules.spec.ts b/x-pack/legacy/plugins/siem/cypress/integration/signal_detection_rules.spec.ts new file mode 100644 index 00000000000000..f2ed9d48daaf63 --- /dev/null +++ b/x-pack/legacy/plugins/siem/cypress/integration/signal_detection_rules.spec.ts @@ -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; + * you may not use this file except in compliance with the Elastic License. + */ + +import { ELASTIC_RULES_BTN, RULES_TABLE, RULES_ROW } from '../screens/signal_detection_rules'; + +import { + changeToThreeHundredRowsPerPage, + loadPrebuiltDetectionRules, + waitForLoadElasticPrebuiltDetectionRulesTableToBeLoaded, + waitForPrebuiltDetectionRulesToBeLoaded, + waitForRulesToBeLoaded, +} from '../tasks/signal_detection_rules'; +import { + goToManageSignalDetectionRules, + waitForSignalsIndexToBeCreated, + waitForSignalsPanelToBeLoaded, +} from '../tasks/detections'; +import { loginAndWaitForPageWithoutDateRange } from '../tasks/login'; + +import { DETECTIONS } from '../urls/navigation'; + +describe('Signal detection rules', () => { + before(() => { + loginAndWaitForPageWithoutDateRange(DETECTIONS); + }); + it('Loads prebuilt rules', () => { + waitForSignalsPanelToBeLoaded(); + waitForSignalsIndexToBeCreated(); + goToManageSignalDetectionRules(); + waitForLoadElasticPrebuiltDetectionRulesTableToBeLoaded(); + loadPrebuiltDetectionRules(); + waitForPrebuiltDetectionRulesToBeLoaded(); + + const expectedElasticRulesBtnText = 'Elastic rules (92)'; + cy.get(ELASTIC_RULES_BTN) + .invoke('text') + .should('eql', expectedElasticRulesBtnText); + + changeToThreeHundredRowsPerPage(); + waitForRulesToBeLoaded(); + + const expectedNumberOfRules = 92; + cy.get(RULES_TABLE).then($table => { + cy.wrap($table.find(RULES_ROW).length).should('eql', expectedNumberOfRules); + }); + }); +}); diff --git a/x-pack/legacy/plugins/siem/cypress/screens/detections.ts b/x-pack/legacy/plugins/siem/cypress/screens/detections.ts new file mode 100644 index 00000000000000..8089b028a10d48 --- /dev/null +++ b/x-pack/legacy/plugins/siem/cypress/screens/detections.ts @@ -0,0 +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; + * you may not use this file except in compliance with the Elastic License. + */ + +export const LOADING_SIGNALS_PANEL = '[data-test-subj="loading-signals-panel"]'; + +export const MANAGE_SIGNAL_DETECTION_RULES_BTN = '[data-test-subj="manage-signal-detection-rules"]'; diff --git a/x-pack/legacy/plugins/siem/cypress/screens/signal_detection_rules.ts b/x-pack/legacy/plugins/siem/cypress/screens/signal_detection_rules.ts new file mode 100644 index 00000000000000..bfaa86e83f301c --- /dev/null +++ b/x-pack/legacy/plugins/siem/cypress/screens/signal_detection_rules.ts @@ -0,0 +1,22 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +export const ELASTIC_RULES_BTN = '[data-test-subj="show-elastic-rules-filter-button"]'; + +export const LOAD_PREBUILT_RULES_BTN = '[data-test-subj="load-prebuilt-rules"]'; + +export const LOADING_INITIAL_PREBUILT_RULES_TABLE = + '[data-test-subj="initialLoadingPanelAllRulesTable"]'; + +export const LOADING_SPINNER = '[data-test-subj="loading-spinner"]'; + +export const PAGINATION_POPOVER_BTN = '[data-test-subj="tablePaginationPopoverButton"]'; + +export const RULES_TABLE = '[data-test-subj="rules-table"]'; + +export const RULES_ROW = '.euiTableRow'; + +export const THREE_HUNDRED_ROWS = '[data-test-subj="tablePagination-300-rows"]'; diff --git a/x-pack/legacy/plugins/siem/cypress/tasks/detections.ts b/x-pack/legacy/plugins/siem/cypress/tasks/detections.ts new file mode 100644 index 00000000000000..4a0a565a74e27d --- /dev/null +++ b/x-pack/legacy/plugins/siem/cypress/tasks/detections.ts @@ -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; + * you may not use this file except in compliance with the Elastic License. + */ + +import { LOADING_SIGNALS_PANEL, MANAGE_SIGNAL_DETECTION_RULES_BTN } from '../screens/detections'; + +export const goToManageSignalDetectionRules = () => { + cy.get(MANAGE_SIGNAL_DETECTION_RULES_BTN) + .should('exist') + .click({ force: true }); +}; + +export const waitForSignalsIndexToBeCreated = () => { + cy.request({ url: '/api/detection_engine/index', retryOnStatusCodeFailure: true }).then( + response => { + if (response.status !== 200) { + cy.wait(7500); + } + } + ); +}; + +export const waitForSignalsPanelToBeLoaded = () => { + cy.get(LOADING_SIGNALS_PANEL).should('exist'); + cy.get(LOADING_SIGNALS_PANEL).should('not.exist'); +}; diff --git a/x-pack/legacy/plugins/siem/cypress/tasks/signal_detection_rules.ts b/x-pack/legacy/plugins/siem/cypress/tasks/signal_detection_rules.ts new file mode 100644 index 00000000000000..cc0e4bce1035af --- /dev/null +++ b/x-pack/legacy/plugins/siem/cypress/tasks/signal_detection_rules.ts @@ -0,0 +1,40 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { + LOAD_PREBUILT_RULES_BTN, + LOADING_INITIAL_PREBUILT_RULES_TABLE, + LOADING_SPINNER, + PAGINATION_POPOVER_BTN, + RULES_TABLE, + THREE_HUNDRED_ROWS, +} from '../screens/signal_detection_rules'; + +export const changeToThreeHundredRowsPerPage = () => { + cy.get(PAGINATION_POPOVER_BTN).click({ force: true }); + cy.get(THREE_HUNDRED_ROWS).click(); +}; + +export const loadPrebuiltDetectionRules = () => { + cy.get(LOAD_PREBUILT_RULES_BTN) + .should('exist') + .click({ force: true }); +}; + +export const waitForLoadElasticPrebuiltDetectionRulesTableToBeLoaded = () => { + cy.get(LOADING_INITIAL_PREBUILT_RULES_TABLE).should('exist'); + cy.get(LOADING_INITIAL_PREBUILT_RULES_TABLE).should('not.exist'); +}; + +export const waitForPrebuiltDetectionRulesToBeLoaded = () => { + cy.get(LOAD_PREBUILT_RULES_BTN).should('not.exist'); + cy.get(RULES_TABLE).should('exist'); +}; + +export const waitForRulesToBeLoaded = () => { + cy.get(LOADING_SPINNER).should('exist'); + cy.get(LOADING_SPINNER).should('not.exist'); +}; diff --git a/x-pack/legacy/plugins/siem/cypress/urls/navigation.ts b/x-pack/legacy/plugins/siem/cypress/urls/navigation.ts index 8fdc939e7ee51d..5e65e5aa34c186 100644 --- a/x-pack/legacy/plugins/siem/cypress/urls/navigation.ts +++ b/x-pack/legacy/plugins/siem/cypress/urls/navigation.ts @@ -4,6 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ +export const DETECTIONS = 'app/siem#/detections'; export const HOSTS_PAGE = '/app/siem#/hosts/allHosts'; export const HOSTS_PAGE_TAB_URLS = { allHosts: '/app/siem#/hosts/allHosts', diff --git a/x-pack/legacy/plugins/siem/public/components/flyout/pane/index.tsx b/x-pack/legacy/plugins/siem/public/components/flyout/pane/index.tsx index fb977417ffbbfd..38ec4a4b6f1f38 100644 --- a/x-pack/legacy/plugins/siem/public/components/flyout/pane/index.tsx +++ b/x-pack/legacy/plugins/siem/public/components/flyout/pane/index.tsx @@ -49,7 +49,7 @@ const EuiFlyoutContainer = styled.div<{ headerHeight: number }>` .timeline-flyout-body { overflow-y: hidden; padding: 0; - .euiFlyoutBody__overflow { + .euiFlyoutBody__overflowContent { padding: 0; } } diff --git a/x-pack/legacy/plugins/siem/public/components/loader/__snapshots__/index.test.tsx.snap b/x-pack/legacy/plugins/siem/public/components/loader/__snapshots__/index.test.tsx.snap index 0885f15b1efba3..ad2d57b948ba00 100644 --- a/x-pack/legacy/plugins/siem/public/components/loader/__snapshots__/index.test.tsx.snap +++ b/x-pack/legacy/plugins/siem/public/components/loader/__snapshots__/index.test.tsx.snap @@ -16,6 +16,7 @@ exports[`rendering renders correctly 1`] = ` grow={false} > diff --git a/x-pack/legacy/plugins/siem/public/components/loader/index.tsx b/x-pack/legacy/plugins/siem/public/components/loader/index.tsx index be2ce3dde951cd..e78f148418588c 100644 --- a/x-pack/legacy/plugins/siem/public/components/loader/index.tsx +++ b/x-pack/legacy/plugins/siem/public/components/loader/index.tsx @@ -62,7 +62,7 @@ export const Loader = React.memo(({ children, overlay, overlayBackg