diff --git a/NOTICE.txt b/NOTICE.txt index 4ede43610ca7b..1694193892e16 100644 --- a/NOTICE.txt +++ b/NOTICE.txt @@ -295,7 +295,7 @@ MIT License http://www.opensource.org/licenses/mit-license --- This product includes code that is adapted from mapbox-gl-js, which is available under a "BSD-3-Clause" license. -https://github.com/mapbox/mapbox-gl-js/blob/master/src/util/image.js +https://github.com/mapbox/mapbox-gl-js/blob/v1.13.2/src/util/image.js Copyright (c) 2016, Mapbox diff --git a/dev_docs/tutorials/expressions.mdx b/dev_docs/tutorials/expressions.mdx index c4b37a125838e..d9abf3dd57eb8 100644 --- a/dev_docs/tutorials/expressions.mdx +++ b/dev_docs/tutorials/expressions.mdx @@ -57,7 +57,7 @@ const result = await executionContract.getData(); ``` - Check the full spec of execute function [here](https://github.com/elastic/kibana/blob/main/docs/development/plugins/expressions/public/kibana-plugin-plugins-expressions-public.execution.md) + Check the full spec of execute function In addition, on the browser side, there are two additional ways to run expressions and render the results. @@ -71,7 +71,7 @@ This is the easiest way to get expressions rendered inside your application. ``` - Check the full spec of ReactExpressionRenderer component props [here](https://github.com/elastic/kibana/blob/main/docs/development/plugins/expressions/public/kibana-plugin-plugins-expressions-public.reactexpressionrendererprops.md) + Check the full spec of ReactExpressionRenderer component props #### Expression loader @@ -83,7 +83,7 @@ const handler = loader(domElement, expression, params); ``` - Check the full spec of expression loader params [here](https://github.com/elastic/kibana/blob/main/docs/development/plugins/expressions/public/kibana-plugin-plugins-expressions-public.iexpressionloaderparams.md) + Check the full spec of expression loader params ### Creating new expression functions @@ -106,7 +106,7 @@ expressions.registerFunction(functionDefinition); ``` - Check the full interface of ExpressionFuntionDefinition [here](https://github.com/elastic/kibana/blob/main/docs/development/plugins/expressions/public/kibana-plugin-plugins-expressions-public.expressionfunctiondefinition.md) + Check the full interface of ExpressionFuntionDefinition ### Creating new expression renderers @@ -128,5 +128,5 @@ expressions.registerRenderer(rendererDefinition); ``` - Check the full interface of ExpressionRendererDefinition [here](https://github.com/elastic/kibana/blob/main/docs/development/plugins/expressions/public/kibana-plugin-plugins-expressions-public.expressionrenderdefinition.md) + Check the full interface of ExpressionRendererDefinition diff --git a/docs/developer/getting-started/debugging.asciidoc b/docs/developer/getting-started/debugging.asciidoc index f3308a1267386..1254462d2e4ea 100644 --- a/docs/developer/getting-started/debugging.asciidoc +++ b/docs/developer/getting-started/debugging.asciidoc @@ -130,71 +130,3 @@ Once you're finished, you can stop Kibana normally, then stop the {es} and APM s ---- ./scripts/compose.py stop ---- - -=== Using {kib} server logs -{kib} Logs is a great way to see what's going on in your application and to debug performance issues. Navigating through a large number of generated logs can be overwhelming, and following are some techniques that you can use to optimize the process. - -Start by defining a problem area that you are interested in. For example, you might be interested in seeing how a particular {kib} Plugin is performing, so no need to gather logs for all of {kib}. Or you might want to focus on a particular feature, such as requests from the {kib} server to the {es} server. -Depending on your needs, you can configure {kib} to generate logs for a specific feature. -[source,yml] ----- -logging: - appenders: - file: - type: file - fileName: ./kibana.log - layout: - type: json - -### gather all the Kibana logs into a file -logging.root: - appenders: [file] - level: all - -### or gather a subset of the logs -logging.loggers: - ### responses to an HTTP request - - name: http.server.response - level: debug - appenders: [file] - ### result of a query to the Elasticsearch server - - name: elasticsearch.query - level: debug - appenders: [file] - ### logs generated by my plugin - - name: plugins.myPlugin - level: debug - appenders: [file] ----- -WARNING: Kibana's `file` appender is configured to produce logs in https://www.elastic.co/guide/en/ecs/master/ecs-reference.html[ECS JSON] format. It's the only format that includes the meta information necessary for https://www.elastic.co/guide/en/apm/agent/nodejs/current/log-correlation.html[log correlation] out-of-the-box. - -The next step is to define what https://www.elastic.co/observability[observability tools] are available. -For a better experience, set up an https://www.elastic.co/guide/en/apm/get-started/current/observability-integrations.html[Observability integration] provided by Elastic to debug your application with the <> -To debug something quickly without setting up additional tooling, you can work with <> - -[[debugging-logs-apm-ui]] -==== APM UI -*Prerequisites* {kib} logs are configured to be in https://www.elastic.co/guide/en/ecs/master/ecs-reference.html[ECS JSON] format to include tracing identifiers. - -To debug {kib} with the APM UI, you must set up the APM infrastructure. You can find instructions for the setup process -https://www.elastic.co/guide/en/apm/get-started/current/observability-integrations.html[on the Observability integrations page]. - -Once you set up the APM infrastructure, you can enable the APM agent and put {kib} under load to collect APM events. To analyze the collected metrics and logs, use the APM UI as demonstrated https://www.elastic.co/guide/en/kibana/master/transactions.html#transaction-trace-sample[in the docs]. - -[[plain-kibana-logs]] -==== Plain {kib} logs -*Prerequisites* {kib} logs are configured to be in https://www.elastic.co/guide/en/ecs/master/ecs-reference.html[ECS JSON] format to include tracing identifiers. - -Open {kib} Logs and search for an operation you are interested in. -For example, suppose you want to investigate the response times for queries to the `/api/telemetry/v2/clusters/_stats` {kib} endpoint. -Open Kibana Logs and search for the HTTP server response for the endpoint. It looks similar to the following (some fields are omitted for brevity). -[source,json] ----- -{ - "message":"POST /api/telemetry/v2/clusters/_stats 200 1014ms - 43.2KB", - "log":{"level":"DEBUG","logger":"http.server.response"}, - "trace":{"id":"9b99131a6f66587971ef085ef97dfd07"}, - "transaction":{"id":"d0c5bbf14f5febca"} -} ----- -You are interested in the https://www.elastic.co/guide/en/ecs/current/ecs-tracing.html#field-trace-id[trace.id] field, which is a unique identifier of a trace. The `trace.id` provides a way to group multiple events, like transactions, which belong together. You can search for `"trace":{"id":"9b99131a6f66587971ef085ef97dfd07"}` to get all the logs that belong to the same trace. This enables you to see how many {es} requests were triggered during the `9b99131a6f66587971ef085ef97dfd07` trace, what they looked like, what {es} endpoints were hit, and so on. diff --git a/docs/development/core/public/kibana-plugin-core-public.doclinksstart.links.md b/docs/development/core/public/kibana-plugin-core-public.doclinksstart.links.md index 8f6f1f6c98ab2..63c29df44019d 100644 --- a/docs/development/core/public/kibana-plugin-core-public.doclinksstart.links.md +++ b/docs/development/core/public/kibana-plugin-core-public.doclinksstart.links.md @@ -290,7 +290,14 @@ readonly links: { }>; readonly watcher: Record; readonly ccs: Record; - readonly plugins: Record; + readonly plugins: { + azureRepo: string; + gcsRepo: string; + hdfsRepo: string; + s3Repo: string; + snapshotRestoreRepos: string; + mapperSize: string; + }; readonly snapshotRestore: Record; readonly ingest: Record; readonly fleet: Readonly<{ diff --git a/docs/development/core/public/kibana-plugin-core-public.doclinksstart.md b/docs/development/core/public/kibana-plugin-core-public.doclinksstart.md index a9828f04672e9..b60f9ad17e9c4 100644 --- a/docs/development/core/public/kibana-plugin-core-public.doclinksstart.md +++ b/docs/development/core/public/kibana-plugin-core-public.doclinksstart.md @@ -17,5 +17,5 @@ export interface DocLinksStart | --- | --- | --- | | [DOC\_LINK\_VERSION](./kibana-plugin-core-public.doclinksstart.doc_link_version.md) | string | | | [ELASTIC\_WEBSITE\_URL](./kibana-plugin-core-public.doclinksstart.elastic_website_url.md) | string | | -| [links](./kibana-plugin-core-public.doclinksstart.links.md) | { readonly settings: string; readonly elasticStackGetStarted: string; readonly upgrade: { readonly upgradingElasticStack: string; }; readonly apm: { readonly kibanaSettings: string; readonly supportedServiceMaps: string; readonly customLinks: string; readonly droppedTransactionSpans: string; readonly upgrading: string; readonly metaData: string; }; readonly canvas: { readonly guide: string; }; readonly cloud: { readonly indexManagement: string; }; readonly dashboard: { readonly guide: string; readonly drilldowns: string; readonly drilldownsTriggerPicker: string; readonly urlDrilldownTemplateSyntax: string; readonly urlDrilldownVariables: string; }; readonly discover: Record<string, string>; readonly filebeat: { readonly base: string; readonly installation: string; readonly configuration: string; readonly elasticsearchOutput: string; readonly elasticsearchModule: string; readonly startup: string; readonly exportedFields: string; readonly suricataModule: string; readonly zeekModule: string; }; readonly auditbeat: { readonly base: string; readonly auditdModule: string; readonly systemModule: string; }; readonly metricbeat: { readonly base: string; readonly configure: string; readonly httpEndpoint: string; readonly install: string; readonly start: string; }; readonly appSearch: { readonly apiRef: string; readonly apiClients: string; readonly apiKeys: string; readonly authentication: string; readonly crawlRules: string; readonly curations: string; readonly duplicateDocuments: string; readonly entryPoints: string; readonly guide: string; readonly indexingDocuments: string; readonly indexingDocumentsSchema: string; readonly logSettings: string; readonly metaEngines: string; readonly recisionTuning: string; readonly relevanceTuning: string; readonly resultSettings: string; readonly searchUI: string; readonly security: string; readonly synonyms: string; readonly webCrawler: string; readonly webCrawlerEventLogs: string; }; readonly enterpriseSearch: { readonly configuration: string; readonly licenseManagement: string; readonly mailService: string; readonly usersAccess: string; }; readonly workplaceSearch: { readonly apiKeys: string; readonly box: string; readonly confluenceCloud: string; readonly confluenceServer: string; readonly customSources: string; readonly customSourcePermissions: string; readonly documentPermissions: string; readonly dropbox: string; readonly externalIdentities: string; readonly gitHub: string; readonly gettingStarted: string; readonly gmail: string; readonly googleDrive: string; readonly indexingSchedule: string; readonly jiraCloud: string; readonly jiraServer: string; readonly oneDrive: string; readonly permissions: string; readonly salesforce: string; readonly security: string; readonly serviceNow: string; readonly sharePoint: string; readonly slack: string; readonly synch: string; readonly zendesk: string; }; readonly heartbeat: { readonly base: string; }; readonly libbeat: { readonly getStarted: string; }; readonly logstash: { readonly base: string; }; readonly functionbeat: { readonly base: string; }; readonly winlogbeat: { readonly base: string; }; readonly aggs: { readonly composite: string; readonly composite\_missing\_bucket: string; readonly date\_histogram: string; readonly date\_range: string; readonly date\_format\_pattern: string; readonly filter: string; readonly filters: string; readonly geohash\_grid: string; readonly histogram: string; readonly ip\_range: string; readonly range: string; readonly significant\_terms: string; readonly terms: string; readonly terms\_doc\_count\_error: string; readonly avg: string; readonly avg\_bucket: string; readonly max\_bucket: string; readonly min\_bucket: string; readonly sum\_bucket: string; readonly cardinality: string; readonly count: string; readonly cumulative\_sum: string; readonly derivative: string; readonly geo\_bounds: string; readonly geo\_centroid: string; readonly max: string; readonly median: string; readonly min: string; readonly moving\_avg: string; readonly percentile\_ranks: string; readonly serial\_diff: string; readonly std\_dev: string; readonly sum: string; readonly top\_hits: string; }; readonly runtimeFields: { readonly overview: string; readonly mapping: string; }; readonly scriptedFields: { readonly scriptFields: string; readonly scriptAggs: string; readonly painless: string; readonly painlessApi: string; readonly painlessLangSpec: string; readonly painlessSyntax: string; readonly painlessWalkthrough: string; readonly luceneExpressions: string; }; readonly search: { readonly sessions: string; readonly sessionLimits: string; }; readonly indexPatterns: { readonly introduction: string; readonly fieldFormattersNumber: string; readonly fieldFormattersString: string; readonly runtimeFields: string; }; readonly addData: string; readonly kibana: string; readonly upgradeAssistant: { readonly overview: string; readonly batchReindex: string; readonly remoteReindex: string; }; readonly rollupJobs: string; readonly elasticsearch: Record<string, string>; readonly siem: { readonly privileges: string; readonly guide: string; readonly gettingStarted: string; readonly ml: string; readonly ruleChangeLog: string; readonly detectionsReq: string; readonly networkMap: string; readonly troubleshootGaps: string; }; readonly securitySolution: { readonly trustedApps: string; }; readonly query: { readonly eql: string; readonly kueryQuerySyntax: string; readonly luceneQuerySyntax: string; readonly percolate: string; readonly queryDsl: string; }; readonly date: { readonly dateMath: string; readonly dateMathIndexNames: string; }; readonly management: Record<string, string>; readonly ml: Record<string, string>; readonly transforms: Record<string, string>; readonly visualize: Record<string, string>; readonly apis: Readonly<{ bulkIndexAlias: string; byteSizeUnits: string; createAutoFollowPattern: string; createFollower: string; createIndex: string; createSnapshotLifecyclePolicy: string; createRoleMapping: string; createRoleMappingTemplates: string; createRollupJobsRequest: string; createApiKey: string; createPipeline: string; createTransformRequest: string; cronExpressions: string; executeWatchActionModes: string; indexExists: string; openIndex: string; putComponentTemplate: string; painlessExecute: string; painlessExecuteAPIContexts: string; putComponentTemplateMetadata: string; putSnapshotLifecyclePolicy: string; putIndexTemplateV1: string; putWatch: string; simulatePipeline: string; timeUnits: string; updateTransform: string; }>; readonly observability: Readonly<{ guide: string; infrastructureThreshold: string; logsThreshold: string; metricsThreshold: string; monitorStatus: string; monitorUptime: string; tlsCertificate: string; uptimeDurationAnomaly: string; }>; readonly alerting: Record<string, string>; readonly maps: Readonly<{ guide: string; importGeospatialPrivileges: string; gdalTutorial: string; }>; readonly monitoring: Record<string, string>; readonly security: Readonly<{ apiKeyServiceSettings: string; clusterPrivileges: string; elasticsearchSettings: string; elasticsearchEnableSecurity: string; elasticsearchEnableApiKeys: string; indicesPrivileges: string; kibanaTLS: string; kibanaPrivileges: string; mappingRoles: string; mappingRolesFieldRules: string; runAsPrivilege: string; }>; readonly spaces: Readonly<{ kibanaLegacyUrlAliases: string; kibanaDisableLegacyUrlAliasesApi: string; }>; readonly watcher: Record<string, string>; readonly ccs: Record<string, string>; readonly plugins: Record<string, string>; readonly snapshotRestore: Record<string, string>; readonly ingest: Record<string, string>; readonly fleet: Readonly<{ beatsAgentComparison: string; guide: string; fleetServer: string; fleetServerAddFleetServer: string; settings: string; settingsFleetServerHostSettings: string; settingsFleetServerProxySettings: string; troubleshooting: string; elasticAgent: string; datastreams: string; datastreamsNamingScheme: string; installElasticAgent: string; installElasticAgentStandalone: string; upgradeElasticAgent: string; upgradeElasticAgent712lower: string; learnMoreBlog: string; apiKeysLearnMore: string; onPremRegistry: string; }>; readonly ecs: { readonly guide: string; }; readonly clients: { readonly guide: string; readonly goOverview: string; readonly javaIndex: string; readonly jsIntro: string; readonly netGuide: string; readonly perlGuide: string; readonly phpGuide: string; readonly pythonGuide: string; readonly rubyOverview: string; readonly rustGuide: string; }; readonly endpoints: { readonly troubleshooting: string; }; } | | +| [links](./kibana-plugin-core-public.doclinksstart.links.md) | { readonly settings: string; readonly elasticStackGetStarted: string; readonly upgrade: { readonly upgradingElasticStack: string; }; readonly apm: { readonly kibanaSettings: string; readonly supportedServiceMaps: string; readonly customLinks: string; readonly droppedTransactionSpans: string; readonly upgrading: string; readonly metaData: string; }; readonly canvas: { readonly guide: string; }; readonly cloud: { readonly indexManagement: string; }; readonly dashboard: { readonly guide: string; readonly drilldowns: string; readonly drilldownsTriggerPicker: string; readonly urlDrilldownTemplateSyntax: string; readonly urlDrilldownVariables: string; }; readonly discover: Record<string, string>; readonly filebeat: { readonly base: string; readonly installation: string; readonly configuration: string; readonly elasticsearchOutput: string; readonly elasticsearchModule: string; readonly startup: string; readonly exportedFields: string; readonly suricataModule: string; readonly zeekModule: string; }; readonly auditbeat: { readonly base: string; readonly auditdModule: string; readonly systemModule: string; }; readonly metricbeat: { readonly base: string; readonly configure: string; readonly httpEndpoint: string; readonly install: string; readonly start: string; }; readonly appSearch: { readonly apiRef: string; readonly apiClients: string; readonly apiKeys: string; readonly authentication: string; readonly crawlRules: string; readonly curations: string; readonly duplicateDocuments: string; readonly entryPoints: string; readonly guide: string; readonly indexingDocuments: string; readonly indexingDocumentsSchema: string; readonly logSettings: string; readonly metaEngines: string; readonly precisionTuning: string; readonly relevanceTuning: string; readonly resultSettings: string; readonly searchUI: string; readonly security: string; readonly synonyms: string; readonly webCrawler: string; readonly webCrawlerEventLogs: string; }; readonly enterpriseSearch: { readonly configuration: string; readonly licenseManagement: string; readonly mailService: string; readonly usersAccess: string; }; readonly workplaceSearch: { readonly apiKeys: string; readonly box: string; readonly confluenceCloud: string; readonly confluenceServer: string; readonly customSources: string; readonly customSourcePermissions: string; readonly documentPermissions: string; readonly dropbox: string; readonly externalIdentities: string; readonly gitHub: string; readonly gettingStarted: string; readonly gmail: string; readonly googleDrive: string; readonly indexingSchedule: string; readonly jiraCloud: string; readonly jiraServer: string; readonly oneDrive: string; readonly permissions: string; readonly salesforce: string; readonly security: string; readonly serviceNow: string; readonly sharePoint: string; readonly slack: string; readonly synch: string; readonly zendesk: string; }; readonly heartbeat: { readonly base: string; }; readonly libbeat: { readonly getStarted: string; }; readonly logstash: { readonly base: string; }; readonly functionbeat: { readonly base: string; }; readonly winlogbeat: { readonly base: string; }; readonly aggs: { readonly composite: string; readonly composite\_missing\_bucket: string; readonly date\_histogram: string; readonly date\_range: string; readonly date\_format\_pattern: string; readonly filter: string; readonly filters: string; readonly geohash\_grid: string; readonly histogram: string; readonly ip\_range: string; readonly range: string; readonly significant\_terms: string; readonly terms: string; readonly terms\_doc\_count\_error: string; readonly avg: string; readonly avg\_bucket: string; readonly max\_bucket: string; readonly min\_bucket: string; readonly sum\_bucket: string; readonly cardinality: string; readonly count: string; readonly cumulative\_sum: string; readonly derivative: string; readonly geo\_bounds: string; readonly geo\_centroid: string; readonly max: string; readonly median: string; readonly min: string; readonly moving\_avg: string; readonly percentile\_ranks: string; readonly serial\_diff: string; readonly std\_dev: string; readonly sum: string; readonly top\_hits: string; }; readonly runtimeFields: { readonly overview: string; readonly mapping: string; }; readonly scriptedFields: { readonly scriptFields: string; readonly scriptAggs: string; readonly painless: string; readonly painlessApi: string; readonly painlessLangSpec: string; readonly painlessSyntax: string; readonly painlessWalkthrough: string; readonly luceneExpressions: string; }; readonly search: { readonly sessions: string; readonly sessionLimits: string; }; readonly indexPatterns: { readonly introduction: string; readonly fieldFormattersNumber: string; readonly fieldFormattersString: string; readonly runtimeFields: string; }; readonly addData: string; readonly kibana: string; readonly upgradeAssistant: { readonly overview: string; readonly batchReindex: string; readonly remoteReindex: string; }; readonly rollupJobs: string; readonly elasticsearch: Record<string, string>; readonly siem: { readonly privileges: string; readonly guide: string; readonly gettingStarted: string; readonly ml: string; readonly ruleChangeLog: string; readonly detectionsReq: string; readonly networkMap: string; readonly troubleshootGaps: string; }; readonly securitySolution: { readonly trustedApps: string; }; readonly query: { readonly eql: string; readonly kueryQuerySyntax: string; readonly luceneQuerySyntax: string; readonly percolate: string; readonly queryDsl: string; }; readonly date: { readonly dateMath: string; readonly dateMathIndexNames: string; }; readonly management: Record<string, string>; readonly ml: Record<string, string>; readonly transforms: Record<string, string>; readonly visualize: Record<string, string>; readonly apis: Readonly<{ bulkIndexAlias: string; byteSizeUnits: string; createAutoFollowPattern: string; createFollower: string; createIndex: string; createSnapshotLifecyclePolicy: string; createRoleMapping: string; createRoleMappingTemplates: string; createRollupJobsRequest: string; createApiKey: string; createPipeline: string; createTransformRequest: string; cronExpressions: string; executeWatchActionModes: string; indexExists: string; openIndex: string; putComponentTemplate: string; painlessExecute: string; painlessExecuteAPIContexts: string; putComponentTemplateMetadata: string; putSnapshotLifecyclePolicy: string; putIndexTemplateV1: string; putWatch: string; simulatePipeline: string; timeUnits: string; updateTransform: string; }>; readonly observability: Readonly<{ guide: string; infrastructureThreshold: string; logsThreshold: string; metricsThreshold: string; monitorStatus: string; monitorUptime: string; tlsCertificate: string; uptimeDurationAnomaly: string; }>; readonly alerting: Record<string, string>; readonly maps: Readonly<{ guide: string; importGeospatialPrivileges: string; gdalTutorial: string; }>; readonly monitoring: Record<string, string>; readonly security: Readonly<{ apiKeyServiceSettings: string; clusterPrivileges: string; elasticsearchSettings: string; elasticsearchEnableSecurity: string; elasticsearchEnableApiKeys: string; indicesPrivileges: string; kibanaTLS: string; kibanaPrivileges: string; mappingRoles: string; mappingRolesFieldRules: string; runAsPrivilege: string; }>; readonly spaces: Readonly<{ kibanaLegacyUrlAliases: string; kibanaDisableLegacyUrlAliasesApi: string; }>; readonly watcher: Record<string, string>; readonly ccs: Record<string, string>; readonly plugins: { azureRepo: string; gcsRepo: string; hdfsRepo: string; s3Repo: string; snapshotRestoreRepos: string; mapperSize: string; }; readonly snapshotRestore: Record<string, string>; readonly ingest: Record<string, string>; readonly fleet: Readonly<{ beatsAgentComparison: string; guide: string; fleetServer: string; fleetServerAddFleetServer: string; settings: string; settingsFleetServerHostSettings: string; settingsFleetServerProxySettings: string; troubleshooting: string; elasticAgent: string; datastreams: string; datastreamsNamingScheme: string; installElasticAgent: string; installElasticAgentStandalone: string; upgradeElasticAgent: string; upgradeElasticAgent712lower: string; learnMoreBlog: string; apiKeysLearnMore: string; onPremRegistry: string; }>; readonly ecs: { readonly guide: string; }; readonly clients: { readonly guide: string; readonly goOverview: string; readonly javaIndex: string; readonly jsIntro: string; readonly netGuide: string; readonly perlGuide: string; readonly phpGuide: string; readonly pythonGuide: string; readonly rubyOverview: string; readonly rustGuide: string; }; readonly endpoints: { readonly troubleshooting: string; }; } | | diff --git a/docs/settings/apm-settings.asciidoc b/docs/settings/apm-settings.asciidoc index 77a250a14f929..27ea7f4dc7cd0 100644 --- a/docs/settings/apm-settings.asciidoc +++ b/docs/settings/apm-settings.asciidoc @@ -101,8 +101,8 @@ Changing these settings may disable features of the APM App. | `xpack.apm.indices.sourcemap` {ess-icon} | Matcher for all source map indices. Defaults to `apm-*`. -| `xpack.apm.autocreateApmIndexPattern` {ess-icon} - | Set to `false` to disable the automatic creation of the APM index pattern when the APM app is opened. Defaults to `true`. +| `xpack.apm.autoCreateApmDataView` {ess-icon} + | Set to `false` to disable the automatic creation of the APM data view when the APM app is opened. Defaults to `true`. |=== -// end::general-apm-settings[] \ No newline at end of file +// end::general-apm-settings[] diff --git a/docs/settings/fleet-settings.asciidoc b/docs/settings/fleet-settings.asciidoc index f0dfeb619bb38..a088f31937cc8 100644 --- a/docs/settings/fleet-settings.asciidoc +++ b/docs/settings/fleet-settings.asciidoc @@ -87,6 +87,7 @@ Optional properties are: `data_output_id`:: ID of the output to send data (Need to be identical to `monitoring_output_id`) `monitoring_output_id`:: ID of the output to send monitoring data. (Need to be identical to `data_output_id`) `package_policies`:: List of integration policies to add to this policy. + `id`::: Unique ID of the integration policy. The ID may be a number or string. `name`::: (required) Name of the integration policy. `package`::: (required) Integration that this policy configures `name`:::: Name of the integration associated with this policy. @@ -128,6 +129,7 @@ xpack.fleet.agentPolicies: - package: name: system name: System Integration + id: preconfigured-system inputs: - type: system/metrics enabled: true diff --git a/docs/settings/task-manager-settings.asciidoc b/docs/settings/task-manager-settings.asciidoc index c61ef83953347..286bb71542b3a 100644 --- a/docs/settings/task-manager-settings.asciidoc +++ b/docs/settings/task-manager-settings.asciidoc @@ -9,51 +9,59 @@ Task Manager runs background tasks by polling for work on an interval. You can [float] [[task-manager-settings]] -==== Task Manager settings +==== Task Manager settings -[cols="2*<"] -|=== -| `xpack.task_manager.max_attempts` - | The maximum number of times a task will be attempted before being abandoned as failed. Defaults to 3. -| `xpack.task_manager.poll_interval` - | How often, in milliseconds, the task manager will look for more work. Defaults to 3000 and cannot be lower than 100. -| `xpack.task_manager.request_capacity` - | How many requests can Task Manager buffer before it rejects new requests. Defaults to 1000. +`xpack.task_manager.max_attempts`:: +The maximum number of times a task will be attempted before being abandoned as failed. Defaults to 3. - | `xpack.task_manager.max_workers` - | The maximum number of tasks that this Kibana instance will run simultaneously. Defaults to 10. - Starting in 8.0, it will not be possible to set the value greater than 100. +`xpack.task_manager.poll_interval`:: +How often, in milliseconds, the task manager will look for more work. Defaults to 3000 and cannot be lower than 100. - | `xpack.task_manager.` - `monitored_stats_health_verbose_log.enabled` - | This flag will enable automatic warn and error logging if task manager self detects a performance issue, such as the time between when a task is scheduled to execute and when it actually executes. Defaults to false. +`xpack.task_manager.request_capacity`:: +How many requests can Task Manager buffer before it rejects new requests. Defaults to 1000. - | `xpack.task_manager.` - `monitored_stats_health_verbose_log.` - `warn_delayed_task_start_in_seconds` - | The amount of seconds we allow a task to delay before printing a warning server log. Defaults to 60. +`xpack.task_manager.max_workers`:: +The maximum number of tasks that this Kibana instance will run simultaneously. Defaults to 10. +Starting in 8.0, it will not be possible to set the value greater than 100. - | `xpack.task_manager.ephemeral_tasks.enabled` - | Enables an experimental feature that executes a limited (and configurable) number of actions in the same task as the alert which triggered them. - These action tasks will reduce the latency of the time it takes an action to run after it's triggered, but are not persisted as SavedObjects. - These non-persisted action tasks have a risk that they won't be run at all if the Kibana instance running them exits unexpectedly. Defaults to false. +`xpack.task_manager.monitored_stats_health_verbose_log.enabled`:: +This flag will enable automatic warn and error logging if task manager self detects a performance issue, such as the time between when a task is scheduled to execute and when it actually executes. Defaults to false. + +`xpack.task_manager.monitored_stats_health_verbose_log.warn_delayed_task_start_in_seconds`:: +The amount of seconds we allow a task to delay before printing a warning server log. Defaults to 60. + +`xpack.task_manager.ephemeral_tasks.enabled`:: +Enables an experimental feature that executes a limited (and configurable) number of actions in the same task as the alert which triggered them. +These action tasks will reduce the latency of the time it takes an action to run after it's triggered, but are not persisted as SavedObjects. +These non-persisted action tasks have a risk that they won't be run at all if the Kibana instance running them exits unexpectedly. Defaults to false. + +`xpack.task_manager.ephemeral_tasks.request_capacity`:: +Sets the size of the ephemeral queue defined above. Defaults to 10. - | `xpack.task_manager.ephemeral_tasks.request_capacity` - | Sets the size of the ephemeral queue defined above. Defaults to 10. -|=== [float] [[task-manager-health-settings]] -==== Task Manager Health settings +==== Task Manager Health settings Settings that configure the <> endpoint. -[cols="2*<"] -|=== -| `xpack.task_manager.` -`monitored_task_execution_thresholds` - | Configures the threshold of failed task executions at which point the `warn` or `error` health status is set under each task type execution status (under `stats.runtime.value.execution.result_frequency_percent_as_number[${task type}].status`). This setting allows configuration of both the default level and a custom task type specific level. By default, this setting is configured to mark the health of every task type as `warning` when it exceeds 80% failed executions, and as `error` at 90%. Custom configurations allow you to reduce this threshold to catch failures sooner for task types that you might consider critical, such as alerting tasks. This value can be set to any number between 0 to 100, and a threshold is hit when the value *exceeds* this number. This means that you can avoid setting the status to `error` by setting the threshold at 100, or hit `error` the moment any task fails by setting the threshold to 0 (as it will exceed 0 once a single failure occurs). - -|=== +`xpack.task_manager.monitored_task_execution_thresholds`:: +Configures the threshold of failed task executions at which point the `warn` or +`error` health status is set under each task type execution status +(under `stats.runtime.value.execution.result_frequency_percent_as_number[${task type}].status`). ++ +This setting allows configuration of both the default level and a +custom task type specific level. By default, this setting is configured to mark +the health of every task type as `warning` when it exceeds 80% failed executions, +and as `error` at 90%. ++ +Custom configurations allow you to reduce this threshold to catch failures sooner +for task types that you might consider critical, such as alerting tasks. ++ +This value can be set to any number between 0 to 100, and a threshold is hit +when the value *exceeds* this number. This means that you can avoid setting the +status to `error` by setting the threshold at 100, or hit `error` the moment +any task fails by setting the threshold to 0 (as it will exceed 0 once a +single failure occurs). diff --git a/docs/settings/telemetry-settings.asciidoc b/docs/settings/telemetry-settings.asciidoc index 0329e2f010e80..65f78a2eaf12d 100644 --- a/docs/settings/telemetry-settings.asciidoc +++ b/docs/settings/telemetry-settings.asciidoc @@ -17,29 +17,26 @@ See our https://www.elastic.co/legal/privacy-statement[Privacy Statement] to lea [[telemetry-general-settings]] ==== General telemetry settings -[cols="2*<"] -|=== -|[[telemetry-enabled]] `telemetry.enabled` - | Set to `true` to send cluster statistics to Elastic. Reporting your + +[[telemetry-enabled]] `telemetry.enabled`:: + Set to `true` to send cluster statistics to Elastic. Reporting your cluster statistics helps us improve your user experience. Your data is never shared with anyone. Set to `false` to disable statistics reporting from any browser connected to the {kib} instance. Defaults to `true`. -| `telemetry.sendUsageFrom` - | Set to `'server'` to report the cluster statistics from the {kib} server. +`telemetry.sendUsageFrom`:: + Set to `'server'` to report the cluster statistics from the {kib} server. If the server fails to connect to our endpoint at https://telemetry.elastic.co/, it assumes it is behind a firewall and falls back to `'browser'` to send it from users' browsers when they are navigating through {kib}. Defaults to `'server'`. -|[[telemetry-optIn]] `telemetry.optIn` - | Set to `true` to automatically opt into reporting cluster statistics. You can also opt out through +[[telemetry-optIn]] `telemetry.optIn`:: + Set to `true` to automatically opt into reporting cluster statistics. You can also opt out through *Advanced Settings* in {kib}. Defaults to `true`. -| `telemetry.allowChangingOptInStatus` - | Set to `true` to allow overwriting the <> setting via the {kib} UI. Defaults to `true`. + - -|=== - +`telemetry.allowChangingOptInStatus`:: + Set to `true` to allow overwriting the <> setting via the {kib} UI. Defaults to `true`. + ++ [NOTE] ============ When `false`, <> must be `true`. To disable telemetry and not allow users to change that parameter, use <>. diff --git a/docs/setup/upgrade.asciidoc b/docs/setup/upgrade.asciidoc index a139b8a50ca4d..c828b837d8efd 100644 --- a/docs/setup/upgrade.asciidoc +++ b/docs/setup/upgrade.asciidoc @@ -44,13 +44,20 @@ a| [[upgrade-before-you-begin]] === Before you begin -WARNING: {kib} automatically runs upgrade migrations when required. To roll back to an earlier version in case of an upgrade failure, you **must** have a {ref}/snapshot-restore.html[backup snapshot] available. This snapshot must include the `kibana` feature state or all `kibana*` indices. For more information see <>. +[WARNING] +==== +{kib} automatically runs upgrade migrations when required. To roll back to an +earlier version in case of an upgrade failure, you **must** have a +{ref}/snapshot-restore.html[backup snapshot] that includes the `kibana` feature +state. Snapshots include this feature state by default. + +For more information, refer to <>. +==== Before you upgrade {kib}: * Consult the <>. -* {ref}/snapshots-take-snapshot.html[Take a snapshot] of your data. To roll back to an earlier version, the snapshot must include the `kibana` feature state or all `.kibana*` indices. -* Although not a requirement for rollbacks, we recommend taking a snapshot of all {kib} indices created by the plugins you use such as the `.reporting*` indices created by the reporting plugin. +* {ref}/snapshots-take-snapshot.html[Take a snapshot] of your data. To roll back to an earlier version, the snapshot must include the `kibana` feature state. * Before you upgrade production servers, test the upgrades in a dev environment. * See <> for common reasons upgrades fail and how to prevent these. * If you are using custom plugins, check that a compatible version is diff --git a/docs/setup/upgrade/upgrade-migrations.asciidoc b/docs/setup/upgrade/upgrade-migrations.asciidoc index c47c2c1745e94..e9e1b757fd71d 100644 --- a/docs/setup/upgrade/upgrade-migrations.asciidoc +++ b/docs/setup/upgrade/upgrade-migrations.asciidoc @@ -151,17 +151,18 @@ In order to rollback after a failed upgrade migration, the saved object indices [float] ===== Rollback by restoring a backup snapshot: -1. Before proceeding, {ref}/snapshots-take-snapshot.html[take a snapshot] that contains the `kibana` feature state or all `.kibana*` indices. +1. Before proceeding, {ref}/snapshots-take-snapshot.html[take a snapshot] that contains the `kibana` feature state. + Snapshots include this feature state by default. 2. Shutdown all {kib} instances to be 100% sure that there are no instances currently performing a migration. 3. Delete all saved object indices with `DELETE /.kibana*` -4. {ref}/snapshots-restore-snapshot.html[Restore] the `kibana` feature state or all `.kibana* indices and their aliases from the snapshot. +4. {ref}/snapshots-restore-snapshot.html[Restore] the `kibana` feature state from the snapshot. 5. Start up all {kib} instances on the older version you wish to rollback to. [float] ===== (Not recommended) Rollback without a backup snapshot: 1. Shutdown all {kib} instances to be 100% sure that there are no {kib} instances currently performing a migration. -2. {ref}/snapshots-take-snapshot.html[Take a snapshot] that includes the `kibana` feature state or all `.kibana*` indices. +2. {ref}/snapshots-take-snapshot.html[Take a snapshot] that includes the `kibana` feature state. Snapshots include this feature state by default. 3. Delete the version specific indices created by the failed upgrade migration. E.g. if you wish to rollback from a failed upgrade to v7.12.0 `DELETE /.kibana_7.12.0_*,.kibana_task_manager_7.12.0_*` 4. Inspect the output of `GET /_cat/aliases`. If either the `.kibana` and/or `.kibana_task_manager` alias is missing, these will have to be created manually. Find the latest index from the output of `GET /_cat/indices` and create the missing alias to point to the latest index. E.g. if the `.kibana` alias was missing and the latest index is `.kibana_3` create a new alias with `POST /.kibana_3/_aliases/.kibana`. 5. Remove the write block from the rollback indices. `PUT /.kibana,.kibana_task_manager/_settings {"index.blocks.write": false}` diff --git a/docs/user/alerting/alerting-troubleshooting.asciidoc b/docs/user/alerting/alerting-troubleshooting.asciidoc index 74a32b94975ad..5f3c566e82d42 100644 --- a/docs/user/alerting/alerting-troubleshooting.asciidoc +++ b/docs/user/alerting/alerting-troubleshooting.asciidoc @@ -15,7 +15,7 @@ Rules and connectors log to the Kibana logger with tags of [alerting] and [actio [source, txt] -------------------------------------------------- -server log [11:39:40.389] [error][alerting][alerting][plugins][plugins] Executing Alert "5b6237b0-c6f6-11eb-b0ff-a1a0cbcf29b6" has resulted in Error: Saved object [action/fdbc8610-c6f5-11eb-b0ff-a1a0cbcf29b6] not found +server log [11:39:40.389] [error][alerting][alerting][plugins][plugins] Executing Rule "5b6237b0-c6f6-11eb-b0ff-a1a0cbcf29b6" has resulted in Error: Saved object [action/fdbc8610-c6f5-11eb-b0ff-a1a0cbcf29b6] not found -------------------------------------------------- Some of the resources, such as saved objects and API keys, may no longer be available or valid, yielding error messages about those missing resources. diff --git a/docs/user/alerting/troubleshooting/event-log-index.asciidoc b/docs/user/alerting/troubleshooting/event-log-index.asciidoc index 393b982b279f5..5016b6d6f19c9 100644 --- a/docs/user/alerting/troubleshooting/event-log-index.asciidoc +++ b/docs/user/alerting/troubleshooting/event-log-index.asciidoc @@ -170,7 +170,7 @@ And see the errors for the rules you might provide the next search query: } ], }, - "message": "alert executed: .index-threshold:30d856c0-b14b-11eb-9a7c-9df284da9f99: 'test'", + "message": "rule executed: .index-threshold:30d856c0-b14b-11eb-9a7c-9df284da9f99: 'test'", "error" : { "message" : "Saved object [action/ef0e2530-b14a-11eb-9a7c-9df284da9f99] not found" }, diff --git a/docs/user/commands/cli-commands.asciidoc b/docs/user/commands/cli-commands.asciidoc new file mode 100644 index 0000000000000..35a25235bc238 --- /dev/null +++ b/docs/user/commands/cli-commands.asciidoc @@ -0,0 +1,8 @@ +[[cli-commands]] +== Command line tools + +{kib} provides the following tools for configuring security and performing other tasks from the command line: + +* <> + +include::kibana-verification-code.asciidoc[] \ No newline at end of file diff --git a/docs/user/commands/kibana-verification-code.asciidoc b/docs/user/commands/kibana-verification-code.asciidoc new file mode 100644 index 0000000000000..3ad1b0da51e2b --- /dev/null +++ b/docs/user/commands/kibana-verification-code.asciidoc @@ -0,0 +1,44 @@ +[[kibana-verification-code]] +=== kibana-verification-code + +The `kibana-verification-code` tool retrieves a verification code for enrolling +a {kib} instance with a secured {es} cluster. + +[discrete] +==== Synopsis + +[source,shell] +---- +bin/kibana-verification-code +[-V, --version] [-h, --help] +---- + +[discrete] +==== Description + +Use this command to retrieve a verification code for {kib}. You enter this code +in {kib} when manually configuring a secure connection with an {es} cluster. +This tool is useful if you don’t have access to the {kib} terminal output, such +as on a hosted environment. You can connect to a machine where {kib} is +running (such as using SSH) and retrieve a verification code that you enter in +{kib}. + +IMPORTANT: You must run this tool on the same machine where {kib} is running. + +[discrete] +[[kibana-verification-code-parameters]] +==== Parameters + +`-h, --help`:: Returns all of the command parameters. + +`-V, --version`:: Displays the {kib} version number. + +[discrete] +==== Examples + +The following command retrieves a verification code for {kib}. + +[source,shell] +---- +bin/kibana-verification-code +---- \ No newline at end of file diff --git a/docs/user/index.asciidoc b/docs/user/index.asciidoc index 75d0da1c597b6..57668b3f5bccf 100644 --- a/docs/user/index.asciidoc +++ b/docs/user/index.asciidoc @@ -45,3 +45,5 @@ include::management.asciidoc[] include::api.asciidoc[] include::plugins.asciidoc[] + +include::troubleshooting.asciidoc[] diff --git a/docs/user/production-considerations/task-manager-troubleshooting.asciidoc b/docs/user/production-considerations/task-manager-troubleshooting.asciidoc index 09eb304646e96..a22d46902f54c 100644 --- a/docs/user/production-considerations/task-manager-troubleshooting.asciidoc +++ b/docs/user/production-considerations/task-manager-troubleshooting.asciidoc @@ -1020,7 +1020,7 @@ This log message tells us that when Task Manager was running one of our rules, i For example, in this case, we’d expect to see a corresponding log line from the Alerting framework itself, saying that the rule failed. You should look in the Kibana log for a line similar to the log line below (probably shortly before the Task Manager log line): -Executing Alert "27559295-44e4-4983-aa1b-94fe043ab4f9" has resulted in Error: Unable to load resource ‘/api/something’ +Executing Rule "27559295-44e4-4983-aa1b-94fe043ab4f9" has resulted in Error: Unable to load resource ‘/api/something’ This would confirm that the error did in fact happen in the rule itself (rather than the Task Manager) and it would help us pin-point the specific ID of the rule which failed: 27559295-44e4-4983-aa1b-94fe043ab4f9 diff --git a/docs/user/setup.asciidoc b/docs/user/setup.asciidoc index 546cc8f974865..87213249e0d97 100644 --- a/docs/user/setup.asciidoc +++ b/docs/user/setup.asciidoc @@ -70,3 +70,5 @@ include::monitoring/configuring-monitoring.asciidoc[leveloffset=+1] include::monitoring/monitoring-metricbeat.asciidoc[leveloffset=+2] include::monitoring/viewing-metrics.asciidoc[leveloffset=+2] include::monitoring/monitoring-kibana.asciidoc[leveloffset=+2] + +include::commands/cli-commands.asciidoc[] diff --git a/docs/user/troubleshooting.asciidoc b/docs/user/troubleshooting.asciidoc new file mode 100644 index 0000000000000..8b32471c98d86 --- /dev/null +++ b/docs/user/troubleshooting.asciidoc @@ -0,0 +1,70 @@ +[[kibana-troubleshooting]] +== Troubleshooting + +=== Using {kib} server logs +{kib} Logs is a great way to see what's going on in your application and to debug performance issues. Navigating through a large number of generated logs can be overwhelming, and following are some techniques that you can use to optimize the process. + +Start by defining a problem area that you are interested in. For example, you might be interested in seeing how a particular {kib} Plugin is performing, so no need to gather logs for all of {kib}. Or you might want to focus on a particular feature, such as requests from the {kib} server to the {es} server. +Depending on your needs, you can configure {kib} to generate logs for a specific feature. +[source,yml] +---- +logging: + appenders: + file: + type: file + fileName: ./kibana.log + layout: + type: json + +### gather all the Kibana logs into a file +logging.root: + appenders: [file] + level: all + +### or gather a subset of the logs +logging.loggers: + ### responses to an HTTP request + - name: http.server.response + level: debug + appenders: [file] + ### result of a query to the Elasticsearch server + - name: elasticsearch.query + level: debug + appenders: [file] + ### logs generated by my plugin + - name: plugins.myPlugin + level: debug + appenders: [file] +---- +WARNING: Kibana's `file` appender is configured to produce logs in https://www.elastic.co/guide/en/ecs/master/ecs-reference.html[ECS JSON] format. It's the only format that includes the meta information necessary for https://www.elastic.co/guide/en/apm/agent/nodejs/current/log-correlation.html[log correlation] out-of-the-box. + +The next step is to define what https://www.elastic.co/observability[observability tools] are available. +For a better experience, set up an https://www.elastic.co/guide/en/apm/get-started/current/observability-integrations.html[Observability integration] provided by Elastic to debug your application with the <> +To debug something quickly without setting up additional tooling, you can work with <> + +[[debugging-logs-apm-ui]] +==== APM UI +*Prerequisites* {kib} logs are configured to be in https://www.elastic.co/guide/en/ecs/master/ecs-reference.html[ECS JSON] format to include tracing identifiers. + +To debug {kib} with the APM UI, you must set up the APM infrastructure. You can find instructions for the setup process +https://www.elastic.co/guide/en/apm/get-started/current/observability-integrations.html[on the Observability integrations page]. + +Once you set up the APM infrastructure, you can enable the APM agent and put {kib} under load to collect APM events. To analyze the collected metrics and logs, use the APM UI as demonstrated https://www.elastic.co/guide/en/kibana/master/transactions.html#transaction-trace-sample[in the docs]. + +[[plain-kibana-logs]] +==== Plain {kib} logs +*Prerequisites* {kib} logs are configured to be in https://www.elastic.co/guide/en/ecs/master/ecs-reference.html[ECS JSON] format to include tracing identifiers. + +Open {kib} Logs and search for an operation you are interested in. +For example, suppose you want to investigate the response times for queries to the `/api/telemetry/v2/clusters/_stats` {kib} endpoint. +Open Kibana Logs and search for the HTTP server response for the endpoint. It looks similar to the following (some fields are omitted for brevity). +[source,json] +---- +{ + "message":"POST /api/telemetry/v2/clusters/_stats 200 1014ms - 43.2KB", + "log":{"level":"DEBUG","logger":"http.server.response"}, + "trace":{"id":"9b99131a6f66587971ef085ef97dfd07"}, + "transaction":{"id":"d0c5bbf14f5febca"} +} +---- +You are interested in the https://www.elastic.co/guide/en/ecs/current/ecs-tracing.html#field-trace-id[trace.id] field, which is a unique identifier of a trace. The `trace.id` provides a way to group multiple events, like transactions, which belong together. You can search for `"trace":{"id":"9b99131a6f66587971ef085ef97dfd07"}` to get all the logs that belong to the same trace. This enables you to see how many {es} requests were triggered during the `9b99131a6f66587971ef085ef97dfd07` trace, what they looked like, what {es} endpoints were hit, and so on. diff --git a/package.json b/package.json index 6b7d6662eb70b..cae25e40ccf07 100644 --- a/package.json +++ b/package.json @@ -100,16 +100,15 @@ "@dnd-kit/core": "^3.1.1", "@dnd-kit/sortable": "^4.0.0", "@dnd-kit/utilities": "^2.0.0", - "@elastic/apm-rum": "^5.9.1", - "@elastic/apm-rum-react": "^1.3.1", + "@elastic/apm-rum": "^5.10.0", + "@elastic/apm-rum-react": "^1.3.2", "@elastic/apm-synthtrace": "link:bazel-bin/packages/elastic-apm-synthtrace", "@elastic/charts": "40.1.0", "@elastic/datemath": "link:bazel-bin/packages/elastic-datemath", "@elastic/elasticsearch": "npm:@elastic/elasticsearch-canary@^8.0.0-canary.35", "@elastic/ems-client": "8.0.0", - "@elastic/eui": "41.0.0", + "@elastic/eui": "41.2.3", "@elastic/filesaver": "1.1.2", - "@elastic/maki": "6.3.0", "@elastic/node-crypto": "1.2.1", "@elastic/numeral": "^2.5.1", "@elastic/react-search-ui": "^1.6.0", @@ -196,8 +195,10 @@ "archiver": "^5.2.0", "axios": "^0.21.1", "base64-js": "^1.3.1", + "bitmap-sdf": "^1.0.3", "brace": "0.11.1", "broadcast-channel": "^4.7.0", + "canvg": "^3.0.9", "chalk": "^4.1.0", "cheerio": "^1.0.0-rc.10", "chokidar": "^3.4.3", @@ -225,7 +226,7 @@ "deep-freeze-strict": "^1.1.1", "deepmerge": "^4.2.2", "del": "^5.1.0", - "elastic-apm-node": "^3.25.0", + "elastic-apm-node": "^3.26.0", "execa": "^4.0.2", "exit-hook": "^2.2.0", "expiry-js": "0.1.7", @@ -368,7 +369,7 @@ "redux-thunks": "^1.0.0", "regenerator-runtime": "^0.13.3", "remark-parse": "^8.0.3", - "remark-stringify": "^9.0.0", + "remark-stringify": "^8.0.3", "require-in-the-middle": "^5.1.0", "reselect": "^4.0.0", "resize-observer-polyfill": "^1.5.1", @@ -520,7 +521,6 @@ "@types/ejs": "^3.0.6", "@types/elastic__apm-synthtrace": "link:bazel-bin/packages/elastic-apm-synthtrace/npm_module_types", "@types/elastic__datemath": "link:bazel-bin/packages/elastic-datemath/npm_module_types", - "@types/elasticsearch": "^5.0.33", "@types/enzyme": "^3.10.8", "@types/eslint": "^7.28.0", "@types/express": "^4.17.13", @@ -567,7 +567,10 @@ "@types/kbn__config": "link:bazel-bin/packages/kbn-config/npm_module_types", "@types/kbn__config-schema": "link:bazel-bin/packages/kbn-config-schema/npm_module_types", "@types/kbn__crypto": "link:bazel-bin/packages/kbn-crypto/npm_module_types", + "@types/kbn__dev-utils": "link:bazel-bin/packages/kbn-dev-utils/npm_module_types", "@types/kbn__docs-utils": "link:bazel-bin/packages/kbn-docs-utils/npm_module_types", + "@types/kbn__es-archiver": "link:bazel-bin/packages/kbn-es-archiver/npm_module_types", + "@types/kbn__es-query": "link:bazel-bin/packages/kbn-es-query/npm_module_types", "@types/kbn__i18n": "link:bazel-bin/packages/kbn-i18n/npm_module_types", "@types/kbn__i18n-react": "link:bazel-bin/packages/kbn-i18n-react/npm_module_types", "@types/license-checker": "15.0.0", diff --git a/packages/BUILD.bazel b/packages/BUILD.bazel index aa90c3c122171..5fdaa9931bc4d 100644 --- a/packages/BUILD.bazel +++ b/packages/BUILD.bazel @@ -86,7 +86,10 @@ filegroup( "//packages/kbn-config:build_types", "//packages/kbn-config-schema:build_types", "//packages/kbn-crypto:build_types", + "//packages/kbn-dev-utils:build_types", "//packages/kbn-docs-utils:build_types", + "//packages/kbn-es-archiver:build_types", + "//packages/kbn-es-query:build_types", "//packages/kbn-i18n:build_types", "//packages/kbn-i18n-react:build_types", ], diff --git a/packages/elastic-apm-synthtrace/src/lib/apm/apm_fields.ts b/packages/elastic-apm-synthtrace/src/lib/apm/apm_fields.ts index a7a826d144d0e..e0a48fdcf2b89 100644 --- a/packages/elastic-apm-synthtrace/src/lib/apm/apm_fields.ts +++ b/packages/elastic-apm-synthtrace/src/lib/apm/apm_fields.ts @@ -15,6 +15,9 @@ export type ApmApplicationMetricFields = Partial<{ 'system.cpu.total.norm.pct': number; 'system.process.memory.rss.bytes': number; 'system.process.cpu.total.norm.pct': number; + 'jvm.memory.heap.used': number; + 'jvm.memory.non_heap.used': number; + 'jvm.thread.count': number; }>; export type ApmUserAgentFields = Partial<{ diff --git a/packages/elastic-apm-synthtrace/src/lib/apm/service.ts b/packages/elastic-apm-synthtrace/src/lib/apm/service.ts index 16917821c7ee4..d55f60d86e4db 100644 --- a/packages/elastic-apm-synthtrace/src/lib/apm/service.ts +++ b/packages/elastic-apm-synthtrace/src/lib/apm/service.ts @@ -15,6 +15,7 @@ export class Service extends Entity { return new Instance({ ...this.fields, ['service.node.name']: instanceName, + 'host.name': instanceName, 'container.id': instanceName, }); } diff --git a/packages/elastic-apm-synthtrace/src/test/scenarios/01_simple_trace.test.ts b/packages/elastic-apm-synthtrace/src/test/scenarios/01_simple_trace.test.ts index b38d34266f3ac..a78f1ec987bcf 100644 --- a/packages/elastic-apm-synthtrace/src/test/scenarios/01_simple_trace.test.ts +++ b/packages/elastic-apm-synthtrace/src/test/scenarios/01_simple_trace.test.ts @@ -70,6 +70,7 @@ describe('simple trace', () => { 'agent.name': 'java', 'container.id': 'instance-1', 'event.outcome': 'success', + 'host.name': 'instance-1', 'processor.event': 'transaction', 'processor.name': 'transaction', 'service.environment': 'production', @@ -92,6 +93,7 @@ describe('simple trace', () => { 'agent.name': 'java', 'container.id': 'instance-1', 'event.outcome': 'success', + 'host.name': 'instance-1', 'parent.id': '0000000000000300', 'processor.event': 'span', 'processor.name': 'transaction', diff --git a/packages/elastic-apm-synthtrace/src/test/scenarios/__snapshots__/01_simple_trace.test.ts.snap b/packages/elastic-apm-synthtrace/src/test/scenarios/__snapshots__/01_simple_trace.test.ts.snap index 76a76d41ec81d..1a5fca39e9fd9 100644 --- a/packages/elastic-apm-synthtrace/src/test/scenarios/__snapshots__/01_simple_trace.test.ts.snap +++ b/packages/elastic-apm-synthtrace/src/test/scenarios/__snapshots__/01_simple_trace.test.ts.snap @@ -7,6 +7,7 @@ Array [ "agent.name": "java", "container.id": "instance-1", "event.outcome": "success", + "host.name": "instance-1", "processor.event": "transaction", "processor.name": "transaction", "service.environment": "production", @@ -24,6 +25,7 @@ Array [ "agent.name": "java", "container.id": "instance-1", "event.outcome": "success", + "host.name": "instance-1", "parent.id": "0000000000000000", "processor.event": "span", "processor.name": "transaction", @@ -43,6 +45,7 @@ Array [ "agent.name": "java", "container.id": "instance-1", "event.outcome": "success", + "host.name": "instance-1", "processor.event": "transaction", "processor.name": "transaction", "service.environment": "production", @@ -60,6 +63,7 @@ Array [ "agent.name": "java", "container.id": "instance-1", "event.outcome": "success", + "host.name": "instance-1", "parent.id": "0000000000000004", "processor.event": "span", "processor.name": "transaction", @@ -79,6 +83,7 @@ Array [ "agent.name": "java", "container.id": "instance-1", "event.outcome": "success", + "host.name": "instance-1", "processor.event": "transaction", "processor.name": "transaction", "service.environment": "production", @@ -96,6 +101,7 @@ Array [ "agent.name": "java", "container.id": "instance-1", "event.outcome": "success", + "host.name": "instance-1", "parent.id": "0000000000000008", "processor.event": "span", "processor.name": "transaction", @@ -115,6 +121,7 @@ Array [ "agent.name": "java", "container.id": "instance-1", "event.outcome": "success", + "host.name": "instance-1", "processor.event": "transaction", "processor.name": "transaction", "service.environment": "production", @@ -132,6 +139,7 @@ Array [ "agent.name": "java", "container.id": "instance-1", "event.outcome": "success", + "host.name": "instance-1", "parent.id": "0000000000000012", "processor.event": "span", "processor.name": "transaction", @@ -151,6 +159,7 @@ Array [ "agent.name": "java", "container.id": "instance-1", "event.outcome": "success", + "host.name": "instance-1", "processor.event": "transaction", "processor.name": "transaction", "service.environment": "production", @@ -168,6 +177,7 @@ Array [ "agent.name": "java", "container.id": "instance-1", "event.outcome": "success", + "host.name": "instance-1", "parent.id": "0000000000000016", "processor.event": "span", "processor.name": "transaction", @@ -187,6 +197,7 @@ Array [ "agent.name": "java", "container.id": "instance-1", "event.outcome": "success", + "host.name": "instance-1", "processor.event": "transaction", "processor.name": "transaction", "service.environment": "production", @@ -204,6 +215,7 @@ Array [ "agent.name": "java", "container.id": "instance-1", "event.outcome": "success", + "host.name": "instance-1", "parent.id": "0000000000000020", "processor.event": "span", "processor.name": "transaction", @@ -223,6 +235,7 @@ Array [ "agent.name": "java", "container.id": "instance-1", "event.outcome": "success", + "host.name": "instance-1", "processor.event": "transaction", "processor.name": "transaction", "service.environment": "production", @@ -240,6 +253,7 @@ Array [ "agent.name": "java", "container.id": "instance-1", "event.outcome": "success", + "host.name": "instance-1", "parent.id": "0000000000000024", "processor.event": "span", "processor.name": "transaction", @@ -259,6 +273,7 @@ Array [ "agent.name": "java", "container.id": "instance-1", "event.outcome": "success", + "host.name": "instance-1", "processor.event": "transaction", "processor.name": "transaction", "service.environment": "production", @@ -276,6 +291,7 @@ Array [ "agent.name": "java", "container.id": "instance-1", "event.outcome": "success", + "host.name": "instance-1", "parent.id": "0000000000000028", "processor.event": "span", "processor.name": "transaction", @@ -295,6 +311,7 @@ Array [ "agent.name": "java", "container.id": "instance-1", "event.outcome": "success", + "host.name": "instance-1", "processor.event": "transaction", "processor.name": "transaction", "service.environment": "production", @@ -312,6 +329,7 @@ Array [ "agent.name": "java", "container.id": "instance-1", "event.outcome": "success", + "host.name": "instance-1", "parent.id": "0000000000000032", "processor.event": "span", "processor.name": "transaction", @@ -331,6 +349,7 @@ Array [ "agent.name": "java", "container.id": "instance-1", "event.outcome": "success", + "host.name": "instance-1", "processor.event": "transaction", "processor.name": "transaction", "service.environment": "production", @@ -348,6 +367,7 @@ Array [ "agent.name": "java", "container.id": "instance-1", "event.outcome": "success", + "host.name": "instance-1", "parent.id": "0000000000000036", "processor.event": "span", "processor.name": "transaction", @@ -367,6 +387,7 @@ Array [ "agent.name": "java", "container.id": "instance-1", "event.outcome": "success", + "host.name": "instance-1", "processor.event": "transaction", "processor.name": "transaction", "service.environment": "production", @@ -384,6 +405,7 @@ Array [ "agent.name": "java", "container.id": "instance-1", "event.outcome": "success", + "host.name": "instance-1", "parent.id": "0000000000000040", "processor.event": "span", "processor.name": "transaction", @@ -403,6 +425,7 @@ Array [ "agent.name": "java", "container.id": "instance-1", "event.outcome": "success", + "host.name": "instance-1", "processor.event": "transaction", "processor.name": "transaction", "service.environment": "production", @@ -420,6 +443,7 @@ Array [ "agent.name": "java", "container.id": "instance-1", "event.outcome": "success", + "host.name": "instance-1", "parent.id": "0000000000000044", "processor.event": "span", "processor.name": "transaction", @@ -439,6 +463,7 @@ Array [ "agent.name": "java", "container.id": "instance-1", "event.outcome": "success", + "host.name": "instance-1", "processor.event": "transaction", "processor.name": "transaction", "service.environment": "production", @@ -456,6 +481,7 @@ Array [ "agent.name": "java", "container.id": "instance-1", "event.outcome": "success", + "host.name": "instance-1", "parent.id": "0000000000000048", "processor.event": "span", "processor.name": "transaction", @@ -475,6 +501,7 @@ Array [ "agent.name": "java", "container.id": "instance-1", "event.outcome": "success", + "host.name": "instance-1", "processor.event": "transaction", "processor.name": "transaction", "service.environment": "production", @@ -492,6 +519,7 @@ Array [ "agent.name": "java", "container.id": "instance-1", "event.outcome": "success", + "host.name": "instance-1", "parent.id": "0000000000000052", "processor.event": "span", "processor.name": "transaction", @@ -511,6 +539,7 @@ Array [ "agent.name": "java", "container.id": "instance-1", "event.outcome": "success", + "host.name": "instance-1", "processor.event": "transaction", "processor.name": "transaction", "service.environment": "production", @@ -528,6 +557,7 @@ Array [ "agent.name": "java", "container.id": "instance-1", "event.outcome": "success", + "host.name": "instance-1", "parent.id": "0000000000000056", "processor.event": "span", "processor.name": "transaction", diff --git a/packages/elastic-eslint-config-kibana/react.js b/packages/elastic-eslint-config-kibana/react.js index 29000bdb15684..0b1cce15de9ad 100644 --- a/packages/elastic-eslint-config-kibana/react.js +++ b/packages/elastic-eslint-config-kibana/react.js @@ -1,5 +1,5 @@ const semver = require('semver'); -const { kibanaPackageJson: PKG } = require('@kbn/dev-utils'); +const { kibanaPackageJson: PKG } = require('@kbn/utils'); module.exports = { plugins: [ diff --git a/packages/elastic-eslint-config-kibana/typescript.js b/packages/elastic-eslint-config-kibana/typescript.js index 1a0ef81ae2f1e..3ada725cb1805 100644 --- a/packages/elastic-eslint-config-kibana/typescript.js +++ b/packages/elastic-eslint-config-kibana/typescript.js @@ -4,7 +4,7 @@ // as this package was moved from typescript-eslint-parser to @typescript-eslint/parser const semver = require('semver'); -const { kibanaPackageJson: PKG } = require('@kbn/dev-utils'); +const { kibanaPackageJson: PKG } = require('@kbn/utils'); const eslintConfigPrettierTypescriptEslintRules = require('eslint-config-prettier/@typescript-eslint').rules; diff --git a/packages/kbn-cli-dev-mode/BUILD.bazel b/packages/kbn-cli-dev-mode/BUILD.bazel index dfb441dffc6ef..cdc40e85c972a 100644 --- a/packages/kbn-cli-dev-mode/BUILD.bazel +++ b/packages/kbn-cli-dev-mode/BUILD.bazel @@ -50,7 +50,7 @@ RUNTIME_DEPS = [ TYPES_DEPS = [ "//packages/kbn-config:npm_module_types", "//packages/kbn-config-schema:npm_module_types", - "//packages/kbn-dev-utils", + "//packages/kbn-dev-utils:npm_module_types", "//packages/kbn-logging", "//packages/kbn-optimizer", "//packages/kbn-server-http-tools", diff --git a/packages/kbn-cli-dev-mode/src/cli_dev_mode.test.ts b/packages/kbn-cli-dev-mode/src/cli_dev_mode.test.ts index e5e009e51e69e..0066644d0825a 100644 --- a/packages/kbn-cli-dev-mode/src/cli_dev_mode.test.ts +++ b/packages/kbn-cli-dev-mode/src/cli_dev_mode.test.ts @@ -8,11 +8,9 @@ import Path from 'path'; import * as Rx from 'rxjs'; -import { - REPO_ROOT, - createAbsolutePathSerializer, - createAnyInstanceSerializer, -} from '@kbn/dev-utils'; +import { createAbsolutePathSerializer, createAnyInstanceSerializer } from '@kbn/dev-utils'; + +import { REPO_ROOT } from '@kbn/utils'; import { TestLog } from './log'; import { CliDevMode, SomeCliArgs } from './cli_dev_mode'; diff --git a/packages/kbn-cli-dev-mode/src/cli_dev_mode.ts b/packages/kbn-cli-dev-mode/src/cli_dev_mode.ts index 2396b316aa3a2..9cf688b675e67 100644 --- a/packages/kbn-cli-dev-mode/src/cli_dev_mode.ts +++ b/packages/kbn-cli-dev-mode/src/cli_dev_mode.ts @@ -22,7 +22,8 @@ import { takeUntil, } from 'rxjs/operators'; import { CliArgs } from '@kbn/config'; -import { REPO_ROOT, CiStatsReporter } from '@kbn/dev-utils'; +import { CiStatsReporter } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import { Log, CliLog } from './log'; import { Optimizer } from './optimizer'; diff --git a/packages/kbn-cli-dev-mode/src/get_server_watch_paths.test.ts b/packages/kbn-cli-dev-mode/src/get_server_watch_paths.test.ts index 06ded8d8bf526..25bc59bf78458 100644 --- a/packages/kbn-cli-dev-mode/src/get_server_watch_paths.test.ts +++ b/packages/kbn-cli-dev-mode/src/get_server_watch_paths.test.ts @@ -8,7 +8,8 @@ import Path from 'path'; -import { REPO_ROOT, createAbsolutePathSerializer } from '@kbn/dev-utils'; +import { createAbsolutePathSerializer } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import { getServerWatchPaths } from './get_server_watch_paths'; diff --git a/packages/kbn-cli-dev-mode/src/get_server_watch_paths.ts b/packages/kbn-cli-dev-mode/src/get_server_watch_paths.ts index f075dc806b6ec..acfc9aeecdc80 100644 --- a/packages/kbn-cli-dev-mode/src/get_server_watch_paths.ts +++ b/packages/kbn-cli-dev-mode/src/get_server_watch_paths.ts @@ -9,7 +9,7 @@ import Path from 'path'; import Fs from 'fs'; -import { REPO_ROOT } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; interface Options { pluginPaths: string[]; 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 a5d0142853416..7a2e3a5d6cb0f 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 @@ -30,6 +30,11 @@ describe('parsing units', () => { expect(ByteSizeValue.parse('1gb').getValueInBytes()).toBe(1073741824); }); + test('case insensitive units', () => { + expect(ByteSizeValue.parse('1KB').getValueInBytes()).toBe(1024); + expect(ByteSizeValue.parse('1Mb').getValueInBytes()).toBe(1024 * 1024); + }); + test('throws an error when unsupported unit specified', () => { 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."` 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 fb90bd70ed5c6..6fabe35b30024 100644 --- a/packages/kbn-config-schema/src/byte_size_value/index.ts +++ b/packages/kbn-config-schema/src/byte_size_value/index.ts @@ -22,7 +22,7 @@ function renderUnit(value: number, unit: string) { export class ByteSizeValue { public static parse(text: string): ByteSizeValue { - const match = /([1-9][0-9]*)(b|kb|mb|gb)/.exec(text); + const match = /([1-9][0-9]*)(b|kb|mb|gb)/i.exec(text); if (!match) { const number = Number(text); if (typeof number !== 'number' || isNaN(number)) { @@ -35,7 +35,7 @@ export class ByteSizeValue { } const value = parseInt(match[1], 10); - const unit = match[2]; + const unit = match[2].toLowerCase(); return new ByteSizeValue(value * unitMultiplier[unit]); } diff --git a/packages/kbn-config/src/deprecation/apply_deprecations.test.ts b/packages/kbn-config/src/deprecation/apply_deprecations.test.ts index 70945b2d96b32..3f84eed867655 100644 --- a/packages/kbn-config/src/deprecation/apply_deprecations.test.ts +++ b/packages/kbn-config/src/deprecation/apply_deprecations.test.ts @@ -116,6 +116,36 @@ describe('applyDeprecations', () => { expect(migrated).toEqual({ foo: 'bar', newname: 'renamed' }); }); + it('nested properties take into account if their parents are empty objects, and remove them if so', () => { + const initialConfig = { + foo: 'bar', + deprecated: { nested: 'deprecated' }, + nested: { + from: { + rename: 'renamed', + }, + to: { + keep: 'keep', + }, + }, + }; + + const { config: migrated } = applyDeprecations(initialConfig, [ + wrapHandler(deprecations.unused('deprecated.nested')), + wrapHandler(deprecations.rename('nested.from.rename', 'nested.to.renamed')), + ]); + + expect(migrated).toStrictEqual({ + foo: 'bar', + nested: { + to: { + keep: 'keep', + renamed: 'renamed', + }, + }, + }); + }); + it('does not alter the initial config', () => { const initialConfig = { foo: 'bar', deprecated: 'deprecated' }; diff --git a/packages/kbn-config/src/deprecation/apply_deprecations.ts b/packages/kbn-config/src/deprecation/apply_deprecations.ts index 11b35840969d0..9b0c409204414 100644 --- a/packages/kbn-config/src/deprecation/apply_deprecations.ts +++ b/packages/kbn-config/src/deprecation/apply_deprecations.ts @@ -6,13 +6,14 @@ * Side Public License, v 1. */ -import { cloneDeep, unset } from 'lodash'; +import { cloneDeep } from 'lodash'; import { set } from '@elastic/safer-lodash-set'; import type { AddConfigDeprecation, ChangedDeprecatedPaths, ConfigDeprecationWithContext, } from './types'; +import { unsetAndCleanEmptyParent } from './unset_and_clean_empty_parent'; const noopAddDeprecationFactory: () => AddConfigDeprecation = () => () => undefined; @@ -45,7 +46,7 @@ export const applyDeprecations = ( if (commands.unset) { changedPaths.unset.push(...commands.unset.map((c) => c.path)); commands.unset.forEach(function ({ path: commandPath }) { - unset(result, commandPath); + unsetAndCleanEmptyParent(result, commandPath); }); } } diff --git a/packages/kbn-config/src/deprecation/types.ts b/packages/kbn-config/src/deprecation/types.ts index 7b1eb4a0ea6c1..6abe4cd94a6fb 100644 --- a/packages/kbn-config/src/deprecation/types.ts +++ b/packages/kbn-config/src/deprecation/types.ts @@ -186,6 +186,25 @@ export interface ConfigDeprecationFactory { * rename('oldKey', 'newKey'), * ] * ``` + * + * @remarks + * If the oldKey is a nested property and it's the last property in an object, it may remove any empty-object parent keys. + * ``` + * // Original object + * { + * a: { + * b: { c: 1 }, + * d: { e: 1 } + * } + * } + * + * // If rename('a.b.c', 'a.d.c'), the resulting object removes the entire "a.b" tree because "c" was the last property in that branch + * { + * a: { + * d: { c: 1, e: 1 } + * } + * } + * ``` */ rename( oldKey: string, @@ -207,6 +226,25 @@ export interface ConfigDeprecationFactory { * renameFromRoot('oldplugin.key', 'newplugin.key'), * ] * ``` + * + * @remarks + * If the oldKey is a nested property and it's the last property in an object, it may remove any empty-object parent keys. + * ``` + * // Original object + * { + * a: { + * b: { c: 1 }, + * d: { e: 1 } + * } + * } + * + * // If renameFromRoot('a.b.c', 'a.d.c'), the resulting object removes the entire "a.b" tree because "c" was the last property in that branch + * { + * a: { + * d: { c: 1, e: 1 } + * } + * } + * ``` */ renameFromRoot( oldKey: string, @@ -225,6 +263,25 @@ export interface ConfigDeprecationFactory { * unused('deprecatedKey'), * ] * ``` + * + * @remarks + * If the path is a nested property and it's the last property in an object, it may remove any empty-object parent keys. + * ``` + * // Original object + * { + * a: { + * b: { c: 1 }, + * d: { e: 1 } + * } + * } + * + * // If unused('a.b.c'), the resulting object removes the entire "a.b" tree because "c" was the last property in that branch + * { + * a: { + * d: { e: 1 } + * } + * } + * ``` */ unused(unusedKey: string, details?: Partial): ConfigDeprecation; @@ -242,6 +299,25 @@ export interface ConfigDeprecationFactory { * unusedFromRoot('somepath.deprecatedProperty'), * ] * ``` + * + * @remarks + * If the path is a nested property and it's the last property in an object, it may remove any empty-object parent keys. + * ``` + * // Original object + * { + * a: { + * b: { c: 1 }, + * d: { e: 1 } + * } + * } + * + * // If unused('a.b.c'), the resulting object removes the entire "a.b" tree because "c" was the last property in that branch + * { + * a: { + * d: { e: 1 } + * } + * } + * ``` */ unusedFromRoot(unusedKey: string, details?: Partial): ConfigDeprecation; } diff --git a/packages/kbn-config/src/deprecation/unset_and_clean_empty_parent.test.ts b/packages/kbn-config/src/deprecation/unset_and_clean_empty_parent.test.ts new file mode 100644 index 0000000000000..115730c106137 --- /dev/null +++ b/packages/kbn-config/src/deprecation/unset_and_clean_empty_parent.test.ts @@ -0,0 +1,41 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { unsetAndCleanEmptyParent } from './unset_and_clean_empty_parent'; + +describe('unsetAndcleanEmptyParent', () => { + test('unsets the property of the root object, and returns an empty root object', () => { + const config = { toRemove: 'toRemove' }; + unsetAndCleanEmptyParent(config, 'toRemove'); + expect(config).toStrictEqual({}); + }); + + test('unsets a nested property of the root object, and removes the empty parent property', () => { + const config = { nestedToRemove: { toRemove: 'toRemove' } }; + unsetAndCleanEmptyParent(config, 'nestedToRemove.toRemove'); + expect(config).toStrictEqual({}); + }); + + describe('Navigating to parent known issue: Array paths', () => { + // We navigate to the parent property by splitting the "." and dropping the last item in the path. + // This means that paths that are declared as prop1[idx] cannot apply the parent's cleanup logic. + // The use cases for this are quite limited, so we'll accept it as a documented limitation. + + test('does not remove a parent array when the index is specified with square brackets', () => { + const config = { nestedToRemove: [{ toRemove: 'toRemove' }] }; + unsetAndCleanEmptyParent(config, 'nestedToRemove[0].toRemove'); + expect(config).toStrictEqual({ nestedToRemove: [{}] }); + }); + + test('removes a parent array when the index is specified with dots', () => { + const config = { nestedToRemove: [{ toRemove: 'toRemove' }] }; + unsetAndCleanEmptyParent(config, 'nestedToRemove.0.toRemove'); + expect(config).toStrictEqual({}); + }); + }); +}); diff --git a/packages/kbn-config/src/deprecation/unset_and_clean_empty_parent.ts b/packages/kbn-config/src/deprecation/unset_and_clean_empty_parent.ts new file mode 100644 index 0000000000000..c5f5e5951adc4 --- /dev/null +++ b/packages/kbn-config/src/deprecation/unset_and_clean_empty_parent.ts @@ -0,0 +1,42 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { get, unset } from 'lodash'; + +/** + * Unsets the path and checks if the parent property is an empty object. + * If so, it removes the property from the config object (mutation is applied). + * + * @internal + */ +export const unsetAndCleanEmptyParent = ( + config: Record, + path: string | string[] +): void => { + // 1. Unset the provided path + const didUnset = unset(config, path); + + // Check if the unset actually removed anything. + // This way we avoid some CPU cycles when the previous action didn't apply any changes. + if (didUnset) { + // 2. Check if the parent property in the resulting object is an empty object + const pathArray = Array.isArray(path) ? path : path.split('.'); + const parentPath = pathArray.slice(0, -1); + if (parentPath.length === 0) { + return; + } + const parentObj = get(config, parentPath); + if ( + typeof parentObj === 'object' && + parentObj !== null && + Object.keys(parentObj).length === 0 + ) { + unsetAndCleanEmptyParent(config, parentPath); + } + } +}; diff --git a/packages/kbn-crypto/BUILD.bazel b/packages/kbn-crypto/BUILD.bazel index 81ee6d770103c..f71c8b866fd5d 100644 --- a/packages/kbn-crypto/BUILD.bazel +++ b/packages/kbn-crypto/BUILD.bazel @@ -34,7 +34,7 @@ RUNTIME_DEPS = [ ] TYPES_DEPS = [ - "//packages/kbn-dev-utils", + "//packages/kbn-dev-utils:npm_module_types", "@npm//@types/flot", "@npm//@types/jest", "@npm//@types/node", diff --git a/packages/kbn-dev-utils/BUILD.bazel b/packages/kbn-dev-utils/BUILD.bazel index 4fd99e0144cb6..89df1870a3cec 100644 --- a/packages/kbn-dev-utils/BUILD.bazel +++ b/packages/kbn-dev-utils/BUILD.bazel @@ -1,9 +1,10 @@ -load("@npm//@bazel/typescript:index.bzl", "ts_config", "ts_project") -load("@build_bazel_rules_nodejs//:index.bzl", "js_library", "pkg_npm") -load("//src/dev/bazel:index.bzl", "jsts_transpiler") +load("@npm//@bazel/typescript:index.bzl", "ts_config") +load("@build_bazel_rules_nodejs//:index.bzl", "js_library") +load("//src/dev/bazel:index.bzl", "jsts_transpiler", "pkg_npm", "pkg_npm_types", "ts_project") PKG_BASE_NAME = "kbn-dev-utils" PKG_REQUIRE_NAME = "@kbn/dev-utils" +TYPES_PKG_REQUIRE_NAME = "@types/kbn__dev-utils" SOURCE_FILES = glob( [ @@ -43,7 +44,6 @@ NPM_MODULE_EXTRA_FILES = [ ] RUNTIME_DEPS = [ - "//packages/kbn-std", "//packages/kbn-utils", "@npm//@babel/core", "@npm//axios", @@ -66,7 +66,6 @@ RUNTIME_DEPS = [ ] TYPES_DEPS = [ - "//packages/kbn-std", "//packages/kbn-utils", "@npm//@babel/parser", "@npm//@babel/types", @@ -124,7 +123,7 @@ ts_project( js_library( name = PKG_BASE_NAME, srcs = NPM_MODULE_EXTRA_FILES, - deps = RUNTIME_DEPS + [":target_node", ":tsc_types"], + deps = RUNTIME_DEPS + [":target_node"], package_name = PKG_REQUIRE_NAME, visibility = ["//visibility:public"], ) @@ -143,3 +142,20 @@ filegroup( ], visibility = ["//visibility:public"], ) + +pkg_npm_types( + name = "npm_module_types", + srcs = SRCS, + deps = [":tsc_types"], + package_name = TYPES_PKG_REQUIRE_NAME, + tsconfig = ":tsconfig", + visibility = ["//visibility:public"], +) + +filegroup( + name = "build_types", + srcs = [ + ":npm_module_types", + ], + visibility = ["//visibility:public"], +) diff --git a/packages/kbn-dev-utils/package.json b/packages/kbn-dev-utils/package.json index 9d6e6dde86fac..ab4f489e7d345 100644 --- a/packages/kbn-dev-utils/package.json +++ b/packages/kbn-dev-utils/package.json @@ -4,7 +4,6 @@ "private": true, "license": "SSPL-1.0 OR Elastic License 2.0", "main": "./target_node/index.js", - "types": "./target_types/index.d.ts", "kibana": { "devOnly": true } diff --git a/packages/kbn-dev-utils/src/index.ts b/packages/kbn-dev-utils/src/index.ts index 381e99ac677f5..9b207ad9e9966 100644 --- a/packages/kbn-dev-utils/src/index.ts +++ b/packages/kbn-dev-utils/src/index.ts @@ -6,7 +6,6 @@ * Side Public License, v 1. */ -export * from '@kbn/utils'; export { withProcRunner, ProcRunner } from './proc_runner'; export * from './tooling_log'; export * from './serializers'; diff --git a/packages/kbn-dev-utils/src/tooling_log/__snapshots__/tooling_log_text_writer.test.ts.snap b/packages/kbn-dev-utils/src/tooling_log/__snapshots__/tooling_log_text_writer.test.ts.snap index 7ff982acafbe4..5fa074d4c7739 100644 --- a/packages/kbn-dev-utils/src/tooling_log/__snapshots__/tooling_log_text_writer.test.ts.snap +++ b/packages/kbn-dev-utils/src/tooling_log/__snapshots__/tooling_log_text_writer.test.ts.snap @@ -170,6 +170,14 @@ exports[`level:warning/type:warning snapshots: output 1`] = ` " `; +exports[`never ignores write messages from the kibana elasticsearch.deprecation logger context 1`] = ` +" │[elasticsearch.deprecation] + │{ foo: { bar: { '1': [Array] } }, bar: { bar: { '1': [Array] } } } + │ + │Infinity +" +`; + exports[`throws error if created with invalid level 1`] = `"Invalid log level \\"foo\\" (expected one of silent,error,warning,success,info,debug,verbose)"`; exports[`throws error if writeTo config is not defined or doesn't have a write method 1`] = `"ToolingLogTextWriter requires the \`writeTo\` option be set to a stream (like process.stdout)"`; diff --git a/packages/kbn-dev-utils/src/tooling_log/tooling_log_text_writer.test.ts b/packages/kbn-dev-utils/src/tooling_log/tooling_log_text_writer.test.ts index b4668f29b6e21..fbccfdcdf6ac0 100644 --- a/packages/kbn-dev-utils/src/tooling_log/tooling_log_text_writer.test.ts +++ b/packages/kbn-dev-utils/src/tooling_log/tooling_log_text_writer.test.ts @@ -88,3 +88,55 @@ it('formats %s patterns and indents multi-line messages correctly', () => { const output = write.mock.calls.reduce((acc, chunk) => `${acc}${chunk}`, ''); expect(output).toMatchSnapshot(); }); + +it('does not write messages from sources in ignoreSources', () => { + const write = jest.fn(); + const writer = new ToolingLogTextWriter({ + ignoreSources: ['myIgnoredSource'], + level: 'debug', + writeTo: { + write, + }, + }); + + writer.write({ + source: 'myIgnoredSource', + type: 'success', + indent: 10, + args: [ + '%s\n%O\n\n%d', + 'foo bar', + { foo: { bar: { 1: [1, 2, 3] } }, bar: { bar: { 1: [1, 2, 3] } } }, + Infinity, + ], + }); + + const output = write.mock.calls.reduce((acc, chunk) => `${acc}${chunk}`, ''); + expect(output).toEqual(''); +}); + +it('never ignores write messages from the kibana elasticsearch.deprecation logger context', () => { + const write = jest.fn(); + const writer = new ToolingLogTextWriter({ + ignoreSources: ['myIgnoredSource'], + level: 'debug', + writeTo: { + write, + }, + }); + + writer.write({ + source: 'myIgnoredSource', + type: 'write', + indent: 10, + args: [ + '%s\n%O\n\n%d', + '[elasticsearch.deprecation]', + { foo: { bar: { 1: [1, 2, 3] } }, bar: { bar: { 1: [1, 2, 3] } } }, + Infinity, + ], + }); + + const output = write.mock.calls.reduce((acc, chunk) => `${acc}${chunk}`, ''); + expect(output).toMatchSnapshot(); +}); diff --git a/packages/kbn-dev-utils/src/tooling_log/tooling_log_text_writer.ts b/packages/kbn-dev-utils/src/tooling_log/tooling_log_text_writer.ts index 660dae3fa1f55..4fe33241cf77e 100644 --- a/packages/kbn-dev-utils/src/tooling_log/tooling_log_text_writer.ts +++ b/packages/kbn-dev-utils/src/tooling_log/tooling_log_text_writer.ts @@ -92,7 +92,15 @@ export class ToolingLogTextWriter implements Writer { } if (this.ignoreSources && msg.source && this.ignoreSources.includes(msg.source)) { - return false; + if (msg.type === 'write') { + const txt = format(msg.args[0], ...msg.args.slice(1)); + // Ensure that Elasticsearch deprecation log messages from Kibana aren't ignored + if (!/elasticsearch\.deprecation/.test(txt)) { + return false; + } + } else { + return false; + } } const prefix = has(MSG_PREFIXES, msg.type) ? MSG_PREFIXES[msg.type] : ''; diff --git a/packages/kbn-docs-utils/BUILD.bazel b/packages/kbn-docs-utils/BUILD.bazel index 37e5bb06377cc..edfd3ee96c181 100644 --- a/packages/kbn-docs-utils/BUILD.bazel +++ b/packages/kbn-docs-utils/BUILD.bazel @@ -38,7 +38,7 @@ RUNTIME_DEPS = [ TYPES_DEPS = [ "//packages/kbn-config:npm_module_types", - "//packages/kbn-dev-utils", + "//packages/kbn-dev-utils:npm_module_types", "//packages/kbn-utils", "@npm//ts-morph", "@npm//@types/dedent", diff --git a/packages/kbn-docs-utils/src/api_docs/build_api_docs_cli.ts b/packages/kbn-docs-utils/src/api_docs/build_api_docs_cli.ts index 2e4ce08540714..3c9137b260a3e 100644 --- a/packages/kbn-docs-utils/src/api_docs/build_api_docs_cli.ts +++ b/packages/kbn-docs-utils/src/api_docs/build_api_docs_cli.ts @@ -9,7 +9,8 @@ import Fs from 'fs'; import Path from 'path'; -import { REPO_ROOT, run, CiStatsReporter, createFlagError } from '@kbn/dev-utils'; +import { run, CiStatsReporter, createFlagError } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import { Project } from 'ts-morph'; import { writePluginDocs } from './mdx/write_plugin_mdx_docs'; @@ -241,7 +242,7 @@ export function runBuildApiDocsCli() { boolean: ['references'], help: ` --plugin Optionally, run for only a specific plugin - --stats Optionally print API stats. Must be one or more of: any, comments or exports. + --stats Optionally print API stats. Must be one or more of: any, comments or exports. --references Collect references for API items `, }, diff --git a/packages/kbn-docs-utils/src/api_docs/find_plugins.ts b/packages/kbn-docs-utils/src/api_docs/find_plugins.ts index 78cba3f3a9476..774452a6f1f9f 100644 --- a/packages/kbn-docs-utils/src/api_docs/find_plugins.ts +++ b/packages/kbn-docs-utils/src/api_docs/find_plugins.ts @@ -12,7 +12,8 @@ import globby from 'globby'; import loadJsonFile from 'load-json-file'; import { getPluginSearchPaths } from '@kbn/config'; -import { simpleKibanaPlatformPluginDiscovery, REPO_ROOT } from '@kbn/dev-utils'; +import { simpleKibanaPlatformPluginDiscovery } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import { ApiScope, PluginOrPackage } from './types'; export function findPlugins(): PluginOrPackage[] { diff --git a/packages/kbn-es-archiver/BUILD.bazel b/packages/kbn-es-archiver/BUILD.bazel index 2dc311ed74406..da8aaf913ab67 100644 --- a/packages/kbn-es-archiver/BUILD.bazel +++ b/packages/kbn-es-archiver/BUILD.bazel @@ -1,9 +1,10 @@ -load("@npm//@bazel/typescript:index.bzl", "ts_config", "ts_project") -load("@build_bazel_rules_nodejs//:index.bzl", "js_library", "pkg_npm") -load("//src/dev/bazel:index.bzl", "jsts_transpiler") +load("@npm//@bazel/typescript:index.bzl", "ts_config") +load("@build_bazel_rules_nodejs//:index.bzl", "js_library") +load("//src/dev/bazel:index.bzl", "jsts_transpiler", "pkg_npm", "pkg_npm_types", "ts_project") PKG_BASE_NAME = "kbn-es-archiver" PKG_REQUIRE_NAME = "@kbn/es-archiver" +TYPES_PKG_REQUIRE_NAME = "@types/kbn__es-archiver" SOURCE_FILES = glob( [ @@ -43,7 +44,7 @@ RUNTIME_DEPS = [ ] TYPES_DEPS = [ - "//packages/kbn-dev-utils", + "//packages/kbn-dev-utils:npm_module_types", "//packages/kbn-test", "//packages/kbn-utils", "@npm//@elastic/elasticsearch", @@ -90,7 +91,7 @@ ts_project( js_library( name = PKG_BASE_NAME, srcs = NPM_MODULE_EXTRA_FILES, - deps = RUNTIME_DEPS + [":target_node", ":tsc_types"], + deps = RUNTIME_DEPS + [":target_node"], package_name = PKG_REQUIRE_NAME, visibility = ["//visibility:public"], ) @@ -109,3 +110,20 @@ filegroup( ], visibility = ["//visibility:public"], ) + +pkg_npm_types( + name = "npm_module_types", + srcs = SRCS, + deps = [":tsc_types"], + package_name = TYPES_PKG_REQUIRE_NAME, + tsconfig = ":tsconfig", + visibility = ["//visibility:public"], +) + +filegroup( + name = "build_types", + srcs = [ + ":npm_module_types", + ], + visibility = ["//visibility:public"], +) diff --git a/packages/kbn-es-archiver/package.json b/packages/kbn-es-archiver/package.json index 0cce08eaf0352..bff3990a0c1bc 100644 --- a/packages/kbn-es-archiver/package.json +++ b/packages/kbn-es-archiver/package.json @@ -4,7 +4,6 @@ "license": "SSPL-1.0 OR Elastic License 2.0", "private": "true", "main": "target_node/index.js", - "types": "target_types/index.d.ts", "kibana": { "devOnly": true } diff --git a/packages/kbn-es-archiver/src/actions/load.ts b/packages/kbn-es-archiver/src/actions/load.ts index 0a7235c566b52..0a318f895deb3 100644 --- a/packages/kbn-es-archiver/src/actions/load.ts +++ b/packages/kbn-es-archiver/src/actions/load.ts @@ -9,7 +9,8 @@ import { resolve, relative } from 'path'; import { createReadStream } from 'fs'; import { Readable } from 'stream'; -import { ToolingLog, REPO_ROOT } from '@kbn/dev-utils'; +import { ToolingLog } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import { KbnClient } from '@kbn/test'; import type { Client } from '@elastic/elasticsearch'; import { createPromiseFromStreams, concatStreamProviders } from '@kbn/utils'; @@ -85,15 +86,17 @@ export async function loadAction({ progress.deactivate(); const result = stats.toJSON(); + const indicesWithDocs: string[] = []; for (const [index, { docs }] of Object.entries(result)) { if (docs && docs.indexed > 0) { log.info('[%s] Indexed %d docs into %j', name, docs.indexed, index); + indicesWithDocs.push(index); } } await client.indices.refresh( { - index: '_all', + index: indicesWithDocs.join(','), allow_no_indices: true, }, { diff --git a/packages/kbn-es-archiver/src/actions/rebuild_all.ts b/packages/kbn-es-archiver/src/actions/rebuild_all.ts index 360fdb438f2db..27fcae0c7cec5 100644 --- a/packages/kbn-es-archiver/src/actions/rebuild_all.ts +++ b/packages/kbn-es-archiver/src/actions/rebuild_all.ts @@ -10,8 +10,8 @@ import { resolve, relative } from 'path'; import { Stats, createReadStream, createWriteStream } from 'fs'; import { stat, rename } from 'fs/promises'; import { Readable, Writable } from 'stream'; -import { ToolingLog, REPO_ROOT } from '@kbn/dev-utils'; -import { createPromiseFromStreams } from '@kbn/utils'; +import { ToolingLog } from '@kbn/dev-utils'; +import { createPromiseFromStreams, REPO_ROOT } from '@kbn/utils'; import { prioritizeMappings, readDirectory, diff --git a/packages/kbn-es-archiver/src/actions/save.ts b/packages/kbn-es-archiver/src/actions/save.ts index 9cb5be05ac060..e5e3f06b8436d 100644 --- a/packages/kbn-es-archiver/src/actions/save.ts +++ b/packages/kbn-es-archiver/src/actions/save.ts @@ -10,8 +10,8 @@ import { resolve, relative } from 'path'; import { createWriteStream, mkdirSync } from 'fs'; import { Readable, Writable } from 'stream'; import type { Client } from '@elastic/elasticsearch'; -import { ToolingLog, REPO_ROOT } from '@kbn/dev-utils'; -import { createListStream, createPromiseFromStreams } from '@kbn/utils'; +import { ToolingLog } from '@kbn/dev-utils'; +import { createListStream, createPromiseFromStreams, REPO_ROOT } from '@kbn/utils'; import { createStats, diff --git a/packages/kbn-es-archiver/src/actions/unload.ts b/packages/kbn-es-archiver/src/actions/unload.ts index 1c5f4cd5d7d03..22830b7289174 100644 --- a/packages/kbn-es-archiver/src/actions/unload.ts +++ b/packages/kbn-es-archiver/src/actions/unload.ts @@ -10,9 +10,9 @@ import { resolve, relative } from 'path'; import { createReadStream } from 'fs'; import { Readable, Writable } from 'stream'; import type { Client } from '@elastic/elasticsearch'; -import { ToolingLog, REPO_ROOT } from '@kbn/dev-utils'; +import { ToolingLog } from '@kbn/dev-utils'; import { KbnClient } from '@kbn/test'; -import { createPromiseFromStreams } from '@kbn/utils'; +import { createPromiseFromStreams, REPO_ROOT } from '@kbn/utils'; import { isGzip, diff --git a/packages/kbn-es-archiver/src/es_archiver.ts b/packages/kbn-es-archiver/src/es_archiver.ts index 354197a98fa46..e13e20f25a703 100644 --- a/packages/kbn-es-archiver/src/es_archiver.ts +++ b/packages/kbn-es-archiver/src/es_archiver.ts @@ -10,7 +10,8 @@ import Fs from 'fs'; import Path from 'path'; import type { Client } from '@elastic/elasticsearch'; -import { ToolingLog, REPO_ROOT } from '@kbn/dev-utils'; +import { ToolingLog } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import { KbnClient } from '@kbn/test'; import { diff --git a/packages/kbn-es-archiver/src/lib/docs/generate_doc_records_stream.test.ts b/packages/kbn-es-archiver/src/lib/docs/generate_doc_records_stream.test.ts index ae21649690a99..2590074a25411 100644 --- a/packages/kbn-es-archiver/src/lib/docs/generate_doc_records_stream.test.ts +++ b/packages/kbn-es-archiver/src/lib/docs/generate_doc_records_stream.test.ts @@ -6,13 +6,14 @@ * Side Public License, v 1. */ +import { ToolingLog } from '@kbn/dev-utils'; + import { createListStream, createPromiseFromStreams, createConcatStream, createMapStream, - ToolingLog, -} from '@kbn/dev-utils'; +} from '@kbn/utils'; import { createGenerateDocRecordsStream } from './generate_doc_records_stream'; import { Progress } from '../progress'; diff --git a/packages/kbn-es-archiver/src/lib/docs/index_doc_records_stream.test.ts b/packages/kbn-es-archiver/src/lib/docs/index_doc_records_stream.test.ts index bcf28a4976a1c..9c0ff4a8f91ec 100644 --- a/packages/kbn-es-archiver/src/lib/docs/index_doc_records_stream.test.ts +++ b/packages/kbn-es-archiver/src/lib/docs/index_doc_records_stream.test.ts @@ -6,12 +6,9 @@ * Side Public License, v 1. */ -import { - createListStream, - createPromiseFromStreams, - ToolingLog, - createRecursiveSerializer, -} from '@kbn/dev-utils'; +import { ToolingLog, createRecursiveSerializer } from '@kbn/dev-utils'; + +import { createListStream, createPromiseFromStreams } from '@kbn/utils'; import { Progress } from '../progress'; import { createIndexDocRecordsStream } from './index_doc_records_stream'; diff --git a/packages/kbn-es-query/BUILD.bazel b/packages/kbn-es-query/BUILD.bazel index 70d8d659c99fe..86f3d3ccc13a8 100644 --- a/packages/kbn-es-query/BUILD.bazel +++ b/packages/kbn-es-query/BUILD.bazel @@ -1,10 +1,11 @@ -load("@npm//@bazel/typescript:index.bzl", "ts_config", "ts_project") +load("@npm//@bazel/typescript:index.bzl", "ts_config") load("@npm//peggy:index.bzl", "peggy") -load("@build_bazel_rules_nodejs//:index.bzl", "js_library", "pkg_npm") -load("//src/dev/bazel:index.bzl", "jsts_transpiler") +load("@build_bazel_rules_nodejs//:index.bzl", "js_library") +load("//src/dev/bazel:index.bzl", "jsts_transpiler", "pkg_npm", "pkg_npm_types", "ts_project") PKG_BASE_NAME = "kbn-es-query" PKG_REQUIRE_NAME = "@kbn/es-query" +TYPES_PKG_REQUIRE_NAME = "@types/kbn__es-query" SOURCE_FILES = glob( [ @@ -104,7 +105,7 @@ ts_project( js_library( name = PKG_BASE_NAME, srcs = NPM_MODULE_EXTRA_FILES + [":grammar"], - deps = RUNTIME_DEPS + [":target_node", ":target_web", ":tsc_types"], + deps = RUNTIME_DEPS + [":target_node", ":target_web"], package_name = PKG_REQUIRE_NAME, visibility = ["//visibility:public"], ) @@ -123,3 +124,20 @@ filegroup( ], visibility = ["//visibility:public"], ) + +pkg_npm_types( + name = "npm_module_types", + srcs = SRCS, + deps = [":tsc_types"], + package_name = TYPES_PKG_REQUIRE_NAME, + tsconfig = ":tsconfig", + visibility = ["//visibility:public"], +) + +filegroup( + name = "build_types", + srcs = [ + ":npm_module_types", + ], + visibility = ["//visibility:public"], +) diff --git a/packages/kbn-es-query/package.json b/packages/kbn-es-query/package.json index 335ef61b8b360..b317ce4ca4c95 100644 --- a/packages/kbn-es-query/package.json +++ b/packages/kbn-es-query/package.json @@ -2,7 +2,6 @@ "name": "@kbn/es-query", "browser": "./target_web/index.js", "main": "./target_node/index.js", - "types": "./target_types/index.d.ts", "version": "1.0.0", "license": "SSPL-1.0 OR Elastic License 2.0", "private": true diff --git a/packages/kbn-es-query/src/kuery/index.ts b/packages/kbn-es-query/src/kuery/index.ts index 868904125dc44..13039956916cb 100644 --- a/packages/kbn-es-query/src/kuery/index.ts +++ b/packages/kbn-es-query/src/kuery/index.ts @@ -23,4 +23,5 @@ export const toElasticsearchQuery = (...params: Parameters { it('should return artifact metadata for the correct architecture', async () => { const artifact = await Artifact.getSnapshot('oss', MOCK_VERSION, log); - expect(artifact.getFilename()).toEqual(MOCK_FILENAME + `-${ARCHITECTURE}.oss`); + expect(artifact.spec.filename).toEqual(MOCK_FILENAME + `-${ARCHITECTURE}.oss`); }); }); @@ -182,7 +182,7 @@ describe('Artifact', () => { describe('with latest unverified snapshot', () => { beforeEach(() => { - process.env.KBN_ES_SNAPSHOT_USE_UNVERIFIED = 1; + process.env.KBN_ES_SNAPSHOT_USE_UNVERIFIED = '1'; mockFetch(MOCKS.valid); }); diff --git a/packages/kbn-es/src/artifact.js b/packages/kbn-es/src/artifact.ts similarity index 65% rename from packages/kbn-es/src/artifact.js rename to packages/kbn-es/src/artifact.ts index 0fa2c7a1727d0..9c5935c96e8cd 100644 --- a/packages/kbn-es/src/artifact.js +++ b/packages/kbn-es/src/artifact.ts @@ -6,25 +6,69 @@ * Side Public License, v 1. */ -const fetch = require('node-fetch'); -const AbortController = require('abort-controller'); -const fs = require('fs'); -const { promisify } = require('util'); -const { pipeline, Transform } = require('stream'); -const chalk = require('chalk'); -const { createHash } = require('crypto'); -const path = require('path'); +import fs from 'fs'; +import { promisify } from 'util'; +import path from 'path'; +import { createHash } from 'crypto'; +import { pipeline, Transform } from 'stream'; +import { setTimeout } from 'timers/promises'; + +import fetch, { Headers } from 'node-fetch'; +import AbortController from 'abort-controller'; +import chalk from 'chalk'; +import { ToolingLog } from '@kbn/dev-utils'; + +import { cache } from './utils/cache'; +import { resolveCustomSnapshotUrl } from './custom_snapshots'; +import { createCliError, isCliError } from './errors'; const asyncPipeline = promisify(pipeline); const DAILY_SNAPSHOTS_BASE_URL = 'https://storage.googleapis.com/kibana-ci-es-snapshots-daily'; const PERMANENT_SNAPSHOTS_BASE_URL = 'https://storage.googleapis.com/kibana-ci-es-snapshots-permanent'; -const { cache } = require('./utils'); -const { resolveCustomSnapshotUrl } = require('./custom_snapshots'); -const { createCliError, isCliError } = require('./errors'); +type ChecksumType = 'sha512'; +export type ArtifactLicense = 'oss' | 'basic' | 'trial'; + +interface ArtifactManifest { + id: string; + bucket: string; + branch: string; + sha: string; + sha_short: string; + version: string; + generated: string; + archives: Array<{ + filename: string; + checksum: string; + url: string; + version: string; + platform: string; + architecture: string; + license: string; + }>; +} + +export interface ArtifactSpec { + url: string; + checksumUrl: string; + checksumType: ChecksumType; + filename: string; +} + +interface ArtifactDownloaded { + cached: false; + checksum: string; + etag?: string; + contentLength: number; + first500Bytes: Buffer; + headers: Headers; +} +interface ArtifactCached { + cached: true; +} -function getChecksumType(checksumUrl) { +function getChecksumType(checksumUrl: string): ChecksumType { if (checksumUrl.endsWith('.sha512')) { return 'sha512'; } @@ -32,15 +76,18 @@ function getChecksumType(checksumUrl) { throw new Error(`unable to determine checksum type: ${checksumUrl}`); } -function headersToString(headers, indent = '') { +function headersToString(headers: Headers, indent = '') { return [...headers.entries()].reduce( (acc, [key, value]) => `${acc}\n${indent}${key}: ${value}`, '' ); } -async function retry(log, fn) { - async function doAttempt(attempt) { +async function retry(log: ToolingLog, fn: () => Promise): Promise { + let attempt = 0; + while (true) { + attempt += 1; + try { return await fn(); } catch (error) { @@ -49,13 +96,10 @@ async function retry(log, fn) { } log.warning('...failure, retrying in 5 seconds:', error.message); - await new Promise((resolve) => setTimeout(resolve, 5000)); + await setTimeout(5000); log.info('...retrying'); - return await doAttempt(attempt + 1); } } - - return await doAttempt(1); } // Setting this flag provides an easy way to run the latest un-promoted snapshot without having to look it up @@ -63,7 +107,7 @@ function shouldUseUnverifiedSnapshot() { return !!process.env.KBN_ES_SNAPSHOT_USE_UNVERIFIED; } -async function fetchSnapshotManifest(url, log) { +async function fetchSnapshotManifest(url: string, log: ToolingLog) { log.info('Downloading snapshot manifest from %s', chalk.bold(url)); const abc = new AbortController(); @@ -73,7 +117,11 @@ async function fetchSnapshotManifest(url, log) { return { abc, resp, json }; } -async function getArtifactSpecForSnapshot(urlVersion, license, log) { +async function getArtifactSpecForSnapshot( + urlVersion: string, + license: string, + log: ToolingLog +): Promise { const desiredVersion = urlVersion.replace('-SNAPSHOT', ''); const desiredLicense = license === 'oss' ? 'oss' : 'default'; @@ -103,17 +151,16 @@ async function getArtifactSpecForSnapshot(urlVersion, license, log) { throw new Error(`Unable to read snapshot manifest: ${resp.statusText}\n ${json}`); } - const manifest = JSON.parse(json); - + const manifest: ArtifactManifest = JSON.parse(json); const platform = process.platform === 'win32' ? 'windows' : process.platform; const arch = process.arch === 'arm64' ? 'aarch64' : 'x86_64'; const archive = manifest.archives.find( - (archive) => - archive.version === desiredVersion && - archive.platform === platform && - archive.license === desiredLicense && - archive.architecture === arch + (a) => + a.version === desiredVersion && + a.platform === platform && + a.license === desiredLicense && + a.architecture === arch ); if (!archive) { @@ -130,93 +177,65 @@ async function getArtifactSpecForSnapshot(urlVersion, license, log) { }; } -exports.Artifact = class Artifact { +export class Artifact { /** * Fetch an Artifact from the Artifact API for a license level and version - * @param {('oss'|'basic'|'trial')} license - * @param {string} version - * @param {ToolingLog} log */ - static async getSnapshot(license, version, log) { + static async getSnapshot(license: ArtifactLicense, version: string, log: ToolingLog) { const urlVersion = `${encodeURIComponent(version)}-SNAPSHOT`; const customSnapshotArtifactSpec = resolveCustomSnapshotUrl(urlVersion, license); if (customSnapshotArtifactSpec) { - return new Artifact(customSnapshotArtifactSpec, log); + return new Artifact(log, customSnapshotArtifactSpec); } const artifactSpec = await getArtifactSpecForSnapshot(urlVersion, license, log); - return new Artifact(artifactSpec, log); + return new Artifact(log, artifactSpec); } /** * Fetch an Artifact from the Elasticsearch past releases url - * @param {string} url - * @param {ToolingLog} log */ - static async getArchive(url, log) { + static async getArchive(url: string, log: ToolingLog) { const shaUrl = `${url}.sha512`; - const artifactSpec = { - url: url, + return new Artifact(log, { + url, filename: path.basename(url), checksumUrl: shaUrl, checksumType: getChecksumType(shaUrl), - }; - - return new Artifact(artifactSpec, log); - } - - constructor(spec, log) { - this._spec = spec; - this._log = log; - } - - getUrl() { - return this._spec.url; - } - - getChecksumUrl() { - return this._spec.checksumUrl; + }); } - getChecksumType() { - return this._spec.checksumType; - } - - getFilename() { - return this._spec.filename; - } + constructor(private readonly log: ToolingLog, public readonly spec: ArtifactSpec) {} /** * Download the artifact to disk, skips the download if the cache is * up-to-date, verifies checksum when downloaded - * @param {string} dest - * @return {Promise} */ - async download(dest, { useCached = false }) { - await retry(this._log, async () => { + async download(dest: string, { useCached = false }: { useCached?: boolean } = {}) { + await retry(this.log, async () => { const cacheMeta = cache.readMeta(dest); const tmpPath = `${dest}.tmp`; if (useCached) { if (cacheMeta.exists) { - this._log.info( + this.log.info( 'use-cached passed, forcing to use existing snapshot', chalk.bold(cacheMeta.ts) ); return; } else { - this._log.info('use-cached passed but no cached snapshot found. Continuing to download'); + this.log.info('use-cached passed but no cached snapshot found. Continuing to download'); } } - const artifactResp = await this._download(tmpPath, cacheMeta.etag, cacheMeta.ts); + const artifactResp = await this.fetchArtifact(tmpPath, cacheMeta.etag, cacheMeta.ts); if (artifactResp.cached) { return; } - await this._verifyChecksum(artifactResp); + await this.verifyChecksum(artifactResp); // cache the etag for future downloads cache.writeMeta(dest, { etag: artifactResp.etag }); @@ -228,18 +247,18 @@ exports.Artifact = class Artifact { /** * Fetch the artifact with an etag - * @param {string} tmpPath - * @param {string} etag - * @param {string} ts - * @return {{ cached: true }|{ checksum: string, etag: string, first500Bytes: Buffer }} */ - async _download(tmpPath, etag, ts) { - const url = this.getUrl(); + private async fetchArtifact( + tmpPath: string, + etag: string, + ts: string + ): Promise { + const url = this.spec.url; if (etag) { - this._log.info('verifying cache of %s', chalk.bold(url)); + this.log.info('verifying cache of %s', chalk.bold(url)); } else { - this._log.info('downloading artifact from %s', chalk.bold(url)); + this.log.info('downloading artifact from %s', chalk.bold(url)); } const abc = new AbortController(); @@ -251,7 +270,7 @@ exports.Artifact = class Artifact { }); if (resp.status === 304) { - this._log.info('etags match, reusing cache from %s', chalk.bold(ts)); + this.log.info('etags match, reusing cache from %s', chalk.bold(ts)); abc.abort(); return { @@ -270,10 +289,10 @@ exports.Artifact = class Artifact { } if (etag) { - this._log.info('cache invalid, redownloading'); + this.log.info('cache invalid, redownloading'); } - const hash = createHash(this.getChecksumType()); + const hash = createHash(this.spec.checksumType); let first500Bytes = Buffer.alloc(0); let contentLength = 0; @@ -300,8 +319,9 @@ exports.Artifact = class Artifact { ); return { + cached: false, checksum: hash.digest('hex'), - etag: resp.headers.get('etag'), + etag: resp.headers.get('etag') ?? undefined, contentLength, first500Bytes, headers: resp.headers, @@ -310,14 +330,12 @@ exports.Artifact = class Artifact { /** * Verify the checksum of the downloaded artifact with the checksum at checksumUrl - * @param {{ checksum: string, contentLength: number, first500Bytes: Buffer }} artifactResp - * @return {Promise} */ - async _verifyChecksum(artifactResp) { - this._log.info('downloading artifact checksum from %s', chalk.bold(this.getChecksumUrl())); + private async verifyChecksum(artifactResp: ArtifactDownloaded) { + this.log.info('downloading artifact checksum from %s', chalk.bold(this.spec.checksumUrl)); const abc = new AbortController(); - const resp = await fetch(this.getChecksumUrl(), { + const resp = await fetch(this.spec.checksumUrl, { signal: abc.signal, }); @@ -338,7 +356,7 @@ exports.Artifact = class Artifact { const lenString = `${len} / ${artifactResp.contentLength}`; throw createCliError( - `artifact downloaded from ${this.getUrl()} does not match expected checksum\n` + + `artifact downloaded from ${this.spec.url} does not match expected checksum\n` + ` expected: ${expectedChecksum}\n` + ` received: ${artifactResp.checksum}\n` + ` headers: ${headersToString(artifactResp.headers, ' ')}\n` + @@ -346,6 +364,6 @@ exports.Artifact = class Artifact { ); } - this._log.info('checksum verified'); + this.log.info('checksum verified'); } -}; +} diff --git a/packages/kbn-es/src/custom_snapshots.js b/packages/kbn-es/src/custom_snapshots.ts similarity index 82% rename from packages/kbn-es/src/custom_snapshots.js rename to packages/kbn-es/src/custom_snapshots.ts index 9dd8097244947..f3e6d3ecaf857 100644 --- a/packages/kbn-es/src/custom_snapshots.js +++ b/packages/kbn-es/src/custom_snapshots.ts @@ -6,13 +6,15 @@ * Side Public License, v 1. */ -const { basename } = require('path'); +import Path from 'path'; -function isVersionFlag(a) { +import type { ArtifactSpec } from './artifact'; + +function isVersionFlag(a: string) { return a.startsWith('--version'); } -function getCustomSnapshotUrl() { +export function getCustomSnapshotUrl() { // force use of manually created snapshots until ReindexPutMappings fix if ( !process.env.ES_SNAPSHOT_MANIFEST && @@ -28,7 +30,10 @@ function getCustomSnapshotUrl() { } } -function resolveCustomSnapshotUrl(urlVersion, license) { +export function resolveCustomSnapshotUrl( + urlVersion: string, + license: string +): ArtifactSpec | undefined { const customSnapshotUrl = getCustomSnapshotUrl(); if (!customSnapshotUrl) { @@ -48,8 +53,6 @@ function resolveCustomSnapshotUrl(urlVersion, license) { url: overrideUrl, checksumUrl: overrideUrl + '.sha512', checksumType: 'sha512', - filename: basename(overrideUrl), + filename: Path.basename(overrideUrl), }; } - -module.exports = { getCustomSnapshotUrl, resolveCustomSnapshotUrl }; diff --git a/packages/kbn-es/src/errors.js b/packages/kbn-es/src/errors.js deleted file mode 100644 index 87490168bf5ee..0000000000000 --- a/packages/kbn-es/src/errors.js +++ /dev/null @@ -1,17 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0 and the Server Side Public License, v 1; you may not use this file except - * in compliance with, at your election, the Elastic License 2.0 or the Server - * Side Public License, v 1. - */ - -exports.createCliError = function (message) { - const error = new Error(message); - error.isCliError = true; - return error; -}; - -exports.isCliError = function (error) { - return error && error.isCliError; -}; diff --git a/packages/kbn-es/src/errors.ts b/packages/kbn-es/src/errors.ts new file mode 100644 index 0000000000000..a0c526dc48a9c --- /dev/null +++ b/packages/kbn-es/src/errors.ts @@ -0,0 +1,25 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +interface CliError extends Error { + isCliError: boolean; +} + +export function createCliError(message: string) { + return Object.assign(new Error(message), { + isCliError: true, + }); +} + +function isObj(x: unknown): x is Record { + return typeof x === 'object' && x !== null; +} + +export function isCliError(error: unknown): error is CliError { + return isObj(error) && error.isCliError === true; +} diff --git a/packages/kbn-es/src/index.js b/packages/kbn-es/src/index.ts similarity index 72% rename from packages/kbn-es/src/index.js rename to packages/kbn-es/src/index.ts index 3b12de68234fa..68fd931794c0c 100644 --- a/packages/kbn-es/src/index.js +++ b/packages/kbn-es/src/index.ts @@ -6,5 +6,7 @@ * Side Public License, v 1. */ -exports.run = require('./cli').run; -exports.Cluster = require('./cluster').Cluster; +// @ts-expect-error not typed yet +export { run } from './cli'; +// @ts-expect-error not typed yet +export { Cluster } from './cluster'; diff --git a/src/plugins/discover/public/utils/get_single_doc_url.ts b/packages/kbn-es/src/install/index.ts similarity index 65% rename from src/plugins/discover/public/utils/get_single_doc_url.ts rename to packages/kbn-es/src/install/index.ts index 913463e6d44a4..e827dee2247f9 100644 --- a/src/plugins/discover/public/utils/get_single_doc_url.ts +++ b/packages/kbn-es/src/install/index.ts @@ -6,6 +6,6 @@ * Side Public License, v 1. */ -export const getSingleDocUrl = (indexPatternId: string, rowIndex: string, rowId: string) => { - return `/app/discover#/doc/${indexPatternId}/${rowIndex}?id=${encodeURIComponent(rowId)}`; -}; +export { installArchive } from './install_archive'; +export { installSnapshot, downloadSnapshot } from './install_snapshot'; +export { installSource } from './install_source'; diff --git a/packages/kbn-es/src/install/archive.js b/packages/kbn-es/src/install/install_archive.ts similarity index 64% rename from packages/kbn-es/src/install/archive.js rename to packages/kbn-es/src/install/install_archive.ts index 76db5a4427e6d..ee04d9e4b62b5 100644 --- a/packages/kbn-es/src/install/archive.js +++ b/packages/kbn-es/src/install/install_archive.ts @@ -6,29 +6,40 @@ * Side Public License, v 1. */ -const fs = require('fs'); -const path = require('path'); -const chalk = require('chalk'); -const execa = require('execa'); -const del = require('del'); -const url = require('url'); -const { extract } = require('@kbn/dev-utils'); -const { log: defaultLog } = require('../utils'); -const { BASE_PATH, ES_CONFIG, ES_KEYSTORE_BIN } = require('../paths'); -const { Artifact } = require('../artifact'); -const { parseSettings, SettingsFilter } = require('../settings'); +import fs from 'fs'; +import path from 'path'; + +import chalk from 'chalk'; +import execa from 'execa'; +import del from 'del'; +import { extract, ToolingLog } from '@kbn/dev-utils'; + +import { BASE_PATH, ES_CONFIG, ES_KEYSTORE_BIN } from '../paths'; +import { Artifact } from '../artifact'; +import { parseSettings, SettingsFilter } from '../settings'; +import { log as defaultLog } from '../utils/log'; + +interface InstallArchiveOptions { + license?: string; + password?: string; + basePath?: string; + installPath?: string; + log?: ToolingLog; + esArgs?: string[]; +} + +const isHttpUrl = (str: string) => { + try { + return ['http:', 'https:'].includes(new URL(str).protocol); + } catch { + return false; + } +}; /** * Extracts an ES archive and optionally installs plugins - * - * @param {String} archive - path to tar - * @param {Object} options - * @property {('oss'|'basic'|'trial')} options.license - * @property {String} options.basePath - * @property {String} options.installPath - * @property {ToolingLog} options.log */ -exports.installArchive = async function installArchive(archive, options = {}) { +export async function installArchive(archive: string, options: InstallArchiveOptions = {}) { const { license = 'basic', password = 'changeme', @@ -39,9 +50,9 @@ exports.installArchive = async function installArchive(archive, options = {}) { } = options; let dest = archive; - if (['http:', 'https:'].includes(url.parse(archive).protocol)) { + if (isHttpUrl(archive)) { const artifact = await Artifact.getArchive(archive, log); - dest = path.resolve(basePath, 'cache', artifact.getFilename()); + dest = path.resolve(basePath, 'cache', artifact.spec.filename); await artifact.download(dest); } @@ -75,28 +86,23 @@ exports.installArchive = async function installArchive(archive, options = {}) { } return { installPath }; -}; +} /** * Appends single line to elasticsearch.yml config file - * - * @param {String} installPath - * @param {String} key - * @param {String} value */ -async function appendToConfig(installPath, key, value) { +async function appendToConfig(installPath: string, key: string, value: string) { fs.appendFileSync(path.resolve(installPath, ES_CONFIG), `${key}: ${value}\n`, 'utf8'); } /** * Creates and configures Keystore - * - * @param {String} installPath - * @param {ToolingLog} log - * @param {Array<[string, string]>} secureSettings List of custom Elasticsearch secure settings to - * add into the keystore. */ -async function configureKeystore(installPath, log = defaultLog, secureSettings) { +async function configureKeystore( + installPath: string, + log: ToolingLog = defaultLog, + secureSettings: Array<[string, string]> +) { const env = { JAVA_HOME: '' }; await execa(ES_KEYSTORE_BIN, ['create'], { cwd: installPath, env }); diff --git a/packages/kbn-es/src/install/snapshot.js b/packages/kbn-es/src/install/install_snapshot.ts similarity index 55% rename from packages/kbn-es/src/install/snapshot.js rename to packages/kbn-es/src/install/install_snapshot.ts index cf1ce50f7e413..84d713745eb82 100644 --- a/packages/kbn-es/src/install/snapshot.js +++ b/packages/kbn-es/src/install/install_snapshot.ts @@ -6,56 +6,58 @@ * Side Public License, v 1. */ -const chalk = require('chalk'); -const path = require('path'); -const { BASE_PATH } = require('../paths'); -const { installArchive } = require('./archive'); -const { log: defaultLog } = require('../utils'); -const { Artifact } = require('../artifact'); +import path from 'path'; + +import chalk from 'chalk'; +import { ToolingLog } from '@kbn/dev-utils'; + +import { BASE_PATH } from '../paths'; +import { installArchive } from './install_archive'; +import { log as defaultLog } from '../utils/log'; +import { Artifact, ArtifactLicense } from '../artifact'; + +interface DownloadSnapshotOptions { + version: string; + license?: ArtifactLicense; + basePath?: string; + installPath?: string; + log?: ToolingLog; + useCached?: boolean; +} /** * Download an ES snapshot - * - * @param {Object} options - * @property {('oss'|'basic'|'trial')} options.license - * @property {String} options.version - * @property {String} options.basePath - * @property {String} options.installPath - * @property {ToolingLog} options.log */ -exports.downloadSnapshot = async function installSnapshot({ +export async function downloadSnapshot({ license = 'basic', version, basePath = BASE_PATH, installPath = path.resolve(basePath, version), log = defaultLog, useCached = false, -}) { +}: DownloadSnapshotOptions) { log.info('version: %s', chalk.bold(version)); log.info('install path: %s', chalk.bold(installPath)); log.info('license: %s', chalk.bold(license)); const artifact = await Artifact.getSnapshot(license, version, log); - const dest = path.resolve(basePath, 'cache', artifact.getFilename()); + const dest = path.resolve(basePath, 'cache', artifact.spec.filename); await artifact.download(dest, { useCached }); return { downloadPath: dest, }; -}; +} + +interface InstallSnapshotOptions extends DownloadSnapshotOptions { + password?: string; + esArgs?: string[]; +} /** * Installs ES from snapshot - * - * @param {Object} options - * @property {('oss'|'basic'|'trial')} options.license - * @property {String} options.password - * @property {String} options.version - * @property {String} options.basePath - * @property {String} options.installPath - * @property {ToolingLog} options.log */ -exports.installSnapshot = async function installSnapshot({ +export async function installSnapshot({ license = 'basic', password = 'password', version, @@ -64,8 +66,8 @@ exports.installSnapshot = async function installSnapshot({ log = defaultLog, esArgs, useCached = false, -}) { - const { downloadPath } = await exports.downloadSnapshot({ +}: InstallSnapshotOptions) { + const { downloadPath } = await downloadSnapshot({ license, version, basePath, @@ -82,4 +84,4 @@ exports.installSnapshot = async function installSnapshot({ log, esArgs, }); -}; +} diff --git a/packages/kbn-es/src/install/source.js b/packages/kbn-es/src/install/install_source.ts similarity index 73% rename from packages/kbn-es/src/install/source.js rename to packages/kbn-es/src/install/install_source.ts index 81a1019509906..d8c272677058e 100644 --- a/packages/kbn-es/src/install/source.js +++ b/packages/kbn-es/src/install/install_source.ts @@ -6,28 +6,35 @@ * Side Public License, v 1. */ -const path = require('path'); -const fs = require('fs'); -const os = require('os'); -const chalk = require('chalk'); -const crypto = require('crypto'); -const simpleGit = require('simple-git/promise'); -const { installArchive } = require('./archive'); -const { log: defaultLog, cache, buildSnapshot, archiveForPlatform } = require('../utils'); -const { BASE_PATH } = require('../paths'); +import path from 'path'; +import fs from 'fs'; +import os from 'os'; +import crypto from 'crypto'; + +import chalk from 'chalk'; +import simpleGit from 'simple-git/promise'; +import { ToolingLog } from '@kbn/dev-utils'; + +import { installArchive } from './install_archive'; +import { log as defaultLog } from '../utils/log'; +import { cache } from '../utils/cache'; +import { buildSnapshot, archiveForPlatform } from '../utils/build_snapshot'; +import { BASE_PATH } from '../paths'; + +interface InstallSourceOptions { + sourcePath: string; + license?: string; + password?: string; + basePath?: string; + installPath?: string; + log?: ToolingLog; + esArgs?: string[]; +} /** * Installs ES from source - * - * @param {Object} options - * @property {('oss'|'basic'|'trial')} options.license - * @property {String} options.password - * @property {String} options.sourcePath - * @property {String} options.basePath - * @property {String} options.installPath - * @property {ToolingLog} options.log */ -exports.installSource = async function installSource({ +export async function installSource({ license = 'basic', password = 'changeme', sourcePath, @@ -35,7 +42,7 @@ exports.installSource = async function installSource({ installPath = path.resolve(basePath, 'source'), log = defaultLog, esArgs, -}) { +}: InstallSourceOptions) { log.info('source path: %s', chalk.bold(sourcePath)); log.info('install path: %s', chalk.bold(installPath)); log.info('license: %s', chalk.bold(license)); @@ -62,14 +69,9 @@ exports.installSource = async function installSource({ log, esArgs, }); -}; +} -/** - * - * @param {String} cwd - * @param {ToolingLog} log - */ -async function sourceInfo(cwd, license, log = defaultLog) { +async function sourceInfo(cwd: string, license: string, log: ToolingLog = defaultLog) { if (!fs.existsSync(cwd)) { throw new Error(`${cwd} does not exist`); } diff --git a/packages/kbn-es/src/paths.js b/packages/kbn-es/src/paths.js deleted file mode 100644 index 5c8d3b654ecf9..0000000000000 --- a/packages/kbn-es/src/paths.js +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0 and the Server Side Public License, v 1; you may not use this file except - * in compliance with, at your election, the Elastic License 2.0 or the Server - * Side Public License, v 1. - */ - -const os = require('os'); -const path = require('path'); - -function maybeUseBat(bin) { - return os.platform().startsWith('win') ? `${bin}.bat` : bin; -} - -const tempDir = os.tmpdir(); - -exports.BASE_PATH = path.resolve(tempDir, 'kbn-es'); - -exports.GRADLE_BIN = maybeUseBat('./gradlew'); -exports.ES_BIN = maybeUseBat('bin/elasticsearch'); -exports.ES_CONFIG = 'config/elasticsearch.yml'; - -exports.ES_KEYSTORE_BIN = maybeUseBat('./bin/elasticsearch-keystore'); diff --git a/packages/kbn-es/src/paths.ts b/packages/kbn-es/src/paths.ts new file mode 100644 index 0000000000000..c1b859af4e1f5 --- /dev/null +++ b/packages/kbn-es/src/paths.ts @@ -0,0 +1,24 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import Os from 'os'; +import Path from 'path'; + +function maybeUseBat(bin: string) { + return Os.platform().startsWith('win') ? `${bin}.bat` : bin; +} + +const tempDir = Os.tmpdir(); + +export const BASE_PATH = Path.resolve(tempDir, 'kbn-es'); + +export const GRADLE_BIN = maybeUseBat('./gradlew'); +export const ES_BIN = maybeUseBat('bin/elasticsearch'); +export const ES_CONFIG = 'config/elasticsearch.yml'; + +export const ES_KEYSTORE_BIN = maybeUseBat('./bin/elasticsearch-keystore'); diff --git a/packages/kbn-es/src/utils/build_snapshot.js b/packages/kbn-es/src/utils/build_snapshot.ts similarity index 53% rename from packages/kbn-es/src/utils/build_snapshot.js rename to packages/kbn-es/src/utils/build_snapshot.ts index ec26ba69e658b..542e63dcc0748 100644 --- a/packages/kbn-es/src/utils/build_snapshot.js +++ b/packages/kbn-es/src/utils/build_snapshot.ts @@ -6,25 +6,25 @@ * Side Public License, v 1. */ -const execa = require('execa'); -const path = require('path'); -const os = require('os'); -const readline = require('readline'); -const { createCliError } = require('../errors'); -const { findMostRecentlyChanged } = require('../utils'); -const { GRADLE_BIN } = require('../paths'); +import path from 'path'; +import os from 'os'; -const onceEvent = (emitter, event) => new Promise((resolve) => emitter.once(event, resolve)); +import { ToolingLog, withProcRunner } from '@kbn/dev-utils'; + +import { createCliError } from '../errors'; +import { findMostRecentlyChanged } from './find_most_recently_changed'; +import { GRADLE_BIN } from '../paths'; + +interface BuildSnapshotOptions { + license: string; + sourcePath: string; + log: ToolingLog; + platform?: string; +} /** * Creates archive from source * - * @param {Object} options - * @property {('oss'|'basic'|'trial')} options.license - * @property {String} options.sourcePath - * @property {ToolingLog} options.log - * @returns {Object} containing archive and optional plugins - * * Gradle tasks: * $ ./gradlew tasks --all | grep 'distribution.*assemble\s' * :distribution:archives:darwin-tar:assemble @@ -34,39 +34,27 @@ const onceEvent = (emitter, event) => new Promise((resolve) => emitter.once(even * :distribution:archives:oss-linux-tar:assemble * :distribution:archives:oss-windows-zip:assemble */ -exports.buildSnapshot = async ({ license, sourcePath, log, platform = os.platform() }) => { +export async function buildSnapshot({ + license, + sourcePath, + log, + platform = os.platform(), +}: BuildSnapshotOptions) { const { task, ext } = exports.archiveForPlatform(platform, license); const buildArgs = [`:distribution:archives:${task}:assemble`]; log.info('%s %s', GRADLE_BIN, buildArgs.join(' ')); log.debug('cwd:', sourcePath); - const build = execa(GRADLE_BIN, buildArgs, { - cwd: sourcePath, - stdio: ['ignore', 'pipe', 'pipe'], + await withProcRunner(log, async (procs) => { + await procs.run('gradle', { + cmd: GRADLE_BIN, + args: buildArgs, + cwd: sourcePath, + wait: true, + }); }); - const stdout = readline.createInterface({ input: build.stdout }); - const stderr = readline.createInterface({ input: build.stderr }); - - stdout.on('line', (line) => log.debug(line)); - stderr.on('line', (line) => log.error(line)); - - const [exitCode] = await Promise.all([ - Promise.race([ - onceEvent(build, 'exit'), - onceEvent(build, 'error').then((error) => { - throw createCliError(`Error spawning gradle: ${error.message}`); - }), - ]), - onceEvent(stdout, 'close'), - onceEvent(stderr, 'close'), - ]); - - if (exitCode > 0) { - throw createCliError('unable to build ES'); - } - const archivePattern = `distribution/archives/${task}/build/distributions/elasticsearch-*.${ext}`; const esArchivePath = findMostRecentlyChanged(path.resolve(sourcePath, archivePattern)); @@ -75,9 +63,9 @@ exports.buildSnapshot = async ({ license, sourcePath, log, platform = os.platfor } return esArchivePath; -}; +} -exports.archiveForPlatform = (platform, license) => { +export function archiveForPlatform(platform: NodeJS.Platform, license: string) { const taskPrefix = license === 'oss' ? 'oss-' : ''; switch (platform) { @@ -88,6 +76,6 @@ exports.archiveForPlatform = (platform, license) => { case 'linux': return { format: 'tar', ext: 'tar.gz', task: `${taskPrefix}linux-tar`, platform: 'linux' }; default: - throw new Error(`unknown platform: ${platform}`); + throw new Error(`unsupported platform: ${platform}`); } -}; +} diff --git a/packages/kbn-es/src/utils/cache.js b/packages/kbn-es/src/utils/cache.js deleted file mode 100644 index 248faf23bbc46..0000000000000 --- a/packages/kbn-es/src/utils/cache.js +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0 and the Server Side Public License, v 1; you may not use this file except - * in compliance with, at your election, the Elastic License 2.0 or the Server - * Side Public License, v 1. - */ - -const fs = require('fs'); -const path = require('path'); - -exports.readMeta = function readMeta(file) { - try { - const meta = fs.readFileSync(`${file}.meta`, { - encoding: 'utf8', - }); - - return { - exists: fs.existsSync(file), - ...JSON.parse(meta), - }; - } catch (e) { - if (e.code !== 'ENOENT') { - throw e; - } - - return { - exists: false, - }; - } -}; - -exports.writeMeta = function readMeta(file, details = {}) { - const meta = { - ts: new Date(), - ...details, - }; - - fs.mkdirSync(path.dirname(file), { recursive: true }); - fs.writeFileSync(`${file}.meta`, JSON.stringify(meta, null, 2)); -}; diff --git a/packages/kbn-es/src/utils/cache.ts b/packages/kbn-es/src/utils/cache.ts new file mode 100644 index 0000000000000..819119b6ce010 --- /dev/null +++ b/packages/kbn-es/src/utils/cache.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 + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import Fs from 'fs'; +import Path from 'path'; + +export const cache = { + readMeta(path: string) { + try { + const meta = Fs.readFileSync(`${path}.meta`, { + encoding: 'utf8', + }); + + return { + ...JSON.parse(meta), + }; + } catch (e) { + if (e.code !== 'ENOENT') { + throw e; + } + + return {}; + } + }, + + writeMeta(path: string, details = {}) { + const meta = { + ts: new Date(), + ...details, + }; + + Fs.mkdirSync(Path.dirname(path), { recursive: true }); + Fs.writeFileSync(`${path}.meta`, JSON.stringify(meta, null, 2)); + }, +}; diff --git a/packages/kbn-es/src/utils/find_most_recently_changed.test.js b/packages/kbn-es/src/utils/find_most_recently_changed.test.ts similarity index 93% rename from packages/kbn-es/src/utils/find_most_recently_changed.test.js rename to packages/kbn-es/src/utils/find_most_recently_changed.test.ts index 8198495e7197f..721e5baba7513 100644 --- a/packages/kbn-es/src/utils/find_most_recently_changed.test.js +++ b/packages/kbn-es/src/utils/find_most_recently_changed.test.ts @@ -6,6 +6,8 @@ * Side Public License, v 1. */ +import { findMostRecentlyChanged } from './find_most_recently_changed'; + jest.mock('fs', () => ({ statSync: jest.fn().mockImplementation((path) => { if (path.includes('oldest')) { @@ -31,8 +33,6 @@ jest.mock('fs', () => ({ }), })); -const { findMostRecentlyChanged } = require('./find_most_recently_changed'); - test('returns newest file', () => { const file = findMostRecentlyChanged('/data/*.yml'); expect(file).toEqual('/data/newest.yml'); diff --git a/packages/kbn-es/src/utils/find_most_recently_changed.js b/packages/kbn-es/src/utils/find_most_recently_changed.ts similarity index 65% rename from packages/kbn-es/src/utils/find_most_recently_changed.js rename to packages/kbn-es/src/utils/find_most_recently_changed.ts index 16d300f080b8d..29e1edcc5fcc9 100644 --- a/packages/kbn-es/src/utils/find_most_recently_changed.js +++ b/packages/kbn-es/src/utils/find_most_recently_changed.ts @@ -6,25 +6,22 @@ * Side Public License, v 1. */ -const path = require('path'); -const fs = require('fs'); -const glob = require('glob'); +import path from 'path'; +import fs from 'fs'; +import glob from 'glob'; /** * Find the most recently modified file that matches the pattern pattern - * - * @param {String} pattern absolute path with glob expressions - * @return {String} Absolute path */ -exports.findMostRecentlyChanged = function findMostRecentlyChanged(pattern) { +export function findMostRecentlyChanged(pattern: string) { if (!path.isAbsolute(pattern)) { throw new TypeError(`Pattern must be absolute, got ${pattern}`); } - const ctime = (path) => fs.statSync(path).ctime.getTime(); + const ctime = (p: string) => fs.statSync(p).ctime.getTime(); return glob .sync(pattern) .sort((a, b) => ctime(a) - ctime(b)) .pop(); -}; +} diff --git a/packages/kbn-es/src/utils/index.js b/packages/kbn-es/src/utils/index.js deleted file mode 100644 index ed83495e5310a..0000000000000 --- a/packages/kbn-es/src/utils/index.js +++ /dev/null @@ -1,16 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0 and the Server Side Public License, v 1; you may not use this file except - * in compliance with, at your election, the Elastic License 2.0 or the Server - * Side Public License, v 1. - */ - -exports.cache = require('./cache'); -exports.log = require('./log').log; -exports.parseEsLog = require('./parse_es_log').parseEsLog; -exports.findMostRecentlyChanged = require('./find_most_recently_changed').findMostRecentlyChanged; -exports.extractConfigFiles = require('./extract_config_files').extractConfigFiles; -exports.NativeRealm = require('./native_realm').NativeRealm; -exports.buildSnapshot = require('./build_snapshot').buildSnapshot; -exports.archiveForPlatform = require('./build_snapshot').archiveForPlatform; diff --git a/packages/kbn-es/src/utils/index.ts b/packages/kbn-es/src/utils/index.ts new file mode 100644 index 0000000000000..ce0a222dafd3b --- /dev/null +++ b/packages/kbn-es/src/utils/index.ts @@ -0,0 +1,19 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +export { cache } from './cache'; +export { log } from './log'; +// @ts-expect-error not typed yet +export { parseEsLog } from './parse_es_log'; +export { findMostRecentlyChanged } from './find_most_recently_changed'; +// @ts-expect-error not typed yet +export { extractConfigFiles } from './extract_config_files'; +// @ts-expect-error not typed yet +export { NativeRealm } from './native_realm'; +export { buildSnapshot } from './build_snapshot'; +export { archiveForPlatform } from './build_snapshot'; diff --git a/packages/kbn-es/src/utils/log.js b/packages/kbn-es/src/utils/log.ts similarity index 80% rename from packages/kbn-es/src/utils/log.js rename to packages/kbn-es/src/utils/log.ts index b33ae509c6c45..a0299f885cf6a 100644 --- a/packages/kbn-es/src/utils/log.js +++ b/packages/kbn-es/src/utils/log.ts @@ -6,11 +6,9 @@ * Side Public License, v 1. */ -const { ToolingLog } = require('@kbn/dev-utils'); +import { ToolingLog } from '@kbn/dev-utils'; -const log = new ToolingLog({ +export const log = new ToolingLog({ level: 'verbose', writeTo: process.stdout, }); - -exports.log = log; diff --git a/packages/kbn-eslint-import-resolver-kibana/BUILD.bazel b/packages/kbn-eslint-import-resolver-kibana/BUILD.bazel index a4d96f76053e1..759f4ac706471 100644 --- a/packages/kbn-eslint-import-resolver-kibana/BUILD.bazel +++ b/packages/kbn-eslint-import-resolver-kibana/BUILD.bazel @@ -1,4 +1,5 @@ -load("@build_bazel_rules_nodejs//:index.bzl", "js_library", "pkg_npm") +load("@build_bazel_rules_nodejs//:index.bzl", "js_library") +load("//src/dev/bazel:index.bzl", "pkg_npm") PKG_BASE_NAME = "kbn-eslint-import-resolver-kibana" PKG_REQUIRE_NAME = "@kbn/eslint-import-resolver-kibana" diff --git a/packages/kbn-eslint-plugin-eslint/BUILD.bazel b/packages/kbn-eslint-plugin-eslint/BUILD.bazel index 5baab89d6f03d..c02a468456f77 100644 --- a/packages/kbn-eslint-plugin-eslint/BUILD.bazel +++ b/packages/kbn-eslint-plugin-eslint/BUILD.bazel @@ -1,4 +1,5 @@ -load("@build_bazel_rules_nodejs//:index.bzl", "js_library", "pkg_npm") +load("@build_bazel_rules_nodejs//:index.bzl", "js_library") +load("//src/dev/bazel:index.bzl", "pkg_npm") PKG_BASE_NAME = "kbn-eslint-plugin-eslint" PKG_REQUIRE_NAME = "@kbn/eslint-plugin-eslint" @@ -28,7 +29,7 @@ NPM_MODULE_EXTRA_FILES = [ "README.md", ] -DEPS = [ +RUNTIME_DEPS = [ "@npm//@babel/eslint-parser", "@npm//dedent", "@npm//eslint", @@ -41,7 +42,7 @@ js_library( srcs = NPM_MODULE_EXTRA_FILES + [ ":srcs", ], - deps = DEPS, + deps = RUNTIME_DEPS, package_name = PKG_REQUIRE_NAME, visibility = ["//visibility:public"], ) diff --git a/packages/kbn-eslint-plugin-eslint/helpers/exports.js b/packages/kbn-eslint-plugin-eslint/helpers/exports.js index b7af8e83d7661..971364633356c 100644 --- a/packages/kbn-eslint-plugin-eslint/helpers/exports.js +++ b/packages/kbn-eslint-plugin-eslint/helpers/exports.js @@ -9,7 +9,7 @@ const Fs = require('fs'); const Path = require('path'); const ts = require('typescript'); -const { REPO_ROOT } = require('@kbn/dev-utils'); +const { REPO_ROOT } = require('@kbn/utils'); const { ExportSet } = require('./export_set'); /** @typedef {import("@typescript-eslint/types").TSESTree.ExportAllDeclaration} ExportAllDeclaration */ diff --git a/packages/kbn-expect/BUILD.bazel b/packages/kbn-expect/BUILD.bazel index b7eb91a451b9a..9f74cfe6a093d 100644 --- a/packages/kbn-expect/BUILD.bazel +++ b/packages/kbn-expect/BUILD.bazel @@ -1,4 +1,5 @@ -load("@build_bazel_rules_nodejs//:index.bzl", "js_library", "pkg_npm") +load("@build_bazel_rules_nodejs//:index.bzl", "js_library") +load("//src/dev/bazel:index.bzl", "pkg_npm") PKG_BASE_NAME = "kbn-expect" PKG_REQUIRE_NAME = "@kbn/expect" diff --git a/packages/kbn-optimizer/BUILD.bazel b/packages/kbn-optimizer/BUILD.bazel index a389086c9ee3c..3bd41249e2d51 100644 --- a/packages/kbn-optimizer/BUILD.bazel +++ b/packages/kbn-optimizer/BUILD.bazel @@ -38,10 +38,12 @@ RUNTIME_DEPS = [ "//packages/kbn-ui-shared-deps-npm", "//packages/kbn-ui-shared-deps-src", "//packages/kbn-utils", + "@npm//@babel/core", "@npm//chalk", "@npm//clean-webpack-plugin", "@npm//compression-webpack-plugin", "@npm//cpy", + "@npm//dedent", "@npm//del", "@npm//execa", "@npm//jest-diff", @@ -64,7 +66,7 @@ RUNTIME_DEPS = [ TYPES_DEPS = [ "//packages/kbn-config:npm_module_types", "//packages/kbn-config-schema:npm_module_types", - "//packages/kbn-dev-utils", + "//packages/kbn-dev-utils:npm_module_types", "//packages/kbn-std", "//packages/kbn-ui-shared-deps-npm", "//packages/kbn-ui-shared-deps-src", @@ -79,7 +81,9 @@ TYPES_DEPS = [ "@npm//pirates", "@npm//rxjs", "@npm//zlib", + "@npm//@types/babel__core", "@npm//@types/compression-webpack-plugin", + "@npm//@types/dedent", "@npm//@types/jest", "@npm//@types/json-stable-stringify", "@npm//@types/js-yaml", diff --git a/packages/kbn-optimizer/src/babel_runtime_helpers/find_babel_runtime_helpers_in_entry_bundles.ts b/packages/kbn-optimizer/src/babel_runtime_helpers/find_babel_runtime_helpers_in_entry_bundles.ts index f00905f3f4920..c07a9764af76f 100644 --- a/packages/kbn-optimizer/src/babel_runtime_helpers/find_babel_runtime_helpers_in_entry_bundles.ts +++ b/packages/kbn-optimizer/src/babel_runtime_helpers/find_babel_runtime_helpers_in_entry_bundles.ts @@ -8,7 +8,8 @@ import Path from 'path'; -import { run, REPO_ROOT } from '@kbn/dev-utils'; +import { run } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import { OptimizerConfig } from '../optimizer'; import { parseStats, inAnyEntryChunk } from './parse_stats'; diff --git a/packages/kbn-optimizer/src/node/node_auto_tranpilation.ts b/packages/kbn-optimizer/src/node/node_auto_tranpilation.ts index 6f5dabf410ffa..2710ba8a54210 100644 --- a/packages/kbn-optimizer/src/node/node_auto_tranpilation.ts +++ b/packages/kbn-optimizer/src/node/node_auto_tranpilation.ts @@ -39,7 +39,7 @@ import Crypto from 'crypto'; import * as babel from '@babel/core'; import { addHook } from 'pirates'; -import { REPO_ROOT, UPSTREAM_BRANCH } from '@kbn/dev-utils'; +import { REPO_ROOT, UPSTREAM_BRANCH } from '@kbn/utils'; import sourceMapSupport from 'source-map-support'; import { Cache } from './cache'; diff --git a/packages/kbn-optimizer/src/optimizer/get_changes.test.ts b/packages/kbn-optimizer/src/optimizer/get_changes.test.ts index d3cc5cceefddf..d1754248dba17 100644 --- a/packages/kbn-optimizer/src/optimizer/get_changes.test.ts +++ b/packages/kbn-optimizer/src/optimizer/get_changes.test.ts @@ -9,7 +9,8 @@ jest.mock('execa'); import { getChanges } from './get_changes'; -import { REPO_ROOT, createAbsolutePathSerializer } from '@kbn/dev-utils'; +import { createAbsolutePathSerializer } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; const execa: jest.Mock = jest.requireMock('execa'); diff --git a/packages/kbn-optimizer/src/optimizer/get_changes.ts b/packages/kbn-optimizer/src/optimizer/get_changes.ts index c5f8abe99c322..b59f938eb8c37 100644 --- a/packages/kbn-optimizer/src/optimizer/get_changes.ts +++ b/packages/kbn-optimizer/src/optimizer/get_changes.ts @@ -10,7 +10,7 @@ import Path from 'path'; import execa from 'execa'; -import { REPO_ROOT } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; export type Changes = Map; diff --git a/packages/kbn-plugin-generator/BUILD.bazel b/packages/kbn-plugin-generator/BUILD.bazel index c935d1763dae8..488f09bdd5d52 100644 --- a/packages/kbn-plugin-generator/BUILD.bazel +++ b/packages/kbn-plugin-generator/BUILD.bazel @@ -51,7 +51,7 @@ RUNTIME_DEPS = [ TYPES_DEPS = [ "//packages/kbn-utils", - "//packages/kbn-dev-utils", + "//packages/kbn-dev-utils:npm_module_types", "@npm//del", "@npm//execa", "@npm//globby", diff --git a/packages/kbn-plugin-helpers/BUILD.bazel b/packages/kbn-plugin-helpers/BUILD.bazel index d7744aecac26e..47f205f1530b7 100644 --- a/packages/kbn-plugin-helpers/BUILD.bazel +++ b/packages/kbn-plugin-helpers/BUILD.bazel @@ -42,7 +42,7 @@ RUNTIME_DEPS = [ ] TYPES_DEPS = [ - "//packages/kbn-dev-utils", + "//packages/kbn-dev-utils:npm_module_types", "//packages/kbn-optimizer", "//packages/kbn-utils", "@npm//del", diff --git a/packages/kbn-pm/dist/index.js b/packages/kbn-pm/dist/index.js index c1d0f69e4ea07..fc92d18698132 100644 --- a/packages/kbn-pm/dist/index.js +++ b/packages/kbn-pm/dist/index.js @@ -6639,7 +6639,15 @@ class ToolingLogTextWriter { } if (this.ignoreSources && msg.source && this.ignoreSources.includes(msg.source)) { - return false; + if (msg.type === 'write') { + const txt = (0, _util.format)(msg.args[0], ...msg.args.slice(1)); // Ensure that Elasticsearch deprecation log messages from Kibana aren't ignored + + if (!/elasticsearch\.deprecation/.test(txt)) { + return false; + } + } else { + return false; + } } const prefix = has(MSG_PREFIXES, msg.type) ? MSG_PREFIXES[msg.type] : ''; diff --git a/packages/kbn-rule-data-utils/BUILD.bazel b/packages/kbn-rule-data-utils/BUILD.bazel index 730e907aafc65..d23cf25f181ca 100644 --- a/packages/kbn-rule-data-utils/BUILD.bazel +++ b/packages/kbn-rule-data-utils/BUILD.bazel @@ -34,7 +34,7 @@ RUNTIME_DEPS = [ ] TYPES_DEPS = [ - "//packages/kbn-es-query", + "//packages/kbn-es-query:npm_module_types", "@npm//@elastic/elasticsearch", "@npm//tslib", "@npm//utility-types", diff --git a/packages/kbn-rule-data-utils/src/technical_field_names.ts b/packages/kbn-rule-data-utils/src/technical_field_names.ts index 349719c019c22..fde8deade36b5 100644 --- a/packages/kbn-rule-data-utils/src/technical_field_names.ts +++ b/packages/kbn-rule-data-utils/src/technical_field_names.ts @@ -24,6 +24,7 @@ const VERSION = `${KIBANA_NAMESPACE}.version` as const; // Fields pertaining to the alert const ALERT_ACTION_GROUP = `${ALERT_NAMESPACE}.action_group` as const; +const ALERT_BUILDING_BLOCK_TYPE = `${ALERT_NAMESPACE}.building_block_type` as const; const ALERT_DURATION = `${ALERT_NAMESPACE}.duration.us` as const; const ALERT_END = `${ALERT_NAMESPACE}.end` as const; const ALERT_EVALUATION_THRESHOLD = `${ALERT_NAMESPACE}.evaluation.threshold` as const; @@ -91,6 +92,7 @@ const fields = { TAGS, TIMESTAMP, ALERT_ACTION_GROUP, + ALERT_BUILDING_BLOCK_TYPE, ALERT_DURATION, ALERT_END, ALERT_EVALUATION_THRESHOLD, @@ -141,6 +143,7 @@ const fields = { export { ALERT_ACTION_GROUP, + ALERT_BUILDING_BLOCK_TYPE, ALERT_DURATION, ALERT_END, ALERT_EVALUATION_THRESHOLD, diff --git a/packages/kbn-securitysolution-autocomplete/BUILD.bazel b/packages/kbn-securitysolution-autocomplete/BUILD.bazel index 57ac8c62273e0..50df292b8796e 100644 --- a/packages/kbn-securitysolution-autocomplete/BUILD.bazel +++ b/packages/kbn-securitysolution-autocomplete/BUILD.bazel @@ -45,7 +45,7 @@ RUNTIME_DEPS = [ ] TYPES_DEPS = [ - "//packages/kbn-es-query", + "//packages/kbn-es-query:npm_module_types", "//packages/kbn-i18n", "//packages/kbn-securitysolution-list-hooks", "//packages/kbn-securitysolution-list-utils", diff --git a/packages/kbn-securitysolution-io-ts-list-types/src/common/endpoint/entries/index.mock.ts b/packages/kbn-securitysolution-io-ts-list-types/src/common/endpoint/entries/index.mock.ts index e491b50b0f9c8..176a6357b30e7 100644 --- a/packages/kbn-securitysolution-io-ts-list-types/src/common/endpoint/entries/index.mock.ts +++ b/packages/kbn-securitysolution-io-ts-list-types/src/common/endpoint/entries/index.mock.ts @@ -10,9 +10,11 @@ import { EndpointEntriesArray } from '.'; import { getEndpointEntryMatchMock } from '../entry_match/index.mock'; import { getEndpointEntryMatchAnyMock } from '../entry_match_any/index.mock'; import { getEndpointEntryNestedMock } from '../entry_nested/index.mock'; +import { getEndpointEntryMatchWildcard } from '../entry_match_wildcard/index.mock'; export const getEndpointEntriesArrayMock = (): EndpointEntriesArray => [ getEndpointEntryMatchMock(), getEndpointEntryMatchAnyMock(), getEndpointEntryNestedMock(), + getEndpointEntryMatchWildcard(), ]; diff --git a/packages/kbn-securitysolution-io-ts-list-types/src/common/endpoint/entries/index.test.ts b/packages/kbn-securitysolution-io-ts-list-types/src/common/endpoint/entries/index.test.ts index 09f1740567bc1..ca852e15c5c2a 100644 --- a/packages/kbn-securitysolution-io-ts-list-types/src/common/endpoint/entries/index.test.ts +++ b/packages/kbn-securitysolution-io-ts-list-types/src/common/endpoint/entries/index.test.ts @@ -20,6 +20,7 @@ import { getEndpointEntryNestedMock } from '../entry_nested/index.mock'; import { getEndpointEntriesArrayMock } from './index.mock'; import { getEntryListMock } from '../../entries_list/index.mock'; import { getEntryExistsMock } from '../../entries_exist/index.mock'; +import { getEndpointEntryMatchWildcard } from '../entry_match_wildcard/index.mock'; describe('Endpoint', () => { describe('entriesArray', () => { @@ -99,6 +100,15 @@ describe('Endpoint', () => { expect(message.schema).toEqual(payload); }); + test('it should validate an array with wildcard entry', () => { + const payload = [getEndpointEntryMatchWildcard()]; + const decoded = endpointEntriesArray.decode(payload); + const message = pipe(decoded, foldLeftRight); + + expect(getPaths(left(message.errors))).toEqual([]); + expect(message.schema).toEqual(payload); + }); + test('it should validate an array with all types of entries', () => { const payload = getEndpointEntriesArrayMock(); const decoded = endpointEntriesArray.decode(payload); diff --git a/packages/kbn-securitysolution-io-ts-list-types/src/common/endpoint/entries/index.ts b/packages/kbn-securitysolution-io-ts-list-types/src/common/endpoint/entries/index.ts index 451131dafc459..58b0d80f9c1fa 100644 --- a/packages/kbn-securitysolution-io-ts-list-types/src/common/endpoint/entries/index.ts +++ b/packages/kbn-securitysolution-io-ts-list-types/src/common/endpoint/entries/index.ts @@ -11,9 +11,15 @@ import { Either } from 'fp-ts/lib/Either'; import { endpointEntryMatch } from '../entry_match'; import { endpointEntryMatchAny } from '../entry_match_any'; import { endpointEntryNested } from '../entry_nested'; +import { endpointEntryMatchWildcard } from '../entry_match_wildcard'; export const endpointEntriesArray = t.array( - t.union([endpointEntryMatch, endpointEntryMatchAny, endpointEntryNested]) + t.union([ + endpointEntryMatch, + endpointEntryMatchAny, + endpointEntryMatchWildcard, + endpointEntryNested, + ]) ); export type EndpointEntriesArray = t.TypeOf; diff --git a/packages/kbn-es/src/install/index.js b/packages/kbn-securitysolution-io-ts-list-types/src/common/endpoint/entry_match_wildcard/index.mock.ts similarity index 53% rename from packages/kbn-es/src/install/index.js rename to packages/kbn-securitysolution-io-ts-list-types/src/common/endpoint/entry_match_wildcard/index.mock.ts index 07582f73c663a..e001552277e0c 100644 --- a/packages/kbn-es/src/install/index.js +++ b/packages/kbn-securitysolution-io-ts-list-types/src/common/endpoint/entry_match_wildcard/index.mock.ts @@ -6,7 +6,12 @@ * Side Public License, v 1. */ -exports.installArchive = require('./archive').installArchive; -exports.installSnapshot = require('./snapshot').installSnapshot; -exports.downloadSnapshot = require('./snapshot').downloadSnapshot; -exports.installSource = require('./source').installSource; +import { ENTRY_VALUE, FIELD, OPERATOR, WILDCARD } from '../../../constants/index.mock'; +import { EndpointEntryMatchWildcard } from './index'; + +export const getEndpointEntryMatchWildcard = (): EndpointEntryMatchWildcard => ({ + field: FIELD, + operator: OPERATOR, + type: WILDCARD, + value: ENTRY_VALUE, +}); diff --git a/packages/kbn-securitysolution-io-ts-list-types/src/request/import_exception_item_schema/index.mock.ts b/packages/kbn-securitysolution-io-ts-list-types/src/request/import_exception_item_schema/index.mock.ts new file mode 100644 index 0000000000000..03ec225351e6d --- /dev/null +++ b/packages/kbn-securitysolution-io-ts-list-types/src/request/import_exception_item_schema/index.mock.ts @@ -0,0 +1,34 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { ENTRIES } from '../../constants/index.mock'; +import { ImportExceptionListItemSchema, ImportExceptionListItemSchemaDecoded } from '.'; + +export const getImportExceptionsListItemSchemaMock = ( + itemId = 'item_id_1', + listId = 'detection_list_id' +): ImportExceptionListItemSchema => ({ + description: 'some description', + entries: ENTRIES, + item_id: itemId, + list_id: listId, + name: 'Query with a rule id', + type: 'simple', +}); + +export const getImportExceptionsListItemSchemaDecodedMock = ( + itemId = 'item_id_1', + listId = 'detection_list_id' +): ImportExceptionListItemSchemaDecoded => ({ + ...getImportExceptionsListItemSchemaMock(itemId, listId), + comments: [], + meta: undefined, + namespace_type: 'single', + os_types: [], + tags: [], +}); diff --git a/packages/kbn-securitysolution-io-ts-list-types/src/request/import_exception_item_schema/index.test.ts b/packages/kbn-securitysolution-io-ts-list-types/src/request/import_exception_item_schema/index.test.ts new file mode 100644 index 0000000000000..d202f65b57ab5 --- /dev/null +++ b/packages/kbn-securitysolution-io-ts-list-types/src/request/import_exception_item_schema/index.test.ts @@ -0,0 +1,143 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { left } from 'fp-ts/lib/Either'; +import { pipe } from 'fp-ts/lib/pipeable'; +import { exactCheck, foldLeftRight, getPaths } from '@kbn/securitysolution-io-ts-utils'; + +import { importExceptionListItemSchema, ImportExceptionListItemSchema } from '.'; +import { + getImportExceptionsListItemSchemaDecodedMock, + getImportExceptionsListItemSchemaMock, +} from './index.mock'; + +describe('import_list_item_schema', () => { + test('it should validate a typical item request', () => { + const payload = getImportExceptionsListItemSchemaMock(); + const decoded = importExceptionListItemSchema.decode(payload); + const checked = exactCheck(payload, decoded); + const message = pipe(checked, foldLeftRight); + + expect(getPaths(left(message.errors))).toEqual([]); + expect(message.schema).toEqual(getImportExceptionsListItemSchemaDecodedMock()); + }); + + test('it should NOT accept an undefined for "item_id"', () => { + const payload: Partial> = + getImportExceptionsListItemSchemaMock(); + delete payload.item_id; + const decoded = importExceptionListItemSchema.decode(payload); + const checked = exactCheck(payload, decoded); + const message = pipe(checked, foldLeftRight); + expect(getPaths(left(message.errors))).toEqual([ + 'Invalid value "undefined" supplied to "item_id"', + ]); + expect(message.schema).toEqual({}); + }); + + test('it should NOT accept an undefined for "list_id"', () => { + const payload: Partial> = + getImportExceptionsListItemSchemaMock(); + delete payload.list_id; + const decoded = importExceptionListItemSchema.decode(payload); + const checked = exactCheck(payload, decoded); + const message = pipe(checked, foldLeftRight); + expect(getPaths(left(message.errors))).toEqual([ + 'Invalid value "undefined" supplied to "list_id"', + ]); + expect(message.schema).toEqual({}); + }); + + test('it should NOT accept an undefined for "description"', () => { + const payload: Partial> = + getImportExceptionsListItemSchemaMock(); + delete payload.description; + const decoded = importExceptionListItemSchema.decode(payload); + const checked = exactCheck(payload, decoded); + const message = pipe(checked, foldLeftRight); + expect(getPaths(left(message.errors))).toEqual([ + 'Invalid value "undefined" supplied to "description"', + ]); + expect(message.schema).toEqual({}); + }); + + test('it should NOT accept an undefined for "name"', () => { + const payload: Partial> = + getImportExceptionsListItemSchemaMock(); + delete payload.name; + const decoded = importExceptionListItemSchema.decode(payload); + const checked = exactCheck(payload, decoded); + const message = pipe(checked, foldLeftRight); + expect(getPaths(left(message.errors))).toEqual([ + 'Invalid value "undefined" supplied to "name"', + ]); + expect(message.schema).toEqual({}); + }); + + test('it should NOT accept an undefined for "type"', () => { + const payload: Partial> = + getImportExceptionsListItemSchemaMock(); + delete payload.type; + const decoded = importExceptionListItemSchema.decode(payload); + const checked = exactCheck(payload, decoded); + const message = pipe(checked, foldLeftRight); + expect(getPaths(left(message.errors))).toEqual([ + 'Invalid value "undefined" supplied to "type"', + ]); + expect(message.schema).toEqual({}); + }); + + test('it should NOT accept an undefined for "entries"', () => { + const payload: Partial> = + getImportExceptionsListItemSchemaMock(); + delete payload.entries; + const decoded = importExceptionListItemSchema.decode(payload); + const checked = exactCheck(payload, decoded); + const message = pipe(checked, foldLeftRight); + expect(getPaths(left(message.errors))).toEqual([ + 'Invalid value "undefined" supplied to "entries"', + ]); + expect(message.schema).toEqual({}); + }); + + test('it should accept any partial fields', () => { + const payload: ImportExceptionListItemSchema = { + ...getImportExceptionsListItemSchemaMock(), + id: '123', + namespace_type: 'single', + comments: [], + os_types: [], + tags: ['123'], + created_at: '2018-08-24T17:49:30.145142000', + created_by: 'elastic', + updated_at: '2018-08-24T17:49:30.145142000', + updated_by: 'elastic', + tie_breaker_id: '123', + _version: '3', + meta: undefined, + }; + + const decoded = importExceptionListItemSchema.decode(payload); + const checked = exactCheck(payload, decoded); + const message = pipe(checked, foldLeftRight); + expect(getPaths(left(message.errors))).toEqual([]); + expect(message.schema).toEqual(payload); + }); + + test('it should not allow an extra key to be sent in', () => { + const payload: ImportExceptionListItemSchema & { + extraKey?: string; + } = getImportExceptionsListItemSchemaMock(); + payload.extraKey = 'some new value'; + const decoded = importExceptionListItemSchema.decode(payload); + const checked = exactCheck(payload, decoded); + const message = pipe(checked, foldLeftRight); + expect(getPaths(left(message.errors))).toEqual(['invalid keys "extraKey"']); + expect(message.schema).toEqual({}); + }); +}); diff --git a/packages/kbn-securitysolution-io-ts-list-types/src/request/import_exception_item_schema/index.ts b/packages/kbn-securitysolution-io-ts-list-types/src/request/import_exception_item_schema/index.ts new file mode 100644 index 0000000000000..3da30a21a0115 --- /dev/null +++ b/packages/kbn-securitysolution-io-ts-list-types/src/request/import_exception_item_schema/index.ts @@ -0,0 +1,87 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import * as t from 'io-ts'; + +import { OsTypeArray, osTypeArrayOrUndefined } from '../../common/os_type'; +import { Tags } from '../../common/tags'; +import { NamespaceType } from '../../common/default_namespace'; +import { name } from '../../common/name'; +import { description } from '../../common/description'; +import { namespace_type } from '../../common/namespace_type'; +import { tags } from '../../common/tags'; +import { meta } from '../../common/meta'; +import { list_id } from '../../common/list_id'; +import { item_id } from '../../common/item_id'; +import { id } from '../../common/id'; +import { created_at } from '../../common/created_at'; +import { created_by } from '../../common/created_by'; +import { updated_at } from '../../common/updated_at'; +import { updated_by } from '../../common/updated_by'; +import { _version } from '../../common/underscore_version'; +import { tie_breaker_id } from '../../common/tie_breaker_id'; +import { nonEmptyEntriesArray } from '../../common/non_empty_entries_array'; +import { exceptionListItemType } from '../../common/exception_list_item_type'; +import { ItemId } from '../../common/item_id'; +import { EntriesArray } from '../../common/entries'; +import { CreateCommentsArray } from '../../common/create_comment'; +import { DefaultCreateCommentsArray } from '../../common/default_create_comments_array'; + +/** + * Differences from this and the createExceptionsListItemSchema are + * - item_id is required + * - id is optional (but ignored in the import code - item_id is exclusively used for imports) + * - immutable is optional but if it is any value other than false it will be rejected + * - created_at is optional (but ignored in the import code) + * - updated_at is optional (but ignored in the import code) + * - created_by is optional (but ignored in the import code) + * - updated_by is optional (but ignored in the import code) + */ +export const importExceptionListItemSchema = t.intersection([ + t.exact( + t.type({ + description, + entries: nonEmptyEntriesArray, + item_id, + list_id, + name, + type: exceptionListItemType, + }) + ), + t.exact( + t.partial({ + id, // defaults to undefined if not set during decode + comments: DefaultCreateCommentsArray, // defaults to empty array if not set during decode + created_at, // defaults undefined if not set during decode + updated_at, // defaults undefined if not set during decode + created_by, // defaults undefined if not set during decode + updated_by, // defaults undefined if not set during decode + _version, // defaults to undefined if not set during decode + tie_breaker_id, + meta, // defaults to undefined if not set during decode + namespace_type, // defaults to 'single' if not set during decode + os_types: osTypeArrayOrUndefined, // defaults to empty array if not set during decode + tags, // defaults to empty array if not set during decode + }) + ), +]); + +export type ImportExceptionListItemSchema = t.OutputOf; + +// This type is used after a decode since some things are defaults after a decode. +export type ImportExceptionListItemSchemaDecoded = Omit< + ImportExceptionListItemSchema, + 'tags' | 'item_id' | 'entries' | 'namespace_type' | 'comments' +> & { + comments: CreateCommentsArray; + tags: Tags; + item_id: ItemId; + entries: EntriesArray; + namespace_type: NamespaceType; + os_types: OsTypeArray; +}; diff --git a/packages/kbn-securitysolution-io-ts-list-types/src/request/import_exception_list_schema/index.mock.ts b/packages/kbn-securitysolution-io-ts-list-types/src/request/import_exception_list_schema/index.mock.ts new file mode 100644 index 0000000000000..dc6aa8644c1f5 --- /dev/null +++ b/packages/kbn-securitysolution-io-ts-list-types/src/request/import_exception_list_schema/index.mock.ts @@ -0,0 +1,30 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { ImportExceptionListSchemaDecoded, ImportExceptionsListSchema } from '.'; + +export const getImportExceptionsListSchemaMock = ( + listId = 'detection_list_id' +): ImportExceptionsListSchema => ({ + description: 'some description', + list_id: listId, + name: 'Query with a rule id', + type: 'detection', +}); + +export const getImportExceptionsListSchemaDecodedMock = ( + listId = 'detection_list_id' +): ImportExceptionListSchemaDecoded => ({ + ...getImportExceptionsListSchemaMock(listId), + immutable: false, + meta: undefined, + namespace_type: 'single', + os_types: [], + tags: [], + version: 1, +}); diff --git a/packages/kbn-securitysolution-io-ts-list-types/src/request/import_exception_list_schema/index.test.ts b/packages/kbn-securitysolution-io-ts-list-types/src/request/import_exception_list_schema/index.test.ts new file mode 100644 index 0000000000000..92a24cd4352f5 --- /dev/null +++ b/packages/kbn-securitysolution-io-ts-list-types/src/request/import_exception_list_schema/index.test.ts @@ -0,0 +1,132 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { left } from 'fp-ts/lib/Either'; +import { pipe } from 'fp-ts/lib/pipeable'; +import { exactCheck, foldLeftRight, getPaths } from '@kbn/securitysolution-io-ts-utils'; + +import { importExceptionsListSchema, ImportExceptionsListSchema } from '.'; +import { + getImportExceptionsListSchemaMock, + getImportExceptionsListSchemaDecodedMock, +} from './index.mock'; + +describe('import_list_item_schema', () => { + test('it should validate a typical lists request', () => { + const payload = getImportExceptionsListSchemaMock(); + const decoded = importExceptionsListSchema.decode(payload); + const checked = exactCheck(payload, decoded); + const message = pipe(checked, foldLeftRight); + + expect(getPaths(left(message.errors))).toEqual([]); + expect(message.schema).toEqual(getImportExceptionsListSchemaDecodedMock()); + }); + + test('it should NOT accept an undefined for "list_id"', () => { + const payload: Partial> = + getImportExceptionsListSchemaMock(); + delete payload.list_id; + const decoded = importExceptionsListSchema.decode(payload); + const checked = exactCheck(payload, decoded); + const message = pipe(checked, foldLeftRight); + expect(getPaths(left(message.errors))).toEqual([ + 'Invalid value "undefined" supplied to "list_id"', + ]); + expect(message.schema).toEqual({}); + }); + + test('it should NOT accept an undefined for "description"', () => { + const payload: Partial> = + getImportExceptionsListSchemaMock(); + delete payload.description; + const decoded = importExceptionsListSchema.decode(payload); + const checked = exactCheck(payload, decoded); + const message = pipe(checked, foldLeftRight); + expect(getPaths(left(message.errors))).toEqual([ + 'Invalid value "undefined" supplied to "description"', + ]); + expect(message.schema).toEqual({}); + }); + + test('it should NOT accept an undefined for "name"', () => { + const payload: Partial> = + getImportExceptionsListSchemaMock(); + delete payload.name; + const decoded = importExceptionsListSchema.decode(payload); + const checked = exactCheck(payload, decoded); + const message = pipe(checked, foldLeftRight); + expect(getPaths(left(message.errors))).toEqual([ + 'Invalid value "undefined" supplied to "name"', + ]); + expect(message.schema).toEqual({}); + }); + + test('it should NOT accept an undefined for "type"', () => { + const payload: Partial> = + getImportExceptionsListSchemaMock(); + delete payload.type; + const decoded = importExceptionsListSchema.decode(payload); + const checked = exactCheck(payload, decoded); + const message = pipe(checked, foldLeftRight); + expect(getPaths(left(message.errors))).toEqual([ + 'Invalid value "undefined" supplied to "type"', + ]); + expect(message.schema).toEqual({}); + }); + + test('it should NOT accept value of "true" for "immutable"', () => { + const payload: ImportExceptionsListSchema = { + ...getImportExceptionsListSchemaMock(), + immutable: true, + }; + + const decoded = importExceptionsListSchema.decode(payload); + const checked = exactCheck(payload, decoded); + const message = pipe(checked, foldLeftRight); + expect(getPaths(left(message.errors))).toEqual([ + 'Invalid value "true" supplied to "immutable"', + ]); + expect(message.schema).toEqual({}); + }); + + test('it should accept any partial fields', () => { + const payload: ImportExceptionsListSchema = { + ...getImportExceptionsListSchemaMock(), + namespace_type: 'single', + immutable: false, + os_types: [], + tags: ['123'], + created_at: '2018-08-24T17:49:30.145142000', + created_by: 'elastic', + updated_at: '2018-08-24T17:49:30.145142000', + updated_by: 'elastic', + version: 3, + tie_breaker_id: '123', + _version: '3', + meta: undefined, + }; + + const decoded = importExceptionsListSchema.decode(payload); + const checked = exactCheck(payload, decoded); + const message = pipe(checked, foldLeftRight); + expect(getPaths(left(message.errors))).toEqual([]); + expect(message.schema).toEqual(payload); + }); + + test('it should not allow an extra key to be sent in', () => { + const payload: ImportExceptionsListSchema & { + extraKey?: string; + } = getImportExceptionsListSchemaMock(); + payload.extraKey = 'some new value'; + const decoded = importExceptionsListSchema.decode(payload); + const checked = exactCheck(payload, decoded); + const message = pipe(checked, foldLeftRight); + expect(getPaths(left(message.errors))).toEqual(['invalid keys "extraKey"']); + expect(message.schema).toEqual({}); + }); +}); diff --git a/packages/kbn-securitysolution-io-ts-list-types/src/request/import_exception_list_schema/index.ts b/packages/kbn-securitysolution-io-ts-list-types/src/request/import_exception_list_schema/index.ts new file mode 100644 index 0000000000000..610bbae97f579 --- /dev/null +++ b/packages/kbn-securitysolution-io-ts-list-types/src/request/import_exception_list_schema/index.ts @@ -0,0 +1,87 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import * as t from 'io-ts'; + +import { + DefaultVersionNumber, + DefaultVersionNumberDecoded, + OnlyFalseAllowed, +} from '@kbn/securitysolution-io-ts-types'; + +import { exceptionListType } from '../../common/exception_list'; +import { OsTypeArray, osTypeArrayOrUndefined } from '../../common/os_type'; +import { Tags } from '../../common/tags'; +import { ListId } from '../../common/list_id'; +import { NamespaceType } from '../../common/default_namespace'; +import { name } from '../../common/name'; +import { description } from '../../common/description'; +import { namespace_type } from '../../common/namespace_type'; +import { tags } from '../../common/tags'; +import { meta } from '../../common/meta'; +import { list_id } from '../../common/list_id'; +import { id } from '../../common/id'; +import { created_at } from '../../common/created_at'; +import { created_by } from '../../common/created_by'; +import { updated_at } from '../../common/updated_at'; +import { updated_by } from '../../common/updated_by'; +import { _version } from '../../common/underscore_version'; +import { tie_breaker_id } from '../../common/tie_breaker_id'; + +/** + * Differences from this and the createExceptionsSchema are + * - list_id is required + * - id is optional (but ignored in the import code - list_id is exclusively used for imports) + * - immutable is optional but if it is any value other than false it will be rejected + * - created_at is optional (but ignored in the import code) + * - updated_at is optional (but ignored in the import code) + * - created_by is optional (but ignored in the import code) + * - updated_by is optional (but ignored in the import code) + */ +export const importExceptionsListSchema = t.intersection([ + t.exact( + t.type({ + description, + name, + type: exceptionListType, + list_id, + }) + ), + t.exact( + t.partial({ + id, // defaults to undefined if not set during decode + immutable: OnlyFalseAllowed, + meta, // defaults to undefined if not set during decode + namespace_type, // defaults to 'single' if not set during decode + os_types: osTypeArrayOrUndefined, // defaults to empty array if not set during decode + tags, // defaults to empty array if not set during decode + created_at, // defaults "undefined" if not set during decode + updated_at, // defaults "undefined" if not set during decode + created_by, // defaults "undefined" if not set during decode + updated_by, // defaults "undefined" if not set during decode + _version, // defaults to undefined if not set during decode + tie_breaker_id, + version: DefaultVersionNumber, // defaults to numerical 1 if not set during decode + }) + ), +]); + +export type ImportExceptionsListSchema = t.TypeOf; + +// This type is used after a decode since some things are defaults after a decode. +export type ImportExceptionListSchemaDecoded = Omit< + ImportExceptionsListSchema, + 'tags' | 'list_id' | 'namespace_type' | 'os_types' | 'immutable' +> & { + immutable: false; + tags: Tags; + list_id: ListId; + namespace_type: NamespaceType; + os_types: OsTypeArray; + version: DefaultVersionNumberDecoded; +}; diff --git a/packages/kbn-securitysolution-io-ts-list-types/src/request/index.ts b/packages/kbn-securitysolution-io-ts-list-types/src/request/index.ts index 3d3c41aed5a72..da8bd7ed8306e 100644 --- a/packages/kbn-securitysolution-io-ts-list-types/src/request/index.ts +++ b/packages/kbn-securitysolution-io-ts-list-types/src/request/index.ts @@ -23,6 +23,8 @@ export * from './find_exception_list_item_schema'; export * from './find_list_item_schema'; export * from './find_list_schema'; export * from './import_list_item_query_schema'; +export * from './import_exception_list_schema'; +export * from './import_exception_item_schema'; export * from './import_list_item_schema'; export * from './patch_list_item_schema'; export * from './patch_list_schema'; diff --git a/packages/kbn-securitysolution-io-ts-list-types/src/response/import_exceptions_schema/index.mock.ts b/packages/kbn-securitysolution-io-ts-list-types/src/response/import_exceptions_schema/index.mock.ts new file mode 100644 index 0000000000000..d4c17c7f9422e --- /dev/null +++ b/packages/kbn-securitysolution-io-ts-list-types/src/response/import_exceptions_schema/index.mock.ts @@ -0,0 +1,23 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { ImportExceptionsResponseSchema } from '.'; + +export const getImportExceptionsResponseSchemaMock = ( + success = 0, + lists = 0, + items = 0 +): ImportExceptionsResponseSchema => ({ + errors: [], + success: true, + success_count: success, + success_exception_lists: true, + success_count_exception_lists: lists, + success_exception_list_items: true, + success_count_exception_list_items: items, +}); diff --git a/packages/kbn-securitysolution-io-ts-list-types/src/response/import_exceptions_schema/index.test.ts b/packages/kbn-securitysolution-io-ts-list-types/src/response/import_exceptions_schema/index.test.ts new file mode 100644 index 0000000000000..dc6780d4b1ce2 --- /dev/null +++ b/packages/kbn-securitysolution-io-ts-list-types/src/response/import_exceptions_schema/index.test.ts @@ -0,0 +1,129 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { left } from 'fp-ts/lib/Either'; +import { pipe } from 'fp-ts/lib/pipeable'; +import { exactCheck, foldLeftRight, getPaths } from '@kbn/securitysolution-io-ts-utils'; + +import { importExceptionsResponseSchema, ImportExceptionsResponseSchema } from '.'; +import { getImportExceptionsResponseSchemaMock } from './index.mock'; + +describe('importExceptionsResponseSchema', () => { + test('it should validate a typical exceptions import response', () => { + const payload = getImportExceptionsResponseSchemaMock(); + const decoded = importExceptionsResponseSchema.decode(payload); + const checked = exactCheck(payload, decoded); + const message = pipe(checked, foldLeftRight); + + expect(getPaths(left(message.errors))).toEqual([]); + expect(message.schema).toEqual(payload); + }); + + test('it should NOT accept an undefined for "errors"', () => { + const payload: Partial> = + getImportExceptionsResponseSchemaMock(); + delete payload.errors; + const decoded = importExceptionsResponseSchema.decode(payload); + const checked = exactCheck(payload, decoded); + const message = pipe(checked, foldLeftRight); + expect(getPaths(left(message.errors))).toEqual([ + 'Invalid value "undefined" supplied to "errors"', + ]); + expect(message.schema).toEqual({}); + }); + + test('it should NOT accept an undefined for "success"', () => { + const payload: Partial> = + getImportExceptionsResponseSchemaMock(); + delete payload.success; + const decoded = importExceptionsResponseSchema.decode(payload); + const checked = exactCheck(payload, decoded); + const message = pipe(checked, foldLeftRight); + expect(getPaths(left(message.errors))).toEqual([ + 'Invalid value "undefined" supplied to "success"', + ]); + expect(message.schema).toEqual({}); + }); + + test('it should NOT accept an undefined for "success_count"', () => { + const payload: Partial> = + getImportExceptionsResponseSchemaMock(); + delete payload.success_count; + const decoded = importExceptionsResponseSchema.decode(payload); + const checked = exactCheck(payload, decoded); + const message = pipe(checked, foldLeftRight); + expect(getPaths(left(message.errors))).toEqual([ + 'Invalid value "undefined" supplied to "success_count"', + ]); + expect(message.schema).toEqual({}); + }); + + test('it should NOT accept an undefined for "success_exception_lists"', () => { + const payload: Partial> = + getImportExceptionsResponseSchemaMock(); + delete payload.success_exception_lists; + const decoded = importExceptionsResponseSchema.decode(payload); + const checked = exactCheck(payload, decoded); + const message = pipe(checked, foldLeftRight); + expect(getPaths(left(message.errors))).toEqual([ + 'Invalid value "undefined" supplied to "success_exception_lists"', + ]); + expect(message.schema).toEqual({}); + }); + + test('it should NOT accept an undefined for "success_count_exception_lists"', () => { + const payload: Partial> = + getImportExceptionsResponseSchemaMock(); + delete payload.success_count_exception_lists; + const decoded = importExceptionsResponseSchema.decode(payload); + const checked = exactCheck(payload, decoded); + const message = pipe(checked, foldLeftRight); + expect(getPaths(left(message.errors))).toEqual([ + 'Invalid value "undefined" supplied to "success_count_exception_lists"', + ]); + expect(message.schema).toEqual({}); + }); + + test('it should NOT accept an undefined for "success_exception_list_items"', () => { + const payload: Partial> = + getImportExceptionsResponseSchemaMock(); + delete payload.success_exception_list_items; + const decoded = importExceptionsResponseSchema.decode(payload); + const checked = exactCheck(payload, decoded); + const message = pipe(checked, foldLeftRight); + expect(getPaths(left(message.errors))).toEqual([ + 'Invalid value "undefined" supplied to "success_exception_list_items"', + ]); + expect(message.schema).toEqual({}); + }); + + test('it should NOT accept an undefined for "success_count_exception_list_items"', () => { + const payload: Partial> = + getImportExceptionsResponseSchemaMock(); + delete payload.success_count_exception_list_items; + const decoded = importExceptionsResponseSchema.decode(payload); + const checked = exactCheck(payload, decoded); + const message = pipe(checked, foldLeftRight); + expect(getPaths(left(message.errors))).toEqual([ + 'Invalid value "undefined" supplied to "success_count_exception_list_items"', + ]); + expect(message.schema).toEqual({}); + }); + + test('it should not allow an extra key to be sent in', () => { + const payload: ImportExceptionsResponseSchema & { + extraKey?: string; + } = getImportExceptionsResponseSchemaMock(); + payload.extraKey = 'some new value'; + const decoded = importExceptionsResponseSchema.decode(payload); + const checked = exactCheck(payload, decoded); + const message = pipe(checked, foldLeftRight); + expect(getPaths(left(message.errors))).toEqual(['invalid keys "extraKey"']); + expect(message.schema).toEqual({}); + }); +}); diff --git a/packages/kbn-securitysolution-io-ts-list-types/src/response/import_exceptions_schema/index.ts b/packages/kbn-securitysolution-io-ts-list-types/src/response/import_exceptions_schema/index.ts new file mode 100644 index 0000000000000..f50356d2789f8 --- /dev/null +++ b/packages/kbn-securitysolution-io-ts-list-types/src/response/import_exceptions_schema/index.ts @@ -0,0 +1,51 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import * as t from 'io-ts'; + +import { PositiveInteger } from '@kbn/securitysolution-io-ts-types'; + +import { id } from '../../common/id'; +import { list_id } from '../../common/list_id'; +import { item_id } from '../../common/item_id'; + +export const bulkErrorErrorSchema = t.exact( + t.type({ + status_code: t.number, + message: t.string, + }) +); + +export const bulkErrorSchema = t.intersection([ + t.exact( + t.type({ + error: bulkErrorErrorSchema, + }) + ), + t.partial({ + id, + list_id, + item_id, + }), +]); + +export type BulkErrorSchema = t.TypeOf; + +export const importExceptionsResponseSchema = t.exact( + t.type({ + errors: t.array(bulkErrorSchema), + success: t.boolean, + success_count: PositiveInteger, + success_exception_lists: t.boolean, + success_count_exception_lists: PositiveInteger, + success_exception_list_items: t.boolean, + success_count_exception_list_items: PositiveInteger, + }) +); + +export type ImportExceptionsResponseSchema = t.TypeOf; diff --git a/packages/kbn-securitysolution-io-ts-list-types/src/response/index.ts b/packages/kbn-securitysolution-io-ts-list-types/src/response/index.ts index dc29bdf16ab48..c37b092eb3477 100644 --- a/packages/kbn-securitysolution-io-ts-list-types/src/response/index.ts +++ b/packages/kbn-securitysolution-io-ts-list-types/src/response/index.ts @@ -14,6 +14,7 @@ export * from './found_exception_list_item_schema'; export * from './found_exception_list_schema'; export * from './found_list_item_schema'; export * from './found_list_schema'; +export * from './import_exceptions_schema'; export * from './list_item_schema'; export * from './list_schema'; export * from './exception_list_summary_schema'; diff --git a/packages/kbn-securitysolution-io-ts-types/src/import_query_schema/index.test.ts b/packages/kbn-securitysolution-io-ts-types/src/import_query_schema/index.test.ts new file mode 100644 index 0000000000000..03ec9df51a318 --- /dev/null +++ b/packages/kbn-securitysolution-io-ts-types/src/import_query_schema/index.test.ts @@ -0,0 +1,54 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { left } from 'fp-ts/lib/Either'; +import { ImportQuerySchema, importQuerySchema } from '.'; +import { exactCheck, foldLeftRight, getPaths } from '@kbn/securitysolution-io-ts-utils'; + +describe('importQuerySchema', () => { + test('it should validate proper schema', () => { + const payload = { + overwrite: true, + }; + const decoded = importQuerySchema.decode(payload); + const checked = exactCheck(payload, decoded); + const message = foldLeftRight(checked); + + expect(getPaths(left(message.errors))).toEqual([]); + expect(message.schema).toEqual(payload); + }); + + test('it should NOT validate a non boolean value for "overwrite"', () => { + const payload: Omit & { overwrite: string } = { + overwrite: 'wrong', + }; + const decoded = importQuerySchema.decode(payload); + const checked = exactCheck(payload, decoded); + const message = foldLeftRight(checked); + + expect(getPaths(left(message.errors))).toEqual([ + 'Invalid value "wrong" supplied to "overwrite"', + ]); + expect(message.schema).toEqual({}); + }); + + test('it should NOT allow an extra key to be sent in', () => { + const payload: ImportQuerySchema & { + extraKey?: string; + } = { + extraKey: 'extra', + overwrite: true, + }; + + const decoded = importQuerySchema.decode(payload); + const checked = exactCheck(payload, decoded); + const message = foldLeftRight(checked); + expect(getPaths(left(message.errors))).toEqual(['invalid keys "extraKey"']); + expect(message.schema).toEqual({}); + }); +}); diff --git a/packages/kbn-securitysolution-io-ts-types/src/import_query_schema/index.ts b/packages/kbn-securitysolution-io-ts-types/src/import_query_schema/index.ts new file mode 100644 index 0000000000000..95cbf96b2ef8d --- /dev/null +++ b/packages/kbn-securitysolution-io-ts-types/src/import_query_schema/index.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 + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import * as t from 'io-ts'; + +import { DefaultStringBooleanFalse } from '../default_string_boolean_false'; + +export const importQuerySchema = t.exact( + t.partial({ + overwrite: DefaultStringBooleanFalse, + }) +); + +export type ImportQuerySchema = t.TypeOf; +export type ImportQuerySchemaDecoded = Omit & { + overwrite: boolean; +}; diff --git a/packages/kbn-securitysolution-io-ts-types/src/index.ts b/packages/kbn-securitysolution-io-ts-types/src/index.ts index b85bff63fe2a7..0bb99e4c766e7 100644 --- a/packages/kbn-securitysolution-io-ts-types/src/index.ts +++ b/packages/kbn-securitysolution-io-ts-types/src/index.ts @@ -17,6 +17,7 @@ export * from './default_version_number'; export * from './empty_string_array'; export * from './enumeration'; export * from './iso_date_string'; +export * from './import_query_schema'; export * from './non_empty_array'; export * from './non_empty_or_nullable_string_array'; export * from './non_empty_string_array'; diff --git a/packages/kbn-securitysolution-list-utils/BUILD.bazel b/packages/kbn-securitysolution-list-utils/BUILD.bazel index eb33eb1a03b66..30568ca725041 100644 --- a/packages/kbn-securitysolution-list-utils/BUILD.bazel +++ b/packages/kbn-securitysolution-list-utils/BUILD.bazel @@ -38,11 +38,12 @@ RUNTIME_DEPS = [ ] TYPES_DEPS = [ - "//packages/kbn-es-query", - "//packages/kbn-i18n", + "//packages/kbn-es-query:npm_module_types", + "//packages/kbn-i18n:npm_module_types", "//packages/kbn-securitysolution-io-ts-list-types", "//packages/kbn-securitysolution-list-constants", "//packages/kbn-securitysolution-utils", + "@npm//@elastic/elasticsearch", "@npm//@types/jest", "@npm//@types/lodash", "@npm//@types/node", diff --git a/packages/kbn-storybook/BUILD.bazel b/packages/kbn-storybook/BUILD.bazel index f2a7bf25fb407..5dbe22b56c63f 100644 --- a/packages/kbn-storybook/BUILD.bazel +++ b/packages/kbn-storybook/BUILD.bazel @@ -32,6 +32,7 @@ RUNTIME_DEPS = [ "//packages/kbn-dev-utils", "//packages/kbn-ui-shared-deps-npm", "//packages/kbn-ui-shared-deps-src", + "//packages/kbn-utils", "@npm//@storybook/addons", "@npm//@storybook/api", "@npm//@storybook/components", @@ -47,9 +48,10 @@ RUNTIME_DEPS = [ ] TYPES_DEPS = [ - "//packages/kbn-dev-utils", + "//packages/kbn-dev-utils:npm_module_types", "//packages/kbn-ui-shared-deps-npm", "//packages/kbn-ui-shared-deps-src", + "//packages/kbn-utils", "@npm//@storybook/addons", "@npm//@storybook/api", "@npm//@storybook/components", diff --git a/packages/kbn-storybook/src/lib/constants.ts b/packages/kbn-storybook/src/lib/constants.ts index 722f789fde786..69b05c94ea1b0 100644 --- a/packages/kbn-storybook/src/lib/constants.ts +++ b/packages/kbn-storybook/src/lib/constants.ts @@ -7,7 +7,7 @@ */ import { resolve } from 'path'; -import { REPO_ROOT as KIBANA_ROOT } from '@kbn/dev-utils'; +import { REPO_ROOT as KIBANA_ROOT } from '@kbn/utils'; export const REPO_ROOT = KIBANA_ROOT; export const ASSET_DIR = resolve(KIBANA_ROOT, 'built_assets/storybook'); diff --git a/packages/kbn-telemetry-tools/BUILD.bazel b/packages/kbn-telemetry-tools/BUILD.bazel index 1183de2586424..d2ea3a704f154 100644 --- a/packages/kbn-telemetry-tools/BUILD.bazel +++ b/packages/kbn-telemetry-tools/BUILD.bazel @@ -38,8 +38,9 @@ RUNTIME_DEPS = [ ] TYPES_DEPS = [ - "//packages/kbn-dev-utils", + "//packages/kbn-dev-utils:npm_module_types", "//packages/kbn-utility-types", + "@npm//tslib", "@npm//@types/glob", "@npm//@types/jest", "@npm//@types/listr", diff --git a/packages/kbn-test/BUILD.bazel b/packages/kbn-test/BUILD.bazel index 1d1d95d639861..eae0fe2cdf5dc 100644 --- a/packages/kbn-test/BUILD.bazel +++ b/packages/kbn-test/BUILD.bazel @@ -44,11 +44,13 @@ RUNTIME_DEPS = [ "@npm//axios", "@npm//@babel/traverse", "@npm//chance", + "@npm//dedent", "@npm//del", "@npm//enzyme", "@npm//execa", "@npm//exit-hook", "@npm//form-data", + "@npm//getopts", "@npm//globby", "@npm//he", "@npm//history", @@ -59,6 +61,7 @@ RUNTIME_DEPS = [ "@npm//@jest/reporters", "@npm//joi", "@npm//mustache", + "@npm//normalize-path", "@npm//parse-link-header", "@npm//prettier", "@npm//react-dom", @@ -72,13 +75,17 @@ RUNTIME_DEPS = [ ] TYPES_DEPS = [ - "//packages/kbn-dev-utils", + "//packages/kbn-dev-utils:npm_module_types", "//packages/kbn-i18n-react:npm_module_types", + "//packages/kbn-std", "//packages/kbn-utils", "@npm//@elastic/elasticsearch", + "@npm//axios", "@npm//elastic-apm-node", "@npm//del", + "@npm//exit-hook", "@npm//form-data", + "@npm//getopts", "@npm//jest", "@npm//jest-cli", "@npm//jest-snapshot", @@ -86,6 +93,7 @@ TYPES_DEPS = [ "@npm//rxjs", "@npm//xmlbuilder", "@npm//@types/chance", + "@npm//@types/dedent", "@npm//@types/enzyme", "@npm//@types/he", "@npm//@types/history", @@ -93,6 +101,7 @@ TYPES_DEPS = [ "@npm//@types/joi", "@npm//@types/lodash", "@npm//@types/mustache", + "@npm//@types/normalize-path", "@npm//@types/node", "@npm//@types/parse-link-header", "@npm//@types/prettier", diff --git a/packages/kbn-test/src/es/es_test_config.ts b/packages/kbn-test/src/es/es_test_config.ts index db5d705710a75..70000c8068e9f 100644 --- a/packages/kbn-test/src/es/es_test_config.ts +++ b/packages/kbn-test/src/es/es_test_config.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -import { kibanaPackageJson as pkg } from '@kbn/dev-utils'; +import { kibanaPackageJson as pkg } from '@kbn/utils'; import Url from 'url'; import { adminTestUser } from '../kbn'; diff --git a/packages/kbn-test/src/failed_tests_reporter/buildkite_metadata.ts b/packages/kbn-test/src/failed_tests_reporter/buildkite_metadata.ts new file mode 100644 index 0000000000000..d63f0166390cb --- /dev/null +++ b/packages/kbn-test/src/failed_tests_reporter/buildkite_metadata.ts @@ -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 + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +export interface BuildkiteMetadata { + buildId?: string; + jobId?: string; + url?: string; + jobName?: string; + jobUrl?: string; +} + +export function getBuildkiteMetadata(): BuildkiteMetadata { + // Buildkite steps that use `parallelism` need a numerical suffix added to identify them + // We should also increment the number by one, since it's 0-based + const jobNumberSuffix = process.env.BUILDKITE_PARALLEL_JOB + ? ` #${parseInt(process.env.BUILDKITE_PARALLEL_JOB, 10) + 1}` + : ''; + + const buildUrl = process.env.BUILDKITE_BUILD_URL; + const jobUrl = process.env.BUILDKITE_JOB_ID + ? `${buildUrl}#${process.env.BUILDKITE_JOB_ID}` + : undefined; + + return { + buildId: process.env.BUJILDKITE_BUILD_ID, + jobId: process.env.BUILDKITE_JOB_ID, + url: buildUrl, + jobUrl, + jobName: process.env.BUILDKITE_LABEL + ? `${process.env.BUILDKITE_LABEL}${jobNumberSuffix}` + : undefined, + }; +} diff --git a/packages/kbn-test/src/failed_tests_reporter/github_api.ts b/packages/kbn-test/src/failed_tests_reporter/github_api.ts index adaae11b7aa16..bb7570225a013 100644 --- a/packages/kbn-test/src/failed_tests_reporter/github_api.ts +++ b/packages/kbn-test/src/failed_tests_reporter/github_api.ts @@ -42,6 +42,7 @@ export class GithubApi { private readonly token: string | undefined; private readonly dryRun: boolean; private readonly x: AxiosInstance; + private requestCount: number = 0; /** * Create a GithubApi helper object, if token is undefined requests won't be @@ -68,6 +69,10 @@ export class GithubApi { }); } + getRequestCount() { + return this.requestCount; + } + private failedTestIssuesPageCache: { pages: GithubIssue[][]; nextRequest: RequestOptions | undefined; @@ -191,53 +196,50 @@ export class GithubApi { }> { const executeRequest = !this.dryRun || options.safeForDryRun; const maxAttempts = options.maxAttempts || 5; - const attempt = options.attempt || 1; - - this.log.verbose('Github API', executeRequest ? 'Request' : 'Dry Run', options); - - if (!executeRequest) { - return { - status: 200, - statusText: 'OK', - headers: {}, - data: dryRunResponse, - }; - } - try { - return await this.x.request(options); - } catch (error) { - const unableToReachGithub = isAxiosRequestError(error); - const githubApiFailed = isAxiosResponseError(error) && error.response.status >= 500; - const errorResponseLog = - isAxiosResponseError(error) && - `[${error.config.method} ${error.config.url}] ${error.response.status} ${error.response.statusText} Error`; + let attempt = 0; + while (true) { + attempt += 1; + this.log.verbose('Github API', executeRequest ? 'Request' : 'Dry Run', options); + + if (!executeRequest) { + return { + status: 200, + statusText: 'OK', + headers: {}, + data: dryRunResponse, + }; + } - if ((unableToReachGithub || githubApiFailed) && attempt < maxAttempts) { - const waitMs = 1000 * attempt; + try { + this.requestCount += 1; + return await this.x.request(options); + } catch (error) { + const unableToReachGithub = isAxiosRequestError(error); + const githubApiFailed = isAxiosResponseError(error) && error.response.status >= 500; + const errorResponseLog = + isAxiosResponseError(error) && + `[${error.config.method} ${error.config.url}] ${error.response.status} ${error.response.statusText} Error`; + + if ((unableToReachGithub || githubApiFailed) && attempt < maxAttempts) { + const waitMs = 1000 * attempt; + + if (errorResponseLog) { + this.log.error(`${errorResponseLog}: waiting ${waitMs}ms to retry`); + } else { + this.log.error(`Unable to reach github, waiting ${waitMs}ms to retry`); + } + + await new Promise((resolve) => setTimeout(resolve, waitMs)); + continue; + } if (errorResponseLog) { - this.log.error(`${errorResponseLog}: waiting ${waitMs}ms to retry`); - } else { - this.log.error(`Unable to reach github, waiting ${waitMs}ms to retry`); + throw new Error(`${errorResponseLog}: ${JSON.stringify(error.response.data)}`); } - await new Promise((resolve) => setTimeout(resolve, waitMs)); - return await this.request( - { - ...options, - maxAttempts, - attempt: attempt + 1, - }, - dryRunResponse - ); + throw error; } - - if (errorResponseLog) { - throw new Error(`${errorResponseLog}: ${JSON.stringify(error.response.data)}`); - } - - throw error; } } } diff --git a/packages/kbn-test/src/failed_tests_reporter/report_failures_to_file.ts b/packages/kbn-test/src/failed_tests_reporter/report_failures_to_file.ts index e481da019945c..33dab240ec8b4 100644 --- a/packages/kbn-test/src/failed_tests_reporter/report_failures_to_file.ts +++ b/packages/kbn-test/src/failed_tests_reporter/report_failures_to_file.ts @@ -14,6 +14,7 @@ import { ToolingLog } from '@kbn/dev-utils'; import { REPO_ROOT } from '@kbn/utils'; import { escape } from 'he'; +import { BuildkiteMetadata } from './buildkite_metadata'; import { TestFailure } from './get_failures'; const findScreenshots = (dirPath: string, allScreenshots: string[] = []) => { @@ -37,7 +38,11 @@ const findScreenshots = (dirPath: string, allScreenshots: string[] = []) => { return allScreenshots; }; -export function reportFailuresToFile(log: ToolingLog, failures: TestFailure[]) { +export function reportFailuresToFile( + log: ToolingLog, + failures: TestFailure[], + bkMeta: BuildkiteMetadata +) { if (!failures?.length) { return; } @@ -76,28 +81,15 @@ export function reportFailuresToFile(log: ToolingLog, failures: TestFailure[]) { .flat() .join('\n'); - // Buildkite steps that use `parallelism` need a numerical suffix added to identify them - // We should also increment the number by one, since it's 0-based - const jobNumberSuffix = process.env.BUILDKITE_PARALLEL_JOB - ? ` #${parseInt(process.env.BUILDKITE_PARALLEL_JOB, 10) + 1}` - : ''; - - const buildUrl = process.env.BUILDKITE_BUILD_URL || ''; - const jobUrl = process.env.BUILDKITE_JOB_ID - ? `${buildUrl}#${process.env.BUILDKITE_JOB_ID}` - : ''; - const failureJSON = JSON.stringify( { ...failure, hash, - buildId: process.env.BUJILDKITE_BUILD_ID || '', - jobId: process.env.BUILDKITE_JOB_ID || '', - url: buildUrl, - jobUrl, - jobName: process.env.BUILDKITE_LABEL - ? `${process.env.BUILDKITE_LABEL}${jobNumberSuffix}` - : '', + buildId: bkMeta.buildId, + jobId: bkMeta.jobId, + url: bkMeta.url, + jobUrl: bkMeta.jobUrl, + jobName: bkMeta.jobName, }, null, 2 @@ -149,11 +141,11 @@ export function reportFailuresToFile(log: ToolingLog, failures: TestFailure[]) {

${ - jobUrl + bkMeta.jobUrl ? `

Buildkite Job
- ${escape(jobUrl)} + ${escape(bkMeta.jobUrl)}

` : '' diff --git a/packages/kbn-test/src/failed_tests_reporter/run_failed_tests_reporter_cli.ts b/packages/kbn-test/src/failed_tests_reporter/run_failed_tests_reporter_cli.ts index 193bc668ce003..6ab135a6afa7e 100644 --- a/packages/kbn-test/src/failed_tests_reporter/run_failed_tests_reporter_cli.ts +++ b/packages/kbn-test/src/failed_tests_reporter/run_failed_tests_reporter_cli.ts @@ -9,7 +9,7 @@ import Path from 'path'; import { REPO_ROOT } from '@kbn/utils'; -import { run, createFailError, createFlagError } from '@kbn/dev-utils'; +import { run, createFailError, createFlagError, CiStatsReporter } from '@kbn/dev-utils'; import globby from 'globby'; import normalize from 'normalize-path'; @@ -22,6 +22,7 @@ import { addMessagesToReport } from './add_messages_to_report'; import { getReportMessageIter } from './report_metadata'; import { reportFailuresToEs } from './report_failures_to_es'; import { reportFailuresToFile } from './report_failures_to_file'; +import { getBuildkiteMetadata } from './buildkite_metadata'; const DEFAULT_PATTERNS = [Path.resolve(REPO_ROOT, 'target/junit/**/*.xml')]; @@ -71,108 +72,129 @@ export function runFailedTestsReporterCli() { dryRun: !updateGithub, }); - const buildUrl = flags['build-url'] || (updateGithub ? '' : 'http://buildUrl'); - if (typeof buildUrl !== 'string' || !buildUrl) { - throw createFlagError('Missing --build-url or process.env.BUILD_URL'); - } + const bkMeta = getBuildkiteMetadata(); - const patterns = (flags._.length ? flags._ : DEFAULT_PATTERNS).map((p) => - normalize(Path.resolve(p)) - ); - log.info('Searching for reports at', patterns); - const reportPaths = await globby(patterns, { - absolute: true, - }); + try { + const buildUrl = flags['build-url'] || (updateGithub ? '' : 'http://buildUrl'); + if (typeof buildUrl !== 'string' || !buildUrl) { + throw createFlagError('Missing --build-url or process.env.BUILD_URL'); + } - if (!reportPaths.length) { - throw createFailError(`Unable to find any junit reports with patterns [${patterns}]`); - } + const patterns = (flags._.length ? flags._ : DEFAULT_PATTERNS).map((p) => + normalize(Path.resolve(p)) + ); + log.info('Searching for reports at', patterns); + const reportPaths = await globby(patterns, { + absolute: true, + }); - log.info('found', reportPaths.length, 'junit reports', reportPaths); - const newlyCreatedIssues: Array<{ - failure: TestFailure; - newIssue: GithubIssueMini; - }> = []; + if (!reportPaths.length) { + throw createFailError(`Unable to find any junit reports with patterns [${patterns}]`); + } - for (const reportPath of reportPaths) { - const report = await readTestReport(reportPath); - const messages = Array.from(getReportMessageIter(report)); - const failures = await getFailures(report); + log.info('found', reportPaths.length, 'junit reports', reportPaths); + const newlyCreatedIssues: Array<{ + failure: TestFailure; + newIssue: GithubIssueMini; + }> = []; - if (indexInEs) { - await reportFailuresToEs(log, failures); - } + for (const reportPath of reportPaths) { + const report = await readTestReport(reportPath); + const messages = Array.from(getReportMessageIter(report)); + const failures = await getFailures(report); - for (const failure of failures) { - const pushMessage = (msg: string) => { - messages.push({ - classname: failure.classname, - name: failure.name, - message: msg, - }); - }; - - if (failure.likelyIrrelevant) { - pushMessage( - 'Failure is likely irrelevant' + - (updateGithub ? ', so an issue was not created or updated' : '') - ); - continue; + if (indexInEs) { + await reportFailuresToEs(log, failures); } - let existingIssue: GithubIssueMini | undefined = await githubApi.findFailedTestIssue( - (i) => - getIssueMetadata(i.body, 'test.class') === failure.classname && - getIssueMetadata(i.body, 'test.name') === failure.name - ); + for (const failure of failures) { + const pushMessage = (msg: string) => { + messages.push({ + classname: failure.classname, + name: failure.name, + message: msg, + }); + }; + + if (failure.likelyIrrelevant) { + pushMessage( + 'Failure is likely irrelevant' + + (updateGithub ? ', so an issue was not created or updated' : '') + ); + continue; + } - if (!existingIssue) { - const newlyCreated = newlyCreatedIssues.find( - ({ failure: f }) => f.classname === failure.classname && f.name === failure.name - ); + let existingIssue: GithubIssueMini | undefined = updateGithub + ? await githubApi.findFailedTestIssue( + (i) => + getIssueMetadata(i.body, 'test.class') === failure.classname && + getIssueMetadata(i.body, 'test.name') === failure.name + ) + : undefined; + + if (!existingIssue) { + const newlyCreated = newlyCreatedIssues.find( + ({ failure: f }) => f.classname === failure.classname && f.name === failure.name + ); + + if (newlyCreated) { + existingIssue = newlyCreated.newIssue; + } + } - if (newlyCreated) { - existingIssue = newlyCreated.newIssue; + if (existingIssue) { + const newFailureCount = await updateFailureIssue( + buildUrl, + existingIssue, + githubApi, + branch + ); + const url = existingIssue.html_url; + failure.githubIssue = url; + failure.failureCount = updateGithub ? newFailureCount : newFailureCount - 1; + pushMessage( + `Test has failed ${newFailureCount - 1} times on tracked branches: ${url}` + ); + if (updateGithub) { + pushMessage(`Updated existing issue: ${url} (fail count: ${newFailureCount})`); + } + continue; } - } - if (existingIssue) { - const newFailureCount = await updateFailureIssue( - buildUrl, - existingIssue, - githubApi, - branch - ); - const url = existingIssue.html_url; - failure.githubIssue = url; - failure.failureCount = updateGithub ? newFailureCount : newFailureCount - 1; - pushMessage(`Test has failed ${newFailureCount - 1} times on tracked branches: ${url}`); + const newIssue = await createFailureIssue(buildUrl, failure, githubApi, branch); + pushMessage('Test has not failed recently on tracked branches'); if (updateGithub) { - pushMessage(`Updated existing issue: ${url} (fail count: ${newFailureCount})`); + pushMessage(`Created new issue: ${newIssue.html_url}`); + failure.githubIssue = newIssue.html_url; } - continue; - } - - const newIssue = await createFailureIssue(buildUrl, failure, githubApi, branch); - pushMessage('Test has not failed recently on tracked branches'); - if (updateGithub) { - pushMessage(`Created new issue: ${newIssue.html_url}`); - failure.githubIssue = newIssue.html_url; + newlyCreatedIssues.push({ failure, newIssue }); + failure.failureCount = updateGithub ? 1 : 0; } - newlyCreatedIssues.push({ failure, newIssue }); - failure.failureCount = updateGithub ? 1 : 0; - } - // mutates report to include messages and writes updated report to disk - await addMessagesToReport({ - report, - messages, - log, - reportPath, - dryRun: !flags['report-update'], - }); + // mutates report to include messages and writes updated report to disk + await addMessagesToReport({ + report, + messages, + log, + reportPath, + dryRun: !flags['report-update'], + }); - reportFailuresToFile(log, failures); + reportFailuresToFile(log, failures, bkMeta); + } + } finally { + await CiStatsReporter.fromEnv(log).metrics([ + { + group: 'github api request count', + id: `failed test reporter`, + value: githubApi.getRequestCount(), + meta: Object.fromEntries( + Object.entries(bkMeta).map( + ([k, v]) => [`buildkite${k[0].toUpperCase()}${k.slice(1)}`, v] as const + ) + ), + }, + ]); } }, { diff --git a/packages/kbn-test/src/functional_test_runner/lib/mocha/validate_ci_group_tags.js b/packages/kbn-test/src/functional_test_runner/lib/mocha/validate_ci_group_tags.js index 3446c5be5d4a7..4f798839d7231 100644 --- a/packages/kbn-test/src/functional_test_runner/lib/mocha/validate_ci_group_tags.js +++ b/packages/kbn-test/src/functional_test_runner/lib/mocha/validate_ci_group_tags.js @@ -8,7 +8,7 @@ import Path from 'path'; -import { REPO_ROOT } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; /** * Traverse the suites configured and ensure that each suite has no more than one ciGroup assigned diff --git a/packages/kbn-test/src/functional_test_runner/lib/suite_tracker.test.ts b/packages/kbn-test/src/functional_test_runner/lib/suite_tracker.test.ts index e87f316a100a7..53ce4c74c1388 100644 --- a/packages/kbn-test/src/functional_test_runner/lib/suite_tracker.test.ts +++ b/packages/kbn-test/src/functional_test_runner/lib/suite_tracker.test.ts @@ -14,7 +14,7 @@ jest.mock('@kbn/utils', () => { return { REPO_ROOT: '/dev/null/root' }; }); -import { REPO_ROOT } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import { Lifecycle } from './lifecycle'; import { SuiteTracker } from './suite_tracker'; import { Suite } from '../fake_mocha_types'; diff --git a/packages/kbn-test/src/functional_tests/lib/babel_register_for_test_plugins.js b/packages/kbn-test/src/functional_tests/lib/babel_register_for_test_plugins.js index 03947f7e267ba..63d2b56350ba1 100644 --- a/packages/kbn-test/src/functional_tests/lib/babel_register_for_test_plugins.js +++ b/packages/kbn-test/src/functional_tests/lib/babel_register_for_test_plugins.js @@ -9,7 +9,7 @@ const Fs = require('fs'); const Path = require('path'); -const { REPO_ROOT: REPO_ROOT_FOLLOWING_SYMLINKS } = require('@kbn/dev-utils'); +const { REPO_ROOT: REPO_ROOT_FOLLOWING_SYMLINKS } = require('@kbn/utils'); const BASE_REPO_ROOT = Path.resolve( Fs.realpathSync(Path.resolve(REPO_ROOT_FOLLOWING_SYMLINKS, 'package.json')), '..' diff --git a/packages/kbn-test/src/functional_tests/tasks.ts b/packages/kbn-test/src/functional_tests/tasks.ts index 6dde114d3a98e..6a6c7edb98c79 100644 --- a/packages/kbn-test/src/functional_tests/tasks.ts +++ b/packages/kbn-test/src/functional_tests/tasks.ts @@ -9,7 +9,8 @@ import { relative } from 'path'; import * as Rx from 'rxjs'; import { startWith, switchMap, take } from 'rxjs/operators'; -import { withProcRunner, ToolingLog, REPO_ROOT, getTimeReporter } from '@kbn/dev-utils'; +import { withProcRunner, ToolingLog, getTimeReporter } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import dedent from 'dedent'; import { diff --git a/packages/kbn-test/src/kbn_client/kbn_client_import_export.ts b/packages/kbn-test/src/kbn_client/kbn_client_import_export.ts index 4adae7d1cd031..6da34228bbe7f 100644 --- a/packages/kbn-test/src/kbn_client/kbn_client_import_export.ts +++ b/packages/kbn-test/src/kbn_client/kbn_client_import_export.ts @@ -12,7 +12,8 @@ import { existsSync } from 'fs'; import Path from 'path'; import FormData from 'form-data'; -import { ToolingLog, isAxiosResponseError, createFailError, REPO_ROOT } from '@kbn/dev-utils'; +import { ToolingLog, isAxiosResponseError, createFailError } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import { KbnClientRequester, uriencode, ReqOptions } from './kbn_client_requester'; import { KbnClientSavedObjects } from './kbn_client_saved_objects'; diff --git a/packages/kbn-typed-react-router-config/src/types/index.ts b/packages/kbn-typed-react-router-config/src/types/index.ts index c1ae5afd816ee..f15fd99a02a87 100644 --- a/packages/kbn-typed-react-router-config/src/types/index.ts +++ b/packages/kbn-typed-react-router-config/src/types/index.ts @@ -13,97 +13,13 @@ import { RequiredKeys, ValuesType } from 'utility-types'; // import { unconst } from '../unconst'; import { NormalizePath } from './utils'; -type PathsOfRoute = - | TRoute['path'] - | (TRoute extends { children: Route[] } - ? AppendPath | PathsOf - : never); - -export type PathsOf = TRoutes extends [] - ? never - : TRoutes extends [Route] - ? PathsOfRoute - : TRoutes extends [Route, Route] - ? PathsOfRoute | PathsOfRoute - : TRoutes extends [Route, Route, Route] - ? PathsOfRoute | PathsOfRoute | PathsOfRoute - : TRoutes extends [Route, Route, Route, Route] - ? - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - : TRoutes extends [Route, Route, Route, Route, Route] - ? - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - : TRoutes extends [Route, Route, Route, Route, Route, Route] - ? - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - : TRoutes extends [Route, Route, Route, Route, Route, Route, Route] - ? - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - : TRoutes extends [Route, Route, Route, Route, Route, Route, Route, Route] - ? - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - : TRoutes extends [Route, Route, Route, Route, Route, Route, Route, Route, Route] - ? - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - : TRoutes extends [Route, Route, Route, Route, Route, Route, Route, Route, Route, Route] - ? - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - : TRoutes extends [Route, Route, Route, Route, Route, Route, Route, Route, Route, Route, Route] - ? - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - : string; +// type PathsOfRoute = +// | TRoute['path'] +// | (TRoute extends { children: Route[] } +// ? AppendPath | PathsOf +// : never); + +export type PathsOf = keyof MapRoutes & string; export interface RouteMatch { route: TRoute; @@ -347,6 +263,14 @@ type MapRoutes = TRoutes extends [Route] // const routes = unconst([ // { +// path: '/link-to/transaction/{transactionId}', +// element, +// }, +// { +// path: '/link-to/trace/{traceId}', +// element, +// }, +// { // path: '/', // element, // children: [ @@ -393,6 +317,10 @@ type MapRoutes = TRoutes extends [Route] // element, // }, // { +// path: '/settings/agent-keys', +// element, +// }, +// { // path: '/settings', // element, // }, @@ -430,11 +358,19 @@ type MapRoutes = TRoutes extends [Route] // element, // }, // { +// path: '/services/:serviceName/transactions/view', +// element, +// }, +// { +// path: '/services/:serviceName/dependencies', +// element, +// }, +// { // path: '/services/:serviceName/errors', // element, // children: [ // { -// path: '/:groupId', +// path: '/services/:serviceName/errors/:groupId', // element, // params: t.type({ // path: t.type({ @@ -443,7 +379,7 @@ type MapRoutes = TRoutes extends [Route] // }), // }, // { -// path: '/services/:serviceName', +// path: '/services/:serviceName/errors', // element, // params: t.partial({ // query: t.partial({ @@ -457,15 +393,33 @@ type MapRoutes = TRoutes extends [Route] // ], // }, // { -// path: '/services/:serviceName/foo', +// path: '/services/:serviceName/metrics', +// element, +// }, +// { +// path: '/services/:serviceName/nodes', +// element, +// children: [ +// { +// path: '/services/{serviceName}/nodes/{serviceNodeName}/metrics', +// element, +// }, +// { +// path: '/services/:serviceName/nodes', +// element, +// }, +// ], +// }, +// { +// path: '/services/:serviceName/service-map', // element, // }, // { -// path: '/services/:serviceName/bar', +// path: '/services/:serviceName/logs', // element, // }, // { -// path: '/services/:serviceName/baz', +// path: '/services/:serviceName/profiling', // element, // }, // { @@ -497,6 +451,24 @@ type MapRoutes = TRoutes extends [Route] // element, // }, // { +// path: '/backends', +// element, +// children: [ +// { +// path: '/backends/{backendName}/overview', +// element, +// }, +// { +// path: '/backends/overview', +// element, +// }, +// { +// path: '/backends', +// element, +// }, +// ], +// }, +// { // path: '/', // element, // }, @@ -509,10 +481,11 @@ type MapRoutes = TRoutes extends [Route] // type Routes = typeof routes; // type Mapped = keyof MapRoutes; +// type Paths = PathsOf; // type Bar = ValuesType>['route']['path']; // type Foo = OutputOf; -// type Baz = OutputOf; +// // type Baz = OutputOf; // const { path }: Foo = {} as any; @@ -520,4 +493,4 @@ type MapRoutes = TRoutes extends [Route] // return {} as any; // } -// const params = _useApmParams('/*'); +// // const params = _useApmParams('/services/:serviceName/nodes/*'); diff --git a/src/cli/serve/integration_tests/invalid_config.test.ts b/src/cli/serve/integration_tests/invalid_config.test.ts index 2de902582a548..ca051f37a816e 100644 --- a/src/cli/serve/integration_tests/invalid_config.test.ts +++ b/src/cli/serve/integration_tests/invalid_config.test.ts @@ -8,7 +8,7 @@ import { spawnSync } from 'child_process'; -import { REPO_ROOT } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; const INVALID_CONFIG_PATH = require.resolve('./__fixtures__/invalid_config.yml'); diff --git a/src/core/public/apm_system.test.ts b/src/core/public/apm_system.test.ts index f62421cb55abc..842d5de7e5afc 100644 --- a/src/core/public/apm_system.test.ts +++ b/src/core/public/apm_system.test.ts @@ -9,6 +9,7 @@ jest.mock('@elastic/apm-rum'); import type { DeeplyMockedKeys, MockedKeys } from '@kbn/utility-types/jest'; import { init, apm } from '@elastic/apm-rum'; +import type { Transaction } from '@elastic/apm-rum'; import { ApmSystem } from './apm_system'; import { Subject } from 'rxjs'; import { InternalApplicationStart } from './application/types'; diff --git a/src/core/public/apm_system.ts b/src/core/public/apm_system.ts index f15a317f9f934..2231f394381f0 100644 --- a/src/core/public/apm_system.ts +++ b/src/core/public/apm_system.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -import type { ApmBase, AgentConfigOptions } from '@elastic/apm-rum'; +import type { ApmBase, AgentConfigOptions, Transaction } from '@elastic/apm-rum'; import { modifyUrl } from '@kbn/std'; import { CachedResourceObserver } from './apm_resource_counter'; import type { InternalApplicationStart } from './application'; diff --git a/src/core/public/doc_links/doc_links_service.ts b/src/core/public/doc_links/doc_links_service.ts index 692367cd0f580..fed3aa3093166 100644 --- a/src/core/public/doc_links/doc_links_service.ts +++ b/src/core/public/doc_links/doc_links_service.ts @@ -486,6 +486,7 @@ export class DocLinksService { hdfsRepo: `${PLUGIN_DOCS}repository-hdfs.html`, s3Repo: `${PLUGIN_DOCS}repository-s3.html`, snapshotRestoreRepos: `${PLUGIN_DOCS}repository.html`, + mapperSize: `${PLUGIN_DOCS}mapper-size-usage.html`, }, snapshotRestore: { guide: `${ELASTICSEARCH_DOCS}snapshot-restore.html`, @@ -874,7 +875,14 @@ export interface DocLinksStart { }>; readonly watcher: Record; readonly ccs: Record; - readonly plugins: Record; + readonly plugins: { + azureRepo: string; + gcsRepo: string; + hdfsRepo: string; + s3Repo: string; + snapshotRestoreRepos: string; + mapperSize: string; + }; readonly snapshotRestore: Record; readonly ingest: Record; readonly fleet: Readonly<{ diff --git a/src/core/public/i18n/__snapshots__/i18n_service.test.tsx.snap b/src/core/public/i18n/__snapshots__/i18n_service.test.tsx.snap index e93ef34c38025..1c394112a404c 100644 --- a/src/core/public/i18n/__snapshots__/i18n_service.test.tsx.snap +++ b/src/core/public/i18n/__snapshots__/i18n_service.test.tsx.snap @@ -98,6 +98,7 @@ exports[`#start() returns \`Context\` component 1`] = ` "euiDataGridToolbar.fullScreenButtonActive": "Exit full screen", "euiDatePopoverButton.invalidTitle": [Function], "euiDatePopoverButton.outdatedTitle": [Function], + "euiErrorBoundary.error": "Error", "euiFieldPassword.maskPassword": "Mask password", "euiFieldPassword.showPassword": "Show password as plain text. Note: this will visually expose your password on the screen.", "euiFilePicker.clearSelectedFiles": "Clear selected files", @@ -218,7 +219,7 @@ exports[`#start() returns \`Context\` component 1`] = ` "euiStyleSelector.labelExpanded": "Expanded density", "euiStyleSelector.labelNormal": "Normal density", "euiSuperDatePicker.showDatesButtonLabel": "Show dates", - "euiSuperSelect.screenReaderAnnouncement": [Function], + "euiSuperSelect.screenReaderAnnouncement": "You are in a form selector and must select a single option. Use the up and down keys to navigate or escape to close.", "euiSuperSelectControl.selectAnOption": [Function], "euiSuperUpdateButton.cannotUpdateTooltip": "Cannot update", "euiSuperUpdateButton.clickToApplyTooltip": "Click to apply", diff --git a/src/core/public/i18n/i18n_eui_mapping.tsx b/src/core/public/i18n/i18n_eui_mapping.tsx index 7c4d39fa2b11a..e3357d138e794 100644 --- a/src/core/public/i18n/i18n_eui_mapping.tsx +++ b/src/core/public/i18n/i18n_eui_mapping.tsx @@ -663,6 +663,10 @@ export const getEuiContextMapping = (): EuiTokensObject => { defaultMessage: '+ {messagesLength} more', values: { messagesLength }, }), + 'euiErrorBoundary.error': i18n.translate('core.euiErrorBoundary.error', { + defaultMessage: 'Error', + description: 'Error boundary for uncaught exceptions when rendering part of the application', + }), 'euiNotificationEventMessages.accordionAriaLabelButtonText': ({ messagesLength, eventName, @@ -1046,12 +1050,13 @@ export const getEuiContextMapping = (): EuiTokensObject => { description: 'Displayed in a button that shows date picker', } ), - 'euiSuperSelect.screenReaderAnnouncement': ({ optionsCount }: EuiValues) => - i18n.translate('core.euiSuperSelect.screenReaderAnnouncement', { + 'euiSuperSelect.screenReaderAnnouncement': i18n.translate( + 'core.euiSuperSelect.screenReaderAnnouncement', + { defaultMessage: - 'You are in a form selector of {optionsCount} items and must select a single option. Use the up and down keys to navigate or escape to close.', - values: { optionsCount }, - }), + 'You are in a form selector and must select a single option. Use the up and down keys to navigate or escape to close.', + } + ), 'euiSuperSelectControl.selectAnOption': ({ selectedValue }: EuiValues) => i18n.translate('core.euiSuperSelectControl.selectAnOption', { defaultMessage: 'Select an option: {selectedValue}, is selected', diff --git a/src/core/public/public.api.md b/src/core/public/public.api.md index 08d41ab1301b0..63e0898b5fb90 100644 --- a/src/core/public/public.api.md +++ b/src/core/public/public.api.md @@ -773,7 +773,14 @@ export interface DocLinksStart { }>; readonly watcher: Record; readonly ccs: Record; - readonly plugins: Record; + readonly plugins: { + azureRepo: string; + gcsRepo: string; + hdfsRepo: string; + s3Repo: string; + snapshotRestoreRepos: string; + mapperSize: string; + }; readonly snapshotRestore: Record; readonly ingest: Record; readonly fleet: Readonly<{ diff --git a/src/core/server/capabilities/integration_tests/capabilities_service.test.ts b/src/core/server/capabilities/integration_tests/capabilities_service.test.ts index 2e80fbb9d20c0..c1f6ffb5add77 100644 --- a/src/core/server/capabilities/integration_tests/capabilities_service.test.ts +++ b/src/core/server/capabilities/integration_tests/capabilities_service.test.ts @@ -7,7 +7,7 @@ */ import supertest from 'supertest'; -import { REPO_ROOT } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import { HttpService, InternalHttpServicePreboot, InternalHttpServiceSetup } from '../../http'; import { contextServiceMock } from '../../context/context_service.mock'; import { executionContextServiceMock } from '../../execution_context/execution_context_service.mock'; diff --git a/src/core/server/core_context.mock.ts b/src/core/server/core_context.mock.ts index ddb87d31383c8..4d7b4e1ba5548 100644 --- a/src/core/server/core_context.mock.ts +++ b/src/core/server/core_context.mock.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -import { REPO_ROOT } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import type { DeeplyMockedKeys } from '@kbn/utility-types/jest'; import { CoreContext } from './core_context'; import { Env, IConfigService } from './config'; diff --git a/src/core/server/elasticsearch/client/configure_client.test.ts b/src/core/server/elasticsearch/client/configure_client.test.ts index 7988e81045d17..f252993415afa 100644 --- a/src/core/server/elasticsearch/client/configure_client.test.ts +++ b/src/core/server/elasticsearch/client/configure_client.test.ts @@ -6,21 +6,16 @@ * Side Public License, v 1. */ -import { Buffer } from 'buffer'; -import { Readable } from 'stream'; - -import { errors } from '@elastic/elasticsearch'; -import type { - TransportRequestOptions, - TransportRequestParams, - DiagnosticResult, - RequestBody, -} from '@elastic/elasticsearch'; +jest.mock('./log_query_and_deprecation.ts', () => ({ + __esModule: true, + instrumentEsQueryAndDeprecationLogger: jest.fn(), +})); import { parseClientOptionsMock, ClientMock } from './configure_client.test.mocks'; import { loggingSystemMock } from '../../logging/logging_system.mock'; import type { ElasticsearchClientConfig } from './client_config'; import { configureClient } from './configure_client'; +import { instrumentEsQueryAndDeprecationLogger } from './log_query_and_deprecation'; const createFakeConfig = ( parts: Partial = {} @@ -36,40 +31,9 @@ const createFakeClient = () => { const client = new actualEs.Client({ nodes: ['http://localhost'], // Enforcing `nodes` because it's mandatory }); - jest.spyOn(client.diagnostic, 'on'); return client; }; -const createApiResponse = ({ - body, - statusCode = 200, - headers = {}, - warnings = [], - params, - requestOptions = {}, -}: { - body: T; - statusCode?: number; - headers?: Record; - warnings?: string[]; - params?: TransportRequestParams; - requestOptions?: TransportRequestOptions; -}): DiagnosticResult => { - return { - body, - statusCode, - headers, - warnings, - meta: { - body, - request: { - params: params!, - options: requestOptions, - } as any, - } as any, - }; -}; - describe('configureClient', () => { let logger: ReturnType; let config: ElasticsearchClientConfig; @@ -84,6 +48,7 @@ describe('configureClient', () => { afterEach(() => { parseClientOptionsMock.mockReset(); ClientMock.mockReset(); + jest.clearAllMocks(); }); it('calls `parseClientOptions` with the correct parameters', () => { @@ -113,366 +78,14 @@ describe('configureClient', () => { expect(client).toBe(ClientMock.mock.results[0].value); }); - it('listens to client on `response` events', () => { + it('calls instrumentEsQueryAndDeprecationLogger', () => { const client = configureClient(config, { logger, type: 'test', scoped: false }); - expect(client.diagnostic.on).toHaveBeenCalledTimes(1); - expect(client.diagnostic.on).toHaveBeenCalledWith('response', expect.any(Function)); - }); - - describe('Client logging', () => { - function createResponseWithBody(body?: RequestBody) { - return createApiResponse({ - body: {}, - statusCode: 200, - params: { - method: 'GET', - path: '/foo', - querystring: { hello: 'dolly' }, - body, - }, - }); - } - - describe('logs each query', () => { - it('creates a query logger context based on the `type` parameter', () => { - configureClient(createFakeConfig(), { logger, type: 'test123' }); - expect(logger.get).toHaveBeenCalledWith('query', 'test123'); - }); - - it('when request body is an object', () => { - const client = configureClient(createFakeConfig(), { logger, type: 'test', scoped: false }); - - const response = createResponseWithBody({ - seq_no_primary_term: true, - query: { - term: { user: 'kimchy' }, - }, - }); - - client.diagnostic.emit('response', null, response); - expect(loggingSystemMock.collect(logger).debug).toMatchInlineSnapshot(` - Array [ - Array [ - "200 - GET /foo?hello=dolly - {\\"seq_no_primary_term\\":true,\\"query\\":{\\"term\\":{\\"user\\":\\"kimchy\\"}}}", - undefined, - ], - ] - `); - }); - - it('when request body is a string', () => { - const client = configureClient(createFakeConfig(), { logger, type: 'test', scoped: false }); - - const response = createResponseWithBody( - JSON.stringify({ - seq_no_primary_term: true, - query: { - term: { user: 'kimchy' }, - }, - }) - ); - - client.diagnostic.emit('response', null, response); - expect(loggingSystemMock.collect(logger).debug).toMatchInlineSnapshot(` - Array [ - Array [ - "200 - GET /foo?hello=dolly - {\\"seq_no_primary_term\\":true,\\"query\\":{\\"term\\":{\\"user\\":\\"kimchy\\"}}}", - undefined, - ], - ] - `); - }); - - it('when request body is a buffer', () => { - const client = configureClient(createFakeConfig(), { logger, type: 'test', scoped: false }); - - const response = createResponseWithBody( - Buffer.from( - JSON.stringify({ - seq_no_primary_term: true, - query: { - term: { user: 'kimchy' }, - }, - }) - ) - ); - - client.diagnostic.emit('response', null, response); - expect(loggingSystemMock.collect(logger).debug).toMatchInlineSnapshot(` - Array [ - Array [ - "200 - GET /foo?hello=dolly - [buffer]", - undefined, - ], - ] - `); - }); - - it('when request body is a readable stream', () => { - const client = configureClient(createFakeConfig(), { logger, type: 'test', scoped: false }); - - const response = createResponseWithBody( - Readable.from( - JSON.stringify({ - seq_no_primary_term: true, - query: { - term: { user: 'kimchy' }, - }, - }) - ) - ); - - client.diagnostic.emit('response', null, response); - expect(loggingSystemMock.collect(logger).debug).toMatchInlineSnapshot(` - Array [ - Array [ - "200 - GET /foo?hello=dolly - [stream]", - undefined, - ], - ] - `); - }); - - it('when request body is not defined', () => { - const client = configureClient(createFakeConfig(), { logger, type: 'test', scoped: false }); - - const response = createResponseWithBody(); - - client.diagnostic.emit('response', null, response); - expect(loggingSystemMock.collect(logger).debug).toMatchInlineSnapshot(` - Array [ - Array [ - "200 - GET /foo?hello=dolly", - undefined, - ], - ] - `); - }); - - it('properly encode queries', () => { - const client = configureClient(createFakeConfig(), { logger, type: 'test', scoped: false }); - - const response = createApiResponse({ - body: {}, - statusCode: 200, - params: { - method: 'GET', - path: '/foo', - querystring: { city: 'Münich' }, - }, - }); - - client.diagnostic.emit('response', null, response); - - expect(loggingSystemMock.collect(logger).debug).toMatchInlineSnapshot(` - Array [ - Array [ - "200 - GET /foo?city=M%C3%BCnich", - undefined, - ], - ] - `); - }); - - it('logs queries even in case of errors', () => { - const client = configureClient(createFakeConfig(), { logger, type: 'test', scoped: false }); - - const response = createApiResponse({ - statusCode: 500, - body: { - error: { - type: 'internal server error', - }, - }, - params: { - method: 'GET', - path: '/foo', - querystring: { hello: 'dolly' }, - body: { - seq_no_primary_term: true, - query: { - term: { user: 'kimchy' }, - }, - }, - }, - }); - client.diagnostic.emit('response', new errors.ResponseError(response), response); - - expect(loggingSystemMock.collect(logger).debug).toMatchInlineSnapshot(` - Array [ - Array [ - "500 - GET /foo?hello=dolly - {\\"seq_no_primary_term\\":true,\\"query\\":{\\"term\\":{\\"user\\":\\"kimchy\\"}}} [internal server error]: internal server error", - undefined, - ], - ] - `); - }); - - it('logs debug when the client emits an @elastic/elasticsearch error', () => { - const client = configureClient(createFakeConfig(), { logger, type: 'test', scoped: false }); - - const response = createApiResponse({ body: {} }); - client.diagnostic.emit('response', new errors.TimeoutError('message', response), response); - - expect(loggingSystemMock.collect(logger).debug).toMatchInlineSnapshot(` - Array [ - Array [ - "[TimeoutError]: message", - undefined, - ], - ] - `); - }); - - it('logs debug when the client emits an ResponseError returned by elasticsearch', () => { - const client = configureClient(createFakeConfig(), { logger, type: 'test', scoped: false }); - - const response = createApiResponse({ - statusCode: 400, - headers: {}, - params: { - method: 'GET', - path: '/_path', - querystring: { hello: 'dolly' }, - }, - body: { - error: { - type: 'illegal_argument_exception', - reason: 'request [/_path] contains unrecognized parameter: [name]', - }, - }, - }); - client.diagnostic.emit('response', new errors.ResponseError(response), response); - - expect(loggingSystemMock.collect(logger).debug).toMatchInlineSnapshot(` - Array [ - Array [ - "400 - GET /_path?hello=dolly [illegal_argument_exception]: request [/_path] contains unrecognized parameter: [name]", - undefined, - ], - ] - `); - }); - - it('logs default error info when the error response body is empty', () => { - const client = configureClient(createFakeConfig(), { logger, type: 'test', scoped: false }); - - let response: DiagnosticResult = createApiResponse({ - statusCode: 400, - headers: {}, - params: { - method: 'GET', - path: '/_path', - }, - body: { - error: {}, - }, - }); - client.diagnostic.emit('response', new errors.ResponseError(response), response); - - expect(loggingSystemMock.collect(logger).debug).toMatchInlineSnapshot(` - Array [ - Array [ - "400 - GET /_path [undefined]: {\\"error\\":{}}", - undefined, - ], - ] - `); - - logger.debug.mockClear(); - - response = createApiResponse({ - statusCode: 400, - headers: {}, - params: { - method: 'GET', - path: '/_path', - }, - body: undefined, - }); - client.diagnostic.emit('response', new errors.ResponseError(response), response); - - expect(loggingSystemMock.collect(logger).debug).toMatchInlineSnapshot(` - Array [ - Array [ - "400 - GET /_path [undefined]: Response Error", - undefined, - ], - ] - `); - }); - - it('adds meta information to logs', () => { - const client = configureClient(createFakeConfig(), { logger, type: 'test', scoped: false }); - - let response = createApiResponse({ - statusCode: 400, - headers: {}, - params: { - method: 'GET', - path: '/_path', - }, - requestOptions: { - opaqueId: 'opaque-id', - }, - body: { - error: {}, - }, - }); - client.diagnostic.emit('response', null, response); - - expect(loggingSystemMock.collect(logger).debug[0][1]).toMatchInlineSnapshot(` - Object { - "http": Object { - "request": Object { - "id": "opaque-id", - }, - }, - } - `); - - logger.debug.mockClear(); - - response = createApiResponse({ - statusCode: 400, - headers: {}, - params: { - method: 'GET', - path: '/_path', - }, - requestOptions: { - opaqueId: 'opaque-id', - }, - body: {} as any, - }); - client.diagnostic.emit('response', new errors.ResponseError(response), response); - - expect(loggingSystemMock.collect(logger).debug[0][1]).toMatchInlineSnapshot(` - Object { - "http": Object { - "request": Object { - "id": "opaque-id", - }, - }, - } - `); - }); + expect(instrumentEsQueryAndDeprecationLogger).toHaveBeenCalledTimes(1); + expect(instrumentEsQueryAndDeprecationLogger).toHaveBeenCalledWith({ + logger, + client, + type: 'test', }); }); }); diff --git a/src/core/server/elasticsearch/client/configure_client.ts b/src/core/server/elasticsearch/client/configure_client.ts index fc8a06660cc5e..e48a36fa4fe58 100644 --- a/src/core/server/elasticsearch/client/configure_client.ts +++ b/src/core/server/elasticsearch/client/configure_client.ts @@ -6,21 +6,17 @@ * Side Public License, v 1. */ -import { Buffer } from 'buffer'; -import { stringify } from 'querystring'; -import { Client, errors, Transport, HttpConnection } from '@elastic/elasticsearch'; +import { Client, Transport, HttpConnection } from '@elastic/elasticsearch'; import type { KibanaClient } from '@elastic/elasticsearch/lib/api/kibana'; import type { TransportRequestParams, TransportRequestOptions, TransportResult, - DiagnosticResult, - RequestBody, } from '@elastic/elasticsearch'; import { Logger } from '../../logging'; import { parseClientOptions, ElasticsearchClientConfig } from './client_config'; -import type { ElasticsearchErrorDetails } from './types'; +import { instrumentEsQueryAndDeprecationLogger } from './log_query_and_deprecation'; const noop = () => undefined; @@ -61,91 +57,8 @@ export const configureClient = ( Transport: KibanaTransport, Connection: HttpConnection, }); - addLogging(client, logger.get('query', type)); - return client as KibanaClient; -}; - -const convertQueryString = (qs: string | Record | undefined): string => { - if (qs === undefined || typeof qs === 'string') { - return qs ?? ''; - } - return stringify(qs); -}; - -function ensureString(body: RequestBody): string { - if (typeof body === 'string') return body; - if (Buffer.isBuffer(body)) return '[buffer]'; - if ('readable' in body && body.readable && typeof body._read === 'function') return '[stream]'; - return JSON.stringify(body); -} - -/** - * Returns a debug message from an Elasticsearch error in the following format: - * [error type] error reason - */ -export function getErrorMessage(error: errors.ElasticsearchClientError): string { - if (error instanceof errors.ResponseError) { - const errorBody = error.meta.body as ElasticsearchErrorDetails; - return `[${errorBody?.error?.type}]: ${errorBody?.error?.reason ?? error.message}`; - } - return `[${error.name}]: ${error.message}`; -} + instrumentEsQueryAndDeprecationLogger({ logger, client, type }); -/** - * returns a string in format: - * - * status code - * method URL - * request body - * - * so it could be copy-pasted into the Dev console - */ -function getResponseMessage(event: DiagnosticResult): string { - const errorMeta = getRequestDebugMeta(event); - const body = errorMeta.body ? `\n${errorMeta.body}` : ''; - return `${errorMeta.statusCode}\n${errorMeta.method} ${errorMeta.url}${body}`; -} - -/** - * Returns stringified debug information from an Elasticsearch request event - * useful for logging in case of an unexpected failure. - */ -export function getRequestDebugMeta(event: DiagnosticResult): { - url: string; - body: string; - statusCode: number | null; - method: string; -} { - const params = event.meta.request.params; - // definition is wrong, `params.querystring` can be either a string or an object - const querystring = convertQueryString(params.querystring); - return { - url: `${params.path}${querystring ? `?${querystring}` : ''}`, - body: params.body ? `${ensureString(params.body)}` : '', - method: params.method, - statusCode: event.statusCode!, - }; -} - -const addLogging = (client: Client, logger: Logger) => { - client.diagnostic.on('response', (error, event) => { - if (event) { - const opaqueId = event.meta.request.options.opaqueId; - const meta = opaqueId - ? { - http: { request: { id: event.meta.request.options.opaqueId } }, - } - : undefined; // do not clutter logs if opaqueId is not present - if (error) { - if (error instanceof errors.ResponseError) { - logger.debug(`${getResponseMessage(event)} ${getErrorMessage(error)}`, meta); - } else { - logger.debug(getErrorMessage(error), meta); - } - } else { - logger.debug(getResponseMessage(event), meta); - } - } - }); + return client as KibanaClient; }; diff --git a/src/core/server/elasticsearch/client/index.ts b/src/core/server/elasticsearch/client/index.ts index 2cf5a0229a489..123c498f1ee21 100644 --- a/src/core/server/elasticsearch/client/index.ts +++ b/src/core/server/elasticsearch/client/index.ts @@ -21,5 +21,6 @@ export type { IScopedClusterClient } from './scoped_cluster_client'; export type { ElasticsearchClientConfig } from './client_config'; export { ClusterClient } from './cluster_client'; export type { IClusterClient, ICustomClusterClient } from './cluster_client'; -export { configureClient, getRequestDebugMeta, getErrorMessage } from './configure_client'; +export { configureClient } from './configure_client'; +export { getRequestDebugMeta, getErrorMessage } from './log_query_and_deprecation'; export { retryCallCluster, migrationRetryCallCluster } from './retry_call_cluster'; diff --git a/src/core/server/elasticsearch/client/log_query_and_deprecation.test.ts b/src/core/server/elasticsearch/client/log_query_and_deprecation.test.ts new file mode 100644 index 0000000000000..30d5d8b87ed1c --- /dev/null +++ b/src/core/server/elasticsearch/client/log_query_and_deprecation.test.ts @@ -0,0 +1,624 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { Buffer } from 'buffer'; +import { Readable } from 'stream'; + +import { + Client, + ConnectionRequestParams, + errors, + TransportRequestOptions, + TransportRequestParams, +} from '@elastic/elasticsearch'; +import type { DiagnosticResult, RequestBody } from '@elastic/elasticsearch'; + +import { parseClientOptionsMock, ClientMock } from './configure_client.test.mocks'; +import { loggingSystemMock } from '../../logging/logging_system.mock'; +import { instrumentEsQueryAndDeprecationLogger } from './log_query_and_deprecation'; + +const createApiResponse = ({ + body, + statusCode = 200, + headers = {}, + warnings = null, + params, + requestOptions = {}, +}: { + body: T; + statusCode?: number; + headers?: Record; + warnings?: string[] | null; + params?: TransportRequestParams | ConnectionRequestParams; + requestOptions?: TransportRequestOptions; +}): DiagnosticResult => { + return { + body, + statusCode, + headers, + warnings, + meta: { + body, + request: { + params: params!, + options: requestOptions, + } as any, + } as any, + }; +}; + +const createFakeClient = () => { + const actualEs = jest.requireActual('@elastic/elasticsearch'); + const client = new actualEs.Client({ + nodes: ['http://localhost'], // Enforcing `nodes` because it's mandatory + }); + jest.spyOn(client.diagnostic, 'on'); + return client as Client; +}; + +describe('instrumentQueryAndDeprecationLogger', () => { + let logger: ReturnType; + const client = createFakeClient(); + + beforeEach(() => { + logger = loggingSystemMock.createLogger(); + parseClientOptionsMock.mockReturnValue({}); + ClientMock.mockImplementation(() => createFakeClient()); + }); + + afterEach(() => { + parseClientOptionsMock.mockReset(); + ClientMock.mockReset(); + jest.clearAllMocks(); + }); + + function createResponseWithBody(body?: RequestBody) { + return createApiResponse({ + body: {}, + statusCode: 200, + params: { + method: 'GET', + path: '/foo', + querystring: { hello: 'dolly' }, + body, + }, + }); + } + + it('creates a query logger context based on the `type` parameter', () => { + instrumentEsQueryAndDeprecationLogger({ logger, client, type: 'test123' }); + expect(logger.get).toHaveBeenCalledWith('query', 'test123'); + }); + + describe('logs each query', () => { + it('when request body is an object', () => { + instrumentEsQueryAndDeprecationLogger({ logger, client, type: 'test type' }); + + const response = createResponseWithBody({ + seq_no_primary_term: true, + query: { + term: { user: 'kimchy' }, + }, + }); + + client.diagnostic.emit('response', null, response); + expect(loggingSystemMock.collect(logger).debug).toMatchInlineSnapshot(` + Array [ + Array [ + "200 + GET /foo?hello=dolly + {\\"seq_no_primary_term\\":true,\\"query\\":{\\"term\\":{\\"user\\":\\"kimchy\\"}}}", + undefined, + ], + ] + `); + }); + + it('when request body is a string', () => { + instrumentEsQueryAndDeprecationLogger({ logger, client, type: 'test type' }); + + const response = createResponseWithBody( + JSON.stringify({ + seq_no_primary_term: true, + query: { + term: { user: 'kimchy' }, + }, + }) + ); + + client.diagnostic.emit('response', null, response); + expect(loggingSystemMock.collect(logger).debug).toMatchInlineSnapshot(` + Array [ + Array [ + "200 + GET /foo?hello=dolly + {\\"seq_no_primary_term\\":true,\\"query\\":{\\"term\\":{\\"user\\":\\"kimchy\\"}}}", + undefined, + ], + ] + `); + }); + + it('when request body is a buffer', () => { + instrumentEsQueryAndDeprecationLogger({ logger, client, type: 'test type' }); + + const response = createResponseWithBody( + Buffer.from( + JSON.stringify({ + seq_no_primary_term: true, + query: { + term: { user: 'kimchy' }, + }, + }) + ) + ); + + client.diagnostic.emit('response', null, response); + expect(loggingSystemMock.collect(logger).debug).toMatchInlineSnapshot(` + Array [ + Array [ + "200 + GET /foo?hello=dolly + [buffer]", + undefined, + ], + ] + `); + }); + + it('when request body is a readable stream', () => { + instrumentEsQueryAndDeprecationLogger({ logger, client, type: 'test type' }); + + const response = createResponseWithBody( + Readable.from( + JSON.stringify({ + seq_no_primary_term: true, + query: { + term: { user: 'kimchy' }, + }, + }) + ) + ); + + client.diagnostic.emit('response', null, response); + expect(loggingSystemMock.collect(logger).debug).toMatchInlineSnapshot(` + Array [ + Array [ + "200 + GET /foo?hello=dolly + [stream]", + undefined, + ], + ] + `); + }); + + it('when request body is not defined', () => { + instrumentEsQueryAndDeprecationLogger({ logger, client, type: 'test type' }); + + const response = createResponseWithBody(); + + client.diagnostic.emit('response', null, response); + expect(loggingSystemMock.collect(logger).debug).toMatchInlineSnapshot(` + Array [ + Array [ + "200 + GET /foo?hello=dolly", + undefined, + ], + ] + `); + }); + + it('properly encode queries', () => { + instrumentEsQueryAndDeprecationLogger({ logger, client, type: 'test type' }); + + const response = createApiResponse({ + body: {}, + statusCode: 200, + params: { + method: 'GET', + path: '/foo', + querystring: { city: 'Münich' }, + }, + }); + + client.diagnostic.emit('response', null, response); + + expect(loggingSystemMock.collect(logger).debug).toMatchInlineSnapshot(` + Array [ + Array [ + "200 + GET /foo?city=M%C3%BCnich", + undefined, + ], + ] + `); + }); + + it('logs queries even in case of errors', () => { + instrumentEsQueryAndDeprecationLogger({ logger, client, type: 'test type' }); + + const response = createApiResponse({ + statusCode: 500, + body: { + error: { + type: 'internal server error', + }, + }, + params: { + method: 'GET', + path: '/foo', + querystring: { hello: 'dolly' }, + body: { + seq_no_primary_term: true, + query: { + term: { user: 'kimchy' }, + }, + }, + }, + }); + client.diagnostic.emit('response', new errors.ResponseError(response), response); + + expect(loggingSystemMock.collect(logger).debug).toMatchInlineSnapshot(` + Array [ + Array [ + "500 + GET /foo?hello=dolly + {\\"seq_no_primary_term\\":true,\\"query\\":{\\"term\\":{\\"user\\":\\"kimchy\\"}}} [internal server error]: internal server error", + undefined, + ], + ] + `); + }); + + it('logs debug when the client emits an @elastic/elasticsearch error', () => { + instrumentEsQueryAndDeprecationLogger({ logger, client, type: 'test type' }); + + const response = createApiResponse({ body: {} }); + client.diagnostic.emit('response', new errors.TimeoutError('message', response), response); + + expect(loggingSystemMock.collect(logger).debug).toMatchInlineSnapshot(` + Array [ + Array [ + "[TimeoutError]: message", + undefined, + ], + ] + `); + }); + + it('logs debug when the client emits an ResponseError returned by elasticsearch', () => { + instrumentEsQueryAndDeprecationLogger({ logger, client, type: 'test type' }); + + const response = createApiResponse({ + statusCode: 400, + headers: {}, + params: { + method: 'GET', + path: '/_path', + querystring: { hello: 'dolly' }, + }, + body: { + error: { + type: 'illegal_argument_exception', + reason: 'request [/_path] contains unrecognized parameter: [name]', + }, + }, + }); + client.diagnostic.emit('response', new errors.ResponseError(response), response); + + expect(loggingSystemMock.collect(logger).debug).toMatchInlineSnapshot(` + Array [ + Array [ + "400 + GET /_path?hello=dolly [illegal_argument_exception]: request [/_path] contains unrecognized parameter: [name]", + undefined, + ], + ] + `); + }); + + it('logs default error info when the error response body is empty', () => { + instrumentEsQueryAndDeprecationLogger({ logger, client, type: 'test type' }); + + let response: DiagnosticResult = createApiResponse({ + statusCode: 400, + headers: {}, + params: { + method: 'GET', + path: '/_path', + }, + body: { + error: {}, + }, + }); + client.diagnostic.emit('response', new errors.ResponseError(response), response); + + expect(loggingSystemMock.collect(logger).debug).toMatchInlineSnapshot(` + Array [ + Array [ + "400 + GET /_path [undefined]: {\\"error\\":{}}", + undefined, + ], + ] + `); + + logger.debug.mockClear(); + + response = createApiResponse({ + statusCode: 400, + headers: {}, + params: { + method: 'GET', + path: '/_path', + }, + body: undefined, + }); + client.diagnostic.emit('response', new errors.ResponseError(response), response); + + expect(loggingSystemMock.collect(logger).debug).toMatchInlineSnapshot(` + Array [ + Array [ + "400 + GET /_path [undefined]: Response Error", + undefined, + ], + ] + `); + }); + + it('adds meta information to logs', () => { + instrumentEsQueryAndDeprecationLogger({ logger, client, type: 'test type' }); + + let response = createApiResponse({ + statusCode: 400, + headers: {}, + params: { + method: 'GET', + path: '/_path', + }, + requestOptions: { + opaqueId: 'opaque-id', + }, + body: { + error: {}, + }, + }); + client.diagnostic.emit('response', null, response); + + expect(loggingSystemMock.collect(logger).debug[0][1]).toMatchInlineSnapshot(` + Object { + "http": Object { + "request": Object { + "id": "opaque-id", + }, + }, + } + `); + + logger.debug.mockClear(); + + response = createApiResponse({ + statusCode: 400, + headers: {}, + params: { + method: 'GET', + path: '/_path', + }, + requestOptions: { + opaqueId: 'opaque-id', + }, + body: {} as any, + }); + client.diagnostic.emit('response', new errors.ResponseError(response), response); + + expect(loggingSystemMock.collect(logger).debug[0][1]).toMatchInlineSnapshot(` + Object { + "http": Object { + "request": Object { + "id": "opaque-id", + }, + }, + } + `); + }); + }); + + describe('deprecation warnings from response headers', () => { + it('does not log when no deprecation warning header is returned', () => { + instrumentEsQueryAndDeprecationLogger({ logger, client, type: 'test type' }); + + const response = createApiResponse({ + statusCode: 200, + warnings: null, + params: { + method: 'GET', + path: '/_path', + querystring: { hello: 'dolly' }, + }, + body: { + hits: [ + { + _source: 'may the source be with you', + }, + ], + }, + }); + client.diagnostic.emit('response', new errors.ResponseError(response), response); + + // One debug log entry from 'elasticsearch.query' context + expect(loggingSystemMock.collect(logger).debug.length).toEqual(1); + expect(loggingSystemMock.collect(logger).info).toEqual([]); + }); + + it('does not log when warning header comes from a warn-agent that is not elasticsearch', () => { + instrumentEsQueryAndDeprecationLogger({ logger, client, type: 'test type' }); + + const response = createApiResponse({ + statusCode: 200, + warnings: [ + '299 nginx/2.3.1 "GET /_path is deprecated"', + '299 nginx/2.3.1 "GET hello query param is deprecated"', + ], + params: { + method: 'GET', + path: '/_path', + querystring: { hello: 'dolly' }, + }, + body: { + hits: [ + { + _source: 'may the source be with you', + }, + ], + }, + }); + client.diagnostic.emit('response', new errors.ResponseError(response), response); + + // One debug log entry from 'elasticsearch.query' context + expect(loggingSystemMock.collect(logger).debug.length).toEqual(1); + expect(loggingSystemMock.collect(logger).info).toEqual([]); + }); + + it('logs error when the client receives an Elasticsearch error response for a deprecated request originating from a user', () => { + instrumentEsQueryAndDeprecationLogger({ logger, client, type: 'test type' }); + + const response = createApiResponse({ + statusCode: 400, + warnings: ['299 Elasticsearch-8.1.0 "GET /_path is deprecated"'], + params: { + method: 'GET', + path: '/_path', + querystring: { hello: 'dolly' }, + }, + body: { + error: { + type: 'illegal_argument_exception', + reason: 'request [/_path] contains unrecognized parameter: [name]', + }, + }, + }); + client.diagnostic.emit('response', new errors.ResponseError(response), response); + + expect(loggingSystemMock.collect(logger).info).toEqual([]); + // Test debug[1] since theree is one log entry from 'elasticsearch.query' context + expect(loggingSystemMock.collect(logger).debug[1][0]).toMatch( + 'Elasticsearch deprecation: 299 Elasticsearch-8.1.0 "GET /_path is deprecated"' + ); + expect(loggingSystemMock.collect(logger).debug[1][0]).toMatch('Origin:user'); + expect(loggingSystemMock.collect(logger).debug[1][0]).toMatch(/Stack trace:\n.*at/); + expect(loggingSystemMock.collect(logger).debug[1][0]).toMatch( + /Query:\n.*400\n.*GET \/_path\?hello\=dolly \[illegal_argument_exception\]: request \[\/_path\] contains unrecognized parameter: \[name\]/ + ); + }); + + it('logs warning when the client receives an Elasticsearch error response for a deprecated request originating from kibana', () => { + instrumentEsQueryAndDeprecationLogger({ logger, client, type: 'test type' }); + + const response = createApiResponse({ + statusCode: 400, + warnings: ['299 Elasticsearch-8.1.0 "GET /_path is deprecated"'], + params: { + method: 'GET', + path: '/_path', + querystring: { hello: 'dolly' }, + // Set the request header to indicate to Elasticsearch that this is a request over which users have no control + headers: { 'x-elastic-product-origin': 'kibana' }, + }, + body: { + error: { + type: 'illegal_argument_exception', + reason: 'request [/_path] contains unrecognized parameter: [name]', + }, + }, + }); + client.diagnostic.emit('response', new errors.ResponseError(response), response); + + // One debug log entry from 'elasticsearch.query' context + expect(loggingSystemMock.collect(logger).debug.length).toEqual(1); + expect(loggingSystemMock.collect(logger).info[0][0]).toMatch( + 'Elasticsearch deprecation: 299 Elasticsearch-8.1.0 "GET /_path is deprecated"' + ); + expect(loggingSystemMock.collect(logger).info[0][0]).toMatch('Origin:kibana'); + expect(loggingSystemMock.collect(logger).info[0][0]).toMatch(/Stack trace:\n.*at/); + expect(loggingSystemMock.collect(logger).info[0][0]).toMatch( + /Query:\n.*400\n.*GET \/_path\?hello\=dolly \[illegal_argument_exception\]: request \[\/_path\] contains unrecognized parameter: \[name\]/ + ); + }); + + it('logs error when the client receives an Elasticsearch success response for a deprecated request originating from a user', () => { + instrumentEsQueryAndDeprecationLogger({ logger, client, type: 'test type' }); + + const response = createApiResponse({ + statusCode: 200, + warnings: ['299 Elasticsearch-8.1.0 "GET /_path is deprecated"'], + params: { + method: 'GET', + path: '/_path', + querystring: { hello: 'dolly' }, + }, + body: { + hits: [ + { + _source: 'may the source be with you', + }, + ], + }, + }); + client.diagnostic.emit('response', null, response); + + expect(loggingSystemMock.collect(logger).info).toEqual([]); + // Test debug[1] since theree is one log entry from 'elasticsearch.query' context + expect(loggingSystemMock.collect(logger).debug[1][0]).toMatch( + 'Elasticsearch deprecation: 299 Elasticsearch-8.1.0 "GET /_path is deprecated"' + ); + expect(loggingSystemMock.collect(logger).debug[1][0]).toMatch('Origin:user'); + expect(loggingSystemMock.collect(logger).debug[1][0]).toMatch(/Stack trace:\n.*at/); + expect(loggingSystemMock.collect(logger).debug[1][0]).toMatch( + /Query:\n.*200\n.*GET \/_path\?hello\=dolly/ + ); + }); + + it('logs warning when the client receives an Elasticsearch success response for a deprecated request originating from kibana', () => { + instrumentEsQueryAndDeprecationLogger({ logger, client, type: 'test type' }); + + const response = createApiResponse({ + statusCode: 200, + warnings: ['299 Elasticsearch-8.1.0 "GET /_path is deprecated"'], + params: { + method: 'GET', + path: '/_path', + querystring: { hello: 'dolly' }, + // Set the request header to indicate to Elasticsearch that this is a request over which users have no control + headers: { 'x-elastic-product-origin': 'kibana' }, + }, + body: { + hits: [ + { + _source: 'may the source be with you', + }, + ], + }, + }); + client.diagnostic.emit('response', null, response); + + // One debug log entry from 'elasticsearch.query' context + expect(loggingSystemMock.collect(logger).debug.length).toEqual(1); + expect(loggingSystemMock.collect(logger).info[0][0]).toMatch( + 'Elasticsearch deprecation: 299 Elasticsearch-8.1.0 "GET /_path is deprecated"' + ); + expect(loggingSystemMock.collect(logger).info[0][0]).toMatch('Origin:kibana'); + expect(loggingSystemMock.collect(logger).info[0][0]).toMatch(/Stack trace:\n.*at/); + expect(loggingSystemMock.collect(logger).info[0][0]).toMatch( + /Query:\n.*200\n.*GET \/_path\?hello\=dolly/ + ); + }); + }); +}); diff --git a/src/core/server/elasticsearch/client/log_query_and_deprecation.ts b/src/core/server/elasticsearch/client/log_query_and_deprecation.ts new file mode 100644 index 0000000000000..fc5a0fa6e1111 --- /dev/null +++ b/src/core/server/elasticsearch/client/log_query_and_deprecation.ts @@ -0,0 +1,143 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { Buffer } from 'buffer'; +import { stringify } from 'querystring'; +import { errors, DiagnosticResult, RequestBody, Client } from '@elastic/elasticsearch'; +import type { ElasticsearchErrorDetails } from './types'; +import { Logger } from '../../logging'; + +const convertQueryString = (qs: string | Record | undefined): string => { + if (qs === undefined || typeof qs === 'string') { + return qs ?? ''; + } + return stringify(qs); +}; + +function ensureString(body: RequestBody): string { + if (typeof body === 'string') return body; + if (Buffer.isBuffer(body)) return '[buffer]'; + if ('readable' in body && body.readable && typeof body._read === 'function') return '[stream]'; + return JSON.stringify(body); +} + +/** + * Returns a debug message from an Elasticsearch error in the following format: + * [error type] error reason + */ +export function getErrorMessage(error: errors.ElasticsearchClientError): string { + if (error instanceof errors.ResponseError) { + const errorBody = error.meta.body as ElasticsearchErrorDetails; + return `[${errorBody?.error?.type}]: ${errorBody?.error?.reason ?? error.message}`; + } + return `[${error.name}]: ${error.message}`; +} + +/** + * returns a string in format: + * + * status code + * method URL + * request body + * + * so it could be copy-pasted into the Dev console + */ +function getResponseMessage(event: DiagnosticResult): string { + const errorMeta = getRequestDebugMeta(event); + const body = errorMeta.body ? `\n${errorMeta.body}` : ''; + return `${errorMeta.statusCode}\n${errorMeta.method} ${errorMeta.url}${body}`; +} + +/** + * Returns stringified debug information from an Elasticsearch request event + * useful for logging in case of an unexpected failure. + */ +export function getRequestDebugMeta(event: DiagnosticResult): { + url: string; + body: string; + statusCode: number | null; + method: string; +} { + const params = event.meta.request.params; + // definition is wrong, `params.querystring` can be either a string or an object + const querystring = convertQueryString(params.querystring); + return { + url: `${params.path}${querystring ? `?${querystring}` : ''}`, + body: params.body ? `${ensureString(params.body)}` : '', + method: params.method, + statusCode: event.statusCode!, + }; +} + +/** HTTP Warning headers have the following syntax: + * (where warn-code is a three digit number) + * This function tests if a warning comes from an Elasticsearch warn-agent + * */ +const isEsWarning = (warning: string) => /\d\d\d Elasticsearch-/.test(warning); + +export const instrumentEsQueryAndDeprecationLogger = ({ + logger, + client, + type, +}: { + logger: Logger; + client: Client; + type: string; +}) => { + const queryLogger = logger.get('query', type); + const deprecationLogger = logger.get('deprecation'); + client.diagnostic.on('response', (error, event) => { + if (event) { + const opaqueId = event.meta.request.options.opaqueId; + const meta = opaqueId + ? { + http: { request: { id: event.meta.request.options.opaqueId } }, + } + : undefined; // do not clutter logs if opaqueId is not present + let queryMsg = ''; + if (error) { + if (error instanceof errors.ResponseError) { + queryMsg = `${getResponseMessage(event)} ${getErrorMessage(error)}`; + } else { + queryMsg = getErrorMessage(error); + } + } else { + queryMsg = getResponseMessage(event); + } + + queryLogger.debug(queryMsg, meta); + + if (event.warnings && event.warnings.filter(isEsWarning).length > 0) { + // Plugins can explicitly mark requests as originating from a user by + // removing the `'x-elastic-product-origin': 'kibana'` header that's + // added by default. User requests will be shown to users in the + // upgrade assistant UI as an action item that has to be addressed + // before they upgrade. + // Kibana requests will be hidden from the upgrade assistant UI and are + // only logged to help developers maintain their plugins + const requestOrigin = + (event.meta.request.params.headers != null && + (event.meta.request.params.headers[ + 'x-elastic-product-origin' + ] as unknown as string)) === 'kibana' + ? 'kibana' + : 'user'; + + // Strip the first 5 stack trace lines as these are irrelavent to finding the call site + const stackTrace = new Error().stack?.split('\n').slice(5).join('\n'); + + const deprecationMsg = `Elasticsearch deprecation: ${event.warnings}\nOrigin:${requestOrigin}\nStack trace:\n${stackTrace}\nQuery:\n${queryMsg}`; + if (requestOrigin === 'kibana') { + deprecationLogger.info(deprecationMsg); + } else { + deprecationLogger.debug(deprecationMsg); + } + } + } + }); +}; diff --git a/src/core/server/elasticsearch/elasticsearch_service.test.ts b/src/core/server/elasticsearch/elasticsearch_service.test.ts index 3b75d19b80a10..ce5672ad30519 100644 --- a/src/core/server/elasticsearch/elasticsearch_service.test.ts +++ b/src/core/server/elasticsearch/elasticsearch_service.test.ts @@ -21,7 +21,7 @@ import { MockClusterClient, isScriptingEnabledMock } from './elasticsearch_servi import type { NodesVersionCompatibility } from './version_check/ensure_es_version'; import { BehaviorSubject } from 'rxjs'; import { first } from 'rxjs/operators'; -import { REPO_ROOT } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import { Env } from '../config'; import { configServiceMock, getEnvOptions } from '../config/mocks'; import { CoreContext } from '../core_context'; diff --git a/src/core/server/http/cookie_session_storage.test.ts b/src/core/server/http/cookie_session_storage.test.ts index ad05d37c81e99..8e2cd58733faf 100644 --- a/src/core/server/http/cookie_session_storage.test.ts +++ b/src/core/server/http/cookie_session_storage.test.ts @@ -8,7 +8,7 @@ import { parse as parseCookie } from 'tough-cookie'; import supertest from 'supertest'; -import { REPO_ROOT } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import { ByteSizeValue } from '@kbn/config-schema'; import { BehaviorSubject } from 'rxjs'; diff --git a/src/core/server/http/http_service.test.ts b/src/core/server/http/http_service.test.ts index 4955d19668580..3a387cdfd5e35 100644 --- a/src/core/server/http/http_service.test.ts +++ b/src/core/server/http/http_service.test.ts @@ -10,7 +10,7 @@ import { mockHttpServer } from './http_service.test.mocks'; import { noop } from 'lodash'; import { BehaviorSubject } from 'rxjs'; -import { REPO_ROOT } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import { getEnvOptions } from '../config/mocks'; import { HttpService } from '.'; import { HttpConfigType, config } from './http_config'; diff --git a/src/core/server/http/test_utils.ts b/src/core/server/http/test_utils.ts index 4e1a88e967f8f..8a8c545b365b3 100644 --- a/src/core/server/http/test_utils.ts +++ b/src/core/server/http/test_utils.ts @@ -8,7 +8,7 @@ import { BehaviorSubject } from 'rxjs'; import moment from 'moment'; -import { REPO_ROOT } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import { ByteSizeValue } from '@kbn/config-schema'; import { Env } from '../config'; import { HttpService } from './http_service'; diff --git a/src/core/server/metrics/logging/get_ops_metrics_log.test.ts b/src/core/server/metrics/logging/get_ops_metrics_log.test.ts index cba188c94c74e..3fd3c4a7a24d6 100644 --- a/src/core/server/metrics/logging/get_ops_metrics_log.test.ts +++ b/src/core/server/metrics/logging/get_ops_metrics_log.test.ts @@ -42,6 +42,7 @@ const testMetrics = { memory: { heap: { used_in_bytes: 100 } }, uptime_in_millis: 1500, event_loop_delay: 50, + event_loop_delay_histogram: { percentiles: { '50': 50, '75': 75, '95': 95, '99': 99 } }, }, os: { load: { @@ -56,7 +57,7 @@ describe('getEcsOpsMetricsLog', () => { it('provides correctly formatted message', () => { const result = getEcsOpsMetricsLog(createMockOpsMetrics(testMetrics)); expect(result.message).toMatchInlineSnapshot( - `"memory: 100.0B uptime: 0:00:01 load: [10.00,20.00,30.00] delay: 50.000"` + `"memory: 100.0B uptime: 0:00:01 load: [10.00,20.00,30.00] mean delay: 50.000 delay histogram: { 50: 50.000; 95: 95.000; 99: 99.000 }"` ); }); @@ -70,6 +71,7 @@ describe('getEcsOpsMetricsLog', () => { const missingMetrics = { ...baseMetrics, process: {}, + processes: [], os: {}, } as unknown as OpsMetrics; const logMeta = getEcsOpsMetricsLog(missingMetrics); @@ -77,39 +79,41 @@ describe('getEcsOpsMetricsLog', () => { }); it('provides an ECS-compatible response', () => { - const logMeta = getEcsOpsMetricsLog(createBaseOpsMetrics()); - expect(logMeta).toMatchInlineSnapshot(` + const logMeta = getEcsOpsMetricsLog(createMockOpsMetrics(testMetrics)); + expect(logMeta.meta).toMatchInlineSnapshot(` Object { - "message": "memory: 1.0B load: [1.00,1.00,1.00] delay: 1.000", - "meta": Object { - "event": Object { - "category": Array [ - "process", - "host", - ], - "kind": "metric", - "type": Array [ - "info", - ], - }, - "host": Object { - "os": Object { - "load": Object { - "15m": 1, - "1m": 1, - "5m": 1, - }, + "event": Object { + "category": Array [ + "process", + "host", + ], + "kind": "metric", + "type": Array [ + "info", + ], + }, + "host": Object { + "os": Object { + "load": Object { + "15m": 30, + "1m": 10, + "5m": 20, }, }, - "process": Object { - "eventLoopDelay": 1, - "memory": Object { - "heap": Object { - "usedInBytes": 1, - }, + }, + "process": Object { + "eventLoopDelay": 50, + "eventLoopDelayHistogram": Object { + "50": 50, + "95": 95, + "99": 99, + }, + "memory": Object { + "heap": Object { + "usedInBytes": 100, }, - "uptime": 0, }, + "uptime": 1, }, } `); diff --git a/src/core/server/metrics/logging/get_ops_metrics_log.ts b/src/core/server/metrics/logging/get_ops_metrics_log.ts index 7e13f35889ec7..6211407ae86f0 100644 --- a/src/core/server/metrics/logging/get_ops_metrics_log.ts +++ b/src/core/server/metrics/logging/get_ops_metrics_log.ts @@ -30,10 +30,29 @@ export function getEcsOpsMetricsLog(metrics: OpsMetrics) { // HH:mm:ss message format for backward compatibility const uptimeValMsg = uptimeVal ? `uptime: ${numeral(uptimeVal).format('00:00:00')} ` : ''; - // Event loop delay is in ms + // Event loop delay metrics are in ms const eventLoopDelayVal = process?.event_loop_delay; const eventLoopDelayValMsg = eventLoopDelayVal - ? `delay: ${numeral(process?.event_loop_delay).format('0.000')}` + ? `mean delay: ${numeral(process?.event_loop_delay).format('0.000')}` + : ''; + + const eventLoopDelayPercentiles = process?.event_loop_delay_histogram?.percentiles; + + // Extract 50th, 95th and 99th percentiles for log meta + const eventLoopDelayHistVals = eventLoopDelayPercentiles + ? { + 50: eventLoopDelayPercentiles[50], + 95: eventLoopDelayPercentiles[95], + 99: eventLoopDelayPercentiles[99], + } + : undefined; + // Format message from 50th, 95th and 99th percentiles + const eventLoopDelayHistMsg = eventLoopDelayPercentiles + ? ` delay histogram: { 50: ${numeral(eventLoopDelayPercentiles['50']).format( + '0.000' + )}; 95: ${numeral(eventLoopDelayPercentiles['95']).format('0.000')}; 99: ${numeral( + eventLoopDelayPercentiles['99'] + ).format('0.000')} }` : ''; const loadEntries = { @@ -65,6 +84,7 @@ export function getEcsOpsMetricsLog(metrics: OpsMetrics) { }, }, eventLoopDelay: eventLoopDelayVal, + eventLoopDelayHistogram: eventLoopDelayHistVals, }, host: { os: { @@ -75,7 +95,13 @@ export function getEcsOpsMetricsLog(metrics: OpsMetrics) { }; return { - message: `${processMemoryUsedInBytesMsg}${uptimeValMsg}${loadValsMsg}${eventLoopDelayValMsg}`, + message: [ + processMemoryUsedInBytesMsg, + uptimeValMsg, + loadValsMsg, + eventLoopDelayValMsg, + eventLoopDelayHistMsg, + ].join(''), meta, }; } diff --git a/src/core/server/metrics/metrics_service.test.ts b/src/core/server/metrics/metrics_service.test.ts index d7de41fd7ccf7..27043b8fa2c8a 100644 --- a/src/core/server/metrics/metrics_service.test.ts +++ b/src/core/server/metrics/metrics_service.test.ts @@ -203,6 +203,7 @@ describe('MetricsService', () => { }, "process": Object { "eventLoopDelay": undefined, + "eventLoopDelayHistogram": undefined, "memory": Object { "heap": Object { "usedInBytes": undefined, diff --git a/src/core/server/plugins/discovery/plugins_discovery.test.ts b/src/core/server/plugins/discovery/plugins_discovery.test.ts index 958e051d0476d..a6ffdff4422be 100644 --- a/src/core/server/plugins/discovery/plugins_discovery.test.ts +++ b/src/core/server/plugins/discovery/plugins_discovery.test.ts @@ -7,7 +7,7 @@ */ // must be before mocks imports to avoid conflicting with `REPO_ROOT` accessor. -import { REPO_ROOT } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import { mockPackage, scanPluginSearchPathsMock } from './plugins_discovery.test.mocks'; import mockFs from 'mock-fs'; import { loggingSystemMock } from '../../logging/logging_system.mock'; diff --git a/src/core/server/plugins/integration_tests/plugins_service.test.ts b/src/core/server/plugins/integration_tests/plugins_service.test.ts index 4170d9422f277..ebbb3fa473b6d 100644 --- a/src/core/server/plugins/integration_tests/plugins_service.test.ts +++ b/src/core/server/plugins/integration_tests/plugins_service.test.ts @@ -7,7 +7,7 @@ */ // must be before mocks imports to avoid conflicting with `REPO_ROOT` accessor. -import { REPO_ROOT } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import { mockPackage, mockDiscover } from './plugins_service.test.mocks'; import { join } from 'path'; diff --git a/src/core/server/plugins/plugin.test.ts b/src/core/server/plugins/plugin.test.ts index 513e893992005..92cbda2a69cfe 100644 --- a/src/core/server/plugins/plugin.test.ts +++ b/src/core/server/plugins/plugin.test.ts @@ -8,7 +8,7 @@ import { join } from 'path'; import { BehaviorSubject } from 'rxjs'; -import { REPO_ROOT } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import { schema } from '@kbn/config-schema'; import { Env } from '../config'; diff --git a/src/core/server/plugins/plugin_context.test.ts b/src/core/server/plugins/plugin_context.test.ts index 867d4d978314b..7bcf392ed510b 100644 --- a/src/core/server/plugins/plugin_context.test.ts +++ b/src/core/server/plugins/plugin_context.test.ts @@ -8,7 +8,7 @@ import { duration } from 'moment'; import { first } from 'rxjs/operators'; -import { REPO_ROOT } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import { fromRoot } from '@kbn/utils'; import { createPluginInitializerContext, diff --git a/src/core/server/plugins/plugins_config.test.ts b/src/core/server/plugins/plugins_config.test.ts index d65b057fb65c0..b9225054e63ef 100644 --- a/src/core/server/plugins/plugins_config.test.ts +++ b/src/core/server/plugins/plugins_config.test.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -import { REPO_ROOT } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import { getEnvOptions } from '../config/mocks'; import { PluginsConfig, PluginsConfigType } from './plugins_config'; import { Env } from '../config'; diff --git a/src/core/server/plugins/plugins_service.test.ts b/src/core/server/plugins/plugins_service.test.ts index 0c077d732c67b..5a05817d2111f 100644 --- a/src/core/server/plugins/plugins_service.test.ts +++ b/src/core/server/plugins/plugins_service.test.ts @@ -11,7 +11,8 @@ import { mockDiscover, mockPackage } from './plugins_service.test.mocks'; import { resolve, join } from 'path'; import { BehaviorSubject, from } from 'rxjs'; import { schema } from '@kbn/config-schema'; -import { createAbsolutePathSerializer, REPO_ROOT } from '@kbn/dev-utils'; +import { createAbsolutePathSerializer } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import { ConfigPath, ConfigService, Env } from '../config'; import { rawConfigServiceMock, getEnvOptions } from '../config/mocks'; diff --git a/src/core/server/plugins/plugins_system.test.ts b/src/core/server/plugins/plugins_system.test.ts index 4cd8e4c551bea..3d8a47005b362 100644 --- a/src/core/server/plugins/plugins_system.test.ts +++ b/src/core/server/plugins/plugins_system.test.ts @@ -14,7 +14,7 @@ import { import { BehaviorSubject } from 'rxjs'; -import { REPO_ROOT } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import { Env } from '../config'; import { configServiceMock, getEnvOptions } from '../config/mocks'; import { CoreContext } from '../core_context'; diff --git a/src/core/server/preboot/preboot_service.test.ts b/src/core/server/preboot/preboot_service.test.ts index dd4b1cb7d1df0..77242f0c5765f 100644 --- a/src/core/server/preboot/preboot_service.test.ts +++ b/src/core/server/preboot/preboot_service.test.ts @@ -7,7 +7,7 @@ */ import { nextTick } from '@kbn/test/jest'; -import { REPO_ROOT } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import { LoggerFactory } from '@kbn/logging'; import { Env } from '@kbn/config'; import { getEnvOptions } from '../config/mocks'; diff --git a/src/core/server/root/index.test.ts b/src/core/server/root/index.test.ts index 7eba051a128f0..6ea3e05b9c2c2 100644 --- a/src/core/server/root/index.test.ts +++ b/src/core/server/root/index.test.ts @@ -10,7 +10,7 @@ import { rawConfigService, configService, logger, mockServer } from './index.tes import { BehaviorSubject } from 'rxjs'; import { filter, first } from 'rxjs/operators'; -import { REPO_ROOT } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import { getEnvOptions } from '../config/mocks'; import { Root } from '.'; import { Env } from '../config'; diff --git a/src/core/server/saved_objects/migrations/integration_tests/7.7.2_xpack_100k.test.ts b/src/core/server/saved_objects/migrations/integration_tests/7.7.2_xpack_100k.test.ts index c22c6154c2605..139cd298d28ed 100644 --- a/src/core/server/saved_objects/migrations/integration_tests/7.7.2_xpack_100k.test.ts +++ b/src/core/server/saved_objects/migrations/integration_tests/7.7.2_xpack_100k.test.ts @@ -8,7 +8,7 @@ import path from 'path'; import { unlink } from 'fs/promises'; -import { REPO_ROOT } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import { Env } from '@kbn/config'; import { getEnvOptions } from '../../../config/mocks'; import * as kbnTestServer from '../../../../test_helpers/kbn_server'; diff --git a/src/core/server/saved_objects/migrations/integration_tests/7_13_0_failed_action_tasks.test.ts b/src/core/server/saved_objects/migrations/integration_tests/7_13_0_failed_action_tasks.test.ts index 2def8e375c81f..479b1e78e1b72 100644 --- a/src/core/server/saved_objects/migrations/integration_tests/7_13_0_failed_action_tasks.test.ts +++ b/src/core/server/saved_objects/migrations/integration_tests/7_13_0_failed_action_tasks.test.ts @@ -19,8 +19,7 @@ async function removeLogFile() { await fs.unlink(logFilePath).catch(() => void 0); } -// FLAKY: https://github.com/elastic/kibana/issues/118626 -describe.skip('migration from 7.13 to 7.14+ with many failed action_tasks', () => { +describe('migration from 7.13 to 7.14+ with many failed action_tasks', () => { let esServer: kbnTestServer.TestElasticsearchUtils; let root: Root; let startES: () => Promise; diff --git a/src/core/server/saved_objects/migrations/integration_tests/migration_from_older_v1.test.ts b/src/core/server/saved_objects/migrations/integration_tests/migration_from_older_v1.test.ts index 0ed9262017263..c341463b78910 100644 --- a/src/core/server/saved_objects/migrations/integration_tests/migration_from_older_v1.test.ts +++ b/src/core/server/saved_objects/migrations/integration_tests/migration_from_older_v1.test.ts @@ -10,7 +10,7 @@ import Path from 'path'; import Fs from 'fs'; import Util from 'util'; import Semver from 'semver'; -import { REPO_ROOT } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import { Env } from '@kbn/config'; import { getEnvOptions } from '../../../config/mocks'; import * as kbnTestServer from '../../../../test_helpers/kbn_server'; diff --git a/src/core/server/saved_objects/migrations/integration_tests/migration_from_same_v1.test.ts b/src/core/server/saved_objects/migrations/integration_tests/migration_from_same_v1.test.ts index 15d985daccba6..34d1317755c14 100644 --- a/src/core/server/saved_objects/migrations/integration_tests/migration_from_same_v1.test.ts +++ b/src/core/server/saved_objects/migrations/integration_tests/migration_from_same_v1.test.ts @@ -10,7 +10,7 @@ import Path from 'path'; import Fs from 'fs'; import Util from 'util'; import Semver from 'semver'; -import { REPO_ROOT } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import { Env } from '@kbn/config'; import { getEnvOptions } from '../../../config/mocks'; import * as kbnTestServer from '../../../../test_helpers/kbn_server'; diff --git a/src/core/server/saved_objects/migrations/integration_tests/type_registrations.test.ts b/src/core/server/saved_objects/migrations/integration_tests/type_registrations.test.ts index 7597657e7706c..4ff66151db925 100644 --- a/src/core/server/saved_objects/migrations/integration_tests/type_registrations.test.ts +++ b/src/core/server/saved_objects/migrations/integration_tests/type_registrations.test.ts @@ -34,6 +34,7 @@ const previouslyRegisteredTypes = [ 'cases-sub-case', 'cases-user-actions', 'config', + 'connector_token', 'core-usage-stats', 'dashboard', 'endpoint:user-artifact', diff --git a/src/core/server/saved_objects/saved_objects_service.test.ts b/src/core/server/saved_objects/saved_objects_service.test.ts index a4f6c019c9624..a8bda95af46f9 100644 --- a/src/core/server/saved_objects/saved_objects_service.test.ts +++ b/src/core/server/saved_objects/saved_objects_service.test.ts @@ -19,7 +19,7 @@ import { import { BehaviorSubject } from 'rxjs'; import { RawPackageInfo } from '@kbn/config'; import { ByteSizeValue } from '@kbn/config-schema'; -import { REPO_ROOT } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import { SavedObjectsService } from './saved_objects_service'; import { mockCoreContext } from '../core_context.mock'; diff --git a/src/core/server/saved_objects/service/lib/repository.test.ts b/src/core/server/saved_objects/service/lib/repository.test.ts index ab692b146e7f6..ebab5898a0eb9 100644 --- a/src/core/server/saved_objects/service/lib/repository.test.ts +++ b/src/core/server/saved_objects/service/lib/repository.test.ts @@ -2272,7 +2272,16 @@ describe('SavedObjectsRepository', () => { it(`self-generates an id if none is provided`, async () => { await createSuccess(type, attributes); - expect(client.create).toHaveBeenCalledWith( + expect(client.create).toHaveBeenNthCalledWith( + 1, + expect.objectContaining({ + id: expect.objectContaining(/index-pattern:[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}/), + }), + expect.anything() + ); + await createSuccess(type, attributes, { id: '' }); + expect(client.create).toHaveBeenNthCalledWith( + 2, expect.objectContaining({ id: expect.objectContaining(/index-pattern:[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}/), }), @@ -3558,6 +3567,20 @@ describe('SavedObjectsRepository', () => { }); }); + it('search for the right fields when typeToNamespacesMap is set', async () => { + const relevantOpts = { + ...commonOptions, + fields: ['title'], + type: '', + namespaces: [], + typeToNamespacesMap: new Map([[type, [namespace]]]), + }; + + await findSuccess(relevantOpts, namespace); + const esOptions = client.search.mock.calls[0][0]; + expect(esOptions?._source ?? []).toContain('index-pattern.title'); + }); + it(`accepts hasReferenceOperator`, async () => { const relevantOpts: SavedObjectsFindOptions = { ...commonOptions, @@ -4147,6 +4170,13 @@ describe('SavedObjectsRepository', () => { await test({}); }); + it(`throws when id is empty`, async () => { + await expect( + savedObjectsRepository.incrementCounter(type, '', counterFields) + ).rejects.toThrowError(createBadRequestError('id cannot be empty')); + expect(client.update).not.toHaveBeenCalled(); + }); + it(`throws when counterField is not CounterField type`, async () => { const test = async (field: unknown[]) => { await expect( @@ -4673,6 +4703,13 @@ describe('SavedObjectsRepository', () => { expect(client.update).not.toHaveBeenCalled(); }); + it(`throws when id is empty`, async () => { + await expect(savedObjectsRepository.update(type, '', attributes)).rejects.toThrowError( + createBadRequestError('id cannot be empty') + ); + expect(client.update).not.toHaveBeenCalled(); + }); + it(`throws when ES is unable to find the document during get`, async () => { client.get.mockResolvedValueOnce( elasticsearchClientMock.createSuccessTransportRequestPromise( diff --git a/src/core/server/saved_objects/service/lib/repository.ts b/src/core/server/saved_objects/service/lib/repository.ts index 0d17525016043..9af85499295b5 100644 --- a/src/core/server/saved_objects/service/lib/repository.ts +++ b/src/core/server/saved_objects/service/lib/repository.ts @@ -303,7 +303,6 @@ export class SavedObjectsRepository { options: SavedObjectsCreateOptions = {} ): Promise> { const { - id = SavedObjectsUtils.generateId(), migrationVersion, coreMigrationVersion, overwrite = false, @@ -313,6 +312,7 @@ export class SavedObjectsRepository { initialNamespaces, version, } = options; + const id = options.id || SavedObjectsUtils.generateId(); const namespace = normalizeNamespace(options.namespace); this.validateInitialNamespaces(type, initialNamespaces); @@ -930,7 +930,7 @@ export class SavedObjectsRepository { index: pit ? undefined : this.getIndicesForTypes(allowedTypes), // If `searchAfter` is provided, we drop `from` as it will not be used for pagination. from: searchAfter ? undefined : perPage * (page - 1), - _source: includedFields(type, fields), + _source: includedFields(allowedTypes, fields), preference, rest_total_hits_as_int: true, size: perPage, @@ -938,7 +938,7 @@ export class SavedObjectsRepository { size: perPage, seq_no_primary_term: true, from: perPage * (page - 1), - _source: includedFields(type, fields), + _source: includedFields(allowedTypes, fields), ...(aggsObject ? { aggs: aggsObject } : {}), ...getSearchDsl(this._mappings, this._registry, { search, @@ -1231,6 +1231,9 @@ export class SavedObjectsRepository { if (!this._allowedTypes.includes(type)) { throw SavedObjectsErrorHelpers.createGenericNotFoundError(type, id); } + if (!id) { + throw SavedObjectsErrorHelpers.createBadRequestError('id cannot be empty'); // prevent potentially upserting a saved object with an empty ID + } const { version, references, upsert, refresh = DEFAULT_REFRESH_SETTING } = options; const namespace = normalizeNamespace(options.namespace); @@ -1754,6 +1757,10 @@ export class SavedObjectsRepository { upsertAttributes, } = options; + if (!id) { + throw SavedObjectsErrorHelpers.createBadRequestError('id cannot be empty'); // prevent potentially upserting a saved object with an empty ID + } + const normalizedCounterFields = counterFields.map((counterField) => { /** * no counterField configs provided, instead a field name string was passed. diff --git a/src/core/server/server.test.ts b/src/core/server/server.test.ts index 112693aae0279..48547883d5f67 100644 --- a/src/core/server/server.test.ts +++ b/src/core/server/server.test.ts @@ -26,7 +26,7 @@ import { } from './server.test.mocks'; import { BehaviorSubject } from 'rxjs'; -import { REPO_ROOT } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import { rawConfigServiceMock, getEnvOptions } from './config/mocks'; import { Env } from './config'; import { Server } from './server'; diff --git a/src/core/server/ui_settings/integration_tests/index.test.ts b/src/core/server/ui_settings/integration_tests/index.test.ts index ef635e90dac70..3f85beb2acec6 100644 --- a/src/core/server/ui_settings/integration_tests/index.test.ts +++ b/src/core/server/ui_settings/integration_tests/index.test.ts @@ -7,7 +7,7 @@ */ import { Env } from '@kbn/config'; -import { REPO_ROOT } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import { getEnvOptions } from '../../config/mocks'; import { startServers, stopServers } from './lib'; import { docExistsSuite } from './doc_exists'; diff --git a/src/core/test_helpers/kbn_server.ts b/src/core/test_helpers/kbn_server.ts index 58720be637e2f..c326c7a35df63 100644 --- a/src/core/test_helpers/kbn_server.ts +++ b/src/core/test_helpers/kbn_server.ts @@ -6,7 +6,8 @@ * Side Public License, v 1. */ -import { ToolingLog, REPO_ROOT } from '@kbn/dev-utils'; +import { ToolingLog } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import { createTestEsCluster, CreateTestEsClusterOptions, diff --git a/src/core/types/elasticsearch/search.ts b/src/core/types/elasticsearch/search.ts index c28bf3c258f77..ac93a45da3258 100644 --- a/src/core/types/elasticsearch/search.ts +++ b/src/core/types/elasticsearch/search.ts @@ -9,6 +9,11 @@ import { ValuesType, UnionToIntersection } from 'utility-types'; import * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; +interface AggregationsAggregationContainer extends Record { + aggs?: any; + aggregations?: any; +} + type InvalidAggregationRequest = unknown; // ensures aggregations work with requests where aggregation options are a union type, @@ -31,7 +36,7 @@ type KeysOfSources = T extends [any] ? KeyOfSource & KeyOfSource & KeyOfSource & KeyOfSource : Record; -type CompositeKeysOf = +type CompositeKeysOf = TAggregationContainer extends { composite: { sources: [...infer TSource] }; } @@ -40,7 +45,7 @@ type CompositeKeysOf = +type TopMetricKeysOf = TAggregationContainer extends { top_metrics: { metrics: { field: infer TField } } } ? TField : TAggregationContainer extends { top_metrics: { metrics: Array<{ field: infer TField }> } } @@ -92,17 +97,9 @@ type HitsOf< > >; -type AggregationTypeName = Exclude< - keyof estypes.AggregationsAggregationContainer, - 'aggs' | 'aggregations' ->; +type AggregationMap = Partial>; -type AggregationMap = Partial>; - -type TopLevelAggregationRequest = Pick< - estypes.AggregationsAggregationContainer, - 'aggs' | 'aggregations' ->; +type TopLevelAggregationRequest = Pick; type MaybeKeyed< TAggregationContainer, @@ -113,448 +110,460 @@ type MaybeKeyed< : { buckets: TBucket[] }; export type AggregateOf< - TAggregationContainer extends estypes.AggregationsAggregationContainer, + TAggregationContainer extends AggregationsAggregationContainer, TDocument -> = (Record & { - adjacency_matrix: { - buckets: Array< - { - key: string; - doc_count: number; - } & SubAggregateOf - >; - }; - auto_date_histogram: { - interval: string; - buckets: Array< - { - key: number; - key_as_string: string; - doc_count: number; - } & SubAggregateOf - >; - }; - avg: { - value: number | null; - value_as_string?: string; - }; - avg_bucket: { - value: number | null; - }; - boxplot: { - min: number | null; - max: number | null; - q1: number | null; - q2: number | null; - q3: number | null; - }; - bucket_script: { - value: unknown; - }; - cardinality: { - value: number; - }; - children: { - doc_count: number; - } & SubAggregateOf; - composite: { - after_key: CompositeKeysOf; - buckets: Array< - { +> = ValuesType< + Pick< + Record & { + adjacency_matrix: { + buckets: Array< + { + key: string; + doc_count: number; + } & SubAggregateOf + >; + }; + auto_date_histogram: { + interval: string; + buckets: Array< + { + key: number; + key_as_string: string; + doc_count: number; + } & SubAggregateOf + >; + }; + avg: { + value: number | null; + value_as_string?: string; + }; + avg_bucket: { + value: number | null; + }; + boxplot: { + min: number | null; + max: number | null; + q1: number | null; + q2: number | null; + q3: number | null; + }; + bucket_script: { + value: unknown; + }; + cardinality: { + value: number; + }; + children: { doc_count: number; - key: CompositeKeysOf; - } & SubAggregateOf - >; - }; - cumulative_cardinality: { - value: number; - }; - cumulative_sum: { - value: number; - }; - date_histogram: MaybeKeyed< - TAggregationContainer, - { - key: number; - key_as_string: string; - doc_count: number; - } & SubAggregateOf - >; - date_range: MaybeKeyed< - TAggregationContainer, - Partial<{ from: string | number; from_as_string: string }> & - Partial<{ to: string | number; to_as_string: string }> & { + } & SubAggregateOf; + composite: { + after_key: CompositeKeysOf; + buckets: Array< + { + doc_count: number; + key: CompositeKeysOf; + } & SubAggregateOf + >; + }; + cumulative_cardinality: { + value: number; + }; + cumulative_sum: { + value: number; + }; + date_histogram: MaybeKeyed< + TAggregationContainer, + { + key: number; + key_as_string: string; + doc_count: number; + } & SubAggregateOf + >; + date_range: MaybeKeyed< + TAggregationContainer, + Partial<{ from: string | number; from_as_string: string }> & + Partial<{ to: string | number; to_as_string: string }> & { + doc_count: number; + key: string; + } + >; + derivative: + | { + value: number | null; + } + | undefined; + extended_stats: { + count: number; + min: number | null; + max: number | null; + avg: number | null; + sum: number; + sum_of_squares: number | null; + variance: number | null; + variance_population: number | null; + variance_sampling: number | null; + std_deviation: number | null; + std_deviation_population: number | null; + std_deviation_sampling: number | null; + std_deviation_bounds: { + upper: number | null; + lower: number | null; + upper_population: number | null; + lower_population: number | null; + upper_sampling: number | null; + lower_sampling: number | null; + }; + } & ( + | { + min_as_string: string; + max_as_string: string; + avg_as_string: string; + sum_of_squares_as_string: string; + variance_population_as_string: string; + variance_sampling_as_string: string; + std_deviation_as_string: string; + std_deviation_population_as_string: string; + std_deviation_sampling_as_string: string; + std_deviation_bounds_as_string: { + upper: string; + lower: string; + upper_population: string; + lower_population: string; + upper_sampling: string; + lower_sampling: string; + }; + } + | {} + ); + extended_stats_bucket: { + count: number; + min: number | null; + max: number | null; + avg: number | null; + sum: number | null; + sum_of_squares: number | null; + variance: number | null; + variance_population: number | null; + variance_sampling: number | null; + std_deviation: number | null; + std_deviation_population: number | null; + std_deviation_sampling: number | null; + std_deviation_bounds: { + upper: number | null; + lower: number | null; + upper_population: number | null; + lower_population: number | null; + upper_sampling: number | null; + lower_sampling: number | null; + }; + }; + filter: { doc_count: number; - key: string; - } - >; - derivative: - | { - value: number | null; - } - | undefined; - extended_stats: { - count: number; - min: number | null; - max: number | null; - avg: number | null; - sum: number; - sum_of_squares: number | null; - variance: number | null; - variance_population: number | null; - variance_sampling: number | null; - std_deviation: number | null; - std_deviation_population: number | null; - std_deviation_sampling: number | null; - std_deviation_bounds: { - upper: number | null; - lower: number | null; - upper_population: number | null; - lower_population: number | null; - upper_sampling: number | null; - lower_sampling: number | null; - }; - } & ( - | { - min_as_string: string; - max_as_string: string; - avg_as_string: string; - sum_of_squares_as_string: string; - variance_population_as_string: string; - variance_sampling_as_string: string; - std_deviation_as_string: string; - std_deviation_population_as_string: string; - std_deviation_sampling_as_string: string; - std_deviation_bounds_as_string: { - upper: string; - lower: string; - upper_population: string; - lower_population: string; - upper_sampling: string; - lower_sampling: string; + } & SubAggregateOf; + filters: { + buckets: TAggregationContainer extends { filters: { filters: any[] } } + ? Array< + { + doc_count: number; + } & SubAggregateOf + > + : TAggregationContainer extends { filters: { filters: Record } } + ? { + [key in keyof TAggregationContainer['filters']['filters']]: { + doc_count: number; + } & SubAggregateOf; + } & (TAggregationContainer extends { + filters: { other_bucket_key: infer TOtherBucketKey }; + } + ? Record< + TOtherBucketKey & string, + { doc_count: number } & SubAggregateOf + > + : unknown) & + (TAggregationContainer extends { filters: { other_bucket: true } } + ? { + _other: { doc_count: number } & SubAggregateOf< + TAggregationContainer, + TDocument + >; + } + : unknown) + : unknown; + }; + geo_bounds: { + top_left: { + lat: number | null; + lon: number | null; }; - } - | {} - ); - extended_stats_bucket: { - count: number; - min: number | null; - max: number | null; - avg: number | null; - sum: number | null; - sum_of_squares: number | null; - variance: number | null; - variance_population: number | null; - variance_sampling: number | null; - std_deviation: number | null; - std_deviation_population: number | null; - std_deviation_sampling: number | null; - std_deviation_bounds: { - upper: number | null; - lower: number | null; - upper_population: number | null; - lower_population: number | null; - upper_sampling: number | null; - lower_sampling: number | null; - }; - }; - filter: { - doc_count: number; - } & SubAggregateOf; - filters: { - buckets: TAggregationContainer extends { filters: { filters: any[] } } - ? Array< + bottom_right: { + lat: number | null; + lon: number | null; + }; + }; + geo_centroid: { + count: number; + location: { + lat: number; + lon: number; + }; + }; + geo_distance: MaybeKeyed< + TAggregationContainer, + { + from: number; + to?: number; + doc_count: number; + } & SubAggregateOf + >; + geo_hash: { + buckets: Array< { doc_count: number; + key: string; } & SubAggregateOf - > - : TAggregationContainer extends { filters: { filters: Record } } - ? { - [key in keyof TAggregationContainer['filters']['filters']]: { + >; + }; + geotile_grid: { + buckets: Array< + { doc_count: number; - } & SubAggregateOf; - } & (TAggregationContainer extends { filters: { other_bucket_key: infer TOtherBucketKey } } - ? Record< - TOtherBucketKey & string, - { doc_count: number } & SubAggregateOf - > - : unknown) & - (TAggregationContainer extends { filters: { other_bucket: true } } - ? { _other: { doc_count: number } & SubAggregateOf } - : unknown) - : unknown; - }; - geo_bounds: { - top_left: { - lat: number | null; - lon: number | null; - }; - bottom_right: { - lat: number | null; - lon: number | null; - }; - }; - geo_centroid: { - count: number; - location: { - lat: number; - lon: number; - }; - }; - geo_distance: MaybeKeyed< - TAggregationContainer, - { - from: number; - to?: number; - doc_count: number; - } & SubAggregateOf - >; - geo_hash: { - buckets: Array< - { + key: string; + } & SubAggregateOf + >; + }; + global: { doc_count: number; - key: string; - } & SubAggregateOf - >; - }; - geotile_grid: { - buckets: Array< - { + } & SubAggregateOf; + histogram: MaybeKeyed< + TAggregationContainer, + { + key: number; + doc_count: number; + } & SubAggregateOf + >; + ip_range: MaybeKeyed< + TAggregationContainer, + { + key: string; + from?: string; + to?: string; + doc_count: number; + }, + TAggregationContainer extends { ip_range: { ranges: Array } } + ? TRangeType extends { key: infer TKeys } + ? TKeys + : string + : string + >; + inference: { + value: number; + prediction_probability: number; + prediction_score: number; + }; + max: { + value: number | null; + value_as_string?: string; + }; + max_bucket: { + value: number | null; + }; + min: { + value: number | null; + value_as_string?: string; + }; + min_bucket: { + value: number | null; + }; + median_absolute_deviation: { + value: number | null; + }; + moving_avg: + | { + value: number | null; + } + | undefined; + moving_fn: { + value: number | null; + }; + moving_percentiles: TAggregationContainer extends Record + ? Array<{ key: number; value: number | null }> + : Record | undefined; + missing: { doc_count: number; - key: string; - } & SubAggregateOf - >; - }; - global: { - doc_count: number; - } & SubAggregateOf; - histogram: MaybeKeyed< - TAggregationContainer, - { - key: number; - doc_count: number; - } & SubAggregateOf - >; - ip_range: MaybeKeyed< - TAggregationContainer, - { - key: string; - from?: string; - to?: string; - doc_count: number; - }, - TAggregationContainer extends { ip_range: { ranges: Array } } - ? TRangeType extends { key: infer TKeys } - ? TKeys - : string - : string - >; - inference: { - value: number; - prediction_probability: number; - prediction_score: number; - }; - max: { - value: number | null; - value_as_string?: string; - }; - max_bucket: { - value: number | null; - }; - min: { - value: number | null; - value_as_string?: string; - }; - min_bucket: { - value: number | null; - }; - median_absolute_deviation: { - value: number | null; - }; - moving_avg: - | { + } & SubAggregateOf; + multi_terms: { + doc_count_error_upper_bound: number; + sum_other_doc_count: number; + buckets: Array< + { + doc_count: number; + key: string[]; + } & SubAggregateOf + >; + }; + nested: { + doc_count: number; + } & SubAggregateOf; + normalize: { value: number | null; - } - | undefined; - moving_fn: { - value: number | null; - }; - moving_percentiles: TAggregationContainer extends Record - ? Array<{ key: number; value: number | null }> - : Record | undefined; - missing: { - doc_count: number; - } & SubAggregateOf; - multi_terms: { - doc_count_error_upper_bound: number; - sum_other_doc_count: number; - buckets: Array< - { + // TODO: should be perhaps based on input? ie when `format` is specified + value_as_string?: string; + }; + parent: { doc_count: number; - key: string[]; - } & SubAggregateOf - >; - }; - nested: { - doc_count: number; - } & SubAggregateOf; - normalize: { - value: number | null; - // TODO: should be perhaps based on input? ie when `format` is specified - value_as_string?: string; - }; - parent: { - doc_count: number; - } & SubAggregateOf; - percentiles: { - values: TAggregationContainer extends Record - ? Array<{ key: number; value: number | null }> - : Record; - }; - percentile_ranks: { - values: TAggregationContainer extends Record - ? Array<{ key: number; value: number | null }> - : Record; - }; - percentiles_bucket: { - values: TAggregationContainer extends Record - ? Array<{ key: number; value: number | null }> - : Record; - }; - range: MaybeKeyed< - TAggregationContainer, - { - key: string; - from?: number; - from_as_string?: string; - to?: number; - to_as_string?: string; - doc_count: number; - }, - TAggregationContainer extends { range: { ranges: Array } } - ? TRangeType extends { key: infer TKeys } - ? TKeys - : string - : string - >; - rare_terms: Array< - { - key: string | number; - doc_count: number; - } & SubAggregateOf - >; - rate: { - value: number | null; - }; - reverse_nested: { - doc_count: number; - } & SubAggregateOf; - sampler: { - doc_count: number; - } & SubAggregateOf; - scripted_metric: { - value: unknown; - }; - serial_diff: { - value: number | null; - // TODO: should be perhaps based on input? ie when `format` is specified - value_as_string?: string; - }; - significant_terms: { - doc_count: number; - bg_count: number; - buckets: Array< - { - key: string | number; - score: number; + } & SubAggregateOf; + percentiles: { + values: TAggregationContainer extends Record + ? Array<{ key: number; value: number | null }> + : Record; + }; + percentile_ranks: { + values: TAggregationContainer extends Record + ? Array<{ key: number; value: number | null }> + : Record; + }; + percentiles_bucket: { + values: TAggregationContainer extends Record + ? Array<{ key: number; value: number | null }> + : Record; + }; + range: MaybeKeyed< + TAggregationContainer, + { + key: string; + from?: number; + from_as_string?: string; + to?: number; + to_as_string?: string; + doc_count: number; + }, + TAggregationContainer extends { range: { ranges: Array } } + ? TRangeType extends { key: infer TKeys } + ? TKeys + : string + : string + >; + rare_terms: Array< + { + key: string | number; + doc_count: number; + } & SubAggregateOf + >; + rate: { + value: number | null; + }; + reverse_nested: { + doc_count: number; + } & SubAggregateOf; + sampler: { + doc_count: number; + } & SubAggregateOf; + scripted_metric: { + value: unknown; + }; + serial_diff: { + value: number | null; + // TODO: should be perhaps based on input? ie when `format` is specified + value_as_string?: string; + }; + significant_terms: { doc_count: number; bg_count: number; - } & SubAggregateOf - >; - }; - significant_text: { - doc_count: number; - buckets: Array<{ - key: string; - doc_count: number; - score: number; - bg_count: number; - }>; - }; - stats: { - count: number; - min: number | null; - max: number | null; - avg: number | null; - sum: number; - } & ( - | { - min_as_string: string; - max_as_string: string; - avg_as_string: string; - sum_as_string: string; - } - | {} - ); - stats_bucket: { - count: number; - min: number | null; - max: number | null; - avg: number | null; - sum: number; - }; - string_stats: { - count: number; - min_length: number | null; - max_length: number | null; - avg_length: number | null; - entropy: number | null; - distribution: Record; - }; - sum: { - value: number | null; - value_as_string?: string; - }; - sum_bucket: { - value: number | null; - }; - terms: { - doc_count_error_upper_bound: number; - sum_other_doc_count: number; - buckets: Array< - { + buckets: Array< + { + key: string | number; + score: number; + doc_count: number; + bg_count: number; + } & SubAggregateOf + >; + }; + significant_text: { doc_count: number; - key: string | number; - } & SubAggregateOf - >; - }; - top_hits: { - hits: { - total: { + buckets: Array<{ + key: string; + doc_count: number; + score: number; + bg_count: number; + }>; + }; + stats: { + count: number; + min: number | null; + max: number | null; + avg: number | null; + sum: number; + } & ( + | { + min_as_string: string; + max_as_string: string; + avg_as_string: string; + sum_as_string: string; + } + | {} + ); + stats_bucket: { + count: number; + min: number | null; + max: number | null; + avg: number | null; + sum: number; + }; + string_stats: { + count: number; + min_length: number | null; + max_length: number | null; + avg_length: number | null; + entropy: number | null; + distribution: Record; + }; + sum: { + value: number | null; + value_as_string?: string; + }; + sum_bucket: { + value: number | null; + }; + terms: { + doc_count_error_upper_bound: number; + sum_other_doc_count: number; + buckets: Array< + { + doc_count: number; + key: string | number; + } & SubAggregateOf + >; + }; + top_hits: { + hits: { + total: { + value: number; + relation: 'eq' | 'gte'; + }; + max_score: number | null; + hits: TAggregationContainer extends { top_hits: estypes.AggregationsTopHitsAggregation } + ? HitsOf + : estypes.SearchHitsMetadata; + }; + }; + top_metrics: { + top: Array<{ + sort: number[] | string[]; + metrics: Record, string | number | null>; + }>; + }; + weighted_avg: { value: number | null }; + value_count: { value: number; - relation: 'eq' | 'gte'; }; - max_score: number | null; - hits: TAggregationContainer extends { top_hits: estypes.AggregationsTopHitsAggregation } - ? HitsOf - : estypes.SearchHitsMetadata; - }; - }; - top_metrics: { - top: Array<{ - sort: number[] | string[]; - metrics: Record, string | number | null>; - }>; - }; - weighted_avg: { value: number | null }; - value_count: { - value: number; - }; - // t_test: {} not defined -})[ValidAggregationKeysOf & AggregationTypeName]; + // t_test: {} not defined + }, + Exclude, 'aggs' | 'aggregations'> & string + > +>; type AggregateOfMap = { - [TAggregationName in keyof TAggregationMap]: Required[TAggregationName] extends estypes.AggregationsAggregationContainer + [TAggregationName in keyof TAggregationMap]: Required[TAggregationName] extends AggregationsAggregationContainer ? AggregateOf : never; // using never means we effectively ignore optional keys, using {} creates a union type of { ... } | {} }; diff --git a/src/dev/build/lib/integration_tests/version_info.test.ts b/src/dev/build/lib/integration_tests/version_info.test.ts index e7a3a04c04734..9385de6e00a4f 100644 --- a/src/dev/build/lib/integration_tests/version_info.test.ts +++ b/src/dev/build/lib/integration_tests/version_info.test.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -import { kibanaPackageJson as pkg } from '@kbn/dev-utils'; +import { kibanaPackageJson as pkg } from '@kbn/utils'; import { getVersionInfo } from '../version_info'; diff --git a/src/dev/build/tasks/build_kibana_example_plugins.ts b/src/dev/build/tasks/build_kibana_example_plugins.ts index 7eb696ffdd3b2..93ebf41d259e7 100644 --- a/src/dev/build/tasks/build_kibana_example_plugins.ts +++ b/src/dev/build/tasks/build_kibana_example_plugins.ts @@ -13,17 +13,23 @@ import { exec, mkdirp, copyAll, Task } from '../lib'; export const BuildKibanaExamplePlugins: Task = { description: 'Building distributable versions of Kibana example plugins', - async run(config, log, build) { - const examplesDir = Path.resolve(REPO_ROOT, 'examples'); + async run(config, log) { const args = [ - '../../scripts/plugin_helpers', + Path.resolve(REPO_ROOT, 'scripts/plugin_helpers'), 'build', `--kibana-version=${config.getBuildVersion()}`, ]; - const folders = Fs.readdirSync(examplesDir, { withFileTypes: true }) - .filter((f) => f.isDirectory()) - .map((f) => Path.resolve(REPO_ROOT, 'examples', f.name)); + const getExampleFolders = (dir: string) => { + return Fs.readdirSync(dir, { withFileTypes: true }) + .filter((f) => f.isDirectory()) + .map((f) => Path.resolve(dir, f.name)); + }; + + const folders = [ + ...getExampleFolders(Path.resolve(REPO_ROOT, 'examples')), + ...getExampleFolders(Path.resolve(REPO_ROOT, 'x-pack/examples')), + ]; for (const examplePlugin of folders) { try { @@ -40,8 +46,8 @@ export const BuildKibanaExamplePlugins: Task = { const pluginsDir = config.resolveFromTarget('example_plugins'); await mkdirp(pluginsDir); - await copyAll(examplesDir, pluginsDir, { - select: ['*/build/*.zip'], + await copyAll(REPO_ROOT, pluginsDir, { + select: ['examples/*/build/*.zip', 'x-pack/examples/*/build/*.zip'], }); }, }; diff --git a/src/dev/build/tasks/os_packages/docker_generator/bundle_dockerfiles.ts b/src/dev/build/tasks/os_packages/docker_generator/bundle_dockerfiles.ts index 02b469820f900..cc1ffb5f3e301 100644 --- a/src/dev/build/tasks/os_packages/docker_generator/bundle_dockerfiles.ts +++ b/src/dev/build/tasks/os_packages/docker_generator/bundle_dockerfiles.ts @@ -10,7 +10,8 @@ import { resolve } from 'path'; import { readFileSync } from 'fs'; import { copyFile } from 'fs/promises'; -import { ToolingLog, REPO_ROOT } from '@kbn/dev-utils'; +import { ToolingLog } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import Mustache from 'mustache'; import { compressTar, copyAll, mkdirp, write, Config } from '../../../lib'; diff --git a/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker b/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker index 895c42ad5f47d..a7d8fe684ef95 100755 --- a/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker +++ b/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker @@ -354,6 +354,7 @@ kibana_vars=( xpack.security.showInsecureClusterWarning xpack.securitySolution.alertMergeStrategy xpack.securitySolution.alertIgnoreFields + xpack.securitySolution.maxExceptionsImportSize xpack.securitySolution.maxRuleImportExportSize xpack.securitySolution.maxRuleImportPayloadBytes xpack.securitySolution.maxTimelineImportExportSize diff --git a/src/dev/build/tasks/os_packages/docker_generator/run.ts b/src/dev/build/tasks/os_packages/docker_generator/run.ts index 6a192baed3fa3..085b4393caa66 100644 --- a/src/dev/build/tasks/os_packages/docker_generator/run.ts +++ b/src/dev/build/tasks/os_packages/docker_generator/run.ts @@ -10,7 +10,8 @@ import { access, link, unlink, chmod } from 'fs'; import { resolve, basename } from 'path'; import { promisify } from 'util'; -import { ToolingLog, kibanaPackageJson } from '@kbn/dev-utils'; +import { ToolingLog } from '@kbn/dev-utils'; +import { kibanaPackageJson } from '@kbn/utils'; import { write, copyAll, mkdirp, exec, Config, Build } from '../../../lib'; import * as dockerTemplates from './templates'; diff --git a/src/dev/build/tasks/os_packages/docker_generator/templates/base/Dockerfile b/src/dev/build/tasks/os_packages/docker_generator/templates/base/Dockerfile index b1d9fafffab57..90a622e64efe4 100644 --- a/src/dev/build/tasks/os_packages/docker_generator/templates/base/Dockerfile +++ b/src/dev/build/tasks/os_packages/docker_generator/templates/base/Dockerfile @@ -16,7 +16,7 @@ RUN {{packageManager}} install -y findutils tar gzip {{/ubi}} {{#usePublicArtifact}} -RUN cd /opt && \ +RUN cd /tmp && \ curl --retry 8 -s -L \ --output kibana.tar.gz \ https://artifacts.elastic.co/downloads/kibana/{{artifactPrefix}}-$(arch).tar.gz && \ diff --git a/src/dev/build/tasks/os_packages/docker_generator/templates/ironbank/Dockerfile b/src/dev/build/tasks/os_packages/docker_generator/templates/ironbank/Dockerfile index dbdace85eda01..e9a6ef3539692 100644 --- a/src/dev/build/tasks/os_packages/docker_generator/templates/ironbank/Dockerfile +++ b/src/dev/build/tasks/os_packages/docker_generator/templates/ironbank/Dockerfile @@ -2,9 +2,9 @@ # Build stage 0 # Extract Kibana and make various file manipulations. ################################################################################ -ARG BASE_REGISTRY=registry1.dsop.io +ARG BASE_REGISTRY=registry1.dso.mil ARG BASE_IMAGE=redhat/ubi/ubi8 -ARG BASE_TAG=8.4 +ARG BASE_TAG=8.5 FROM ${BASE_REGISTRY}/${BASE_IMAGE}:${BASE_TAG} as prep_files diff --git a/src/dev/build/tasks/os_packages/docker_generator/templates/ironbank/hardening_manifest.yaml b/src/dev/build/tasks/os_packages/docker_generator/templates/ironbank/hardening_manifest.yaml index 24614039e5eb7..1c7926c2fcbc2 100644 --- a/src/dev/build/tasks/os_packages/docker_generator/templates/ironbank/hardening_manifest.yaml +++ b/src/dev/build/tasks/os_packages/docker_generator/templates/ironbank/hardening_manifest.yaml @@ -14,7 +14,7 @@ tags: # Build args passed to Dockerfile ARGs args: BASE_IMAGE: 'redhat/ubi/ubi8' - BASE_TAG: '8.4' + BASE_TAG: '8.5' # Docker image labels labels: @@ -59,4 +59,4 @@ maintainers: - email: "yalabe.dukuly@anchore.com" name: "Yalabe Dukuly" username: "yalabe.dukuly" - cht_member: true \ No newline at end of file + cht_member: true diff --git a/src/dev/chromium_version.ts b/src/dev/chromium_version.ts index 410fcc72fbc0f..1f55330a92bb6 100644 --- a/src/dev/chromium_version.ts +++ b/src/dev/chromium_version.ts @@ -6,7 +6,8 @@ * Side Public License, v 1. */ -import { run, REPO_ROOT, ToolingLog } from '@kbn/dev-utils'; +import { run, ToolingLog } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import chalk from 'chalk'; import cheerio from 'cheerio'; import fs from 'fs'; diff --git a/src/dev/code_coverage/ingest_coverage/__tests__/enumerate_patterns.test.js b/src/dev/code_coverage/ingest_coverage/__tests__/enumerate_patterns.test.js index 57467d84f1f61..40d36ed46ea34 100644 --- a/src/dev/code_coverage/ingest_coverage/__tests__/enumerate_patterns.test.js +++ b/src/dev/code_coverage/ingest_coverage/__tests__/enumerate_patterns.test.js @@ -7,7 +7,8 @@ */ import { enumeratePatterns } from '../team_assignment/enumerate_patterns'; -import { ToolingLog, REPO_ROOT } from '@kbn/dev-utils'; +import { ToolingLog } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; const log = new ToolingLog({ level: 'info', diff --git a/src/dev/code_coverage/ingest_coverage/team_assignment/index.js b/src/dev/code_coverage/ingest_coverage/team_assignment/index.js index 0e341a3aac1dc..a38c4ee50b40a 100644 --- a/src/dev/code_coverage/ingest_coverage/team_assignment/index.js +++ b/src/dev/code_coverage/ingest_coverage/team_assignment/index.js @@ -6,7 +6,8 @@ * Side Public License, v 1. */ -import { run, createFlagError, REPO_ROOT } from '@kbn/dev-utils'; +import { run, createFlagError } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import { parse } from './parse_owners'; import { flush } from './flush'; import { enumeratePatterns } from './enumerate_patterns'; diff --git a/src/dev/ensure_all_tests_in_ci_group.ts b/src/dev/ensure_all_tests_in_ci_group.ts index aeccefae05d2c..a2d9729d3352b 100644 --- a/src/dev/ensure_all_tests_in_ci_group.ts +++ b/src/dev/ensure_all_tests_in_ci_group.ts @@ -12,7 +12,8 @@ import Fs from 'fs/promises'; import execa from 'execa'; import { safeLoad } from 'js-yaml'; -import { run, REPO_ROOT } from '@kbn/dev-utils'; +import { run } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import { schema } from '@kbn/config-schema'; const RELATIVE_JOBS_YAML_PATH = '.ci/ci_groups.yml'; diff --git a/src/dev/eslint/run_eslint_with_types.ts b/src/dev/eslint/run_eslint_with_types.ts index 750011dea1031..0f2a10d07d681 100644 --- a/src/dev/eslint/run_eslint_with_types.ts +++ b/src/dev/eslint/run_eslint_with_types.ts @@ -14,7 +14,8 @@ import execa from 'execa'; import * as Rx from 'rxjs'; import { mergeMap, reduce } from 'rxjs/operators'; import { supportsColor } from 'chalk'; -import { REPO_ROOT, run, createFailError } from '@kbn/dev-utils'; +import { run, createFailError } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import { lastValueFrom } from '@kbn/std'; import { PROJECTS } from '../typescript/projects'; diff --git a/src/dev/license_checker/config.ts b/src/dev/license_checker/config.ts index 52b1f816090df..9674694c0d655 100644 --- a/src/dev/license_checker/config.ts +++ b/src/dev/license_checker/config.ts @@ -76,6 +76,6 @@ export const LICENSE_OVERRIDES = { 'jsts@1.6.2': ['Eclipse Distribution License - v 1.0'], // cf. https://github.com/bjornharrtell/jsts '@mapbox/jsonlint-lines-primitives@2.0.2': ['MIT'], // license in readme https://github.com/tmcw/jsonlint '@elastic/ems-client@8.0.0': ['Elastic License 2.0'], - '@elastic/eui@41.0.0': ['SSPL-1.0 OR Elastic License 2.0'], + '@elastic/eui@41.2.3': ['SSPL-1.0 OR Elastic License 2.0'], 'language-subtag-registry@0.3.21': ['CC-BY-4.0'], // retired ODC‑By license https://github.com/mattcg/language-subtag-registry }; diff --git a/src/dev/plugin_discovery/find_plugins.ts b/src/dev/plugin_discovery/find_plugins.ts index f1725f34d1f8e..53a53bc08e15b 100644 --- a/src/dev/plugin_discovery/find_plugins.ts +++ b/src/dev/plugin_discovery/find_plugins.ts @@ -8,11 +8,9 @@ import Path from 'path'; import { getPluginSearchPaths } from '@kbn/config'; -import { - KibanaPlatformPlugin, - REPO_ROOT, - simpleKibanaPlatformPluginDiscovery, -} from '@kbn/dev-utils'; +import { KibanaPlatformPlugin, simpleKibanaPlatformPluginDiscovery } from '@kbn/dev-utils'; + +import { REPO_ROOT } from '@kbn/utils'; export interface SearchOptions { oss: boolean; diff --git a/src/dev/run_build_docs_cli.ts b/src/dev/run_build_docs_cli.ts index aad524b4437d3..8ee75912c1a7e 100644 --- a/src/dev/run_build_docs_cli.ts +++ b/src/dev/run_build_docs_cli.ts @@ -9,7 +9,8 @@ import Path from 'path'; import dedent from 'dedent'; -import { run, REPO_ROOT, createFailError } from '@kbn/dev-utils'; +import { run, createFailError } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; const DEFAULT_DOC_REPO_PATH = Path.resolve(REPO_ROOT, '..', 'docs'); diff --git a/src/dev/run_find_plugins_with_circular_deps.ts b/src/dev/run_find_plugins_with_circular_deps.ts index f7974b464fcaf..f9ee7bd84c54f 100644 --- a/src/dev/run_find_plugins_with_circular_deps.ts +++ b/src/dev/run_find_plugins_with_circular_deps.ts @@ -10,7 +10,8 @@ import dedent from 'dedent'; import { parseDependencyTree, parseCircular, prettyCircular } from 'dpdm'; import { relative } from 'path'; import { getPluginSearchPaths } from '@kbn/config'; -import { REPO_ROOT, run } from '@kbn/dev-utils'; +import { run } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; interface Options { debug?: boolean; diff --git a/src/dev/run_precommit_hook.js b/src/dev/run_precommit_hook.js index a7bd0a9f57f6e..dfa3a94426bb2 100644 --- a/src/dev/run_precommit_hook.js +++ b/src/dev/run_precommit_hook.js @@ -8,7 +8,8 @@ import SimpleGit from 'simple-git/promise'; -import { run, combineErrors, createFlagError, REPO_ROOT } from '@kbn/dev-utils'; +import { run, combineErrors, createFlagError } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import * as Eslint from './eslint'; import * as Stylelint from './stylelint'; import { getFilesForCommit, checkFileCasing } from './precommit_hook'; diff --git a/src/dev/typescript/build_ts_refs.ts b/src/dev/typescript/build_ts_refs.ts index aaa8c0d12fa4d..f3896cf676e27 100644 --- a/src/dev/typescript/build_ts_refs.ts +++ b/src/dev/typescript/build_ts_refs.ts @@ -8,7 +8,8 @@ import Path from 'path'; -import { ToolingLog, REPO_ROOT, ProcRunner } from '@kbn/dev-utils'; +import { ToolingLog, ProcRunner } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import { ROOT_REFS_CONFIG_PATH } from './root_refs_config'; import { Project } from './project'; diff --git a/src/dev/typescript/build_ts_refs_cli.ts b/src/dev/typescript/build_ts_refs_cli.ts index c68424c2a98f7..09866315fc8dd 100644 --- a/src/dev/typescript/build_ts_refs_cli.ts +++ b/src/dev/typescript/build_ts_refs_cli.ts @@ -8,7 +8,8 @@ import Path from 'path'; -import { run, REPO_ROOT, createFlagError } from '@kbn/dev-utils'; +import { run, createFlagError } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import del from 'del'; import { RefOutputCache } from './ref_output_cache'; diff --git a/src/dev/typescript/ref_output_cache/ref_output_cache.ts b/src/dev/typescript/ref_output_cache/ref_output_cache.ts index b7e641ceb33d5..32b08ec1ba0df 100644 --- a/src/dev/typescript/ref_output_cache/ref_output_cache.ts +++ b/src/dev/typescript/ref_output_cache/ref_output_cache.ts @@ -9,7 +9,8 @@ import Path from 'path'; import Fs from 'fs/promises'; -import { ToolingLog, kibanaPackageJson, extract } from '@kbn/dev-utils'; +import { ToolingLog, extract } from '@kbn/dev-utils'; +import { kibanaPackageJson } from '@kbn/utils'; import del from 'del'; import tempy from 'tempy'; diff --git a/src/dev/typescript/root_refs_config.ts b/src/dev/typescript/root_refs_config.ts index f4aa88f1ea6b2..e20b1ab46cd82 100644 --- a/src/dev/typescript/root_refs_config.ts +++ b/src/dev/typescript/root_refs_config.ts @@ -10,7 +10,8 @@ import Path from 'path'; import Fs from 'fs/promises'; import dedent from 'dedent'; -import { REPO_ROOT, ToolingLog } from '@kbn/dev-utils'; +import { ToolingLog } from '@kbn/dev-utils'; +import { REPO_ROOT } from '@kbn/utils'; import normalize from 'normalize-path'; import { PROJECTS } from './projects'; diff --git a/src/plugins/advanced_settings/kibana.json b/src/plugins/advanced_settings/kibana.json index 7562b6a660193..033d5e9da9eab 100644 --- a/src/plugins/advanced_settings/kibana.json +++ b/src/plugins/advanced_settings/kibana.json @@ -5,7 +5,7 @@ "ui": true, "requiredPlugins": ["management"], "optionalPlugins": ["home", "usageCollection"], - "requiredBundles": ["kibanaReact", "kibanaUtils", "home", "esUiShared"], + "requiredBundles": ["kibanaReact", "kibanaUtils", "home"], "owner": { "name": "Vis Editors", "githubTeam": "kibana-vis-editors" diff --git a/src/plugins/advanced_settings/public/management_app/advanced_settings.tsx b/src/plugins/advanced_settings/public/management_app/advanced_settings.tsx index c0decf516fbad..e0966d70aeb98 100644 --- a/src/plugins/advanced_settings/public/management_app/advanced_settings.tsx +++ b/src/plugins/advanced_settings/public/management_app/advanced_settings.tsx @@ -33,6 +33,7 @@ import { getAriaName, toEditableConfig, fieldSorter, DEFAULT_CATEGORY } from './ import { FieldSetting, SettingsChanges } from './types'; import { parseErrorMsg } from './components/search/search'; +import { KibanaContextProvider } from '../../../../../src/plugins/kibana_react/public'; export const QUERY = 'query'; @@ -259,21 +260,23 @@ export class AdvancedSettings extends Component -
+ + + - @@ -1906,30 +1893,17 @@ exports[`Field for json setting should render as read only with help text if ove
-
@@ -1989,30 +1963,17 @@ exports[`Field for json setting should render custom setting icon if it is custo
-
@@ -2103,30 +2064,17 @@ exports[`Field for json setting should render default value if there is no user
-
@@ -2192,35 +2140,22 @@ exports[`Field for json setting should render unsaved value if there are unsaved
-
@@ -2318,30 +2253,17 @@ exports[`Field for json setting should render user value if there is user value
-
@@ -2390,30 +2312,17 @@ exports[`Field for markdown setting should render as read only if saving is disa
-
@@ -2494,30 +2403,17 @@ exports[`Field for markdown setting should render as read only with help text if
-
@@ -2577,30 +2473,17 @@ exports[`Field for markdown setting should render custom setting icon if it is c
-
@@ -2649,30 +2532,17 @@ exports[`Field for markdown setting should render default value if there is no u
-
@@ -2738,31 +2608,18 @@ exports[`Field for markdown setting should render unsaved value if there are uns
-
@@ -2857,30 +2714,17 @@ exports[`Field for markdown setting should render user value if there is user va
-
diff --git a/src/plugins/advanced_settings/public/management_app/components/field/field.test.tsx b/src/plugins/advanced_settings/public/management_app/components/field/field.test.tsx index 7047959522427..b77a687b50cd9 100644 --- a/src/plugins/advanced_settings/public/management_app/components/field/field.test.tsx +++ b/src/plugins/advanced_settings/public/management_app/components/field/field.test.tsx @@ -17,8 +17,9 @@ import { notificationServiceMock, docLinksServiceMock } from '../../../../../../ import { findTestSubject } from '@elastic/eui/lib/test'; import { Field, getEditableValue } from './field'; -jest.mock('brace/theme/textmate', () => 'brace/theme/textmate'); -jest.mock('brace/mode/markdown', () => 'brace/mode/markdown'); +jest.mock('../../../../../kibana_react/public/ui_settings/use_ui_setting', () => ({ + useUiSetting: jest.fn(), +})); const defaults = { requiresPageReload: false, diff --git a/src/plugins/advanced_settings/public/management_app/components/field/field.tsx b/src/plugins/advanced_settings/public/management_app/components/field/field.tsx index 586609fa1bf64..e43f30e52ee74 100644 --- a/src/plugins/advanced_settings/public/management_app/components/field/field.tsx +++ b/src/plugins/advanced_settings/public/management_app/components/field/field.tsx @@ -8,10 +8,6 @@ import React, { PureComponent, Fragment } from 'react'; import classNames from 'classnames'; -import 'react-ace'; -import 'brace/theme/textmate'; -import 'brace/mode/markdown'; -import 'brace/mode/json'; import { EuiBadge, @@ -36,10 +32,10 @@ import { } from '@elastic/eui'; import { i18n } from '@kbn/i18n'; import { FormattedMessage } from '@kbn/i18n-react'; +import { FieldCodeEditor } from './field_code_editor'; import { FieldSetting, FieldState } from '../../types'; import { isDefaultValue } from '../../lib'; import { UiSettingsType, DocLinksStart, ToastsStart } from '../../../../../../core/public'; -import { EuiCodeEditor } from '../../../../../es_ui_shared/public'; interface FieldProps { setting: FieldSetting; @@ -130,7 +126,7 @@ export class Field extends PureComponent { switch (type) { case 'json': const isJsonArray = Array.isArray(JSON.parse((defVal as string) || '{}')); - newUnsavedValue = value.trim() || (isJsonArray ? '[]' : '{}'); + newUnsavedValue = value || (isJsonArray ? '[]' : '{}'); try { JSON.parse(newUnsavedValue); } catch (e) { @@ -291,26 +287,13 @@ export class Field extends PureComponent { case 'json': return (
-
); diff --git a/src/plugins/advanced_settings/public/management_app/components/field/field_code_editor.tsx b/src/plugins/advanced_settings/public/management_app/components/field/field_code_editor.tsx new file mode 100644 index 0000000000000..5ba1c55e67ec8 --- /dev/null +++ b/src/plugins/advanced_settings/public/management_app/components/field/field_code_editor.tsx @@ -0,0 +1,106 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import React, { useCallback } from 'react'; +import { monaco, XJsonLang } from '@kbn/monaco'; +import { CodeEditor, MarkdownLang } from '../../../../../../../src/plugins/kibana_react/public'; + +interface FieldCodeEditorProps { + value: string; + onChange: (value: string) => void; + type: 'markdown' | 'json'; + isReadOnly: boolean; + a11yProps: Record; + name: string; +} + +const MIN_DEFAULT_LINES_COUNT = 6; +const MAX_DEFAULT_LINES_COUNT = 30; + +export const FieldCodeEditor = ({ + value, + onChange, + type, + isReadOnly, + a11yProps, + name, +}: FieldCodeEditorProps) => { + // setting editor height based on lines height and count to stretch and fit its content + const setEditorCalculatedHeight = useCallback( + (editor: monaco.editor.IStandaloneCodeEditor) => { + const editorElement = editor.getDomNode(); + + if (!editorElement) { + return; + } + + const lineHeight = editor.getOption(monaco.editor.EditorOption.lineHeight); + let lineCount = editor.getModel()?.getLineCount() || MIN_DEFAULT_LINES_COUNT; + if (lineCount < MIN_DEFAULT_LINES_COUNT) { + lineCount = MIN_DEFAULT_LINES_COUNT; + } else if (lineCount > MAX_DEFAULT_LINES_COUNT) { + lineCount = MAX_DEFAULT_LINES_COUNT; + } + const height = lineHeight * lineCount; + + editorElement.id = name; + editorElement.style.height = `${height}px`; + editor.layout(); + }, + [name] + ); + + const trimEditorBlankLines = useCallback((editor: monaco.editor.IStandaloneCodeEditor) => { + const editorModel = editor.getModel(); + + if (!editorModel) { + return; + } + const trimmedValue = editorModel.getValue().trim(); + editorModel.setValue(trimmedValue); + }, []); + + const editorDidMount = useCallback( + (editor) => { + setEditorCalculatedHeight(editor); + + editor.onDidChangeModelContent(() => { + setEditorCalculatedHeight(editor); + }); + + editor.onDidBlurEditorWidget(() => { + trimEditorBlankLines(editor); + }); + }, + [setEditorCalculatedHeight, trimEditorBlankLines] + ); + + return ( + + ); +}; diff --git a/src/plugins/console/public/application/components/editor_example.tsx b/src/plugins/console/public/application/components/editor_example.tsx index 577f32fa912fb..21e3ab0c7d274 100644 --- a/src/plugins/console/public/application/components/editor_example.tsx +++ b/src/plugins/console/public/application/components/editor_example.tsx @@ -8,8 +8,10 @@ import { EuiScreenReaderOnly } from '@elastic/eui'; import { i18n } from '@kbn/i18n'; -import React, { useEffect } from 'react'; -import { createReadOnlyAceEditor } from '../models/legacy_core_editor'; +import React, { useEffect, useRef } from 'react'; +import { createReadOnlyAceEditor, CustomAceEditor } from '../models/sense_editor'; +// @ts-ignore +import { Mode } from '../models/legacy_core_editor/mode/input'; interface EditorExampleProps { panel: string; @@ -27,21 +29,33 @@ GET index/_doc/1 `; export function EditorExample(props: EditorExampleProps) { - const elemId = `help-example-${props.panel}`; const inputId = `help-example-${props.panel}-input`; + const wrapperDivRef = useRef(null); + const editorRef = useRef(); useEffect(() => { - const el = document.getElementById(elemId)!; - el.textContent = exampleText.trim(); - const editor = createReadOnlyAceEditor(el); - const textarea = el.querySelector('textarea')!; - textarea.setAttribute('id', inputId); - textarea.setAttribute('readonly', 'true'); + if (wrapperDivRef.current) { + editorRef.current = createReadOnlyAceEditor(wrapperDivRef.current); + + const editor = editorRef.current; + editor.update(exampleText.trim()); + editor.session.setMode(new Mode()); + editor.session.setUseWorker(false); + editor.setHighlightActiveLine(false); + + const textareaElement = wrapperDivRef.current.querySelector('textarea'); + if (textareaElement) { + textareaElement.setAttribute('id', inputId); + textareaElement.setAttribute('readonly', 'true'); + } + } return () => { - editor.destroy(); + if (editorRef.current) { + editorRef.current.destroy(); + } }; - }, [elemId, inputId]); + }, [inputId]); return ( <> @@ -52,7 +66,7 @@ export function EditorExample(props: EditorExampleProps) { })}
-
+
); } diff --git a/src/plugins/console/public/application/contexts/services_context.mock.ts b/src/plugins/console/public/application/contexts/services_context.mock.ts index c19413bdd0413..90a5d9ddce010 100644 --- a/src/plugins/console/public/application/contexts/services_context.mock.ts +++ b/src/plugins/console/public/application/contexts/services_context.mock.ts @@ -7,7 +7,7 @@ */ import { notificationServiceMock } from '../../../../../core/public/mocks'; -import { httpServiceMock } from '../../../../../core/public/mocks'; +import { httpServiceMock, themeServiceMock } from '../../../../../core/public/mocks'; import type { ObjectStorageClient } from '../../../common/types'; import { HistoryMock } from '../../services/history.mock'; @@ -35,6 +35,7 @@ export const serviceContextMock = { objectStorageClient: {} as unknown as ObjectStorageClient, }, docLinkVersion: 'NA', + theme$: themeServiceMock.create().start().theme$, }; }, }; diff --git a/src/plugins/console/public/application/contexts/services_context.tsx b/src/plugins/console/public/application/contexts/services_context.tsx index 53c021d4d0982..5912de0375590 100644 --- a/src/plugins/console/public/application/contexts/services_context.tsx +++ b/src/plugins/console/public/application/contexts/services_context.tsx @@ -7,7 +7,9 @@ */ import React, { createContext, useContext, useEffect } from 'react'; -import { NotificationsSetup } from 'kibana/public'; +import { Observable } from 'rxjs'; +import { NotificationsSetup, CoreTheme } from 'kibana/public'; + import { History, Settings, Storage } from '../../services'; import { ObjectStorageClient } from '../../../common/types'; import { MetricsTracker } from '../../types'; @@ -26,6 +28,7 @@ interface ContextServices { export interface ContextValue { services: ContextServices; docLinkVersion: string; + theme$: Observable; } interface ContextProps { diff --git a/src/plugins/console/public/application/hooks/use_send_current_request_to_es/use_send_current_request_to_es.ts b/src/plugins/console/public/application/hooks/use_send_current_request_to_es/use_send_current_request_to_es.ts index d025760c19d0a..81aa571b45a20 100644 --- a/src/plugins/console/public/application/hooks/use_send_current_request_to_es/use_send_current_request_to_es.ts +++ b/src/plugins/console/public/application/hooks/use_send_current_request_to_es/use_send_current_request_to_es.ts @@ -8,20 +8,21 @@ import { i18n } from '@kbn/i18n'; import { useCallback } from 'react'; + +import { toMountPoint } from '../../../shared_imports'; import { isQuotaExceededError } from '../../../services/history'; +// @ts-ignore +import { retrieveAutoCompleteInfo } from '../../../lib/mappings/mappings'; import { instance as registry } from '../../contexts/editor_context/editor_registry'; import { useRequestActionContext, useServicesContext } from '../../contexts'; +import { StorageQuotaError } from '../../components/storage_quota_error'; import { sendRequestToES } from './send_request_to_es'; import { track } from './track'; -import { toMountPoint } from '../../../../../kibana_react/public'; - -// @ts-ignore -import { retrieveAutoCompleteInfo } from '../../../lib/mappings/mappings'; -import { StorageQuotaError } from '../../components/storage_quota_error'; export const useSendCurrentRequestToES = () => { const { services: { history, settings, notifications, trackUiMetric }, + theme$, } = useServicesContext(); const dispatch = useRequestActionContext(); @@ -83,7 +84,8 @@ export const useSendCurrentRequestToES = () => { settings.setHistoryDisabled(true); notifications.toasts.remove(toast); }, - }) + }), + { theme$ } ), }); } else { @@ -127,5 +129,5 @@ export const useSendCurrentRequestToES = () => { }); } } - }, [dispatch, settings, history, notifications, trackUiMetric]); + }, [dispatch, settings, history, notifications, trackUiMetric, theme$]); }; diff --git a/src/plugins/console/public/application/index.tsx b/src/plugins/console/public/application/index.tsx index 0b41095f8cc19..719975874cd44 100644 --- a/src/plugins/console/public/application/index.tsx +++ b/src/plugins/console/public/application/index.tsx @@ -8,13 +8,16 @@ import React from 'react'; import { render, unmountComponentAtNode } from 'react-dom'; -import { HttpSetup, NotificationsSetup, I18nStart } from 'src/core/public'; -import { ServicesContextProvider, EditorContextProvider, RequestContextProvider } from './contexts'; -import { Main } from './containers'; +import { Observable } from 'rxjs'; +import { HttpSetup, NotificationsSetup, I18nStart, CoreTheme } from 'src/core/public'; + +import { UsageCollectionSetup } from '../../../usage_collection/public'; +import { KibanaThemeProvider } from '../shared_imports'; import { createStorage, createHistory, createSettings } from '../services'; -import * as localStorageObjectClient from '../lib/local_storage_object_client'; import { createUsageTracker } from '../services/tracker'; -import { UsageCollectionSetup } from '../../../usage_collection/public'; +import * as localStorageObjectClient from '../lib/local_storage_object_client'; +import { Main } from './containers'; +import { ServicesContextProvider, EditorContextProvider, RequestContextProvider } from './contexts'; import { createApi, createEsHostService } from './lib'; export interface BootDependencies { @@ -24,6 +27,7 @@ export interface BootDependencies { notifications: NotificationsSetup; usageCollection?: UsageCollectionSetup; element: HTMLElement; + theme$: Observable; } export function renderApp({ @@ -33,6 +37,7 @@ export function renderApp({ usageCollection, element, http, + theme$, }: BootDependencies) { const trackUiMetric = createUsageTracker(usageCollection); trackUiMetric.load('opened_app'); @@ -49,26 +54,29 @@ export function renderApp({ render( - - - -
- - - + + + + +
+ + + + , element ); diff --git a/src/plugins/console/public/plugin.ts b/src/plugins/console/public/plugin.ts index d61769c23dfe0..f46f60b485d55 100644 --- a/src/plugins/console/public/plugin.ts +++ b/src/plugins/console/public/plugin.ts @@ -52,7 +52,7 @@ export class ConsoleUIPlugin implements Plugin { + mount: async ({ element, theme$ }) => { const [core] = await getStartServices(); const { @@ -69,6 +69,7 @@ export class ConsoleUIPlugin implements Plugin { // populated by a global rule }, }, + script_score: { + __template: { + script: {}, + query: {}, + }, + script: {}, + query: {}, + min_score: '', + boost: 1.0, + }, wrapper: { __template: { query: 'QUERY_BASE64_ENCODED', diff --git a/src/plugins/console/server/lib/spec_definitions/json/overrides/search.json b/src/plugins/console/server/lib/spec_definitions/json/overrides/search.json new file mode 100644 index 0000000000000..1028422b303f2 --- /dev/null +++ b/src/plugins/console/server/lib/spec_definitions/json/overrides/search.json @@ -0,0 +1,7 @@ +{ + "search": { + "url_params": { + "error_trace": true + } + } +} diff --git a/src/plugins/console/server/lib/spec_definitions/json/overrides/snapshot.create_repository.json b/src/plugins/console/server/lib/spec_definitions/json/overrides/snapshot.create_repository.json index c513292f2bd59..3559b8e3811c0 100644 --- a/src/plugins/console/server/lib/spec_definitions/json/overrides/snapshot.create_repository.json +++ b/src/plugins/console/server/lib/spec_definitions/json/overrides/snapshot.create_repository.json @@ -9,7 +9,7 @@ "settings": { "__one_of": [{ "__condition": { - "lines_regex": "type[\"']\\s*:\\s*[\"']fs`" + "lines_regex": "type[\"']\\s*:\\s*[\"']fs" }, "__template": { "location": "path" diff --git a/src/plugins/dashboard/public/application/embeddable/dashboard_container.tsx b/src/plugins/dashboard/public/application/embeddable/dashboard_container.tsx index 3e259d4e26179..36261fbe130a3 100644 --- a/src/plugins/dashboard/public/application/embeddable/dashboard_container.tsx +++ b/src/plugins/dashboard/public/application/embeddable/dashboard_container.tsx @@ -101,6 +101,7 @@ export class DashboardContainer extends Container void; public controlGroup?: ControlGroupContainer; + private domNode?: HTMLElement; public getPanelCount = () => { return Object.keys(this.getInput().panels).length; @@ -258,6 +259,10 @@ export class DashboardContainer extends Container @@ -275,6 +280,7 @@ export class DashboardContainer extends Container( original as unknown as DashboardDiffCommonFilters, newState as unknown as DashboardDiffCommonFilters, - ['viewMode', 'panels', 'options', 'savedQuery', 'expandedPanelId', 'controlGroupInput'], + [ + 'viewMode', + 'panels', + 'options', + 'fullScreenMode', + 'savedQuery', + 'expandedPanelId', + 'controlGroupInput', + ], true ); diff --git a/src/plugins/dashboard/public/application/lib/filter_utils.ts b/src/plugins/dashboard/public/application/lib/filter_utils.ts index a31b83ec2df8f..c6b9ae2d01cf3 100644 --- a/src/plugins/dashboard/public/application/lib/filter_utils.ts +++ b/src/plugins/dashboard/public/application/lib/filter_utils.ts @@ -72,7 +72,7 @@ export const cleanFiltersForComparison = (filters: Filter[]) => { export const cleanFiltersForSerialize = (filters: Filter[]): Filter[] => { return filters.map((filter) => { - if (filter.meta.value) { + if (filter.meta?.value) { delete filter.meta.value; } return filter; diff --git a/src/plugins/dashboard/public/application/lib/load_saved_dashboard_state.ts b/src/plugins/dashboard/public/application/lib/load_saved_dashboard_state.ts index 31579e92bd1ec..03a03842c0e66 100644 --- a/src/plugins/dashboard/public/application/lib/load_saved_dashboard_state.ts +++ b/src/plugins/dashboard/public/application/lib/load_saved_dashboard_state.ts @@ -8,10 +8,10 @@ import _ from 'lodash'; +import { getDashboard60Warning, dashboardLoadingErrorStrings } from '../../dashboard_strings'; import { savedObjectToDashboardState } from './convert_dashboard_state'; import { DashboardState, DashboardBuildContext } from '../../types'; import { DashboardConstants, DashboardSavedObject } from '../..'; -import { getDashboard60Warning } from '../../dashboard_strings'; import { migrateLegacyQuery } from './migrate_legacy_query'; import { cleanFiltersForSerialize } from './filter_utils'; import { ViewMode } from '../../services/embeddable'; @@ -52,34 +52,33 @@ export const loadSavedDashboardState = async ({ return; } await indexPatterns.ensureDefaultDataView(); - let savedDashboard: DashboardSavedObject | undefined; try { - savedDashboard = (await savedDashboards.get({ + const savedDashboard = (await savedDashboards.get({ id: savedDashboardId, useResolve: true, })) as DashboardSavedObject; + const savedDashboardState = savedObjectToDashboardState({ + savedDashboard, + usageCollection, + showWriteControls, + savedObjectsTagging, + version: initializerContext.env.packageInfo.version, + }); + + const isViewMode = !showWriteControls || Boolean(savedDashboard.id); + savedDashboardState.viewMode = isViewMode ? ViewMode.VIEW : ViewMode.EDIT; + savedDashboardState.filters = cleanFiltersForSerialize(savedDashboardState.filters); + savedDashboardState.query = migrateLegacyQuery( + savedDashboardState.query || queryString.getDefaultQuery() + ); + + return { savedDashboardState, savedDashboard }; } catch (error) { // E.g. a corrupt or deleted dashboard - notifications.toasts.addDanger(error.message); + notifications.toasts.addDanger( + dashboardLoadingErrorStrings.getDashboardLoadError(error.message) + ); history.push(DashboardConstants.LANDING_PAGE_PATH); return; } - if (!savedDashboard) return; - - const savedDashboardState = savedObjectToDashboardState({ - savedDashboard, - usageCollection, - showWriteControls, - savedObjectsTagging, - version: initializerContext.env.packageInfo.version, - }); - - const isViewMode = !showWriteControls || Boolean(savedDashboard.id); - savedDashboardState.viewMode = isViewMode ? ViewMode.VIEW : ViewMode.EDIT; - savedDashboardState.filters = cleanFiltersForSerialize(savedDashboardState.filters); - savedDashboardState.query = migrateLegacyQuery( - savedDashboardState.query || queryString.getDefaultQuery() - ); - - return { savedDashboardState, savedDashboard }; }; diff --git a/src/plugins/dashboard/public/dashboard_strings.ts b/src/plugins/dashboard/public/dashboard_strings.ts index ca0f51976f3fb..52961c43cc1a2 100644 --- a/src/plugins/dashboard/public/dashboard_strings.ts +++ b/src/plugins/dashboard/public/dashboard_strings.ts @@ -359,6 +359,14 @@ export const panelStorageErrorStrings = { }), }; +export const dashboardLoadingErrorStrings = { + getDashboardLoadError: (message: string) => + i18n.translate('dashboard.loadingError.errorMessage', { + defaultMessage: 'Error encountered while loading saved dashboard: {message}', + values: { message }, + }), +}; + /* Empty Screen */ diff --git a/src/plugins/data/common/search/aggs/agg_types.ts b/src/plugins/data/common/search/aggs/agg_types.ts index dd930887f9d19..87496767a33b2 100644 --- a/src/plugins/data/common/search/aggs/agg_types.ts +++ b/src/plugins/data/common/search/aggs/agg_types.ts @@ -14,7 +14,6 @@ import * as metrics from './metrics'; import { BUCKET_TYPES, CalculateBoundsFn } from './buckets'; import { METRIC_TYPES } from './metrics'; -/** @internal */ export interface AggTypesDependencies { calculateBounds: CalculateBoundsFn; getConfig: (key: string) => T; @@ -62,6 +61,8 @@ export const getAggTypes = () => ({ { name: BUCKET_TYPES.SIGNIFICANT_TERMS, fn: buckets.getSignificantTermsBucketAgg }, { name: BUCKET_TYPES.GEOHASH_GRID, fn: buckets.getGeoHashBucketAgg }, { name: BUCKET_TYPES.GEOTILE_GRID, fn: buckets.getGeoTitleBucketAgg }, + { name: BUCKET_TYPES.SAMPLER, fn: buckets.getSamplerBucketAgg }, + { name: BUCKET_TYPES.DIVERSIFIED_SAMPLER, fn: buckets.getDiversifiedSamplerBucketAgg }, ], }); @@ -79,6 +80,8 @@ export const getAggTypesFunctions = () => [ buckets.aggDateHistogram, buckets.aggTerms, buckets.aggMultiTerms, + buckets.aggSampler, + buckets.aggDiversifiedSampler, metrics.aggAvg, metrics.aggBucketAvg, metrics.aggBucketMax, diff --git a/src/plugins/data/common/search/aggs/agg_types_registry.ts b/src/plugins/data/common/search/aggs/agg_types_registry.ts index 108b1eb379ddd..4e57b4db3fb50 100644 --- a/src/plugins/data/common/search/aggs/agg_types_registry.ts +++ b/src/plugins/data/common/search/aggs/agg_types_registry.ts @@ -16,8 +16,6 @@ export type AggTypesRegistrySetup = ReturnType; * real start contract we will need to return the initialized versions. * So we need to provide the correct typings so they can be overwritten * on client/server. - * - * @internal */ export interface AggTypesRegistryStart { get: (id: string) => BucketAggType | MetricAggType; diff --git a/src/plugins/data/common/search/aggs/aggs_service.test.ts b/src/plugins/data/common/search/aggs/aggs_service.test.ts index be3fbae26174a..571083c18156f 100644 --- a/src/plugins/data/common/search/aggs/aggs_service.test.ts +++ b/src/plugins/data/common/search/aggs/aggs_service.test.ts @@ -73,6 +73,8 @@ describe('Aggs service', () => { "significant_terms", "geohash_grid", "geotile_grid", + "sampler", + "diversified_sampler", "foo", ] `); @@ -122,6 +124,8 @@ describe('Aggs service', () => { "significant_terms", "geohash_grid", "geotile_grid", + "sampler", + "diversified_sampler", ] `); expect(bStart.types.getAll().metrics.map((t) => t(aggTypesDependencies).name)) diff --git a/src/plugins/data/common/search/aggs/aggs_service.ts b/src/plugins/data/common/search/aggs/aggs_service.ts index 86bda5019a496..58f65bb0cab44 100644 --- a/src/plugins/data/common/search/aggs/aggs_service.ts +++ b/src/plugins/data/common/search/aggs/aggs_service.ts @@ -32,12 +32,10 @@ export const aggsRequiredUiSettings = [ UI_SETTINGS.COURIER_IGNORE_FILTER_IF_FIELD_NOT_IN_INDEX, ]; -/** @internal */ export interface AggsCommonSetupDependencies { registerFunction: ExpressionsServiceSetup['registerFunction']; } -/** @internal */ export interface AggsCommonStartDependencies { getConfig: GetConfigFn; getIndexPattern(id: string): Promise; diff --git a/src/plugins/data/common/search/aggs/buckets/bucket_agg_types.ts b/src/plugins/data/common/search/aggs/buckets/bucket_agg_types.ts index 0c01bff90bfee..671266ef15997 100644 --- a/src/plugins/data/common/search/aggs/buckets/bucket_agg_types.ts +++ b/src/plugins/data/common/search/aggs/buckets/bucket_agg_types.ts @@ -19,4 +19,6 @@ export enum BUCKET_TYPES { GEOHASH_GRID = 'geohash_grid', GEOTILE_GRID = 'geotile_grid', DATE_HISTOGRAM = 'date_histogram', + SAMPLER = 'sampler', + DIVERSIFIED_SAMPLER = 'diversified_sampler', } diff --git a/src/plugins/data/common/search/aggs/buckets/diversified_sampler.ts b/src/plugins/data/common/search/aggs/buckets/diversified_sampler.ts new file mode 100644 index 0000000000000..31ebaa094c368 --- /dev/null +++ b/src/plugins/data/common/search/aggs/buckets/diversified_sampler.ts @@ -0,0 +1,62 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { i18n } from '@kbn/i18n'; +import { BucketAggType } from './bucket_agg_type'; +import { BaseAggParams } from '../types'; +import { aggDiversifiedSamplerFnName } from './diversified_sampler_fn'; + +export const DIVERSIFIED_SAMPLER_AGG_NAME = 'diversified_sampler'; + +const title = i18n.translate('data.search.aggs.buckets.diversifiedSamplerTitle', { + defaultMessage: 'Diversified sampler', + description: 'Diversified sampler aggregation title', +}); + +export interface AggParamsDiversifiedSampler extends BaseAggParams { + /** + * Is used to provide values used for de-duplication + */ + field: string; + + /** + * Limits how many top-scoring documents are collected in the sample processed on each shard. + */ + shard_size?: number; + + /** + * Limits how many documents are permitted per choice of de-duplicating value + */ + max_docs_per_value?: number; +} + +/** + * Like the sampler aggregation this is a filtering aggregation used to limit any sub aggregations' processing to a sample of the top-scoring documents. + * The diversified_sampler aggregation adds the ability to limit the number of matches that share a common value. + */ +export const getDiversifiedSamplerBucketAgg = () => + new BucketAggType({ + name: DIVERSIFIED_SAMPLER_AGG_NAME, + title, + customLabels: false, + expressionName: aggDiversifiedSamplerFnName, + params: [ + { + name: 'shard_size', + type: 'number', + }, + { + name: 'max_docs_per_value', + type: 'number', + }, + { + name: 'field', + type: 'field', + }, + ], + }); diff --git a/src/plugins/data/common/search/aggs/buckets/diversified_sampler_fn.test.ts b/src/plugins/data/common/search/aggs/buckets/diversified_sampler_fn.test.ts new file mode 100644 index 0000000000000..e874542289bb2 --- /dev/null +++ b/src/plugins/data/common/search/aggs/buckets/diversified_sampler_fn.test.ts @@ -0,0 +1,58 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { functionWrapper } from '../test_helpers'; +import { aggDiversifiedSampler } from './diversified_sampler_fn'; + +describe('aggDiversifiedSampler', () => { + const fn = functionWrapper(aggDiversifiedSampler()); + + test('fills in defaults when only required args are provided', () => { + const actual = fn({ id: 'sampler', schema: 'bucket', field: 'author' }); + expect(actual).toMatchInlineSnapshot(` + Object { + "type": "agg_type", + "value": Object { + "enabled": true, + "id": "sampler", + "params": Object { + "field": "author", + "max_docs_per_value": undefined, + "shard_size": undefined, + }, + "schema": "bucket", + "type": "diversified_sampler", + }, + } + `); + }); + + test('includes optional params when they are provided', () => { + const actual = fn({ + id: 'sampler', + schema: 'bucket', + shard_size: 300, + field: 'author', + max_docs_per_value: 3, + }); + + expect(actual.value).toMatchInlineSnapshot(` + Object { + "enabled": true, + "id": "sampler", + "params": Object { + "field": "author", + "max_docs_per_value": 3, + "shard_size": 300, + }, + "schema": "bucket", + "type": "diversified_sampler", + } + `); + }); +}); diff --git a/src/plugins/data/common/search/aggs/buckets/diversified_sampler_fn.ts b/src/plugins/data/common/search/aggs/buckets/diversified_sampler_fn.ts new file mode 100644 index 0000000000000..0e1b235dd576d --- /dev/null +++ b/src/plugins/data/common/search/aggs/buckets/diversified_sampler_fn.ts @@ -0,0 +1,90 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { i18n } from '@kbn/i18n'; +import { ExpressionFunctionDefinition } from 'src/plugins/expressions/common'; +import { AggExpressionFunctionArgs, AggExpressionType, BUCKET_TYPES } from '../'; +import { DIVERSIFIED_SAMPLER_AGG_NAME } from './diversified_sampler'; + +export const aggDiversifiedSamplerFnName = 'aggDiversifiedSampler'; + +type Input = any; +type Arguments = AggExpressionFunctionArgs; + +type Output = AggExpressionType; +type FunctionDefinition = ExpressionFunctionDefinition< + typeof aggDiversifiedSamplerFnName, + Input, + Arguments, + Output +>; + +export const aggDiversifiedSampler = (): FunctionDefinition => ({ + name: aggDiversifiedSamplerFnName, + help: i18n.translate('data.search.aggs.function.buckets.diversifiedSampler.help', { + defaultMessage: 'Generates a serialized agg config for a Diversified sampler agg', + }), + type: 'agg_type', + args: { + id: { + types: ['string'], + help: i18n.translate('data.search.aggs.buckets.diversifiedSampler.id.help', { + defaultMessage: 'ID for this aggregation', + }), + }, + enabled: { + types: ['boolean'], + default: true, + help: i18n.translate('data.search.aggs.buckets.diversifiedSampler.enabled.help', { + defaultMessage: 'Specifies whether this aggregation should be enabled', + }), + }, + schema: { + types: ['string'], + help: i18n.translate('data.search.aggs.buckets.diversifiedSampler.schema.help', { + defaultMessage: 'Schema to use for this aggregation', + }), + }, + shard_size: { + types: ['number'], + help: i18n.translate('data.search.aggs.buckets.diversifiedSampler.shardSize.help', { + defaultMessage: + 'The shard_size parameter limits how many top-scoring documents are collected in the sample processed on each shard.', + }), + }, + max_docs_per_value: { + types: ['number'], + help: i18n.translate('data.search.aggs.buckets.diversifiedSampler.maxDocsPerValue.help', { + defaultMessage: + 'Limits how many documents are permitted per choice of de-duplicating value.', + }), + }, + field: { + types: ['string'], + help: i18n.translate('data.search.aggs.buckets.diversifiedSampler.field.help', { + defaultMessage: 'Used to provide values used for de-duplication.', + }), + }, + }, + fn: (input, args) => { + const { id, enabled, schema, ...rest } = args; + + return { + type: 'agg_type', + value: { + id, + enabled, + schema, + type: DIVERSIFIED_SAMPLER_AGG_NAME, + params: { + ...rest, + }, + }, + }; + }, +}); diff --git a/src/plugins/data/common/search/aggs/buckets/index.ts b/src/plugins/data/common/search/aggs/buckets/index.ts index 421fa0fcfdaf4..bf96a9ef860c0 100644 --- a/src/plugins/data/common/search/aggs/buckets/index.ts +++ b/src/plugins/data/common/search/aggs/buckets/index.ts @@ -38,3 +38,7 @@ export * from './terms_fn'; export * from './terms'; export * from './multi_terms_fn'; export * from './multi_terms'; +export * from './sampler_fn'; +export * from './sampler'; +export * from './diversified_sampler_fn'; +export * from './diversified_sampler'; diff --git a/src/plugins/data/common/search/aggs/buckets/multi_terms.ts b/src/plugins/data/common/search/aggs/buckets/multi_terms.ts index c320c7e242798..02bf6bd12d319 100644 --- a/src/plugins/data/common/search/aggs/buckets/multi_terms.ts +++ b/src/plugins/data/common/search/aggs/buckets/multi_terms.ts @@ -34,6 +34,7 @@ export interface AggParamsMultiTerms extends BaseAggParams { size?: number; otherBucket?: boolean; otherBucketLabel?: string; + separatorLabel?: string; } export const getMultiTermsBucketAgg = () => { @@ -83,6 +84,7 @@ export const getMultiTermsBucketAgg = () => { params: { otherBucketLabel: params.otherBucketLabel, paramsPerField: formats, + separator: agg.params.separatorLabel, }, }; }, @@ -142,6 +144,11 @@ export const getMultiTermsBucketAgg = () => { shouldShow: (agg) => agg.getParam('otherBucket'), write: noop, }, + { + name: 'separatorLabel', + type: 'string', + write: noop, + }, ], }); }; diff --git a/src/plugins/data/common/search/aggs/buckets/multi_terms_fn.ts b/src/plugins/data/common/search/aggs/buckets/multi_terms_fn.ts index 58e49479cd2c1..12b9c6d156548 100644 --- a/src/plugins/data/common/search/aggs/buckets/multi_terms_fn.ts +++ b/src/plugins/data/common/search/aggs/buckets/multi_terms_fn.ts @@ -111,6 +111,12 @@ export const aggMultiTerms = (): FunctionDefinition => ({ defaultMessage: 'Represents a custom label for this aggregation', }), }, + separatorLabel: { + types: ['string'], + help: i18n.translate('data.search.aggs.buckets.multiTerms.separatorLabel.help', { + defaultMessage: 'The separator label used to join each term combination', + }), + }, }, fn: (input, args) => { const { id, enabled, schema, ...rest } = args; diff --git a/src/plugins/data/common/search/aggs/buckets/sampler.ts b/src/plugins/data/common/search/aggs/buckets/sampler.ts new file mode 100644 index 0000000000000..7eb4f74115095 --- /dev/null +++ b/src/plugins/data/common/search/aggs/buckets/sampler.ts @@ -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 + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { i18n } from '@kbn/i18n'; +import { BucketAggType } from './bucket_agg_type'; +import { BaseAggParams } from '../types'; +import { aggSamplerFnName } from './sampler_fn'; + +export const SAMPLER_AGG_NAME = 'sampler'; + +const title = i18n.translate('data.search.aggs.buckets.samplerTitle', { + defaultMessage: 'Sampler', + description: 'Sampler aggregation title', +}); + +export interface AggParamsSampler extends BaseAggParams { + /** + * Limits how many top-scoring documents are collected in the sample processed on each shard. + */ + shard_size?: number; +} + +/** + * A filtering aggregation used to limit any sub aggregations' processing to a sample of the top-scoring documents. + */ +export const getSamplerBucketAgg = () => + new BucketAggType({ + name: SAMPLER_AGG_NAME, + title, + customLabels: false, + expressionName: aggSamplerFnName, + params: [ + { + name: 'shard_size', + type: 'number', + }, + ], + }); diff --git a/src/plugins/data/common/search/aggs/buckets/sampler_fn.test.ts b/src/plugins/data/common/search/aggs/buckets/sampler_fn.test.ts new file mode 100644 index 0000000000000..76ef901671e72 --- /dev/null +++ b/src/plugins/data/common/search/aggs/buckets/sampler_fn.test.ts @@ -0,0 +1,52 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { functionWrapper } from '../test_helpers'; +import { aggSampler } from './sampler_fn'; + +describe('aggSampler', () => { + const fn = functionWrapper(aggSampler()); + + test('fills in defaults when only required args are provided', () => { + const actual = fn({ id: 'sampler', schema: 'bucket' }); + expect(actual).toMatchInlineSnapshot(` + Object { + "type": "agg_type", + "value": Object { + "enabled": true, + "id": "sampler", + "params": Object { + "shard_size": undefined, + }, + "schema": "bucket", + "type": "sampler", + }, + } + `); + }); + + test('includes optional params when they are provided', () => { + const actual = fn({ + id: 'sampler', + schema: 'bucket', + shard_size: 300, + }); + + expect(actual.value).toMatchInlineSnapshot(` + Object { + "enabled": true, + "id": "sampler", + "params": Object { + "shard_size": 300, + }, + "schema": "bucket", + "type": "sampler", + } + `); + }); +}); diff --git a/src/plugins/data/common/search/aggs/buckets/sampler_fn.ts b/src/plugins/data/common/search/aggs/buckets/sampler_fn.ts new file mode 100644 index 0000000000000..2cb30eb70a230 --- /dev/null +++ b/src/plugins/data/common/search/aggs/buckets/sampler_fn.ts @@ -0,0 +1,77 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { i18n } from '@kbn/i18n'; +import { ExpressionFunctionDefinition } from 'src/plugins/expressions/common'; +import { AggExpressionFunctionArgs, AggExpressionType, BUCKET_TYPES } from '../'; +import { SAMPLER_AGG_NAME } from './sampler'; + +export const aggSamplerFnName = 'aggSampler'; + +type Input = any; +type Arguments = AggExpressionFunctionArgs; + +type Output = AggExpressionType; +type FunctionDefinition = ExpressionFunctionDefinition< + typeof aggSamplerFnName, + Input, + Arguments, + Output +>; + +export const aggSampler = (): FunctionDefinition => ({ + name: aggSamplerFnName, + help: i18n.translate('data.search.aggs.function.buckets.sampler.help', { + defaultMessage: 'Generates a serialized agg config for a Sampler agg', + }), + type: 'agg_type', + args: { + id: { + types: ['string'], + help: i18n.translate('data.search.aggs.buckets.sampler.id.help', { + defaultMessage: 'ID for this aggregation', + }), + }, + enabled: { + types: ['boolean'], + default: true, + help: i18n.translate('data.search.aggs.buckets.sampler.enabled.help', { + defaultMessage: 'Specifies whether this aggregation should be enabled', + }), + }, + schema: { + types: ['string'], + help: i18n.translate('data.search.aggs.buckets.sampler.schema.help', { + defaultMessage: 'Schema to use for this aggregation', + }), + }, + shard_size: { + types: ['number'], + help: i18n.translate('data.search.aggs.buckets.sampler.shardSize.help', { + defaultMessage: + 'The shard_size parameter limits how many top-scoring documents are collected in the sample processed on each shard.', + }), + }, + }, + fn: (input, args) => { + const { id, enabled, schema, ...rest } = args; + + return { + type: 'agg_type', + value: { + id, + enabled, + schema, + type: SAMPLER_AGG_NAME, + params: { + ...rest, + }, + }, + }; + }, +}); diff --git a/src/plugins/data/common/search/aggs/metrics/percentile_ranks.ts b/src/plugins/data/common/search/aggs/metrics/percentile_ranks.ts index fb142ee1f77c8..8f976ba979b95 100644 --- a/src/plugins/data/common/search/aggs/metrics/percentile_ranks.ts +++ b/src/plugins/data/common/search/aggs/metrics/percentile_ranks.ts @@ -13,7 +13,8 @@ import { AggTypesDependencies } from '../agg_types'; import { BaseAggParams } from '../types'; import { MetricAggType } from './metric_agg_type'; -import { getResponseAggConfigClass, IResponseAggConfig } from './lib/get_response_agg_config_class'; +import { getResponseAggConfigClass } from './lib/get_response_agg_config_class'; +import type { IResponseAggConfig } from './lib/get_response_agg_config_class'; import { aggPercentileRanksFnName } from './percentile_ranks_fn'; import { getPercentileValue } from './percentiles_get_value'; import { METRIC_TYPES } from './metric_agg_types'; diff --git a/src/plugins/data/common/search/aggs/metrics/percentiles.test.ts b/src/plugins/data/common/search/aggs/metrics/percentiles.test.ts index 26189e022e7c6..17c49e2484a80 100644 --- a/src/plugins/data/common/search/aggs/metrics/percentiles.test.ts +++ b/src/plugins/data/common/search/aggs/metrics/percentiles.test.ts @@ -10,7 +10,7 @@ import { IPercentileAggConfig, getPercentilesMetricAgg } from './percentiles'; import { AggConfigs, IAggConfigs } from '../agg_configs'; import { mockAggTypesRegistry } from '../test_helpers'; import { METRIC_TYPES } from './metric_agg_types'; -import { IResponseAggConfig } from './lib/get_response_agg_config_class'; +import type { IResponseAggConfig } from './lib/get_response_agg_config_class'; describe('AggTypesMetricsPercentilesProvider class', () => { let aggConfigs: IAggConfigs; diff --git a/src/plugins/data/common/search/aggs/metrics/percentiles.ts b/src/plugins/data/common/search/aggs/metrics/percentiles.ts index 07c4ac2bf2646..d0e1c6df77696 100644 --- a/src/plugins/data/common/search/aggs/metrics/percentiles.ts +++ b/src/plugins/data/common/search/aggs/metrics/percentiles.ts @@ -10,7 +10,8 @@ import { i18n } from '@kbn/i18n'; import { MetricAggType } from './metric_agg_type'; import { METRIC_TYPES } from './metric_agg_types'; import { KBN_FIELD_TYPES } from '../../../../common'; -import { getResponseAggConfigClass, IResponseAggConfig } from './lib/get_response_agg_config_class'; +import { getResponseAggConfigClass } from './lib/get_response_agg_config_class'; +import type { IResponseAggConfig } from './lib/get_response_agg_config_class'; import { aggPercentilesFnName } from './percentiles_fn'; import { getPercentileValue } from './percentiles_get_value'; import { ordinalSuffix } from './lib/ordinal_suffix'; diff --git a/src/plugins/data/common/search/aggs/metrics/percentiles_get_value.ts b/src/plugins/data/common/search/aggs/metrics/percentiles_get_value.ts index 90585909db42a..242a12da35128 100644 --- a/src/plugins/data/common/search/aggs/metrics/percentiles_get_value.ts +++ b/src/plugins/data/common/search/aggs/metrics/percentiles_get_value.ts @@ -7,7 +7,7 @@ */ import { find } from 'lodash'; -import { IResponseAggConfig } from './lib/get_response_agg_config_class'; +import type { IResponseAggConfig } from './lib/get_response_agg_config_class'; export const getPercentileValue = ( agg: TAggConfig, diff --git a/src/plugins/data/common/search/aggs/metrics/std_deviation.ts b/src/plugins/data/common/search/aggs/metrics/std_deviation.ts index fa160e5e9d161..9a4c38e296635 100644 --- a/src/plugins/data/common/search/aggs/metrics/std_deviation.ts +++ b/src/plugins/data/common/search/aggs/metrics/std_deviation.ts @@ -11,7 +11,8 @@ import { i18n } from '@kbn/i18n'; import { MetricAggType } from './metric_agg_type'; import { aggStdDeviationFnName } from './std_deviation_fn'; import { METRIC_TYPES } from './metric_agg_types'; -import { getResponseAggConfigClass, IResponseAggConfig } from './lib/get_response_agg_config_class'; +import { getResponseAggConfigClass } from './lib/get_response_agg_config_class'; +import type { IResponseAggConfig } from './lib/get_response_agg_config_class'; import { KBN_FIELD_TYPES } from '../../../../common'; import { BaseAggParams } from '../types'; diff --git a/src/plugins/data/common/search/aggs/types.ts b/src/plugins/data/common/search/aggs/types.ts index b9a977e0a8a09..74356263845d1 100644 --- a/src/plugins/data/common/search/aggs/types.ts +++ b/src/plugins/data/common/search/aggs/types.ts @@ -90,6 +90,8 @@ import { aggFilteredMetric, aggSinglePercentile, } from './'; +import { AggParamsSampler } from './buckets/sampler'; +import { AggParamsDiversifiedSampler } from './buckets/diversified_sampler'; export type { IAggConfig, AggConfigSerialized } from './agg_config'; export type { CreateAggConfigParams, IAggConfigs } from './agg_configs'; @@ -100,12 +102,10 @@ export type { IMetricAggType } from './metrics/metric_agg_type'; export type { IpRangeKey } from './buckets/lib/ip_range'; export type { OptionedValueProp } from './param_types/optioned'; -/** @internal */ export interface AggsCommonSetup { types: AggTypesRegistrySetup; } -/** @internal */ export interface AggsCommonStart { calculateAutoTimeExpression: ReturnType; datatableUtilities: { @@ -129,14 +129,12 @@ export interface AggsCommonStart { */ export type AggsStart = Assign; -/** @internal */ export interface BaseAggParams { json?: string; customLabel?: string; timeShift?: string; } -/** @internal */ export interface AggExpressionType { type: 'agg_type'; value: AggConfigSerialized; @@ -166,6 +164,8 @@ export interface AggParamsMapping { [BUCKET_TYPES.DATE_HISTOGRAM]: AggParamsDateHistogram; [BUCKET_TYPES.TERMS]: AggParamsTerms; [BUCKET_TYPES.MULTI_TERMS]: AggParamsMultiTerms; + [BUCKET_TYPES.SAMPLER]: AggParamsSampler; + [BUCKET_TYPES.DIVERSIFIED_SAMPLER]: AggParamsDiversifiedSampler; [METRIC_TYPES.AVG]: AggParamsAvg; [METRIC_TYPES.CARDINALITY]: AggParamsCardinality; [METRIC_TYPES.COUNT]: BaseAggParams; diff --git a/src/plugins/data/common/search/aggs/utils/get_aggs_formats.test.ts b/src/plugins/data/common/search/aggs/utils/get_aggs_formats.test.ts index 76112980c55fb..8510acf1572c7 100644 --- a/src/plugins/data/common/search/aggs/utils/get_aggs_formats.test.ts +++ b/src/plugins/data/common/search/aggs/utils/get_aggs_formats.test.ts @@ -13,6 +13,7 @@ import { IFieldFormat, SerializedFieldFormat, } from '../../../../../field_formats/common'; +import { MultiFieldKey } from '../buckets/multi_field_key'; import { getAggsFormats } from './get_aggs_formats'; const getAggFormat = ( @@ -119,4 +120,35 @@ describe('getAggsFormats', () => { expect(format.convert('__missing__')).toBe(mapping.params.missingBucketLabel); expect(getFormat).toHaveBeenCalledTimes(3); }); + + test('uses a default separator for multi terms', () => { + const terms = ['source', 'geo.src', 'geo.dest']; + const mapping = { + id: 'multi_terms', + params: { + paramsPerField: Array(terms.length).fill({ id: 'terms' }), + }, + }; + + const format = getAggFormat(mapping, getFormat); + + expect(format.convert(new MultiFieldKey({ key: terms }))).toBe('source › geo.src › geo.dest'); + expect(getFormat).toHaveBeenCalledTimes(terms.length); + }); + + test('uses a custom separator for multi terms when passed', () => { + const terms = ['source', 'geo.src', 'geo.dest']; + const mapping = { + id: 'multi_terms', + params: { + paramsPerField: Array(terms.length).fill({ id: 'terms' }), + separator: ' - ', + }, + }; + + const format = getAggFormat(mapping, getFormat); + + expect(format.convert(new MultiFieldKey({ key: terms }))).toBe('source - geo.src - geo.dest'); + expect(getFormat).toHaveBeenCalledTimes(terms.length); + }); }); diff --git a/src/plugins/data/common/search/aggs/utils/get_aggs_formats.ts b/src/plugins/data/common/search/aggs/utils/get_aggs_formats.ts index aade8bc70e4ee..f14f981fdec65 100644 --- a/src/plugins/data/common/search/aggs/utils/get_aggs_formats.ts +++ b/src/plugins/data/common/search/aggs/utils/get_aggs_formats.ts @@ -143,9 +143,11 @@ export function getAggsFormats(getFieldFormat: GetFieldFormat): FieldFormatInsta return params.otherBucketLabel; } + const joinTemplate = params.separator ?? ' › '; + return (val as MultiFieldKey).keys .map((valPart, i) => formats[i].convert(valPart, type)) - .join(' › '); + .join(joinTemplate); }; getConverterFor = (type: FieldFormatsContentType) => (val: string) => this.convert(val, type); }, diff --git a/src/plugins/data/common/search/expressions/esaggs/request_handler.test.ts b/src/plugins/data/common/search/expressions/esaggs/request_handler.test.ts index a44613cb98b50..eefaf8a9dcd54 100644 --- a/src/plugins/data/common/search/expressions/esaggs/request_handler.test.ts +++ b/src/plugins/data/common/search/expressions/esaggs/request_handler.test.ts @@ -14,7 +14,7 @@ import type { IAggConfigs } from '../../aggs'; import type { ISearchSource } from '../../search_source'; import { searchSourceCommonMock, searchSourceInstanceMock } from '../../search_source/mocks'; -import { handleRequest, RequestHandlerParams } from './request_handler'; +import { handleRequest } from './request_handler'; jest.mock('../../tabify', () => ({ tabifyAggResponse: jest.fn(), @@ -25,7 +25,7 @@ import { of } from 'rxjs'; import { toArray } from 'rxjs/operators'; describe('esaggs expression function - public', () => { - let mockParams: MockedKeys; + let mockParams: MockedKeys[0]>; beforeEach(() => { jest.clearAllMocks(); diff --git a/src/plugins/data/common/search/expressions/esaggs/request_handler.ts b/src/plugins/data/common/search/expressions/esaggs/request_handler.ts index 87c1685c9730d..d395baed2f08e 100644 --- a/src/plugins/data/common/search/expressions/esaggs/request_handler.ts +++ b/src/plugins/data/common/search/expressions/esaggs/request_handler.ts @@ -17,8 +17,7 @@ import { IAggConfigs } from '../../aggs'; import { ISearchStartSearchSource } from '../../search_source'; import { tabifyAggResponse } from '../../tabify'; -/** @internal */ -export interface RequestHandlerParams { +interface RequestHandlerParams { abortSignal?: AbortSignal; aggs: IAggConfigs; filters?: Filter[]; diff --git a/src/plugins/data/common/search/expressions/esdsl.ts b/src/plugins/data/common/search/expressions/esdsl.ts index faa43dab65657..69e3c54e43806 100644 --- a/src/plugins/data/common/search/expressions/esdsl.ts +++ b/src/plugins/data/common/search/expressions/esdsl.ts @@ -34,8 +34,7 @@ export type EsdslExpressionFunctionDefinition = ExpressionFunctionDefinition< Output >; -/** @internal */ -export interface EsdslStartDependencies { +interface EsdslStartDependencies { search: ISearchGeneric; uiSettingsClient: UiSettingsCommon; } diff --git a/src/plugins/data/common/search/expressions/kibana_context.ts b/src/plugins/data/common/search/expressions/kibana_context.ts index 47ca24b5be42b..6e38e2a3949d5 100644 --- a/src/plugins/data/common/search/expressions/kibana_context.ts +++ b/src/plugins/data/common/search/expressions/kibana_context.ts @@ -19,7 +19,6 @@ import { KibanaTimerangeOutput } from './timerange'; import { SavedObjectReference } from '../../../../../core/types'; import { SavedObjectsClientCommon } from '../..'; -/** @internal */ export interface KibanaContextStartDependencies { savedObjectsClient: SavedObjectsClientCommon; } diff --git a/src/plugins/data/common/search/search_source/extract_references.ts b/src/plugins/data/common/search/search_source/extract_references.ts index de32836ced124..954d336cb8a92 100644 --- a/src/plugins/data/common/search/search_source/extract_references.ts +++ b/src/plugins/data/common/search/search_source/extract_references.ts @@ -14,7 +14,7 @@ import { DATA_VIEW_SAVED_OBJECT_TYPE } from '../../../../data/common'; export const extractReferences = ( state: SerializedSearchSourceFields -): [SerializedSearchSourceFields & { indexRefName?: string }, SavedObjectReference[]] => { +): [SerializedSearchSourceFields, SavedObjectReference[]] => { let searchSourceFields: SerializedSearchSourceFields & { indexRefName?: string } = { ...state }; const references: SavedObjectReference[] = []; if (searchSourceFields.index) { diff --git a/src/plugins/data/common/search/search_source/fetch/get_search_params.ts b/src/plugins/data/common/search/search_source/fetch/get_search_params.ts index 28ee7993c175c..ae01dcf4ea051 100644 --- a/src/plugins/data/common/search/search_source/fetch/get_search_params.ts +++ b/src/plugins/data/common/search/search_source/fetch/get_search_params.ts @@ -9,7 +9,7 @@ import { UI_SETTINGS } from '../../../constants'; import { GetConfigFn } from '../../../types'; import { ISearchRequestParams } from '../../index'; -import { SearchRequest } from './types'; +import type { SearchRequest } from './types'; const sessionId = Date.now(); diff --git a/src/plugins/data/common/search/search_source/mocks.ts b/src/plugins/data/common/search/search_source/mocks.ts index dee5c09a6b858..77ba2a761fbf0 100644 --- a/src/plugins/data/common/search/search_source/mocks.ts +++ b/src/plugins/data/common/search/search_source/mocks.ts @@ -40,6 +40,10 @@ export const searchSourceInstanceMock: MockedKeys = { export const searchSourceCommonMock: jest.Mocked = { create: jest.fn().mockReturnValue(searchSourceInstanceMock), createEmpty: jest.fn().mockReturnValue(searchSourceInstanceMock), + telemetry: jest.fn(), + getAllMigrations: jest.fn(), + inject: jest.fn(), + extract: jest.fn(), }; export const createSearchSourceMock = (fields?: SearchSourceFields, response?: any) => diff --git a/src/plugins/data/common/search/search_source/search_source.ts b/src/plugins/data/common/search/search_source/search_source.ts index 3ac6b623fbc80..8acdb0514cccb 100644 --- a/src/plugins/data/common/search/search_source/search_source.ts +++ b/src/plugins/data/common/search/search_source/search_source.ts @@ -95,7 +95,8 @@ import type { SearchSourceFields, SearchSourceOptions, } from './types'; -import { FetchHandlers, getSearchParamsFromRequest, RequestFailure, SearchRequest } from './fetch'; +import { getSearchParamsFromRequest, RequestFailure } from './fetch'; +import type { FetchHandlers, SearchRequest } from './fetch'; import { getRequestInspectorStats, getResponseInspectorStats } from './inspect'; import { diff --git a/src/plugins/data/common/search/search_source/search_source_service.test.ts b/src/plugins/data/common/search/search_source/search_source_service.test.ts index dc63b96d5258d..a1b49fc433925 100644 --- a/src/plugins/data/common/search/search_source/search_source_service.test.ts +++ b/src/plugins/data/common/search/search_source/search_source_service.test.ts @@ -28,7 +28,14 @@ describe('SearchSource service', () => { dependencies ); - expect(Object.keys(start)).toEqual(['create', 'createEmpty']); + expect(Object.keys(start)).toEqual([ + 'create', + 'createEmpty', + 'extract', + 'inject', + 'getAllMigrations', + 'telemetry', + ]); }); }); }); diff --git a/src/plugins/data/common/search/search_source/search_source_service.ts b/src/plugins/data/common/search/search_source/search_source_service.ts index 886420365f548..a97596d322ccd 100644 --- a/src/plugins/data/common/search/search_source/search_source_service.ts +++ b/src/plugins/data/common/search/search_source/search_source_service.ts @@ -6,8 +6,18 @@ * Side Public License, v 1. */ -import { createSearchSource, SearchSource, SearchSourceDependencies } from './'; +import { mapValues } from 'lodash'; +import { + createSearchSource, + extractReferences, + injectReferences, + SearchSource, + SearchSourceDependencies, + SerializedSearchSourceFields, +} from './'; import { IndexPatternsContract } from '../..'; +import { mergeMigrationFunctionMaps } from '../../../../kibana_utils/common'; +import { getAllMigrations as filtersGetAllMigrations } from '../../query/persistable_state'; export class SearchSourceService { public setup() {} @@ -24,6 +34,28 @@ export class SearchSourceService { createEmpty: () => { return new SearchSource({}, dependencies); }, + extract: (state: SerializedSearchSourceFields) => { + const [newState, references] = extractReferences(state); + return { state: newState, references }; + }, + inject: injectReferences, + getAllMigrations: () => { + const searchSourceMigrations = {}; + + // we don't know if embeddables have any migrations defined so we need to fetch them and map the received functions so we pass + // them the correct input and that we correctly map the response + const filterMigrations = mapValues(filtersGetAllMigrations(), (migrate) => { + return (state: SerializedSearchSourceFields) => ({ + ...state, + filter: migrate(state.filter), + }); + }); + + return mergeMigrationFunctionMaps(searchSourceMigrations, filterMigrations); + }, + telemetry: () => { + return {}; + }, }; } diff --git a/src/plugins/data/common/search/search_source/types.ts b/src/plugins/data/common/search/search_source/types.ts index acfdf17263169..94697ba9521e9 100644 --- a/src/plugins/data/common/search/search_source/types.ts +++ b/src/plugins/data/common/search/search_source/types.ts @@ -12,7 +12,8 @@ import { SerializableRecord } from '@kbn/utility-types'; import { Query } from '../..'; import { Filter } from '../../es_query'; import { IndexPattern } from '../..'; -import { SearchSource } from './search_source'; +import type { SearchSource } from './search_source'; +import { PersistableStateService } from '../../../../kibana_utils/common'; /** * search source interface @@ -24,7 +25,8 @@ export type ISearchSource = Pick; * high level search service * @public */ -export interface ISearchStartSearchSource { +export interface ISearchStartSearchSource + extends PersistableStateService { /** * creates {@link SearchSource} based on provided serialized {@link SearchSourceFields} * @param fields @@ -43,15 +45,17 @@ export enum SortDirection { desc = 'desc', } -export interface SortDirectionFormat { +// eslint-disable-next-line @typescript-eslint/consistent-type-definitions +export type SortDirectionFormat = { order: SortDirection; format?: string; -} +}; -export interface SortDirectionNumeric { +// eslint-disable-next-line @typescript-eslint/consistent-type-definitions +export type SortDirectionNumeric = { order: SortDirection; numeric_type?: 'double' | 'long' | 'date' | 'date_nanos'; -} +}; export type EsQuerySortValue = Record< string, @@ -114,7 +118,8 @@ export interface SearchSourceFields { parent?: SearchSourceFields; } -export interface SerializedSearchSourceFields { +// eslint-disable-next-line @typescript-eslint/consistent-type-definitions +export type SerializedSearchSourceFields = { type?: string; /** * {@link Query} @@ -159,7 +164,7 @@ export interface SerializedSearchSourceFields { terminate_after?: number; parent?: SerializedSearchSourceFields; -} +}; export interface SearchSourceOptions { callParentStartHandlers?: boolean; diff --git a/src/plugins/data/common/search/tabify/get_columns.test.ts b/src/plugins/data/common/search/tabify/get_columns.test.ts index d679b3fb36311..1741abfe729d7 100644 --- a/src/plugins/data/common/search/tabify/get_columns.test.ts +++ b/src/plugins/data/common/search/tabify/get_columns.test.ts @@ -7,7 +7,7 @@ */ import { tabifyGetColumns } from './get_columns'; -import { TabbedAggColumn } from './types'; +import type { TabbedAggColumn } from './types'; import { AggConfigs } from '../aggs'; import { mockAggTypesRegistry } from '../aggs/test_helpers'; diff --git a/src/plugins/data/common/search/tabify/get_columns.ts b/src/plugins/data/common/search/tabify/get_columns.ts index 62798ba8bf680..8957c96a69881 100644 --- a/src/plugins/data/common/search/tabify/get_columns.ts +++ b/src/plugins/data/common/search/tabify/get_columns.ts @@ -8,7 +8,7 @@ import { groupBy } from 'lodash'; import { IAggConfig } from '../aggs'; -import { TabbedAggColumn } from './types'; +import type { TabbedAggColumn } from './types'; const getColumn = (agg: IAggConfig, i: number): TabbedAggColumn => { let name = ''; diff --git a/src/plugins/data/common/search/tabify/response_writer.test.ts b/src/plugins/data/common/search/tabify/response_writer.test.ts index cee297d255db3..ec131458b8510 100644 --- a/src/plugins/data/common/search/tabify/response_writer.test.ts +++ b/src/plugins/data/common/search/tabify/response_writer.test.ts @@ -9,7 +9,7 @@ import { TabbedAggResponseWriter } from './response_writer'; import { AggConfigs, BUCKET_TYPES, METRIC_TYPES } from '../aggs'; import { mockAggTypesRegistry } from '../aggs/test_helpers'; -import { TabbedResponseWriterOptions } from './types'; +import type { TabbedResponseWriterOptions } from './types'; describe('TabbedAggResponseWriter class', () => { let responseWriter: TabbedAggResponseWriter; diff --git a/src/plugins/data/common/search/tabify/tabify.ts b/src/plugins/data/common/search/tabify/tabify.ts index d3273accff974..5b1247a8f1719 100644 --- a/src/plugins/data/common/search/tabify/tabify.ts +++ b/src/plugins/data/common/search/tabify/tabify.ts @@ -9,7 +9,7 @@ import { get } from 'lodash'; import { TabbedAggResponseWriter } from './response_writer'; import { TabifyBuckets } from './buckets'; -import { TabbedResponseWriterOptions } from './types'; +import type { TabbedResponseWriterOptions } from './types'; import { AggResponseBucket } from './types'; import { AggGroupNames, IAggConfigs } from '../aggs'; diff --git a/src/plugins/data/common/search/tabify/tabify_docs.ts b/src/plugins/data/common/search/tabify/tabify_docs.ts index 43b6155f6662f..08172a918c042 100644 --- a/src/plugins/data/common/search/tabify/tabify_docs.ts +++ b/src/plugins/data/common/search/tabify/tabify_docs.ts @@ -48,7 +48,7 @@ function isValidMetaFieldName(field: string): field is ValidMetaFieldNames { return (VALID_META_FIELD_NAMES as string[]).includes(field); } -export interface TabifyDocsOptions { +interface TabifyDocsOptions { shallow?: boolean; /** * If set to `false` the _source of the document, if requested, won't be diff --git a/src/plugins/data/common/search/tabify/types.ts b/src/plugins/data/common/search/tabify/types.ts index 9fadb0ef860e3..bf0a99725e2ab 100644 --- a/src/plugins/data/common/search/tabify/types.ts +++ b/src/plugins/data/common/search/tabify/types.ts @@ -22,7 +22,6 @@ export interface TimeRangeInformation { timeFields: string[]; } -/** @internal **/ export interface TabbedResponseWriterOptions { metricsAtAllLevels: boolean; partialRows: boolean; diff --git a/src/plugins/data/public/actions/filters/create_filters_from_range_select.ts b/src/plugins/data/public/actions/filters/create_filters_from_range_select.ts index ea17e91d085e7..2ae1805c8aa28 100644 --- a/src/plugins/data/public/actions/filters/create_filters_from_range_select.ts +++ b/src/plugins/data/public/actions/filters/create_filters_from_range_select.ts @@ -13,8 +13,7 @@ import { esFilters, IFieldType, RangeFilterParams } from '../../../public'; import { getIndexPatterns, getSearchService } from '../../../public/services'; import { AggConfigSerialized } from '../../../common/search/aggs'; -/** @internal */ -export interface RangeSelectDataContext { +interface RangeSelectDataContext { table: Datatable; column: number; range: number[]; diff --git a/src/plugins/data/public/actions/filters/create_filters_from_value_click.test.ts b/src/plugins/data/public/actions/filters/create_filters_from_value_click.test.ts index e4854dac9408b..5163f979d3ff5 100644 --- a/src/plugins/data/public/actions/filters/create_filters_from_value_click.test.ts +++ b/src/plugins/data/public/actions/filters/create_filters_from_value_click.test.ts @@ -9,10 +9,7 @@ import { IndexPatternsContract } from '../../../public'; import { dataPluginMock } from '../../../public/mocks'; import { setIndexPatterns, setSearchService } from '../../../public/services'; -import { - createFiltersFromValueClickAction, - ValueClickDataContext, -} from './create_filters_from_value_click'; +import { createFiltersFromValueClickAction } from './create_filters_from_value_click'; import { FieldFormatsGetConfigFn, BytesFormat } from '../../../../field_formats/common'; import { RangeFilter } from '@kbn/es-query'; @@ -22,7 +19,7 @@ const mockField = { }; describe('createFiltersFromValueClick', () => { - let dataPoints: ValueClickDataContext['data']; + let dataPoints: Parameters[0]['data']; beforeEach(() => { dataPoints = [ diff --git a/src/plugins/data/public/actions/filters/create_filters_from_value_click.ts b/src/plugins/data/public/actions/filters/create_filters_from_value_click.ts index e1088b42e37b6..23ab718e512bd 100644 --- a/src/plugins/data/public/actions/filters/create_filters_from_value_click.ts +++ b/src/plugins/data/public/actions/filters/create_filters_from_value_click.ts @@ -12,8 +12,7 @@ import { esFilters, Filter } from '../../../public'; import { getIndexPatterns, getSearchService } from '../../../public/services'; import { AggConfigSerialized } from '../../../common/search/aggs'; -/** @internal */ -export interface ValueClickDataContext { +interface ValueClickDataContext { data: Array<{ table: Pick; column: number; diff --git a/src/plugins/data/public/autocomplete/autocomplete_service.ts b/src/plugins/data/public/autocomplete/autocomplete_service.ts index 67efbe2af29ce..0d21c7e765501 100644 --- a/src/plugins/data/public/autocomplete/autocomplete_service.ts +++ b/src/plugins/data/public/autocomplete/autocomplete_service.ts @@ -8,13 +8,13 @@ import { CoreSetup, PluginInitializerContext } from 'src/core/public'; import moment from 'moment'; -import { TimefilterSetup } from '../query'; +import type { TimefilterSetup } from '../query'; import { QuerySuggestionGetFn } from './providers/query_suggestion_provider'; import { getEmptyValueSuggestions, setupValueSuggestionProvider, - ValueSuggestionsGetFn, } from './providers/value_suggestion_provider'; +import type { ValueSuggestionsGetFn } from './providers/value_suggestion_provider'; import { ConfigSchema } from '../../config'; import { UsageCollectionSetup } from '../../../usage_collection/public'; diff --git a/src/plugins/data/public/autocomplete/providers/value_suggestion_provider.test.ts b/src/plugins/data/public/autocomplete/providers/value_suggestion_provider.test.ts index 7ecd371e39db7..4a68c7232ea7e 100644 --- a/src/plugins/data/public/autocomplete/providers/value_suggestion_provider.test.ts +++ b/src/plugins/data/public/autocomplete/providers/value_suggestion_provider.test.ts @@ -7,8 +7,9 @@ */ import { stubIndexPattern, stubFields } from '../../stubs'; -import { TimefilterSetup } from '../../query'; -import { setupValueSuggestionProvider, ValueSuggestionsGetFn } from './value_suggestion_provider'; +import type { TimefilterSetup } from '../../query'; +import { setupValueSuggestionProvider } from './value_suggestion_provider'; +import type { ValueSuggestionsGetFn } from './value_suggestion_provider'; import { IUiSettingsClient, CoreSetup } from 'kibana/public'; import { UI_SETTINGS } from '../../../common'; diff --git a/src/plugins/data/public/autocomplete/providers/value_suggestion_provider.ts b/src/plugins/data/public/autocomplete/providers/value_suggestion_provider.ts index 588bac4739c53..31f886daeb4cc 100644 --- a/src/plugins/data/public/autocomplete/providers/value_suggestion_provider.ts +++ b/src/plugins/data/public/autocomplete/providers/value_suggestion_provider.ts @@ -11,7 +11,7 @@ import { buildQueryFromFilters } from '@kbn/es-query'; import { memoize } from 'lodash'; import { CoreSetup } from 'src/core/public'; import { IIndexPattern, IFieldType, UI_SETTINGS, ValueSuggestionsMethod } from '../../../common'; -import { TimefilterSetup } from '../../query'; +import type { TimefilterSetup } from '../../query'; import { AutocompleteUsageCollector } from '../collectors'; export type ValueSuggestionsGetFn = (args: ValueSuggestionsGetFnArgs) => Promise; diff --git a/src/plugins/data/public/plugin.ts b/src/plugins/data/public/plugin.ts index 25f649f69a052..7d6983725b179 100644 --- a/src/plugins/data/public/plugin.ts +++ b/src/plugins/data/public/plugin.ts @@ -11,7 +11,7 @@ import './index.scss'; import { PluginInitializerContext, CoreSetup, CoreStart, Plugin } from 'src/core/public'; import { ConfigSchema } from '../config'; import { Storage, IStorageWrapper, createStartServicesGetter } from '../../kibana_utils/public'; -import { +import type { DataPublicPluginSetup, DataPublicPluginStart, DataSetupDependencies, diff --git a/src/plugins/data/public/query/filter_manager/filter_manager.ts b/src/plugins/data/public/query/filter_manager/filter_manager.ts index f076a2c591fb1..bfedf444cf23e 100644 --- a/src/plugins/data/public/query/filter_manager/filter_manager.ts +++ b/src/plugins/data/public/query/filter_manager/filter_manager.ts @@ -14,7 +14,6 @@ import { IUiSettingsClient } from 'src/core/public'; import { isFilterPinned, onlyDisabledFiltersChanged, Filter } from '@kbn/es-query'; import { sortFilters } from './lib/sort_filters'; import { mapAndFlattenFilters } from './lib/map_and_flatten_filters'; -import { PartitionedFilters } from './types'; import { FilterStateStore, @@ -31,6 +30,11 @@ import { telemetry, } from '../../../common/query/persistable_state'; +interface PartitionedFilters { + globalFilters: Filter[]; + appFilters: Filter[]; +} + export class FilterManager implements PersistableStateService { private filters: Filter[] = []; private updated$: Subject = new Subject(); diff --git a/src/plugins/data/public/query/lib/get_default_query.ts b/src/plugins/data/public/query/lib/get_default_query.ts index 015c128171a8e..fd571e46083f5 100644 --- a/src/plugins/data/public/query/lib/get_default_query.ts +++ b/src/plugins/data/public/query/lib/get_default_query.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -export type QueryLanguage = 'kuery' | 'lucene'; +type QueryLanguage = 'kuery' | 'lucene'; export function getDefaultQuery(language: QueryLanguage = 'kuery') { return { diff --git a/src/plugins/data/public/query/query_service.ts b/src/plugins/data/public/query/query_service.ts index 314f13e3524db..dc6b9586b0b4b 100644 --- a/src/plugins/data/public/query/query_service.ts +++ b/src/plugins/data/public/query/query_service.ts @@ -12,10 +12,12 @@ import { IStorageWrapper } from 'src/plugins/kibana_utils/public'; import { buildEsQuery } from '@kbn/es-query'; import { FilterManager } from './filter_manager'; import { createAddToQueryLog } from './lib'; -import { TimefilterService, TimefilterSetup } from './timefilter'; +import { TimefilterService } from './timefilter'; +import type { TimefilterSetup } from './timefilter'; import { createSavedQueryService } from './saved_query/saved_query_service'; import { createQueryStateObservable } from './state_sync/create_global_query_observable'; -import { QueryStringContract, QueryStringManager } from './query_string'; +import type { QueryStringContract } from './query_string'; +import { QueryStringManager } from './query_string'; import { getEsQueryConfig, TimeRange } from '../../common'; import { getUiSettings } from '../services'; import { NowProviderInternalContract } from '../now_provider'; diff --git a/src/plugins/data/public/query/query_string/query_string_manager.mock.ts b/src/plugins/data/public/query/query_string/query_string_manager.mock.ts index 976d3ce13e7de..6d20f2a4bea34 100644 --- a/src/plugins/data/public/query/query_string/query_string_manager.mock.ts +++ b/src/plugins/data/public/query/query_string/query_string_manager.mock.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -import { QueryStringContract } from '.'; +import type { QueryStringContract } from '.'; import { Observable } from 'rxjs'; const createSetupContractMock = () => { diff --git a/src/plugins/data/public/query/saved_query/saved_query_service.test.ts b/src/plugins/data/public/query/saved_query/saved_query_service.test.ts index 047051c302083..57af09a0ea824 100644 --- a/src/plugins/data/public/query/saved_query/saved_query_service.test.ts +++ b/src/plugins/data/public/query/saved_query/saved_query_service.test.ts @@ -8,7 +8,7 @@ import { createSavedQueryService } from './saved_query_service'; import { httpServiceMock } from '../../../../../core/public/mocks'; -import { SavedQueryAttributes } from '../../../common'; +import type { SavedQueryAttributes } from '../../../common'; const http = httpServiceMock.createStartContract(); diff --git a/src/plugins/data/public/query/saved_query/saved_query_service.ts b/src/plugins/data/public/query/saved_query/saved_query_service.ts index 17b47c78c7000..b5a21e2ac2095 100644 --- a/src/plugins/data/public/query/saved_query/saved_query_service.ts +++ b/src/plugins/data/public/query/saved_query/saved_query_service.ts @@ -8,7 +8,7 @@ import { HttpStart } from 'src/core/public'; import { SavedQuery } from './types'; -import { SavedQueryAttributes } from '../../../common'; +import type { SavedQueryAttributes } from '../../../common'; export const createSavedQueryService = (http: HttpStart) => { const createQuery = async (attributes: SavedQueryAttributes, { overwrite = false } = {}) => { diff --git a/src/plugins/data/public/query/state_sync/create_global_query_observable.ts b/src/plugins/data/public/query/state_sync/create_global_query_observable.ts index 3c94d6eb3c056..3577478154c31 100644 --- a/src/plugins/data/public/query/state_sync/create_global_query_observable.ts +++ b/src/plugins/data/public/query/state_sync/create_global_query_observable.ts @@ -9,12 +9,12 @@ import { Observable, Subscription } from 'rxjs'; import { map, tap } from 'rxjs/operators'; import { isFilterPinned } from '@kbn/es-query'; -import { TimefilterSetup } from '../timefilter'; +import type { TimefilterSetup } from '../timefilter'; import { FilterManager } from '../filter_manager'; import { QueryState, QueryStateChange } from './index'; import { createStateContainer } from '../../../../kibana_utils/public'; import { compareFilters, COMPARE_ALL_OPTIONS } from '../../../common'; -import { QueryStringContract } from '../query_string'; +import type { QueryStringContract } from '../query_string'; export function createQueryStateObservable({ timefilter: { timefilter }, diff --git a/src/plugins/data/public/query/timefilter/lib/diff_time_picker_vals.ts b/src/plugins/data/public/query/timefilter/lib/diff_time_picker_vals.ts index 2d815ea168f6b..9b50c8d93d496 100644 --- a/src/plugins/data/public/query/timefilter/lib/diff_time_picker_vals.ts +++ b/src/plugins/data/public/query/timefilter/lib/diff_time_picker_vals.ts @@ -9,7 +9,7 @@ import _ from 'lodash'; import { RefreshInterval } from '../../../../common'; -import { InputTimeRange } from '../types'; +import type { InputTimeRange } from '../types'; const valueOf = function (o: any) { if (o) return o.valueOf(); diff --git a/src/plugins/data/public/query/timefilter/timefilter.ts b/src/plugins/data/public/query/timefilter/timefilter.ts index f3520abb2f46e..e13e8b17a7f43 100644 --- a/src/plugins/data/public/query/timefilter/timefilter.ts +++ b/src/plugins/data/public/query/timefilter/timefilter.ts @@ -11,7 +11,7 @@ import { Subject, BehaviorSubject } from 'rxjs'; import moment from 'moment'; import { PublicMethodsOf } from '@kbn/utility-types'; import { areRefreshIntervalsDifferent, areTimeRangesDifferent } from './lib/diff_time_picker_vals'; -import { TimefilterConfig, InputTimeRange, TimeRangeBounds } from './types'; +import type { TimefilterConfig, InputTimeRange, TimeRangeBounds } from './types'; import { NowProviderInternalContract } from '../../now_provider'; import { calculateBounds, diff --git a/src/plugins/data/public/search/aggs/aggs_service.test.ts b/src/plugins/data/public/search/aggs/aggs_service.test.ts index 20e07360a68e5..c7df4354cc76b 100644 --- a/src/plugins/data/public/search/aggs/aggs_service.test.ts +++ b/src/plugins/data/public/search/aggs/aggs_service.test.ts @@ -53,7 +53,7 @@ describe('AggsService - public', () => { test('registers default agg types', () => { service.setup(setupDeps); const start = service.start(startDeps); - expect(start.types.getAll().buckets.length).toBe(12); + expect(start.types.getAll().buckets.length).toBe(14); expect(start.types.getAll().metrics.length).toBe(23); }); @@ -69,7 +69,7 @@ describe('AggsService - public', () => { ); const start = service.start(startDeps); - expect(start.types.getAll().buckets.length).toBe(13); + expect(start.types.getAll().buckets.length).toBe(15); expect(start.types.getAll().buckets.some(({ name }) => name === 'foo')).toBe(true); expect(start.types.getAll().metrics.length).toBe(24); expect(start.types.getAll().metrics.some(({ name }) => name === 'bar')).toBe(true); diff --git a/src/plugins/data/public/search/collectors/types.ts b/src/plugins/data/public/search/collectors/types.ts index 49c240d1ccb16..d0a2e61f45109 100644 --- a/src/plugins/data/public/search/collectors/types.ts +++ b/src/plugins/data/public/search/collectors/types.ts @@ -68,9 +68,6 @@ export enum SEARCH_EVENT_TYPE { SESSIONS_LIST_LOADED = 'sessionsListLoaded', } -/** - * @internal - */ export interface SearchUsageCollector { trackQueryTimedOut: () => Promise; trackSessionIndicatorTourLoading: () => Promise; diff --git a/src/plugins/data/public/search/errors/types.ts b/src/plugins/data/public/search/errors/types.ts index d541e53be78f9..8f18ab06fcd94 100644 --- a/src/plugins/data/public/search/errors/types.ts +++ b/src/plugins/data/public/search/errors/types.ts @@ -32,7 +32,7 @@ export interface Reason { }; } -export interface IEsErrorAttributes { +interface IEsErrorAttributes { type: string; reason: string; root_cause?: Reason[]; diff --git a/src/plugins/data/public/search/fetch/handle_response.tsx b/src/plugins/data/public/search/fetch/handle_response.tsx index 9e68209af2b92..10b2f69a2a320 100644 --- a/src/plugins/data/public/search/fetch/handle_response.tsx +++ b/src/plugins/data/public/search/fetch/handle_response.tsx @@ -13,7 +13,7 @@ import { IKibanaSearchResponse } from 'src/plugins/data/common'; import { ShardFailureOpenModalButton } from '../../ui/shard_failure_modal'; import { toMountPoint } from '../../../../kibana_react/public'; import { getNotifications } from '../../services'; -import { SearchRequest } from '..'; +import type { SearchRequest } from '..'; export function handleResponse(request: SearchRequest, response: IKibanaSearchResponse) { const { rawResponse } = response; diff --git a/src/plugins/data/public/search/mocks.ts b/src/plugins/data/public/search/mocks.ts index 562b367b92c92..b82e0776777c5 100644 --- a/src/plugins/data/public/search/mocks.ts +++ b/src/plugins/data/public/search/mocks.ts @@ -8,7 +8,7 @@ import { searchAggsSetupMock, searchAggsStartMock } from './aggs/mocks'; import { searchSourceMock } from './search_source/mocks'; -import { ISearchSetup, ISearchStart } from './types'; +import type { ISearchSetup, ISearchStart } from './types'; import { getSessionsClientMock, getSessionServiceMock } from './session/mocks'; import { createSearchUsageCollectorMock } from './collectors/mocks'; diff --git a/src/plugins/data/public/search/search_service.ts b/src/plugins/data/public/search/search_service.ts index ecc0e84917251..76aae8582287d 100644 --- a/src/plugins/data/public/search/search_service.ts +++ b/src/plugins/data/public/search/search_service.ts @@ -15,7 +15,7 @@ import { } from 'src/core/public'; import { BehaviorSubject } from 'rxjs'; import { BfetchPublicSetup } from 'src/plugins/bfetch/public'; -import { ISearchSetup, ISearchStart } from './types'; +import type { ISearchSetup, ISearchStart } from './types'; import { handleResponse } from './fetch'; import { diff --git a/src/plugins/data/public/search/search_source/mocks.ts b/src/plugins/data/public/search/search_source/mocks.ts index 75ab8dbac7d2d..169ac4b84a505 100644 --- a/src/plugins/data/public/search/search_source/mocks.ts +++ b/src/plugins/data/public/search/search_source/mocks.ts @@ -7,7 +7,7 @@ */ import { searchSourceCommonMock } from '../../../common/search/search_source/mocks'; -import { ISearchStart } from '../types'; +import type { ISearchStart } from '../types'; function createStartContract(): jest.Mocked { return searchSourceCommonMock; diff --git a/src/plugins/data/public/search/session/mocks.ts b/src/plugins/data/public/search/session/mocks.ts index dee0216530205..c6706ff8cf72d 100644 --- a/src/plugins/data/public/search/session/mocks.ts +++ b/src/plugins/data/public/search/session/mocks.ts @@ -9,7 +9,8 @@ import { BehaviorSubject } from 'rxjs'; import { ISessionsClient } from './sessions_client'; import { ISessionService } from './session_service'; -import { SearchSessionState, SessionMeta } from './search_session_state'; +import { SearchSessionState } from './search_session_state'; +import type { SessionMeta } from './search_session_state'; export function getSessionsClientMock(): jest.Mocked { return { diff --git a/src/plugins/data/public/search/session/search_session_state.test.ts b/src/plugins/data/public/search/session/search_session_state.test.ts index ef18275da12fa..1137ceddb0da6 100644 --- a/src/plugins/data/public/search/session/search_session_state.test.ts +++ b/src/plugins/data/public/search/session/search_session_state.test.ts @@ -7,7 +7,7 @@ */ import { createSessionStateContainer, SearchSessionState } from './search_session_state'; -import { SearchSessionSavedObject } from './sessions_client'; +import type { SearchSessionSavedObject } from './sessions_client'; import { SearchSessionStatus } from '../../../common'; const mockSavedObject: SearchSessionSavedObject = { diff --git a/src/plugins/data/public/search/session/search_session_state.ts b/src/plugins/data/public/search/session/search_session_state.ts index 73c75d046da96..c714a3e387641 100644 --- a/src/plugins/data/public/search/session/search_session_state.ts +++ b/src/plugins/data/public/search/session/search_session_state.ts @@ -11,7 +11,7 @@ import deepEqual from 'fast-deep-equal'; import { Observable } from 'rxjs'; import { distinctUntilChanged, map, shareReplay } from 'rxjs/operators'; import { createStateContainer, StateContainer } from '../../../../kibana_utils/public'; -import { SearchSessionSavedObject } from './sessions_client'; +import type { SearchSessionSavedObject } from './sessions_client'; /** * Possible state that current session can be in diff --git a/src/plugins/data/public/search/session/session_service.test.ts b/src/plugins/data/public/search/session/session_service.test.ts index 4a11cdb38bb7d..ad131fbea60b2 100644 --- a/src/plugins/data/public/search/session/session_service.test.ts +++ b/src/plugins/data/public/search/session/session_service.test.ts @@ -15,7 +15,7 @@ import { SearchSessionState } from './search_session_state'; import { createNowProviderMock } from '../../now_provider/mocks'; import { NowProviderInternalContract } from '../../now_provider'; import { SEARCH_SESSIONS_MANAGEMENT_ID } from './constants'; -import { SearchSessionSavedObject, ISessionsClient } from './sessions_client'; +import type { SearchSessionSavedObject, ISessionsClient } from './sessions_client'; import { SearchSessionStatus } from '../../../common'; import { CoreStart } from 'kibana/public'; diff --git a/src/plugins/data/public/search/session/session_service.ts b/src/plugins/data/public/search/session/session_service.ts index 360e8808c186d..9a02e336ecf86 100644 --- a/src/plugins/data/public/search/session/session_service.ts +++ b/src/plugins/data/public/search/session/session_service.ts @@ -16,8 +16,8 @@ import { } from 'kibana/public'; import { i18n } from '@kbn/i18n'; import { ConfigSchema } from '../../../config'; -import { - createSessionStateContainer, +import { createSessionStateContainer } from './search_session_state'; +import type { SearchSessionState, SessionMeta, SessionStateContainer, @@ -31,7 +31,7 @@ import { formatSessionName } from './lib/session_name_formatter'; export type ISessionService = PublicContract; -export interface TrackSearchDescriptor { +interface TrackSearchDescriptor { abort: () => void; } @@ -66,7 +66,7 @@ export interface SearchSessionInfoProvider

; const LazyFilterLabel = React.lazy(() => import('./filter_editor/lib/filter_label')); -export const FilterLabel = (props: FilterLabelProps) => ( +export const FilterLabel = (props: React.ComponentProps) => ( }> ); -import type { FilterItemProps } from './filter_item'; - const LazyFilterItem = React.lazy(() => import('./filter_item')); -export const FilterItem = (props: FilterItemProps) => ( +export const FilterItem = (props: React.ComponentProps) => ( }> diff --git a/src/plugins/data/public/ui/query_string_input/query_bar_top_row.tsx b/src/plugins/data/public/ui/query_string_input/query_bar_top_row.tsx index a78055f0d61a1..a5f59b976d3ba 100644 --- a/src/plugins/data/public/ui/query_string_input/query_bar_top_row.tsx +++ b/src/plugins/data/public/ui/query_string_input/query_bar_top_row.tsx @@ -24,7 +24,8 @@ import { IDataPluginServices, IIndexPattern, TimeRange, TimeHistoryContract, Que import { useKibana, withKibana } from '../../../../kibana_react/public'; import QueryStringInputUI from './query_string_input'; import { UI_SETTINGS } from '../../../common'; -import { PersistedLog, getQueryLog } from '../../query'; +import { getQueryLog } from '../../query'; +import type { PersistedLog } from '../../query'; import { NoDataPopover } from './no_data_popover'; const QueryStringInput = withKibana(QueryStringInputUI); diff --git a/src/plugins/data/public/ui/query_string_input/query_string_input.tsx b/src/plugins/data/public/ui/query_string_input/query_string_input.tsx index 5d3e359ca5fc5..2e150b2c1e1bc 100644 --- a/src/plugins/data/public/ui/query_string_input/query_string_input.tsx +++ b/src/plugins/data/public/ui/query_string_input/query_string_input.tsx @@ -34,8 +34,9 @@ import { QuerySuggestion, QuerySuggestionTypes } from '../../autocomplete'; import { KibanaReactContextValue, toMountPoint } from '../../../../kibana_react/public'; import { fetchIndexPatterns } from './fetch_index_patterns'; import { QueryLanguageSwitcher } from './language_switcher'; -import { PersistedLog, getQueryLog, matchPairs, toUser, fromUser } from '../../query'; -import { SuggestionsListSize } from '../typeahead/suggestions_component'; +import { getQueryLog, matchPairs, toUser, fromUser } from '../../query'; +import type { PersistedLog } from '../../query'; +import type { SuggestionsListSize } from '../typeahead/suggestions_component'; import { SuggestionsComponent } from '..'; import { KIBANA_USER_QUERY_LANGUAGE_KEY, getFieldSubtypeNested } from '../../../common'; diff --git a/src/plugins/data/public/ui/search_bar/create_search_bar.tsx b/src/plugins/data/public/ui/search_bar/create_search_bar.tsx index 7b7538441c38f..fda6a74e4b500 100644 --- a/src/plugins/data/public/ui/search_bar/create_search_bar.tsx +++ b/src/plugins/data/public/ui/search_bar/create_search_bar.tsx @@ -12,7 +12,8 @@ import { CoreStart } from 'src/core/public'; import { IStorageWrapper } from 'src/plugins/kibana_utils/public'; import { KibanaContextProvider } from '../../../../kibana_react/public'; import { QueryStart, SavedQuery } from '../../query'; -import { SearchBar, SearchBarOwnProps } from './'; +import { SearchBar } from '.'; +import type { SearchBarOwnProps } from '.'; import { useFilterManager } from './lib/use_filter_manager'; import { useTimefilter } from './lib/use_timefilter'; import { useSavedQuery } from './lib/use_saved_query'; diff --git a/src/plugins/data/public/ui/search_bar/lib/use_query_string_manager.ts b/src/plugins/data/public/ui/search_bar/lib/use_query_string_manager.ts index 508ae711f52a9..713020f249ae3 100644 --- a/src/plugins/data/public/ui/search_bar/lib/use_query_string_manager.ts +++ b/src/plugins/data/public/ui/search_bar/lib/use_query_string_manager.ts @@ -9,7 +9,7 @@ import { useState, useEffect } from 'react'; import { Subscription } from 'rxjs'; import { Query } from '../../..'; -import { QueryStringContract } from '../../../query/query_string'; +import type { QueryStringContract } from '../../../query/query_string'; interface UseQueryStringProps { query?: Query; diff --git a/src/plugins/data/public/ui/search_bar/search_bar.tsx b/src/plugins/data/public/ui/search_bar/search_bar.tsx index 385f052adece6..3fef455be41c3 100644 --- a/src/plugins/data/public/ui/search_bar/search_bar.tsx +++ b/src/plugins/data/public/ui/search_bar/search_bar.tsx @@ -18,7 +18,7 @@ import { Query, Filter } from '@kbn/es-query'; import { withKibana, KibanaReactContextValue } from '../../../../kibana_react/public'; import QueryBarTopRow from '../query_string_input/query_bar_top_row'; -import { SavedQueryAttributes, TimeHistoryContract, SavedQuery } from '../../query'; +import type { SavedQueryAttributes, TimeHistoryContract, SavedQuery } from '../../query'; import { IDataPluginServices } from '../../types'; import { TimeRange, IIndexPattern } from '../../../common'; import { FilterBar } from '../filter_bar/filter_bar'; diff --git a/src/plugins/data/public/ui/typeahead/index.tsx b/src/plugins/data/public/ui/typeahead/index.tsx index 103580875151b..fb565d2711f64 100644 --- a/src/plugins/data/public/ui/typeahead/index.tsx +++ b/src/plugins/data/public/ui/typeahead/index.tsx @@ -7,12 +7,13 @@ */ import React from 'react'; -import type { SuggestionsComponentProps } from './suggestions_component'; const Fallback = () =>
; const LazySuggestionsComponent = React.lazy(() => import('./suggestions_component')); -export const SuggestionsComponent = (props: SuggestionsComponentProps) => ( +export const SuggestionsComponent = ( + props: React.ComponentProps +) => ( }> diff --git a/src/plugins/data/public/ui/typeahead/suggestions_component.tsx b/src/plugins/data/public/ui/typeahead/suggestions_component.tsx index 6bc91619fe868..f7d6e2c3d6403 100644 --- a/src/plugins/data/public/ui/typeahead/suggestions_component.tsx +++ b/src/plugins/data/public/ui/typeahead/suggestions_component.tsx @@ -19,8 +19,7 @@ import { } from './constants'; import { SuggestionOnClick } from './types'; -// @internal -export interface SuggestionsComponentProps { +interface SuggestionsComponentProps { index: number | null; onClick: SuggestionOnClick; onMouseEnter: (index: number) => void; diff --git a/src/plugins/data/public/utils/table_inspector_view/components/__snapshots__/data_view.test.tsx.snap b/src/plugins/data/public/utils/table_inspector_view/components/__snapshots__/data_view.test.tsx.snap index b59756ef1e90e..3262ee70dff86 100644 --- a/src/plugins/data/public/utils/table_inspector_view/components/__snapshots__/data_view.test.tsx.snap +++ b/src/plugins/data/public/utils/table_inspector_view/components/__snapshots__/data_view.test.tsx.snap @@ -173,56 +173,74 @@ exports[`Inspector Data View component should render empty state 1`] = ` } > -
- -

- - No data available - -

-
- - -
- - -
-

- +

- The element did not provide any data. - -

+ + No data available + +

+ + + + +
+ + +
+

+ + The element did not provide any data. + +

+
+
+ +
- -
-
-
+
+
+
+ diff --git a/src/plugins/data/server/config_deprecations.test.ts b/src/plugins/data/server/config_deprecations.test.ts index 6c09b060aa763..3df1ea9119292 100644 --- a/src/plugins/data/server/config_deprecations.test.ts +++ b/src/plugins/data/server/config_deprecations.test.ts @@ -50,7 +50,7 @@ describe('Config Deprecations', () => { }, }; const { messages, migrated } = applyConfigDeprecations(cloneDeep(config)); - expect(migrated.kibana.autocompleteTerminateAfter).not.toBeDefined(); + expect(migrated.kibana?.autocompleteTerminateAfter).not.toBeDefined(); expect(migrated.data.autocomplete.valueSuggestions.terminateAfter).toEqual(123); expect(messages).toMatchInlineSnapshot(` Array [ @@ -66,7 +66,7 @@ describe('Config Deprecations', () => { }, }; const { messages, migrated } = applyConfigDeprecations(cloneDeep(config)); - expect(migrated.kibana.autocompleteTimeout).not.toBeDefined(); + expect(migrated.kibana?.autocompleteTimeout).not.toBeDefined(); expect(migrated.data.autocomplete.valueSuggestions.timeout).toEqual(123); expect(messages).toMatchInlineSnapshot(` Array [ diff --git a/src/plugins/data/server/plugin.ts b/src/plugins/data/server/plugin.ts index cb52500e78f94..74b4edde21ae0 100644 --- a/src/plugins/data/server/plugin.ts +++ b/src/plugins/data/server/plugin.ts @@ -11,7 +11,7 @@ import { ExpressionsServerSetup } from 'src/plugins/expressions/server'; import { BfetchServerSetup } from 'src/plugins/bfetch/server'; import { PluginStart as DataViewsServerPluginStart } from 'src/plugins/data_views/server'; import { ConfigSchema } from '../config'; -import { ISearchSetup, ISearchStart, SearchEnhancements } from './search'; +import type { ISearchSetup, ISearchStart, SearchEnhancements } from './search'; import { SearchService } from './search/search_service'; import { QueryService } from './query/query_service'; import { ScriptsService } from './scripts'; @@ -21,7 +21,7 @@ import { AutocompleteService } from './autocomplete'; import { FieldFormatsSetup, FieldFormatsStart } from '../../field_formats/server'; import { getUiSettings } from './ui_settings'; -export interface DataEnhancements { +interface DataEnhancements { search: SearchEnhancements; } diff --git a/src/plugins/data/server/query/route_handler_context.test.ts b/src/plugins/data/server/query/route_handler_context.test.ts index cc7686a06cb67..f8c14d59e0f85 100644 --- a/src/plugins/data/server/query/route_handler_context.test.ts +++ b/src/plugins/data/server/query/route_handler_context.test.ts @@ -7,12 +7,8 @@ */ import { coreMock } from '../../../../core/server/mocks'; -import { - DATA_VIEW_SAVED_OBJECT_TYPE, - FilterStateStore, - SavedObject, - SavedQueryAttributes, -} from '../../common'; +import { DATA_VIEW_SAVED_OBJECT_TYPE, FilterStateStore } from '../../common'; +import type { SavedObject, SavedQueryAttributes } from '../../common'; import { registerSavedQueryRouteHandlerContext } from './route_handler_context'; import { SavedObjectsFindResponse, SavedObjectsUpdateResponse } from 'kibana/server'; diff --git a/src/plugins/data/server/search/mocks.ts b/src/plugins/data/server/search/mocks.ts index f358cd78d8f90..bca01c6a15d55 100644 --- a/src/plugins/data/server/search/mocks.ts +++ b/src/plugins/data/server/search/mocks.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -import { ISearchSetup, ISearchStart } from './types'; +import type { ISearchSetup, ISearchStart } from './types'; import { searchAggsSetupMock, searchAggsStartMock } from './aggs/mocks'; import { searchSourceMock } from './search_source/mocks'; diff --git a/src/plugins/data/server/search/routes/bsearch.ts b/src/plugins/data/server/search/routes/bsearch.ts index 9a15f84687f43..314de4254851f 100644 --- a/src/plugins/data/server/search/routes/bsearch.ts +++ b/src/plugins/data/server/search/routes/bsearch.ts @@ -14,7 +14,7 @@ import { IKibanaSearchResponse, ISearchOptionsSerializable, } from '../../../common/search'; -import { ISearchStart } from '../types'; +import type { ISearchStart } from '../types'; export function registerBsearchRoute( bfetch: BfetchServerSetup, diff --git a/src/plugins/data/server/search/search_service.test.ts b/src/plugins/data/server/search/search_service.test.ts index d8fc180ea1781..f449018612cef 100644 --- a/src/plugins/data/server/search/search_service.test.ts +++ b/src/plugins/data/server/search/search_service.test.ts @@ -17,7 +17,7 @@ import { createIndexPatternsStartMock } from '../data_views/mocks'; import { SearchService, SearchServiceSetupDependencies } from './search_service'; import { bfetchPluginMock } from '../../../bfetch/server/mocks'; import { of } from 'rxjs'; -import { +import type { IEsSearchRequest, IEsSearchResponse, IScopedSearchClient, @@ -25,8 +25,8 @@ import { ISearchSessionService, ISearchStart, ISearchStrategy, - NoSearchIdInSessionError, } from '.'; +import { NoSearchIdInSessionError } from '.'; // eslint-disable-next-line @kbn/eslint/no-restricted-paths import { expressionsPluginMock } from '../../../expressions/public/mocks'; import { createSearchSessionsClientMock } from './mocks'; diff --git a/src/plugins/data/server/search/search_source/mocks.ts b/src/plugins/data/server/search/search_source/mocks.ts index c990597d9a217..6ae30e0391000 100644 --- a/src/plugins/data/server/search/search_source/mocks.ts +++ b/src/plugins/data/server/search/search_source/mocks.ts @@ -10,7 +10,7 @@ import type { MockedKeys } from '@kbn/utility-types/jest'; import { KibanaRequest } from 'src/core/server'; import { searchSourceCommonMock } from '../../../common/search/search_source/mocks'; -import { ISearchStart } from '../types'; +import type { ISearchStart } from '../types'; function createStartContract(): MockedKeys { return { diff --git a/src/plugins/data/server/search/session/mocks.ts b/src/plugins/data/server/search/session/mocks.ts index b55292e4ac469..047f12df822c4 100644 --- a/src/plugins/data/server/search/session/mocks.ts +++ b/src/plugins/data/server/search/session/mocks.ts @@ -7,7 +7,7 @@ */ import moment from 'moment'; -import { IScopedSearchSessionsClient } from './types'; +import type { IScopedSearchSessionsClient } from './types'; import { SearchSessionsConfigSchema } from '../../../config'; export function createSearchSessionsClientMock(): jest.Mocked< diff --git a/src/plugins/data/server/search/strategies/ese_search/response_utils.ts b/src/plugins/data/server/search/strategies/ese_search/response_utils.ts index 0a92c95dac615..c9390a1b381d5 100644 --- a/src/plugins/data/server/search/strategies/ese_search/response_utils.ts +++ b/src/plugins/data/server/search/strategies/ese_search/response_utils.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -import { AsyncSearchResponse } from './types'; +import type { AsyncSearchResponse } from './types'; import { getTotalLoaded } from '../es_search'; /** diff --git a/src/plugins/data/server/search/types.ts b/src/plugins/data/server/search/types.ts index 026ff9139d932..b2e28eec40c09 100644 --- a/src/plugins/data/server/search/types.ts +++ b/src/plugins/data/server/search/types.ts @@ -26,7 +26,7 @@ import { } from '../../common/search'; import { AggsSetup, AggsStart } from './aggs'; import { SearchUsage } from './collectors'; -import { IScopedSearchSessionsClient, ISearchSessionService } from './session'; +import type { IScopedSearchSessionsClient, ISearchSessionService } from './session'; export interface SearchEnhancements { sessionService: ISearchSessionService; @@ -123,9 +123,6 @@ export interface ISearchStart< export type SearchRequestHandlerContext = IScopedSearchClient; -/** - * @internal - */ export interface DataRequestHandlerContext extends RequestHandlerContext { search: SearchRequestHandlerContext; } diff --git a/src/plugins/data_view_field_editor/public/components/field_editor/form_fields/script_field.tsx b/src/plugins/data_view_field_editor/public/components/field_editor/form_fields/script_field.tsx index 602db0cd55274..c5eaeb02c05cf 100644 --- a/src/plugins/data_view_field_editor/public/components/field_editor/form_fields/script_field.tsx +++ b/src/plugins/data_view_field_editor/public/components/field_editor/form_fields/script_field.tsx @@ -218,6 +218,7 @@ const ScriptFieldComponent = ({ existingConcreteFields, links }: Props) => { <> + text, long `; +exports[`Table should render mixed, non-conflicting type 1`] = ` + + keyword, constant_keyword + +`; + exports[`Table should render normal field name 1`] = ` Elastic diff --git a/src/plugins/data_view_management/public/components/edit_index_pattern/indexed_fields_table/components/table/table.test.tsx b/src/plugins/data_view_management/public/components/edit_index_pattern/indexed_fields_table/components/table/table.test.tsx index ec18665ccbaf3..dd78b00f9775e 100644 --- a/src/plugins/data_view_management/public/components/edit_index_pattern/indexed_fields_table/components/table/table.test.tsx +++ b/src/plugins/data_view_management/public/components/edit_index_pattern/indexed_fields_table/components/table/table.test.tsx @@ -126,7 +126,7 @@ describe('Table', () => { const tableCell = shallow( renderTable() .prop('columns')[1] - .render('conflict', { + .render('text, long', { kbnType: 'conflict', conflictDescriptions: { keyword: ['index_a'], long: ['index_b'] }, }) @@ -134,6 +134,15 @@ describe('Table', () => { expect(tableCell).toMatchSnapshot(); }); + test('should render mixed, non-conflicting type', () => { + const tableCell = shallow( + renderTable().prop('columns')[1].render('keyword, constant_keyword', { + kbnType: 'string', + }) + ); + expect(tableCell).toMatchSnapshot(); + }); + test('should allow edits', () => { const editField = jest.fn(); diff --git a/src/plugins/data_view_management/public/components/edit_index_pattern/indexed_fields_table/components/table/table.tsx b/src/plugins/data_view_management/public/components/edit_index_pattern/indexed_fields_table/components/table/table.tsx index e08b153f0b262..6a82d0380629c 100644 --- a/src/plugins/data_view_management/public/components/edit_index_pattern/indexed_fields_table/components/table/table.tsx +++ b/src/plugins/data_view_management/public/components/edit_index_pattern/indexed_fields_table/components/table/table.tsx @@ -349,9 +349,11 @@ export class Table extends PureComponent { } renderFieldType(type: string, field: IndexedFieldItem) { + const conflictDescription = + field.conflictDescriptions && field.conflictDescriptions[field.name]; return ( - {type !== 'conflict' ? type : ''} + {type === 'conflict' && conflictDescription ? '' : type} {field.conflictDescriptions ? getConflictBtn(field.name, field.conflictDescriptions, this.props.openModal) : ''} diff --git a/src/plugins/data_view_management/public/components/edit_index_pattern/indexed_fields_table/indexed_fields_table.tsx b/src/plugins/data_view_management/public/components/edit_index_pattern/indexed_fields_table/indexed_fields_table.tsx index 1e0d36f465be5..a72c87655fd63 100644 --- a/src/plugins/data_view_management/public/components/edit_index_pattern/indexed_fields_table/indexed_fields_table.tsx +++ b/src/plugins/data_view_management/public/components/edit_index_pattern/indexed_fields_table/indexed_fields_table.tsx @@ -7,7 +7,6 @@ */ import React, { Component } from 'react'; -import { i18n } from '@kbn/i18n'; import { createSelector } from 'reselect'; import { OverlayStart } from 'src/core/public'; import { IndexPatternField, IndexPattern } from '../../../../../../plugins/data/public'; @@ -68,25 +67,12 @@ class IndexedFields extends Component) => f.value); const fieldWildcardMatch = fieldWildcardMatcher(sourceFilters || []); - const getDisplayEsType = (arr: string[]): string => { - const length = arr.length; - if (length < 1) { - return ''; - } - if (length > 1) { - return i18n.translate('indexPatternManagement.editIndexPattern.fields.conflictType', { - defaultMessage: 'conflict', - }); - } - return arr[0]; - }; - return ( (fields && fields.map((field) => { return { ...field.spec, - type: getDisplayEsType(field.esTypes || []), + type: field.esTypes?.join(', ') || '', kbnType: field.type, displayName: field.displayName, format: indexPattern.getFormatterForFieldNoDefault(field.name)?.type?.title || '', @@ -117,7 +103,14 @@ class IndexedFields extends Component field.type === indexedFieldTypeFilter); + // match conflict fields + fields = fields.filter((field) => { + if (indexedFieldTypeFilter === 'conflict' && field.kbnType === 'conflict') { + return true; + } + // match one of multiple types on a field + return field.esTypes?.length && field.esTypes?.indexOf(indexedFieldTypeFilter) !== -1; + }); } return fields; diff --git a/src/plugins/data_view_management/public/components/edit_index_pattern/tabs/tabs.tsx b/src/plugins/data_view_management/public/components/edit_index_pattern/tabs/tabs.tsx index c79871dbc8d71..b5940fa8d1bb0 100644 --- a/src/plugins/data_view_management/public/components/edit_index_pattern/tabs/tabs.tsx +++ b/src/plugins/data_view_management/public/components/edit_index_pattern/tabs/tabs.tsx @@ -93,13 +93,6 @@ export function Tabs({ const closeEditorHandler = useRef<() => void | undefined>(); const { DeleteRuntimeFieldProvider } = dataViewFieldEditor; - const conflict = i18n.translate( - 'indexPatternManagement.editIndexPattern.fieldTypes.conflictType', - { - defaultMessage: 'conflict', - } - ); - const refreshFilters = useCallback(() => { const tempIndexedFieldTypes: string[] = []; const tempScriptedFieldLanguages: string[] = []; @@ -109,8 +102,13 @@ export function Tabs({ tempScriptedFieldLanguages.push(field.lang); } } else { + // for conflicted fields, add conflict as a type + if (field.type === 'conflict') { + tempIndexedFieldTypes.push('conflict'); + } if (field.esTypes) { - tempIndexedFieldTypes.push(field.esTypes.length === 1 ? field.esTypes[0] : conflict); + // add all types, may be multiple + field.esTypes.forEach((item) => tempIndexedFieldTypes.push(item)); } } }); @@ -119,7 +117,7 @@ export function Tabs({ setScriptedFieldLanguages( convertToEuiSelectOption(tempScriptedFieldLanguages, 'scriptedFieldLanguages') ); - }, [indexPattern, conflict]); + }, [indexPattern]); const closeFieldEditor = useCallback(() => { if (closeEditorHandler.current) { diff --git a/src/plugins/data_views/server/fetcher/index_patterns_fetcher.test.ts b/src/plugins/data_views/server/fetcher/index_patterns_fetcher.test.ts index 1a8b705480258..f55609d75a066 100644 --- a/src/plugins/data_views/server/fetcher/index_patterns_fetcher.test.ts +++ b/src/plugins/data_views/server/fetcher/index_patterns_fetcher.test.ts @@ -32,9 +32,15 @@ describe('Index Pattern Fetcher - server', () => { indexPatterns = new IndexPatternsFetcher(esClient); }); it('Removes pattern without matching indices', async () => { + // first field caps request returns empty const result = await indexPatterns.validatePatternListActive(patternList); expect(result).toEqual(['b', 'c']); }); + it('Keeps matching and negating patterns', async () => { + // first field caps request returns empty + const result = await indexPatterns.validatePatternListActive(['-a', 'b', 'c']); + expect(result).toEqual(['-a', 'c']); + }); it('Returns all patterns when all match indices', async () => { esClient = { fieldCaps: jest.fn().mockResolvedValue(response), diff --git a/src/plugins/data_views/server/fetcher/index_patterns_fetcher.ts b/src/plugins/data_views/server/fetcher/index_patterns_fetcher.ts index c054d547e956f..bceefac22e0f0 100644 --- a/src/plugins/data_views/server/fetcher/index_patterns_fetcher.ts +++ b/src/plugins/data_views/server/fetcher/index_patterns_fetcher.ts @@ -133,6 +133,10 @@ export class IndexPatternsFetcher { const result = await Promise.all( patternList .map(async (index) => { + // perserve negated patterns + if (index.startsWith('-')) { + return true; + } const searchResponse = await this.elasticsearchClient.fieldCaps({ index, fields: '_id', diff --git a/src/plugins/dev_tools/kibana.json b/src/plugins/dev_tools/kibana.json index 75a1e82f1d910..9b2ae8a3f995f 100644 --- a/src/plugins/dev_tools/kibana.json +++ b/src/plugins/dev_tools/kibana.json @@ -7,5 +7,6 @@ "name": "Stack Management", "githubTeam": "kibana-stack-management" }, - "requiredPlugins": ["urlForwarding"] + "requiredPlugins": ["urlForwarding"], + "requiredBundles": ["kibanaReact"] } diff --git a/src/plugins/dev_tools/public/application.tsx b/src/plugins/dev_tools/public/application.tsx index a4fdaf28e0eb4..dc72cfda790d4 100644 --- a/src/plugins/dev_tools/public/application.tsx +++ b/src/plugins/dev_tools/public/application.tsx @@ -16,6 +16,7 @@ import { i18n } from '@kbn/i18n'; import { euiThemeVars } from '@kbn/ui-shared-deps-src/theme'; import { ApplicationStart, ChromeStart, ScopedHistory, CoreTheme } from 'src/core/public'; +import { KibanaThemeProvider } from '../../kibana_react/public'; import type { DocTitleService, BreadcrumbService } from './services'; import { DevToolApp } from './dev_tool'; @@ -177,32 +178,34 @@ export function renderApp( ReactDOM.render( - - - {devTools - // Only create routes for devtools that are not disabled - .filter((devTool) => !devTool.isDisabled()) - .map((devTool) => ( - ( - - )} - /> - ))} - - - - - + + + + {devTools + // Only create routes for devtools that are not disabled + .filter((devTool) => !devTool.isDisabled()) + .map((devTool) => ( + ( + + )} + /> + ))} + + + + + + , element ); diff --git a/src/plugins/discover/public/__mocks__/services.ts b/src/plugins/discover/public/__mocks__/services.ts index 6a90ed42417e6..337d44227139e 100644 --- a/src/plugins/discover/public/__mocks__/services.ts +++ b/src/plugins/discover/public/__mocks__/services.ts @@ -78,7 +78,7 @@ export const discoverServiceMock = { http: { basePath: '/', }, - indexPatternFieldEditor: { + dataViewFieldEditor: { openEditor: jest.fn(), userPermissions: { editIndexPattern: jest.fn(), @@ -97,4 +97,5 @@ export const discoverServiceMock = { storage: { get: jest.fn(), }, + addBasePath: jest.fn(), } as unknown as DiscoverServices; diff --git a/src/plugins/discover/public/application/context/context_app_route.tsx b/src/plugins/discover/public/application/context/context_app_route.tsx index dfc318021b93e..80feea833ec94 100644 --- a/src/plugins/discover/public/application/context/context_app_route.tsx +++ b/src/plugins/discover/public/application/context/context_app_route.tsx @@ -15,6 +15,7 @@ import { ContextApp } from './context_app'; import { getRootBreadcrumbs } from '../../utils/breadcrumbs'; import { LoadingIndicator } from '../../components/common/loading_indicator'; import { useIndexPattern } from '../../utils/use_index_pattern'; +import { useMainRouteBreadcrumb } from '../../utils/use_navigation_props'; export interface ContextAppProps { /** @@ -33,17 +34,18 @@ export function ContextAppRoute(props: ContextAppProps) { const { chrome } = services; const { indexPatternId, id } = useParams(); + const breadcrumb = useMainRouteBreadcrumb(); useEffect(() => { chrome.setBreadcrumbs([ - ...getRootBreadcrumbs(), + ...getRootBreadcrumbs(breadcrumb), { text: i18n.translate('discover.context.breadcrumb', { defaultMessage: 'Surrounding documents', }), }, ]); - }, [chrome]); + }, [chrome, breadcrumb]); const { indexPattern, error } = useIndexPattern(services.indexPatterns, indexPatternId); diff --git a/src/plugins/discover/public/application/doc/single_doc_route.tsx b/src/plugins/discover/public/application/doc/single_doc_route.tsx index e5ddb784b9080..0a5cc3a8a82b6 100644 --- a/src/plugins/discover/public/application/doc/single_doc_route.tsx +++ b/src/plugins/discover/public/application/doc/single_doc_route.tsx @@ -14,6 +14,7 @@ import { getRootBreadcrumbs } from '../../utils/breadcrumbs'; import { Doc } from './components/doc'; import { LoadingIndicator } from '../../components/common/loading_indicator'; import { useIndexPattern } from '../../utils/use_index_pattern'; +import { useMainRouteBreadcrumb } from '../../utils/use_navigation_props'; export interface SingleDocRouteProps { /** @@ -36,18 +37,19 @@ export function SingleDocRoute(props: SingleDocRouteProps) { const { chrome, timefilter } = services; const { indexPatternId, index } = useParams(); + const breadcrumb = useMainRouteBreadcrumb(); const query = useQuery(); const docId = query.get('id') || ''; useEffect(() => { chrome.setBreadcrumbs([ - ...getRootBreadcrumbs(), + ...getRootBreadcrumbs(breadcrumb), { text: `${index}#${docId}`, }, ]); - }, [chrome, index, docId]); + }, [chrome, index, docId, breadcrumb]); useEffect(() => { timefilter.disableAutoRefreshSelector(); diff --git a/src/plugins/discover/public/application/main/components/sidebar/__snapshots__/discover_index_pattern_management.test.tsx.snap b/src/plugins/discover/public/application/main/components/sidebar/__snapshots__/discover_index_pattern_management.test.tsx.snap index 6cb6a15aa0f66..17d414215af55 100644 --- a/src/plugins/discover/public/application/main/components/sidebar/__snapshots__/discover_index_pattern_management.test.tsx.snap +++ b/src/plugins/discover/public/application/main/components/sidebar/__snapshots__/discover_index_pattern_management.test.tsx.snap @@ -653,13 +653,13 @@ exports[`Discover IndexPattern Management renders correctly 1`] = ` "navigateToApp": [MockFunction], }, }, - "history": [Function], - "indexPatternFieldEditor": Object { + "dataViewFieldEditor": Object { "openEditor": [MockFunction], "userPermissions": Object { "editIndexPattern": [Function], }, }, + "history": [Function], "uiSettings": Object { "get": [Function], }, diff --git a/src/plugins/discover/public/application/main/components/sidebar/discover_index_pattern_management.test.tsx b/src/plugins/discover/public/application/main/components/sidebar/discover_index_pattern_management.test.tsx index c5b1f4d2612d6..4132e4fb1b9b8 100644 --- a/src/plugins/discover/public/application/main/components/sidebar/discover_index_pattern_management.test.tsx +++ b/src/plugins/discover/public/application/main/components/sidebar/discover_index_pattern_management.test.tsx @@ -39,7 +39,7 @@ const mockServices = { } }, }, - indexPatternFieldEditor: { + dataViewFieldEditor: { openEditor: jest.fn(), userPermissions: { editIndexPattern: () => { diff --git a/src/plugins/discover/public/application/main/components/sidebar/discover_index_pattern_management.tsx b/src/plugins/discover/public/application/main/components/sidebar/discover_index_pattern_management.tsx index 9353073e7fad6..7fbb518ca3034 100644 --- a/src/plugins/discover/public/application/main/components/sidebar/discover_index_pattern_management.tsx +++ b/src/plugins/discover/public/application/main/components/sidebar/discover_index_pattern_management.tsx @@ -33,14 +33,13 @@ export interface DiscoverIndexPatternManagementProps { } export function DiscoverIndexPatternManagement(props: DiscoverIndexPatternManagementProps) { - const { indexPatternFieldEditor, core } = props.services; + const { dataViewFieldEditor, core } = props.services; const { useNewFieldsApi, selectedIndexPattern, editField } = props; - const indexPatternFieldEditPermission = - indexPatternFieldEditor?.userPermissions.editIndexPattern(); - const canEditIndexPatternField = !!indexPatternFieldEditPermission && useNewFieldsApi; + const dataViewEditPermission = dataViewFieldEditor?.userPermissions.editIndexPattern(); + const canEditDataViewField = !!dataViewEditPermission && useNewFieldsApi; const [isAddIndexPatternFieldPopoverOpen, setIsAddIndexPatternFieldPopoverOpen] = useState(false); - if (!useNewFieldsApi || !selectedIndexPattern || !canEditIndexPatternField) { + if (!useNewFieldsApi || !selectedIndexPattern || !canEditDataViewField) { return null; } diff --git a/src/plugins/discover/public/application/main/components/sidebar/discover_sidebar.tsx b/src/plugins/discover/public/application/main/components/sidebar/discover_sidebar.tsx index 78aee49d1b288..ea7b6fd31923e 100644 --- a/src/plugins/discover/public/application/main/components/sidebar/discover_sidebar.tsx +++ b/src/plugins/discover/public/application/main/components/sidebar/discover_sidebar.tsx @@ -109,10 +109,9 @@ export function DiscoverSidebarComponent({ }: DiscoverSidebarProps) { const [fields, setFields] = useState(null); - const { indexPatternFieldEditor } = services; - const indexPatternFieldEditPermission = - indexPatternFieldEditor?.userPermissions.editIndexPattern(); - const canEditIndexPatternField = !!indexPatternFieldEditPermission && useNewFieldsApi; + const { dataViewFieldEditor } = services; + const dataViewFieldEditPermission = dataViewFieldEditor?.userPermissions.editIndexPattern(); + const canEditDataViewField = !!dataViewFieldEditPermission && useNewFieldsApi; const [scrollContainer, setScrollContainer] = useState(null); const [fieldsToRender, setFieldsToRender] = useState(FIELDS_PER_PAGE); const [fieldsPerPage, setFieldsPerPage] = useState(FIELDS_PER_PAGE); @@ -243,9 +242,9 @@ export function DiscoverSidebarComponent({ const deleteField = useMemo( () => - canEditIndexPatternField && selectedIndexPattern + canEditDataViewField && selectedIndexPattern ? async (fieldName: string) => { - const ref = indexPatternFieldEditor.openDeleteModal({ + const ref = dataViewFieldEditor.openDeleteModal({ ctx: { dataView: selectedIndexPattern, }, @@ -264,11 +263,11 @@ export function DiscoverSidebarComponent({ : undefined, [ selectedIndexPattern, - canEditIndexPatternField, + canEditDataViewField, setFieldEditorRef, closeFlyout, onEditRuntimeField, - indexPatternFieldEditor, + dataViewFieldEditor, ] ); @@ -413,8 +412,8 @@ export function DiscoverSidebarComponent({ selected={true} trackUiMetric={trackUiMetric} multiFields={multiFields?.get(field.name)} - onEditField={canEditIndexPatternField ? editField : undefined} - onDeleteField={canEditIndexPatternField ? deleteField : undefined} + onEditField={canEditDataViewField ? editField : undefined} + onDeleteField={canEditDataViewField ? deleteField : undefined} showFieldStats={showFieldStats} /> @@ -473,8 +472,8 @@ export function DiscoverSidebarComponent({ getDetails={getDetailsByField} trackUiMetric={trackUiMetric} multiFields={multiFields?.get(field.name)} - onEditField={canEditIndexPatternField ? editField : undefined} - onDeleteField={canEditIndexPatternField ? deleteField : undefined} + onEditField={canEditDataViewField ? editField : undefined} + onDeleteField={canEditDataViewField ? deleteField : undefined} showFieldStats={showFieldStats} /> @@ -502,8 +501,8 @@ export function DiscoverSidebarComponent({ getDetails={getDetailsByField} trackUiMetric={trackUiMetric} multiFields={multiFields?.get(field.name)} - onEditField={canEditIndexPatternField ? editField : undefined} - onDeleteField={canEditIndexPatternField ? deleteField : undefined} + onEditField={canEditDataViewField ? editField : undefined} + onDeleteField={canEditDataViewField ? deleteField : undefined} showFieldStats={showFieldStats} /> diff --git a/src/plugins/discover/public/application/main/components/sidebar/discover_sidebar_responsive.tsx b/src/plugins/discover/public/application/main/components/sidebar/discover_sidebar_responsive.tsx index a4e84bd831619..6316369ff4c6f 100644 --- a/src/plugins/discover/public/application/main/components/sidebar/discover_sidebar_responsive.tsx +++ b/src/plugins/discover/public/application/main/components/sidebar/discover_sidebar_responsive.tsx @@ -180,17 +180,17 @@ export function DiscoverSidebarResponsive(props: DiscoverSidebarResponsiveProps) setIsFlyoutVisible(false); }, []); - const { indexPatternFieldEditor } = props.services; + const { dataViewFieldEditor } = props.services; const editField = useCallback( (fieldName?: string) => { const indexPatternFieldEditPermission = - indexPatternFieldEditor?.userPermissions.editIndexPattern(); + dataViewFieldEditor?.userPermissions.editIndexPattern(); const canEditIndexPatternField = !!indexPatternFieldEditPermission && useNewFieldsApi; if (!canEditIndexPatternField || !selectedIndexPattern) { return; } - const ref = indexPatternFieldEditor.openEditor({ + const ref = dataViewFieldEditor.openEditor({ ctx: { dataView: selectedIndexPattern, }, @@ -208,7 +208,7 @@ export function DiscoverSidebarResponsive(props: DiscoverSidebarResponsiveProps) }, [ closeFlyout, - indexPatternFieldEditor, + dataViewFieldEditor, selectedIndexPattern, setFieldEditorRef, onEditRuntimeField, diff --git a/src/plugins/discover/public/application/main/utils/fetch_all.test.ts b/src/plugins/discover/public/application/main/utils/fetch_all.test.ts index 9f17054de18d4..a2dae5cc99b7d 100644 --- a/src/plugins/discover/public/application/main/utils/fetch_all.test.ts +++ b/src/plugins/discover/public/application/main/utils/fetch_all.test.ts @@ -6,23 +6,66 @@ * Side Public License, v 1. */ import { FetchStatus } from '../../types'; -import { BehaviorSubject } from 'rxjs'; +import { BehaviorSubject, Subject } from 'rxjs'; +import { reduce } from 'rxjs/operators'; +import { SearchSource } from '../../../../../data/common'; import { RequestAdapter } from '../../../../../inspector'; import { savedSearchMock } from '../../../__mocks__/saved_search'; import { ReduxLikeStateContainer } from '../../../../../kibana_utils/common'; import { AppState } from '../services/discover_state'; import { discoverServiceMock } from '../../../__mocks__/services'; import { fetchAll } from './fetch_all'; +import { + DataChartsMessage, + DataDocumentsMsg, + DataMainMsg, + DataTotalHitsMsg, + SavedSearchData, +} from './use_saved_search'; + +import { fetchDocuments } from './fetch_documents'; +import { fetchChart } from './fetch_chart'; +import { fetchTotalHits } from './fetch_total_hits'; + +jest.mock('./fetch_documents', () => ({ + fetchDocuments: jest.fn().mockResolvedValue([]), +})); + +jest.mock('./fetch_chart', () => ({ + fetchChart: jest.fn(), +})); + +jest.mock('./fetch_total_hits', () => ({ + fetchTotalHits: jest.fn(), +})); + +const mockFetchDocuments = fetchDocuments as unknown as jest.MockedFunction; +const mockFetchTotalHits = fetchTotalHits as unknown as jest.MockedFunction; +const mockFetchChart = fetchChart as unknown as jest.MockedFunction; + +function subjectCollector(subject: Subject): () => Promise { + const promise = subject + .pipe(reduce((history, value) => history.concat([value]), [] as T[])) + .toPromise(); + + return () => { + subject.complete(); + return promise; + }; +} describe('test fetchAll', () => { - test('changes of fetchStatus when starting with FetchStatus.UNINITIALIZED', async (done) => { - const subjects = { - main$: new BehaviorSubject({ fetchStatus: FetchStatus.UNINITIALIZED }), - documents$: new BehaviorSubject({ fetchStatus: FetchStatus.UNINITIALIZED }), - totalHits$: new BehaviorSubject({ fetchStatus: FetchStatus.UNINITIALIZED }), - charts$: new BehaviorSubject({ fetchStatus: FetchStatus.UNINITIALIZED }), + let subjects: SavedSearchData; + let deps: Parameters[3]; + let searchSource: SearchSource; + beforeEach(() => { + subjects = { + main$: new BehaviorSubject({ fetchStatus: FetchStatus.UNINITIALIZED }), + documents$: new BehaviorSubject({ fetchStatus: FetchStatus.UNINITIALIZED }), + totalHits$: new BehaviorSubject({ fetchStatus: FetchStatus.UNINITIALIZED }), + charts$: new BehaviorSubject({ fetchStatus: FetchStatus.UNINITIALIZED }), }; - const deps = { + deps = { appStateContainer: { getState: () => { return { interval: 'auto' }; @@ -31,29 +74,126 @@ describe('test fetchAll', () => { abortController: new AbortController(), data: discoverServiceMock.data, inspectorAdapters: { requests: new RequestAdapter() }, - onResults: jest.fn(), searchSessionId: '123', initialFetchStatus: FetchStatus.UNINITIALIZED, useNewFieldsApi: true, services: discoverServiceMock, }; + searchSource = savedSearchMock.searchSource.createChild(); + + mockFetchDocuments.mockReset().mockResolvedValue([]); + mockFetchTotalHits.mockReset().mockResolvedValue(42); + mockFetchChart + .mockReset() + // eslint-disable-next-line @typescript-eslint/no-explicit-any + .mockResolvedValue({ totalHits: 42, chartData: {} as any, bucketInterval: {} }); + }); + test('changes of fetchStatus when starting with FetchStatus.UNINITIALIZED', async () => { const stateArr: FetchStatus[] = []; subjects.main$.subscribe((value) => stateArr.push(value.fetchStatus)); - const parentSearchSource = savedSearchMock.searchSource; - const childSearchSource = parentSearchSource.createChild(); - - fetchAll(subjects, childSearchSource, false, deps).subscribe({ - complete: () => { - expect(stateArr).toEqual([ - FetchStatus.UNINITIALIZED, - FetchStatus.LOADING, - FetchStatus.COMPLETE, - ]); - done(); - }, - }); + await fetchAll(subjects, searchSource, false, deps); + + expect(stateArr).toEqual([ + FetchStatus.UNINITIALIZED, + FetchStatus.LOADING, + FetchStatus.COMPLETE, + ]); + }); + + test('emits loading and documents on documents$ correctly', async () => { + const collect = subjectCollector(subjects.documents$); + const hits = [ + { _id: '1', _index: 'logs' }, + { _id: '2', _index: 'logs' }, + ]; + mockFetchDocuments.mockResolvedValue(hits); + await fetchAll(subjects, searchSource, false, deps); + expect(await collect()).toEqual([ + { fetchStatus: FetchStatus.UNINITIALIZED }, + { fetchStatus: FetchStatus.LOADING }, + { fetchStatus: FetchStatus.COMPLETE, result: hits }, + ]); + }); + + test('emits loading and hit count on totalHits$ correctly', async () => { + const collect = subjectCollector(subjects.totalHits$); + const hits = [ + { _id: '1', _index: 'logs' }, + { _id: '2', _index: 'logs' }, + ]; + searchSource.getField('index')!.isTimeBased = () => false; + mockFetchDocuments.mockResolvedValue(hits); + mockFetchTotalHits.mockResolvedValue(42); + await fetchAll(subjects, searchSource, false, deps); + expect(await collect()).toEqual([ + { fetchStatus: FetchStatus.UNINITIALIZED }, + { fetchStatus: FetchStatus.LOADING }, + { fetchStatus: FetchStatus.PARTIAL, result: 2 }, + { fetchStatus: FetchStatus.COMPLETE, result: 42 }, + ]); + }); + + test('emits loading and chartData on charts$ correctly', async () => { + const collect = subjectCollector(subjects.charts$); + searchSource.getField('index')!.isTimeBased = () => true; + await fetchAll(subjects, searchSource, false, deps); + expect(await collect()).toEqual([ + { fetchStatus: FetchStatus.UNINITIALIZED }, + { fetchStatus: FetchStatus.LOADING }, + { fetchStatus: FetchStatus.COMPLETE, bucketInterval: {}, chartData: {} }, + ]); + }); + + test('should use charts query to fetch total hit count when chart is visible', async () => { + const collect = subjectCollector(subjects.totalHits$); + searchSource.getField('index')!.isTimeBased = () => true; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + mockFetchChart.mockResolvedValue({ bucketInterval: {}, chartData: {} as any, totalHits: 32 }); + await fetchAll(subjects, searchSource, false, deps); + expect(await collect()).toEqual([ + { fetchStatus: FetchStatus.UNINITIALIZED }, + { fetchStatus: FetchStatus.LOADING }, + { fetchStatus: FetchStatus.PARTIAL, result: 0 }, // From documents query + { fetchStatus: FetchStatus.COMPLETE, result: 32 }, + ]); + expect(mockFetchTotalHits).not.toHaveBeenCalled(); + }); + + test('should only fail totalHits$ query not main$ for error from that query', async () => { + const collectTotalHits = subjectCollector(subjects.totalHits$); + const collectMain = subjectCollector(subjects.main$); + searchSource.getField('index')!.isTimeBased = () => false; + mockFetchTotalHits.mockRejectedValue({ msg: 'Oh noes!' }); + mockFetchDocuments.mockResolvedValue([{ _id: '1', _index: 'logs' }]); + await fetchAll(subjects, searchSource, false, deps); + expect(await collectTotalHits()).toEqual([ + { fetchStatus: FetchStatus.UNINITIALIZED }, + { fetchStatus: FetchStatus.LOADING }, + { fetchStatus: FetchStatus.PARTIAL, result: 1 }, + { fetchStatus: FetchStatus.ERROR, error: { msg: 'Oh noes!' } }, + ]); + expect(await collectMain()).toEqual([ + { fetchStatus: FetchStatus.UNINITIALIZED }, + { fetchStatus: FetchStatus.LOADING }, + { fetchStatus: FetchStatus.PARTIAL }, + { fetchStatus: FetchStatus.COMPLETE, foundDocuments: true }, + ]); + }); + + test('should not set COMPLETE if an ERROR has been set on main$', async () => { + const collectMain = subjectCollector(subjects.main$); + searchSource.getField('index')!.isTimeBased = () => false; + mockFetchDocuments.mockRejectedValue({ msg: 'This query failed' }); + await fetchAll(subjects, searchSource, false, deps); + expect(await collectMain()).toEqual([ + { fetchStatus: FetchStatus.UNINITIALIZED }, + { fetchStatus: FetchStatus.LOADING }, + { fetchStatus: FetchStatus.PARTIAL }, // From totalHits query + { fetchStatus: FetchStatus.ERROR, error: { msg: 'This query failed' } }, + // Here should be no COMPLETE coming anymore + ]); }); }); diff --git a/src/plugins/discover/public/application/main/utils/fetch_all.ts b/src/plugins/discover/public/application/main/utils/fetch_all.ts index 471616c9d4261..29279152ca321 100644 --- a/src/plugins/discover/public/application/main/utils/fetch_all.ts +++ b/src/plugins/discover/public/application/main/utils/fetch_all.ts @@ -5,11 +5,11 @@ * in compliance with, at your election, the Elastic License 2.0 or the Server * Side Public License, v 1. */ -import { forkJoin, of } from 'rxjs'; import { sendCompleteMsg, sendErrorMsg, sendLoadingMsg, + sendNoResultsFoundMsg, sendPartialMsg, sendResetMsg, } from './use_saved_search_messages'; @@ -23,11 +23,25 @@ import { Adapters } from '../../../../../inspector'; import { AppState } from '../services/discover_state'; import { FetchStatus } from '../../types'; import { DataPublicPluginStart } from '../../../../../data/public'; -import { SavedSearchData } from './use_saved_search'; +import { + DataCharts$, + DataDocuments$, + DataMain$, + DataTotalHits$, + SavedSearchData, +} from './use_saved_search'; import { DiscoverServices } from '../../../build_services'; import { ReduxLikeStateContainer } from '../../../../../kibana_utils/common'; import { DataViewType } from '../../../../../data_views/common'; +/** + * This function starts fetching all required queries in Discover. This will be the query to load the individual + * documents, and depending on whether a chart is shown either the aggregation query to load the chart data + * or a query to retrieve just the total hits. + * + * This method returns a promise, which will resolve (without a value), as soon as all queries that have been started + * have been completed (failed or successfully). + */ export function fetchAll( dataSubjects: SavedSearchData, searchSource: ISearchSource, @@ -42,57 +56,137 @@ export function fetchAll( services: DiscoverServices; useNewFieldsApi: boolean; } -) { +): Promise { const { initialFetchStatus, appStateContainer, services, useNewFieldsApi, data } = fetchDeps; - const indexPattern = searchSource.getField('index')!; + /** + * Method to create a an error handler that will forward the received error + * to the specified subjects. It will ignore AbortErrors and will use the data + * plugin to show a toast for the error (e.g. allowing better insights into shard failures). + */ + const sendErrorTo = ( + ...errorSubjects: Array + ) => { + return (error: Error) => { + if (error instanceof Error && error.name === 'AbortError') { + return; + } - if (reset) { - sendResetMsg(dataSubjects, initialFetchStatus); - } + data.search.showError(error); + errorSubjects.forEach((subject) => sendErrorMsg(subject, error)); + }; + }; - sendLoadingMsg(dataSubjects.main$); - - const { hideChart, sort } = appStateContainer.getState(); - // Update the base searchSource, base for all child fetches - updateSearchSource(searchSource, false, { - indexPattern, - services, - sort: sort as SortOrder[], - useNewFieldsApi, - }); - - const subFetchDeps = { - ...fetchDeps, - onResults: (foundDocuments: boolean) => { - if (!foundDocuments) { - sendCompleteMsg(dataSubjects.main$, foundDocuments); - } else { + try { + const indexPattern = searchSource.getField('index')!; + + if (reset) { + sendResetMsg(dataSubjects, initialFetchStatus); + } + + const { hideChart, sort } = appStateContainer.getState(); + + // Update the base searchSource, base for all child fetches + updateSearchSource(searchSource, false, { + indexPattern, + services, + sort: sort as SortOrder[], + useNewFieldsApi, + }); + + // Mark all subjects as loading + sendLoadingMsg(dataSubjects.main$); + sendLoadingMsg(dataSubjects.documents$); + sendLoadingMsg(dataSubjects.totalHits$); + sendLoadingMsg(dataSubjects.charts$); + + const isChartVisible = + !hideChart && indexPattern.isTimeBased() && indexPattern.type !== DataViewType.ROLLUP; + + // Start fetching all required requests + const documents = fetchDocuments(searchSource.createCopy(), fetchDeps); + const charts = isChartVisible ? fetchChart(searchSource.createCopy(), fetchDeps) : undefined; + const totalHits = !isChartVisible + ? fetchTotalHits(searchSource.createCopy(), fetchDeps) + : undefined; + + /** + * This method checks the passed in hit count and will send a PARTIAL message to main$ + * if there are results, indicating that we have finished some of the requests that have been + * sent. If there are no results we already COMPLETE main$ with no results found, so Discover + * can show the "no results" screen. We know at that point, that the other query returning + * will neither carry any data, since there are no documents. + */ + const checkHitCount = (hitsCount: number) => { + if (hitsCount > 0) { sendPartialMsg(dataSubjects.main$); + } else { + sendNoResultsFoundMsg(dataSubjects.main$); } - }, - }; + }; - const isChartVisible = - !hideChart && indexPattern.isTimeBased() && indexPattern.type !== DataViewType.ROLLUP; - - const all = forkJoin({ - documents: fetchDocuments(dataSubjects, searchSource.createCopy(), subFetchDeps), - totalHits: !isChartVisible - ? fetchTotalHits(dataSubjects, searchSource.createCopy(), subFetchDeps) - : of(null), - chart: isChartVisible - ? fetchChart(dataSubjects, searchSource.createCopy(), subFetchDeps) - : of(null), - }); - - all.subscribe( - () => sendCompleteMsg(dataSubjects.main$, true), - (error) => { - if (error instanceof Error && error.name === 'AbortError') return; - data.search.showError(error); - sendErrorMsg(dataSubjects.main$, error); - } - ); - return all; + // Handle results of the individual queries and forward the results to the corresponding dataSubjects + + documents + .then((docs) => { + // If the total hits (or chart) query is still loading, emit a partial + // hit count that's at least our retrieved document count + if (dataSubjects.totalHits$.getValue().fetchStatus === FetchStatus.LOADING) { + dataSubjects.totalHits$.next({ + fetchStatus: FetchStatus.PARTIAL, + result: docs.length, + }); + } + + dataSubjects.documents$.next({ + fetchStatus: FetchStatus.COMPLETE, + result: docs, + }); + + checkHitCount(docs.length); + }) + // Only the document query should send its errors to main$, to cause the full Discover app + // to get into an error state. The other queries will not cause all of Discover to error out + // but their errors will be shown in-place (e.g. of the chart). + .catch(sendErrorTo(dataSubjects.documents$, dataSubjects.main$)); + + charts + ?.then((chart) => { + dataSubjects.totalHits$.next({ + fetchStatus: FetchStatus.COMPLETE, + result: chart.totalHits, + }); + + dataSubjects.charts$.next({ + fetchStatus: FetchStatus.COMPLETE, + chartData: chart.chartData, + bucketInterval: chart.bucketInterval, + }); + + checkHitCount(chart.totalHits); + }) + .catch(sendErrorTo(dataSubjects.charts$, dataSubjects.totalHits$)); + + totalHits + ?.then((hitCount) => { + dataSubjects.totalHits$.next({ fetchStatus: FetchStatus.COMPLETE, result: hitCount }); + checkHitCount(hitCount); + }) + .catch(sendErrorTo(dataSubjects.totalHits$)); + + // Return a promise that will resolve once all the requests have finished or failed + return Promise.allSettled([documents, charts, totalHits]).then(() => { + // Send a complete message to main$ once all queries are done and if main$ + // is not already in an ERROR state, e.g. because the document query has failed. + // This will only complete main$, if it hasn't already been completed previously + // by a query finding no results. + if (dataSubjects.main$.getValue().fetchStatus !== FetchStatus.ERROR) { + sendCompleteMsg(dataSubjects.main$); + } + }); + } catch (error) { + sendErrorMsg(dataSubjects.main$, error); + // We also want to return a resolved promise in an error case, since it just indicates we're done with querying. + return Promise.resolve(); + } } diff --git a/src/plugins/discover/public/application/main/utils/fetch_chart.test.ts b/src/plugins/discover/public/application/main/utils/fetch_chart.test.ts index 5f57484aaa653..b8c2f643acae7 100644 --- a/src/plugins/discover/public/application/main/utils/fetch_chart.test.ts +++ b/src/plugins/discover/public/application/main/utils/fetch_chart.test.ts @@ -5,8 +5,7 @@ * in compliance with, at your election, the Elastic License 2.0 or the Server * Side Public License, v 1. */ -import { FetchStatus } from '../../types'; -import { BehaviorSubject, of, throwError as throwErrorRx } from 'rxjs'; +import { of, throwError as throwErrorRx } from 'rxjs'; import { RequestAdapter } from '../../../../../inspector'; import { savedSearchMockWithTimeField } from '../../../__mocks__/saved_search'; import { fetchChart, updateSearchSource } from './fetch_chart'; @@ -16,15 +15,6 @@ import { discoverServiceMock } from '../../../__mocks__/services'; import { calculateBounds, IKibanaSearchResponse } from '../../../../../data/common'; import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; -function getDataSubjects() { - return { - main$: new BehaviorSubject({ fetchStatus: FetchStatus.UNINITIALIZED }), - documents$: new BehaviorSubject({ fetchStatus: FetchStatus.UNINITIALIZED }), - totalHits$: new BehaviorSubject({ fetchStatus: FetchStatus.UNINITIALIZED }), - charts$: new BehaviorSubject({ fetchStatus: FetchStatus.UNINITIALIZED }), - }; -} - describe('test fetchCharts', () => { test('updateSearchSource helper function', () => { const chartAggConfigs = updateSearchSource( @@ -61,8 +51,7 @@ describe('test fetchCharts', () => { `); }); - test('changes of fetchStatus when starting with FetchStatus.UNINITIALIZED', async (done) => { - const subjects = getDataSubjects(); + test('resolves with summarized chart data', async () => { const deps = { appStateContainer: { getState: () => { @@ -82,12 +71,6 @@ describe('test fetchCharts', () => { deps.data.query.timefilter.timefilter.calculateBounds = (timeRange) => calculateBounds(timeRange); - const stateArrChart: FetchStatus[] = []; - const stateArrHits: FetchStatus[] = []; - - subjects.charts$.subscribe((value) => stateArrChart.push(value.fetchStatus)); - subjects.totalHits$.subscribe((value) => stateArrHits.push(value.fetchStatus)); - savedSearchMockWithTimeField.searchSource.fetch$ = () => of({ id: 'Fjk5bndxTHJWU2FldVRVQ0tYR0VqOFEcRWtWNDhOdG5SUzJYcFhONVVZVTBJQToxMDMwOQ==', @@ -95,7 +78,7 @@ describe('test fetchCharts', () => { took: 2, timed_out: false, _shards: { total: 1, successful: 1, skipped: 0, failed: 0 }, - hits: { max_score: null, hits: [] }, + hits: { max_score: null, hits: [], total: 42 }, aggregations: { '2': { buckets: [ @@ -115,25 +98,13 @@ describe('test fetchCharts', () => { isRestored: false, } as unknown as IKibanaSearchResponse>); - fetchChart(subjects, savedSearchMockWithTimeField.searchSource, deps).subscribe({ - complete: () => { - expect(stateArrChart).toEqual([ - FetchStatus.UNINITIALIZED, - FetchStatus.LOADING, - FetchStatus.COMPLETE, - ]); - expect(stateArrHits).toEqual([ - FetchStatus.UNINITIALIZED, - FetchStatus.LOADING, - FetchStatus.COMPLETE, - ]); - done(); - }, - }); + const result = await fetchChart(savedSearchMockWithTimeField.searchSource, deps); + expect(result).toHaveProperty('totalHits', 42); + expect(result).toHaveProperty('bucketInterval.description', '0 milliseconds'); + expect(result).toHaveProperty('chartData'); }); - test('change of fetchStatus on fetch error', async (done) => { - const subjects = getDataSubjects(); + test('rejects promise on query failure', async () => { const deps = { appStateContainer: { getState: () => { @@ -149,26 +120,8 @@ describe('test fetchCharts', () => { savedSearchMockWithTimeField.searchSource.fetch$ = () => throwErrorRx({ msg: 'Oh noes!' }); - const stateArrChart: FetchStatus[] = []; - const stateArrHits: FetchStatus[] = []; - - subjects.charts$.subscribe((value) => stateArrChart.push(value.fetchStatus)); - subjects.totalHits$.subscribe((value) => stateArrHits.push(value.fetchStatus)); - - fetchChart(subjects, savedSearchMockWithTimeField.searchSource, deps).subscribe({ - error: () => { - expect(stateArrChart).toEqual([ - FetchStatus.UNINITIALIZED, - FetchStatus.LOADING, - FetchStatus.ERROR, - ]); - expect(stateArrHits).toEqual([ - FetchStatus.UNINITIALIZED, - FetchStatus.LOADING, - FetchStatus.ERROR, - ]); - done(); - }, + await expect(fetchChart(savedSearchMockWithTimeField.searchSource, deps)).rejects.toEqual({ + msg: 'Oh noes!', }); }); }); diff --git a/src/plugins/discover/public/application/main/utils/fetch_chart.ts b/src/plugins/discover/public/application/main/utils/fetch_chart.ts index 59377970acb12..7f74f693eb784 100644 --- a/src/plugins/discover/public/application/main/utils/fetch_chart.ts +++ b/src/plugins/discover/public/application/main/utils/fetch_chart.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ import { i18n } from '@kbn/i18n'; -import { filter } from 'rxjs/operators'; +import { filter, map } from 'rxjs/operators'; import { DataPublicPluginStart, isCompleteResponse, @@ -16,40 +16,36 @@ import { import { Adapters } from '../../../../../inspector'; import { getChartAggConfigs, getDimensions } from './index'; import { tabifyAggResponse } from '../../../../../data/common'; -import { buildPointSeriesData } from '../components/chart/point_series'; -import { FetchStatus } from '../../types'; -import { SavedSearchData } from './use_saved_search'; +import { buildPointSeriesData, Chart } from '../components/chart/point_series'; +import { TimechartBucketInterval } from './use_saved_search'; import { AppState } from '../services/discover_state'; import { ReduxLikeStateContainer } from '../../../../../kibana_utils/common'; -import { sendErrorMsg, sendLoadingMsg } from './use_saved_search_messages'; + +interface Result { + totalHits: number; + chartData: Chart; + bucketInterval: TimechartBucketInterval | undefined; +} export function fetchChart( - data$: SavedSearchData, searchSource: ISearchSource, { abortController, appStateContainer, data, inspectorAdapters, - onResults, searchSessionId, }: { abortController: AbortController; appStateContainer: ReduxLikeStateContainer; data: DataPublicPluginStart; inspectorAdapters: Adapters; - onResults: (foundDocuments: boolean) => void; searchSessionId: string; } -) { - const { charts$, totalHits$ } = data$; - +): Promise { const interval = appStateContainer.getState().interval ?? 'auto'; const chartAggConfigs = updateSearchSource(searchSource, interval, data); - sendLoadingMsg(charts$); - sendLoadingMsg(totalHits$); - const executionContext = { type: 'application', name: 'discover', @@ -74,15 +70,9 @@ export function fetchChart( }, executionContext, }) - .pipe(filter((res) => isCompleteResponse(res))); - - fetch$.subscribe( - (res) => { - try { - const totalHitsNr = res.rawResponse.hits.total as number; - totalHits$.next({ fetchStatus: FetchStatus.COMPLETE, result: totalHitsNr }); - onResults(totalHitsNr > 0); - + .pipe( + filter((res) => isCompleteResponse(res)), + map((res) => { const bucketAggConfig = chartAggConfigs.aggs[1]; const tabifiedData = tabifyAggResponse(chartAggConfigs, res.rawResponse); const dimensions = getDimensions(chartAggConfigs, data); @@ -90,27 +80,15 @@ export function fetchChart( ? bucketAggConfig?.buckets?.getInterval() : undefined; const chartData = buildPointSeriesData(tabifiedData, dimensions!); - charts$.next({ - fetchStatus: FetchStatus.COMPLETE, + return { chartData, bucketInterval, - }); - } catch (e) { - charts$.next({ - fetchStatus: FetchStatus.ERROR, - error: e, - }); - } - }, - (error) => { - if (error instanceof Error && error.name === 'AbortError') { - return; - } - sendErrorMsg(charts$, error); - sendErrorMsg(totalHits$, error); - } - ); - return fetch$; + totalHits: res.rawResponse.hits.total as number, + }; + }) + ); + + return fetch$.toPromise(); } export function updateSearchSource( diff --git a/src/plugins/discover/public/application/main/utils/fetch_documents.test.ts b/src/plugins/discover/public/application/main/utils/fetch_documents.test.ts index 291da255b5068..1342378f5a90b 100644 --- a/src/plugins/discover/public/application/main/utils/fetch_documents.test.ts +++ b/src/plugins/discover/public/application/main/utils/fetch_documents.test.ts @@ -6,74 +6,37 @@ * Side Public License, v 1. */ import { fetchDocuments } from './fetch_documents'; -import { FetchStatus } from '../../types'; -import { BehaviorSubject, throwError as throwErrorRx } from 'rxjs'; +import { throwError as throwErrorRx, of } from 'rxjs'; import { RequestAdapter } from '../../../../../inspector'; import { savedSearchMock } from '../../../__mocks__/saved_search'; import { discoverServiceMock } from '../../../__mocks__/services'; - -function getDataSubjects() { - return { - main$: new BehaviorSubject({ fetchStatus: FetchStatus.UNINITIALIZED }), - documents$: new BehaviorSubject({ fetchStatus: FetchStatus.UNINITIALIZED }), - totalHits$: new BehaviorSubject({ fetchStatus: FetchStatus.UNINITIALIZED }), - charts$: new BehaviorSubject({ fetchStatus: FetchStatus.UNINITIALIZED }), - }; -} +import { IKibanaSearchResponse } from 'src/plugins/data/common'; +import { SearchResponse } from '@elastic/elasticsearch/lib/api/types'; + +const getDeps = () => ({ + abortController: new AbortController(), + inspectorAdapters: { requests: new RequestAdapter() }, + onResults: jest.fn(), + searchSessionId: '123', + services: discoverServiceMock, +}); describe('test fetchDocuments', () => { - test('changes of fetchStatus are correct when starting with FetchStatus.UNINITIALIZED', async (done) => { - const subjects = getDataSubjects(); - const { documents$ } = subjects; - const deps = { - abortController: new AbortController(), - inspectorAdapters: { requests: new RequestAdapter() }, - onResults: jest.fn(), - searchSessionId: '123', - services: discoverServiceMock, - }; - - const stateArr: FetchStatus[] = []; - - documents$.subscribe((value) => stateArr.push(value.fetchStatus)); - - fetchDocuments(subjects, savedSearchMock.searchSource, deps).subscribe({ - complete: () => { - expect(stateArr).toEqual([ - FetchStatus.UNINITIALIZED, - FetchStatus.LOADING, - FetchStatus.COMPLETE, - ]); - done(); - }, - }); + test('resolves with returned documents', async () => { + const hits = [ + { _id: '1', foo: 'bar' }, + { _id: '2', foo: 'baz' }, + ]; + savedSearchMock.searchSource.fetch$ = () => + of({ rawResponse: { hits: { hits } } } as unknown as IKibanaSearchResponse); + expect(fetchDocuments(savedSearchMock.searchSource, getDeps())).resolves.toEqual(hits); }); - test('change of fetchStatus on fetch error', async (done) => { - const subjects = getDataSubjects(); - const { documents$ } = subjects; - const deps = { - abortController: new AbortController(), - inspectorAdapters: { requests: new RequestAdapter() }, - onResults: jest.fn(), - searchSessionId: '123', - services: discoverServiceMock, - }; + test('rejects on query failure', () => { savedSearchMock.searchSource.fetch$ = () => throwErrorRx({ msg: 'Oh noes!' }); - const stateArr: FetchStatus[] = []; - - documents$.subscribe((value) => stateArr.push(value.fetchStatus)); - - fetchDocuments(subjects, savedSearchMock.searchSource, deps).subscribe({ - error: () => { - expect(stateArr).toEqual([ - FetchStatus.UNINITIALIZED, - FetchStatus.LOADING, - FetchStatus.ERROR, - ]); - done(); - }, + expect(fetchDocuments(savedSearchMock.searchSource, getDeps())).rejects.toEqual({ + msg: 'Oh noes!', }); }); }); diff --git a/src/plugins/discover/public/application/main/utils/fetch_documents.ts b/src/plugins/discover/public/application/main/utils/fetch_documents.ts index b23dd3a0ed932..0c83b85b2bc62 100644 --- a/src/plugins/discover/public/application/main/utils/fetch_documents.ts +++ b/src/plugins/discover/public/application/main/utils/fetch_documents.ts @@ -6,34 +6,30 @@ * Side Public License, v 1. */ import { i18n } from '@kbn/i18n'; -import { filter } from 'rxjs/operators'; +import { filter, map } from 'rxjs/operators'; import { Adapters } from '../../../../../inspector/common'; import { isCompleteResponse, ISearchSource } from '../../../../../data/common'; -import { FetchStatus } from '../../types'; -import { SavedSearchData } from './use_saved_search'; -import { sendErrorMsg, sendLoadingMsg } from './use_saved_search_messages'; import { SAMPLE_SIZE_SETTING } from '../../../../common'; import { DiscoverServices } from '../../../build_services'; +/** + * Requests the documents for Discover. This will return a promise that will resolve + * with the documents. + */ export const fetchDocuments = ( - data$: SavedSearchData, searchSource: ISearchSource, { abortController, inspectorAdapters, - onResults, searchSessionId, services, }: { abortController: AbortController; inspectorAdapters: Adapters; - onResults: (foundDocuments: boolean) => void; searchSessionId: string; services: DiscoverServices; } ) => { - const { documents$, totalHits$ } = data$; - searchSource.setField('size', services.uiSettings.get(SAMPLE_SIZE_SETTING)); searchSource.setField('trackTotalHits', false); searchSource.setField('highlightAll', true); @@ -46,8 +42,6 @@ export const fetchDocuments = ( searchSource.setOverwriteDataViewType(undefined); } - sendLoadingMsg(documents$); - const executionContext = { type: 'application', name: 'discover', @@ -71,34 +65,10 @@ export const fetchDocuments = ( }, executionContext, }) - .pipe(filter((res) => isCompleteResponse(res))); - - fetch$.subscribe( - (res) => { - const documents = res.rawResponse.hits.hits; - - // If the total hits query is still loading for hits, emit a partial - // hit count that's at least our document count - if (totalHits$.getValue().fetchStatus === FetchStatus.LOADING) { - totalHits$.next({ - fetchStatus: FetchStatus.PARTIAL, - result: documents.length, - }); - } - - documents$.next({ - fetchStatus: FetchStatus.COMPLETE, - result: documents, - }); - onResults(documents.length > 0); - }, - (error) => { - if (error instanceof Error && error.name === 'AbortError') { - return; - } + .pipe( + filter((res) => isCompleteResponse(res)), + map((res) => res.rawResponse.hits.hits) + ); - sendErrorMsg(documents$, error); - } - ); - return fetch$; + return fetch$.toPromise(); }; diff --git a/src/plugins/discover/public/application/main/utils/fetch_total_hits.test.ts b/src/plugins/discover/public/application/main/utils/fetch_total_hits.test.ts index c593c9c157422..7b564906f95a7 100644 --- a/src/plugins/discover/public/application/main/utils/fetch_total_hits.test.ts +++ b/src/plugins/discover/public/application/main/utils/fetch_total_hits.test.ts @@ -5,76 +5,34 @@ * in compliance with, at your election, the Elastic License 2.0 or the Server * Side Public License, v 1. */ -import { FetchStatus } from '../../types'; -import { BehaviorSubject, throwError as throwErrorRx } from 'rxjs'; +import { throwError as throwErrorRx, of } from 'rxjs'; import { RequestAdapter } from '../../../../../inspector'; import { savedSearchMock } from '../../../__mocks__/saved_search'; import { fetchTotalHits } from './fetch_total_hits'; import { discoverServiceMock } from '../../../__mocks__/services'; - -function getDataSubjects() { - return { - main$: new BehaviorSubject({ fetchStatus: FetchStatus.UNINITIALIZED }), - documents$: new BehaviorSubject({ fetchStatus: FetchStatus.UNINITIALIZED }), - totalHits$: new BehaviorSubject({ fetchStatus: FetchStatus.UNINITIALIZED }), - charts$: new BehaviorSubject({ fetchStatus: FetchStatus.UNINITIALIZED }), - }; -} +import { SearchResponse } from '@elastic/elasticsearch/lib/api/types'; +import { IKibanaSearchResponse } from 'src/plugins/data/common'; + +const getDeps = () => ({ + abortController: new AbortController(), + inspectorAdapters: { requests: new RequestAdapter() }, + searchSessionId: '123', + data: discoverServiceMock.data, +}); describe('test fetchTotalHits', () => { - test('changes of fetchStatus are correct when starting with FetchStatus.UNINITIALIZED', async (done) => { - const subjects = getDataSubjects(); - const { totalHits$ } = subjects; - - const deps = { - abortController: new AbortController(), - inspectorAdapters: { requests: new RequestAdapter() }, - onResults: jest.fn(), - searchSessionId: '123', - data: discoverServiceMock.data, - }; - - const stateArr: FetchStatus[] = []; + test('resolves returned promise with hit count', async () => { + savedSearchMock.searchSource.fetch$ = () => + of({ rawResponse: { hits: { total: 45 } } } as IKibanaSearchResponse); - totalHits$.subscribe((value) => stateArr.push(value.fetchStatus)); - - fetchTotalHits(subjects, savedSearchMock.searchSource, deps).subscribe({ - complete: () => { - expect(stateArr).toEqual([ - FetchStatus.UNINITIALIZED, - FetchStatus.LOADING, - FetchStatus.COMPLETE, - ]); - done(); - }, - }); + await expect(fetchTotalHits(savedSearchMock.searchSource, getDeps())).resolves.toBe(45); }); - test('change of fetchStatus on fetch error', async (done) => { - const subjects = getDataSubjects(); - const { totalHits$ } = subjects; - const deps = { - abortController: new AbortController(), - inspectorAdapters: { requests: new RequestAdapter() }, - onResults: jest.fn(), - searchSessionId: '123', - data: discoverServiceMock.data, - }; + test('rejects in case of an error', async () => { savedSearchMock.searchSource.fetch$ = () => throwErrorRx({ msg: 'Oh noes!' }); - const stateArr: FetchStatus[] = []; - - totalHits$.subscribe((value) => stateArr.push(value.fetchStatus)); - - fetchTotalHits(subjects, savedSearchMock.searchSource, deps).subscribe({ - error: () => { - expect(stateArr).toEqual([ - FetchStatus.UNINITIALIZED, - FetchStatus.LOADING, - FetchStatus.ERROR, - ]); - done(); - }, + await expect(fetchTotalHits(savedSearchMock.searchSource, getDeps())).rejects.toEqual({ + msg: 'Oh noes!', }); }); }); diff --git a/src/plugins/discover/public/application/main/utils/fetch_total_hits.ts b/src/plugins/discover/public/application/main/utils/fetch_total_hits.ts index 197e00ce0449f..55fc9c1c17235 100644 --- a/src/plugins/discover/public/application/main/utils/fetch_total_hits.ts +++ b/src/plugins/discover/public/application/main/utils/fetch_total_hits.ts @@ -7,36 +7,23 @@ */ import { i18n } from '@kbn/i18n'; -import { filter } from 'rxjs/operators'; -import { - DataPublicPluginStart, - isCompleteResponse, - ISearchSource, -} from '../../../../../data/public'; +import { filter, map } from 'rxjs/operators'; +import { isCompleteResponse, ISearchSource } from '../../../../../data/public'; import { DataViewType } from '../../../../../data_views/common'; import { Adapters } from '../../../../../inspector/common'; -import { FetchStatus } from '../../types'; -import { SavedSearchData } from './use_saved_search'; -import { sendErrorMsg, sendLoadingMsg } from './use_saved_search_messages'; export function fetchTotalHits( - data$: SavedSearchData, searchSource: ISearchSource, { abortController, - data, inspectorAdapters, - onResults, searchSessionId, }: { abortController: AbortController; - data: DataPublicPluginStart; - onResults: (foundDocuments: boolean) => void; inspectorAdapters: Adapters; searchSessionId: string; } ) { - const { totalHits$ } = data$; searchSource.setField('trackTotalHits', true); searchSource.setField('size', 0); searchSource.removeField('sort'); @@ -50,8 +37,6 @@ export function fetchTotalHits( searchSource.setOverwriteDataViewType(undefined); } - sendLoadingMsg(totalHits$); - const executionContext = { type: 'application', name: 'discover', @@ -75,21 +60,10 @@ export function fetchTotalHits( sessionId: searchSessionId, executionContext, }) - .pipe(filter((res) => isCompleteResponse(res))); - - fetch$.subscribe( - (res) => { - const totalHitsNr = res.rawResponse.hits.total as number; - totalHits$.next({ fetchStatus: FetchStatus.COMPLETE, result: totalHitsNr }); - onResults(totalHitsNr > 0); - }, - (error) => { - if (error instanceof Error && error.name === 'AbortError') { - return; - } - sendErrorMsg(totalHits$, error); - } - ); + .pipe( + filter((res) => isCompleteResponse(res)), + map((res) => res.rawResponse.hits.total as number) + ); - return fetch$; + return fetch$.toPromise(); } diff --git a/src/plugins/discover/public/application/main/utils/use_saved_search.ts b/src/plugins/discover/public/application/main/utils/use_saved_search.ts index 0f4b9058316a0..f37fdef4bd655 100644 --- a/src/plugins/discover/public/application/main/utils/use_saved_search.ts +++ b/src/plugins/discover/public/application/main/utils/use_saved_search.ts @@ -159,7 +159,7 @@ export const useSavedSearch = ({ initialFetchStatus, }); - const subscription = fetch$.subscribe((val) => { + const subscription = fetch$.subscribe(async (val) => { if (!validateTimeRange(timefilter.getTime(), services.toastNotifications)) { return; } @@ -167,28 +167,26 @@ export const useSavedSearch = ({ refs.current.abortController?.abort(); refs.current.abortController = new AbortController(); - try { - fetchAll(dataSubjects, searchSource, val === 'reset', { - abortController: refs.current.abortController, - appStateContainer: stateContainer.appStateContainer, - inspectorAdapters, - data, - initialFetchStatus, - searchSessionId: searchSessionManager.getNextSearchSessionId(), - services, - useNewFieldsApi, - }).subscribe({ - complete: () => { - // if this function was set and is executed, another refresh fetch can be triggered - refs.current.autoRefreshDone?.(); - refs.current.autoRefreshDone = undefined; - }, - }); - } catch (error) { - main$.next({ - fetchStatus: FetchStatus.ERROR, - error, - }); + const autoRefreshDone = refs.current.autoRefreshDone; + + await fetchAll(dataSubjects, searchSource, val === 'reset', { + abortController: refs.current.abortController, + appStateContainer: stateContainer.appStateContainer, + inspectorAdapters, + data, + initialFetchStatus, + searchSessionId: searchSessionManager.getNextSearchSessionId(), + services, + useNewFieldsApi, + }); + + // If the autoRefreshCallback is still the same as when we started i.e. there was no newer call + // replacing this current one, call it to make sure we tell that the auto refresh is done + // and a new one can be scheduled. + if (autoRefreshDone === refs.current.autoRefreshDone) { + // if this function was set and is executed, another refresh fetch can be triggered + refs.current.autoRefreshDone?.(); + refs.current.autoRefreshDone = undefined; } }); diff --git a/src/plugins/discover/public/application/main/utils/use_saved_search_messages.test.ts b/src/plugins/discover/public/application/main/utils/use_saved_search_messages.test.ts index 2fa264690329e..0d74061ac46a3 100644 --- a/src/plugins/discover/public/application/main/utils/use_saved_search_messages.test.ts +++ b/src/plugins/discover/public/application/main/utils/use_saved_search_messages.test.ts @@ -9,14 +9,16 @@ import { sendCompleteMsg, sendErrorMsg, sendLoadingMsg, + sendNoResultsFoundMsg, sendPartialMsg, } from './use_saved_search_messages'; import { FetchStatus } from '../../types'; import { BehaviorSubject } from 'rxjs'; import { DataMainMsg } from './use_saved_search'; +import { filter } from 'rxjs/operators'; describe('test useSavedSearch message generators', () => { - test('sendCompleteMsg', async (done) => { + test('sendCompleteMsg', (done) => { const main$ = new BehaviorSubject({ fetchStatus: FetchStatus.LOADING }); main$.subscribe((value) => { if (value.fetchStatus !== FetchStatus.LOADING) { @@ -28,7 +30,18 @@ describe('test useSavedSearch message generators', () => { }); sendCompleteMsg(main$, true); }); - test('sendPartialMessage', async (done) => { + test('sendNoResultsFoundMsg', (done) => { + const main$ = new BehaviorSubject({ fetchStatus: FetchStatus.LOADING }); + main$ + .pipe(filter(({ fetchStatus }) => fetchStatus !== FetchStatus.LOADING)) + .subscribe((value) => { + expect(value.fetchStatus).toBe(FetchStatus.COMPLETE); + expect(value.foundDocuments).toBe(false); + done(); + }); + sendNoResultsFoundMsg(main$); + }); + test('sendPartialMessage', (done) => { const main$ = new BehaviorSubject({ fetchStatus: FetchStatus.LOADING }); main$.subscribe((value) => { if (value.fetchStatus !== FetchStatus.LOADING) { @@ -38,7 +51,7 @@ describe('test useSavedSearch message generators', () => { }); sendPartialMsg(main$); }); - test('sendLoadingMsg', async (done) => { + test('sendLoadingMsg', (done) => { const main$ = new BehaviorSubject({ fetchStatus: FetchStatus.COMPLETE }); main$.subscribe((value) => { if (value.fetchStatus !== FetchStatus.COMPLETE) { @@ -48,7 +61,7 @@ describe('test useSavedSearch message generators', () => { }); sendLoadingMsg(main$); }); - test('sendErrorMsg', async (done) => { + test('sendErrorMsg', (done) => { const main$ = new BehaviorSubject({ fetchStatus: FetchStatus.PARTIAL }); main$.subscribe((value) => { if (value.fetchStatus === FetchStatus.ERROR) { @@ -60,7 +73,7 @@ describe('test useSavedSearch message generators', () => { sendErrorMsg(main$, new Error('Pls help!')); }); - test('sendCompleteMsg cleaning error state message', async (done) => { + test('sendCompleteMsg cleaning error state message', (done) => { const initialState = { fetchStatus: FetchStatus.ERROR, error: new Error('Oh noes!'), diff --git a/src/plugins/discover/public/application/main/utils/use_saved_search_messages.ts b/src/plugins/discover/public/application/main/utils/use_saved_search_messages.ts index 325d63eb6d21a..a2d42147a9e8f 100644 --- a/src/plugins/discover/public/application/main/utils/use_saved_search_messages.ts +++ b/src/plugins/discover/public/application/main/utils/use_saved_search_messages.ts @@ -15,6 +15,15 @@ import { SavedSearchData, } from './use_saved_search'; +/** + * Sends COMPLETE message to the main$ observable with the information + * that no documents have been found, allowing Discover to show a no + * results message. + */ +export function sendNoResultsFoundMsg(main$: DataMain$) { + sendCompleteMsg(main$, false); +} + /** * Send COMPLETE message via main observable used when * 1.) first fetch resolved, and there are no documents diff --git a/src/plugins/discover/public/build_services.ts b/src/plugins/discover/public/build_services.ts index 9cc2eb78aafbe..9f21294efdfc1 100644 --- a/src/plugins/discover/public/build_services.ts +++ b/src/plugins/discover/public/build_services.ts @@ -66,7 +66,7 @@ export interface DiscoverServices { toastNotifications: ToastsStart; uiSettings: IUiSettingsClient; trackUiMetric?: (metricType: UiCounterMetricType, eventName: string | string[]) => void; - indexPatternFieldEditor: IndexPatternFieldEditorStart; + dataViewFieldEditor: IndexPatternFieldEditorStart; http: HttpStart; storage: Storage; spaces?: SpacesApi; @@ -105,7 +105,7 @@ export function buildServices( uiSettings: core.uiSettings, storage, trackUiMetric: usageCollection?.reportUiCounter.bind(usageCollection, 'discover'), - indexPatternFieldEditor: plugins.dataViewFieldEditor, + dataViewFieldEditor: plugins.dataViewFieldEditor, http: core.http, spaces: plugins.spaces, }; diff --git a/src/plugins/discover/public/components/discover_grid/discover_grid_flyout.tsx b/src/plugins/discover/public/components/discover_grid/discover_grid_flyout.tsx index 30e0cf24f7d52..27f4268224904 100644 --- a/src/plugins/discover/public/components/discover_grid/discover_grid_flyout.tsx +++ b/src/plugins/discover/public/components/discover_grid/discover_grid_flyout.tsx @@ -27,8 +27,7 @@ import { import { DocViewer } from '../../services/doc_views/components/doc_viewer/doc_viewer'; import { DocViewFilterFn } from '../../services/doc_views/doc_views_types'; import { DiscoverServices } from '../../build_services'; -import { getContextUrl } from '../../utils/get_context_url'; -import { getSingleDocUrl } from '../../utils/get_single_doc_url'; +import { useNavigationProps } from '../../utils/use_navigation_props'; import { ElasticSearchHit } from '../../types'; interface Props { @@ -103,6 +102,15 @@ export function DiscoverGridFlyout({ [activePage, setPage] ); + const { singleDocProps, surrDocsProps } = useNavigationProps({ + indexPatternId: indexPattern.id!, + rowIndex: hit._index, + rowId: hit._id, + filterManager: services.filterManager, + addBasePath: services.addBasePath, + columns, + }); + return ( {i18n.translate('discover.grid.tableRow.viewSingleDocumentLinkTextSimple', { defaultMessage: 'Single document', @@ -157,13 +165,7 @@ export function DiscoverGridFlyout({ size="xs" iconType="documents" flush="left" - href={getContextUrl( - String(hit._id), - indexPattern.id, - columns, - services.filterManager, - services.addBasePath - )} + {...surrDocsProps} data-test-subj="docTableRowAction" > {i18n.translate('discover.grid.tableRow.viewSurroundingDocumentsLinkTextSimple', { diff --git a/src/plugins/discover/public/components/doc_table/components/table_row.tsx b/src/plugins/discover/public/components/doc_table/components/table_row.tsx index 2eee9a177e4f8..2d9e8fa6e9584 100644 --- a/src/plugins/discover/public/components/doc_table/components/table_row.tsx +++ b/src/plugins/discover/public/components/doc_table/components/table_row.tsx @@ -15,12 +15,11 @@ import { flattenHit } from '../../../../../data/common'; import { DocViewer } from '../../../services/doc_views/components/doc_viewer/doc_viewer'; import { FilterManager, IndexPattern } from '../../../../../data/public'; import { TableCell } from './table_row/table_cell'; -import { DocViewFilterFn } from '../../../services/doc_views/doc_views_types'; -import { getContextUrl } from '../../../utils/get_context_url'; -import { getSingleDocUrl } from '../../../utils/get_single_doc_url'; -import { TableRowDetails } from './table_row_details'; import { formatRow, formatTopLevelObject } from '../lib/row_formatter'; +import { useNavigationProps } from '../../../utils/use_navigation_props'; +import { DocViewFilterFn } from '../../../services/doc_views/doc_views_types'; import { ElasticSearchHit } from '../../../types'; +import { TableRowDetails } from './table_row_details'; export type DocTableRow = ElasticSearchHit & { isAnchor?: boolean; @@ -100,13 +99,14 @@ export const TableRow = ({ [filter, flattenedRow, indexPattern.fields] ); - const getContextAppHref = () => { - return getContextUrl(row._id, indexPattern.id!, columns, filterManager, addBasePath); - }; - - const getSingleDocHref = () => { - return addBasePath(getSingleDocUrl(indexPattern.id!, row._index, row._id)); - }; + const { singleDocProps, surrDocsProps } = useNavigationProps({ + indexPatternId: indexPattern.id!, + rowIndex: row._index, + rowId: row._id, + filterManager, + addBasePath, + columns, + }); const rowCells = [ @@ -208,8 +208,8 @@ export const TableRow = ({ open={open} colLength={(columns.length || 1) + 2} isTimeBased={indexPattern.isTimeBased()} - getContextAppHref={getContextAppHref} - getSingleDocHref={getSingleDocHref} + singleDocProps={singleDocProps} + surrDocsProps={surrDocsProps} > string; - getSingleDocHref: () => string; + singleDocProps: DiscoverNavigationProps; + surrDocsProps: DiscoverNavigationProps; children: JSX.Element; } @@ -22,8 +23,8 @@ export const TableRowDetails = ({ open, colLength, isTimeBased, - getContextAppHref, - getSingleDocHref, + singleDocProps, + surrDocsProps, children, }: TableRowDetailsProps) => { if (!open) { @@ -54,7 +55,7 @@ export const TableRowDetails = ({ {isTimeBased && ( - + - + } > -
- - - -
- - -

- An Error Occurred -

-
- - - + + + +
+
- - -
-
- Could not fetch data at this time. Refresh the tab to try again. - +

-
- - + + + - - - - -
+ + + +

+
+
+ +
- - - -
+
+
+
+ `; diff --git a/src/plugins/discover/public/utils/breadcrumbs.ts b/src/plugins/discover/public/utils/breadcrumbs.ts index 4a3df34e2da75..4d79598ce5389 100644 --- a/src/plugins/discover/public/utils/breadcrumbs.ts +++ b/src/plugins/discover/public/utils/breadcrumbs.ts @@ -10,13 +10,13 @@ import { ChromeStart } from 'kibana/public'; import { i18n } from '@kbn/i18n'; import { SavedSearch } from '../services/saved_searches'; -export function getRootBreadcrumbs() { +export function getRootBreadcrumbs(breadcrumb?: string) { return [ { text: i18n.translate('discover.rootBreadcrumb', { defaultMessage: 'Discover', }), - href: '#/', + href: breadcrumb || '#/', }, ]; } diff --git a/src/plugins/discover/public/utils/get_context_url.test.ts b/src/plugins/discover/public/utils/get_context_url.test.ts deleted file mode 100644 index d6d1db5ca393b..0000000000000 --- a/src/plugins/discover/public/utils/get_context_url.test.ts +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0 and the Server Side Public License, v 1; you may not use this file except - * in compliance with, at your election, the Elastic License 2.0 or the Server - * Side Public License, v 1. - */ - -import { getContextUrl } from './get_context_url'; -import { FilterManager } from '../../../data/public/query/filter_manager'; -const filterManager = { - getGlobalFilters: () => [], - getAppFilters: () => [], -} as unknown as FilterManager; -const addBasePath = (path: string) => `/base${path}`; - -describe('Get context url', () => { - test('returning a valid context url', async () => { - const url = await getContextUrl( - 'docId', - 'ipId', - ['test1', 'test2'], - filterManager, - addBasePath - ); - expect(url).toMatchInlineSnapshot( - `"/base/app/discover#/context/ipId/docId?_g=(filters:!())&_a=(columns:!(test1,test2),filters:!())"` - ); - }); - - test('returning a valid context url when docId contains whitespace', async () => { - const url = await getContextUrl( - 'doc Id', - 'ipId', - ['test1', 'test2'], - filterManager, - addBasePath - ); - expect(url).toMatchInlineSnapshot( - `"/base/app/discover#/context/ipId/doc%20Id?_g=(filters:!())&_a=(columns:!(test1,test2),filters:!())"` - ); - }); -}); diff --git a/src/plugins/discover/public/utils/get_context_url.tsx b/src/plugins/discover/public/utils/get_context_url.tsx deleted file mode 100644 index 68c0e935f17e9..0000000000000 --- a/src/plugins/discover/public/utils/get_context_url.tsx +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0 and the Server Side Public License, v 1; you may not use this file except - * in compliance with, at your election, the Elastic License 2.0 or the Server - * Side Public License, v 1. - */ - -import { stringify } from 'query-string'; -import rison from 'rison-node'; -import { url } from '../../../kibana_utils/common'; -import { esFilters, FilterManager } from '../../../data/public'; -import { DiscoverServices } from '../build_services'; - -/** - * Helper function to generate an URL to a document in Discover's context view - */ -export function getContextUrl( - documentId: string, - indexPatternId: string, - columns: string[], - filterManager: FilterManager, - addBasePath: DiscoverServices['addBasePath'] -) { - const globalFilters = filterManager.getGlobalFilters(); - const appFilters = filterManager.getAppFilters(); - - const hash = stringify( - url.encodeQuery({ - _g: rison.encode({ - filters: globalFilters || [], - }), - _a: rison.encode({ - columns, - filters: (appFilters || []).map(esFilters.disableFilter), - }), - }), - { encode: false, sort: false } - ); - - return addBasePath( - `/app/discover#/context/${encodeURIComponent(indexPatternId)}/${encodeURIComponent( - documentId - )}?${hash}` - ); -} diff --git a/src/plugins/discover/public/utils/use_navigation_props.test.tsx b/src/plugins/discover/public/utils/use_navigation_props.test.tsx new file mode 100644 index 0000000000000..29d4976f265c3 --- /dev/null +++ b/src/plugins/discover/public/utils/use_navigation_props.test.tsx @@ -0,0 +1,101 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import React, { ReactElement } from 'react'; +import { renderHook } from '@testing-library/react-hooks'; +import { createFilterManagerMock } from '../../../data/public/query/filter_manager/filter_manager.mock'; +import { + getContextHash, + HistoryState, + useNavigationProps, + UseNavigationProps, +} from './use_navigation_props'; +import { Router } from 'react-router-dom'; +import { createMemoryHistory } from 'history'; +import { setServices } from '../kibana_services'; +import { DiscoverServices } from '../build_services'; + +const filterManager = createFilterManagerMock(); +const defaultProps = { + indexPatternId: 'ff959d40-b880-11e8-a6d9-e546fe2bba5f', + rowIndex: 'kibana_sample_data_ecommerce', + rowId: 'QmsYdX0BQ6gV8MTfoPYE', + columns: ['customer_first_name', 'products.manufacturer'], + filterManager, + addBasePath: jest.fn(), +} as UseNavigationProps; +const basePathPrefix = 'localhost:5601/xqj'; + +const getSearch = () => { + return `?_g=(filters:!(),refreshInterval:(pause:!t,value:0),time:(from:now-15m,to:now)) + &_a=(columns:!(${defaultProps.columns.join()}),filters:!(),index:${defaultProps.indexPatternId} + ,interval:auto,query:(language:kuery,query:''),sort:!(!(order_date,desc)))`; +}; + +const getSingeDocRoute = () => { + return `/doc/${defaultProps.indexPatternId}/${defaultProps.rowIndex}`; +}; + +const getContextRoute = () => { + return `/context/${defaultProps.indexPatternId}/${defaultProps.rowId}`; +}; + +const render = () => { + const history = createMemoryHistory({ + initialEntries: ['/' + getSearch()], + }); + setServices({ history: () => history } as unknown as DiscoverServices); + const wrapper = ({ children }: { children: ReactElement }) => ( + {children} + ); + return { + result: renderHook(() => useNavigationProps(defaultProps), { wrapper }).result, + history, + }; +}; + +describe('useNavigationProps', () => { + test('should provide valid breadcrumb for single doc page from main view', () => { + const { result, history } = render(); + + result.current.singleDocProps.onClick?.(); + expect(history.location.pathname).toEqual(getSingeDocRoute()); + expect(history.location.search).toEqual(`?id=${defaultProps.rowId}`); + expect(history.location.state?.breadcrumb).toEqual(`#/${getSearch()}`); + }); + + test('should provide valid breadcrumb for context page from main view', () => { + const { result, history } = render(); + + result.current.surrDocsProps.onClick?.(); + expect(history.location.pathname).toEqual(getContextRoute()); + expect(history.location.search).toEqual( + `?${getContextHash(defaultProps.columns, filterManager)}` + ); + expect(history.location.state?.breadcrumb).toEqual(`#/${getSearch()}`); + }); + + test('should create valid links to the context and single doc pages from embeddable', () => { + const { result } = renderHook(() => + useNavigationProps({ + ...defaultProps, + addBasePath: (val: string) => `${basePathPrefix}${val}`, + }) + ); + + expect(result.current.singleDocProps.href!).toEqual( + `${basePathPrefix}/app/discover#${getSingeDocRoute()}?id=${defaultProps.rowId}` + ); + expect(result.current.surrDocsProps.href!).toEqual( + `${basePathPrefix}/app/discover#${getContextRoute()}?${getContextHash( + defaultProps.columns, + filterManager + )}` + ); + }); +}); diff --git a/src/plugins/discover/public/utils/use_navigation_props.tsx b/src/plugins/discover/public/utils/use_navigation_props.tsx new file mode 100644 index 0000000000000..6f1dedf75e730 --- /dev/null +++ b/src/plugins/discover/public/utils/use_navigation_props.tsx @@ -0,0 +1,132 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { useMemo, useRef } from 'react'; +import { useHistory, matchPath } from 'react-router-dom'; +import { stringify } from 'query-string'; +import rison from 'rison-node'; +import { esFilters, FilterManager } from '../../../data/public'; +import { url } from '../../../kibana_utils/common'; +import { getServices } from '../kibana_services'; + +export type DiscoverNavigationProps = { onClick: () => void } | { href: string }; + +export interface UseNavigationProps { + indexPatternId: string; + rowIndex: string; + rowId: string; + columns: string[]; + filterManager: FilterManager; + addBasePath: (url: string) => string; +} + +export type HistoryState = { breadcrumb?: string } | undefined; + +export const getContextHash = (columns: string[], filterManager: FilterManager) => { + const globalFilters = filterManager.getGlobalFilters(); + const appFilters = filterManager.getAppFilters(); + + const hash = stringify( + url.encodeQuery({ + _g: rison.encode({ + filters: globalFilters || [], + }), + _a: rison.encode({ + columns, + filters: (appFilters || []).map(esFilters.disableFilter), + }), + }), + { encode: false, sort: false } + ); + + return hash; +}; + +/** + * When it's context route, breadcrumb link should point to the main discover page anyway. + * Otherwise, we are on main page and should create breadcrumb link from it. + * Current history object should be used in callback, since url state might be changed + * after expanded document opened. + */ +const getCurrentBreadcrumbs = (isContextRoute: boolean, prevBreadcrumb?: string) => { + const { history: getHistory } = getServices(); + const currentHistory = getHistory(); + return isContextRoute + ? prevBreadcrumb + : '#' + currentHistory?.location.pathname + currentHistory?.location.search; +}; + +export const useMainRouteBreadcrumb = () => { + // useRef needed to retrieve initial breadcrumb link from the push state without updates + return useRef(useHistory().location.state?.breadcrumb).current; +}; + +export const useNavigationProps = ({ + indexPatternId, + rowIndex, + rowId, + columns, + filterManager, + addBasePath, +}: UseNavigationProps) => { + const history = useHistory(); + const prevBreadcrumb = useRef(history?.location.state?.breadcrumb).current; + const contextSearchHash = useMemo( + () => getContextHash(columns, filterManager), + [columns, filterManager] + ); + + /** + * When history can be accessed via hooks, + * it is discover main or context route. + */ + if (!!history) { + const isContextRoute = matchPath(history.location.pathname, { + path: '/context/:indexPatternId/:id', + exact: true, + }); + + const onOpenSingleDoc = () => { + history.push({ + pathname: `/doc/${indexPatternId}/${rowIndex}`, + search: `?id=${encodeURIComponent(rowId)}`, + state: { breadcrumb: getCurrentBreadcrumbs(!!isContextRoute, prevBreadcrumb) }, + }); + }; + + const onOpenSurrDocs = () => + history.push({ + pathname: `/context/${encodeURIComponent(indexPatternId)}/${encodeURIComponent( + String(rowId) + )}`, + search: `?${contextSearchHash}`, + state: { breadcrumb: getCurrentBreadcrumbs(!!isContextRoute, prevBreadcrumb) }, + }); + + return { + singleDocProps: { onClick: onOpenSingleDoc }, + surrDocsProps: { onClick: onOpenSurrDocs }, + }; + } + + // for embeddable absolute href should be kept + return { + singleDocProps: { + href: addBasePath( + `/app/discover#/doc/${indexPatternId}/${rowIndex}?id=${encodeURIComponent(rowId)}` + ), + }, + surrDocsProps: { + href: addBasePath( + `/app/discover#/context/${encodeURIComponent(indexPatternId)}/${encodeURIComponent( + rowId + )}?${contextSearchHash}` + ), + }, + }; +}; diff --git a/src/plugins/expressions/common/execution/execution.test.ts b/src/plugins/expressions/common/execution/execution.test.ts index 9b889c62e9ff5..6dab9f7c683ed 100644 --- a/src/plugins/expressions/common/execution/execution.test.ts +++ b/src/plugins/expressions/common/execution/execution.test.ts @@ -310,6 +310,13 @@ describe('Execution', () => { const { result } = await run('var name="foo"', { variables }); expect(result).toBe('bar'); }); + + test('can access variables set from the parent expression', async () => { + const { result } = await run( + 'var_set name="a" value="bar" | var_set name="b" value={var name="a"} | var name="b"' + ); + expect(result).toBe('bar'); + }); }); describe('inspector adapters', () => { diff --git a/src/plugins/expressions/common/execution/execution.ts b/src/plugins/expressions/common/execution/execution.ts index c81e398dbc9e8..f355710698b69 100644 --- a/src/plugins/expressions/common/execution/execution.ts +++ b/src/plugins/expressions/common/execution/execution.ts @@ -542,10 +542,10 @@ export class Execution< interpret(ast: ExpressionAstNode, input: T): Observable> { switch (getType(ast)) { case 'expression': - const execution = this.execution.executor.createExecution( - ast as ExpressionAstExpression, - this.execution.params - ); + const execution = this.execution.executor.createExecution(ast as ExpressionAstExpression, { + ...this.execution.params, + variables: this.context.variables, + }); this.childExecutions.push(execution); return execution.start(input, true); diff --git a/src/plugins/home/server/plugin.ts b/src/plugins/home/server/plugin.ts index 6f082dd561e93..04d2d80898b50 100644 --- a/src/plugins/home/server/plugin.ts +++ b/src/plugins/home/server/plugin.ts @@ -27,12 +27,13 @@ export interface HomeServerPluginSetupDependencies { } export class HomeServerPlugin implements Plugin { - private readonly tutorialsRegistry = new TutorialsRegistry(); + private readonly tutorialsRegistry; private readonly sampleDataRegistry: SampleDataRegistry; private customIntegrations?: CustomIntegrationsPluginSetup; constructor(private readonly initContext: PluginInitializerContext) { this.sampleDataRegistry = new SampleDataRegistry(this.initContext); + this.tutorialsRegistry = new TutorialsRegistry(this.initContext); } public setup(core: CoreSetup, plugins: HomeServerPluginSetupDependencies): HomeServerPluginSetup { diff --git a/src/plugins/home/server/services/tutorials/lib/tutorials_registry_types.ts b/src/plugins/home/server/services/tutorials/lib/tutorials_registry_types.ts index 4c80c8858a475..aeebecf6cab32 100644 --- a/src/plugins/home/server/services/tutorials/lib/tutorials_registry_types.ts +++ b/src/plugins/home/server/services/tutorials/lib/tutorials_registry_types.ts @@ -29,6 +29,7 @@ export enum TutorialsCategory { export type Platform = 'WINDOWS' | 'OSX' | 'DEB' | 'RPM'; export interface TutorialContext { + kibanaBranch: string; [key: string]: unknown; } export type TutorialProvider = (context: TutorialContext) => TutorialSchema; diff --git a/src/plugins/home/server/services/tutorials/tutorials_registry.test.ts b/src/plugins/home/server/services/tutorials/tutorials_registry.test.ts index ee73c8e13f62b..dec1d23e05787 100644 --- a/src/plugins/home/server/services/tutorials/tutorials_registry.test.ts +++ b/src/plugins/home/server/services/tutorials/tutorials_registry.test.ts @@ -69,6 +69,7 @@ const validTutorialProvider = VALID_TUTORIAL; describe('TutorialsRegistry', () => { let mockCoreSetup: MockedKeys; + let mockInitContext: ReturnType; let testProvider: TutorialProvider; let testScopedTutorialContextFactory: ScopedTutorialContextFactory; let mockCustomIntegrationsPluginSetup: jest.Mocked; @@ -80,6 +81,7 @@ describe('TutorialsRegistry', () => { describe('GET /api/kibana/home/tutorials', () => { beforeEach(() => { mockCoreSetup = coreMock.createSetup(); + mockInitContext = coreMock.createPluginInitializerContext(); }); test('has a router that retrieves registered tutorials', () => { @@ -90,13 +92,19 @@ describe('TutorialsRegistry', () => { describe('setup', () => { test('exposes proper contract', () => { - const setup = new TutorialsRegistry().setup(mockCoreSetup, mockCustomIntegrationsPluginSetup); + const setup = new TutorialsRegistry(mockInitContext).setup( + mockCoreSetup, + mockCustomIntegrationsPluginSetup + ); expect(setup).toHaveProperty('registerTutorial'); expect(setup).toHaveProperty('addScopedTutorialContextFactory'); }); test('registerTutorial throws when registering a tutorial with an invalid schema', () => { - const setup = new TutorialsRegistry().setup(mockCoreSetup, mockCustomIntegrationsPluginSetup); + const setup = new TutorialsRegistry(mockInitContext).setup( + mockCoreSetup, + mockCustomIntegrationsPluginSetup + ); testProvider = ({}) => invalidTutorialProvider; expect(() => setup.registerTutorial(testProvider)).toThrowErrorMatchingInlineSnapshot( `"Unable to register tutorial spec because its invalid. Error: [name]: is not allowed to be empty"` @@ -104,7 +112,10 @@ describe('TutorialsRegistry', () => { }); test('registerTutorial registers a tutorial with a valid schema', () => { - const setup = new TutorialsRegistry().setup(mockCoreSetup, mockCustomIntegrationsPluginSetup); + const setup = new TutorialsRegistry(mockInitContext).setup( + mockCoreSetup, + mockCustomIntegrationsPluginSetup + ); testProvider = ({}) => validTutorialProvider; expect(() => setup.registerTutorial(testProvider)).not.toThrowError(); expect(mockCustomIntegrationsPluginSetup.registerCustomIntegration.mock.calls).toEqual([ @@ -129,7 +140,10 @@ describe('TutorialsRegistry', () => { }); test('addScopedTutorialContextFactory throws when given a scopedTutorialContextFactory that is not a function', () => { - const setup = new TutorialsRegistry().setup(mockCoreSetup, mockCustomIntegrationsPluginSetup); + const setup = new TutorialsRegistry(mockInitContext).setup( + mockCoreSetup, + mockCustomIntegrationsPluginSetup + ); const testItem = {} as TutorialProvider; expect(() => setup.addScopedTutorialContextFactory(testItem) @@ -139,7 +153,10 @@ describe('TutorialsRegistry', () => { }); test('addScopedTutorialContextFactory adds a scopedTutorialContextFactory when given a function', () => { - const setup = new TutorialsRegistry().setup(mockCoreSetup, mockCustomIntegrationsPluginSetup); + const setup = new TutorialsRegistry(mockInitContext).setup( + mockCoreSetup, + mockCustomIntegrationsPluginSetup + ); testScopedTutorialContextFactory = ({}) => 'string'; expect(() => setup.addScopedTutorialContextFactory(testScopedTutorialContextFactory) @@ -149,7 +166,7 @@ describe('TutorialsRegistry', () => { describe('start', () => { test('exposes proper contract', () => { - const start = new TutorialsRegistry().start( + const start = new TutorialsRegistry(mockInitContext).start( coreMock.createStart(), mockCustomIntegrationsPluginSetup ); diff --git a/src/plugins/home/server/services/tutorials/tutorials_registry.ts b/src/plugins/home/server/services/tutorials/tutorials_registry.ts index 723c92e6dfaf4..7d93a57b2073d 100644 --- a/src/plugins/home/server/services/tutorials/tutorials_registry.ts +++ b/src/plugins/home/server/services/tutorials/tutorials_registry.ts @@ -6,11 +6,12 @@ * Side Public License, v 1. */ -import { CoreSetup, CoreStart } from 'src/core/server'; +import { CoreSetup, CoreStart, PluginInitializerContext } from 'src/core/server'; import { TutorialProvider, TutorialContextFactory, ScopedTutorialContextFactory, + TutorialContext, } from './lib/tutorials_registry_types'; import { TutorialSchema, tutorialSchema } from './lib/tutorial_schema'; import { builtInTutorials } from '../../tutorials/register'; @@ -71,12 +72,14 @@ export class TutorialsRegistry { private tutorialProviders: TutorialProvider[] = []; // pre-register all the tutorials we know we want in here private readonly scopedTutorialContextFactories: TutorialContextFactory[] = []; + constructor(private readonly initContext: PluginInitializerContext) {} + public setup(core: CoreSetup, customIntegrations?: CustomIntegrationsPluginSetup) { const router = core.http.createRouter(); router.get( { path: '/api/kibana/home/tutorials', validate: false }, async (context, req, res) => { - const initialContext = {}; + const initialContext = this.baseTutorialContext; const scopedContext = this.scopedTutorialContextFactories.reduce( (accumulatedContext, contextFactory) => { return { ...accumulatedContext, ...contextFactory(req) }; @@ -92,7 +95,7 @@ export class TutorialsRegistry { ); return { registerTutorial: (specProvider: TutorialProvider) => { - const emptyContext = {}; + const emptyContext = this.baseTutorialContext; let tutorial: TutorialSchema; try { tutorial = tutorialSchema.validate(specProvider(emptyContext)); @@ -132,12 +135,16 @@ export class TutorialsRegistry { if (customIntegrations) { builtInTutorials.forEach((provider) => { - const tutorial = provider({}); + const tutorial = provider(this.baseTutorialContext); registerBeatsTutorialsWithCustomIntegrations(core, customIntegrations, tutorial); }); } return {}; } + + private get baseTutorialContext(): TutorialContext { + return { kibanaBranch: this.initContext.env.packageInfo.branch }; + } } /** @public */ diff --git a/src/plugins/home/server/tutorials/activemq_logs/index.ts b/src/plugins/home/server/tutorials/activemq_logs/index.ts index a277b37838562..cc84f9a536b22 100644 --- a/src/plugins/home/server/tutorials/activemq_logs/index.ts +++ b/src/plugins/home/server/tutorials/activemq_logs/index.ts @@ -56,8 +56,8 @@ export function activemqLogsSpecProvider(context: TutorialContext): TutorialSche completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/activemq_logs/screenshot.png', onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['web'], }; } diff --git a/src/plugins/home/server/tutorials/activemq_metrics/index.ts b/src/plugins/home/server/tutorials/activemq_metrics/index.ts index 9a001c149cda0..9c98c9c2ffc7a 100644 --- a/src/plugins/home/server/tutorials/activemq_metrics/index.ts +++ b/src/plugins/home/server/tutorials/activemq_metrics/index.ts @@ -54,8 +54,8 @@ export function activemqMetricsSpecProvider(context: TutorialContext): TutorialS }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['web'], }; diff --git a/src/plugins/home/server/tutorials/aerospike_metrics/index.ts b/src/plugins/home/server/tutorials/aerospike_metrics/index.ts index 3e574f2c75496..1cc350af579cb 100644 --- a/src/plugins/home/server/tutorials/aerospike_metrics/index.ts +++ b/src/plugins/home/server/tutorials/aerospike_metrics/index.ts @@ -54,8 +54,8 @@ export function aerospikeMetricsSpecProvider(context: TutorialContext): Tutorial }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['web'], }; } diff --git a/src/plugins/home/server/tutorials/apache_logs/index.ts b/src/plugins/home/server/tutorials/apache_logs/index.ts index 6e588fd86588d..aea8e3c188d94 100644 --- a/src/plugins/home/server/tutorials/apache_logs/index.ts +++ b/src/plugins/home/server/tutorials/apache_logs/index.ts @@ -57,8 +57,8 @@ export function apacheLogsSpecProvider(context: TutorialContext): TutorialSchema completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/apache_logs/screenshot.png', onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['web'], }; } diff --git a/src/plugins/home/server/tutorials/apache_metrics/index.ts b/src/plugins/home/server/tutorials/apache_metrics/index.ts index 17b495d1460c5..0af719610c24d 100644 --- a/src/plugins/home/server/tutorials/apache_metrics/index.ts +++ b/src/plugins/home/server/tutorials/apache_metrics/index.ts @@ -56,8 +56,8 @@ export function apacheMetricsSpecProvider(context: TutorialContext): TutorialSch completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/apache_metrics/screenshot.png', onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['web'], }; } diff --git a/src/plugins/home/server/tutorials/auditbeat/index.ts b/src/plugins/home/server/tutorials/auditbeat/index.ts index 96e5d4bcda393..666fcf15635c3 100644 --- a/src/plugins/home/server/tutorials/auditbeat/index.ts +++ b/src/plugins/home/server/tutorials/auditbeat/index.ts @@ -56,8 +56,8 @@ processes, users, logins, sockets information, file accesses, and more. \ completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/auditbeat/screenshot.png', onPrem: onPremInstructions(platforms, context), - elasticCloud: cloudInstructions(platforms), - onPremElasticCloud: onPremCloudInstructions(platforms), + elasticCloud: cloudInstructions(platforms, context), + onPremElasticCloud: onPremCloudInstructions(platforms, context), integrationBrowserCategories: ['web'], }; } diff --git a/src/plugins/home/server/tutorials/auditd_logs/index.ts b/src/plugins/home/server/tutorials/auditd_logs/index.ts index 6993196d93417..24857045ccc28 100644 --- a/src/plugins/home/server/tutorials/auditd_logs/index.ts +++ b/src/plugins/home/server/tutorials/auditd_logs/index.ts @@ -57,8 +57,8 @@ export function auditdLogsSpecProvider(context: TutorialContext): TutorialSchema completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/auditd_logs/screenshot.png', onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['os_system'], }; } diff --git a/src/plugins/home/server/tutorials/aws_logs/index.ts b/src/plugins/home/server/tutorials/aws_logs/index.ts index 62fbcc4eebc18..60187490318ae 100644 --- a/src/plugins/home/server/tutorials/aws_logs/index.ts +++ b/src/plugins/home/server/tutorials/aws_logs/index.ts @@ -57,8 +57,8 @@ export function awsLogsSpecProvider(context: TutorialContext): TutorialSchema { completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/aws_logs/screenshot.png', onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['aws', 'cloud', 'datastore', 'security', 'network'], }; } diff --git a/src/plugins/home/server/tutorials/aws_metrics/index.ts b/src/plugins/home/server/tutorials/aws_metrics/index.ts index 6bf1bf64bff9f..6541b4f5f29c8 100644 --- a/src/plugins/home/server/tutorials/aws_metrics/index.ts +++ b/src/plugins/home/server/tutorials/aws_metrics/index.ts @@ -58,8 +58,8 @@ export function awsMetricsSpecProvider(context: TutorialContext): TutorialSchema completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/aws_metrics/screenshot.png', onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['aws', 'cloud', 'datastore', 'security', 'network'], }; } diff --git a/src/plugins/home/server/tutorials/azure_logs/index.ts b/src/plugins/home/server/tutorials/azure_logs/index.ts index 3c9438d9a6298..163496813567a 100644 --- a/src/plugins/home/server/tutorials/azure_logs/index.ts +++ b/src/plugins/home/server/tutorials/azure_logs/index.ts @@ -58,8 +58,8 @@ export function azureLogsSpecProvider(context: TutorialContext): TutorialSchema completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/azure_logs/screenshot.png', onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['azure', 'cloud', 'network', 'security'], }; } diff --git a/src/plugins/home/server/tutorials/azure_metrics/index.ts b/src/plugins/home/server/tutorials/azure_metrics/index.ts index 310f954104634..edf4062812b42 100644 --- a/src/plugins/home/server/tutorials/azure_metrics/index.ts +++ b/src/plugins/home/server/tutorials/azure_metrics/index.ts @@ -57,8 +57,8 @@ export function azureMetricsSpecProvider(context: TutorialContext): TutorialSche completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/azure_metrics/screenshot.png', onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['azure', 'cloud', 'network', 'security'], }; } diff --git a/src/plugins/home/server/tutorials/barracuda_logs/index.ts b/src/plugins/home/server/tutorials/barracuda_logs/index.ts index cdfd75b9728b9..7cf333ec6f7e5 100644 --- a/src/plugins/home/server/tutorials/barracuda_logs/index.ts +++ b/src/plugins/home/server/tutorials/barracuda_logs/index.ts @@ -55,8 +55,8 @@ export function barracudaLogsSpecProvider(context: TutorialContext): TutorialSch }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['network', 'security'], }; } diff --git a/src/plugins/home/server/tutorials/bluecoat_logs/index.ts b/src/plugins/home/server/tutorials/bluecoat_logs/index.ts index a7db5b04ee40d..f35cd0ac4e450 100644 --- a/src/plugins/home/server/tutorials/bluecoat_logs/index.ts +++ b/src/plugins/home/server/tutorials/bluecoat_logs/index.ts @@ -54,8 +54,8 @@ export function bluecoatLogsSpecProvider(context: TutorialContext): TutorialSche }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['network', 'security'], }; } diff --git a/src/plugins/home/server/tutorials/cef_logs/index.ts b/src/plugins/home/server/tutorials/cef_logs/index.ts index 1366198d610d7..bf1f402a09a65 100644 --- a/src/plugins/home/server/tutorials/cef_logs/index.ts +++ b/src/plugins/home/server/tutorials/cef_logs/index.ts @@ -61,8 +61,8 @@ export function cefLogsSpecProvider(context: TutorialContext): TutorialSchema { }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['network', 'security'], }; } diff --git a/src/plugins/home/server/tutorials/ceph_metrics/index.ts b/src/plugins/home/server/tutorials/ceph_metrics/index.ts index 6a53789d26f7c..e7d2c67ec2a99 100644 --- a/src/plugins/home/server/tutorials/ceph_metrics/index.ts +++ b/src/plugins/home/server/tutorials/ceph_metrics/index.ts @@ -54,8 +54,8 @@ export function cephMetricsSpecProvider(context: TutorialContext): TutorialSchem }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['network', 'security'], }; } diff --git a/src/plugins/home/server/tutorials/checkpoint_logs/index.ts b/src/plugins/home/server/tutorials/checkpoint_logs/index.ts index b5ea6be42403b..83ce8d27ec861 100644 --- a/src/plugins/home/server/tutorials/checkpoint_logs/index.ts +++ b/src/plugins/home/server/tutorials/checkpoint_logs/index.ts @@ -54,8 +54,8 @@ export function checkpointLogsSpecProvider(context: TutorialContext): TutorialSc }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['security'], }; } diff --git a/src/plugins/home/server/tutorials/cisco_logs/index.ts b/src/plugins/home/server/tutorials/cisco_logs/index.ts index 922cfbf1e23ee..3c855996873af 100644 --- a/src/plugins/home/server/tutorials/cisco_logs/index.ts +++ b/src/plugins/home/server/tutorials/cisco_logs/index.ts @@ -57,8 +57,8 @@ export function ciscoLogsSpecProvider(context: TutorialContext): TutorialSchema completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/cisco_logs/screenshot.png', onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['network', 'security'], }; } diff --git a/src/plugins/home/server/tutorials/cloudwatch_logs/index.ts b/src/plugins/home/server/tutorials/cloudwatch_logs/index.ts index 5564d11be4d19..a4172fae4ff4d 100644 --- a/src/plugins/home/server/tutorials/cloudwatch_logs/index.ts +++ b/src/plugins/home/server/tutorials/cloudwatch_logs/index.ts @@ -51,8 +51,8 @@ export function cloudwatchLogsSpecProvider(context: TutorialContext): TutorialSc }, completionTimeMinutes: 10, onPrem: onPremInstructions([], context), - elasticCloud: cloudInstructions(), - onPremElasticCloud: onPremCloudInstructions(), + elasticCloud: cloudInstructions(context), + onPremElasticCloud: onPremCloudInstructions(context), integrationBrowserCategories: ['aws', 'cloud', 'datastore', 'security', 'network'], }; } diff --git a/src/plugins/home/server/tutorials/cockroachdb_metrics/index.ts b/src/plugins/home/server/tutorials/cockroachdb_metrics/index.ts index 535c8aaa90768..d53fd7f1f73aa 100644 --- a/src/plugins/home/server/tutorials/cockroachdb_metrics/index.ts +++ b/src/plugins/home/server/tutorials/cockroachdb_metrics/index.ts @@ -59,8 +59,8 @@ export function cockroachdbMetricsSpecProvider(context: TutorialContext): Tutori completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/cockroachdb_metrics/screenshot.png', onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['security', 'network', 'web'], }; } diff --git a/src/plugins/home/server/tutorials/consul_metrics/index.ts b/src/plugins/home/server/tutorials/consul_metrics/index.ts index ca7179d55fd89..26fff9e58f511 100644 --- a/src/plugins/home/server/tutorials/consul_metrics/index.ts +++ b/src/plugins/home/server/tutorials/consul_metrics/index.ts @@ -56,8 +56,8 @@ export function consulMetricsSpecProvider(context: TutorialContext): TutorialSch completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/consul_metrics/screenshot.png', onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['security', 'network', 'web'], }; } diff --git a/src/plugins/home/server/tutorials/coredns_logs/index.ts b/src/plugins/home/server/tutorials/coredns_logs/index.ts index 1261c67135001..876e6e09d61d6 100644 --- a/src/plugins/home/server/tutorials/coredns_logs/index.ts +++ b/src/plugins/home/server/tutorials/coredns_logs/index.ts @@ -57,8 +57,8 @@ export function corednsLogsSpecProvider(context: TutorialContext): TutorialSchem completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/coredns_logs/screenshot.png', onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['security', 'network', 'web'], }; } diff --git a/src/plugins/home/server/tutorials/coredns_metrics/index.ts b/src/plugins/home/server/tutorials/coredns_metrics/index.ts index 3abc14314a6ba..b854f4d448361 100644 --- a/src/plugins/home/server/tutorials/coredns_metrics/index.ts +++ b/src/plugins/home/server/tutorials/coredns_metrics/index.ts @@ -54,8 +54,8 @@ export function corednsMetricsSpecProvider(context: TutorialContext): TutorialSc completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/coredns_metrics/screenshot.png', onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['security', 'network', 'web'], }; } diff --git a/src/plugins/home/server/tutorials/couchbase_metrics/index.ts b/src/plugins/home/server/tutorials/couchbase_metrics/index.ts index 5c29aa2d9a524..2a71a6d0457f1 100644 --- a/src/plugins/home/server/tutorials/couchbase_metrics/index.ts +++ b/src/plugins/home/server/tutorials/couchbase_metrics/index.ts @@ -54,8 +54,8 @@ export function couchbaseMetricsSpecProvider(context: TutorialContext): Tutorial }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['security', 'network', 'web'], }; } diff --git a/src/plugins/home/server/tutorials/couchdb_metrics/index.ts b/src/plugins/home/server/tutorials/couchdb_metrics/index.ts index 00bea11d13d99..a379b3b04f4c7 100644 --- a/src/plugins/home/server/tutorials/couchdb_metrics/index.ts +++ b/src/plugins/home/server/tutorials/couchdb_metrics/index.ts @@ -59,8 +59,8 @@ export function couchdbMetricsSpecProvider(context: TutorialContext): TutorialSc completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/couchdb_metrics/screenshot.png', onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['security', 'network', 'web'], }; } diff --git a/src/plugins/home/server/tutorials/crowdstrike_logs/index.ts b/src/plugins/home/server/tutorials/crowdstrike_logs/index.ts index a48ed4288210b..2c5a32b63f75f 100644 --- a/src/plugins/home/server/tutorials/crowdstrike_logs/index.ts +++ b/src/plugins/home/server/tutorials/crowdstrike_logs/index.ts @@ -58,8 +58,8 @@ export function crowdstrikeLogsSpecProvider(context: TutorialContext): TutorialS }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['security'], }; } diff --git a/src/plugins/home/server/tutorials/cylance_logs/index.ts b/src/plugins/home/server/tutorials/cylance_logs/index.ts index 64b79a41cd2e0..d8b72963678fa 100644 --- a/src/plugins/home/server/tutorials/cylance_logs/index.ts +++ b/src/plugins/home/server/tutorials/cylance_logs/index.ts @@ -54,8 +54,8 @@ export function cylanceLogsSpecProvider(context: TutorialContext): TutorialSchem }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['security'], }; } diff --git a/src/plugins/home/server/tutorials/docker_metrics/index.ts b/src/plugins/home/server/tutorials/docker_metrics/index.ts index ab80e6d644dbc..e36d590650454 100644 --- a/src/plugins/home/server/tutorials/docker_metrics/index.ts +++ b/src/plugins/home/server/tutorials/docker_metrics/index.ts @@ -56,8 +56,8 @@ export function dockerMetricsSpecProvider(context: TutorialContext): TutorialSch completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/docker_metrics/screenshot.png', onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['containers', 'os_system'], }; } diff --git a/src/plugins/home/server/tutorials/dropwizard_metrics/index.ts b/src/plugins/home/server/tutorials/dropwizard_metrics/index.ts index 9864d376966bb..f01119e6ba1d2 100644 --- a/src/plugins/home/server/tutorials/dropwizard_metrics/index.ts +++ b/src/plugins/home/server/tutorials/dropwizard_metrics/index.ts @@ -54,8 +54,8 @@ export function dropwizardMetricsSpecProvider(context: TutorialContext): Tutoria }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['elastic_stack', 'datastore'], }; } diff --git a/src/plugins/home/server/tutorials/elasticsearch_logs/index.ts b/src/plugins/home/server/tutorials/elasticsearch_logs/index.ts index 6415781d02c06..a1df2d8a4085e 100644 --- a/src/plugins/home/server/tutorials/elasticsearch_logs/index.ts +++ b/src/plugins/home/server/tutorials/elasticsearch_logs/index.ts @@ -56,8 +56,8 @@ export function elasticsearchLogsSpecProvider(context: TutorialContext): Tutoria completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/elasticsearch_logs/screenshot.png', onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['containers', 'os_system'], }; } diff --git a/src/plugins/home/server/tutorials/elasticsearch_metrics/index.ts b/src/plugins/home/server/tutorials/elasticsearch_metrics/index.ts index 3961d7f78c86c..009e441c725d9 100644 --- a/src/plugins/home/server/tutorials/elasticsearch_metrics/index.ts +++ b/src/plugins/home/server/tutorials/elasticsearch_metrics/index.ts @@ -54,8 +54,8 @@ export function elasticsearchMetricsSpecProvider(context: TutorialContext): Tuto }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['elastic_stack', 'datastore'], }; } diff --git a/src/plugins/home/server/tutorials/envoyproxy_logs/index.ts b/src/plugins/home/server/tutorials/envoyproxy_logs/index.ts index 55c85a5bdd2a4..d39b182b81eaf 100644 --- a/src/plugins/home/server/tutorials/envoyproxy_logs/index.ts +++ b/src/plugins/home/server/tutorials/envoyproxy_logs/index.ts @@ -60,8 +60,8 @@ export function envoyproxyLogsSpecProvider(context: TutorialContext): TutorialSc completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/envoyproxy_logs/screenshot.png', onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['elastic_stack', 'datastore'], }; } diff --git a/src/plugins/home/server/tutorials/envoyproxy_metrics/index.ts b/src/plugins/home/server/tutorials/envoyproxy_metrics/index.ts index e2f3b84739685..84ea8099e3d93 100644 --- a/src/plugins/home/server/tutorials/envoyproxy_metrics/index.ts +++ b/src/plugins/home/server/tutorials/envoyproxy_metrics/index.ts @@ -47,8 +47,8 @@ export function envoyproxyMetricsSpecProvider(context: TutorialContext): Tutoria }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['elastic_stack', 'datastore'], }; } diff --git a/src/plugins/home/server/tutorials/etcd_metrics/index.ts b/src/plugins/home/server/tutorials/etcd_metrics/index.ts index 9ed153c21c257..c4c68e80d40eb 100644 --- a/src/plugins/home/server/tutorials/etcd_metrics/index.ts +++ b/src/plugins/home/server/tutorials/etcd_metrics/index.ts @@ -54,8 +54,8 @@ export function etcdMetricsSpecProvider(context: TutorialContext): TutorialSchem }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['elastic_stack', 'datastore'], }; } diff --git a/src/plugins/home/server/tutorials/f5_logs/index.ts b/src/plugins/home/server/tutorials/f5_logs/index.ts index a407d1d3d5142..381fdd487eb24 100644 --- a/src/plugins/home/server/tutorials/f5_logs/index.ts +++ b/src/plugins/home/server/tutorials/f5_logs/index.ts @@ -55,8 +55,8 @@ export function f5LogsSpecProvider(context: TutorialContext): TutorialSchema { completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/f5_logs/screenshot.png', onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['network', 'security'], }; } diff --git a/src/plugins/home/server/tutorials/fortinet_logs/index.ts b/src/plugins/home/server/tutorials/fortinet_logs/index.ts index 2f6af3ba47280..6a73c5f8e3f66 100644 --- a/src/plugins/home/server/tutorials/fortinet_logs/index.ts +++ b/src/plugins/home/server/tutorials/fortinet_logs/index.ts @@ -54,8 +54,8 @@ export function fortinetLogsSpecProvider(context: TutorialContext): TutorialSche }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['security'], }; } diff --git a/src/plugins/home/server/tutorials/gcp_logs/index.ts b/src/plugins/home/server/tutorials/gcp_logs/index.ts index 23d8e3364eb69..d02c08cd2be9a 100644 --- a/src/plugins/home/server/tutorials/gcp_logs/index.ts +++ b/src/plugins/home/server/tutorials/gcp_logs/index.ts @@ -59,8 +59,8 @@ export function gcpLogsSpecProvider(context: TutorialContext): TutorialSchema { completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/gcp_logs/screenshot.png', onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['google_cloud', 'cloud', 'network', 'security'], }; } diff --git a/src/plugins/home/server/tutorials/gcp_metrics/index.ts b/src/plugins/home/server/tutorials/gcp_metrics/index.ts index 7f397c1e1be7b..ea5351d010a42 100644 --- a/src/plugins/home/server/tutorials/gcp_metrics/index.ts +++ b/src/plugins/home/server/tutorials/gcp_metrics/index.ts @@ -57,8 +57,8 @@ export function gcpMetricsSpecProvider(context: TutorialContext): TutorialSchema completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/gcp_metrics/screenshot.png', onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['google_cloud', 'cloud', 'network', 'security'], }; } diff --git a/src/plugins/home/server/tutorials/golang_metrics/index.ts b/src/plugins/home/server/tutorials/golang_metrics/index.ts index 50d09e42e8791..e179e69734ad5 100644 --- a/src/plugins/home/server/tutorials/golang_metrics/index.ts +++ b/src/plugins/home/server/tutorials/golang_metrics/index.ts @@ -57,8 +57,8 @@ export function golangMetricsSpecProvider(context: TutorialContext): TutorialSch }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['google_cloud', 'cloud', 'network', 'security'], }; } diff --git a/src/plugins/home/server/tutorials/gsuite_logs/index.ts b/src/plugins/home/server/tutorials/gsuite_logs/index.ts index 718558321cf78..ba193bdb08c08 100644 --- a/src/plugins/home/server/tutorials/gsuite_logs/index.ts +++ b/src/plugins/home/server/tutorials/gsuite_logs/index.ts @@ -54,8 +54,8 @@ export function gsuiteLogsSpecProvider(context: TutorialContext): TutorialSchema }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['security'], }; } diff --git a/src/plugins/home/server/tutorials/haproxy_logs/index.ts b/src/plugins/home/server/tutorials/haproxy_logs/index.ts index c3765317ecbe0..05fc23fa16bcd 100644 --- a/src/plugins/home/server/tutorials/haproxy_logs/index.ts +++ b/src/plugins/home/server/tutorials/haproxy_logs/index.ts @@ -57,8 +57,8 @@ export function haproxyLogsSpecProvider(context: TutorialContext): TutorialSchem completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/haproxy_logs/screenshot.png', onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['network', 'web'], }; } diff --git a/src/plugins/home/server/tutorials/haproxy_metrics/index.ts b/src/plugins/home/server/tutorials/haproxy_metrics/index.ts index 49f1d32dc4c82..fa7c451889ba3 100644 --- a/src/plugins/home/server/tutorials/haproxy_metrics/index.ts +++ b/src/plugins/home/server/tutorials/haproxy_metrics/index.ts @@ -54,8 +54,8 @@ export function haproxyMetricsSpecProvider(context: TutorialContext): TutorialSc }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['network', 'web'], }; } diff --git a/src/plugins/home/server/tutorials/ibmmq_logs/index.ts b/src/plugins/home/server/tutorials/ibmmq_logs/index.ts index 21b60a9ab5a5c..90b35d0e78842 100644 --- a/src/plugins/home/server/tutorials/ibmmq_logs/index.ts +++ b/src/plugins/home/server/tutorials/ibmmq_logs/index.ts @@ -56,8 +56,8 @@ export function ibmmqLogsSpecProvider(context: TutorialContext): TutorialSchema completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/ibmmq_logs/screenshot.png', onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['security'], }; } diff --git a/src/plugins/home/server/tutorials/ibmmq_metrics/index.ts b/src/plugins/home/server/tutorials/ibmmq_metrics/index.ts index 706003f0eab48..6329df6836b06 100644 --- a/src/plugins/home/server/tutorials/ibmmq_metrics/index.ts +++ b/src/plugins/home/server/tutorials/ibmmq_metrics/index.ts @@ -55,8 +55,8 @@ export function ibmmqMetricsSpecProvider(context: TutorialContext): TutorialSche completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/ibmmq_metrics/screenshot.png', onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['security'], }; } diff --git a/src/plugins/home/server/tutorials/icinga_logs/index.ts b/src/plugins/home/server/tutorials/icinga_logs/index.ts index dc730022262c2..c65e92d0fe856 100644 --- a/src/plugins/home/server/tutorials/icinga_logs/index.ts +++ b/src/plugins/home/server/tutorials/icinga_logs/index.ts @@ -57,8 +57,8 @@ export function icingaLogsSpecProvider(context: TutorialContext): TutorialSchema completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/icinga_logs/screenshot.png', onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['security'], }; } diff --git a/src/plugins/home/server/tutorials/iis_logs/index.ts b/src/plugins/home/server/tutorials/iis_logs/index.ts index 0dbc5bbdc75b8..423f2f917c84e 100644 --- a/src/plugins/home/server/tutorials/iis_logs/index.ts +++ b/src/plugins/home/server/tutorials/iis_logs/index.ts @@ -58,8 +58,8 @@ export function iisLogsSpecProvider(context: TutorialContext): TutorialSchema { completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/iis_logs/screenshot.png', onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['web'], }; } diff --git a/src/plugins/home/server/tutorials/iis_metrics/index.ts b/src/plugins/home/server/tutorials/iis_metrics/index.ts index d57e4688ba753..3c3159c2838d1 100644 --- a/src/plugins/home/server/tutorials/iis_metrics/index.ts +++ b/src/plugins/home/server/tutorials/iis_metrics/index.ts @@ -57,8 +57,8 @@ export function iisMetricsSpecProvider(context: TutorialContext): TutorialSchema completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/iis_metrics/screenshot.png', onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['web'], }; } diff --git a/src/plugins/home/server/tutorials/imperva_logs/index.ts b/src/plugins/home/server/tutorials/imperva_logs/index.ts index 1cbe707f813ee..35e0a668ec7f0 100644 --- a/src/plugins/home/server/tutorials/imperva_logs/index.ts +++ b/src/plugins/home/server/tutorials/imperva_logs/index.ts @@ -54,8 +54,8 @@ export function impervaLogsSpecProvider(context: TutorialContext): TutorialSchem }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['network', 'security'], }; } diff --git a/src/plugins/home/server/tutorials/infoblox_logs/index.ts b/src/plugins/home/server/tutorials/infoblox_logs/index.ts index 8dce2bf00b2e2..21d1fcf9a156c 100644 --- a/src/plugins/home/server/tutorials/infoblox_logs/index.ts +++ b/src/plugins/home/server/tutorials/infoblox_logs/index.ts @@ -54,8 +54,8 @@ export function infobloxLogsSpecProvider(context: TutorialContext): TutorialSche }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['network'], }; } diff --git a/src/plugins/home/server/tutorials/instructions/auditbeat_instructions.ts b/src/plugins/home/server/tutorials/instructions/auditbeat_instructions.ts index d0a0f97e26037..3968aff312380 100644 --- a/src/plugins/home/server/tutorials/instructions/auditbeat_instructions.ts +++ b/src/plugins/home/server/tutorials/instructions/auditbeat_instructions.ts @@ -13,271 +13,317 @@ import { getSpaceIdForBeatsTutorial } from './get_space_id_for_beats_tutorial'; import { Platform, TutorialContext } from '../../services/tutorials/lib/tutorials_registry_types'; import { cloudPasswordAndResetLink } from './cloud_instructions'; -export const createAuditbeatInstructions = (context?: TutorialContext) => ({ - INSTALL: { - OSX: { - title: i18n.translate('home.tutorials.common.auditbeatInstructions.install.osxTitle', { - defaultMessage: 'Download and install Auditbeat', - }), - textPre: i18n.translate('home.tutorials.common.auditbeatInstructions.install.osxTextPre', { - defaultMessage: 'First time using Auditbeat? See the [Quick Start]({linkUrl}).', - values: { - linkUrl: '{config.docs.beats.auditbeat}/auditbeat-installation-configuration.html', - }, - }), - commands: [ - 'curl -L -O https://artifacts.elastic.co/downloads/beats/auditbeat/auditbeat-{config.kibana.version}-darwin-x86_64.tar.gz', - 'tar xzvf auditbeat-{config.kibana.version}-darwin-x86_64.tar.gz', - 'cd auditbeat-{config.kibana.version}-darwin-x86_64/', - ], - }, - DEB: { - title: i18n.translate('home.tutorials.common.auditbeatInstructions.install.debTitle', { - defaultMessage: 'Download and install Auditbeat', - }), - textPre: i18n.translate('home.tutorials.common.auditbeatInstructions.install.debTextPre', { - defaultMessage: 'First time using Auditbeat? See the [Quick Start]({linkUrl}).', - values: { - linkUrl: '{config.docs.beats.auditbeat}/auditbeat-installation-configuration.html', - }, - }), - commands: [ - 'curl -L -O https://artifacts.elastic.co/downloads/beats/auditbeat/auditbeat-{config.kibana.version}-amd64.deb', - 'sudo dpkg -i auditbeat-{config.kibana.version}-amd64.deb', - ], - textPost: i18n.translate('home.tutorials.common.auditbeatInstructions.install.debTextPost', { - defaultMessage: 'Looking for the 32-bit packages? See the [Download page]({linkUrl}).', - values: { - linkUrl: 'https://www.elastic.co/downloads/beats/auditbeat', - }, - }), - }, - RPM: { - title: i18n.translate('home.tutorials.common.auditbeatInstructions.install.rpmTitle', { - defaultMessage: 'Download and install Auditbeat', - }), - textPre: i18n.translate('home.tutorials.common.auditbeatInstructions.install.rpmTextPre', { - defaultMessage: 'First time using Auditbeat? See the [Quick Start]({linkUrl}).', - values: { - linkUrl: '{config.docs.beats.auditbeat}/auditbeat-installation-configuration.html', - }, - }), - commands: [ - 'curl -L -O https://artifacts.elastic.co/downloads/beats/auditbeat/auditbeat-{config.kibana.version}-x86_64.rpm', - 'sudo rpm -vi auditbeat-{config.kibana.version}-x86_64.rpm', - ], - textPost: i18n.translate('home.tutorials.common.auditbeatInstructions.install.rpmTextPost', { - defaultMessage: 'Looking for the 32-bit packages? See the [Download page]({linkUrl}).', - values: { - linkUrl: 'https://www.elastic.co/downloads/beats/auditbeat', - }, - }), - }, - WINDOWS: { - title: i18n.translate('home.tutorials.common.auditbeatInstructions.install.windowsTitle', { - defaultMessage: 'Download and install Auditbeat', - }), - textPre: i18n.translate( - 'home.tutorials.common.auditbeatInstructions.install.windowsTextPre', - { - defaultMessage: - 'First time using Auditbeat? See the [Quick Start]({guideLinkUrl}).\n\ +export const createAuditbeatInstructions = (context: TutorialContext) => { + const SSL_DOC_URL = `https://www.elastic.co/guide/en/beats/auditbeat/${context.kibanaBranch}/configuration-ssl.html#ca-sha256`; + + return { + INSTALL: { + OSX: { + title: i18n.translate('home.tutorials.common.auditbeatInstructions.install.osxTitle', { + defaultMessage: 'Download and install Auditbeat', + }), + textPre: i18n.translate('home.tutorials.common.auditbeatInstructions.install.osxTextPre', { + defaultMessage: 'First time using Auditbeat? See the [Quick Start]({linkUrl}).', + values: { + linkUrl: '{config.docs.beats.auditbeat}/auditbeat-installation-configuration.html', + }, + }), + commands: [ + 'curl -L -O https://artifacts.elastic.co/downloads/beats/auditbeat/auditbeat-{config.kibana.version}-darwin-x86_64.tar.gz', + 'tar xzvf auditbeat-{config.kibana.version}-darwin-x86_64.tar.gz', + 'cd auditbeat-{config.kibana.version}-darwin-x86_64/', + ], + }, + DEB: { + title: i18n.translate('home.tutorials.common.auditbeatInstructions.install.debTitle', { + defaultMessage: 'Download and install Auditbeat', + }), + textPre: i18n.translate('home.tutorials.common.auditbeatInstructions.install.debTextPre', { + defaultMessage: 'First time using Auditbeat? See the [Quick Start]({linkUrl}).', + values: { + linkUrl: '{config.docs.beats.auditbeat}/auditbeat-installation-configuration.html', + }, + }), + commands: [ + 'curl -L -O https://artifacts.elastic.co/downloads/beats/auditbeat/auditbeat-{config.kibana.version}-amd64.deb', + 'sudo dpkg -i auditbeat-{config.kibana.version}-amd64.deb', + ], + textPost: i18n.translate( + 'home.tutorials.common.auditbeatInstructions.install.debTextPost', + { + defaultMessage: 'Looking for the 32-bit packages? See the [Download page]({linkUrl}).', + values: { + linkUrl: 'https://www.elastic.co/downloads/beats/auditbeat', + }, + } + ), + }, + RPM: { + title: i18n.translate('home.tutorials.common.auditbeatInstructions.install.rpmTitle', { + defaultMessage: 'Download and install Auditbeat', + }), + textPre: i18n.translate('home.tutorials.common.auditbeatInstructions.install.rpmTextPre', { + defaultMessage: 'First time using Auditbeat? See the [Quick Start]({linkUrl}).', + values: { + linkUrl: '{config.docs.beats.auditbeat}/auditbeat-installation-configuration.html', + }, + }), + commands: [ + 'curl -L -O https://artifacts.elastic.co/downloads/beats/auditbeat/auditbeat-{config.kibana.version}-x86_64.rpm', + 'sudo rpm -vi auditbeat-{config.kibana.version}-x86_64.rpm', + ], + textPost: i18n.translate( + 'home.tutorials.common.auditbeatInstructions.install.rpmTextPost', + { + defaultMessage: 'Looking for the 32-bit packages? See the [Download page]({linkUrl}).', + values: { + linkUrl: 'https://www.elastic.co/downloads/beats/auditbeat', + }, + } + ), + }, + WINDOWS: { + title: i18n.translate('home.tutorials.common.auditbeatInstructions.install.windowsTitle', { + defaultMessage: 'Download and install Auditbeat', + }), + textPre: i18n.translate( + 'home.tutorials.common.auditbeatInstructions.install.windowsTextPre', + { + defaultMessage: + 'First time using Auditbeat? See the [Quick Start]({guideLinkUrl}).\n\ 1. Download the Auditbeat Windows zip file from the [Download]({auditbeatLinkUrl}) page.\n\ 2. Extract the contents of the zip file into {folderPath}.\n\ 3. Rename the `{directoryName}` directory to `Auditbeat`.\n\ 4. Open a PowerShell prompt as an Administrator (right-click the PowerShell icon and select \ **Run As Administrator**). If you are running Windows XP, you might need to download and install PowerShell.\n\ 5. From the PowerShell prompt, run the following commands to install Auditbeat as a Windows service.', + values: { + folderPath: '`C:\\Program Files`', + guideLinkUrl: + '{config.docs.beats.auditbeat}/auditbeat-installation-configuration.html', + auditbeatLinkUrl: 'https://www.elastic.co/downloads/beats/auditbeat', + directoryName: 'auditbeat-{config.kibana.version}-windows', + }, + } + ), + commands: ['cd "C:\\Program Files\\Auditbeat"', '.\\install-service-auditbeat.ps1'], + textPost: i18n.translate( + 'home.tutorials.common.auditbeatInstructions.install.windowsTextPost', + { + defaultMessage: + 'Modify the settings under {propertyName} in the {auditbeatPath} file to point to your Elasticsearch installation.', + values: { + propertyName: '`output.elasticsearch`', + auditbeatPath: '`C:\\Program Files\\Auditbeat\\auditbeat.yml`', + }, + } + ), + }, + }, + START: { + OSX: { + title: i18n.translate('home.tutorials.common.auditbeatInstructions.start.osxTitle', { + defaultMessage: 'Start Auditbeat', + }), + textPre: i18n.translate('home.tutorials.common.auditbeatInstructions.start.osxTextPre', { + defaultMessage: + 'The `setup` command loads the Kibana dashboards. If the dashboards are already set up, omit this command.', + }), + commands: ['./auditbeat setup', './auditbeat -e'], + }, + DEB: { + title: i18n.translate('home.tutorials.common.auditbeatInstructions.start.debTitle', { + defaultMessage: 'Start Auditbeat', + }), + textPre: i18n.translate('home.tutorials.common.auditbeatInstructions.start.debTextPre', { + defaultMessage: + 'The `setup` command loads the Kibana dashboards. If the dashboards are already set up, omit this command.', + }), + commands: ['sudo auditbeat setup', 'sudo service auditbeat start'], + }, + RPM: { + title: i18n.translate('home.tutorials.common.auditbeatInstructions.start.rpmTitle', { + defaultMessage: 'Start Auditbeat', + }), + textPre: i18n.translate('home.tutorials.common.auditbeatInstructions.start.rpmTextPre', { + defaultMessage: + 'The `setup` command loads the Kibana dashboards. If the dashboards are already set up, omit this command.', + }), + commands: ['sudo auditbeat setup', 'sudo service auditbeat start'], + }, + WINDOWS: { + title: i18n.translate('home.tutorials.common.auditbeatInstructions.start.windowsTitle', { + defaultMessage: 'Start Auditbeat', + }), + textPre: i18n.translate( + 'home.tutorials.common.auditbeatInstructions.start.windowsTextPre', + { + defaultMessage: + 'The `setup` command loads the Kibana dashboards. If the dashboards are already set up, omit this command.', + } + ), + commands: ['.\\auditbeat.exe setup', 'Start-Service auditbeat'], + }, + }, + CONFIG: { + OSX: { + title: i18n.translate('home.tutorials.common.auditbeatInstructions.config.osxTitle', { + defaultMessage: 'Edit the configuration', + }), + textPre: i18n.translate('home.tutorials.common.auditbeatInstructions.config.osxTextPre', { + defaultMessage: 'Modify {path} to set the connection information:', values: { - folderPath: '`C:\\Program Files`', - guideLinkUrl: '{config.docs.beats.auditbeat}/auditbeat-installation-configuration.html', - auditbeatLinkUrl: 'https://www.elastic.co/downloads/beats/auditbeat', - directoryName: 'auditbeat-{config.kibana.version}-windows', + path: '`auditbeat.yml`', }, - } - ), - commands: ['cd "C:\\Program Files\\Auditbeat"', '.\\install-service-auditbeat.ps1'], - textPost: i18n.translate( - 'home.tutorials.common.auditbeatInstructions.install.windowsTextPost', - { - defaultMessage: - 'Modify the settings under {propertyName} in the {auditbeatPath} file to point to your Elasticsearch installation.', + }), + commands: [ + 'output.elasticsearch:', + ' hosts: [""]', + ' username: "elastic"', + ' password: ""', + " # If using Elasticsearch's default certificate", + ' ssl.ca_trusted_fingerprint: ""', + 'setup.kibana:', + ' host: ""', + getSpaceIdForBeatsTutorial(context), + ], + textPost: i18n.translate( + 'home.tutorials.common.auditbeatInstructions.config.osxTextPostMarkdown', + { + defaultMessage: + 'Where {passwordTemplate} is the password of the `elastic` user, {esUrlTemplate} is the URL of \ + Elasticsearch, and {kibanaUrlTemplate} is the URL of Kibana. To [configure SSL]({configureSslUrl}) with the \ + default certificate generated by Elasticsearch, add its fingerprint in {esCertFingerprintTemplate}.', + values: { + passwordTemplate: '``', + esUrlTemplate: '``', + kibanaUrlTemplate: '``', + configureSslUrl: SSL_DOC_URL, + esCertFingerprintTemplate: '``', + }, + } + ), + }, + DEB: { + title: i18n.translate('home.tutorials.common.auditbeatInstructions.config.debTitle', { + defaultMessage: 'Edit the configuration', + }), + textPre: i18n.translate('home.tutorials.common.auditbeatInstructions.config.debTextPre', { + defaultMessage: 'Modify {path} to set the connection information:', values: { - propertyName: '`output.elasticsearch`', - auditbeatPath: '`C:\\Program Files\\Auditbeat\\auditbeat.yml`', + path: '`/etc/auditbeat/auditbeat.yml`', }, - } - ), - }, - }, - START: { - OSX: { - title: i18n.translate('home.tutorials.common.auditbeatInstructions.start.osxTitle', { - defaultMessage: 'Start Auditbeat', - }), - textPre: i18n.translate('home.tutorials.common.auditbeatInstructions.start.osxTextPre', { - defaultMessage: - 'The `setup` command loads the Kibana dashboards. If the dashboards are already set up, omit this command.', - }), - commands: ['./auditbeat setup', './auditbeat -e'], - }, - DEB: { - title: i18n.translate('home.tutorials.common.auditbeatInstructions.start.debTitle', { - defaultMessage: 'Start Auditbeat', - }), - textPre: i18n.translate('home.tutorials.common.auditbeatInstructions.start.debTextPre', { - defaultMessage: - 'The `setup` command loads the Kibana dashboards. If the dashboards are already set up, omit this command.', - }), - commands: ['sudo auditbeat setup', 'sudo service auditbeat start'], - }, - RPM: { - title: i18n.translate('home.tutorials.common.auditbeatInstructions.start.rpmTitle', { - defaultMessage: 'Start Auditbeat', - }), - textPre: i18n.translate('home.tutorials.common.auditbeatInstructions.start.rpmTextPre', { - defaultMessage: - 'The `setup` command loads the Kibana dashboards. If the dashboards are already set up, omit this command.', - }), - commands: ['sudo auditbeat setup', 'sudo service auditbeat start'], - }, - WINDOWS: { - title: i18n.translate('home.tutorials.common.auditbeatInstructions.start.windowsTitle', { - defaultMessage: 'Start Auditbeat', - }), - textPre: i18n.translate('home.tutorials.common.auditbeatInstructions.start.windowsTextPre', { - defaultMessage: - 'The `setup` command loads the Kibana dashboards. If the dashboards are already set up, omit this command.', - }), - commands: ['.\\auditbeat.exe setup', 'Start-Service auditbeat'], - }, - }, - CONFIG: { - OSX: { - title: i18n.translate('home.tutorials.common.auditbeatInstructions.config.osxTitle', { - defaultMessage: 'Edit the configuration', - }), - textPre: i18n.translate('home.tutorials.common.auditbeatInstructions.config.osxTextPre', { - defaultMessage: 'Modify {path} to set the connection information:', - values: { - path: '`auditbeat.yml`', - }, - }), - commands: [ - 'output.elasticsearch:', - ' hosts: [""]', - ' username: "elastic"', - ' password: ""', - 'setup.kibana:', - ' host: ""', - getSpaceIdForBeatsTutorial(context), - ], - textPost: i18n.translate('home.tutorials.common.auditbeatInstructions.config.osxTextPost', { - defaultMessage: - 'Where {passwordTemplate} is the password of the `elastic` user, {esUrlTemplate} is the URL of Elasticsearch, \ -and {kibanaUrlTemplate} is the URL of Kibana.', - values: { - passwordTemplate: '``', - esUrlTemplate: '``', - kibanaUrlTemplate: '``', - }, - }), - }, - DEB: { - title: i18n.translate('home.tutorials.common.auditbeatInstructions.config.debTitle', { - defaultMessage: 'Edit the configuration', - }), - textPre: i18n.translate('home.tutorials.common.auditbeatInstructions.config.debTextPre', { - defaultMessage: 'Modify {path} to set the connection information:', - values: { - path: '`/etc/auditbeat/auditbeat.yml`', - }, - }), - commands: [ - 'output.elasticsearch:', - ' hosts: [""]', - ' username: "elastic"', - ' password: ""', - 'setup.kibana:', - ' host: ""', - getSpaceIdForBeatsTutorial(context), - ], - textPost: i18n.translate('home.tutorials.common.auditbeatInstructions.config.debTextPost', { - defaultMessage: - 'Where {passwordTemplate} is the password of the `elastic` user, {esUrlTemplate} is the URL of Elasticsearch, \ -and {kibanaUrlTemplate} is the URL of Kibana.', - values: { - passwordTemplate: '``', - esUrlTemplate: '``', - kibanaUrlTemplate: '``', - }, - }), - }, - RPM: { - title: i18n.translate('home.tutorials.common.auditbeatInstructions.config.rpmTitle', { - defaultMessage: 'Edit the configuration', - }), - textPre: i18n.translate('home.tutorials.common.auditbeatInstructions.config.rpmTextPre', { - defaultMessage: 'Modify {path} to set the connection information:', - values: { - path: '`/etc/auditbeat/auditbeat.yml`', - }, - }), - commands: [ - 'output.elasticsearch:', - ' hosts: [""]', - ' username: "elastic"', - ' password: ""', - 'setup.kibana:', - ' host: ""', - getSpaceIdForBeatsTutorial(context), - ], - textPost: i18n.translate('home.tutorials.common.auditbeatInstructions.config.rpmTextPost', { - defaultMessage: - 'Where {passwordTemplate} is the password of the `elastic` user, {esUrlTemplate} is the URL of Elasticsearch, \ -and {kibanaUrlTemplate} is the URL of Kibana.', - values: { - passwordTemplate: '``', - esUrlTemplate: '``', - kibanaUrlTemplate: '``', - }, - }), - }, - WINDOWS: { - title: i18n.translate('home.tutorials.common.auditbeatInstructions.config.windowsTitle', { - defaultMessage: 'Edit the configuration', - }), - textPre: i18n.translate('home.tutorials.common.auditbeatInstructions.config.windowsTextPre', { - defaultMessage: 'Modify {path} to set the connection information:', - values: { - path: '`C:\\Program Files\\Auditbeat\\auditbeat.yml`', - }, - }), - commands: [ - 'output.elasticsearch:', - ' hosts: [""]', - ' username: "elastic"', - ' password: ""', - 'setup.kibana:', - ' host: ""', - getSpaceIdForBeatsTutorial(context), - ], - textPost: i18n.translate( - 'home.tutorials.common.auditbeatInstructions.config.windowsTextPost', - { - defaultMessage: - 'Where {passwordTemplate} is the password of the `elastic` user, {esUrlTemplate} is the URL of Elasticsearch, \ -and {kibanaUrlTemplate} is the URL of Kibana.', + }), + commands: [ + 'output.elasticsearch:', + ' hosts: [""]', + ' username: "elastic"', + ' password: ""', + " # If using Elasticsearch's default certificate", + ' ssl.ca_trusted_fingerprint: ""', + 'setup.kibana:', + ' host: ""', + getSpaceIdForBeatsTutorial(context), + ], + textPost: i18n.translate( + 'home.tutorials.common.auditbeatInstructions.config.debTextPostMarkdown', + { + defaultMessage: + 'Where {passwordTemplate} is the password of the `elastic` user, {esUrlTemplate} is the URL of \ + Elasticsearch, and {kibanaUrlTemplate} is the URL of Kibana. To [configure SSL]({configureSslUrl}) with the \ + default certificate generated by Elasticsearch, add its fingerprint in {esCertFingerprintTemplate}.', + values: { + passwordTemplate: '``', + esUrlTemplate: '``', + kibanaUrlTemplate: '``', + configureSslUrl: SSL_DOC_URL, + esCertFingerprintTemplate: '``', + }, + } + ), + }, + RPM: { + title: i18n.translate('home.tutorials.common.auditbeatInstructions.config.rpmTitle', { + defaultMessage: 'Edit the configuration', + }), + textPre: i18n.translate('home.tutorials.common.auditbeatInstructions.config.rpmTextPre', { + defaultMessage: 'Modify {path} to set the connection information:', values: { - passwordTemplate: '``', - esUrlTemplate: '``', - kibanaUrlTemplate: '``', + path: '`/etc/auditbeat/auditbeat.yml`', }, - } - ), + }), + commands: [ + 'output.elasticsearch:', + ' hosts: [""]', + ' username: "elastic"', + ' password: ""', + " # If using Elasticsearch's default certificate", + ' ssl.ca_trusted_fingerprint: ""', + 'setup.kibana:', + ' host: ""', + getSpaceIdForBeatsTutorial(context), + ], + textPost: i18n.translate( + 'home.tutorials.common.auditbeatInstructions.config.rpmTextPostMarkdown', + { + defaultMessage: + 'Where {passwordTemplate} is the password of the `elastic` user, {esUrlTemplate} is the URL of \ + Elasticsearch, and {kibanaUrlTemplate} is the URL of Kibana. To [configure SSL]({configureSslUrl}) with the \ + default certificate generated by Elasticsearch, add its fingerprint in {esCertFingerprintTemplate}.', + values: { + passwordTemplate: '``', + esUrlTemplate: '``', + kibanaUrlTemplate: '``', + configureSslUrl: SSL_DOC_URL, + esCertFingerprintTemplate: '``', + }, + } + ), + }, + WINDOWS: { + title: i18n.translate('home.tutorials.common.auditbeatInstructions.config.windowsTitle', { + defaultMessage: 'Edit the configuration', + }), + textPre: i18n.translate( + 'home.tutorials.common.auditbeatInstructions.config.windowsTextPre', + { + defaultMessage: 'Modify {path} to set the connection information:', + values: { + path: '`C:\\Program Files\\Auditbeat\\auditbeat.yml`', + }, + } + ), + commands: [ + 'output.elasticsearch:', + ' hosts: [""]', + ' username: "elastic"', + ' password: ""', + " # If using Elasticsearch's default certificate", + ' ssl.ca_trusted_fingerprint: ""', + 'setup.kibana:', + ' host: ""', + getSpaceIdForBeatsTutorial(context), + ], + textPost: i18n.translate( + 'home.tutorials.common.auditbeatInstructions.config.windowsTextPostMarkdown', + { + defaultMessage: + 'Where {passwordTemplate} is the password of the `elastic` user, {esUrlTemplate} is the URL of \ + Elasticsearch, and {kibanaUrlTemplate} is the URL of Kibana. To [configure SSL]({configureSslUrl}) with the \ + default certificate generated by Elasticsearch, add its fingerprint in {esCertFingerprintTemplate}.', + values: { + passwordTemplate: '``', + esUrlTemplate: '``', + kibanaUrlTemplate: '``', + configureSslUrl: SSL_DOC_URL, + esCertFingerprintTemplate: '``', + }, + } + ), + }, }, - }, -}); + }; +}; export const createAuditbeatCloudInstructions = () => ({ CONFIG: { @@ -383,7 +429,7 @@ export function auditbeatStatusCheck() { }; } -export function onPremInstructions(platforms: readonly Platform[], context?: TutorialContext) { +export function onPremInstructions(platforms: readonly Platform[], context: TutorialContext) { const AUDITBEAT_INSTRUCTIONS = createAuditbeatInstructions(context); const variants = []; @@ -414,8 +460,8 @@ export function onPremInstructions(platforms: readonly Platform[], context?: Tut }; } -export function onPremCloudInstructions(platforms: readonly Platform[]) { - const AUDITBEAT_INSTRUCTIONS = createAuditbeatInstructions(); +export function onPremCloudInstructions(platforms: readonly Platform[], context: TutorialContext) { + const AUDITBEAT_INSTRUCTIONS = createAuditbeatInstructions(context); const TRYCLOUD_OPTION1 = createTrycloudOption1(); const TRYCLOUD_OPTION2 = createTrycloudOption2(); @@ -450,8 +496,8 @@ export function onPremCloudInstructions(platforms: readonly Platform[]) { }; } -export function cloudInstructions(platforms: readonly Platform[]) { - const AUDITBEAT_INSTRUCTIONS = createAuditbeatInstructions(); +export function cloudInstructions(platforms: readonly Platform[], context: TutorialContext) { + const AUDITBEAT_INSTRUCTIONS = createAuditbeatInstructions(context); const AUDITBEAT_CLOUD_INSTRUCTIONS = createAuditbeatCloudInstructions(); const variants = []; diff --git a/src/plugins/home/server/tutorials/instructions/cloud_instructions.ts b/src/plugins/home/server/tutorials/instructions/cloud_instructions.ts index ed9e588a999b4..6d547b2a1d40d 100644 --- a/src/plugins/home/server/tutorials/instructions/cloud_instructions.ts +++ b/src/plugins/home/server/tutorials/instructions/cloud_instructions.ts @@ -7,13 +7,14 @@ */ import { i18n } from '@kbn/i18n'; + export const cloudPasswordAndResetLink = i18n.translate( 'home.tutorials.common.cloudInstructions.passwordAndResetLink', { defaultMessage: 'Where {passwordTemplate} is the password of the `elastic` user.' + `\\{#config.cloud.profileUrl\\} - Forgot the password? [Reset in Elastic Cloud](\\{config.cloud.baseUrl\\}\\{config.cloud.profileUrl\\}). + Forgot the password? [Reset in Elastic Cloud](\\{config.cloud.baseUrl\\}\\{config.cloud.deploymentUrl\\}/security). \\{/config.cloud.profileUrl\\}`, values: { passwordTemplate: '``' }, } diff --git a/src/plugins/home/server/tutorials/instructions/filebeat_instructions.ts b/src/plugins/home/server/tutorials/instructions/filebeat_instructions.ts index c6aa44932ee45..89445510f2b3d 100644 --- a/src/plugins/home/server/tutorials/instructions/filebeat_instructions.ts +++ b/src/plugins/home/server/tutorials/instructions/filebeat_instructions.ts @@ -13,268 +13,307 @@ import { getSpaceIdForBeatsTutorial } from './get_space_id_for_beats_tutorial'; import { Platform, TutorialContext } from '../../services/tutorials/lib/tutorials_registry_types'; import { cloudPasswordAndResetLink } from './cloud_instructions'; -export const createFilebeatInstructions = (context?: TutorialContext) => ({ - INSTALL: { - OSX: { - title: i18n.translate('home.tutorials.common.filebeatInstructions.install.osxTitle', { - defaultMessage: 'Download and install Filebeat', - }), - textPre: i18n.translate('home.tutorials.common.filebeatInstructions.install.osxTextPre', { - defaultMessage: 'First time using Filebeat? See the [Quick Start]({linkUrl}).', - values: { - linkUrl: '{config.docs.beats.filebeat}/filebeat-installation-configuration.html', - }, - }), - commands: [ - 'curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-{config.kibana.version}-darwin-x86_64.tar.gz', - 'tar xzvf filebeat-{config.kibana.version}-darwin-x86_64.tar.gz', - 'cd filebeat-{config.kibana.version}-darwin-x86_64/', - ], - }, - DEB: { - title: i18n.translate('home.tutorials.common.filebeatInstructions.install.debTitle', { - defaultMessage: 'Download and install Filebeat', - }), - textPre: i18n.translate('home.tutorials.common.filebeatInstructions.install.debTextPre', { - defaultMessage: 'First time using Filebeat? See the [Quick Start]({linkUrl}).', - values: { - linkUrl: '{config.docs.beats.filebeat}/filebeat-installation-configuration.html', - }, - }), - commands: [ - 'curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-{config.kibana.version}-amd64.deb', - 'sudo dpkg -i filebeat-{config.kibana.version}-amd64.deb', - ], - textPost: i18n.translate('home.tutorials.common.filebeatInstructions.install.debTextPost', { - defaultMessage: 'Looking for the 32-bit packages? See the [Download page]({linkUrl}).', - values: { - linkUrl: 'https://www.elastic.co/downloads/beats/filebeat', - }, - }), - }, - RPM: { - title: i18n.translate('home.tutorials.common.filebeatInstructions.install.rpmTitle', { - defaultMessage: 'Download and install Filebeat', - }), - textPre: i18n.translate('home.tutorials.common.filebeatInstructions.install.rpmTextPre', { - defaultMessage: 'First time using Filebeat? See the [Quick Start]({linkUrl}).', - values: { - linkUrl: '{config.docs.beats.filebeat}/filebeat-installation-configuration.html', - }, - }), - commands: [ - 'curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-{config.kibana.version}-x86_64.rpm', - 'sudo rpm -vi filebeat-{config.kibana.version}-x86_64.rpm', - ], - textPost: i18n.translate('home.tutorials.common.filebeatInstructions.install.rpmTextPost', { - defaultMessage: 'Looking for the 32-bit packages? See the [Download page]({linkUrl}).', - values: { - linkUrl: 'https://www.elastic.co/downloads/beats/filebeat', - }, - }), - }, - WINDOWS: { - title: i18n.translate('home.tutorials.common.filebeatInstructions.install.windowsTitle', { - defaultMessage: 'Download and install Filebeat', - }), - textPre: i18n.translate('home.tutorials.common.filebeatInstructions.install.windowsTextPre', { - defaultMessage: - 'First time using Filebeat? See the [Quick Start]({guideLinkUrl}).\n\ +export const createFilebeatInstructions = (context: TutorialContext) => { + const SSL_DOC_URL = `https://www.elastic.co/guide/en/beats/filebeat/${context.kibanaBranch}/configuration-ssl.html#ca-sha256`; + + return { + INSTALL: { + OSX: { + title: i18n.translate('home.tutorials.common.filebeatInstructions.install.osxTitle', { + defaultMessage: 'Download and install Filebeat', + }), + textPre: i18n.translate('home.tutorials.common.filebeatInstructions.install.osxTextPre', { + defaultMessage: 'First time using Filebeat? See the [Quick Start]({linkUrl}).', + values: { + linkUrl: '{config.docs.beats.filebeat}/filebeat-installation-configuration.html', + }, + }), + commands: [ + 'curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-{config.kibana.version}-darwin-x86_64.tar.gz', + 'tar xzvf filebeat-{config.kibana.version}-darwin-x86_64.tar.gz', + 'cd filebeat-{config.kibana.version}-darwin-x86_64/', + ], + }, + DEB: { + title: i18n.translate('home.tutorials.common.filebeatInstructions.install.debTitle', { + defaultMessage: 'Download and install Filebeat', + }), + textPre: i18n.translate('home.tutorials.common.filebeatInstructions.install.debTextPre', { + defaultMessage: 'First time using Filebeat? See the [Quick Start]({linkUrl}).', + values: { + linkUrl: '{config.docs.beats.filebeat}/filebeat-installation-configuration.html', + }, + }), + commands: [ + 'curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-{config.kibana.version}-amd64.deb', + 'sudo dpkg -i filebeat-{config.kibana.version}-amd64.deb', + ], + textPost: i18n.translate('home.tutorials.common.filebeatInstructions.install.debTextPost', { + defaultMessage: 'Looking for the 32-bit packages? See the [Download page]({linkUrl}).', + values: { + linkUrl: 'https://www.elastic.co/downloads/beats/filebeat', + }, + }), + }, + RPM: { + title: i18n.translate('home.tutorials.common.filebeatInstructions.install.rpmTitle', { + defaultMessage: 'Download and install Filebeat', + }), + textPre: i18n.translate('home.tutorials.common.filebeatInstructions.install.rpmTextPre', { + defaultMessage: 'First time using Filebeat? See the [Quick Start]({linkUrl}).', + values: { + linkUrl: '{config.docs.beats.filebeat}/filebeat-installation-configuration.html', + }, + }), + commands: [ + 'curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-{config.kibana.version}-x86_64.rpm', + 'sudo rpm -vi filebeat-{config.kibana.version}-x86_64.rpm', + ], + textPost: i18n.translate('home.tutorials.common.filebeatInstructions.install.rpmTextPost', { + defaultMessage: 'Looking for the 32-bit packages? See the [Download page]({linkUrl}).', + values: { + linkUrl: 'https://www.elastic.co/downloads/beats/filebeat', + }, + }), + }, + WINDOWS: { + title: i18n.translate('home.tutorials.common.filebeatInstructions.install.windowsTitle', { + defaultMessage: 'Download and install Filebeat', + }), + textPre: i18n.translate( + 'home.tutorials.common.filebeatInstructions.install.windowsTextPre', + { + defaultMessage: + 'First time using Filebeat? See the [Quick Start]({guideLinkUrl}).\n\ 1. Download the Filebeat Windows zip file from the [Download]({filebeatLinkUrl}) page.\n\ 2. Extract the contents of the zip file into {folderPath}.\n\ 3. Rename the `{directoryName}` directory to `Filebeat`.\n\ 4. Open a PowerShell prompt as an Administrator (right-click the PowerShell icon and select \ **Run As Administrator**). If you are running Windows XP, you might need to download and install PowerShell.\n\ 5. From the PowerShell prompt, run the following commands to install Filebeat as a Windows service.', - values: { - folderPath: '`C:\\Program Files`', - guideLinkUrl: '{config.docs.beats.filebeat}/filebeat-installation-configuration.html', - filebeatLinkUrl: 'https://www.elastic.co/downloads/beats/filebeat', - directoryName: 'filebeat-{config.kibana.version}-windows', - }, - }), - commands: ['cd "C:\\Program Files\\Filebeat"', '.\\install-service-filebeat.ps1'], - textPost: i18n.translate( - 'home.tutorials.common.filebeatInstructions.install.windowsTextPost', - { + values: { + folderPath: '`C:\\Program Files`', + guideLinkUrl: '{config.docs.beats.filebeat}/filebeat-installation-configuration.html', + filebeatLinkUrl: 'https://www.elastic.co/downloads/beats/filebeat', + directoryName: 'filebeat-{config.kibana.version}-windows', + }, + } + ), + commands: ['cd "C:\\Program Files\\Filebeat"', '.\\install-service-filebeat.ps1'], + textPost: i18n.translate( + 'home.tutorials.common.filebeatInstructions.install.windowsTextPost', + { + defaultMessage: + 'Modify the settings under {propertyName} in the {filebeatPath} file to point to your Elasticsearch installation.', + values: { + propertyName: '`output.elasticsearch`', + filebeatPath: '`C:\\Program Files\\Filebeat\\filebeat.yml`', + }, + } + ), + }, + }, + START: { + OSX: { + title: i18n.translate('home.tutorials.common.filebeatInstructions.start.osxTitle', { + defaultMessage: 'Start Filebeat', + }), + textPre: i18n.translate('home.tutorials.common.filebeatInstructions.start.osxTextPre', { + defaultMessage: + 'The `setup` command loads the Kibana dashboards. If the dashboards are already set up, omit this command.', + }), + commands: ['./filebeat setup', './filebeat -e'], + }, + DEB: { + title: i18n.translate('home.tutorials.common.filebeatInstructions.start.debTitle', { + defaultMessage: 'Start Filebeat', + }), + textPre: i18n.translate('home.tutorials.common.filebeatInstructions.start.debTextPre', { + defaultMessage: + 'The `setup` command loads the Kibana dashboards. If the dashboards are already set up, omit this command.', + }), + commands: ['sudo filebeat setup', 'sudo service filebeat start'], + }, + RPM: { + title: i18n.translate('home.tutorials.common.filebeatInstructions.start.rpmTitle', { + defaultMessage: 'Start Filebeat', + }), + textPre: i18n.translate('home.tutorials.common.filebeatInstructions.start.rpmTextPre', { defaultMessage: - 'Modify the settings under {propertyName} in the {filebeatPath} file to point to your Elasticsearch installation.', + 'The `setup` command loads the Kibana dashboards. If the dashboards are already set up, omit this command.', + }), + commands: ['sudo filebeat setup', 'sudo service filebeat start'], + }, + WINDOWS: { + title: i18n.translate('home.tutorials.common.filebeatInstructions.start.windowsTitle', { + defaultMessage: 'Start Filebeat', + }), + textPre: i18n.translate('home.tutorials.common.filebeatInstructions.start.windowsTextPre', { + defaultMessage: + 'The `setup` command loads the Kibana dashboards. If the dashboards are already set up, omit this command.', + }), + commands: ['.\\filebeat.exe setup', 'Start-Service filebeat'], + }, + }, + CONFIG: { + OSX: { + title: i18n.translate('home.tutorials.common.filebeatInstructions.config.osxTitle', { + defaultMessage: 'Edit the configuration', + }), + textPre: i18n.translate('home.tutorials.common.filebeatInstructions.config.osxTextPre', { + defaultMessage: 'Modify {path} to set the connection information:', values: { - propertyName: '`output.elasticsearch`', - filebeatPath: '`C:\\Program Files\\Filebeat\\filebeat.yml`', + path: '`filebeat.yml`', }, - } - ), - }, - }, - START: { - OSX: { - title: i18n.translate('home.tutorials.common.filebeatInstructions.start.osxTitle', { - defaultMessage: 'Start Filebeat', - }), - textPre: i18n.translate('home.tutorials.common.filebeatInstructions.start.osxTextPre', { - defaultMessage: - 'The `setup` command loads the Kibana dashboards. If the dashboards are already set up, omit this command.', - }), - commands: ['./filebeat setup', './filebeat -e'], - }, - DEB: { - title: i18n.translate('home.tutorials.common.filebeatInstructions.start.debTitle', { - defaultMessage: 'Start Filebeat', - }), - textPre: i18n.translate('home.tutorials.common.filebeatInstructions.start.debTextPre', { - defaultMessage: - 'The `setup` command loads the Kibana dashboards. If the dashboards are already set up, omit this command.', - }), - commands: ['sudo filebeat setup', 'sudo service filebeat start'], - }, - RPM: { - title: i18n.translate('home.tutorials.common.filebeatInstructions.start.rpmTitle', { - defaultMessage: 'Start Filebeat', - }), - textPre: i18n.translate('home.tutorials.common.filebeatInstructions.start.rpmTextPre', { - defaultMessage: - 'The `setup` command loads the Kibana dashboards. If the dashboards are already set up, omit this command.', - }), - commands: ['sudo filebeat setup', 'sudo service filebeat start'], - }, - WINDOWS: { - title: i18n.translate('home.tutorials.common.filebeatInstructions.start.windowsTitle', { - defaultMessage: 'Start Filebeat', - }), - textPre: i18n.translate('home.tutorials.common.filebeatInstructions.start.windowsTextPre', { - defaultMessage: - 'The `setup` command loads the Kibana dashboards. If the dashboards are already set up, omit this command.', - }), - commands: ['.\\filebeat.exe setup', 'Start-Service filebeat'], - }, - }, - CONFIG: { - OSX: { - title: i18n.translate('home.tutorials.common.filebeatInstructions.config.osxTitle', { - defaultMessage: 'Edit the configuration', - }), - textPre: i18n.translate('home.tutorials.common.filebeatInstructions.config.osxTextPre', { - defaultMessage: 'Modify {path} to set the connection information:', - values: { - path: '`filebeat.yml`', - }, - }), - commands: [ - 'output.elasticsearch:', - ' hosts: [""]', - ' username: "elastic"', - ' password: ""', - 'setup.kibana:', - ' host: ""', - getSpaceIdForBeatsTutorial(context), - ], - textPost: i18n.translate('home.tutorials.common.filebeatInstructions.config.osxTextPost', { - defaultMessage: - 'Where {passwordTemplate} is the password of the `elastic` user, {esUrlTemplate} is the URL of Elasticsearch, \ -and {kibanaUrlTemplate} is the URL of Kibana.', - values: { - passwordTemplate: '``', - esUrlTemplate: '``', - kibanaUrlTemplate: '``', - }, - }), - }, - DEB: { - title: i18n.translate('home.tutorials.common.filebeatInstructions.config.debTitle', { - defaultMessage: 'Edit the configuration', - }), - textPre: i18n.translate('home.tutorials.common.filebeatInstructions.config.debTextPre', { - defaultMessage: 'Modify {path} to set the connection information:', - values: { - path: '`/etc/filebeat/filebeat.yml`', - }, - }), - commands: [ - 'output.elasticsearch:', - ' hosts: [""]', - ' username: "elastic"', - ' password: ""', - 'setup.kibana:', - ' host: ""', - getSpaceIdForBeatsTutorial(context), - ], - textPost: i18n.translate('home.tutorials.common.filebeatInstructions.config.debTextPost', { - defaultMessage: - 'Where {passwordTemplate} is the password of the `elastic` user, {esUrlTemplate} is the URL of Elasticsearch, \ -and {kibanaUrlTemplate} is the URL of Kibana.', - values: { - passwordTemplate: '``', - esUrlTemplate: '``', - kibanaUrlTemplate: '``', - }, - }), - }, - RPM: { - title: i18n.translate('home.tutorials.common.filebeatInstructions.config.rpmTitle', { - defaultMessage: 'Edit the configuration', - }), - textPre: i18n.translate('home.tutorials.common.filebeatInstructions.config.rpmTextPre', { - defaultMessage: 'Modify {path} to set the connection information:', - values: { - path: '`/etc/filebeat/filebeat.yml`', - }, - }), - commands: [ - 'output.elasticsearch:', - ' hosts: [""]', - ' username: "elastic"', - ' password: ""', - 'setup.kibana:', - ' host: ""', - getSpaceIdForBeatsTutorial(context), - ], - textPost: i18n.translate('home.tutorials.common.filebeatInstructions.config.rpmTextPost', { - defaultMessage: - 'Where {passwordTemplate} is the password of the `elastic` user, {esUrlTemplate} is the URL of Elasticsearch, \ -and {kibanaUrlTemplate} is the URL of Kibana.', - values: { - passwordTemplate: '``', - esUrlTemplate: '``', - kibanaUrlTemplate: '``', - }, - }), - }, - WINDOWS: { - title: i18n.translate('home.tutorials.common.filebeatInstructions.config.windowsTitle', { - defaultMessage: 'Edit the configuration', - }), - textPre: i18n.translate('home.tutorials.common.filebeatInstructions.config.windowsTextPre', { - defaultMessage: 'Modify {path} to set the connection information:', - values: { - path: '`C:\\Program Files\\Filebeat\\filebeat.yml`', - }, - }), - commands: [ - 'output.elasticsearch:', - ' hosts: [""]', - ' username: "elastic"', - ' password: ""', - 'setup.kibana:', - ' host: ""', - getSpaceIdForBeatsTutorial(context), - ], - textPost: i18n.translate( - 'home.tutorials.common.filebeatInstructions.config.windowsTextPost', - { - defaultMessage: - 'Where {passwordTemplate} is the password of the `elastic` user, {esUrlTemplate} is the URL of Elasticsearch, \ -and {kibanaUrlTemplate} is the URL of Kibana.', + }), + commands: [ + 'output.elasticsearch:', + ' hosts: [""]', + ' username: "elastic"', + ' password: ""', + " # If using Elasticsearch's default certificate", + ' ssl.ca_trusted_fingerprint: ""', + 'setup.kibana:', + ' host: ""', + getSpaceIdForBeatsTutorial(context), + ], + textPost: i18n.translate( + 'home.tutorials.common.filebeatInstructions.config.osxTextPostMarkdown', + { + defaultMessage: + 'Where {passwordTemplate} is the password of the `elastic` user, {esUrlTemplate} is the URL of \ + Elasticsearch, and {kibanaUrlTemplate} is the URL of Kibana. To [configure SSL]({configureSslUrl}) with the \ + default certificate generated by Elasticsearch, add its fingerprint in {esCertFingerprintTemplate}.', + values: { + passwordTemplate: '``', + esUrlTemplate: '``', + kibanaUrlTemplate: '``', + configureSslUrl: SSL_DOC_URL, + esCertFingerprintTemplate: '``', + }, + } + ), + }, + DEB: { + title: i18n.translate('home.tutorials.common.filebeatInstructions.config.debTitle', { + defaultMessage: 'Edit the configuration', + }), + textPre: i18n.translate('home.tutorials.common.filebeatInstructions.config.debTextPre', { + defaultMessage: 'Modify {path} to set the connection information:', values: { - passwordTemplate: '``', - esUrlTemplate: '``', - kibanaUrlTemplate: '``', + path: '`/etc/filebeat/filebeat.yml`', }, - } - ), + }), + commands: [ + 'output.elasticsearch:', + ' hosts: [""]', + ' username: "elastic"', + ' password: ""', + " # If using Elasticsearch's default certificate", + ' ssl.ca_trusted_fingerprint: ""', + 'setup.kibana:', + ' host: ""', + getSpaceIdForBeatsTutorial(context), + ], + textPost: i18n.translate( + 'home.tutorials.common.filebeatInstructions.config.debTextPostMarkdown', + { + defaultMessage: + 'Where {passwordTemplate} is the password of the `elastic` user, {esUrlTemplate} is the URL of \ + Elasticsearch, and {kibanaUrlTemplate} is the URL of Kibana. To [configure SSL]({configureSslUrl}) with the \ + default certificate generated by Elasticsearch, add its fingerprint in {esCertFingerprintTemplate}.', + values: { + passwordTemplate: '``', + esUrlTemplate: '``', + kibanaUrlTemplate: '``', + configureSslUrl: SSL_DOC_URL, + esCertFingerprintTemplate: '``', + }, + } + ), + }, + RPM: { + title: i18n.translate('home.tutorials.common.filebeatInstructions.config.rpmTitle', { + defaultMessage: 'Edit the configuration', + }), + textPre: i18n.translate('home.tutorials.common.filebeatInstructions.config.rpmTextPre', { + defaultMessage: 'Modify {path} to set the connection information:', + values: { + path: '`/etc/filebeat/filebeat.yml`', + }, + }), + commands: [ + 'output.elasticsearch:', + ' hosts: [""]', + ' username: "elastic"', + ' password: ""', + " # If using Elasticsearch's default certificate", + ' ssl.ca_trusted_fingerprint: ""', + 'setup.kibana:', + ' host: ""', + getSpaceIdForBeatsTutorial(context), + ], + textPost: i18n.translate( + 'home.tutorials.common.filebeatInstructions.config.rpmTextPostMarkdown', + { + defaultMessage: + 'Where {passwordTemplate} is the password of the `elastic` user, {esUrlTemplate} is the URL of \ + Elasticsearch, and {kibanaUrlTemplate} is the URL of Kibana. To [configure SSL]({configureSslUrl}) with the \ + default certificate generated by Elasticsearch, add its fingerprint in {esCertFingerprintTemplate}.', + values: { + passwordTemplate: '``', + esUrlTemplate: '``', + kibanaUrlTemplate: '``', + configureSslUrl: SSL_DOC_URL, + esCertFingerprintTemplate: '``', + }, + } + ), + }, + WINDOWS: { + title: i18n.translate('home.tutorials.common.filebeatInstructions.config.windowsTitle', { + defaultMessage: 'Edit the configuration', + }), + textPre: i18n.translate( + 'home.tutorials.common.filebeatInstructions.config.windowsTextPre', + { + defaultMessage: 'Modify {path} to set the connection information:', + values: { + path: '`C:\\Program Files\\Filebeat\\filebeat.yml`', + }, + } + ), + commands: [ + 'output.elasticsearch:', + ' hosts: [""]', + ' username: "elastic"', + ' password: ""', + " # If using Elasticsearch's default certificate", + ' ssl.ca_trusted_fingerprint: ""', + 'setup.kibana:', + ' host: ""', + getSpaceIdForBeatsTutorial(context), + ], + textPost: i18n.translate( + 'home.tutorials.common.filebeatInstructions.config.windowsTextPostMarkdown', + { + defaultMessage: + 'Where {passwordTemplate} is the password of the `elastic` user, {esUrlTemplate} is the URL of \ + Elasticsearch, and {kibanaUrlTemplate} is the URL of Kibana. To [configure SSL]({configureSslUrl}) with the \ + default certificate generated by Elasticsearch, add its fingerprint in {esCertFingerprintTemplate}.', + values: { + passwordTemplate: '``', + esUrlTemplate: '``', + kibanaUrlTemplate: '``', + configureSslUrl: SSL_DOC_URL, + esCertFingerprintTemplate: '``', + }, + } + ), + }, }, - }, -}); + }; +}; export const createFilebeatCloudInstructions = () => ({ CONFIG: { @@ -430,7 +469,7 @@ export function filebeatStatusCheck(moduleName: string) { export function onPremInstructions( moduleName: string, platforms: readonly Platform[] = [], - context?: TutorialContext + context: TutorialContext ) { const FILEBEAT_INSTRUCTIONS = createFilebeatInstructions(context); @@ -463,8 +502,12 @@ export function onPremInstructions( }; } -export function onPremCloudInstructions(moduleName: string, platforms: readonly Platform[] = []) { - const FILEBEAT_INSTRUCTIONS = createFilebeatInstructions(); +export function onPremCloudInstructions( + moduleName: string, + platforms: readonly Platform[] = [], + context: TutorialContext +) { + const FILEBEAT_INSTRUCTIONS = createFilebeatInstructions(context); const TRYCLOUD_OPTION1 = createTrycloudOption1(); const TRYCLOUD_OPTION2 = createTrycloudOption2(); @@ -500,8 +543,12 @@ export function onPremCloudInstructions(moduleName: string, platforms: readonly }; } -export function cloudInstructions(moduleName: string, platforms: readonly Platform[] = []) { - const FILEBEAT_INSTRUCTIONS = createFilebeatInstructions(); +export function cloudInstructions( + moduleName: string, + platforms: readonly Platform[] = [], + context: TutorialContext +) { + const FILEBEAT_INSTRUCTIONS = createFilebeatInstructions(context); const FILEBEAT_CLOUD_INSTRUCTIONS = createFilebeatCloudInstructions(); const variants = []; diff --git a/src/plugins/home/server/tutorials/instructions/functionbeat_instructions.ts b/src/plugins/home/server/tutorials/instructions/functionbeat_instructions.ts index 24a6fe3719f8f..60d6fa5cb813b 100644 --- a/src/plugins/home/server/tutorials/instructions/functionbeat_instructions.ts +++ b/src/plugins/home/server/tutorials/instructions/functionbeat_instructions.ts @@ -13,171 +13,203 @@ import { getSpaceIdForBeatsTutorial } from './get_space_id_for_beats_tutorial'; import { Platform, TutorialContext } from '../../services/tutorials/lib/tutorials_registry_types'; import { cloudPasswordAndResetLink } from './cloud_instructions'; -export const createFunctionbeatInstructions = (context?: TutorialContext) => ({ - INSTALL: { - OSX: { - title: i18n.translate('home.tutorials.common.functionbeatInstructions.install.osxTitle', { - defaultMessage: 'Download and install Functionbeat', - }), - textPre: i18n.translate('home.tutorials.common.functionbeatInstructions.install.osxTextPre', { - defaultMessage: 'First time using Functionbeat? See the [Quick Start]({link}).', - values: { - link: '{config.docs.beats.functionbeat}/functionbeat-installation-configuration.html', - }, - }), - commands: [ - 'curl -L -O https://artifacts.elastic.co/downloads/beats/functionbeat/functionbeat-{config.kibana.version}-darwin-x86_64.tar.gz', - 'tar xzvf functionbeat-{config.kibana.version}-darwin-x86_64.tar.gz', - 'cd functionbeat-{config.kibana.version}-darwin-x86_64/', - ], - }, - LINUX: { - title: i18n.translate('home.tutorials.common.functionbeatInstructions.install.linuxTitle', { - defaultMessage: 'Download and install Functionbeat', - }), - textPre: i18n.translate( - 'home.tutorials.common.functionbeatInstructions.install.linuxTextPre', - { - defaultMessage: 'First time using Functionbeat? See the [Quick Start]({link}).', - values: { - link: '{config.docs.beats.functionbeat}/functionbeat-installation-configuration.html', - }, - } - ), - commands: [ - 'curl -L -O https://artifacts.elastic.co/downloads/beats/functionbeat/functionbeat-{config.kibana.version}-linux-x86_64.tar.gz', - 'tar xzvf functionbeat-{config.kibana.version}-linux-x86_64.tar.gz', - 'cd functionbeat-{config.kibana.version}-linux-x86_64/', - ], - }, - WINDOWS: { - title: i18n.translate('home.tutorials.common.functionbeatInstructions.install.windowsTitle', { - defaultMessage: 'Download and install Functionbeat', - }), - textPre: i18n.translate( - 'home.tutorials.common.functionbeatInstructions.install.windowsTextPre', - { - defaultMessage: - 'First time using Functionbeat? See the [Quick Start]({functionbeatLink}).\n\ +export const createFunctionbeatInstructions = (context: TutorialContext) => { + const SSL_DOC_URL = `https://www.elastic.co/guide/en/beats/functionbeat/${context.kibanaBranch}/configuration-ssl.html#ca-sha256`; + + return { + INSTALL: { + OSX: { + title: i18n.translate('home.tutorials.common.functionbeatInstructions.install.osxTitle', { + defaultMessage: 'Download and install Functionbeat', + }), + textPre: i18n.translate( + 'home.tutorials.common.functionbeatInstructions.install.osxTextPre', + { + defaultMessage: 'First time using Functionbeat? See the [Quick Start]({link}).', + values: { + link: '{config.docs.beats.functionbeat}/functionbeat-installation-configuration.html', + }, + } + ), + commands: [ + 'curl -L -O https://artifacts.elastic.co/downloads/beats/functionbeat/functionbeat-{config.kibana.version}-darwin-x86_64.tar.gz', + 'tar xzvf functionbeat-{config.kibana.version}-darwin-x86_64.tar.gz', + 'cd functionbeat-{config.kibana.version}-darwin-x86_64/', + ], + }, + LINUX: { + title: i18n.translate('home.tutorials.common.functionbeatInstructions.install.linuxTitle', { + defaultMessage: 'Download and install Functionbeat', + }), + textPre: i18n.translate( + 'home.tutorials.common.functionbeatInstructions.install.linuxTextPre', + { + defaultMessage: 'First time using Functionbeat? See the [Quick Start]({link}).', + values: { + link: '{config.docs.beats.functionbeat}/functionbeat-installation-configuration.html', + }, + } + ), + commands: [ + 'curl -L -O https://artifacts.elastic.co/downloads/beats/functionbeat/functionbeat-{config.kibana.version}-linux-x86_64.tar.gz', + 'tar xzvf functionbeat-{config.kibana.version}-linux-x86_64.tar.gz', + 'cd functionbeat-{config.kibana.version}-linux-x86_64/', + ], + }, + WINDOWS: { + title: i18n.translate( + 'home.tutorials.common.functionbeatInstructions.install.windowsTitle', + { + defaultMessage: 'Download and install Functionbeat', + } + ), + textPre: i18n.translate( + 'home.tutorials.common.functionbeatInstructions.install.windowsTextPre', + { + defaultMessage: + 'First time using Functionbeat? See the [Quick Start]({functionbeatLink}).\n\ 1. Download the Functionbeat Windows zip file from the [Download]({elasticLink}) page.\n\ 2. Extract the contents of the zip file into {folderPath}.\n\ 3. Rename the {directoryName} directory to `Functionbeat`.\n\ 4. Open a PowerShell prompt as an Administrator (right-click the PowerShell icon and select \ **Run As Administrator**). If you are running Windows XP, you might need to download and install PowerShell.\n\ 5. From the PowerShell prompt, go to the Functionbeat directory:', - values: { - directoryName: '`functionbeat-{config.kibana.version}-windows`', - folderPath: '`C:\\Program Files`', - functionbeatLink: - '{config.docs.beats.functionbeat}/functionbeat-installation-configuration.html', - elasticLink: 'https://www.elastic.co/downloads/beats/functionbeat', - }, - } - ), - commands: ['cd "C:\\Program Files\\Functionbeat"'], + values: { + directoryName: '`functionbeat-{config.kibana.version}-windows`', + folderPath: '`C:\\Program Files`', + functionbeatLink: + '{config.docs.beats.functionbeat}/functionbeat-installation-configuration.html', + elasticLink: 'https://www.elastic.co/downloads/beats/functionbeat', + }, + } + ), + commands: ['cd "C:\\Program Files\\Functionbeat"'], + }, }, - }, - DEPLOY: { - OSX_LINUX: { - title: i18n.translate('home.tutorials.common.functionbeatInstructions.deploy.osxTitle', { - defaultMessage: 'Deploy Functionbeat to AWS Lambda', - }), - textPre: i18n.translate('home.tutorials.common.functionbeatInstructions.deploy.osxTextPre', { - defaultMessage: - 'This installs Functionbeat as a Lambda function.\ + DEPLOY: { + OSX_LINUX: { + title: i18n.translate('home.tutorials.common.functionbeatInstructions.deploy.osxTitle', { + defaultMessage: 'Deploy Functionbeat to AWS Lambda', + }), + textPre: i18n.translate( + 'home.tutorials.common.functionbeatInstructions.deploy.osxTextPre', + { + defaultMessage: + 'This installs Functionbeat as a Lambda function.\ The `setup` command checks the Elasticsearch configuration and loads the \ Kibana index pattern. It is normally safe to omit this command.', - }), - commands: ['./functionbeat setup', './functionbeat deploy fn-cloudwatch-logs'], - }, - WINDOWS: { - title: i18n.translate('home.tutorials.common.functionbeatInstructions.deploy.windowsTitle', { - defaultMessage: 'Deploy Functionbeat to AWS Lambda', - }), - textPre: i18n.translate( - 'home.tutorials.common.functionbeatInstructions.deploy.windowsTextPre', - { - defaultMessage: - 'This installs Functionbeat as a Lambda function.\ + } + ), + commands: ['./functionbeat setup', './functionbeat deploy fn-cloudwatch-logs'], + }, + WINDOWS: { + title: i18n.translate( + 'home.tutorials.common.functionbeatInstructions.deploy.windowsTitle', + { + defaultMessage: 'Deploy Functionbeat to AWS Lambda', + } + ), + textPre: i18n.translate( + 'home.tutorials.common.functionbeatInstructions.deploy.windowsTextPre', + { + defaultMessage: + 'This installs Functionbeat as a Lambda function.\ The `setup` command checks the Elasticsearch configuration and loads the \ Kibana index pattern. It is normally safe to omit this command.', - } - ), - commands: ['.\\functionbeat.exe setup', '.\\functionbeat.exe deploy fn-cloudwatch-logs'], - }, - }, - CONFIG: { - OSX_LINUX: { - title: i18n.translate('home.tutorials.common.functionbeatInstructions.config.osxTitle', { - defaultMessage: 'Configure the Elastic cluster', - }), - textPre: i18n.translate('home.tutorials.common.functionbeatInstructions.config.osxTextPre', { - defaultMessage: 'Modify {path} to set the connection information:', - values: { - path: '`functionbeat.yml`', - }, - }), - commands: [ - 'output.elasticsearch:', - ' hosts: [""]', - ' username: "elastic"', - ' password: ""', - 'setup.kibana:', - ' host: ""', - getSpaceIdForBeatsTutorial(context), - ], - textPost: i18n.translate( - 'home.tutorials.common.functionbeatInstructions.config.osxTextPost', - { - defaultMessage: - 'Where {passwordTemplate} is the password of the `elastic` user, {esUrlTemplate} is the URL of Elasticsearch, \ -and {kibanaUrlTemplate} is the URL of Kibana.', - values: { - passwordTemplate: '``', - esUrlTemplate: '``', - kibanaUrlTemplate: '``', - }, - } - ), + } + ), + commands: ['.\\functionbeat.exe setup', '.\\functionbeat.exe deploy fn-cloudwatch-logs'], + }, }, - WINDOWS: { - title: i18n.translate('home.tutorials.common.functionbeatInstructions.config.windowsTitle', { - defaultMessage: 'Edit the configuration', - }), - textPre: i18n.translate( - 'home.tutorials.common.functionbeatInstructions.config.windowsTextPre', - { - defaultMessage: 'Modify {path} to set the connection information:', - values: { - path: '`C:\\Program Files\\Functionbeat\\functionbeat.yml`', - }, - } - ), - commands: [ - 'output.elasticsearch:', - ' hosts: [""]', - ' username: "elastic"', - ' password: ""', - 'setup.kibana:', - ' host: ""', - getSpaceIdForBeatsTutorial(context), - ], - textPost: i18n.translate( - 'home.tutorials.common.functionbeatInstructions.config.windowsTextPost', - { - defaultMessage: - 'Where {passwordTemplate} is the password of the `elastic` user, {esUrlTemplate} is the URL of Elasticsearch, \ -and {kibanaUrlTemplate} is the URL of Kibana.', - values: { - passwordTemplate: '``', - esUrlTemplate: '``', - kibanaUrlTemplate: '``', - }, - } - ), + CONFIG: { + OSX_LINUX: { + title: i18n.translate('home.tutorials.common.functionbeatInstructions.config.osxTitle', { + defaultMessage: 'Configure the Elastic cluster', + }), + textPre: i18n.translate( + 'home.tutorials.common.functionbeatInstructions.config.osxTextPre', + { + defaultMessage: 'Modify {path} to set the connection information:', + values: { + path: '`functionbeat.yml`', + }, + } + ), + commands: [ + 'output.elasticsearch:', + ' hosts: [""]', + ' username: "elastic"', + ' password: ""', + " # If using Elasticsearch's default certificate", + ' ssl.ca_trusted_fingerprint: ""', + 'setup.kibana:', + ' host: ""', + getSpaceIdForBeatsTutorial(context), + ], + textPost: i18n.translate( + 'home.tutorials.common.functionbeatInstructions.config.osxTextPostMarkdown', + { + defaultMessage: + 'Where {passwordTemplate} is the password of the `elastic` user, {esUrlTemplate} is the URL of \ + Elasticsearch, and {kibanaUrlTemplate} is the URL of Kibana. To [configure SSL]({configureSslUrl}) with the \ + default certificate generated by Elasticsearch, add its fingerprint in {esCertFingerprintTemplate}.', + values: { + passwordTemplate: '``', + esUrlTemplate: '``', + kibanaUrlTemplate: '``', + configureSslUrl: SSL_DOC_URL, + esCertFingerprintTemplate: '``', + }, + } + ), + }, + WINDOWS: { + title: i18n.translate( + 'home.tutorials.common.functionbeatInstructions.config.windowsTitle', + { + defaultMessage: 'Edit the configuration', + } + ), + textPre: i18n.translate( + 'home.tutorials.common.functionbeatInstructions.config.windowsTextPre', + { + defaultMessage: 'Modify {path} to set the connection information:', + values: { + path: '`C:\\Program Files\\Functionbeat\\functionbeat.yml`', + }, + } + ), + commands: [ + 'output.elasticsearch:', + ' hosts: [""]', + ' username: "elastic"', + ' password: ""', + " # If using Elasticsearch's default certificate", + ' ssl.ca_trusted_fingerprint: ""', + 'setup.kibana:', + ' host: ""', + getSpaceIdForBeatsTutorial(context), + ], + textPost: i18n.translate( + 'home.tutorials.common.functionbeatInstructions.config.windowsTextPostMarkdown', + { + defaultMessage: + 'Where {passwordTemplate} is the password of the `elastic` user, {esUrlTemplate} is the URL of \ + Elasticsearch, and {kibanaUrlTemplate} is the URL of Kibana. To [configure SSL]({configureSslUrl}) with the \ + default certificate generated by Elasticsearch, add its fingerprint in {esCertFingerprintTemplate}.', + values: { + passwordTemplate: '``', + esUrlTemplate: '``', + kibanaUrlTemplate: '``', + configureSslUrl: SSL_DOC_URL, + esCertFingerprintTemplate: '``', + }, + } + ), + }, }, - }, -}); + }; +}; export const createFunctionbeatCloudInstructions = () => ({ CONFIG: { @@ -336,7 +368,7 @@ export function functionbeatStatusCheck() { }; } -export function onPremInstructions(platforms: Platform[], context?: TutorialContext) { +export function onPremInstructions(platforms: Platform[], context: TutorialContext) { const FUNCTIONBEAT_INSTRUCTIONS = createFunctionbeatInstructions(context); return { @@ -386,10 +418,10 @@ export function onPremInstructions(platforms: Platform[], context?: TutorialCont }; } -export function onPremCloudInstructions() { +export function onPremCloudInstructions(context: TutorialContext) { const TRYCLOUD_OPTION1 = createTrycloudOption1(); const TRYCLOUD_OPTION2 = createTrycloudOption2(); - const FUNCTIONBEAT_INSTRUCTIONS = createFunctionbeatInstructions(); + const FUNCTIONBEAT_INSTRUCTIONS = createFunctionbeatInstructions(context); return { instructionSets: [ @@ -444,8 +476,8 @@ export function onPremCloudInstructions() { }; } -export function cloudInstructions() { - const FUNCTIONBEAT_INSTRUCTIONS = createFunctionbeatInstructions(); +export function cloudInstructions(context: TutorialContext) { + const FUNCTIONBEAT_INSTRUCTIONS = createFunctionbeatInstructions(context); const FUNCTIONBEAT_CLOUD_INSTRUCTIONS = createFunctionbeatCloudInstructions(); return { diff --git a/src/plugins/home/server/tutorials/instructions/heartbeat_instructions.ts b/src/plugins/home/server/tutorials/instructions/heartbeat_instructions.ts index ce3e76a5f827e..5cbd1641bf09a 100644 --- a/src/plugins/home/server/tutorials/instructions/heartbeat_instructions.ts +++ b/src/plugins/home/server/tutorials/instructions/heartbeat_instructions.ts @@ -13,247 +13,298 @@ import { getSpaceIdForBeatsTutorial } from './get_space_id_for_beats_tutorial'; import { Platform, TutorialContext } from '../../services/tutorials/lib/tutorials_registry_types'; import { cloudPasswordAndResetLink } from './cloud_instructions'; -export const createHeartbeatInstructions = (context?: TutorialContext) => ({ - INSTALL: { - OSX: { - title: i18n.translate('home.tutorials.common.heartbeatInstructions.install.osxTitle', { - defaultMessage: 'Download and install Heartbeat', - }), - textPre: i18n.translate('home.tutorials.common.heartbeatInstructions.install.osxTextPre', { - defaultMessage: 'First time using Heartbeat? See the [Quick Start]({link}).', - values: { link: '{config.docs.beats.heartbeat}/heartbeat-installation-configuration.html' }, - }), - commands: [ - 'curl -L -O https://artifacts.elastic.co/downloads/beats/heartbeat/heartbeat-{config.kibana.version}-darwin-x86_64.tar.gz', - 'tar xzvf heartbeat-{config.kibana.version}-darwin-x86_64.tar.gz', - 'cd heartbeat-{config.kibana.version}-darwin-x86_64/', - ], - }, - DEB: { - title: i18n.translate('home.tutorials.common.heartbeatInstructions.install.debTitle', { - defaultMessage: 'Download and install Heartbeat', - }), - textPre: i18n.translate('home.tutorials.common.heartbeatInstructions.install.debTextPre', { - defaultMessage: 'First time using Heartbeat? See the [Quick Start]({link}).', - values: { link: '{config.docs.beats.heartbeat}/heartbeat-installation-configuration.html' }, - }), - commands: [ - 'curl -L -O https://artifacts.elastic.co/downloads/beats/heartbeat/heartbeat-{config.kibana.version}-amd64.deb', - 'sudo dpkg -i heartbeat-{config.kibana.version}-amd64.deb', - ], - textPost: i18n.translate('home.tutorials.common.heartbeatInstructions.install.debTextPost', { - defaultMessage: 'Looking for the 32-bit packages? See the [Download page]({link}).', - values: { link: 'https://www.elastic.co/downloads/beats/heartbeat' }, - }), - }, - RPM: { - title: i18n.translate('home.tutorials.common.heartbeatInstructions.install.rpmTitle', { - defaultMessage: 'Download and install Heartbeat', - }), - textPre: i18n.translate('home.tutorials.common.heartbeatInstructions.install.rpmTextPre', { - defaultMessage: 'First time using Heartbeat? See the [Quick Start]({link}).', - values: { link: '{config.docs.beats.heartbeat}/heartbeat-installation-configuration.html' }, - }), - commands: [ - 'curl -L -O https://artifacts.elastic.co/downloads/beats/heartbeat/heartbeat-{config.kibana.version}-x86_64.rpm', - 'sudo rpm -vi heartbeat-{config.kibana.version}-x86_64.rpm', - ], - textPost: i18n.translate('home.tutorials.common.heartbeatInstructions.install.debTextPost', { - defaultMessage: 'Looking for the 32-bit packages? See the [Download page]({link}).', - values: { link: 'https://www.elastic.co/downloads/beats/heartbeat' }, - }), - }, - WINDOWS: { - title: i18n.translate('home.tutorials.common.heartbeatInstructions.install.windowsTitle', { - defaultMessage: 'Download and install Heartbeat', - }), - textPre: i18n.translate( - 'home.tutorials.common.heartbeatInstructions.install.windowsTextPre', - { - defaultMessage: - 'First time using Heartbeat? See the [Quick Start]({heartbeatLink}).\n\ +export const createHeartbeatInstructions = (context: TutorialContext) => { + const SSL_DOC_URL = `https://www.elastic.co/guide/en/beats/heartbeat/${context.kibanaBranch}/configuration-ssl.html#ca-sha256`; + + return { + INSTALL: { + OSX: { + title: i18n.translate('home.tutorials.common.heartbeatInstructions.install.osxTitle', { + defaultMessage: 'Download and install Heartbeat', + }), + textPre: i18n.translate('home.tutorials.common.heartbeatInstructions.install.osxTextPre', { + defaultMessage: 'First time using Heartbeat? See the [Quick Start]({link}).', + values: { + link: '{config.docs.beats.heartbeat}/heartbeat-installation-configuration.html', + }, + }), + commands: [ + 'curl -L -O https://artifacts.elastic.co/downloads/beats/heartbeat/heartbeat-{config.kibana.version}-darwin-x86_64.tar.gz', + 'tar xzvf heartbeat-{config.kibana.version}-darwin-x86_64.tar.gz', + 'cd heartbeat-{config.kibana.version}-darwin-x86_64/', + ], + }, + DEB: { + title: i18n.translate('home.tutorials.common.heartbeatInstructions.install.debTitle', { + defaultMessage: 'Download and install Heartbeat', + }), + textPre: i18n.translate('home.tutorials.common.heartbeatInstructions.install.debTextPre', { + defaultMessage: 'First time using Heartbeat? See the [Quick Start]({link}).', + values: { + link: '{config.docs.beats.heartbeat}/heartbeat-installation-configuration.html', + }, + }), + commands: [ + 'curl -L -O https://artifacts.elastic.co/downloads/beats/heartbeat/heartbeat-{config.kibana.version}-amd64.deb', + 'sudo dpkg -i heartbeat-{config.kibana.version}-amd64.deb', + ], + textPost: i18n.translate( + 'home.tutorials.common.heartbeatInstructions.install.debTextPost', + { + defaultMessage: 'Looking for the 32-bit packages? See the [Download page]({link}).', + values: { link: 'https://www.elastic.co/downloads/beats/heartbeat' }, + } + ), + }, + RPM: { + title: i18n.translate('home.tutorials.common.heartbeatInstructions.install.rpmTitle', { + defaultMessage: 'Download and install Heartbeat', + }), + textPre: i18n.translate('home.tutorials.common.heartbeatInstructions.install.rpmTextPre', { + defaultMessage: 'First time using Heartbeat? See the [Quick Start]({link}).', + values: { + link: '{config.docs.beats.heartbeat}/heartbeat-installation-configuration.html', + }, + }), + commands: [ + 'curl -L -O https://artifacts.elastic.co/downloads/beats/heartbeat/heartbeat-{config.kibana.version}-x86_64.rpm', + 'sudo rpm -vi heartbeat-{config.kibana.version}-x86_64.rpm', + ], + textPost: i18n.translate( + 'home.tutorials.common.heartbeatInstructions.install.debTextPost', + { + defaultMessage: 'Looking for the 32-bit packages? See the [Download page]({link}).', + values: { link: 'https://www.elastic.co/downloads/beats/heartbeat' }, + } + ), + }, + WINDOWS: { + title: i18n.translate('home.tutorials.common.heartbeatInstructions.install.windowsTitle', { + defaultMessage: 'Download and install Heartbeat', + }), + textPre: i18n.translate( + 'home.tutorials.common.heartbeatInstructions.install.windowsTextPre', + { + defaultMessage: + 'First time using Heartbeat? See the [Quick Start]({heartbeatLink}).\n\ 1. Download the Heartbeat Windows zip file from the [Download]({elasticLink}) page.\n\ 2. Extract the contents of the zip file into {folderPath}.\n\ 3. Rename the {directoryName} directory to `Heartbeat`.\n\ 4. Open a PowerShell prompt as an Administrator (right-click the PowerShell icon and select \ **Run As Administrator**). If you are running Windows XP, you might need to download and install PowerShell.\n\ 5. From the PowerShell prompt, run the following commands to install Heartbeat as a Windows service.', - values: { - directoryName: '`heartbeat-{config.kibana.version}-windows`', - folderPath: '`C:\\Program Files`', - heartbeatLink: - '{config.docs.beats.heartbeat}/heartbeat-installation-configuration.html', - elasticLink: 'https://www.elastic.co/downloads/beats/heartbeat', - }, - } - ), - commands: ['cd "C:\\Program Files\\Heartbeat"', '.\\install-service-heartbeat.ps1'], - }, - }, - START: { - OSX: { - title: i18n.translate('home.tutorials.common.heartbeatInstructions.start.osxTitle', { - defaultMessage: 'Start Heartbeat', - }), - textPre: i18n.translate('home.tutorials.common.heartbeatInstructions.start.osxTextPre', { - defaultMessage: 'The `setup` command loads the Kibana index pattern.', - }), - commands: ['./heartbeat setup', './heartbeat -e'], - }, - DEB: { - title: i18n.translate('home.tutorials.common.heartbeatInstructions.start.debTitle', { - defaultMessage: 'Start Heartbeat', - }), - textPre: i18n.translate('home.tutorials.common.heartbeatInstructions.start.debTextPre', { - defaultMessage: 'The `setup` command loads the Kibana index pattern.', - }), - commands: ['sudo heartbeat setup', 'sudo service heartbeat-elastic start'], - }, - RPM: { - title: i18n.translate('home.tutorials.common.heartbeatInstructions.start.rpmTitle', { - defaultMessage: 'Start Heartbeat', - }), - textPre: i18n.translate('home.tutorials.common.heartbeatInstructions.start.rpmTextPre', { - defaultMessage: 'The `setup` command loads the Kibana index pattern.', - }), - commands: ['sudo heartbeat setup', 'sudo service heartbeat-elastic start'], - }, - WINDOWS: { - title: i18n.translate('home.tutorials.common.heartbeatInstructions.start.windowsTitle', { - defaultMessage: 'Start Heartbeat', - }), - textPre: i18n.translate('home.tutorials.common.heartbeatInstructions.start.windowsTextPre', { - defaultMessage: 'The `setup` command loads the Kibana index pattern.', - }), - commands: ['.\\heartbeat.exe setup', 'Start-Service heartbeat'], - }, - }, - CONFIG: { - OSX: { - title: i18n.translate('home.tutorials.common.heartbeatInstructions.config.osxTitle', { - defaultMessage: 'Edit the configuration', - }), - textPre: i18n.translate('home.tutorials.common.heartbeatInstructions.config.osxTextPre', { - defaultMessage: 'Modify {path} to set the connection information:', - values: { - path: '`heartbeat.yml`', - }, - }), - commands: [ - 'output.elasticsearch:', - ' hosts: [""]', - ' username: "elastic"', - ' password: ""', - 'setup.kibana:', - ' host: ""', - getSpaceIdForBeatsTutorial(context), - ], - textPost: i18n.translate('home.tutorials.common.heartbeatInstructions.config.osxTextPost', { - defaultMessage: - 'Where {passwordTemplate} is the password of the `elastic` user, {esUrlTemplate} is the URL of Elasticsearch, \ -and {kibanaUrlTemplate} is the URL of Kibana.', - values: { - passwordTemplate: '``', - esUrlTemplate: '``', - kibanaUrlTemplate: '``', - }, - }), - }, - DEB: { - title: i18n.translate('home.tutorials.common.heartbeatInstructions.config.debTitle', { - defaultMessage: 'Edit the configuration', - }), - textPre: i18n.translate('home.tutorials.common.heartbeatInstructions.config.debTextPre', { - defaultMessage: 'Modify {path} to set the connection information:', - values: { - path: '`/etc/heartbeat/heartbeat.yml`', - }, - }), - commands: [ - 'output.elasticsearch:', - ' hosts: [""]', - ' username: "elastic"', - ' password: ""', - 'setup.kibana:', - ' host: ""', - getSpaceIdForBeatsTutorial(context), - ], - textPost: i18n.translate('home.tutorials.common.heartbeatInstructions.config.debTextPost', { - defaultMessage: - 'Where {passwordTemplate} is the password of the `elastic` user, {esUrlTemplate} is the URL of Elasticsearch, \ -and {kibanaUrlTemplate} is the URL of Kibana.', - values: { - passwordTemplate: '``', - esUrlTemplate: '``', - kibanaUrlTemplate: '``', - }, - }), + values: { + directoryName: '`heartbeat-{config.kibana.version}-windows`', + folderPath: '`C:\\Program Files`', + heartbeatLink: + '{config.docs.beats.heartbeat}/heartbeat-installation-configuration.html', + elasticLink: 'https://www.elastic.co/downloads/beats/heartbeat', + }, + } + ), + commands: ['cd "C:\\Program Files\\Heartbeat"', '.\\install-service-heartbeat.ps1'], + }, }, - RPM: { - title: i18n.translate('home.tutorials.common.heartbeatInstructions.config.rpmTitle', { - defaultMessage: 'Edit the configuration', - }), - textPre: i18n.translate('home.tutorials.common.heartbeatInstructions.config.rpmTextPre', { - defaultMessage: 'Modify {path} to set the connection information:', - values: { - path: '`/etc/heartbeat/heartbeat.yml`', - }, - }), - commands: [ - 'output.elasticsearch:', - ' hosts: [""]', - ' username: "elastic"', - ' password: ""', - 'setup.kibana:', - ' host: ""', - getSpaceIdForBeatsTutorial(context), - ], - textPost: i18n.translate('home.tutorials.common.heartbeatInstructions.config.rpmTextPost', { - defaultMessage: - 'Where {passwordTemplate} is the password of the `elastic` user, {esUrlTemplate} is the URL of Elasticsearch, \ -and {kibanaUrlTemplate} is the URL of Kibana.', - values: { - passwordTemplate: '``', - esUrlTemplate: '``', - kibanaUrlTemplate: '``', - }, - }), + START: { + OSX: { + title: i18n.translate('home.tutorials.common.heartbeatInstructions.start.osxTitle', { + defaultMessage: 'Start Heartbeat', + }), + textPre: i18n.translate('home.tutorials.common.heartbeatInstructions.start.osxTextPre', { + defaultMessage: 'The `setup` command loads the Kibana index pattern.', + }), + commands: ['./heartbeat setup', './heartbeat -e'], + }, + DEB: { + title: i18n.translate('home.tutorials.common.heartbeatInstructions.start.debTitle', { + defaultMessage: 'Start Heartbeat', + }), + textPre: i18n.translate('home.tutorials.common.heartbeatInstructions.start.debTextPre', { + defaultMessage: 'The `setup` command loads the Kibana index pattern.', + }), + commands: ['sudo heartbeat setup', 'sudo service heartbeat-elastic start'], + }, + RPM: { + title: i18n.translate('home.tutorials.common.heartbeatInstructions.start.rpmTitle', { + defaultMessage: 'Start Heartbeat', + }), + textPre: i18n.translate('home.tutorials.common.heartbeatInstructions.start.rpmTextPre', { + defaultMessage: 'The `setup` command loads the Kibana index pattern.', + }), + commands: ['sudo heartbeat setup', 'sudo service heartbeat-elastic start'], + }, + WINDOWS: { + title: i18n.translate('home.tutorials.common.heartbeatInstructions.start.windowsTitle', { + defaultMessage: 'Start Heartbeat', + }), + textPre: i18n.translate( + 'home.tutorials.common.heartbeatInstructions.start.windowsTextPre', + { + defaultMessage: 'The `setup` command loads the Kibana index pattern.', + } + ), + commands: ['.\\heartbeat.exe setup', 'Start-Service heartbeat'], + }, }, - WINDOWS: { - title: i18n.translate('home.tutorials.common.heartbeatInstructions.config.windowsTitle', { - defaultMessage: 'Edit the configuration', - }), - textPre: i18n.translate('home.tutorials.common.heartbeatInstructions.config.windowsTextPre', { - defaultMessage: 'Modify {path} to set the connection information:', - values: { - path: '`C:\\Program Files\\Heartbeat\\heartbeat.yml`', - }, - }), - commands: [ - 'output.elasticsearch:', - ' hosts: [""]', - ' username: "elastic"', - ' password: ""', - 'setup.kibana:', - ' host: ""', - getSpaceIdForBeatsTutorial(context), - ], - textPost: i18n.translate( - 'home.tutorials.common.heartbeatInstructions.config.windowsTextPost', - { - defaultMessage: - 'Where {passwordTemplate} is the password of the `elastic` user, {esUrlTemplate} is the URL of Elasticsearch, \ -and {kibanaUrlTemplate} is the URL of Kibana.', + CONFIG: { + OSX: { + title: i18n.translate('home.tutorials.common.heartbeatInstructions.config.osxTitle', { + defaultMessage: 'Edit the configuration', + }), + textPre: i18n.translate('home.tutorials.common.heartbeatInstructions.config.osxTextPre', { + defaultMessage: 'Modify {path} to set the connection information:', values: { - passwordTemplate: '``', - esUrlTemplate: '``', - kibanaUrlTemplate: '``', + path: '`heartbeat.yml`', }, - } - ), + }), + commands: [ + 'output.elasticsearch:', + ' hosts: [""]', + ' username: "elastic"', + ' password: ""', + " # If using Elasticsearch's default certificate", + ' ssl.ca_trusted_fingerprint: ""', + 'setup.kibana:', + ' host: ""', + getSpaceIdForBeatsTutorial(context), + ], + textPost: i18n.translate( + 'home.tutorials.common.heartbeatInstructions.config.osxTextPostMarkdown', + { + defaultMessage: + 'Where {passwordTemplate} is the password of the `elastic` user, {esUrlTemplate} is the URL of \ + Elasticsearch, and {kibanaUrlTemplate} is the URL of Kibana. To [configure SSL]({configureSslUrl}) with the \ + default certificate generated by Elasticsearch, add its fingerprint in {esCertFingerprintTemplate}.', + values: { + passwordTemplate: '``', + esUrlTemplate: '``', + kibanaUrlTemplate: '``', + configureSslUrl: SSL_DOC_URL, + esCertFingerprintTemplate: '``', + }, + } + ), + }, + DEB: { + title: i18n.translate('home.tutorials.common.heartbeatInstructions.config.debTitle', { + defaultMessage: 'Edit the configuration', + }), + textPre: i18n.translate('home.tutorials.common.heartbeatInstructions.config.debTextPre', { + defaultMessage: 'Modify {path} to set the connection information:', + values: { + path: '`/etc/heartbeat/heartbeat.yml`', + }, + }), + commands: [ + 'output.elasticsearch:', + ' hosts: [""]', + ' username: "elastic"', + ' password: ""', + " # If using Elasticsearch's default certificate", + ' ssl.ca_trusted_fingerprint: ""', + 'setup.kibana:', + ' host: ""', + getSpaceIdForBeatsTutorial(context), + ], + textPost: i18n.translate( + 'home.tutorials.common.heartbeatInstructions.config.debTextPostMarkdown', + { + defaultMessage: + 'Where {passwordTemplate} is the password of the `elastic` user, {esUrlTemplate} is the URL of \ + Elasticsearch, and {kibanaUrlTemplate} is the URL of Kibana. To [configure SSL]({configureSslUrl}) with the \ + default certificate generated by Elasticsearch, add its fingerprint in {esCertFingerprintTemplate}.', + values: { + passwordTemplate: '``', + esUrlTemplate: '``', + kibanaUrlTemplate: '``', + configureSslUrl: SSL_DOC_URL, + esCertFingerprintTemplate: '``', + }, + } + ), + }, + RPM: { + title: i18n.translate('home.tutorials.common.heartbeatInstructions.config.rpmTitle', { + defaultMessage: 'Edit the configuration', + }), + textPre: i18n.translate('home.tutorials.common.heartbeatInstructions.config.rpmTextPre', { + defaultMessage: 'Modify {path} to set the connection information:', + values: { + path: '`/etc/heartbeat/heartbeat.yml`', + }, + }), + commands: [ + 'output.elasticsearch:', + ' hosts: [""]', + ' username: "elastic"', + ' password: ""', + " # If using Elasticsearch's default certificate", + ' ssl.ca_trusted_fingerprint: ""', + 'setup.kibana:', + ' host: ""', + getSpaceIdForBeatsTutorial(context), + ], + textPost: i18n.translate( + 'home.tutorials.common.heartbeatInstructions.config.rpmTextPostMarkdown', + { + defaultMessage: + 'Where {passwordTemplate} is the password of the `elastic` user, {esUrlTemplate} is the URL of \ + Elasticsearch, and {kibanaUrlTemplate} is the URL of Kibana. To [configure SSL]({configureSslUrl}) with the \ + default certificate generated by Elasticsearch, add its fingerprint in {esCertFingerprintTemplate}.', + values: { + passwordTemplate: '``', + esUrlTemplate: '``', + kibanaUrlTemplate: '``', + configureSslUrl: SSL_DOC_URL, + esCertFingerprintTemplate: '``', + }, + } + ), + }, + WINDOWS: { + title: i18n.translate('home.tutorials.common.heartbeatInstructions.config.windowsTitle', { + defaultMessage: 'Edit the configuration', + }), + textPre: i18n.translate( + 'home.tutorials.common.heartbeatInstructions.config.windowsTextPre', + { + defaultMessage: 'Modify {path} to set the connection information:', + values: { + path: '`C:\\Program Files\\Heartbeat\\heartbeat.yml`', + }, + } + ), + commands: [ + 'output.elasticsearch:', + ' hosts: [""]', + ' username: "elastic"', + ' password: ""', + " # If using Elasticsearch's default certificate", + ' ssl.ca_trusted_fingerprint: ""', + 'setup.kibana:', + ' host: ""', + getSpaceIdForBeatsTutorial(context), + ], + textPost: i18n.translate( + 'home.tutorials.common.heartbeatInstructions.config.windowsTextPostMarkdown', + { + defaultMessage: + 'Where {passwordTemplate} is the password of the `elastic` user, {esUrlTemplate} is the URL of \ + Elasticsearch, and {kibanaUrlTemplate} is the URL of Kibana. To [configure SSL]({configureSslUrl}) with the \ + default certificate generated by Elasticsearch, add its fingerprint in {esCertFingerprintTemplate}.', + values: { + passwordTemplate: '``', + esUrlTemplate: '``', + kibanaUrlTemplate: '``', + configureSslUrl: SSL_DOC_URL, + esCertFingerprintTemplate: '``', + }, + } + ), + }, }, - }, -}); + }; +}; export const createHeartbeatCloudInstructions = () => ({ CONFIG: { @@ -486,7 +537,7 @@ export function heartbeatStatusCheck() { }; } -export function onPremInstructions(platforms: Platform[], context?: TutorialContext) { +export function onPremInstructions(platforms: Platform[], context: TutorialContext) { const HEARTBEAT_INSTRUCTIONS = createHeartbeatInstructions(context); return { @@ -542,10 +593,10 @@ export function onPremInstructions(platforms: Platform[], context?: TutorialCont }; } -export function onPremCloudInstructions() { +export function onPremCloudInstructions(context: TutorialContext) { const TRYCLOUD_OPTION1 = createTrycloudOption1(); const TRYCLOUD_OPTION2 = createTrycloudOption2(); - const HEARTBEAT_INSTRUCTIONS = createHeartbeatInstructions(); + const HEARTBEAT_INSTRUCTIONS = createHeartbeatInstructions(context); return { instructionSets: [ @@ -608,8 +659,8 @@ export function onPremCloudInstructions() { }; } -export function cloudInstructions() { - const HEARTBEAT_INSTRUCTIONS = createHeartbeatInstructions(); +export function cloudInstructions(context: TutorialContext) { + const HEARTBEAT_INSTRUCTIONS = createHeartbeatInstructions(context); const HEARTBEAT_CLOUD_INSTRUCTIONS = createHeartbeatCloudInstructions(); return { diff --git a/src/plugins/home/server/tutorials/instructions/metricbeat_instructions.ts b/src/plugins/home/server/tutorials/instructions/metricbeat_instructions.ts index d6f2fcb232f12..02cd53dddbc1f 100644 --- a/src/plugins/home/server/tutorials/instructions/metricbeat_instructions.ts +++ b/src/plugins/home/server/tutorials/instructions/metricbeat_instructions.ts @@ -13,268 +13,310 @@ import { getSpaceIdForBeatsTutorial } from './get_space_id_for_beats_tutorial'; import { TutorialContext } from '../../services/tutorials/lib/tutorials_registry_types'; import { cloudPasswordAndResetLink } from './cloud_instructions'; -export const createMetricbeatInstructions = (context?: TutorialContext) => ({ - INSTALL: { - OSX: { - title: i18n.translate('home.tutorials.common.metricbeatInstructions.install.osxTitle', { - defaultMessage: 'Download and install Metricbeat', - }), - textPre: i18n.translate('home.tutorials.common.metricbeatInstructions.install.osxTextPre', { - defaultMessage: 'First time using Metricbeat? See the [Quick Start]({link}).', - values: { - link: '{config.docs.beats.metricbeat}/metricbeat-installation-configuration.html', - }, - }), - commands: [ - 'curl -L -O https://artifacts.elastic.co/downloads/beats/metricbeat/metricbeat-{config.kibana.version}-darwin-x86_64.tar.gz', - 'tar xzvf metricbeat-{config.kibana.version}-darwin-x86_64.tar.gz', - 'cd metricbeat-{config.kibana.version}-darwin-x86_64/', - ], - }, - DEB: { - title: i18n.translate('home.tutorials.common.metricbeatInstructions.install.debTitle', { - defaultMessage: 'Download and install Metricbeat', - }), - textPre: i18n.translate('home.tutorials.common.metricbeatInstructions.install.debTextPre', { - defaultMessage: 'First time using Metricbeat? See the [Quick Start]({link}).', - values: { - link: '{config.docs.beats.metricbeat}/metricbeat-installation-configuration.html', - }, - }), - commands: [ - 'curl -L -O https://artifacts.elastic.co/downloads/beats/metricbeat/metricbeat-{config.kibana.version}-amd64.deb', - 'sudo dpkg -i metricbeat-{config.kibana.version}-amd64.deb', - ], - textPost: i18n.translate('home.tutorials.common.metricbeatInstructions.install.debTextPost', { - defaultMessage: 'Looking for the 32-bit packages? See the [Download page]({link}).', - values: { link: 'https://www.elastic.co/downloads/beats/metricbeat' }, - }), - }, - RPM: { - title: i18n.translate('home.tutorials.common.metricbeatInstructions.install.rpmTitle', { - defaultMessage: 'Download and install Metricbeat', - }), - textPre: i18n.translate('home.tutorials.common.metricbeatInstructions.install.rpmTextPre', { - defaultMessage: 'First time using Metricbeat? See the [Quick Start]({link}).', - values: { - link: '{config.docs.beats.metricbeat}/metricbeat-installation-configuration.html', - }, - }), - commands: [ - 'curl -L -O https://artifacts.elastic.co/downloads/beats/metricbeat/metricbeat-{config.kibana.version}-x86_64.rpm', - 'sudo rpm -vi metricbeat-{config.kibana.version}-x86_64.rpm', - ], - textPost: i18n.translate('home.tutorials.common.metricbeatInstructions.install.debTextPost', { - defaultMessage: 'Looking for the 32-bit packages? See the [Download page]({link}).', - values: { link: 'https://www.elastic.co/downloads/beats/metricbeat' }, - }), - }, - WINDOWS: { - title: i18n.translate('home.tutorials.common.metricbeatInstructions.install.windowsTitle', { - defaultMessage: 'Download and install Metricbeat', - }), - textPre: i18n.translate( - 'home.tutorials.common.metricbeatInstructions.install.windowsTextPre', - { - defaultMessage: - 'First time using Metricbeat? See the [Quick Start]({metricbeatLink}).\n\ +export const createMetricbeatInstructions = (context: TutorialContext) => { + const SSL_DOC_URL = `https://www.elastic.co/guide/en/beats/metricbeat/${context.kibanaBranch}/configuration-ssl.html#ca-sha256`; + + return { + INSTALL: { + OSX: { + title: i18n.translate('home.tutorials.common.metricbeatInstructions.install.osxTitle', { + defaultMessage: 'Download and install Metricbeat', + }), + textPre: i18n.translate('home.tutorials.common.metricbeatInstructions.install.osxTextPre', { + defaultMessage: 'First time using Metricbeat? See the [Quick Start]({link}).', + values: { + link: '{config.docs.beats.metricbeat}/metricbeat-installation-configuration.html', + }, + }), + commands: [ + 'curl -L -O https://artifacts.elastic.co/downloads/beats/metricbeat/metricbeat-{config.kibana.version}-darwin-x86_64.tar.gz', + 'tar xzvf metricbeat-{config.kibana.version}-darwin-x86_64.tar.gz', + 'cd metricbeat-{config.kibana.version}-darwin-x86_64/', + ], + }, + DEB: { + title: i18n.translate('home.tutorials.common.metricbeatInstructions.install.debTitle', { + defaultMessage: 'Download and install Metricbeat', + }), + textPre: i18n.translate('home.tutorials.common.metricbeatInstructions.install.debTextPre', { + defaultMessage: 'First time using Metricbeat? See the [Quick Start]({link}).', + values: { + link: '{config.docs.beats.metricbeat}/metricbeat-installation-configuration.html', + }, + }), + commands: [ + 'curl -L -O https://artifacts.elastic.co/downloads/beats/metricbeat/metricbeat-{config.kibana.version}-amd64.deb', + 'sudo dpkg -i metricbeat-{config.kibana.version}-amd64.deb', + ], + textPost: i18n.translate( + 'home.tutorials.common.metricbeatInstructions.install.debTextPost', + { + defaultMessage: 'Looking for the 32-bit packages? See the [Download page]({link}).', + values: { link: 'https://www.elastic.co/downloads/beats/metricbeat' }, + } + ), + }, + RPM: { + title: i18n.translate('home.tutorials.common.metricbeatInstructions.install.rpmTitle', { + defaultMessage: 'Download and install Metricbeat', + }), + textPre: i18n.translate('home.tutorials.common.metricbeatInstructions.install.rpmTextPre', { + defaultMessage: 'First time using Metricbeat? See the [Quick Start]({link}).', + values: { + link: '{config.docs.beats.metricbeat}/metricbeat-installation-configuration.html', + }, + }), + commands: [ + 'curl -L -O https://artifacts.elastic.co/downloads/beats/metricbeat/metricbeat-{config.kibana.version}-x86_64.rpm', + 'sudo rpm -vi metricbeat-{config.kibana.version}-x86_64.rpm', + ], + textPost: i18n.translate( + 'home.tutorials.common.metricbeatInstructions.install.debTextPost', + { + defaultMessage: 'Looking for the 32-bit packages? See the [Download page]({link}).', + values: { link: 'https://www.elastic.co/downloads/beats/metricbeat' }, + } + ), + }, + WINDOWS: { + title: i18n.translate('home.tutorials.common.metricbeatInstructions.install.windowsTitle', { + defaultMessage: 'Download and install Metricbeat', + }), + textPre: i18n.translate( + 'home.tutorials.common.metricbeatInstructions.install.windowsTextPre', + { + defaultMessage: + 'First time using Metricbeat? See the [Quick Start]({metricbeatLink}).\n\ 1. Download the Metricbeat Windows zip file from the [Download]({elasticLink}) page.\n\ 2. Extract the contents of the zip file into {folderPath}.\n\ 3. Rename the {directoryName} directory to `Metricbeat`.\n\ 4. Open a PowerShell prompt as an Administrator (right-click the PowerShell icon and select \ **Run As Administrator**). If you are running Windows XP, you might need to download and install PowerShell.\n\ 5. From the PowerShell prompt, run the following commands to install Metricbeat as a Windows service.', - values: { - directoryName: '`metricbeat-{config.kibana.version}-windows`', - folderPath: '`C:\\Program Files`', - metricbeatLink: - '{config.docs.beats.metricbeat}/metricbeat-installation-configuration.html', - elasticLink: 'https://www.elastic.co/downloads/beats/metricbeat', - }, - } - ), - commands: ['cd "C:\\Program Files\\Metricbeat"', '.\\install-service-metricbeat.ps1'], - textPost: i18n.translate( - 'home.tutorials.common.metricbeatInstructions.install.windowsTextPost', - { - defaultMessage: - 'Modify the settings under `output.elasticsearch` in the {path} file to point to your Elasticsearch installation.', - values: { path: '`C:\\Program Files\\Metricbeat\\metricbeat.yml`' }, - } - ), - }, - }, - START: { - OSX: { - title: i18n.translate('home.tutorials.common.metricbeatInstructions.start.osxTitle', { - defaultMessage: 'Start Metricbeat', - }), - textPre: i18n.translate('home.tutorials.common.metricbeatInstructions.start.osxTextPre', { - defaultMessage: - 'The `setup` command loads the Kibana dashboards. If the dashboards are already set up, omit this command.', - }), - commands: ['./metricbeat setup', './metricbeat -e'], - }, - DEB: { - title: i18n.translate('home.tutorials.common.metricbeatInstructions.start.debTitle', { - defaultMessage: 'Start Metricbeat', - }), - textPre: i18n.translate('home.tutorials.common.metricbeatInstructions.start.debTextPre', { - defaultMessage: - 'The `setup` command loads the Kibana dashboards. If the dashboards are already set up, omit this command.', - }), - commands: ['sudo metricbeat setup', 'sudo service metricbeat start'], - }, - RPM: { - title: i18n.translate('home.tutorials.common.metricbeatInstructions.start.rpmTitle', { - defaultMessage: 'Start Metricbeat', - }), - textPre: i18n.translate('home.tutorials.common.metricbeatInstructions.start.rpmTextPre', { - defaultMessage: - 'The `setup` command loads the Kibana dashboards. If the dashboards are already set up, omit this command.', - }), - commands: ['sudo metricbeat setup', 'sudo service metricbeat start'], - }, - WINDOWS: { - title: i18n.translate('home.tutorials.common.metricbeatInstructions.start.windowsTitle', { - defaultMessage: 'Start Metricbeat', - }), - textPre: i18n.translate('home.tutorials.common.metricbeatInstructions.start.windowsTextPre', { - defaultMessage: - 'The `setup` command loads the Kibana dashboards. If the dashboards are already set up, omit this command.', - }), - commands: ['.\\metricbeat.exe setup', 'Start-Service metricbeat'], - }, - }, - CONFIG: { - OSX: { - title: i18n.translate('home.tutorials.common.metricbeatInstructions.config.osxTitle', { - defaultMessage: 'Edit the configuration', - }), - textPre: i18n.translate('home.tutorials.common.metricbeatInstructions.config.osxTextPre', { - defaultMessage: 'Modify {path} to set the connection information:', - values: { - path: '`metricbeat.yml`', - }, - }), - commands: [ - 'output.elasticsearch:', - ' hosts: [""]', - ' username: "elastic"', - ' password: ""', - 'setup.kibana:', - ' host: ""', - getSpaceIdForBeatsTutorial(context), - ], - textPost: i18n.translate('home.tutorials.common.metricbeatInstructions.config.osxTextPost', { - defaultMessage: - 'Where {passwordTemplate} is the password of the `elastic` user, {esUrlTemplate} is the URL of Elasticsearch, \ -and {kibanaUrlTemplate} is the URL of Kibana.', - values: { - passwordTemplate: '``', - esUrlTemplate: '``', - kibanaUrlTemplate: '``', - }, - }), - }, - DEB: { - title: i18n.translate('home.tutorials.common.metricbeatInstructions.config.debTitle', { - defaultMessage: 'Edit the configuration', - }), - textPre: i18n.translate('home.tutorials.common.metricbeatInstructions.config.debTextPre', { - defaultMessage: 'Modify {path} to set the connection information:', - values: { - path: '`/etc/metricbeat/metricbeat.yml`', - }, - }), - commands: [ - 'output.elasticsearch:', - ' hosts: [""]', - ' username: "elastic"', - ' password: ""', - 'setup.kibana:', - ' host: ""', - getSpaceIdForBeatsTutorial(context), - ], - textPost: i18n.translate('home.tutorials.common.metricbeatInstructions.config.debTextPost', { - defaultMessage: - 'Where {passwordTemplate} is the password of the `elastic` user, {esUrlTemplate} is the URL of Elasticsearch, \ -and {kibanaUrlTemplate} is the URL of Kibana.', - values: { - passwordTemplate: '``', - esUrlTemplate: '``', - kibanaUrlTemplate: '``', - }, - }), + values: { + directoryName: '`metricbeat-{config.kibana.version}-windows`', + folderPath: '`C:\\Program Files`', + metricbeatLink: + '{config.docs.beats.metricbeat}/metricbeat-installation-configuration.html', + elasticLink: 'https://www.elastic.co/downloads/beats/metricbeat', + }, + } + ), + commands: ['cd "C:\\Program Files\\Metricbeat"', '.\\install-service-metricbeat.ps1'], + textPost: i18n.translate( + 'home.tutorials.common.metricbeatInstructions.install.windowsTextPost', + { + defaultMessage: + 'Modify the settings under `output.elasticsearch` in the {path} file to point to your Elasticsearch installation.', + values: { path: '`C:\\Program Files\\Metricbeat\\metricbeat.yml`' }, + } + ), + }, }, - RPM: { - title: i18n.translate('home.tutorials.common.metricbeatInstructions.config.rpmTitle', { - defaultMessage: 'Edit the configuration', - }), - textPre: i18n.translate('home.tutorials.common.metricbeatInstructions.config.rpmTextPre', { - defaultMessage: 'Modify {path} to set the connection information:', - values: { - path: '`/etc/metricbeat/metricbeat.yml`', - }, - }), - commands: [ - 'output.elasticsearch:', - ' hosts: [""]', - ' username: "elastic"', - ' password: ""', - 'setup.kibana:', - ' host: ""', - getSpaceIdForBeatsTutorial(context), - ], - textPost: i18n.translate('home.tutorials.common.metricbeatInstructions.config.rpmTextPost', { - defaultMessage: - 'Where {passwordTemplate} is the password of the `elastic` user, {esUrlTemplate} is the URL of Elasticsearch, \ -and {kibanaUrlTemplate} is the URL of Kibana.', - values: { - passwordTemplate: '``', - esUrlTemplate: '``', - kibanaUrlTemplate: '``', - }, - }), + START: { + OSX: { + title: i18n.translate('home.tutorials.common.metricbeatInstructions.start.osxTitle', { + defaultMessage: 'Start Metricbeat', + }), + textPre: i18n.translate('home.tutorials.common.metricbeatInstructions.start.osxTextPre', { + defaultMessage: + 'The `setup` command loads the Kibana dashboards. If the dashboards are already set up, omit this command.', + }), + commands: ['./metricbeat setup', './metricbeat -e'], + }, + DEB: { + title: i18n.translate('home.tutorials.common.metricbeatInstructions.start.debTitle', { + defaultMessage: 'Start Metricbeat', + }), + textPre: i18n.translate('home.tutorials.common.metricbeatInstructions.start.debTextPre', { + defaultMessage: + 'The `setup` command loads the Kibana dashboards. If the dashboards are already set up, omit this command.', + }), + commands: ['sudo metricbeat setup', 'sudo service metricbeat start'], + }, + RPM: { + title: i18n.translate('home.tutorials.common.metricbeatInstructions.start.rpmTitle', { + defaultMessage: 'Start Metricbeat', + }), + textPre: i18n.translate('home.tutorials.common.metricbeatInstructions.start.rpmTextPre', { + defaultMessage: + 'The `setup` command loads the Kibana dashboards. If the dashboards are already set up, omit this command.', + }), + commands: ['sudo metricbeat setup', 'sudo service metricbeat start'], + }, + WINDOWS: { + title: i18n.translate('home.tutorials.common.metricbeatInstructions.start.windowsTitle', { + defaultMessage: 'Start Metricbeat', + }), + textPre: i18n.translate( + 'home.tutorials.common.metricbeatInstructions.start.windowsTextPre', + { + defaultMessage: + 'The `setup` command loads the Kibana dashboards. If the dashboards are already set up, omit this command.', + } + ), + commands: ['.\\metricbeat.exe setup', 'Start-Service metricbeat'], + }, }, - WINDOWS: { - title: i18n.translate('home.tutorials.common.metricbeatInstructions.config.windowsTitle', { - defaultMessage: 'Edit the configuration', - }), - textPre: i18n.translate( - 'home.tutorials.common.metricbeatInstructions.config.windowsTextPre', - { + CONFIG: { + OSX: { + title: i18n.translate('home.tutorials.common.metricbeatInstructions.config.osxTitle', { + defaultMessage: 'Edit the configuration', + }), + textPre: i18n.translate('home.tutorials.common.metricbeatInstructions.config.osxTextPre', { defaultMessage: 'Modify {path} to set the connection information:', values: { - path: '`C:\\Program Files\\Metricbeat\\metricbeat.yml`', + path: '`metricbeat.yml`', }, - } - ), - commands: [ - 'output.elasticsearch:', - ' hosts: [""]', - ' username: "elastic"', - ' password: ""', - 'setup.kibana:', - ' host: ""', - getSpaceIdForBeatsTutorial(context), - ], - textPost: i18n.translate( - 'home.tutorials.common.metricbeatInstructions.config.windowsTextPost', - { - defaultMessage: - 'Where {passwordTemplate} is the password of the `elastic` user, {esUrlTemplate} is the URL of Elasticsearch, \ -and {kibanaUrlTemplate} is the URL of Kibana.', + }), + commands: [ + 'output.elasticsearch:', + ' hosts: [""]', + ' username: "elastic"', + ' password: ""', + " # If using Elasticsearch's default certificate", + ' ssl.ca_trusted_fingerprint: ""', + 'setup.kibana:', + ' host: ""', + getSpaceIdForBeatsTutorial(context), + ], + textPost: i18n.translate( + 'home.tutorials.common.metricbeatInstructions.config.osxTextPostMarkdown', + { + defaultMessage: + 'Where {passwordTemplate} is the password of the `elastic` user, {esUrlTemplate} is the URL of \ + Elasticsearch, and {kibanaUrlTemplate} is the URL of Kibana. To [configure SSL]({configureSslUrl}) with the \ + default certificate generated by Elasticsearch, add its fingerprint in {esCertFingerprintTemplate}.', + values: { + passwordTemplate: '``', + esUrlTemplate: '``', + kibanaUrlTemplate: '``', + configureSslUrl: SSL_DOC_URL, + esCertFingerprintTemplate: '``', + }, + } + ), + }, + DEB: { + title: i18n.translate('home.tutorials.common.metricbeatInstructions.config.debTitle', { + defaultMessage: 'Edit the configuration', + }), + textPre: i18n.translate('home.tutorials.common.metricbeatInstructions.config.debTextPre', { + defaultMessage: 'Modify {path} to set the connection information:', values: { - passwordTemplate: '``', - esUrlTemplate: '``', - kibanaUrlTemplate: '``', + path: '`/etc/metricbeat/metricbeat.yml`', }, - } - ), + }), + commands: [ + 'output.elasticsearch:', + ' hosts: [""]', + ' username: "elastic"', + ' password: ""', + " # If using Elasticsearch's default certificate", + ' ssl.ca_trusted_fingerprint: ""', + 'setup.kibana:', + ' host: ""', + getSpaceIdForBeatsTutorial(context), + ], + textPost: i18n.translate( + 'home.tutorials.common.metricbeatInstructions.config.debTextPostMarkdown', + { + defaultMessage: + 'Where {passwordTemplate} is the password of the `elastic` user, {esUrlTemplate} is the URL of \ + Elasticsearch, and {kibanaUrlTemplate} is the URL of Kibana. To [configure SSL]({configureSslUrl}) with the \ + default certificate generated by Elasticsearch, add its fingerprint in {esCertFingerprintTemplate}.', + values: { + passwordTemplate: '``', + esUrlTemplate: '``', + kibanaUrlTemplate: '``', + configureSslUrl: SSL_DOC_URL, + esCertFingerprintTemplate: '``', + }, + } + ), + }, + RPM: { + title: i18n.translate('home.tutorials.common.metricbeatInstructions.config.rpmTitle', { + defaultMessage: 'Edit the configuration', + }), + textPre: i18n.translate('home.tutorials.common.metricbeatInstructions.config.rpmTextPre', { + defaultMessage: 'Modify {path} to set the connection information:', + values: { + path: '`/etc/metricbeat/metricbeat.yml`', + }, + }), + commands: [ + 'output.elasticsearch:', + ' hosts: [""]', + ' username: "elastic"', + ' password: ""', + " # If using Elasticsearch's default certificate", + ' ssl.ca_trusted_fingerprint: ""', + 'setup.kibana:', + ' host: ""', + getSpaceIdForBeatsTutorial(context), + ], + textPost: i18n.translate( + 'home.tutorials.common.metricbeatInstructions.config.rpmTextPostMarkdown', + { + defaultMessage: + 'Where {passwordTemplate} is the password of the `elastic` user, {esUrlTemplate} is the URL of \ + Elasticsearch, and {kibanaUrlTemplate} is the URL of Kibana. To [configure SSL]({configureSslUrl}) with the \ + default certificate generated by Elasticsearch, add its fingerprint in {esCertFingerprintTemplate}.', + values: { + passwordTemplate: '``', + esUrlTemplate: '``', + kibanaUrlTemplate: '``', + configureSslUrl: SSL_DOC_URL, + esCertFingerprintTemplate: '``', + }, + } + ), + }, + WINDOWS: { + title: i18n.translate('home.tutorials.common.metricbeatInstructions.config.windowsTitle', { + defaultMessage: 'Edit the configuration', + }), + textPre: i18n.translate( + 'home.tutorials.common.metricbeatInstructions.config.windowsTextPre', + { + defaultMessage: 'Modify {path} to set the connection information:', + values: { + path: '`C:\\Program Files\\Metricbeat\\metricbeat.yml`', + }, + } + ), + commands: [ + 'output.elasticsearch:', + ' hosts: [""]', + ' username: "elastic"', + ' password: ""', + " # If using Elasticsearch's default certificate", + ' ssl.ca_trusted_fingerprint: ""', + 'setup.kibana:', + ' host: ""', + getSpaceIdForBeatsTutorial(context), + ], + textPost: i18n.translate( + 'home.tutorials.common.metricbeatInstructions.config.windowsTextPostMarkdown', + { + defaultMessage: + 'Where {passwordTemplate} is the password of the `elastic` user, {esUrlTemplate} is the URL of \ + Elasticsearch, and {kibanaUrlTemplate} is the URL of Kibana. To [configure SSL]({configureSslUrl}) with the \ + default certificate generated by Elasticsearch, add its fingerprint in {esCertFingerprintTemplate}.', + values: { + passwordTemplate: '``', + esUrlTemplate: '``', + kibanaUrlTemplate: '``', + configureSslUrl: SSL_DOC_URL, + esCertFingerprintTemplate: '``', + }, + } + ), + }, }, - }, -}); + }; +}; export const createMetricbeatCloudInstructions = () => ({ CONFIG: { @@ -442,7 +484,7 @@ export function metricbeatStatusCheck(moduleName: string) { }; } -export function onPremInstructions(moduleName: string, context?: TutorialContext) { +export function onPremInstructions(moduleName: string, context: TutorialContext) { const METRICBEAT_INSTRUCTIONS = createMetricbeatInstructions(context); return { @@ -498,10 +540,10 @@ export function onPremInstructions(moduleName: string, context?: TutorialContext }; } -export function onPremCloudInstructions(moduleName: string) { +export function onPremCloudInstructions(moduleName: string, context: TutorialContext) { const TRYCLOUD_OPTION1 = createTrycloudOption1(); const TRYCLOUD_OPTION2 = createTrycloudOption2(); - const METRICBEAT_INSTRUCTIONS = createMetricbeatInstructions(); + const METRICBEAT_INSTRUCTIONS = createMetricbeatInstructions(context); return { instructionSets: [ @@ -564,8 +606,8 @@ export function onPremCloudInstructions(moduleName: string) { }; } -export function cloudInstructions(moduleName: string) { - const METRICBEAT_INSTRUCTIONS = createMetricbeatInstructions(); +export function cloudInstructions(moduleName: string, context: TutorialContext) { + const METRICBEAT_INSTRUCTIONS = createMetricbeatInstructions(context); const METRICBEAT_CLOUD_INSTRUCTIONS = createMetricbeatCloudInstructions(); return { diff --git a/src/plugins/home/server/tutorials/instructions/winlogbeat_instructions.ts b/src/plugins/home/server/tutorials/instructions/winlogbeat_instructions.ts index 7e90795448a6c..2c33285899f65 100644 --- a/src/plugins/home/server/tutorials/instructions/winlogbeat_instructions.ts +++ b/src/plugins/home/server/tutorials/instructions/winlogbeat_instructions.ts @@ -13,94 +13,106 @@ import { getSpaceIdForBeatsTutorial } from './get_space_id_for_beats_tutorial'; import { TutorialContext } from '../../services/tutorials/lib/tutorials_registry_types'; import { cloudPasswordAndResetLink } from './cloud_instructions'; -export const createWinlogbeatInstructions = (context?: TutorialContext) => ({ - INSTALL: { - WINDOWS: { - title: i18n.translate('home.tutorials.common.winlogbeatInstructions.install.windowsTitle', { - defaultMessage: 'Download and install Winlogbeat', - }), - textPre: i18n.translate( - 'home.tutorials.common.winlogbeatInstructions.install.windowsTextPre', - { - defaultMessage: - 'First time using Winlogbeat? See the [Quick Start]({winlogbeatLink}).\n\ +export const createWinlogbeatInstructions = (context: TutorialContext) => { + const SSL_DOC_URL = `https://www.elastic.co/guide/en/beats/winlogbeat/${context.kibanaBranch}/configuration-ssl.html#ca-sha256`; + + return { + INSTALL: { + WINDOWS: { + title: i18n.translate('home.tutorials.common.winlogbeatInstructions.install.windowsTitle', { + defaultMessage: 'Download and install Winlogbeat', + }), + textPre: i18n.translate( + 'home.tutorials.common.winlogbeatInstructions.install.windowsTextPre', + { + defaultMessage: + 'First time using Winlogbeat? See the [Quick Start]({winlogbeatLink}).\n\ 1. Download the Winlogbeat Windows zip file from the [Download]({elasticLink}) page.\n\ 2. Extract the contents of the zip file into {folderPath}.\n\ 3. Rename the {directoryName} directory to `Winlogbeat`.\n\ 4. Open a PowerShell prompt as an Administrator (right-click the PowerShell icon and select \ **Run As Administrator**). If you are running Windows XP, you might need to download and install PowerShell.\n\ 5. From the PowerShell prompt, run the following commands to install Winlogbeat as a Windows service.', - values: { - directoryName: '`winlogbeat-{config.kibana.version}-windows`', - folderPath: '`C:\\Program Files`', - winlogbeatLink: - '{config.docs.beats.winlogbeat}/winlogbeat-installation-configuration.html', - elasticLink: 'https://www.elastic.co/downloads/beats/winlogbeat', - }, - } - ), - commands: ['cd "C:\\Program Files\\Winlogbeat"', '.\\install-service-winlogbeat.ps1'], - textPost: i18n.translate( - 'home.tutorials.common.winlogbeatInstructions.install.windowsTextPost', - { - defaultMessage: - 'Modify the settings under `output.elasticsearch` in the {path} file to point to your Elasticsearch installation.', - values: { path: '`C:\\Program Files\\Winlogbeat\\winlogbeat.yml`' }, - } - ), + values: { + directoryName: '`winlogbeat-{config.kibana.version}-windows`', + folderPath: '`C:\\Program Files`', + winlogbeatLink: + '{config.docs.beats.winlogbeat}/winlogbeat-installation-configuration.html', + elasticLink: 'https://www.elastic.co/downloads/beats/winlogbeat', + }, + } + ), + commands: ['cd "C:\\Program Files\\Winlogbeat"', '.\\install-service-winlogbeat.ps1'], + textPost: i18n.translate( + 'home.tutorials.common.winlogbeatInstructions.install.windowsTextPost', + { + defaultMessage: + 'Modify the settings under `output.elasticsearch` in the {path} file to point to your Elasticsearch installation.', + values: { path: '`C:\\Program Files\\Winlogbeat\\winlogbeat.yml`' }, + } + ), + }, }, - }, - START: { - WINDOWS: { - title: i18n.translate('home.tutorials.common.winlogbeatInstructions.start.windowsTitle', { - defaultMessage: 'Start Winlogbeat', - }), - textPre: i18n.translate('home.tutorials.common.winlogbeatInstructions.start.windowsTextPre', { - defaultMessage: - 'The `setup` command loads the Kibana dashboards. If the dashboards are already set up, omit this command.', - }), - commands: ['.\\winlogbeat.exe setup', 'Start-Service winlogbeat'], + START: { + WINDOWS: { + title: i18n.translate('home.tutorials.common.winlogbeatInstructions.start.windowsTitle', { + defaultMessage: 'Start Winlogbeat', + }), + textPre: i18n.translate( + 'home.tutorials.common.winlogbeatInstructions.start.windowsTextPre', + { + defaultMessage: + 'The `setup` command loads the Kibana dashboards. If the dashboards are already set up, omit this command.', + } + ), + commands: ['.\\winlogbeat.exe setup', 'Start-Service winlogbeat'], + }, }, - }, - CONFIG: { - WINDOWS: { - title: i18n.translate('home.tutorials.common.winlogbeatInstructions.config.windowsTitle', { - defaultMessage: 'Edit the configuration', - }), - textPre: i18n.translate( - 'home.tutorials.common.winlogbeatInstructions.config.windowsTextPre', - { - defaultMessage: 'Modify {path} to set the connection information:', - values: { - path: '`C:\\Program Files\\Winlogbeat\\winlogbeat.yml`', - }, - } - ), - commands: [ - 'output.elasticsearch:', - ' hosts: [""]', - ' username: "elastic"', - ' password: ""', - 'setup.kibana:', - ' host: ""', - getSpaceIdForBeatsTutorial(context), - ], - textPost: i18n.translate( - 'home.tutorials.common.winlogbeatInstructions.config.windowsTextPost', - { - defaultMessage: - 'Where {passwordTemplate} is the password of the `elastic` user, {esUrlTemplate} is the URL of Elasticsearch, \ -and {kibanaUrlTemplate} is the URL of Kibana.', - values: { - passwordTemplate: '``', - esUrlTemplate: '``', - kibanaUrlTemplate: '``', - }, - } - ), + CONFIG: { + WINDOWS: { + title: i18n.translate('home.tutorials.common.winlogbeatInstructions.config.windowsTitle', { + defaultMessage: 'Edit the configuration', + }), + textPre: i18n.translate( + 'home.tutorials.common.winlogbeatInstructions.config.windowsTextPre', + { + defaultMessage: 'Modify {path} to set the connection information:', + values: { + path: '`C:\\Program Files\\Winlogbeat\\winlogbeat.yml`', + }, + } + ), + commands: [ + 'output.elasticsearch:', + ' hosts: [""]', + ' username: "elastic"', + ' password: ""', + " # If using Elasticsearch's default certificate", + ' ssl.ca_trusted_fingerprint: ""', + 'setup.kibana:', + ' host: ""', + getSpaceIdForBeatsTutorial(context), + ], + textPost: i18n.translate( + 'home.tutorials.common.winlogbeatInstructions.config.windowsTextPostMarkdown', + { + defaultMessage: + 'Where {passwordTemplate} is the password of the `elastic` user, {esUrlTemplate} is the URL of \ + Elasticsearch, and {kibanaUrlTemplate} is the URL of Kibana. To [configure SSL]({configureSslUrl}) with the \ + default certificate generated by Elasticsearch, add its fingerprint in {esCertFingerprintTemplate}.', + values: { + passwordTemplate: '``', + esUrlTemplate: '``', + kibanaUrlTemplate: '``', + configureSslUrl: SSL_DOC_URL, + esCertFingerprintTemplate: '``', + }, + } + ), + }, }, - }, -}); + }; +}; export const createWinlogbeatCloudInstructions = () => ({ CONFIG: { @@ -158,7 +170,7 @@ export function winlogbeatStatusCheck() { }; } -export function onPremInstructions(context?: TutorialContext) { +export function onPremInstructions(context: TutorialContext) { const WINLOGBEAT_INSTRUCTIONS = createWinlogbeatInstructions(context); return { @@ -186,10 +198,10 @@ export function onPremInstructions(context?: TutorialContext) { }; } -export function onPremCloudInstructions() { +export function onPremCloudInstructions(context: TutorialContext) { const TRYCLOUD_OPTION1 = createTrycloudOption1(); const TRYCLOUD_OPTION2 = createTrycloudOption2(); - const WINLOGBEAT_INSTRUCTIONS = createWinlogbeatInstructions(); + const WINLOGBEAT_INSTRUCTIONS = createWinlogbeatInstructions(context); return { instructionSets: [ @@ -218,8 +230,8 @@ export function onPremCloudInstructions() { }; } -export function cloudInstructions() { - const WINLOGBEAT_INSTRUCTIONS = createWinlogbeatInstructions(); +export function cloudInstructions(context: TutorialContext) { + const WINLOGBEAT_INSTRUCTIONS = createWinlogbeatInstructions(context); const WINLOGBEAT_CLOUD_INSTRUCTIONS = createWinlogbeatCloudInstructions(); return { diff --git a/src/plugins/home/server/tutorials/iptables_logs/index.ts b/src/plugins/home/server/tutorials/iptables_logs/index.ts index 6d298e88a2dfb..f4469de3336cc 100644 --- a/src/plugins/home/server/tutorials/iptables_logs/index.ts +++ b/src/plugins/home/server/tutorials/iptables_logs/index.ts @@ -60,8 +60,8 @@ export function iptablesLogsSpecProvider(context: TutorialContext): TutorialSche completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/iptables_logs/screenshot.png', onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['network', 'security'], }; } diff --git a/src/plugins/home/server/tutorials/juniper_logs/index.ts b/src/plugins/home/server/tutorials/juniper_logs/index.ts index 7430e4705a5f4..a6d34d1e8447f 100644 --- a/src/plugins/home/server/tutorials/juniper_logs/index.ts +++ b/src/plugins/home/server/tutorials/juniper_logs/index.ts @@ -54,8 +54,8 @@ export function juniperLogsSpecProvider(context: TutorialContext): TutorialSchem }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['network', 'security'], }; } diff --git a/src/plugins/home/server/tutorials/kafka_logs/index.ts b/src/plugins/home/server/tutorials/kafka_logs/index.ts index 9ccc06eb222c7..6e377f3c1f295 100644 --- a/src/plugins/home/server/tutorials/kafka_logs/index.ts +++ b/src/plugins/home/server/tutorials/kafka_logs/index.ts @@ -57,8 +57,8 @@ export function kafkaLogsSpecProvider(context: TutorialContext): TutorialSchema completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/kafka_logs/screenshot.png', onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['message_queue'], }; } diff --git a/src/plugins/home/server/tutorials/kafka_metrics/index.ts b/src/plugins/home/server/tutorials/kafka_metrics/index.ts index 973ec06b58fdf..5e6250989d0ab 100644 --- a/src/plugins/home/server/tutorials/kafka_metrics/index.ts +++ b/src/plugins/home/server/tutorials/kafka_metrics/index.ts @@ -54,8 +54,8 @@ export function kafkaMetricsSpecProvider(context: TutorialContext): TutorialSche }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['message_queue'], }; } diff --git a/src/plugins/home/server/tutorials/kibana_logs/index.ts b/src/plugins/home/server/tutorials/kibana_logs/index.ts index 9863a53700a55..969e4972875f4 100644 --- a/src/plugins/home/server/tutorials/kibana_logs/index.ts +++ b/src/plugins/home/server/tutorials/kibana_logs/index.ts @@ -53,8 +53,8 @@ export function kibanaLogsSpecProvider(context: TutorialContext): TutorialSchema }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['message_queue'], }; } diff --git a/src/plugins/home/server/tutorials/kibana_metrics/index.ts b/src/plugins/home/server/tutorials/kibana_metrics/index.ts index 3d0eb691ede51..ff8ec0eb6e43c 100644 --- a/src/plugins/home/server/tutorials/kibana_metrics/index.ts +++ b/src/plugins/home/server/tutorials/kibana_metrics/index.ts @@ -54,8 +54,8 @@ export function kibanaMetricsSpecProvider(context: TutorialContext): TutorialSch }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['message_queue'], }; } diff --git a/src/plugins/home/server/tutorials/kubernetes_metrics/index.ts b/src/plugins/home/server/tutorials/kubernetes_metrics/index.ts index 9c66125ee0cfe..acd65e0bdc69d 100644 --- a/src/plugins/home/server/tutorials/kubernetes_metrics/index.ts +++ b/src/plugins/home/server/tutorials/kubernetes_metrics/index.ts @@ -59,8 +59,8 @@ export function kubernetesMetricsSpecProvider(context: TutorialContext): Tutoria completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/kubernetes_metrics/screenshot.png', onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['containers', 'kubernetes'], }; } diff --git a/src/plugins/home/server/tutorials/logstash_logs/index.ts b/src/plugins/home/server/tutorials/logstash_logs/index.ts index 688ad8245b78d..5978241d7e669 100644 --- a/src/plugins/home/server/tutorials/logstash_logs/index.ts +++ b/src/plugins/home/server/tutorials/logstash_logs/index.ts @@ -56,8 +56,8 @@ export function logstashLogsSpecProvider(context: TutorialContext): TutorialSche }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['custom'], }; } diff --git a/src/plugins/home/server/tutorials/logstash_metrics/index.ts b/src/plugins/home/server/tutorials/logstash_metrics/index.ts index 9ae4bcdcecbf1..d8d7db1b464b1 100644 --- a/src/plugins/home/server/tutorials/logstash_metrics/index.ts +++ b/src/plugins/home/server/tutorials/logstash_metrics/index.ts @@ -55,8 +55,8 @@ export function logstashMetricsSpecProvider(context: TutorialContext): TutorialS }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['custom'], }; } diff --git a/src/plugins/home/server/tutorials/memcached_metrics/index.ts b/src/plugins/home/server/tutorials/memcached_metrics/index.ts index 891567f72ca7c..a48db78e89d88 100644 --- a/src/plugins/home/server/tutorials/memcached_metrics/index.ts +++ b/src/plugins/home/server/tutorials/memcached_metrics/index.ts @@ -54,8 +54,8 @@ export function memcachedMetricsSpecProvider(context: TutorialContext): Tutorial }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['custom'], }; } diff --git a/src/plugins/home/server/tutorials/microsoft_logs/index.ts b/src/plugins/home/server/tutorials/microsoft_logs/index.ts index 88893e22bc9ff..39400f4661071 100644 --- a/src/plugins/home/server/tutorials/microsoft_logs/index.ts +++ b/src/plugins/home/server/tutorials/microsoft_logs/index.ts @@ -57,8 +57,8 @@ export function microsoftLogsSpecProvider(context: TutorialContext): TutorialSch completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/microsoft_logs/screenshot.png', onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['network', 'security', 'azure'], }; } diff --git a/src/plugins/home/server/tutorials/misp_logs/index.ts b/src/plugins/home/server/tutorials/misp_logs/index.ts index ea2147a296534..4fb70aa1018f7 100644 --- a/src/plugins/home/server/tutorials/misp_logs/index.ts +++ b/src/plugins/home/server/tutorials/misp_logs/index.ts @@ -57,8 +57,8 @@ export function mispLogsSpecProvider(context: TutorialContext): TutorialSchema { completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/misp_logs/screenshot.png', onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['network', 'security', 'azure'], }; } diff --git a/src/plugins/home/server/tutorials/mongodb_logs/index.ts b/src/plugins/home/server/tutorials/mongodb_logs/index.ts index a7f9869d440ed..28e323a2b15a9 100644 --- a/src/plugins/home/server/tutorials/mongodb_logs/index.ts +++ b/src/plugins/home/server/tutorials/mongodb_logs/index.ts @@ -57,8 +57,8 @@ export function mongodbLogsSpecProvider(context: TutorialContext): TutorialSchem completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/mongodb_logs/screenshot.png', onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['datastore'], }; } diff --git a/src/plugins/home/server/tutorials/mongodb_metrics/index.ts b/src/plugins/home/server/tutorials/mongodb_metrics/index.ts index cc0ecc0574fa9..db843d09abfd8 100644 --- a/src/plugins/home/server/tutorials/mongodb_metrics/index.ts +++ b/src/plugins/home/server/tutorials/mongodb_metrics/index.ts @@ -59,8 +59,8 @@ export function mongodbMetricsSpecProvider(context: TutorialContext): TutorialSc completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/mongodb_metrics/screenshot.png', onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['datastore'], }; } diff --git a/src/plugins/home/server/tutorials/mssql_logs/index.ts b/src/plugins/home/server/tutorials/mssql_logs/index.ts index 06cafd95283c8..5e19a2204b22c 100644 --- a/src/plugins/home/server/tutorials/mssql_logs/index.ts +++ b/src/plugins/home/server/tutorials/mssql_logs/index.ts @@ -54,8 +54,8 @@ export function mssqlLogsSpecProvider(context: TutorialContext): TutorialSchema }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['datastore'], }; } diff --git a/src/plugins/home/server/tutorials/mssql_metrics/index.ts b/src/plugins/home/server/tutorials/mssql_metrics/index.ts index e3c9e3c338209..3e73714784f0f 100644 --- a/src/plugins/home/server/tutorials/mssql_metrics/index.ts +++ b/src/plugins/home/server/tutorials/mssql_metrics/index.ts @@ -57,8 +57,8 @@ export function mssqlMetricsSpecProvider(context: TutorialContext): TutorialSche completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/mssql_metrics/screenshot.png', onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['datastore'], }; } diff --git a/src/plugins/home/server/tutorials/munin_metrics/index.ts b/src/plugins/home/server/tutorials/munin_metrics/index.ts index 12621d05d0766..963e9b63e9ba8 100644 --- a/src/plugins/home/server/tutorials/munin_metrics/index.ts +++ b/src/plugins/home/server/tutorials/munin_metrics/index.ts @@ -54,8 +54,8 @@ export function muninMetricsSpecProvider(context: TutorialContext): TutorialSche }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['datastore'], }; } diff --git a/src/plugins/home/server/tutorials/mysql_logs/index.ts b/src/plugins/home/server/tutorials/mysql_logs/index.ts index b0c6f0e69dcfb..9af0a3d078cab 100644 --- a/src/plugins/home/server/tutorials/mysql_logs/index.ts +++ b/src/plugins/home/server/tutorials/mysql_logs/index.ts @@ -57,8 +57,8 @@ export function mysqlLogsSpecProvider(context: TutorialContext): TutorialSchema completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/mysql_logs/screenshot.png', onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['datastore'], }; } diff --git a/src/plugins/home/server/tutorials/mysql_metrics/index.ts b/src/plugins/home/server/tutorials/mysql_metrics/index.ts index 09c55dc81ff84..8339561d060d6 100644 --- a/src/plugins/home/server/tutorials/mysql_metrics/index.ts +++ b/src/plugins/home/server/tutorials/mysql_metrics/index.ts @@ -56,8 +56,8 @@ export function mysqlMetricsSpecProvider(context: TutorialContext): TutorialSche completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/mysql_metrics/screenshot.png', onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['datastore'], }; } diff --git a/src/plugins/home/server/tutorials/nats_logs/index.ts b/src/plugins/home/server/tutorials/nats_logs/index.ts index b6ef0a192d92f..971f0c2849bda 100644 --- a/src/plugins/home/server/tutorials/nats_logs/index.ts +++ b/src/plugins/home/server/tutorials/nats_logs/index.ts @@ -58,8 +58,8 @@ export function natsLogsSpecProvider(context: TutorialContext): TutorialSchema { completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/nats_logs/screenshot.png', onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['message_queue'], }; } diff --git a/src/plugins/home/server/tutorials/nats_metrics/index.ts b/src/plugins/home/server/tutorials/nats_metrics/index.ts index 54f034ad44b19..cdd633d88140c 100644 --- a/src/plugins/home/server/tutorials/nats_metrics/index.ts +++ b/src/plugins/home/server/tutorials/nats_metrics/index.ts @@ -56,8 +56,8 @@ export function natsMetricsSpecProvider(context: TutorialContext): TutorialSchem completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/nats_metrics/screenshot.png', onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['message_queue'], }; } diff --git a/src/plugins/home/server/tutorials/netflow_logs/index.ts b/src/plugins/home/server/tutorials/netflow_logs/index.ts index c659d9c1d31b1..7a81159503468 100644 --- a/src/plugins/home/server/tutorials/netflow_logs/index.ts +++ b/src/plugins/home/server/tutorials/netflow_logs/index.ts @@ -56,8 +56,8 @@ export function netflowLogsSpecProvider(context: TutorialContext): TutorialSchem }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['network', 'security'], }; } diff --git a/src/plugins/home/server/tutorials/netscout_logs/index.ts b/src/plugins/home/server/tutorials/netscout_logs/index.ts index e6c22947f8057..2b1a469a9bbb7 100644 --- a/src/plugins/home/server/tutorials/netscout_logs/index.ts +++ b/src/plugins/home/server/tutorials/netscout_logs/index.ts @@ -54,8 +54,8 @@ export function netscoutLogsSpecProvider(context: TutorialContext): TutorialSche }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['security'], }; } diff --git a/src/plugins/home/server/tutorials/nginx_logs/index.ts b/src/plugins/home/server/tutorials/nginx_logs/index.ts index e6f2fc4efb01c..3797f2496ee17 100644 --- a/src/plugins/home/server/tutorials/nginx_logs/index.ts +++ b/src/plugins/home/server/tutorials/nginx_logs/index.ts @@ -57,8 +57,8 @@ export function nginxLogsSpecProvider(context: TutorialContext): TutorialSchema completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/nginx_logs/screenshot.png', onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['web', 'security'], }; } diff --git a/src/plugins/home/server/tutorials/nginx_metrics/index.ts b/src/plugins/home/server/tutorials/nginx_metrics/index.ts index 680dd664912d3..f32e9388c1f5b 100644 --- a/src/plugins/home/server/tutorials/nginx_metrics/index.ts +++ b/src/plugins/home/server/tutorials/nginx_metrics/index.ts @@ -61,8 +61,8 @@ which must be enabled in your Nginx installation. \ completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/nginx_metrics/screenshot.png', onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['web', 'security'], }; } diff --git a/src/plugins/home/server/tutorials/o365_logs/index.ts b/src/plugins/home/server/tutorials/o365_logs/index.ts index 3cd4d3a5c5e18..cbdabc7223b32 100644 --- a/src/plugins/home/server/tutorials/o365_logs/index.ts +++ b/src/plugins/home/server/tutorials/o365_logs/index.ts @@ -60,8 +60,8 @@ export function o365LogsSpecProvider(context: TutorialContext): TutorialSchema { completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/o365_logs/screenshot.png', onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['security'], }; } diff --git a/src/plugins/home/server/tutorials/okta_logs/index.ts b/src/plugins/home/server/tutorials/okta_logs/index.ts index aad18409de329..f45ffbfb800b5 100644 --- a/src/plugins/home/server/tutorials/okta_logs/index.ts +++ b/src/plugins/home/server/tutorials/okta_logs/index.ts @@ -58,8 +58,8 @@ export function oktaLogsSpecProvider(context: TutorialContext): TutorialSchema { completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/okta_logs/screenshot.png', onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['security'], }; } diff --git a/src/plugins/home/server/tutorials/openmetrics_metrics/index.ts b/src/plugins/home/server/tutorials/openmetrics_metrics/index.ts index 02625b341549b..d2611fb77895e 100644 --- a/src/plugins/home/server/tutorials/openmetrics_metrics/index.ts +++ b/src/plugins/home/server/tutorials/openmetrics_metrics/index.ts @@ -48,8 +48,8 @@ export function openmetricsMetricsSpecProvider(context: TutorialContext): Tutori }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['security'], }; } diff --git a/src/plugins/home/server/tutorials/oracle_metrics/index.ts b/src/plugins/home/server/tutorials/oracle_metrics/index.ts index 14cf5392c5231..263f2f5ab184b 100644 --- a/src/plugins/home/server/tutorials/oracle_metrics/index.ts +++ b/src/plugins/home/server/tutorials/oracle_metrics/index.ts @@ -55,8 +55,8 @@ export function oracleMetricsSpecProvider(context: TutorialContext): TutorialSch }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['security'], }; } diff --git a/src/plugins/home/server/tutorials/osquery_logs/index.ts b/src/plugins/home/server/tutorials/osquery_logs/index.ts index 4f87fc4e256e1..1c77222ce43a0 100644 --- a/src/plugins/home/server/tutorials/osquery_logs/index.ts +++ b/src/plugins/home/server/tutorials/osquery_logs/index.ts @@ -60,8 +60,8 @@ export function osqueryLogsSpecProvider(context: TutorialContext): TutorialSchem }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['security', 'os_system'], }; } diff --git a/src/plugins/home/server/tutorials/panw_logs/index.ts b/src/plugins/home/server/tutorials/panw_logs/index.ts index f5158c48f30d5..4b44038c07ade 100644 --- a/src/plugins/home/server/tutorials/panw_logs/index.ts +++ b/src/plugins/home/server/tutorials/panw_logs/index.ts @@ -60,8 +60,8 @@ export function panwLogsSpecProvider(context: TutorialContext): TutorialSchema { completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/panw_logs/screenshot.png', onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['security'], }; } diff --git a/src/plugins/home/server/tutorials/php_fpm_metrics/index.ts b/src/plugins/home/server/tutorials/php_fpm_metrics/index.ts index 40b35984fb17a..0a033e6378729 100644 --- a/src/plugins/home/server/tutorials/php_fpm_metrics/index.ts +++ b/src/plugins/home/server/tutorials/php_fpm_metrics/index.ts @@ -54,8 +54,8 @@ export function phpfpmMetricsSpecProvider(context: TutorialContext): TutorialSch }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['security'], }; } diff --git a/src/plugins/home/server/tutorials/postgresql_logs/index.ts b/src/plugins/home/server/tutorials/postgresql_logs/index.ts index 3a092e61b0bd9..a628f422dfb72 100644 --- a/src/plugins/home/server/tutorials/postgresql_logs/index.ts +++ b/src/plugins/home/server/tutorials/postgresql_logs/index.ts @@ -60,8 +60,8 @@ export function postgresqlLogsSpecProvider(context: TutorialContext): TutorialSc completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/postgresql_logs/screenshot.png', onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['datastore'], }; } diff --git a/src/plugins/home/server/tutorials/postgresql_metrics/index.ts b/src/plugins/home/server/tutorials/postgresql_metrics/index.ts index 501ea252cd16f..0ef48c33a7475 100644 --- a/src/plugins/home/server/tutorials/postgresql_metrics/index.ts +++ b/src/plugins/home/server/tutorials/postgresql_metrics/index.ts @@ -56,8 +56,8 @@ export function postgresqlMetricsSpecProvider(context: TutorialContext): Tutoria }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['datastore'], }; } diff --git a/src/plugins/home/server/tutorials/prometheus_metrics/index.ts b/src/plugins/home/server/tutorials/prometheus_metrics/index.ts index 2f422e5e3be70..92a08bcce0ca4 100644 --- a/src/plugins/home/server/tutorials/prometheus_metrics/index.ts +++ b/src/plugins/home/server/tutorials/prometheus_metrics/index.ts @@ -55,8 +55,8 @@ export function prometheusMetricsSpecProvider(context: TutorialContext): Tutoria }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['monitoring', 'datastore'], }; } diff --git a/src/plugins/home/server/tutorials/rabbitmq_logs/index.ts b/src/plugins/home/server/tutorials/rabbitmq_logs/index.ts index 8a1634e7da038..be6576de45a98 100644 --- a/src/plugins/home/server/tutorials/rabbitmq_logs/index.ts +++ b/src/plugins/home/server/tutorials/rabbitmq_logs/index.ts @@ -54,8 +54,8 @@ export function rabbitmqLogsSpecProvider(context: TutorialContext): TutorialSche }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['message_queue'], }; } diff --git a/src/plugins/home/server/tutorials/rabbitmq_metrics/index.ts b/src/plugins/home/server/tutorials/rabbitmq_metrics/index.ts index abfc895088d91..4487a187fa373 100644 --- a/src/plugins/home/server/tutorials/rabbitmq_metrics/index.ts +++ b/src/plugins/home/server/tutorials/rabbitmq_metrics/index.ts @@ -60,8 +60,8 @@ export function rabbitmqMetricsSpecProvider(context: TutorialContext): TutorialS completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/rabbitmq_metrics/screenshot.png', onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['message_queue'], }; } diff --git a/src/plugins/home/server/tutorials/radware_logs/index.ts b/src/plugins/home/server/tutorials/radware_logs/index.ts index 3e918a0a4064c..4abd897c0aff3 100644 --- a/src/plugins/home/server/tutorials/radware_logs/index.ts +++ b/src/plugins/home/server/tutorials/radware_logs/index.ts @@ -54,8 +54,8 @@ export function radwareLogsSpecProvider(context: TutorialContext): TutorialSchem }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['security'], }; } diff --git a/src/plugins/home/server/tutorials/redis_logs/index.ts b/src/plugins/home/server/tutorials/redis_logs/index.ts index f6aada27dec48..bb5d902d089e2 100644 --- a/src/plugins/home/server/tutorials/redis_logs/index.ts +++ b/src/plugins/home/server/tutorials/redis_logs/index.ts @@ -63,8 +63,8 @@ Note that the `slowlog` fileset is experimental. \ completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/redis_logs/screenshot.png', onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['datastore', 'message_queue'], }; } diff --git a/src/plugins/home/server/tutorials/redis_metrics/index.ts b/src/plugins/home/server/tutorials/redis_metrics/index.ts index 2bb300c48ff65..d2e8ed1efb779 100644 --- a/src/plugins/home/server/tutorials/redis_metrics/index.ts +++ b/src/plugins/home/server/tutorials/redis_metrics/index.ts @@ -56,8 +56,8 @@ export function redisMetricsSpecProvider(context: TutorialContext): TutorialSche completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/redis_metrics/screenshot.png', onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['datastore', 'message_queue'], }; } diff --git a/src/plugins/home/server/tutorials/redisenterprise_metrics/index.ts b/src/plugins/home/server/tutorials/redisenterprise_metrics/index.ts index 62e1386f29dbb..85d6dce9adc52 100644 --- a/src/plugins/home/server/tutorials/redisenterprise_metrics/index.ts +++ b/src/plugins/home/server/tutorials/redisenterprise_metrics/index.ts @@ -55,8 +55,8 @@ export function redisenterpriseMetricsSpecProvider(context: TutorialContext): Tu completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/redisenterprise_metrics/screenshot.png', onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['datastore', 'message_queue'], }; } diff --git a/src/plugins/home/server/tutorials/santa_logs/index.ts b/src/plugins/home/server/tutorials/santa_logs/index.ts index da9f2e940066e..65a7bb0bd26cb 100644 --- a/src/plugins/home/server/tutorials/santa_logs/index.ts +++ b/src/plugins/home/server/tutorials/santa_logs/index.ts @@ -58,8 +58,8 @@ export function santaLogsSpecProvider(context: TutorialContext): TutorialSchema completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/santa_logs/screenshot.png', onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['security', 'os_system'], }; } diff --git a/src/plugins/home/server/tutorials/sonicwall_logs/index.ts b/src/plugins/home/server/tutorials/sonicwall_logs/index.ts index 04bf7a3968320..40eb324014b15 100644 --- a/src/plugins/home/server/tutorials/sonicwall_logs/index.ts +++ b/src/plugins/home/server/tutorials/sonicwall_logs/index.ts @@ -54,8 +54,8 @@ export function sonicwallLogsSpecProvider(context: TutorialContext): TutorialSch }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['network', 'security'], }; } diff --git a/src/plugins/home/server/tutorials/sophos_logs/index.ts b/src/plugins/home/server/tutorials/sophos_logs/index.ts index 4fadcecb6e1bd..c6d6f7318b6ed 100644 --- a/src/plugins/home/server/tutorials/sophos_logs/index.ts +++ b/src/plugins/home/server/tutorials/sophos_logs/index.ts @@ -54,8 +54,8 @@ export function sophosLogsSpecProvider(context: TutorialContext): TutorialSchema }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['security'], }; } diff --git a/src/plugins/home/server/tutorials/squid_logs/index.ts b/src/plugins/home/server/tutorials/squid_logs/index.ts index 2d8f055d7fa6b..f325dbbd650ca 100644 --- a/src/plugins/home/server/tutorials/squid_logs/index.ts +++ b/src/plugins/home/server/tutorials/squid_logs/index.ts @@ -54,8 +54,8 @@ export function squidLogsSpecProvider(context: TutorialContext): TutorialSchema }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['security'], }; } diff --git a/src/plugins/home/server/tutorials/stan_metrics/index.ts b/src/plugins/home/server/tutorials/stan_metrics/index.ts index 0b3c0352b663d..50f2b9dbd2e87 100644 --- a/src/plugins/home/server/tutorials/stan_metrics/index.ts +++ b/src/plugins/home/server/tutorials/stan_metrics/index.ts @@ -56,8 +56,8 @@ export function stanMetricsSpecProvider(context: TutorialContext): TutorialSchem completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/stan_metrics/screenshot.png', onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['message_queue', 'kubernetes'], }; } diff --git a/src/plugins/home/server/tutorials/statsd_metrics/index.ts b/src/plugins/home/server/tutorials/statsd_metrics/index.ts index 1be010a01d5a6..c6ea0cf7ee879 100644 --- a/src/plugins/home/server/tutorials/statsd_metrics/index.ts +++ b/src/plugins/home/server/tutorials/statsd_metrics/index.ts @@ -45,8 +45,8 @@ export function statsdMetricsSpecProvider(context: TutorialContext): TutorialSch completionTimeMinutes: 10, // previewImagePath: '', onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['message_queue', 'kubernetes'], }; } diff --git a/src/plugins/home/server/tutorials/suricata_logs/index.ts b/src/plugins/home/server/tutorials/suricata_logs/index.ts index 373522e333379..a511be4a7a968 100644 --- a/src/plugins/home/server/tutorials/suricata_logs/index.ts +++ b/src/plugins/home/server/tutorials/suricata_logs/index.ts @@ -58,8 +58,8 @@ export function suricataLogsSpecProvider(context: TutorialContext): TutorialSche completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/suricata_logs/screenshot.png', onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['network', 'security'], }; } diff --git a/src/plugins/home/server/tutorials/system_logs/index.ts b/src/plugins/home/server/tutorials/system_logs/index.ts index fcc5745f48252..1de6d9df10ffb 100644 --- a/src/plugins/home/server/tutorials/system_logs/index.ts +++ b/src/plugins/home/server/tutorials/system_logs/index.ts @@ -56,8 +56,8 @@ export function systemLogsSpecProvider(context: TutorialContext): TutorialSchema }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['os_system', 'security'], }; } diff --git a/src/plugins/home/server/tutorials/system_metrics/index.ts b/src/plugins/home/server/tutorials/system_metrics/index.ts index 1348535d9bb72..10a6c741721b8 100644 --- a/src/plugins/home/server/tutorials/system_metrics/index.ts +++ b/src/plugins/home/server/tutorials/system_metrics/index.ts @@ -58,8 +58,8 @@ It collects system wide statistics and statistics per process and filesystem. \ completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/system_metrics/screenshot.png', onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['os_system', 'security'], }; } diff --git a/src/plugins/home/server/tutorials/tomcat_logs/index.ts b/src/plugins/home/server/tutorials/tomcat_logs/index.ts index 3258d3eff5a16..2f24354742771 100644 --- a/src/plugins/home/server/tutorials/tomcat_logs/index.ts +++ b/src/plugins/home/server/tutorials/tomcat_logs/index.ts @@ -54,8 +54,8 @@ export function tomcatLogsSpecProvider(context: TutorialContext): TutorialSchema }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['web', 'security'], }; } diff --git a/src/plugins/home/server/tutorials/traefik_logs/index.ts b/src/plugins/home/server/tutorials/traefik_logs/index.ts index 30b9db4022137..7411e396a5655 100644 --- a/src/plugins/home/server/tutorials/traefik_logs/index.ts +++ b/src/plugins/home/server/tutorials/traefik_logs/index.ts @@ -56,8 +56,8 @@ export function traefikLogsSpecProvider(context: TutorialContext): TutorialSchem }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['web', 'security'], }; } diff --git a/src/plugins/home/server/tutorials/traefik_metrics/index.ts b/src/plugins/home/server/tutorials/traefik_metrics/index.ts index 6f76be3056110..6e1d8d621e62e 100644 --- a/src/plugins/home/server/tutorials/traefik_metrics/index.ts +++ b/src/plugins/home/server/tutorials/traefik_metrics/index.ts @@ -44,8 +44,8 @@ export function traefikMetricsSpecProvider(context: TutorialContext): TutorialSc }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['web', 'security'], }; } diff --git a/src/plugins/home/server/tutorials/uptime_monitors/index.ts b/src/plugins/home/server/tutorials/uptime_monitors/index.ts index 118174d0e5717..9015cb4783163 100644 --- a/src/plugins/home/server/tutorials/uptime_monitors/index.ts +++ b/src/plugins/home/server/tutorials/uptime_monitors/index.ts @@ -55,8 +55,8 @@ export function uptimeMonitorsSpecProvider(context: TutorialContext): TutorialSc completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/uptime_monitors/screenshot.png', onPrem: onPremInstructions([], context), - elasticCloud: cloudInstructions(), - onPremElasticCloud: onPremCloudInstructions(), + elasticCloud: cloudInstructions(context), + onPremElasticCloud: onPremCloudInstructions(context), integrationBrowserCategories: ['web', 'security'], }; } diff --git a/src/plugins/home/server/tutorials/uwsgi_metrics/index.ts b/src/plugins/home/server/tutorials/uwsgi_metrics/index.ts index b1dbeb89bdb26..bb288ba72ab02 100644 --- a/src/plugins/home/server/tutorials/uwsgi_metrics/index.ts +++ b/src/plugins/home/server/tutorials/uwsgi_metrics/index.ts @@ -57,8 +57,8 @@ export function uwsgiMetricsSpecProvider(context: TutorialContext): TutorialSche completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/uwsgi_metrics/screenshot.png', onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['web', 'security'], }; } diff --git a/src/plugins/home/server/tutorials/vsphere_metrics/index.ts b/src/plugins/home/server/tutorials/vsphere_metrics/index.ts index 14a574872221a..0070be6622294 100644 --- a/src/plugins/home/server/tutorials/vsphere_metrics/index.ts +++ b/src/plugins/home/server/tutorials/vsphere_metrics/index.ts @@ -54,8 +54,8 @@ export function vSphereMetricsSpecProvider(context: TutorialContext): TutorialSc }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['web', 'security'], }; } diff --git a/src/plugins/home/server/tutorials/windows_event_logs/index.ts b/src/plugins/home/server/tutorials/windows_event_logs/index.ts index 008468487ea64..baab0f4c95080 100644 --- a/src/plugins/home/server/tutorials/windows_event_logs/index.ts +++ b/src/plugins/home/server/tutorials/windows_event_logs/index.ts @@ -54,8 +54,8 @@ export function windowsEventLogsSpecProvider(context: TutorialContext): Tutorial }, completionTimeMinutes: 10, onPrem: onPremInstructions(context), - elasticCloud: cloudInstructions(), - onPremElasticCloud: onPremCloudInstructions(), + elasticCloud: cloudInstructions(context), + onPremElasticCloud: onPremCloudInstructions(context), integrationBrowserCategories: ['os_system', 'security'], }; } diff --git a/src/plugins/home/server/tutorials/windows_metrics/index.ts b/src/plugins/home/server/tutorials/windows_metrics/index.ts index 31d9b3f8962ce..ebd5e6864a229 100644 --- a/src/plugins/home/server/tutorials/windows_metrics/index.ts +++ b/src/plugins/home/server/tutorials/windows_metrics/index.ts @@ -54,8 +54,8 @@ export function windowsMetricsSpecProvider(context: TutorialContext): TutorialSc }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['os_system', 'security'], }; } diff --git a/src/plugins/home/server/tutorials/zeek_logs/index.ts b/src/plugins/home/server/tutorials/zeek_logs/index.ts index df86518978c52..3eded8336df74 100644 --- a/src/plugins/home/server/tutorials/zeek_logs/index.ts +++ b/src/plugins/home/server/tutorials/zeek_logs/index.ts @@ -58,8 +58,8 @@ export function zeekLogsSpecProvider(context: TutorialContext): TutorialSchema { completionTimeMinutes: 10, previewImagePath: '/plugins/home/assets/zeek_logs/screenshot.png', onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['network', 'monitoring', 'security'], }; } diff --git a/src/plugins/home/server/tutorials/zookeeper_metrics/index.ts b/src/plugins/home/server/tutorials/zookeeper_metrics/index.ts index 8f732969a07f3..4e4206bc1ca29 100644 --- a/src/plugins/home/server/tutorials/zookeeper_metrics/index.ts +++ b/src/plugins/home/server/tutorials/zookeeper_metrics/index.ts @@ -55,8 +55,8 @@ export function zookeeperMetricsSpecProvider(context: TutorialContext): Tutorial }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, context), - elasticCloud: cloudInstructions(moduleName), - onPremElasticCloud: onPremCloudInstructions(moduleName), + elasticCloud: cloudInstructions(moduleName, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, context), integrationBrowserCategories: ['datastore', 'config_management'], }; } diff --git a/src/plugins/home/server/tutorials/zscaler_logs/index.ts b/src/plugins/home/server/tutorials/zscaler_logs/index.ts index 977bbb242c62a..316590c74fd76 100644 --- a/src/plugins/home/server/tutorials/zscaler_logs/index.ts +++ b/src/plugins/home/server/tutorials/zscaler_logs/index.ts @@ -54,8 +54,8 @@ export function zscalerLogsSpecProvider(context: TutorialContext): TutorialSchem }, completionTimeMinutes: 10, onPrem: onPremInstructions(moduleName, platforms, context), - elasticCloud: cloudInstructions(moduleName, platforms), - onPremElasticCloud: onPremCloudInstructions(moduleName, platforms), + elasticCloud: cloudInstructions(moduleName, platforms, context), + onPremElasticCloud: onPremCloudInstructions(moduleName, platforms, context), integrationBrowserCategories: ['network', 'security'], }; } diff --git a/src/plugins/interactive_setup/server/kibana_config_writer.test.ts b/src/plugins/interactive_setup/server/kibana_config_writer.test.ts index 82d02882698d4..d097d7cc4a05d 100644 --- a/src/plugins/interactive_setup/server/kibana_config_writer.test.ts +++ b/src/plugins/interactive_setup/server/kibana_config_writer.test.ts @@ -35,7 +35,8 @@ describe('KibanaConfigWriter', () => { throw new Error('Invalid certificate'); } return { - fingerprint256: 'fingerprint256', + fingerprint256: + 'D4:86:CE:00:AC:71:E4:1D:2B:70:D0:87:A5:55:FA:5D:D1:93:6C:DB:45:80:79:53:7B:A3:AC:13:3E:48:34:D6', }; }; @@ -131,7 +132,7 @@ describe('KibanaConfigWriter', () => { elasticsearch.hosts: [some-host] elasticsearch.serviceAccountToken: some-value elasticsearch.ssl.certificateAuthorities: [/data/ca_1234.crt] - xpack.fleet.outputs: [{id: fleet-default-output, name: default, is_default: true, is_default_monitoring: true, type: elasticsearch, hosts: [some-host], ca_sha256: fingerprint256}] + xpack.fleet.outputs: [{id: fleet-default-output, name: default, is_default: true, is_default_monitoring: true, type: elasticsearch, hosts: [some-host], ca_trusted_fingerprint: d486ce00ac71e41d2b70d087a555fa5dd1936cdb458079537ba3ac133e4834d6}] ", ], @@ -198,7 +199,7 @@ describe('KibanaConfigWriter', () => { elasticsearch.username: username elasticsearch.password: password elasticsearch.ssl.certificateAuthorities: [/data/ca_1234.crt] - xpack.fleet.outputs: [{id: fleet-default-output, name: default, is_default: true, is_default_monitoring: true, type: elasticsearch, hosts: [some-host], ca_sha256: fingerprint256}] + xpack.fleet.outputs: [{id: fleet-default-output, name: default, is_default: true, is_default_monitoring: true, type: elasticsearch, hosts: [some-host], ca_trusted_fingerprint: d486ce00ac71e41d2b70d087a555fa5dd1936cdb458079537ba3ac133e4834d6}] ", ], @@ -275,7 +276,7 @@ describe('KibanaConfigWriter', () => { elasticsearch.hosts: [some-host] elasticsearch.serviceAccountToken: some-value elasticsearch.ssl.certificateAuthorities: [/data/ca_1234.crt] - xpack.fleet.outputs: [{id: fleet-default-output, name: default, is_default: true, is_default_monitoring: true, type: elasticsearch, hosts: [some-host], ca_sha256: fingerprint256}] + xpack.fleet.outputs: [{id: fleet-default-output, name: default, is_default: true, is_default_monitoring: true, type: elasticsearch, hosts: [some-host], ca_trusted_fingerprint: d486ce00ac71e41d2b70d087a555fa5dd1936cdb458079537ba3ac133e4834d6}] ", ], @@ -329,7 +330,7 @@ describe('KibanaConfigWriter', () => { monitoring.ui.container.elasticsearch.enabled: true elasticsearch.serviceAccountToken: some-value elasticsearch.ssl.certificateAuthorities: [/data/ca_1234.crt] - xpack.fleet.outputs: [{id: fleet-default-output, name: default, is_default: true, is_default_monitoring: true, type: elasticsearch, hosts: [some-host], ca_sha256: fingerprint256}] + xpack.fleet.outputs: [{id: fleet-default-output, name: default, is_default: true, is_default_monitoring: true, type: elasticsearch, hosts: [some-host], ca_trusted_fingerprint: d486ce00ac71e41d2b70d087a555fa5dd1936cdb458079537ba3ac133e4834d6}] ", ], diff --git a/src/plugins/interactive_setup/server/kibana_config_writer.ts b/src/plugins/interactive_setup/server/kibana_config_writer.ts index af177fee33bce..eac1bd0cef175 100644 --- a/src/plugins/interactive_setup/server/kibana_config_writer.ts +++ b/src/plugins/interactive_setup/server/kibana_config_writer.ts @@ -38,7 +38,7 @@ interface FleetOutputConfig { is_default_monitoring: boolean; type: 'elasticsearch'; hosts: string[]; - ca_sha256: string; + ca_trusted_fingerprint: string; } export class KibanaConfigWriter { @@ -187,7 +187,8 @@ export class KibanaConfigWriter { */ private static getFleetDefaultOutputConfig(caCert: string, host: string): FleetOutputConfig[] { const cert = new X509Certificate(caCert); - const certFingerprint = cert.fingerprint256; + // fingerprint256 is a ":" separated uppercase hexadecimal string + const certFingerprint = cert.fingerprint256.split(':').join('').toLowerCase(); return [ { @@ -197,7 +198,7 @@ export class KibanaConfigWriter { is_default_monitoring: true, type: 'elasticsearch', hosts: [host], - ca_sha256: certFingerprint, + ca_trusted_fingerprint: certFingerprint, }, ]; } diff --git a/src/plugins/management/public/components/management_app/management_app.tsx b/src/plugins/management/public/components/management_app/management_app.tsx index 8a520a2629c3b..4d306ffd2e266 100644 --- a/src/plugins/management/public/components/management_app/management_app.tsx +++ b/src/plugins/management/public/components/management_app/management_app.tsx @@ -8,17 +8,18 @@ import './management_app.scss'; import React, { useState, useEffect, useCallback } from 'react'; -import { AppMountParameters, ChromeBreadcrumb, ScopedHistory } from 'kibana/public'; import { I18nProvider } from '@kbn/i18n-react'; import { i18n } from '@kbn/i18n'; -import { ManagementSection, MANAGEMENT_BREADCRUMB } from '../../utils'; +import { AppMountParameters, ChromeBreadcrumb, ScopedHistory } from 'kibana/public'; +import { ManagementSection, MANAGEMENT_BREADCRUMB } from '../../utils'; import { ManagementRouter } from './management_router'; import { managementSidebarNav } from '../management_sidebar_nav/management_sidebar_nav'; import { KibanaPageTemplate, KibanaPageTemplateProps, reactRouterNavigate, + KibanaThemeProvider, } from '../../../../kibana_react/public'; import { SectionsServiceStart } from '../../types'; @@ -83,24 +84,26 @@ export const ManagementApp = ({ dependencies, history, theme$ }: ManagementAppPr return ( - - - + + + + + ); }; diff --git a/src/plugins/vis_types/heatmap/public/vis_type/heatmap.tsx b/src/plugins/vis_types/heatmap/public/vis_type/heatmap.tsx index 5d466c2f4b3c8..e20c7eb0f8c21 100644 --- a/src/plugins/vis_types/heatmap/public/vis_type/heatmap.tsx +++ b/src/plugins/vis_types/heatmap/public/vis_type/heatmap.tsx @@ -98,7 +98,14 @@ export const getHeatmapVisTypeDefinition = ({ }), min: 0, max: 1, - aggFilter: ['!geohash_grid', '!geotile_grid', '!filter', '!multi_terms'], + aggFilter: [ + '!geohash_grid', + '!geotile_grid', + '!filter', + '!sampler', + '!diversified_sampler', + '!multi_terms', + ], }, { group: AggGroupNames.Buckets, @@ -106,7 +113,14 @@ export const getHeatmapVisTypeDefinition = ({ title: i18n.translate('visTypeHeatmap.heatmap.groupTitle', { defaultMessage: 'Y-axis' }), min: 0, max: 1, - aggFilter: ['!geohash_grid', '!geotile_grid', '!filter', '!multi_terms'], + aggFilter: [ + '!geohash_grid', + '!geotile_grid', + '!filter', + '!sampler', + '!diversified_sampler', + '!multi_terms', + ], }, { group: AggGroupNames.Buckets, @@ -121,7 +135,14 @@ export const getHeatmapVisTypeDefinition = ({ }), min: 0, max: 1, - aggFilter: ['!geohash_grid', '!geotile_grid', '!filter', '!multi_terms'], + aggFilter: [ + '!geohash_grid', + '!geotile_grid', + '!filter', + '!sampler', + '!diversified_sampler', + '!multi_terms', + ], }, ], }, diff --git a/src/plugins/vis_types/metric/public/metric_vis_type.ts b/src/plugins/vis_types/metric/public/metric_vis_type.ts index d4db2ac9e4671..ffb34248aeccc 100644 --- a/src/plugins/vis_types/metric/public/metric_vis_type.ts +++ b/src/plugins/vis_types/metric/public/metric_vis_type.ts @@ -86,7 +86,14 @@ export const createMetricVisTypeDefinition = (): VisTypeDefinition => }), min: 0, max: 1, - aggFilter: ['!geohash_grid', '!geotile_grid', '!filter', '!multi_terms'], + aggFilter: [ + '!geohash_grid', + '!geotile_grid', + '!filter', + '!sampler', + '!diversified_sampler', + '!multi_terms', + ], }, ], }, diff --git a/src/plugins/vis_types/pie/public/sample_vis.test.mocks.ts b/src/plugins/vis_types/pie/public/sample_vis.test.mocks.ts index 15a3675125f61..545456b6dcce0 100644 --- a/src/plugins/vis_types/pie/public/sample_vis.test.mocks.ts +++ b/src/plugins/vis_types/pie/public/sample_vis.test.mocks.ts @@ -87,7 +87,14 @@ export const samplePieVis = { title: 'Split slices', min: 0, max: null, - aggFilter: ['!geohash_grid', '!geotile_grid', '!filter', '!multi_terms'], + aggFilter: [ + '!geohash_grid', + '!geotile_grid', + '!filter', + '!sampler', + '!diversified_sampler', + '!multi_terms', + ], editor: false, params: [], }, @@ -98,7 +105,14 @@ export const samplePieVis = { mustBeFirst: true, min: 0, max: 1, - aggFilter: ['!geohash_grid', '!geotile_grid', '!filter', '!multi_terms'], + aggFilter: [ + '!geohash_grid', + '!geotile_grid', + '!filter', + '!sampler', + '!diversified_sampler', + '!multi_terms', + ], params: [ { name: 'row', diff --git a/src/plugins/vis_types/pie/public/vis_type/pie.ts b/src/plugins/vis_types/pie/public/vis_type/pie.ts index 0d012ed95b5d9..f10af053bd161 100644 --- a/src/plugins/vis_types/pie/public/vis_type/pie.ts +++ b/src/plugins/vis_types/pie/public/vis_type/pie.ts @@ -80,7 +80,14 @@ export const getPieVisTypeDefinition = ({ }), min: 0, max: Infinity, - aggFilter: ['!geohash_grid', '!geotile_grid', '!filter', '!multi_terms'], + aggFilter: [ + '!geohash_grid', + '!geotile_grid', + '!filter', + '!sampler', + '!diversified_sampler', + '!multi_terms', + ], }, { group: AggGroupNames.Buckets, @@ -91,7 +98,14 @@ export const getPieVisTypeDefinition = ({ mustBeFirst: true, min: 0, max: 1, - aggFilter: ['!geohash_grid', '!geotile_grid', '!filter', '!multi_terms'], + aggFilter: [ + '!geohash_grid', + '!geotile_grid', + '!filter', + '!sampler', + '!diversified_sampler', + '!multi_terms', + ], }, ], }, diff --git a/src/plugins/vis_types/table/public/table_vis_type.ts b/src/plugins/vis_types/table/public/table_vis_type.ts index a641224e23f52..2f1642e29107a 100644 --- a/src/plugins/vis_types/table/public/table_vis_type.ts +++ b/src/plugins/vis_types/table/public/table_vis_type.ts @@ -62,7 +62,7 @@ export const tableVisTypeDefinition: VisTypeDefinition = { title: i18n.translate('visTypeTable.tableVisEditorConfig.schemas.bucketTitle', { defaultMessage: 'Split rows', }), - aggFilter: ['!filter'], + aggFilter: ['!filter', '!sampler', '!diversified_sampler', '!multi_terms'], }, { group: AggGroupNames.Buckets, @@ -72,7 +72,7 @@ export const tableVisTypeDefinition: VisTypeDefinition = { }), min: 0, max: 1, - aggFilter: ['!filter'], + aggFilter: ['!filter', '!sampler', '!diversified_sampler', '!multi_terms'], }, ], }, diff --git a/src/plugins/vis_types/vislib/public/gauge.ts b/src/plugins/vis_types/vislib/public/gauge.ts index 51cd7ea7622df..31a44a5d1d73f 100644 --- a/src/plugins/vis_types/vislib/public/gauge.ts +++ b/src/plugins/vis_types/vislib/public/gauge.ts @@ -132,7 +132,14 @@ export const gaugeVisTypeDefinition: VisTypeDefinition = { }), min: 0, max: 1, - aggFilter: ['!geohash_grid', '!geotile_grid', '!filter', '!multi_terms'], + aggFilter: [ + '!geohash_grid', + '!geotile_grid', + '!filter', + '!sampler', + '!diversified_sampler', + '!multi_terms', + ], }, ], }, diff --git a/src/plugins/vis_types/vislib/public/goal.ts b/src/plugins/vis_types/vislib/public/goal.ts index 05ad1f53904d7..26bc598790839 100644 --- a/src/plugins/vis_types/vislib/public/goal.ts +++ b/src/plugins/vis_types/vislib/public/goal.ts @@ -96,7 +96,14 @@ export const goalVisTypeDefinition: VisTypeDefinition = { }), min: 0, max: 1, - aggFilter: ['!geohash_grid', '!geotile_grid', '!filter', '!multi_terms'], + aggFilter: [ + '!geohash_grid', + '!geotile_grid', + '!filter', + '!sampler', + '!diversified_sampler', + '!multi_terms', + ], }, ], }, diff --git a/src/plugins/vis_types/xy/public/editor/components/options/point_series/point_series.mocks.ts b/src/plugins/vis_types/xy/public/editor/components/options/point_series/point_series.mocks.ts index 41ab13d54f7c6..401afc5a7473a 100644 --- a/src/plugins/vis_types/xy/public/editor/components/options/point_series/point_series.mocks.ts +++ b/src/plugins/vis_types/xy/public/editor/components/options/point_series/point_series.mocks.ts @@ -625,7 +625,14 @@ export const getVis = (bucketType: string) => { title: 'X-axis', min: 0, max: 1, - aggFilter: ['!geohash_grid', '!geotile_grid', '!filter', '!multi_terms'], + aggFilter: [ + '!geohash_grid', + '!geotile_grid', + '!filter', + '!sampler', + '!diversified_sampler', + '!multi_terms', + ], params: [], }, { @@ -634,7 +641,14 @@ export const getVis = (bucketType: string) => { title: 'Split series', min: 0, max: 3, - aggFilter: ['!geohash_grid', '!geotile_grid', '!filter', '!multi_terms'], + aggFilter: [ + '!geohash_grid', + '!geotile_grid', + '!filter', + '!sampler', + '!diversified_sampler', + '!multi_terms', + ], params: [], }, { @@ -643,7 +657,14 @@ export const getVis = (bucketType: string) => { title: 'Split chart', min: 0, max: 1, - aggFilter: ['!geohash_grid', '!geotile_grid', '!filter', '!multi_terms'], + aggFilter: [ + '!geohash_grid', + '!geotile_grid', + '!filter', + '!sampler', + '!diversified_sampler', + '!multi_terms', + ], params: [ { name: 'row', @@ -688,7 +709,14 @@ export const getVis = (bucketType: string) => { title: 'X-axis', min: 0, max: 1, - aggFilter: ['!geohash_grid', '!geotile_grid', '!filter', '!multi_terms'], + aggFilter: [ + '!geohash_grid', + '!geotile_grid', + '!filter', + '!sampler', + '!diversified_sampler', + '!multi_terms', + ], params: [], }, { @@ -697,7 +725,14 @@ export const getVis = (bucketType: string) => { title: 'Split series', min: 0, max: 3, - aggFilter: ['!geohash_grid', '!geotile_grid', '!filter', '!multi_terms'], + aggFilter: [ + '!geohash_grid', + '!geotile_grid', + '!filter', + '!sampler', + '!diversified_sampler', + '!multi_terms', + ], params: [], }, { @@ -706,7 +741,14 @@ export const getVis = (bucketType: string) => { title: 'Split chart', min: 0, max: 1, - aggFilter: ['!geohash_grid', '!geotile_grid', '!filter', '!multi_terms'], + aggFilter: [ + '!geohash_grid', + '!geotile_grid', + '!filter', + '!sampler', + '!diversified_sampler', + '!multi_terms', + ], params: [ { name: 'row', @@ -722,7 +764,14 @@ export const getVis = (bucketType: string) => { title: 'X-axis', min: 0, max: 1, - aggFilter: ['!geohash_grid', '!geotile_grid', '!filter', '!multi_terms'], + aggFilter: [ + '!geohash_grid', + '!geotile_grid', + '!filter', + '!sampler', + '!diversified_sampler', + '!multi_terms', + ], params: [], }, { @@ -731,7 +780,14 @@ export const getVis = (bucketType: string) => { title: 'Split series', min: 0, max: 3, - aggFilter: ['!geohash_grid', '!geotile_grid', '!filter', '!multi_terms'], + aggFilter: [ + '!geohash_grid', + '!geotile_grid', + '!filter', + '!sampler', + '!diversified_sampler', + '!multi_terms', + ], params: [], }, { @@ -740,7 +796,14 @@ export const getVis = (bucketType: string) => { title: 'Split chart', min: 0, max: 1, - aggFilter: ['!geohash_grid', '!geotile_grid', '!filter', '!multi_terms'], + aggFilter: [ + '!geohash_grid', + '!geotile_grid', + '!filter', + '!sampler', + '!diversified_sampler', + '!multi_terms', + ], params: [ { name: 'row', diff --git a/src/plugins/vis_types/xy/public/sample_vis.test.mocks.ts b/src/plugins/vis_types/xy/public/sample_vis.test.mocks.ts index 6077732a9cc6b..766929a2cd654 100644 --- a/src/plugins/vis_types/xy/public/sample_vis.test.mocks.ts +++ b/src/plugins/vis_types/xy/public/sample_vis.test.mocks.ts @@ -149,7 +149,14 @@ export const sampleAreaVis = { title: 'X-axis', min: 0, max: 1, - aggFilter: ['!geohash_grid', '!geotile_grid', '!filter', '!multi_terms'], + aggFilter: [ + '!geohash_grid', + '!geotile_grid', + '!filter', + '!sampler', + '!diversified_sampler', + '!multi_terms', + ], editor: false, params: [], }, @@ -159,7 +166,14 @@ export const sampleAreaVis = { title: 'Split series', min: 0, max: 3, - aggFilter: ['!geohash_grid', '!geotile_grid', '!filter', '!multi_terms'], + aggFilter: [ + '!geohash_grid', + '!geotile_grid', + '!filter', + '!sampler', + '!diversified_sampler', + '!multi_terms', + ], editor: false, params: [], }, @@ -169,7 +183,14 @@ export const sampleAreaVis = { title: 'Split chart', min: 0, max: 1, - aggFilter: ['!geohash_grid', '!geotile_grid', '!filter', '!multi_terms'], + aggFilter: [ + '!geohash_grid', + '!geotile_grid', + '!filter', + '!sampler', + '!diversified_sampler', + '!multi_terms', + ], params: [ { name: 'row', diff --git a/src/plugins/vis_types/xy/public/utils/get_series_params.test.ts b/src/plugins/vis_types/xy/public/utils/get_series_params.test.ts index 67b8a1c160d40..5c22527d5b9d7 100644 --- a/src/plugins/vis_types/xy/public/utils/get_series_params.test.ts +++ b/src/plugins/vis_types/xy/public/utils/get_series_params.test.ts @@ -45,7 +45,7 @@ describe('getSeriesParams', () => { ); expect(seriesParams).toStrictEqual([ { - circlesRadius: 3, + circlesRadius: 1, data: { id: '1', label: 'Total quantity', diff --git a/src/plugins/vis_types/xy/public/utils/get_series_params.ts b/src/plugins/vis_types/xy/public/utils/get_series_params.ts index 987c8df83b01f..0acd2a0913282 100644 --- a/src/plugins/vis_types/xy/public/utils/get_series_params.ts +++ b/src/plugins/vis_types/xy/public/utils/get_series_params.ts @@ -22,7 +22,7 @@ const makeSerie = ( type: ChartType.Line, drawLinesBetweenPoints: true, showCircles: true, - circlesRadius: 3, + circlesRadius: 1, interpolate: InterpolationMode.Linear, lineWidth: 2, valueAxis: defaultValueAxis, diff --git a/src/plugins/vis_types/xy/public/vis_types/area.ts b/src/plugins/vis_types/xy/public/vis_types/area.ts index 3b8f78db25d36..efeb4142ff0d7 100644 --- a/src/plugins/vis_types/xy/public/vis_types/area.ts +++ b/src/plugins/vis_types/xy/public/vis_types/area.ts @@ -97,7 +97,7 @@ export const areaVisTypeDefinition = { drawLinesBetweenPoints: true, lineWidth: 2, showCircles: true, - circlesRadius: 3, + circlesRadius: 1, interpolate: InterpolationMode.Linear, valueAxis: 'ValueAxis-1', }, @@ -157,7 +157,14 @@ export const areaVisTypeDefinition = { }), min: 0, max: 1, - aggFilter: ['!geohash_grid', '!geotile_grid', '!filter', '!multi_terms'], + aggFilter: [ + '!geohash_grid', + '!geotile_grid', + '!filter', + '!sampler', + '!diversified_sampler', + '!multi_terms', + ], }, { group: AggGroupNames.Buckets, @@ -167,7 +174,14 @@ export const areaVisTypeDefinition = { }), min: 0, max: 3, - aggFilter: ['!geohash_grid', '!geotile_grid', '!filter', '!multi_terms'], + aggFilter: [ + '!geohash_grid', + '!geotile_grid', + '!filter', + '!sampler', + '!diversified_sampler', + '!multi_terms', + ], }, { group: AggGroupNames.Buckets, @@ -177,7 +191,14 @@ export const areaVisTypeDefinition = { }), min: 0, max: 1, - aggFilter: ['!geohash_grid', '!geotile_grid', '!filter', '!multi_terms'], + aggFilter: [ + '!geohash_grid', + '!geotile_grid', + '!filter', + '!sampler', + '!diversified_sampler', + '!multi_terms', + ], }, ], }, diff --git a/src/plugins/vis_types/xy/public/vis_types/histogram.ts b/src/plugins/vis_types/xy/public/vis_types/histogram.ts index 79b3fd72de452..1cd346abec6e7 100644 --- a/src/plugins/vis_types/xy/public/vis_types/histogram.ts +++ b/src/plugins/vis_types/xy/public/vis_types/histogram.ts @@ -101,7 +101,7 @@ export const histogramVisTypeDefinition = { drawLinesBetweenPoints: true, lineWidth: 2, showCircles: true, - circlesRadius: 3, + circlesRadius: 1, }, ], radiusRatio: 0, @@ -160,7 +160,14 @@ export const histogramVisTypeDefinition = { }), min: 0, max: 1, - aggFilter: ['!geohash_grid', '!geotile_grid', '!filter', '!multi_terms'], + aggFilter: [ + '!geohash_grid', + '!geotile_grid', + '!filter', + '!sampler', + '!diversified_sampler', + '!multi_terms', + ], }, { group: AggGroupNames.Buckets, @@ -170,7 +177,14 @@ export const histogramVisTypeDefinition = { }), min: 0, max: 3, - aggFilter: ['!geohash_grid', '!geotile_grid', '!filter', '!multi_terms'], + aggFilter: [ + '!geohash_grid', + '!geotile_grid', + '!filter', + '!sampler', + '!diversified_sampler', + '!multi_terms', + ], }, { group: AggGroupNames.Buckets, @@ -180,7 +194,14 @@ export const histogramVisTypeDefinition = { }), min: 0, max: 1, - aggFilter: ['!geohash_grid', '!geotile_grid', '!filter', '!multi_terms'], + aggFilter: [ + '!geohash_grid', + '!geotile_grid', + '!filter', + '!sampler', + '!diversified_sampler', + '!multi_terms', + ], }, ], }, diff --git a/src/plugins/vis_types/xy/public/vis_types/horizontal_bar.ts b/src/plugins/vis_types/xy/public/vis_types/horizontal_bar.ts index 5ac833190dd38..4e6056bbdae4f 100644 --- a/src/plugins/vis_types/xy/public/vis_types/horizontal_bar.ts +++ b/src/plugins/vis_types/xy/public/vis_types/horizontal_bar.ts @@ -102,7 +102,7 @@ export const horizontalBarVisTypeDefinition = { drawLinesBetweenPoints: true, lineWidth: 2, showCircles: true, - circlesRadius: 3, + circlesRadius: 1, }, ], addTooltip: true, @@ -159,7 +159,14 @@ export const horizontalBarVisTypeDefinition = { }), min: 0, max: 1, - aggFilter: ['!geohash_grid', '!geotile_grid', '!filter', '!multi_terms'], + aggFilter: [ + '!geohash_grid', + '!geotile_grid', + '!filter', + '!sampler', + '!diversified_sampler', + '!multi_terms', + ], }, { group: AggGroupNames.Buckets, @@ -169,7 +176,14 @@ export const horizontalBarVisTypeDefinition = { }), min: 0, max: 3, - aggFilter: ['!geohash_grid', '!geotile_grid', '!filter', '!multi_terms'], + aggFilter: [ + '!geohash_grid', + '!geotile_grid', + '!filter', + '!sampler', + '!diversified_sampler', + '!multi_terms', + ], }, { group: AggGroupNames.Buckets, @@ -179,7 +193,14 @@ export const horizontalBarVisTypeDefinition = { }), min: 0, max: 1, - aggFilter: ['!geohash_grid', '!geotile_grid', '!filter', '!multi_terms'], + aggFilter: [ + '!geohash_grid', + '!geotile_grid', + '!filter', + '!sampler', + '!diversified_sampler', + '!multi_terms', + ], }, ], }, diff --git a/src/plugins/vis_types/xy/public/vis_types/line.ts b/src/plugins/vis_types/xy/public/vis_types/line.ts index f7467ca53fa0e..affcc64320df6 100644 --- a/src/plugins/vis_types/xy/public/vis_types/line.ts +++ b/src/plugins/vis_types/xy/public/vis_types/line.ts @@ -99,7 +99,7 @@ export const lineVisTypeDefinition = { lineWidth: 2, interpolate: InterpolationMode.Linear, showCircles: true, - circlesRadius: 3, + circlesRadius: 1, }, ], addTooltip: true, @@ -151,7 +151,14 @@ export const lineVisTypeDefinition = { title: i18n.translate('visTypeXy.line.segmentTitle', { defaultMessage: 'X-axis' }), min: 0, max: 1, - aggFilter: ['!geohash_grid', '!geotile_grid', '!filter', '!multi_terms'], + aggFilter: [ + '!geohash_grid', + '!geotile_grid', + '!filter', + '!sampler', + '!diversified_sampler', + '!multi_terms', + ], }, { group: AggGroupNames.Buckets, @@ -161,7 +168,14 @@ export const lineVisTypeDefinition = { }), min: 0, max: 3, - aggFilter: ['!geohash_grid', '!geotile_grid', '!filter', '!multi_terms'], + aggFilter: [ + '!geohash_grid', + '!geotile_grid', + '!filter', + '!sampler', + '!diversified_sampler', + '!multi_terms', + ], }, { group: AggGroupNames.Buckets, @@ -171,7 +185,14 @@ export const lineVisTypeDefinition = { }), min: 0, max: 1, - aggFilter: ['!geohash_grid', '!geotile_grid', '!filter', '!multi_terms'], + aggFilter: [ + '!geohash_grid', + '!geotile_grid', + '!filter', + '!sampler', + '!diversified_sampler', + '!multi_terms', + ], }, ], }, diff --git a/src/plugins/visualizations/public/components/__snapshots__/visualization_noresults.test.js.snap b/src/plugins/visualizations/public/components/__snapshots__/visualization_noresults.test.js.snap index 56e2cb1b60f3c..98d37568e4541 100644 --- a/src/plugins/visualizations/public/components/__snapshots__/visualization_noresults.test.js.snap +++ b/src/plugins/visualizations/public/components/__snapshots__/visualization_noresults.test.js.snap @@ -6,29 +6,42 @@ exports[`VisualizationNoResults should render according to snapshot 1`] = ` data-test-subj="visNoResult" >
-
-
+ +
+
- No results found + +
+
+ No results found +
+
+
-
+
`; diff --git a/src/plugins/visualizations/public/utils/saved_objects_utils/check_for_duplicate_title.ts b/src/plugins/visualizations/public/utils/saved_objects_utils/check_for_duplicate_title.ts new file mode 100644 index 0000000000000..c0820cce45c90 --- /dev/null +++ b/src/plugins/visualizations/public/utils/saved_objects_utils/check_for_duplicate_title.ts @@ -0,0 +1,66 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import type { OverlayStart, SavedObjectsClientContract } from 'kibana/public'; +import type { SavedObject } from '../../../../saved_objects/public'; +import { SAVE_DUPLICATE_REJECTED } from './constants'; +import { findObjectByTitle } from './find_object_by_title'; +import { displayDuplicateTitleConfirmModal } from './display_duplicate_title_confirm_modal'; + +/** + * check for an existing SavedObject with the same title in ES + * returns Promise when it's no duplicate, or the modal displaying the warning + * that's there's a duplicate is confirmed, else it returns a rejected Promise + * @param savedObject + * @param isTitleDuplicateConfirmed + * @param onTitleDuplicate + * @param services + */ +export async function checkForDuplicateTitle( + savedObject: Pick< + SavedObject, + 'id' | 'title' | 'getDisplayName' | 'lastSavedTitle' | 'copyOnSave' | 'getEsType' + >, + isTitleDuplicateConfirmed: boolean, + onTitleDuplicate: (() => void) | undefined, + services: { + savedObjectsClient: SavedObjectsClientContract; + overlays: OverlayStart; + } +): Promise { + const { savedObjectsClient, overlays } = services; + // Don't check for duplicates if user has already confirmed save with duplicate title + if (isTitleDuplicateConfirmed) { + return true; + } + + // Don't check if the user isn't updating the title, otherwise that would become very annoying to have + // to confirm the save every time, except when copyOnSave is true, then we do want to check. + if (savedObject.title === savedObject.lastSavedTitle && !savedObject.copyOnSave) { + return true; + } + + const duplicate = await findObjectByTitle( + savedObjectsClient, + savedObject.getEsType(), + savedObject.title + ); + + if (!duplicate || duplicate.id === savedObject.id) { + return true; + } + + if (onTitleDuplicate) { + onTitleDuplicate(); + return Promise.reject(new Error(SAVE_DUPLICATE_REJECTED)); + } + + // TODO: make onTitleDuplicate a required prop and remove UI components from this class + // Need to leave here until all users pass onTitleDuplicate. + return displayDuplicateTitleConfirmModal(savedObject, overlays); +} diff --git a/src/plugins/visualizations/public/utils/saved_objects_utils/confirm_modal_promise.tsx b/src/plugins/visualizations/public/utils/saved_objects_utils/confirm_modal_promise.tsx new file mode 100644 index 0000000000000..3c29fd958465b --- /dev/null +++ b/src/plugins/visualizations/public/utils/saved_objects_utils/confirm_modal_promise.tsx @@ -0,0 +1,46 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import React from 'react'; +import { i18n } from '@kbn/i18n'; +import type { OverlayStart } from 'kibana/public'; +import { EuiConfirmModal } from '@elastic/eui'; +import { toMountPoint } from '../../../../kibana_react/public'; + +export function confirmModalPromise( + message = '', + title = '', + confirmBtnText = '', + overlays: OverlayStart +): Promise { + return new Promise((resolve, reject) => { + const cancelButtonText = i18n.translate('visualizations.confirmModal.cancelButtonLabel', { + defaultMessage: 'Cancel', + }); + + const modal = overlays.openModal( + toMountPoint( + { + modal.close(); + reject(); + }} + onConfirm={() => { + modal.close(); + resolve(true); + }} + confirmButtonText={confirmBtnText} + cancelButtonText={cancelButtonText} + title={title} + > + {message} + + ) + ); + }); +} diff --git a/src/plugins/visualizations/public/utils/saved_objects_utils/constants.ts b/src/plugins/visualizations/public/utils/saved_objects_utils/constants.ts new file mode 100644 index 0000000000000..fcabc0b493f68 --- /dev/null +++ b/src/plugins/visualizations/public/utils/saved_objects_utils/constants.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 + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { i18n } from '@kbn/i18n'; + +/** An error message to be used when the user rejects a confirm overwrite. */ +export const OVERWRITE_REJECTED = i18n.translate('visualizations.overwriteRejectedDescription', { + defaultMessage: 'Overwrite confirmation was rejected', +}); + +/** An error message to be used when the user rejects a confirm save with duplicate title. */ +export const SAVE_DUPLICATE_REJECTED = i18n.translate( + 'visualizations.saveDuplicateRejectedDescription', + { + defaultMessage: 'Save with duplicate title confirmation was rejected', + } +); diff --git a/src/plugins/visualizations/public/utils/saved_objects_utils/display_duplicate_title_confirm_modal.ts b/src/plugins/visualizations/public/utils/saved_objects_utils/display_duplicate_title_confirm_modal.ts new file mode 100644 index 0000000000000..48ada48511812 --- /dev/null +++ b/src/plugins/visualizations/public/utils/saved_objects_utils/display_duplicate_title_confirm_modal.ts @@ -0,0 +1,36 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { i18n } from '@kbn/i18n'; +import type { OverlayStart } from 'kibana/public'; +import type { SavedObject } from '../../../../saved_objects/public'; +import { SAVE_DUPLICATE_REJECTED } from './constants'; +import { confirmModalPromise } from './confirm_modal_promise'; + +export function displayDuplicateTitleConfirmModal( + savedObject: Pick, + overlays: OverlayStart +): Promise { + const confirmMessage = i18n.translate( + 'visualizations.confirmModal.saveDuplicateConfirmationMessage', + { + defaultMessage: `A {name} with the title '{title}' already exists. Would you like to save anyway?`, + values: { title: savedObject.title, name: savedObject.getDisplayName() }, + } + ); + + const confirmButtonText = i18n.translate('visualizations.confirmModal.saveDuplicateButtonLabel', { + defaultMessage: 'Save {name}', + values: { name: savedObject.getDisplayName() }, + }); + try { + return confirmModalPromise(confirmMessage, '', confirmButtonText, overlays); + } catch { + return Promise.reject(new Error(SAVE_DUPLICATE_REJECTED)); + } +} diff --git a/src/plugins/visualizations/public/utils/saved_objects_utils/find_object_by_title.test.ts b/src/plugins/visualizations/public/utils/saved_objects_utils/find_object_by_title.test.ts new file mode 100644 index 0000000000000..d61fe1c13eee4 --- /dev/null +++ b/src/plugins/visualizations/public/utils/saved_objects_utils/find_object_by_title.test.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 + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { findObjectByTitle } from './find_object_by_title'; +import { + SimpleSavedObject, + SavedObjectsClientContract, + SavedObject, +} from '../../../../../core/public'; + +describe('findObjectByTitle', () => { + const savedObjectsClient: SavedObjectsClientContract = {} as SavedObjectsClientContract; + + beforeEach(() => { + savedObjectsClient.find = jest.fn(); + }); + + it('returns undefined if title is not provided', async () => { + const match = await findObjectByTitle(savedObjectsClient, 'index-pattern', ''); + expect(match).toBeUndefined(); + }); + + it('matches any case', async () => { + const indexPattern = new SimpleSavedObject(savedObjectsClient, { + attributes: { title: 'foo' }, + } as SavedObject); + savedObjectsClient.find = jest.fn().mockImplementation(() => + Promise.resolve({ + savedObjects: [indexPattern], + }) + ); + const match = await findObjectByTitle(savedObjectsClient, 'index-pattern', 'FOO'); + expect(match).toEqual(indexPattern); + }); +}); diff --git a/src/plugins/visualizations/public/utils/saved_objects_utils/find_object_by_title.ts b/src/plugins/visualizations/public/utils/saved_objects_utils/find_object_by_title.ts new file mode 100644 index 0000000000000..10289ac0f2f53 --- /dev/null +++ b/src/plugins/visualizations/public/utils/saved_objects_utils/find_object_by_title.ts @@ -0,0 +1,37 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import type { + SavedObjectsClientContract, + SimpleSavedObject, + SavedObjectAttributes, +} from 'kibana/public'; + +/** Returns an object matching a given title */ +export async function findObjectByTitle( + savedObjectsClient: SavedObjectsClientContract, + type: string, + title: string +): Promise | void> { + if (!title) { + return; + } + + // Elastic search will return the most relevant results first, which means exact matches should come + // first, and so we shouldn't need to request everything. Using 10 just to be on the safe side. + const response = await savedObjectsClient.find({ + type, + perPage: 10, + search: `"${title}"`, + searchFields: ['title'], + fields: ['title'], + }); + return response.savedObjects.find( + (obj) => obj.get('title').toLowerCase() === title.toLowerCase() + ); +} diff --git a/src/plugins/data/public/query/filter_manager/types.ts b/src/plugins/visualizations/public/utils/saved_objects_utils/index.ts similarity index 72% rename from src/plugins/data/public/query/filter_manager/types.ts rename to src/plugins/visualizations/public/utils/saved_objects_utils/index.ts index 5c2667fbf1d2a..e993ddd96a7d9 100644 --- a/src/plugins/data/public/query/filter_manager/types.ts +++ b/src/plugins/visualizations/public/utils/saved_objects_utils/index.ts @@ -6,9 +6,5 @@ * Side Public License, v 1. */ -import { Filter } from '../../../common'; - -export interface PartitionedFilters { - globalFilters: Filter[]; - appFilters: Filter[]; -} +export { saveWithConfirmation } from './save_with_confirmation'; +export { checkForDuplicateTitle } from './check_for_duplicate_title'; diff --git a/src/plugins/visualizations/public/utils/saved_objects_utils/save_with_confirmation.test.ts b/src/plugins/visualizations/public/utils/saved_objects_utils/save_with_confirmation.test.ts new file mode 100644 index 0000000000000..6d2c8f6bbe089 --- /dev/null +++ b/src/plugins/visualizations/public/utils/saved_objects_utils/save_with_confirmation.test.ts @@ -0,0 +1,82 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import type { SavedObjectAttributes, SavedObjectsCreateOptions, OverlayStart } from 'kibana/public'; +import type { SavedObjectsClientContract } from 'kibana/public'; +import { saveWithConfirmation } from './save_with_confirmation'; +import * as deps from './confirm_modal_promise'; +import { OVERWRITE_REJECTED } from './constants'; + +describe('saveWithConfirmation', () => { + const savedObjectsClient: SavedObjectsClientContract = {} as SavedObjectsClientContract; + const overlays: OverlayStart = {} as OverlayStart; + const source: SavedObjectAttributes = {} as SavedObjectAttributes; + const options: SavedObjectsCreateOptions = {} as SavedObjectsCreateOptions; + const savedObject = { + getEsType: () => 'test type', + title: 'test title', + displayName: 'test display name', + }; + + beforeEach(() => { + savedObjectsClient.create = jest.fn(); + jest.spyOn(deps, 'confirmModalPromise').mockReturnValue(Promise.resolve({} as any)); + }); + + test('should call create of savedObjectsClient', async () => { + await saveWithConfirmation(source, savedObject, options, { savedObjectsClient, overlays }); + expect(savedObjectsClient.create).toHaveBeenCalledWith( + savedObject.getEsType(), + source, + options + ); + }); + + test('should call confirmModalPromise when such record exists', async () => { + savedObjectsClient.create = jest + .fn() + .mockImplementation((type, src, opt) => + opt && opt.overwrite ? Promise.resolve({} as any) : Promise.reject({ res: { status: 409 } }) + ); + + await saveWithConfirmation(source, savedObject, options, { savedObjectsClient, overlays }); + expect(deps.confirmModalPromise).toHaveBeenCalledWith( + expect.any(String), + expect.any(String), + expect.any(String), + overlays + ); + }); + + test('should call create of savedObjectsClient when overwriting confirmed', async () => { + savedObjectsClient.create = jest + .fn() + .mockImplementation((type, src, opt) => + opt && opt.overwrite ? Promise.resolve({} as any) : Promise.reject({ res: { status: 409 } }) + ); + + await saveWithConfirmation(source, savedObject, options, { savedObjectsClient, overlays }); + expect(savedObjectsClient.create).toHaveBeenLastCalledWith(savedObject.getEsType(), source, { + overwrite: true, + ...options, + }); + }); + + test('should reject when overwriting denied', async () => { + savedObjectsClient.create = jest.fn().mockReturnValue(Promise.reject({ res: { status: 409 } })); + jest.spyOn(deps, 'confirmModalPromise').mockReturnValue(Promise.reject()); + + expect.assertions(1); + await expect( + saveWithConfirmation(source, savedObject, options, { + savedObjectsClient, + overlays, + }) + ).rejects.toThrow(OVERWRITE_REJECTED); + }); +}); diff --git a/src/plugins/visualizations/public/utils/saved_objects_utils/save_with_confirmation.ts b/src/plugins/visualizations/public/utils/saved_objects_utils/save_with_confirmation.ts new file mode 100644 index 0000000000000..de9ba38343548 --- /dev/null +++ b/src/plugins/visualizations/public/utils/saved_objects_utils/save_with_confirmation.ts @@ -0,0 +1,76 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { get } from 'lodash'; +import { i18n } from '@kbn/i18n'; +import type { + SavedObjectAttributes, + SavedObjectsCreateOptions, + OverlayStart, + SavedObjectsClientContract, +} from 'kibana/public'; +import { OVERWRITE_REJECTED } from './constants'; +import { confirmModalPromise } from './confirm_modal_promise'; + +/** + * Attempts to create the current object using the serialized source. If an object already + * exists, a warning message requests an overwrite confirmation. + * @param source - serialized version of this object what will be indexed into elasticsearch. + * @param savedObject - a simple object that contains properties title and displayName, and getEsType method + * @param options - options to pass to the saved object create method + * @param services - provides Kibana services savedObjectsClient and overlays + * @returns {Promise} - A promise that is resolved with the objects id if the object is + * successfully indexed. If the overwrite confirmation was rejected, an error is thrown with + * a confirmRejected = true parameter so that case can be handled differently than + * a create or index error. + * @resolved {SavedObject} + */ +export async function saveWithConfirmation( + source: SavedObjectAttributes, + savedObject: { + getEsType(): string; + title: string; + displayName: string; + }, + options: SavedObjectsCreateOptions, + services: { savedObjectsClient: SavedObjectsClientContract; overlays: OverlayStart } +) { + const { savedObjectsClient, overlays } = services; + try { + return await savedObjectsClient.create(savedObject.getEsType(), source, options); + } catch (err) { + // record exists, confirm overwriting + if (get(err, 'res.status') === 409) { + const confirmMessage = i18n.translate( + 'visualizations.confirmModal.overwriteConfirmationMessage', + { + defaultMessage: 'Are you sure you want to overwrite {title}?', + values: { title: savedObject.title }, + } + ); + + const title = i18n.translate('visualizations.confirmModal.overwriteTitle', { + defaultMessage: 'Overwrite {name}?', + values: { name: savedObject.displayName }, + }); + const confirmButtonText = i18n.translate('visualizations.confirmModal.overwriteButtonLabel', { + defaultMessage: 'Overwrite', + }); + + return confirmModalPromise(confirmMessage, title, confirmButtonText, overlays) + .then(() => + savedObjectsClient.create(savedObject.getEsType(), source, { + overwrite: true, + ...options, + }) + ) + .catch(() => Promise.reject(new Error(OVERWRITE_REJECTED))); + } + return await Promise.reject(err); + } +} diff --git a/src/plugins/visualizations/public/utils/saved_visualize_utils.test.ts b/src/plugins/visualizations/public/utils/saved_visualize_utils.test.ts index 5c8c0594d3563..fe2453fbb78a4 100644 --- a/src/plugins/visualizations/public/utils/saved_visualize_utils.test.ts +++ b/src/plugins/visualizations/public/utils/saved_visualize_utils.test.ts @@ -56,10 +56,11 @@ const mockCheckForDuplicateTitle = jest.fn(() => { } }); const mockSaveWithConfirmation = jest.fn(() => ({ id: 'test-after-confirm' })); -jest.mock('../../../../plugins/saved_objects/public', () => ({ +jest.mock('./saved_objects_utils/check_for_duplicate_title', () => ({ checkForDuplicateTitle: jest.fn(() => mockCheckForDuplicateTitle()), +})); +jest.mock('./saved_objects_utils/save_with_confirmation', () => ({ saveWithConfirmation: jest.fn(() => mockSaveWithConfirmation()), - isErrorNonFatal: jest.fn(() => true), })); describe('saved_visualize_utils', () => { @@ -263,15 +264,19 @@ describe('saved_visualize_utils', () => { describe('isTitleDuplicateConfirmed', () => { it('as false we should not save vis with duplicated title', async () => { isTitleDuplicateConfirmed = false; - const savedVisId = await saveVisualization( - vis, - { isTitleDuplicateConfirmed }, - { savedObjectsClient, overlays } - ); + try { + const savedVisId = await saveVisualization( + vis, + { isTitleDuplicateConfirmed }, + { savedObjectsClient, overlays } + ); + expect(savedVisId).toBe(''); + } catch { + // ignore + } expect(savedObjectsClient.create).not.toHaveBeenCalled(); expect(mockSaveWithConfirmation).not.toHaveBeenCalled(); expect(mockCheckForDuplicateTitle).toHaveBeenCalled(); - expect(savedVisId).toBe(''); expect(vis.id).toBeUndefined(); }); diff --git a/src/plugins/visualizations/public/utils/saved_visualize_utils.ts b/src/plugins/visualizations/public/utils/saved_visualize_utils.ts index a28ee9486c4d2..f221fa6a208b8 100644 --- a/src/plugins/visualizations/public/utils/saved_visualize_utils.ts +++ b/src/plugins/visualizations/public/utils/saved_visualize_utils.ts @@ -22,11 +22,7 @@ import { parseSearchSourceJSON, DataPublicPluginStart, } from '../../../../plugins/data/public'; -import { - checkForDuplicateTitle, - saveWithConfirmation, - isErrorNonFatal, -} from '../../../../plugins/saved_objects/public'; +import { saveWithConfirmation, checkForDuplicateTitle } from './saved_objects_utils'; import type { SavedObjectsTaggingApi } from '../../../saved_objects_tagging_oss/public'; import type { SpacesPluginStart } from '../../../../../x-pack/plugins/spaces/public'; import { VisualizationsAppExtension } from '../vis_types/vis_type_alias_registry'; @@ -41,6 +37,7 @@ import type { TypesStart, BaseVisType } from '../vis_types'; // @ts-ignore import { updateOldState } from '../legacy/vis_update_state'; import { injectReferences, extractReferences } from './saved_visualization_references'; +import { OVERWRITE_REJECTED, SAVE_DUPLICATE_REJECTED } from './saved_objects_utils/constants'; export const SAVED_VIS_TYPE = 'visualization'; @@ -395,7 +392,7 @@ export async function saveVisualization( return savedObject.id; } catch (err: any) { savedObject.id = originalId; - if (isErrorNonFatal(err)) { + if (err && [OVERWRITE_REJECTED, SAVE_DUPLICATE_REJECTED].includes(err.message)) { return ''; } return Promise.reject(err); diff --git a/src/plugins/visualizations/server/migrations/visualization_saved_object_migrations.test.ts b/src/plugins/visualizations/server/migrations/visualization_saved_object_migrations.test.ts index 1f6fbfeb47e59..06e06a4fefa0c 100644 --- a/src/plugins/visualizations/server/migrations/visualization_saved_object_migrations.test.ts +++ b/src/plugins/visualizations/server/migrations/visualization_saved_object_migrations.test.ts @@ -1674,7 +1674,8 @@ describe('migration visualization', () => { type = 'area', categoryAxes?: object[], valueAxes?: object[], - hasPalette = false + hasPalette = false, + hasCirclesRadius = false ) => ({ attributes: { title: 'My Vis', @@ -1694,6 +1695,21 @@ describe('migration visualization', () => { labels: {}, }, ], + seriesParams: [ + { + show: true, + type, + mode: 'stacked', + drawLinesBetweenPoints: true, + lineWidth: 2, + showCircles: true, + interpolate: 'linear', + valueAxis: 'ValueAxis-1', + ...(hasCirclesRadius && { + circlesRadius: 3, + }), + }, + ], ...(hasPalette && { palette: { type: 'palette', @@ -1732,6 +1748,20 @@ describe('migration visualization', () => { expect(palette.name).toEqual('default'); }); + it("should decorate existing docs with the circlesRadius attribute if it doesn't exist", () => { + const migratedTestDoc = migrate(getTestDoc()); + const [result] = JSON.parse(migratedTestDoc.attributes.visState).params.seriesParams; + + expect(result.circlesRadius).toEqual(1); + }); + + it('should not decorate existing docs with the circlesRadius attribute if it exists', () => { + const migratedTestDoc = migrate(getTestDoc('area', undefined, undefined, true, true)); + const [result] = JSON.parse(migratedTestDoc.attributes.visState).params.seriesParams; + + expect(result.circlesRadius).toEqual(3); + }); + describe('labels.filter', () => { it('should keep existing categoryAxes labels.filter value', () => { const migratedTestDoc = migrate(getTestDoc('area', [{ labels: { filter: false } }])); diff --git a/src/plugins/visualizations/server/migrations/visualization_saved_object_migrations.ts b/src/plugins/visualizations/server/migrations/visualization_saved_object_migrations.ts index b598d34943e6c..4c8771a2f6924 100644 --- a/src/plugins/visualizations/server/migrations/visualization_saved_object_migrations.ts +++ b/src/plugins/visualizations/server/migrations/visualization_saved_object_migrations.ts @@ -867,6 +867,20 @@ const decorateAxes = ( }, })); +/** + * Defaults circlesRadius to 1 if it is not configured + */ +const addCirclesRadius = (axes: T[]): T[] => + axes.map((axis) => { + const hasCircleRadiusAttribute = Number.isFinite(axis?.circlesRadius); + return { + ...axis, + ...(!hasCircleRadiusAttribute && { + circlesRadius: 1, + }), + }; + }); + // Inlined from vis_type_xy const CHART_TYPE_AREA = 'area'; const CHART_TYPE_LINE = 'line'; @@ -913,10 +927,12 @@ const migrateVislibAreaLineBarTypes: SavedObjectMigrationFn = (doc) => valueAxes: visState.params.valueAxes && decorateAxes(visState.params.valueAxes, isHorizontalBar), + seriesParams: + visState.params.seriesParams && addCirclesRadius(visState.params.seriesParams), isVislibVis: true, detailedTooltip: true, ...(isLineOrArea && { - fittingFunction: 'zero', + fittingFunction: 'linear', }), }, }), diff --git a/test/common/config.js b/test/common/config.js index b9ab24450ac82..1a60932581847 100644 --- a/test/common/config.js +++ b/test/common/config.js @@ -56,6 +56,10 @@ export default function () { ...(!!process.env.CODE_COVERAGE ? [`--plugin-path=${path.join(__dirname, 'fixtures', 'plugins', 'coverage')}`] : []), + '--logging.appenders.deprecation.type=console', + '--logging.appenders.deprecation.layout.type=json', + '--logging.loggers[0].name=elasticsearch.deprecation', + '--logging.loggers[0].appenders[0]=deprecation', ], }, services, diff --git a/test/functional/apps/context/_context_navigation.ts b/test/functional/apps/context/_context_navigation.ts index 9b8d33208dfb1..c7337b91bf0c9 100644 --- a/test/functional/apps/context/_context_navigation.ts +++ b/test/functional/apps/context/_context_navigation.ts @@ -6,6 +6,7 @@ * Side Public License, v 1. */ +import expect from '@kbn/expect'; import { FtrProviderContext } from '../../ftr_provider_context'; const TEST_FILTER_COLUMN_NAMES = [ @@ -22,6 +23,8 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { const docTable = getService('docTable'); const PageObjects = getPageObjects(['common', 'context', 'discover', 'timePicker']); const kibanaServer = getService('kibanaServer'); + const filterBar = getService('filterBar'); + const find = getService('find'); describe('discover - context - back navigation', function contextSize() { before(async function () { @@ -56,5 +59,29 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { return initialHitCount === hitCount; }); }); + + it('should go back via breadcrumbs with preserved state', async function () { + await retry.waitFor( + 'user navigating to context and returning to discover via breadcrumbs', + async () => { + await docTable.clickRowToggle({ rowIndex: 0 }); + const rowActions = await docTable.getRowActions({ rowIndex: 0 }); + await rowActions[0].click(); + await PageObjects.context.waitUntilContextLoadingHasFinished(); + + await find.clickByCssSelector(`[data-test-subj="breadcrumb first"]`); + await PageObjects.discover.waitForDocTableLoadingComplete(); + + for (const [columnName, value] of TEST_FILTER_COLUMN_NAMES) { + expect(await filterBar.hasFilter(columnName, value)).to.eql(true); + } + expect(await PageObjects.timePicker.getTimeConfigAsAbsoluteTimes()).to.eql({ + start: 'Sep 18, 2015 @ 06:31:44.000', + end: 'Sep 23, 2015 @ 18:31:44.000', + }); + return true; + } + ); + }); }); } diff --git a/test/functional/apps/dashboard/dashboard_filtering.ts b/test/functional/apps/dashboard/dashboard_filtering.ts index 796e8e35f0d49..6c8a378831340 100644 --- a/test/functional/apps/dashboard/dashboard_filtering.ts +++ b/test/functional/apps/dashboard/dashboard_filtering.ts @@ -29,8 +29,6 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { const PageObjects = getPageObjects(['common', 'dashboard', 'header', 'visualize', 'timePicker']); describe('dashboard filtering', function () { - this.tags('includeFirefox'); - const populateDashboard = async () => { await PageObjects.dashboard.clickNewDashboard(); await PageObjects.timePicker.setDefaultDataRange(); @@ -67,8 +65,7 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { await security.testUser.restoreDefaults(); }); - // FLAKY: https://github.com/elastic/kibana/issues/120195 - describe.skip('adding a filter that excludes all data', () => { + describe('adding a filter that excludes all data', () => { before(async () => { await populateDashboard(); await addFilterAndRefresh(); diff --git a/test/functional/apps/dashboard/full_screen_mode.ts b/test/functional/apps/dashboard/full_screen_mode.ts index 02669759f68ea..fcfd0fc49dd2b 100644 --- a/test/functional/apps/dashboard/full_screen_mode.ts +++ b/test/functional/apps/dashboard/full_screen_mode.ts @@ -12,6 +12,7 @@ import { FtrProviderContext } from '../../ftr_provider_context'; export default function ({ getService, getPageObjects }: FtrProviderContext) { const retry = getService('retry'); + const browser = getService('browser'); const esArchiver = getService('esArchiver'); const kibanaServer = getService('kibanaServer'); const dashboardPanelActions = getService('dashboardPanelActions'); @@ -93,5 +94,20 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { }); await filterBar.removeFilter('bytes'); }); + + it('exits full screen mode when back button pressed', async () => { + await PageObjects.dashboard.clickFullScreenMode(); + await browser.goBack(); + await retry.try(async () => { + const isChromeVisible = await PageObjects.common.isChromeVisible(); + expect(isChromeVisible).to.be(true); + }); + + await browser.goForward(); + await retry.try(async () => { + const isChromeVisible = await PageObjects.common.isChromeVisible(); + expect(isChromeVisible).to.be(true); + }); + }); }); } diff --git a/test/functional/apps/dashboard/url_field_formatter.ts b/test/functional/apps/dashboard/url_field_formatter.ts index 254d71294d8c7..16cdb62768219 100644 --- a/test/functional/apps/dashboard/url_field_formatter.ts +++ b/test/functional/apps/dashboard/url_field_formatter.ts @@ -56,7 +56,7 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { await common.navigateToApp('dashboard'); await dashboard.loadSavedDashboard('dashboard with table'); await dashboard.waitForRenderComplete(); - const fieldLink = await visChart.getFieldLinkInVisTable(`${fieldName}: Descending`, 1); + const fieldLink = await visChart.getFieldLinkInVisTable(`${fieldName}: Descending`); await clickFieldAndCheckUrl(fieldLink); }); diff --git a/test/functional/apps/discover/_data_grid_field_data.ts b/test/functional/apps/discover/_data_grid_field_data.ts index 91c2d5914732d..4a4e06e28c321 100644 --- a/test/functional/apps/discover/_data_grid_field_data.ts +++ b/test/functional/apps/discover/_data_grid_field_data.ts @@ -71,7 +71,7 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { await PageObjects.discover.waitUntilSearchingHasFinished(); await retry.waitFor('first cell contains expected timestamp', async () => { - const cell = await dataGrid.getCellElement(1, 3); + const cell = await dataGrid.getCellElement(0, 2); const text = await cell.getVisibleText(); return text === expectedTimeStamp; }); diff --git a/test/functional/apps/visualize/_data_table.ts b/test/functional/apps/visualize/_data_table.ts index 14181c084a77f..77973b8fb9b67 100644 --- a/test/functional/apps/visualize/_data_table.ts +++ b/test/functional/apps/visualize/_data_table.ts @@ -268,7 +268,7 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { }); it('should apply correct filter', async () => { - await PageObjects.visChart.filterOnTableCell(1, 3); + await PageObjects.visChart.filterOnTableCell(0, 2); await PageObjects.visChart.waitForVisualizationRenderingStabilized(); const data = await PageObjects.visChart.getTableVisContent(); expect(data).to.be.eql([ diff --git a/test/functional/apps/visualize/_data_table_notimeindex_filters.ts b/test/functional/apps/visualize/_data_table_notimeindex_filters.ts index ef664bf4b3054..51ceef947bfac 100644 --- a/test/functional/apps/visualize/_data_table_notimeindex_filters.ts +++ b/test/functional/apps/visualize/_data_table_notimeindex_filters.ts @@ -70,7 +70,7 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { await retry.try(async () => { // hover and click on cell to filter - await PageObjects.visChart.filterOnTableCell(1, 2); + await PageObjects.visChart.filterOnTableCell(0, 1); await PageObjects.header.waitUntilLoadingHasFinished(); await renderable.waitForRender(); diff --git a/test/functional/apps/visualize/_embedding_chart.ts b/test/functional/apps/visualize/_embedding_chart.ts index 93ab2987dc4a8..9531eafc33bed 100644 --- a/test/functional/apps/visualize/_embedding_chart.ts +++ b/test/functional/apps/visualize/_embedding_chart.ts @@ -83,7 +83,7 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { it('should allow to change timerange from the visualization in embedded mode', async () => { await retry.try(async () => { - await PageObjects.visChart.filterOnTableCell(1, 7); + await PageObjects.visChart.filterOnTableCell(0, 6); await PageObjects.header.waitUntilLoadingHasFinished(); await renderable.waitForRender(); diff --git a/test/functional/page_objects/visualize_chart_page.ts b/test/functional/page_objects/visualize_chart_page.ts index d9f183ddd5332..dc36197034691 100644 --- a/test/functional/page_objects/visualize_chart_page.ts +++ b/test/functional/page_objects/visualize_chart_page.ts @@ -349,10 +349,12 @@ export class VisualizeChartPageObject extends FtrService { return await this.testSubjects.getVisibleText('dataGridHeader'); } - public async getFieldLinkInVisTable(fieldName: string, rowIndex: number = 1) { - const headers = await this.dataGrid.getHeaders(); - const fieldColumnIndex = headers.indexOf(fieldName); - const cell = await this.dataGrid.getCellElement(rowIndex, fieldColumnIndex + 1); + public async getFieldLinkInVisTable( + fieldName: string, + rowIndex: number = 0, + colIndex: number = 0 + ) { + const cell = await this.dataGrid.getCellElement(rowIndex, colIndex); return await cell.findByTagName('a'); } diff --git a/test/functional/services/data_grid.ts b/test/functional/services/data_grid.ts index f54e7b65a46e2..d49ef5fa0990a 100644 --- a/test/functional/services/data_grid.ts +++ b/test/functional/services/data_grid.ts @@ -81,18 +81,12 @@ export class DataGridService extends FtrService { /** * Returns a grid cell element by row & column indexes. - * The row offset equals 1 since the first row of data grid is the header row. - * @param rowIndex data row index starting from 1 (1 means 1st row) - * @param columnIndex column index starting from 1 (1 means 1st column) + * @param rowIndex data row index starting from 0 (0 means 1st row) + * @param columnIndex column index starting from 0 (0 means 1st column) */ - public async getCellElement(rowIndex: number, columnIndex: number) { - const table = await this.find.byCssSelector('.euiDataGrid'); - const $ = await table.parseDomContent(); - const columnNumber = $('.euiDataGridHeaderCell__content').length; + public async getCellElement(rowIndex: number = 0, columnIndex: number = 0) { return await this.find.byCssSelector( - `[data-test-subj="dataGridWrapper"] [data-test-subj="dataGridRowCell"]:nth-of-type(${ - columnNumber * (rowIndex - 1) + columnIndex + 1 - })` + `[data-test-subj="dataGridWrapper"] [data-test-subj="dataGridRowCell"][data-gridcell-id="${rowIndex},${columnIndex}"]` ); } diff --git a/test/interpreter_functional/test_suites/run_pipeline/esaggs_sampler.ts b/test/interpreter_functional/test_suites/run_pipeline/esaggs_sampler.ts new file mode 100644 index 0000000000000..d2411b2416067 --- /dev/null +++ b/test/interpreter_functional/test_suites/run_pipeline/esaggs_sampler.ts @@ -0,0 +1,121 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import expect from '@kbn/expect'; +import { ExpectExpression, expectExpressionProvider } from './helpers'; +import { FtrProviderContext } from '../../../functional/ftr_provider_context'; + +export default function ({ + getService, + updateBaselines, +}: FtrProviderContext & { updateBaselines: boolean }) { + let expectExpression: ExpectExpression; + + describe('esaggs_sampler', () => { + before(() => { + expectExpression = expectExpressionProvider({ getService, updateBaselines }); + }); + + const timeRange = { + from: '2015-09-21T00:00:00Z', + to: '2015-09-22T00:00:00Z', + }; + + describe('aggSampler', () => { + it('can execute aggSampler', async () => { + const expression = ` + kibana_context timeRange={timerange from='${timeRange.from}' to='${timeRange.to}'} + | esaggs index={indexPatternLoad id='logstash-*'} + aggs={aggSampler id="0" enabled=true schema="bucket"} + aggs={aggAvg id="1" enabled=true schema="metric" field="bytes"} + `; + const result = await expectExpression('sampler', expression).getResponse(); + + expect(result.columns.length).to.be(2); + const samplerColumn = result.columns[0]; + expect(samplerColumn.name).to.be('sampler'); + expect(samplerColumn.meta.sourceParams.params).to.eql({}); + + expect(result.rows.length).to.be(1); + expect(Object.keys(result.rows[0]).length).to.be(1); + const resultFromSample = result.rows[0]['col-1-1']; // check that sampler bucket doesn't produce columns + expect(typeof resultFromSample).to.be('number'); + expect(resultFromSample).to.greaterThan(0); // can't check exact metric using sample + }); + + it('can execute aggSampler with custom shard_size', async () => { + const expression = ` + kibana_context timeRange={timerange from='${timeRange.from}' to='${timeRange.to}'} + | esaggs index={indexPatternLoad id='logstash-*'} + aggs={aggSampler id="0" enabled=true schema="bucket" shard_size=20} + aggs={aggAvg id="1" enabled=true schema="metric" field="bytes"} + `; + const result = await expectExpression('sampler', expression).getResponse(); + + expect(result.columns.length).to.be(2); + const samplerColumn = result.columns[0]; + expect(samplerColumn.name).to.be('sampler'); + expect(samplerColumn.meta.sourceParams.params).to.eql({ shard_size: 20 }); + + expect(result.rows.length).to.be(1); + expect(Object.keys(result.rows[0]).length).to.be(1); // check that sampler bucket doesn't produce columns + const resultFromSample = result.rows[0]['col-1-1']; + expect(typeof resultFromSample).to.be('number'); + expect(resultFromSample).to.greaterThan(0); // can't check exact metric using sample + }); + }); + + describe('aggDiversifiedSampler', () => { + it('can execute aggDiversifiedSampler', async () => { + const expression = ` + kibana_context timeRange={timerange from='${timeRange.from}' to='${timeRange.to}'} + | esaggs index={indexPatternLoad id='logstash-*'} + aggs={aggDiversifiedSampler id="0" enabled=true schema="bucket" field="extension.raw"} + aggs={aggAvg id="1" enabled=true schema="metric" field="bytes"} + `; + const result = await expectExpression('sampler', expression).getResponse(); + + expect(result.columns.length).to.be(2); + const samplerColumn = result.columns[0]; + expect(samplerColumn.name).to.be('diversified_sampler'); + expect(samplerColumn.meta.sourceParams.params).to.eql({ field: 'extension.raw' }); + + expect(result.rows.length).to.be(1); + expect(Object.keys(result.rows[0]).length).to.be(1); + const resultFromSample = result.rows[0]['col-1-1']; // check that sampler bucket doesn't produce columns + expect(typeof resultFromSample).to.be('number'); + expect(resultFromSample).to.greaterThan(0); // can't check exact metric using sample + }); + + it('can execute aggSampler with custom shard_size and max_docs_per_value', async () => { + const expression = ` + kibana_context timeRange={timerange from='${timeRange.from}' to='${timeRange.to}'} + | esaggs index={indexPatternLoad id='logstash-*'} + aggs={aggDiversifiedSampler id="0" enabled=true schema="bucket" field="extension.raw" shard_size=20 max_docs_per_value=3} + aggs={aggAvg id="1" enabled=true schema="metric" field="bytes"} + `; + const result = await expectExpression('sampler', expression).getResponse(); + + expect(result.columns.length).to.be(2); + const samplerColumn = result.columns[0]; + expect(samplerColumn.name).to.be('diversified_sampler'); + expect(samplerColumn.meta.sourceParams.params).to.eql({ + field: 'extension.raw', + max_docs_per_value: 3, + shard_size: 20, + }); + + expect(result.rows.length).to.be(1); + expect(Object.keys(result.rows[0]).length).to.be(1); // check that sampler bucket doesn't produce columns + const resultFromSample = result.rows[0]['col-1-1']; + expect(typeof resultFromSample).to.be('number'); + expect(resultFromSample).to.greaterThan(0); // can't check exact metric using sample + }); + }); + }); +} diff --git a/test/interpreter_functional/test_suites/run_pipeline/index.ts b/test/interpreter_functional/test_suites/run_pipeline/index.ts index 32f59fcf3df9c..fe2ccce23d94a 100644 --- a/test/interpreter_functional/test_suites/run_pipeline/index.ts +++ b/test/interpreter_functional/test_suites/run_pipeline/index.ts @@ -44,5 +44,6 @@ export default function ({ getService, getPageObjects, loadTestFile }: FtrProvid loadTestFile(require.resolve('./esaggs')); loadTestFile(require.resolve('./esaggs_timeshift')); loadTestFile(require.resolve('./esaggs_multiterms')); + loadTestFile(require.resolve('./esaggs_sampler')); }); } diff --git a/x-pack/examples/alerting_example/public/components/view_alert.tsx b/x-pack/examples/alerting_example/public/components/view_alert.tsx index 0269654806c51..735dcd0899814 100644 --- a/x-pack/examples/alerting_example/public/components/view_alert.tsx +++ b/x-pack/examples/alerting_example/public/components/view_alert.tsx @@ -24,7 +24,7 @@ import { isEmpty } from 'lodash'; import { ALERTING_EXAMPLE_APP_ID } from '../../common/constants'; import { Alert, - AlertTaskState, + RuleTaskState, LEGACY_BASE_ALERT_API_PATH, } from '../../../../plugins/alerting/common'; @@ -34,7 +34,7 @@ type Props = RouteComponentProps & { }; export const ViewAlertPage = withRouter(({ http, id }: Props) => { const [alert, setAlert] = useState(null); - const [alertState, setAlertState] = useState(null); + const [alertState, setAlertState] = useState(null); useEffect(() => { if (!alert) { @@ -42,7 +42,7 @@ export const ViewAlertPage = withRouter(({ http, id }: Props) => { } if (!alertState) { http - .get(`${LEGACY_BASE_ALERT_API_PATH}/alert/${id}/state`) + .get(`${LEGACY_BASE_ALERT_API_PATH}/alert/${id}/state`) .then(setAlertState); } }, [alert, alertState, http, id]); diff --git a/x-pack/examples/alerting_example/public/components/view_astros_alert.tsx b/x-pack/examples/alerting_example/public/components/view_astros_alert.tsx index 44ca8f624c197..282256601547d 100644 --- a/x-pack/examples/alerting_example/public/components/view_astros_alert.tsx +++ b/x-pack/examples/alerting_example/public/components/view_astros_alert.tsx @@ -26,7 +26,7 @@ import { isEmpty } from 'lodash'; import { ALERTING_EXAMPLE_APP_ID, AlwaysFiringParams } from '../../common/constants'; import { Alert, - AlertTaskState, + RuleTaskState, LEGACY_BASE_ALERT_API_PATH, } from '../../../../plugins/alerting/common'; @@ -40,7 +40,7 @@ function hasCraft(state: any): state is { craft: string } { } export const ViewPeopleInSpaceAlertPage = withRouter(({ http, id }: Props) => { const [alert, setAlert] = useState | null>(null); - const [alertState, setAlertState] = useState(null); + const [alertState, setAlertState] = useState(null); useEffect(() => { if (!alert) { @@ -50,7 +50,7 @@ export const ViewPeopleInSpaceAlertPage = withRouter(({ http, id }: Props) => { } if (!alertState) { http - .get(`${LEGACY_BASE_ALERT_API_PATH}/alert/${id}/state`) + .get(`${LEGACY_BASE_ALERT_API_PATH}/alert/${id}/state`) .then(setAlertState); } }, [alert, alertState, http, id]); diff --git a/x-pack/examples/alerting_example/server/alert_types/always_firing.ts b/x-pack/examples/alerting_example/server/alert_types/always_firing.ts index 974fb8bf35ae0..dc89a473a38ab 100644 --- a/x-pack/examples/alerting_example/server/alert_types/always_firing.ts +++ b/x-pack/examples/alerting_example/server/alert_types/always_firing.ts @@ -7,7 +7,7 @@ import uuid from 'uuid'; import { range } from 'lodash'; -import { AlertType } from '../../../../plugins/alerting/server'; +import { RuleType } from '../../../../plugins/alerting/server'; import { DEFAULT_INSTANCES_TO_GENERATE, ALERTING_EXAMPLE_APP_ID, @@ -37,7 +37,7 @@ function getTShirtSizeByIdAndThreshold( return DEFAULT_ACTION_GROUP; } -export const alertType: AlertType< +export const alertType: RuleType< AlwaysFiringParams, never, { count?: number }, diff --git a/x-pack/examples/alerting_example/server/alert_types/astros.ts b/x-pack/examples/alerting_example/server/alert_types/astros.ts index 93bdeb2eada9c..c5d4af6872c83 100644 --- a/x-pack/examples/alerting_example/server/alert_types/astros.ts +++ b/x-pack/examples/alerting_example/server/alert_types/astros.ts @@ -6,7 +6,7 @@ */ import axios from 'axios'; -import { AlertType } from '../../../../plugins/alerting/server'; +import { RuleType } from '../../../../plugins/alerting/server'; import { Operator, Craft, ALERTING_EXAMPLE_APP_ID } from '../../common/constants'; interface PeopleInSpace { @@ -39,7 +39,7 @@ function getCraftFilter(craft: string) { craft === Craft.OuterSpace ? true : craft === person.craft; } -export const alertType: AlertType< +export const alertType: RuleType< { outerSpaceCapacity: number; craft: string; op: string }, never, { peopleInSpace: number }, diff --git a/x-pack/plugins/actions/server/actions_client.test.ts b/x-pack/plugins/actions/server/actions_client.test.ts index 5f6260eb2451c..868b8be7a041c 100644 --- a/x-pack/plugins/actions/server/actions_client.test.ts +++ b/x-pack/plugins/actions/server/actions_client.test.ts @@ -18,7 +18,7 @@ import { actionsConfigMock } from './actions_config.mock'; import { getActionsConfigurationUtilities } from './actions_config'; import { licenseStateMock } from './lib/license_state.mock'; import { licensingMock } from '../../licensing/server/mocks'; -import { httpServerMock } from '../../../../src/core/server/mocks'; +import { httpServerMock, loggingSystemMock } from '../../../../src/core/server/mocks'; import { auditServiceMock } from '../../security/server/audit/index.mock'; import { usageCountersServiceMock } from 'src/plugins/usage_collection/server/usage_counters/usage_counters_service.mock'; @@ -37,6 +37,10 @@ import { actionsAuthorizationMock } from './authorization/actions_authorization. import { trackLegacyRBACExemption } from './lib/track_legacy_rbac_exemption'; // eslint-disable-next-line @kbn/eslint/no-restricted-paths import { elasticsearchClientMock } from '../../../../src/core/server/elasticsearch/client/mocks'; +import { ConnectorTokenClient } from './builtin_action_types/lib/connector_token_client'; +import { encryptedSavedObjectsMock } from '../../encrypted_saved_objects/server/mocks'; +import { Logger } from 'kibana/server'; +import { connectorTokenClientMock } from './builtin_action_types/lib/connector_token_client.mock'; jest.mock('../../../../src/core/server/saved_objects/service/lib/utils', () => ({ SavedObjectsUtils: { @@ -71,7 +75,7 @@ const request = httpServerMock.createKibanaRequest(); const auditLogger = auditServiceMock.create().asScoped(request); const mockUsageCountersSetup = usageCountersServiceMock.createSetupContract(); const mockUsageCounter = mockUsageCountersSetup.createUsageCounter('test'); - +const logger = loggingSystemMock.create().get() as jest.Mocked; const mockTaskManager = taskManagerMock.createSetup(); let actionsClient: ActionsClient; @@ -82,6 +86,8 @@ const executor: ExecutorType<{}, {}, {}, void> = async (options) => { return { status: 'ok', actionId: options.actionId }; }; +const connectorTokenClient = connectorTokenClientMock.create(); + beforeEach(() => { jest.resetAllMocks(); mockedLicenseState = licenseStateMock.create(); @@ -107,6 +113,7 @@ beforeEach(() => { authorization: authorization as unknown as ActionsAuthorization, auditLogger, usageCounter: mockUsageCounter, + connectorTokenClient, }); }); @@ -512,6 +519,7 @@ describe('create()', () => { ephemeralExecutionEnqueuer, request, authorization: authorization as unknown as ActionsAuthorization, + connectorTokenClient: connectorTokenClientMock.create(), }); const savedObjectCreateResult = { @@ -627,6 +635,7 @@ describe('get()', () => { }, }, ], + connectorTokenClient: connectorTokenClientMock.create(), }); await actionsClient.get({ id: 'testPreconfigured' }); @@ -683,6 +692,7 @@ describe('get()', () => { }, }, ], + connectorTokenClient: connectorTokenClientMock.create(), }); authorization.ensureAuthorized.mockRejectedValue( @@ -800,6 +810,7 @@ describe('get()', () => { }, }, ], + connectorTokenClient: connectorTokenClientMock.create(), }); const result = await actionsClient.get({ id: 'testPreconfigured' }); @@ -868,6 +879,7 @@ describe('getAll()', () => { }, }, ], + connectorTokenClient: connectorTokenClientMock.create(), }); return actionsClient.getAll(); } @@ -1006,6 +1018,7 @@ describe('getAll()', () => { }, }, ], + connectorTokenClient: connectorTokenClientMock.create(), }); const result = await actionsClient.getAll(); expect(result).toEqual([ @@ -1082,6 +1095,7 @@ describe('getBulk()', () => { }, }, ], + connectorTokenClient: connectorTokenClientMock.create(), }); return actionsClient.getBulk(['1', 'testPreconfigured']); } @@ -1214,6 +1228,7 @@ describe('getBulk()', () => { }, }, ], + connectorTokenClient: connectorTokenClientMock.create(), }); const result = await actionsClient.getBulk(['1', 'testPreconfigured']); expect(result).toEqual([ @@ -1246,6 +1261,7 @@ describe('delete()', () => { test('ensures user is authorised to delete actions', async () => { await actionsClient.delete({ id: '1' }); expect(authorization.ensureAuthorized).toHaveBeenCalledWith('delete'); + expect(connectorTokenClient.deleteConnectorTokens).toHaveBeenCalledTimes(1); }); test('throws when user is not authorised to create the type of action', async () => { @@ -1983,6 +1999,11 @@ describe('isPreconfigured()', () => { }, }, ], + connectorTokenClient: new ConnectorTokenClient({ + unsecuredSavedObjectsClient: savedObjectsClientMock.create(), + encryptedSavedObjectsClient: encryptedSavedObjectsMock.createClient(), + logger, + }), }); expect(actionsClient.isPreconfigured('testPreconfigured')).toEqual(true); @@ -2013,6 +2034,11 @@ describe('isPreconfigured()', () => { }, }, ], + connectorTokenClient: new ConnectorTokenClient({ + unsecuredSavedObjectsClient: savedObjectsClientMock.create(), + encryptedSavedObjectsClient: encryptedSavedObjectsMock.createClient(), + logger, + }), }); expect(actionsClient.isPreconfigured(uuid.v4())).toEqual(false); diff --git a/x-pack/plugins/actions/server/actions_client.ts b/x-pack/plugins/actions/server/actions_client.ts index deaa1a79d1640..7d753a9106a1d 100644 --- a/x-pack/plugins/actions/server/actions_client.ts +++ b/x-pack/plugins/actions/server/actions_client.ts @@ -29,6 +29,7 @@ import { RawAction, PreConfiguredAction, ActionTypeExecutorResult, + ConnectorTokenClientContract, } from './types'; import { PreconfiguredActionDisabledModificationError } from './lib/errors/preconfigured_action_disabled_modification'; import { ExecuteOptions } from './lib/action_executor'; @@ -77,6 +78,7 @@ interface ConstructorOptions { authorization: ActionsAuthorization; auditLogger?: AuditLogger; usageCounter?: UsageCounter; + connectorTokenClient: ConnectorTokenClientContract; } export interface UpdateOptions { @@ -97,6 +99,7 @@ export class ActionsClient { private readonly ephemeralExecutionEnqueuer: ExecutionEnqueuer; private readonly auditLogger?: AuditLogger; private readonly usageCounter?: UsageCounter; + private readonly connectorTokenClient: ConnectorTokenClientContract; constructor({ actionTypeRegistry, @@ -111,6 +114,7 @@ export class ActionsClient { authorization, auditLogger, usageCounter, + connectorTokenClient, }: ConstructorOptions) { this.actionTypeRegistry = actionTypeRegistry; this.unsecuredSavedObjectsClient = unsecuredSavedObjectsClient; @@ -124,6 +128,7 @@ export class ActionsClient { this.authorization = authorization; this.auditLogger = auditLogger; this.usageCounter = usageCounter; + this.connectorTokenClient = connectorTokenClient; } /** @@ -475,6 +480,17 @@ export class ActionsClient { }) ); + try { + await this.connectorTokenClient.deleteConnectorTokens({ connectorId: id }); + } catch (error) { + this.auditLogger?.log( + connectorAuditEvent({ + action: ConnectorAuditAction.DELETE, + savedObject: { type: 'action', id }, + error, + }) + ); + } return await this.unsecuredSavedObjectsClient.delete('action', id); } diff --git a/x-pack/plugins/actions/server/builtin_action_types/email.test.ts b/x-pack/plugins/actions/server/builtin_action_types/email.test.ts index 48110e29ff911..456a105a0f081 100644 --- a/x-pack/plugins/actions/server/builtin_action_types/email.test.ts +++ b/x-pack/plugins/actions/server/builtin_action_types/email.test.ts @@ -469,6 +469,7 @@ describe('execute()', () => { "isHostnameAllowed": [MockFunction], "isUriAllowed": [MockFunction], }, + "connectorId": "some-id", "content": Object { "message": "a message to you @@ -531,6 +532,7 @@ describe('execute()', () => { "isHostnameAllowed": [MockFunction], "isUriAllowed": [MockFunction], }, + "connectorId": "some-id", "content": Object { "message": "a message to you @@ -593,6 +595,7 @@ describe('execute()', () => { "isHostnameAllowed": [MockFunction], "isUriAllowed": [MockFunction], }, + "connectorId": "some-id", "content": Object { "message": "a message to you diff --git a/x-pack/plugins/actions/server/builtin_action_types/email.ts b/x-pack/plugins/actions/server/builtin_action_types/email.ts index 624fb2b418f48..ed9509cf98bc6 100644 --- a/x-pack/plugins/actions/server/builtin_action_types/email.ts +++ b/x-pack/plugins/actions/server/builtin_action_types/email.ts @@ -241,6 +241,7 @@ async function executor( const config = execOptions.config; const secrets = execOptions.secrets; const params = execOptions.params; + const connectorTokenClient = execOptions.services.connectorTokenClient; const transport: Transport = {}; @@ -283,6 +284,7 @@ async function executor( }); const sendEmailOptions: SendEmailOptions = { + connectorId: actionId, transport, routing: { from: config.from, @@ -301,7 +303,7 @@ async function executor( let result; try { - result = await sendEmail(logger, sendEmailOptions); + result = await sendEmail(logger, sendEmailOptions, connectorTokenClient); } catch (err) { const message = i18n.translate('xpack.actions.builtin.email.errorSendingErrorMessage', { defaultMessage: 'error sending email', diff --git a/x-pack/plugins/actions/server/builtin_action_types/lib/connector_token_client.mock.ts b/x-pack/plugins/actions/server/builtin_action_types/lib/connector_token_client.mock.ts new file mode 100644 index 0000000000000..71d0a2f4466a4 --- /dev/null +++ b/x-pack/plugins/actions/server/builtin_action_types/lib/connector_token_client.mock.ts @@ -0,0 +1,23 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { PublicMethodsOf } from '@kbn/utility-types'; +import { ConnectorTokenClient } from './connector_token_client'; + +const createConnectorTokenClientMock = () => { + const mocked: jest.Mocked> = { + create: jest.fn(), + get: jest.fn(), + update: jest.fn(), + deleteConnectorTokens: jest.fn(), + }; + return mocked; +}; + +export const connectorTokenClientMock = { + create: createConnectorTokenClientMock, +}; diff --git a/x-pack/plugins/actions/server/builtin_action_types/lib/connector_token_client.test.ts b/x-pack/plugins/actions/server/builtin_action_types/lib/connector_token_client.test.ts new file mode 100644 index 0000000000000..1fa02d172ab36 --- /dev/null +++ b/x-pack/plugins/actions/server/builtin_action_types/lib/connector_token_client.test.ts @@ -0,0 +1,359 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { loggingSystemMock, savedObjectsClientMock } from '../../../../../../src/core/server/mocks'; +import { encryptedSavedObjectsMock } from '../../../../encrypted_saved_objects/server/mocks'; +import { ConnectorTokenClient } from './connector_token_client'; +import { Logger } from '../../../../../../src/core/server'; +import { ConnectorToken } from '../../types'; + +const logger = loggingSystemMock.create().get() as jest.Mocked; +jest.mock('../../../../../../src/core/server/saved_objects/service/lib/utils', () => ({ + SavedObjectsUtils: { + generateId: () => 'mock-saved-object-id', + }, +})); + +const unsecuredSavedObjectsClient = savedObjectsClientMock.create(); +const encryptedSavedObjectsClient = encryptedSavedObjectsMock.createClient(); + +let connectorTokenClient: ConnectorTokenClient; + +beforeEach(() => { + jest.resetAllMocks(); + connectorTokenClient = new ConnectorTokenClient({ + unsecuredSavedObjectsClient, + encryptedSavedObjectsClient, + logger, + }); +}); + +describe('create()', () => { + test('creates connector_token with all given properties', async () => { + const expiresAt = new Date().toISOString(); + const savedObjectCreateResult = { + id: '1', + type: 'connector_token', + attributes: { + connectorId: '123', + tokenType: 'access_token', + token: 'testtokenvalue', + expiresAt, + }, + references: [], + }; + + unsecuredSavedObjectsClient.create.mockResolvedValueOnce(savedObjectCreateResult); + const result = await connectorTokenClient.create({ + connectorId: '123', + expiresAtMillis: expiresAt, + token: 'testtokenvalue', + }); + expect(result).toEqual({ + connectorId: '123', + tokenType: 'access_token', + token: 'testtokenvalue', + expiresAt, + }); + expect(unsecuredSavedObjectsClient.create).toHaveBeenCalledTimes(1); + expect((unsecuredSavedObjectsClient.create.mock.calls[0][1] as ConnectorToken).token).toBe( + 'testtokenvalue' + ); + }); +}); + +describe('get()', () => { + test('calls unsecuredSavedObjectsClient with parameters', async () => { + const expiresAt = new Date().toISOString(); + const createdAt = new Date().toISOString(); + const expectedResult = { + total: 1, + per_page: 10, + page: 1, + saved_objects: [ + { + id: '1', + type: 'connector_token', + attributes: { + connectorId: '123', + tokenType: 'access_token', + createdAt, + expiresAt, + }, + score: 1, + references: [], + }, + ], + }; + unsecuredSavedObjectsClient.find.mockResolvedValueOnce(expectedResult); + encryptedSavedObjectsClient.getDecryptedAsInternalUser.mockResolvedValueOnce({ + id: '1', + type: 'connector_token', + references: [], + attributes: { + token: 'testtokenvalue', + }, + }); + const result = await connectorTokenClient.get({ + connectorId: '123', + tokenType: 'access_token', + }); + expect(result).toEqual({ + hasErrors: false, + connectorToken: { + id: '1', + connectorId: '123', + tokenType: 'access_token', + token: 'testtokenvalue', + createdAt, + expiresAt, + }, + }); + }); + + test('return null if there is not tokens for connectorId', async () => { + const expectedResult = { + total: 0, + per_page: 10, + page: 1, + saved_objects: [], + }; + unsecuredSavedObjectsClient.find.mockResolvedValueOnce(expectedResult); + + const result = await connectorTokenClient.get({ + connectorId: '123', + tokenType: 'access_token', + }); + expect(result).toEqual({ connectorToken: null, hasErrors: false }); + }); + + test('return null and log the error if unsecuredSavedObjectsClient thows an error', async () => { + unsecuredSavedObjectsClient.find.mockRejectedValueOnce(new Error('Fail')); + + const result = await connectorTokenClient.get({ + connectorId: '123', + tokenType: 'access_token', + }); + + expect(logger.error.mock.calls[0]).toMatchObject([ + `Failed to fetch connector_token for connectorId "123" and tokenType: "access_token". Error: Fail`, + ]); + expect(result).toEqual({ connectorToken: null, hasErrors: true }); + }); + + test('return null and log the error if encryptedSavedObjectsClient decrypt method thows an error', async () => { + const expectedResult = { + total: 1, + per_page: 10, + page: 1, + saved_objects: [ + { + id: '1', + type: 'connector_token', + attributes: { + connectorId: '123', + tokenType: 'access_token', + createdAt: new Date().toISOString(), + expiresAt: new Date().toISOString(), + }, + score: 1, + references: [], + }, + ], + }; + unsecuredSavedObjectsClient.find.mockResolvedValueOnce(expectedResult); + encryptedSavedObjectsClient.getDecryptedAsInternalUser.mockRejectedValueOnce(new Error('Fail')); + + const result = await connectorTokenClient.get({ + connectorId: '123', + tokenType: 'access_token', + }); + + expect(logger.error.mock.calls[0]).toMatchObject([ + `Failed to decrypt connector_token for connectorId "123" and tokenType: "access_token". Error: Fail`, + ]); + expect(result).toEqual({ connectorToken: null, hasErrors: true }); + }); +}); + +describe('update()', () => { + test('updates the connector token with all given properties', async () => { + const expiresAt = new Date().toISOString(); + + unsecuredSavedObjectsClient.get.mockResolvedValueOnce({ + id: '1', + type: 'connector_token', + attributes: { + connectorId: '123', + tokenType: 'access_token', + token: 'testtokenvalue', + createdAt: new Date().toISOString(), + }, + references: [], + }); + unsecuredSavedObjectsClient.create.mockResolvedValueOnce({ + id: '1', + type: 'connector_token', + attributes: { + connectorId: '123', + tokenType: 'access_token', + token: 'testtokenvalue', + expiresAt, + }, + references: [], + }); + unsecuredSavedObjectsClient.checkConflicts.mockResolvedValueOnce({ + errors: [], + }); + const result = await connectorTokenClient.update({ + id: '1', + tokenType: 'access_token', + token: 'testtokenvalue', + expiresAtMillis: expiresAt, + }); + expect(result).toEqual({ + connectorId: '123', + tokenType: 'access_token', + token: 'testtokenvalue', + expiresAt, + }); + expect(unsecuredSavedObjectsClient.create).toHaveBeenCalledTimes(1); + expect((unsecuredSavedObjectsClient.create.mock.calls[0][1] as ConnectorToken).token).toBe( + 'testtokenvalue' + ); + expect(unsecuredSavedObjectsClient.get).toHaveBeenCalledTimes(1); + expect(unsecuredSavedObjectsClient.get.mock.calls[0]).toMatchInlineSnapshot(` + Array [ + "connector_token", + "1", + ] + `); + }); + + test('should log error, when failed to update the connector token if there are a conflict errors', async () => { + const expiresAt = new Date().toISOString(); + + unsecuredSavedObjectsClient.get.mockResolvedValueOnce({ + id: '1', + type: 'connector_token', + attributes: { + connectorId: '123', + tokenType: 'access_token', + token: 'testtokenvalue', + createdAt: new Date().toISOString(), + }, + references: [], + }); + unsecuredSavedObjectsClient.checkConflicts.mockResolvedValueOnce({ + errors: [ + { + id: '1', + error: { + error: 'error', + statusCode: 503, + message: 'There is a conflict.', + }, + type: 'conflict', + }, + ], + }); + + const result = await connectorTokenClient.update({ + id: '1', + tokenType: 'access_token', + token: 'testtokenvalue', + expiresAtMillis: expiresAt, + }); + expect(result).toEqual(null); + expect(unsecuredSavedObjectsClient.create).toHaveBeenCalledTimes(0); + expect(logger.error.mock.calls[0]).toMatchObject([ + 'Failed to update connector_token for id "1" and tokenType: "access_token". Error: There is a conflict. ', + ]); + }); + + test('throws an error when unsecuredSavedObjectsClient throws', async () => { + const expiresAt = new Date().toISOString(); + unsecuredSavedObjectsClient.get.mockResolvedValueOnce({ + id: '1', + type: 'connector_token', + attributes: { + connectorId: '123', + tokenType: 'access_token', + token: 'testtokenvalue', + createdAt: new Date().toISOString(), + }, + references: [], + }); + unsecuredSavedObjectsClient.checkConflicts.mockResolvedValueOnce({ + errors: [], + }); + unsecuredSavedObjectsClient.create.mockRejectedValueOnce(new Error('Fail')); + await expect( + connectorTokenClient.update({ + id: 'my-action', + tokenType: 'access_token', + token: 'testtokenvalue', + expiresAtMillis: expiresAt, + }) + ).rejects.toThrowErrorMatchingInlineSnapshot(`"Fail"`); + }); +}); + +describe('delete()', () => { + test('calls unsecuredSavedObjectsClient delete for all connector token records by connectorId', async () => { + const expectedResult = Symbol(); + unsecuredSavedObjectsClient.delete.mockResolvedValue(expectedResult); + + const findResult = { + total: 2, + per_page: 10, + page: 1, + saved_objects: [ + { + id: 'token1', + type: 'connector_token', + attributes: { + connectorId: '1', + tokenType: 'access_token', + createdAt: new Date().toISOString(), + expiresAt: new Date().toISOString(), + }, + score: 1, + references: [], + }, + { + id: 'token2', + type: 'connector_token', + attributes: { + connectorId: '1', + tokenType: 'refresh_token', + createdAt: new Date().toISOString(), + expiresAt: new Date().toISOString(), + }, + score: 1, + references: [], + }, + ], + }; + unsecuredSavedObjectsClient.find.mockResolvedValueOnce(findResult); + const result = await connectorTokenClient.deleteConnectorTokens({ connectorId: '1' }); + expect(JSON.stringify(result)).toEqual(JSON.stringify([Symbol(), Symbol()])); + expect(unsecuredSavedObjectsClient.delete).toHaveBeenCalledTimes(2); + expect(unsecuredSavedObjectsClient.delete.mock.calls[0]).toMatchInlineSnapshot(` + Array [ + "connector_token", + "token1", + ] + `); + expect(unsecuredSavedObjectsClient.delete.mock.calls[1]).toMatchInlineSnapshot(` + Array [ + "connector_token", + "token2", + ] + `); + }); +}); diff --git a/x-pack/plugins/actions/server/builtin_action_types/lib/connector_token_client.ts b/x-pack/plugins/actions/server/builtin_action_types/lib/connector_token_client.ts new file mode 100644 index 0000000000000..b5a91d6e3db69 --- /dev/null +++ b/x-pack/plugins/actions/server/builtin_action_types/lib/connector_token_client.ts @@ -0,0 +1,252 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { omitBy, isUndefined } from 'lodash'; +import { ConnectorToken } from '../../types'; +import { EncryptedSavedObjectsClient } from '../../../../encrypted_saved_objects/server'; +import { + Logger, + SavedObjectsClientContract, + SavedObjectsUtils, +} from '../../../../../../src/core/server'; +import { CONNECTOR_TOKEN_SAVED_OBJECT_TYPE } from '../../constants/saved_objects'; + +export const MAX_TOKENS_RETURNED = 1; + +interface ConstructorOptions { + encryptedSavedObjectsClient: EncryptedSavedObjectsClient; + unsecuredSavedObjectsClient: SavedObjectsClientContract; + logger: Logger; +} + +interface CreateOptions { + connectorId: string; + token: string; + expiresAtMillis: string; + tokenType?: string; +} + +export interface UpdateOptions { + id: string; + token: string; + expiresAtMillis: string; + tokenType?: string; +} + +export class ConnectorTokenClient { + private readonly logger: Logger; + private readonly unsecuredSavedObjectsClient: SavedObjectsClientContract; + private readonly encryptedSavedObjectsClient: EncryptedSavedObjectsClient; + + constructor({ + unsecuredSavedObjectsClient, + encryptedSavedObjectsClient, + logger, + }: ConstructorOptions) { + this.encryptedSavedObjectsClient = encryptedSavedObjectsClient; + this.unsecuredSavedObjectsClient = unsecuredSavedObjectsClient; + this.logger = logger; + } + + /** + * Create new token for connector + */ + public async create({ + connectorId, + token, + expiresAtMillis, + tokenType, + }: CreateOptions): Promise { + const id = SavedObjectsUtils.generateId(); + const createTime = Date.now(); + try { + const result = await this.unsecuredSavedObjectsClient.create( + CONNECTOR_TOKEN_SAVED_OBJECT_TYPE, + { + connectorId, + token, + expiresAt: expiresAtMillis, + tokenType: tokenType ?? 'access_token', + createdAt: new Date(createTime).toISOString(), + updatedAt: new Date(createTime).toISOString(), + }, + { id } + ); + + return result.attributes as ConnectorToken; + } catch (err) { + this.logger.error( + `Failed to create connector_token for connectorId "${connectorId}" and tokenType: "${ + tokenType ?? 'access_token' + }". Error: ${err.message}` + ); + throw err; + } + } + + /** + * Update connector token + */ + public async update({ + id, + token, + expiresAtMillis, + tokenType, + }: UpdateOptions): Promise { + const { attributes, references, version } = + await this.unsecuredSavedObjectsClient.get( + CONNECTOR_TOKEN_SAVED_OBJECT_TYPE, + id + ); + const createTime = Date.now(); + const conflicts = await this.unsecuredSavedObjectsClient.checkConflicts([ + { id, type: 'connector_token' }, + ]); + try { + if (conflicts.errors.length > 0) { + this.logger.error( + `Failed to update connector_token for id "${id}" and tokenType: "${ + tokenType ?? 'access_token' + }". ${conflicts.errors.reduce( + (messages, errorObj) => `Error: ${errorObj.error.message} ${messages}`, + '' + )}` + ); + return null; + } else { + const result = await this.unsecuredSavedObjectsClient.create( + CONNECTOR_TOKEN_SAVED_OBJECT_TYPE, + { + ...attributes, + token, + expiresAt: expiresAtMillis, + tokenType: tokenType ?? 'access_token', + updatedAt: new Date(createTime).toISOString(), + }, + omitBy( + { + id, + overwrite: true, + references, + version, + }, + isUndefined + ) + ); + return result.attributes as ConnectorToken; + } + } catch (err) { + this.logger.error( + `Failed to update connector_token for id "${id}" and tokenType: "${ + tokenType ?? 'access_token' + }". Error: ${err.message}` + ); + throw err; + } + } + + /** + * Get connector token + */ + public async get({ + connectorId, + tokenType, + }: { + connectorId: string; + tokenType?: string; + }): Promise<{ + hasErrors: boolean; + connectorToken: ConnectorToken | null; + }> { + const connectorTokensResult = []; + const tokenTypeFilter = tokenType + ? ` AND ${CONNECTOR_TOKEN_SAVED_OBJECT_TYPE}.attributes.tokenType: "${tokenType}"` + : ''; + + try { + connectorTokensResult.push( + ...( + await this.unsecuredSavedObjectsClient.find({ + perPage: MAX_TOKENS_RETURNED, + type: CONNECTOR_TOKEN_SAVED_OBJECT_TYPE, + filter: `${CONNECTOR_TOKEN_SAVED_OBJECT_TYPE}.attributes.connectorId: "${connectorId}"${tokenTypeFilter}`, + sortField: 'updatedAt', + sortOrder: 'desc', + }) + ).saved_objects + ); + } catch (err) { + this.logger.error( + `Failed to fetch connector_token for connectorId "${connectorId}" and tokenType: "${ + tokenType ?? 'access_token' + }". Error: ${err.message}` + ); + return { hasErrors: true, connectorToken: null }; + } + + if (connectorTokensResult.length === 0) { + return { hasErrors: false, connectorToken: null }; + } + + try { + const { + attributes: { token }, + } = await this.encryptedSavedObjectsClient.getDecryptedAsInternalUser( + CONNECTOR_TOKEN_SAVED_OBJECT_TYPE, + connectorTokensResult[0].id + ); + + return { + hasErrors: false, + connectorToken: { + id: connectorTokensResult[0].id, + ...connectorTokensResult[0].attributes, + token, + }, + }; + } catch (err) { + this.logger.error( + `Failed to decrypt connector_token for connectorId "${connectorId}" and tokenType: "${ + tokenType ?? 'access_token' + }". Error: ${err.message}` + ); + return { hasErrors: true, connectorToken: null }; + } + } + + /** + * Delete all connector tokens + */ + public async deleteConnectorTokens({ + connectorId, + tokenType, + }: { + connectorId: string; + tokenType?: string; + }) { + const tokenTypeFilter = tokenType + ? ` AND ${CONNECTOR_TOKEN_SAVED_OBJECT_TYPE}.attributes.tokenType: "${tokenType}"` + : ''; + try { + const result = await this.unsecuredSavedObjectsClient.find({ + type: CONNECTOR_TOKEN_SAVED_OBJECT_TYPE, + filter: `${CONNECTOR_TOKEN_SAVED_OBJECT_TYPE}.attributes.connectorId: "${connectorId}"${tokenTypeFilter}`, + }); + return Promise.all( + result.saved_objects.map( + async (obj) => + await this.unsecuredSavedObjectsClient.delete(CONNECTOR_TOKEN_SAVED_OBJECT_TYPE, obj.id) + ) + ); + } catch (err) { + this.logger.error( + `Failed to delete connector_token records for connectorId "${connectorId}". Error: ${err.message}` + ); + throw err; + } + } +} diff --git a/x-pack/plugins/actions/server/builtin_action_types/lib/send_email.test.ts b/x-pack/plugins/actions/server/builtin_action_types/lib/send_email.test.ts index 3efc33c339de5..c3fc1c8128ffc 100644 --- a/x-pack/plugins/actions/server/builtin_action_types/lib/send_email.test.ts +++ b/x-pack/plugins/actions/server/builtin_action_types/lib/send_email.test.ts @@ -18,18 +18,29 @@ jest.mock('./request_oauth_client_credentials_token', () => ({ import { Logger } from '../../../../../../src/core/server'; import { sendEmail } from './send_email'; -import { loggingSystemMock } from '../../../../../../src/core/server/mocks'; +import { loggingSystemMock, savedObjectsClientMock } from '../../../../../../src/core/server/mocks'; import nodemailer from 'nodemailer'; import { ProxySettings } from '../../types'; import { actionsConfigMock } from '../../actions_config.mock'; import { CustomHostSettings } from '../../config'; import { sendEmailGraphApi } from './send_email_graph_api'; import { requestOAuthClientCredentialsToken } from './request_oauth_client_credentials_token'; +import { ConnectorTokenClient } from './connector_token_client'; +import { encryptedSavedObjectsMock } from '../../../../encrypted_saved_objects/server/mocks'; +import { connectorTokenClientMock } from './connector_token_client.mock'; const createTransportMock = nodemailer.createTransport as jest.Mock; const sendMailMockResult = { result: 'does not matter' }; const sendMailMock = jest.fn(); const mockLogger = loggingSystemMock.create().get() as jest.Mocked; +const unsecuredSavedObjectsClient = savedObjectsClientMock.create(); +const encryptedSavedObjectsClient = encryptedSavedObjectsMock.createClient(); + +const connectorTokenClient = new ConnectorTokenClient({ + unsecuredSavedObjectsClient, + encryptedSavedObjectsClient, + logger: mockLogger, +}); describe('send_email module', () => { beforeEach(() => { @@ -40,7 +51,7 @@ describe('send_email module', () => { test('handles authenticated email using service', async () => { const sendEmailOptions = getSendEmailOptions({ transport: { service: 'other' } }); - const result = await sendEmail(mockLogger, sendEmailOptions); + const result = await sendEmail(mockLogger, sendEmailOptions, connectorTokenClient); expect(result).toBe(sendMailMockResult); expect(createTransportMock.mock.calls[0]).toMatchInlineSnapshot(` Array [ @@ -90,11 +101,22 @@ describe('send_email module', () => { }, }); requestOAuthClientCredentialsTokenMock.mockReturnValueOnce({ - status: 200, - data: { - tokenType: 'Bearer', - accessToken: 'dfjsdfgdjhfgsjdf', - expiresIn: 123, + tokenType: 'Bearer', + accessToken: 'dfjsdfgdjhfgsjdf', + expiresIn: 123, + }); + const date = new Date(); + date.setDate(date.getDate() + 5); + + unsecuredSavedObjectsClient.create.mockResolvedValueOnce({ + id: '1', + type: 'connector_token', + references: [], + attributes: { + connectorId: '123', + expiresAt: date.toISOString(), + tokenType: 'access_token', + token: '11111111', }, }); @@ -102,7 +124,13 @@ describe('send_email module', () => { status: 202, }); - await sendEmail(mockLogger, sendEmailOptions); + unsecuredSavedObjectsClient.find.mockResolvedValueOnce({ + total: 0, + saved_objects: [], + per_page: 500, + page: 1, + }); + await sendEmail(mockLogger, sendEmailOptions, connectorTokenClient); expect(requestOAuthClientCredentialsTokenMock.mock.calls[0]).toMatchInlineSnapshot(` Array [ "https://login.microsoftonline.com/undefined/oauth2/v2.0/token", @@ -153,7 +181,162 @@ describe('send_email module', () => { Object { "graphApiUrl": undefined, "headers": Object { - "Authorization": "undefined undefined", + "Authorization": "Bearer dfjsdfgdjhfgsjdf", + "Content-Type": "application/json", + }, + "messageHTML": "

a message

+ ", + "options": Object { + "configurationUtilities": Object { + "ensureActionTypeEnabled": [MockFunction], + "ensureHostnameAllowed": [MockFunction], + "ensureUriAllowed": [MockFunction], + "getCustomHostSettings": [MockFunction], + "getMicrosoftGraphApiUrl": [MockFunction] { + "calls": Array [ + Array [], + ], + "results": Array [ + Object { + "type": "return", + "value": undefined, + }, + ], + }, + "getProxySettings": [MockFunction], + "getResponseSettings": [MockFunction], + "getSSLSettings": [MockFunction], + "isActionTypeEnabled": [MockFunction], + "isHostnameAllowed": [MockFunction], + "isUriAllowed": [MockFunction], + }, + "connectorId": "1", + "content": Object { + "message": "a message", + "subject": "a subject", + }, + "hasAuth": true, + "routing": Object { + "bcc": Array [], + "cc": Array [ + "bob@example.com", + "robert@example.com", + ], + "from": "fred@example.com", + "to": Array [ + "jim@example.com", + ], + }, + "transport": Object { + "clientId": "123456", + "clientSecret": "sdfhkdsjhfksdjfh", + "password": "changeme", + "service": "exchange_server", + "user": "elastic", + }, + }, + }, + Object { + "context": Array [], + "debug": [MockFunction], + "error": [MockFunction], + "fatal": [MockFunction], + "get": [MockFunction], + "info": [MockFunction], + "log": [MockFunction], + "trace": [MockFunction], + "warn": [MockFunction], + }, + Object { + "ensureActionTypeEnabled": [MockFunction], + "ensureHostnameAllowed": [MockFunction], + "ensureUriAllowed": [MockFunction], + "getCustomHostSettings": [MockFunction], + "getMicrosoftGraphApiUrl": [MockFunction] { + "calls": Array [ + Array [], + ], + "results": Array [ + Object { + "type": "return", + "value": undefined, + }, + ], + }, + "getProxySettings": [MockFunction], + "getResponseSettings": [MockFunction], + "getSSLSettings": [MockFunction], + "isActionTypeEnabled": [MockFunction], + "isHostnameAllowed": [MockFunction], + "isUriAllowed": [MockFunction], + }, + ] + `); + + expect(unsecuredSavedObjectsClient.create.mock.calls.length).toBe(1); + }); + + test('uses existing "access_token" from "connector_token" SO for authentication for email using "exchange_server" service', async () => { + const sendEmailGraphApiMock = sendEmailGraphApi as jest.Mock; + const requestOAuthClientCredentialsTokenMock = requestOAuthClientCredentialsToken as jest.Mock; + const sendEmailOptions = getSendEmailOptions({ + transport: { + service: 'exchange_server', + clientId: '123456', + clientSecret: 'sdfhkdsjhfksdjfh', + }, + }); + requestOAuthClientCredentialsTokenMock.mockReturnValueOnce({ + tokenType: 'Bearer', + accessToken: 'dfjsdfgdjhfgsjdf', + expiresIn: 123, + }); + + sendEmailGraphApiMock.mockReturnValue({ + status: 202, + }); + const date = new Date(); + date.setDate(date.getDate() + 5); + + unsecuredSavedObjectsClient.checkConflicts.mockResolvedValueOnce({ + errors: [], + }); + unsecuredSavedObjectsClient.find.mockResolvedValueOnce({ + total: 2, + saved_objects: [ + { + id: '1', + score: 1, + type: 'connector_token', + references: [], + attributes: { + connectorId: '123', + expiresAt: date.toISOString(), + tokenType: 'access_token', + }, + }, + ], + per_page: 500, + page: 1, + }); + encryptedSavedObjectsClient.getDecryptedAsInternalUser.mockResolvedValueOnce({ + id: '1', + type: 'connector_token', + references: [], + attributes: { + token: '11111111', + }, + }); + + await sendEmail(mockLogger, sendEmailOptions, connectorTokenClient); + expect(requestOAuthClientCredentialsTokenMock.mock.calls.length).toBe(0); + + expect(sendEmailGraphApiMock.mock.calls[0]).toMatchInlineSnapshot(` + Array [ + Object { + "graphApiUrl": undefined, + "headers": Object { + "Authorization": "11111111", "Content-Type": "application/json", }, "messageHTML": "

a message

@@ -182,6 +365,477 @@ describe('send_email module', () => { "isHostnameAllowed": [MockFunction], "isUriAllowed": [MockFunction], }, + "connectorId": "1", + "content": Object { + "message": "a message", + "subject": "a subject", + }, + "hasAuth": true, + "routing": Object { + "bcc": Array [], + "cc": Array [ + "bob@example.com", + "robert@example.com", + ], + "from": "fred@example.com", + "to": Array [ + "jim@example.com", + ], + }, + "transport": Object { + "clientId": "123456", + "clientSecret": "sdfhkdsjhfksdjfh", + "password": "changeme", + "service": "exchange_server", + "user": "elastic", + }, + }, + }, + Object { + "context": Array [], + "debug": [MockFunction], + "error": [MockFunction], + "fatal": [MockFunction], + "get": [MockFunction], + "info": [MockFunction], + "log": [MockFunction], + "trace": [MockFunction], + "warn": [MockFunction], + }, + Object { + "ensureActionTypeEnabled": [MockFunction], + "ensureHostnameAllowed": [MockFunction], + "ensureUriAllowed": [MockFunction], + "getCustomHostSettings": [MockFunction], + "getMicrosoftGraphApiUrl": [MockFunction] { + "calls": Array [ + Array [], + ], + "results": Array [ + Object { + "type": "return", + "value": undefined, + }, + ], + }, + "getProxySettings": [MockFunction], + "getResponseSettings": [MockFunction], + "getSSLSettings": [MockFunction], + "isActionTypeEnabled": [MockFunction], + "isHostnameAllowed": [MockFunction], + "isUriAllowed": [MockFunction], + }, + ] + `); + + expect(unsecuredSavedObjectsClient.create.mock.calls.length).toBe(0); + }); + + test('request the new token and update existing "access_token" when it is expired for "exchange_server" email service', async () => { + const sendEmailGraphApiMock = sendEmailGraphApi as jest.Mock; + const requestOAuthClientCredentialsTokenMock = requestOAuthClientCredentialsToken as jest.Mock; + const sendEmailOptions = getSendEmailOptions({ + transport: { + service: 'exchange_server', + clientId: '123456', + clientSecret: 'sdfhkdsjhfksdjfh', + }, + }); + requestOAuthClientCredentialsTokenMock.mockReturnValueOnce({ + tokenType: 'Bearer', + accessToken: 'dfjsdfgdjhfgsjdf', + expiresIn: 123, + }); + + sendEmailGraphApiMock.mockReturnValue({ + status: 202, + }); + const date = new Date(); + date.setDate(date.getDate() - 5); + + unsecuredSavedObjectsClient.find.mockResolvedValueOnce({ + total: 2, + saved_objects: [ + { + id: '1', + score: 1, + type: 'connector_token', + references: [], + attributes: { + connectorId: '123', + expiresAt: date.toISOString(), + tokenType: 'access_token', + }, + }, + ], + per_page: 500, + page: 1, + }); + encryptedSavedObjectsClient.getDecryptedAsInternalUser.mockResolvedValueOnce({ + id: '1', + type: 'connector_token', + references: [], + attributes: { + token: '11111111', + }, + }); + + unsecuredSavedObjectsClient.get.mockResolvedValueOnce({ + id: '1', + type: 'connector_token', + references: [], + attributes: { + connectorId: '123', + expiresAt: date.toISOString(), + tokenType: 'access_token', + token: '11111111', + }, + }); + unsecuredSavedObjectsClient.checkConflicts.mockResolvedValueOnce({ + errors: [], + }); + + unsecuredSavedObjectsClient.create.mockResolvedValueOnce({ + id: '1', + type: 'connector_token', + references: [], + attributes: { + connectorId: '123', + expiresAt: date.toISOString(), + tokenType: 'access_token', + token: '11111111', + }, + }); + + await sendEmail(mockLogger, sendEmailOptions, connectorTokenClient); + expect(requestOAuthClientCredentialsTokenMock.mock.calls.length).toBe(1); + + expect(sendEmailGraphApiMock.mock.calls[0]).toMatchInlineSnapshot(` + Array [ + Object { + "graphApiUrl": undefined, + "headers": Object { + "Authorization": "Bearer dfjsdfgdjhfgsjdf", + "Content-Type": "application/json", + }, + "messageHTML": "

a message

+ ", + "options": Object { + "configurationUtilities": Object { + "ensureActionTypeEnabled": [MockFunction], + "ensureHostnameAllowed": [MockFunction], + "ensureUriAllowed": [MockFunction], + "getCustomHostSettings": [MockFunction], + "getMicrosoftGraphApiUrl": [MockFunction] { + "calls": Array [ + Array [], + ], + "results": Array [ + Object { + "type": "return", + "value": undefined, + }, + ], + }, + "getProxySettings": [MockFunction], + "getResponseSettings": [MockFunction], + "getSSLSettings": [MockFunction], + "isActionTypeEnabled": [MockFunction], + "isHostnameAllowed": [MockFunction], + "isUriAllowed": [MockFunction], + }, + "connectorId": "1", + "content": Object { + "message": "a message", + "subject": "a subject", + }, + "hasAuth": true, + "routing": Object { + "bcc": Array [], + "cc": Array [ + "bob@example.com", + "robert@example.com", + ], + "from": "fred@example.com", + "to": Array [ + "jim@example.com", + ], + }, + "transport": Object { + "clientId": "123456", + "clientSecret": "sdfhkdsjhfksdjfh", + "password": "changeme", + "service": "exchange_server", + "user": "elastic", + }, + }, + }, + Object { + "context": Array [], + "debug": [MockFunction], + "error": [MockFunction], + "fatal": [MockFunction], + "get": [MockFunction], + "info": [MockFunction], + "log": [MockFunction], + "trace": [MockFunction], + "warn": [MockFunction], + }, + Object { + "ensureActionTypeEnabled": [MockFunction], + "ensureHostnameAllowed": [MockFunction], + "ensureUriAllowed": [MockFunction], + "getCustomHostSettings": [MockFunction], + "getMicrosoftGraphApiUrl": [MockFunction] { + "calls": Array [ + Array [], + ], + "results": Array [ + Object { + "type": "return", + "value": undefined, + }, + ], + }, + "getProxySettings": [MockFunction], + "getResponseSettings": [MockFunction], + "getSSLSettings": [MockFunction], + "isActionTypeEnabled": [MockFunction], + "isHostnameAllowed": [MockFunction], + "isUriAllowed": [MockFunction], + }, + ] + `); + + expect(unsecuredSavedObjectsClient.create.mock.calls.length).toBe(1); + }); + + test('sending email for "exchange_server" wont fail if connectorTokenClient throw the errors, just log warning message', async () => { + const sendEmailGraphApiMock = sendEmailGraphApi as jest.Mock; + const requestOAuthClientCredentialsTokenMock = requestOAuthClientCredentialsToken as jest.Mock; + const sendEmailOptions = getSendEmailOptions({ + transport: { + service: 'exchange_server', + clientId: '123456', + clientSecret: 'sdfhkdsjhfksdjfh', + }, + }); + requestOAuthClientCredentialsTokenMock.mockReturnValueOnce({ + tokenType: 'Bearer', + accessToken: 'dfjsdfgdjhfgsjdf', + expiresIn: 123, + }); + + sendEmailGraphApiMock.mockReturnValue({ + status: 202, + }); + const date = new Date(); + date.setDate(date.getDate() + 5); + + unsecuredSavedObjectsClient.find.mockResolvedValueOnce({ + total: 0, + saved_objects: [], + per_page: 500, + page: 1, + }); + unsecuredSavedObjectsClient.create.mockRejectedValueOnce(new Error('Fail')); + + await sendEmail(mockLogger, sendEmailOptions, connectorTokenClient); + expect(requestOAuthClientCredentialsTokenMock.mock.calls.length).toBe(1); + expect(unsecuredSavedObjectsClient.create.mock.calls.length).toBe(1); + expect(mockLogger.warn.mock.calls[0]).toMatchObject([ + `Not able to update connector token for connectorId: 1 due to error: Fail`, + ]); + + expect(sendEmailGraphApiMock.mock.calls[0]).toMatchInlineSnapshot(` + Array [ + Object { + "graphApiUrl": undefined, + "headers": Object { + "Authorization": "Bearer dfjsdfgdjhfgsjdf", + "Content-Type": "application/json", + }, + "messageHTML": "

a message

+ ", + "options": Object { + "configurationUtilities": Object { + "ensureActionTypeEnabled": [MockFunction], + "ensureHostnameAllowed": [MockFunction], + "ensureUriAllowed": [MockFunction], + "getCustomHostSettings": [MockFunction], + "getMicrosoftGraphApiUrl": [MockFunction] { + "calls": Array [ + Array [], + ], + "results": Array [ + Object { + "type": "return", + "value": undefined, + }, + ], + }, + "getProxySettings": [MockFunction], + "getResponseSettings": [MockFunction], + "getSSLSettings": [MockFunction], + "isActionTypeEnabled": [MockFunction], + "isHostnameAllowed": [MockFunction], + "isUriAllowed": [MockFunction], + }, + "connectorId": "1", + "content": Object { + "message": "a message", + "subject": "a subject", + }, + "hasAuth": true, + "routing": Object { + "bcc": Array [], + "cc": Array [ + "bob@example.com", + "robert@example.com", + ], + "from": "fred@example.com", + "to": Array [ + "jim@example.com", + ], + }, + "transport": Object { + "clientId": "123456", + "clientSecret": "sdfhkdsjhfksdjfh", + "password": "changeme", + "service": "exchange_server", + "user": "elastic", + }, + }, + }, + Object { + "context": Array [], + "debug": [MockFunction], + "error": [MockFunction] { + "calls": Array [ + Array [ + "Failed to create connector_token for connectorId \\"1\\" and tokenType: \\"access_token\\". Error: Fail", + ], + ], + "results": Array [ + Object { + "type": "return", + "value": undefined, + }, + ], + }, + "fatal": [MockFunction], + "get": [MockFunction], + "info": [MockFunction], + "log": [MockFunction], + "trace": [MockFunction], + "warn": [MockFunction] { + "calls": Array [ + Array [ + "Not able to update connector token for connectorId: 1 due to error: Fail", + ], + ], + "results": Array [ + Object { + "type": "return", + "value": undefined, + }, + ], + }, + }, + Object { + "ensureActionTypeEnabled": [MockFunction], + "ensureHostnameAllowed": [MockFunction], + "ensureUriAllowed": [MockFunction], + "getCustomHostSettings": [MockFunction], + "getMicrosoftGraphApiUrl": [MockFunction] { + "calls": Array [ + Array [], + ], + "results": Array [ + Object { + "type": "return", + "value": undefined, + }, + ], + }, + "getProxySettings": [MockFunction], + "getResponseSettings": [MockFunction], + "getSSLSettings": [MockFunction], + "isActionTypeEnabled": [MockFunction], + "isHostnameAllowed": [MockFunction], + "isUriAllowed": [MockFunction], + }, + ] + `); + }); + + test('delete duplication tokens if connectorTokenClient get method has the errors, like decription error', async () => { + const sendEmailGraphApiMock = sendEmailGraphApi as jest.Mock; + const requestOAuthClientCredentialsTokenMock = requestOAuthClientCredentialsToken as jest.Mock; + const sendEmailOptions = getSendEmailOptions({ + transport: { + service: 'exchange_server', + clientId: '123456', + clientSecret: 'sdfhkdsjhfksdjfh', + }, + }); + requestOAuthClientCredentialsTokenMock.mockReturnValueOnce({ + tokenType: 'Bearer', + accessToken: 'dfjsdfgdjhfgsjdf', + expiresIn: 123, + }); + + sendEmailGraphApiMock.mockReturnValue({ + status: 202, + }); + const date = new Date(); + date.setDate(date.getDate() + 5); + + const connectorTokenClientM = connectorTokenClientMock.create(); + connectorTokenClientM.get.mockResolvedValueOnce({ + hasErrors: true, + connectorToken: null, + }); + + await sendEmail(mockLogger, sendEmailOptions, connectorTokenClientM); + expect(requestOAuthClientCredentialsTokenMock.mock.calls.length).toBe(1); + expect(connectorTokenClientM.deleteConnectorTokens.mock.calls.length).toBe(1); + + expect(sendEmailGraphApiMock.mock.calls[0]).toMatchInlineSnapshot(` + Array [ + Object { + "graphApiUrl": undefined, + "headers": Object { + "Authorization": "Bearer dfjsdfgdjhfgsjdf", + "Content-Type": "application/json", + }, + "messageHTML": "

a message

+ ", + "options": Object { + "configurationUtilities": Object { + "ensureActionTypeEnabled": [MockFunction], + "ensureHostnameAllowed": [MockFunction], + "ensureUriAllowed": [MockFunction], + "getCustomHostSettings": [MockFunction], + "getMicrosoftGraphApiUrl": [MockFunction] { + "calls": Array [ + Array [], + ], + "results": Array [ + Object { + "type": "return", + "value": undefined, + }, + ], + }, + "getProxySettings": [MockFunction], + "getResponseSettings": [MockFunction], + "getSSLSettings": [MockFunction], + "isActionTypeEnabled": [MockFunction], + "isHostnameAllowed": [MockFunction], + "isUriAllowed": [MockFunction], + }, + "connectorId": "1", "content": Object { "message": "a message", "subject": "a subject", @@ -264,7 +918,7 @@ describe('send_email module', () => { } ); - const result = await sendEmail(mockLogger, sendEmailOptions); + const result = await sendEmail(mockLogger, sendEmailOptions, connectorTokenClient); expect(result).toBe(sendMailMockResult); expect(createTransportMock.mock.calls[0]).toMatchInlineSnapshot(` Array [ @@ -314,7 +968,7 @@ describe('send_email module', () => { delete sendEmailOptions.transport.user; // @ts-expect-error delete sendEmailOptions.transport.password; - const result = await sendEmail(mockLogger, sendEmailOptions); + const result = await sendEmail(mockLogger, sendEmailOptions, connectorTokenClient); expect(result).toBe(sendMailMockResult); expect(createTransportMock.mock.calls[0]).toMatchInlineSnapshot(` Array [ @@ -364,7 +1018,7 @@ describe('send_email module', () => { // @ts-expect-error delete sendEmailOptions.transport.password; - const result = await sendEmail(mockLogger, sendEmailOptions); + const result = await sendEmail(mockLogger, sendEmailOptions, connectorTokenClient); expect(result).toBe(sendMailMockResult); expect(createTransportMock.mock.calls[0]).toMatchInlineSnapshot(` Array [ @@ -405,7 +1059,9 @@ describe('send_email module', () => { sendMailMock.mockReset(); sendMailMock.mockRejectedValue(new Error('wops')); - await expect(sendEmail(mockLogger, sendEmailOptions)).rejects.toThrow('wops'); + await expect(sendEmail(mockLogger, sendEmailOptions, connectorTokenClient)).rejects.toThrow( + 'wops' + ); }); test('it bypasses with proxyBypassHosts when expected', async () => { @@ -426,7 +1082,7 @@ describe('send_email module', () => { } ); - const result = await sendEmail(mockLogger, sendEmailOptions); + const result = await sendEmail(mockLogger, sendEmailOptions, connectorTokenClient); expect(result).toBe(sendMailMockResult); expect(createTransportMock.mock.calls[0]).toMatchInlineSnapshot(` Array [ @@ -460,7 +1116,7 @@ describe('send_email module', () => { } ); - const result = await sendEmail(mockLogger, sendEmailOptions); + const result = await sendEmail(mockLogger, sendEmailOptions, connectorTokenClient); expect(result).toBe(sendMailMockResult); expect(createTransportMock.mock.calls[0]).toMatchInlineSnapshot(` Array [ @@ -496,7 +1152,7 @@ describe('send_email module', () => { } ); - const result = await sendEmail(mockLogger, sendEmailOptions); + const result = await sendEmail(mockLogger, sendEmailOptions, connectorTokenClient); expect(result).toBe(sendMailMockResult); expect(createTransportMock.mock.calls[0]).toMatchInlineSnapshot(` Array [ @@ -530,7 +1186,7 @@ describe('send_email module', () => { } ); - const result = await sendEmail(mockLogger, sendEmailOptions); + const result = await sendEmail(mockLogger, sendEmailOptions, connectorTokenClient); expect(result).toBe(sendMailMockResult); expect(createTransportMock.mock.calls[0]).toMatchInlineSnapshot(` Array [ @@ -567,7 +1223,7 @@ describe('send_email module', () => { } ); - const result = await sendEmail(mockLogger, sendEmailOptions); + const result = await sendEmail(mockLogger, sendEmailOptions, connectorTokenClient); expect(result).toBe(sendMailMockResult); // note in the object below, the rejectUnauthenticated got set to false, @@ -610,7 +1266,7 @@ describe('send_email module', () => { } ); - const result = await sendEmail(mockLogger, sendEmailOptions); + const result = await sendEmail(mockLogger, sendEmailOptions, connectorTokenClient); expect(result).toBe(sendMailMockResult); // in this case, rejectUnauthorized is true, as the custom host settings @@ -657,7 +1313,7 @@ describe('send_email module', () => { } ); - const result = await sendEmail(mockLogger, sendEmailOptions); + const result = await sendEmail(mockLogger, sendEmailOptions, connectorTokenClient); expect(result).toBe(sendMailMockResult); expect(createTransportMock.mock.calls[0]).toMatchInlineSnapshot(` Array [ @@ -711,6 +1367,7 @@ function getSendEmailOptions( }, hasAuth: true, configurationUtilities, + connectorId: '1', }; } @@ -745,5 +1402,6 @@ function getSendEmailOptionsNoAuth( }, hasAuth: false, configurationUtilities, + connectorId: '2', }; } diff --git a/x-pack/plugins/actions/server/builtin_action_types/lib/send_email.ts b/x-pack/plugins/actions/server/builtin_action_types/lib/send_email.ts index 53c70fddc5a09..378edc174e790 100644 --- a/x-pack/plugins/actions/server/builtin_action_types/lib/send_email.ts +++ b/x-pack/plugins/actions/server/builtin_action_types/lib/send_email.ts @@ -15,7 +15,7 @@ import { CustomHostSettings } from '../../config'; import { getNodeSSLOptions, getSSLSettingsFromConfig } from './get_node_ssl_options'; import { sendEmailGraphApi } from './send_email_graph_api'; import { requestOAuthClientCredentialsToken } from './request_oauth_client_credentials_token'; -import { ProxySettings } from '../../types'; +import { ConnectorTokenClientContract, ProxySettings } from '../../types'; import { AdditionalEmailServices } from '../../../common'; // an email "service" which doesn't actually send, just returns what it would send @@ -25,6 +25,7 @@ export const GRAPH_API_OAUTH_SCOPE = 'https://graph.microsoft.com/.default'; export const EXCHANGE_ONLINE_SERVER_HOST = 'https://login.microsoftonline.com'; export interface SendEmailOptions { + connectorId: string; transport: Transport; routing: Routing; content: Content; @@ -59,13 +60,17 @@ export interface Content { message: string; } -export async function sendEmail(logger: Logger, options: SendEmailOptions): Promise { +export async function sendEmail( + logger: Logger, + options: SendEmailOptions, + connectorTokenClient: ConnectorTokenClientContract +): Promise { const { transport, content } = options; const { message } = content; const messageHTML = htmlFromMarkdown(logger, message); if (transport.service === AdditionalEmailServices.EXCHANGE) { - return await sendEmailWithExchange(logger, options, messageHTML); + return await sendEmailWithExchange(logger, options, messageHTML, connectorTokenClient); } else { return await sendEmailWithNodemailer(logger, options, messageHTML); } @@ -75,25 +80,67 @@ export async function sendEmail(logger: Logger, options: SendEmailOptions): Prom async function sendEmailWithExchange( logger: Logger, options: SendEmailOptions, - messageHTML: string + messageHTML: string, + connectorTokenClient: ConnectorTokenClientContract ): Promise { - const { transport, configurationUtilities } = options; + const { transport, configurationUtilities, connectorId } = options; const { clientId, clientSecret, tenantId, oauthTokenUrl } = transport; - // request access token for microsoft exchange online server with Graph API scope - const tokenResult = await requestOAuthClientCredentialsToken( - oauthTokenUrl ?? `${EXCHANGE_ONLINE_SERVER_HOST}/${tenantId}/oauth2/v2.0/token`, - logger, - { - scope: GRAPH_API_OAUTH_SCOPE, - clientId, - clientSecret, - }, - configurationUtilities - ); + let accessToken: string; + + const { connectorToken, hasErrors } = await connectorTokenClient.get({ connectorId }); + if (connectorToken === null || Date.parse(connectorToken.expiresAt) <= Date.now()) { + // request new access token for microsoft exchange online server with Graph API scope + const tokenResult = await requestOAuthClientCredentialsToken( + oauthTokenUrl ?? `${EXCHANGE_ONLINE_SERVER_HOST}/${tenantId}/oauth2/v2.0/token`, + logger, + { + scope: GRAPH_API_OAUTH_SCOPE, + clientId, + clientSecret, + }, + configurationUtilities + ); + accessToken = `${tokenResult.tokenType} ${tokenResult.accessToken}`; + + // try to update connector_token SO + try { + if (connectorToken === null) { + if (hasErrors) { + // delete existing access tokens + await connectorTokenClient.deleteConnectorTokens({ + connectorId, + tokenType: 'access_token', + }); + } + await connectorTokenClient.create({ + connectorId, + token: accessToken, + // convert MS Exchange expiresIn from seconds to milliseconds + expiresAtMillis: new Date(Date.now() + tokenResult.expiresIn * 1000).toISOString(), + tokenType: 'access_token', + }); + } else { + await connectorTokenClient.update({ + id: connectorToken.id!.toString(), + token: accessToken, + // convert MS Exchange expiresIn from seconds to milliseconds + expiresAtMillis: new Date(Date.now() + tokenResult.expiresIn * 1000).toISOString(), + tokenType: 'access_token', + }); + } + } catch (err) { + logger.warn( + `Not able to update connector token for connectorId: ${connectorId} due to error: ${err.message}` + ); + } + } else { + // use existing valid token + accessToken = connectorToken.token; + } const headers = { 'Content-Type': 'application/json', - Authorization: `${tokenResult.tokenType} ${tokenResult.accessToken}`, + Authorization: accessToken, }; return await sendEmailGraphApi( diff --git a/x-pack/plugins/actions/server/builtin_action_types/lib/send_email_graph_api.test.ts b/x-pack/plugins/actions/server/builtin_action_types/lib/send_email_graph_api.test.ts index a50dee8f1cbc2..63cd3523b0026 100644 --- a/x-pack/plugins/actions/server/builtin_action_types/lib/send_email_graph_api.test.ts +++ b/x-pack/plugins/actions/server/builtin_action_types/lib/send_email_graph_api.test.ts @@ -350,5 +350,6 @@ function getSendEmailOptions( }, hasAuth: true, configurationUtilities, + connectorId: '1', }; } diff --git a/x-pack/plugins/actions/server/constants/saved_objects.ts b/x-pack/plugins/actions/server/constants/saved_objects.ts index aa79d56fac874..9064b6e71b84f 100644 --- a/x-pack/plugins/actions/server/constants/saved_objects.ts +++ b/x-pack/plugins/actions/server/constants/saved_objects.ts @@ -8,3 +8,4 @@ export const ACTION_SAVED_OBJECT_TYPE = 'action'; export const ALERT_SAVED_OBJECT_TYPE = 'alert'; export const ACTION_TASK_PARAMS_SAVED_OBJECT_TYPE = 'action_task_params'; +export const CONNECTOR_TOKEN_SAVED_OBJECT_TYPE = 'connector_token'; diff --git a/x-pack/plugins/actions/server/feature.ts b/x-pack/plugins/actions/server/feature.ts index 42227d3b885ad..c6265a17b122e 100644 --- a/x-pack/plugins/actions/server/feature.ts +++ b/x-pack/plugins/actions/server/feature.ts @@ -9,6 +9,7 @@ import { i18n } from '@kbn/i18n'; import { ACTION_SAVED_OBJECT_TYPE, ACTION_TASK_PARAMS_SAVED_OBJECT_TYPE, + CONNECTOR_TOKEN_SAVED_OBJECT_TYPE, } from './constants/saved_objects'; import { DEFAULT_APP_CATEGORIES } from '../../../../src/core/server'; @@ -31,7 +32,11 @@ export const ACTIONS_FEATURE = { insightsAndAlerting: ['triggersActions'], }, savedObject: { - all: [ACTION_SAVED_OBJECT_TYPE, ACTION_TASK_PARAMS_SAVED_OBJECT_TYPE], + all: [ + ACTION_SAVED_OBJECT_TYPE, + ACTION_TASK_PARAMS_SAVED_OBJECT_TYPE, + CONNECTOR_TOKEN_SAVED_OBJECT_TYPE, + ], read: [], }, ui: ['show', 'execute', 'save', 'delete'], @@ -45,7 +50,7 @@ export const ACTIONS_FEATURE = { }, savedObject: { // action execution requires 'read' over `actions`, but 'all' over `action_task_params` - all: [ACTION_TASK_PARAMS_SAVED_OBJECT_TYPE], + all: [ACTION_TASK_PARAMS_SAVED_OBJECT_TYPE, CONNECTOR_TOKEN_SAVED_OBJECT_TYPE], read: [ACTION_SAVED_OBJECT_TYPE], }, ui: ['show', 'execute'], diff --git a/x-pack/plugins/actions/server/mocks.ts b/x-pack/plugins/actions/server/mocks.ts index 4afdd01777f4f..bdfdab5124e29 100644 --- a/x-pack/plugins/actions/server/mocks.ts +++ b/x-pack/plugins/actions/server/mocks.ts @@ -10,11 +10,16 @@ import { PluginSetupContract, PluginStartContract, renderActionParameterTemplate import { Services } from './types'; import { elasticsearchServiceMock, + loggingSystemMock, savedObjectsClientMock, } from '../../../../src/core/server/mocks'; import { actionsAuthorizationMock } from './authorization/actions_authorization.mock'; +import { encryptedSavedObjectsMock } from '../../encrypted_saved_objects/server/mocks'; +import { ConnectorTokenClient } from './builtin_action_types/lib/connector_token_client'; +import { Logger } from '../../../../src/core/server'; export { actionsAuthorizationMock }; export { actionsClientMock }; +const logger = loggingSystemMock.create().get() as jest.Mocked; const createSetupMock = () => { const mock: jest.Mocked = { @@ -56,6 +61,11 @@ const createServicesMock = () => { > = { savedObjectsClient: savedObjectsClientMock.create(), scopedClusterClient: elasticsearchServiceMock.createScopedClusterClient().asCurrentUser, + connectorTokenClient: new ConnectorTokenClient({ + unsecuredSavedObjectsClient: savedObjectsClientMock.create(), + encryptedSavedObjectsClient: encryptedSavedObjectsMock.createClient(), + logger, + }), }; return mock; }; diff --git a/x-pack/plugins/actions/server/plugin.test.ts b/x-pack/plugins/actions/server/plugin.test.ts index 08ea99df67c8e..2d854df6a5853 100644 --- a/x-pack/plugins/actions/server/plugin.test.ts +++ b/x-pack/plugins/actions/server/plugin.test.ts @@ -60,6 +60,14 @@ describe('Actions Plugin', () => { usageCollection: usageCollectionPluginMock.createSetupContract(), features: featuresPluginMock.createSetup(), }; + coreSetup.getStartServices.mockResolvedValue([ + coreMock.createStart(), + { + ...pluginsSetup, + encryptedSavedObjects: encryptedSavedObjectsMock.createStart(), + }, + {}, + ]); }); it('should log warning when Encrypted Saved Objects plugin is missing encryption key', async () => { diff --git a/x-pack/plugins/actions/server/plugin.ts b/x-pack/plugins/actions/server/plugin.ts index bbf00572935fa..985bbaf688e12 100644 --- a/x-pack/plugins/actions/server/plugin.ts +++ b/x-pack/plugins/actions/server/plugin.ts @@ -21,6 +21,7 @@ import { } from '../../../../src/core/server'; import { + EncryptedSavedObjectsClient, EncryptedSavedObjectsPluginSetup, EncryptedSavedObjectsPluginStart, } from '../../encrypted_saved_objects/server'; @@ -70,6 +71,7 @@ import { ACTION_SAVED_OBJECT_TYPE, ACTION_TASK_PARAMS_SAVED_OBJECT_TYPE, ALERT_SAVED_OBJECT_TYPE, + CONNECTOR_TOKEN_SAVED_OBJECT_TYPE, } from './constants/saved_objects'; import { setupSavedObjects } from './saved_objects'; import { ACTIONS_FEATURE } from './feature'; @@ -85,6 +87,7 @@ import { getAlertHistoryEsIndex } from './preconfigured_connectors/alert_history import { createAlertHistoryIndexTemplate } from './preconfigured_connectors/alert_history_es_index/create_alert_history_index_template'; import { ACTIONS_FEATURE_ID, AlertHistoryEsIndexConnectorId } from '../common'; import { EVENT_LOG_ACTIONS, EVENT_LOG_PROVIDER } from './constants/event_log'; +import { ConnectorTokenClient } from './builtin_action_types/lib/connector_token_client'; export interface PluginSetupContract { registerType< @@ -144,6 +147,7 @@ const includedHiddenTypes = [ ACTION_SAVED_OBJECT_TYPE, ACTION_TASK_PARAMS_SAVED_OBJECT_TYPE, ALERT_SAVED_OBJECT_TYPE, + CONNECTOR_TOKEN_SAVED_OBJECT_TYPE, ]; export class ActionsPlugin implements Plugin { @@ -376,6 +380,11 @@ export class ActionsPlugin implements Plugin this.getUnsecuredSavedObjectsClient(core.savedObjects, request) ), encryptedSavedObjectsClient, actionTypeRegistry: actionTypeRegistry!, @@ -484,12 +495,21 @@ export class ActionsPlugin implements Plugin SavedObjectsClientContract, - elasticsearch: ElasticsearchServiceStart + elasticsearch: ElasticsearchServiceStart, + encryptedSavedObjectsClient: EncryptedSavedObjectsClient, + unsecuredSavedObjectsClient: (request: KibanaRequest) => SavedObjectsClientContract ): (request: KibanaRequest) => Services { - return (request) => ({ - savedObjectsClient: getScopedClient(request), - scopedClusterClient: elasticsearch.client.asScoped(request).asCurrentUser, - }); + return (request) => { + return { + savedObjectsClient: getScopedClient(request), + scopedClusterClient: elasticsearch.client.asScoped(request).asCurrentUser, + connectorTokenClient: new ConnectorTokenClient({ + unsecuredSavedObjectsClient: unsecuredSavedObjectsClient(request), + encryptedSavedObjectsClient, + logger: this.logger, + }), + }; + }; } private createRouteHandlerContext = ( @@ -504,10 +524,12 @@ export class ActionsPlugin implements Plugin { if (isESOCanEncrypt !== true) { @@ -515,11 +537,12 @@ export class ActionsPlugin implements Plugin string | undefine export type ActionTypeConfig = Record; export type ActionTypeSecrets = Record; export type ActionTypeParams = Record; +export type ConnectorTokenClientContract = PublicMethodsOf; export interface Services { savedObjectsClient: SavedObjectsClientContract; scopedClusterClient: ElasticsearchClient; + connectorTokenClient: ConnectorTokenClient; } export interface ActionsApiRequestHandlerContext { @@ -173,3 +176,12 @@ export interface ResponseSettings { export interface SSLSettings { verificationMode?: 'none' | 'certificate' | 'full'; } + +export interface ConnectorToken extends SavedObjectAttributes { + connectorId: string; + tokenType: string; + token: string; + expiresAt: string; + createdAt: string; + updatedAt?: string; +} diff --git a/x-pack/plugins/alerting/README.md b/x-pack/plugins/alerting/README.md index 3646dbddb347d..fcb31977806c9 100644 --- a/x-pack/plugins/alerting/README.md +++ b/x-pack/plugins/alerting/README.md @@ -230,7 +230,7 @@ interface MyRuleTypeAlertContext extends AlertInstanceContext { type MyRuleTypeActionGroups = 'default' | 'warning'; -const myRuleType: AlertType< +const myRuleType: RuleType< MyRuleTypeParams, MyRuleTypeExtractedParams, MyRuleTypeState, diff --git a/x-pack/plugins/alerting/common/builtin_action_groups.ts b/x-pack/plugins/alerting/common/builtin_action_groups.ts index ada5f08e85d92..ed1d0746f0f26 100644 --- a/x-pack/plugins/alerting/common/builtin_action_groups.ts +++ b/x-pack/plugins/alerting/common/builtin_action_groups.ts @@ -6,7 +6,7 @@ */ import { i18n } from '@kbn/i18n'; -import { ActionGroup } from './alert_type'; +import { ActionGroup } from './rule_type'; export type DefaultActionGroupId = 'default'; diff --git a/x-pack/plugins/alerting/common/index.ts b/x-pack/plugins/alerting/common/index.ts index 1c7525a065760..7296766e6955e 100644 --- a/x-pack/plugins/alerting/common/index.ts +++ b/x-pack/plugins/alerting/common/index.ts @@ -11,9 +11,9 @@ import { AlertsHealth } from './alert'; export * from './alert'; -export * from './alert_type'; +export * from './rule_type'; export * from './alert_instance'; -export * from './alert_task_instance'; +export * from './rule_task_instance'; export * from './alert_navigation'; export * from './alert_summary'; export * from './builtin_action_groups'; diff --git a/x-pack/plugins/alerting/common/alert_task_instance.ts b/x-pack/plugins/alerting/common/rule_task_instance.ts similarity index 74% rename from x-pack/plugins/alerting/common/alert_task_instance.ts rename to x-pack/plugins/alerting/common/rule_task_instance.ts index fc8b27495e521..fdd308a6395a1 100644 --- a/x-pack/plugins/alerting/common/alert_task_instance.ts +++ b/x-pack/plugins/alerting/common/rule_task_instance.ts @@ -9,15 +9,15 @@ import * as t from 'io-ts'; import { rawAlertInstance } from './alert_instance'; import { DateFromString } from './date_from_string'; -export const alertStateSchema = t.partial({ +export const ruleStateSchema = t.partial({ alertTypeState: t.record(t.string, t.unknown), alertInstances: t.record(t.string, rawAlertInstance), previousStartedAt: t.union([t.null, DateFromString]), }); -export type AlertTaskState = t.TypeOf; +export type RuleTaskState = t.TypeOf; -export const alertParamsSchema = t.intersection([ +export const ruleParamsSchema = t.intersection([ t.type({ alertId: t.string, }), @@ -25,4 +25,4 @@ export const alertParamsSchema = t.intersection([ spaceId: t.string, }), ]); -export type AlertTaskParams = t.TypeOf; +export type RuleTaskParams = t.TypeOf; diff --git a/x-pack/plugins/alerting/common/alert_type.ts b/x-pack/plugins/alerting/common/rule_type.ts similarity index 97% rename from x-pack/plugins/alerting/common/alert_type.ts rename to x-pack/plugins/alerting/common/rule_type.ts index 1b0ac28c9fa74..f1907917d01fd 100644 --- a/x-pack/plugins/alerting/common/alert_type.ts +++ b/x-pack/plugins/alerting/common/rule_type.ts @@ -8,7 +8,7 @@ import { LicenseType } from '../../licensing/common/types'; import { RecoveredActionGroupId, DefaultActionGroupId } from './builtin_action_groups'; -export interface AlertType< +export interface RuleType< ActionGroupIds extends Exclude = DefaultActionGroupId, RecoveryActionGroupId extends string = RecoveredActionGroupId > { diff --git a/x-pack/plugins/alerting/public/alert_api.test.ts b/x-pack/plugins/alerting/public/alert_api.test.ts index dd2f7d167c1c3..cabccbacb42df 100644 --- a/x-pack/plugins/alerting/public/alert_api.test.ts +++ b/x-pack/plugins/alerting/public/alert_api.test.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { AlertType, RecoveredActionGroup } from '../common'; +import { RuleType, RecoveredActionGroup } from '../common'; import { httpServiceMock } from '../../../../src/core/public/mocks'; import { loadAlert, loadAlertType, loadAlertTypes } from './alert_api'; import uuid from 'uuid'; @@ -16,7 +16,7 @@ beforeEach(() => jest.resetAllMocks()); describe('loadAlertTypes', () => { test('should call get alert types API', async () => { - const resolvedValue: AlertType[] = [ + const resolvedValue: RuleType[] = [ { id: 'test', name: 'Test', @@ -43,7 +43,7 @@ describe('loadAlertTypes', () => { describe('loadAlertType', () => { test('should call get alert types API', async () => { - const alertType: AlertType = { + const alertType: RuleType = { id: 'test', name: 'Test', actionVariables: ['var1'], @@ -66,7 +66,7 @@ describe('loadAlertType', () => { }); test('should find the required alertType', async () => { - const alertType: AlertType = { + const alertType: RuleType = { id: 'test-another', name: 'Test Another', actionVariables: [], diff --git a/x-pack/plugins/alerting/public/alert_api.ts b/x-pack/plugins/alerting/public/alert_api.ts index f3faa65a4b384..e323ce9dc41e5 100644 --- a/x-pack/plugins/alerting/public/alert_api.ts +++ b/x-pack/plugins/alerting/public/alert_api.ts @@ -7,9 +7,9 @@ import { HttpSetup } from 'kibana/public'; import { LEGACY_BASE_ALERT_API_PATH } from '../common'; -import type { Alert, AlertType } from '../common'; +import type { Alert, RuleType } from '../common'; -export async function loadAlertTypes({ http }: { http: HttpSetup }): Promise { +export async function loadAlertTypes({ http }: { http: HttpSetup }): Promise { return await http.get(`${LEGACY_BASE_ALERT_API_PATH}/list_alert_types`); } @@ -18,11 +18,11 @@ export async function loadAlertType({ id, }: { http: HttpSetup; - id: AlertType['id']; -}): Promise { + id: RuleType['id']; +}): Promise { const alertTypes = (await http.get( `${LEGACY_BASE_ALERT_API_PATH}/list_alert_types` - )) as AlertType[]; + )) as RuleType[]; return alertTypes.find((type) => type.id === id); } diff --git a/x-pack/plugins/alerting/public/alert_navigation_registry/alert_navigation_registry.test.ts b/x-pack/plugins/alerting/public/alert_navigation_registry/alert_navigation_registry.test.ts index af009217ed99b..f28f3dd5f7b78 100644 --- a/x-pack/plugins/alerting/public/alert_navigation_registry/alert_navigation_registry.test.ts +++ b/x-pack/plugins/alerting/public/alert_navigation_registry/alert_navigation_registry.test.ts @@ -6,12 +6,12 @@ */ import { AlertNavigationRegistry } from './alert_navigation_registry'; -import { AlertType, RecoveredActionGroup, SanitizedAlert } from '../../common'; +import { RuleType, RecoveredActionGroup, SanitizedAlert } from '../../common'; import uuid from 'uuid'; beforeEach(() => jest.resetAllMocks()); -const mockAlertType = (id: string): AlertType => ({ +const mockAlertType = (id: string): RuleType => ({ id, name: id, actionGroups: [], diff --git a/x-pack/plugins/alerting/public/alert_navigation_registry/alert_navigation_registry.ts b/x-pack/plugins/alerting/public/alert_navigation_registry/alert_navigation_registry.ts index 0c7bf052fef4c..4bc65c6baf4ef 100644 --- a/x-pack/plugins/alerting/public/alert_navigation_registry/alert_navigation_registry.ts +++ b/x-pack/plugins/alerting/public/alert_navigation_registry/alert_navigation_registry.ts @@ -6,7 +6,7 @@ */ import { i18n } from '@kbn/i18n'; -import { AlertType } from '../../common'; +import { RuleType } from '../../common'; import { AlertNavigationHandler } from './types'; const DEFAULT_HANDLER = Symbol('*'); @@ -14,7 +14,7 @@ export class AlertNavigationRegistry { private readonly alertNavigations: Map> = new Map(); - public has(consumer: string, alertType: AlertType) { + public has(consumer: string, alertType: RuleType) { return this.hasTypedHandler(consumer, alertType.id) || this.hasDefaultHandler(consumer); } @@ -70,7 +70,7 @@ export class AlertNavigationRegistry { consumerNavigations.set(ruleTypeId, handler); } - public get(consumer: string, alertType: AlertType): AlertNavigationHandler { + public get(consumer: string, alertType: RuleType): AlertNavigationHandler { if (this.has(consumer, alertType)) { const consumerHandlers = this.alertNavigations.get(consumer)!; return (consumerHandlers.get(alertType.id) ?? consumerHandlers.get(DEFAULT_HANDLER))!; diff --git a/x-pack/plugins/alerting/server/health/get_health.ts b/x-pack/plugins/alerting/server/health/get_health.ts index 6966c9b75ca43..09a5922576192 100644 --- a/x-pack/plugins/alerting/server/health/get_health.ts +++ b/x-pack/plugins/alerting/server/health/get_health.ts @@ -6,7 +6,7 @@ */ import { ISavedObjectsRepository, SavedObjectsServiceStart } from 'src/core/server'; -import { AlertsHealth, HealthStatus, RawAlert, AlertExecutionStatusErrorReasons } from '../types'; +import { AlertsHealth, HealthStatus, RawRule, AlertExecutionStatusErrorReasons } from '../types'; export const getHealth = async ( internalSavedObjectsRepository: ISavedObjectsRepository @@ -26,7 +26,7 @@ export const getHealth = async ( }, }; - const { saved_objects: decryptErrorData } = await internalSavedObjectsRepository.find({ + const { saved_objects: decryptErrorData } = await internalSavedObjectsRepository.find({ filter: `alert.attributes.executionStatus.status:error and alert.attributes.executionStatus.error.reason:${AlertExecutionStatusErrorReasons.Decrypt}`, fields: ['executionStatus'], type: 'alert', @@ -44,7 +44,7 @@ export const getHealth = async ( }; } - const { saved_objects: executeErrorData } = await internalSavedObjectsRepository.find({ + const { saved_objects: executeErrorData } = await internalSavedObjectsRepository.find({ filter: `alert.attributes.executionStatus.status:error and alert.attributes.executionStatus.error.reason:${AlertExecutionStatusErrorReasons.Execute}`, fields: ['executionStatus'], type: 'alert', @@ -62,7 +62,7 @@ export const getHealth = async ( }; } - const { saved_objects: readErrorData } = await internalSavedObjectsRepository.find({ + const { saved_objects: readErrorData } = await internalSavedObjectsRepository.find({ filter: `alert.attributes.executionStatus.status:error and alert.attributes.executionStatus.error.reason:${AlertExecutionStatusErrorReasons.Read}`, fields: ['executionStatus'], type: 'alert', @@ -80,7 +80,7 @@ export const getHealth = async ( }; } - const { saved_objects: noErrorData } = await internalSavedObjectsRepository.find({ + const { saved_objects: noErrorData } = await internalSavedObjectsRepository.find({ filter: 'not alert.attributes.executionStatus.status:error', fields: ['executionStatus'], type: 'alert', diff --git a/x-pack/plugins/alerting/server/index.ts b/x-pack/plugins/alerting/server/index.ts index 8ed91cc821412..90bda8b1e09d4 100644 --- a/x-pack/plugins/alerting/server/index.ts +++ b/x-pack/plugins/alerting/server/index.ts @@ -14,7 +14,7 @@ import { AlertsConfigType } from './types'; export type RulesClient = PublicMethodsOf; export type { - AlertType, + RuleType, ActionGroup, ActionGroupIdsOf, AlertingPlugin, diff --git a/x-pack/plugins/alerting/server/lib/create_alert_event_log_record_object.test.ts b/x-pack/plugins/alerting/server/lib/create_alert_event_log_record_object.test.ts index 0731886bcaeb0..a7a00034e7064 100644 --- a/x-pack/plugins/alerting/server/lib/create_alert_event_log_record_object.test.ts +++ b/x-pack/plugins/alerting/server/lib/create_alert_event_log_record_object.test.ts @@ -6,11 +6,11 @@ */ import { createAlertEventLogRecordObject } from './create_alert_event_log_record_object'; -import { UntypedNormalizedAlertType } from '../rule_type_registry'; +import { UntypedNormalizedRuleType } from '../rule_type_registry'; import { RecoveredActionGroup } from '../types'; describe('createAlertEventLogRecordObject', () => { - const ruleType: jest.Mocked = { + const ruleType: jest.Mocked = { id: 'test', name: 'My test alert', actionGroups: [{ id: 'default', name: 'Default' }, RecoveredActionGroup], diff --git a/x-pack/plugins/alerting/server/lib/create_alert_event_log_record_object.ts b/x-pack/plugins/alerting/server/lib/create_alert_event_log_record_object.ts index 12300211cb0bb..e06b5bf893bac 100644 --- a/x-pack/plugins/alerting/server/lib/create_alert_event_log_record_object.ts +++ b/x-pack/plugins/alerting/server/lib/create_alert_event_log_record_object.ts @@ -7,13 +7,13 @@ import { AlertInstanceState } from '../types'; import { IEvent } from '../../../event_log/server'; -import { UntypedNormalizedAlertType } from '../rule_type_registry'; +import { UntypedNormalizedRuleType } from '../rule_type_registry'; export type Event = Exclude; interface CreateAlertEventLogRecordParams { ruleId: string; - ruleType: UntypedNormalizedAlertType; + ruleType: UntypedNormalizedRuleType; action: string; ruleName?: string; instanceId?: string; diff --git a/x-pack/plugins/alerting/server/lib/get_alert_type_feature_usage_name.ts b/x-pack/plugins/alerting/server/lib/get_rule_type_feature_usage_name.ts similarity index 70% rename from x-pack/plugins/alerting/server/lib/get_alert_type_feature_usage_name.ts rename to x-pack/plugins/alerting/server/lib/get_rule_type_feature_usage_name.ts index 71879e1dca8ac..85f3e9bfb50b4 100644 --- a/x-pack/plugins/alerting/server/lib/get_alert_type_feature_usage_name.ts +++ b/x-pack/plugins/alerting/server/lib/get_rule_type_feature_usage_name.ts @@ -5,6 +5,6 @@ * 2.0. */ -export function getAlertTypeFeatureUsageName(alertTypeName: string) { - return `Alert: ${alertTypeName}`; +export function getRuleTypeFeatureUsageName(ruleTypeName: string) { + return `Rule: ${ruleTypeName}`; } diff --git a/x-pack/plugins/alerting/server/lib/index.ts b/x-pack/plugins/alerting/server/lib/index.ts index 24f2513e1c650..29526f17268f2 100644 --- a/x-pack/plugins/alerting/server/lib/index.ts +++ b/x-pack/plugins/alerting/server/lib/index.ts @@ -8,7 +8,7 @@ export { parseDuration, validateDurationSchema } from '../../common/parse_duration'; export type { ILicenseState } from './license_state'; export { LicenseState } from './license_state'; -export { validateAlertTypeParams } from './validate_alert_type_params'; +export { validateRuleTypeParams } from './validate_rule_type_params'; export { getAlertNotifyWhenType } from './get_alert_notify_when_type'; export { verifyApiAccess } from './license_api_access'; export { ErrorWithReason, getReasonFromError, isErrorWithReason } from './error_with_reason'; @@ -21,6 +21,6 @@ export { AlertTypeDisabledError, isErrorThatHandlesItsOwnResponse } from './erro export { executionStatusFromState, executionStatusFromError, - alertExecutionStatusToRaw, - alertExecutionStatusFromRaw, -} from './alert_execution_status'; + ruleExecutionStatusToRaw, + ruleExecutionStatusFromRaw, +} from './rule_execution_status'; diff --git a/x-pack/plugins/alerting/server/lib/license_state.mock.ts b/x-pack/plugins/alerting/server/lib/license_state.mock.ts index 1521a1cf25da9..0f1b618eea761 100644 --- a/x-pack/plugins/alerting/server/lib/license_state.mock.ts +++ b/x-pack/plugins/alerting/server/lib/license_state.mock.ts @@ -11,8 +11,8 @@ export const createLicenseStateMock = () => { const licenseState: jest.Mocked = { clean: jest.fn(), getLicenseInformation: jest.fn(), - ensureLicenseForAlertType: jest.fn(), - getLicenseCheckForAlertType: jest.fn().mockResolvedValue({ + ensureLicenseForRuleType: jest.fn(), + getLicenseCheckForRuleType: jest.fn().mockResolvedValue({ isValid: true, }), checkLicense: jest.fn().mockResolvedValue({ diff --git a/x-pack/plugins/alerting/server/lib/license_state.test.ts b/x-pack/plugins/alerting/server/lib/license_state.test.ts index e20acafbab314..0a261c7248484 100644 --- a/x-pack/plugins/alerting/server/lib/license_state.test.ts +++ b/x-pack/plugins/alerting/server/lib/license_state.test.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { AlertType } from '../types'; +import { RuleType } from '../types'; import { Subject } from 'rxjs'; import { LicenseState, ILicenseState } from './license_state'; import { licensingMock } from '../../../licensing/server/mocks'; @@ -53,11 +53,11 @@ describe('checkLicense()', () => { }); }); -describe('getLicenseCheckForAlertType', () => { +describe('getLicenseCheckForRuleType', () => { let license: Subject; let licenseState: ILicenseState; const mockNotifyUsage = jest.fn(); - const alertType: AlertType = { + const ruleType: RuleType = { id: 'test', name: 'Test', actionGroups: [ @@ -82,10 +82,10 @@ describe('getLicenseCheckForAlertType', () => { test('should return false when license not defined', () => { expect( - licenseState.getLicenseCheckForAlertType( - alertType.id, - alertType.name, - alertType.minimumLicenseRequired + licenseState.getLicenseCheckForRuleType( + ruleType.id, + ruleType.name, + ruleType.minimumLicenseRequired ) ).toEqual({ isValid: false, @@ -96,10 +96,10 @@ describe('getLicenseCheckForAlertType', () => { test('should return false when license not available', () => { license.next(createUnavailableLicense()); expect( - licenseState.getLicenseCheckForAlertType( - alertType.id, - alertType.name, - alertType.minimumLicenseRequired + licenseState.getLicenseCheckForRuleType( + ruleType.id, + ruleType.name, + ruleType.minimumLicenseRequired ) ).toEqual({ isValid: false, @@ -111,10 +111,10 @@ describe('getLicenseCheckForAlertType', () => { const expiredLicense = licensingMock.createLicense({ license: { status: 'expired' } }); license.next(expiredLicense); expect( - licenseState.getLicenseCheckForAlertType( - alertType.id, - alertType.name, - alertType.minimumLicenseRequired + licenseState.getLicenseCheckForRuleType( + ruleType.id, + ruleType.name, + ruleType.minimumLicenseRequired ) ).toEqual({ isValid: false, @@ -128,10 +128,10 @@ describe('getLicenseCheckForAlertType', () => { }); license.next(basicLicense); expect( - licenseState.getLicenseCheckForAlertType( - alertType.id, - alertType.name, - alertType.minimumLicenseRequired + licenseState.getLicenseCheckForRuleType( + ruleType.id, + ruleType.name, + ruleType.minimumLicenseRequired ) ).toEqual({ isValid: false, @@ -145,10 +145,10 @@ describe('getLicenseCheckForAlertType', () => { }); license.next(goldLicense); expect( - licenseState.getLicenseCheckForAlertType( - alertType.id, - alertType.name, - alertType.minimumLicenseRequired + licenseState.getLicenseCheckForRuleType( + ruleType.id, + ruleType.name, + ruleType.minimumLicenseRequired ) ).toEqual({ isValid: true, @@ -160,7 +160,7 @@ describe('getLicenseCheckForAlertType', () => { license: { status: 'active', type: 'gold' }, }); license.next(goldLicense); - licenseState.getLicenseCheckForAlertType(alertType.id, alertType.name, 'gold'); + licenseState.getLicenseCheckForRuleType(ruleType.id, ruleType.name, 'gold'); expect(mockNotifyUsage).not.toHaveBeenCalled(); }); @@ -169,7 +169,7 @@ describe('getLicenseCheckForAlertType', () => { license: { status: 'active', type: 'basic' }, }); license.next(basicLicense); - licenseState.getLicenseCheckForAlertType(alertType.id, alertType.name, 'basic'); + licenseState.getLicenseCheckForRuleType(ruleType.id, ruleType.name, 'basic'); expect(mockNotifyUsage).not.toHaveBeenCalled(); }); @@ -178,21 +178,21 @@ describe('getLicenseCheckForAlertType', () => { license: { status: 'active', type: 'gold' }, }); license.next(goldLicense); - licenseState.getLicenseCheckForAlertType( - alertType.id, - alertType.name, - alertType.minimumLicenseRequired, + licenseState.getLicenseCheckForRuleType( + ruleType.id, + ruleType.name, + ruleType.minimumLicenseRequired, { notifyUsage: true } ); - expect(mockNotifyUsage).toHaveBeenCalledWith('Alert: Test'); + expect(mockNotifyUsage).toHaveBeenCalledWith('Rule: Test'); }); }); -describe('ensureLicenseForAlertType()', () => { +describe('ensureLicenseForRuleType()', () => { let license: Subject; let licenseState: ILicenseState; const mockNotifyUsage = jest.fn(); - const alertType: AlertType = { + const ruleType: RuleType = { id: 'test', name: 'Test', actionGroups: [ @@ -217,18 +217,18 @@ describe('ensureLicenseForAlertType()', () => { test('should throw when license not defined', () => { expect(() => - licenseState.ensureLicenseForAlertType(alertType) + licenseState.ensureLicenseForRuleType(ruleType) ).toThrowErrorMatchingInlineSnapshot( - `"Alert type test is disabled because license information is not available at this time."` + `"Rule type test is disabled because license information is not available at this time."` ); }); test('should throw when license not available', () => { license.next(createUnavailableLicense()); expect(() => - licenseState.ensureLicenseForAlertType(alertType) + licenseState.ensureLicenseForRuleType(ruleType) ).toThrowErrorMatchingInlineSnapshot( - `"Alert type test is disabled because license information is not available at this time."` + `"Rule type test is disabled because license information is not available at this time."` ); }); @@ -236,9 +236,9 @@ describe('ensureLicenseForAlertType()', () => { const expiredLicense = licensingMock.createLicense({ license: { status: 'expired' } }); license.next(expiredLicense); expect(() => - licenseState.ensureLicenseForAlertType(alertType) + licenseState.ensureLicenseForRuleType(ruleType) ).toThrowErrorMatchingInlineSnapshot( - `"Alert type test is disabled because your basic license has expired."` + `"Rule type test is disabled because your basic license has expired."` ); }); @@ -248,9 +248,9 @@ describe('ensureLicenseForAlertType()', () => { }); license.next(basicLicense); expect(() => - licenseState.ensureLicenseForAlertType(alertType) + licenseState.ensureLicenseForRuleType(ruleType) ).toThrowErrorMatchingInlineSnapshot( - `"Alert test is disabled because it requires a Gold license. Go to License Management to view upgrade options."` + `"Rule test is disabled because it requires a Gold license. Go to License Management to view upgrade options."` ); }); @@ -259,7 +259,7 @@ describe('ensureLicenseForAlertType()', () => { license: { status: 'active', type: 'gold' }, }); license.next(goldLicense); - licenseState.ensureLicenseForAlertType(alertType); + licenseState.ensureLicenseForRuleType(ruleType); }); test('should call notifyUsage', () => { @@ -267,8 +267,8 @@ describe('ensureLicenseForAlertType()', () => { license: { status: 'active', type: 'gold' }, }); license.next(goldLicense); - licenseState.ensureLicenseForAlertType(alertType); - expect(mockNotifyUsage).toHaveBeenCalledWith('Alert: Test'); + licenseState.ensureLicenseForRuleType(ruleType); + expect(mockNotifyUsage).toHaveBeenCalledWith('Rule: Test'); }); }); diff --git a/x-pack/plugins/alerting/server/lib/license_state.ts b/x-pack/plugins/alerting/server/lib/license_state.ts index 9f6fd1b292af8..162823f8d5850 100644 --- a/x-pack/plugins/alerting/server/lib/license_state.ts +++ b/x-pack/plugins/alerting/server/lib/license_state.ts @@ -14,9 +14,9 @@ import { Observable, Subscription } from 'rxjs'; import { LicensingPluginStart } from '../../../licensing/server'; import { ILicense, LicenseType } from '../../../licensing/common/types'; import { PLUGIN } from '../constants/plugin'; -import { getAlertTypeFeatureUsageName } from './get_alert_type_feature_usage_name'; +import { getRuleTypeFeatureUsageName } from './get_rule_type_feature_usage_name'; import { - AlertType, + RuleType, AlertTypeParams, AlertTypeState, AlertInstanceState, @@ -68,21 +68,21 @@ export class LicenseState { this._notifyUsage = notifyUsage; } - public getLicenseCheckForAlertType( - alertTypeId: string, - alertTypeName: string, + public getLicenseCheckForRuleType( + ruleTypeId: string, + ruleTypeName: string, minimumLicenseRequired: LicenseType, { notifyUsage }: { notifyUsage: boolean } = { notifyUsage: false } ): { isValid: true } | { isValid: false; reason: 'unavailable' | 'expired' | 'invalid' } { if (notifyUsage) { - this.notifyUsage(alertTypeName, minimumLicenseRequired); + this.notifyUsage(ruleTypeName, minimumLicenseRequired); } if (!this.license?.isAvailable) { return { isValid: false, reason: 'unavailable' }; } - const check = this.license.check(alertTypeId, minimumLicenseRequired); + const check = this.license.check(ruleTypeId, minimumLicenseRequired); switch (check.state) { case 'expired': @@ -98,10 +98,10 @@ export class LicenseState { } } - private notifyUsage(alertTypeName: string, minimumLicenseRequired: LicenseType) { + private notifyUsage(ruleTypeName: string, minimumLicenseRequired: LicenseType) { // No need to notify usage on basic alert types if (this._notifyUsage && minimumLicenseRequired !== 'basic') { - this._notifyUsage(getAlertTypeFeatureUsageName(alertTypeName)); + this._notifyUsage(getRuleTypeFeatureUsageName(ruleTypeName)); } } @@ -147,7 +147,7 @@ export class LicenseState { } } - public ensureLicenseForAlertType< + public ensureLicenseForRuleType< Params extends AlertTypeParams, ExtractedParams extends AlertTypeParams, State extends AlertTypeState, @@ -156,7 +156,7 @@ export class LicenseState { ActionGroupIds extends string, RecoveryActionGroupId extends string >( - alertType: AlertType< + ruleType: RuleType< Params, ExtractedParams, State, @@ -166,12 +166,12 @@ export class LicenseState { RecoveryActionGroupId > ) { - this.notifyUsage(alertType.name, alertType.minimumLicenseRequired); + this.notifyUsage(ruleType.name, ruleType.minimumLicenseRequired); - const check = this.getLicenseCheckForAlertType( - alertType.id, - alertType.name, - alertType.minimumLicenseRequired + const check = this.getLicenseCheckForRuleType( + ruleType.id, + ruleType.name, + ruleType.minimumLicenseRequired ); if (check.isValid) { @@ -182,9 +182,9 @@ export class LicenseState { throw new AlertTypeDisabledError( i18n.translate('xpack.alerting.serverSideErrors.unavailableLicenseErrorMessage', { defaultMessage: - 'Alert type {alertTypeId} is disabled because license information is not available at this time.', + 'Rule type {ruleTypeId} is disabled because license information is not available at this time.', values: { - alertTypeId: alertType.id, + ruleTypeId: ruleType.id, }, }), 'license_unavailable' @@ -193,8 +193,8 @@ export class LicenseState { throw new AlertTypeDisabledError( i18n.translate('xpack.alerting.serverSideErrors.expirerdLicenseErrorMessage', { defaultMessage: - 'Alert type {alertTypeId} is disabled because your {licenseType} license has expired.', - values: { alertTypeId: alertType.id, licenseType: this.license!.type }, + 'Rule type {ruleTypeId} is disabled because your {licenseType} license has expired.', + values: { ruleTypeId: ruleType.id, licenseType: this.license!.type }, }), 'license_expired' ); @@ -202,10 +202,10 @@ export class LicenseState { throw new AlertTypeDisabledError( i18n.translate('xpack.alerting.serverSideErrors.invalidLicenseErrorMessage', { defaultMessage: - 'Alert {alertTypeId} is disabled because it requires a {licenseType} license. Go to License Management to view upgrade options.', + 'Rule {ruleTypeId} is disabled because it requires a {licenseType} license. Go to License Management to view upgrade options.', values: { - alertTypeId: alertType.id, - licenseType: capitalize(alertType.minimumLicenseRequired), + ruleTypeId: ruleType.id, + licenseType: capitalize(ruleType.minimumLicenseRequired), }, }), 'license_invalid' diff --git a/x-pack/plugins/alerting/server/lib/alert_execution_status.test.ts b/x-pack/plugins/alerting/server/lib/rule_execution_status.test.ts similarity index 82% rename from x-pack/plugins/alerting/server/lib/alert_execution_status.test.ts rename to x-pack/plugins/alerting/server/lib/rule_execution_status.test.ts index 93cf0c656c692..4ee5bbe48c162 100644 --- a/x-pack/plugins/alerting/server/lib/alert_execution_status.test.ts +++ b/x-pack/plugins/alerting/server/lib/rule_execution_status.test.ts @@ -10,14 +10,14 @@ import { AlertExecutionStatusErrorReasons } from '../types'; import { executionStatusFromState, executionStatusFromError, - alertExecutionStatusToRaw, - alertExecutionStatusFromRaw, -} from './alert_execution_status'; + ruleExecutionStatusToRaw, + ruleExecutionStatusFromRaw, +} from './rule_execution_status'; import { ErrorWithReason } from './error_with_reason'; const MockLogger = loggingSystemMock.create().get(); -describe('AlertExecutionStatus', () => { +describe('RuleExecutionStatus', () => { beforeEach(() => { jest.resetAllMocks(); }); @@ -71,14 +71,14 @@ describe('AlertExecutionStatus', () => { }); }); - describe('alertExecutionStatusToRaw()', () => { + describe('ruleExecutionStatusToRaw()', () => { const date = new Date('2020-09-03T16:26:58Z'); const status = 'ok'; const reason = AlertExecutionStatusErrorReasons.Decrypt; const error = { reason, message: 'wops' }; test('status without an error', () => { - expect(alertExecutionStatusToRaw({ lastExecutionDate: date, status })).toMatchInlineSnapshot(` + expect(ruleExecutionStatusToRaw({ lastExecutionDate: date, status })).toMatchInlineSnapshot(` Object { "error": null, "lastDuration": 0, @@ -89,7 +89,7 @@ describe('AlertExecutionStatus', () => { }); test('status with an error', () => { - expect(alertExecutionStatusToRaw({ lastExecutionDate: date, status, error })) + expect(ruleExecutionStatusToRaw({ lastExecutionDate: date, status, error })) .toMatchInlineSnapshot(` Object { "error": Object { @@ -104,7 +104,7 @@ describe('AlertExecutionStatus', () => { }); test('status with a duration', () => { - expect(alertExecutionStatusToRaw({ lastExecutionDate: date, status, lastDuration: 1234 })) + expect(ruleExecutionStatusToRaw({ lastExecutionDate: date, status, lastDuration: 1234 })) .toMatchInlineSnapshot(` Object { "error": null, @@ -116,41 +116,41 @@ describe('AlertExecutionStatus', () => { }); }); - describe('alertExecutionStatusFromRaw()', () => { + describe('ruleExecutionStatusFromRaw()', () => { const date = new Date('2020-09-03T16:26:58Z').toISOString(); const status = 'active'; const reason = AlertExecutionStatusErrorReasons.Execute; const error = { reason, message: 'wops' }; test('no input', () => { - const result = alertExecutionStatusFromRaw(MockLogger, 'alert-id'); + const result = ruleExecutionStatusFromRaw(MockLogger, 'rule-id'); expect(result).toBe(undefined); }); test('undefined input', () => { - const result = alertExecutionStatusFromRaw(MockLogger, 'alert-id', undefined); + const result = ruleExecutionStatusFromRaw(MockLogger, 'rule-id', undefined); expect(result).toBe(undefined); }); test('null input', () => { - const result = alertExecutionStatusFromRaw(MockLogger, 'alert-id', null); + const result = ruleExecutionStatusFromRaw(MockLogger, 'rule-id', null); expect(result).toBe(undefined); }); test('invalid date', () => { - const result = alertExecutionStatusFromRaw(MockLogger, 'alert-id', { + const result = ruleExecutionStatusFromRaw(MockLogger, 'rule-id', { lastExecutionDate: 'an invalid date', })!; checkDateIsNearNow(result.lastExecutionDate); expect(result.status).toBe('unknown'); expect(result.error).toBe(undefined); expect(MockLogger.debug).toBeCalledWith( - 'invalid alertExecutionStatus lastExecutionDate "an invalid date" in raw alert alert-id' + 'invalid ruleExecutionStatus lastExecutionDate "an invalid date" in raw rule rule-id' ); }); test('valid date', () => { - const result = alertExecutionStatusFromRaw(MockLogger, 'alert-id', { + const result = ruleExecutionStatusFromRaw(MockLogger, 'rule-id', { lastExecutionDate: date, }); expect(result).toMatchInlineSnapshot(` @@ -162,7 +162,7 @@ describe('AlertExecutionStatus', () => { }); test('valid status and date', () => { - const result = alertExecutionStatusFromRaw(MockLogger, 'alert-id', { + const result = ruleExecutionStatusFromRaw(MockLogger, 'rule-id', { status, lastExecutionDate: date, }); @@ -175,7 +175,7 @@ describe('AlertExecutionStatus', () => { }); test('valid status, date and error', () => { - const result = alertExecutionStatusFromRaw(MockLogger, 'alert-id', { + const result = ruleExecutionStatusFromRaw(MockLogger, 'rule-id', { status, lastExecutionDate: date, error, @@ -193,7 +193,7 @@ describe('AlertExecutionStatus', () => { }); test('valid status, date and duration', () => { - const result = alertExecutionStatusFromRaw(MockLogger, 'alert-id', { + const result = ruleExecutionStatusFromRaw(MockLogger, 'rule-id', { status, lastExecutionDate: date, lastDuration: 1234, @@ -208,7 +208,7 @@ describe('AlertExecutionStatus', () => { }); test('valid status, date, error and duration', () => { - const result = alertExecutionStatusFromRaw(MockLogger, 'alert-id', { + const result = ruleExecutionStatusFromRaw(MockLogger, 'rule-id', { status, lastExecutionDate: date, error, diff --git a/x-pack/plugins/alerting/server/lib/alert_execution_status.ts b/x-pack/plugins/alerting/server/lib/rule_execution_status.ts similarity index 68% rename from x-pack/plugins/alerting/server/lib/alert_execution_status.ts rename to x-pack/plugins/alerting/server/lib/rule_execution_status.ts index 82d8514331704..f631884de12c5 100644 --- a/x-pack/plugins/alerting/server/lib/alert_execution_status.ts +++ b/x-pack/plugins/alerting/server/lib/rule_execution_status.ts @@ -6,16 +6,16 @@ */ import { Logger } from 'src/core/server'; -import { AlertTaskState, AlertExecutionStatus, RawAlertExecutionStatus } from '../types'; +import { RuleTaskState, AlertExecutionStatus, RawRuleExecutionStatus } from '../types'; import { getReasonFromError } from './error_with_reason'; import { getEsErrorMessage } from './errors'; import { AlertExecutionStatuses } from '../../common'; -export function executionStatusFromState(state: AlertTaskState): AlertExecutionStatus { - const instanceIds = Object.keys(state.alertInstances ?? {}); +export function executionStatusFromState(state: RuleTaskState): AlertExecutionStatus { + const alertIds = Object.keys(state.alertInstances ?? {}); return { lastExecutionDate: new Date(), - status: instanceIds.length === 0 ? 'ok' : 'active', + status: alertIds.length === 0 ? 'ok' : 'active', }; } @@ -30,12 +30,12 @@ export function executionStatusFromError(error: Error): AlertExecutionStatus { }; } -export function alertExecutionStatusToRaw({ +export function ruleExecutionStatusToRaw({ lastExecutionDate, lastDuration, status, error, -}: AlertExecutionStatus): RawAlertExecutionStatus { +}: AlertExecutionStatus): RawRuleExecutionStatus { return { lastExecutionDate: lastExecutionDate.toISOString(), lastDuration: lastDuration ?? 0, @@ -45,19 +45,19 @@ export function alertExecutionStatusToRaw({ }; } -export function alertExecutionStatusFromRaw( +export function ruleExecutionStatusFromRaw( logger: Logger, - alertId: string, - rawAlertExecutionStatus?: Partial | null | undefined + ruleId: string, + rawRuleExecutionStatus?: Partial | null | undefined ): AlertExecutionStatus | undefined { - if (!rawAlertExecutionStatus) return undefined; + if (!rawRuleExecutionStatus) return undefined; - const { lastExecutionDate, lastDuration, status = 'unknown', error } = rawAlertExecutionStatus; + const { lastExecutionDate, lastDuration, status = 'unknown', error } = rawRuleExecutionStatus; let parsedDateMillis = lastExecutionDate ? Date.parse(lastExecutionDate) : Date.now(); if (isNaN(parsedDateMillis)) { logger.debug( - `invalid alertExecutionStatus lastExecutionDate "${lastExecutionDate}" in raw alert ${alertId}` + `invalid ruleExecutionStatus lastExecutionDate "${lastExecutionDate}" in raw rule ${ruleId}` ); parsedDateMillis = Date.now(); } @@ -78,7 +78,7 @@ export function alertExecutionStatusFromRaw( return executionStatus; } -export const getAlertExecutionStatusPending = (lastExecutionDate: string) => ({ +export const getRuleExecutionStatusPending = (lastExecutionDate: string) => ({ status: 'pending' as AlertExecutionStatuses, lastExecutionDate, error: null, diff --git a/x-pack/plugins/alerting/server/lib/validate_alert_type_params.test.ts b/x-pack/plugins/alerting/server/lib/validate_rule_type_params.test.ts similarity index 85% rename from x-pack/plugins/alerting/server/lib/validate_alert_type_params.test.ts rename to x-pack/plugins/alerting/server/lib/validate_rule_type_params.test.ts index 6422b8680d0b1..d6f802f047fd2 100644 --- a/x-pack/plugins/alerting/server/lib/validate_alert_type_params.test.ts +++ b/x-pack/plugins/alerting/server/lib/validate_rule_type_params.test.ts @@ -6,17 +6,17 @@ */ import { schema } from '@kbn/config-schema'; -import { validateAlertTypeParams } from './validate_alert_type_params'; +import { validateRuleTypeParams } from './validate_rule_type_params'; test('should return passed in params when validation not defined', () => { - const result = validateAlertTypeParams({ + const result = validateRuleTypeParams({ foo: true, }); expect(result).toEqual({ foo: true }); }); test('should validate and apply defaults when params is valid', () => { - const result = validateAlertTypeParams( + const result = validateRuleTypeParams( { param1: 'value' }, schema.object({ param1: schema.string(), @@ -31,7 +31,7 @@ test('should validate and apply defaults when params is valid', () => { test('should validate and throw error when params is invalid', () => { expect(() => - validateAlertTypeParams( + validateRuleTypeParams( {}, schema.object({ param1: schema.string(), diff --git a/x-pack/plugins/alerting/server/lib/validate_alert_type_params.ts b/x-pack/plugins/alerting/server/lib/validate_rule_type_params.ts similarity index 89% rename from x-pack/plugins/alerting/server/lib/validate_alert_type_params.ts rename to x-pack/plugins/alerting/server/lib/validate_rule_type_params.ts index a2913e8fdbcf3..eef6ecb32c1b1 100644 --- a/x-pack/plugins/alerting/server/lib/validate_alert_type_params.ts +++ b/x-pack/plugins/alerting/server/lib/validate_rule_type_params.ts @@ -8,7 +8,7 @@ import Boom from '@hapi/boom'; import { AlertTypeParams, AlertTypeParamsValidator } from '../types'; -export function validateAlertTypeParams( +export function validateRuleTypeParams( params: Record, validator?: AlertTypeParamsValidator ): Params { diff --git a/x-pack/plugins/alerting/server/plugin.test.ts b/x-pack/plugins/alerting/server/plugin.test.ts index a8da891a3dd14..3716ecfcc1260 100644 --- a/x-pack/plugins/alerting/server/plugin.test.ts +++ b/x-pack/plugins/alerting/server/plugin.test.ts @@ -16,7 +16,7 @@ import { KibanaRequest } from 'kibana/server'; import { featuresPluginMock } from '../../features/server/mocks'; import { KibanaFeature } from '../../features/server'; import { AlertsConfig } from './config'; -import { AlertType } from './types'; +import { RuleType } from './types'; import { eventLogMock } from '../../event_log/server/mocks'; import { actionsMock } from '../../actions/server/mocks'; @@ -99,7 +99,7 @@ describe('Alerting Plugin', () => { describe('registerType()', () => { let setup: PluginSetupContract; - const sampleAlertType: AlertType = { + const sampleRuleType: RuleType = { id: 'test', name: 'test', minimumLicenseRequired: 'basic', @@ -126,7 +126,7 @@ describe('Alerting Plugin', () => { it('should throw error when license type is invalid', async () => { expect(() => setup.registerType({ - ...sampleAlertType, + ...sampleRuleType, // eslint-disable-next-line @typescript-eslint/no-explicit-any minimumLicenseRequired: 'foo' as any, }) @@ -135,52 +135,52 @@ describe('Alerting Plugin', () => { it('should not throw when license type is gold', async () => { setup.registerType({ - ...sampleAlertType, + ...sampleRuleType, minimumLicenseRequired: 'gold', }); }); it('should not throw when license type is basic', async () => { setup.registerType({ - ...sampleAlertType, + ...sampleRuleType, minimumLicenseRequired: 'basic', }); }); it('should apply default config value for ruleTaskTimeout if no value is specified', async () => { const ruleType = { - ...sampleAlertType, + ...sampleRuleType, minimumLicenseRequired: 'basic', - } as AlertType; + } as RuleType; await setup.registerType(ruleType); expect(ruleType.ruleTaskTimeout).toBe('5m'); }); it('should apply value for ruleTaskTimeout if specified', async () => { const ruleType = { - ...sampleAlertType, + ...sampleRuleType, minimumLicenseRequired: 'basic', ruleTaskTimeout: '20h', - } as AlertType; + } as RuleType; await setup.registerType(ruleType); expect(ruleType.ruleTaskTimeout).toBe('20h'); }); it('should apply default config value for cancelAlertsOnRuleTimeout if no value is specified', async () => { const ruleType = { - ...sampleAlertType, + ...sampleRuleType, minimumLicenseRequired: 'basic', - } as AlertType; + } as RuleType; await setup.registerType(ruleType); expect(ruleType.cancelAlertsOnRuleTimeout).toBe(true); }); it('should apply value for cancelAlertsOnRuleTimeout if specified', async () => { const ruleType = { - ...sampleAlertType, + ...sampleRuleType, minimumLicenseRequired: 'basic', cancelAlertsOnRuleTimeout: false, - } as AlertType; + } as RuleType; await setup.registerType(ruleType); expect(ruleType.cancelAlertsOnRuleTimeout).toBe(false); }); diff --git a/x-pack/plugins/alerting/server/plugin.ts b/x-pack/plugins/alerting/server/plugin.ts index 8be96170e664a..b466d1b18ed70 100644 --- a/x-pack/plugins/alerting/server/plugin.ts +++ b/x-pack/plugins/alerting/server/plugin.ts @@ -46,7 +46,7 @@ import { AlertInstanceContext, AlertInstanceState, AlertsHealth, - AlertType, + RuleType, AlertTypeParams, AlertTypeState, Services, @@ -91,7 +91,7 @@ export interface PluginSetupContract { ActionGroupIds extends string = never, RecoveryActionGroupId extends string = never >( - alertType: AlertType< + ruleType: RuleType< Params, ExtractedParams, State, @@ -273,7 +273,7 @@ export class AlertingPlugin { ActionGroupIds extends string = never, RecoveryActionGroupId extends string = never >( - alertType: AlertType< + ruleType: RuleType< Params, ExtractedParams, State, @@ -283,15 +283,15 @@ export class AlertingPlugin { RecoveryActionGroupId > ) { - if (!(alertType.minimumLicenseRequired in LICENSE_TYPE)) { - throw new Error(`"${alertType.minimumLicenseRequired}" is not a valid license type`); + if (!(ruleType.minimumLicenseRequired in LICENSE_TYPE)) { + throw new Error(`"${ruleType.minimumLicenseRequired}" is not a valid license type`); } alertingConfig.then((config) => { - alertType.ruleTaskTimeout = alertType.ruleTaskTimeout ?? config.defaultRuleTaskTimeout; - alertType.cancelAlertsOnRuleTimeout = - alertType.cancelAlertsOnRuleTimeout ?? config.cancelAlertsOnRuleTimeout; - ruleTypeRegistry.register(alertType); + ruleType.ruleTaskTimeout = ruleType.ruleTaskTimeout ?? config.defaultRuleTaskTimeout; + ruleType.cancelAlertsOnRuleTimeout = + ruleType.cancelAlertsOnRuleTimeout ?? config.cancelAlertsOnRuleTimeout; + ruleTypeRegistry.register(ruleType); }); }, getSecurityHealth: async () => { @@ -390,7 +390,7 @@ export class AlertingPlugin { ruleTypeRegistry: this.ruleTypeRegistry!, kibanaBaseUrl: this.kibanaBaseUrl, supportsEphemeralTasks: plugins.taskManager.supportsEphemeralTasks(), - maxEphemeralActionsPerAlert: config.maxEphemeralActionsPerAlert, + maxEphemeralActionsPerRule: config.maxEphemeralActionsPerAlert, cancelAlertsOnRuleTimeout: config.cancelAlertsOnRuleTimeout, }); }); diff --git a/x-pack/plugins/alerting/server/routes/_mock_handler_arguments.ts b/x-pack/plugins/alerting/server/routes/_mock_handler_arguments.ts index c19beee0e841f..c94d46e3a4558 100644 --- a/x-pack/plugins/alerting/server/routes/_mock_handler_arguments.ts +++ b/x-pack/plugins/alerting/server/routes/_mock_handler_arguments.ts @@ -10,7 +10,7 @@ import { identity } from 'lodash'; import type { MethodKeysOf } from '@kbn/utility-types'; import { httpServerMock } from '../../../../../src/core/server/mocks'; import { rulesClientMock, RulesClientMock } from '../rules_client.mock'; -import { AlertsHealth, AlertType } from '../../common'; +import { AlertsHealth, RuleType } from '../../common'; import type { AlertingRequestHandlerContext } from '../types'; export function mockHandlerArguments( @@ -21,7 +21,7 @@ export function mockHandlerArguments( areApiKeysEnabled, }: { rulesClient?: RulesClientMock; - listTypes?: AlertType[]; + listTypes?: RuleType[]; getFrameworkHealth?: jest.MockInstance, []> & (() => Promise); areApiKeysEnabled?: () => Promise; diff --git a/x-pack/plugins/alerting/server/routes/get_rule_state.ts b/x-pack/plugins/alerting/server/routes/get_rule_state.ts index 6337bed39f150..1ebe3ab367443 100644 --- a/x-pack/plugins/alerting/server/routes/get_rule_state.ts +++ b/x-pack/plugins/alerting/server/routes/get_rule_state.ts @@ -12,14 +12,14 @@ import { RewriteResponseCase, verifyAccessAndContext } from './lib'; import { AlertingRequestHandlerContext, INTERNAL_BASE_ALERTING_API_PATH, - AlertTaskState, + RuleTaskState, } from '../types'; const paramSchema = schema.object({ id: schema.string(), }); -const rewriteBodyRes: RewriteResponseCase = ({ +const rewriteBodyRes: RewriteResponseCase = ({ alertTypeState, alertInstances, previousStartedAt, diff --git a/x-pack/plugins/alerting/server/rule_type_registry.test.ts b/x-pack/plugins/alerting/server/rule_type_registry.test.ts index 895a5047339ef..e23c7f25a4f76 100644 --- a/x-pack/plugins/alerting/server/rule_type_registry.test.ts +++ b/x-pack/plugins/alerting/server/rule_type_registry.test.ts @@ -7,7 +7,7 @@ import { TaskRunnerFactory } from './task_runner'; import { RuleTypeRegistry, ConstructorOptions } from './rule_type_registry'; -import { ActionGroup, AlertType } from './types'; +import { ActionGroup, RuleType } from './types'; import { taskManagerMock } from '../../task_manager/server/mocks'; import { ILicenseState } from './lib/license_state'; import { licenseStateMock } from './lib/license_state.mock'; @@ -56,8 +56,8 @@ describe('has()', () => { }); describe('register()', () => { - test('throws if AlertType Id contains invalid characters', () => { - const alertType: AlertType = { + test('throws if RuleType Id contains invalid characters', () => { + const ruleType: RuleType = { id: 'test', name: 'Test', actionGroups: [ @@ -76,21 +76,21 @@ describe('register()', () => { const invalidCharacters = [' ', ':', '*', '*', '/']; for (const char of invalidCharacters) { - expect(() => registry.register({ ...alertType, id: `${alertType.id}${char}` })).toThrowError( - new Error(`expected AlertType Id not to include invalid character: ${char}`) + expect(() => registry.register({ ...ruleType, id: `${ruleType.id}${char}` })).toThrowError( + new Error(`expected RuleType Id not to include invalid character: ${char}`) ); } const [first, second] = invalidCharacters; expect(() => - registry.register({ ...alertType, id: `${first}${alertType.id}${second}` }) + registry.register({ ...ruleType, id: `${first}${ruleType.id}${second}` }) ).toThrowError( - new Error(`expected AlertType Id not to include invalid characters: ${first}, ${second}`) + new Error(`expected RuleType Id not to include invalid characters: ${first}, ${second}`) ); }); - test('throws if AlertType Id isnt a string', () => { - const alertType: AlertType = { + test('throws if RuleType Id isnt a string', () => { + const ruleType: RuleType = { id: 123 as unknown as string, name: 'Test', actionGroups: [ @@ -107,13 +107,13 @@ describe('register()', () => { }; const registry = new RuleTypeRegistry(ruleTypeRegistryParams); - expect(() => registry.register(alertType)).toThrowError( + expect(() => registry.register(ruleType)).toThrowError( new Error(`expected value of type [string] but got [number]`) ); }); - test('throws if AlertType ruleTaskTimeout is not a valid duration', () => { - const alertType: AlertType = { + test('throws if RuleType ruleTaskTimeout is not a valid duration', () => { + const ruleType: RuleType = { id: '123', name: 'Test', actionGroups: [ @@ -131,7 +131,7 @@ describe('register()', () => { }; const registry = new RuleTypeRegistry(ruleTypeRegistryParams); - expect(() => registry.register(alertType)).toThrowError( + expect(() => registry.register(ruleType)).toThrowError( new Error( `Rule type \"123\" has invalid timeout: string is not a valid duration: 23 milisec.` ) @@ -139,7 +139,7 @@ describe('register()', () => { }); test('throws if defaultScheduleInterval isnt valid', () => { - const alertType: AlertType = { + const ruleType: RuleType = { id: '123', name: 'Test', actionGroups: [ @@ -158,7 +158,7 @@ describe('register()', () => { }; const registry = new RuleTypeRegistry(ruleTypeRegistryParams); - expect(() => registry.register(alertType)).toThrowError( + expect(() => registry.register(ruleType)).toThrowError( new Error( `Rule type \"123\" has invalid default interval: string is not a valid duration: foobar.` ) @@ -166,7 +166,7 @@ describe('register()', () => { }); test('throws if minimumScheduleInterval isnt valid', () => { - const alertType: AlertType = { + const ruleType: RuleType = { id: '123', name: 'Test', actionGroups: [ @@ -184,7 +184,7 @@ describe('register()', () => { }; const registry = new RuleTypeRegistry(ruleTypeRegistryParams); - expect(() => registry.register(alertType)).toThrowError( + expect(() => registry.register(ruleType)).toThrowError( new Error( `Rule type \"123\" has invalid minimum interval: string is not a valid duration: foobar.` ) @@ -192,7 +192,7 @@ describe('register()', () => { }); test('throws if RuleType action groups contains reserved group id', () => { - const alertType: AlertType = { + const ruleType: RuleType = { id: 'test', name: 'Test', actionGroups: [ @@ -217,15 +217,15 @@ describe('register()', () => { }; const registry = new RuleTypeRegistry(ruleTypeRegistryParams); - expect(() => registry.register(alertType)).toThrowError( + expect(() => registry.register(ruleType)).toThrowError( new Error( - `Rule type [id="${alertType.id}"] cannot be registered. Action groups [recovered] are reserved by the framework.` + `Rule type [id="${ruleType.id}"] cannot be registered. Action groups [recovered] are reserved by the framework.` ) ); }); - test('allows an AlertType to specify a custom recovery group', () => { - const alertType: AlertType = { + test('allows an RuleType to specify a custom recovery group', () => { + const ruleType: RuleType = { id: 'test', name: 'Test', actionGroups: [ @@ -245,7 +245,7 @@ describe('register()', () => { isExportable: true, }; const registry = new RuleTypeRegistry(ruleTypeRegistryParams); - registry.register(alertType); + registry.register(ruleType); expect(registry.get('test').actionGroups).toMatchInlineSnapshot(` Array [ Object { @@ -260,8 +260,8 @@ describe('register()', () => { `); }); - test('allows an AlertType to specify a custom rule task timeout', () => { - const alertType: AlertType = { + test('allows an RuleType to specify a custom rule task timeout', () => { + const ruleType: RuleType = { id: 'test', name: 'Test', actionGroups: [ @@ -278,12 +278,12 @@ describe('register()', () => { isExportable: true, }; const registry = new RuleTypeRegistry(ruleTypeRegistryParams); - registry.register(alertType); + registry.register(ruleType); expect(registry.get('test').ruleTaskTimeout).toBe('13m'); }); - test('throws if the custom recovery group is contained in the AlertType action groups', () => { - const alertType: AlertType< + test('throws if the custom recovery group is contained in the RuleType action groups', () => { + const ruleType: RuleType< never, never, never, @@ -316,15 +316,15 @@ describe('register()', () => { }; const registry = new RuleTypeRegistry(ruleTypeRegistryParams); - expect(() => registry.register(alertType)).toThrowError( + expect(() => registry.register(ruleType)).toThrowError( new Error( - `Rule type [id="${alertType.id}"] cannot be registered. Action group [backToAwesome] cannot be used as both a recovery and an active action group.` + `Rule type [id="${ruleType.id}"] cannot be registered. Action group [backToAwesome] cannot be used as both a recovery and an active action group.` ) ); }); test('registers the executor with the task manager', () => { - const alertType: AlertType = { + const ruleType: RuleType = { id: 'test', name: 'Test', actionGroups: [ @@ -341,7 +341,7 @@ describe('register()', () => { ruleTaskTimeout: '20m', }; const registry = new RuleTypeRegistry(ruleTypeRegistryParams); - registry.register(alertType); + registry.register(ruleType); expect(taskManager.registerTaskDefinitions).toHaveBeenCalledTimes(1); expect(taskManager.registerTaskDefinitions.mock.calls[0]).toMatchInlineSnapshot(` Array [ @@ -357,7 +357,7 @@ describe('register()', () => { }); test('shallow clones the given rule type', () => { - const alertType: AlertType = { + const ruleType: RuleType = { id: 'test', name: 'Test', actionGroups: [ @@ -373,8 +373,8 @@ describe('register()', () => { producer: 'alerts', }; const registry = new RuleTypeRegistry(ruleTypeRegistryParams); - registry.register(alertType); - alertType.name = 'Changed'; + registry.register(ruleType); + ruleType.name = 'Changed'; expect(registry.get('test').name).toEqual('Test'); }); @@ -433,8 +433,8 @@ describe('get()', () => { executor: jest.fn(), producer: 'alerts', }); - const alertType = registry.get('test'); - expect(alertType).toMatchInlineSnapshot(` + const ruleType = registry.get('test'); + expect(ruleType).toMatchInlineSnapshot(` Object { "actionGroups": Array [ Object { @@ -539,12 +539,12 @@ describe('list()', () => { test('should return action variables state and empty context', () => { const registry = new RuleTypeRegistry(ruleTypeRegistryParams); - registry.register(alertTypeWithVariables('x', '', 's')); - const alertType = registry.get('x'); - expect(alertType.actionVariables).toBeTruthy(); + registry.register(ruleTypeWithVariables('x', '', 's')); + const ruleType = registry.get('x'); + expect(ruleType.actionVariables).toBeTruthy(); - const context = alertType.actionVariables!.context; - const state = alertType.actionVariables!.state; + const context = ruleType.actionVariables!.context; + const state = ruleType.actionVariables!.state; expect(context).toBeTruthy(); expect(context!.length).toBe(0); @@ -556,12 +556,12 @@ describe('list()', () => { test('should return action variables context and empty state', () => { const registry = new RuleTypeRegistry(ruleTypeRegistryParams); - registry.register(alertTypeWithVariables('x', 'c', '')); - const alertType = registry.get('x'); - expect(alertType.actionVariables).toBeTruthy(); + registry.register(ruleTypeWithVariables('x', 'c', '')); + const ruleType = registry.get('x'); + expect(ruleType.actionVariables).toBeTruthy(); - const context = alertType.actionVariables!.context; - const state = alertType.actionVariables!.state; + const context = ruleType.actionVariables!.context; + const state = ruleType.actionVariables!.state; expect(state).toBeTruthy(); expect(state!.length).toBe(0); @@ -597,11 +597,11 @@ describe('ensureRuleTypeEnabled', () => { test('should call ensureLicenseForAlertType on the license state', async () => { ruleTypeRegistry.ensureRuleTypeEnabled('test'); - expect(mockedLicenseState.ensureLicenseForAlertType).toHaveBeenCalled(); + expect(mockedLicenseState.ensureLicenseForRuleType).toHaveBeenCalled(); }); test('should throw when ensureLicenseForAlertType throws', async () => { - mockedLicenseState.ensureLicenseForAlertType.mockImplementation(() => { + mockedLicenseState.ensureLicenseForRuleType.mockImplementation(() => { throw new Error('Fail'); }); expect(() => ruleTypeRegistry.ensureRuleTypeEnabled('test')).toThrowErrorMatchingInlineSnapshot( @@ -610,12 +610,12 @@ describe('ensureRuleTypeEnabled', () => { }); }); -function alertTypeWithVariables( +function ruleTypeWithVariables( id: ActionGroupIds, context: string, state: string -): AlertType { - const baseAlert: AlertType = { +): RuleType { + const baseAlert: RuleType = { id, name: `${id}-name`, actionGroups: [], diff --git a/x-pack/plugins/alerting/server/rule_type_registry.ts b/x-pack/plugins/alerting/server/rule_type_registry.ts index 452729a9a01e9..9b4f94f3510be 100644 --- a/x-pack/plugins/alerting/server/rule_type_registry.ts +++ b/x-pack/plugins/alerting/server/rule_type_registry.ts @@ -14,7 +14,7 @@ import { LicensingPluginSetup } from '../../licensing/server'; import { RunContext, TaskManagerSetupContract } from '../../task_manager/server'; import { TaskRunnerFactory } from './task_runner'; import { - AlertType, + RuleType, AlertTypeParams, AlertTypeState, AlertInstanceState, @@ -28,7 +28,7 @@ import { validateDurationSchema, } from '../common'; import { ILicenseState } from './lib/license_state'; -import { getAlertTypeFeatureUsageName } from './lib/get_alert_type_feature_usage_name'; +import { getRuleTypeFeatureUsageName } from './lib/get_rule_type_feature_usage_name'; export interface ConstructorOptions { taskManager: TaskManagerSetupContract; @@ -39,7 +39,7 @@ export interface ConstructorOptions { export interface RegistryRuleType extends Pick< - UntypedNormalizedAlertType, + UntypedNormalizedRuleType, | 'name' | 'actionGroups' | 'recoveryActionGroup' @@ -57,26 +57,26 @@ export interface RegistryRuleType } /** - * AlertType IDs are used as part of the authorization strings used to + * RuleType IDs are used as part of the authorization strings used to * grant users privileged operations. There is a limited range of characters * we can use in these auth strings, so we apply these same limitations to - * the AlertType Ids. + * the RuleType Ids. * If you wish to change this, please confer with the Kibana security team. */ -const alertIdSchema = schema.string({ +const ruleTypeIdSchema = schema.string({ validate(value: string): string | void { if (typeof value !== 'string') { - return `expected AlertType Id of type [string] but got [${typeDetect(value)}]`; + return `expected RuleType Id of type [string] but got [${typeDetect(value)}]`; } else if (!value.match(/^[a-zA-Z0-9_\-\.]*$/)) { const invalid = value.match(/[^a-zA-Z0-9_\-\.]+/g)!; - return `expected AlertType Id not to include invalid character${ + return `expected RuleType Id not to include invalid character${ invalid.length > 1 ? `s` : `` }: ${invalid?.join(`, `)}`; } }, }); -export type NormalizedAlertType< +export type NormalizedRuleType< Params extends AlertTypeParams, ExtractedParams extends AlertTypeParams, State extends AlertTypeState, @@ -87,7 +87,7 @@ export type NormalizedAlertType< > = { actionGroups: Array>; } & Omit< - AlertType< + RuleType< Params, ExtractedParams, State, @@ -100,7 +100,7 @@ export type NormalizedAlertType< > & Pick< Required< - AlertType< + RuleType< Params, ExtractedParams, State, @@ -113,7 +113,7 @@ export type NormalizedAlertType< 'recoveryActionGroup' >; -export type UntypedNormalizedAlertType = NormalizedAlertType< +export type UntypedNormalizedRuleType = NormalizedRuleType< AlertTypeParams, AlertTypeParams, AlertTypeState, @@ -125,7 +125,7 @@ export type UntypedNormalizedAlertType = NormalizedAlertType< export class RuleTypeRegistry { private readonly taskManager: TaskManagerSetupContract; - private readonly ruleTypes: Map = new Map(); + private readonly ruleTypes: Map = new Map(); private readonly taskRunnerFactory: TaskRunnerFactory; private readonly licenseState: ILicenseState; private readonly licensing: LicensingPluginSetup; @@ -142,7 +142,7 @@ export class RuleTypeRegistry { } public ensureRuleTypeEnabled(id: string) { - this.licenseState.ensureLicenseForAlertType(this.get(id)); + this.licenseState.ensureLicenseForRuleType(this.get(id)); } public register< @@ -154,7 +154,7 @@ export class RuleTypeRegistry { ActionGroupIds extends string, RecoveryActionGroupId extends string >( - alertType: AlertType< + ruleType: RuleType< Params, ExtractedParams, State, @@ -164,44 +164,44 @@ export class RuleTypeRegistry { RecoveryActionGroupId > ) { - if (this.has(alertType.id)) { + if (this.has(ruleType.id)) { throw new Error( - i18n.translate('xpack.alerting.ruleTypeRegistry.register.duplicateAlertTypeError', { + i18n.translate('xpack.alerting.ruleTypeRegistry.register.duplicateRuleTypeError', { defaultMessage: 'Rule type "{id}" is already registered.', values: { - id: alertType.id, + id: ruleType.id, }, }) ); } // validate ruleTypeTimeout here - if (alertType.ruleTaskTimeout) { - const invalidTimeout = validateDurationSchema(alertType.ruleTaskTimeout); + if (ruleType.ruleTaskTimeout) { + const invalidTimeout = validateDurationSchema(ruleType.ruleTaskTimeout); if (invalidTimeout) { throw new Error( - i18n.translate('xpack.alerting.ruleTypeRegistry.register.invalidTimeoutAlertTypeError', { + i18n.translate('xpack.alerting.ruleTypeRegistry.register.invalidTimeoutRuleTypeError', { defaultMessage: 'Rule type "{id}" has invalid timeout: {errorMessage}.', values: { - id: alertType.id, + id: ruleType.id, errorMessage: invalidTimeout, }, }) ); } } - alertType.actionVariables = normalizedActionVariables(alertType.actionVariables); + ruleType.actionVariables = normalizedActionVariables(ruleType.actionVariables); // validate defaultScheduleInterval here - if (alertType.defaultScheduleInterval) { - const invalidDefaultTimeout = validateDurationSchema(alertType.defaultScheduleInterval); + if (ruleType.defaultScheduleInterval) { + const invalidDefaultTimeout = validateDurationSchema(ruleType.defaultScheduleInterval); if (invalidDefaultTimeout) { throw new Error( i18n.translate( - 'xpack.alerting.ruleTypeRegistry.register.invalidDefaultTimeoutAlertTypeError', + 'xpack.alerting.ruleTypeRegistry.register.invalidDefaultTimeoutRuleTypeError', { defaultMessage: 'Rule type "{id}" has invalid default interval: {errorMessage}.', values: { - id: alertType.id, + id: ruleType.id, errorMessage: invalidDefaultTimeout, }, } @@ -211,16 +211,16 @@ export class RuleTypeRegistry { } // validate minimumScheduleInterval here - if (alertType.minimumScheduleInterval) { - const invalidMinimumTimeout = validateDurationSchema(alertType.minimumScheduleInterval); + if (ruleType.minimumScheduleInterval) { + const invalidMinimumTimeout = validateDurationSchema(ruleType.minimumScheduleInterval); if (invalidMinimumTimeout) { throw new Error( i18n.translate( - 'xpack.alerting.ruleTypeRegistry.register.invalidMinimumTimeoutAlertTypeError', + 'xpack.alerting.ruleTypeRegistry.register.invalidMinimumTimeoutRuleTypeError', { defaultMessage: 'Rule type "{id}" has invalid minimum interval: {errorMessage}.', values: { - id: alertType.id, + id: ruleType.id, errorMessage: invalidMinimumTimeout, }, } @@ -229,7 +229,7 @@ export class RuleTypeRegistry { } } - const normalizedAlertType = augmentActionGroupsWithReserved< + const normalizedRuleType = augmentActionGroupsWithReserved< Params, ExtractedParams, State, @@ -237,17 +237,17 @@ export class RuleTypeRegistry { InstanceContext, ActionGroupIds, RecoveryActionGroupId - >(alertType); + >(ruleType); this.ruleTypes.set( - alertIdSchema.validate(alertType.id), - /** stripping the typing is required in order to store the AlertTypes in a Map */ - normalizedAlertType as unknown as UntypedNormalizedAlertType + ruleTypeIdSchema.validate(ruleType.id), + /** stripping the typing is required in order to store the RuleTypes in a Map */ + normalizedRuleType as unknown as UntypedNormalizedRuleType ); this.taskManager.registerTaskDefinitions({ - [`alerting:${alertType.id}`]: { - title: alertType.name, - timeout: alertType.ruleTaskTimeout, + [`alerting:${ruleType.id}`]: { + title: ruleType.name, + timeout: ruleType.ruleTaskTimeout, createTaskRunner: (context: RunContext) => this.taskRunnerFactory.create< Params, @@ -257,14 +257,14 @@ export class RuleTypeRegistry { InstanceContext, ActionGroupIds, RecoveryActionGroupId | RecoveredActionGroupId - >(normalizedAlertType, context), + >(normalizedRuleType, context), }, }); // No need to notify usage on basic alert types - if (alertType.minimumLicenseRequired !== 'basic') { + if (ruleType.minimumLicenseRequired !== 'basic') { this.licensing.featureUsage.register( - getAlertTypeFeatureUsageName(alertType.name), - alertType.minimumLicenseRequired + getRuleTypeFeatureUsageName(ruleType.name), + ruleType.minimumLicenseRequired ); } } @@ -279,7 +279,7 @@ export class RuleTypeRegistry { RecoveryActionGroupId extends string = string >( id: string - ): NormalizedAlertType< + ): NormalizedRuleType< Params, ExtractedParams, State, @@ -290,7 +290,7 @@ export class RuleTypeRegistry { > { if (!this.has(id)) { throw Boom.badRequest( - i18n.translate('xpack.alerting.ruleTypeRegistry.get.missingAlertTypeError', { + i18n.translate('xpack.alerting.ruleTypeRegistry.get.missingRuleTypeError', { defaultMessage: 'Rule type "{id}" is not registered.', values: { id, @@ -299,11 +299,11 @@ export class RuleTypeRegistry { ); } /** - * When we store the AlertTypes in the Map we strip the typing. - * This means that returning a typed AlertType in `get` is an inherently + * When we store the RuleTypes in the Map we strip the typing. + * This means that returning a typed RuleType in `get` is an inherently * unsafe operation. Down casting to `unknown` is the only way to achieve this. */ - return this.ruleTypes.get(id)! as unknown as NormalizedAlertType< + return this.ruleTypes.get(id)! as unknown as NormalizedRuleType< Params, ExtractedParams, State, @@ -332,7 +332,7 @@ export class RuleTypeRegistry { minimumScheduleInterval, defaultScheduleInterval, }, - ]: [string, UntypedNormalizedAlertType]) => ({ + ]: [string, UntypedNormalizedRuleType]) => ({ id, name, actionGroups, @@ -345,7 +345,7 @@ export class RuleTypeRegistry { ruleTaskTimeout, minimumScheduleInterval, defaultScheduleInterval, - enabledInLicense: !!this.licenseState.getLicenseCheckForAlertType( + enabledInLicense: !!this.licenseState.getLicenseCheckForRuleType( id, name, minimumLicenseRequired @@ -356,7 +356,7 @@ export class RuleTypeRegistry { } } -function normalizedActionVariables(actionVariables: AlertType['actionVariables']) { +function normalizedActionVariables(actionVariables: RuleType['actionVariables']) { return { context: actionVariables?.context ?? [], state: actionVariables?.state ?? [], @@ -373,7 +373,7 @@ function augmentActionGroupsWithReserved< ActionGroupIds extends string, RecoveryActionGroupId extends string >( - alertType: AlertType< + ruleType: RuleType< Params, ExtractedParams, State, @@ -382,7 +382,7 @@ function augmentActionGroupsWithReserved< ActionGroupIds, RecoveryActionGroupId > -): NormalizedAlertType< +): NormalizedRuleType< Params, ExtractedParams, State, @@ -391,8 +391,8 @@ function augmentActionGroupsWithReserved< ActionGroupIds, RecoveredActionGroupId | RecoveryActionGroupId > { - const reservedActionGroups = getBuiltinActionGroups(alertType.recoveryActionGroup); - const { id, actionGroups, recoveryActionGroup } = alertType; + const reservedActionGroups = getBuiltinActionGroups(ruleType.recoveryActionGroup); + const { id, actionGroups, recoveryActionGroup } = ruleType; const activeActionGroups = new Set(actionGroups.map((item) => item.id)); const intersectingReservedActionGroups = intersection( @@ -427,7 +427,7 @@ function augmentActionGroupsWithReserved< } return { - ...alertType, + ...ruleType, actionGroups: [...actionGroups, ...reservedActionGroups], recoveryActionGroup: recoveryActionGroup ?? RecoveredActionGroup, }; diff --git a/x-pack/plugins/alerting/server/rules_client/rules_client.ts b/x-pack/plugins/alerting/server/rules_client/rules_client.ts index 674f659ba6a87..e182a5c2b0058 100644 --- a/x-pack/plugins/alerting/server/rules_client/rules_client.ts +++ b/x-pack/plugins/alerting/server/rules_client/rules_client.ts @@ -24,27 +24,23 @@ import { ActionsClient, ActionsAuthorization } from '../../../actions/server'; import { Alert, PartialAlert, - RawAlert, + RawRule, RuleTypeRegistry, AlertAction, IntervalSchedule, SanitizedAlert, - AlertTaskState, + RuleTaskState, AlertSummary, AlertExecutionStatusValues, AlertNotifyWhenType, AlertTypeParams, ResolvedSanitizedRule, AlertWithLegacyId, - SanitizedAlertWithLegacyId, + SanitizedRuleWithLegacyId, PartialAlertWithLegacyId, RawAlertInstance, } from '../types'; -import { - validateAlertTypeParams, - alertExecutionStatusFromRaw, - getAlertNotifyWhenType, -} from '../lib'; +import { validateRuleTypeParams, ruleExecutionStatusFromRaw, getAlertNotifyWhenType } from '../lib'; import { GrantAPIKeyResult as SecurityPluginGrantAPIKeyResult, InvalidateAPIKeyResult as SecurityPluginInvalidateAPIKeyResult, @@ -52,7 +48,7 @@ import { import { EncryptedSavedObjectsClient } from '../../../encrypted_saved_objects/server'; import { TaskManagerStartContract } from '../../../task_manager/server'; import { taskInstanceToAlertTaskInstance } from '../task_runner/alert_task_instance'; -import { RegistryRuleType, UntypedNormalizedAlertType } from '../rule_type_registry'; +import { RegistryRuleType, UntypedNormalizedRuleType } from '../rule_type_registry'; import { AlertingAuthorization, WriteOperations, @@ -77,7 +73,7 @@ import { markApiKeyForInvalidation } from '../invalidate_pending_api_keys/mark_a import { ruleAuditEvent, RuleAuditAction } from './audit_events'; import { KueryNode, nodeBuilder } from '../../../../../src/plugins/data/common'; import { mapSortField } from './lib'; -import { getAlertExecutionStatusPending } from '../lib/alert_execution_status'; +import { getRuleExecutionStatusPending } from '../lib/rule_execution_status'; import { AlertInstance } from '../alert_instance'; import { EVENT_LOG_ACTIONS } from '../plugin'; import { createAlertEventLogRecordObject } from '../lib/create_alert_event_log_record_object'; @@ -316,10 +312,7 @@ export class RulesClient { // Throws an error if alert type isn't registered const ruleType = this.ruleTypeRegistry.get(data.alertTypeId); - const validatedAlertTypeParams = validateAlertTypeParams( - data.params, - ruleType.validate?.params - ); + const validatedAlertTypeParams = validateRuleTypeParams(data.params, ruleType.validate?.params); const username = await this.getUserName(); let createdAPIKey = null; @@ -355,7 +348,7 @@ export class RulesClient { const legacyId = Semver.lt(this.kibanaVersion, '8.0.0') ? id : null; const notifyWhen = getAlertNotifyWhenType(data.notifyWhen, data.throttle); - const rawAlert: RawAlert = { + const rawRule: RawRule = { ...data, ...this.apiKeyAsAlertAttributes(createdAPIKey, username), legacyId, @@ -364,11 +357,11 @@ export class RulesClient { updatedBy: username, createdAt: new Date(createTime).toISOString(), updatedAt: new Date(createTime).toISOString(), - params: updatedParams as RawAlert['params'], + params: updatedParams as RawRule['params'], muteAll: false, mutedInstanceIds: [], notifyWhen, - executionStatus: getAlertExecutionStatusPending(new Date().toISOString()), + executionStatus: getRuleExecutionStatusPending(new Date().toISOString()), }; this.auditLogger?.log( @@ -379,11 +372,11 @@ export class RulesClient { }) ); - let createdAlert: SavedObject; + let createdAlert: SavedObject; try { createdAlert = await this.unsecuredSavedObjectsClient.create( 'alert', - this.updateMeta(rawAlert), + this.updateMeta(rawRule), { ...options, references, @@ -393,7 +386,7 @@ export class RulesClient { } catch (e) { // Avoid unused API key markApiKeyForInvalidation( - { apiKey: rawAlert.apiKey }, + { apiKey: rawRule.apiKey }, this.logger, this.unsecuredSavedObjectsClient ); @@ -404,7 +397,7 @@ export class RulesClient { try { scheduledTask = await this.scheduleRule( createdAlert.id, - rawAlert.alertTypeId, + rawRule.alertTypeId, data.schedule, true ); @@ -420,7 +413,7 @@ export class RulesClient { } throw e; } - await this.unsecuredSavedObjectsClient.update('alert', createdAlert.id, { + await this.unsecuredSavedObjectsClient.update('alert', createdAlert.id, { scheduledTaskId: scheduledTask.id, }); createdAlert.attributes.scheduledTaskId = scheduledTask.id; @@ -439,8 +432,8 @@ export class RulesClient { }: { id: string; includeLegacyId?: boolean; - }): Promise | SanitizedAlertWithLegacyId> { - const result = await this.unsecuredSavedObjectsClient.get('alert', id); + }): Promise | SanitizedRuleWithLegacyId> { + const result = await this.unsecuredSavedObjectsClient.get('alert', id); try { await this.authorization.ensureAuthorized({ ruleTypeId: result.attributes.alertTypeId, @@ -481,7 +474,7 @@ export class RulesClient { includeLegacyId?: boolean; }): Promise> { const { saved_object: result, ...resolveResponse } = - await this.unsecuredSavedObjectsClient.resolve('alert', id); + await this.unsecuredSavedObjectsClient.resolve('alert', id); try { await this.authorization.ensureAuthorized({ ruleTypeId: result.attributes.alertTypeId, @@ -520,7 +513,7 @@ export class RulesClient { }; } - public async getAlertState({ id }: { id: string }): Promise { + public async getAlertState({ id }: { id: string }): Promise { const alert = await this.get({ id }); await this.authorization.ensureAuthorized({ ruleTypeId: alert.alertTypeId, @@ -539,7 +532,7 @@ export class RulesClient { public async getAlertSummary({ id, dateStart }: GetAlertSummaryParams): Promise { this.logger.debug(`getAlertSummary(): getting alert ${id}`); - const rule = (await this.get({ id, includeLegacyId: true })) as SanitizedAlertWithLegacyId; + const rule = (await this.get({ id, includeLegacyId: true })) as SanitizedRuleWithLegacyId; await this.authorization.ensureAuthorized({ ruleTypeId: rule.alertTypeId, @@ -612,7 +605,7 @@ export class RulesClient { per_page: perPage, total, saved_objects: data, - } = await this.unsecuredSavedObjectsClient.find({ + } = await this.unsecuredSavedObjectsClient.find({ ...options, sortField: mapSortField(options.sortField), filter: @@ -646,7 +639,7 @@ export class RulesClient { return this.getAlertFromRaw( id, attributes.alertTypeId, - fields ? (pick(attributes, fields) as RawAlert) : attributes, + fields ? (pick(attributes, fields) as RawRule) : attributes, references ); }); @@ -687,7 +680,7 @@ export class RulesClient { throw error; } const { filter: authorizationFilter } = authorizationTuple; - const resp = await this.unsecuredSavedObjectsClient.find({ + const resp = await this.unsecuredSavedObjectsClient.find({ ...options, filter: (authorizationFilter && filter @@ -776,11 +769,11 @@ export class RulesClient { private async deleteWithOCC({ id }: { id: string }) { let taskIdToRemove: string | undefined | null; let apiKeyToInvalidate: string | null = null; - let attributes: RawAlert; + let attributes: RawRule; try { const decryptedAlert = - await this.encryptedSavedObjectsClient.getDecryptedAsInternalUser('alert', id, { + await this.encryptedSavedObjectsClient.getDecryptedAsInternalUser('alert', id, { namespace: this.namespace, }); apiKeyToInvalidate = decryptedAlert.attributes.apiKey; @@ -792,7 +785,7 @@ export class RulesClient { `delete(): Failed to load API key to invalidate on alert ${id}: ${e.message}` ); // Still attempt to load the scheduledTaskId using SOC - const alert = await this.unsecuredSavedObjectsClient.get('alert', id); + const alert = await this.unsecuredSavedObjectsClient.get('alert', id); taskIdToRemove = alert.attributes.scheduledTaskId; attributes = alert.attributes; } @@ -854,20 +847,23 @@ export class RulesClient { id, data, }: UpdateOptions): Promise> { - let alertSavedObject: SavedObject; + let alertSavedObject: SavedObject; try { - alertSavedObject = - await this.encryptedSavedObjectsClient.getDecryptedAsInternalUser('alert', id, { + alertSavedObject = await this.encryptedSavedObjectsClient.getDecryptedAsInternalUser( + 'alert', + id, + { namespace: this.namespace, - }); + } + ); } catch (e) { // We'll skip invalidating the API key since we failed to load the decrypted saved object this.logger.error( `update(): Failed to load API key to invalidate on alert ${id}: ${e.message}` ); // Still attempt to load the object using SOC - alertSavedObject = await this.unsecuredSavedObjectsClient.get('alert', id); + alertSavedObject = await this.unsecuredSavedObjectsClient.get('alert', id); } try { @@ -934,15 +930,12 @@ export class RulesClient { private async updateAlert( { id, data }: UpdateOptions, - { attributes, version }: SavedObject + { attributes, version }: SavedObject ): Promise> { const ruleType = this.ruleTypeRegistry.get(attributes.alertTypeId); // Validate - const validatedAlertTypeParams = validateAlertTypeParams( - data.params, - ruleType.validate?.params - ); + const validatedAlertTypeParams = validateRuleTypeParams(data.params, ruleType.validate?.params); await this.validateActions(ruleType, data.actions); // Validate intervals, if configured @@ -977,19 +970,19 @@ export class RulesClient { const apiKeyAttributes = this.apiKeyAsAlertAttributes(createdAPIKey, username); const notifyWhen = getAlertNotifyWhenType(data.notifyWhen, data.throttle); - let updatedObject: SavedObject; + let updatedObject: SavedObject; const createAttributes = this.updateMeta({ ...attributes, ...data, ...apiKeyAttributes, - params: updatedParams as RawAlert['params'], + params: updatedParams as RawRule['params'], actions, notifyWhen, updatedBy: username, updatedAt: new Date().toISOString(), }); try { - updatedObject = await this.unsecuredSavedObjectsClient.create( + updatedObject = await this.unsecuredSavedObjectsClient.create( 'alert', createAttributes, { @@ -1020,7 +1013,7 @@ export class RulesClient { private apiKeyAsAlertAttributes( apiKey: CreateAPIKeyResult | null, username: string | null - ): Pick { + ): Pick { return apiKey && apiKey.apiKeysEnabled ? { apiKeyOwner: username, @@ -1042,12 +1035,12 @@ export class RulesClient { private async updateApiKeyWithOCC({ id }: { id: string }) { let apiKeyToInvalidate: string | null = null; - let attributes: RawAlert; + let attributes: RawRule; let version: string | undefined; try { const decryptedAlert = - await this.encryptedSavedObjectsClient.getDecryptedAsInternalUser('alert', id, { + await this.encryptedSavedObjectsClient.getDecryptedAsInternalUser('alert', id, { namespace: this.namespace, }); apiKeyToInvalidate = decryptedAlert.attributes.apiKey; @@ -1059,7 +1052,7 @@ export class RulesClient { `updateApiKey(): Failed to load API key to invalidate on alert ${id}: ${e.message}` ); // Still attempt to load the attributes and version using SOC - const alert = await this.unsecuredSavedObjectsClient.get('alert', id); + const alert = await this.unsecuredSavedObjectsClient.get('alert', id); attributes = alert.attributes; version = alert.version; } @@ -1146,12 +1139,12 @@ export class RulesClient { private async enableWithOCC({ id }: { id: string }) { let apiKeyToInvalidate: string | null = null; - let attributes: RawAlert; + let attributes: RawRule; let version: string | undefined; try { const decryptedAlert = - await this.encryptedSavedObjectsClient.getDecryptedAsInternalUser('alert', id, { + await this.encryptedSavedObjectsClient.getDecryptedAsInternalUser('alert', id, { namespace: this.namespace, }); apiKeyToInvalidate = decryptedAlert.attributes.apiKey; @@ -1163,7 +1156,7 @@ export class RulesClient { `enable(): Failed to load API key to invalidate on alert ${id}: ${e.message}` ); // Still attempt to load the attributes and version using SOC - const alert = await this.unsecuredSavedObjectsClient.get('alert', id); + const alert = await this.unsecuredSavedObjectsClient.get('alert', id); attributes = alert.attributes; version = alert.version; } @@ -1265,12 +1258,12 @@ export class RulesClient { private async disableWithOCC({ id }: { id: string }) { let apiKeyToInvalidate: string | null = null; - let attributes: RawAlert; + let attributes: RawRule; let version: string | undefined; try { const decryptedAlert = - await this.encryptedSavedObjectsClient.getDecryptedAsInternalUser('alert', id, { + await this.encryptedSavedObjectsClient.getDecryptedAsInternalUser('alert', id, { namespace: this.namespace, }); apiKeyToInvalidate = decryptedAlert.attributes.apiKey; @@ -1282,7 +1275,7 @@ export class RulesClient { `disable(): Failed to load API key to invalidate on alert ${id}: ${e.message}` ); // Still attempt to load the attributes and version using SOC - const alert = await this.unsecuredSavedObjectsClient.get('alert', id); + const alert = await this.unsecuredSavedObjectsClient.get('alert', id); attributes = alert.attributes; version = alert.version; } @@ -1403,7 +1396,7 @@ export class RulesClient { } private async muteAllWithOCC({ id }: { id: string }) { - const { attributes, version } = await this.unsecuredSavedObjectsClient.get( + const { attributes, version } = await this.unsecuredSavedObjectsClient.get( 'alert', id ); @@ -1465,7 +1458,7 @@ export class RulesClient { } private async unmuteAllWithOCC({ id }: { id: string }) { - const { attributes, version } = await this.unsecuredSavedObjectsClient.get( + const { attributes, version } = await this.unsecuredSavedObjectsClient.get( 'alert', id ); @@ -1633,7 +1626,7 @@ export class RulesClient { const mutedInstanceIds = attributes.mutedInstanceIds || []; if (!attributes.muteAll && mutedInstanceIds.includes(alertInstanceId)) { - await this.unsecuredSavedObjectsClient.update( + await this.unsecuredSavedObjectsClient.update( 'alert', alertId, this.updateMeta({ @@ -1691,7 +1684,7 @@ export class RulesClient { private injectReferencesIntoActions( alertId: string, - actions: RawAlert['actions'], + actions: RawRule['actions'], references: SavedObjectReference[] ) { return actions.map((action) => { @@ -1716,18 +1709,18 @@ export class RulesClient { private getAlertFromRaw( id: string, ruleTypeId: string, - rawAlert: RawAlert, + rawRule: RawRule, references: SavedObjectReference[] | undefined, includeLegacyId: boolean = false ): Alert | AlertWithLegacyId { const ruleType = this.ruleTypeRegistry.get(ruleTypeId); // In order to support the partial update API of Saved Objects we have to support - // partial updates of an Alert, but when we receive an actual RawAlert, it is safe + // partial updates of an Alert, but when we receive an actual RawRule, it is safe // to cast the result to an Alert const res = this.getPartialAlertFromRaw( id, ruleType, - rawAlert, + rawRule, references, includeLegacyId ); @@ -1741,7 +1734,7 @@ export class RulesClient { private getPartialAlertFromRaw( id: string, - ruleType: UntypedNormalizedAlertType, + ruleType: UntypedNormalizedRuleType, { createdAt, updatedAt, @@ -1753,15 +1746,15 @@ export class RulesClient { executionStatus, schedule, actions, - ...partialRawAlert - }: Partial, + ...partialRawRule + }: Partial, references: SavedObjectReference[] | undefined, includeLegacyId: boolean = false ): PartialAlert | PartialAlertWithLegacyId { const rule = { id, notifyWhen, - ...partialRawAlert, + ...partialRawRule, // we currently only support the Interval Schedule type // Once we support additional types, this type signature will likely change schedule: schedule as IntervalSchedule, @@ -1771,7 +1764,7 @@ export class RulesClient { ...(createdAt ? { createdAt: new Date(createdAt) } : {}), ...(scheduledTaskId ? { scheduledTaskId } : {}), ...(executionStatus - ? { executionStatus: alertExecutionStatusFromRaw(this.logger, id, executionStatus) } + ? { executionStatus: ruleExecutionStatusFromRaw(this.logger, id, executionStatus) } : {}), }; return includeLegacyId @@ -1780,7 +1773,7 @@ export class RulesClient { } private async validateActions( - alertType: UntypedNormalizedAlertType, + alertType: UntypedNormalizedRuleType, actions: NormalizedAlertAction[] ): Promise { if (actions.length === 0) { @@ -1831,11 +1824,11 @@ export class RulesClient { Params extends AlertTypeParams, ExtractedParams extends AlertTypeParams >( - ruleType: UntypedNormalizedAlertType, + ruleType: UntypedNormalizedRuleType, ruleActions: NormalizedAlertAction[], ruleParams: Params ): Promise<{ - actions: RawAlert['actions']; + actions: RawRule['actions']; params: ExtractedParams; references: SavedObjectReference[]; }> { @@ -1868,7 +1861,7 @@ export class RulesClient { ExtractedParams extends AlertTypeParams >( ruleId: string, - ruleType: UntypedNormalizedAlertType, + ruleType: UntypedNormalizedRuleType, ruleParams: SavedObjectAttributes | undefined, references: SavedObjectReference[] ): Params { @@ -1896,9 +1889,9 @@ export class RulesClient { private async denormalizeActions( alertActions: NormalizedAlertAction[] - ): Promise<{ actions: RawAlert['actions']; references: SavedObjectReference[] }> { + ): Promise<{ actions: RawRule['actions']; references: SavedObjectReference[] }> { const references: SavedObjectReference[] = []; - const actions: RawAlert['actions'] = []; + const actions: RawRule['actions'] = []; if (alertActions.length) { const actionsClient = await this.getActionsClient(); const actionIds = [...new Set(alertActions.map((alertAction) => alertAction.id))]; @@ -1953,7 +1946,7 @@ export class RulesClient { return truncate(`Alerting: ${alertTypeId}/${trim(alertName)}`, { length: 256 }); } - private updateMeta>(alertAttributes: T): T { + private updateMeta>(alertAttributes: T): T { if (alertAttributes.hasOwnProperty('apiKey') || alertAttributes.hasOwnProperty('apiKeyOwner')) { alertAttributes.meta = alertAttributes.meta ?? {}; alertAttributes.meta.versionApiKeyLastmodified = this.kibanaVersion; diff --git a/x-pack/plugins/alerting/server/rules_client/tests/get_alert_summary.test.ts b/x-pack/plugins/alerting/server/rules_client/tests/get_alert_summary.test.ts index 86bb97fec6ed8..7a7ab035aa391 100644 --- a/x-pack/plugins/alerting/server/rules_client/tests/get_alert_summary.test.ts +++ b/x-pack/plugins/alerting/server/rules_client/tests/get_alert_summary.test.ts @@ -19,7 +19,7 @@ import { eventLogClientMock } from '../../../../event_log/server/mocks'; import { QueryEventsBySavedObjectResult } from '../../../../event_log/server'; import { SavedObject } from 'kibana/server'; import { EventsFactory } from '../../lib/alert_summary_from_event_log.test'; -import { RawAlert } from '../../types'; +import { RawRule } from '../../types'; import { getBeforeSetup, mockedDateString, setGlobalDate } from './lib'; const taskManager = taskManagerMock.createStart(); @@ -64,7 +64,7 @@ const AlertSummaryFindEventsResult: QueryEventsBySavedObjectResult = { const RuleIntervalSeconds = 1; -const BaseRuleSavedObject: SavedObject = { +const BaseRuleSavedObject: SavedObject = { id: '1', type: 'alert', attributes: { @@ -96,7 +96,7 @@ const BaseRuleSavedObject: SavedObject = { references: [], }; -function getRuleSavedObject(attributes: Partial = {}): SavedObject { +function getRuleSavedObject(attributes: Partial = {}): SavedObject { return { ...BaseRuleSavedObject, attributes: { ...BaseRuleSavedObject.attributes, ...attributes }, diff --git a/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.ts b/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.ts index 113b4cf796d2f..50e6979d6ee72 100644 --- a/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.ts +++ b/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.ts @@ -12,7 +12,7 @@ import { } from 'kibana/server'; import { AlertTypeParams } from '../../index'; import { Query } from '../../../../../../src/plugins/data/common/query'; -import { RawAlert } from '../../types'; +import { RawRule } from '../../types'; // These definitions are dupes of the SO-types in stack_alerts/geo_containment // There are not exported to avoid deep imports from stack_alerts plugins into here @@ -69,8 +69,8 @@ export function extractEntityAndBoundaryReferences(params: GeoContainmentParams) } export function extractRefsFromGeoContainmentAlert( - doc: SavedObjectUnsanitizedDoc -): SavedObjectUnsanitizedDoc { + doc: SavedObjectUnsanitizedDoc +): SavedObjectUnsanitizedDoc { if (doc.attributes.alertTypeId !== GEO_CONTAINMENT_ID) { return doc; } diff --git a/x-pack/plugins/alerting/server/saved_objects/get_import_warnings.test.ts b/x-pack/plugins/alerting/server/saved_objects/get_import_warnings.test.ts index 18c26336721fc..0bf2db556c2dc 100644 --- a/x-pack/plugins/alerting/server/saved_objects/get_import_warnings.test.ts +++ b/x-pack/plugins/alerting/server/saved_objects/get_import_warnings.test.ts @@ -6,7 +6,7 @@ */ import { SavedObject } from 'kibana/server'; -import { RawAlert } from '../types'; +import { RawRule } from '../types'; import { getImportWarnings } from './get_import_warnings'; describe('getImportWarnings', () => { @@ -71,13 +71,13 @@ describe('getImportWarnings', () => { references: [], }, ]; - const warnings = getImportWarnings(savedObjectRules as unknown as Array>); + const warnings = getImportWarnings(savedObjectRules as unknown as Array>); expect(warnings[0].message).toBe('2 rules must be enabled after the import.'); }); it('return no warning messages if no rules were imported', () => { - const savedObjectRules = [] as Array>; - const warnings = getImportWarnings(savedObjectRules as unknown as Array>); + const savedObjectRules = [] as Array>; + const warnings = getImportWarnings(savedObjectRules as unknown as Array>); expect(warnings.length).toBe(0); }); }); diff --git a/x-pack/plugins/alerting/server/saved_objects/index.ts b/x-pack/plugins/alerting/server/saved_objects/index.ts index eb561b3c285f8..d53635ec4f05d 100644 --- a/x-pack/plugins/alerting/server/saved_objects/index.ts +++ b/x-pack/plugins/alerting/server/saved_objects/index.ts @@ -16,7 +16,7 @@ import mappings from './mappings.json'; import { getMigrations } from './migrations'; import { EncryptedSavedObjectsPluginSetup } from '../../../encrypted_saved_objects/server'; import { transformRulesForExport } from './transform_rule_for_export'; -import { RawAlert } from '../types'; +import { RawRule } from '../types'; import { getImportWarnings } from './get_import_warnings'; import { isRuleExportable } from './is_rule_exportable'; import { RuleTypeRegistry } from '../rule_type_registry'; @@ -60,7 +60,7 @@ export function setupSavedObjects( management: { displayName: 'rule', importableAndExportable: true, - getTitle(ruleSavedObject: SavedObject) { + getTitle(ruleSavedObject: SavedObject) { return `Rule: [${ruleSavedObject.attributes.name}]`; }, onImport(ruleSavedObjects) { @@ -68,13 +68,13 @@ export function setupSavedObjects( warnings: getImportWarnings(ruleSavedObjects), }; }, - onExport( + onExport( context: SavedObjectsExportTransformContext, - objects: Array> + objects: Array> ) { return transformRulesForExport(objects); }, - isExportable(ruleSavedObject: SavedObject) { + isExportable(ruleSavedObject: SavedObject) { return isRuleExportable(ruleSavedObject, ruleTypeRegistry, logger); }, }, diff --git a/x-pack/plugins/alerting/server/saved_objects/is_rule_exportable.ts b/x-pack/plugins/alerting/server/saved_objects/is_rule_exportable.ts index 9c4e4f2b4d409..31ada226cacfd 100644 --- a/x-pack/plugins/alerting/server/saved_objects/is_rule_exportable.ts +++ b/x-pack/plugins/alerting/server/saved_objects/is_rule_exportable.ts @@ -6,7 +6,7 @@ */ import { Logger, SavedObject } from 'kibana/server'; -import { RawAlert } from '../types'; +import { RawRule } from '../types'; import { RuleTypeRegistry } from '../rule_type_registry'; export function isRuleExportable( @@ -14,7 +14,7 @@ export function isRuleExportable( ruleTypeRegistry: RuleTypeRegistry, logger: Logger ): boolean { - const ruleSO = rule as SavedObject; + const ruleSO = rule as SavedObject; try { const ruleType = ruleTypeRegistry.get(ruleSO.attributes.alertTypeId); if (!ruleType.isExportable) { diff --git a/x-pack/plugins/alerting/server/saved_objects/migrations.test.ts b/x-pack/plugins/alerting/server/saved_objects/migrations.test.ts index 481edb07cedb9..08312d0be0419 100644 --- a/x-pack/plugins/alerting/server/saved_objects/migrations.test.ts +++ b/x-pack/plugins/alerting/server/saved_objects/migrations.test.ts @@ -7,7 +7,7 @@ import uuid from 'uuid'; import { getMigrations, isAnyActionSupportIncidents } from './migrations'; -import { RawAlert } from '../types'; +import { RawRule } from '../types'; import { SavedObjectUnsanitizedDoc } from 'kibana/server'; import { encryptedSavedObjectsMock } from '../../../encrypted_saved_objects/server/mocks'; import { migrationMocks } from 'src/core/server/mocks'; @@ -512,7 +512,7 @@ describe('successful migrations', () => { (actionTypeId) => { const doc = { attributes: { actions: [{ actionTypeId }, { actionTypeId: '.server-log' }] }, - } as SavedObjectUnsanitizedDoc; + } as SavedObjectUnsanitizedDoc; expect(isAnyActionSupportIncidents(doc)).toBe(true); } ); @@ -520,7 +520,7 @@ describe('successful migrations', () => { test('isAnyActionSupportIncidents should return false when there is no connector that supports incidents', () => { const doc = { attributes: { actions: [{ actionTypeId: '.server-log' }] }, - } as SavedObjectUnsanitizedDoc; + } as SavedObjectUnsanitizedDoc; expect(isAnyActionSupportIncidents(doc)).toBe(false); }); @@ -2254,7 +2254,7 @@ function getUpdatedAt(): string { function getMockData( overwrites: Record = {}, withSavedObjectUpdatedAt: boolean = false -): SavedObjectUnsanitizedDoc> { +): SavedObjectUnsanitizedDoc> { return { attributes: { enabled: true, diff --git a/x-pack/plugins/alerting/server/saved_objects/migrations.ts b/x-pack/plugins/alerting/server/saved_objects/migrations.ts index 201c78ed2340d..6736fd3573adb 100644 --- a/x-pack/plugins/alerting/server/saved_objects/migrations.ts +++ b/x-pack/plugins/alerting/server/saved_objects/migrations.ts @@ -17,7 +17,7 @@ import { SavedObjectAttribute, SavedObjectReference, } from '../../../../../src/core/server'; -import { RawAlert, RawAlertAction } from '../types'; +import { RawRule, RawAlertAction } from '../types'; import { EncryptedSavedObjectsPluginSetup } from '../../../encrypted_saved_objects/server'; import type { IsMigrationNeededPredicate } from '../../../encrypted_saved_objects/server'; import { extractRefsFromGeoContainmentAlert } from './geo_containment/migrations'; @@ -28,19 +28,19 @@ export const LEGACY_LAST_MODIFIED_VERSION = 'pre-7.10.0'; export const FILEBEAT_7X_INDICATOR_PATH = 'threatintel.indicator'; interface AlertLogMeta extends LogMeta { - migrations: { alertDocument: SavedObjectUnsanitizedDoc }; + migrations: { alertDocument: SavedObjectUnsanitizedDoc }; } type AlertMigration = ( - doc: SavedObjectUnsanitizedDoc -) => SavedObjectUnsanitizedDoc; + doc: SavedObjectUnsanitizedDoc +) => SavedObjectUnsanitizedDoc; function createEsoMigration( encryptedSavedObjects: EncryptedSavedObjectsPluginSetup, - isMigrationNeededPredicate: IsMigrationNeededPredicate, + isMigrationNeededPredicate: IsMigrationNeededPredicate, migrationFunc: AlertMigration ) { - return encryptedSavedObjects.createMigration({ + return encryptedSavedObjects.createMigration({ isMigrationNeededPredicate, migration: migrationFunc, shouldMigrateIfDecryptionFails: true, // shouldMigrateIfDecryptionFails flag that applies the migration to undecrypted document if decryption fails @@ -49,13 +49,13 @@ function createEsoMigration( const SUPPORT_INCIDENTS_ACTION_TYPES = ['.servicenow', '.jira', '.resilient']; -export const isAnyActionSupportIncidents = (doc: SavedObjectUnsanitizedDoc): boolean => +export const isAnyActionSupportIncidents = (doc: SavedObjectUnsanitizedDoc): boolean => doc.attributes.actions.some((action) => SUPPORT_INCIDENTS_ACTION_TYPES.includes(action.actionTypeId) ); // Deprecated in 8.0 -export const isSiemSignalsRuleType = (doc: SavedObjectUnsanitizedDoc): boolean => +export const isSiemSignalsRuleType = (doc: SavedObjectUnsanitizedDoc): boolean => doc.attributes.alertTypeId === 'siem.signals'; /** @@ -66,7 +66,7 @@ export const isSiemSignalsRuleType = (doc: SavedObjectUnsanitizedDoc): * @deprecated Once we are confident all rules relying on side-car actions SO's have been migrated to SO references we should remove this function */ export const isSecuritySolutionLegacyNotification = ( - doc: SavedObjectUnsanitizedDoc + doc: SavedObjectUnsanitizedDoc ): boolean => doc.attributes.alertTypeId === 'siem.notifications'; export function getMigrations( @@ -76,7 +76,7 @@ export function getMigrations( const migrationWhenRBACWasIntroduced = createEsoMigration( encryptedSavedObjects, // migrate all documents in 7.10 in order to add the "meta" RBAC field - (doc): doc is SavedObjectUnsanitizedDoc => true, + (doc): doc is SavedObjectUnsanitizedDoc => true, pipeMigrations( markAsLegacyAndChangeConsumer, setAlertIdAsDefaultDedupkeyOnPagerDutyActions, @@ -87,37 +87,37 @@ export function getMigrations( const migrationAlertUpdatedAtAndNotifyWhen = createEsoMigration( encryptedSavedObjects, // migrate all documents in 7.11 in order to add the "updatedAt" and "notifyWhen" fields - (doc): doc is SavedObjectUnsanitizedDoc => true, + (doc): doc is SavedObjectUnsanitizedDoc => true, pipeMigrations(setAlertUpdatedAtDate, setNotifyWhen) ); const migrationActions7112 = createEsoMigration( encryptedSavedObjects, - (doc): doc is SavedObjectUnsanitizedDoc => isAnyActionSupportIncidents(doc), + (doc): doc is SavedObjectUnsanitizedDoc => isAnyActionSupportIncidents(doc), pipeMigrations(restructureConnectorsThatSupportIncident) ); const migrationSecurityRules713 = createEsoMigration( encryptedSavedObjects, - (doc): doc is SavedObjectUnsanitizedDoc => isSiemSignalsRuleType(doc), + (doc): doc is SavedObjectUnsanitizedDoc => isSiemSignalsRuleType(doc), pipeMigrations(removeNullsFromSecurityRules) ); const migrationSecurityRules714 = createEsoMigration( encryptedSavedObjects, - (doc): doc is SavedObjectUnsanitizedDoc => isSiemSignalsRuleType(doc), + (doc): doc is SavedObjectUnsanitizedDoc => isSiemSignalsRuleType(doc), pipeMigrations(removeNullAuthorFromSecurityRules) ); const migrationSecurityRules715 = createEsoMigration( encryptedSavedObjects, - (doc): doc is SavedObjectUnsanitizedDoc => isSiemSignalsRuleType(doc), + (doc): doc is SavedObjectUnsanitizedDoc => isSiemSignalsRuleType(doc), pipeMigrations(addExceptionListsToReferences) ); const migrateRules716 = createEsoMigration( encryptedSavedObjects, - (doc): doc is SavedObjectUnsanitizedDoc => true, + (doc): doc is SavedObjectUnsanitizedDoc => true, pipeMigrations( setLegacyId, getRemovePreconfiguredConnectorsFromReferencesFn(isPreconfigured), @@ -128,7 +128,7 @@ export function getMigrations( const migrationRules800 = createEsoMigration( encryptedSavedObjects, - (doc: SavedObjectUnsanitizedDoc): doc is SavedObjectUnsanitizedDoc => true, + (doc: SavedObjectUnsanitizedDoc): doc is SavedObjectUnsanitizedDoc => true, pipeMigrations( addThreatIndicatorPathToThreatMatchRules, addRACRuleTypes, @@ -149,10 +149,10 @@ export function getMigrations( } function executeMigrationWithErrorHandling( - migrationFunc: SavedObjectMigrationFn, + migrationFunc: SavedObjectMigrationFn, version: string ) { - return (doc: SavedObjectUnsanitizedDoc, context: SavedObjectMigrationContext) => { + return (doc: SavedObjectUnsanitizedDoc, context: SavedObjectMigrationContext) => { try { return migrationFunc(doc, context); } catch (ex) { @@ -170,8 +170,8 @@ function executeMigrationWithErrorHandling( } const setAlertUpdatedAtDate = ( - doc: SavedObjectUnsanitizedDoc -): SavedObjectUnsanitizedDoc => { + doc: SavedObjectUnsanitizedDoc +): SavedObjectUnsanitizedDoc => { const updatedAt = doc.updated_at || doc.attributes.createdAt; return { ...doc, @@ -183,8 +183,8 @@ const setAlertUpdatedAtDate = ( }; const setNotifyWhen = ( - doc: SavedObjectUnsanitizedDoc -): SavedObjectUnsanitizedDoc => { + doc: SavedObjectUnsanitizedDoc +): SavedObjectUnsanitizedDoc => { const notifyWhen = doc.attributes.throttle ? 'onThrottleInterval' : 'onActiveAlert'; return { ...doc, @@ -204,8 +204,8 @@ const consumersToChange: Map = new Map( ); function markAsLegacyAndChangeConsumer( - doc: SavedObjectUnsanitizedDoc -): SavedObjectUnsanitizedDoc { + doc: SavedObjectUnsanitizedDoc +): SavedObjectUnsanitizedDoc { const { attributes: { consumer }, } = doc; @@ -223,8 +223,8 @@ function markAsLegacyAndChangeConsumer( } function setAlertIdAsDefaultDedupkeyOnPagerDutyActions( - doc: SavedObjectUnsanitizedDoc -): SavedObjectUnsanitizedDoc { + doc: SavedObjectUnsanitizedDoc +): SavedObjectUnsanitizedDoc { const { attributes } = doc; return { ...doc, @@ -251,8 +251,8 @@ function setAlertIdAsDefaultDedupkeyOnPagerDutyActions( } function initializeExecutionStatus( - doc: SavedObjectUnsanitizedDoc -): SavedObjectUnsanitizedDoc { + doc: SavedObjectUnsanitizedDoc +): SavedObjectUnsanitizedDoc { const { attributes } = doc; return { ...doc, @@ -277,8 +277,8 @@ function isEmptyObject(obj: {}) { } function restructureConnectorsThatSupportIncident( - doc: SavedObjectUnsanitizedDoc -): SavedObjectUnsanitizedDoc { + doc: SavedObjectUnsanitizedDoc +): SavedObjectUnsanitizedDoc { const { actions } = doc.attributes; const newActions = actions.reduce((acc, action) => { if ( @@ -416,8 +416,8 @@ function convertNullToUndefined(attribute: SavedObjectAttribute) { } function removeNullsFromSecurityRules( - doc: SavedObjectUnsanitizedDoc -): SavedObjectUnsanitizedDoc { + doc: SavedObjectUnsanitizedDoc +): SavedObjectUnsanitizedDoc { const { attributes: { params }, } = doc; @@ -490,8 +490,8 @@ function removeNullsFromSecurityRules( * @returns The document with the author field fleshed in. */ function removeNullAuthorFromSecurityRules( - doc: SavedObjectUnsanitizedDoc -): SavedObjectUnsanitizedDoc { + doc: SavedObjectUnsanitizedDoc +): SavedObjectUnsanitizedDoc { const { attributes: { params }, } = doc; @@ -519,8 +519,8 @@ function removeNullAuthorFromSecurityRules( * @returns The document migrated with saved object references */ function addExceptionListsToReferences( - doc: SavedObjectUnsanitizedDoc -): SavedObjectUnsanitizedDoc { + doc: SavedObjectUnsanitizedDoc +): SavedObjectUnsanitizedDoc { const { attributes: { params: { exceptionsList }, @@ -610,8 +610,8 @@ function removeMalformedExceptionsList( * @returns The document migrated with saved object references */ function addRuleIdsToLegacyNotificationReferences( - doc: SavedObjectUnsanitizedDoc -): SavedObjectUnsanitizedDoc { + doc: SavedObjectUnsanitizedDoc +): SavedObjectUnsanitizedDoc { const { attributes: { params: { ruleAlertId }, @@ -641,9 +641,7 @@ function addRuleIdsToLegacyNotificationReferences( } } -function setLegacyId( - doc: SavedObjectUnsanitizedDoc -): SavedObjectUnsanitizedDoc { +function setLegacyId(doc: SavedObjectUnsanitizedDoc): SavedObjectUnsanitizedDoc { const { id } = doc; return { ...doc, @@ -655,8 +653,8 @@ function setLegacyId( } function addRACRuleTypes( - doc: SavedObjectUnsanitizedDoc -): SavedObjectUnsanitizedDoc { + doc: SavedObjectUnsanitizedDoc +): SavedObjectUnsanitizedDoc { const ruleType = doc.attributes.params.type; return isSiemSignalsRuleType(doc) && isRuleType(ruleType) ? { @@ -674,8 +672,8 @@ function addRACRuleTypes( } function addThreatIndicatorPathToThreatMatchRules( - doc: SavedObjectUnsanitizedDoc -): SavedObjectUnsanitizedDoc { + doc: SavedObjectUnsanitizedDoc +): SavedObjectUnsanitizedDoc { return isSiemSignalsRuleType(doc) && doc.attributes.params?.type === 'threat_match' && !doc.attributes.params.threatIndicatorPath @@ -695,15 +693,15 @@ function addThreatIndicatorPathToThreatMatchRules( function getRemovePreconfiguredConnectorsFromReferencesFn( isPreconfigured: (connectorId: string) => boolean ) { - return (doc: SavedObjectUnsanitizedDoc) => { + return (doc: SavedObjectUnsanitizedDoc) => { return removePreconfiguredConnectorsFromReferences(doc, isPreconfigured); }; } function removePreconfiguredConnectorsFromReferences( - doc: SavedObjectUnsanitizedDoc, + doc: SavedObjectUnsanitizedDoc, isPreconfigured: (connectorId: string) => boolean -): SavedObjectUnsanitizedDoc { +): SavedObjectUnsanitizedDoc { const { attributes: { actions }, references, @@ -719,7 +717,7 @@ function removePreconfiguredConnectorsFromReferences( ); const updatedConnectorReferences: SavedObjectReference[] = []; - const updatedActions: RawAlert['actions'] = []; + const updatedActions: RawRule['actions'] = []; // For each connector reference, check if connector is preconfigured // If yes, we need to remove from the references array and update @@ -758,8 +756,8 @@ function removePreconfiguredConnectorsFromReferences( // This fixes an issue whereby metrics.alert.inventory.threshold rules had the // group for actions incorrectly spelt as metrics.invenotry_threshold.fired vs metrics.inventory_threshold.fired function fixInventoryThresholdGroupId( - doc: SavedObjectUnsanitizedDoc -): SavedObjectUnsanitizedDoc { + doc: SavedObjectUnsanitizedDoc +): SavedObjectUnsanitizedDoc { if (doc.attributes.alertTypeId === 'metrics.alert.inventory.threshold') { const { attributes: { actions }, @@ -805,6 +803,6 @@ function getCorrespondingAction( } function pipeMigrations(...migrations: AlertMigration[]): AlertMigration { - return (doc: SavedObjectUnsanitizedDoc) => + return (doc: SavedObjectUnsanitizedDoc) => migrations.reduce((migratedDoc, nextMigration) => nextMigration(migratedDoc), doc); } diff --git a/x-pack/plugins/alerting/server/saved_objects/partially_update_alert.ts b/x-pack/plugins/alerting/server/saved_objects/partially_update_alert.ts index bb211c87867c0..76303722fc876 100644 --- a/x-pack/plugins/alerting/server/saved_objects/partially_update_alert.ts +++ b/x-pack/plugins/alerting/server/saved_objects/partially_update_alert.ts @@ -6,7 +6,7 @@ */ import { pick } from 'lodash'; -import { RawAlert } from '../types'; +import { RawRule } from '../types'; import { SavedObjectsClient, @@ -17,7 +17,7 @@ import { import { AlertAttributesExcludedFromAAD, AlertAttributesExcludedFromAADType } from './index'; export type PartiallyUpdateableAlertAttributes = Partial< - Pick + Pick >; export interface PartiallyUpdateAlertSavedObjectOptions { @@ -40,7 +40,7 @@ export async function partiallyUpdateAlert( ): Promise { // ensure we only have the valid attributes excluded from AAD const attributeUpdates = pick(attributes, AlertAttributesExcludedFromAAD); - const updateOptions: SavedObjectsUpdateOptions = pick( + const updateOptions: SavedObjectsUpdateOptions = pick( options, 'namespace', 'version', @@ -48,7 +48,7 @@ export async function partiallyUpdateAlert( ); try { - await savedObjectsClient.update('alert', id, attributeUpdates, updateOptions); + await savedObjectsClient.update('alert', id, attributeUpdates, updateOptions); } catch (err) { if (options?.ignore404 && SavedObjectsErrorHelpers.isNotFoundError(err)) { return; diff --git a/x-pack/plugins/alerting/server/saved_objects/transform_rule_for_export.test.ts b/x-pack/plugins/alerting/server/saved_objects/transform_rule_for_export.test.ts index 8236c4455478c..a5befc94a340a 100644 --- a/x-pack/plugins/alerting/server/saved_objects/transform_rule_for_export.test.ts +++ b/x-pack/plugins/alerting/server/saved_objects/transform_rule_for_export.test.ts @@ -6,8 +6,8 @@ */ import { transformRulesForExport } from './transform_rule_for_export'; -jest.mock('../lib/alert_execution_status', () => ({ - getAlertExecutionStatusPending: () => ({ +jest.mock('../lib/rule_execution_status', () => ({ + getRuleExecutionStatusPending: () => ({ status: 'pending', lastExecutionDate: '2020-08-20T19:23:38Z', error: null, diff --git a/x-pack/plugins/alerting/server/saved_objects/transform_rule_for_export.ts b/x-pack/plugins/alerting/server/saved_objects/transform_rule_for_export.ts index 97fd226b49e8e..5b89f6394e3c5 100644 --- a/x-pack/plugins/alerting/server/saved_objects/transform_rule_for_export.ts +++ b/x-pack/plugins/alerting/server/saved_objects/transform_rule_for_export.ts @@ -6,18 +6,18 @@ */ import { SavedObject } from 'kibana/server'; -import { getAlertExecutionStatusPending } from '../lib/alert_execution_status'; -import { RawAlert } from '../types'; +import { getRuleExecutionStatusPending } from '../lib/rule_execution_status'; +import { RawRule } from '../types'; -export function transformRulesForExport(rules: SavedObject[]): Array> { +export function transformRulesForExport(rules: SavedObject[]): Array> { const exportDate = new Date().toISOString(); - return rules.map((rule) => transformRuleForExport(rule as SavedObject, exportDate)); + return rules.map((rule) => transformRuleForExport(rule as SavedObject, exportDate)); } function transformRuleForExport( - rule: SavedObject, + rule: SavedObject, exportDate: string -): SavedObject { +): SavedObject { return { ...rule, attributes: { @@ -27,7 +27,7 @@ function transformRuleForExport( apiKey: null, apiKeyOwner: null, scheduledTaskId: null, - executionStatus: getAlertExecutionStatusPending(exportDate), + executionStatus: getRuleExecutionStatusPending(exportDate), }, }; } diff --git a/x-pack/plugins/alerting/server/task_runner/alert_task_instance.ts b/x-pack/plugins/alerting/server/task_runner/alert_task_instance.ts index 1ea9a473b914a..a6bb6a68ceae8 100644 --- a/x-pack/plugins/alerting/server/task_runner/alert_task_instance.ts +++ b/x-pack/plugins/alerting/server/task_runner/alert_task_instance.ts @@ -11,16 +11,16 @@ import { fold } from 'fp-ts/lib/Either'; import { ConcreteTaskInstance } from '../../../task_manager/server'; import { SanitizedAlert, - AlertTaskState, - alertParamsSchema, - alertStateSchema, - AlertTaskParams, + RuleTaskState, + ruleParamsSchema, + ruleStateSchema, + RuleTaskParams, AlertTypeParams, } from '../../common'; export interface AlertTaskInstance extends ConcreteTaskInstance { - state: AlertTaskState; - params: AlertTaskParams; + state: RuleTaskState; + params: RuleTaskParams; } const enumerateErrorFields = (e: t.Errors) => @@ -33,7 +33,7 @@ export function taskInstanceToAlertTaskInstance( return { ...taskInstance, params: pipe( - alertParamsSchema.decode(taskInstance.params), + ruleParamsSchema.decode(taskInstance.params), fold((e: t.Errors) => { throw new Error( `Task "${taskInstance.id}" ${ @@ -43,7 +43,7 @@ export function taskInstanceToAlertTaskInstance( }, t.identity) ), state: pipe( - alertStateSchema.decode(taskInstance.state), + ruleStateSchema.decode(taskInstance.state), fold((e: t.Errors) => { throw new Error( `Task "${taskInstance.id}" ${ diff --git a/x-pack/plugins/alerting/server/task_runner/create_execution_handler.test.ts b/x-pack/plugins/alerting/server/task_runner/create_execution_handler.test.ts index fc5c5cf8897f0..69b094585d703 100644 --- a/x-pack/plugins/alerting/server/task_runner/create_execution_handler.test.ts +++ b/x-pack/plugins/alerting/server/task_runner/create_execution_handler.test.ts @@ -16,7 +16,7 @@ import { eventLoggerMock } from '../../../event_log/server/event_logger.mock'; import { KibanaRequest } from 'kibana/server'; import { asSavedObjectExecutionSource } from '../../../actions/server'; import { InjectActionParamsOpts } from './inject_action_params'; -import { NormalizedAlertType } from '../rule_type_registry'; +import { NormalizedRuleType } from '../rule_type_registry'; import { AlertTypeParams, AlertTypeState, @@ -28,7 +28,7 @@ jest.mock('./inject_action_params', () => ({ injectActionParams: jest.fn(), })); -const alertType: NormalizedAlertType< +const ruleType: NormalizedRuleType< AlertTypeParams, AlertTypeParams, AlertTypeState, @@ -71,12 +71,12 @@ const createExecutionHandlerParams: jest.Mocked< > = { actionsPlugin: mockActionsPlugin, spaceId: 'test1', - alertId: '1', - alertName: 'name-of-alert', + ruleId: '1', + ruleName: 'name-of-alert', tags: ['tag-A', 'tag-B'], apiKey: 'MTIzOmFiYw==', kibanaBaseUrl: 'http://localhost:5601', - alertType, + ruleType, logger: loggingSystemMock.create().get(), eventLogger: mockEventLogger, actions: [ @@ -93,13 +93,13 @@ const createExecutionHandlerParams: jest.Mocked< }, ], request: {} as KibanaRequest, - alertParams: { + ruleParams: { foo: true, contextVal: 'My other {{context.value}} goes here', stateVal: 'My other {{state.value}} goes here', }, supportsEphemeralTasks: false, - maxEphemeralActionsPerAlert: 10, + maxEphemeralActionsPerRule: 10, }; beforeEach(() => { @@ -123,7 +123,7 @@ test('enqueues execution per selected action', async () => { actionGroup: 'default', state: {}, context: {}, - alertInstanceId: '2', + alertId: '2', }); expect(mockActionsPlugin.getActionsClientWithRequest).toHaveBeenCalledWith( createExecutionHandlerParams.request @@ -244,7 +244,7 @@ test(`doesn't call actionsPlugin.execute for disabled actionTypes`, async () => actionGroup: 'default', state: {}, context: {}, - alertInstanceId: '2', + alertId: '2', }); expect(actionsClient.enqueueExecution).toHaveBeenCalledTimes(1); expect(actionsClient.enqueueExecution).toHaveBeenCalledWith({ @@ -296,7 +296,7 @@ test('trow error error message when action type is disabled', async () => { actionGroup: 'default', state: {}, context: {}, - alertInstanceId: '2', + alertId: '2', }); expect(actionsClient.enqueueExecution).toHaveBeenCalledTimes(0); @@ -310,7 +310,7 @@ test('trow error error message when action type is disabled', async () => { actionGroup: 'default', state: {}, context: {}, - alertInstanceId: '2', + alertId: '2', }); expect(actionsClient.enqueueExecution).toHaveBeenCalledTimes(1); }); @@ -321,7 +321,7 @@ test('limits actionsPlugin.execute per action group', async () => { actionGroup: 'other-group', state: {}, context: {}, - alertInstanceId: '2', + alertId: '2', }); expect(actionsClient.enqueueExecution).not.toHaveBeenCalled(); }); @@ -332,7 +332,7 @@ test('context attribute gets parameterized', async () => { actionGroup: 'default', context: { value: 'context-val' }, state: {}, - alertInstanceId: '2', + alertId: '2', }); expect(actionsClient.enqueueExecution).toHaveBeenCalledTimes(1); expect(actionsClient.enqueueExecution.mock.calls[0]).toMatchInlineSnapshot(` @@ -373,7 +373,7 @@ test('state attribute gets parameterized', async () => { actionGroup: 'default', context: {}, state: { value: 'state-val' }, - alertInstanceId: '2', + alertId: '2', }); expect(actionsClient.enqueueExecution).toHaveBeenCalledTimes(1); expect(actionsClient.enqueueExecution.mock.calls[0]).toMatchInlineSnapshot(` @@ -408,7 +408,7 @@ test('state attribute gets parameterized', async () => { `); }); -test(`logs an error when action group isn't part of actionGroups available for the alertType`, async () => { +test(`logs an error when action group isn't part of actionGroups available for the ruleType`, async () => { const executionHandler = createExecutionHandler(createExecutionHandlerParams); const result = await executionHandler({ // we have to trick the compiler as this is an invalid type and this test checks whether we @@ -416,10 +416,10 @@ test(`logs an error when action group isn't part of actionGroups available for t actionGroup: 'invalid-group' as 'default' | 'other-group', context: {}, state: {}, - alertInstanceId: '2', + alertId: '2', }); expect(result).toBeUndefined(); expect(createExecutionHandlerParams.logger.error).toHaveBeenCalledWith( - 'Invalid action group "invalid-group" for alert "test".' + 'Invalid action group "invalid-group" for rule "test".' ); }); diff --git a/x-pack/plugins/alerting/server/task_runner/create_execution_handler.ts b/x-pack/plugins/alerting/server/task_runner/create_execution_handler.ts index d93d8cd6d1312..112cb949e3ad7 100644 --- a/x-pack/plugins/alerting/server/task_runner/create_execution_handler.ts +++ b/x-pack/plugins/alerting/server/task_runner/create_execution_handler.ts @@ -19,9 +19,9 @@ import { AlertTypeState, AlertInstanceState, AlertInstanceContext, - RawAlert, + RawRule, } from '../types'; -import { NormalizedAlertType, UntypedNormalizedAlertType } from '../rule_type_registry'; +import { NormalizedRuleType, UntypedNormalizedRuleType } from '../rule_type_registry'; import { isEphemeralTaskRejectedDueToCapacityError } from '../../../task_manager/server'; import { createAlertEventLogRecordObject } from '../lib/create_alert_event_log_record_object'; @@ -34,15 +34,15 @@ export interface CreateExecutionHandlerOptions< ActionGroupIds extends string, RecoveryActionGroupId extends string > { - alertId: string; - alertName: string; + ruleId: string; + ruleName: string; tags?: string[]; actionsPlugin: ActionsPluginStartContract; actions: AlertAction[]; spaceId: string; - apiKey: RawAlert['apiKey']; + apiKey: RawRule['apiKey']; kibanaBaseUrl: string | undefined; - alertType: NormalizedAlertType< + ruleType: NormalizedRuleType< Params, ExtractedParams, State, @@ -54,15 +54,15 @@ export interface CreateExecutionHandlerOptions< logger: Logger; eventLogger: IEventLogger; request: KibanaRequest; - alertParams: AlertTypeParams; + ruleParams: AlertTypeParams; supportsEphemeralTasks: boolean; - maxEphemeralActionsPerAlert: number; + maxEphemeralActionsPerRule: number; } interface ExecutionHandlerOptions { actionGroup: ActionGroupIds; actionSubgroup?: string; - alertInstanceId: string; + alertId: string; context: AlertInstanceContext; state: AlertInstanceState; } @@ -81,20 +81,20 @@ export function createExecutionHandler< RecoveryActionGroupId extends string >({ logger, - alertId, - alertName, + ruleId, + ruleName, tags, actionsPlugin, - actions: alertActions, + actions: ruleActions, spaceId, apiKey, - alertType, + ruleType, kibanaBaseUrl, eventLogger, request, - alertParams, + ruleParams, supportsEphemeralTasks, - maxEphemeralActionsPerAlert, + maxEphemeralActionsPerRule, }: CreateExecutionHandlerOptions< Params, ExtractedParams, @@ -104,66 +104,66 @@ export function createExecutionHandler< ActionGroupIds, RecoveryActionGroupId >): ExecutionHandler { - const alertTypeActionGroups = new Map( - alertType.actionGroups.map((actionGroup) => [actionGroup.id, actionGroup.name]) + const ruleTypeActionGroups = new Map( + ruleType.actionGroups.map((actionGroup) => [actionGroup.id, actionGroup.name]) ); return async ({ actionGroup, actionSubgroup, context, state, - alertInstanceId, + alertId, }: ExecutionHandlerOptions) => { - if (!alertTypeActionGroups.has(actionGroup)) { - logger.error(`Invalid action group "${actionGroup}" for alert "${alertType.id}".`); + if (!ruleTypeActionGroups.has(actionGroup)) { + logger.error(`Invalid action group "${actionGroup}" for rule "${ruleType.id}".`); return; } - const actions = alertActions + const actions = ruleActions .filter(({ group }) => group === actionGroup) .map((action) => { return { ...action, params: transformActionParams({ actionsPlugin, - alertId, - alertType: alertType.id, + alertId: ruleId, + alertType: ruleType.id, actionTypeId: action.actionTypeId, - alertName, + alertName: ruleName, spaceId, tags, - alertInstanceId, + alertInstanceId: alertId, alertActionGroup: actionGroup, - alertActionGroupName: alertTypeActionGroups.get(actionGroup)!, + alertActionGroupName: ruleTypeActionGroups.get(actionGroup)!, alertActionSubgroup: actionSubgroup, context, actionParams: action.params, actionId: action.id, state, kibanaBaseUrl, - alertParams, + alertParams: ruleParams, }), }; }) .map((action) => ({ ...action, params: injectActionParams({ - ruleId: alertId, + ruleId, spaceId, actionParams: action.params, actionTypeId: action.actionTypeId, }), })); - const alertLabel = `${alertType.id}:${alertId}: '${alertName}'`; + const ruleLabel = `${ruleType.id}:${ruleId}: '${ruleName}'`; const actionsClient = await actionsPlugin.getActionsClientWithRequest(request); - let ephemeralActionsToSchedule = maxEphemeralActionsPerAlert; + let ephemeralActionsToSchedule = maxEphemeralActionsPerRule; for (const action of actions) { if ( !actionsPlugin.isActionExecutable(action.id, action.actionTypeId, { notifyUsage: true }) ) { logger.warn( - `Alert "${alertId}" skipped scheduling action "${action.id}" because it is disabled` + `Rule "${ruleId}" skipped scheduling action "${action.id}" because it is disabled` ); continue; } @@ -176,15 +176,15 @@ export function createExecutionHandler< spaceId, apiKey: apiKey ?? null, source: asSavedObjectExecutionSource({ - id: alertId, + id: ruleId, type: 'alert', }), relatedSavedObjects: [ { - id: alertId, + id: ruleId, type: 'alert', namespace: namespace.namespace, - typeId: alertType.id, + typeId: ruleType.id, }, ], }; @@ -203,18 +203,18 @@ export function createExecutionHandler< } const event = createAlertEventLogRecordObject({ - ruleId: alertId, - ruleType: alertType as UntypedNormalizedAlertType, + ruleId, + ruleType: ruleType as UntypedNormalizedRuleType, action: EVENT_LOG_ACTIONS.executeAction, - instanceId: alertInstanceId, + instanceId: alertId, group: actionGroup, subgroup: actionSubgroup, - ruleName: alertName, + ruleName, savedObjects: [ { type: 'alert', - id: alertId, - typeId: alertType.id, + id: ruleId, + typeId: ruleType.id, relation: SAVED_OBJECT_REL_PRIMARY, }, { @@ -224,7 +224,7 @@ export function createExecutionHandler< }, ], ...namespace, - message: `alert: ${alertLabel} instanceId: '${alertInstanceId}' scheduled ${ + message: `alert: ${ruleLabel} instanceId: '${alertId}' scheduled ${ actionSubgroup ? `actionGroup(subgroup): '${actionGroup}(${actionSubgroup})'` : `actionGroup: '${actionGroup}'` diff --git a/x-pack/plugins/alerting/server/task_runner/task_runner.test.ts b/x-pack/plugins/alerting/server/task_runner/task_runner.test.ts index d370a278e0a5c..eb5529a9db853 100644 --- a/x-pack/plugins/alerting/server/task_runner/task_runner.test.ts +++ b/x-pack/plugins/alerting/server/task_runner/task_runner.test.ts @@ -37,13 +37,13 @@ import { IEventLogger } from '../../../event_log/server'; import { SavedObjectsErrorHelpers } from '../../../../../src/core/server'; import { Alert, RecoveredActionGroup } from '../../common'; import { omit } from 'lodash'; -import { UntypedNormalizedAlertType } from '../rule_type_registry'; +import { UntypedNormalizedRuleType } from '../rule_type_registry'; import { ruleTypeRegistryMock } from '../rule_type_registry.mock'; import { ExecuteOptions } from '../../../actions/server/create_execute_function'; -const alertType: jest.Mocked = { +const ruleType: jest.Mocked = { id: 'test', - name: 'My test alert', + name: 'My test rule', actionGroups: [{ id: 'default', name: 'Default' }, RecoveredActionGroup], defaultActionGroupId: 'default', minimumLicenseRequired: 'basic', @@ -107,7 +107,7 @@ describe('Task Runner', () => { ruleTypeRegistry, kibanaBaseUrl: 'https://localhost:5601', supportsEphemeralTasks: false, - maxEphemeralActionsPerAlert: 10, + maxEphemeralActionsPerRule: 10, cancelAlertsOnRuleTimeout: true, }; @@ -133,7 +133,7 @@ describe('Task Runner', () => { const mockDate = new Date('2019-02-12T21:01:22.479Z'); - const mockedAlertTypeSavedObject: Alert = { + const mockedRuleTypeSavedObject: Alert = { id: '1', consumer: 'bar', createdAt: mockDate, @@ -142,14 +142,14 @@ describe('Task Runner', () => { muteAll: false, notifyWhen: 'onActiveAlert', enabled: true, - alertTypeId: alertType.id, + alertTypeId: ruleType.id, apiKey: '', apiKeyOwner: 'elastic', schedule: { interval: '10s' }, - name: 'alert-name', - tags: ['alert-', '-tags'], - createdBy: 'alert-creator', - updatedBy: 'alert-updater', + name: 'rule-name', + tags: ['rule-', '-tags'], + createdBy: 'rule-creator', + updatedBy: 'rule-updater', mutedInstanceIds: [], params: { bar: true, @@ -188,7 +188,7 @@ describe('Task Runner', () => { taskRunnerFactoryInitializerParams.actionsPlugin.renderActionParameterTemplates.mockImplementation( (actionTypeId, actionId, params) => params ); - ruleTypeRegistry.get.mockReturnValue(alertType); + ruleTypeRegistry.get.mockReturnValue(ruleType); taskRunnerFactoryInitializerParams.executionContext.withContext.mockImplementation((ctx, fn) => fn() ); @@ -196,7 +196,7 @@ describe('Task Runner', () => { test('successfully executes the task', async () => { const taskRunner = new TaskRunner( - alertType, + ruleType, { ...mockedTaskInstance, state: { @@ -206,7 +206,7 @@ describe('Task Runner', () => { }, taskRunnerFactoryInitializerParams ); - rulesClient.get.mockResolvedValue(mockedAlertTypeSavedObject); + rulesClient.get.mockResolvedValue(mockedRuleTypeSavedObject); encryptedSavedObjectsClient.getDecryptedAsInternalUser.mockResolvedValue({ id: '1', type: 'alert', @@ -229,8 +229,8 @@ describe('Task Runner', () => { }, } `); - expect(alertType.executor).toHaveBeenCalledTimes(1); - const call = alertType.executor.mock.calls[0][0]; + expect(ruleType.executor).toHaveBeenCalledTimes(1); + const call = ruleType.executor.mock.calls[0][0]; expect(call.params).toMatchInlineSnapshot(` Object { "bar": true, @@ -239,13 +239,13 @@ describe('Task Runner', () => { expect(call.startedAt).toMatchInlineSnapshot(`1970-01-01T00:00:00.000Z`); expect(call.previousStartedAt).toMatchInlineSnapshot(`1969-12-31T23:55:00.000Z`); expect(call.state).toMatchInlineSnapshot(`Object {}`); - expect(call.name).toBe('alert-name'); - expect(call.tags).toEqual(['alert-', '-tags']); - expect(call.createdBy).toBe('alert-creator'); - expect(call.updatedBy).toBe('alert-updater'); + expect(call.name).toBe('rule-name'); + expect(call.tags).toEqual(['rule-', '-tags']); + expect(call.createdBy).toBe('rule-creator'); + expect(call.updatedBy).toBe('rule-updater'); expect(call.rule).not.toBe(null); - expect(call.rule.name).toBe('alert-name'); - expect(call.rule.tags).toEqual(['alert-', '-tags']); + expect(call.rule.name).toBe('rule-name'); + expect(call.rule.tags).toEqual(['rule-', '-tags']); expect(call.rule.consumer).toBe('bar'); expect(call.rule.enabled).toBe(true); expect(call.rule.schedule).toMatchInlineSnapshot(` @@ -253,15 +253,15 @@ describe('Task Runner', () => { "interval": "10s", } `); - expect(call.rule.createdBy).toBe('alert-creator'); - expect(call.rule.updatedBy).toBe('alert-updater'); + expect(call.rule.createdBy).toBe('rule-creator'); + expect(call.rule.updatedBy).toBe('rule-updater'); expect(call.rule.createdAt).toBe(mockDate); expect(call.rule.updatedAt).toBe(mockDate); expect(call.rule.notifyWhen).toBe('onActiveAlert'); expect(call.rule.throttle).toBe(null); expect(call.rule.producer).toBe('alerts'); expect(call.rule.ruleTypeId).toBe('test'); - expect(call.rule.ruleTypeName).toBe('My test alert'); + expect(call.rule.ruleTypeName).toBe('My test rule'); expect(call.rule.actions).toMatchInlineSnapshot(` Array [ Object { @@ -288,10 +288,10 @@ describe('Task Runner', () => { const logger = taskRunnerFactoryInitializerParams.logger; expect(logger.debug).toHaveBeenCalledTimes(3); - expect(logger.debug).nthCalledWith(1, 'executing alert test:1 at 1970-01-01T00:00:00.000Z'); + expect(logger.debug).nthCalledWith(1, 'executing rule test:1 at 1970-01-01T00:00:00.000Z'); expect(logger.debug).nthCalledWith( 2, - 'alertExecutionStatus for test:1: {"lastExecutionDate":"1970-01-01T00:00:00.000Z","status":"ok"}' + 'ruleExecutionStatus for test:1: {"lastExecutionDate":"1970-01-01T00:00:00.000Z","status":"ok"}' ); const eventLogger = taskRunnerFactoryInitializerParams.eventLogger; @@ -299,7 +299,6 @@ describe('Task Runner', () => { expect(eventLogger.startTiming).toHaveBeenCalledTimes(1); expect(eventLogger.logEvent.mock.calls[0][0]).toMatchInlineSnapshot(` Object { - "@timestamp": "1970-01-01T00:00:00.000Z", "event": Object { "action": "execute-start", "category": Array [ @@ -322,7 +321,7 @@ describe('Task Runner', () => { "scheduled": "1970-01-01T00:00:00.000Z", }, }, - "message": "alert execution start: \\"1\\"", + "message": "rule execution start: \\"1\\"", "rule": Object { "category": "test", "id": "1", @@ -354,14 +353,14 @@ describe('Task Runner', () => { id: '1', name: 'execute test', type: 'alert', - description: 'execute [test] with name [alert-name] in [default] namespace', + description: 'execute [test] with name [rule-name] in [default] namespace', }, expect.any(Function) ); }); testAgainstEphemeralSupport( - 'actionsPlugin.execute is called per alert instance that is scheduled', + 'actionsPlugin.execute is called per alert alert that is scheduled', ( customTaskRunnerFactoryInitializerParams: TaskRunnerFactoryInitializerParamsType, enqueueFunction: (options: ExecuteOptions) => Promise @@ -374,7 +373,7 @@ describe('Task Runner', () => { true ); actionsClient.ephemeralEnqueuedExecution.mockResolvedValue(new Promise(() => {})); - alertType.executor.mockImplementation( + ruleType.executor.mockImplementation( async ({ services: executorServices, }: AlertExecutorOptions< @@ -390,11 +389,11 @@ describe('Task Runner', () => { } ); const taskRunner = new TaskRunner( - alertType, + ruleType, mockedTaskInstance, customTaskRunnerFactoryInitializerParams ); - rulesClient.get.mockResolvedValue(mockedAlertTypeSavedObject); + rulesClient.get.mockResolvedValue(mockedRuleTypeSavedObject); encryptedSavedObjectsClient.getDecryptedAsInternalUser.mockResolvedValue({ id: '1', type: 'alert', @@ -436,21 +435,20 @@ describe('Task Runner', () => { const logger = customTaskRunnerFactoryInitializerParams.logger; expect(logger.debug).toHaveBeenCalledTimes(4); - expect(logger.debug).nthCalledWith(1, 'executing alert test:1 at 1970-01-01T00:00:00.000Z'); + expect(logger.debug).nthCalledWith(1, 'executing rule test:1 at 1970-01-01T00:00:00.000Z'); expect(logger.debug).nthCalledWith( 2, - `alert test:1: 'alert-name' has 1 active alert instances: [{\"instanceId\":\"1\",\"actionGroup\":\"default\"}]` + `rule test:1: 'rule-name' has 1 active alerts: [{\"instanceId\":\"1\",\"actionGroup\":\"default\"}]` ); expect(logger.debug).nthCalledWith( 3, - 'alertExecutionStatus for test:1: {"lastExecutionDate":"1970-01-01T00:00:00.000Z","status":"active"}' + 'ruleExecutionStatus for test:1: {"lastExecutionDate":"1970-01-01T00:00:00.000Z","status":"active"}' ); - // alertExecutionStatus for test:1: {\"lastExecutionDate\":\"1970-01-01T00:00:00.000Z\",\"status\":\"error\",\"error\":{\"reason\":\"unknown\",\"message\":\"Cannot read property 'catch' of undefined\"}} + // ruleExecutionStatus for test:1: {\"lastExecutionDate\":\"1970-01-01T00:00:00.000Z\",\"status\":\"error\",\"error\":{\"reason\":\"unknown\",\"message\":\"Cannot read property 'catch' of undefined\"}} const eventLogger = customTaskRunnerFactoryInitializerParams.eventLogger; expect(eventLogger.logEvent).toHaveBeenCalledTimes(5); expect(eventLogger.logEvent).toHaveBeenNthCalledWith(1, { - '@timestamp': '1970-01-01T00:00:00.000Z', event: { action: 'execute-start', category: ['alerts'], @@ -471,7 +469,7 @@ describe('Task Runner', () => { }, ], }, - message: `alert execution start: "1"`, + message: `rule execution start: "1"`, rule: { category: 'test', id: '1', @@ -503,12 +501,12 @@ describe('Task Runner', () => { }, ], }, - message: "test:1: 'alert-name' created new instance: '1'", + message: "test:1: 'rule-name' created new alert: '1'", rule: { category: 'test', id: '1', license: 'basic', - name: 'alert-name', + name: 'rule-name', namespace: undefined, ruleset: 'alerts', }, @@ -532,12 +530,12 @@ describe('Task Runner', () => { ], }, message: - "test:1: 'alert-name' active instance: '1' in actionGroup(subgroup): 'default(subDefault)'", + "test:1: 'rule-name' active alert: '1' in actionGroup(subgroup): 'default(subDefault)'", rule: { category: 'test', id: '1', license: 'basic', - name: 'alert-name', + name: 'rule-name', namespace: undefined, ruleset: 'alerts', }, @@ -571,18 +569,17 @@ describe('Task Runner', () => { ], }, message: - "alert: test:1: 'alert-name' instanceId: '1' scheduled actionGroup(subgroup): 'default(subDefault)' action: action:1", + "alert: test:1: 'rule-name' instanceId: '1' scheduled actionGroup(subgroup): 'default(subDefault)' action: action:1", rule: { category: 'test', id: '1', license: 'basic', - name: 'alert-name', + name: 'rule-name', namespace: undefined, ruleset: 'alerts', }, }); expect(eventLogger.logEvent).toHaveBeenNthCalledWith(5, { - '@timestamp': '1970-01-01T00:00:00.000Z', event: { action: 'execute', category: ['alerts'], kind: 'alert', outcome: 'success' }, kibana: { alerting: { @@ -602,12 +599,12 @@ describe('Task Runner', () => { }, ], }, - message: "alert executed: test:1: 'alert-name'", + message: "rule executed: test:1: 'rule-name'", rule: { category: 'test', id: '1', license: 'basic', - name: 'alert-name', + name: 'rule-name', ruleset: 'alerts', }, }); @@ -617,7 +614,7 @@ describe('Task Runner', () => { test('actionsPlugin.execute is skipped if muteAll is true', async () => { taskRunnerFactoryInitializerParams.actionsPlugin.isActionTypeEnabled.mockReturnValue(true); taskRunnerFactoryInitializerParams.actionsPlugin.isActionExecutable.mockReturnValue(true); - alertType.executor.mockImplementation( + ruleType.executor.mockImplementation( async ({ services: executorServices, }: AlertExecutorOptions< @@ -631,12 +628,12 @@ describe('Task Runner', () => { } ); const taskRunner = new TaskRunner( - alertType, + ruleType, mockedTaskInstance, taskRunnerFactoryInitializerParams ); rulesClient.get.mockResolvedValue({ - ...mockedAlertTypeSavedObject, + ...mockedRuleTypeSavedObject, muteAll: true, }); encryptedSavedObjectsClient.getDecryptedAsInternalUser.mockResolvedValue({ @@ -653,25 +650,24 @@ describe('Task Runner', () => { const logger = taskRunnerFactoryInitializerParams.logger; expect(logger.debug).toHaveBeenCalledTimes(5); - expect(logger.debug).nthCalledWith(1, 'executing alert test:1 at 1970-01-01T00:00:00.000Z'); + expect(logger.debug).nthCalledWith(1, 'executing rule test:1 at 1970-01-01T00:00:00.000Z'); expect(logger.debug).nthCalledWith( 2, - `alert test:1: 'alert-name' has 1 active alert instances: [{\"instanceId\":\"1\",\"actionGroup\":\"default\"}]` + `rule test:1: 'rule-name' has 1 active alerts: [{\"instanceId\":\"1\",\"actionGroup\":\"default\"}]` ); expect(logger.debug).nthCalledWith( 3, - `no scheduling of actions for alert test:1: 'alert-name': alert is muted.` + `no scheduling of actions for rule test:1: 'rule-name': rule is muted.` ); expect(logger.debug).nthCalledWith( 4, - 'alertExecutionStatus for test:1: {"lastExecutionDate":"1970-01-01T00:00:00.000Z","status":"active"}' + 'ruleExecutionStatus for test:1: {"lastExecutionDate":"1970-01-01T00:00:00.000Z","status":"active"}' ); const eventLogger = taskRunnerFactoryInitializerParams.eventLogger; expect(eventLogger.startTiming).toHaveBeenCalledTimes(1); expect(eventLogger.logEvent).toHaveBeenCalledTimes(4); expect(eventLogger.logEvent).toHaveBeenNthCalledWith(1, { - '@timestamp': '1970-01-01T00:00:00.000Z', event: { action: 'execute-start', category: ['alerts'], @@ -692,7 +688,7 @@ describe('Task Runner', () => { }, ], }, - message: `alert execution start: \"1\"`, + message: `rule execution start: \"1\"`, rule: { category: 'test', id: '1', @@ -723,12 +719,12 @@ describe('Task Runner', () => { }, ], }, - message: "test:1: 'alert-name' created new instance: '1'", + message: "test:1: 'rule-name' created new alert: '1'", rule: { category: 'test', id: '1', license: 'basic', - name: 'alert-name', + name: 'rule-name', namespace: undefined, ruleset: 'alerts', }, @@ -756,18 +752,17 @@ describe('Task Runner', () => { }, ], }, - message: "test:1: 'alert-name' active instance: '1' in actionGroup: 'default'", + message: "test:1: 'rule-name' active alert: '1' in actionGroup: 'default'", rule: { category: 'test', id: '1', license: 'basic', - name: 'alert-name', + name: 'rule-name', namespace: undefined, ruleset: 'alerts', }, }); expect(eventLogger.logEvent).toHaveBeenNthCalledWith(4, { - '@timestamp': '1970-01-01T00:00:00.000Z', event: { action: 'execute', category: ['alerts'], @@ -792,19 +787,19 @@ describe('Task Runner', () => { }, ], }, - message: "alert executed: test:1: 'alert-name'", + message: "rule executed: test:1: 'rule-name'", rule: { category: 'test', id: '1', license: 'basic', - name: 'alert-name', + name: 'rule-name', ruleset: 'alerts', }, }); }); testAgainstEphemeralSupport( - 'skips firing actions for active instance if instance is muted', + 'skips firing actions for active alert if alert is muted', ( customTaskRunnerFactoryInitializerParams: TaskRunnerFactoryInitializerParamsType, enqueueFunction: (options: ExecuteOptions) => Promise @@ -817,7 +812,7 @@ describe('Task Runner', () => { true ); actionsClient.ephemeralEnqueuedExecution.mockResolvedValue(new Promise(() => {})); - alertType.executor.mockImplementation( + ruleType.executor.mockImplementation( async ({ services: executorServices, }: AlertExecutorOptions< @@ -832,12 +827,12 @@ describe('Task Runner', () => { } ); const taskRunner = new TaskRunner( - alertType, + ruleType, mockedTaskInstance, customTaskRunnerFactoryInitializerParams ); rulesClient.get.mockResolvedValue({ - ...mockedAlertTypeSavedObject, + ...mockedRuleTypeSavedObject, mutedInstanceIds: ['2'], }); encryptedSavedObjectsClient.getDecryptedAsInternalUser.mockResolvedValue({ @@ -854,26 +849,26 @@ describe('Task Runner', () => { const logger = customTaskRunnerFactoryInitializerParams.logger; expect(logger.debug).toHaveBeenCalledTimes(5); - expect(logger.debug).nthCalledWith(1, 'executing alert test:1 at 1970-01-01T00:00:00.000Z'); + expect(logger.debug).nthCalledWith(1, 'executing rule test:1 at 1970-01-01T00:00:00.000Z'); expect(logger.debug).nthCalledWith( 2, - `alert test:1: 'alert-name' has 2 active alert instances: [{\"instanceId\":\"1\",\"actionGroup\":\"default\"},{\"instanceId\":\"2\",\"actionGroup\":\"default\"}]` + `rule test:1: 'rule-name' has 2 active alerts: [{\"instanceId\":\"1\",\"actionGroup\":\"default\"},{\"instanceId\":\"2\",\"actionGroup\":\"default\"}]` ); expect(logger.debug).nthCalledWith( 3, - `skipping scheduling of actions for '2' in alert test:1: 'alert-name': instance is muted` + `skipping scheduling of actions for '2' in rule test:1: 'rule-name': rule is muted` ); expect(logger.debug).nthCalledWith( 4, - 'alertExecutionStatus for test:1: {"lastExecutionDate":"1970-01-01T00:00:00.000Z","status":"active"}' + 'ruleExecutionStatus for test:1: {"lastExecutionDate":"1970-01-01T00:00:00.000Z","status":"active"}' ); } ); - test('actionsPlugin.execute is not called when notifyWhen=onActionGroupChange and alert instance state does not change', async () => { + test('actionsPlugin.execute is not called when notifyWhen=onActionGroupChange and alert alert state does not change', async () => { taskRunnerFactoryInitializerParams.actionsPlugin.isActionTypeEnabled.mockReturnValue(true); taskRunnerFactoryInitializerParams.actionsPlugin.isActionExecutable.mockReturnValue(true); - alertType.executor.mockImplementation( + ruleType.executor.mockImplementation( async ({ services: executorServices, }: AlertExecutorOptions< @@ -887,7 +882,7 @@ describe('Task Runner', () => { } ); const taskRunner = new TaskRunner( - alertType, + ruleType, { ...mockedTaskInstance, state: { @@ -909,7 +904,7 @@ describe('Task Runner', () => { taskRunnerFactoryInitializerParams ); rulesClient.get.mockResolvedValue({ - ...mockedAlertTypeSavedObject, + ...mockedRuleTypeSavedObject, notifyWhen: 'onActionGroupChange', }); encryptedSavedObjectsClient.getDecryptedAsInternalUser.mockResolvedValue({ @@ -931,7 +926,6 @@ describe('Task Runner', () => { Array [ Array [ Object { - "@timestamp": "1970-01-01T00:00:00.000Z", "event": Object { "action": "execute-start", "category": Array [ @@ -954,7 +948,7 @@ describe('Task Runner', () => { "scheduled": "1970-01-01T00:00:00.000Z", }, }, - "message": "alert execution start: \\"1\\"", + "message": "rule execution start: \\"1\\"", "rule": Object { "category": "test", "id": "1", @@ -989,19 +983,18 @@ describe('Task Runner', () => { }, ], }, - "message": "test:1: 'alert-name' active instance: '1' in actionGroup: 'default'", + "message": "test:1: 'rule-name' active alert: '1' in actionGroup: 'default'", "rule": Object { "category": "test", "id": "1", "license": "basic", - "name": "alert-name", + "name": "rule-name", "ruleset": "alerts", }, }, ], Array [ Object { - "@timestamp": "1970-01-01T00:00:00.000Z", "event": Object { "action": "execute", "category": Array [ @@ -1028,12 +1021,12 @@ describe('Task Runner', () => { "scheduled": "1970-01-01T00:00:00.000Z", }, }, - "message": "alert executed: test:1: 'alert-name'", + "message": "rule executed: test:1: 'rule-name'", "rule": Object { "category": "test", "id": "1", "license": "basic", - "name": "alert-name", + "name": "rule-name", "ruleset": "alerts", }, }, @@ -1043,7 +1036,7 @@ describe('Task Runner', () => { }); testAgainstEphemeralSupport( - 'actionsPlugin.execute is called when notifyWhen=onActionGroupChange and alert instance state has changed', + 'actionsPlugin.execute is called when notifyWhen=onActionGroupChange and alert alert state has changed', ( customTaskRunnerFactoryInitializerParams: TaskRunnerFactoryInitializerParamsType, enqueueFunction: (options: ExecuteOptions) => Promise @@ -1055,7 +1048,7 @@ describe('Task Runner', () => { customTaskRunnerFactoryInitializerParams.actionsPlugin.isActionExecutable.mockReturnValue( true ); - alertType.executor.mockImplementation( + ruleType.executor.mockImplementation( async ({ services: executorServices, }: AlertExecutorOptions< @@ -1069,7 +1062,7 @@ describe('Task Runner', () => { } ); const taskRunner = new TaskRunner( - alertType, + ruleType, { ...mockedTaskInstance, state: { @@ -1087,7 +1080,7 @@ describe('Task Runner', () => { customTaskRunnerFactoryInitializerParams ); rulesClient.get.mockResolvedValue({ - ...mockedAlertTypeSavedObject, + ...mockedRuleTypeSavedObject, notifyWhen: 'onActionGroupChange', }); encryptedSavedObjectsClient.getDecryptedAsInternalUser.mockResolvedValue({ @@ -1105,7 +1098,7 @@ describe('Task Runner', () => { ); testAgainstEphemeralSupport( - 'actionsPlugin.execute is called when notifyWhen=onActionGroupChange and alert instance state subgroup has changed', + 'actionsPlugin.execute is called when notifyWhen=onActionGroupChange and alert state subgroup has changed', ( customTaskRunnerFactoryInitializerParams: TaskRunnerFactoryInitializerParamsType, enqueueFunction: (options: ExecuteOptions) => Promise @@ -1118,7 +1111,7 @@ describe('Task Runner', () => { customTaskRunnerFactoryInitializerParams.actionsPlugin.isActionExecutable.mockReturnValue( true ); - alertType.executor.mockImplementation( + ruleType.executor.mockImplementation( async ({ services: executorServices, }: AlertExecutorOptions< @@ -1134,7 +1127,7 @@ describe('Task Runner', () => { } ); const taskRunner = new TaskRunner( - alertType, + ruleType, { ...mockedTaskInstance, state: { @@ -1156,7 +1149,7 @@ describe('Task Runner', () => { customTaskRunnerFactoryInitializerParams ); rulesClient.get.mockResolvedValue({ - ...mockedAlertTypeSavedObject, + ...mockedRuleTypeSavedObject, notifyWhen: 'onActionGroupChange', }); encryptedSavedObjectsClient.getDecryptedAsInternalUser.mockResolvedValue({ @@ -1187,7 +1180,7 @@ describe('Task Runner', () => { true ); actionsClient.ephemeralEnqueuedExecution.mockResolvedValue(new Promise(() => {})); - alertType.executor.mockImplementation( + ruleType.executor.mockImplementation( async ({ services: executorServices, }: AlertExecutorOptions< @@ -1201,11 +1194,11 @@ describe('Task Runner', () => { } ); const taskRunner = new TaskRunner( - alertType, + ruleType, mockedTaskInstance, customTaskRunnerFactoryInitializerParams ); - rulesClient.get.mockResolvedValue(mockedAlertTypeSavedObject); + rulesClient.get.mockResolvedValue(mockedRuleTypeSavedObject); encryptedSavedObjectsClient.getDecryptedAsInternalUser.mockResolvedValueOnce({ id: '1', type: 'alert', @@ -1272,7 +1265,6 @@ describe('Task Runner', () => { Array [ Array [ Object { - "@timestamp": "1970-01-01T00:00:00.000Z", "event": Object { "action": "execute-start", "category": Array [ @@ -1295,7 +1287,7 @@ describe('Task Runner', () => { "scheduled": "1970-01-01T00:00:00.000Z", }, }, - "message": "alert execution start: \\"1\\"", + "message": "rule execution start: \\"1\\"", "rule": Object { "category": "test", "id": "1", @@ -1330,12 +1322,12 @@ describe('Task Runner', () => { }, ], }, - "message": "test:1: 'alert-name' created new instance: '1'", + "message": "test:1: 'rule-name' created new alert: '1'", "rule": Object { "category": "test", "id": "1", "license": "basic", - "name": "alert-name", + "name": "rule-name", "ruleset": "alerts", }, }, @@ -1366,12 +1358,12 @@ describe('Task Runner', () => { }, ], }, - "message": "test:1: 'alert-name' active instance: '1' in actionGroup: 'default'", + "message": "test:1: 'rule-name' active alert: '1' in actionGroup: 'default'", "rule": Object { "category": "test", "id": "1", "license": "basic", - "name": "alert-name", + "name": "rule-name", "ruleset": "alerts", }, }, @@ -1406,19 +1398,18 @@ describe('Task Runner', () => { }, ], }, - "message": "alert: test:1: 'alert-name' instanceId: '1' scheduled actionGroup: 'default' action: action:1", + "message": "alert: test:1: 'rule-name' instanceId: '1' scheduled actionGroup: 'default' action: action:1", "rule": Object { "category": "test", "id": "1", "license": "basic", - "name": "alert-name", + "name": "rule-name", "ruleset": "alerts", }, }, ], Array [ Object { - "@timestamp": "1970-01-01T00:00:00.000Z", "event": Object { "action": "execute", "category": Array [ @@ -1445,12 +1436,12 @@ describe('Task Runner', () => { "scheduled": "1970-01-01T00:00:00.000Z", }, }, - "message": "alert executed: test:1: 'alert-name'", + "message": "rule executed: test:1: 'rule-name'", "rule": Object { "category": "test", "id": "1", "license": "basic", - "name": "alert-name", + "name": "rule-name", "ruleset": "alerts", }, }, @@ -1475,7 +1466,7 @@ describe('Task Runner', () => { ); actionsClient.ephemeralEnqueuedExecution.mockResolvedValue(new Promise(() => {})); - alertType.executor.mockImplementation( + ruleType.executor.mockImplementation( async ({ services: executorServices, }: AlertExecutorOptions< @@ -1489,7 +1480,7 @@ describe('Task Runner', () => { } ); const taskRunner = new TaskRunner( - alertType, + ruleType, { ...mockedTaskInstance, state: { @@ -1516,7 +1507,7 @@ describe('Task Runner', () => { }, customTaskRunnerFactoryInitializerParams ); - rulesClient.get.mockResolvedValue(mockedAlertTypeSavedObject); + rulesClient.get.mockResolvedValue(mockedRuleTypeSavedObject); encryptedSavedObjectsClient.getDecryptedAsInternalUser.mockResolvedValue({ id: '1', type: 'alert', @@ -1548,18 +1539,18 @@ describe('Task Runner', () => { const logger = customTaskRunnerFactoryInitializerParams.logger; expect(logger.debug).toHaveBeenCalledTimes(5); - expect(logger.debug).nthCalledWith(1, 'executing alert test:1 at 1970-01-01T00:00:00.000Z'); + expect(logger.debug).nthCalledWith(1, 'executing rule test:1 at 1970-01-01T00:00:00.000Z'); expect(logger.debug).nthCalledWith( 2, - `alert test:1: 'alert-name' has 1 active alert instances: [{\"instanceId\":\"1\",\"actionGroup\":\"default\"}]` + `rule test:1: 'rule-name' has 1 active alerts: [{\"instanceId\":\"1\",\"actionGroup\":\"default\"}]` ); expect(logger.debug).nthCalledWith( 3, - `alert test:1: 'alert-name' has 1 recovered alert instances: [\"2\"]` + `rule test:1: 'rule-name' has 1 recovered alerts: [\"2\"]` ); expect(logger.debug).nthCalledWith( 4, - 'alertExecutionStatus for test:1: {"lastExecutionDate":"1970-01-01T00:00:00.000Z","status":"active"}' + 'ruleExecutionStatus for test:1: {"lastExecutionDate":"1970-01-01T00:00:00.000Z","status":"active"}' ); const eventLogger = customTaskRunnerFactoryInitializerParams.eventLogger; @@ -1569,7 +1560,6 @@ describe('Task Runner', () => { Array [ Array [ Object { - "@timestamp": "1970-01-01T00:00:00.000Z", "event": Object { "action": "execute-start", "category": Array [ @@ -1592,7 +1582,7 @@ describe('Task Runner', () => { "scheduled": "1970-01-01T00:00:00.000Z", }, }, - "message": "alert execution start: \\"1\\"", + "message": "rule execution start: \\"1\\"", "rule": Object { "category": "test", "id": "1", @@ -1627,12 +1617,12 @@ describe('Task Runner', () => { }, ], }, - "message": "test:1: 'alert-name' instance '2' has recovered", + "message": "test:1: 'rule-name' alert '2' has recovered", "rule": Object { "category": "test", "id": "1", "license": "basic", - "name": "alert-name", + "name": "rule-name", "ruleset": "alerts", }, }, @@ -1663,12 +1653,12 @@ describe('Task Runner', () => { }, ], }, - "message": "test:1: 'alert-name' active instance: '1' in actionGroup: 'default'", + "message": "test:1: 'rule-name' active alert: '1' in actionGroup: 'default'", "rule": Object { "category": "test", "id": "1", "license": "basic", - "name": "alert-name", + "name": "rule-name", "ruleset": "alerts", }, }, @@ -1703,12 +1693,12 @@ describe('Task Runner', () => { }, ], }, - "message": "alert: test:1: 'alert-name' instanceId: '2' scheduled actionGroup: 'recovered' action: action:2", + "message": "alert: test:1: 'rule-name' instanceId: '2' scheduled actionGroup: 'recovered' action: action:2", "rule": Object { "category": "test", "id": "1", "license": "basic", - "name": "alert-name", + "name": "rule-name", "ruleset": "alerts", }, }, @@ -1743,19 +1733,18 @@ describe('Task Runner', () => { }, ], }, - "message": "alert: test:1: 'alert-name' instanceId: '1' scheduled actionGroup: 'default' action: action:1", + "message": "alert: test:1: 'rule-name' instanceId: '1' scheduled actionGroup: 'default' action: action:1", "rule": Object { "category": "test", "id": "1", "license": "basic", - "name": "alert-name", + "name": "rule-name", "ruleset": "alerts", }, }, ], Array [ Object { - "@timestamp": "1970-01-01T00:00:00.000Z", "event": Object { "action": "execute", "category": Array [ @@ -1782,12 +1771,12 @@ describe('Task Runner', () => { "scheduled": "1970-01-01T00:00:00.000Z", }, }, - "message": "alert executed: test:1: 'alert-name'", + "message": "rule executed: test:1: 'rule-name'", "rule": Object { "category": "test", "id": "1", "license": "basic", - "name": "alert-name", + "name": "rule-name", "ruleset": "alerts", }, }, @@ -1842,7 +1831,7 @@ describe('Task Runner', () => { ); actionsClient.ephemeralEnqueuedExecution.mockResolvedValue(new Promise(() => {})); - alertType.executor.mockImplementation( + ruleType.executor.mockImplementation( async ({ services: executorServices, }: AlertExecutorOptions< @@ -1859,7 +1848,7 @@ describe('Task Runner', () => { } ); const taskRunner = new TaskRunner( - alertType, + ruleType, { ...mockedTaskInstance, state: { @@ -1875,7 +1864,7 @@ describe('Task Runner', () => { }, customTaskRunnerFactoryInitializerParams ); - rulesClient.get.mockResolvedValue(mockedAlertTypeSavedObject); + rulesClient.get.mockResolvedValue(mockedRuleTypeSavedObject); encryptedSavedObjectsClient.getDecryptedAsInternalUser.mockResolvedValue({ id: alertId, type: 'alert', @@ -1905,16 +1894,16 @@ describe('Task Runner', () => { const logger = customTaskRunnerFactoryInitializerParams.logger; expect(logger.debug).toHaveBeenCalledWith( - `alert test:${alertId}: 'alert-name' has 1 active alert instances: [{\"instanceId\":\"1\",\"actionGroup\":\"default\"}]` + `rule test:${alertId}: 'rule-name' has 1 active alerts: [{\"instanceId\":\"1\",\"actionGroup\":\"default\"}]` ); expect(logger.debug).nthCalledWith( 3, - `alert test:${alertId}: 'alert-name' has 1 recovered alert instances: [\"2\"]` + `rule test:${alertId}: 'rule-name' has 1 recovered alerts: [\"2\"]` ); expect(logger.debug).nthCalledWith( 4, - `alertExecutionStatus for test:${alertId}: {"lastExecutionDate":"1970-01-01T00:00:00.000Z","status":"active"}` + `ruleExecutionStatus for test:${alertId}: {"lastExecutionDate":"1970-01-01T00:00:00.000Z","status":"active"}` ); const eventLogger = customTaskRunnerFactoryInitializerParams.eventLogger; @@ -1945,13 +1934,13 @@ describe('Task Runner', () => { id: 'customRecovered', name: 'Custom Recovered', }; - const alertTypeWithCustomRecovery = { - ...alertType, + const ruleTypeWithCustomRecovery = { + ...ruleType, recoveryActionGroup, actionGroups: [{ id: 'default', name: 'Default' }, recoveryActionGroup], }; - alertTypeWithCustomRecovery.executor.mockImplementation( + ruleTypeWithCustomRecovery.executor.mockImplementation( async ({ services: executorServices, }: AlertExecutorOptions< @@ -1965,7 +1954,7 @@ describe('Task Runner', () => { } ); const taskRunner = new TaskRunner( - alertTypeWithCustomRecovery, + ruleTypeWithCustomRecovery, { ...mockedTaskInstance, state: { @@ -1979,7 +1968,7 @@ describe('Task Runner', () => { customTaskRunnerFactoryInitializerParams ); rulesClient.get.mockResolvedValue({ - ...mockedAlertTypeSavedObject, + ...mockedRuleTypeSavedObject, actions: [ { group: 'default', @@ -2060,7 +2049,7 @@ describe('Task Runner', () => { ); test('persists alertInstances passed in from state, only if they are scheduled for execution', async () => { - alertType.executor.mockImplementation( + ruleType.executor.mockImplementation( async ({ services: executorServices, }: AlertExecutorOptions< @@ -2075,7 +2064,7 @@ describe('Task Runner', () => { ); const date = new Date().toISOString(); const taskRunner = new TaskRunner( - alertType, + ruleType, { ...mockedTaskInstance, state: { @@ -2102,7 +2091,7 @@ describe('Task Runner', () => { }, taskRunnerFactoryInitializerParams ); - rulesClient.get.mockResolvedValue(mockedAlertTypeSavedObject); + rulesClient.get.mockResolvedValue(mockedRuleTypeSavedObject); encryptedSavedObjectsClient.getDecryptedAsInternalUser.mockResolvedValue({ id: '1', type: 'alert', @@ -2139,7 +2128,6 @@ describe('Task Runner', () => { Array [ Array [ Object { - "@timestamp": "1970-01-01T00:00:00.000Z", "event": Object { "action": "execute-start", "category": Array [ @@ -2162,7 +2150,7 @@ describe('Task Runner', () => { "scheduled": "1970-01-01T00:00:00.000Z", }, }, - "message": "alert execution start: \\"1\\"", + "message": "rule execution start: \\"1\\"", "rule": Object { "category": "test", "id": "1", @@ -2198,12 +2186,12 @@ describe('Task Runner', () => { }, ], }, - "message": "test:1: 'alert-name' instance '2' has recovered", + "message": "test:1: 'rule-name' alert '2' has recovered", "rule": Object { "category": "test", "id": "1", "license": "basic", - "name": "alert-name", + "name": "rule-name", "ruleset": "alerts", }, }, @@ -2234,19 +2222,18 @@ describe('Task Runner', () => { }, ], }, - "message": "test:1: 'alert-name' active instance: '1' in actionGroup: 'default'", + "message": "test:1: 'rule-name' active alert: '1' in actionGroup: 'default'", "rule": Object { "category": "test", "id": "1", "license": "basic", - "name": "alert-name", + "name": "rule-name", "ruleset": "alerts", }, }, ], Array [ Object { - "@timestamp": "1970-01-01T00:00:00.000Z", "event": Object { "action": "execute", "category": Array [ @@ -2273,12 +2260,12 @@ describe('Task Runner', () => { "scheduled": "1970-01-01T00:00:00.000Z", }, }, - "message": "alert executed: test:1: 'alert-name'", + "message": "rule executed: test:1: 'rule-name'", "rule": Object { "category": "test", "id": "1", "license": "basic", - "name": "alert-name", + "name": "rule-name", "ruleset": "alerts", }, }, @@ -2290,7 +2277,7 @@ describe('Task Runner', () => { test('validates params before executing the alert type', async () => { const taskRunner = new TaskRunner( { - ...alertType, + ...ruleType, validate: { params: schema.object({ param1: schema.string(), @@ -2306,7 +2293,7 @@ describe('Task Runner', () => { }, taskRunnerFactoryInitializerParams ); - rulesClient.get.mockResolvedValue(mockedAlertTypeSavedObject); + rulesClient.get.mockResolvedValue(mockedRuleTypeSavedObject); encryptedSavedObjectsClient.getDecryptedAsInternalUser.mockResolvedValueOnce({ id: '1', type: 'alert', @@ -2325,17 +2312,17 @@ describe('Task Runner', () => { } `); expect(taskRunnerFactoryInitializerParams.logger.error).toHaveBeenCalledWith( - `Executing Alert foo:test:1 has resulted in Error: params invalid: [param1]: expected value of type [string] but got [undefined]` + `Executing Rule foo:test:1 has resulted in Error: params invalid: [param1]: expected value of type [string] but got [undefined]` ); }); test('uses API key when provided', async () => { const taskRunner = new TaskRunner( - alertType, + ruleType, mockedTaskInstance, taskRunnerFactoryInitializerParams ); - rulesClient.get.mockResolvedValue(mockedAlertTypeSavedObject); + rulesClient.get.mockResolvedValue(mockedRuleTypeSavedObject); encryptedSavedObjectsClient.getDecryptedAsInternalUser.mockResolvedValueOnce({ id: '1', type: 'alert', @@ -2365,11 +2352,11 @@ describe('Task Runner', () => { test(`doesn't use API key when not provided`, async () => { const taskRunner = new TaskRunner( - alertType, + ruleType, mockedTaskInstance, taskRunnerFactoryInitializerParams ); - rulesClient.get.mockResolvedValue(mockedAlertTypeSavedObject); + rulesClient.get.mockResolvedValue(mockedRuleTypeSavedObject); encryptedSavedObjectsClient.getDecryptedAsInternalUser.mockResolvedValueOnce({ id: '1', type: 'alert', @@ -2397,14 +2384,14 @@ describe('Task Runner', () => { test('rescheduled the Alert if the schedule has update during a task run', async () => { const taskRunner = new TaskRunner( - alertType, + ruleType, mockedTaskInstance, taskRunnerFactoryInitializerParams ); - rulesClient.get.mockResolvedValueOnce(mockedAlertTypeSavedObject); + rulesClient.get.mockResolvedValueOnce(mockedRuleTypeSavedObject); rulesClient.get.mockResolvedValueOnce({ - ...mockedAlertTypeSavedObject, + ...mockedRuleTypeSavedObject, schedule: { interval: '30s' }, }); encryptedSavedObjectsClient.getDecryptedAsInternalUser.mockResolvedValueOnce({ @@ -2431,8 +2418,8 @@ describe('Task Runner', () => { `); }); - test('recovers gracefully when the AlertType executor throws an exception', async () => { - alertType.executor.mockImplementation( + test('recovers gracefully when the RuleType executor throws an exception', async () => { + ruleType.executor.mockImplementation( async ({ services: executorServices, }: AlertExecutorOptions< @@ -2447,12 +2434,12 @@ describe('Task Runner', () => { ); const taskRunner = new TaskRunner( - alertType, + ruleType, mockedTaskInstance, taskRunnerFactoryInitializerParams ); - rulesClient.get.mockResolvedValue(mockedAlertTypeSavedObject); + rulesClient.get.mockResolvedValue(mockedRuleTypeSavedObject); encryptedSavedObjectsClient.getDecryptedAsInternalUser.mockResolvedValueOnce({ id: '1', type: 'alert', @@ -2481,7 +2468,6 @@ describe('Task Runner', () => { Array [ Array [ Object { - "@timestamp": "1970-01-01T00:00:00.000Z", "event": Object { "action": "execute-start", "category": Array [ @@ -2504,7 +2490,7 @@ describe('Task Runner', () => { "scheduled": "1970-01-01T00:00:00.000Z", }, }, - "message": "alert execution start: \\"1\\"", + "message": "rule execution start: \\"1\\"", "rule": Object { "category": "test", "id": "1", @@ -2515,7 +2501,6 @@ describe('Task Runner', () => { ], Array [ Object { - "@timestamp": "1970-01-01T00:00:00.000Z", "error": Object { "message": "OMG", }, @@ -2546,7 +2531,7 @@ describe('Task Runner', () => { "scheduled": "1970-01-01T00:00:00.000Z", }, }, - "message": "alert execution failure: test:1: 'alert-name'", + "message": "rule execution failure: test:1: 'rule-name'", "rule": Object { "category": "test", "id": "1", @@ -2565,12 +2550,12 @@ describe('Task Runner', () => { }); const taskRunner = new TaskRunner( - alertType, + ruleType, mockedTaskInstance, taskRunnerFactoryInitializerParams ); - rulesClient.get.mockResolvedValue(mockedAlertTypeSavedObject); + rulesClient.get.mockResolvedValue(mockedRuleTypeSavedObject); const runnerResult = await taskRunner.run(); @@ -2590,7 +2575,6 @@ describe('Task Runner', () => { Array [ Array [ Object { - "@timestamp": "1970-01-01T00:00:00.000Z", "event": Object { "action": "execute-start", "category": Array [ @@ -2613,7 +2597,7 @@ describe('Task Runner', () => { "scheduled": "1970-01-01T00:00:00.000Z", }, }, - "message": "alert execution start: \\"1\\"", + "message": "rule execution start: \\"1\\"", "rule": Object { "category": "test", "id": "1", @@ -2624,7 +2608,6 @@ describe('Task Runner', () => { ], Array [ Object { - "@timestamp": "1970-01-01T00:00:00.000Z", "error": Object { "message": "OMG", }, @@ -2674,12 +2657,12 @@ describe('Task Runner', () => { }); const taskRunner = new TaskRunner( - alertType, + ruleType, mockedTaskInstance, taskRunnerFactoryInitializerParams ); - rulesClient.get.mockResolvedValue(mockedAlertTypeSavedObject); + rulesClient.get.mockResolvedValue(mockedRuleTypeSavedObject); encryptedSavedObjectsClient.getDecryptedAsInternalUser.mockResolvedValue({ id: '1', type: 'alert', @@ -2708,7 +2691,6 @@ describe('Task Runner', () => { Array [ Array [ Object { - "@timestamp": "1970-01-01T00:00:00.000Z", "event": Object { "action": "execute-start", "category": Array [ @@ -2731,7 +2713,7 @@ describe('Task Runner', () => { "scheduled": "1970-01-01T00:00:00.000Z", }, }, - "message": "alert execution start: \\"1\\"", + "message": "rule execution start: \\"1\\"", "rule": Object { "category": "test", "id": "1", @@ -2742,7 +2724,6 @@ describe('Task Runner', () => { ], Array [ Object { - "@timestamp": "1970-01-01T00:00:00.000Z", "error": Object { "message": "OMG", }, @@ -2792,12 +2773,12 @@ describe('Task Runner', () => { }); const taskRunner = new TaskRunner( - alertType, + ruleType, mockedTaskInstance, taskRunnerFactoryInitializerParams ); - rulesClient.get.mockResolvedValue(mockedAlertTypeSavedObject); + rulesClient.get.mockResolvedValue(mockedRuleTypeSavedObject); encryptedSavedObjectsClient.getDecryptedAsInternalUser.mockResolvedValue({ id: '1', type: 'alert', @@ -2826,7 +2807,6 @@ describe('Task Runner', () => { Array [ Array [ Object { - "@timestamp": "1970-01-01T00:00:00.000Z", "event": Object { "action": "execute-start", "category": Array [ @@ -2849,7 +2829,7 @@ describe('Task Runner', () => { "scheduled": "1970-01-01T00:00:00.000Z", }, }, - "message": "alert execution start: \\"1\\"", + "message": "rule execution start: \\"1\\"", "rule": Object { "category": "test", "id": "1", @@ -2860,7 +2840,6 @@ describe('Task Runner', () => { ], Array [ Object { - "@timestamp": "1970-01-01T00:00:00.000Z", "error": Object { "message": "OMG", }, @@ -2910,7 +2889,7 @@ describe('Task Runner', () => { }); const taskRunner = new TaskRunner( - alertType, + ruleType, mockedTaskInstance, taskRunnerFactoryInitializerParams ); @@ -2943,7 +2922,6 @@ describe('Task Runner', () => { Array [ Array [ Object { - "@timestamp": "1970-01-01T00:00:00.000Z", "event": Object { "action": "execute-start", "category": Array [ @@ -2966,7 +2944,7 @@ describe('Task Runner', () => { "scheduled": "1970-01-01T00:00:00.000Z", }, }, - "message": "alert execution start: \\"1\\"", + "message": "rule execution start: \\"1\\"", "rule": Object { "category": "test", "id": "1", @@ -2977,7 +2955,6 @@ describe('Task Runner', () => { ], Array [ Object { - "@timestamp": "1970-01-01T00:00:00.000Z", "error": Object { "message": "OMG", }, @@ -3031,7 +3008,7 @@ describe('Task Runner', () => { const legacyTaskInstance = omit(mockedTaskInstance, 'schedule'); const taskRunner = new TaskRunner( - alertType, + ruleType, legacyTaskInstance, taskRunnerFactoryInitializerParams ); @@ -3063,7 +3040,7 @@ describe('Task Runner', () => { previousStartedAt: '1970-01-05T00:00:00.000Z', }; - alertType.executor.mockImplementation( + ruleType.executor.mockImplementation( async ({ services: executorServices, }: AlertExecutorOptions< @@ -3078,7 +3055,7 @@ describe('Task Runner', () => { ); const taskRunner = new TaskRunner( - alertType, + ruleType, { ...mockedTaskInstance, state: originalAlertSate, @@ -3086,7 +3063,7 @@ describe('Task Runner', () => { taskRunnerFactoryInitializerParams ); - rulesClient.get.mockResolvedValue(mockedAlertTypeSavedObject); + rulesClient.get.mockResolvedValue(mockedRuleTypeSavedObject); encryptedSavedObjectsClient.getDecryptedAsInternalUser.mockResolvedValue({ id: '1', type: 'alert', @@ -3110,7 +3087,7 @@ describe('Task Runner', () => { }); const taskRunner = new TaskRunner( - alertType, + ruleType, { ...mockedTaskInstance, params: { @@ -3135,7 +3112,7 @@ describe('Task Runner', () => { return taskRunner.run().catch((ex) => { expect(ex).toMatchInlineSnapshot(`[Error: Saved object [alert/1] not found]`); expect(logger.debug).toHaveBeenCalledWith( - `Executing Alert foo:test:1 has resulted in Error: Saved object [alert/1] not found` + `Executing Rule foo:test:1 has resulted in Error: Saved object [alert/1] not found` ); expect(logger.warn).toHaveBeenCalledTimes(1); expect(logger.warn).nthCalledWith( @@ -3152,7 +3129,7 @@ describe('Task Runner', () => { }); const taskRunner = new TaskRunner( - alertType, + ruleType, { ...mockedTaskInstance, params: { @@ -3177,7 +3154,7 @@ describe('Task Runner', () => { return taskRunner.run().catch((ex) => { expect(ex).toMatchInlineSnapshot(`[Error: Saved object [alert/1] not found]`); expect(logger.debug).toHaveBeenCalledWith( - `Executing Alert test space:test:1 has resulted in Error: Saved object [alert/1] not found` + `Executing Rule test space:test:1 has resulted in Error: Saved object [alert/1] not found` ); expect(logger.warn).toHaveBeenCalledTimes(1); expect(logger.warn).nthCalledWith( @@ -3190,7 +3167,7 @@ describe('Task Runner', () => { test('start time is logged for new alerts', async () => { taskRunnerFactoryInitializerParams.actionsPlugin.isActionTypeEnabled.mockReturnValue(true); taskRunnerFactoryInitializerParams.actionsPlugin.isActionExecutable.mockReturnValue(true); - alertType.executor.mockImplementation( + ruleType.executor.mockImplementation( async ({ services: executorServices, }: AlertExecutorOptions< @@ -3205,7 +3182,7 @@ describe('Task Runner', () => { } ); const taskRunner = new TaskRunner( - alertType, + ruleType, { ...mockedTaskInstance, state: { @@ -3216,7 +3193,7 @@ describe('Task Runner', () => { taskRunnerFactoryInitializerParams ); rulesClient.get.mockResolvedValue({ - ...mockedAlertTypeSavedObject, + ...mockedRuleTypeSavedObject, notifyWhen: 'onActionGroupChange', actions: [], }); @@ -3238,7 +3215,6 @@ describe('Task Runner', () => { Array [ Array [ Object { - "@timestamp": "1970-01-01T00:00:00.000Z", "event": Object { "action": "execute-start", "category": Array [ @@ -3261,7 +3237,7 @@ describe('Task Runner', () => { "scheduled": "1970-01-01T00:00:00.000Z", }, }, - "message": "alert execution start: \\"1\\"", + "message": "rule execution start: \\"1\\"", "rule": Object { "category": "test", "id": "1", @@ -3296,12 +3272,12 @@ describe('Task Runner', () => { }, ], }, - "message": "test:1: 'alert-name' created new instance: '1'", + "message": "test:1: 'rule-name' created new alert: '1'", "rule": Object { "category": "test", "id": "1", "license": "basic", - "name": "alert-name", + "name": "rule-name", "ruleset": "alerts", }, }, @@ -3332,12 +3308,12 @@ describe('Task Runner', () => { }, ], }, - "message": "test:1: 'alert-name' created new instance: '2'", + "message": "test:1: 'rule-name' created new alert: '2'", "rule": Object { "category": "test", "id": "1", "license": "basic", - "name": "alert-name", + "name": "rule-name", "ruleset": "alerts", }, }, @@ -3368,12 +3344,12 @@ describe('Task Runner', () => { }, ], }, - "message": "test:1: 'alert-name' active instance: '1' in actionGroup: 'default'", + "message": "test:1: 'rule-name' active alert: '1' in actionGroup: 'default'", "rule": Object { "category": "test", "id": "1", "license": "basic", - "name": "alert-name", + "name": "rule-name", "ruleset": "alerts", }, }, @@ -3404,19 +3380,18 @@ describe('Task Runner', () => { }, ], }, - "message": "test:1: 'alert-name' active instance: '2' in actionGroup: 'default'", + "message": "test:1: 'rule-name' active alert: '2' in actionGroup: 'default'", "rule": Object { "category": "test", "id": "1", "license": "basic", - "name": "alert-name", + "name": "rule-name", "ruleset": "alerts", }, }, ], Array [ Object { - "@timestamp": "1970-01-01T00:00:00.000Z", "event": Object { "action": "execute", "category": Array [ @@ -3443,12 +3418,12 @@ describe('Task Runner', () => { "scheduled": "1970-01-01T00:00:00.000Z", }, }, - "message": "alert executed: test:1: 'alert-name'", + "message": "rule executed: test:1: 'rule-name'", "rule": Object { "category": "test", "id": "1", "license": "basic", - "name": "alert-name", + "name": "rule-name", "ruleset": "alerts", }, }, @@ -3460,7 +3435,7 @@ describe('Task Runner', () => { test('duration is updated for active alerts when alert state contains start time', async () => { taskRunnerFactoryInitializerParams.actionsPlugin.isActionTypeEnabled.mockReturnValue(true); taskRunnerFactoryInitializerParams.actionsPlugin.isActionExecutable.mockReturnValue(true); - alertType.executor.mockImplementation( + ruleType.executor.mockImplementation( async ({ services: executorServices, }: AlertExecutorOptions< @@ -3475,7 +3450,7 @@ describe('Task Runner', () => { } ); const taskRunner = new TaskRunner( - alertType, + ruleType, { ...mockedTaskInstance, state: { @@ -3503,7 +3478,7 @@ describe('Task Runner', () => { taskRunnerFactoryInitializerParams ); rulesClient.get.mockResolvedValue({ - ...mockedAlertTypeSavedObject, + ...mockedRuleTypeSavedObject, notifyWhen: 'onActionGroupChange', actions: [], }); @@ -3525,7 +3500,6 @@ describe('Task Runner', () => { Array [ Array [ Object { - "@timestamp": "1970-01-01T00:00:00.000Z", "event": Object { "action": "execute-start", "category": Array [ @@ -3548,7 +3522,7 @@ describe('Task Runner', () => { "scheduled": "1970-01-01T00:00:00.000Z", }, }, - "message": "alert execution start: \\"1\\"", + "message": "rule execution start: \\"1\\"", "rule": Object { "category": "test", "id": "1", @@ -3583,12 +3557,12 @@ describe('Task Runner', () => { }, ], }, - "message": "test:1: 'alert-name' active instance: '1' in actionGroup: 'default'", + "message": "test:1: 'rule-name' active alert: '1' in actionGroup: 'default'", "rule": Object { "category": "test", "id": "1", "license": "basic", - "name": "alert-name", + "name": "rule-name", "ruleset": "alerts", }, }, @@ -3619,19 +3593,18 @@ describe('Task Runner', () => { }, ], }, - "message": "test:1: 'alert-name' active instance: '2' in actionGroup: 'default'", + "message": "test:1: 'rule-name' active alert: '2' in actionGroup: 'default'", "rule": Object { "category": "test", "id": "1", "license": "basic", - "name": "alert-name", + "name": "rule-name", "ruleset": "alerts", }, }, ], Array [ Object { - "@timestamp": "1970-01-01T00:00:00.000Z", "event": Object { "action": "execute", "category": Array [ @@ -3658,12 +3631,12 @@ describe('Task Runner', () => { "scheduled": "1970-01-01T00:00:00.000Z", }, }, - "message": "alert executed: test:1: 'alert-name'", + "message": "rule executed: test:1: 'rule-name'", "rule": Object { "category": "test", "id": "1", "license": "basic", - "name": "alert-name", + "name": "rule-name", "ruleset": "alerts", }, }, @@ -3675,7 +3648,7 @@ describe('Task Runner', () => { test('duration is not calculated for active alerts when alert state does not contain start time', async () => { taskRunnerFactoryInitializerParams.actionsPlugin.isActionTypeEnabled.mockReturnValue(true); taskRunnerFactoryInitializerParams.actionsPlugin.isActionExecutable.mockReturnValue(true); - alertType.executor.mockImplementation( + ruleType.executor.mockImplementation( async ({ services: executorServices, }: AlertExecutorOptions< @@ -3690,7 +3663,7 @@ describe('Task Runner', () => { } ); const taskRunner = new TaskRunner( - alertType, + ruleType, { ...mockedTaskInstance, state: { @@ -3710,7 +3683,7 @@ describe('Task Runner', () => { taskRunnerFactoryInitializerParams ); rulesClient.get.mockResolvedValue({ - ...mockedAlertTypeSavedObject, + ...mockedRuleTypeSavedObject, notifyWhen: 'onActionGroupChange', actions: [], }); @@ -3732,7 +3705,6 @@ describe('Task Runner', () => { Array [ Array [ Object { - "@timestamp": "1970-01-01T00:00:00.000Z", "event": Object { "action": "execute-start", "category": Array [ @@ -3755,7 +3727,7 @@ describe('Task Runner', () => { "scheduled": "1970-01-01T00:00:00.000Z", }, }, - "message": "alert execution start: \\"1\\"", + "message": "rule execution start: \\"1\\"", "rule": Object { "category": "test", "id": "1", @@ -3788,12 +3760,12 @@ describe('Task Runner', () => { }, ], }, - "message": "test:1: 'alert-name' active instance: '1' in actionGroup: 'default'", + "message": "test:1: 'rule-name' active alert: '1' in actionGroup: 'default'", "rule": Object { "category": "test", "id": "1", "license": "basic", - "name": "alert-name", + "name": "rule-name", "ruleset": "alerts", }, }, @@ -3822,19 +3794,18 @@ describe('Task Runner', () => { }, ], }, - "message": "test:1: 'alert-name' active instance: '2' in actionGroup: 'default'", + "message": "test:1: 'rule-name' active alert: '2' in actionGroup: 'default'", "rule": Object { "category": "test", "id": "1", "license": "basic", - "name": "alert-name", + "name": "rule-name", "ruleset": "alerts", }, }, ], Array [ Object { - "@timestamp": "1970-01-01T00:00:00.000Z", "event": Object { "action": "execute", "category": Array [ @@ -3861,12 +3832,12 @@ describe('Task Runner', () => { "scheduled": "1970-01-01T00:00:00.000Z", }, }, - "message": "alert executed: test:1: 'alert-name'", + "message": "rule executed: test:1: 'rule-name'", "rule": Object { "category": "test", "id": "1", "license": "basic", - "name": "alert-name", + "name": "rule-name", "ruleset": "alerts", }, }, @@ -3878,9 +3849,9 @@ describe('Task Runner', () => { test('end is logged for active alerts when alert state contains start time and alert recovers', async () => { taskRunnerFactoryInitializerParams.actionsPlugin.isActionTypeEnabled.mockReturnValue(true); taskRunnerFactoryInitializerParams.actionsPlugin.isActionExecutable.mockReturnValue(true); - alertType.executor.mockImplementation(async () => {}); + ruleType.executor.mockImplementation(async () => {}); const taskRunner = new TaskRunner( - alertType, + ruleType, { ...mockedTaskInstance, state: { @@ -3908,7 +3879,7 @@ describe('Task Runner', () => { taskRunnerFactoryInitializerParams ); rulesClient.get.mockResolvedValue({ - ...mockedAlertTypeSavedObject, + ...mockedRuleTypeSavedObject, notifyWhen: 'onActionGroupChange', actions: [], }); @@ -3930,7 +3901,6 @@ describe('Task Runner', () => { Array [ Array [ Object { - "@timestamp": "1970-01-01T00:00:00.000Z", "event": Object { "action": "execute-start", "category": Array [ @@ -3953,7 +3923,7 @@ describe('Task Runner', () => { "scheduled": "1970-01-01T00:00:00.000Z", }, }, - "message": "alert execution start: \\"1\\"", + "message": "rule execution start: \\"1\\"", "rule": Object { "category": "test", "id": "1", @@ -3988,12 +3958,12 @@ describe('Task Runner', () => { }, ], }, - "message": "test:1: 'alert-name' instance '1' has recovered", + "message": "test:1: 'rule-name' alert '1' has recovered", "rule": Object { "category": "test", "id": "1", "license": "basic", - "name": "alert-name", + "name": "rule-name", "ruleset": "alerts", }, }, @@ -4024,19 +3994,18 @@ describe('Task Runner', () => { }, ], }, - "message": "test:1: 'alert-name' instance '2' has recovered", + "message": "test:1: 'rule-name' alert '2' has recovered", "rule": Object { "category": "test", "id": "1", "license": "basic", - "name": "alert-name", + "name": "rule-name", "ruleset": "alerts", }, }, ], Array [ Object { - "@timestamp": "1970-01-01T00:00:00.000Z", "event": Object { "action": "execute", "category": Array [ @@ -4063,12 +4032,12 @@ describe('Task Runner', () => { "scheduled": "1970-01-01T00:00:00.000Z", }, }, - "message": "alert executed: test:1: 'alert-name'", + "message": "rule executed: test:1: 'rule-name'", "rule": Object { "category": "test", "id": "1", "license": "basic", - "name": "alert-name", + "name": "rule-name", "ruleset": "alerts", }, }, @@ -4080,7 +4049,7 @@ describe('Task Runner', () => { test('end calculation is skipped for active alerts when alert state does not contain start time and alert recovers', async () => { taskRunnerFactoryInitializerParams.actionsPlugin.isActionTypeEnabled.mockReturnValue(true); taskRunnerFactoryInitializerParams.actionsPlugin.isActionExecutable.mockReturnValue(true); - alertType.executor.mockImplementation( + ruleType.executor.mockImplementation( async ({ services: executorServices, }: AlertExecutorOptions< @@ -4092,7 +4061,7 @@ describe('Task Runner', () => { >) => {} ); const taskRunner = new TaskRunner( - alertType, + ruleType, { ...mockedTaskInstance, state: { @@ -4112,7 +4081,7 @@ describe('Task Runner', () => { taskRunnerFactoryInitializerParams ); rulesClient.get.mockResolvedValue({ - ...mockedAlertTypeSavedObject, + ...mockedRuleTypeSavedObject, notifyWhen: 'onActionGroupChange', actions: [], }); @@ -4134,7 +4103,6 @@ describe('Task Runner', () => { Array [ Array [ Object { - "@timestamp": "1970-01-01T00:00:00.000Z", "event": Object { "action": "execute-start", "category": Array [ @@ -4157,7 +4125,7 @@ describe('Task Runner', () => { "scheduled": "1970-01-01T00:00:00.000Z", }, }, - "message": "alert execution start: \\"1\\"", + "message": "rule execution start: \\"1\\"", "rule": Object { "category": "test", "id": "1", @@ -4189,12 +4157,12 @@ describe('Task Runner', () => { }, ], }, - "message": "test:1: 'alert-name' instance '1' has recovered", + "message": "test:1: 'rule-name' alert '1' has recovered", "rule": Object { "category": "test", "id": "1", "license": "basic", - "name": "alert-name", + "name": "rule-name", "ruleset": "alerts", }, }, @@ -4222,19 +4190,18 @@ describe('Task Runner', () => { }, ], }, - "message": "test:1: 'alert-name' instance '2' has recovered", + "message": "test:1: 'rule-name' alert '2' has recovered", "rule": Object { "category": "test", "id": "1", "license": "basic", - "name": "alert-name", + "name": "rule-name", "ruleset": "alerts", }, }, ], Array [ Object { - "@timestamp": "1970-01-01T00:00:00.000Z", "event": Object { "action": "execute", "category": Array [ @@ -4261,12 +4228,12 @@ describe('Task Runner', () => { "scheduled": "1970-01-01T00:00:00.000Z", }, }, - "message": "alert executed: test:1: 'alert-name'", + "message": "rule executed: test:1: 'rule-name'", "rule": Object { "category": "test", "id": "1", "license": "basic", - "name": "alert-name", + "name": "rule-name", "ruleset": "alerts", }, }, @@ -4277,7 +4244,7 @@ describe('Task Runner', () => { test('successfully executes the task with ephemeral tasks enabled', async () => { const taskRunner = new TaskRunner( - alertType, + ruleType, { ...mockedTaskInstance, state: { @@ -4290,7 +4257,7 @@ describe('Task Runner', () => { supportsEphemeralTasks: true, } ); - rulesClient.get.mockResolvedValue(mockedAlertTypeSavedObject); + rulesClient.get.mockResolvedValue(mockedRuleTypeSavedObject); encryptedSavedObjectsClient.getDecryptedAsInternalUser.mockResolvedValue({ id: '1', type: 'alert', @@ -4313,8 +4280,8 @@ describe('Task Runner', () => { }, } `); - expect(alertType.executor).toHaveBeenCalledTimes(1); - const call = alertType.executor.mock.calls[0][0]; + expect(ruleType.executor).toHaveBeenCalledTimes(1); + const call = ruleType.executor.mock.calls[0][0]; expect(call.params).toMatchInlineSnapshot(` Object { "bar": true, @@ -4323,13 +4290,13 @@ describe('Task Runner', () => { expect(call.startedAt).toMatchInlineSnapshot(`1970-01-01T00:00:00.000Z`); expect(call.previousStartedAt).toMatchInlineSnapshot(`1969-12-31T23:55:00.000Z`); expect(call.state).toMatchInlineSnapshot(`Object {}`); - expect(call.name).toBe('alert-name'); - expect(call.tags).toEqual(['alert-', '-tags']); - expect(call.createdBy).toBe('alert-creator'); - expect(call.updatedBy).toBe('alert-updater'); + expect(call.name).toBe('rule-name'); + expect(call.tags).toEqual(['rule-', '-tags']); + expect(call.createdBy).toBe('rule-creator'); + expect(call.updatedBy).toBe('rule-updater'); expect(call.rule).not.toBe(null); - expect(call.rule.name).toBe('alert-name'); - expect(call.rule.tags).toEqual(['alert-', '-tags']); + expect(call.rule.name).toBe('rule-name'); + expect(call.rule.tags).toEqual(['rule-', '-tags']); expect(call.rule.consumer).toBe('bar'); expect(call.rule.enabled).toBe(true); expect(call.rule.schedule).toMatchInlineSnapshot(` @@ -4337,15 +4304,15 @@ describe('Task Runner', () => { "interval": "10s", } `); - expect(call.rule.createdBy).toBe('alert-creator'); - expect(call.rule.updatedBy).toBe('alert-updater'); + expect(call.rule.createdBy).toBe('rule-creator'); + expect(call.rule.updatedBy).toBe('rule-updater'); expect(call.rule.createdAt).toBe(mockDate); expect(call.rule.updatedAt).toBe(mockDate); expect(call.rule.notifyWhen).toBe('onActiveAlert'); expect(call.rule.throttle).toBe(null); expect(call.rule.producer).toBe('alerts'); expect(call.rule.ruleTypeId).toBe('test'); - expect(call.rule.ruleTypeName).toBe('My test alert'); + expect(call.rule.ruleTypeName).toBe('My test rule'); expect(call.rule.actions).toMatchInlineSnapshot(` Array [ Object { @@ -4372,10 +4339,10 @@ describe('Task Runner', () => { const logger = taskRunnerFactoryInitializerParams.logger; expect(logger.debug).toHaveBeenCalledTimes(3); - expect(logger.debug).nthCalledWith(1, 'executing alert test:1 at 1970-01-01T00:00:00.000Z'); + expect(logger.debug).nthCalledWith(1, 'executing rule test:1 at 1970-01-01T00:00:00.000Z'); expect(logger.debug).nthCalledWith( 2, - 'alertExecutionStatus for test:1: {"lastExecutionDate":"1970-01-01T00:00:00.000Z","status":"ok"}' + 'ruleExecutionStatus for test:1: {"lastExecutionDate":"1970-01-01T00:00:00.000Z","status":"ok"}' ); const eventLogger = taskRunnerFactoryInitializerParams.eventLogger; @@ -4383,7 +4350,6 @@ describe('Task Runner', () => { expect(eventLogger.startTiming).toHaveBeenCalledTimes(1); expect(eventLogger.logEvent.mock.calls[0][0]).toMatchInlineSnapshot(` Object { - "@timestamp": "1970-01-01T00:00:00.000Z", "event": Object { "action": "execute-start", "category": Array [ @@ -4406,7 +4372,7 @@ describe('Task Runner', () => { "scheduled": "1970-01-01T00:00:00.000Z", }, }, - "message": "alert execution start: \\"1\\"", + "message": "rule execution start: \\"1\\"", "rule": Object { "category": "test", "id": "1", @@ -4439,14 +4405,14 @@ describe('Task Runner', () => { previousStartedAt: new Date(Date.now() - 5 * 60 * 1000).toISOString(), }; const taskRunner = new TaskRunner( - alertType, + ruleType, { ...mockedTaskInstance, state, }, taskRunnerFactoryInitializerParams ); - rulesClient.get.mockResolvedValue(mockedAlertTypeSavedObject); + rulesClient.get.mockResolvedValue(mockedRuleTypeSavedObject); encryptedSavedObjectsClient.getDecryptedAsInternalUser.mockResolvedValue({ id: '1', type: 'alert', @@ -4463,7 +4429,6 @@ describe('Task Runner', () => { const eventLogger = taskRunnerFactoryInitializerParams.eventLogger; expect(eventLogger.logEvent).toHaveBeenCalledTimes(2); expect(eventLogger.logEvent.mock.calls[0][0]).toStrictEqual({ - '@timestamp': '1970-01-01T00:00:00.000Z', event: { action: 'execute-start', kind: 'alert', @@ -4481,10 +4446,9 @@ describe('Task Runner', () => { category: 'test', ruleset: 'alerts', }, - message: 'alert execution start: "1"', + message: 'rule execution start: "1"', }); expect(eventLogger.logEvent.mock.calls[1][0]).toStrictEqual({ - '@timestamp': '1970-01-01T00:00:00.000Z', event: { action: 'execute', kind: 'alert', diff --git a/x-pack/plugins/alerting/server/task_runner/task_runner.ts b/x-pack/plugins/alerting/server/task_runner/task_runner.ts index 0cf5202787392..91c9683b948a0 100644 --- a/x-pack/plugins/alerting/server/task_runner/task_runner.ts +++ b/x-pack/plugins/alerting/server/task_runner/task_runner.ts @@ -15,19 +15,19 @@ import { ConcreteTaskInstance, throwUnrecoverableError } from '../../../task_man import { createExecutionHandler, ExecutionHandler } from './create_execution_handler'; import { AlertInstance, createAlertInstanceFactory } from '../alert_instance'; import { - validateAlertTypeParams, + validateRuleTypeParams, executionStatusFromState, executionStatusFromError, - alertExecutionStatusToRaw, + ruleExecutionStatusToRaw, ErrorWithReason, ElasticsearchError, } from '../lib'; import { - RawAlert, + RawRule, IntervalSchedule, Services, RawAlertInstance, - AlertTaskState, + RuleTaskState, Alert, SanitizedAlert, AlertExecutionStatus, @@ -49,7 +49,7 @@ import { AlertInstanceContext, WithoutReservedActionGroups, } from '../../common'; -import { NormalizedAlertType, UntypedNormalizedAlertType } from '../rule_type_registry'; +import { NormalizedRuleType, UntypedNormalizedRuleType } from '../rule_type_registry'; import { getEsErrorMessage } from '../lib/errors'; import { createAlertEventLogRecordObject, @@ -61,13 +61,13 @@ const FALLBACK_RETRY_INTERVAL = '5m'; // 1,000,000 nanoseconds in 1 millisecond const Millis2Nanos = 1000 * 1000; -interface AlertTaskRunResult { - state: AlertTaskState; +interface RuleTaskRunResult { + state: RuleTaskState; schedule: IntervalSchedule | undefined; } -interface AlertTaskInstance extends ConcreteTaskInstance { - state: AlertTaskState; +interface RuleTaskInstance extends ConcreteTaskInstance { + state: RuleTaskState; } export class TaskRunner< @@ -81,9 +81,9 @@ export class TaskRunner< > { private context: TaskRunnerContext; private logger: Logger; - private taskInstance: AlertTaskInstance; + private taskInstance: RuleTaskInstance; private ruleName: string | null; - private alertType: NormalizedAlertType< + private ruleType: NormalizedRuleType< Params, ExtractedParams, State, @@ -96,7 +96,7 @@ export class TaskRunner< private cancelled: boolean; constructor( - alertType: NormalizedAlertType< + ruleType: NormalizedRuleType< Params, ExtractedParams, State, @@ -110,7 +110,7 @@ export class TaskRunner< ) { this.context = context; this.logger = context.logger; - this.alertType = alertType; + this.ruleType = ruleType; this.ruleName = null; this.taskInstance = taskInstanceToAlertTaskInstance(taskInstance); this.ruleTypeRegistry = context.ruleTypeRegistry; @@ -126,7 +126,7 @@ export class TaskRunner< // scoped with the API key to fetch the remaining data. const { attributes: { apiKey, enabled }, - } = await this.context.encryptedSavedObjectsClient.getDecryptedAsInternalUser( + } = await this.context.encryptedSavedObjectsClient.getDecryptedAsInternalUser( 'alert', ruleId, { namespace } @@ -135,7 +135,7 @@ export class TaskRunner< return { apiKey, enabled }; } - private getFakeKibanaRequest(spaceId: string, apiKey: RawAlert['apiKey']) { + private getFakeKibanaRequest(spaceId: string, apiKey: RawRule['apiKey']) { const requestHeaders: Record = {}; if (apiKey) { @@ -165,21 +165,21 @@ export class TaskRunner< private getServicesWithSpaceLevelPermissions( spaceId: string, - apiKey: RawAlert['apiKey'] + apiKey: RawRule['apiKey'] ): [Services, PublicMethodsOf] { const request = this.getFakeKibanaRequest(spaceId, apiKey); return [this.context.getServices(request), this.context.getRulesClientWithRequest(request)]; } private getExecutionHandler( - alertId: string, - alertName: string, + ruleId: string, + ruleName: string, tags: string[] | undefined, spaceId: string, - apiKey: RawAlert['apiKey'], + apiKey: RawRule['apiKey'], kibanaBaseUrl: string | undefined, actions: Alert['actions'], - alertParams: Params + ruleParams: Params ) { return createExecutionHandler< Params, @@ -190,43 +190,43 @@ export class TaskRunner< ActionGroupIds, RecoveryActionGroupId >({ - alertId, - alertName, + ruleId, + ruleName, tags, logger: this.logger, actionsPlugin: this.context.actionsPlugin, apiKey, actions, spaceId, - alertType: this.alertType, + ruleType: this.ruleType, kibanaBaseUrl, eventLogger: this.context.eventLogger, request: this.getFakeKibanaRequest(spaceId, apiKey), - alertParams, + ruleParams, supportsEphemeralTasks: this.context.supportsEphemeralTasks, - maxEphemeralActionsPerAlert: this.context.maxEphemeralActionsPerAlert, + maxEphemeralActionsPerRule: this.context.maxEphemeralActionsPerRule, }); } private async updateRuleExecutionStatus( - alertId: string, + ruleId: string, namespace: string | undefined, executionStatus: AlertExecutionStatus ) { const client = this.context.internalSavedObjectsRepository; const attributes = { - executionStatus: alertExecutionStatusToRaw(executionStatus), + executionStatus: ruleExecutionStatusToRaw(executionStatus), }; try { - await partiallyUpdateAlert(client, alertId, attributes, { + await partiallyUpdateAlert(client, ruleId, attributes, { ignore404: true, namespace, refresh: false, }); } catch (err) { this.logger.error( - `error updating rule execution status for ${this.alertType.id}:${alertId} ${err.message}` + `error updating rule execution status for ${this.ruleType.id}:${ruleId} ${err.message}` ); } } @@ -238,12 +238,12 @@ export class TaskRunner< } // if execution has been cancelled, return true if EITHER alerting config or rule type indicate to proceed with scheduling actions - return !this.context.cancelAlertsOnRuleTimeout || !this.alertType.cancelAlertsOnRuleTimeout; + return !this.context.cancelAlertsOnRuleTimeout || !this.ruleType.cancelAlertsOnRuleTimeout; } - async executeAlertInstance( - alertInstanceId: string, - alertInstance: AlertInstance, + async executeAlert( + alertId: string, + alert: AlertInstance, executionHandler: ExecutionHandler ) { const { @@ -251,20 +251,20 @@ export class TaskRunner< subgroup: actionSubgroup, context, state, - } = alertInstance.getScheduledActionOptions()!; - alertInstance.updateLastScheduledActions(actionGroup, actionSubgroup); - alertInstance.unscheduleActions(); - return executionHandler({ actionGroup, actionSubgroup, context, state, alertInstanceId }); + } = alert.getScheduledActionOptions()!; + alert.updateLastScheduledActions(actionGroup, actionSubgroup); + alert.unscheduleActions(); + return executionHandler({ actionGroup, actionSubgroup, context, state, alertId }); } - async executeAlertInstances( + async executeAlerts( services: Services, - alert: SanitizedAlert, + rule: SanitizedAlert, params: Params, executionHandler: ExecutionHandler, spaceId: string, event: Event - ): Promise { + ): Promise { const { alertTypeId, consumer, @@ -281,48 +281,45 @@ export class TaskRunner< updatedAt, enabled, actions, - } = alert; + } = rule; const { - params: { alertId }, + params: { alertId: ruleId }, state: { alertInstances: alertRawInstances = {}, alertTypeState = {}, previousStartedAt }, } = this.taskInstance; const namespace = this.context.spaceIdToNamespace(spaceId); - const alertType = this.ruleTypeRegistry.get(alertTypeId); + const ruleType = this.ruleTypeRegistry.get(alertTypeId); - const alertInstances = mapValues< + const alerts = mapValues< Record, AlertInstance - >( - alertRawInstances, - (rawAlertInstance) => new AlertInstance(rawAlertInstance) - ); - const originalAlertInstances = cloneDeep(alertInstances); - const originalAlertInstanceIds = new Set(Object.keys(originalAlertInstances)); + >(alertRawInstances, (rawAlert) => new AlertInstance(rawAlert)); + const originalAlerts = cloneDeep(alerts); + const originalAlertIds = new Set(Object.keys(originalAlerts)); const eventLogger = this.context.eventLogger; - const alertLabel = `${this.alertType.id}:${alertId}: '${name}'`; + const ruleLabel = `${this.ruleType.id}:${ruleId}: '${name}'`; - let updatedAlertTypeState: void | Record; + let updatedRuleTypeState: void | Record; try { const ctx = { type: 'alert', - name: `execute ${alert.alertTypeId}`, - id: alertId, - description: `execute [${alert.alertTypeId}] with name [${name}] in [${ + name: `execute ${rule.alertTypeId}`, + id: ruleId, + description: `execute [${rule.alertTypeId}] with name [${name}] in [${ namespace ?? 'default' }] namespace`, }; - updatedAlertTypeState = await this.context.executionContext.withContext(ctx, () => - this.alertType.executor({ - alertId, + updatedRuleTypeState = await this.context.executionContext.withContext(ctx, () => + this.ruleType.executor({ + alertId: ruleId, services: { ...services, alertInstanceFactory: createAlertInstanceFactory< InstanceState, InstanceContext, WithoutReservedActionGroups - >(alertInstances), + >(alerts), shouldWriteAlerts: () => this.shouldLogAndScheduleActionsForAlerts(), }, params, @@ -339,9 +336,9 @@ export class TaskRunner< name, tags, consumer, - producer: alertType.producer, - ruleTypeId: alert.alertTypeId, - ruleTypeName: alertType.name, + producer: ruleType.producer, + ruleTypeId: rule.alertTypeId, + ruleTypeName: ruleType.name, enabled, schedule, actions, @@ -355,7 +352,7 @@ export class TaskRunner< }) ); } catch (err) { - event.message = `alert execution failure: ${alertLabel}`; + event.message = `rule execution failure: ${ruleLabel}`; event.error = event.error || {}; event.error.message = err.message; event.event = event.event || {}; @@ -363,93 +360,85 @@ export class TaskRunner< throw new ErrorWithReason(AlertExecutionStatusErrorReasons.Execute, err); } - event.message = `alert executed: ${alertLabel}`; + event.message = `rule executed: ${ruleLabel}`; event.event = event.event || {}; event.event.outcome = 'success'; event.rule = { ...event.rule, - name: alert.name, + name: rule.name, }; - // Cleanup alert instances that are no longer scheduling actions to avoid over populating the alertInstances object - const instancesWithScheduledActions = pickBy( - alertInstances, - (alertInstance: AlertInstance) => - alertInstance.hasScheduledActions() + // Cleanup alerts that are no longer scheduling actions to avoid over populating the alertInstances object + const alertsWithScheduledActions = pickBy( + alerts, + (alert: AlertInstance) => alert.hasScheduledActions() ); - const recoveredAlertInstances = pickBy( - alertInstances, - (alertInstance: AlertInstance, id) => - !alertInstance.hasScheduledActions() && originalAlertInstanceIds.has(id) + const recoveredAlerts = pickBy( + alerts, + (alert: AlertInstance, id) => + !alert.hasScheduledActions() && originalAlertIds.has(id) ); - logActiveAndRecoveredInstances({ + logActiveAndRecoveredAlerts({ logger: this.logger, - activeAlertInstances: instancesWithScheduledActions, - recoveredAlertInstances, - alertLabel, + activeAlerts: alertsWithScheduledActions, + recoveredAlerts, + ruleLabel, }); trackAlertDurations({ - originalAlerts: originalAlertInstances, - currentAlerts: instancesWithScheduledActions, - recoveredAlerts: recoveredAlertInstances, + originalAlerts, + currentAlerts: alertsWithScheduledActions, + recoveredAlerts, }); if (this.shouldLogAndScheduleActionsForAlerts()) { - generateNewAndRecoveredInstanceEvents({ + generateNewAndRecoveredAlertEvents({ eventLogger, - originalAlertInstances, - currentAlertInstances: instancesWithScheduledActions, - recoveredAlertInstances, - alertId, - alertLabel, + originalAlerts, + currentAlerts: alertsWithScheduledActions, + recoveredAlerts, + ruleId, + ruleLabel, namespace, - ruleType: alertType, - rule: alert, + ruleType, + rule, }); } if (!muteAll && this.shouldLogAndScheduleActionsForAlerts()) { - const mutedInstanceIdsSet = new Set(mutedInstanceIds); + const mutedAlertIdsSet = new Set(mutedInstanceIds); - scheduleActionsForRecoveredInstances({ - recoveryActionGroup: this.alertType.recoveryActionGroup, - recoveredAlertInstances, + scheduleActionsForRecoveredAlerts({ + recoveryActionGroup: this.ruleType.recoveryActionGroup, + recoveredAlerts, executionHandler, - mutedInstanceIdsSet, + mutedAlertIdsSet, logger: this.logger, - alertLabel, + ruleLabel, }); - const instancesToExecute = + const alertsToExecute = notifyWhen === 'onActionGroupChange' - ? Object.entries(instancesWithScheduledActions).filter( - ([alertInstanceName, alertInstance]: [ - string, - AlertInstance - ]) => { - const shouldExecuteAction = - alertInstance.scheduledActionGroupOrSubgroupHasChanged(); + ? Object.entries(alertsWithScheduledActions).filter( + ([alertName, alert]: [string, AlertInstance]) => { + const shouldExecuteAction = alert.scheduledActionGroupOrSubgroupHasChanged(); if (!shouldExecuteAction) { this.logger.debug( - `skipping scheduling of actions for '${alertInstanceName}' in alert ${alertLabel}: instance is active but action group has not changed` + `skipping scheduling of actions for '${alertName}' in rule ${ruleLabel}: alert is active but action group has not changed` ); } return shouldExecuteAction; } ) - : Object.entries(instancesWithScheduledActions).filter( - ([alertInstanceName, alertInstance]: [ - string, - AlertInstance - ]) => { - const throttled = alertInstance.isThrottled(throttle); - const muted = mutedInstanceIdsSet.has(alertInstanceName); + : Object.entries(alertsWithScheduledActions).filter( + ([alertName, alert]: [string, AlertInstance]) => { + const throttled = alert.isThrottled(throttle); + const muted = mutedAlertIdsSet.has(alertName); const shouldExecuteAction = !throttled && !muted; if (!shouldExecuteAction) { this.logger.debug( - `skipping scheduling of actions for '${alertInstanceName}' in alert ${alertLabel}: instance is ${ + `skipping scheduling of actions for '${alertName}' in rule ${ruleLabel}: rule is ${ muted ? 'muted' : 'throttled' }` ); @@ -459,71 +448,64 @@ export class TaskRunner< ); await Promise.all( - instancesToExecute.map( - ([id, alertInstance]: [string, AlertInstance]) => - this.executeAlertInstance(id, alertInstance, executionHandler) + alertsToExecute.map( + ([alertId, alert]: [string, AlertInstance]) => + this.executeAlert(alertId, alert, executionHandler) ) ); } else { if (muteAll) { - this.logger.debug(`no scheduling of actions for alert ${alertLabel}: alert is muted.`); + this.logger.debug(`no scheduling of actions for rule ${ruleLabel}: rule is muted.`); } if (!this.shouldLogAndScheduleActionsForAlerts()) { this.logger.debug( - `no scheduling of actions for alert ${alertLabel}: alert execution has been cancelled.` + `no scheduling of actions for rule ${ruleLabel}: rule execution has been cancelled.` ); } } return { - alertTypeState: updatedAlertTypeState || undefined, + alertTypeState: updatedRuleTypeState || undefined, alertInstances: mapValues< Record>, RawAlertInstance - >(instancesWithScheduledActions, (alertInstance) => alertInstance.toRaw()), + >(alertsWithScheduledActions, (alert) => alert.toRaw()), }; } - async validateAndExecuteAlert( + async validateAndExecuteRule( services: Services, - apiKey: RawAlert['apiKey'], - alert: SanitizedAlert, + apiKey: RawRule['apiKey'], + rule: SanitizedAlert, event: Event ) { const { - params: { alertId, spaceId }, + params: { alertId: ruleId, spaceId }, } = this.taskInstance; // Validate - const validatedParams = validateAlertTypeParams(alert.params, this.alertType.validate?.params); + const validatedParams = validateRuleTypeParams(rule.params, this.ruleType.validate?.params); const executionHandler = this.getExecutionHandler( - alertId, - alert.name, - alert.tags, + ruleId, + rule.name, + rule.tags, spaceId, apiKey, this.context.kibanaBaseUrl, - alert.actions, - alert.params - ); - return this.executeAlertInstances( - services, - alert, - validatedParams, - executionHandler, - spaceId, - event + rule.actions, + rule.params ); + return this.executeAlerts(services, rule, validatedParams, executionHandler, spaceId, event); } - async loadAlertAttributesAndRun(event: Event): Promise> { + async loadRuleAttributesAndRun(event: Event): Promise> { const { - params: { alertId, spaceId }, + params: { alertId: ruleId, spaceId }, } = this.taskInstance; let enabled: boolean; let apiKey: string | null; try { - const decryptedAttributes = await this.getDecryptedAttributes(alertId, spaceId); + const decryptedAttributes = await this.getDecryptedAttributes(ruleId, spaceId); apiKey = decryptedAttributes.apiKey; enabled = decryptedAttributes.enabled; } catch (err) { @@ -539,48 +521,48 @@ export class TaskRunner< const [services, rulesClient] = this.getServicesWithSpaceLevelPermissions(spaceId, apiKey); - let alert: SanitizedAlert; + let rule: SanitizedAlert; // Ensure API key is still valid and user has access try { - alert = await rulesClient.get({ id: alertId }); + rule = await rulesClient.get({ id: ruleId }); if (apm.currentTransaction) { - apm.currentTransaction.name = `Execute Alerting Rule: "${alert.name}"`; + apm.currentTransaction.name = `Execute Alerting Rule: "${rule.name}"`; apm.currentTransaction.addLabels({ - alerting_rule_consumer: alert.consumer, - alerting_rule_name: alert.name, - alerting_rule_tags: alert.tags.join(', '), - alerting_rule_type_id: alert.alertTypeId, - alerting_rule_params: JSON.stringify(alert.params), + alerting_rule_consumer: rule.consumer, + alerting_rule_name: rule.name, + alerting_rule_tags: rule.tags.join(', '), + alerting_rule_type_id: rule.alertTypeId, + alerting_rule_params: JSON.stringify(rule.params), }); } } catch (err) { throw new ErrorWithReason(AlertExecutionStatusErrorReasons.Read, err); } - this.ruleName = alert.name; + this.ruleName = rule.name; try { - this.ruleTypeRegistry.ensureRuleTypeEnabled(alert.alertTypeId); + this.ruleTypeRegistry.ensureRuleTypeEnabled(rule.alertTypeId); } catch (err) { throw new ErrorWithReason(AlertExecutionStatusErrorReasons.License, err); } return { - state: await promiseResult( - this.validateAndExecuteAlert(services, apiKey, alert, event) + state: await promiseResult( + this.validateAndExecuteRule(services, apiKey, rule, event) ), schedule: asOk( - // fetch the alert again to ensure we return the correct schedule as it may have + // fetch the rule again to ensure we return the correct schedule as it may have // cahnged during the task execution - (await rulesClient.get({ id: alertId })).schedule + (await rulesClient.get({ id: ruleId })).schedule ), }; } - async run(): Promise { + async run(): Promise { const { - params: { alertId, spaceId }, + params: { alertId: ruleId, spaceId }, startedAt, state: originalState, schedule: taskSchedule, @@ -589,22 +571,21 @@ export class TaskRunner< if (apm.currentTransaction) { apm.currentTransaction.name = `Execute Alerting Rule`; apm.currentTransaction.addLabels({ - alerting_rule_id: alertId, + alerting_rule_id: ruleId, }); } const runDate = new Date(); const runDateString = runDate.toISOString(); - this.logger.debug(`executing alert ${this.alertType.id}:${alertId} at ${runDateString}`); + this.logger.debug(`executing rule ${this.ruleType.id}:${ruleId} at ${runDateString}`); const namespace = this.context.spaceIdToNamespace(spaceId); const eventLogger = this.context.eventLogger; const scheduleDelay = runDate.getTime() - this.taskInstance.runAt.getTime(); const event = createAlertEventLogRecordObject({ - timestamp: runDateString, - ruleId: alertId, - ruleType: this.alertType as UntypedNormalizedAlertType, + ruleId, + ruleType: this.ruleType as UntypedNormalizedRuleType, action: EVENT_LOG_ACTIONS.execute, namespace, task: { @@ -613,9 +594,9 @@ export class TaskRunner< }, savedObjects: [ { - id: alertId, + id: ruleId, type: 'alert', - typeId: this.alertType.id, + typeId: this.ruleType.id, relation: SAVED_OBJECT_REL_PRIMARY, }, ], @@ -629,17 +610,17 @@ export class TaskRunner< ...event.event, action: EVENT_LOG_ACTIONS.executeStart, }, - message: `alert execution start: "${alertId}"`, + message: `rule execution start: "${ruleId}"`, }); eventLogger.logEvent(startEvent); - const { state, schedule } = await errorAsAlertTaskRunResult( - this.loadAlertAttributesAndRun(event) + const { state, schedule } = await errorAsRuleTaskRunResult( + this.loadRuleAttributesAndRun(event) ); const executionStatus: AlertExecutionStatus = map( state, - (alertTaskState: AlertTaskState) => executionStatusFromState(alertTaskState), + (ruleTaskState: RuleTaskState) => executionStatusFromState(ruleTaskState), (err: ElasticsearchError) => executionStatusFromError(err) ); @@ -657,7 +638,7 @@ export class TaskRunner< } this.logger.debug( - `alertExecutionStatus for ${this.alertType.id}:${alertId}: ${JSON.stringify(executionStatus)}` + `ruleExecutionStatus for ${this.ruleType.id}:${ruleId}: ${JSON.stringify(executionStatus)}` ); eventLogger.stopTiming(event); @@ -679,7 +660,7 @@ export class TaskRunner< event.error = event.error || {}; event.error.message = event.error.message || executionStatus.error.message; if (!event.message) { - event.message = `${this.alertType.id}:${alertId}: execution failed`; + event.message = `${this.ruleType.id}:${ruleId}: execution failed`; } } @@ -687,27 +668,27 @@ export class TaskRunner< if (!this.cancelled) { this.logger.debug( - `Updating rule task for ${this.alertType.id} rule with id ${alertId} - ${JSON.stringify( + `Updating rule task for ${this.ruleType.id} rule with id ${ruleId} - ${JSON.stringify( executionStatus )}` ); - await this.updateRuleExecutionStatus(alertId, namespace, executionStatus); + await this.updateRuleExecutionStatus(ruleId, namespace, executionStatus); } return { - state: map( + state: map( state, - (stateUpdates: AlertTaskState) => { + (stateUpdates: RuleTaskState) => { return { ...stateUpdates, previousStartedAt: startedAt, }; }, (err: ElasticsearchError) => { - const message = `Executing Alert ${spaceId}:${ - this.alertType.id - }:${alertId} has resulted in Error: ${getEsErrorMessage(err)}`; - if (isAlertSavedObjectNotFoundError(err, alertId)) { + const message = `Executing Rule ${spaceId}:${ + this.ruleType.id + }:${ruleId} has resulted in Error: ${getEsErrorMessage(err)}`; + if (isAlertSavedObjectNotFoundError(err, ruleId)) { this.logger.debug(message); } else { this.logger.error(message); @@ -716,10 +697,10 @@ export class TaskRunner< } ), schedule: resolveErr(schedule, (error) => { - if (isAlertSavedObjectNotFoundError(error, alertId)) { + if (isAlertSavedObjectNotFoundError(error, ruleId)) { const spaceMessage = spaceId ? `in the "${spaceId}" space ` : ''; this.logger.warn( - `Unable to execute rule "${alertId}" ${spaceMessage}because ${error.message} - this rule will not be rescheduled. To restart rule execution, try disabling and re-enabling this rule.` + `Unable to execute rule "${ruleId}" ${spaceMessage}because ${error.message} - this rule will not be rescheduled. To restart rule execution, try disabling and re-enabling this rule.` ); throwUnrecoverableError(error); } @@ -737,43 +718,42 @@ export class TaskRunner< // Write event log entry const { - params: { alertId, spaceId }, + params: { alertId: ruleId, spaceId }, } = this.taskInstance; const namespace = this.context.spaceIdToNamespace(spaceId); this.logger.debug( - `Cancelling rule type ${this.alertType.id} with id ${alertId} - execution exceeded rule type timeout of ${this.alertType.ruleTaskTimeout}` + `Cancelling rule type ${this.ruleType.id} with id ${ruleId} - execution exceeded rule type timeout of ${this.ruleType.ruleTaskTimeout}` ); const eventLogger = this.context.eventLogger; const event: IEvent = { - '@timestamp': new Date().toISOString(), event: { action: EVENT_LOG_ACTIONS.executeTimeout, kind: 'alert', - category: [this.alertType.producer], + category: [this.ruleType.producer], }, - message: `rule: ${this.alertType.id}:${alertId}: '${ + message: `rule: ${this.ruleType.id}:${ruleId}: '${ this.ruleName ?? '' }' execution cancelled due to timeout - exceeded rule type timeout of ${ - this.alertType.ruleTaskTimeout + this.ruleType.ruleTaskTimeout }`, kibana: { saved_objects: [ { rel: SAVED_OBJECT_REL_PRIMARY, type: 'alert', - id: alertId, - type_id: this.alertType.id, + id: ruleId, + type_id: this.ruleType.id, namespace, }, ], }, rule: { - id: alertId, - license: this.alertType.minimumLicenseRequired, - category: this.alertType.id, - ruleset: this.alertType.producer, + id: ruleId, + license: this.ruleType.minimumLicenseRequired, + category: this.ruleType.id, + ruleset: this.ruleType.producer, ...(this.ruleName ? { name: this.ruleName } : {}), }, }; @@ -785,13 +765,13 @@ export class TaskRunner< status: 'error', error: { reason: AlertExecutionStatusErrorReasons.Timeout, - message: `${this.alertType.id}:${alertId}: execution cancelled due to timeout - exceeded rule type timeout of ${this.alertType.ruleTaskTimeout}`, + message: `${this.ruleType.id}:${ruleId}: execution cancelled due to timeout - exceeded rule type timeout of ${this.ruleType.ruleTaskTimeout}`, }, }; this.logger.debug( - `Updating rule task for ${this.alertType.id} rule with id ${alertId} - execution error due to timeout` + `Updating rule task for ${this.ruleType.id} rule with id ${ruleId} - execution error due to timeout` ); - await this.updateRuleExecutionStatus(alertId, namespace, executionStatus); + await this.updateRuleExecutionStatus(ruleId, namespace, executionStatus); } } @@ -815,13 +795,13 @@ function trackAlertDurations< const recoveredAlertIds = Object.keys(recoveredAlerts); const newAlertIds = without(currentAlertIds, ...originalAlertIds); - // Inject start time into instance state of new instances + // Inject start time into alert state of new alerts for (const id of newAlertIds) { const state = currentAlerts[id].getState(); currentAlerts[id].replaceState({ ...state, start: currentTime }); } - // Calculate duration to date for active instances + // Calculate duration to date for active alerts for (const id of currentAlertIds) { const state = originalAlertIds.includes(id) ? originalAlerts[id].getState() @@ -836,7 +816,7 @@ function trackAlertDurations< }); } - // Inject end time into instance state of recovered instances + // Inject end time into alert state of recovered alerts for (const id of recoveredAlertIds) { const state = recoveredAlerts[id].getState(); const duration = state.start @@ -850,18 +830,18 @@ function trackAlertDurations< } } -interface GenerateNewAndRecoveredInstanceEventsParams< +interface GenerateNewAndRecoveredAlertEventsParams< InstanceState extends AlertInstanceState, InstanceContext extends AlertInstanceContext > { eventLogger: IEventLogger; - originalAlertInstances: Dictionary>; - currentAlertInstances: Dictionary>; - recoveredAlertInstances: Dictionary>; - alertId: string; - alertLabel: string; + originalAlerts: Dictionary>; + currentAlerts: Dictionary>; + recoveredAlerts: Dictionary>; + ruleId: string; + ruleLabel: string; namespace: string | undefined; - ruleType: NormalizedAlertType< + ruleType: NormalizedRuleType< AlertTypeParams, AlertTypeParams, AlertTypeState, @@ -877,24 +857,24 @@ interface GenerateNewAndRecoveredInstanceEventsParams< rule: SanitizedAlert; } -function generateNewAndRecoveredInstanceEvents< +function generateNewAndRecoveredAlertEvents< InstanceState extends AlertInstanceState, InstanceContext extends AlertInstanceContext ->(params: GenerateNewAndRecoveredInstanceEventsParams) { +>(params: GenerateNewAndRecoveredAlertEventsParams) { const { eventLogger, - alertId, + ruleId, namespace, - currentAlertInstances, - originalAlertInstances, - recoveredAlertInstances, + currentAlerts, + originalAlerts, + recoveredAlerts, rule, ruleType, } = params; - const originalAlertInstanceIds = Object.keys(originalAlertInstances); - const currentAlertInstanceIds = Object.keys(currentAlertInstances); - const recoveredAlertInstanceIds = Object.keys(recoveredAlertInstances); - const newIds = without(currentAlertInstanceIds, ...originalAlertInstanceIds); + const originalAlertIds = Object.keys(originalAlerts); + const currentAlertIds = Object.keys(currentAlerts); + const recoveredAlertIds = Object.keys(recoveredAlerts); + const newIds = without(currentAlertIds, ...originalAlertIds); if (apm.currentTransaction) { apm.currentTransaction.addLabels({ @@ -902,12 +882,12 @@ function generateNewAndRecoveredInstanceEvents< }); } - for (const id of recoveredAlertInstanceIds) { + for (const id of recoveredAlertIds) { const { group: actionGroup, subgroup: actionSubgroup } = - recoveredAlertInstances[id].getLastScheduledActions() ?? {}; - const state = recoveredAlertInstances[id].getState(); - const message = `${params.alertLabel} instance '${id}' has recovered`; - logInstanceEvent( + recoveredAlerts[id].getLastScheduledActions() ?? {}; + const state = recoveredAlerts[id].getState(); + const message = `${params.ruleLabel} alert '${id}' has recovered`; + logAlertEvent( id, EVENT_LOG_ACTIONS.recoveredInstance, message, @@ -919,29 +899,22 @@ function generateNewAndRecoveredInstanceEvents< for (const id of newIds) { const { actionGroup, subgroup: actionSubgroup } = - currentAlertInstances[id].getScheduledActionOptions() ?? {}; - const state = currentAlertInstances[id].getState(); - const message = `${params.alertLabel} created new instance: '${id}'`; - logInstanceEvent( - id, - EVENT_LOG_ACTIONS.newInstance, - message, - state, - actionGroup, - actionSubgroup - ); + currentAlerts[id].getScheduledActionOptions() ?? {}; + const state = currentAlerts[id].getState(); + const message = `${params.ruleLabel} created new alert: '${id}'`; + logAlertEvent(id, EVENT_LOG_ACTIONS.newInstance, message, state, actionGroup, actionSubgroup); } - for (const id of currentAlertInstanceIds) { + for (const id of currentAlertIds) { const { actionGroup, subgroup: actionSubgroup } = - currentAlertInstances[id].getScheduledActionOptions() ?? {}; - const state = currentAlertInstances[id].getState(); - const message = `${params.alertLabel} active instance: '${id}' in ${ + currentAlerts[id].getScheduledActionOptions() ?? {}; + const state = currentAlerts[id].getState(); + const message = `${params.ruleLabel} active alert: '${id}' in ${ actionSubgroup ? `actionGroup(subgroup): '${actionGroup}(${actionSubgroup})'` : `actionGroup: '${actionGroup}'` }`; - logInstanceEvent( + logAlertEvent( id, EVENT_LOG_ACTIONS.activeInstance, message, @@ -951,8 +924,8 @@ function generateNewAndRecoveredInstanceEvents< ); } - function logInstanceEvent( - instanceId: string, + function logAlertEvent( + alertId: string, action: string, message: string, state: InstanceState, @@ -970,7 +943,7 @@ function generateNewAndRecoveredInstanceEvents< }, kibana: { alerting: { - instance_id: instanceId, + instance_id: alertId, ...(group ? { action_group_id: group } : {}), ...(subgroup ? { action_subgroup: subgroup } : {}), }, @@ -978,7 +951,7 @@ function generateNewAndRecoveredInstanceEvents< { rel: SAVED_OBJECT_REL_PRIMARY, type: 'alert', - id: alertId, + id: ruleId, type_id: ruleType.id, namespace, }, @@ -997,27 +970,25 @@ function generateNewAndRecoveredInstanceEvents< } } -interface ScheduleActionsForRecoveredInstancesParams< +interface ScheduleActionsForRecoveredAlertsParams< InstanceState extends AlertInstanceState, InstanceContext extends AlertInstanceContext, RecoveryActionGroupId extends string > { logger: Logger; recoveryActionGroup: ActionGroup; - recoveredAlertInstances: Dictionary< - AlertInstance - >; + recoveredAlerts: Dictionary>; executionHandler: ExecutionHandler; - mutedInstanceIdsSet: Set; - alertLabel: string; + mutedAlertIdsSet: Set; + ruleLabel: string; } -function scheduleActionsForRecoveredInstances< +function scheduleActionsForRecoveredAlerts< InstanceState extends AlertInstanceState, InstanceContext extends AlertInstanceContext, RecoveryActionGroupId extends string >( - params: ScheduleActionsForRecoveredInstancesParams< + params: ScheduleActionsForRecoveredAlertsParams< InstanceState, InstanceContext, RecoveryActionGroupId @@ -1026,96 +997,94 @@ function scheduleActionsForRecoveredInstances< const { logger, recoveryActionGroup, - recoveredAlertInstances, + recoveredAlerts, executionHandler, - mutedInstanceIdsSet, - alertLabel, + mutedAlertIdsSet, + ruleLabel, } = params; - const recoveredIds = Object.keys(recoveredAlertInstances); + const recoveredIds = Object.keys(recoveredAlerts); for (const id of recoveredIds) { - if (mutedInstanceIdsSet.has(id)) { + if (mutedAlertIdsSet.has(id)) { logger.debug( - `skipping scheduling of actions for '${id}' in alert ${alertLabel}: instance is muted` + `skipping scheduling of actions for '${id}' in rule ${ruleLabel}: instance is muted` ); } else { - const instance = recoveredAlertInstances[id]; - instance.updateLastScheduledActions(recoveryActionGroup.id); - instance.unscheduleActions(); + const alert = recoveredAlerts[id]; + alert.updateLastScheduledActions(recoveryActionGroup.id); + alert.unscheduleActions(); executionHandler({ actionGroup: recoveryActionGroup.id, context: {}, state: {}, - alertInstanceId: id, + alertId: id, }); - instance.scheduleActions(recoveryActionGroup.id); + alert.scheduleActions(recoveryActionGroup.id); } } } -interface LogActiveAndRecoveredInstancesParams< +interface LogActiveAndRecoveredAlertsParams< InstanceState extends AlertInstanceState, InstanceContext extends AlertInstanceContext, ActionGroupIds extends string, RecoveryActionGroupId extends string > { logger: Logger; - activeAlertInstances: Dictionary>; - recoveredAlertInstances: Dictionary< - AlertInstance - >; - alertLabel: string; + activeAlerts: Dictionary>; + recoveredAlerts: Dictionary>; + ruleLabel: string; } -function logActiveAndRecoveredInstances< +function logActiveAndRecoveredAlerts< InstanceState extends AlertInstanceState, InstanceContext extends AlertInstanceContext, ActionGroupIds extends string, RecoveryActionGroupId extends string >( - params: LogActiveAndRecoveredInstancesParams< + params: LogActiveAndRecoveredAlertsParams< InstanceState, InstanceContext, ActionGroupIds, RecoveryActionGroupId > ) { - const { logger, activeAlertInstances, recoveredAlertInstances, alertLabel } = params; - const activeInstanceIds = Object.keys(activeAlertInstances); - const recoveredInstanceIds = Object.keys(recoveredAlertInstances); + const { logger, activeAlerts, recoveredAlerts, ruleLabel } = params; + const activeAlertIds = Object.keys(activeAlerts); + const recoveredAlertIds = Object.keys(recoveredAlerts); if (apm.currentTransaction) { apm.currentTransaction.addLabels({ - alerting_active_alerts: activeInstanceIds.length, - alerting_recovered_alerts: recoveredInstanceIds.length, + alerting_active_alerts: activeAlertIds.length, + alerting_recovered_alerts: recoveredAlertIds.length, }); } - if (activeInstanceIds.length > 0) { + if (activeAlertIds.length > 0) { logger.debug( - `alert ${alertLabel} has ${activeInstanceIds.length} active alert instances: ${JSON.stringify( - activeInstanceIds.map((instanceId) => ({ - instanceId, - actionGroup: activeAlertInstances[instanceId].getScheduledActionOptions()?.actionGroup, + `rule ${ruleLabel} has ${activeAlertIds.length} active alerts: ${JSON.stringify( + activeAlertIds.map((alertId) => ({ + instanceId: alertId, + actionGroup: activeAlerts[alertId].getScheduledActionOptions()?.actionGroup, })) )}` ); } - if (recoveredInstanceIds.length > 0) { + if (recoveredAlertIds.length > 0) { logger.debug( - `alert ${alertLabel} has ${ - recoveredInstanceIds.length - } recovered alert instances: ${JSON.stringify(recoveredInstanceIds)}` + `rule ${ruleLabel} has ${recoveredAlertIds.length} recovered alerts: ${JSON.stringify( + recoveredAlertIds + )}` ); } } /** - * If an error is thrown, wrap it in an AlertTaskRunResult + * If an error is thrown, wrap it in an RuleTaskRunResult * so that we can treat each field independantly */ -async function errorAsAlertTaskRunResult( - future: Promise> -): Promise> { +async function errorAsRuleTaskRunResult( + future: Promise> +): Promise> { try { return await future; } catch (e) { diff --git a/x-pack/plugins/alerting/server/task_runner/task_runner_cancel.test.ts b/x-pack/plugins/alerting/server/task_runner/task_runner_cancel.test.ts index c82cc0a7f21e8..1f5730395e79d 100644 --- a/x-pack/plugins/alerting/server/task_runner/task_runner_cancel.test.ts +++ b/x-pack/plugins/alerting/server/task_runner/task_runner_cancel.test.ts @@ -29,10 +29,10 @@ import { alertsMock, rulesClientMock } from '../mocks'; import { eventLoggerMock } from '../../../event_log/server/event_logger.mock'; import { IEventLogger } from '../../../event_log/server'; import { Alert, RecoveredActionGroup } from '../../common'; -import { UntypedNormalizedAlertType } from '../rule_type_registry'; +import { UntypedNormalizedRuleType } from '../rule_type_registry'; import { ruleTypeRegistryMock } from '../rule_type_registry.mock'; -const ruleType: jest.Mocked = { +const ruleType: jest.Mocked = { id: 'test', name: 'My test rule', actionGroups: [{ id: 'default', name: 'Default' }, RecoveredActionGroup], @@ -100,7 +100,7 @@ describe('Task Runner Cancel', () => { ruleTypeRegistry, kibanaBaseUrl: 'https://localhost:5601', supportsEphemeralTasks: false, - maxEphemeralActionsPerAlert: 10, + maxEphemeralActionsPerRule: 10, cancelAlertsOnRuleTimeout: true, }; @@ -196,7 +196,6 @@ describe('Task Runner Cancel', () => { expect(eventLogger.logEvent).toHaveBeenCalledTimes(3); expect(eventLogger.startTiming).toHaveBeenCalledTimes(1); expect(eventLogger.logEvent).toHaveBeenNthCalledWith(1, { - '@timestamp': '1970-01-01T00:00:00.000Z', event: { action: 'execute-start', category: ['alerts'], @@ -216,7 +215,7 @@ describe('Task Runner Cancel', () => { scheduled: '1970-01-01T00:00:00.000Z', }, }, - message: 'alert execution start: "1"', + message: 'rule execution start: "1"', rule: { category: 'test', id: '1', @@ -225,7 +224,6 @@ describe('Task Runner Cancel', () => { }, }); expect(eventLogger.logEvent).toHaveBeenNthCalledWith(2, { - '@timestamp': '1970-01-01T00:00:00.000Z', event: { action: 'execute-timeout', category: ['alerts'], @@ -250,7 +248,6 @@ describe('Task Runner Cancel', () => { }, }); expect(eventLogger.logEvent).toHaveBeenNthCalledWith(3, { - '@timestamp': '1970-01-01T00:00:00.000Z', event: { action: 'execute', category: ['alerts'], @@ -274,7 +271,7 @@ describe('Task Runner Cancel', () => { scheduled: '1970-01-01T00:00:00.000Z', }, }, - message: `alert executed: test:1: 'rule-name'`, + message: `rule executed: test:1: 'rule-name'`, rule: { category: 'test', id: '1', @@ -398,7 +395,7 @@ describe('Task Runner Cancel', () => { const logger = taskRunnerFactoryInitializerParams.logger; expect(logger.debug).toHaveBeenCalledTimes(6); - expect(logger.debug).nthCalledWith(1, 'executing alert test:1 at 1970-01-01T00:00:00.000Z'); + expect(logger.debug).nthCalledWith(1, 'executing rule test:1 at 1970-01-01T00:00:00.000Z'); expect(logger.debug).nthCalledWith( 2, `Cancelling rule type test with id 1 - execution exceeded rule type timeout of 5m` @@ -409,22 +406,21 @@ describe('Task Runner Cancel', () => { ); expect(logger.debug).nthCalledWith( 4, - `alert test:1: 'rule-name' has 1 active alert instances: [{\"instanceId\":\"1\",\"actionGroup\":\"default\"}]` + `rule test:1: 'rule-name' has 1 active alerts: [{\"instanceId\":\"1\",\"actionGroup\":\"default\"}]` ); expect(logger.debug).nthCalledWith( 5, - `no scheduling of actions for alert test:1: 'rule-name': alert execution has been cancelled.` + `no scheduling of actions for rule test:1: 'rule-name': rule execution has been cancelled.` ); expect(logger.debug).nthCalledWith( 6, - 'alertExecutionStatus for test:1: {"lastExecutionDate":"1970-01-01T00:00:00.000Z","status":"active"}' + 'ruleExecutionStatus for test:1: {"lastExecutionDate":"1970-01-01T00:00:00.000Z","status":"active"}' ); const eventLogger = taskRunnerFactoryInitializerParams.eventLogger; expect(eventLogger.startTiming).toHaveBeenCalledTimes(1); expect(eventLogger.logEvent).toHaveBeenCalledTimes(3); expect(eventLogger.logEvent).toHaveBeenNthCalledWith(1, { - '@timestamp': '1970-01-01T00:00:00.000Z', event: { action: 'execute-start', category: ['alerts'], @@ -444,7 +440,7 @@ describe('Task Runner Cancel', () => { }, ], }, - message: `alert execution start: \"1\"`, + message: `rule execution start: \"1\"`, rule: { category: 'test', id: '1', @@ -453,7 +449,6 @@ describe('Task Runner Cancel', () => { }, }); expect(eventLogger.logEvent).toHaveBeenNthCalledWith(2, { - '@timestamp': '1970-01-01T00:00:00.000Z', event: { action: 'execute-timeout', category: ['alerts'], @@ -479,7 +474,6 @@ describe('Task Runner Cancel', () => { }, }); expect(eventLogger.logEvent).toHaveBeenNthCalledWith(3, { - '@timestamp': '1970-01-01T00:00:00.000Z', event: { action: 'execute', category: ['alerts'], @@ -504,7 +498,7 @@ describe('Task Runner Cancel', () => { }, ], }, - message: "alert executed: test:1: 'rule-name'", + message: "rule executed: test:1: 'rule-name'", rule: { category: 'test', id: '1', @@ -518,7 +512,7 @@ describe('Task Runner Cancel', () => { function testActionsExecute() { const logger = taskRunnerFactoryInitializerParams.logger; expect(logger.debug).toHaveBeenCalledTimes(5); - expect(logger.debug).nthCalledWith(1, 'executing alert test:1 at 1970-01-01T00:00:00.000Z'); + expect(logger.debug).nthCalledWith(1, 'executing rule test:1 at 1970-01-01T00:00:00.000Z'); expect(logger.debug).nthCalledWith( 2, `Cancelling rule type test with id 1 - execution exceeded rule type timeout of 5m` @@ -529,17 +523,16 @@ describe('Task Runner Cancel', () => { ); expect(logger.debug).nthCalledWith( 4, - `alert test:1: 'rule-name' has 1 active alert instances: [{\"instanceId\":\"1\",\"actionGroup\":\"default\"}]` + `rule test:1: 'rule-name' has 1 active alerts: [{\"instanceId\":\"1\",\"actionGroup\":\"default\"}]` ); expect(logger.debug).nthCalledWith( 5, - 'alertExecutionStatus for test:1: {"lastExecutionDate":"1970-01-01T00:00:00.000Z","status":"active"}' + 'ruleExecutionStatus for test:1: {"lastExecutionDate":"1970-01-01T00:00:00.000Z","status":"active"}' ); const eventLogger = taskRunnerFactoryInitializerParams.eventLogger; expect(eventLogger.logEvent).toHaveBeenCalledTimes(6); expect(eventLogger.logEvent).toHaveBeenNthCalledWith(1, { - '@timestamp': '1970-01-01T00:00:00.000Z', event: { action: 'execute-start', category: ['alerts'], @@ -560,7 +553,7 @@ describe('Task Runner Cancel', () => { }, ], }, - message: `alert execution start: "1"`, + message: `rule execution start: "1"`, rule: { category: 'test', id: '1', @@ -569,7 +562,6 @@ describe('Task Runner Cancel', () => { }, }); expect(eventLogger.logEvent).toHaveBeenNthCalledWith(2, { - '@timestamp': '1970-01-01T00:00:00.000Z', event: { action: 'execute-timeout', category: ['alerts'], @@ -617,7 +609,7 @@ describe('Task Runner Cancel', () => { }, ], }, - message: "test:1: 'rule-name' created new instance: '1'", + message: "test:1: 'rule-name' created new alert: '1'", rule: { category: 'test', id: '1', @@ -644,7 +636,7 @@ describe('Task Runner Cancel', () => { { id: '1', namespace: undefined, rel: 'primary', type: 'alert', type_id: 'test' }, ], }, - message: "test:1: 'rule-name' active instance: '1' in actionGroup: 'default'", + message: "test:1: 'rule-name' active alert: '1' in actionGroup: 'default'", rule: { category: 'test', id: '1', @@ -689,7 +681,6 @@ describe('Task Runner Cancel', () => { }, }); expect(eventLogger.logEvent).toHaveBeenNthCalledWith(6, { - '@timestamp': '1970-01-01T00:00:00.000Z', event: { action: 'execute', category: ['alerts'], kind: 'alert', outcome: 'success' }, kibana: { alerting: { @@ -709,7 +700,7 @@ describe('Task Runner Cancel', () => { }, ], }, - message: "alert executed: test:1: 'rule-name'", + message: "rule executed: test:1: 'rule-name'", rule: { category: 'test', id: '1', diff --git a/x-pack/plugins/alerting/server/task_runner/task_runner_factory.test.ts b/x-pack/plugins/alerting/server/task_runner/task_runner_factory.test.ts index b799dd2f4043d..038eecda349a1 100644 --- a/x-pack/plugins/alerting/server/task_runner/task_runner_factory.test.ts +++ b/x-pack/plugins/alerting/server/task_runner/task_runner_factory.test.ts @@ -17,13 +17,13 @@ import { import { actionsMock } from '../../../actions/server/mocks'; import { alertsMock, rulesClientMock } from '../mocks'; import { eventLoggerMock } from '../../../event_log/server/event_logger.mock'; -import { UntypedNormalizedAlertType } from '../rule_type_registry'; +import { UntypedNormalizedRuleType } from '../rule_type_registry'; import { ruleTypeRegistryMock } from '../rule_type_registry.mock'; import { executionContextServiceMock } from '../../../../../src/core/server/mocks'; const executionContext = executionContextServiceMock.createSetupContract(); -const alertType: UntypedNormalizedAlertType = { +const ruleType: UntypedNormalizedRuleType = { id: 'test', name: 'My test alert', actionGroups: [{ id: 'default', name: 'Default' }], @@ -83,7 +83,7 @@ describe('Task Runner Factory', () => { ruleTypeRegistry: ruleTypeRegistryMock.create(), kibanaBaseUrl: 'https://localhost:5601', supportsEphemeralTasks: true, - maxEphemeralActionsPerAlert: 10, + maxEphemeralActionsPerRule: 10, cancelAlertsOnRuleTimeout: true, executionContext, }; @@ -96,7 +96,7 @@ describe('Task Runner Factory', () => { test(`throws an error if factory isn't initialized`, () => { const factory = new TaskRunnerFactory(); expect(() => - factory.create(alertType, { taskInstance: mockedTaskInstance }) + factory.create(ruleType, { taskInstance: mockedTaskInstance }) ).toThrowErrorMatchingInlineSnapshot(`"TaskRunnerFactory not initialized"`); }); diff --git a/x-pack/plugins/alerting/server/task_runner/task_runner_factory.ts b/x-pack/plugins/alerting/server/task_runner/task_runner_factory.ts index fc4b8eee89f5e..69c8ff471c8bb 100644 --- a/x-pack/plugins/alerting/server/task_runner/task_runner_factory.ts +++ b/x-pack/plugins/alerting/server/task_runner/task_runner_factory.ts @@ -28,7 +28,7 @@ import { import { TaskRunner } from './task_runner'; import { IEventLogger } from '../../../event_log/server'; import { RulesClient } from '../rules_client'; -import { NormalizedAlertType } from '../rule_type_registry'; +import { NormalizedRuleType } from '../rule_type_registry'; export interface TaskRunnerContext { logger: Logger; @@ -44,7 +44,7 @@ export interface TaskRunnerContext { ruleTypeRegistry: RuleTypeRegistry; kibanaBaseUrl: string | undefined; supportsEphemeralTasks: boolean; - maxEphemeralActionsPerAlert: number; + maxEphemeralActionsPerRule: number; cancelAlertsOnRuleTimeout: boolean; } @@ -69,7 +69,7 @@ export class TaskRunnerFactory { ActionGroupIds extends string, RecoveryActionGroupId extends string >( - alertType: NormalizedAlertType< + ruleType: NormalizedRuleType< Params, ExtractedParams, State, @@ -92,6 +92,6 @@ export class TaskRunnerFactory { InstanceContext, ActionGroupIds, RecoveryActionGroupId - >(alertType, taskInstance, this.taskRunnerContext!); + >(ruleType, taskInstance, this.taskRunnerContext!); } } diff --git a/x-pack/plugins/alerting/server/types.ts b/x-pack/plugins/alerting/server/types.ts index 343b717dcb1aa..6671810a0b738 100644 --- a/x-pack/plugins/alerting/server/types.ts +++ b/x-pack/plugins/alerting/server/types.ts @@ -118,7 +118,7 @@ export type ExecutorType< export interface AlertTypeParamsValidator { validate: (object: unknown) => Params; } -export interface AlertType< +export interface RuleType< Params extends AlertTypeParams = never, ExtractedParams extends AlertTypeParams = never, State extends AlertTypeState = never, @@ -163,7 +163,7 @@ export interface AlertType< ruleTaskTimeout?: string; cancelAlertsOnRuleTimeout?: boolean; } -export type UntypedAlertType = AlertType< +export type UntypedRuleType = RuleType< AlertTypeParams, AlertTypeState, AlertInstanceState, @@ -184,7 +184,7 @@ export interface AlertMeta extends SavedObjectAttributes { // note that the `error` property is "null-able", as we're doing a partial // update on the alert when we update this data, but need to ensure we // delete any previous error if the current status has no error -export interface RawAlertExecutionStatus extends SavedObjectAttributes { +export interface RawRuleExecutionStatus extends SavedObjectAttributes { status: AlertExecutionStatuses; lastExecutionDate: string; lastDuration?: number; @@ -201,7 +201,7 @@ export interface AlertWithLegacyId exten legacyId: string | null; } -export type SanitizedAlertWithLegacyId = Omit< +export type SanitizedRuleWithLegacyId = Omit< AlertWithLegacyId, 'apiKey' >; @@ -212,11 +212,11 @@ export type PartialAlertWithLegacyId = P > & Partial, 'id'>>; -export interface RawAlert extends SavedObjectAttributes { +export interface RawRule extends SavedObjectAttributes { enabled: boolean; name: string; tags: string[]; - alertTypeId: string; + alertTypeId: string; // this cannot be renamed since it is in the saved object consumer: string; legacyId: string | null; schedule: SavedObjectAttributes; @@ -234,11 +234,11 @@ export interface RawAlert extends SavedObjectAttributes { muteAll: boolean; mutedInstanceIds: string[]; meta?: AlertMeta; - executionStatus: RawAlertExecutionStatus; + executionStatus: RawRuleExecutionStatus; } export type AlertInfoParams = Pick< - RawAlert, + RawRule, | 'params' | 'throttle' | 'notifyWhen' diff --git a/x-pack/plugins/apm/common/__snapshots__/elasticsearch_fieldnames.test.ts.snap b/x-pack/plugins/apm/common/__snapshots__/elasticsearch_fieldnames.test.ts.snap index 6da21bf2bf2c7..5dd3588674179 100644 --- a/x-pack/plugins/apm/common/__snapshots__/elasticsearch_fieldnames.test.ts.snap +++ b/x-pack/plugins/apm/common/__snapshots__/elasticsearch_fieldnames.test.ts.snap @@ -73,11 +73,11 @@ Object { } `; -exports[`Error HOST_NAME 1`] = `"my hostname"`; +exports[`Error HOST_HOSTNAME 1`] = `"my hostname"`; -exports[`Error HOST_OS_PLATFORM 1`] = `undefined`; +exports[`Error HOST_NAME 1`] = `undefined`; -exports[`Error HOSTNAME 1`] = `undefined`; +exports[`Error HOST_OS_PLATFORM 1`] = `undefined`; exports[`Error HTTP_REQUEST_METHOD 1`] = `undefined`; @@ -314,12 +314,12 @@ exports[`Span FID_FIELD 1`] = `undefined`; exports[`Span HOST 1`] = `undefined`; +exports[`Span HOST_HOSTNAME 1`] = `undefined`; + exports[`Span HOST_NAME 1`] = `undefined`; exports[`Span HOST_OS_PLATFORM 1`] = `undefined`; -exports[`Span HOSTNAME 1`] = `undefined`; - exports[`Span HTTP_REQUEST_METHOD 1`] = `undefined`; exports[`Span HTTP_RESPONSE_STATUS_CODE 1`] = `undefined`; @@ -555,11 +555,11 @@ Object { } `; -exports[`Transaction HOST_NAME 1`] = `"my hostname"`; +exports[`Transaction HOST_HOSTNAME 1`] = `"my hostname"`; -exports[`Transaction HOST_OS_PLATFORM 1`] = `undefined`; +exports[`Transaction HOST_NAME 1`] = `undefined`; -exports[`Transaction HOSTNAME 1`] = `undefined`; +exports[`Transaction HOST_OS_PLATFORM 1`] = `undefined`; exports[`Transaction HTTP_REQUEST_METHOD 1`] = `"GET"`; diff --git a/x-pack/plugins/apm/common/agent_key_types.ts b/x-pack/plugins/apm/common/agent_key_types.ts new file mode 100644 index 0000000000000..986e67d35698e --- /dev/null +++ b/x-pack/plugins/apm/common/agent_key_types.ts @@ -0,0 +1,13 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +export interface CreateApiKeyResponse { + api_key: string; + expiration?: number; + id: string; + name: string; +} diff --git a/x-pack/plugins/apm/common/correlations/field_stats_types.ts b/x-pack/plugins/apm/common/correlations/field_stats_types.ts index 50dc7919fbd00..41f7e3c3c6649 100644 --- a/x-pack/plugins/apm/common/correlations/field_stats_types.ts +++ b/x-pack/plugins/apm/common/correlations/field_stats_types.ts @@ -8,9 +8,7 @@ import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; import { CorrelationsParams } from './types'; -export interface FieldStatsCommonRequestParams extends CorrelationsParams { - samplerShardSize: number; -} +export type FieldStatsCommonRequestParams = CorrelationsParams; export interface Field { fieldName: string; @@ -55,3 +53,5 @@ export type FieldStats = | NumericFieldStats | KeywordFieldStats | BooleanFieldStats; + +export type FieldValueFieldStats = TopValuesStats; diff --git a/x-pack/plugins/apm/common/elasticsearch_fieldnames.ts b/x-pack/plugins/apm/common/elasticsearch_fieldnames.ts index b42c23ee2df94..5c7c953d8d900 100644 --- a/x-pack/plugins/apm/common/elasticsearch_fieldnames.ts +++ b/x-pack/plugins/apm/common/elasticsearch_fieldnames.ts @@ -113,8 +113,8 @@ export const METRICSET_NAME = 'metricset.name'; export const LABEL_NAME = 'labels.name'; export const HOST = 'host'; -export const HOST_NAME = 'host.hostname'; -export const HOSTNAME = 'host.name'; +export const HOST_HOSTNAME = 'host.hostname'; // Do not use. Please use `HOST_NAME` instead. +export const HOST_NAME = 'host.name'; export const HOST_OS_PLATFORM = 'host.os.platform'; export const CONTAINER_ID = 'container.id'; export const KUBERNETES = 'kubernetes'; diff --git a/x-pack/plugins/apm/common/fleet.ts b/x-pack/plugins/apm/common/fleet.ts index 00a958952d2de..bd8c6cf2653c2 100644 --- a/x-pack/plugins/apm/common/fleet.ts +++ b/x-pack/plugins/apm/common/fleet.ts @@ -8,7 +8,7 @@ import semverParse from 'semver/functions/parse'; export const POLICY_ELASTIC_AGENT_ON_CLOUD = 'policy-elastic-agent-on-cloud'; -export const SUPPORTED_APM_PACKAGE_VERSION = '7.16.0'; +export const SUPPORTED_APM_PACKAGE_VERSION = '8.0.0-dev4'; // TODO update to just '8.0.0' once published export function isPrereleaseVersion(version: string) { return semverParse(version)?.prerelease?.length ?? 0 > 0; diff --git a/x-pack/plugins/apm/common/privilege_type.ts b/x-pack/plugins/apm/common/privilege_type.ts new file mode 100644 index 0000000000000..e5a67d2a807f0 --- /dev/null +++ b/x-pack/plugins/apm/common/privilege_type.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 + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import * as t from 'io-ts'; + +export const enum PrivilegeType { + SOURCEMAP = 'sourcemap:write', + EVENT = 'event:write', + AGENT_CONFIG = 'config_agent:read', +} + +export const privilegesTypeRt = t.array( + t.union([ + t.literal(PrivilegeType.SOURCEMAP), + t.literal(PrivilegeType.EVENT), + t.literal(PrivilegeType.AGENT_CONFIG), + ]) +); diff --git a/x-pack/plugins/apm/public/components/app/Settings/agent_keys/agent_keys_table.tsx b/x-pack/plugins/apm/public/components/app/Settings/agent_keys/agent_keys_table.tsx index 4a05f38d8e505..ccd409f1798a5 100644 --- a/x-pack/plugins/apm/public/components/app/Settings/agent_keys/agent_keys_table.tsx +++ b/x-pack/plugins/apm/public/components/app/Settings/agent_keys/agent_keys_table.tsx @@ -18,10 +18,10 @@ import { ConfirmDeleteModal } from './confirm_delete_modal'; interface Props { agentKeys: ApiKey[]; - refetchAgentKeys: () => void; + onKeyDelete: () => void; } -export function AgentKeysTable({ agentKeys, refetchAgentKeys }: Props) { +export function AgentKeysTable({ agentKeys, onKeyDelete }: Props) { const [agentKeyToBeDeleted, setAgentKeyToBeDeleted] = useState(); const columns: Array> = [ @@ -82,7 +82,7 @@ export function AgentKeysTable({ agentKeys, refetchAgentKeys }: Props) { description: i18n.translate( 'xpack.apm.settings.agentKeys.table.deleteActionDescription', { - defaultMessage: 'Delete this agent key', + defaultMessage: 'Delete this APM agent key', } ), icon: 'trash', @@ -144,7 +144,7 @@ export function AgentKeysTable({ agentKeys, refetchAgentKeys }: Props) { tableCaption={i18n.translate( 'xpack.apm.settings.agentKeys.tableCaption', { - defaultMessage: 'Agent keys', + defaultMessage: 'APM agent keys', } )} items={agentKeys ?? []} @@ -159,7 +159,7 @@ export function AgentKeysTable({ agentKeys, refetchAgentKeys }: Props) { agentKey={agentKeyToBeDeleted} onConfirm={() => { setAgentKeyToBeDeleted(undefined); - refetchAgentKeys(); + onKeyDelete(); }} /> )} diff --git a/x-pack/plugins/apm/public/components/app/Settings/agent_keys/confirm_delete_modal.tsx b/x-pack/plugins/apm/public/components/app/Settings/agent_keys/confirm_delete_modal.tsx index 6125a238f11aa..1fb18499641d9 100644 --- a/x-pack/plugins/apm/public/components/app/Settings/agent_keys/confirm_delete_modal.tsx +++ b/x-pack/plugins/apm/public/components/app/Settings/agent_keys/confirm_delete_modal.tsx @@ -34,14 +34,14 @@ export function ConfirmDeleteModal({ agentKey, onCancel, onConfirm }: Props) { }); toasts.addSuccess( i18n.translate('xpack.apm.settings.agentKeys.invalidate.succeeded', { - defaultMessage: 'Deleted agent key "{name}"', + defaultMessage: 'Deleted APM agent key "{name}"', values: { name }, }) ); } catch (error) { toasts.addDanger( i18n.translate('xpack.apm.settings.agentKeys.invalidate.failed', { - defaultMessage: 'Error deleting agent key "{name}"', + defaultMessage: 'Error deleting APM agent key "{name}"', values: { name }, }) ); @@ -53,7 +53,7 @@ export function ConfirmDeleteModal({ agentKey, onCancel, onConfirm }: Props) { title={i18n.translate( 'xpack.apm.settings.agentKeys.deleteConfirmModal.title', { - defaultMessage: 'Delete agent key "{name}"?', + defaultMessage: 'Delete APM agent key "{name}"?', values: { name }, } )} diff --git a/x-pack/plugins/apm/public/components/app/Settings/agent_keys/create_agent_key.tsx b/x-pack/plugins/apm/public/components/app/Settings/agent_keys/create_agent_key.tsx new file mode 100644 index 0000000000000..01b5bfe907bbc --- /dev/null +++ b/x-pack/plugins/apm/public/components/app/Settings/agent_keys/create_agent_key.tsx @@ -0,0 +1,261 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import React, { useState } from 'react'; +import { i18n } from '@kbn/i18n'; +import { + EuiButton, + EuiFlyout, + EuiFlyoutHeader, + EuiTitle, + EuiFlyoutBody, + EuiFlexGroup, + EuiFlexItem, + EuiFlyoutFooter, + EuiButtonEmpty, + EuiForm, + EuiFormRow, + EuiSpacer, + EuiFieldText, + EuiText, + EuiFormFieldset, + EuiCheckbox, + htmlIdGenerator, +} from '@elastic/eui'; +import { isEmpty } from 'lodash'; +import { callApmApi } from '../../../../services/rest/createCallApmApi'; +import { CreateApiKeyResponse } from '../../../../../common/agent_key_types'; +import { useCurrentUser } from '../../../../hooks/use_current_user'; +import { PrivilegeType } from '../../../../../common/privilege_type'; + +interface Props { + onCancel: () => void; + onSuccess: (agentKey: CreateApiKeyResponse) => void; + onError: (keyName: string, message: string) => void; +} + +export function CreateAgentKeyFlyout({ onCancel, onSuccess, onError }: Props) { + const [formTouched, setFormTouched] = useState(false); + + const [agentKeyBody, setAgentKeyBody] = useState({ + name: '', + sourcemap: true, + event: true, + agentConfig: true, + }); + + const { name, sourcemap, event, agentConfig } = agentKeyBody; + + const currentUser = useCurrentUser(); + + const isInputInvalid = isEmpty(name); + const isFormInvalid = formTouched && isInputInvalid; + + const formError = i18n.translate( + 'xpack.apm.settings.agentKeys.createKeyFlyout.name.placeholder', + { defaultMessage: 'Enter a name' } + ); + + const createAgentKeyTitle = i18n.translate( + 'xpack.apm.settings.agentKeys.createKeyFlyout.createAgentKey', + { defaultMessage: 'Create APM agent key' } + ); + + const createAgentKey = async () => { + setFormTouched(true); + if (isInputInvalid) { + return; + } + + try { + const privileges: PrivilegeType[] = []; + if (sourcemap) { + privileges.push(PrivilegeType.SOURCEMAP); + } + + if (event) { + privileges.push(PrivilegeType.EVENT); + } + + if (agentConfig) { + privileges.push(PrivilegeType.AGENT_CONFIG); + } + + const { agentKey } = await callApmApi({ + endpoint: 'POST /api/apm/agent_keys', + signal: null, + params: { + body: { + name, + privileges, + }, + }, + }); + + onSuccess(agentKey); + } catch (error) { + onError(name, error.body?.message || error.message); + } + }; + + return ( + + + +

{createAgentKeyTitle}

+
+
+ + + + {currentUser && ( + + {currentUser?.username} + + )} + + + setAgentKeyBody((state) => ({ ...state, name: e.target.value })) + } + isInvalid={isFormInvalid} + onBlur={() => setFormTouched(true)} + /> + + + + + + setAgentKeyBody((state) => ({ + ...state, + agentConfig: !state.agentConfig, + })) + } + /> + + + + + setAgentKeyBody((state) => ({ + ...state, + event: !state.event, + })) + } + /> + + + + + setAgentKeyBody((state) => ({ + ...state, + sourcemap: !state.sourcemap, + })) + } + /> + + + + + + + + + + + {i18n.translate( + 'xpack.apm.settings.agentKeys.createKeyFlyout.cancelButton', + { + defaultMessage: 'Cancel', + } + )} + + + + + {createAgentKeyTitle} + + + + +
+ ); +} diff --git a/x-pack/plugins/apm/public/components/app/Settings/agent_keys/create_agent_key/agent_key_callout.tsx b/x-pack/plugins/apm/public/components/app/Settings/agent_keys/create_agent_key/agent_key_callout.tsx new file mode 100644 index 0000000000000..a96446ce2a2b3 --- /dev/null +++ b/x-pack/plugins/apm/public/components/app/Settings/agent_keys/create_agent_key/agent_key_callout.tsx @@ -0,0 +1,79 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import React from 'react'; +import { i18n } from '@kbn/i18n'; +import { + EuiSpacer, + EuiCallOut, + EuiButtonIcon, + EuiCopy, + EuiFieldText, +} from '@elastic/eui'; + +interface Props { + name: string; + token: string; +} + +export function AgentKeyCallOut({ name, token }: Props) { + return ( + <> + +

+ {i18n.translate( + 'xpack.apm.settings.agentKeys.copyAgentKeyField.message', + { + defaultMessage: + 'Copy this key now. You will not be able to view it again.', + } + )} +

+ + {(copy) => ( + + )} + + } + /> +
+ + + ); +} diff --git a/x-pack/plugins/apm/public/components/app/Settings/agent_keys/index.tsx b/x-pack/plugins/apm/public/components/app/Settings/agent_keys/index.tsx index 23acc2e98dd73..3305f05dd90f9 100644 --- a/x-pack/plugins/apm/public/components/app/Settings/agent_keys/index.tsx +++ b/x-pack/plugins/apm/public/components/app/Settings/agent_keys/index.tsx @@ -4,7 +4,7 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ -import React, { Fragment } from 'react'; +import React, { Fragment, useState } from 'react'; import { isEmpty } from 'lodash'; import { i18n } from '@kbn/i18n'; import { @@ -21,6 +21,11 @@ import { useFetcher, FETCH_STATUS } from '../../../../hooks/use_fetcher'; import { PermissionDenied } from './prompts/permission_denied'; import { ApiKeysNotEnabled } from './prompts/api_keys_not_enabled'; import { AgentKeysTable } from './agent_keys_table'; +import { CreateAgentKeyFlyout } from './create_agent_key'; +import { AgentKeyCallOut } from './create_agent_key/agent_key_callout'; +import { CreateApiKeyResponse } from '../../../../../common/agent_key_types'; +import { useApmPluginContext } from '../../../../context/apm_plugin/use_apm_plugin_context'; +import { ApiKey } from '../../../../../../security/common/model'; const INITIAL_DATA = { areApiKeysEnabled: false, @@ -28,33 +33,12 @@ const INITIAL_DATA = { }; export function AgentKeys() { - return ( - - - {i18n.translate('xpack.apm.settings.agentKeys.descriptionText', { - defaultMessage: - 'View and delete agent keys. An agent key sends requests on behalf of a user.', - })} - - - - - -

- {i18n.translate('xpack.apm.settings.agentKeys.title', { - defaultMessage: 'Agent keys', - })} -

-
-
-
- - -
- ); -} + const { toasts } = useApmPluginContext().core.notifications; + + const [isFlyoutVisible, setIsFlyoutVisible] = useState(false); + const [createdAgentKey, setCreatedAgentKey] = + useState(); -function AgentKeysContent() { const { data: { areApiKeysEnabled, canManage } = INITIAL_DATA, status: privilegesStatus, @@ -85,16 +69,113 @@ function AgentKeysContent() { ); const agentKeys = data?.agentKeys; - const isLoading = - privilegesStatus === FETCH_STATUS.LOADING || - status === FETCH_STATUS.LOADING; - const requestFailed = - privilegesStatus === FETCH_STATUS.FAILURE || - status === FETCH_STATUS.FAILURE; + return ( + + + {i18n.translate('xpack.apm.settings.agentKeys.descriptionText', { + defaultMessage: + 'View and delete APM agent keys. An APM agent key sends requests on behalf of a user.', + })} + + + + + +

+ {i18n.translate('xpack.apm.settings.agentKeys.title', { + defaultMessage: 'APM agent keys', + })} +

+
+
+ {areApiKeysEnabled && canManage && !isEmpty(agentKeys) && ( + + setIsFlyoutVisible(true)} + fill={true} + iconType="plusInCircle" + > + {i18n.translate( + 'xpack.apm.settings.agentKeys.createAgentKeyButton', + { + defaultMessage: 'Create APM agent key', + } + )} + + + )} +
+ + {createdAgentKey && ( + + )} + {isFlyoutVisible && ( + { + setIsFlyoutVisible(false); + }} + onSuccess={(agentKey: CreateApiKeyResponse) => { + setCreatedAgentKey(agentKey); + setIsFlyoutVisible(false); + refetchAgentKeys(); + }} + onError={(keyName: string, message: string) => { + toasts.addDanger( + i18n.translate('xpack.apm.settings.agentKeys.crate.failed', { + defaultMessage: + 'Error creating APM agent key "{keyName}". Error: "{message}"', + values: { keyName, message }, + }) + ); + setIsFlyoutVisible(false); + }} + /> + )} + { + setCreatedAgentKey(undefined); + refetchAgentKeys(); + }} + onCreateAgentClick={() => setIsFlyoutVisible(true)} + /> +
+ ); +} +function AgentKeysContent({ + loading, + requestFailed, + canManage, + areApiKeysEnabled, + agentKeys, + onKeyDelete, + onCreateAgentClick, +}: { + loading: boolean; + requestFailed: boolean; + canManage: boolean; + areApiKeysEnabled: boolean; + agentKeys?: ApiKey[]; + onKeyDelete: () => void; + onCreateAgentClick: () => void; +}) { if (!agentKeys) { - if (isLoading) { + if (loading) { return ( } @@ -104,7 +185,7 @@ function AgentKeysContent() { {i18n.translate( 'xpack.apm.settings.agentKeys.agentKeysLoadingPromptTitle', { - defaultMessage: 'Loading Agent keys...', + defaultMessage: 'Loading APM agent keys...', } )} @@ -122,7 +203,7 @@ function AgentKeysContent() { {i18n.translate( 'xpack.apm.settings.agentKeys.agentKeysErrorPromptTitle', { - defaultMessage: 'Could not load agent keys.', + defaultMessage: 'Could not load APM agent keys.', } )} @@ -147,7 +228,7 @@ function AgentKeysContent() { title={

{i18n.translate('xpack.apm.settings.agentKeys.emptyPromptTitle', { - defaultMessage: 'Create your first agent key', + defaultMessage: 'Create your first key', })}

} @@ -155,16 +236,20 @@ function AgentKeysContent() {

{i18n.translate('xpack.apm.settings.agentKeys.emptyPromptBody', { defaultMessage: - 'Create agent keys to authorize requests to the APM Server.', + 'Create APM agent keys to authorize APM agent requests to the APM Server.', })}

} actions={ - + {i18n.translate( 'xpack.apm.settings.agentKeys.createAgentKeyButton', { - defaultMessage: 'Create agent key', + defaultMessage: 'Create APM agent key', } )} @@ -175,10 +260,7 @@ function AgentKeysContent() { if (agentKeys && !isEmpty(agentKeys)) { return ( - + ); } diff --git a/x-pack/plugins/apm/public/components/app/correlations/context_popover/context_popover.tsx b/x-pack/plugins/apm/public/components/app/correlations/context_popover/context_popover.tsx index f1d0d194749c5..d7043ea669a03 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/context_popover/context_popover.tsx +++ b/x-pack/plugins/apm/public/components/app/correlations/context_popover/context_popover.tsx @@ -11,14 +11,11 @@ import { EuiFlexItem, EuiPopover, EuiPopoverTitle, - EuiSpacer, - EuiText, EuiTitle, EuiToolTip, } from '@elastic/eui'; -import React, { Fragment, useState } from 'react'; +import React, { useState } from 'react'; import { i18n } from '@kbn/i18n'; -import { FormattedMessage } from '@kbn/i18n-react'; import { FieldStats } from '../../../../../common/correlations/field_stats_types'; import { OnAddFilter, TopValues } from './top_values'; import { useTheme } from '../../../../hooks/use_theme'; @@ -97,27 +94,11 @@ export function CorrelationsContextPopover({ {infoIsOpen ? ( - <> - - {topValueStats.topValuesSampleSize !== undefined && ( - - - - - - - )} - + ) : null} ); diff --git a/x-pack/plugins/apm/public/components/app/correlations/context_popover/top_values.tsx b/x-pack/plugins/apm/public/components/app/correlations/context_popover/top_values.tsx index 05b4f6d56fa45..fbf33899a2de2 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/context_popover/top_values.tsx +++ b/x-pack/plugins/apm/public/components/app/correlations/context_popover/top_values.tsx @@ -12,11 +12,21 @@ import { EuiProgress, EuiSpacer, EuiToolTip, + EuiText, + EuiHorizontalRule, + EuiLoadingSpinner, } from '@elastic/eui'; import { i18n } from '@kbn/i18n'; -import { FieldStats } from '../../../../../common/correlations/field_stats_types'; +import numeral from '@elastic/numeral'; +import { FormattedMessage } from '@kbn/i18n-react'; +import { + FieldStats, + TopValueBucket, +} from '../../../../../common/correlations/field_stats_types'; import { asPercent } from '../../../../../common/utils/formatters'; import { useTheme } from '../../../../hooks/use_theme'; +import { FETCH_STATUS, useFetcher } from '../../../../hooks/use_fetcher'; +import { useFetchParams } from '../use_fetch_params'; export type OnAddFilter = ({ fieldName, @@ -28,23 +38,179 @@ export type OnAddFilter = ({ include: boolean; }) => void; -interface Props { +interface TopValueProps { + progressBarMax: number; + barColor: string; + value: TopValueBucket; + isHighlighted: boolean; + fieldName: string; + onAddFilter?: OnAddFilter; + valueText?: string; + reverseLabel?: boolean; +} +export function TopValue({ + progressBarMax, + barColor, + value, + isHighlighted, + fieldName, + onAddFilter, + valueText, + reverseLabel = false, +}: TopValueProps) { + const theme = useTheme(); + return ( + + + + {value.key} + + } + className="eui-textTruncate" + aria-label={value.key.toString()} + valueText={valueText} + labelProps={ + isHighlighted + ? { + style: { fontWeight: 'bold' }, + } + : undefined + } + /> + + {fieldName !== undefined && + value.key !== undefined && + onAddFilter !== undefined ? ( + <> + { + onAddFilter({ + fieldName, + fieldValue: + typeof value.key === 'number' + ? value.key.toString() + : value.key, + include: true, + }); + }} + aria-label={i18n.translate( + 'xpack.apm.correlations.fieldContextPopover.addFilterAriaLabel', + { + defaultMessage: 'Filter for {fieldName}: "{value}"', + values: { fieldName, value: value.key }, + } + )} + data-test-subj={`apmFieldContextTopValuesAddFilterButton-${value.key}-${value.key}`} + style={{ + minHeight: 'auto', + width: theme.eui.euiSizeL, + paddingRight: 2, + paddingLeft: 2, + paddingTop: 0, + paddingBottom: 0, + }} + /> + { + onAddFilter({ + fieldName, + fieldValue: + typeof value.key === 'number' + ? value.key.toString() + : value.key, + include: false, + }); + }} + aria-label={i18n.translate( + 'xpack.apm.correlations.fieldContextPopover.removeFilterAriaLabel', + { + defaultMessage: 'Filter out {fieldName}: "{value}"', + values: { fieldName, value: value.key }, + } + )} + data-test-subj={`apmFieldContextTopValuesExcludeFilterButton-${value.key}-${value.key}`} + style={{ + minHeight: 'auto', + width: theme.eui.euiSizeL, + paddingTop: 0, + paddingBottom: 0, + paddingRight: 2, + paddingLeft: 2, + }} + /> + + ) : null} + + ); +} + +interface TopValuesProps { topValueStats: FieldStats; compressed?: boolean; onAddFilter?: OnAddFilter; fieldValue?: string | number; } -export function TopValues({ topValueStats, onAddFilter, fieldValue }: Props) { +export function TopValues({ + topValueStats, + onAddFilter, + fieldValue, +}: TopValuesProps) { const { topValues, topValuesSampleSize, count, fieldName } = topValueStats; const theme = useTheme(); - if (!Array.isArray(topValues) || topValues.length === 0) return null; + const idxToHighlight = Array.isArray(topValues) + ? topValues.findIndex((value) => value.key === fieldValue) + : null; + + const params = useFetchParams(); + const { data: fieldValueStats, status } = useFetcher( + (callApmApi) => { + if ( + idxToHighlight === -1 && + fieldName !== undefined && + fieldValue !== undefined + ) { + return callApmApi({ + endpoint: 'GET /internal/apm/correlations/field_value_stats', + params: { + query: { + ...params, + fieldName, + fieldValue, + }, + }, + }); + } + }, + [params, fieldName, fieldValue, idxToHighlight] + ); + if ( + !Array.isArray(topValues) || + topValues?.length === 0 || + fieldValue === undefined + ) + return null; const sampledSize = typeof topValuesSampleSize === 'string' ? parseInt(topValuesSampleSize, 10) : topValuesSampleSize; + const progressBarMax = sampledSize ?? count; return (
- - - - {value.key} - - } - className="eui-textTruncate" - aria-label={value.key.toString()} - valueText={valueText} - labelProps={ - isHighlighted - ? { - style: { fontWeight: 'bold' }, - } - : undefined - } - /> - - {fieldName !== undefined && - value.key !== undefined && - onAddFilter !== undefined ? ( - <> - { - onAddFilter({ - fieldName, - fieldValue: - typeof value.key === 'number' - ? value.key.toString() - : value.key, - include: true, - }); - }} - aria-label={i18n.translate( - 'xpack.apm.correlations.fieldContextPopover.addFilterAriaLabel', - { - defaultMessage: 'Filter for {fieldName}: "{value}"', - values: { fieldName, value: value.key }, - } - )} - data-test-subj={`apmFieldContextTopValuesAddFilterButton-${value.key}-${value.key}`} - style={{ - minHeight: 'auto', - width: theme.eui.euiSizeL, - paddingRight: 2, - paddingLeft: 2, - paddingTop: 0, - paddingBottom: 0, - }} - /> - { - onAddFilter({ - fieldName, - fieldValue: - typeof value.key === 'number' - ? value.key.toString() - : value.key, - include: false, - }); - }} - aria-label={i18n.translate( - 'xpack.apm.correlations.fieldContextPopover.removeFilterAriaLabel', - { - defaultMessage: 'Filter out {fieldName}: "{value}"', - values: { fieldName, value: value.key }, - } - )} - data-test-subj={`apmFieldContextTopValuesExcludeFilterButton-${value.key}-${value.key}`} - style={{ - minHeight: 'auto', - width: theme.eui.euiSizeL, - paddingTop: 0, - paddingBottom: 0, - paddingRight: 2, - paddingLeft: 2, - }} - /> - - ) : null} - + ); })} + + {idxToHighlight === -1 && ( + <> + + + + + + {status === FETCH_STATUS.SUCCESS && + Array.isArray(fieldValueStats?.topValues) ? ( + fieldValueStats?.topValues.map((value) => { + const valueText = + progressBarMax !== undefined + ? asPercent(value.doc_count, progressBarMax) + : undefined; + + return ( + + ); + }) + ) : ( + + + + )} + + )} + + {topValueStats.topValuesSampleSize !== undefined && ( + <> + + + {i18n.translate( + 'xpack.apm.correlations.fieldContextPopover.calculatedFromSampleDescription', + { + defaultMessage: + 'Calculated from sample of {sampleSize} documents', + values: { sampleSize: topValueStats.topValuesSampleSize }, + } + )} + + + )}
); } diff --git a/x-pack/plugins/apm/public/components/app/correlations/correlations_table.tsx b/x-pack/plugins/apm/public/components/app/correlations/correlations_table.tsx index a2026b0a8abea..a530b950cf061 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/correlations_table.tsx +++ b/x-pack/plugins/apm/public/components/app/correlations/correlations_table.tsx @@ -122,7 +122,7 @@ export function CorrelationsTable({ const loadingText = i18n.translate( 'xpack.apm.correlations.correlationsTable.loadingText', - { defaultMessage: 'Loading' } + { defaultMessage: 'Loading...' } ); const noDataText = i18n.translate( diff --git a/x-pack/plugins/apm/public/components/app/service_inventory/index.tsx b/x-pack/plugins/apm/public/components/app/service_inventory/index.tsx index 1994d3641ee53..163082cf044cd 100644 --- a/x-pack/plugins/apm/public/components/app/service_inventory/index.tsx +++ b/x-pack/plugins/apm/public/components/app/service_inventory/index.tsx @@ -19,7 +19,7 @@ import { useAnomalyDetectionJobsContext } from '../../../context/anomaly_detecti import { useApmPluginContext } from '../../../context/apm_plugin/use_apm_plugin_context'; import { useLegacyUrlParams } from '../../../context/url_params_context/use_url_params'; import { useLocalStorage } from '../../../hooks/useLocalStorage'; -import { useApmParams } from '../../../hooks/use_apm_params'; +import { useAnyOfApmParams } from '../../../hooks/use_apm_params'; import { FETCH_STATUS, useFetcher } from '../../../hooks/use_fetcher'; import { useTimeRange } from '../../../hooks/use_time_range'; import { useUpgradeAssistantHref } from '../../shared/Links/kibana'; @@ -46,9 +46,7 @@ function useServicesFetcher() { const { query: { rangeFrom, rangeTo, environment, kuery }, - } = - // @ts-ignore 4.3.5 upgrade - Type instantiation is excessively deep and possibly infinite. - useApmParams('/services/{serviceName}', '/services'); + } = useAnyOfApmParams('/services/{serviceName}', '/services'); const { start, end } = useTimeRange({ rangeFrom, rangeTo }); diff --git a/x-pack/plugins/apm/public/components/app/service_inventory/service_list/index.tsx b/x-pack/plugins/apm/public/components/app/service_inventory/service_list/index.tsx index ea65c837a4177..fe91b14e64e8a 100644 --- a/x-pack/plugins/apm/public/components/app/service_inventory/service_list/index.tsx +++ b/x-pack/plugins/apm/public/components/app/service_inventory/service_list/index.tsx @@ -66,9 +66,11 @@ export function getServiceColumns({ showTransactionTypeColumn, comparisonData, breakpoints, + showHealthStatusColumn, }: { query: TypeOf['query']; showTransactionTypeColumn: boolean; + showHealthStatusColumn: boolean; breakpoints: Breakpoints; comparisonData?: ServicesDetailedStatisticsAPIResponse; }): Array> { @@ -76,21 +78,25 @@ export function getServiceColumns({ const showWhenSmallOrGreaterThanLarge = isSmall || !isLarge; const showWhenSmallOrGreaterThanXL = isSmall || !isXl; return [ - { - field: 'healthStatus', - name: i18n.translate('xpack.apm.servicesTable.healthColumnLabel', { - defaultMessage: 'Health', - }), - width: `${unit * 6}px`, - sortable: true, - render: (_, { healthStatus }) => { - return ( - - ); - }, - }, + ...(showHealthStatusColumn + ? [ + { + field: 'healthStatus', + name: i18n.translate('xpack.apm.servicesTable.healthColumnLabel', { + defaultMessage: 'Health', + }), + width: `${unit * 6}px`, + sortable: true, + render: (_, { healthStatus }) => { + return ( + + ); + }, + } as ITableColumn, + ] + : []), { field: 'serviceName', name: i18n.translate('xpack.apm.servicesTable.nameColumnLabel', { @@ -248,13 +254,17 @@ export function ServiceList({ showTransactionTypeColumn, comparisonData, breakpoints, + showHealthStatusColumn: displayHealthStatus, }), - [query, showTransactionTypeColumn, comparisonData, breakpoints] + [ + query, + showTransactionTypeColumn, + comparisonData, + breakpoints, + displayHealthStatus, + ] ); - const columns = displayHealthStatus - ? serviceColumns - : serviceColumns.filter((column) => column.field !== 'healthStatus'); const initialSortField = displayHealthStatus ? 'healthStatus' : 'transactionsPerMinute'; @@ -300,7 +310,7 @@ export function ServiceList({ { it('renders empty state', async () => { @@ -29,34 +55,10 @@ describe('ServiceList', () => { }); describe('responsive columns', () => { - const query = { - rangeFrom: 'now-15m', - rangeTo: 'now', - environment: ENVIRONMENT_ALL.value, - kuery: '', - }; - - const service: any = { - serviceName: 'opbeans-python', - agentName: 'python', - transactionsPerMinute: { - value: 86.93333333333334, - timeseries: [], - }, - errorsPerMinute: { - value: 12.6, - timeseries: [], - }, - avgResponseTime: { - value: 91535.42944785276, - timeseries: [], - }, - environments: ['test'], - transactionType: 'request', - }; describe('when small', () => { it('shows environment, transaction type and sparklines', () => { const renderedColumns = getServiceColumns({ + showHealthStatusColumn: true, query, showTransactionTypeColumn: true, breakpoints: { @@ -91,6 +93,7 @@ describe('ServiceList', () => { describe('when Large', () => { it('hides environment, transaction type and sparklines', () => { const renderedColumns = getServiceColumns({ + showHealthStatusColumn: true, query, showTransactionTypeColumn: true, breakpoints: { @@ -114,6 +117,7 @@ describe('ServiceList', () => { describe('when XL', () => { it('hides transaction type', () => { const renderedColumns = getServiceColumns({ + showHealthStatusColumn: true, query, showTransactionTypeColumn: true, breakpoints: { @@ -147,6 +151,7 @@ describe('ServiceList', () => { describe('when XXL', () => { it('hides transaction type', () => { const renderedColumns = getServiceColumns({ + showHealthStatusColumn: true, query, showTransactionTypeColumn: true, breakpoints: { @@ -181,20 +186,34 @@ describe('ServiceList', () => { }); describe('without ML data', () => { - it('sorts by throughput', async () => { - render(); - - expect(await screen.findByTitle('Throughput')).toBeInTheDocument(); + it('hides healthStatus column', () => { + const renderedColumns = getServiceColumns({ + showHealthStatusColumn: false, + query, + showTransactionTypeColumn: true, + breakpoints: { + isSmall: false, + isLarge: false, + isXl: false, + } as Breakpoints, + }).map((c) => c.field); + expect(renderedColumns.includes('healthStatus')).toBeFalsy(); }); }); describe('with ML data', () => { - it('renders the health column', async () => { - render(); - - expect( - await screen.findByRole('button', { name: /Health/ }) - ).toBeInTheDocument(); + it('shows healthStatus column', () => { + const renderedColumns = getServiceColumns({ + showHealthStatusColumn: true, + query, + showTransactionTypeColumn: true, + breakpoints: { + isSmall: false, + isLarge: false, + isXl: false, + } as Breakpoints, + }).map((c) => c.field); + expect(renderedColumns.includes('healthStatus')).toBeTruthy(); }); }); }); diff --git a/x-pack/plugins/apm/public/components/app/service_logs/index.test.ts b/x-pack/plugins/apm/public/components/app/service_logs/index.test.ts index b0cc134778d21..74a49d06d761b 100644 --- a/x-pack/plugins/apm/public/components/app/service_logs/index.test.ts +++ b/x-pack/plugins/apm/public/components/app/service_logs/index.test.ts @@ -7,26 +7,53 @@ import { getInfrastructureKQLFilter } from './'; describe('service logs', () => { + const serviceName = 'opbeans-node'; + describe('getInfrastructureKQLFilter', () => { - it('filter by container id', () => { + it('filter by service name', () => { + expect( + getInfrastructureKQLFilter( + { + serviceInfrastructure: { + containerIds: [], + hostNames: [], + }, + }, + serviceName + ) + ).toEqual('service.name: "opbeans-node"'); + }); + + it('filter by container id as fallback', () => { expect( - getInfrastructureKQLFilter({ - serviceInfrastructure: { - containerIds: ['foo', 'bar'], - hostNames: ['baz', `quz`], + getInfrastructureKQLFilter( + { + serviceInfrastructure: { + containerIds: ['foo', 'bar'], + hostNames: ['baz', `quz`], + }, }, - }) - ).toEqual('container.id: "foo" or container.id: "bar"'); + serviceName + ) + ).toEqual( + 'service.name: "opbeans-node" or (not service.name and (container.id: "foo" or container.id: "bar"))' + ); }); - it('filter by host names', () => { + + it('filter by host names as fallback', () => { expect( - getInfrastructureKQLFilter({ - serviceInfrastructure: { - containerIds: [], - hostNames: ['baz', `quz`], + getInfrastructureKQLFilter( + { + serviceInfrastructure: { + containerIds: [], + hostNames: ['baz', `quz`], + }, }, - }) - ).toEqual('host.name: "baz" or host.name: "quz"'); + serviceName + ) + ).toEqual( + 'service.name: "opbeans-node" or (not service.name and (host.name: "baz" or host.name: "quz"))' + ); }); }); }); diff --git a/x-pack/plugins/apm/public/components/app/service_logs/index.tsx b/x-pack/plugins/apm/public/components/app/service_logs/index.tsx index bb32919196f84..4f1c517d14b26 100644 --- a/x-pack/plugins/apm/public/components/app/service_logs/index.tsx +++ b/x-pack/plugins/apm/public/components/app/service_logs/index.tsx @@ -17,7 +17,8 @@ import { APIReturnType } from '../../../services/rest/createCallApmApi'; import { CONTAINER_ID, - HOSTNAME, + HOST_NAME, + SERVICE_NAME, } from '../../../../common/elasticsearch_fieldnames'; import { useApmParams } from '../../../hooks/use_apm_params'; import { useTimeRange } from '../../../hooks/use_time_range'; @@ -86,20 +87,27 @@ export function ServiceLogs() { height={'60vh'} startTimestamp={moment(start).valueOf()} endTimestamp={moment(end).valueOf()} - query={getInfrastructureKQLFilter(data)} + query={getInfrastructureKQLFilter(data, serviceName)} /> ); } export const getInfrastructureKQLFilter = ( - data?: APIReturnType<'GET /internal/apm/services/{serviceName}/infrastructure'> + data: + | APIReturnType<'GET /internal/apm/services/{serviceName}/infrastructure'> + | undefined, + serviceName: string ) => { const containerIds = data?.serviceInfrastructure?.containerIds ?? []; const hostNames = data?.serviceInfrastructure?.hostNames ?? []; - const kqlFilter = containerIds.length + const infraAttributes = containerIds.length ? containerIds.map((id) => `${CONTAINER_ID}: "${id}"`) - : hostNames.map((id) => `${HOSTNAME}: "${id}"`); + : hostNames.map((id) => `${HOST_NAME}: "${id}"`); - return kqlFilter.join(' or '); + const infraAttributesJoined = infraAttributes.join(' or '); + + return infraAttributes.length + ? `${SERVICE_NAME}: "${serviceName}" or (not ${SERVICE_NAME} and (${infraAttributesJoined}))` + : `${SERVICE_NAME}: "${serviceName}"`; }; diff --git a/x-pack/plugins/apm/public/components/app/service_map/Controls.tsx b/x-pack/plugins/apm/public/components/app/service_map/Controls.tsx index 4605952a6f396..a48fb77b45585 100644 --- a/x-pack/plugins/apm/public/components/app/service_map/Controls.tsx +++ b/x-pack/plugins/apm/public/components/app/service_map/Controls.tsx @@ -16,7 +16,7 @@ import { useLegacyUrlParams } from '../../../context/url_params_context/use_url_ import { APMQueryParams } from '../../shared/Links/url_helpers'; import { CytoscapeContext } from './Cytoscape'; import { getAnimationOptions, getNodeHeight } from './cytoscape_options'; -import { useApmParams } from '../../../hooks/use_apm_params'; +import { useAnyOfApmParams } from '../../../hooks/use_apm_params'; const ControlsContainer = euiStyled('div')` left: ${({ theme }) => theme.eui.gutterTypes.gutterMedium}; @@ -107,7 +107,7 @@ export function Controls() { const { query: { kuery }, - } = useApmParams('/service-map', '/services/{serviceName}/service-map'); + } = useAnyOfApmParams('/service-map', '/services/{serviceName}/service-map'); const [zoom, setZoom] = useState((cy && cy.zoom()) || 1); const duration = parseInt(theme.eui.euiAnimSpeedFast, 10); diff --git a/x-pack/plugins/apm/public/components/app/service_map/Popover/backend_contents.tsx b/x-pack/plugins/apm/public/components/app/service_map/Popover/backend_contents.tsx index a862ff872f61a..8fa93e22a90fe 100644 --- a/x-pack/plugins/apm/public/components/app/service_map/Popover/backend_contents.tsx +++ b/x-pack/plugins/apm/public/components/app/service_map/Popover/backend_contents.tsx @@ -13,7 +13,7 @@ import React from 'react'; import { useUiTracker } from '../../../../../../observability/public'; import { ContentsProps } from '.'; import { NodeStats } from '../../../../../common/service_map'; -import { useApmParams } from '../../../../hooks/use_apm_params'; +import { useAnyOfApmParams } from '../../../../hooks/use_apm_params'; import { useApmRouter } from '../../../../hooks/use_apm_router'; import { FETCH_STATUS, useFetcher } from '../../../../hooks/use_fetcher'; import { ApmRoutes } from '../../../routing/apm_route_config'; @@ -25,8 +25,7 @@ export function BackendContents({ start, end, }: ContentsProps) { - // @ts-ignore 4.3.5 upgrade - Type instantiation is excessively deep and possibly infinite. - const { query } = useApmParams( + const { query } = useAnyOfApmParams( '/service-map', '/services/{serviceName}/service-map' ); diff --git a/x-pack/plugins/apm/public/components/fleet_integration/apm_agents/agent_instructions_accordion.tsx b/x-pack/plugins/apm/public/components/fleet_integration/apm_agents/agent_instructions_accordion.tsx index 8f66658785b97..a82fa3121bb3b 100644 --- a/x-pack/plugins/apm/public/components/fleet_integration/apm_agents/agent_instructions_accordion.tsx +++ b/x-pack/plugins/apm/public/components/fleet_integration/apm_agents/agent_instructions_accordion.tsx @@ -12,19 +12,29 @@ import { EuiSpacer, EuiText, EuiCodeBlock, + EuiTabbedContent, + EuiBetaBadge, } from '@elastic/eui'; import { i18n } from '@kbn/i18n'; -import React from 'react'; -import { CreateAgentInstructions } from './agent_instructions_mappings'; +import React, { ComponentType } from 'react'; +import styled from 'styled-components'; +import { + AgentRuntimeAttachmentProps, + CreateAgentInstructions, +} from './agent_instructions_mappings'; import { Markdown, useKibana, } from '../../../../../../../src/plugins/kibana_react/public'; import { AgentName } from '../../../../typings/es_schemas/ui/fields/agent'; import { AgentIcon } from '../../shared/agent_icon'; -import { NewPackagePolicy } from '../apm_policy_form/typings'; +import type { + NewPackagePolicy, + PackagePolicy, + PackagePolicyEditExtensionComponentProps, +} from '../apm_policy_form/typings'; import { getCommands } from '../../../tutorial/config_agent/commands/get_commands'; -import { replaceTemplateStrings } from './replace_template_strings'; +import { renderMustache } from './render_mustache'; function AccordionButtonContent({ agentName, @@ -97,96 +107,175 @@ function TutorialConfigAgent({ } interface Props { + policy: PackagePolicy; newPolicy: NewPackagePolicy; + onChange: PackagePolicyEditExtensionComponentProps['onChange']; agentName: AgentName; title: string; variantId: string; createAgentInstructions: CreateAgentInstructions; + AgentRuntimeAttachment?: ComponentType; } +const StyledEuiAccordion = styled(EuiAccordion)` + // This is an alternative fix suggested by the EUI team to fix drag elements inside EuiAccordion + // This Issue tracks the fix on the Eui side https://github.com/elastic/eui/issues/3548#issuecomment-639041283 + .euiAccordion__childWrapper { + transform: none; + } +`; + export function AgentInstructionsAccordion({ + policy, newPolicy, + onChange, agentName, title, createAgentInstructions, variantId, + AgentRuntimeAttachment, }: Props) { const docLinks = useKibana().services.docLinks; const vars = newPolicy?.inputs?.[0]?.vars; const apmServerUrl = vars?.url.value; const secretToken = vars?.secret_token.value; const steps = createAgentInstructions(apmServerUrl, secretToken); + const stepsElements = steps.map( + ( + { title: stepTitle, textPre, textPost, customComponentName, commands }, + index + ) => { + const commandBlock = commands + ? renderMustache({ + text: commands, + docLinks, + }) + : ''; + + return ( +
+ +

{stepTitle}

+
+ + + {textPre && ( + + )} + {commandBlock && ( + <> + + + {commandBlock} + + + )} + {customComponentName === 'TutorialConfigAgent' && ( + + )} + {customComponentName === 'TutorialConfigAgentRumScript' && ( + + )} + {textPost && ( + <> + + + + )} + + +
+ ); + } + ); + + const manualInstrumentationContent = ( + <> + + {stepsElements} + + ); + return ( - } > - - {steps.map( - ( - { - title: stepTitle, - textPre, - textPost, - customComponentName, - commands, - }, - index - ) => { - const commandBlock = replaceTemplateStrings( - Array.isArray(commands) ? commands.join('\n') : commands || '', - docLinks - ); - return ( -
- -

{stepTitle}

-
- - - {textPre && ( - - )} - {commandBlock && ( - <> - - - {commandBlock} - - - )} - {customComponentName === 'TutorialConfigAgent' && ( - - )} - {customComponentName === 'TutorialConfigAgentRumScript' && ( - - )} - {textPost && ( + {AgentRuntimeAttachment ? ( + <> + + + + {i18n.translate( + 'xpack.apm.fleetIntegration.apmAgent.runtimeAttachment.autoAttachment', + { defaultMessage: 'Auto-Attachment' } + )} + + + + + + ), + content: ( <> - - )} - - -
- ); - } + ), + }, + ]} + /> + + ) : ( + manualInstrumentationContent )} -
+ ); } diff --git a/x-pack/plugins/apm/public/components/fleet_integration/apm_agents/agent_instructions_mappings.ts b/x-pack/plugins/apm/public/components/fleet_integration/apm_agents/agent_instructions_mappings.ts index 8bfdafe61d44e..5e992094ac64c 100644 --- a/x-pack/plugins/apm/public/components/fleet_integration/apm_agents/agent_instructions_mappings.ts +++ b/x-pack/plugins/apm/public/components/fleet_integration/apm_agents/agent_instructions_mappings.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { ComponentType } from 'react'; import { createDotNetAgentInstructions, createDjangoAgentInstructions, @@ -18,6 +19,18 @@ import { createRackAgentInstructions, } from '../../../../common/tutorial/instructions/apm_agent_instructions'; import { AgentName } from '../../../../typings/es_schemas/ui/fields/agent'; +import { JavaRuntimeAttachment } from './runtime_attachment/supported_agents/java_runtime_attachment'; +import { + NewPackagePolicy, + PackagePolicy, + PackagePolicyEditExtensionComponentProps, +} from '../apm_policy_form/typings'; + +export interface AgentRuntimeAttachmentProps { + policy: PackagePolicy; + newPolicy: NewPackagePolicy; + onChange: PackagePolicyEditExtensionComponentProps['onChange']; +} export type CreateAgentInstructions = ( apmServerUrl?: string, @@ -35,12 +48,14 @@ export const ApmAgentInstructionsMappings: Array<{ title: string; variantId: string; createAgentInstructions: CreateAgentInstructions; + AgentRuntimeAttachment?: ComponentType; }> = [ { agentName: 'java', title: 'Java', variantId: 'java', createAgentInstructions: createJavaAgentInstructions, + AgentRuntimeAttachment: JavaRuntimeAttachment, }, { agentName: 'rum-js', diff --git a/x-pack/plugins/apm/public/components/fleet_integration/apm_agents/index.tsx b/x-pack/plugins/apm/public/components/fleet_integration/apm_agents/index.tsx index d6a43a1e1268a..09b638fb184df 100644 --- a/x-pack/plugins/apm/public/components/fleet_integration/apm_agents/index.tsx +++ b/x-pack/plugins/apm/public/components/fleet_integration/apm_agents/index.tsx @@ -21,19 +21,28 @@ interface Props { onChange: PackagePolicyEditExtensionComponentProps['onChange']; } -export function ApmAgents({ newPolicy }: Props) { +export function ApmAgents({ policy, newPolicy, onChange }: Props) { return (
{ApmAgentInstructionsMappings.map( - ({ agentName, title, createAgentInstructions, variantId }) => ( + ({ + agentName, + title, + createAgentInstructions, + variantId, + AgentRuntimeAttachment, + }) => ( diff --git a/x-pack/plugins/apm/public/components/fleet_integration/apm_agents/replace_template_strings.ts b/x-pack/plugins/apm/public/components/fleet_integration/apm_agents/render_mustache.ts similarity index 65% rename from x-pack/plugins/apm/public/components/fleet_integration/apm_agents/replace_template_strings.ts rename to x-pack/plugins/apm/public/components/fleet_integration/apm_agents/render_mustache.ts index d36d76d466308..ebf5fea7f2b85 100644 --- a/x-pack/plugins/apm/public/components/fleet_integration/apm_agents/replace_template_strings.ts +++ b/x-pack/plugins/apm/public/components/fleet_integration/apm_agents/render_mustache.ts @@ -10,12 +10,17 @@ import Mustache from 'mustache'; const TEMPLATE_TAGS = ['{', '}']; -export function replaceTemplateStrings( - text: string, - docLinks?: CoreStart['docLinks'] -) { - Mustache.parse(text, TEMPLATE_TAGS); - return Mustache.render(text, { +export function renderMustache({ + text, + docLinks, +}: { + text: string | string[]; + docLinks?: CoreStart['docLinks']; +}) { + const template = Array.isArray(text) ? text.join('\n') : text; + + Mustache.parse(template, TEMPLATE_TAGS); + return Mustache.render(template, { config: { docs: { base_url: docLinks?.ELASTIC_WEBSITE_URL, diff --git a/x-pack/plugins/apm/public/components/fleet_integration/apm_agents/runtime_attachment/default_discovery_rule.tsx b/x-pack/plugins/apm/public/components/fleet_integration/apm_agents/runtime_attachment/default_discovery_rule.tsx new file mode 100644 index 0000000000000..848582bb3feb6 --- /dev/null +++ b/x-pack/plugins/apm/public/components/fleet_integration/apm_agents/runtime_attachment/default_discovery_rule.tsx @@ -0,0 +1,30 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { + EuiText, + EuiFlexGroup, + EuiFlexItem, + EuiPanel, + EuiBadge, +} from '@elastic/eui'; +import React from 'react'; + +export function DefaultDiscoveryRule() { + return ( + + + + Exclude + + + Everything else + + + + ); +} diff --git a/x-pack/plugins/apm/public/components/fleet_integration/apm_agents/runtime_attachment/discovery_rule.tsx b/x-pack/plugins/apm/public/components/fleet_integration/apm_agents/runtime_attachment/discovery_rule.tsx new file mode 100644 index 0000000000000..f7b1b3db3a4c4 --- /dev/null +++ b/x-pack/plugins/apm/public/components/fleet_integration/apm_agents/runtime_attachment/discovery_rule.tsx @@ -0,0 +1,125 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ +import { i18n } from '@kbn/i18n'; +import { + EuiText, + EuiFlexGroup, + EuiFlexItem, + EuiIcon, + EuiBadge, + EuiPanel, + DraggableProvidedDragHandleProps, + EuiButtonIcon, +} from '@elastic/eui'; +import React, { useMemo } from 'react'; +import { Operation } from '.'; + +interface Props { + id: string; + order: number; + operation: string; + type: string; + probe: string; + providedDragHandleProps?: DraggableProvidedDragHandleProps; + onDelete: (discoveryItemId: string) => void; + onEdit: (discoveryItemId: string) => void; + operationTypes: Operation[]; +} + +export function DiscoveryRule({ + id, + order, + operation, + type, + probe, + providedDragHandleProps, + onDelete, + onEdit, + operationTypes, +}: Props) { + const operationTypesLabels = useMemo(() => { + return operationTypes.reduce<{ + [operationValue: string]: { + label: string; + types: { [typeValue: string]: string }; + }; + }>((acc, current) => { + return { + ...acc, + [current.operation.value]: { + label: current.operation.label, + types: current.types.reduce((memo, { value, label }) => { + return { ...memo, [value]: label }; + }, {}), + }, + }; + }, {}); + }, [operationTypes]); + return ( + + + +
+ +
+
+ + + + {order} + + + + {operationTypesLabels[operation].label} + + + + + + +

{operationTypesLabels[operation].types[type]}

+
+
+ + {probe} + +
+
+ + + + { + onEdit(id); + }} + /> + + + { + onDelete(id); + }} + /> + + + +
+
+
+
+ ); +} diff --git a/x-pack/plugins/apm/public/components/fleet_integration/apm_agents/runtime_attachment/edit_discovery_rule.tsx b/x-pack/plugins/apm/public/components/fleet_integration/apm_agents/runtime_attachment/edit_discovery_rule.tsx new file mode 100644 index 0000000000000..5059bbabfce91 --- /dev/null +++ b/x-pack/plugins/apm/public/components/fleet_integration/apm_agents/runtime_attachment/edit_discovery_rule.tsx @@ -0,0 +1,181 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { + EuiFlexGroup, + EuiFlexItem, + EuiPanel, + EuiButton, + EuiButtonEmpty, + EuiFormFieldset, + EuiSelect, + EuiFieldText, + EuiFormRow, + EuiSuperSelect, + EuiText, +} from '@elastic/eui'; +import { i18n } from '@kbn/i18n'; +import React from 'react'; +import { + Operation, + DISCOVERY_RULE_TYPE_ALL, + STAGED_DISCOVERY_RULE_ID, +} from '.'; + +interface Props { + id: string; + onChangeOperation: (discoveryItemId: string) => void; + operation: string; + onChangeType: (discoveryItemId: string) => void; + type: string; + onChangeProbe: (discoveryItemId: string) => void; + probe: string; + onCancel: () => void; + onSubmit: () => void; + operationTypes: Operation[]; +} + +export function EditDiscoveryRule({ + id, + onChangeOperation, + operation, + onChangeType, + type, + onChangeProbe, + probe, + onCancel, + onSubmit, + operationTypes, +}: Props) { + return ( + + + + + ({ + text: item.operation.label, + value: item.operation.value, + }))} + value={operation} + onChange={(e) => { + onChangeOperation(e.target.value); + }} + /> + + + + + + + + + definedOperation.value === operation + ) + ?.types.map((item) => ({ + inputDisplay: item.label, + value: item.value, + dropdownDisplay: ( + <> + {item.label} + +

{item.description}

+
+ + ), + })) ?? [] + } + valueOfSelected={type} + onChange={onChangeType} + /> +
+
+
+
+ {type !== DISCOVERY_RULE_TYPE_ALL && ( + + + + + onChangeProbe(e.target.value)} + /> + + + + + )} + + + Cancel + + + + {id === STAGED_DISCOVERY_RULE_ID + ? i18n.translate( + 'xpack.apm.fleetIntegration.apmAgent.runtimeAttachment.editRule.add', + { defaultMessage: 'Add' } + ) + : i18n.translate( + 'xpack.apm.fleetIntegration.apmAgent.runtimeAttachment.editRule.save', + { defaultMessage: 'Save' } + )} + + + +
+ ); +} diff --git a/x-pack/plugins/apm/public/components/fleet_integration/apm_agents/runtime_attachment/index.tsx b/x-pack/plugins/apm/public/components/fleet_integration/apm_agents/runtime_attachment/index.tsx new file mode 100644 index 0000000000000..8f2a1d3d1dea1 --- /dev/null +++ b/x-pack/plugins/apm/public/components/fleet_integration/apm_agents/runtime_attachment/index.tsx @@ -0,0 +1,327 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { + htmlIdGenerator, + euiDragDropReorder, + DropResult, + EuiComboBoxOptionOption, +} from '@elastic/eui'; +import React, { useState, useCallback, ReactNode } from 'react'; +import { RuntimeAttachment as RuntimeAttachmentStateless } from './runtime_attachment'; + +export const STAGED_DISCOVERY_RULE_ID = 'STAGED_DISCOVERY_RULE_ID'; +export const DISCOVERY_RULE_TYPE_ALL = 'all'; + +export interface IDiscoveryRule { + operation: string; + type: string; + probe: string; +} + +export type IDiscoveryRuleList = Array<{ + id: string; + discoveryRule: IDiscoveryRule; +}>; + +export interface RuntimeAttachmentSettings { + enabled: boolean; + discoveryRules: IDiscoveryRule[]; + version: string | null; +} + +interface Props { + onChange?: (runtimeAttachmentSettings: RuntimeAttachmentSettings) => void; + toggleDescription: ReactNode; + discoveryRulesDescription: ReactNode; + showUnsavedWarning?: boolean; + initialIsEnabled?: boolean; + initialDiscoveryRules?: IDiscoveryRule[]; + operationTypes: Operation[]; + selectedVersion: string; + versions: string[]; +} + +interface Option { + value: string; + label: string; + description?: string; +} + +export interface Operation { + operation: Option; + types: Option[]; +} + +const versionRegex = new RegExp(/^\d+\.\d+\.\d+$/); +function validateVersion(version: string) { + return versionRegex.test(version); +} + +export function RuntimeAttachment(props: Props) { + const { initialDiscoveryRules = [], onChange = () => {} } = props; + const [isEnabled, setIsEnabled] = useState(Boolean(props.initialIsEnabled)); + const [discoveryRuleList, setDiscoveryRuleList] = + useState( + initialDiscoveryRules.map((discoveryRule) => ({ + id: generateId(), + discoveryRule, + })) + ); + const [editDiscoveryRuleId, setEditDiscoveryRuleId] = useState( + null + ); + const [version, setVersion] = useState(props.selectedVersion); + const [versions, setVersions] = useState(props.versions); + const [isValidVersion, setIsValidVersion] = useState( + validateVersion(version) + ); + + const onToggleEnable = useCallback(() => { + const nextIsEnabled = !isEnabled; + setIsEnabled(nextIsEnabled); + onChange({ + enabled: nextIsEnabled, + discoveryRules: nextIsEnabled + ? discoveryRuleList.map(({ discoveryRule }) => discoveryRule) + : [], + version: nextIsEnabled ? version : null, + }); + }, [isEnabled, onChange, discoveryRuleList, version]); + + const onDelete = useCallback( + (discoveryRuleId: string) => { + const filteredDiscoveryRuleList = discoveryRuleList.filter( + ({ id }) => id !== discoveryRuleId + ); + setDiscoveryRuleList(filteredDiscoveryRuleList); + onChange({ + enabled: isEnabled, + discoveryRules: filteredDiscoveryRuleList.map( + ({ discoveryRule }) => discoveryRule + ), + version, + }); + }, + [isEnabled, discoveryRuleList, onChange, version] + ); + + const onEdit = useCallback( + (discoveryRuleId: string) => { + const editingDiscoveryRule = discoveryRuleList.find( + ({ id }) => id === discoveryRuleId + ); + if (editingDiscoveryRule) { + const { + discoveryRule: { operation, type, probe }, + } = editingDiscoveryRule; + setStagedOperationText(operation); + setStagedTypeText(type); + setStagedProbeText(probe); + setEditDiscoveryRuleId(discoveryRuleId); + } + }, + [discoveryRuleList] + ); + + const [stagedOperationText, setStagedOperationText] = useState(''); + const [stagedTypeText, setStagedTypeText] = useState(''); + const [stagedProbeText, setStagedProbeText] = useState(''); + + const onChangeOperation = useCallback( + (operationText: string) => { + setStagedOperationText(operationText); + const selectedOperationTypes = props.operationTypes.find( + ({ operation }) => operationText === operation.value + ); + const selectedTypeAvailable = selectedOperationTypes?.types.some( + ({ value }) => stagedTypeText === value + ); + if (!selectedTypeAvailable) { + setStagedTypeText(selectedOperationTypes?.types[0].value ?? ''); + } + }, + [props.operationTypes, stagedTypeText] + ); + + const onChangeType = useCallback((operationText: string) => { + setStagedTypeText(operationText); + if (operationText === DISCOVERY_RULE_TYPE_ALL) { + setStagedProbeText(''); + } + }, []); + + const onChangeProbe = useCallback((operationText: string) => { + setStagedProbeText(operationText); + }, []); + + const onCancel = useCallback(() => { + if (editDiscoveryRuleId === STAGED_DISCOVERY_RULE_ID) { + onDelete(STAGED_DISCOVERY_RULE_ID); + } + setEditDiscoveryRuleId(null); + }, [editDiscoveryRuleId, onDelete]); + + const onSubmit = useCallback(() => { + const editDiscoveryRuleIndex = discoveryRuleList.findIndex( + ({ id }) => id === editDiscoveryRuleId + ); + const editDiscoveryRule = discoveryRuleList[editDiscoveryRuleIndex]; + const nextDiscoveryRuleList = [ + ...discoveryRuleList.slice(0, editDiscoveryRuleIndex), + { + id: + editDiscoveryRule.id === STAGED_DISCOVERY_RULE_ID + ? generateId() + : editDiscoveryRule.id, + discoveryRule: { + operation: stagedOperationText, + type: stagedTypeText, + probe: stagedProbeText, + }, + }, + ...discoveryRuleList.slice(editDiscoveryRuleIndex + 1), + ]; + setDiscoveryRuleList(nextDiscoveryRuleList); + setEditDiscoveryRuleId(null); + onChange({ + enabled: isEnabled, + discoveryRules: nextDiscoveryRuleList.map( + ({ discoveryRule }) => discoveryRule + ), + version, + }); + }, [ + isEnabled, + editDiscoveryRuleId, + stagedOperationText, + stagedTypeText, + stagedProbeText, + discoveryRuleList, + onChange, + version, + ]); + + const onAddRule = useCallback(() => { + const firstOperationType = props.operationTypes[0]; + const operationText = firstOperationType.operation.value; + const typeText = firstOperationType.types[0].value; + const valueText = ''; + setStagedOperationText(operationText); + setStagedTypeText(typeText); + setStagedProbeText(valueText); + const nextDiscoveryRuleList = [ + { + id: STAGED_DISCOVERY_RULE_ID, + discoveryRule: { + operation: operationText, + type: typeText, + probe: valueText, + }, + }, + ...discoveryRuleList, + ]; + setDiscoveryRuleList(nextDiscoveryRuleList); + setEditDiscoveryRuleId(STAGED_DISCOVERY_RULE_ID); + }, [discoveryRuleList, props.operationTypes]); + + const onDragEnd = useCallback( + ({ source, destination }: DropResult) => { + if (source && destination) { + const nextDiscoveryRuleList = euiDragDropReorder( + discoveryRuleList, + source.index, + destination.index + ); + setDiscoveryRuleList(nextDiscoveryRuleList); + onChange({ + enabled: isEnabled, + discoveryRules: nextDiscoveryRuleList.map( + ({ discoveryRule }) => discoveryRule + ), + version, + }); + } + }, + [isEnabled, discoveryRuleList, onChange, version] + ); + + function onChangeVersion(nextVersion?: string) { + if (!nextVersion) { + return; + } + setVersion(nextVersion); + onChange({ + enabled: isEnabled, + discoveryRules: isEnabled + ? discoveryRuleList.map(({ discoveryRule }) => discoveryRule) + : [], + version: nextVersion, + }); + } + + function onCreateNewVersion( + newVersion: string, + flattenedOptions: Array> + ) { + const normalizedNewVersion = newVersion.trim().toLowerCase(); + const isNextVersionValid = validateVersion(normalizedNewVersion); + setIsValidVersion(isNextVersionValid); + if (!normalizedNewVersion || !isNextVersionValid) { + return; + } + + // Create the option if it doesn't exist. + if ( + flattenedOptions.findIndex( + (option) => option.label.trim().toLowerCase() === normalizedNewVersion + ) === -1 + ) { + setVersions([...versions, newVersion]); + } + + onChangeVersion(newVersion); + } + + return ( + { + const nextVersion: string | undefined = selectedVersions[0]?.label; + const isNextVersionValid = validateVersion(nextVersion); + setIsValidVersion(isNextVersionValid); + onChangeVersion(nextVersion); + }} + onCreateNewVersion={onCreateNewVersion} + isValidVersion={isValidVersion} + /> + ); +} + +const generateId = htmlIdGenerator(); diff --git a/x-pack/plugins/apm/public/components/fleet_integration/apm_agents/runtime_attachment/runtime_attachment.stories.tsx b/x-pack/plugins/apm/public/components/fleet_integration/apm_agents/runtime_attachment/runtime_attachment.stories.tsx new file mode 100644 index 0000000000000..12f6705284ff9 --- /dev/null +++ b/x-pack/plugins/apm/public/components/fleet_integration/apm_agents/runtime_attachment/runtime_attachment.stories.tsx @@ -0,0 +1,484 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { Meta, Story } from '@storybook/react'; +import React, { useState } from 'react'; +import { RuntimeAttachment } from '.'; +import { JavaRuntimeAttachment } from './supported_agents/java_runtime_attachment'; + +const stories: Meta<{}> = { + title: 'fleet/Runtime agent attachment', + component: RuntimeAttachment, + decorators: [ + (StoryComponent) => { + return ( +
+ +
+ ); + }, + ], +}; +export default stories; + +const excludeOptions = [ + { value: 'main', label: 'main class / jar name' }, + { value: 'vmarg', label: 'vmarg' }, + { value: 'user', label: 'user' }, +]; +const includeOptions = [{ value: 'all', label: 'All' }, ...excludeOptions]; + +const versions = ['1.27.1', '1.27.0', '1.26.0', '1.25.0']; + +export const RuntimeAttachmentExample: Story = () => { + const [runtimeAttachmentSettings, setRuntimeAttachmentSettings] = useState( + {} + ); + return ( + <> + { + setRuntimeAttachmentSettings(settings); + }} + toggleDescription="Attach the Java agent to running and starting Java applications." + discoveryRulesDescription="For every running JVM, the discovery rules are evaluated in the order they are provided. The first matching rule determines the outcome. Learn more in the docs" + showUnsavedWarning={true} + initialIsEnabled={true} + initialDiscoveryRules={[ + { + operation: 'include', + type: 'main', + probe: 'java-opbeans-10010', + }, + { + operation: 'exclude', + type: 'vmarg', + probe: '10948653898867', + }, + ]} + versions={versions} + selectedVersion={versions[0]} + /> +
+
{JSON.stringify(runtimeAttachmentSettings, null, 4)}
+ + ); +}; + +export const JavaRuntimeAttachmentExample: Story = () => { + return ( + {}} + /> + ); +}; + +const policy = { + id: 'cc380ec5-d84e-40e1-885a-d706edbdc968', + version: 'WzM0MzA2LDJd', + name: 'apm-1', + description: '', + namespace: 'default', + policy_id: 'policy-elastic-agent-on-cloud', + enabled: true, + output_id: '', + inputs: [ + { + type: 'apm', + policy_template: 'apmserver', + enabled: true, + streams: [], + vars: { + host: { + value: 'localhost:8200', + type: 'text', + }, + url: { + value: 'http://localhost:8200', + type: 'text', + }, + secret_token: { + type: 'text', + }, + api_key_enabled: { + value: false, + type: 'bool', + }, + enable_rum: { + value: true, + type: 'bool', + }, + anonymous_enabled: { + value: true, + type: 'bool', + }, + anonymous_allow_agent: { + value: ['rum-js', 'js-base', 'iOS/swift'], + type: 'text', + }, + anonymous_allow_service: { + value: [], + type: 'text', + }, + anonymous_rate_limit_event_limit: { + value: 10, + type: 'integer', + }, + anonymous_rate_limit_ip_limit: { + value: 10000, + type: 'integer', + }, + default_service_environment: { + type: 'text', + }, + rum_allow_origins: { + value: ['"*"'], + type: 'text', + }, + rum_allow_headers: { + value: [], + type: 'text', + }, + rum_response_headers: { + type: 'yaml', + }, + rum_library_pattern: { + value: '"node_modules|bower_components|~"', + type: 'text', + }, + rum_exclude_from_grouping: { + value: '"^/webpack"', + type: 'text', + }, + api_key_limit: { + value: 100, + type: 'integer', + }, + max_event_bytes: { + value: 307200, + type: 'integer', + }, + capture_personal_data: { + value: true, + type: 'bool', + }, + max_header_bytes: { + value: 1048576, + type: 'integer', + }, + idle_timeout: { + value: '45s', + type: 'text', + }, + read_timeout: { + value: '3600s', + type: 'text', + }, + shutdown_timeout: { + value: '30s', + type: 'text', + }, + write_timeout: { + value: '30s', + type: 'text', + }, + max_connections: { + value: 0, + type: 'integer', + }, + response_headers: { + type: 'yaml', + }, + expvar_enabled: { + value: false, + type: 'bool', + }, + tls_enabled: { + value: false, + type: 'bool', + }, + tls_certificate: { + type: 'text', + }, + tls_key: { + type: 'text', + }, + tls_supported_protocols: { + value: ['TLSv1.0', 'TLSv1.1', 'TLSv1.2'], + type: 'text', + }, + tls_cipher_suites: { + value: [], + type: 'text', + }, + tls_curve_types: { + value: [], + type: 'text', + }, + tail_sampling_policies: { + type: 'yaml', + }, + tail_sampling_interval: { + type: 'text', + }, + }, + config: { + 'apm-server': { + value: { + rum: { + source_mapping: { + metadata: [], + }, + }, + agent_config: [], + }, + }, + }, + compiled_input: { + 'apm-server': { + auth: { + anonymous: { + allow_agent: ['rum-js', 'js-base', 'iOS/swift'], + allow_service: null, + enabled: true, + rate_limit: { + event_limit: 10, + ip_limit: 10000, + }, + }, + api_key: { + enabled: false, + limit: 100, + }, + secret_token: null, + }, + capture_personal_data: true, + idle_timeout: '45s', + default_service_environment: null, + 'expvar.enabled': false, + host: 'localhost:8200', + max_connections: 0, + max_event_size: 307200, + max_header_size: 1048576, + read_timeout: '3600s', + response_headers: null, + rum: { + allow_headers: null, + allow_origins: ['*'], + enabled: true, + exclude_from_grouping: '^/webpack', + library_pattern: 'node_modules|bower_components|~', + response_headers: null, + }, + shutdown_timeout: '30s', + write_timeout: '30s', + }, + }, + }, + ], + package: { + name: 'apm', + title: 'Elastic APM', + version: '7.16.0', + }, + elasticsearch: { + privileges: { + cluster: ['cluster:monitor/main'], + }, + }, + revision: 1, + created_at: '2021-11-18T02:14:55.758Z', + created_by: 'admin', + updated_at: '2021-11-18T02:14:55.758Z', + updated_by: 'admin', +}; + +const newPolicy = { + version: 'WzM0MzA2LDJd', + name: 'apm-1', + description: '', + namespace: 'default', + policy_id: 'policy-elastic-agent-on-cloud', + enabled: true, + output_id: '', + package: { + name: 'apm', + title: 'Elastic APM', + version: '8.0.0-dev2', + }, + elasticsearch: { + privileges: { + cluster: ['cluster:monitor/main'], + }, + }, + inputs: [ + { + type: 'apm', + policy_template: 'apmserver', + enabled: true, + vars: { + host: { + value: 'localhost:8200', + type: 'text', + }, + url: { + value: 'http://localhost:8200', + type: 'text', + }, + secret_token: { + type: 'text', + }, + api_key_enabled: { + value: false, + type: 'bool', + }, + enable_rum: { + value: true, + type: 'bool', + }, + anonymous_enabled: { + value: true, + type: 'bool', + }, + anonymous_allow_agent: { + value: ['rum-js', 'js-base', 'iOS/swift'], + type: 'text', + }, + anonymous_allow_service: { + value: [], + type: 'text', + }, + anonymous_rate_limit_event_limit: { + value: 10, + type: 'integer', + }, + anonymous_rate_limit_ip_limit: { + value: 10000, + type: 'integer', + }, + default_service_environment: { + type: 'text', + }, + rum_allow_origins: { + value: ['"*"'], + type: 'text', + }, + rum_allow_headers: { + value: [], + type: 'text', + }, + rum_response_headers: { + type: 'yaml', + }, + rum_library_pattern: { + value: '"node_modules|bower_components|~"', + type: 'text', + }, + rum_exclude_from_grouping: { + value: '"^/webpack"', + type: 'text', + }, + api_key_limit: { + value: 100, + type: 'integer', + }, + max_event_bytes: { + value: 307200, + type: 'integer', + }, + capture_personal_data: { + value: true, + type: 'bool', + }, + max_header_bytes: { + value: 1048576, + type: 'integer', + }, + idle_timeout: { + value: '45s', + type: 'text', + }, + read_timeout: { + value: '3600s', + type: 'text', + }, + shutdown_timeout: { + value: '30s', + type: 'text', + }, + write_timeout: { + value: '30s', + type: 'text', + }, + max_connections: { + value: 0, + type: 'integer', + }, + response_headers: { + type: 'yaml', + }, + expvar_enabled: { + value: false, + type: 'bool', + }, + tls_enabled: { + value: false, + type: 'bool', + }, + tls_certificate: { + type: 'text', + }, + tls_key: { + type: 'text', + }, + tls_supported_protocols: { + value: ['TLSv1.0', 'TLSv1.1', 'TLSv1.2'], + type: 'text', + }, + tls_cipher_suites: { + value: [], + type: 'text', + }, + tls_curve_types: { + value: [], + type: 'text', + }, + tail_sampling_policies: { + type: 'yaml', + }, + tail_sampling_interval: { + type: 'text', + }, + }, + config: { + 'apm-server': { + value: { + rum: { + source_mapping: { + metadata: [], + }, + }, + agent_config: [], + }, + }, + }, + streams: [], + }, + ], +}; diff --git a/x-pack/plugins/apm/public/components/fleet_integration/apm_agents/runtime_attachment/runtime_attachment.tsx b/x-pack/plugins/apm/public/components/fleet_integration/apm_agents/runtime_attachment/runtime_attachment.tsx new file mode 100644 index 0000000000000..3592eb4f04745 --- /dev/null +++ b/x-pack/plugins/apm/public/components/fleet_integration/apm_agents/runtime_attachment/runtime_attachment.tsx @@ -0,0 +1,235 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { + EuiCallOut, + EuiSpacer, + EuiSwitch, + EuiText, + EuiFlexGroup, + EuiFlexItem, + EuiButton, + EuiDragDropContext, + EuiDroppable, + EuiDraggable, + EuiIcon, + DropResult, + EuiComboBox, + EuiComboBoxProps, + EuiFormRow, +} from '@elastic/eui'; +import React, { ReactNode } from 'react'; +import { i18n } from '@kbn/i18n'; +import { DiscoveryRule } from './discovery_rule'; +import { DefaultDiscoveryRule } from './default_discovery_rule'; +import { EditDiscoveryRule } from './edit_discovery_rule'; +import { IDiscoveryRuleList, Operation } from '.'; + +interface Props { + isEnabled: boolean; + onToggleEnable: () => void; + discoveryRuleList: IDiscoveryRuleList; + setDiscoveryRuleList: (discoveryRuleItems: IDiscoveryRuleList) => void; + onDelete: (discoveryItemId: string) => void; + editDiscoveryRuleId: null | string; + onEdit: (discoveryItemId: string) => void; + onChangeOperation: (operationText: string) => void; + stagedOperationText: string; + onChangeType: (typeText: string) => void; + stagedTypeText: string; + onChangeProbe: (probeText: string) => void; + stagedProbeText: string; + onCancel: () => void; + onSubmit: () => void; + onAddRule: () => void; + operationTypes: Operation[]; + toggleDescription: ReactNode; + discoveryRulesDescription: ReactNode; + showUnsavedWarning?: boolean; + onDragEnd: (dropResult: DropResult) => void; + selectedVersion: string; + versions: string[]; + onChangeVersion: EuiComboBoxProps['onChange']; + onCreateNewVersion: EuiComboBoxProps['onCreateOption']; + isValidVersion: boolean; +} + +export function RuntimeAttachment({ + isEnabled, + onToggleEnable, + discoveryRuleList, + setDiscoveryRuleList, + onDelete, + editDiscoveryRuleId, + onEdit, + onChangeOperation, + stagedOperationText, + onChangeType, + stagedTypeText, + onChangeProbe, + stagedProbeText, + onCancel, + onSubmit, + onAddRule, + operationTypes, + toggleDescription, + discoveryRulesDescription, + showUnsavedWarning, + onDragEnd, + selectedVersion, + versions, + onChangeVersion, + onCreateNewVersion, + isValidVersion, +}: Props) { + return ( +
+ {showUnsavedWarning && ( + <> + + + + )} + + + + + +

{toggleDescription}

+
+
+ {isEnabled && versions && ( + + + ({ label: _version }))} + onChange={onChangeVersion} + onCreateOption={onCreateNewVersion} + singleSelection + isClearable={false} + /> + + + )} +
+ {isEnabled && ( + <> + + +

+ {i18n.translate( + 'xpack.apm.fleetIntegration.apmAgent.runtimeAttachment.discoveryRules', + { defaultMessage: 'Discovery rules' } + )} +

+
+ + + + + + + +

{discoveryRulesDescription}

+
+
+ + + {i18n.translate( + 'xpack.apm.fleetIntegration.apmAgent.runtimeAttachment.addRule', + { defaultMessage: 'Add rule' } + )} + + +
+ + + + {discoveryRuleList.map(({ discoveryRule, id }, idx) => ( + + {(provided) => + id === editDiscoveryRuleId ? ( + + ) : ( + + ) + } + + ))} + + + + + )} + +
+ ); +} diff --git a/x-pack/plugins/apm/public/components/fleet_integration/apm_agents/runtime_attachment/supported_agents/java_runtime_attachment.tsx b/x-pack/plugins/apm/public/components/fleet_integration/apm_agents/runtime_attachment/supported_agents/java_runtime_attachment.tsx new file mode 100644 index 0000000000000..2284315d4a6ba --- /dev/null +++ b/x-pack/plugins/apm/public/components/fleet_integration/apm_agents/runtime_attachment/supported_agents/java_runtime_attachment.tsx @@ -0,0 +1,276 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import yaml from 'js-yaml'; +import { i18n } from '@kbn/i18n'; +import { FormattedMessage } from '@kbn/i18n-react'; +import React, { useCallback, useState, useMemo } from 'react'; +import { + RuntimeAttachment, + RuntimeAttachmentSettings, + IDiscoveryRule, +} from '..'; +import type { + NewPackagePolicy, + PackagePolicy, + PackagePolicyEditExtensionComponentProps, +} from '../../../apm_policy_form/typings'; + +interface Props { + policy: PackagePolicy; + newPolicy: NewPackagePolicy; + onChange: PackagePolicyEditExtensionComponentProps['onChange']; +} + +const excludeOptions = [ + { + value: 'main', + label: i18n.translate( + 'xpack.apm.fleetIntegration.javaRuntime.operationType.exclude.options.main', + { defaultMessage: 'main' } + ), + description: i18n.translate( + 'xpack.apm.fleetIntegration.javaRuntime.operationType.exclude.options.mainDescription', + { + defaultMessage: + 'A regular expression of fully qualified main class names or paths to JARs of applications the java agent should be attached to. Performs a partial match so that foo matches /bin/foo.jar.', + } + ), + }, + { + value: 'vmarg', + label: i18n.translate( + 'xpack.apm.fleetIntegration.javaRuntime.operationType.exclude.options.vmarg', + { defaultMessage: 'vmarg' } + ), + description: i18n.translate( + 'xpack.apm.fleetIntegration.javaRuntime.operationType.exclude.options.vmargDescription', + { + defaultMessage: + 'A regular expression matched against the arguments passed to the JVM, such as system properties. Performs a partial match so that attach=true matches the system property -Dattach=true.', + } + ), + }, + { + value: 'user', + label: i18n.translate( + 'xpack.apm.fleetIntegration.javaRuntime.operationType.exclude.options.user', + { defaultMessage: 'user' } + ), + description: i18n.translate( + 'xpack.apm.fleetIntegration.javaRuntime.operationType.exclude.options.userDescription', + { + defaultMessage: + 'A username that is matched against the operating system user that runs the JVM.', + } + ), + }, +]; +const includeOptions = [ + { + value: 'all', + label: i18n.translate( + 'xpack.apm.fleetIntegration.javaRuntime.operationType.include.options.all', + { defaultMessage: 'All' } + ), + description: i18n.translate( + 'xpack.apm.fleetIntegration.javaRuntime.operationType.include.options.allDescription', + { defaultMessage: 'Includes all JVMs for attachment.' } + ), + }, + ...excludeOptions, +]; + +const versions = [ + '1.27.1', + '1.27.0', + '1.26.0', + '1.25.0', + '1.24.0', + '1.23.0', + '1.22.0', + '1.21.0', + '1.20.0', + '1.19.0', + '1.18.1', + '1.18.0', + '1.18.0.RC1', + '1.17.0', + '1.16.0', + '1.15.0', + '1.14.0', + '1.13.0', + '1.12.0', + '1.11.0', + '1.10.0', + '1.9.0', + '1.8.0', + '1.7.0', + '1.6.1', + '1.6.0', + '1.5.0', + '1.4.0', + '1.3.0', + '1.2.0', +]; + +function getApmVars(newPolicy: NewPackagePolicy) { + return newPolicy.inputs.find(({ type }) => type === 'apm')?.vars; +} + +export function JavaRuntimeAttachment({ newPolicy, onChange }: Props) { + const [isDirty, setIsDirty] = useState(false); + const onChangePolicy = useCallback( + (runtimeAttachmentSettings: RuntimeAttachmentSettings) => { + const apmInputIdx = newPolicy.inputs.findIndex( + ({ type }) => type === 'apm' + ); + onChange({ + isValid: true, + updatedPolicy: { + ...newPolicy, + inputs: [ + ...newPolicy.inputs.slice(0, apmInputIdx), + { + ...newPolicy.inputs[apmInputIdx], + vars: { + ...newPolicy.inputs[apmInputIdx].vars, + java_attacher_enabled: { + value: runtimeAttachmentSettings.enabled, + type: 'bool', + }, + java_attacher_discovery_rules: { + type: 'yaml', + value: encodeDiscoveryRulesYaml( + runtimeAttachmentSettings.discoveryRules + ), + }, + java_attacher_agent_version: { + type: 'text', + value: runtimeAttachmentSettings.version, + }, + }, + }, + ...newPolicy.inputs.slice(apmInputIdx + 1), + ], + }, + }); + setIsDirty(true); + }, + [newPolicy, onChange] + ); + + const apmVars = useMemo(() => getApmVars(newPolicy), [newPolicy]); + + return ( + + {i18n.translate( + 'xpack.apm.fleetIntegration.javaRuntime.discoveryRulesDescription.docLink', + { defaultMessage: 'docs' } + )} + + ), + }} + /> + } + showUnsavedWarning={isDirty} + initialIsEnabled={apmVars?.java_attacher_enabled?.value} + initialDiscoveryRules={decodeDiscoveryRulesYaml( + apmVars?.java_attacher_discovery_rules?.value ?? '[]\n', + [initialDiscoveryRule] + )} + selectedVersion={ + apmVars?.java_attacher_agent_version?.value || versions[0] + } + versions={versions} + /> + ); +} + +const initialDiscoveryRule = { + operation: 'include', + type: 'vmarg', + probe: 'elastic.apm.attach=true', +}; + +type DiscoveryRulesParsedYaml = Array<{ [operationType: string]: string }>; + +function decodeDiscoveryRulesYaml( + discoveryRulesYaml: string, + defaultDiscoveryRules: IDiscoveryRule[] = [] +): IDiscoveryRule[] { + try { + const parsedYaml: DiscoveryRulesParsedYaml = + yaml.load(discoveryRulesYaml) ?? []; + + if (parsedYaml.length === 0) { + return defaultDiscoveryRules; + } + + // transform into array of discovery rules + return parsedYaml.map((discoveryRuleMap) => { + const [operationType, probe] = Object.entries(discoveryRuleMap)[0]; + return { + operation: operationType.split('-')[0], + type: operationType.split('-')[1], + probe, + }; + }); + } catch (error) { + return defaultDiscoveryRules; + } +} + +function encodeDiscoveryRulesYaml(discoveryRules: IDiscoveryRule[]): string { + // transform into list of key,value objects for expected yaml result + const mappedDiscoveryRules: DiscoveryRulesParsedYaml = discoveryRules.map( + ({ operation, type, probe }) => ({ + [`${operation}-${type}`]: probe, + }) + ); + return yaml.dump(mappedDiscoveryRules); +} diff --git a/x-pack/plugins/apm/public/components/routing/templates/settings_template.tsx b/x-pack/plugins/apm/public/components/routing/templates/settings_template.tsx index ec8366dfb36b4..229f34f7857ad 100644 --- a/x-pack/plugins/apm/public/components/routing/templates/settings_template.tsx +++ b/x-pack/plugins/apm/public/components/routing/templates/settings_template.tsx @@ -18,11 +18,11 @@ import { getLegacyApmHref } from '../../shared/Links/apm/APMLink'; type Tab = NonNullable[0] & { key: | 'agent-configurations' + | 'agent-keys' | 'anomaly-detection' | 'apm-indices' | 'customize-ui' - | 'schema' - | 'agent-keys'; + | 'schema'; hidden?: boolean; }; @@ -76,6 +76,17 @@ function getTabs({ search, }), }, + { + key: 'agent-keys', + label: i18n.translate('xpack.apm.settings.agentKeys', { + defaultMessage: 'Agent Keys', + }), + href: getLegacyApmHref({ + basePath, + path: `/settings/agent-keys`, + search, + }), + }, { key: 'anomaly-detection', label: i18n.translate('xpack.apm.settings.anomalyDetection', { @@ -117,17 +128,6 @@ function getTabs({ }), href: getLegacyApmHref({ basePath, path: `/settings/schema`, search }), }, - { - key: 'agent-keys', - label: i18n.translate('xpack.apm.settings.agentKeys', { - defaultMessage: 'Agent Keys', - }), - href: getLegacyApmHref({ - basePath, - path: `/settings/agent-keys`, - search, - }), - }, ]; return tabs diff --git a/x-pack/plugins/apm/public/components/shared/time_comparison/index.tsx b/x-pack/plugins/apm/public/components/shared/time_comparison/index.tsx index aa6d69c03d8f6..2cb4e0964686f 100644 --- a/x-pack/plugins/apm/public/components/shared/time_comparison/index.tsx +++ b/x-pack/plugins/apm/public/components/shared/time_comparison/index.tsx @@ -15,7 +15,7 @@ import { useUiTracker } from '../../../../../observability/public'; import { TimeRangeComparisonEnum } from '../../../../common/runtime_types/comparison_type_rt'; import { useLegacyUrlParams } from '../../../context/url_params_context/use_url_params'; import { useApmPluginContext } from '../../../context/apm_plugin/use_apm_plugin_context'; -import { useApmParams } from '../../../hooks/use_apm_params'; +import { useAnyOfApmParams } from '../../../hooks/use_apm_params'; import { useBreakpoints } from '../../../hooks/use_breakpoints'; import { useTimeRange } from '../../../hooks/use_time_range'; import * as urlHelpers from '../../shared/Links/url_helpers'; @@ -121,8 +121,7 @@ export function TimeComparison() { const { isSmall } = useBreakpoints(); const { query: { rangeFrom, rangeTo }, - // @ts-expect-error Type instantiation is excessively deep and possibly infinite. - } = useApmParams('/services', '/backends/*', '/services/{serviceName}'); + } = useAnyOfApmParams('/services', '/backends/*', '/services/{serviceName}'); const { exactStart, exactEnd } = useTimeRange({ rangeFrom, diff --git a/x-pack/plugins/apm/public/hooks/use_apm_params.ts b/x-pack/plugins/apm/public/hooks/use_apm_params.ts index 12b79ec7c90ae..b4c17c1b329ae 100644 --- a/x-pack/plugins/apm/public/hooks/use_apm_params.ts +++ b/x-pack/plugins/apm/public/hooks/use_apm_params.ts @@ -4,42 +4,29 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - +import { ValuesType } from 'utility-types'; import { TypeOf, PathsOf, useParams } from '@kbn/typed-react-router-config'; import { ApmRoutes } from '../components/routing/apm_route_config'; -export function useApmParams>( +// these three different functions exist purely to speed up completions from +// TypeScript. One overloaded function is expensive because of the size of the +// union type that is created. + +export function useMaybeApmParams>( path: TPath, optional: true -): TypeOf | undefined; +): TypeOf | undefined { + return useParams(path, optional); +} export function useApmParams>( path: TPath -): TypeOf; - -export function useApmParams< - TPath1 extends PathsOf, - TPath2 extends PathsOf ->( - path1: TPath1, - path2: TPath2 -): TypeOf | TypeOf; - -export function useApmParams< - TPath1 extends PathsOf, - TPath2 extends PathsOf, - TPath3 extends PathsOf ->( - path1: TPath1, - path2: TPath2, - path3: TPath3 -): - | TypeOf - | TypeOf - | TypeOf; +): TypeOf { + return useParams(path)!; +} -export function useApmParams( - ...args: any[] -): TypeOf> | undefined { - return useParams(...args); +export function useAnyOfApmParams>>( + ...paths: TPaths +): TypeOf> { + return useParams(...paths)!; } diff --git a/x-pack/plugins/apm/public/hooks/use_apm_router.ts b/x-pack/plugins/apm/public/hooks/use_apm_router.ts index d10b6da857802..dea66d7b2e1c8 100644 --- a/x-pack/plugins/apm/public/hooks/use_apm_router.ts +++ b/x-pack/plugins/apm/public/hooks/use_apm_router.ts @@ -14,6 +14,8 @@ export function useApmRouter() { const { core } = useApmPluginContext(); const link = (...args: [any]) => { + // @ts-expect-error router.link() expects never type, because + // no routes are specified. that's okay. return core.http.basePath.prepend('/app/apm' + router.link(...args)); }; diff --git a/x-pack/plugins/apm/public/hooks/use_current_user.ts b/x-pack/plugins/apm/public/hooks/use_current_user.ts new file mode 100644 index 0000000000000..6f7c999c01e86 --- /dev/null +++ b/x-pack/plugins/apm/public/hooks/use_current_user.ts @@ -0,0 +1,33 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { useState, useEffect } from 'react'; +import { useKibana } from '../../../../../src/plugins/kibana_react/public'; +import { ApmPluginStartDeps } from '../plugin'; +import { AuthenticatedUser } from '../../../security/common/model'; + +export function useCurrentUser() { + const { + services: { security }, + } = useKibana(); + + const [user, setUser] = useState(); + + useEffect(() => { + const getCurrentUser = async () => { + try { + const authenticatedUser = await security?.authc.getCurrentUser(); + setUser(authenticatedUser); + } catch { + setUser(undefined); + } + }; + getCurrentUser(); + }, [security?.authc]); + + return user; +} diff --git a/x-pack/plugins/apm/public/plugin.ts b/x-pack/plugins/apm/public/plugin.ts index 3a439df245609..d62cca4e07d45 100644 --- a/x-pack/plugins/apm/public/plugin.ts +++ b/x-pack/plugins/apm/public/plugin.ts @@ -54,6 +54,8 @@ import { getLazyApmAgentsTabExtension } from './components/fleet_integration/laz import { getLazyAPMPolicyCreateExtension } from './components/fleet_integration/lazy_apm_policy_create_extension'; import { getLazyAPMPolicyEditExtension } from './components/fleet_integration/lazy_apm_policy_edit_extension'; import { featureCatalogueEntry } from './featureCatalogueEntry'; +import type { SecurityPluginStart } from '../../security/public'; + export type ApmPluginSetup = ReturnType; export type ApmPluginStart = void; @@ -81,6 +83,7 @@ export interface ApmPluginStartDeps { triggersActionsUi: TriggersAndActionsUIPublicPluginStart; observability: ObservabilityPublicStart; fleet?: FleetStart; + security?: SecurityPluginStart; } const servicesTitle = i18n.translate('xpack.apm.navigation.servicesTitle', { diff --git a/x-pack/plugins/apm/server/deprecations/deprecations.test.ts b/x-pack/plugins/apm/server/deprecations/deprecations.test.ts index 11deff82de572..6b00c5cdd9a2b 100644 --- a/x-pack/plugins/apm/server/deprecations/deprecations.test.ts +++ b/x-pack/plugins/apm/server/deprecations/deprecations.test.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { kibanaPackageJson } from '@kbn/dev-utils'; +import { kibanaPackageJson } from '@kbn/utils'; import { GetDeprecationsContext } from '../../../../../src/core/server'; import { CloudSetup } from '../../../cloud/server'; diff --git a/x-pack/plugins/apm/server/index.ts b/x-pack/plugins/apm/server/index.ts index 416a873bac0a9..958bfb672083a 100644 --- a/x-pack/plugins/apm/server/index.ts +++ b/x-pack/plugins/apm/server/index.ts @@ -17,6 +17,7 @@ import { APMPlugin } from './plugin'; // All options should be documented in the APM configuration settings: https://github.com/elastic/kibana/blob/main/docs/settings/apm-settings.asciidoc // and be included on cloud allow list unless there are specific reasons not to const configSchema = schema.object({ + autoCreateApmDataView: schema.boolean({ defaultValue: true }), serviceMapEnabled: schema.boolean({ defaultValue: true }), serviceMapFingerprintBucketSize: schema.number({ defaultValue: 100 }), serviceMapTraceIdBucketSize: schema.number({ defaultValue: 65 }), @@ -25,7 +26,6 @@ const configSchema = schema.object({ }), serviceMapTraceIdGlobalBucketSize: schema.number({ defaultValue: 6 }), serviceMapMaxTracesPerRequest: schema.number({ defaultValue: 50 }), - autocreateApmIndexPattern: schema.boolean({ defaultValue: true }), ui: schema.object({ enabled: schema.boolean({ defaultValue: true }), transactionGroupBucketSize: schema.number({ defaultValue: 1000 }), @@ -59,7 +59,15 @@ const configSchema = schema.object({ // plugin config export const config: PluginConfigDescriptor = { - deprecations: ({ renameFromRoot, deprecateFromRoot, unusedFromRoot }) => [ + deprecations: ({ + rename, + renameFromRoot, + deprecateFromRoot, + unusedFromRoot, + }) => [ + rename('autocreateApmIndexPattern', 'autoCreateApmDataView', { + level: 'warning', + }), renameFromRoot( 'apm_oss.transactionIndices', 'xpack.apm.indices.transaction', diff --git a/x-pack/plugins/apm/server/lib/transaction_groups/__snapshots__/queries.test.ts.snap b/x-pack/plugins/apm/server/lib/transaction_groups/__snapshots__/queries.test.ts.snap deleted file mode 100644 index 00440b2b51853..0000000000000 --- a/x-pack/plugins/apm/server/lib/transaction_groups/__snapshots__/queries.test.ts.snap +++ /dev/null @@ -1,415 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`transaction group queries fetches metrics top traces 1`] = ` -Array [ - Object { - "apm": Object { - "events": Array [ - "metric", - ], - }, - "body": Object { - "aggs": Object { - "transaction_groups": Object { - "aggs": Object { - "transaction_type": Object { - "top_metrics": Object { - "metrics": Array [ - Object { - "field": "transaction.type", - }, - Object { - "field": "agent.name", - }, - ], - "sort": Object { - "@timestamp": "desc", - }, - }, - }, - }, - "composite": Object { - "size": 10000, - "sources": Array [ - Object { - "service.name": Object { - "terms": Object { - "field": "service.name", - }, - }, - }, - Object { - "transaction.name": Object { - "terms": Object { - "field": "transaction.name", - }, - }, - }, - ], - }, - }, - }, - "query": Object { - "bool": Object { - "filter": Array [ - Object { - "exists": Object { - "field": "transaction.duration.histogram", - }, - }, - Object { - "range": Object { - "@timestamp": Object { - "format": "epoch_millis", - "gte": 0, - "lte": 50000, - }, - }, - }, - Object { - "term": Object { - "transaction.root": true, - }, - }, - ], - "must_not": Array [], - }, - }, - "size": 0, - }, - }, - Object { - "apm": Object { - "events": Array [ - "metric", - ], - }, - "body": Object { - "aggs": Object { - "transaction_groups": Object { - "aggs": Object { - "avg": Object { - "avg": Object { - "field": "transaction.duration.histogram", - }, - }, - }, - "composite": Object { - "size": 10000, - "sources": Array [ - Object { - "service.name": Object { - "terms": Object { - "field": "service.name", - }, - }, - }, - Object { - "transaction.name": Object { - "terms": Object { - "field": "transaction.name", - }, - }, - }, - ], - }, - }, - }, - "query": Object { - "bool": Object { - "filter": Array [ - Object { - "exists": Object { - "field": "transaction.duration.histogram", - }, - }, - Object { - "range": Object { - "@timestamp": Object { - "format": "epoch_millis", - "gte": 0, - "lte": 50000, - }, - }, - }, - Object { - "term": Object { - "transaction.root": true, - }, - }, - ], - "must_not": Array [], - }, - }, - "size": 0, - }, - }, - Object { - "apm": Object { - "events": Array [ - "metric", - ], - }, - "body": Object { - "aggs": Object { - "transaction_groups": Object { - "aggs": Object { - "sum": Object { - "sum": Object { - "field": "transaction.duration.histogram", - }, - }, - }, - "composite": Object { - "size": 10000, - "sources": Array [ - Object { - "service.name": Object { - "terms": Object { - "field": "service.name", - }, - }, - }, - Object { - "transaction.name": Object { - "terms": Object { - "field": "transaction.name", - }, - }, - }, - ], - }, - }, - }, - "query": Object { - "bool": Object { - "filter": Array [ - Object { - "exists": Object { - "field": "transaction.duration.histogram", - }, - }, - Object { - "range": Object { - "@timestamp": Object { - "format": "epoch_millis", - "gte": 0, - "lte": 50000, - }, - }, - }, - Object { - "term": Object { - "transaction.root": true, - }, - }, - ], - "must_not": Array [], - }, - }, - "size": 0, - }, - }, -] -`; - -exports[`transaction group queries fetches top traces 1`] = ` -Array [ - Object { - "apm": Object { - "events": Array [ - "transaction", - ], - }, - "body": Object { - "aggs": Object { - "transaction_groups": Object { - "aggs": Object { - "transaction_type": Object { - "top_metrics": Object { - "metrics": Array [ - Object { - "field": "transaction.type", - }, - Object { - "field": "agent.name", - }, - ], - "sort": Object { - "@timestamp": "desc", - }, - }, - }, - }, - "composite": Object { - "size": 10000, - "sources": Array [ - Object { - "service.name": Object { - "terms": Object { - "field": "service.name", - }, - }, - }, - Object { - "transaction.name": Object { - "terms": Object { - "field": "transaction.name", - }, - }, - }, - ], - }, - }, - }, - "query": Object { - "bool": Object { - "filter": Array [ - Object { - "range": Object { - "@timestamp": Object { - "format": "epoch_millis", - "gte": 0, - "lte": 50000, - }, - }, - }, - ], - "must_not": Array [ - Object { - "exists": Object { - "field": "parent.id", - }, - }, - ], - }, - }, - "size": 0, - }, - }, - Object { - "apm": Object { - "events": Array [ - "transaction", - ], - }, - "body": Object { - "aggs": Object { - "transaction_groups": Object { - "aggs": Object { - "avg": Object { - "avg": Object { - "field": "transaction.duration.us", - }, - }, - }, - "composite": Object { - "size": 10000, - "sources": Array [ - Object { - "service.name": Object { - "terms": Object { - "field": "service.name", - }, - }, - }, - Object { - "transaction.name": Object { - "terms": Object { - "field": "transaction.name", - }, - }, - }, - ], - }, - }, - }, - "query": Object { - "bool": Object { - "filter": Array [ - Object { - "range": Object { - "@timestamp": Object { - "format": "epoch_millis", - "gte": 0, - "lte": 50000, - }, - }, - }, - ], - "must_not": Array [ - Object { - "exists": Object { - "field": "parent.id", - }, - }, - ], - }, - }, - "size": 0, - }, - }, - Object { - "apm": Object { - "events": Array [ - "transaction", - ], - }, - "body": Object { - "aggs": Object { - "transaction_groups": Object { - "aggs": Object { - "sum": Object { - "sum": Object { - "field": "transaction.duration.us", - }, - }, - }, - "composite": Object { - "size": 10000, - "sources": Array [ - Object { - "service.name": Object { - "terms": Object { - "field": "service.name", - }, - }, - }, - Object { - "transaction.name": Object { - "terms": Object { - "field": "transaction.name", - }, - }, - }, - ], - }, - }, - }, - "query": Object { - "bool": Object { - "filter": Array [ - Object { - "range": Object { - "@timestamp": Object { - "format": "epoch_millis", - "gte": 0, - "lte": 50000, - }, - }, - }, - ], - "must_not": Array [ - Object { - "exists": Object { - "field": "parent.id", - }, - }, - ], - }, - }, - "size": 0, - }, - }, -] -`; diff --git a/x-pack/plugins/apm/server/lib/transaction_groups/fetcher.ts b/x-pack/plugins/apm/server/lib/transaction_groups/fetcher.ts deleted file mode 100644 index bca71ed71b1f6..0000000000000 --- a/x-pack/plugins/apm/server/lib/transaction_groups/fetcher.ts +++ /dev/null @@ -1,222 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -import { QueryDslQueryContainer } from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; -import { sortBy } from 'lodash'; -import moment from 'moment'; -import { Unionize } from 'utility-types'; -import { AggregationOptionsByType } from '../../../../../../src/core/types/elasticsearch'; -import { - kqlQuery, - rangeQuery, - termQuery, -} from '../../../../observability/server'; -import { - PARENT_ID, - SERVICE_NAME, - TRANSACTION_NAME, - TRANSACTION_ROOT, -} from '../../../common/elasticsearch_fieldnames'; -import { asMutableArray } from '../../../common/utils/as_mutable_array'; -import { environmentQuery } from '../../../common/utils/environment_query'; -import { joinByKey } from '../../../common/utils/join_by_key'; -import { withApmSpan } from '../../utils/with_apm_span'; -import { - getDocumentTypeFilterForTransactions, - getProcessorEventForTransactions, -} from '../helpers/transactions'; -import { Setup } from '../helpers/setup_request'; -import { getAverages, getCounts, getSums } from './get_transaction_group_stats'; -import { AgentName } from '../../../typings/es_schemas/ui/fields/agent'; -export interface TopTraceOptions { - environment: string; - kuery: string; - transactionName?: string; - searchAggregatedTransactions: boolean; - start: number; - end: number; -} - -type Key = Record<'service.name' | 'transaction.name', string>; - -export interface TransactionGroup { - key: Key; - serviceName: string; - transactionName: string; - transactionType: string; - averageResponseTime: number | null | undefined; - transactionsPerMinute: number; - impact: number; - agentName: AgentName; -} - -export type ESResponse = Promise<{ items: TransactionGroup[] }>; - -export type TransactionGroupRequestBase = ReturnType & { - body: { - aggs: { - transaction_groups: Unionize>; - }; - }; -}; - -function getRequest(topTraceOptions: TopTraceOptions) { - const { - searchAggregatedTransactions, - environment, - kuery, - transactionName, - start, - end, - } = topTraceOptions; - - return { - apm: { - events: [getProcessorEventForTransactions(searchAggregatedTransactions)], - }, - body: { - size: 0, - query: { - bool: { - filter: [ - ...termQuery(TRANSACTION_NAME, transactionName), - ...getDocumentTypeFilterForTransactions( - searchAggregatedTransactions - ), - ...rangeQuery(start, end), - ...environmentQuery(environment), - ...kqlQuery(kuery), - ...(searchAggregatedTransactions - ? [ - { - term: { - [TRANSACTION_ROOT]: true, - }, - }, - ] - : []), - ] as QueryDslQueryContainer[], - must_not: [ - ...(!searchAggregatedTransactions - ? [ - { - exists: { - field: PARENT_ID, - }, - }, - ] - : []), - ], - }, - }, - aggs: { - transaction_groups: { - composite: { - sources: asMutableArray([ - { [SERVICE_NAME]: { terms: { field: SERVICE_NAME } } }, - { - [TRANSACTION_NAME]: { - terms: { field: TRANSACTION_NAME }, - }, - }, - ] as const), - // traces overview is hardcoded to 10000 - size: 10000, - }, - }, - }, - }, - }; -} - -export type TransactionGroupSetup = Setup; - -function getItemsWithRelativeImpact( - setup: TransactionGroupSetup, - items: Array<{ - sum?: number | null; - key: Key; - avg?: number | null; - count?: number | null; - transactionType?: string; - agentName?: AgentName; - }>, - start: number, - end: number -) { - const values = items - .map(({ sum }) => sum) - .filter((value) => value !== null) as number[]; - - const max = Math.max(...values); - const min = Math.min(...values); - - const duration = moment.duration(end - start); - const minutes = duration.asMinutes(); - - const itemsWithRelativeImpact = items.map((item) => { - return { - key: item.key, - averageResponseTime: item.avg, - transactionsPerMinute: (item.count ?? 0) / minutes, - transactionType: item.transactionType || '', - impact: - item.sum !== null && item.sum !== undefined - ? ((item.sum - min) / (max - min)) * 100 || 0 - : 0, - agentName: item.agentName as AgentName, - }; - }); - - return itemsWithRelativeImpact; -} - -export function topTransactionGroupsFetcher( - topTraceOptions: TopTraceOptions, - setup: TransactionGroupSetup -): Promise<{ items: TransactionGroup[] }> { - return withApmSpan('get_top_traces', async () => { - const request = getRequest(topTraceOptions); - - const params = { - request, - setup, - searchAggregatedTransactions: - topTraceOptions.searchAggregatedTransactions, - }; - - const [counts, averages, sums] = await Promise.all([ - getCounts(params), - getAverages(params), - getSums(params), - ]); - - const stats = [...averages, ...counts, ...sums]; - - const items = joinByKey(stats, 'key'); - - const { start, end } = topTraceOptions; - - const itemsWithRelativeImpact = getItemsWithRelativeImpact( - setup, - items, - start, - end - ); - - const itemsWithKeys = itemsWithRelativeImpact.map((item) => ({ - ...item, - transactionName: item.key[TRANSACTION_NAME], - serviceName: item.key[SERVICE_NAME], - })); - - return { - // sort by impact by default so most impactful services are not cut off - items: sortBy(itemsWithKeys, 'impact').reverse(), - }; - }); -} diff --git a/x-pack/plugins/apm/server/lib/transaction_groups/get_failed_transaction_rate.ts b/x-pack/plugins/apm/server/lib/transaction_groups/get_failed_transaction_rate.ts index b4f2c4b4bee11..4bd49f0db15e1 100644 --- a/x-pack/plugins/apm/server/lib/transaction_groups/get_failed_transaction_rate.ts +++ b/x-pack/plugins/apm/server/lib/transaction_groups/get_failed_transaction_rate.ts @@ -12,7 +12,6 @@ import { TRANSACTION_TYPE, } from '../../../common/elasticsearch_fieldnames'; import { EventOutcome } from '../../../common/event_outcome'; -import { offsetPreviousPeriodCoordinates } from '../../../common/utils/offset_previous_period_coordinate'; import { kqlQuery, rangeQuery, @@ -121,72 +120,3 @@ export async function getFailedTransactionRate({ return { timeseries, average }; } - -export async function getFailedTransactionRatePeriods({ - environment, - kuery, - serviceName, - transactionType, - transactionName, - setup, - searchAggregatedTransactions, - comparisonStart, - comparisonEnd, - start, - end, -}: { - environment: string; - kuery: string; - serviceName: string; - transactionType?: string; - transactionName?: string; - setup: Setup; - searchAggregatedTransactions: boolean; - comparisonStart?: number; - comparisonEnd?: number; - start: number; - end: number; -}) { - const commonProps = { - environment, - kuery, - serviceName, - transactionType, - transactionName, - setup, - searchAggregatedTransactions, - }; - - const currentPeriodPromise = getFailedTransactionRate({ - ...commonProps, - start, - end, - }); - - const previousPeriodPromise = - comparisonStart && comparisonEnd - ? getFailedTransactionRate({ - ...commonProps, - start: comparisonStart, - end: comparisonEnd, - }) - : { timeseries: [], average: null }; - - const [currentPeriod, previousPeriod] = await Promise.all([ - currentPeriodPromise, - previousPeriodPromise, - ]); - - const currentPeriodTimeseries = currentPeriod.timeseries; - - return { - currentPeriod, - previousPeriod: { - ...previousPeriod, - timeseries: offsetPreviousPeriodCoordinates({ - currentPeriodTimeseries, - previousPeriodTimeseries: previousPeriod.timeseries, - }), - }, - }; -} diff --git a/x-pack/plugins/apm/server/lib/transaction_groups/get_transaction_group_stats.ts b/x-pack/plugins/apm/server/lib/transaction_groups/get_transaction_group_stats.ts deleted file mode 100644 index 97dc298d11c56..0000000000000 --- a/x-pack/plugins/apm/server/lib/transaction_groups/get_transaction_group_stats.ts +++ /dev/null @@ -1,140 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -import { merge } from 'lodash'; -import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; -import { - AGENT_NAME, - TRANSACTION_TYPE, - TRANSACTION_NAME, - SERVICE_NAME, -} from '../../../common/elasticsearch_fieldnames'; -import { arrayUnionToCallable } from '../../../common/utils/array_union_to_callable'; -import { TransactionGroupRequestBase, TransactionGroupSetup } from './fetcher'; -import { getDurationFieldForTransactions } from '../helpers/transactions'; -import { AgentName } from '../../../typings/es_schemas/ui/fields/agent'; -interface MetricParams { - request: TransactionGroupRequestBase; - setup: TransactionGroupSetup; - searchAggregatedTransactions: boolean; -} - -type BucketKey = Record; - -function mergeRequestWithAggs< - TRequestBase extends TransactionGroupRequestBase, - TAggregationMap extends Record< - string, - estypes.AggregationsAggregationContainer - > ->(request: TRequestBase, aggs: TAggregationMap) { - return merge({}, request, { - body: { - aggs: { - transaction_groups: { - aggs, - }, - }, - }, - }); -} - -export async function getAverages({ - request, - setup, - searchAggregatedTransactions, -}: MetricParams) { - const params = mergeRequestWithAggs(request, { - avg: { - avg: { - field: getDurationFieldForTransactions(searchAggregatedTransactions), - }, - }, - }); - - const response = await setup.apmEventClient.search( - 'get_avg_transaction_group_duration', - params - ); - - return arrayUnionToCallable( - response.aggregations?.transaction_groups.buckets ?? [] - ).map((bucket) => { - return { - key: bucket.key as BucketKey, - avg: bucket.avg.value, - }; - }); -} - -export async function getCounts({ request, setup }: MetricParams) { - const params = mergeRequestWithAggs(request, { - transaction_type: { - top_metrics: { - sort: { - '@timestamp': 'desc' as const, - }, - metrics: [ - { - field: TRANSACTION_TYPE, - } as const, - { - field: AGENT_NAME, - } as const, - ], - }, - }, - }); - - const response = await setup.apmEventClient.search( - 'get_transaction_group_transaction_count', - params - ); - - return arrayUnionToCallable( - response.aggregations?.transaction_groups.buckets ?? [] - ).map((bucket) => { - return { - key: bucket.key as BucketKey, - count: bucket.doc_count, - transactionType: bucket.transaction_type.top[0].metrics[ - TRANSACTION_TYPE - ] as string, - agentName: bucket.transaction_type.top[0].metrics[ - AGENT_NAME - ] as AgentName, - }; - }); -} - -export async function getSums({ - request, - setup, - searchAggregatedTransactions, -}: MetricParams) { - const params = mergeRequestWithAggs(request, { - sum: { - sum: { - field: getDurationFieldForTransactions(searchAggregatedTransactions), - }, - }, - }); - - const response = await setup.apmEventClient.search( - 'get_transaction_group_latency_sums', - params - ); - - return arrayUnionToCallable( - response.aggregations?.transaction_groups.buckets ?? [] - ).map((bucket) => { - return { - key: bucket.key as BucketKey, - sum: bucket.sum.value, - }; - }); -} diff --git a/x-pack/plugins/apm/server/lib/transaction_groups/index.ts b/x-pack/plugins/apm/server/lib/transaction_groups/index.ts deleted file mode 100644 index bb16125ae8d09..0000000000000 --- a/x-pack/plugins/apm/server/lib/transaction_groups/index.ts +++ /dev/null @@ -1,16 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -import { Setup } from '../helpers/setup_request'; -import { topTransactionGroupsFetcher, TopTraceOptions } from './fetcher'; - -export async function getTopTransactionGroupList( - options: TopTraceOptions, - setup: Setup -) { - return await topTransactionGroupsFetcher(options, setup); -} diff --git a/x-pack/plugins/apm/server/lib/transaction_groups/mock_responses/transaction_groups_response.ts b/x-pack/plugins/apm/server/lib/transaction_groups/mock_responses/transaction_groups_response.ts deleted file mode 100644 index 1ec8d7cd76ca3..0000000000000 --- a/x-pack/plugins/apm/server/lib/transaction_groups/mock_responses/transaction_groups_response.ts +++ /dev/null @@ -1,2723 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -import { ESResponse } from '../fetcher'; - -export const transactionGroupsResponse = { - took: 139, - timed_out: false, - _shards: { total: 44, successful: 44, skipped: 0, failed: 0 }, - hits: { total: 131557, max_score: null, hits: [] }, - aggregations: { - transaction_groups: { - doc_count_error_upper_bound: 0, - sum_other_doc_count: 0, - buckets: [ - { - key: { transaction: 'POST /api/orders' }, - doc_count: 180, - avg: { value: 255966.30555555556 }, - p95: { values: { '95.0': 320238.5 } }, - sum: { value: 46073935 }, - sample: { - hits: { - total: 180, - max_score: null, - hits: [ - { - _index: 'apm-7.0.0-alpha1-2018.11.18', - _type: 'doc', - _id: 'TBGQKGcBVMxP8Wrugd8L', - _score: null, - _source: { - '@timestamp': '2018-11-18T20:43:32.010Z', - context: { - request: { - http_version: '1.1', - method: 'POST', - url: { - port: '3000', - pathname: '/api/orders', - full: 'http://opbeans-node:3000/api/orders', - raw: '/api/orders', - protocol: 'http:', - hostname: 'opbeans-node', - }, - socket: { - encrypted: false, - remote_address: '::ffff:172.18.0.10', - }, - headers: { - host: 'opbeans-node:3000', - accept: 'application/json', - 'content-type': 'application/json', - 'content-length': '129', - connection: 'close', - 'user-agent': 'workload/2.4.3', - }, - body: '[REDACTED]', - }, - response: { - status_code: 200, - headers: { - date: 'Sun, 18 Nov 2018 20:43:32 GMT', - connection: 'close', - 'x-powered-by': 'Express', - 'content-type': 'application/json; charset=utf-8', - 'content-length': '13', - etag: 'W/"d-g9K2iK4ordyN88lGL4LmPlYNfhc"', - }, - }, - system: { - ip: '172.18.0.10', - hostname: '98195610c255', - architecture: 'x64', - platform: 'linux', - }, - process: { - argv: [ - '/usr/local/bin/node', - '/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js', - ], - pid: 2413, - ppid: 1, - title: 'node /app/server.js', - }, - service: { - runtime: { name: 'node', version: '8.12.0' }, - name: 'opbeans-node', - agent: { name: 'nodejs', version: '1.14.2' }, - version: '1.0.0', - language: { name: 'javascript' }, - }, - user: { - id: '42', - username: 'kimchy', - email: 'kimchy@elastic.co', - }, - tags: { - lorem: - 'ipsum dolor sit amet, consectetur adipiscing elit. Nulla finibus, ipsum id scelerisque consequat, enim leo vulputate massa, vel ultricies ante neque ac risus. Curabitur tincidunt vitae sapien id pulvinar. Mauris eu vestibulum tortor. Integer sit amet lorem fringilla, egestas tellus vitae, vulputate purus. Nulla feugiat blandit nunc et semper. Morbi purus libero, mattis sed mauris non, euismod iaculis lacus. Curabitur eleifend ante eros, non faucibus velit lacinia id. Duis posuere libero augue, at dignissim urna consectetur eget. Praesent eu congue est, iaculis finibus augue.', - 'this-is-a-very-long-tag-name-without-any-spaces': - 'test', - 'multi-line': 'foo\nbar\nbaz', - foo: 'bar', - }, - custom: { containerId: 4669 }, - }, - trace: { id: '2b1252a338249daeecf6afb0c236e31b' }, - timestamp: { us: 1542573812010006 }, - agent: { - type: 'apm-server', - hostname: 'b359e3afece8', - version: '7.0.0-alpha1', - }, - host: { name: 'b359e3afece8' }, - processor: { - name: 'transaction', - event: 'transaction', - }, - transaction: { - sampled: true, - span_count: { started: 16 }, - id: '2c9f39e9ec4a0111', - name: 'POST /api/orders', - duration: { us: 291572 }, - type: 'request', - result: 'HTTP 2xx', - }, - }, - sort: [1542573812010], - }, - ], - }, - }, - }, - { - key: { transaction: 'GET /api' }, - doc_count: 21911, - avg: { value: 48021.972616494 }, - p95: { values: { '95.0': 67138.18364917398 } }, - sum: { value: 1052209442 }, - sample: { - hits: { - total: 21911, - max_score: null, - hits: [ - { - _index: 'apm-7.0.0-alpha1-2018.11.18', - _type: 'doc', - _id: '_hKZKGcBVMxP8Wru1G13', - _score: null, - _source: { - '@timestamp': '2018-11-18T20:53:44.070Z', - timestamp: { us: 1542574424070007 }, - agent: { - hostname: 'b359e3afece8', - version: '7.0.0-alpha1', - type: 'apm-server', - }, - host: { name: 'b359e3afece8' }, - processor: { - name: 'transaction', - event: 'transaction', - }, - transaction: { - sampled: true, - span_count: { started: 1 }, - id: 'a78bca581dcd8ff8', - name: 'GET /api', - duration: { us: 8684 }, - type: 'request', - result: 'HTTP 4xx', - }, - context: { - response: { - status_code: 404, - headers: { - 'content-type': 'application/json;charset=UTF-8', - 'transfer-encoding': 'chunked', - date: 'Sun, 18 Nov 2018 20:53:43 GMT', - connection: 'close', - 'x-powered-by': 'Express', - }, - }, - system: { - hostname: '98195610c255', - architecture: 'x64', - platform: 'linux', - ip: '172.18.0.10', - }, - process: { - argv: [ - '/usr/local/bin/node', - '/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js', - ], - pid: 3756, - ppid: 1, - title: 'node /app/server.js', - }, - service: { - runtime: { name: 'node', version: '8.12.0' }, - name: 'opbeans-node', - agent: { version: '1.14.2', name: 'nodejs' }, - version: '1.0.0', - language: { name: 'javascript' }, - }, - user: { - id: '42', - username: 'kimchy', - email: 'kimchy@elastic.co', - }, - tags: { - foo: 'bar', - lorem: - 'ipsum dolor sit amet, consectetur adipiscing elit. Nulla finibus, ipsum id scelerisque consequat, enim leo vulputate massa, vel ultricies ante neque ac risus. Curabitur tincidunt vitae sapien id pulvinar. Mauris eu vestibulum tortor. Integer sit amet lorem fringilla, egestas tellus vitae, vulputate purus. Nulla feugiat blandit nunc et semper. Morbi purus libero, mattis sed mauris non, euismod iaculis lacus. Curabitur eleifend ante eros, non faucibus velit lacinia id. Duis posuere libero augue, at dignissim urna consectetur eget. Praesent eu congue est, iaculis finibus augue.', - 'this-is-a-very-long-tag-name-without-any-spaces': - 'test', - 'multi-line': 'foo\nbar\nbaz', - }, - custom: { containerId: 5176 }, - request: { - method: 'GET', - url: { - protocol: 'http:', - hostname: 'opbeans-node', - port: '3000', - pathname: '/api/types/3', - full: 'http://opbeans-node:3000/api/types/3', - raw: '/api/types/3', - }, - socket: { - encrypted: false, - remote_address: '::ffff:172.18.0.6', - }, - headers: { - 'accept-encoding': 'gzip, deflate', - accept: '*/*', - connection: 'keep-alive', - 'elastic-apm-traceparent': - '00-86c68779d8a65b06fb78e770ffc436a5-4aaea53dc1791183-01', - host: 'opbeans-node:3000', - 'user-agent': 'python-requests/2.20.0', - }, - http_version: '1.1', - }, - }, - parent: { id: '4aaea53dc1791183' }, - trace: { id: '86c68779d8a65b06fb78e770ffc436a5' }, - }, - sort: [1542574424070], - }, - ], - }, - }, - }, - { - key: { transaction: 'GET /api/orders' }, - doc_count: 3247, - avg: { value: 33265.03326147213 }, - p95: { values: { '95.0': 58827.489999999976 } }, - sum: { value: 108011563 }, - sample: { - hits: { - total: 3247, - max_score: null, - hits: [ - { - _index: 'apm-7.0.0-alpha1-2018.11.18', - _type: 'doc', - _id: '6BKZKGcBVMxP8Wru1G13', - _score: null, - _source: { - '@timestamp': '2018-11-18T20:53:40.973Z', - timestamp: { us: 1542574420973006 }, - agent: { - type: 'apm-server', - hostname: 'b359e3afece8', - version: '7.0.0-alpha1', - }, - host: { name: 'b359e3afece8' }, - processor: { - name: 'transaction', - event: 'transaction', - }, - transaction: { - type: 'request', - result: 'HTTP 2xx', - sampled: true, - span_count: { started: 2 }, - id: '89f200353eb50539', - name: 'GET /api/orders', - duration: { us: 23040 }, - }, - context: { - user: { - username: 'kimchy', - email: 'kimchy@elastic.co', - id: '42', - }, - tags: { - foo: 'bar', - lorem: - 'ipsum dolor sit amet, consectetur adipiscing elit. Nulla finibus, ipsum id scelerisque consequat, enim leo vulputate massa, vel ultricies ante neque ac risus. Curabitur tincidunt vitae sapien id pulvinar. Mauris eu vestibulum tortor. Integer sit amet lorem fringilla, egestas tellus vitae, vulputate purus. Nulla feugiat blandit nunc et semper. Morbi purus libero, mattis sed mauris non, euismod iaculis lacus. Curabitur eleifend ante eros, non faucibus velit lacinia id. Duis posuere libero augue, at dignissim urna consectetur eget. Praesent eu congue est, iaculis finibus augue.', - 'this-is-a-very-long-tag-name-without-any-spaces': - 'test', - 'multi-line': 'foo\nbar\nbaz', - }, - custom: { containerId: 408 }, - request: { - method: 'GET', - url: { - full: 'http://opbeans-node:3000/api/orders', - raw: '/api/orders', - protocol: 'http:', - hostname: 'opbeans-node', - port: '3000', - pathname: '/api/orders', - }, - socket: { - remote_address: '::ffff:172.18.0.10', - encrypted: false, - }, - headers: { - 'user-agent': 'workload/2.4.3', - host: 'opbeans-node:3000', - connection: 'close', - }, - http_version: '1.1', - }, - response: { - status_code: 200, - headers: { - etag: 'W/"194bc-cOw6+iRf7XCeqMXHrle3IOig7tY"', - date: 'Sun, 18 Nov 2018 20:53:40 GMT', - connection: 'close', - 'x-powered-by': 'Express', - 'content-type': 'application/json; charset=utf-8', - 'content-length': '103612', - }, - }, - system: { - platform: 'linux', - ip: '172.18.0.10', - hostname: '98195610c255', - architecture: 'x64', - }, - process: { - title: 'node /app/server.js', - argv: [ - '/usr/local/bin/node', - '/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js', - ], - pid: 3756, - ppid: 1, - }, - service: { - version: '1.0.0', - language: { name: 'javascript' }, - runtime: { name: 'node', version: '8.12.0' }, - name: 'opbeans-node', - agent: { version: '1.14.2', name: 'nodejs' }, - }, - }, - trace: { id: '0afce85f593cbbdd09949936fe964f0f' }, - }, - sort: [1542574420973], - }, - ], - }, - }, - }, - { - key: { transaction: 'GET /log-message' }, - doc_count: 700, - avg: { value: 32900.72714285714 }, - p95: { values: { '95.0': 40444 } }, - sum: { value: 23030509 }, - sample: { - hits: { - total: 700, - max_score: null, - hits: [ - { - _index: 'apm-7.0.0-alpha1-2018.11.18', - _type: 'doc', - _id: 'qBKVKGcBVMxP8Wruqi_j', - _score: null, - _source: { - '@timestamp': '2018-11-18T20:49:09.225Z', - processor: { - name: 'transaction', - event: 'transaction', - }, - transaction: { - sampled: true, - span_count: { started: 0 }, - id: 'b9a8f96d7554d09f', - name: 'GET /log-message', - duration: { us: 32381 }, - type: 'request', - result: 'HTTP 5xx', - }, - context: { - user: { - id: '42', - username: 'kimchy', - email: 'kimchy@elastic.co', - }, - tags: { - lorem: - 'ipsum dolor sit amet, consectetur adipiscing elit. Nulla finibus, ipsum id scelerisque consequat, enim leo vulputate massa, vel ultricies ante neque ac risus. Curabitur tincidunt vitae sapien id pulvinar. Mauris eu vestibulum tortor. Integer sit amet lorem fringilla, egestas tellus vitae, vulputate purus. Nulla feugiat blandit nunc et semper. Morbi purus libero, mattis sed mauris non, euismod iaculis lacus. Curabitur eleifend ante eros, non faucibus velit lacinia id. Duis posuere libero augue, at dignissim urna consectetur eget. Praesent eu congue est, iaculis finibus augue.', - 'this-is-a-very-long-tag-name-without-any-spaces': - 'test', - 'multi-line': 'foo\nbar\nbaz', - foo: 'bar', - }, - custom: { containerId: 321 }, - request: { - socket: { - remote_address: '::ffff:172.18.0.10', - encrypted: false, - }, - headers: { - 'user-agent': 'workload/2.4.3', - host: 'opbeans-node:3000', - connection: 'close', - }, - http_version: '1.1', - method: 'GET', - url: { - raw: '/log-message', - protocol: 'http:', - hostname: 'opbeans-node', - port: '3000', - pathname: '/log-message', - full: 'http://opbeans-node:3000/log-message', - }, - }, - response: { - status_code: 500, - headers: { - 'x-powered-by': 'Express', - 'content-type': 'text/html; charset=utf-8', - 'content-length': '24', - etag: 'W/"18-MS3VbhH7auHMzO0fUuNF6v14N/M"', - date: 'Sun, 18 Nov 2018 20:49:09 GMT', - connection: 'close', - }, - }, - system: { - ip: '172.18.0.10', - hostname: '98195610c255', - architecture: 'x64', - platform: 'linux', - }, - process: { - title: 'node /app/server.js', - argv: [ - '/usr/local/bin/node', - '/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js', - ], - pid: 3142, - ppid: 1, - }, - service: { - language: { name: 'javascript' }, - runtime: { version: '8.12.0', name: 'node' }, - name: 'opbeans-node', - agent: { name: 'nodejs', version: '1.14.2' }, - version: '1.0.0', - }, - }, - trace: { id: 'ba18b741cdd3ac83eca89a5fede47577' }, - timestamp: { us: 1542574149225004 }, - agent: { - type: 'apm-server', - hostname: 'b359e3afece8', - version: '7.0.0-alpha1', - }, - host: { name: 'b359e3afece8' }, - }, - sort: [1542574149225], - }, - ], - }, - }, - }, - { - key: { transaction: 'GET /api/stats' }, - doc_count: 4639, - avg: { value: 32554.36257814184 }, - p95: { values: { '95.0': 59356.73611111111 } }, - sum: { value: 151019688 }, - sample: { - hits: { - total: 4639, - max_score: null, - hits: [ - { - _index: 'apm-7.0.0-alpha1-2018.11.18', - _type: 'doc', - _id: '9hKZKGcBVMxP8Wru1G13', - _score: null, - _source: { - '@timestamp': '2018-11-18T20:53:42.560Z', - trace: { id: '63ccc3b0929dafb7f2fbcabdc7f7af25' }, - timestamp: { us: 1542574422560002 }, - agent: { - hostname: 'b359e3afece8', - version: '7.0.0-alpha1', - type: 'apm-server', - }, - host: { name: 'b359e3afece8' }, - processor: { - name: 'transaction', - event: 'transaction', - }, - transaction: { - sampled: true, - span_count: { started: 7 }, - id: 'fb754e7628da2fb5', - name: 'GET /api/stats', - duration: { us: 28753 }, - type: 'request', - result: 'HTTP 3xx', - }, - context: { - response: { - headers: { - 'x-powered-by': 'Express', - etag: 'W/"77-uxKJrX5GSMJJWTKh3orUFAEVxSs"', - date: 'Sun, 18 Nov 2018 20:53:42 GMT', - connection: 'keep-alive', - }, - status_code: 304, - }, - system: { - ip: '172.18.0.10', - hostname: '98195610c255', - architecture: 'x64', - platform: 'linux', - }, - process: { - pid: 3756, - ppid: 1, - title: 'node /app/server.js', - argv: [ - '/usr/local/bin/node', - '/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js', - ], - }, - service: { - name: 'opbeans-node', - agent: { version: '1.14.2', name: 'nodejs' }, - version: '1.0.0', - language: { name: 'javascript' }, - runtime: { name: 'node', version: '8.12.0' }, - }, - user: { - email: 'kimchy@elastic.co', - id: '42', - username: 'kimchy', - }, - tags: { - 'multi-line': 'foo\nbar\nbaz', - foo: 'bar', - lorem: - 'ipsum dolor sit amet, consectetur adipiscing elit. Nulla finibus, ipsum id scelerisque consequat, enim leo vulputate massa, vel ultricies ante neque ac risus. Curabitur tincidunt vitae sapien id pulvinar. Mauris eu vestibulum tortor. Integer sit amet lorem fringilla, egestas tellus vitae, vulputate purus. Nulla feugiat blandit nunc et semper. Morbi purus libero, mattis sed mauris non, euismod iaculis lacus. Curabitur eleifend ante eros, non faucibus velit lacinia id. Duis posuere libero augue, at dignissim urna consectetur eget. Praesent eu congue est, iaculis finibus augue.', - 'this-is-a-very-long-tag-name-without-any-spaces': - 'test', - }, - custom: { containerId: 207 }, - request: { - url: { - protocol: 'http:', - hostname: 'opbeans-node', - port: '3000', - pathname: '/api/stats', - full: 'http://opbeans-node:3000/api/stats', - raw: '/api/stats', - }, - socket: { - remote_address: '::ffff:172.18.0.7', - encrypted: false, - }, - headers: { - 'if-none-match': 'W/"77-uxKJrX5GSMJJWTKh3orUFAEVxSs"', - host: 'opbeans-node:3000', - connection: 'keep-alive', - 'user-agent': 'Chromeless 1.4.0', - 'elastic-apm-traceparent': - '00-63ccc3b0929dafb7f2fbcabdc7f7af25-821a787e73ab1563-01', - accept: '*/*', - referer: 'http://opbeans-node:3000/dashboard', - 'accept-encoding': 'gzip, deflate', - }, - http_version: '1.1', - method: 'GET', - }, - }, - parent: { id: '821a787e73ab1563' }, - }, - sort: [1542574422560], - }, - ], - }, - }, - }, - { - key: { transaction: 'GET /log-error' }, - doc_count: 736, - avg: { value: 32387.73641304348 }, - p95: { values: { '95.0': 40061.1 } }, - sum: { value: 23837374 }, - sample: { - hits: { - total: 736, - max_score: null, - hits: [ - { - _index: 'apm-7.0.0-alpha1-2018.11.18', - _type: 'doc', - _id: 'rBKYKGcBVMxP8Wru9mC0', - _score: null, - _source: { - '@timestamp': '2018-11-18T20:52:51.462Z', - host: { name: 'b359e3afece8' }, - agent: { - type: 'apm-server', - hostname: 'b359e3afece8', - version: '7.0.0-alpha1', - }, - processor: { - name: 'transaction', - event: 'transaction', - }, - transaction: { - sampled: true, - span_count: { started: 0 }, - id: 'ec9c465c5042ded8', - name: 'GET /log-error', - duration: { us: 33367 }, - type: 'request', - result: 'HTTP 5xx', - }, - context: { - service: { - name: 'opbeans-node', - agent: { version: '1.14.2', name: 'nodejs' }, - version: '1.0.0', - language: { name: 'javascript' }, - runtime: { name: 'node', version: '8.12.0' }, - }, - user: { - id: '42', - username: 'kimchy', - email: 'kimchy@elastic.co', - }, - tags: { - 'multi-line': 'foo\nbar\nbaz', - foo: 'bar', - lorem: - 'ipsum dolor sit amet, consectetur adipiscing elit. Nulla finibus, ipsum id scelerisque consequat, enim leo vulputate massa, vel ultricies ante neque ac risus. Curabitur tincidunt vitae sapien id pulvinar. Mauris eu vestibulum tortor. Integer sit amet lorem fringilla, egestas tellus vitae, vulputate purus. Nulla feugiat blandit nunc et semper. Morbi purus libero, mattis sed mauris non, euismod iaculis lacus. Curabitur eleifend ante eros, non faucibus velit lacinia id. Duis posuere libero augue, at dignissim urna consectetur eget. Praesent eu congue est, iaculis finibus augue.', - 'this-is-a-very-long-tag-name-without-any-spaces': - 'test', - }, - custom: { containerId: 4877 }, - request: { - http_version: '1.1', - method: 'GET', - url: { - full: 'http://opbeans-node:3000/log-error', - raw: '/log-error', - protocol: 'http:', - hostname: 'opbeans-node', - port: '3000', - pathname: '/log-error', - }, - socket: { - remote_address: '::ffff:172.18.0.10', - encrypted: false, - }, - headers: { - 'user-agent': 'workload/2.4.3', - host: 'opbeans-node:3000', - connection: 'close', - }, - }, - response: { - headers: { - date: 'Sun, 18 Nov 2018 20:52:51 GMT', - connection: 'close', - 'x-powered-by': 'Express', - 'content-type': 'text/html; charset=utf-8', - 'content-length': '24', - etag: 'W/"18-MS3VbhH7auHMzO0fUuNF6v14N/M"', - }, - status_code: 500, - }, - system: { - architecture: 'x64', - platform: 'linux', - ip: '172.18.0.10', - hostname: '98195610c255', - }, - process: { - argv: [ - '/usr/local/bin/node', - '/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js', - ], - pid: 3659, - ppid: 1, - title: 'node /app/server.js', - }, - }, - trace: { id: '15366d65659b5fc8f67ff127391b3aff' }, - timestamp: { us: 1542574371462005 }, - }, - sort: [1542574371462], - }, - ], - }, - }, - }, - { - key: { transaction: 'GET /api/customers' }, - doc_count: 3366, - avg: { value: 32159.926322043968 }, - p95: { values: { '95.0': 59845.85714285714 } }, - sum: { value: 108250312 }, - sample: { - hits: { - total: 3366, - max_score: null, - hits: [ - { - _index: 'apm-7.0.0-alpha1-2018.11.18', - _type: 'doc', - _id: 'aRKZKGcBVMxP8Wruf2ly', - _score: null, - _source: { - '@timestamp': '2018-11-18T20:53:21.180Z', - transaction: { - sampled: true, - span_count: { started: 2 }, - id: '94852b9dd1075982', - name: 'GET /api/customers', - duration: { us: 18077 }, - type: 'request', - result: 'HTTP 2xx', - }, - context: { - user: { - id: '42', - username: 'kimchy', - email: 'kimchy@elastic.co', - }, - tags: { - foo: 'bar', - lorem: - 'ipsum dolor sit amet, consectetur adipiscing elit. Nulla finibus, ipsum id scelerisque consequat, enim leo vulputate massa, vel ultricies ante neque ac risus. Curabitur tincidunt vitae sapien id pulvinar. Mauris eu vestibulum tortor. Integer sit amet lorem fringilla, egestas tellus vitae, vulputate purus. Nulla feugiat blandit nunc et semper. Morbi purus libero, mattis sed mauris non, euismod iaculis lacus. Curabitur eleifend ante eros, non faucibus velit lacinia id. Duis posuere libero augue, at dignissim urna consectetur eget. Praesent eu congue est, iaculis finibus augue.', - 'this-is-a-very-long-tag-name-without-any-spaces': - 'test', - 'multi-line': 'foo\nbar\nbaz', - }, - custom: { containerId: 2531 }, - request: { - http_version: '1.1', - method: 'GET', - url: { - protocol: 'http:', - hostname: 'opbeans-node', - port: '3000', - pathname: '/api/customers', - full: 'http://opbeans-node:3000/api/customers', - raw: '/api/customers', - }, - socket: { - remote_address: '::ffff:172.18.0.6', - encrypted: false, - }, - headers: { - accept: '*/*', - connection: 'keep-alive', - 'elastic-apm-traceparent': - '00-541025da8ecc2f51f21c1a4ad6992b77-ca18d9d4c3879519-01', - host: 'opbeans-node:3000', - 'user-agent': 'python-requests/2.20.0', - 'accept-encoding': 'gzip, deflate', - }, - }, - response: { - status_code: 200, - headers: { - etag: 'W/"2d991-yG3J8W/roH7fSxXTudZrO27Ax9s"', - date: 'Sun, 18 Nov 2018 20:53:21 GMT', - connection: 'keep-alive', - 'x-powered-by': 'Express', - 'content-type': 'application/json; charset=utf-8', - 'content-length': '186769', - }, - }, - system: { - platform: 'linux', - ip: '172.18.0.10', - hostname: '98195610c255', - architecture: 'x64', - }, - process: { - title: 'node /app/server.js', - argv: [ - '/usr/local/bin/node', - '/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js', - ], - pid: 3710, - ppid: 1, - }, - service: { - version: '1.0.0', - language: { name: 'javascript' }, - runtime: { name: 'node', version: '8.12.0' }, - name: 'opbeans-node', - agent: { version: '1.14.2', name: 'nodejs' }, - }, - }, - parent: { id: 'ca18d9d4c3879519' }, - trace: { id: '541025da8ecc2f51f21c1a4ad6992b77' }, - timestamp: { us: 1542574401180002 }, - agent: { - type: 'apm-server', - hostname: 'b359e3afece8', - version: '7.0.0-alpha1', - }, - host: { name: 'b359e3afece8' }, - processor: { - name: 'transaction', - event: 'transaction', - }, - }, - sort: [1542574401180], - }, - ], - }, - }, - }, - { - key: { transaction: 'GET /api/products/top' }, - doc_count: 3694, - avg: { value: 27516.89144558744 }, - p95: { values: { '95.0': 56064.679999999986 } }, - sum: { value: 101647397 }, - sample: { - hits: { - total: 3694, - max_score: null, - hits: [ - { - _index: 'apm-7.0.0-alpha1-2018.11.18', - _type: 'doc', - _id: 'LhKZKGcBVMxP8WruHWMl', - _score: null, - _source: { - '@timestamp': '2018-11-18T20:52:57.316Z', - host: { name: 'b359e3afece8' }, - agent: { - type: 'apm-server', - hostname: 'b359e3afece8', - version: '7.0.0-alpha1', - }, - processor: { - name: 'transaction', - event: 'transaction', - }, - transaction: { - span_count: { started: 4 }, - id: 'be4bd5475d5d9e6f', - name: 'GET /api/products/top', - duration: { us: 48781 }, - type: 'request', - result: 'HTTP 2xx', - sampled: true, - }, - context: { - request: { - headers: { - host: 'opbeans-node:3000', - connection: 'keep-alive', - 'user-agent': 'Chromeless 1.4.0', - 'elastic-apm-traceparent': - '00-74f12e705936d66350f4741ebeb55189-fcebe94cd2136215-01', - accept: '*/*', - referer: 'http://opbeans-node:3000/dashboard', - 'accept-encoding': 'gzip, deflate', - }, - http_version: '1.1', - method: 'GET', - url: { - port: '3000', - pathname: '/api/products/top', - full: 'http://opbeans-node:3000/api/products/top', - raw: '/api/products/top', - protocol: 'http:', - hostname: 'opbeans-node', - }, - socket: { - remote_address: '::ffff:172.18.0.7', - encrypted: false, - }, - }, - response: { - status_code: 200, - headers: { - 'x-powered-by': 'Express', - 'content-type': 'application/json; charset=utf-8', - 'content-length': '282', - etag: 'W/"11a-lcI9zuMZYYsDRpEZgYqDYr96cKM"', - date: 'Sun, 18 Nov 2018 20:52:57 GMT', - connection: 'keep-alive', - }, - }, - system: { - hostname: '98195610c255', - architecture: 'x64', - platform: 'linux', - ip: '172.18.0.10', - }, - process: { - title: 'node /app/server.js', - argv: [ - '/usr/local/bin/node', - '/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js', - ], - pid: 3686, - ppid: 1, - }, - service: { - version: '1.0.0', - language: { name: 'javascript' }, - runtime: { name: 'node', version: '8.12.0' }, - name: 'opbeans-node', - agent: { name: 'nodejs', version: '1.14.2' }, - }, - user: { - username: 'kimchy', - email: 'kimchy@elastic.co', - id: '42', - }, - tags: { - foo: 'bar', - lorem: - 'ipsum dolor sit amet, consectetur adipiscing elit. Nulla finibus, ipsum id scelerisque consequat, enim leo vulputate massa, vel ultricies ante neque ac risus. Curabitur tincidunt vitae sapien id pulvinar. Mauris eu vestibulum tortor. Integer sit amet lorem fringilla, egestas tellus vitae, vulputate purus. Nulla feugiat blandit nunc et semper. Morbi purus libero, mattis sed mauris non, euismod iaculis lacus. Curabitur eleifend ante eros, non faucibus velit lacinia id. Duis posuere libero augue, at dignissim urna consectetur eget. Praesent eu congue est, iaculis finibus augue.', - 'this-is-a-very-long-tag-name-without-any-spaces': - 'test', - 'multi-line': 'foo\nbar\nbaz', - }, - custom: { containerId: 5113 }, - }, - parent: { id: 'fcebe94cd2136215' }, - trace: { id: '74f12e705936d66350f4741ebeb55189' }, - timestamp: { us: 1542574377316005 }, - }, - sort: [1542574377316], - }, - ], - }, - }, - }, - { - key: { transaction: 'POST /api' }, - doc_count: 147, - avg: { value: 21331.714285714286 }, - p95: { values: { '95.0': 30938 } }, - sum: { value: 3135762 }, - sample: { - hits: { - total: 147, - max_score: null, - hits: [ - { - _index: 'apm-7.0.0-alpha1-2018.11.18', - _type: 'doc', - _id: 'DhGDKGcBVMxP8WruzRXV', - _score: null, - _source: { - '@timestamp': '2018-11-18T20:29:42.751Z', - transaction: { - duration: { us: 21083 }, - type: 'request', - result: 'HTTP 4xx', - sampled: true, - span_count: { started: 1 }, - id: 'd67c2f7aa897110c', - name: 'POST /api', - }, - context: { - user: { - email: 'kimchy@elastic.co', - id: '42', - username: 'kimchy', - }, - tags: { - 'multi-line': 'foo\nbar\nbaz', - foo: 'bar', - lorem: - 'ipsum dolor sit amet, consectetur adipiscing elit. Nulla finibus, ipsum id scelerisque consequat, enim leo vulputate massa, vel ultricies ante neque ac risus. Curabitur tincidunt vitae sapien id pulvinar. Mauris eu vestibulum tortor. Integer sit amet lorem fringilla, egestas tellus vitae, vulputate purus. Nulla feugiat blandit nunc et semper. Morbi purus libero, mattis sed mauris non, euismod iaculis lacus. Curabitur eleifend ante eros, non faucibus velit lacinia id. Duis posuere libero augue, at dignissim urna consectetur eget. Praesent eu congue est, iaculis finibus augue.', - 'this-is-a-very-long-tag-name-without-any-spaces': - 'test', - }, - custom: { containerId: 2927 }, - request: { - url: { - raw: '/api/orders', - protocol: 'http:', - hostname: 'opbeans-node', - port: '3000', - pathname: '/api/orders', - full: 'http://opbeans-node:3000/api/orders', - }, - socket: { - encrypted: false, - remote_address: '::ffff:172.18.0.10', - }, - headers: { - accept: 'application/json', - 'content-type': 'application/json', - 'content-length': '129', - connection: 'close', - 'user-agent': 'workload/2.4.3', - host: 'opbeans-node:3000', - }, - body: '[REDACTED]', - http_version: '1.1', - method: 'POST', - }, - response: { - status_code: 400, - headers: { - 'x-powered-by': 'Express', - date: 'Sun, 18 Nov 2018 20:29:42 GMT', - 'content-length': '0', - connection: 'close', - }, - }, - system: { - hostname: '98195610c255', - architecture: 'x64', - platform: 'linux', - ip: '172.18.0.10', - }, - process: { - pid: 546, - ppid: 1, - title: 'node /app/server.js', - argv: [ - '/usr/local/bin/node', - '/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js', - ], - }, - service: { - agent: { name: 'nodejs', version: '1.14.2' }, - version: '1.0.0', - language: { name: 'javascript' }, - runtime: { name: 'node', version: '8.12.0' }, - name: 'opbeans-node', - }, - }, - trace: { id: '8ed4d94ec8fc11b1ea1b0aa59c2320ff' }, - timestamp: { us: 1542572982751005 }, - agent: { - version: '7.0.0-alpha1', - type: 'apm-server', - hostname: 'b359e3afece8', - }, - host: { name: 'b359e3afece8' }, - processor: { - name: 'transaction', - event: 'transaction', - }, - }, - sort: [1542572982751], - }, - ], - }, - }, - }, - { - key: { transaction: 'GET /api/products/:id/customers' }, - doc_count: 2102, - avg: { value: 17189.329210275926 }, - p95: { values: { '95.0': 39284.79999999999 } }, - sum: { value: 36131970 }, - sample: { - hits: { - total: 2102, - max_score: null, - hits: [ - { - _index: 'apm-7.0.0-alpha1-2018.11.18', - _type: 'doc', - _id: 'lhKVKGcBVMxP8WruDCUH', - _score: null, - _source: { - '@timestamp': '2018-11-18T20:48:24.769Z', - agent: { - type: 'apm-server', - hostname: 'b359e3afece8', - version: '7.0.0-alpha1', - }, - host: { name: 'b359e3afece8' }, - processor: { - event: 'transaction', - name: 'transaction', - }, - transaction: { - type: 'request', - result: 'HTTP 2xx', - sampled: true, - span_count: { started: 1 }, - id: '2a87ae20ad04ee0c', - name: 'GET /api/products/:id/customers', - duration: { us: 49338 }, - }, - context: { - user: { - id: '42', - username: 'kimchy', - email: 'kimchy@elastic.co', - }, - tags: { - lorem: - 'ipsum dolor sit amet, consectetur adipiscing elit. Nulla finibus, ipsum id scelerisque consequat, enim leo vulputate massa, vel ultricies ante neque ac risus. Curabitur tincidunt vitae sapien id pulvinar. Mauris eu vestibulum tortor. Integer sit amet lorem fringilla, egestas tellus vitae, vulputate purus. Nulla feugiat blandit nunc et semper. Morbi purus libero, mattis sed mauris non, euismod iaculis lacus. Curabitur eleifend ante eros, non faucibus velit lacinia id. Duis posuere libero augue, at dignissim urna consectetur eget. Praesent eu congue est, iaculis finibus augue.', - 'this-is-a-very-long-tag-name-without-any-spaces': - 'test', - 'multi-line': 'foo\nbar\nbaz', - foo: 'bar', - }, - custom: { containerId: 1735 }, - request: { - headers: { - accept: '*/*', - connection: 'keep-alive', - 'elastic-apm-traceparent': - '00-28f178c354d17f400dea04bc4a7b3c57-68f5d1607cac7779-01', - host: 'opbeans-node:3000', - 'user-agent': 'python-requests/2.20.0', - 'accept-encoding': 'gzip, deflate', - }, - http_version: '1.1', - method: 'GET', - url: { - port: '3000', - pathname: '/api/products/2/customers', - full: 'http://opbeans-node:3000/api/products/2/customers', - raw: '/api/products/2/customers', - protocol: 'http:', - hostname: 'opbeans-node', - }, - socket: { - remote_address: '::ffff:172.18.0.6', - encrypted: false, - }, - }, - response: { - status_code: 200, - headers: { - 'content-length': '186570', - etag: 'W/"2d8ca-Z9NzuHyGyxwtzpOkcIxBvzm24iw"', - date: 'Sun, 18 Nov 2018 20:48:24 GMT', - connection: 'keep-alive', - 'x-powered-by': 'Express', - 'content-type': 'application/json; charset=utf-8', - }, - }, - system: { - platform: 'linux', - ip: '172.18.0.10', - hostname: '98195610c255', - architecture: 'x64', - }, - process: { - argv: [ - '/usr/local/bin/node', - '/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js', - ], - pid: 3100, - ppid: 1, - title: 'node /app/server.js', - }, - service: { - language: { name: 'javascript' }, - runtime: { name: 'node', version: '8.12.0' }, - name: 'opbeans-node', - agent: { version: '1.14.2', name: 'nodejs' }, - version: '1.0.0', - }, - }, - parent: { id: '68f5d1607cac7779' }, - trace: { id: '28f178c354d17f400dea04bc4a7b3c57' }, - timestamp: { us: 1542574104769029 }, - }, - sort: [1542574104769], - }, - ], - }, - }, - }, - { - key: { transaction: 'GET /api/types/:id' }, - doc_count: 1449, - avg: { value: 12763.68806073154 }, - p95: { values: { '95.0': 30576.749999999996 } }, - sum: { value: 18494584 }, - sample: { - hits: { - total: 1449, - max_score: null, - hits: [ - { - _index: 'apm-7.0.0-alpha1-2018.11.18', - _type: 'doc', - _id: 'lxKZKGcBVMxP8WrurGuW', - _score: null, - _source: { - '@timestamp': '2018-11-18T20:53:35.967Z', - processor: { - name: 'transaction', - event: 'transaction', - }, - transaction: { - id: '053436abacdec0a4', - name: 'GET /api/types/:id', - duration: { us: 13064 }, - type: 'request', - result: 'HTTP 2xx', - sampled: true, - span_count: { started: 2 }, - }, - context: { - process: { - title: 'node /app/server.js', - argv: [ - '/usr/local/bin/node', - '/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js', - ], - pid: 3756, - ppid: 1, - }, - service: { - name: 'opbeans-node', - agent: { name: 'nodejs', version: '1.14.2' }, - version: '1.0.0', - language: { name: 'javascript' }, - runtime: { name: 'node', version: '8.12.0' }, - }, - user: { - id: '42', - username: 'kimchy', - email: 'kimchy@elastic.co', - }, - tags: { - foo: 'bar', - lorem: - 'ipsum dolor sit amet, consectetur adipiscing elit. Nulla finibus, ipsum id scelerisque consequat, enim leo vulputate massa, vel ultricies ante neque ac risus. Curabitur tincidunt vitae sapien id pulvinar. Mauris eu vestibulum tortor. Integer sit amet lorem fringilla, egestas tellus vitae, vulputate purus. Nulla feugiat blandit nunc et semper. Morbi purus libero, mattis sed mauris non, euismod iaculis lacus. Curabitur eleifend ante eros, non faucibus velit lacinia id. Duis posuere libero augue, at dignissim urna consectetur eget. Praesent eu congue est, iaculis finibus augue.', - 'this-is-a-very-long-tag-name-without-any-spaces': - 'test', - 'multi-line': 'foo\nbar\nbaz', - }, - custom: { containerId: 5345 }, - request: { - socket: { - remote_address: '::ffff:172.18.0.10', - encrypted: false, - }, - headers: { - 'user-agent': 'workload/2.4.3', - host: 'opbeans-node:3000', - connection: 'close', - }, - http_version: '1.1', - method: 'GET', - url: { - pathname: '/api/types/1', - full: 'http://opbeans-node:3000/api/types/1', - raw: '/api/types/1', - protocol: 'http:', - hostname: 'opbeans-node', - port: '3000', - }, - }, - response: { - status_code: 200, - headers: { - 'x-powered-by': 'Express', - 'content-type': 'application/json; charset=utf-8', - 'content-length': '217', - etag: 'W/"d9-cebOOHODBQMZd1wt+ZZBaSPgQLQ"', - date: 'Sun, 18 Nov 2018 20:53:35 GMT', - connection: 'close', - }, - }, - system: { - platform: 'linux', - ip: '172.18.0.10', - hostname: '98195610c255', - architecture: 'x64', - }, - }, - trace: { id: '2223b30b5cbaf2e221fcf70ac6d9abbe' }, - timestamp: { us: 1542574415967005 }, - host: { name: 'b359e3afece8' }, - agent: { - type: 'apm-server', - hostname: 'b359e3afece8', - version: '7.0.0-alpha1', - }, - }, - sort: [1542574415967], - }, - ], - }, - }, - }, - { - key: { transaction: 'GET /api/products' }, - doc_count: 3678, - avg: { value: 12683.190864600327 }, - p95: { values: { '95.0': 35009.67999999999 } }, - sum: { value: 46648776 }, - sample: { - hits: { - total: 3678, - max_score: null, - hits: [ - { - _index: 'apm-7.0.0-alpha1-2018.11.18', - _type: 'doc', - _id: '-hKZKGcBVMxP8Wru1G13', - _score: null, - _source: { - '@timestamp': '2018-11-18T20:53:43.477Z', - trace: { id: 'bee00a8efb523ca4b72adad57f7caba3' }, - timestamp: { us: 1542574423477006 }, - agent: { - type: 'apm-server', - hostname: 'b359e3afece8', - version: '7.0.0-alpha1', - }, - host: { name: 'b359e3afece8' }, - processor: { - name: 'transaction', - event: 'transaction', - }, - transaction: { - span_count: { started: 2 }, - id: 'd8fc6d3b8707b64c', - name: 'GET /api/products', - duration: { us: 6915 }, - type: 'request', - result: 'HTTP 2xx', - sampled: true, - }, - context: { - custom: { containerId: 2857 }, - request: { - headers: { - 'user-agent': 'workload/2.4.3', - host: 'opbeans-node:3000', - connection: 'close', - }, - http_version: '1.1', - method: 'GET', - url: { - full: 'http://opbeans-node:3000/api/products', - raw: '/api/products', - protocol: 'http:', - hostname: 'opbeans-node', - port: '3000', - pathname: '/api/products', - }, - socket: { - remote_address: '::ffff:172.18.0.10', - encrypted: false, - }, - }, - response: { - status_code: 200, - headers: { - connection: 'close', - 'x-powered-by': 'Express', - 'content-type': 'application/json; charset=utf-8', - 'content-length': '1023', - etag: 'W/"3ff-VyOxcDApb+a/lnjkm9FeTOGSDrs"', - date: 'Sun, 18 Nov 2018 20:53:43 GMT', - }, - }, - system: { - hostname: '98195610c255', - architecture: 'x64', - platform: 'linux', - ip: '172.18.0.10', - }, - process: { - pid: 3756, - ppid: 1, - title: 'node /app/server.js', - argv: [ - '/usr/local/bin/node', - '/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js', - ], - }, - service: { - runtime: { name: 'node', version: '8.12.0' }, - name: 'opbeans-node', - agent: { version: '1.14.2', name: 'nodejs' }, - version: '1.0.0', - language: { name: 'javascript' }, - }, - user: { - id: '42', - username: 'kimchy', - email: 'kimchy@elastic.co', - }, - tags: { - foo: 'bar', - lorem: - 'ipsum dolor sit amet, consectetur adipiscing elit. Nulla finibus, ipsum id scelerisque consequat, enim leo vulputate massa, vel ultricies ante neque ac risus. Curabitur tincidunt vitae sapien id pulvinar. Mauris eu vestibulum tortor. Integer sit amet lorem fringilla, egestas tellus vitae, vulputate purus. Nulla feugiat blandit nunc et semper. Morbi purus libero, mattis sed mauris non, euismod iaculis lacus. Curabitur eleifend ante eros, non faucibus velit lacinia id. Duis posuere libero augue, at dignissim urna consectetur eget. Praesent eu congue est, iaculis finibus augue.', - 'this-is-a-very-long-tag-name-without-any-spaces': - 'test', - 'multi-line': 'foo\nbar\nbaz', - }, - }, - }, - sort: [1542574423477], - }, - ], - }, - }, - }, - { - key: { transaction: 'GET /api/types' }, - doc_count: 2400, - avg: { value: 11257.757916666667 }, - p95: { values: { '95.0': 35222.944444444445 } }, - sum: { value: 27018619 }, - sample: { - hits: { - total: 2400, - max_score: null, - hits: [ - { - _index: 'apm-7.0.0-alpha1-2018.11.18', - _type: 'doc', - _id: '_xKZKGcBVMxP8Wru1G13', - _score: null, - _source: { - '@timestamp': '2018-11-18T20:53:44.978Z', - processor: { - name: 'transaction', - event: 'transaction', - }, - transaction: { - id: '0f10668e4fb3adc7', - name: 'GET /api/types', - duration: { us: 7891 }, - type: 'request', - result: 'HTTP 2xx', - sampled: true, - span_count: { started: 2 }, - }, - context: { - request: { - http_version: '1.1', - method: 'GET', - url: { - hostname: 'opbeans-node', - port: '3000', - pathname: '/api/types', - full: 'http://opbeans-node:3000/api/types', - raw: '/api/types', - protocol: 'http:', - }, - socket: { - remote_address: '::ffff:172.18.0.10', - encrypted: false, - }, - headers: { - connection: 'close', - 'user-agent': 'workload/2.4.3', - host: 'opbeans-node:3000', - }, - }, - response: { - status_code: 200, - headers: { - 'content-length': '112', - etag: 'W/"70-1z6hT7P1WHgBgS/BeUEVeHhOCQU"', - date: 'Sun, 18 Nov 2018 20:53:44 GMT', - connection: 'close', - 'x-powered-by': 'Express', - 'content-type': 'application/json; charset=utf-8', - }, - }, - system: { - hostname: '98195610c255', - architecture: 'x64', - platform: 'linux', - ip: '172.18.0.10', - }, - process: { - pid: 3756, - ppid: 1, - title: 'node /app/server.js', - argv: [ - '/usr/local/bin/node', - '/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js', - ], - }, - service: { - version: '1.0.0', - language: { name: 'javascript' }, - runtime: { name: 'node', version: '8.12.0' }, - name: 'opbeans-node', - agent: { version: '1.14.2', name: 'nodejs' }, - }, - user: { - email: 'kimchy@elastic.co', - id: '42', - username: 'kimchy', - }, - tags: { - foo: 'bar', - lorem: - 'ipsum dolor sit amet, consectetur adipiscing elit. Nulla finibus, ipsum id scelerisque consequat, enim leo vulputate massa, vel ultricies ante neque ac risus. Curabitur tincidunt vitae sapien id pulvinar. Mauris eu vestibulum tortor. Integer sit amet lorem fringilla, egestas tellus vitae, vulputate purus. Nulla feugiat blandit nunc et semper. Morbi purus libero, mattis sed mauris non, euismod iaculis lacus. Curabitur eleifend ante eros, non faucibus velit lacinia id. Duis posuere libero augue, at dignissim urna consectetur eget. Praesent eu congue est, iaculis finibus augue.', - 'this-is-a-very-long-tag-name-without-any-spaces': - 'test', - 'multi-line': 'foo\nbar\nbaz', - }, - custom: { containerId: 2193 }, - }, - trace: { id: '0d84126973411c19b470f2d9eea958d3' }, - timestamp: { us: 1542574424978005 }, - agent: { - type: 'apm-server', - hostname: 'b359e3afece8', - version: '7.0.0-alpha1', - }, - host: { name: 'b359e3afece8' }, - }, - sort: [1542574424978], - }, - ], - }, - }, - }, - { - key: { transaction: 'GET /api/orders/:id' }, - doc_count: 1283, - avg: { value: 10584.05144193297 }, - p95: { values: { '95.0': 26555.399999999998 } }, - sum: { value: 13579338 }, - sample: { - hits: { - total: 1283, - max_score: null, - hits: [ - { - _index: 'apm-7.0.0-alpha1-2018.11.18', - _type: 'doc', - _id: 'SRKXKGcBVMxP8Wru41Gf', - _score: null, - _source: { - '@timestamp': '2018-11-18T20:51:36.949Z', - context: { - tags: { - foo: 'bar', - lorem: - 'ipsum dolor sit amet, consectetur adipiscing elit. Nulla finibus, ipsum id scelerisque consequat, enim leo vulputate massa, vel ultricies ante neque ac risus. Curabitur tincidunt vitae sapien id pulvinar. Mauris eu vestibulum tortor. Integer sit amet lorem fringilla, egestas tellus vitae, vulputate purus. Nulla feugiat blandit nunc et semper. Morbi purus libero, mattis sed mauris non, euismod iaculis lacus. Curabitur eleifend ante eros, non faucibus velit lacinia id. Duis posuere libero augue, at dignissim urna consectetur eget. Praesent eu congue est, iaculis finibus augue.', - 'this-is-a-very-long-tag-name-without-any-spaces': - 'test', - 'multi-line': 'foo\nbar\nbaz', - }, - custom: { containerId: 5999 }, - request: { - socket: { - remote_address: '::ffff:172.18.0.10', - encrypted: false, - }, - headers: { - connection: 'close', - 'user-agent': 'workload/2.4.3', - host: 'opbeans-node:3000', - }, - http_version: '1.1', - method: 'GET', - url: { - raw: '/api/orders/183', - protocol: 'http:', - hostname: 'opbeans-node', - port: '3000', - pathname: '/api/orders/183', - full: 'http://opbeans-node:3000/api/orders/183', - }, - }, - response: { - headers: { - date: 'Sun, 18 Nov 2018 20:51:36 GMT', - connection: 'close', - 'content-length': '0', - 'x-powered-by': 'Express', - }, - status_code: 404, - }, - system: { - hostname: '98195610c255', - architecture: 'x64', - platform: 'linux', - ip: '172.18.0.10', - }, - process: { - pid: 3475, - ppid: 1, - title: 'node /app/server.js', - argv: [ - '/usr/local/bin/node', - '/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js', - ], - }, - service: { - agent: { name: 'nodejs', version: '1.14.2' }, - version: '1.0.0', - language: { name: 'javascript' }, - runtime: { name: 'node', version: '8.12.0' }, - name: 'opbeans-node', - }, - user: { - username: 'kimchy', - email: 'kimchy@elastic.co', - id: '42', - }, - }, - trace: { id: 'dab6421fa44a6869887e0edf32e1ad6f' }, - timestamp: { us: 1542574296949004 }, - agent: { - type: 'apm-server', - hostname: 'b359e3afece8', - version: '7.0.0-alpha1', - }, - host: { name: 'b359e3afece8' }, - processor: { - name: 'transaction', - event: 'transaction', - }, - transaction: { - span_count: { started: 1 }, - id: '937ef5588454f74a', - name: 'GET /api/orders/:id', - duration: { us: 5906 }, - type: 'request', - result: 'HTTP 4xx', - sampled: true, - }, - }, - sort: [1542574296949], - }, - ], - }, - }, - }, - { - key: { transaction: 'GET /api/products/:id' }, - doc_count: 1839, - avg: { value: 10548.218597063622 }, - p95: { values: { '95.0': 28413.383333333328 } }, - sum: { value: 19398174 }, - sample: { - hits: { - total: 1839, - max_score: null, - hits: [ - { - _index: 'apm-7.0.0-alpha1-2018.11.18', - _type: 'doc', - _id: 'OxKZKGcBVMxP8WruHWMl', - _score: null, - _source: { - '@timestamp': '2018-11-18T20:52:57.963Z', - agent: { - type: 'apm-server', - hostname: 'b359e3afece8', - version: '7.0.0-alpha1', - }, - host: { name: 'b359e3afece8' }, - processor: { - name: 'transaction', - event: 'transaction', - }, - transaction: { - span_count: { started: 1 }, - id: 'd324897ffb7ebcdc', - name: 'GET /api/products/:id', - duration: { us: 6959 }, - type: 'request', - result: 'HTTP 2xx', - sampled: true, - }, - context: { - service: { - name: 'opbeans-node', - agent: { version: '1.14.2', name: 'nodejs' }, - version: '1.0.0', - language: { name: 'javascript' }, - runtime: { version: '8.12.0', name: 'node' }, - }, - user: { - email: 'kimchy@elastic.co', - id: '42', - username: 'kimchy', - }, - tags: { - 'multi-line': 'foo\nbar\nbaz', - foo: 'bar', - lorem: - 'ipsum dolor sit amet, consectetur adipiscing elit. Nulla finibus, ipsum id scelerisque consequat, enim leo vulputate massa, vel ultricies ante neque ac risus. Curabitur tincidunt vitae sapien id pulvinar. Mauris eu vestibulum tortor. Integer sit amet lorem fringilla, egestas tellus vitae, vulputate purus. Nulla feugiat blandit nunc et semper. Morbi purus libero, mattis sed mauris non, euismod iaculis lacus. Curabitur eleifend ante eros, non faucibus velit lacinia id. Duis posuere libero augue, at dignissim urna consectetur eget. Praesent eu congue est, iaculis finibus augue.', - 'this-is-a-very-long-tag-name-without-any-spaces': - 'test', - }, - custom: { containerId: 7184 }, - request: { - socket: { - remote_address: '::ffff:172.18.0.10', - encrypted: false, - }, - headers: { - host: 'opbeans-node:3000', - connection: 'close', - 'user-agent': 'workload/2.4.3', - }, - http_version: '1.1', - method: 'GET', - url: { - port: '3000', - pathname: '/api/products/3', - full: 'http://opbeans-node:3000/api/products/3', - raw: '/api/products/3', - protocol: 'http:', - hostname: 'opbeans-node', - }, - }, - response: { - status_code: 200, - headers: { - 'x-powered-by': 'Express', - 'content-type': 'application/json; charset=utf-8', - 'content-length': '231', - etag: 'W/"e7-kkuzj37GZDzXDh0CWqh5Gan0VO4"', - date: 'Sun, 18 Nov 2018 20:52:57 GMT', - connection: 'close', - }, - }, - system: { - ip: '172.18.0.10', - hostname: '98195610c255', - architecture: 'x64', - platform: 'linux', - }, - process: { - pid: 3686, - ppid: 1, - title: 'node /app/server.js', - argv: [ - '/usr/local/bin/node', - '/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js', - ], - }, - }, - trace: { id: 'ca86ec845e412e4b4506a715d51548ec' }, - timestamp: { us: 1542574377963005 }, - }, - sort: [1542574377963], - }, - ], - }, - }, - }, - { - key: { transaction: 'GET /api/customers/:id' }, - doc_count: 1900, - avg: { value: 9868.217894736843 }, - p95: { values: { '95.0': 27486.5 } }, - sum: { value: 18749614 }, - sample: { - hits: { - total: 1900, - max_score: null, - hits: [ - { - _index: 'apm-7.0.0-alpha1-2018.11.18', - _type: 'doc', - _id: 'IhKZKGcBVMxP8WruHGPb', - _score: null, - _source: { - '@timestamp': '2018-11-18T20:52:56.797Z', - agent: { - hostname: 'b359e3afece8', - version: '7.0.0-alpha1', - type: 'apm-server', - }, - host: { name: 'b359e3afece8' }, - processor: { - name: 'transaction', - event: 'transaction', - }, - transaction: { - span_count: { started: 1 }, - id: '60e230d12f3f0960', - name: 'GET /api/customers/:id', - duration: { us: 9735 }, - type: 'request', - result: 'HTTP 2xx', - sampled: true, - }, - context: { - response: { - status_code: 200, - headers: { - connection: 'keep-alive', - 'x-powered-by': 'Express', - 'content-type': 'application/json; charset=utf-8', - 'content-length': '193', - etag: 'W/"c1-LbuhkuLzFyZ0H+7+JQGA5b0kvNs"', - date: 'Sun, 18 Nov 2018 20:52:56 GMT', - }, - }, - system: { - architecture: 'x64', - platform: 'linux', - ip: '172.18.0.10', - hostname: '98195610c255', - }, - process: { - ppid: 1, - title: 'node /app/server.js', - argv: [ - '/usr/local/bin/node', - '/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js', - ], - pid: 3686, - }, - service: { - name: 'opbeans-node', - agent: { name: 'nodejs', version: '1.14.2' }, - version: '1.0.0', - language: { name: 'javascript' }, - runtime: { name: 'node', version: '8.12.0' }, - }, - user: { - username: 'kimchy', - email: 'kimchy@elastic.co', - id: '42', - }, - tags: { - 'multi-line': 'foo\nbar\nbaz', - foo: 'bar', - lorem: - 'ipsum dolor sit amet, consectetur adipiscing elit. Nulla finibus, ipsum id scelerisque consequat, enim leo vulputate massa, vel ultricies ante neque ac risus. Curabitur tincidunt vitae sapien id pulvinar. Mauris eu vestibulum tortor. Integer sit amet lorem fringilla, egestas tellus vitae, vulputate purus. Nulla feugiat blandit nunc et semper. Morbi purus libero, mattis sed mauris non, euismod iaculis lacus. Curabitur eleifend ante eros, non faucibus velit lacinia id. Duis posuere libero augue, at dignissim urna consectetur eget. Praesent eu congue est, iaculis finibus augue.', - 'this-is-a-very-long-tag-name-without-any-spaces': - 'test', - }, - custom: { containerId: 8225 }, - request: { - headers: { - 'accept-encoding': 'gzip, deflate', - accept: '*/*', - connection: 'keep-alive', - 'elastic-apm-traceparent': - '00-e6140d30363f18b585f5d3b753f4d025-aa82e2c847265626-01', - host: 'opbeans-node:3000', - 'user-agent': 'python-requests/2.20.0', - }, - http_version: '1.1', - method: 'GET', - url: { - pathname: '/api/customers/700', - full: 'http://opbeans-node:3000/api/customers/700', - raw: '/api/customers/700', - protocol: 'http:', - hostname: 'opbeans-node', - port: '3000', - }, - socket: { - remote_address: '::ffff:172.18.0.6', - encrypted: false, - }, - }, - }, - parent: { id: 'aa82e2c847265626' }, - trace: { id: 'e6140d30363f18b585f5d3b753f4d025' }, - timestamp: { us: 1542574376797031 }, - }, - sort: [1542574376797], - }, - ], - }, - }, - }, - { - key: { transaction: 'POST unknown route' }, - doc_count: 20, - avg: { value: 5192.9 }, - p95: { values: { '95.0': 13230.5 } }, - sum: { value: 103858 }, - sample: { - hits: { - total: 20, - max_score: null, - hits: [ - { - _index: 'apm-7.0.0-alpha1-2018.11.18', - _type: 'doc', - _id: '4wsiKGcBVMxP8Wru2j59', - _score: null, - _source: { - '@timestamp': '2018-11-18T18:43:50.994Z', - host: { name: 'b359e3afece8' }, - processor: { - name: 'transaction', - event: 'transaction', - }, - transaction: { - sampled: true, - span_count: { started: 0 }, - id: '92c3ceea57899061', - name: 'POST unknown route', - duration: { us: 3467 }, - type: 'request', - result: 'HTTP 4xx', - }, - context: { - system: { - platform: 'linux', - ip: '172.18.0.10', - hostname: '98195610c255', - architecture: 'x64', - }, - process: { - pid: 19196, - ppid: 1, - title: 'node /app/server.js', - argv: [ - '/usr/local/bin/node', - '/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js', - ], - }, - service: { - name: 'opbeans-node', - agent: { version: '1.14.2', name: 'nodejs' }, - version: '1.0.0', - language: { name: 'javascript' }, - runtime: { name: 'node', version: '8.12.0' }, - }, - user: { - email: 'kimchy@elastic.co', - id: '42', - username: 'kimchy', - }, - tags: { - 'this-is-a-very-long-tag-name-without-any-spaces': - 'test', - 'multi-line': 'foo\nbar\nbaz', - foo: 'bar', - lorem: - 'ipsum dolor sit amet, consectetur adipiscing elit. Nulla finibus, ipsum id scelerisque consequat, enim leo vulputate massa, vel ultricies ante neque ac risus. Curabitur tincidunt vitae sapien id pulvinar. Mauris eu vestibulum tortor. Integer sit amet lorem fringilla, egestas tellus vitae, vulputate purus. Nulla feugiat blandit nunc et semper. Morbi purus libero, mattis sed mauris non, euismod iaculis lacus. Curabitur eleifend ante eros, non faucibus velit lacinia id. Duis posuere libero augue, at dignissim urna consectetur eget. Praesent eu congue est, iaculis finibus augue.', - }, - custom: { containerId: 6102 }, - request: { - method: 'POST', - url: { - raw: '/api/orders/csv', - protocol: 'http:', - hostname: '172.18.0.9', - port: '3000', - pathname: '/api/orders/csv', - full: 'http://172.18.0.9:3000/api/orders/csv', - }, - socket: { - remote_address: '::ffff:172.18.0.9', - encrypted: false, - }, - headers: { - 'accept-encoding': 'gzip, deflate', - 'content-type': - 'multipart/form-data; boundary=2b2e40be188a4cb5a56c05a0c182f6c9', - 'elastic-apm-traceparent': - '00-19688959ea6cbccda8013c11566ea329-1fc3665eef2dcdfc-01', - 'x-forwarded-for': '172.18.0.11', - host: '172.18.0.9:3000', - 'user-agent': 'Python/3.7 aiohttp/3.3.2', - 'content-length': '380', - accept: '*/*', - }, - body: '[REDACTED]', - http_version: '1.1', - }, - response: { - headers: { - date: 'Sun, 18 Nov 2018 18:43:50 GMT', - connection: 'keep-alive', - 'x-powered-by': 'Express', - 'content-security-policy': "default-src 'self'", - 'x-content-type-options': 'nosniff', - 'content-type': 'text/html; charset=utf-8', - 'content-length': '154', - }, - status_code: 404, - }, - }, - parent: { id: '1fc3665eef2dcdfc' }, - trace: { id: '19688959ea6cbccda8013c11566ea329' }, - timestamp: { us: 1542566630994005 }, - agent: { - version: '7.0.0-alpha1', - type: 'apm-server', - hostname: 'b359e3afece8', - }, - }, - sort: [1542566630994], - }, - ], - }, - }, - }, - { - key: { transaction: 'GET /is-it-coffee-time' }, - doc_count: 358, - avg: { value: 4694.005586592179 }, - p95: { values: { '95.0': 11022.99999999992 } }, - sum: { value: 1680454 }, - sample: { - hits: { - total: 358, - max_score: null, - hits: [ - { - _index: 'apm-7.0.0-alpha1-2018.11.18', - _type: 'doc', - _id: '7RKSKGcBVMxP8Wru-gjC', - _score: null, - _source: { - '@timestamp': '2018-11-18T20:46:19.317Z', - agent: { - type: 'apm-server', - hostname: 'b359e3afece8', - version: '7.0.0-alpha1', - }, - host: { name: 'b359e3afece8' }, - processor: { - name: 'transaction', - event: 'transaction', - }, - transaction: { - id: '319a5c555a1ab207', - name: 'GET /is-it-coffee-time', - duration: { us: 4253 }, - type: 'request', - result: 'HTTP 5xx', - sampled: true, - span_count: { started: 0 }, - }, - context: { - process: { - pid: 2760, - ppid: 1, - title: 'node /app/server.js', - argv: [ - '/usr/local/bin/node', - '/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js', - ], - }, - service: { - agent: { name: 'nodejs', version: '1.14.2' }, - version: '1.0.0', - language: { name: 'javascript' }, - runtime: { name: 'node', version: '8.12.0' }, - name: 'opbeans-node', - }, - user: { - email: 'kimchy@elastic.co', - id: '42', - username: 'kimchy', - }, - tags: { - foo: 'bar', - lorem: - 'ipsum dolor sit amet, consectetur adipiscing elit. Nulla finibus, ipsum id scelerisque consequat, enim leo vulputate massa, vel ultricies ante neque ac risus. Curabitur tincidunt vitae sapien id pulvinar. Mauris eu vestibulum tortor. Integer sit amet lorem fringilla, egestas tellus vitae, vulputate purus. Nulla feugiat blandit nunc et semper. Morbi purus libero, mattis sed mauris non, euismod iaculis lacus. Curabitur eleifend ante eros, non faucibus velit lacinia id. Duis posuere libero augue, at dignissim urna consectetur eget. Praesent eu congue est, iaculis finibus augue.', - 'this-is-a-very-long-tag-name-without-any-spaces': - 'test', - 'multi-line': 'foo\nbar\nbaz', - }, - custom: { containerId: 8593 }, - request: { - headers: { - 'user-agent': 'workload/2.4.3', - host: 'opbeans-node:3000', - connection: 'close', - }, - http_version: '1.1', - method: 'GET', - url: { - port: '3000', - pathname: '/is-it-coffee-time', - full: 'http://opbeans-node:3000/is-it-coffee-time', - raw: '/is-it-coffee-time', - protocol: 'http:', - hostname: 'opbeans-node', - }, - socket: { - remote_address: '::ffff:172.18.0.10', - encrypted: false, - }, - }, - response: { - status_code: 500, - headers: { - date: 'Sun, 18 Nov 2018 20:46:19 GMT', - connection: 'close', - 'x-powered-by': 'Express', - 'content-security-policy': "default-src 'self'", - 'x-content-type-options': 'nosniff', - 'content-type': 'text/html; charset=utf-8', - 'content-length': '148', - }, - }, - system: { - ip: '172.18.0.10', - hostname: '98195610c255', - architecture: 'x64', - platform: 'linux', - }, - }, - trace: { id: '821812b416de4c73ced87f8777fa46a6' }, - timestamp: { us: 1542573979317007 }, - }, - sort: [1542573979317], - }, - ], - }, - }, - }, - { - key: { transaction: 'GET /throw-error' }, - doc_count: 336, - avg: { value: 4549.889880952381 }, - p95: { values: { '95.0': 7719.700000000001 } }, - sum: { value: 1528763 }, - sample: { - hits: { - total: 336, - max_score: null, - hits: [ - { - _index: 'apm-7.0.0-alpha1-2018.11.18', - _type: 'doc', - _id: 'PhKTKGcBVMxP8WruwxSG', - _score: null, - _source: { - '@timestamp': '2018-11-18T20:47:10.714Z', - agent: { - version: '7.0.0-alpha1', - type: 'apm-server', - hostname: 'b359e3afece8', - }, - host: { name: 'b359e3afece8' }, - processor: { - name: 'transaction', - event: 'transaction', - }, - transaction: { - id: 'ecd187dc53f09fbd', - name: 'GET /throw-error', - duration: { us: 4458 }, - type: 'request', - result: 'HTTP 5xx', - sampled: true, - span_count: { started: 0 }, - }, - context: { - user: { - id: '42', - username: 'kimchy', - email: 'kimchy@elastic.co', - }, - tags: { - lorem: - 'ipsum dolor sit amet, consectetur adipiscing elit. Nulla finibus, ipsum id scelerisque consequat, enim leo vulputate massa, vel ultricies ante neque ac risus. Curabitur tincidunt vitae sapien id pulvinar. Mauris eu vestibulum tortor. Integer sit amet lorem fringilla, egestas tellus vitae, vulputate purus. Nulla feugiat blandit nunc et semper. Morbi purus libero, mattis sed mauris non, euismod iaculis lacus. Curabitur eleifend ante eros, non faucibus velit lacinia id. Duis posuere libero augue, at dignissim urna consectetur eget. Praesent eu congue est, iaculis finibus augue.', - 'this-is-a-very-long-tag-name-without-any-spaces': - 'test', - 'multi-line': 'foo\nbar\nbaz', - foo: 'bar', - }, - custom: { containerId: 7220 }, - request: { - http_version: '1.1', - method: 'GET', - url: { - port: '3000', - pathname: '/throw-error', - full: 'http://opbeans-node:3000/throw-error', - raw: '/throw-error', - protocol: 'http:', - hostname: 'opbeans-node', - }, - socket: { - remote_address: '::ffff:172.18.0.10', - encrypted: false, - }, - headers: { - 'user-agent': 'workload/2.4.3', - host: 'opbeans-node:3000', - connection: 'close', - }, - }, - response: { - status_code: 500, - headers: { - 'x-content-type-options': 'nosniff', - 'content-type': 'text/html; charset=utf-8', - 'content-length': '148', - date: 'Sun, 18 Nov 2018 20:47:10 GMT', - connection: 'close', - 'x-powered-by': 'Express', - 'content-security-policy': "default-src 'self'", - }, - }, - system: { - platform: 'linux', - ip: '172.18.0.10', - hostname: '98195610c255', - architecture: 'x64', - }, - process: { - title: 'node /app/server.js', - argv: [ - '/usr/local/bin/node', - '/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js', - ], - pid: 2895, - ppid: 1, - }, - service: { - name: 'opbeans-node', - agent: { version: '1.14.2', name: 'nodejs' }, - version: '1.0.0', - language: { name: 'javascript' }, - runtime: { name: 'node', version: '8.12.0' }, - }, - }, - trace: { id: '6c0ef23e1f963f304ce440a909914d35' }, - timestamp: { us: 1542574030714012 }, - }, - sort: [1542574030714], - }, - ], - }, - }, - }, - { - key: { transaction: 'GET *' }, - doc_count: 7115, - avg: { value: 3504.5108924806746 }, - p95: { values: { '95.0': 11431.738095238095 } }, - sum: { value: 24934595 }, - sample: { - hits: { - total: 7115, - max_score: null, - hits: [ - { - _index: 'apm-7.0.0-alpha1-2018.11.18', - _type: 'doc', - _id: '6hKZKGcBVMxP8Wru1G13', - _score: null, - _source: { - '@timestamp': '2018-11-18T20:53:42.493Z', - agent: { - type: 'apm-server', - hostname: 'b359e3afece8', - version: '7.0.0-alpha1', - }, - host: { name: 'b359e3afece8' }, - processor: { - name: 'transaction', - event: 'transaction', - }, - transaction: { - span_count: { started: 0 }, - id: 'f5fc4621949b63fb', - name: 'GET *', - duration: { us: 1901 }, - type: 'request', - result: 'HTTP 3xx', - sampled: true, - }, - context: { - request: { - http_version: '1.1', - method: 'GET', - url: { - hostname: 'opbeans-node', - port: '3000', - pathname: '/dashboard', - full: 'http://opbeans-node:3000/dashboard', - raw: '/dashboard', - protocol: 'http:', - }, - socket: { - remote_address: '::ffff:172.18.0.7', - encrypted: false, - }, - headers: { - accept: - 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', - 'accept-encoding': 'gzip, deflate', - 'if-none-match': 'W/"280-1670775e878"', - 'if-modified-since': 'Mon, 12 Nov 2018 10:27:07 GMT', - host: 'opbeans-node:3000', - connection: 'keep-alive', - 'upgrade-insecure-requests': '1', - 'user-agent': 'Chromeless 1.4.0', - }, - }, - response: { - status_code: 304, - headers: { - 'x-powered-by': 'Express', - 'accept-ranges': 'bytes', - 'cache-control': 'public, max-age=0', - 'last-modified': 'Mon, 12 Nov 2018 10:27:07 GMT', - etag: 'W/"280-1670775e878"', - date: 'Sun, 18 Nov 2018 20:53:42 GMT', - connection: 'keep-alive', - }, - }, - system: { - hostname: '98195610c255', - architecture: 'x64', - platform: 'linux', - ip: '172.18.0.10', - }, - process: { - title: 'node /app/server.js', - argv: [ - '/usr/local/bin/node', - '/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js', - ], - pid: 3756, - ppid: 1, - }, - service: { - version: '1.0.0', - language: { name: 'javascript' }, - runtime: { name: 'node', version: '8.12.0' }, - name: 'opbeans-node', - agent: { version: '1.14.2', name: 'nodejs' }, - }, - user: { - email: 'kimchy@elastic.co', - id: '42', - username: 'kimchy', - }, - tags: { - 'multi-line': 'foo\nbar\nbaz', - foo: 'bar', - lorem: - 'ipsum dolor sit amet, consectetur adipiscing elit. Nulla finibus, ipsum id scelerisque consequat, enim leo vulputate massa, vel ultricies ante neque ac risus. Curabitur tincidunt vitae sapien id pulvinar. Mauris eu vestibulum tortor. Integer sit amet lorem fringilla, egestas tellus vitae, vulputate purus. Nulla feugiat blandit nunc et semper. Morbi purus libero, mattis sed mauris non, euismod iaculis lacus. Curabitur eleifend ante eros, non faucibus velit lacinia id. Duis posuere libero augue, at dignissim urna consectetur eget. Praesent eu congue est, iaculis finibus augue.', - 'this-is-a-very-long-tag-name-without-any-spaces': - 'test', - }, - custom: { containerId: 6446 }, - }, - trace: { id: '7efb6ade88cdea20cd96ca482681cde7' }, - timestamp: { us: 1542574422493006 }, - }, - sort: [1542574422493], - }, - ], - }, - }, - }, - { - key: { transaction: 'OPTIONS unknown route' }, - doc_count: 364, - avg: { value: 2742.4615384615386 }, - p95: { values: { '95.0': 4370.000000000002 } }, - sum: { value: 998256 }, - sample: { - hits: { - total: 364, - max_score: null, - hits: [ - { - _index: 'apm-7.0.0-alpha1-2018.11.18', - _type: 'doc', - _id: '-xKVKGcBVMxP8WrucSs2', - _score: null, - _source: { - '@timestamp': '2018-11-18T20:49:00.707Z', - timestamp: { us: 1542574140707006 }, - agent: { - type: 'apm-server', - hostname: 'b359e3afece8', - version: '7.0.0-alpha1', - }, - host: { name: 'b359e3afece8' }, - processor: { - name: 'transaction', - event: 'transaction', - }, - transaction: { - span_count: { started: 0 }, - id: 'a8c87ebc7ec68bc0', - name: 'OPTIONS unknown route', - duration: { us: 2371 }, - type: 'request', - result: 'HTTP 2xx', - sampled: true, - }, - context: { - user: { - id: '42', - username: 'kimchy', - email: 'kimchy@elastic.co', - }, - tags: { - 'this-is-a-very-long-tag-name-without-any-spaces': - 'test', - 'multi-line': 'foo\nbar\nbaz', - foo: 'bar', - lorem: - 'ipsum dolor sit amet, consectetur adipiscing elit. Nulla finibus, ipsum id scelerisque consequat, enim leo vulputate massa, vel ultricies ante neque ac risus. Curabitur tincidunt vitae sapien id pulvinar. Mauris eu vestibulum tortor. Integer sit amet lorem fringilla, egestas tellus vitae, vulputate purus. Nulla feugiat blandit nunc et semper. Morbi purus libero, mattis sed mauris non, euismod iaculis lacus. Curabitur eleifend ante eros, non faucibus velit lacinia id. Duis posuere libero augue, at dignissim urna consectetur eget. Praesent eu congue est, iaculis finibus augue.', - }, - custom: { containerId: 3775 }, - request: { - socket: { - remote_address: '::ffff:172.18.0.10', - encrypted: false, - }, - headers: { - 'user-agent': 'workload/2.4.3', - host: 'opbeans-node:3000', - 'content-length': '0', - connection: 'close', - }, - http_version: '1.1', - method: 'OPTIONS', - url: { - port: '3000', - pathname: '/', - full: 'http://opbeans-node:3000/', - raw: '/', - protocol: 'http:', - hostname: 'opbeans-node', - }, - }, - response: { - status_code: 200, - headers: { - 'content-type': 'text/html; charset=utf-8', - 'content-length': '8', - etag: 'W/"8-ZRAf8oNBS3Bjb/SU2GYZCmbtmXg"', - date: 'Sun, 18 Nov 2018 20:49:00 GMT', - connection: 'close', - 'x-powered-by': 'Express', - allow: 'GET,HEAD', - }, - }, - system: { - ip: '172.18.0.10', - hostname: '98195610c255', - architecture: 'x64', - platform: 'linux', - }, - process: { - ppid: 1, - title: 'node /app/server.js', - argv: [ - '/usr/local/bin/node', - '/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js', - ], - pid: 3142, - }, - service: { - agent: { name: 'nodejs', version: '1.14.2' }, - version: '1.0.0', - language: { name: 'javascript' }, - runtime: { name: 'node', version: '8.12.0' }, - name: 'opbeans-node', - }, - }, - trace: { id: '469e3e5f91ffe3195a8e58cdd1cdefa8' }, - }, - sort: [1542574140707], - }, - ], - }, - }, - }, - { - key: { transaction: 'GET static file' }, - doc_count: 62606, - avg: { value: 2651.8784461553205 }, - p95: { values: { '95.0': 6140.579335038363 } }, - sum: { value: 166023502 }, - sample: { - hits: { - total: 62606, - max_score: null, - hits: [ - { - _index: 'apm-7.0.0-alpha1-2018.11.18', - _type: 'doc', - _id: '-RKZKGcBVMxP8Wru1G13', - _score: null, - _source: { - '@timestamp': '2018-11-18T20:53:43.304Z', - context: { - system: { - platform: 'linux', - ip: '172.18.0.10', - hostname: '98195610c255', - architecture: 'x64', - }, - process: { - title: 'node /app/server.js', - argv: [ - '/usr/local/bin/node', - '/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js', - ], - pid: 3756, - ppid: 1, - }, - service: { - name: 'opbeans-node', - agent: { name: 'nodejs', version: '1.14.2' }, - version: '1.0.0', - language: { name: 'javascript' }, - runtime: { name: 'node', version: '8.12.0' }, - }, - request: { - headers: { - 'user-agent': 'curl/7.38.0', - host: 'opbeans-node:3000', - accept: '*/*', - }, - http_version: '1.1', - method: 'GET', - url: { - pathname: '/', - full: 'http://opbeans-node:3000/', - raw: '/', - protocol: 'http:', - hostname: 'opbeans-node', - port: '3000', - }, - socket: { - encrypted: false, - remote_address: '::ffff:172.18.0.10', - }, - }, - response: { - status_code: 200, - headers: { - 'content-length': '640', - 'accept-ranges': 'bytes', - 'cache-control': 'public, max-age=0', - etag: 'W/"280-1670775e878"', - 'x-powered-by': 'Express', - 'last-modified': 'Mon, 12 Nov 2018 10:27:07 GMT', - 'content-type': 'text/html; charset=UTF-8', - date: 'Sun, 18 Nov 2018 20:53:43 GMT', - connection: 'keep-alive', - }, - }, - }, - trace: { id: 'b303d2a4a007946b63b9db7fafe639a0' }, - timestamp: { us: 1542574423304006 }, - agent: { - type: 'apm-server', - hostname: 'b359e3afece8', - version: '7.0.0-alpha1', - }, - host: { name: 'b359e3afece8' }, - processor: { - name: 'transaction', - event: 'transaction', - }, - transaction: { - span_count: { started: 0 }, - id: '2869c13633534be5', - name: 'GET static file', - duration: { us: 1801 }, - type: 'request', - result: 'HTTP 2xx', - sampled: true, - }, - }, - sort: [1542574423304], - }, - ], - }, - }, - }, - { - key: { transaction: 'GET unknown route' }, - doc_count: 7487, - avg: { value: 1422.926672899693 }, - p95: { values: { '95.0': 2311.885238095238 } }, - sum: { value: 10653452 }, - sample: { - hits: { - total: 7487, - max_score: null, - hits: [ - { - _index: 'apm-7.0.0-alpha1-2018.11.18', - _type: 'doc', - _id: '6xKZKGcBVMxP8Wru1G13', - _score: null, - _source: { - '@timestamp': '2018-11-18T20:53:42.504Z', - processor: { - name: 'transaction', - event: 'transaction', - }, - transaction: { - name: 'GET unknown route', - duration: { us: 911 }, - type: 'request', - result: 'HTTP 2xx', - sampled: true, - span_count: { started: 0 }, - id: '107881ae2be1b56d', - }, - context: { - system: { - hostname: '98195610c255', - architecture: 'x64', - platform: 'linux', - ip: '172.18.0.10', - }, - process: { - pid: 3756, - ppid: 1, - title: 'node /app/server.js', - argv: [ - '/usr/local/bin/node', - '/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js', - ], - }, - service: { - agent: { version: '1.14.2', name: 'nodejs' }, - version: '1.0.0', - language: { name: 'javascript' }, - runtime: { name: 'node', version: '8.12.0' }, - name: 'opbeans-node', - }, - request: { - http_version: '1.1', - method: 'GET', - url: { - full: 'http://opbeans-node:3000/rum-config.js', - raw: '/rum-config.js', - protocol: 'http:', - hostname: 'opbeans-node', - port: '3000', - pathname: '/rum-config.js', - }, - socket: { - remote_address: '::ffff:172.18.0.7', - encrypted: false, - }, - headers: { - connection: 'keep-alive', - 'user-agent': 'Chromeless 1.4.0', - accept: '*/*', - referer: 'http://opbeans-node:3000/dashboard', - 'accept-encoding': 'gzip, deflate', - host: 'opbeans-node:3000', - }, - }, - response: { - headers: { - 'x-powered-by': 'Express', - 'content-type': 'text/javascript', - 'content-length': '172', - date: 'Sun, 18 Nov 2018 20:53:42 GMT', - connection: 'keep-alive', - }, - status_code: 200, - }, - }, - trace: { id: '4399e7233e6e7b77e70c2fff111b8f28' }, - timestamp: { us: 1542574422504004 }, - agent: { - type: 'apm-server', - hostname: 'b359e3afece8', - version: '7.0.0-alpha1', - }, - host: { name: 'b359e3afece8' }, - }, - sort: [1542574422504], - }, - ], - }, - }, - }, - ], - }, - }, -} as unknown as ESResponse; diff --git a/x-pack/plugins/apm/server/lib/transaction_groups/queries.test.ts b/x-pack/plugins/apm/server/lib/transaction_groups/queries.test.ts deleted file mode 100644 index fd42ffe42788f..0000000000000 --- a/x-pack/plugins/apm/server/lib/transaction_groups/queries.test.ts +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -import { topTransactionGroupsFetcher } from './fetcher'; -import { - SearchParamsMock, - inspectSearchParams, -} from '../../utils/test_helpers'; -import { ENVIRONMENT_ALL } from '../../../common/environment_filter_values'; - -describe('transaction group queries', () => { - let mock: SearchParamsMock; - - afterEach(() => { - mock.teardown(); - }); - - it('fetches top traces', async () => { - mock = await inspectSearchParams((setup) => - topTransactionGroupsFetcher( - { - searchAggregatedTransactions: false, - environment: ENVIRONMENT_ALL.value, - kuery: '', - start: 0, - end: 50000, - }, - setup - ) - ); - - const allParams = mock.spy.mock.calls.map((call) => call[1]); - - expect(allParams).toMatchSnapshot(); - }); - it('fetches metrics top traces', async () => { - mock = await inspectSearchParams((setup) => - topTransactionGroupsFetcher( - { - searchAggregatedTransactions: true, - environment: ENVIRONMENT_ALL.value, - kuery: '', - start: 0, - end: 50000, - }, - setup - ) - ); - - const allParams = mock.spy.mock.calls.map((call) => call[1]); - - expect(allParams).toMatchSnapshot(); - }); -}); diff --git a/x-pack/plugins/apm/server/plugin.ts b/x-pack/plugins/apm/server/plugin.ts index b603d9e72a2b0..2f8e10d68ae51 100644 --- a/x-pack/plugins/apm/server/plugin.ts +++ b/x-pack/plugins/apm/server/plugin.ts @@ -47,7 +47,6 @@ import { TRANSACTION_TYPE, } from '../common/elasticsearch_fieldnames'; import { tutorialProvider } from './tutorial'; -import { getDeprecations } from './deprecations'; export class APMPlugin implements @@ -197,14 +196,6 @@ export class APMPlugin kibanaVersion: this.initContext.env.packageInfo.version, }); - core.deprecations.registerDeprecations({ - getDeprecations: getDeprecations({ - cloudSetup: plugins.cloud, - fleet: resourcePlugins.fleet, - branch: this.initContext.env.packageInfo.branch, - }), - }); - return { config$, getApmIndices: boundGetApmIndices, diff --git a/x-pack/plugins/apm/server/routes/agent_keys/create_agent_key.ts b/x-pack/plugins/apm/server/routes/agent_keys/create_agent_key.ts new file mode 100644 index 0000000000000..d83a7af2737cd --- /dev/null +++ b/x-pack/plugins/apm/server/routes/agent_keys/create_agent_key.ts @@ -0,0 +1,110 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import Boom from '@hapi/boom'; +import { ApmPluginRequestHandlerContext } from '../typings'; +import { CreateApiKeyResponse } from '../../../common/agent_key_types'; +import { PrivilegeType } from '../../../common/privilege_type'; + +const resource = '*'; + +interface SecurityHasPrivilegesResponse { + application: { + apm: { + [resource]: { + [PrivilegeType.SOURCEMAP]: boolean; + [PrivilegeType.EVENT]: boolean; + [PrivilegeType.AGENT_CONFIG]: boolean; + }; + }; + }; + has_all_requested: boolean; + username: string; +} + +export async function createAgentKey({ + context, + requestBody, +}: { + context: ApmPluginRequestHandlerContext; + requestBody: { + name: string; + privileges: string[]; + }; +}) { + const { name, privileges } = requestBody; + const application = { + application: 'apm', + privileges, + resources: [resource], + }; + + // Elasticsearch will allow a user without the right apm privileges to create API keys, but the keys won't validate + // check first whether the user has the right privileges, and bail out early if not + const { + body: { + application: userApplicationPrivileges, + username, + has_all_requested: hasRequiredPrivileges, + }, + } = await context.core.elasticsearch.client.asCurrentUser.security.hasPrivileges( + { + body: { + application: [application], + }, + } + ); + + if (!hasRequiredPrivileges) { + const missingPrivileges = Object.entries( + userApplicationPrivileges.apm[resource] + ) + .filter((x) => !x[1]) + .map((x) => x[0]); + + const error = `${username} is missing the following requested privilege(s): ${missingPrivileges.join( + ', ' + )}.\ + You might try with the superuser, or add the missing APM application privileges to the role of the authenticated user, eg.: + PUT /_security/role/my_role + { + ... + "applications": [{ + "application": "apm", + "privileges": ${JSON.stringify(missingPrivileges)}, + "resources": [${resource}] + }], + ... + }`; + throw Boom.internal(error); + } + + const body = { + name, + metadata: { + application: 'apm', + }, + role_descriptors: { + apm: { + cluster: [], + index: [], + applications: [application], + }, + }, + }; + + const { body: agentKey } = + await context.core.elasticsearch.client.asCurrentUser.security.createApiKey( + { + body, + } + ); + + return { + agentKey, + }; +} diff --git a/x-pack/plugins/apm/server/routes/agent_keys/invalidate_agent_key.ts b/x-pack/plugins/apm/server/routes/agent_keys/invalidate_agent_key.ts index e2f86298efdca..1ccb63382de4e 100644 --- a/x-pack/plugins/apm/server/routes/agent_keys/invalidate_agent_key.ts +++ b/x-pack/plugins/apm/server/routes/agent_keys/invalidate_agent_key.ts @@ -19,6 +19,7 @@ export async function invalidateAgentKey({ { body: { ids: [id], + owner: true, }, } ); diff --git a/x-pack/plugins/apm/server/routes/agent_keys/route.ts b/x-pack/plugins/apm/server/routes/agent_keys/route.ts index e5f40205b2912..5878ce75680ac 100644 --- a/x-pack/plugins/apm/server/routes/agent_keys/route.ts +++ b/x-pack/plugins/apm/server/routes/agent_keys/route.ts @@ -13,6 +13,8 @@ import { createApmServerRouteRepository } from '../apm_routes/create_apm_server_ import { getAgentKeys } from './get_agent_keys'; import { getAgentKeysPrivileges } from './get_agent_keys_privileges'; import { invalidateAgentKey } from './invalidate_agent_key'; +import { createAgentKey } from './create_agent_key'; +import { privilegesTypeRt } from '../../../common/privilege_type'; const agentKeysRoute = createApmServerRoute({ endpoint: 'GET /internal/apm/agent_keys', @@ -74,10 +76,34 @@ const invalidateAgentKeyRoute = createApmServerRoute({ }, }); +const createAgentKeyRoute = createApmServerRoute({ + endpoint: 'POST /api/apm/agent_keys', + options: { tags: ['access:apm', 'access:apm_write'] }, + params: t.type({ + body: t.type({ + name: t.string, + privileges: privilegesTypeRt, + }), + }), + handler: async (resources) => { + const { context, params } = resources; + + const { body: requestBody } = params; + + const agentKey = await createAgentKey({ + context, + requestBody, + }); + + return agentKey; + }, +}); + export const agentKeysRouteRepository = createApmServerRouteRepository() .add(agentKeysRoute) .add(agentKeysPrivilegesRoute) - .add(invalidateAgentKeyRoute); + .add(invalidateAgentKeyRoute) + .add(createAgentKeyRoute); const SECURITY_REQUIRED_MESSAGE = i18n.translate( 'xpack.apm.api.apiKeys.securityRequired', diff --git a/x-pack/plugins/apm/server/routes/correlations/queries/field_stats/get_boolean_field_stats.ts b/x-pack/plugins/apm/server/routes/correlations/queries/field_stats/get_boolean_field_stats.ts index c936e626a5599..a41e3370c1063 100644 --- a/x-pack/plugins/apm/server/routes/correlations/queries/field_stats/get_boolean_field_stats.ts +++ b/x-pack/plugins/apm/server/routes/correlations/queries/field_stats/get_boolean_field_stats.ts @@ -7,8 +7,6 @@ import { ElasticsearchClient } from 'kibana/server'; import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; - -import { buildSamplerAggregation } from '../../utils/field_stats_utils'; import { FieldValuePair } from '../../../../../common/correlations/types'; import { FieldStatsCommonRequestParams, @@ -25,7 +23,7 @@ export const getBooleanFieldStatsRequest = ( ): estypes.SearchRequest => { const query = getQueryWithParams({ params, termFilters }); - const { index, samplerShardSize } = params; + const { index } = params; const size = 0; const aggs: Aggs = { @@ -42,14 +40,13 @@ export const getBooleanFieldStatsRequest = ( const searchBody = { query, - aggs: { - sample: buildSamplerAggregation(aggs, samplerShardSize), - }, + aggs, }; return { index, size, + track_total_hits: false, body: searchBody, }; }; @@ -67,19 +64,17 @@ export const fetchBooleanFieldStats = async ( ); const { body } = await esClient.search(request); const aggregations = body.aggregations as { - sample: { - sampled_value_count: estypes.AggregationsFiltersBucketItemKeys; - sampled_values: estypes.AggregationsTermsAggregate; - }; + sampled_value_count: estypes.AggregationsFiltersBucketItemKeys; + sampled_values: estypes.AggregationsTermsAggregate; }; const stats: BooleanFieldStats = { fieldName: field.fieldName, - count: aggregations?.sample.sampled_value_count.doc_count ?? 0, + count: aggregations?.sampled_value_count.doc_count ?? 0, }; const valueBuckets: TopValueBucket[] = - aggregations?.sample.sampled_values?.buckets ?? []; + aggregations?.sampled_values?.buckets ?? []; valueBuckets.forEach((bucket) => { stats[`${bucket.key.toString()}Count`] = bucket.doc_count; }); diff --git a/x-pack/plugins/apm/server/routes/correlations/queries/field_stats/get_field_stats.test.ts b/x-pack/plugins/apm/server/routes/correlations/queries/field_stats/get_field_stats.test.ts index 2775d755c9907..30bebc4c24774 100644 --- a/x-pack/plugins/apm/server/routes/correlations/queries/field_stats/get_field_stats.test.ts +++ b/x-pack/plugins/apm/server/routes/correlations/queries/field_stats/get_field_stats.test.ts @@ -20,7 +20,6 @@ const params = { includeFrozen: false, environment: ENVIRONMENT_ALL.value, kuery: '', - samplerShardSize: 5000, }; export const getExpectedQuery = (aggs: any) => { @@ -46,6 +45,7 @@ export const getExpectedQuery = (aggs: any) => { }, index: 'apm-*', size: 0, + track_total_hits: false, }; }; @@ -55,28 +55,16 @@ describe('field_stats', () => { const req = getNumericFieldStatsRequest(params, 'url.path'); const expectedAggs = { - sample: { - aggs: { - sampled_field_stats: { - aggs: { actual_stats: { stats: { field: 'url.path' } } }, - filter: { exists: { field: 'url.path' } }, - }, - sampled_percentiles: { - percentiles: { - field: 'url.path', - keyed: false, - percents: [50], - }, - }, - sampled_top: { - terms: { - field: 'url.path', - order: { _count: 'desc' }, - size: 10, - }, - }, + sampled_field_stats: { + aggs: { actual_stats: { stats: { field: 'url.path' } } }, + filter: { exists: { field: 'url.path' } }, + }, + sampled_top: { + terms: { + field: 'url.path', + order: { _count: 'desc' }, + size: 10, }, - sampler: { shard_size: 5000 }, }, }; expect(req).toEqual(getExpectedQuery(expectedAggs)); @@ -87,13 +75,8 @@ describe('field_stats', () => { const req = getKeywordFieldStatsRequest(params, 'url.path'); const expectedAggs = { - sample: { - sampler: { shard_size: 5000 }, - aggs: { - sampled_top: { - terms: { field: 'url.path', size: 10, order: { _count: 'desc' } }, - }, - }, + sampled_top: { + terms: { field: 'url.path', size: 10 }, }, }; expect(req).toEqual(getExpectedQuery(expectedAggs)); @@ -104,15 +87,10 @@ describe('field_stats', () => { const req = getBooleanFieldStatsRequest(params, 'url.path'); const expectedAggs = { - sample: { - sampler: { shard_size: 5000 }, - aggs: { - sampled_value_count: { - filter: { exists: { field: 'url.path' } }, - }, - sampled_values: { terms: { field: 'url.path', size: 2 } }, - }, + sampled_value_count: { + filter: { exists: { field: 'url.path' } }, }, + sampled_values: { terms: { field: 'url.path', size: 2 } }, }; expect(req).toEqual(getExpectedQuery(expectedAggs)); }); diff --git a/x-pack/plugins/apm/server/routes/correlations/queries/field_stats/get_field_value_stats.ts b/x-pack/plugins/apm/server/routes/correlations/queries/field_stats/get_field_value_stats.ts new file mode 100644 index 0000000000000..0fa508eff508c --- /dev/null +++ b/x-pack/plugins/apm/server/routes/correlations/queries/field_stats/get_field_value_stats.ts @@ -0,0 +1,76 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { ElasticsearchClient } from 'kibana/server'; +import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; +import { FieldValuePair } from '../../../../../common/correlations/types'; +import { + FieldStatsCommonRequestParams, + FieldValueFieldStats, + Aggs, + TopValueBucket, +} from '../../../../../common/correlations/field_stats_types'; +import { getQueryWithParams } from '../get_query_with_params'; + +export const getFieldValueFieldStatsRequest = ( + params: FieldStatsCommonRequestParams, + field?: FieldValuePair +): estypes.SearchRequest => { + const query = getQueryWithParams({ params }); + + const { index } = params; + + const size = 0; + const aggs: Aggs = { + filtered_count: { + filter: { + term: { + [`${field?.fieldName}`]: field?.fieldValue, + }, + }, + }, + }; + + const searchBody = { + query, + aggs, + }; + + return { + index, + size, + track_total_hits: false, + body: searchBody, + }; +}; + +export const fetchFieldValueFieldStats = async ( + esClient: ElasticsearchClient, + params: FieldStatsCommonRequestParams, + field: FieldValuePair +): Promise => { + const request = getFieldValueFieldStatsRequest(params, field); + + const { body } = await esClient.search(request); + const aggregations = body.aggregations as { + filtered_count: estypes.AggregationsFiltersBucketItemKeys; + }; + const topValues: TopValueBucket[] = [ + { + key: field.fieldValue, + doc_count: aggregations.filtered_count.doc_count, + }, + ]; + + const stats = { + fieldName: field.fieldName, + topValues, + topValuesSampleSize: aggregations.filtered_count.doc_count ?? 0, + }; + + return stats; +}; diff --git a/x-pack/plugins/apm/server/routes/correlations/queries/field_stats/get_fields_stats.ts b/x-pack/plugins/apm/server/routes/correlations/queries/field_stats/get_fields_stats.ts index 8b41f7662679c..513252ee65e11 100644 --- a/x-pack/plugins/apm/server/routes/correlations/queries/field_stats/get_fields_stats.ts +++ b/x-pack/plugins/apm/server/routes/correlations/queries/field_stats/get_fields_stats.ts @@ -8,10 +8,7 @@ import { ElasticsearchClient } from 'kibana/server'; import { chunk } from 'lodash'; import { ES_FIELD_TYPES } from '@kbn/field-types'; -import { - FieldValuePair, - CorrelationsParams, -} from '../../../../../common/correlations/types'; +import { FieldValuePair } from '../../../../../common/correlations/types'; import { FieldStats, FieldStatsCommonRequestParams, @@ -23,7 +20,7 @@ import { fetchBooleanFieldStats } from './get_boolean_field_stats'; export const fetchFieldsStats = async ( esClient: ElasticsearchClient, - params: CorrelationsParams, + fieldStatsParams: FieldStatsCommonRequestParams, fieldsToSample: string[], termFilters?: FieldValuePair[] ): Promise<{ stats: FieldStats[]; errors: any[] }> => { @@ -33,14 +30,10 @@ export const fetchFieldsStats = async ( if (fieldsToSample.length === 0) return { stats, errors }; const respMapping = await esClient.fieldCaps({ - ...getRequestBase(params), + ...getRequestBase(fieldStatsParams), fields: fieldsToSample, }); - const fieldStatsParams: FieldStatsCommonRequestParams = { - ...params, - samplerShardSize: 5000, - }; const fieldStatsPromises = Object.entries(respMapping.body.fields) .map(([key, value], idx) => { const field: FieldValuePair = { fieldName: key, fieldValue: '' }; diff --git a/x-pack/plugins/apm/server/routes/correlations/queries/field_stats/get_keyword_field_stats.ts b/x-pack/plugins/apm/server/routes/correlations/queries/field_stats/get_keyword_field_stats.ts index c64bbc6678779..16ba4f24f5e93 100644 --- a/x-pack/plugins/apm/server/routes/correlations/queries/field_stats/get_keyword_field_stats.ts +++ b/x-pack/plugins/apm/server/routes/correlations/queries/field_stats/get_keyword_field_stats.ts @@ -14,7 +14,6 @@ import { Aggs, TopValueBucket, } from '../../../../../common/correlations/field_stats_types'; -import { buildSamplerAggregation } from '../../utils/field_stats_utils'; import { getQueryWithParams } from '../get_query_with_params'; export const getKeywordFieldStatsRequest = ( @@ -24,7 +23,7 @@ export const getKeywordFieldStatsRequest = ( ): estypes.SearchRequest => { const query = getQueryWithParams({ params, termFilters }); - const { index, samplerShardSize } = params; + const { index } = params; const size = 0; const aggs: Aggs = { @@ -32,23 +31,19 @@ export const getKeywordFieldStatsRequest = ( terms: { field: fieldName, size: 10, - order: { - _count: 'desc', - }, }, }, }; const searchBody = { query, - aggs: { - sample: buildSamplerAggregation(aggs, samplerShardSize), - }, + aggs, }; return { index, size, + track_total_hits: false, body: searchBody, }; }; @@ -66,19 +61,16 @@ export const fetchKeywordFieldStats = async ( ); const { body } = await esClient.search(request); const aggregations = body.aggregations as { - sample: { - sampled_top: estypes.AggregationsTermsAggregate; - }; + sampled_top: estypes.AggregationsTermsAggregate; }; - const topValues: TopValueBucket[] = - aggregations?.sample.sampled_top?.buckets ?? []; + const topValues: TopValueBucket[] = aggregations?.sampled_top?.buckets ?? []; const stats = { fieldName: field.fieldName, topValues, topValuesSampleSize: topValues.reduce( (acc, curr) => acc + curr.doc_count, - aggregations.sample.sampled_top?.sum_other_doc_count ?? 0 + aggregations.sampled_top?.sum_other_doc_count ?? 0 ), }; diff --git a/x-pack/plugins/apm/server/routes/correlations/queries/field_stats/get_numeric_field_stats.ts b/x-pack/plugins/apm/server/routes/correlations/queries/field_stats/get_numeric_field_stats.ts index 21e6559fdda25..197ed66c4fe70 100644 --- a/x-pack/plugins/apm/server/routes/correlations/queries/field_stats/get_numeric_field_stats.ts +++ b/x-pack/plugins/apm/server/routes/correlations/queries/field_stats/get_numeric_field_stats.ts @@ -6,7 +6,7 @@ */ import { ElasticsearchClient } from 'kibana/server'; -import { find, get } from 'lodash'; +import { get } from 'lodash'; import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; import { NumericFieldStats, @@ -16,10 +16,6 @@ import { } from '../../../../../common/correlations/field_stats_types'; import { FieldValuePair } from '../../../../../common/correlations/types'; import { getQueryWithParams } from '../get_query_with_params'; -import { buildSamplerAggregation } from '../../utils/field_stats_utils'; - -// Only need 50th percentile for the median -const PERCENTILES = [50]; export const getNumericFieldStatsRequest = ( params: FieldStatsCommonRequestParams, @@ -29,9 +25,8 @@ export const getNumericFieldStatsRequest = ( const query = getQueryWithParams({ params, termFilters }); const size = 0; - const { index, samplerShardSize } = params; + const { index } = params; - const percents = PERCENTILES; const aggs: Aggs = { sampled_field_stats: { filter: { exists: { field: fieldName } }, @@ -41,13 +36,6 @@ export const getNumericFieldStatsRequest = ( }, }, }, - sampled_percentiles: { - percentiles: { - field: fieldName, - percents, - keyed: false, - }, - }, sampled_top: { terms: { field: fieldName, @@ -61,14 +49,13 @@ export const getNumericFieldStatsRequest = ( const searchBody = { query, - aggs: { - sample: buildSamplerAggregation(aggs, samplerShardSize), - }, + aggs, }; return { index, size, + track_total_hits: false, body: searchBody, }; }; @@ -87,19 +74,15 @@ export const fetchNumericFieldStats = async ( const { body } = await esClient.search(request); const aggregations = body.aggregations as { - sample: { - sampled_top: estypes.AggregationsTermsAggregate; - sampled_percentiles: estypes.AggregationsHdrPercentilesAggregate; - sampled_field_stats: { - doc_count: number; - actual_stats: estypes.AggregationsStatsAggregate; - }; + sampled_top: estypes.AggregationsTermsAggregate; + sampled_field_stats: { + doc_count: number; + actual_stats: estypes.AggregationsStatsAggregate; }; }; - const docCount = aggregations?.sample.sampled_field_stats?.doc_count ?? 0; - const fieldStatsResp = - aggregations?.sample.sampled_field_stats?.actual_stats ?? {}; - const topValues = aggregations?.sample.sampled_top?.buckets ?? []; + const docCount = aggregations?.sampled_field_stats?.doc_count ?? 0; + const fieldStatsResp = aggregations?.sampled_field_stats?.actual_stats ?? {}; + const topValues = aggregations?.sampled_top?.buckets ?? []; const stats: NumericFieldStats = { fieldName: field.fieldName, @@ -110,20 +93,9 @@ export const fetchNumericFieldStats = async ( topValues, topValuesSampleSize: topValues.reduce( (acc: number, curr: TopValueBucket) => acc + curr.doc_count, - aggregations.sample.sampled_top?.sum_other_doc_count ?? 0 + aggregations.sampled_top?.sum_other_doc_count ?? 0 ), }; - if (stats.count !== undefined && stats.count > 0) { - const percentiles = aggregations?.sample.sampled_percentiles.values ?? []; - const medianPercentile: { value: number; key: number } | undefined = find( - percentiles, - { - key: 50, - } - ); - stats.median = medianPercentile !== undefined ? medianPercentile!.value : 0; - } - return stats; }; diff --git a/x-pack/plugins/apm/server/routes/correlations/queries/index.ts b/x-pack/plugins/apm/server/routes/correlations/queries/index.ts index 548127eb7647d..d2a86a20bd5c6 100644 --- a/x-pack/plugins/apm/server/routes/correlations/queries/index.ts +++ b/x-pack/plugins/apm/server/routes/correlations/queries/index.ts @@ -16,3 +16,4 @@ export { fetchTransactionDurationCorrelation } from './query_correlation'; export { fetchTransactionDurationCorrelationWithHistogram } from './query_correlation_with_histogram'; export { fetchTransactionDurationHistogramRangeSteps } from './query_histogram_range_steps'; export { fetchTransactionDurationRanges } from './query_ranges'; +export { fetchFieldValueFieldStats } from './field_stats/get_field_value_stats'; diff --git a/x-pack/plugins/apm/server/routes/correlations/route.ts b/x-pack/plugins/apm/server/routes/correlations/route.ts index b02a6fbc6b7a6..377fedf9d1813 100644 --- a/x-pack/plugins/apm/server/routes/correlations/route.ts +++ b/x-pack/plugins/apm/server/routes/correlations/route.ts @@ -19,6 +19,7 @@ import { fetchSignificantCorrelations, fetchTransactionDurationFieldCandidates, fetchTransactionDurationFieldValuePairs, + fetchFieldValueFieldStats, } from './queries'; import { fetchFieldsStats } from './queries/field_stats/get_fields_stats'; @@ -77,12 +78,12 @@ const fieldStatsRoute = createApmServerRoute({ transactionName: t.string, transactionType: t.string, }), - environmentRt, - kueryRt, - rangeRt, t.type({ fieldsToSample: t.array(t.string), }), + environmentRt, + kueryRt, + rangeRt, ]), }), options: { tags: ['access:apm'] }, @@ -112,6 +113,51 @@ const fieldStatsRoute = createApmServerRoute({ }, }); +const fieldValueStatsRoute = createApmServerRoute({ + endpoint: 'GET /internal/apm/correlations/field_value_stats', + params: t.type({ + query: t.intersection([ + t.partial({ + serviceName: t.string, + transactionName: t.string, + transactionType: t.string, + }), + environmentRt, + kueryRt, + rangeRt, + t.type({ + fieldName: t.string, + fieldValue: t.union([t.string, t.number]), + }), + ]), + }), + options: { tags: ['access:apm'] }, + handler: async (resources) => { + const { context } = resources; + if (!isActivePlatinumLicense(context.licensing.license)) { + throw Boom.forbidden(INVALID_LICENSE); + } + + const { indices } = await setupRequest(resources); + const esClient = resources.context.core.elasticsearch.client.asCurrentUser; + + const { fieldName, fieldValue, ...params } = resources.params.query; + + return withApmSpan( + 'get_correlations_field_value_stats', + async () => + await fetchFieldValueFieldStats( + esClient, + { + ...params, + index: indices.transaction, + }, + { fieldName, fieldValue } + ) + ); + }, +}); + const fieldValuePairsRoute = createApmServerRoute({ endpoint: 'POST /internal/apm/correlations/field_value_pairs', params: t.type({ @@ -252,5 +298,6 @@ export const correlationsRouteRepository = createApmServerRouteRepository() .add(pValuesRoute) .add(fieldCandidatesRoute) .add(fieldStatsRoute) + .add(fieldValueStatsRoute) .add(fieldValuePairsRoute) .add(significantCorrelationsRoute); diff --git a/x-pack/plugins/apm/server/routes/correlations/utils/field_stats_utils.ts b/x-pack/plugins/apm/server/routes/correlations/utils/field_stats_utils.ts index 7f98f771c50e2..a60622583781b 100644 --- a/x-pack/plugins/apm/server/routes/correlations/utils/field_stats_utils.ts +++ b/x-pack/plugins/apm/server/routes/correlations/utils/field_stats_utils.ts @@ -5,8 +5,6 @@ * 2.0. */ -import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; - /* * Contains utility functions for building and processing queries. */ @@ -38,22 +36,3 @@ export function buildBaseFilterCriteria( return filterCriteria; } - -// Wraps the supplied aggregations in a sampler aggregation. -// A supplied samplerShardSize (the shard_size parameter of the sampler aggregation) -// of less than 1 indicates no sampling, and the aggs are returned as-is. -export function buildSamplerAggregation( - aggs: any, - samplerShardSize: number -): estypes.AggregationsAggregationContainer { - if (samplerShardSize < 1) { - return aggs; - } - - return { - sampler: { - shard_size: samplerShardSize, - }, - aggs, - }; -} diff --git a/x-pack/plugins/apm/server/routes/data_view/create_static_data_view.test.ts b/x-pack/plugins/apm/server/routes/data_view/create_static_data_view.test.ts index 7d345b5e3bec1..cdea5cd43f02f 100644 --- a/x-pack/plugins/apm/server/routes/data_view/create_static_data_view.test.ts +++ b/x-pack/plugins/apm/server/routes/data_view/create_static_data_view.test.ts @@ -36,7 +36,7 @@ describe('createStaticDataView', () => { const savedObjectsClient = getMockSavedObjectsClient('apm-*'); await createStaticDataView({ setup, - config: { autocreateApmIndexPattern: false } as APMConfig, + config: { autoCreateApmDataView: false } as APMConfig, savedObjectsClient, spaceId: 'default', }); @@ -53,7 +53,7 @@ describe('createStaticDataView', () => { await createStaticDataView({ setup, - config: { autocreateApmIndexPattern: true } as APMConfig, + config: { autoCreateApmDataView: true } as APMConfig, savedObjectsClient, spaceId: 'default', }); @@ -70,7 +70,7 @@ describe('createStaticDataView', () => { await createStaticDataView({ setup, - config: { autocreateApmIndexPattern: true } as APMConfig, + config: { autoCreateApmDataView: true } as APMConfig, savedObjectsClient, spaceId: 'default', }); @@ -90,7 +90,7 @@ describe('createStaticDataView', () => { await createStaticDataView({ setup, - config: { autocreateApmIndexPattern: true } as APMConfig, + config: { autoCreateApmDataView: true } as APMConfig, savedObjectsClient, spaceId: 'default', }); @@ -117,7 +117,7 @@ describe('createStaticDataView', () => { await createStaticDataView({ setup, - config: { autocreateApmIndexPattern: true } as APMConfig, + config: { autoCreateApmDataView: true } as APMConfig, savedObjectsClient, spaceId: 'default', }); diff --git a/x-pack/plugins/apm/server/routes/data_view/create_static_data_view.ts b/x-pack/plugins/apm/server/routes/data_view/create_static_data_view.ts index 20b3d3117dd9f..665f9ca3e96eb 100644 --- a/x-pack/plugins/apm/server/routes/data_view/create_static_data_view.ts +++ b/x-pack/plugins/apm/server/routes/data_view/create_static_data_view.ts @@ -31,8 +31,8 @@ export async function createStaticDataView({ spaceId?: string; }): Promise { return withApmSpan('create_static_data_view', async () => { - // don't autocreate APM data view if it's been disabled via the config - if (!config.autocreateApmIndexPattern) { + // don't auto-create APM data view if it's been disabled via the config + if (!config.autoCreateApmDataView) { return false; } diff --git a/x-pack/plugins/apm/server/routes/errors/get_error_groups/get_error_group_main_statistics.ts b/x-pack/plugins/apm/server/routes/errors/get_error_groups/get_error_group_main_statistics.ts index d32e751a6ca99..e460991029915 100644 --- a/x-pack/plugins/apm/server/routes/errors/get_error_groups/get_error_group_main_statistics.ts +++ b/x-pack/plugins/apm/server/routes/errors/get_error_groups/get_error_group_main_statistics.ts @@ -110,7 +110,6 @@ export async function getErrorGroupMainStatistics({ ); return ( - // @ts-ignore 4.3.5 upgrade - Expression produces a union type that is too complex to represent. ts(2590) response.aggregations?.error_groups.buckets.map((bucket) => ({ groupId: bucket.key as string, name: getErrorName(bucket.sample.hits.hits[0]._source), diff --git a/x-pack/plugins/apm/server/routes/service_map/get_service_anomalies.ts b/x-pack/plugins/apm/server/routes/service_map/get_service_anomalies.ts index 792bc0463aa15..a5fcececad1cc 100644 --- a/x-pack/plugins/apm/server/routes/service_map/get_service_anomalies.ts +++ b/x-pack/plugins/apm/server/routes/service_map/get_service_anomalies.ts @@ -120,7 +120,6 @@ export async function getServiceAnomalies({ const relevantBuckets = uniqBy( sortBy( // make sure we only return data for jobs that are available in this space - // @ts-ignore 4.3.5 upgrade typedAnomalyResponse.aggregations?.services.buckets.filter((bucket) => jobIds.includes(bucket.key.jobId as string) ) ?? [], diff --git a/x-pack/plugins/apm/server/routes/service_nodes/__snapshots__/queries.test.ts.snap b/x-pack/plugins/apm/server/routes/service_nodes/__snapshots__/queries.test.ts.snap index e0591a90b1c19..5022521c46914 100644 --- a/x-pack/plugins/apm/server/routes/service_nodes/__snapshots__/queries.test.ts.snap +++ b/x-pack/plugins/apm/server/routes/service_nodes/__snapshots__/queries.test.ts.snap @@ -17,7 +17,7 @@ Object { }, "host": Object { "terms": Object { - "field": "host.hostname", + "field": "host.name", "size": 1, }, }, @@ -74,7 +74,7 @@ Object { }, "host": Object { "terms": Object { - "field": "host.hostname", + "field": "host.name", "size": 1, }, }, @@ -145,7 +145,7 @@ Object { "top_metrics": Object { "metrics": Array [ Object { - "field": "host.hostname", + "field": "host.name", }, ], "sort": Object { diff --git a/x-pack/plugins/apm/server/routes/service_nodes/get_service_nodes.ts b/x-pack/plugins/apm/server/routes/service_nodes/get_service_nodes.ts index ebd56cb9768ce..58c105289be9c 100644 --- a/x-pack/plugins/apm/server/routes/service_nodes/get_service_nodes.ts +++ b/x-pack/plugins/apm/server/routes/service_nodes/get_service_nodes.ts @@ -109,7 +109,7 @@ const getServiceNodes = async ({ name: bucket.key as string, cpu: bucket.cpu.value, heapMemory: bucket.heapMemory.value, - hostName: bucket.latest.top?.[0]?.metrics?.['host.hostname'] as + hostName: bucket.latest.top?.[0]?.metrics?.[HOST_NAME] as | string | null | undefined, diff --git a/x-pack/plugins/apm/server/routes/services/get_service_infrastructure.ts b/x-pack/plugins/apm/server/routes/services/get_service_infrastructure.ts index cda0beb6b2d70..79d7ff4f1f41e 100644 --- a/x-pack/plugins/apm/server/routes/services/get_service_infrastructure.ts +++ b/x-pack/plugins/apm/server/routes/services/get_service_infrastructure.ts @@ -12,7 +12,7 @@ import { ProcessorEvent } from '../../../common/processor_event'; import { SERVICE_NAME, CONTAINER_ID, - HOSTNAME, + HOST_NAME, } from '../../../common/elasticsearch_fieldnames'; export const getServiceInfrastructure = async ({ @@ -57,7 +57,7 @@ export const getServiceInfrastructure = async ({ }, hostNames: { terms: { - field: HOSTNAME, + field: HOST_NAME, size: 500, }, }, diff --git a/x-pack/plugins/apm/server/routes/services/get_service_instances/get_service_instances_transaction_statistics.ts b/x-pack/plugins/apm/server/routes/services/get_service_instances/get_service_instances_transaction_statistics.ts index a9f5615acb1c0..ec081916f455d 100644 --- a/x-pack/plugins/apm/server/routes/services/get_service_instances/get_service_instances_transaction_statistics.ts +++ b/x-pack/plugins/apm/server/routes/services/get_service_instances/get_service_instances_transaction_statistics.ts @@ -168,7 +168,6 @@ export async function getServiceInstancesTransactionStatistics< const { timeseries } = serviceNodeBucket; return { serviceNodeName, - // @ts-ignore 4.3.5 upgrade - Expression produces a union type that is too complex to represent. errorRate: timeseries.buckets.map((dateBucket) => ({ x: dateBucket.key, y: dateBucket.failures.doc_count / dateBucket.doc_count, diff --git a/x-pack/plugins/apm/server/routes/traces/calculate_impact_builder.ts b/x-pack/plugins/apm/server/routes/traces/calculate_impact_builder.ts new file mode 100644 index 0000000000000..bcbd3ac88aedd --- /dev/null +++ b/x-pack/plugins/apm/server/routes/traces/calculate_impact_builder.ts @@ -0,0 +1,18 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +export function calculateImpactBuilder(sums?: Array) { + const sumValues = (sums ?? []).filter((value) => value !== null) as number[]; + + const max = Math.max(...sumValues); + const min = Math.min(...sumValues); + + return (sum: number) => + sum !== null && sum !== undefined + ? ((sum - min) / (max - min)) * 100 || 0 + : 0; +} diff --git a/x-pack/plugins/apm/server/routes/traces/get_top_traces_primary_stats.ts b/x-pack/plugins/apm/server/routes/traces/get_top_traces_primary_stats.ts new file mode 100644 index 0000000000000..7b0bb729324d7 --- /dev/null +++ b/x-pack/plugins/apm/server/routes/traces/get_top_traces_primary_stats.ts @@ -0,0 +1,188 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; +import { sortBy } from 'lodash'; +import { AgentName } from '../../../typings/es_schemas/ui/fields/agent'; +import { withApmSpan } from '../../utils/with_apm_span'; +import { Setup } from '../../lib/helpers/setup_request'; +import { asMutableArray } from '../../../common/utils/as_mutable_array'; +import { environmentQuery } from '../../../common/utils/environment_query'; +import { calculateImpactBuilder } from './calculate_impact_builder'; +import { calculateThroughputWithRange } from '../../lib/helpers/calculate_throughput'; +import { + kqlQuery, + rangeQuery, + termQuery, +} from '../../../../observability/server'; +import { + getDurationFieldForTransactions, + getDocumentTypeFilterForTransactions, + getProcessorEventForTransactions, +} from '../../lib/helpers/transactions'; +import { + AGENT_NAME, + PARENT_ID, + SERVICE_NAME, + TRANSACTION_TYPE, + TRANSACTION_NAME, + TRANSACTION_ROOT, +} from '../../../common/elasticsearch_fieldnames'; + +export type BucketKey = Record< + typeof TRANSACTION_NAME | typeof SERVICE_NAME, + string +>; + +interface TopTracesParams { + environment: string; + kuery: string; + transactionName?: string; + searchAggregatedTransactions: boolean; + start: number; + end: number; + setup: Setup; +} +export function getTopTracesPrimaryStats({ + environment, + kuery, + transactionName, + searchAggregatedTransactions, + start, + end, + setup, +}: TopTracesParams) { + return withApmSpan('get_top_traces_primary_stats', async () => { + const response = await setup.apmEventClient.search( + 'get_transaction_group_stats', + { + apm: { + events: [ + getProcessorEventForTransactions(searchAggregatedTransactions), + ], + }, + body: { + size: 0, + query: { + bool: { + filter: [ + ...termQuery(TRANSACTION_NAME, transactionName), + ...getDocumentTypeFilterForTransactions( + searchAggregatedTransactions + ), + ...rangeQuery(start, end), + ...environmentQuery(environment), + ...kqlQuery(kuery), + ...(searchAggregatedTransactions + ? [ + { + term: { + [TRANSACTION_ROOT]: true, + }, + }, + ] + : []), + ] as estypes.QueryDslQueryContainer[], + must_not: [ + ...(!searchAggregatedTransactions + ? [ + { + exists: { + field: PARENT_ID, + }, + }, + ] + : []), + ], + }, + }, + aggs: { + transaction_groups: { + composite: { + sources: asMutableArray([ + { [SERVICE_NAME]: { terms: { field: SERVICE_NAME } } }, + { + [TRANSACTION_NAME]: { + terms: { field: TRANSACTION_NAME }, + }, + }, + ] as const), + // traces overview is hardcoded to 10000 + size: 10000, + }, + aggs: { + transaction_type: { + top_metrics: { + sort: { + '@timestamp': 'desc' as const, + }, + metrics: [ + { + field: TRANSACTION_TYPE, + } as const, + { + field: AGENT_NAME, + } as const, + ], + }, + }, + avg: { + avg: { + field: getDurationFieldForTransactions( + searchAggregatedTransactions + ), + }, + }, + sum: { + sum: { + field: getDurationFieldForTransactions( + searchAggregatedTransactions + ), + }, + }, + }, + }, + }, + }, + } + ); + + const calculateImpact = calculateImpactBuilder( + response.aggregations?.transaction_groups.buckets.map( + ({ sum }) => sum.value + ) + ); + + const items = response.aggregations?.transaction_groups.buckets.map( + (bucket) => { + return { + key: bucket.key as BucketKey, + serviceName: bucket.key[SERVICE_NAME] as string, + transactionName: bucket.key[TRANSACTION_NAME] as string, + averageResponseTime: bucket.avg.value, + transactionsPerMinute: calculateThroughputWithRange({ + start, + end, + value: bucket.doc_count ?? 0, + }), + transactionType: bucket.transaction_type.top[0].metrics[ + TRANSACTION_TYPE + ] as string, + impact: calculateImpact(bucket.sum.value ?? 0), + agentName: bucket.transaction_type.top[0].metrics[ + AGENT_NAME + ] as AgentName, + }; + } + ); + + return { + // sort by impact by default so most impactful services are not cut off + items: sortBy(items, 'impact').reverse(), + }; + }); +} diff --git a/x-pack/plugins/apm/server/routes/traces/route.ts b/x-pack/plugins/apm/server/routes/traces/route.ts index 24b5faeedfc00..33f78b7bca11a 100644 --- a/x-pack/plugins/apm/server/routes/traces/route.ts +++ b/x-pack/plugins/apm/server/routes/traces/route.ts @@ -8,7 +8,7 @@ import * as t from 'io-ts'; import { setupRequest } from '../../lib/helpers/setup_request'; import { getTraceItems } from './get_trace_items'; -import { getTopTransactionGroupList } from '../../lib/transaction_groups'; +import { getTopTracesPrimaryStats } from './get_top_traces_primary_stats'; import { createApmServerRoute } from '../apm_routes/create_apm_server_route'; import { environmentRt, kueryRt, rangeRt } from '../default_api_types'; import { getSearchAggregatedTransactions } from '../../lib/helpers/transactions'; @@ -33,10 +33,14 @@ const tracesRoute = createApmServerRoute({ end, }); - return getTopTransactionGroupList( - { environment, kuery, searchAggregatedTransactions, start, end }, - setup - ); + return await getTopTracesPrimaryStats({ + environment, + kuery, + setup, + searchAggregatedTransactions, + start, + end, + }); }, }); diff --git a/x-pack/plugins/apm/server/routes/transactions/get_failed_transaction_rate_periods.ts b/x-pack/plugins/apm/server/routes/transactions/get_failed_transaction_rate_periods.ts new file mode 100644 index 0000000000000..709c867377aff --- /dev/null +++ b/x-pack/plugins/apm/server/routes/transactions/get_failed_transaction_rate_periods.ts @@ -0,0 +1,78 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ +import { Setup } from '../../lib/helpers/setup_request'; +import { getFailedTransactionRate } from '../../lib/transaction_groups/get_failed_transaction_rate'; +import { offsetPreviousPeriodCoordinates } from '../../../common/utils/offset_previous_period_coordinate'; + +export async function getFailedTransactionRatePeriods({ + environment, + kuery, + serviceName, + transactionType, + transactionName, + setup, + searchAggregatedTransactions, + comparisonStart, + comparisonEnd, + start, + end, +}: { + environment: string; + kuery: string; + serviceName: string; + transactionType?: string; + transactionName?: string; + setup: Setup; + searchAggregatedTransactions: boolean; + comparisonStart?: number; + comparisonEnd?: number; + start: number; + end: number; +}) { + const commonProps = { + environment, + kuery, + serviceName, + transactionType, + transactionName, + setup, + searchAggregatedTransactions, + }; + + const currentPeriodPromise = getFailedTransactionRate({ + ...commonProps, + start, + end, + }); + + const previousPeriodPromise = + comparisonStart && comparisonEnd + ? getFailedTransactionRate({ + ...commonProps, + start: comparisonStart, + end: comparisonEnd, + }) + : { timeseries: [], average: null }; + + const [currentPeriod, previousPeriod] = await Promise.all([ + currentPeriodPromise, + previousPeriodPromise, + ]); + + const currentPeriodTimeseries = currentPeriod.timeseries; + + return { + currentPeriod, + previousPeriod: { + ...previousPeriod, + timeseries: offsetPreviousPeriodCoordinates({ + currentPeriodTimeseries, + previousPeriodTimeseries: previousPeriod.timeseries, + }), + }, + }; +} diff --git a/x-pack/plugins/apm/server/routes/transactions/route.ts b/x-pack/plugins/apm/server/routes/transactions/route.ts index b9db2762bce93..cad1c3b8f353b 100644 --- a/x-pack/plugins/apm/server/routes/transactions/route.ts +++ b/x-pack/plugins/apm/server/routes/transactions/route.ts @@ -19,7 +19,7 @@ import { getServiceTransactionGroupDetailedStatisticsPeriods } from '../services import { getTransactionBreakdown } from './breakdown'; import { getTransactionTraceSamples } from './trace_samples'; import { getLatencyPeriods } from './get_latency_charts'; -import { getFailedTransactionRatePeriods } from '../../lib/transaction_groups/get_failed_transaction_rate'; +import { getFailedTransactionRatePeriods } from './get_failed_transaction_rate_periods'; import { createApmServerRoute } from '../apm_routes/create_apm_server_route'; import { createApmServerRouteRepository } from '../apm_routes/create_apm_server_route_repository'; import { diff --git a/x-pack/plugins/canvas/public/components/asset_manager/__stories__/__snapshots__/asset_manager.stories.storyshot b/x-pack/plugins/canvas/public/components/asset_manager/__stories__/__snapshots__/asset_manager.stories.storyshot index b8013818ca58f..d47ecf71b2293 100644 --- a/x-pack/plugins/canvas/public/components/asset_manager/__stories__/__snapshots__/asset_manager.stories.storyshot +++ b/x-pack/plugins/canvas/public/components/asset_manager/__stories__/__snapshots__/asset_manager.stories.storyshot @@ -109,21 +109,34 @@ exports[`Storyshots components/Assets/AssetManager no assets 1`] = ` className="euiPanel euiPanel--paddingMedium euiPanel--borderRadiusMedium euiPanel--plain euiPanel--hasShadow canvasAssetManager__emptyPanel" >
-
-

- Import your assets to get started -

+
+ +
+
+
+

+ Import your assets to get started +

+
+
+
diff --git a/x-pack/plugins/canvas/public/components/home/my_workpads/__snapshots__/empty_prompt.stories.storyshot b/x-pack/plugins/canvas/public/components/home/my_workpads/__snapshots__/empty_prompt.stories.storyshot index 6d782713d8fc1..8f00060a1dd1c 100644 --- a/x-pack/plugins/canvas/public/components/home/my_workpads/__snapshots__/empty_prompt.stories.storyshot +++ b/x-pack/plugins/canvas/public/components/home/my_workpads/__snapshots__/empty_prompt.stories.storyshot @@ -16,49 +16,61 @@ exports[`Storyshots Home/Components/Empty Prompt Empty Prompt 1`] = ` className="euiPanel euiPanel--paddingMedium euiPanel--borderRadiusNone euiPanel--subdued euiPanel--noShadow euiPanel--noBorder" >
-
-

- Add your first workpad -

-
+ className="euiEmptyPrompt__icon" + > + +
-

- Create a new workpad, start from a template, or import a workpad JSON file by dropping it here. -

-

- New to Canvas? - - +

Add your first workpad - - . -

+

+ +
+
+

+ Create a new workpad, start from a template, or import a workpad JSON file by dropping it here. +

+

+ New to Canvas? + + + Add your first workpad + + . +

+
+ +
-
+
diff --git a/x-pack/plugins/canvas/public/components/saved_elements_modal/__stories__/__snapshots__/saved_elements_modal.stories.storyshot b/x-pack/plugins/canvas/public/components/saved_elements_modal/__stories__/__snapshots__/saved_elements_modal.stories.storyshot index f019f9dc8f23d..23202a7a1fb88 100644 --- a/x-pack/plugins/canvas/public/components/saved_elements_modal/__stories__/__snapshots__/saved_elements_modal.stories.storyshot +++ b/x-pack/plugins/canvas/public/components/saved_elements_modal/__stories__/__snapshots__/saved_elements_modal.stories.storyshot @@ -86,36 +86,49 @@ exports[`Storyshots components/SavedElementsModal no custom elements 1`] = ` className="euiSpacer euiSpacer--l" />
-
-

- Add new elements -

-
+ className="euiEmptyPrompt__icon" + > + +
-

- Group and save workpad elements to create new elements -

+
+

+ Add new elements +

+ +
+
+

+ Group and save workpad elements to create new elements +

+
+ +
- +
diff --git a/x-pack/plugins/canvas/shareable_runtime/webpack.config.js b/x-pack/plugins/canvas/shareable_runtime/webpack.config.js index b8074436d350b..42e96ab4471fe 100644 --- a/x-pack/plugins/canvas/shareable_runtime/webpack.config.js +++ b/x-pack/plugins/canvas/shareable_runtime/webpack.config.js @@ -188,7 +188,6 @@ module.exports = { { test: [ require.resolve('@elastic/eui/es/components/drag_and_drop'), - require.resolve('@elastic/eui/packages/react-datepicker'), require.resolve('highlight.js'), ], use: require.resolve('null-loader'), diff --git a/x-pack/plugins/canvas/storybook/storyshots.test.tsx b/x-pack/plugins/canvas/storybook/storyshots.test.tsx index e9b6d71eee274..1b05145d561f5 100644 --- a/x-pack/plugins/canvas/storybook/storyshots.test.tsx +++ b/x-pack/plugins/canvas/storybook/storyshots.test.tsx @@ -36,14 +36,6 @@ Date.now = jest.fn(() => testTime.getTime()); // Mock telemetry service jest.mock('../public/lib/ui_metric', () => ({ trackCanvasUiMetric: () => {} })); -// Mock react-datepicker dep used by eui to avoid rendering the entire large component -jest.mock('@elastic/eui/packages/react-datepicker', () => { - return { - __esModule: true, - default: 'ReactDatePicker', - }; -}); - // Mock React Portal for components that use modals, tooltips, etc // @ts-expect-error Portal mocks are notoriously difficult to type ReactDOM.createPortal = jest.fn((element) => element); diff --git a/x-pack/plugins/cases/common/api/cases/alerts.ts b/x-pack/plugins/cases/common/api/cases/alerts.ts index 1a1abb4cbb66a..3647b1acb3a40 100644 --- a/x-pack/plugins/cases/common/api/cases/alerts.ts +++ b/x-pack/plugins/cases/common/api/cases/alerts.ts @@ -14,5 +14,4 @@ const AlertRt = rt.type({ }); export const AlertResponseRt = rt.array(AlertRt); - export type AlertResponse = rt.TypeOf; diff --git a/x-pack/plugins/cases/common/utils/markdown_plugins/lens/serializer.ts b/x-pack/plugins/cases/common/utils/markdown_plugins/lens/serializer.ts index e561b2f8cfb8a..69d01b0051e18 100644 --- a/x-pack/plugins/cases/common/utils/markdown_plugins/lens/serializer.ts +++ b/x-pack/plugins/cases/common/utils/markdown_plugins/lens/serializer.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { Plugin } from 'unified'; import type { TimeRange } from 'src/plugins/data/common'; import { LENS_ID } from './constants'; @@ -13,8 +14,13 @@ export interface LensSerializerProps { timeRange: TimeRange; } -export const LensSerializer = ({ timeRange, attributes }: LensSerializerProps) => +const serializeLens = ({ timeRange, attributes }: LensSerializerProps) => `!{${LENS_ID}${JSON.stringify({ timeRange, attributes, })}}`; + +export const LensSerializer: Plugin = function () { + const Compiler = this.Compiler; + Compiler.prototype.visitors.lens = serializeLens; +}; diff --git a/x-pack/plugins/cases/common/utils/markdown_plugins/timeline/serializer.ts b/x-pack/plugins/cases/common/utils/markdown_plugins/timeline/serializer.ts index 0a95c9466b1ff..b9448f93d95c3 100644 --- a/x-pack/plugins/cases/common/utils/markdown_plugins/timeline/serializer.ts +++ b/x-pack/plugins/cases/common/utils/markdown_plugins/timeline/serializer.ts @@ -5,8 +5,14 @@ * 2.0. */ +import { Plugin } from 'unified'; export interface TimelineSerializerProps { match: string; } -export const TimelineSerializer = ({ match }: TimelineSerializerProps) => match; +const serializeTimeline = ({ match }: TimelineSerializerProps) => match; + +export const TimelineSerializer: Plugin = function () { + const Compiler = this.Compiler; + Compiler.prototype.visitors.timeline = serializeTimeline; +}; diff --git a/x-pack/plugins/cases/common/utils/markdown_plugins/utils.test.ts b/x-pack/plugins/cases/common/utils/markdown_plugins/utils.test.ts index baee979856511..516aff2300759 100644 --- a/x-pack/plugins/cases/common/utils/markdown_plugins/utils.test.ts +++ b/x-pack/plugins/cases/common/utils/markdown_plugins/utils.test.ts @@ -18,5 +18,93 @@ describe('markdown utils', () => { const parsed = parseCommentString('hello\n'); expect(stringifyMarkdownComment(parsed)).toEqual('hello\n'); }); + + // This check ensures the version of remark-stringify supports tables. From version 9+ this is not supported by default. + it('parses and stringifies github formatted markdown correctly', () => { + const parsed = parseCommentString(`| Tables | Are | Cool | + |----------|:-------------:|------:| + | col 1 is | left-aligned | $1600 | + | col 2 is | centered | $12 | + | col 3 is | right-aligned | $1 |`); + + expect(stringifyMarkdownComment(parsed)).toMatchInlineSnapshot(` + "| Tables | Are | Cool | + | -------- | :-----------: | ----: | + | col 1 is | left-aligned | $1600 | + | col 2 is | centered | $12 | + | col 3 is | right-aligned | $1 | + " + `); + }); + + it('parses a timeline url', () => { + const timelineUrl = + '[asdasdasdasd](http://localhost:5601/moq/app/security/timelines?timeline=(id%3A%27e4362a60-f478-11eb-a4b0-ebefce184d8d%27%2CisOpen%3A!t))'; + + const parsedNodes = parseCommentString(timelineUrl); + + expect(parsedNodes).toMatchInlineSnapshot(` + Object { + "children": Array [ + Object { + "match": "[asdasdasdasd](http://localhost:5601/moq/app/security/timelines?timeline=(id%3A%27e4362a60-f478-11eb-a4b0-ebefce184d8d%27%2CisOpen%3A!t))", + "position": Position { + "end": Object { + "column": 138, + "line": 1, + "offset": 137, + }, + "indent": Array [], + "start": Object { + "column": 1, + "line": 1, + "offset": 0, + }, + }, + "type": "timeline", + }, + ], + "position": Object { + "end": Object { + "column": 138, + "line": 1, + "offset": 137, + }, + "start": Object { + "column": 1, + "line": 1, + "offset": 0, + }, + }, + "type": "root", + } + `); + }); + + it('stringifies a timeline url', () => { + const timelineUrl = + '[asdasdasdasd](http://localhost:5601/moq/app/security/timelines?timeline=(id%3A%27e4362a60-f478-11eb-a4b0-ebefce184d8d%27%2CisOpen%3A!t))'; + + const parsedNodes = parseCommentString(timelineUrl); + + expect(stringifyMarkdownComment(parsedNodes)).toEqual(`${timelineUrl}\n`); + }); + + it('parses a lens visualization', () => { + const lensVisualization = + '!{lens{"timeRange":{"from":"now-7d","to":"now","mode":"relative"},"attributes":{"title":"TEst22","type":"lens","visualizationType":"lnsMetric","state":{"datasourceStates":{"indexpattern":{"layers":{"layer1":{"columnOrder":["col2"],"columns":{"col2":{"dataType":"number","isBucketed":false,"label":"Count of records","operationType":"count","scale":"ratio","sourceField":"Records"}}}}}},"visualization":{"layerId":"layer1","accessor":"col2"},"query":{"language":"kuery","query":""},"filters":[]},"references":[{"type":"index-pattern","id":"90943e30-9a47-11e8-b64d-95841ca0b247","name":"indexpattern-datasource-current-indexpattern"},{"type":"index-pattern","id":"90943e30-9a47-11e8-b64d-95841ca0b247","name":"indexpattern-datasource-layer-layer1"}]}}}'; + + const parsedNodes = parseCommentString(lensVisualization); + expect(parsedNodes.children[0].type).toEqual('lens'); + }); + + it('stringifies a lens visualization', () => { + const lensVisualization = + '!{lens{"timeRange":{"from":"now-7d","to":"now","mode":"relative"},"attributes":{"title":"TEst22","type":"lens","visualizationType":"lnsMetric","state":{"datasourceStates":{"indexpattern":{"layers":{"layer1":{"columnOrder":["col2"],"columns":{"col2":{"dataType":"number","isBucketed":false,"label":"Count of records","operationType":"count","scale":"ratio","sourceField":"Records"}}}}}},"visualization":{"layerId":"layer1","accessor":"col2"},"query":{"language":"kuery","query":""},"filters":[]},"references":[{"type":"index-pattern","id":"90943e30-9a47-11e8-b64d-95841ca0b247","name":"indexpattern-datasource-current-indexpattern"},{"type":"index-pattern","id":"90943e30-9a47-11e8-b64d-95841ca0b247","name":"indexpattern-datasource-layer-layer1"}]}}}'; + + const parsedNodes = parseCommentString(lensVisualization); + + expect(stringifyMarkdownComment(parsedNodes)).toEqual(`${lensVisualization}\n`); + }); }); }); diff --git a/x-pack/plugins/cases/common/utils/markdown_plugins/utils.ts b/x-pack/plugins/cases/common/utils/markdown_plugins/utils.ts index b6b061fcb41d9..e9bda7ae469e2 100644 --- a/x-pack/plugins/cases/common/utils/markdown_plugins/utils.ts +++ b/x-pack/plugins/cases/common/utils/markdown_plugins/utils.ts @@ -45,20 +45,13 @@ export const parseCommentString = (comment: string) => { export const stringifyMarkdownComment = (comment: MarkdownNode) => unified() .use([ - [ - remarkStringify, - { - allowDangerousHtml: true, - handlers: { - /* - because we're using rison in the timeline url we need - to make sure that markdown parser doesn't modify the url - */ - timeline: TimelineSerializer, - lens: LensSerializer, - }, - }, - ], + [remarkStringify], + /* + because we're using rison in the timeline url we need + to make sure that markdown parser doesn't modify the url + */ + LensSerializer, + TimelineSerializer, ]) .stringify(comment); diff --git a/x-pack/plugins/cases/server/authorization/__snapshots__/audit_logger.test.ts.snap b/x-pack/plugins/cases/server/authorization/__snapshots__/audit_logger.test.ts.snap index ed0fa1eb0f3ed..8933c70c8eaf0 100644 --- a/x-pack/plugins/cases/server/authorization/__snapshots__/audit_logger.test.ts.snap +++ b/x-pack/plugins/cases/server/authorization/__snapshots__/audit_logger.test.ts.snap @@ -924,6 +924,90 @@ Object { } `; +exports[`audit_logger log function event structure creates the correct audit event for operation: "getAttachmentMetrics" with an error and entity 1`] = ` +Object { + "error": Object { + "code": "Error", + "message": "an error", + }, + "event": Object { + "action": "case_comment_get_metrics", + "category": Array [ + "database", + ], + "outcome": "failure", + "type": Array [ + "access", + ], + }, + "kibana": Object { + "saved_object": Object { + "id": "1", + "type": "cases-comments", + }, + }, + "message": "Failed attempt to access cases-comments [id=1] as owner \\"awesome\\"", +} +`; + +exports[`audit_logger log function event structure creates the correct audit event for operation: "getAttachmentMetrics" with an error but no entity 1`] = ` +Object { + "error": Object { + "code": "Error", + "message": "an error", + }, + "event": Object { + "action": "case_comment_get_metrics", + "category": Array [ + "database", + ], + "outcome": "failure", + "type": Array [ + "access", + ], + }, + "message": "Failed attempt to access a comments as any owners", +} +`; + +exports[`audit_logger log function event structure creates the correct audit event for operation: "getAttachmentMetrics" without an error but with an entity 1`] = ` +Object { + "event": Object { + "action": "case_comment_get_metrics", + "category": Array [ + "database", + ], + "outcome": "success", + "type": Array [ + "access", + ], + }, + "kibana": Object { + "saved_object": Object { + "id": "5", + "type": "cases-comments", + }, + }, + "message": "User has accessed cases-comments [id=5] as owner \\"super\\"", +} +`; + +exports[`audit_logger log function event structure creates the correct audit event for operation: "getAttachmentMetrics" without an error or entity 1`] = ` +Object { + "event": Object { + "action": "case_comment_get_metrics", + "category": Array [ + "database", + ], + "outcome": "success", + "type": Array [ + "access", + ], + }, + "message": "User has accessed a comments as any owners", +} +`; + exports[`audit_logger log function event structure creates the correct audit event for operation: "getCase" with an error and entity 1`] = ` Object { "error": Object { diff --git a/x-pack/plugins/cases/server/authorization/index.ts b/x-pack/plugins/cases/server/authorization/index.ts index 057e85b460c2e..3596423860bf3 100644 --- a/x-pack/plugins/cases/server/authorization/index.ts +++ b/x-pack/plugins/cases/server/authorization/index.ts @@ -83,11 +83,47 @@ export function isWriteOperation(operation: OperationDetails): boolean { return Object.values(WriteOperations).includes(operation.name as WriteOperations); } -/** - * Definition of all APIs within the cases backend. - */ -export const Operations: Record = { - // case operations +const CaseOperations = { + [ReadOperations.GetCase]: { + ecsType: EVENT_TYPES.access, + name: ACCESS_CASE_OPERATION, + action: 'case_get', + verbs: accessVerbs, + docType: 'case', + savedObjectType: CASE_SAVED_OBJECT, + }, + [ReadOperations.ResolveCase]: { + ecsType: EVENT_TYPES.access, + name: ACCESS_CASE_OPERATION, + action: 'case_resolve', + verbs: accessVerbs, + docType: 'case', + savedObjectType: CASE_SAVED_OBJECT, + }, + [ReadOperations.FindCases]: { + ecsType: EVENT_TYPES.access, + name: ACCESS_CASE_OPERATION, + action: 'case_find', + verbs: accessVerbs, + docType: 'cases', + savedObjectType: CASE_SAVED_OBJECT, + }, + [ReadOperations.GetCaseIDsByAlertID]: { + ecsType: EVENT_TYPES.access, + name: ACCESS_CASE_OPERATION, + action: 'case_ids_by_alert_id_get', + verbs: accessVerbs, + docType: 'cases', + savedObjectType: CASE_COMMENT_SAVED_OBJECT, + }, + [ReadOperations.GetCaseMetrics]: { + ecsType: EVENT_TYPES.access, + name: ACCESS_CASE_OPERATION, + action: 'case_get_metrics', + verbs: accessVerbs, + docType: 'case', + savedObjectType: CASE_SAVED_OBJECT, + }, [WriteOperations.CreateCase]: { ecsType: EVENT_TYPES.creation, name: WriteOperations.CreateCase, @@ -120,6 +156,17 @@ export const Operations: Record = { + ...CaseOperations, + ...ConfigurationOperations, + ...AttachmentOperations, + [ReadOperations.GetTags]: { ecsType: EVENT_TYPES.access, - name: ACCESS_COMMENT_OPERATION, - action: 'case_comment_get_all', + name: ReadOperations.GetTags, + action: 'case_tags_get', verbs: accessVerbs, - docType: 'comments', - savedObjectType: CASE_COMMENT_SAVED_OBJECT, + docType: 'case', + savedObjectType: CASE_SAVED_OBJECT, }, - [ReadOperations.FindComments]: { + [ReadOperations.GetReporters]: { ecsType: EVENT_TYPES.access, - name: ACCESS_COMMENT_OPERATION, - action: 'case_comment_find', + name: ReadOperations.GetReporters, + action: 'case_reporters_get', verbs: accessVerbs, - docType: 'comments', - savedObjectType: CASE_COMMENT_SAVED_OBJECT, + docType: 'case', + savedObjectType: CASE_SAVED_OBJECT, }, - // stats operations [ReadOperations.GetCaseStatuses]: { ecsType: EVENT_TYPES.access, name: ACCESS_CASE_OPERATION, @@ -274,7 +291,6 @@ export const Operations: Record => { - const { unsecuredSavedObjectsClient, authorization, attachmentService } = clientArgs; - - // This will perform an authorization check to ensure the user has access to the parent case - const theCase = await casesClient.cases.get({ - id: caseId, - includeComments: false, - includeSubCaseComments: false, - }); - - const { filter: authorizationFilter, ensureSavedObjectsAreAuthorized } = - await authorization.getAuthorizationFilter(Operations.getAlertsAttachedToCase); - - const alerts = await attachmentService.getAllAlertsAttachToCase({ - unsecuredSavedObjectsClient, - caseId: theCase.id, - filter: authorizationFilter, - }); - - ensureSavedObjectsAreAuthorized( - alerts.map((alert) => ({ - owner: alert.attributes.owner, - id: alert.id, - })) - ); - - return normalizeAlertResponse(alerts); + const { unsecuredSavedObjectsClient, authorization, attachmentService, logger } = clientArgs; + + try { + // This will perform an authorization check to ensure the user has access to the parent case + const theCase = await casesClient.cases.get({ + id: caseId, + includeComments: false, + includeSubCaseComments: false, + }); + + const { filter: authorizationFilter, ensureSavedObjectsAreAuthorized } = + await authorization.getAuthorizationFilter(Operations.getAlertsAttachedToCase); + + const alerts = await attachmentService.getAllAlertsAttachToCase({ + unsecuredSavedObjectsClient, + caseId: theCase.id, + filter: authorizationFilter, + }); + + ensureSavedObjectsAreAuthorized( + alerts.map((alert) => ({ + owner: alert.attributes.owner, + id: alert.id, + })) + ); + + return normalizeAlertResponse(alerts); + } catch (error) { + throw createCaseError({ + message: `Failed to get alerts attached to case id: ${caseId}: ${error}`, + error, + logger, + }); + } }; /** diff --git a/x-pack/plugins/cases/server/client/metrics/alerts_count.ts b/x-pack/plugins/cases/server/client/metrics/alerts_count.ts index aa0e945bc5fcf..118761acb3680 100644 --- a/x-pack/plugins/cases/server/client/metrics/alerts_count.ts +++ b/x-pack/plugins/cases/server/client/metrics/alerts_count.ts @@ -6,18 +6,56 @@ */ import { CaseMetricsResponse } from '../../../common/api'; +import { Operations } from '../../authorization'; +import { createCaseError } from '../../common/error'; +import { CasesClient } from '../client'; +import { CasesClientArgs } from '../types'; import { MetricsHandler } from './types'; export class AlertsCount implements MetricsHandler { + constructor( + private readonly caseId: string, + private readonly casesClient: CasesClient, + private readonly clientArgs: CasesClientArgs + ) {} + public getFeatures(): Set { return new Set(['alertsCount']); } public async compute(): Promise { - return { - alerts: { - count: 0, - }, - }; + const { unsecuredSavedObjectsClient, authorization, attachmentService, logger } = + this.clientArgs; + + try { + // This will perform an authorization check to ensure the user has access to the parent case + const theCase = await this.casesClient.cases.get({ + id: this.caseId, + includeComments: false, + includeSubCaseComments: false, + }); + + const { filter: authorizationFilter } = await authorization.getAuthorizationFilter( + Operations.getAttachmentMetrics + ); + + const alertsCount = await attachmentService.countAlertsAttachedToCase({ + unsecuredSavedObjectsClient, + caseId: theCase.id, + filter: authorizationFilter, + }); + + return { + alerts: { + count: alertsCount ?? 0, + }, + }; + } catch (error) { + throw createCaseError({ + message: `Failed to count alerts attached case id: ${this.caseId}: ${error}`, + error, + logger, + }); + } } } diff --git a/x-pack/plugins/cases/server/client/metrics/get_case_metrics.test.ts b/x-pack/plugins/cases/server/client/metrics/get_case_metrics.test.ts index cd3c9204e3c03..b192e681df109 100644 --- a/x-pack/plugins/cases/server/client/metrics/get_case_metrics.test.ts +++ b/x-pack/plugins/cases/server/client/metrics/get_case_metrics.test.ts @@ -11,7 +11,7 @@ import { createCasesClientMock } from '../mocks'; import { CasesClientArgs } from '../types'; import { createAuthorizationMock } from '../../authorization/mock'; import { loggingSystemMock, savedObjectsClientMock } from '../../../../../../src/core/server/mocks'; -import { createCaseServiceMock } from '../../services/mocks'; +import { createAttachmentServiceMock, createCaseServiceMock } from '../../services/mocks'; import { SavedObject } from 'kibana/server'; describe('getMetrics', () => { @@ -28,7 +28,16 @@ describe('getMetrics', () => { } as unknown as CaseResponse; }); + const attachmentService = createAttachmentServiceMock(); + attachmentService.countAlertsAttachedToCase.mockImplementation(async () => { + return 5; + }); + const authorization = createAuthorizationMock(); + authorization.getAuthorizationFilter.mockImplementation(async () => { + return { filter: undefined, ensureSavedObjectsAreAuthorized: () => {} }; + }); + const soClient = savedObjectsClientMock.create(); const caseService = createCaseServiceMock(); caseService.getCase.mockImplementation(async () => { @@ -47,6 +56,7 @@ describe('getMetrics', () => { unsecuredSavedObjectsClient: soClient, caseService, logger, + attachmentService, } as unknown as CasesClientArgs; beforeEach(() => { @@ -100,7 +110,7 @@ describe('getMetrics', () => { clientArgs ); - expect(metrics.alerts?.count).toBeDefined(); + expect(metrics.alerts?.count).toEqual(5); expect(metrics.alerts?.hosts).toBeDefined(); }); diff --git a/x-pack/plugins/cases/server/client/metrics/get_case_metrics.ts b/x-pack/plugins/cases/server/client/metrics/get_case_metrics.ts index 74628ebd8c9ee..0cc089a1c0882 100644 --- a/x-pack/plugins/cases/server/client/metrics/get_case_metrics.ts +++ b/x-pack/plugins/cases/server/client/metrics/get_case_metrics.ts @@ -5,6 +5,7 @@ * 2.0. */ import { merge } from 'lodash'; +import Boom from '@hapi/boom'; import { CaseMetricsResponseRt, CaseMetricsResponse } from '../../../common/api'; import { Operations } from '../../authorization'; @@ -33,23 +34,33 @@ export const getCaseMetrics = async ( casesClient: CasesClient, clientArgs: CasesClientArgs ): Promise => { - const handlers = buildHandlers(params, casesClient, clientArgs); - await checkAuthorization(params, clientArgs); - checkAndThrowIfInvalidFeatures(params, handlers, clientArgs); + const { logger } = clientArgs; - const computedMetrics = await Promise.all( - params.features.map(async (feature) => { - const handler = handlers.get(feature); + try { + const handlers = buildHandlers(params, casesClient, clientArgs); + await checkAuthorization(params, clientArgs); + checkAndThrowIfInvalidFeatures(params, handlers); - return handler?.compute(); - }) - ); + const computedMetrics = await Promise.all( + params.features.map(async (feature) => { + const handler = handlers.get(feature); - const mergedResults = computedMetrics.reduce((acc, metric) => { - return merge(acc, metric); - }, {}); + return handler?.compute(); + }) + ); - return CaseMetricsResponseRt.encode(mergedResults ?? {}); + const mergedResults = computedMetrics.reduce((acc, metric) => { + return merge(acc, metric); + }, {}); + + return CaseMetricsResponseRt.encode(mergedResults ?? {}); + } catch (error) { + throw createCaseError({ + logger, + message: `Failed to retrieve metrics within client for case id: ${params.caseId}: ${error}`, + error, + }); + } }; const buildHandlers = ( @@ -59,7 +70,7 @@ const buildHandlers = ( ): Map => { const handlers = [ new Lifespan(params.caseId, casesClient), - new AlertsCount(), + new AlertsCount(params.caseId, casesClient, clientArgs), new AlertDetails(), new Connectors(), ]; @@ -75,18 +86,16 @@ const buildHandlers = ( const checkAndThrowIfInvalidFeatures = ( params: CaseMetricsParams, - handlers: Map, - clientArgs: CasesClientArgs + handlers: Map ) => { const invalidFeatures = params.features.filter((feature) => !handlers.has(feature)); if (invalidFeatures.length > 0) { const invalidFeaturesAsString = invalidFeatures.join(', '); const validFeaturesAsString = [...handlers.keys()].join(', '); - throw createCaseError({ - logger: clientArgs.logger, - message: `invalid features: [${invalidFeaturesAsString}], please only provide valid features: [${validFeaturesAsString}]`, - }); + throw Boom.badRequest( + `invalid features: [${invalidFeaturesAsString}], please only provide valid features: [${validFeaturesAsString}]` + ); } }; diff --git a/x-pack/plugins/cases/server/saved_object_types/migrations/comments.test.ts b/x-pack/plugins/cases/server/saved_object_types/migrations/comments.test.ts index 9e8ff1a334686..385c1c5945a11 100644 --- a/x-pack/plugins/cases/server/saved_object_types/migrations/comments.test.ts +++ b/x-pack/plugins/cases/server/saved_object_types/migrations/comments.test.ts @@ -5,7 +5,12 @@ * 2.0. */ -import { createCommentsMigrations, stringifyCommentWithoutTrailingNewline } from './comments'; +import { + createCommentsMigrations, + mergeMigrationFunctionMaps, + migrateByValueLensVisualizations, + stringifyCommentWithoutTrailingNewline, +} from './comments'; import { getLensVisualizations, parseCommentString, @@ -14,84 +19,98 @@ import { import { savedObjectsServiceMock } from '../../../../../../src/core/server/mocks'; import { lensEmbeddableFactory } from '../../../../lens/server/embeddable/lens_embeddable_factory'; import { LensDocShape715 } from '../../../../lens/server'; -import { SavedObjectReference } from 'kibana/server'; +import { + SavedObjectReference, + SavedObjectsMigrationLogger, + SavedObjectUnsanitizedDoc, +} from 'kibana/server'; +import { + MigrateFunction, + MigrateFunctionsObject, +} from '../../../../../../src/plugins/kibana_utils/common'; +import { SerializableRecord } from '@kbn/utility-types'; -const migrations = createCommentsMigrations({ - lensEmbeddableFactory, -}); +describe('comments migrations', () => { + const migrations = createCommentsMigrations({ + lensEmbeddableFactory, + }); -const contextMock = savedObjectsServiceMock.createMigrationContext(); -describe('index migrations', () => { - describe('lens embeddable migrations for by value panels', () => { - describe('7.14.0 remove time zone from Lens visualization date histogram', () => { - const lensVisualizationToMigrate = { - title: 'MyRenamedOps', - description: '', - visualizationType: 'lnsXY', - state: { - datasourceStates: { - indexpattern: { - layers: { - '2': { - columns: { - '3': { - label: '@timestamp', - dataType: 'date', - operationType: 'date_histogram', - sourceField: '@timestamp', - isBucketed: true, - scale: 'interval', - params: { interval: 'auto', timeZone: 'Europe/Berlin' }, - }, - '4': { - label: '@timestamp', - dataType: 'date', - operationType: 'date_histogram', - sourceField: '@timestamp', - isBucketed: true, - scale: 'interval', - params: { interval: 'auto' }, - }, - '5': { - label: '@timestamp', - dataType: 'date', - operationType: 'my_unexpected_operation', - isBucketed: true, - scale: 'interval', - params: { timeZone: 'do not delete' }, - }, - }, - columnOrder: ['3', '4', '5'], - incompleteColumns: {}, + const contextMock = savedObjectsServiceMock.createMigrationContext(); + + const lensVisualizationToMigrate = { + title: 'MyRenamedOps', + description: '', + visualizationType: 'lnsXY', + state: { + datasourceStates: { + indexpattern: { + layers: { + '2': { + columns: { + '3': { + label: '@timestamp', + dataType: 'date', + operationType: 'date_histogram', + sourceField: '@timestamp', + isBucketed: true, + scale: 'interval', + params: { interval: 'auto', timeZone: 'Europe/Berlin' }, + }, + '4': { + label: '@timestamp', + dataType: 'date', + operationType: 'date_histogram', + sourceField: '@timestamp', + isBucketed: true, + scale: 'interval', + params: { interval: 'auto' }, + }, + '5': { + label: '@timestamp', + dataType: 'date', + operationType: 'my_unexpected_operation', + isBucketed: true, + scale: 'interval', + params: { timeZone: 'do not delete' }, }, }, + columnOrder: ['3', '4', '5'], + incompleteColumns: {}, }, }, - visualization: { - title: 'Empty XY chart', - legend: { isVisible: true, position: 'right' }, - valueLabels: 'hide', - preferredSeriesType: 'bar_stacked', - layers: [ - { - layerId: '5ab74ddc-93ca-44e2-9857-ecf85c86b53e', - accessors: [ - '5fea2a56-7b73-44b5-9a50-7f0c0c4f8fd0', - 'e5efca70-edb5-4d6d-a30a-79384066987e', - '7ffb7bde-4f42-47ab-b74d-1b4fd8393e0f', - ], - position: 'top', - seriesType: 'bar_stacked', - showGridlines: false, - xAccessor: '2e57a41e-5a52-42d3-877f-bd211d903ef8', - }, + }, + }, + visualization: { + title: 'Empty XY chart', + legend: { isVisible: true, position: 'right' }, + valueLabels: 'hide', + preferredSeriesType: 'bar_stacked', + layers: [ + { + layerId: '5ab74ddc-93ca-44e2-9857-ecf85c86b53e', + accessors: [ + '5fea2a56-7b73-44b5-9a50-7f0c0c4f8fd0', + 'e5efca70-edb5-4d6d-a30a-79384066987e', + '7ffb7bde-4f42-47ab-b74d-1b4fd8393e0f', ], + position: 'top', + seriesType: 'bar_stacked', + showGridlines: false, + xAccessor: '2e57a41e-5a52-42d3-877f-bd211d903ef8', }, - query: { query: '', language: 'kuery' }, - filters: [], - }, - }; + ], + }, + query: { query: '', language: 'kuery' }, + filters: [], + }, + }; + + beforeEach(() => { + jest.clearAllMocks(); + }); + describe('lens embeddable migrations for by value panels', () => { + describe('7.14.0 remove time zone from Lens visualization date histogram', () => { const expectedLensVisualizationMigrated = { title: 'MyRenamedOps', description: '', @@ -241,43 +260,140 @@ describe('index migrations', () => { expect((columns[2] as { params: {} }).params).toEqual({ timeZone: 'do not delete' }); }); }); + }); - describe('stringifyCommentWithoutTrailingNewline', () => { - it('removes the newline added by the markdown library when the comment did not originally have one', () => { - const originalComment = 'awesome'; - const parsedString = parseCommentString(originalComment); + describe('handles errors', () => { + interface CommentSerializable extends SerializableRecord { + comment?: string; + } - expect(stringifyCommentWithoutTrailingNewline(originalComment, parsedString)).toEqual( - 'awesome' - ); - }); + const migrationFunction: MigrateFunction = ( + comment + ) => { + throw new Error('an error'); + }; - it('leaves the newline if it was in the original comment', () => { - const originalComment = 'awesome\n'; - const parsedString = parseCommentString(originalComment); + const comment = `!{lens{\"timeRange\":{\"from\":\"now-7d\",\"to\":\"now\",\"mode\":\"relative\"},\"editMode\":false,\"attributes\":${JSON.stringify( + lensVisualizationToMigrate + )}}}\n\n`; - expect(stringifyCommentWithoutTrailingNewline(originalComment, parsedString)).toEqual( - 'awesome\n' - ); - }); + const caseComment = { + type: 'cases-comments', + id: '1cefd0d0-e86d-11eb-bae5-3d065cd16a32', + attributes: { + comment, + }, + references: [], + }; - it('does not remove newlines that are not at the end of the comment', () => { - const originalComment = 'awesome\ncomment'; - const parsedString = parseCommentString(originalComment); + it('logs an error when it fails to parse invalid json', () => { + const commentMigrationFunction = migrateByValueLensVisualizations(migrationFunction, '1.0.0'); - expect(stringifyCommentWithoutTrailingNewline(originalComment, parsedString)).toEqual( - 'awesome\ncomment' - ); + const result = commentMigrationFunction(caseComment, contextMock); + // the comment should remain unchanged when there is an error + expect(result.attributes.comment).toEqual(comment); + + const log = contextMock.log as jest.Mocked; + expect(log.error.mock.calls[0]).toMatchInlineSnapshot(` + Array [ + "Failed to migrate comment with doc id: 1cefd0d0-e86d-11eb-bae5-3d065cd16a32 version: 8.0.0 error: an error", + Object { + "migrations": Object { + "comment": Object { + "id": "1cefd0d0-e86d-11eb-bae5-3d065cd16a32", + }, + }, + }, + ] + `); + }); + + describe('mergeMigrationFunctionMaps', () => { + it('logs an error when the passed migration functions fails', () => { + const migrationObj1 = { + '1.0.0': migrateByValueLensVisualizations(migrationFunction, '1.0.0'), + } as unknown as MigrateFunctionsObject; + + const migrationObj2 = { + '2.0.0': (doc: SavedObjectUnsanitizedDoc<{ comment?: string }>) => { + return doc; + }, + }; + + const mergedFunctions = mergeMigrationFunctionMaps(migrationObj1, migrationObj2); + mergedFunctions['1.0.0'](caseComment, contextMock); + + const log = contextMock.log as jest.Mocked; + expect(log.error.mock.calls[0]).toMatchInlineSnapshot(` + Array [ + "Failed to migrate comment with doc id: 1cefd0d0-e86d-11eb-bae5-3d065cd16a32 version: 8.0.0 error: an error", + Object { + "migrations": Object { + "comment": Object { + "id": "1cefd0d0-e86d-11eb-bae5-3d065cd16a32", + }, + }, + }, + ] + `); }); - it('does not remove spaces at the end of the comment', () => { - const originalComment = 'awesome '; - const parsedString = parseCommentString(originalComment); + it('it does not log an error when the migration function does not use the context', () => { + const migrationObj1 = { + '1.0.0': migrateByValueLensVisualizations(migrationFunction, '1.0.0'), + } as unknown as MigrateFunctionsObject; - expect(stringifyCommentWithoutTrailingNewline(originalComment, parsedString)).toEqual( - 'awesome ' - ); + const migrationObj2 = { + '2.0.0': (doc: SavedObjectUnsanitizedDoc<{ comment?: string }>) => { + throw new Error('2.0.0 error'); + }, + }; + + const mergedFunctions = mergeMigrationFunctionMaps(migrationObj1, migrationObj2); + + expect(() => mergedFunctions['2.0.0'](caseComment, contextMock)).toThrow(); + + const log = contextMock.log as jest.Mocked; + expect(log.error).not.toHaveBeenCalled(); }); }); }); + + describe('stringifyCommentWithoutTrailingNewline', () => { + it('removes the newline added by the markdown library when the comment did not originally have one', () => { + const originalComment = 'awesome'; + const parsedString = parseCommentString(originalComment); + + expect(stringifyCommentWithoutTrailingNewline(originalComment, parsedString)).toEqual( + 'awesome' + ); + }); + + it('leaves the newline if it was in the original comment', () => { + const originalComment = 'awesome\n'; + const parsedString = parseCommentString(originalComment); + + expect(stringifyCommentWithoutTrailingNewline(originalComment, parsedString)).toEqual( + 'awesome\n' + ); + }); + + it('does not remove newlines that are not at the end of the comment', () => { + const originalComment = 'awesome\ncomment'; + const parsedString = parseCommentString(originalComment); + + expect(stringifyCommentWithoutTrailingNewline(originalComment, parsedString)).toEqual( + 'awesome\ncomment' + ); + }); + + it('does not remove spaces at the end of the comment', () => { + const originalComment = 'awesome '; + const parsedString = parseCommentString(originalComment); + + expect(stringifyCommentWithoutTrailingNewline(originalComment, parsedString)).toEqual( + 'awesome ' + ); + }); + }); }); diff --git a/x-pack/plugins/cases/server/saved_object_types/migrations/comments.ts b/x-pack/plugins/cases/server/saved_object_types/migrations/comments.ts index e67e1c8b59887..0af9db13fce40 100644 --- a/x-pack/plugins/cases/server/saved_object_types/migrations/comments.ts +++ b/x-pack/plugins/cases/server/saved_object_types/migrations/comments.ts @@ -5,12 +5,9 @@ * 2.0. */ -import { mapValues, trimEnd } from 'lodash'; -import { SerializableRecord } from '@kbn/utility-types'; - -import { LensServerPluginSetup } from '../../../../lens/server'; +import { mapValues, trimEnd, mergeWith } from 'lodash'; +import type { SerializableRecord } from '@kbn/utility-types'; import { - mergeMigrationFunctionMaps, MigrateFunction, MigrateFunctionsObject, } from '../../../../../../src/plugins/kibana_utils/common'; @@ -19,7 +16,9 @@ import { SavedObjectSanitizedDoc, SavedObjectMigrationFn, SavedObjectMigrationMap, + SavedObjectMigrationContext, } from '../../../../../../src/core/server'; +import { LensServerPluginSetup } from '../../../../lens/server'; import { CommentType, AssociationType } from '../../../common/api'; import { isLensMarkdownNode, @@ -29,6 +28,7 @@ import { stringifyMarkdownComment, } from '../../../common/utils/markdown_plugins/utils'; import { addOwnerToSO, SanitizedCaseOwner } from '.'; +import { logError } from './utils'; interface UnsanitizedComment { comment: string; @@ -103,33 +103,41 @@ export const createCommentsMigrations = ( return mergeMigrationFunctionMaps(commentsMigrations, embeddableMigrations); }; -const migrateByValueLensVisualizations = - (migrate: MigrateFunction, version: string): SavedObjectMigrationFn<{ comment?: string }> => - (doc: SavedObjectUnsanitizedDoc<{ comment?: string }>) => { +export const migrateByValueLensVisualizations = + ( + migrate: MigrateFunction, + version: string + ): SavedObjectMigrationFn<{ comment?: string }, { comment?: string }> => + (doc: SavedObjectUnsanitizedDoc<{ comment?: string }>, context: SavedObjectMigrationContext) => { if (doc.attributes.comment == null) { return doc; } - const parsedComment = parseCommentString(doc.attributes.comment); - const migratedComment = parsedComment.children.map((comment) => { - if (isLensMarkdownNode(comment)) { - // casting here because ts complains that comment isn't serializable because LensMarkdownNode - // extends Node which has fields that conflict with SerializableRecord even though it is serializable - return migrate(comment as SerializableRecord) as LensMarkdownNode; - } + try { + const parsedComment = parseCommentString(doc.attributes.comment); + const migratedComment = parsedComment.children.map((comment) => { + if (isLensMarkdownNode(comment)) { + // casting here because ts complains that comment isn't serializable because LensMarkdownNode + // extends Node which has fields that conflict with SerializableRecord even though it is serializable + return migrate(comment as SerializableRecord) as LensMarkdownNode; + } - return comment; - }); + return comment; + }); - const migratedMarkdown = { ...parsedComment, children: migratedComment }; + const migratedMarkdown = { ...parsedComment, children: migratedComment }; - return { - ...doc, - attributes: { - ...doc.attributes, - comment: stringifyCommentWithoutTrailingNewline(doc.attributes.comment, migratedMarkdown), - }, - }; + return { + ...doc, + attributes: { + ...doc.attributes, + comment: stringifyCommentWithoutTrailingNewline(doc.attributes.comment, migratedMarkdown), + }, + }; + } catch (error) { + logError({ id: doc.id, context, error, docType: 'comment', docKey: 'comment' }); + return doc; + } }; export const stringifyCommentWithoutTrailingNewline = ( @@ -147,3 +155,23 @@ export const stringifyCommentWithoutTrailingNewline = ( // so the comment stays consistent return trimEnd(stringifiedComment, '\n'); }; + +/** + * merge function maps adds the context param from the original implementation at: + * src/plugins/kibana_utils/common/persistable_state/merge_migration_function_map.ts + * */ +export const mergeMigrationFunctionMaps = ( + // using the saved object framework types here because they include the context, this avoids type errors in our tests + obj1: SavedObjectMigrationMap, + obj2: SavedObjectMigrationMap +) => { + const customizer = (objValue: SavedObjectMigrationFn, srcValue: SavedObjectMigrationFn) => { + if (!srcValue || !objValue) { + return srcValue || objValue; + } + return (doc: SavedObjectUnsanitizedDoc, context: SavedObjectMigrationContext) => + objValue(srcValue(doc, context), context); + }; + + return mergeWith({ ...obj1 }, obj2, customizer); +}; diff --git a/x-pack/plugins/cases/server/saved_object_types/migrations/user_actions.test.ts b/x-pack/plugins/cases/server/saved_object_types/migrations/user_actions.test.ts index e9ba80322c222..3d0cff814b7d4 100644 --- a/x-pack/plugins/cases/server/saved_object_types/migrations/user_actions.test.ts +++ b/x-pack/plugins/cases/server/saved_object_types/migrations/user_actions.test.ts @@ -7,7 +7,11 @@ /* eslint-disable @typescript-eslint/naming-convention */ -import { SavedObjectMigrationContext, SavedObjectSanitizedDoc } from 'kibana/server'; +import { + SavedObjectMigrationContext, + SavedObjectSanitizedDoc, + SavedObjectsMigrationLogger, +} from 'kibana/server'; import { migrationMocks } from 'src/core/server/mocks'; import { CaseUserActionAttributes } from '../../../common/api'; import { CASE_USER_ACTION_SAVED_OBJECT } from '../../../common/constants'; @@ -217,7 +221,19 @@ describe('user action migrations', () => { userActionsConnectorIdMigration(userAction, context); - expect(context.log.error).toHaveBeenCalled(); + const log = context.log as jest.Mocked; + expect(log.error.mock.calls[0]).toMatchInlineSnapshot(` + Array [ + "Failed to migrate user action connector with doc id: 1 version: 8.0.0 error: Unexpected token a in JSON at position 1", + Object { + "migrations": Object { + "userAction": Object { + "id": "1", + }, + }, + }, + ] + `); }); }); @@ -385,7 +401,19 @@ describe('user action migrations', () => { userActionsConnectorIdMigration(userAction, context); - expect(context.log.error).toHaveBeenCalled(); + const log = context.log as jest.Mocked; + expect(log.error.mock.calls[0]).toMatchInlineSnapshot(` + Array [ + "Failed to migrate user action connector with doc id: 1 version: 8.0.0 error: Unexpected token b in JSON at position 1", + Object { + "migrations": Object { + "userAction": Object { + "id": "1", + }, + }, + }, + ] + `); }); }); @@ -555,7 +583,19 @@ describe('user action migrations', () => { userActionsConnectorIdMigration(userAction, context); - expect(context.log.error).toHaveBeenCalled(); + const log = context.log as jest.Mocked; + expect(log.error.mock.calls[0]).toMatchInlineSnapshot(` + Array [ + "Failed to migrate user action connector with doc id: 1 version: 8.0.0 error: Unexpected token e in JSON at position 1", + Object { + "migrations": Object { + "userAction": Object { + "id": "1", + }, + }, + }, + ] + `); }); }); }); diff --git a/x-pack/plugins/cases/server/saved_object_types/migrations/user_actions.ts b/x-pack/plugins/cases/server/saved_object_types/migrations/user_actions.ts index a47104dfed5f7..4d8395eb189fc 100644 --- a/x-pack/plugins/cases/server/saved_object_types/migrations/user_actions.ts +++ b/x-pack/plugins/cases/server/saved_object_types/migrations/user_actions.ts @@ -12,13 +12,13 @@ import { SavedObjectUnsanitizedDoc, SavedObjectSanitizedDoc, SavedObjectMigrationContext, - LogMeta, } from '../../../../../../src/core/server'; import { isPush, isUpdateConnector, isCreateConnector } from '../../../common/utils/user_actions'; import { ConnectorTypes } from '../../../common/api'; import { extractConnectorIdFromJson } from '../../services/user_actions/transform'; import { UserActionFieldType } from '../../services/user_actions/types'; +import { logError } from './utils'; interface UserActions { action_field: string[]; @@ -33,10 +33,6 @@ interface UserActionUnmigratedConnectorDocument { old_value?: string | null; } -interface UserActionLogMeta extends LogMeta { - migrations: { userAction: { id: string } }; -} - export function userActionsConnectorIdMigration( doc: SavedObjectUnsanitizedDoc, context: SavedObjectMigrationContext @@ -50,7 +46,13 @@ export function userActionsConnectorIdMigration( try { return formatDocumentWithConnectorReferences(doc); } catch (error) { - logError(doc.id, context, error); + logError({ + id: doc.id, + context, + error, + docType: 'user action connector', + docKey: 'userAction', + }); return originalDocWithReferences; } @@ -99,19 +101,6 @@ function formatDocumentWithConnectorReferences( }; } -function logError(id: string, context: SavedObjectMigrationContext, error: Error) { - context.log.error( - `Failed to migrate user action connector doc id: ${id} version: ${context.migrationVersion} error: ${error.message}`, - { - migrations: { - userAction: { - id, - }, - }, - } - ); -} - export const userActionsMigrations = { '7.10.0': (doc: SavedObjectUnsanitizedDoc): SavedObjectSanitizedDoc => { const { action_field, new_value, old_value, ...restAttributes } = doc.attributes; diff --git a/x-pack/plugins/cases/server/saved_object_types/migrations/utils.test.ts b/x-pack/plugins/cases/server/saved_object_types/migrations/utils.test.ts new file mode 100644 index 0000000000000..565688cd6ac3c --- /dev/null +++ b/x-pack/plugins/cases/server/saved_object_types/migrations/utils.test.ts @@ -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 + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { SavedObjectsMigrationLogger } from 'kibana/server'; +import { migrationMocks } from '../../../../../../src/core/server/mocks'; +import { logError } from './utils'; + +describe('migration utils', () => { + const context = migrationMocks.createContext(); + + beforeEach(() => { + jest.clearAllMocks(); + }); + + it('logs an error', () => { + const log = context.log as jest.Mocked; + + logError({ + id: '1', + context, + error: new Error('an error'), + docType: 'a document', + docKey: 'key', + }); + + expect(log.error.mock.calls[0]).toMatchInlineSnapshot(` + Array [ + "Failed to migrate a document with doc id: 1 version: 8.0.0 error: an error", + Object { + "migrations": Object { + "key": Object { + "id": "1", + }, + }, + }, + ] + `); + }); +}); diff --git a/x-pack/plugins/cases/server/saved_object_types/migrations/utils.ts b/x-pack/plugins/cases/server/saved_object_types/migrations/utils.ts new file mode 100644 index 0000000000000..993d70181974d --- /dev/null +++ b/x-pack/plugins/cases/server/saved_object_types/migrations/utils.ts @@ -0,0 +1,41 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { LogMeta, SavedObjectMigrationContext } from '../../../../../../src/core/server'; + +interface MigrationLogMeta extends LogMeta { + migrations: { + [x: string]: { + id: string; + }; + }; +} + +export function logError({ + id, + context, + error, + docType, + docKey, +}: { + id: string; + context: SavedObjectMigrationContext; + error: Error; + docType: string; + docKey: string; +}) { + context.log.error( + `Failed to migrate ${docType} with doc id: ${id} version: ${context.migrationVersion} error: ${error.message}`, + { + migrations: { + [docKey]: { + id, + }, + }, + } + ); +} diff --git a/x-pack/plugins/cases/server/services/attachments/index.ts b/x-pack/plugins/cases/server/services/attachments/index.ts index f4e858eb0ed4f..9553f7c7ed1e2 100644 --- a/x-pack/plugins/cases/server/services/attachments/index.ts +++ b/x-pack/plugins/cases/server/services/attachments/index.ts @@ -12,6 +12,7 @@ import { SavedObjectsUpdateOptions, } from 'kibana/server'; +import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; import type { KueryNode } from '@kbn/es-query'; import { AttributesTypeAlerts, @@ -26,12 +27,15 @@ import { } from '../../../common/constants'; import { ClientArgs } from '..'; import { buildFilter, combineFilters } from '../../client/utils'; +import { defaultSortField } from '../../common/utils'; interface GetAllAlertsAttachToCaseArgs extends ClientArgs { caseId: string; filter?: KueryNode; } +type CountAlertsAttachedToCaseArgs = GetAllAlertsAttachToCaseArgs; + interface GetAttachmentArgs extends ClientArgs { attachmentId: string; } @@ -57,6 +61,51 @@ interface BulkUpdateAttachmentArgs extends ClientArgs { export class AttachmentService { constructor(private readonly log: Logger) {} + public async countAlertsAttachedToCase({ + unsecuredSavedObjectsClient, + caseId, + filter, + }: CountAlertsAttachedToCaseArgs): Promise { + try { + this.log.debug(`Attempting to count alerts for case id ${caseId}`); + const alertsFilter = buildFilter({ + filters: [CommentType.alert, CommentType.generatedAlert], + field: 'type', + operator: 'or', + type: CASE_COMMENT_SAVED_OBJECT, + }); + + const combinedFilter = combineFilters([alertsFilter, filter]); + + const response = await unsecuredSavedObjectsClient.find< + AttachmentAttributes, + { alerts: { value: number } } + >({ + type: CASE_COMMENT_SAVED_OBJECT, + page: 1, + perPage: 1, + sortField: defaultSortField, + aggs: this.buildCountAlertsAggs(), + filter: combinedFilter, + }); + + return response.aggregations?.alerts?.value; + } catch (error) { + this.log.error(`Error while counting alerts for case id ${caseId}: ${error}`); + throw error; + } + } + + private buildCountAlertsAggs(): Record { + return { + alerts: { + cardinality: { + field: `${CASE_COMMENT_SAVED_OBJECT}.attributes.alertId`, + }, + }, + }; + } + /** * Retrieves all the alerts attached to a case. */ diff --git a/x-pack/plugins/cases/server/services/mocks.ts b/x-pack/plugins/cases/server/services/mocks.ts index f46bcd0906c60..3e68126967512 100644 --- a/x-pack/plugins/cases/server/services/mocks.ts +++ b/x-pack/plugins/cases/server/services/mocks.ts @@ -111,6 +111,7 @@ export const createAttachmentServiceMock = (): AttachmentServiceMock => { update: jest.fn(), bulkUpdate: jest.fn(), getAllAlertsAttachToCase: jest.fn(), + countAlertsAttachedToCase: jest.fn(), }; // the cast here is required because jest.Mocked tries to include private members and would throw an error diff --git a/x-pack/plugins/cloud/public/plugin.ts b/x-pack/plugins/cloud/public/plugin.ts index e71b145c438ed..81aad8bf79ccc 100644 --- a/x-pack/plugins/cloud/public/plugin.ts +++ b/x-pack/plugins/cloud/public/plugin.ts @@ -98,7 +98,7 @@ export class CloudPlugin implements Plugin { if (home) { home.environment.update({ cloud: this.isCloudEnabled }); if (this.isCloudEnabled) { - home.tutorials.setVariable('cloud', { id, baseUrl, profileUrl }); + home.tutorials.setVariable('cloud', { id, baseUrl, profileUrl, deploymentUrl }); } } diff --git a/x-pack/plugins/cross_cluster_replication/public/app/index.tsx b/x-pack/plugins/cross_cluster_replication/public/app/index.tsx index ea3eb50c46089..d6dc16a55a99f 100644 --- a/x-pack/plugins/cross_cluster_replication/public/app/index.tsx +++ b/x-pack/plugins/cross_cluster_replication/public/app/index.tsx @@ -8,9 +8,17 @@ import React from 'react'; import { render, unmountComponentAtNode } from 'react-dom'; import { Provider } from 'react-redux'; -import { I18nStart, ScopedHistory, ApplicationStart } from 'kibana/public'; -import { UnmountCallback } from 'src/core/public'; -import { DocLinksStart } from 'kibana/public'; +import { Observable } from 'rxjs'; + +import { + UnmountCallback, + I18nStart, + ScopedHistory, + ApplicationStart, + DocLinksStart, + CoreTheme, +} from 'src/core/public'; +import { KibanaThemeProvider } from '../shared_imports'; import { init as initBreadcrumbs, SetBreadcrumbs } from './services/breadcrumbs'; import { init as initDocumentation } from './services/documentation_links'; import { App } from './app'; @@ -20,13 +28,16 @@ const renderApp = ( element: Element, I18nContext: I18nStart['Context'], history: ScopedHistory, - getUrlForApp: ApplicationStart['getUrlForApp'] + getUrlForApp: ApplicationStart['getUrlForApp'], + theme$: Observable ): UnmountCallback => { render( - - - + + + + + , element ); @@ -41,6 +52,7 @@ export async function mountApp({ docLinks, history, getUrlForApp, + theme$, }: { element: Element; setBreadcrumbs: SetBreadcrumbs; @@ -48,11 +60,12 @@ export async function mountApp({ docLinks: DocLinksStart; history: ScopedHistory; getUrlForApp: ApplicationStart['getUrlForApp']; + theme$: Observable; }): Promise { // Import and initialize additional services here instead of in plugin.ts to reduce the size of the // initial bundle as much as possible. initBreadcrumbs(setBreadcrumbs); initDocumentation(docLinks); - return renderApp(element, I18nContext, history, getUrlForApp); + return renderApp(element, I18nContext, history, getUrlForApp, theme$); } diff --git a/x-pack/plugins/cross_cluster_replication/public/plugin.ts b/x-pack/plugins/cross_cluster_replication/public/plugin.ts index a45862d46beeb..bc2546bdacb2a 100644 --- a/x-pack/plugins/cross_cluster_replication/public/plugin.ts +++ b/x-pack/plugins/cross_cluster_replication/public/plugin.ts @@ -41,7 +41,7 @@ export class CrossClusterReplicationPlugin implements Plugin { id: MANAGEMENT_ID, title: PLUGIN.TITLE, order: 6, - mount: async ({ element, setBreadcrumbs, history }) => { + mount: async ({ element, setBreadcrumbs, history, theme$ }) => { const { mountApp } = await import('./app'); const [coreStart] = await getStartServices(); @@ -61,6 +61,7 @@ export class CrossClusterReplicationPlugin implements Plugin { docLinks, history, getUrlForApp, + theme$, }); return () => { diff --git a/x-pack/plugins/cross_cluster_replication/public/shared_imports.ts b/x-pack/plugins/cross_cluster_replication/public/shared_imports.ts index 38838968ad212..f850e054f9667 100644 --- a/x-pack/plugins/cross_cluster_replication/public/shared_imports.ts +++ b/x-pack/plugins/cross_cluster_replication/public/shared_imports.ts @@ -13,4 +13,6 @@ export { PageLoading, } from '../../../../src/plugins/es_ui_shared/public'; +export { KibanaThemeProvider } from '../../../../src/plugins/kibana_react/public'; + export { APP_WRAPPER_CLASS } from '../../../../src/core/public'; diff --git a/x-pack/plugins/enterprise_search/common/__mocks__/initial_app_data.ts b/x-pack/plugins/enterprise_search/common/__mocks__/initial_app_data.ts index dd6dd58d02f70..bbb08d0ac2b66 100644 --- a/x-pack/plugins/enterprise_search/common/__mocks__/initial_app_data.ts +++ b/x-pack/plugins/enterprise_search/common/__mocks__/initial_app_data.ts @@ -8,6 +8,7 @@ export const DEFAULT_INITIAL_APP_DATA = { kibanaVersion: '7.16.0', enterpriseSearchVersion: '7.16.0', + errorConnectingMessage: '', readOnlyMode: false, searchOAuth: { clientId: 'someUID', diff --git a/x-pack/plugins/enterprise_search/common/types/index.ts b/x-pack/plugins/enterprise_search/common/types/index.ts index 57fe3f3807783..17b3eb17d31bd 100644 --- a/x-pack/plugins/enterprise_search/common/types/index.ts +++ b/x-pack/plugins/enterprise_search/common/types/index.ts @@ -17,6 +17,7 @@ import { export interface InitialAppData { enterpriseSearchVersion?: string; kibanaVersion?: string; + errorConnectingMessage?: string; readOnlyMode?: boolean; searchOAuth?: SearchOAuth; configuredLimits?: ConfiguredLimits; diff --git a/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/components/add_domain/add_domain_flyout.tsx b/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/components/add_domain/add_domain_flyout.tsx index f8511d1e2ef14..f953e8e0fce39 100644 --- a/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/components/add_domain/add_domain_flyout.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/components/add_domain/add_domain_flyout.tsx @@ -64,7 +64,14 @@ export const AddDomainFlyout: React.FC = () => { - }> + + + + + } + > {i18n.translate( 'xpack.enterpriseSearch.appSearch.crawler.addDomainFlyout.description', diff --git a/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/components/crawl_details_flyout/crawl_details_flyout.test.tsx b/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/components/crawl_details_flyout/crawl_details_flyout.test.tsx index 1810b05a938da..9f46d6750590f 100644 --- a/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/components/crawl_details_flyout/crawl_details_flyout.test.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/components/crawl_details_flyout/crawl_details_flyout.test.tsx @@ -5,6 +5,7 @@ * 2.0. */ import { setMockActions, setMockValues } from '../../../../../__mocks__/kea_logic'; +import '../../../../../__mocks__/shallow_useeffect.mock'; import '../../../../__mocks__/engine_logic.mock'; import React from 'react'; @@ -15,21 +16,26 @@ import { EuiCodeBlock, EuiFlyout, EuiTab, EuiTabs } from '@elastic/eui'; import { Loading } from '../../../../../shared/loading'; -import { CrawlDetailActions, CrawlDetailValues } from '../../crawl_detail_logic'; import { CrawlRequestWithDetailsFromServer } from '../../types'; import { CrawlDetailsPreview } from './crawl_details_preview'; import { CrawlDetailsFlyout } from '.'; -const MOCK_VALUES: Partial = { +const MOCK_VALUES = { dataLoading: false, flyoutClosed: false, crawlRequestFromServer: {} as CrawlRequestWithDetailsFromServer, + logRetention: { + crawler: { + enabled: true, + }, + }, }; -const MOCK_ACTIONS: Partial = { +const MOCK_ACTIONS = { setSelectedTab: jest.fn(), + fetchLogRetention: jest.fn(), }; describe('CrawlDetailsFlyout', () => { @@ -38,6 +44,7 @@ describe('CrawlDetailsFlyout', () => { }); it('renders a flyout ', () => { + setMockActions(MOCK_ACTIONS); setMockValues(MOCK_VALUES); const wrapper = shallow(); @@ -82,7 +89,22 @@ describe('CrawlDetailsFlyout', () => { it('shows the human readable version of the crawl details', () => { const wrapper = shallow(); - expect(wrapper.find(CrawlDetailsPreview)).toHaveLength(1); + const crawlDetailsPreview = wrapper.find(CrawlDetailsPreview); + expect(crawlDetailsPreview).toHaveLength(1); + expect(crawlDetailsPreview.prop('crawlerLogsEnabled')).toEqual(true); + }); + + it('shows the preview differently if the crawler logs are disabled', () => { + setMockValues({ + ...MOCK_VALUES, + selectedTab: 'preview', + logRetention: null, + }); + const wrapper = shallow(); + + const crawlDetailsPreview = wrapper.find(CrawlDetailsPreview); + expect(crawlDetailsPreview).toHaveLength(1); + expect(crawlDetailsPreview.prop('crawlerLogsEnabled')).toEqual(false); }); }); diff --git a/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/components/crawl_details_flyout/crawl_details_flyout.tsx b/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/components/crawl_details_flyout/crawl_details_flyout.tsx index 9c3c1da534f72..f1bfd3e5e5e9b 100644 --- a/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/components/crawl_details_flyout/crawl_details_flyout.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/components/crawl_details_flyout/crawl_details_flyout.tsx @@ -4,7 +4,7 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ -import React from 'react'; +import React, { useEffect } from 'react'; import { useActions, useValues } from 'kea'; @@ -21,6 +21,7 @@ import { import { i18n } from '@kbn/i18n'; import { Loading } from '../../../../../shared/loading'; +import { LogRetentionLogic } from '../../../log_retention'; import { CrawlDetailLogic } from '../../crawl_detail_logic'; import { CrawlDetailsPreview } from './crawl_details_preview'; @@ -29,6 +30,12 @@ export const CrawlDetailsFlyout: React.FC = () => { const { closeFlyout, setSelectedTab } = useActions(CrawlDetailLogic); const { crawlRequestFromServer, dataLoading, flyoutClosed, selectedTab } = useValues(CrawlDetailLogic); + const { fetchLogRetention } = useActions(LogRetentionLogic); + const { logRetention } = useValues(LogRetentionLogic); + + useEffect(() => { + fetchLogRetention(); + }, []); if (flyoutClosed) { return null; @@ -73,7 +80,11 @@ export const CrawlDetailsFlyout: React.FC = () => { ) : ( <> - {selectedTab === 'preview' && } + {selectedTab === 'preview' && ( + + )} {selectedTab === 'json' && ( {JSON.stringify(crawlRequestFromServer, null, 2)} diff --git a/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/components/crawl_details_flyout/crawl_details_preview.test.tsx b/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/components/crawl_details_flyout/crawl_details_preview.test.tsx index 646c611901c7f..f97e2ff913150 100644 --- a/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/components/crawl_details_flyout/crawl_details_preview.test.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/components/crawl_details_flyout/crawl_details_preview.test.tsx @@ -9,12 +9,14 @@ import { setMockValues } from '../../../../../__mocks__/kea_logic'; import React from 'react'; import { shallow, ShallowWrapper } from 'enzyme'; +import { set } from 'lodash/fp'; import { CrawlDetailValues } from '../../crawl_detail_logic'; import { CrawlerStatus, CrawlType } from '../../types'; import { AccordionList } from './accordion_list'; import { CrawlDetailsPreview } from './crawl_details_preview'; +import { CrawlDetailsSummary } from './crawl_details_summary'; const MOCK_VALUES: Partial = { crawlRequest: { @@ -28,6 +30,15 @@ const MOCK_VALUES: Partial = { domainAllowlist: ['https://www.elastic.co', 'https://www.swiftype.com'], seedUrls: ['https://www.elastic.co/docs', 'https://www.swiftype.com/documentation'], sitemapUrls: ['https://www.elastic.co/sitemap.xml', 'https://www.swiftype.com/sitemap.xml'], + maxCrawlDepth: 10, + }, + stats: { + status: { + urlsAllowed: 10, + pagesVisited: 10, + crawlDurationMSec: 36000, + avgResponseTimeMSec: 100, + }, }, }, }; @@ -38,16 +49,44 @@ describe('CrawlDetailsPreview', () => { crawlRequest: null, }); - const wrapper = shallow(); + const wrapper = shallow(); expect(wrapper.isEmptyRender()).toBe(true); }); describe('when a crawl request has been loaded', () => { let wrapper: ShallowWrapper; - beforeAll(() => { + beforeEach(() => { setMockValues(MOCK_VALUES); - wrapper = shallow(); + wrapper = shallow(); + }); + + it('contains a summary', () => { + const summary = wrapper.find(CrawlDetailsSummary); + expect(summary.props()).toEqual({ + crawlDepth: 10, + crawlType: 'full', + crawlerLogsEnabled: true, + domainCount: 2, + stats: { + status: { + avgResponseTimeMSec: 100, + crawlDurationMSec: 36000, + pagesVisited: 10, + urlsAllowed: 10, + }, + }, + }); + }); + + it('will default values on summary if missing', () => { + const values = set('crawlRequest.stats', undefined, MOCK_VALUES); + setMockValues(values); + wrapper = shallow(); + + const summary = wrapper.find(CrawlDetailsSummary); + expect(summary.prop('crawlerLogsEnabled')).toEqual(false); + expect(summary.prop('stats')).toEqual(null); }); it('contains a list of domains', () => { diff --git a/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/components/crawl_details_flyout/crawl_details_preview.tsx b/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/components/crawl_details_flyout/crawl_details_preview.tsx index 6f837d1db26e2..a9f3d95edf1fa 100644 --- a/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/components/crawl_details_flyout/crawl_details_preview.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/components/crawl_details_flyout/crawl_details_preview.tsx @@ -15,8 +15,15 @@ import { i18n } from '@kbn/i18n'; import { CrawlDetailLogic } from '../../crawl_detail_logic'; import { AccordionList } from './accordion_list'; +import { CrawlDetailsSummary } from './crawl_details_summary'; -export const CrawlDetailsPreview: React.FC = () => { +interface CrawlDetailsPreviewProps { + crawlerLogsEnabled?: boolean; +} + +export const CrawlDetailsPreview: React.FC = ({ + crawlerLogsEnabled = false, +}) => { const { crawlRequest } = useValues(CrawlDetailLogic); if (crawlRequest === null) { @@ -25,6 +32,14 @@ export const CrawlDetailsPreview: React.FC = () => { return ( <> + + 0} diff --git a/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/components/crawl_details_flyout/crawl_details_summary.test.tsx b/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/components/crawl_details_flyout/crawl_details_summary.test.tsx new file mode 100644 index 0000000000000..f37060a9cef42 --- /dev/null +++ b/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/components/crawl_details_flyout/crawl_details_summary.test.tsx @@ -0,0 +1,82 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import '../../../../__mocks__/engine_logic.mock'; + +import React from 'react'; + +import { shallow, ShallowWrapper } from 'enzyme'; + +import { EuiPanel } from '@elastic/eui'; + +import { CrawlDetailsSummary } from './crawl_details_summary'; + +const MOCK_PROPS = { + crawlDepth: 8, + crawlerLogsEnabled: true, + crawlType: 'full', + domainCount: 15, + stats: { + status: { + urlsAllowed: 108, + crawlDurationMSec: 748382, + pagesVisited: 108, + avgResponseTimeMSec: 42, + statusCodes: { + 401: 4, + 404: 8, + 500: 0, + 503: 3, + }, + }, + }, +}; + +describe('CrawlDetailsSummary', () => { + let wrapper: ShallowWrapper; + + beforeAll(() => { + wrapper = shallow(); + }); + + it('renders as a panel with all fields', () => { + expect(wrapper.is(EuiPanel)).toBe(true); + }); + + it('renders the proper count for errors', () => { + const serverErrors = wrapper.find({ 'data-test-subj': 'serverErrors' }); + const clientErrors = wrapper.find({ 'data-test-subj': 'clientErrors' }); + + expect(serverErrors.prop('title')).toEqual(3); + expect(clientErrors.prop('title')).toEqual(12); + }); + + it('handles missing stats gracefully', () => { + wrapper.setProps({ stats: {} }); + expect(wrapper.find({ 'data-test-subj': 'crawlDuration' }).prop('title')).toEqual('--'); + expect(wrapper.find({ 'data-test-subj': 'pagesVisited' }).prop('title')).toEqual('--'); + expect(wrapper.find({ 'data-test-subj': 'avgResponseTime' }).prop('title')).toEqual('--'); + }); + + it('renders the stat object when logs are disabled but stats are not null', () => { + wrapper.setProps({ crawlerLogsEnabled: false }); + expect(wrapper.find({ 'data-test-subj': 'crawlDuration' })).toHaveLength(1); + expect(wrapper.find({ 'data-test-subj': 'pagesVisited' })).toHaveLength(1); + expect(wrapper.find({ 'data-test-subj': 'avgResponseTime' })).toHaveLength(1); + expect(wrapper.find({ 'data-test-subj': 'urlsAllowed' })).toHaveLength(1); + expect(wrapper.find({ 'data-test-subj': 'logsDisabledMessage' })).toHaveLength(0); + }); + + it('renders a message to enable logs when crawler logs are disabled and stats are null', () => { + wrapper.setProps({ crawlerLogsEnabled: false, stats: null }); + expect(wrapper.find({ 'data-test-subj': 'crawlDuration' })).toHaveLength(0); + expect(wrapper.find({ 'data-test-subj': 'pagesVisited' })).toHaveLength(0); + expect(wrapper.find({ 'data-test-subj': 'avgResponseTime' })).toHaveLength(0); + expect(wrapper.find({ 'data-test-subj': 'urlsAllowed' })).toHaveLength(0); + expect(wrapper.find({ 'data-test-subj': 'logsDisabledMessage' })).toHaveLength(1); + }); +}); diff --git a/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/components/crawl_details_flyout/crawl_details_summary.tsx b/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/components/crawl_details_flyout/crawl_details_summary.tsx new file mode 100644 index 0000000000000..e05cbd9101de6 --- /dev/null +++ b/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/components/crawl_details_flyout/crawl_details_summary.tsx @@ -0,0 +1,261 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import React from 'react'; + +import moment from 'moment'; + +import { + EuiFlexGroup, + EuiFlexItem, + EuiHorizontalRule, + EuiIconTip, + EuiPanel, + EuiSpacer, + EuiStat, + EuiText, +} from '@elastic/eui'; +import { i18n } from '@kbn/i18n'; + +import { CrawlRequestStats } from '../../types'; + +interface ICrawlerSummaryProps { + crawlDepth: number; + crawlType: string; + crawlerLogsEnabled: boolean; + domainCount: number; + stats: CrawlRequestStats | null; +} + +export const CrawlDetailsSummary: React.FC = ({ + crawlDepth, + crawlType, + crawlerLogsEnabled, + domainCount, + stats, +}) => { + const duration = () => { + if (stats && stats.status && stats.status.crawlDurationMSec) { + const milliseconds = moment.duration(stats.status.crawlDurationMSec, 'milliseconds'); + const hours = milliseconds.hours(); + const minutes = milliseconds.minutes(); + const seconds = milliseconds.seconds(); + return `${hours}h ${minutes}m ${seconds}s`; + } else { + return '--'; + } + }; + + const getStatusCount = (code: string, codes: { [code: string]: number }) => { + return Object.entries(codes).reduce((count, [k, v]) => { + if (k[0] !== code) return count; + return v + count; + }, 0); + }; + + const statusCounts = { + clientErrorCount: + stats && stats.status && stats.status.statusCodes + ? getStatusCount('4', stats.status.statusCodes) + : 0, + serverErrorCount: + stats && stats.status && stats.status.statusCodes + ? getStatusCount('5', stats.status.statusCodes) + : 0, + }; + + const shouldHideStats = !crawlerLogsEnabled && !stats; + + return ( + + + + + + + + + {!shouldHideStats && ( + + + + )} + + + {!shouldHideStats ? ( + + + + URLs{' '} + + + } + /> + + + + {i18n.translate( + 'xpack.enterpriseSearch.appSearch.crawler.crawlDetailsSummary.pagesVisitedTooltipTitle', + { + defaultMessage: 'Pages', + } + )}{' '} + + + } + /> + + + + + + + + + + + + ) : ( + + +

+ {i18n.translate( + 'xpack.enterpriseSearch.appSearch.crawler.crawlDetailsSummary.logsDisabledMessage', + { + defaultMessage: + 'Enable Web Crawler logs in settings for more detailed crawl statistics.', + } + )} +

+
+ )} + + ); +}; diff --git a/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/components/crawl_event_type_badge.test.tsx b/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/components/crawl_event_type_badge.test.tsx index fd1f03c586f12..bc5efc7714495 100644 --- a/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/components/crawl_event_type_badge.test.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/components/crawl_event_type_badge.test.tsx @@ -31,6 +31,7 @@ const MOCK_EVENT: CrawlEvent = { domainAllowlist: ['https://www.elastic.co'], seedUrls: [], sitemapUrls: [], + maxCrawlDepth: 10, }, }; diff --git a/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/components/crawl_requests_table.test.tsx b/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/components/crawl_requests_table.test.tsx index 03e5d835df9b3..c2b36f24d7582 100644 --- a/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/components/crawl_requests_table.test.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/components/crawl_requests_table.test.tsx @@ -35,6 +35,7 @@ const values: { events: CrawlEvent[] } = { domainAllowlist: ['https://www.elastic.co'], seedUrls: [], sitemapUrls: [], + maxCrawlDepth: 10, }, }, { @@ -49,6 +50,7 @@ const values: { events: CrawlEvent[] } = { domainAllowlist: ['https://www.elastic.co'], seedUrls: [], sitemapUrls: [], + maxCrawlDepth: 10, }, }, ], diff --git a/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/crawl_detail_logic.test.ts b/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/crawl_detail_logic.test.ts index a7d795c93e0a7..152fe0f64de4d 100644 --- a/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/crawl_detail_logic.test.ts +++ b/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/crawl_detail_logic.test.ts @@ -35,6 +35,19 @@ const crawlRequestResponse: CrawlRequestWithDetailsFromServer = { domain_allowlist: [], seed_urls: [], sitemap_urls: [], + max_crawl_depth: 10, + }, + stats: { + status: { + urls_allowed: 4, + pages_visited: 4, + crawl_duration_msec: 100, + avg_response_time_msec: 10, + status_codes: { + 200: 4, + 404: 0, + }, + }, }, }; diff --git a/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/crawler_logic.test.ts b/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/crawler_logic.test.ts index 5af9b1652c889..0735b5262a20a 100644 --- a/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/crawler_logic.test.ts +++ b/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/crawler_logic.test.ts @@ -138,6 +138,7 @@ describe('CrawlerLogic', () => { domainAllowlist: ['elastic.co'], seedUrls: [], sitemapUrls: [], + maxCrawlDepth: 10, }, }, ], diff --git a/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/crawler_overview.test.tsx b/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/crawler_overview.test.tsx index 0d2c2e60abfa9..4d72b854bddfb 100644 --- a/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/crawler_overview.test.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/crawler_overview.test.tsx @@ -12,17 +12,22 @@ import React from 'react'; import { shallow } from 'enzyme'; +import { useValues } from 'kea'; + import { getPageHeaderActions } from '../../../test_helpers'; import { AddDomainFlyout } from './components/add_domain/add_domain_flyout'; import { AddDomainForm } from './components/add_domain/add_domain_form'; +import { AddDomainFormErrors } from './components/add_domain/add_domain_form_errors'; import { AddDomainFormSubmitButton } from './components/add_domain/add_domain_form_submit_button'; +import { AddDomainLogic } from './components/add_domain/add_domain_logic'; import { CrawlDetailsFlyout } from './components/crawl_details_flyout'; import { CrawlRequestsTable } from './components/crawl_requests_table'; import { CrawlerStatusBanner } from './components/crawler_status_banner'; import { CrawlerStatusIndicator } from './components/crawler_status_indicator/crawler_status_indicator'; import { DomainsTable } from './components/domains_table'; import { ManageCrawlsPopover } from './components/manage_crawls_popover/manage_crawls_popover'; +import { CrawlerLogic } from './crawler_logic'; import { CrawlerOverview } from './crawler_overview'; import { CrawlerDomainFromServer, @@ -80,6 +85,7 @@ const events: CrawlEventFromServer[] = [ domain_allowlist: ['moviedatabase.com', 'swiftype.com'], seed_urls: [], sitemap_urls: [], + max_crawl_depth: 10, }, }, { @@ -94,6 +100,7 @@ const events: CrawlEventFromServer[] = [ domain_allowlist: ['swiftype.com'], seed_urls: [], sitemap_urls: [], + max_crawl_depth: 10, }, }, ]; @@ -189,4 +196,23 @@ describe('CrawlerOverview', () => { expect(wrapper.find(CrawlDetailsFlyout)).toHaveLength(1); }); + + it('contains a AddDomainFormErrors when there are errors', () => { + const errors = ['Domain name already exists']; + + (useValues as jest.Mock).mockImplementation((logic) => { + switch (logic) { + case AddDomainLogic: + return { errors }; + case CrawlerLogic: + return { ...mockValues, domains: [], events: [] }; + default: + return {}; + } + }); + + const wrapper = shallow(); + + expect(wrapper.find(AddDomainFormErrors)).toHaveLength(1); + }); }); diff --git a/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/crawler_overview.tsx b/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/crawler_overview.tsx index c84deb3cb0c99..c68e75790f073 100644 --- a/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/crawler_overview.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/crawler_overview.tsx @@ -19,7 +19,9 @@ import { AppSearchPageTemplate } from '../layout'; import { AddDomainFlyout } from './components/add_domain/add_domain_flyout'; import { AddDomainForm } from './components/add_domain/add_domain_form'; +import { AddDomainFormErrors } from './components/add_domain/add_domain_form_errors'; import { AddDomainFormSubmitButton } from './components/add_domain/add_domain_form_submit_button'; +import { AddDomainLogic } from './components/add_domain/add_domain_logic'; import { CrawlDetailsFlyout } from './components/crawl_details_flyout'; import { CrawlRequestsTable } from './components/crawl_requests_table'; import { CrawlerStatusBanner } from './components/crawler_status_banner'; @@ -31,6 +33,7 @@ import { CrawlerLogic } from './crawler_logic'; export const CrawlerOverview: React.FC = () => { const { events, dataLoading, domains } = useValues(CrawlerLogic); + const { errors: addDomainErrors } = useValues(AddDomainLogic); return ( {

+ {addDomainErrors && ( + <> + + + + )} diff --git a/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/types.ts b/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/types.ts index 85ebb0032971d..3d8881601ae1a 100644 --- a/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/types.ts +++ b/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/types.ts @@ -199,28 +199,54 @@ export interface CrawlRequest { completedAt: string | null; } +export interface CrawlRequestStats { + status: { + avgResponseTimeMSec?: number; + crawlDurationMSec?: number; + pagesVisited?: number; + urlsAllowed?: number; + statusCodes?: { + [code: string]: number; + }; + }; +} + +export interface CrawlRequestStatsFromServer { + status: { + avg_response_time_msec?: number; + crawl_duration_msec?: number; + pages_visited?: number; + urls_allowed?: number; + status_codes?: { + [code: string]: number; + }; + }; +} + export interface CrawlConfig { domainAllowlist: string[]; seedUrls: string[]; sitemapUrls: string[]; + maxCrawlDepth: number; } export interface CrawlConfigFromServer { domain_allowlist: string[]; seed_urls: string[]; sitemap_urls: string[]; + max_crawl_depth: number; } export type CrawlRequestWithDetailsFromServer = CrawlRequestFromServer & { type: CrawlType; crawl_config: CrawlConfigFromServer; - // TODO add other properties like stats + stats: CrawlRequestStatsFromServer; }; export type CrawlRequestWithDetails = CrawlRequest & { type: CrawlType; crawlConfig: CrawlConfig; - // TODO add other properties like stats + stats: CrawlRequestStats | null; }; export type CrawlEventStage = 'crawl' | 'process'; diff --git a/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/utils.test.ts b/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/utils.test.ts index 0df1f57eaefa0..cab4023370291 100644 --- a/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/utils.test.ts +++ b/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/utils.test.ts @@ -22,6 +22,7 @@ import { CrawlRequestWithDetails, CrawlEvent, CrawlEventFromServer, + CrawlRequestStatsFromServer, } from './types'; import { @@ -34,6 +35,7 @@ import { getDeleteDomainConfirmationMessage, getDeleteDomainSuccessMessage, getCrawlRulePathPatternTooltip, + crawlRequestStatsServerToClient, } from './utils'; const DEFAULT_CRAWL_RULE: CrawlRule = { @@ -126,6 +128,36 @@ describe('crawlRequestServerToClient', () => { }); }); +describe('crawlRequestStatsServerToClient', () => { + it('converts the API payload into properties matching our code style', () => { + const defaultServerPayload: CrawlRequestStatsFromServer = { + status: { + urls_allowed: 4, + pages_visited: 4, + crawl_duration_msec: 100, + avg_response_time_msec: 10, + status_codes: { + 200: 4, + 404: 0, + }, + }, + }; + + expect(crawlRequestStatsServerToClient(defaultServerPayload)).toEqual({ + status: { + urlsAllowed: 4, + pagesVisited: 4, + crawlDurationMSec: 100, + avgResponseTimeMSec: 10, + statusCodes: { + 200: 4, + 404: 0, + }, + }, + }); + }); +}); + describe('crawlRequestWithDetailsServerToClient', () => { it('converts the API payload into properties matching our code style', () => { const id = '507f1f77bcf86cd799439011'; @@ -141,6 +173,19 @@ describe('crawlRequestWithDetailsServerToClient', () => { domain_allowlist: [], seed_urls: [], sitemap_urls: [], + max_crawl_depth: 10, + }, + stats: { + status: { + urls_allowed: 4, + pages_visited: 4, + crawl_duration_msec: 100, + avg_response_time_msec: 10, + status_codes: { + 200: 4, + 404: 0, + }, + }, }, }; @@ -155,6 +200,19 @@ describe('crawlRequestWithDetailsServerToClient', () => { domainAllowlist: [], seedUrls: [], sitemapUrls: [], + maxCrawlDepth: 10, + }, + stats: { + status: { + urlsAllowed: 4, + pagesVisited: 4, + crawlDurationMSec: 100, + avgResponseTimeMSec: 10, + statusCodes: { + 200: 4, + 404: 0, + }, + }, }, }; @@ -191,6 +249,7 @@ describe('crawlEventServerToClient', () => { domain_allowlist: [], seed_urls: [], sitemap_urls: [], + max_crawl_depth: 10, }, stage: 'crawl', }; @@ -206,6 +265,7 @@ describe('crawlEventServerToClient', () => { domainAllowlist: [], seedUrls: [], sitemapUrls: [], + maxCrawlDepth: 10, }, stage: 'crawl', }; @@ -274,6 +334,7 @@ describe('crawlerDataServerToClient', () => { domain_allowlist: ['https://www.elastic.co'], seed_urls: [], sitemap_urls: [], + max_crawl_depth: 10, }, }, ], @@ -329,6 +390,7 @@ describe('crawlerDataServerToClient', () => { domainAllowlist: ['https://www.elastic.co'], seedUrls: [], sitemapUrls: [], + maxCrawlDepth: 10, }, }, ]); diff --git a/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/utils.ts b/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/utils.ts index d1203e19c0208..4819b073cccb3 100644 --- a/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/utils.ts +++ b/x-pack/plugins/enterprise_search/public/applications/app_search/components/crawler/utils.ts @@ -16,6 +16,8 @@ import { CrawlerDomainValidationStep, CrawlRequestFromServer, CrawlRequest, + CrawlRequestStats, + CrawlRequestStatsFromServer, CrawlRule, CrawlerRules, CrawlEventFromServer, @@ -66,6 +68,30 @@ export function crawlerDomainServerToClient(payload: CrawlerDomainFromServer): C return clientPayload; } +export function crawlRequestStatsServerToClient( + crawlStats: CrawlRequestStatsFromServer +): CrawlRequestStats { + const { + status: { + avg_response_time_msec: avgResponseTimeMSec, + crawl_duration_msec: crawlDurationMSec, + pages_visited: pagesVisited, + urls_allowed: urlsAllowed, + status_codes: statusCodes, + }, + } = crawlStats; + + return { + status: { + urlsAllowed, + pagesVisited, + avgResponseTimeMSec, + crawlDurationMSec, + statusCodes, + }, + }; +} + export function crawlRequestServerToClient(crawlRequest: CrawlRequestFromServer): CrawlRequest { const { id, @@ -89,12 +115,14 @@ export function crawlConfigServerToClient(crawlConfig: CrawlConfigFromServer): C domain_allowlist: domainAllowlist, seed_urls: seedUrls, sitemap_urls: sitemapUrls, + max_crawl_depth: maxCrawlDepth, } = crawlConfig; return { domainAllowlist, seedUrls, sitemapUrls, + maxCrawlDepth, }; } @@ -126,24 +154,25 @@ export function crawlRequestWithDetailsServerToClient( event: CrawlRequestWithDetailsFromServer ): CrawlRequestWithDetails { const { - id, - status, - created_at: createdAt, began_at: beganAt, completed_at: completedAt, - type, crawl_config: crawlConfig, + created_at: createdAt, + id, + stats: crawlStats, + status, + type, } = event; return { - id, - status, - createdAt, beganAt, completedAt, - type, crawlConfig: crawlConfigServerToClient(crawlConfig), - // TODO add fields like stats + createdAt, + id, + stats: crawlStats && crawlRequestStatsServerToClient(crawlStats), + status, + type, }; } diff --git a/x-pack/plugins/enterprise_search/public/applications/app_search/components/error_connecting/error_connecting.test.tsx b/x-pack/plugins/enterprise_search/public/applications/app_search/components/error_connecting/error_connecting.test.tsx index 9ec3fdda63656..be17bfaeb7127 100644 --- a/x-pack/plugins/enterprise_search/public/applications/app_search/components/error_connecting/error_connecting.test.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/app_search/components/error_connecting/error_connecting.test.tsx @@ -15,8 +15,10 @@ import { ErrorConnecting } from './'; describe('ErrorConnecting', () => { it('renders', () => { - const wrapper = shallow(); + const wrapper = shallow(); - expect(wrapper.find(ErrorStatePrompt)).toHaveLength(1); + const errorStatePrompt = wrapper.find(ErrorStatePrompt); + expect(errorStatePrompt).toHaveLength(1); + expect(errorStatePrompt.prop('errorConnectingMessage')).toEqual('I am an error'); }); }); diff --git a/x-pack/plugins/enterprise_search/public/applications/app_search/components/error_connecting/error_connecting.tsx b/x-pack/plugins/enterprise_search/public/applications/app_search/components/error_connecting/error_connecting.tsx index 84dcb07a07474..a01fb264935c1 100644 --- a/x-pack/plugins/enterprise_search/public/applications/app_search/components/error_connecting/error_connecting.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/app_search/components/error_connecting/error_connecting.tsx @@ -13,14 +13,16 @@ import { ErrorStatePrompt } from '../../../shared/error_state'; import { SetAppSearchChrome as SetPageChrome } from '../../../shared/kibana_chrome'; import { SendAppSearchTelemetry as SendTelemetry } from '../../../shared/telemetry'; -export const ErrorConnecting: React.FC = () => { +export const ErrorConnecting: React.FC<{ errorConnectingMessage?: string }> = ({ + errorConnectingMessage, +}) => { return ( <> - + ); diff --git a/x-pack/plugins/enterprise_search/public/applications/app_search/index.test.tsx b/x-pack/plugins/enterprise_search/public/applications/app_search/index.test.tsx index 2f415840a6c4a..2ffb1f80a3d32 100644 --- a/x-pack/plugins/enterprise_search/public/applications/app_search/index.test.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/app_search/index.test.tsx @@ -57,9 +57,11 @@ describe('AppSearch', () => { it('renders ErrorConnecting when Enterprise Search is unavailable', () => { setMockValues({ errorConnecting: true }); - const wrapper = shallow(); + const wrapper = shallow(); - expect(wrapper.find(ErrorConnecting)).toHaveLength(1); + const errorConnection = wrapper.find(ErrorConnecting); + expect(errorConnection).toHaveLength(1); + expect(errorConnection.prop('errorConnectingMessage')).toEqual('I am an error'); }); it('renders AppSearchConfigured when config.host is set & available', () => { diff --git a/x-pack/plugins/enterprise_search/public/applications/app_search/index.tsx b/x-pack/plugins/enterprise_search/public/applications/app_search/index.tsx index 027a4dbee5ef6..605d82d2af601 100644 --- a/x-pack/plugins/enterprise_search/public/applications/app_search/index.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/app_search/index.tsx @@ -45,7 +45,7 @@ import { export const AppSearch: React.FC = (props) => { const { config } = useValues(KibanaLogic); const { errorConnecting } = useValues(HttpLogic); - const { enterpriseSearchVersion, kibanaVersion } = props; + const { enterpriseSearchVersion, kibanaVersion, errorConnectingMessage } = props; const incompatibleVersions = isVersionMismatch(enterpriseSearchVersion, kibanaVersion); const showView = () => { @@ -59,7 +59,7 @@ export const AppSearch: React.FC = (props) => { /> ); } else if (errorConnecting) { - return ; + return ; } return )} />; diff --git a/x-pack/plugins/enterprise_search/public/applications/enterprise_search/components/error_connecting/error_connecting.test.tsx b/x-pack/plugins/enterprise_search/public/applications/enterprise_search/components/error_connecting/error_connecting.test.tsx index 9ec3fdda63656..be17bfaeb7127 100644 --- a/x-pack/plugins/enterprise_search/public/applications/enterprise_search/components/error_connecting/error_connecting.test.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/enterprise_search/components/error_connecting/error_connecting.test.tsx @@ -15,8 +15,10 @@ import { ErrorConnecting } from './'; describe('ErrorConnecting', () => { it('renders', () => { - const wrapper = shallow(); + const wrapper = shallow(); - expect(wrapper.find(ErrorStatePrompt)).toHaveLength(1); + const errorStatePrompt = wrapper.find(ErrorStatePrompt); + expect(errorStatePrompt).toHaveLength(1); + expect(errorStatePrompt.prop('errorConnectingMessage')).toEqual('I am an error'); }); }); diff --git a/x-pack/plugins/enterprise_search/public/applications/enterprise_search/components/error_connecting/error_connecting.tsx b/x-pack/plugins/enterprise_search/public/applications/enterprise_search/components/error_connecting/error_connecting.tsx index 979847b4cf1c6..f9ffd6c992426 100644 --- a/x-pack/plugins/enterprise_search/public/applications/enterprise_search/components/error_connecting/error_connecting.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/enterprise_search/components/error_connecting/error_connecting.tsx @@ -12,10 +12,12 @@ import { KibanaPageTemplate } from '../../../../../../../../src/plugins/kibana_r import { ErrorStatePrompt } from '../../../shared/error_state'; import { SendEnterpriseSearchTelemetry as SendTelemetry } from '../../../shared/telemetry'; -export const ErrorConnecting: React.FC = () => ( +export const ErrorConnecting: React.FC<{ errorConnectingMessage?: string }> = ({ + errorConnectingMessage, +}) => ( - + ); diff --git a/x-pack/plugins/enterprise_search/public/applications/enterprise_search/index.test.tsx b/x-pack/plugins/enterprise_search/public/applications/enterprise_search/index.test.tsx index 7b5c748b013e5..a366057797925 100644 --- a/x-pack/plugins/enterprise_search/public/applications/enterprise_search/index.test.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/enterprise_search/index.test.tsx @@ -37,10 +37,12 @@ describe('EnterpriseSearch', () => { errorConnecting: true, config: { host: 'localhost' }, }); - const wrapper = shallow(); + const wrapper = shallow(); expect(wrapper.find(VersionMismatchPage)).toHaveLength(0); - expect(wrapper.find(ErrorConnecting)).toHaveLength(1); + const errorConnecting = wrapper.find(ErrorConnecting); + expect(errorConnecting).toHaveLength(1); + expect(errorConnecting.prop('errorConnectingMessage')).toEqual('I am an error'); expect(wrapper.find(ProductSelector)).toHaveLength(0); setMockValues({ diff --git a/x-pack/plugins/enterprise_search/public/applications/enterprise_search/index.tsx b/x-pack/plugins/enterprise_search/public/applications/enterprise_search/index.tsx index 81aa587e3a133..ded5909a0fa43 100644 --- a/x-pack/plugins/enterprise_search/public/applications/enterprise_search/index.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/enterprise_search/index.tsx @@ -26,6 +26,7 @@ export const EnterpriseSearch: React.FC = ({ workplaceSearch, enterpriseSearchVersion, kibanaVersion, + errorConnectingMessage, }) => { const { errorConnecting } = useValues(HttpLogic); const { config } = useValues(KibanaLogic); @@ -45,7 +46,7 @@ export const EnterpriseSearch: React.FC = ({ /> ); } else if (showErrorConnecting) { - return ; + return ; } return ; diff --git a/x-pack/plugins/enterprise_search/public/applications/shared/error_state/error_state_prompt.test.tsx b/x-pack/plugins/enterprise_search/public/applications/shared/error_state/error_state_prompt.test.tsx index e8f0816de5225..2d21ea7c61444 100644 --- a/x-pack/plugins/enterprise_search/public/applications/shared/error_state/error_state_prompt.test.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/shared/error_state/error_state_prompt.test.tsx @@ -5,20 +5,48 @@ * 2.0. */ -import '../../__mocks__/kea_logic'; +import { setMockValues } from '../../__mocks__/kea_logic'; import React from 'react'; -import { shallow } from 'enzyme'; - -import { EuiEmptyPrompt } from '@elastic/eui'; +import { mountWithIntl } from '../../test_helpers'; import { ErrorStatePrompt } from './'; describe('ErrorState', () => { - it('renders', () => { - const wrapper = shallow(); + const values = { + config: {}, + cloud: { isCloudEnabled: true }, + }; + + beforeAll(() => { + setMockValues(values); + }); + + it('renders a cloud specific error on cloud deployments', () => { + setMockValues({ + ...values, + cloud: { isCloudEnabled: true }, + }); + const wrapper = mountWithIntl(); + + expect(wrapper.find('[data-test-subj="CloudError"]').exists()).toBe(true); + expect(wrapper.find('[data-test-subj="SelfManagedError"]').exists()).toBe(false); + }); + + it('renders a different error if not a cloud deployment', () => { + setMockValues({ + ...values, + cloud: { isCloudEnabled: false }, + }); + const wrapper = mountWithIntl(); + + expect(wrapper.find('[data-test-subj="CloudError"]').exists()).toBe(false); + expect(wrapper.find('[data-test-subj="SelfManagedError"]').exists()).toBe(true); + }); - expect(wrapper.find(EuiEmptyPrompt)).toHaveLength(1); + it('renders an error message', () => { + const wrapper = mountWithIntl(); + expect(wrapper.text()).toContain('I am an error'); }); }); diff --git a/x-pack/plugins/enterprise_search/public/applications/shared/error_state/error_state_prompt.tsx b/x-pack/plugins/enterprise_search/public/applications/shared/error_state/error_state_prompt.tsx index eff483df10c7f..fea43b902993d 100644 --- a/x-pack/plugins/enterprise_search/public/applications/shared/error_state/error_state_prompt.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/shared/error_state/error_state_prompt.tsx @@ -9,16 +9,22 @@ import React from 'react'; import { useValues } from 'kea'; -import { EuiEmptyPrompt, EuiCode } from '@elastic/eui'; +import { EuiEmptyPrompt, EuiCode, EuiLink, EuiCodeBlock } from '@elastic/eui'; +import { i18n } from '@kbn/i18n'; import { FormattedMessage } from '@kbn/i18n-react'; +import { CloudSetup } from '../../../../../cloud/public'; + import { KibanaLogic } from '../kibana'; -import { EuiButtonTo } from '../react_router_helpers'; +import { EuiButtonTo, EuiLinkTo } from '../react_router_helpers'; import './error_state_prompt.scss'; -export const ErrorStatePrompt: React.FC = () => { - const { config } = useValues(KibanaLogic); +export const ErrorStatePrompt: React.FC<{ errorConnectingMessage?: string }> = ({ + errorConnectingMessage, +}) => { + const { config, cloud } = useValues(KibanaLogic); + const isCloudEnabled = cloud.isCloudEnabled; return ( {

{config.host}, + enterpriseSearchUrl: ( + + {config.host} + + ), }} />

-
    -
  1. - config/kibana.yml, - }} - /> -
  2. -
  3. - -
  4. -
  5. - -
      -
    • - -
    • -
    • - -
    • -
    -
  6. -
  7. - [enterpriseSearch][plugins], - }} - /> -
  8. -
+ {errorConnectingMessage} + {isCloudEnabled ? cloudError(cloud) : nonCloudError()} } actions={[ @@ -103,3 +69,69 @@ export const ErrorStatePrompt: React.FC = () => { /> ); }; + +const cloudError = (cloud: Partial) => { + const deploymentUrl = cloud?.deploymentUrl; + return ( +

+ + {i18n.translate( + 'xpack.enterpriseSearch.errorConnectingState.cloudErrorMessageLinkText', + { + defaultMessage: 'Check your deployment settings', + } + )} + + ), + }} + /> +

+ ); +}; + +const nonCloudError = () => { + return ( +
    +
  1. + config/kibana.yml, + }} + /> +
  2. +
  3. + +
  4. +
  5. + +
      +
    • + +
    • +
    • + +
    • +
    +
  6. +
+ ); +}; diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/components/shared/license_callout/license_callout.tsx b/x-pack/plugins/enterprise_search/public/applications/workplace_search/components/shared/license_callout/license_callout.tsx index 270daf195bd38..7bf80b5ff9180 100644 --- a/x-pack/plugins/enterprise_search/public/applications/workplace_search/components/shared/license_callout/license_callout.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/components/shared/license_callout/license_callout.tsx @@ -9,8 +9,9 @@ import React from 'react'; import { EuiLink, EuiFlexItem, EuiFlexGroup, EuiText } from '@elastic/eui'; +import { docLinks } from '../../../../shared/doc_links'; + import { EXPLORE_PLATINUM_FEATURES_LINK } from '../../../constants'; -import { ENT_SEARCH_LICENSE_MANAGEMENT } from '../../../routes'; interface LicenseCalloutProps { message?: string; @@ -20,7 +21,7 @@ export const LicenseCallout: React.FC = ({ message }) => { const title = ( <> {message}{' '} - + {EXPLORE_PLATINUM_FEATURES_LINK} diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/index.test.tsx b/x-pack/plugins/enterprise_search/public/applications/workplace_search/index.test.tsx index 7274ee8855705..9fa2c211f1667 100644 --- a/x-pack/plugins/enterprise_search/public/applications/workplace_search/index.test.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/index.test.tsx @@ -51,9 +51,11 @@ describe('WorkplaceSearch', () => { it('renders ErrorState', () => { setMockValues({ errorConnecting: true }); - const wrapper = shallow(); + const wrapper = shallow(); - expect(wrapper.find(ErrorState)).toHaveLength(1); + const errorState = wrapper.find(ErrorState); + expect(errorState).toHaveLength(1); + expect(errorState.prop('errorConnectingMessage')).toEqual('I am an error'); }); }); diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/index.tsx b/x-pack/plugins/enterprise_search/public/applications/workplace_search/index.tsx index e7ffabd54a88c..41ad1670019ba 100644 --- a/x-pack/plugins/enterprise_search/public/applications/workplace_search/index.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/index.tsx @@ -51,7 +51,7 @@ import { SetupGuide } from './views/setup_guide'; export const WorkplaceSearch: React.FC = (props) => { const { config } = useValues(KibanaLogic); const { errorConnecting } = useValues(HttpLogic); - const { enterpriseSearchVersion, kibanaVersion } = props; + const { enterpriseSearchVersion, kibanaVersion, errorConnectingMessage } = props; const incompatibleVersions = isVersionMismatch(enterpriseSearchVersion, kibanaVersion); if (!config.host) { @@ -64,7 +64,7 @@ export const WorkplaceSearch: React.FC = (props) => { /> ); } else if (errorConnecting) { - return ; + return ; } return ; diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/routes.ts b/x-pack/plugins/enterprise_search/public/applications/workplace_search/routes.ts index 1b630a47e2f86..ee180ae52e0b7 100644 --- a/x-pack/plugins/enterprise_search/public/applications/workplace_search/routes.ts +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/routes.ts @@ -7,8 +7,6 @@ import { generatePath } from 'react-router-dom'; -import { docLinks } from '../shared/doc_links'; - import { GITHUB_VIA_APP_SERVICE_TYPE, GITHUB_ENTERPRISE_SERVER_VIA_APP_SERVICE_TYPE, @@ -22,35 +20,6 @@ export const LOGOUT_ROUTE = '/logout'; export const LEAVE_FEEDBACK_EMAIL = 'support@elastic.co'; export const LEAVE_FEEDBACK_URL = `mailto:${LEAVE_FEEDBACK_EMAIL}?Subject=Elastic%20Workplace%20Search%20Feedback`; -export const BOX_DOCS_URL = docLinks.workplaceSearchBox; -export const CONFLUENCE_DOCS_URL = docLinks.workplaceSearchConfluenceCloud; -export const CONFLUENCE_SERVER_DOCS_URL = docLinks.workplaceSearchConfluenceServer; -export const CUSTOM_SOURCE_DOCS_URL = docLinks.workplaceSearchCustomSources; -export const CUSTOM_API_DOCUMENT_PERMISSIONS_DOCS_URL = - docLinks.workplaceSearchCustomSourcePermissions; -export const DIFFERENT_SYNC_TYPES_DOCS_URL = docLinks.workplaceSearchIndexingSchedule; -export const DOCUMENT_PERMISSIONS_DOCS_URL = docLinks.workplaceSearchDocumentPermissions; -export const DROPBOX_DOCS_URL = docLinks.workplaceSearchDropbox; -export const ENT_SEARCH_LICENSE_MANAGEMENT = docLinks.licenseManagement; -export const EXTERNAL_IDENTITIES_DOCS_URL = docLinks.workplaceSearchExternalIdentities; -export const GETTING_STARTED_DOCS_URL = docLinks.workplaceSearchGettingStarted; -export const GITHUB_DOCS_URL = docLinks.workplaceSearchGitHub; -export const GITHUB_ENTERPRISE_DOCS_URL = docLinks.workplaceSearchGitHub; -export const GMAIL_DOCS_URL = docLinks.workplaceSearchGmail; -export const GOOGLE_DRIVE_DOCS_URL = docLinks.workplaceSearchGoogleDrive; -export const JIRA_DOCS_URL = docLinks.workplaceSearchJiraCloud; -export const JIRA_SERVER_DOCS_URL = docLinks.workplaceSearchJiraServer; -export const OBJECTS_AND_ASSETS_DOCS_URL = docLinks.workplaceSearchSynch; -export const ONEDRIVE_DOCS_URL = docLinks.workplaceSearchOneDrive; -export const PRIVATE_SOURCES_DOCS_URL = docLinks.workplaceSearchPermissions; -export const SALESFORCE_DOCS_URL = docLinks.workplaceSearchSalesforce; -export const SECURITY_DOCS_URL = docLinks.workplaceSearchSecurity; -export const SERVICENOW_DOCS_URL = docLinks.workplaceSearchServiceNow; -export const SHAREPOINT_DOCS_URL = docLinks.workplaceSearchSharePoint; -export const SLACK_DOCS_URL = docLinks.workplaceSearchSlack; -export const SYNCHRONIZATION_DOCS_URL = docLinks.workplaceSearchSynch; -export const ZENDESK_DOCS_URL = docLinks.workplaceSearchZendesk; - export const PERSONAL_PATH = '/p'; export const OAUTH_AUTHORIZE_PATH = `${PERSONAL_PATH}/oauth/authorize`; diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/add_source/config_completed.tsx b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/add_source/config_completed.tsx index 167bf1af4b9b1..9b34053bfe524 100644 --- a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/add_source/config_completed.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/add_source/config_completed.tsx @@ -21,13 +21,9 @@ import { import { i18n } from '@kbn/i18n'; import { FormattedMessage } from '@kbn/i18n-react'; +import { docLinks } from '../../../../../shared/doc_links'; import { EuiLinkTo, EuiButtonTo } from '../../../../../shared/react_router_helpers'; -import { - getSourcesPath, - ADD_SOURCE_PATH, - SECURITY_PATH, - PRIVATE_SOURCES_DOCS_URL, -} from '../../../../routes'; +import { getSourcesPath, ADD_SOURCE_PATH, SECURITY_PATH } from '../../../../routes'; import { CONFIG_COMPLETED_PRIVATE_SOURCES_DISABLED_LINK, @@ -126,7 +122,7 @@ export const ConfigCompleted: React.FC = ({ {CONFIG_COMPLETED_PRIVATE_SOURCES_DOCS_LINK} diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/add_source/configure_custom.tsx b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/add_source/configure_custom.tsx index 4682d4329a964..e794323dc169e 100644 --- a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/add_source/configure_custom.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/add_source/configure_custom.tsx @@ -20,7 +20,7 @@ import { } from '@elastic/eui'; import { FormattedMessage } from '@kbn/i18n-react'; -import { CUSTOM_SOURCE_DOCS_URL } from '../../../../routes'; +import { docLinks } from '../../../../../shared/doc_links'; import { SOURCE_NAME_LABEL } from '../../constants'; @@ -63,7 +63,7 @@ export const ConfigureCustom: React.FC = ({ defaultMessage="{link} to learn more about Custom API Sources." values={{ link: ( - + {CONFIG_CUSTOM_LINK_TEXT} ), diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/add_source/document_permissions_callout.tsx b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/add_source/document_permissions_callout.tsx index 3c6980f74bcf5..d3879eabe08de 100644 --- a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/add_source/document_permissions_callout.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/add_source/document_permissions_callout.tsx @@ -17,8 +17,8 @@ import { EuiText, } from '@elastic/eui'; +import { docLinks } from '../../../../../shared/doc_links'; import { EXPLORE_PLATINUM_FEATURES_LINK } from '../../../../constants'; -import { ENT_SEARCH_LICENSE_MANAGEMENT } from '../../../../routes'; import { SOURCE_FEATURES_DOCUMENT_LEVEL_PERMISSIONS_FEATURE, @@ -45,7 +45,7 @@ export const DocumentPermissionsCallout: React.FC = () => { - + {EXPLORE_PLATINUM_FEATURES_LINK} diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/add_source/document_permissions_field.tsx b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/add_source/document_permissions_field.tsx index 1b1043ecbc3d2..1cc953ee7c2ea 100644 --- a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/add_source/document_permissions_field.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/add_source/document_permissions_field.tsx @@ -18,7 +18,7 @@ import { } from '@elastic/eui'; import { FormattedMessage } from '@kbn/i18n-react'; -import { DOCUMENT_PERMISSIONS_DOCS_URL } from '../../../../routes'; +import { docLinks } from '../../../../../shared/doc_links'; import { LEARN_MORE_LINK } from '../../constants'; import { @@ -42,7 +42,7 @@ export const DocumentPermissionsField: React.FC = ({ setValue, }) => { const whichDocsLink = ( - + {CONNECT_WHICH_OPTION_LINK} ); @@ -64,7 +64,7 @@ export const DocumentPermissionsField: React.FC = ({ defaultMessage="Document-level permissions are not yet available for this source. {link}" values={{ link: ( - + {LEARN_MORE_LINK} ), diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/add_source/github_via_app.tsx b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/add_source/github_via_app.tsx index a08f49b8bbe78..b62648348ed80 100644 --- a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/add_source/github_via_app.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/add_source/github_via_app.tsx @@ -44,8 +44,13 @@ interface GithubViaAppProps { export const GitHubViaApp: React.FC = ({ isGithubEnterpriseServer }) => { const { isOrganization } = useValues(AppLogic); - const { githubAppId, githubEnterpriseServerUrl, isSubmitButtonLoading, indexPermissionsValue } = - useValues(GithubViaAppLogic); + const { + githubAppId, + githubEnterpriseServerUrl, + stagedPrivateKey, + isSubmitButtonLoading, + indexPermissionsValue, + } = useValues(GithubViaAppLogic); const { setGithubAppId, setGithubEnterpriseServerUrl, @@ -118,7 +123,12 @@ export const GitHubViaApp: React.FC = ({ isGithubEnterpriseSe fill type="submit" isLoading={isSubmitButtonLoading} - isDisabled={!githubAppId || (isGithubEnterpriseServer && !githubEnterpriseServerUrl)} + isDisabled={ + // disable submit button if any required fields are empty + !githubAppId || + (isGithubEnterpriseServer && !githubEnterpriseServerUrl) || + !stagedPrivateKey + } > {isSubmitButtonLoading ? 'Connecting…' : `Connect ${name}`}
diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/add_source/save_custom.tsx b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/add_source/save_custom.tsx index bbf1b66277c70..9dbbcc537fa31 100644 --- a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/add_source/save_custom.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/add_source/save_custom.tsx @@ -24,14 +24,13 @@ import { import { i18n } from '@kbn/i18n'; import { FormattedMessage } from '@kbn/i18n-react'; +import { docLinks } from '../../../../../shared/doc_links'; import { LicensingLogic } from '../../../../../shared/licensing'; import { EuiLinkTo } from '../../../../../shared/react_router_helpers'; import { LicenseBadge } from '../../../../components/shared/license_badge'; import { SOURCES_PATH, SOURCE_DISPLAY_SETTINGS_PATH, - CUSTOM_API_DOCUMENT_PERMISSIONS_DOCS_URL, - ENT_SEARCH_LICENSE_MANAGEMENT, getContentSourcePath, getSourcesPath, } from '../../../../routes'; @@ -178,7 +177,10 @@ export const SaveCustom: React.FC = ({ defaultMessage="{link} manage content access content on individual or group attributes. Allow or deny access to specific documents." values={{ link: ( - + {SAVE_CUSTOM_DOC_PERMISSIONS_LINK} ), @@ -189,7 +191,7 @@ export const SaveCustom: React.FC = ({ {!hasPlatinumLicense && ( - + {LEARN_CUSTOM_FEATURES_BUTTON} diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/download_diagnostics_button.test.tsx b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/download_diagnostics_button.test.tsx new file mode 100644 index 0000000000000..ca2af637c1d6e --- /dev/null +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/download_diagnostics_button.test.tsx @@ -0,0 +1,51 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { setMockValues } from '../../../../__mocks__/kea_logic'; +import { fullContentSources } from '../../../__mocks__/content_sources.mock'; + +import React from 'react'; + +import { shallow } from 'enzyme'; + +import { EuiButton } from '@elastic/eui'; + +import { DownloadDiagnosticsButton } from './download_diagnostics_button'; + +describe('DownloadDiagnosticsButton', () => { + const label = 'foo123'; + const contentSource = fullContentSources[0]; + const buttonLoading = false; + const isOrganization = true; + + const mockValues = { + contentSource, + buttonLoading, + isOrganization, + }; + + beforeEach(() => { + setMockValues(mockValues); + }); + + it('renders the Download diagnostics button with org href', () => { + const wrapper = shallow(); + + expect(wrapper.find(EuiButton).prop('href')).toEqual( + '/internal/workplace_search/org/sources/123/download_diagnostics' + ); + }); + + it('renders the Download diagnostics button with account href', () => { + setMockValues({ ...mockValues, isOrganization: false }); + const wrapper = shallow(); + + expect(wrapper.find(EuiButton).prop('href')).toEqual( + '/internal/workplace_search/account/sources/123/download_diagnostics' + ); + }); +}); diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/download_diagnostics_button.tsx b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/download_diagnostics_button.tsx new file mode 100644 index 0000000000000..866746f43d653 --- /dev/null +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/download_diagnostics_button.tsx @@ -0,0 +1,48 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import React from 'react'; + +import { useValues } from 'kea'; + +import { EuiButton } from '@elastic/eui'; + +import { HttpLogic } from '../../../../shared/http'; +import { AppLogic } from '../../../app_logic'; + +import { SourceLogic } from '../source_logic'; + +interface Props { + label: string; +} + +export const DownloadDiagnosticsButton: React.FC = ({ label }) => { + const { http } = useValues(HttpLogic); + const { isOrganization } = useValues(AppLogic); + const { + contentSource: { id, serviceType }, + buttonLoading, + } = useValues(SourceLogic); + + const diagnosticsPath = isOrganization + ? http.basePath.prepend(`/internal/workplace_search/org/sources/${id}/download_diagnostics`) + : http.basePath.prepend( + `/internal/workplace_search/account/sources/${id}/download_diagnostics` + ); + + return ( + + {label} + + ); +}; diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/overview.tsx b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/overview.tsx index 29abbf94db397..d3714c2174b66 100644 --- a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/overview.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/overview.tsx @@ -33,6 +33,7 @@ import { import { FormattedMessage } from '@kbn/i18n-react'; import { CANCEL_BUTTON_LABEL, START_BUTTON_LABEL } from '../../../../shared/constants'; +import { docLinks } from '../../../../shared/doc_links'; import { EuiListGroupItemTo, EuiLinkTo } from '../../../../shared/react_router_helpers'; import { AppLogic } from '../../../app_logic'; import aclImage from '../../../assets/supports_acl.svg'; @@ -46,10 +47,6 @@ import { DOCUMENTATION_LINK_TITLE, } from '../../../constants'; import { - CUSTOM_SOURCE_DOCS_URL, - DOCUMENT_PERMISSIONS_DOCS_URL, - ENT_SEARCH_LICENSE_MANAGEMENT, - EXTERNAL_IDENTITIES_DOCS_URL, SYNC_FREQUENCY_PATH, BLOCKED_TIME_WINDOWS_PATH, getGroupPath, @@ -347,7 +344,7 @@ export const Overview: React.FC = () => { defaultMessage="{learnMoreLink} about permissions" values={{ learnMoreLink: ( - + {LEARN_MORE_LINK} ), @@ -408,7 +405,7 @@ export const Overview: React.FC = () => { defaultMessage="The {externalIdentitiesLink} must be used to configure user access mappings. Read the guide to learn more." values={{ externalIdentitiesLink: ( - + {EXTERNAL_IDENTITIES_LINK} ), @@ -466,7 +463,7 @@ export const Overview: React.FC = () => { - + {LEARN_CUSTOM_FEATURES_BUTTON} @@ -569,7 +566,7 @@ export const Overview: React.FC = () => { defaultMessage="{learnMoreLink} about custom sources." values={{ learnMoreLink: ( - + {LEARN_MORE_LINK} ), diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/source_content.tsx b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/source_content.tsx index 6b0e43fbce0c4..e37849033a144 100644 --- a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/source_content.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/source_content.tsx @@ -31,12 +31,12 @@ import { import { i18n } from '@kbn/i18n'; import { FormattedMessage } from '@kbn/i18n-react'; +import { docLinks } from '../../../../shared/doc_links'; import { TruncatedContent } from '../../../../shared/truncate'; import { ComponentLoader } from '../../../components/shared/component_loader'; import { TablePaginationBar } from '../../../components/shared/table_pagination_bar'; import { ViewContentHeader } from '../../../components/shared/view_content_header'; import { NAV, CUSTOM_SERVICE_TYPE } from '../../../constants'; -import { CUSTOM_SOURCE_DOCS_URL } from '../../../routes'; import { SourceContentItem } from '../../../types'; import { NO_CONTENT_MESSAGE, @@ -110,7 +110,7 @@ export const SourceContent: React.FC = () => { defaultMessage="Learn more about adding content in our {documentationLink}" values={{ documentationLink: ( - + {CUSTOM_DOCUMENTATION_LINK} ), diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/source_layout.test.tsx b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/source_layout.test.tsx index 944a54169f0b8..62d1bff27dd78 100644 --- a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/source_layout.test.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/source_layout.test.tsx @@ -18,6 +18,7 @@ import { EuiCallOut } from '@elastic/eui'; import { WorkplaceSearchPageTemplate, PersonalDashboardLayout } from '../../../components/layout'; +import { DownloadDiagnosticsButton } from './download_diagnostics_button'; import { SourceInfoCard } from './source_info_card'; import { SourceLayout } from './source_layout'; @@ -26,6 +27,7 @@ describe('SourceLayout', () => { const mockValues = { contentSource, dataLoading: false, + diagnosticDownloadButtonVisible: false, isOrganization: true, }; @@ -87,4 +89,14 @@ describe('SourceLayout', () => { expect(wrapper.find(EuiCallOut)).toHaveLength(1); }); + + it('renders DownloadDiagnosticsButton', () => { + setMockValues({ + ...mockValues, + diagnosticDownloadButtonVisible: true, + }); + const wrapper = shallow(); + + expect(wrapper.find(DownloadDiagnosticsButton)).toHaveLength(1); + }); }); diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/source_layout.tsx b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/source_layout.tsx index 663088f797c18..727e171d1073c 100644 --- a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/source_layout.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/source_layout.tsx @@ -12,19 +12,21 @@ import moment from 'moment'; import { EuiButton, EuiCallOut, EuiHorizontalRule, EuiSpacer } from '@elastic/eui'; +import { docLinks } from '../../../../shared/doc_links'; import { PageTemplateProps } from '../../../../shared/layout'; import { AppLogic } from '../../../app_logic'; import { WorkplaceSearchPageTemplate, PersonalDashboardLayout } from '../../../components/layout'; import { NAV } from '../../../constants'; -import { ENT_SEARCH_LICENSE_MANAGEMENT } from '../../../routes'; import { + DOWNLOAD_DIAGNOSTIC_BUTTON, SOURCE_DISABLED_CALLOUT_TITLE, SOURCE_DISABLED_CALLOUT_DESCRIPTION, SOURCE_DISABLED_CALLOUT_BUTTON, } from '../constants'; import { SourceLogic } from '../source_logic'; +import { DownloadDiagnosticsButton } from './download_diagnostics_button'; import { SourceInfoCard } from './source_info_card'; export const SourceLayout: React.FC = ({ @@ -32,7 +34,7 @@ export const SourceLayout: React.FC = ({ pageChrome = [], ...props }) => { - const { contentSource, dataLoading } = useValues(SourceLogic); + const { contentSource, dataLoading, diagnosticDownloadButtonVisible } = useValues(SourceLogic); const { isOrganization } = useValues(AppLogic); const { name, createdAt, serviceType, isFederatedSource, supportedByLicense } = contentSource; @@ -53,7 +55,7 @@ export const SourceLayout: React.FC = ({ <>

{SOURCE_DISABLED_CALLOUT_DESCRIPTION}

- + {SOURCE_DISABLED_CALLOUT_BUTTON}
@@ -61,6 +63,13 @@ export const SourceLayout: React.FC = ({ ); + const downloadDiagnosticButton = ( + <> + + + + ); + const Layout = isOrganization ? WorkplaceSearchPageTemplate : PersonalDashboardLayout; return ( @@ -69,6 +78,7 @@ export const SourceLayout: React.FC = ({ {...props} pageChrome={[NAV.SOURCES, name || '...', ...pageChrome]} > + {diagnosticDownloadButtonVisible && downloadDiagnosticButton} {!supportedByLicense && callout} {pageHeader} {children} diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/source_settings.test.tsx b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/source_settings.test.tsx index 83cf21ce86233..ec499293f2fd1 100644 --- a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/source_settings.test.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/source_settings.test.tsx @@ -18,6 +18,7 @@ import { EuiConfirmModal } from '@elastic/eui'; import { SourceConfigFields } from '../../../components/shared/source_config_fields'; +import { DownloadDiagnosticsButton } from './download_diagnostics_button'; import { SourceSettings } from './source_settings'; describe('SourceSettings', () => { @@ -48,6 +49,7 @@ describe('SourceSettings', () => { const wrapper = shallow(); expect(wrapper.find('form')).toHaveLength(1); + expect(wrapper.find(DownloadDiagnosticsButton)).toHaveLength(1); }); it('handles form submission', () => { @@ -104,36 +106,4 @@ describe('SourceSettings', () => { sourceConfigData.configuredFields.publicKey ); }); - - describe('DownloadDiagnosticsButton', () => { - it('renders for org with correct href', () => { - const wrapper = shallow(); - - expect(wrapper.find('[data-test-subj="DownloadDiagnosticsButton"]').prop('href')).toEqual( - '/internal/workplace_search/org/sources/123/download_diagnostics' - ); - }); - - it('renders for account with correct href', () => { - setMockValues({ - ...mockValues, - isOrganization: false, - }); - const wrapper = shallow(); - - expect(wrapper.find('[data-test-subj="DownloadDiagnosticsButton"]').prop('href')).toEqual( - '/internal/workplace_search/account/sources/123/download_diagnostics' - ); - }); - - it('renders with the correct download file name', () => { - jest.spyOn(global.Date, 'now').mockImplementationOnce(() => new Date('1970-01-01').valueOf()); - - const wrapper = shallow(); - - expect(wrapper.find('[data-test-subj="DownloadDiagnosticsButton"]').prop('download')).toEqual( - '123_custom_0_diagnostics.json' - ); - }); - }); }); diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/source_settings.tsx b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/source_settings.tsx index e5924b672c771..484a9ca14b4e1 100644 --- a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/source_settings.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/source_settings.tsx @@ -23,7 +23,6 @@ import { } from '@elastic/eui'; import { FormattedMessage } from '@kbn/i18n-react'; -import { HttpLogic } from '../../../../shared/http'; import { EuiButtonEmptyTo } from '../../../../shared/react_router_helpers'; import { AppLogic } from '../../../app_logic'; import { ContentSection } from '../../../components/shared/content_section'; @@ -61,11 +60,11 @@ import { import { staticSourceData } from '../source_data'; import { SourceLogic } from '../source_logic'; +import { DownloadDiagnosticsButton } from './download_diagnostics_button'; + import { SourceLayout } from './source_layout'; export const SourceSettings: React.FC = () => { - const { http } = useValues(HttpLogic); - const { updateContentSource, removeContentSource, @@ -110,12 +109,6 @@ export const SourceSettings: React.FC = () => { const { clientId, clientSecret, publicKey, consumerKey, baseUrl } = configuredFields || {}; - const diagnosticsPath = isOrganization - ? http.basePath.prepend(`/internal/workplace_search/org/sources/${id}/download_diagnostics`) - : http.basePath.prepend( - `/internal/workplace_search/account/sources/${id}/download_diagnostics` - ); - const handleNameChange = (e: ChangeEvent) => setValue(e.target.value); const submitNameChange = (e: FormEvent) => { @@ -241,15 +234,7 @@ export const SourceSettings: React.FC = () => { )} - - {SYNC_DIAGNOSTICS_BUTTON} - + = ({ tabId }) => { description={ <> {SOURCE_FREQUENCY_DESCRIPTION}{' '} - + {LEARN_MORE_LINK} diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/synchronization/objects_and_assets.tsx b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/synchronization/objects_and_assets.tsx index 2dfa2a6420f7f..460f7e7f42055 100644 --- a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/synchronization/objects_and_assets.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/synchronization/objects_and_assets.tsx @@ -22,10 +22,10 @@ import { } from '@elastic/eui'; import { SAVE_BUTTON_LABEL } from '../../../../../shared/constants'; +import { docLinks } from '../../../../../shared/doc_links'; import { UnsavedChangesPrompt } from '../../../../../shared/unsaved_changes_prompt'; import { ViewContentHeader } from '../../../../components/shared/view_content_header'; import { NAV, RESET_BUTTON } from '../../../../constants'; -import { OBJECTS_AND_ASSETS_DOCS_URL } from '../../../../routes'; import { LEARN_MORE_LINK, SYNC_MANAGEMENT_CONTENT_EXTRACTION_LABEL, @@ -87,7 +87,7 @@ export const ObjectsAndAssets: React.FC = () => { description={ <> {SOURCE_OBJECTS_AND_ASSETS_DESCRIPTION}{' '} - + {LEARN_MORE_LINK} diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/synchronization/synchronization.tsx b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/synchronization/synchronization.tsx index dec275adb3c50..2e777fa906dd6 100644 --- a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/synchronization/synchronization.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/components/synchronization/synchronization.tsx @@ -11,9 +11,9 @@ import { useActions, useValues } from 'kea'; import { EuiCallOut, EuiLink, EuiPanel, EuiSwitch, EuiSpacer, EuiText } from '@elastic/eui'; +import { docLinks } from '../../../../../shared/doc_links'; import { ViewContentHeader } from '../../../../components/shared/view_content_header'; import { NAV } from '../../../../constants'; -import { SYNCHRONIZATION_DOCS_URL } from '../../../../routes'; import { LEARN_MORE_LINK, SOURCE_SYNCHRONIZATION_DESCRIPTION, @@ -68,7 +68,7 @@ export const Synchronization: React.FC = () => { description={ <> {SOURCE_SYNCHRONIZATION_DESCRIPTION}{' '} - + {LEARN_MORE_LINK} diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/constants.ts b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/constants.ts index 61e4aa3fc3884..43b391bc1d824 100644 --- a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/constants.ts +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/constants.ts @@ -370,6 +370,13 @@ export const SYNC_DIAGNOSTICS_BUTTON = i18n.translate( } ); +export const DOWNLOAD_DIAGNOSTIC_BUTTON = i18n.translate( + 'xpack.enterpriseSearch.workplaceSearch.sources.downloadDiagnosticButton', + { + defaultMessage: 'Download diagnostic bundle', + } +); + export const SOURCE_NAME_LABEL = i18n.translate( 'xpack.enterpriseSearch.workplaceSearch.sources.sourceName.label', { diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/source_data.tsx b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/source_data.tsx index 687461296ac9e..20a0673709b5a 100644 --- a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/source_data.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/source_data.tsx @@ -7,6 +7,8 @@ import { i18n } from '@kbn/i18n'; +import { docLinks } from '../../../shared/doc_links'; + import { SOURCE_NAMES, SOURCE_OBJ_TYPES, GITHUB_LINK_TITLE } from '../../constants'; import { ADD_BOX_PATH, @@ -45,23 +47,6 @@ import { EDIT_SLACK_PATH, EDIT_ZENDESK_PATH, EDIT_CUSTOM_PATH, - BOX_DOCS_URL, - CONFLUENCE_DOCS_URL, - CONFLUENCE_SERVER_DOCS_URL, - GITHUB_ENTERPRISE_DOCS_URL, - DROPBOX_DOCS_URL, - GITHUB_DOCS_URL, - GMAIL_DOCS_URL, - GOOGLE_DRIVE_DOCS_URL, - JIRA_DOCS_URL, - JIRA_SERVER_DOCS_URL, - ONEDRIVE_DOCS_URL, - SALESFORCE_DOCS_URL, - SERVICENOW_DOCS_URL, - SHAREPOINT_DOCS_URL, - SLACK_DOCS_URL, - ZENDESK_DOCS_URL, - CUSTOM_SOURCE_DOCS_URL, } from '../../routes'; import { FeatureIds, SourceDataItem } from '../../types'; @@ -75,7 +60,7 @@ export const staticSourceData = [ isPublicKey: false, hasOauthRedirect: true, needsBaseUrl: false, - documentationUrl: BOX_DOCS_URL, + documentationUrl: docLinks.workplaceSearchBox, applicationPortalUrl: 'https://app.box.com/developers/console', }, objTypes: [SOURCE_OBJ_TYPES.FOLDERS, SOURCE_OBJ_TYPES.ALL_FILES], @@ -104,7 +89,7 @@ export const staticSourceData = [ isPublicKey: false, hasOauthRedirect: true, needsBaseUrl: true, - documentationUrl: CONFLUENCE_DOCS_URL, + documentationUrl: docLinks.workplaceSearchConfluenceCloud, applicationPortalUrl: 'https://developer.atlassian.com/console/myapps/', }, objTypes: [ @@ -138,7 +123,7 @@ export const staticSourceData = [ isPublicKey: true, hasOauthRedirect: true, needsBaseUrl: false, - documentationUrl: CONFLUENCE_SERVER_DOCS_URL, + documentationUrl: docLinks.workplaceSearchConfluenceServer, }, objTypes: [ SOURCE_OBJ_TYPES.PAGES, @@ -170,7 +155,7 @@ export const staticSourceData = [ isPublicKey: false, hasOauthRedirect: true, needsBaseUrl: false, - documentationUrl: DROPBOX_DOCS_URL, + documentationUrl: docLinks.workplaceSearchDropbox, applicationPortalUrl: 'https://www.dropbox.com/developers/apps', }, objTypes: [SOURCE_OBJ_TYPES.FOLDERS, SOURCE_OBJ_TYPES.ALL_FILES], @@ -200,7 +185,7 @@ export const staticSourceData = [ hasOauthRedirect: true, needsBaseUrl: false, needsConfiguration: true, - documentationUrl: GITHUB_DOCS_URL, + documentationUrl: docLinks.workplaceSearchGitHub, applicationPortalUrl: 'https://github.com/settings/developers', applicationLinkTitle: GITHUB_LINK_TITLE, }, @@ -242,7 +227,7 @@ export const staticSourceData = [ defaultMessage: 'GitHub Enterprise URL', } ), - documentationUrl: GITHUB_ENTERPRISE_DOCS_URL, + documentationUrl: docLinks.workplaceSearchGitHub, applicationPortalUrl: 'https://github.com/settings/developers', applicationLinkTitle: GITHUB_LINK_TITLE, }, @@ -277,7 +262,7 @@ export const staticSourceData = [ isPublicKey: false, hasOauthRedirect: true, needsBaseUrl: false, - documentationUrl: GMAIL_DOCS_URL, + documentationUrl: docLinks.workplaceSearchGmail, applicationPortalUrl: 'https://console.developers.google.com/', }, objTypes: [SOURCE_OBJ_TYPES.EMAILS], @@ -295,7 +280,7 @@ export const staticSourceData = [ isPublicKey: false, hasOauthRedirect: true, needsBaseUrl: false, - documentationUrl: GOOGLE_DRIVE_DOCS_URL, + documentationUrl: docLinks.workplaceSearchGoogleDrive, applicationPortalUrl: 'https://console.developers.google.com/', }, objTypes: [ @@ -328,7 +313,7 @@ export const staticSourceData = [ isPublicKey: false, hasOauthRedirect: true, needsBaseUrl: true, - documentationUrl: JIRA_DOCS_URL, + documentationUrl: docLinks.workplaceSearchJiraCloud, applicationPortalUrl: 'https://developer.atlassian.com/console/myapps/', }, objTypes: [ @@ -364,7 +349,7 @@ export const staticSourceData = [ isPublicKey: true, hasOauthRedirect: true, needsBaseUrl: false, - documentationUrl: JIRA_SERVER_DOCS_URL, + documentationUrl: docLinks.workplaceSearchJiraServer, applicationPortalUrl: '', }, objTypes: [ @@ -399,7 +384,7 @@ export const staticSourceData = [ isPublicKey: false, hasOauthRedirect: true, needsBaseUrl: false, - documentationUrl: ONEDRIVE_DOCS_URL, + documentationUrl: docLinks.workplaceSearchOneDrive, applicationPortalUrl: 'https://portal.azure.com/', }, objTypes: [SOURCE_OBJ_TYPES.FOLDERS, SOURCE_OBJ_TYPES.ALL_FILES], @@ -428,7 +413,7 @@ export const staticSourceData = [ isPublicKey: false, hasOauthRedirect: true, needsBaseUrl: false, - documentationUrl: SALESFORCE_DOCS_URL, + documentationUrl: docLinks.workplaceSearchSalesforce, applicationPortalUrl: 'https://salesforce.com/', }, objTypes: [ @@ -464,7 +449,7 @@ export const staticSourceData = [ isPublicKey: false, hasOauthRedirect: true, needsBaseUrl: false, - documentationUrl: SALESFORCE_DOCS_URL, + documentationUrl: docLinks.workplaceSearchSalesforce, applicationPortalUrl: 'https://test.salesforce.com/', }, objTypes: [ @@ -500,7 +485,7 @@ export const staticSourceData = [ isPublicKey: false, hasOauthRedirect: false, needsBaseUrl: true, - documentationUrl: SERVICENOW_DOCS_URL, + documentationUrl: docLinks.workplaceSearchServiceNow, applicationPortalUrl: 'https://www.servicenow.com/my-account/sign-in.html', }, objTypes: [ @@ -533,7 +518,7 @@ export const staticSourceData = [ isPublicKey: false, hasOauthRedirect: true, needsBaseUrl: false, - documentationUrl: SHAREPOINT_DOCS_URL, + documentationUrl: docLinks.workplaceSearchSharePoint, applicationPortalUrl: 'https://portal.azure.com/', }, objTypes: [SOURCE_OBJ_TYPES.FOLDERS, SOURCE_OBJ_TYPES.SITES, SOURCE_OBJ_TYPES.ALL_FILES], @@ -562,7 +547,7 @@ export const staticSourceData = [ isPublicKey: false, hasOauthRedirect: true, needsBaseUrl: false, - documentationUrl: SLACK_DOCS_URL, + documentationUrl: docLinks.workplaceSearchSlack, applicationPortalUrl: 'https://api.slack.com/apps/', }, objTypes: [ @@ -585,7 +570,7 @@ export const staticSourceData = [ hasOauthRedirect: true, needsBaseUrl: false, needsSubdomain: true, - documentationUrl: ZENDESK_DOCS_URL, + documentationUrl: docLinks.workplaceSearchZendesk, applicationPortalUrl: 'https://www.zendesk.com/login/', }, objTypes: [SOURCE_OBJ_TYPES.TICKETS], @@ -617,7 +602,7 @@ export const staticSourceData = [ defaultMessage: 'To create a Custom API Source, provide a human-readable and descriptive name. The name will appear as-is in the various search experiences and management interfaces.', }), - documentationUrl: CUSTOM_SOURCE_DOCS_URL, + documentationUrl: docLinks.workplaceSearchCustomSources, applicationPortalUrl: '', }, accountContextOnly: false, diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/source_logic.test.ts b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/source_logic.test.ts index e7888175bb31a..420909df081b3 100644 --- a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/source_logic.test.ts +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/source_logic.test.ts @@ -40,6 +40,7 @@ describe('SourceLogic', () => { dataLoading: true, sectionLoading: true, buttonLoading: false, + diagnosticDownloadButtonVisible: false, contentMeta: DEFAULT_META, contentFilterValue: '', isConfigurationUpdateButtonLoading: false, @@ -125,6 +126,12 @@ describe('SourceLogic', () => { expect(SourceLogic.values.buttonLoading).toEqual(false); }); + + it('showDiagnosticDownloadButton', () => { + SourceLogic.actions.showDiagnosticDownloadButton(); + + expect(SourceLogic.values.diagnosticDownloadButtonVisible).toEqual(true); + }); }); describe('listeners', () => { @@ -183,6 +190,27 @@ describe('SourceLogic', () => { expect(flashAPIErrors).toHaveBeenCalledWith('error'); }); + it('handles error message with diagnostic bundle error message', async () => { + const showDiagnosticDownloadButtonSpy = jest.spyOn( + SourceLogic.actions, + 'showDiagnosticDownloadButton' + ); + + // For contenst source errors, the API returns the source errors in an error property in the success + // response. We don't reject here because we still render the content source with the error. + const promise = Promise.resolve({ + ...contentSource, + errors: [ + 'The database is on fire. [Check diagnostic bundle for details - Message id: 123]', + ], + }); + http.get.mockReturnValue(promise); + SourceLogic.actions.initializeSource(contentSource.id); + await promise; + + expect(showDiagnosticDownloadButtonSpy).toHaveBeenCalled(); + }); + describe('404s', () => { const mock404 = Promise.reject({ response: { status: 404 } }); diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/source_logic.ts b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/source_logic.ts index b76627f57b3a3..8f0cfa8cfa280 100644 --- a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/source_logic.ts +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/source_logic.ts @@ -52,6 +52,7 @@ export interface SourceActions { setButtonNotLoading(): void; setStagedPrivateKey(stagedPrivateKey: string | null): string | null; setConfigurationUpdateButtonNotLoading(): void; + showDiagnosticDownloadButton(): void; } interface SourceValues { @@ -59,6 +60,7 @@ interface SourceValues { dataLoading: boolean; sectionLoading: boolean; buttonLoading: boolean; + diagnosticDownloadButtonVisible: boolean; contentItems: SourceContentItem[]; contentMeta: Meta; contentFilterValue: string; @@ -108,6 +110,7 @@ export const SourceLogic = kea>({ setButtonNotLoading: () => false, setStagedPrivateKey: (stagedPrivateKey: string) => stagedPrivateKey, setConfigurationUpdateButtonNotLoading: () => false, + showDiagnosticDownloadButton: true, }, reducers: { contentSource: [ @@ -147,6 +150,13 @@ export const SourceLogic = kea>({ setSearchResults: () => false, }, ], + diagnosticDownloadButtonVisible: [ + false, + { + showDiagnosticDownloadButton: () => true, + initializeSource: () => false, + }, + ], contentItems: [ [], { @@ -200,6 +210,9 @@ export const SourceLogic = kea>({ } if (response.errors) { setErrorMessage(response.errors); + if (errorsHaveDiagnosticBundleString(response.errors as unknown as string[])) { + actions.showDiagnosticDownloadButton(); + } } else { clearFlashMessages(); } @@ -343,3 +356,8 @@ const setPage = (state: Meta, page: number) => ({ current: page, }, }); + +const errorsHaveDiagnosticBundleString = (errors: string[]) => { + const ERROR_SUBSTRING = 'Check diagnostic bundle for details'; + return errors.find((e) => e.includes(ERROR_SUBSTRING)); +}; diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/sources_view.tsx b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/sources_view.tsx index 8697f10f8afaf..a7c981dad9103 100644 --- a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/sources_view.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/content_sources/sources_view.tsx @@ -24,9 +24,9 @@ import { import { i18n } from '@kbn/i18n'; import { FormattedMessage } from '@kbn/i18n-react'; +import { docLinks } from '../../../shared/doc_links'; import { Loading } from '../../../shared/loading'; import { SourceIcon } from '../../components/shared/source_icon'; -import { EXTERNAL_IDENTITIES_DOCS_URL, DOCUMENT_PERMISSIONS_DOCS_URL } from '../../routes'; import { EXTERNAL_IDENTITIES_LINK, @@ -82,7 +82,7 @@ export const SourcesView: React.FC = ({ children }) => { values={{ addedSourceName, externalIdentitiesLink: ( - + {EXTERNAL_IDENTITIES_LINK} ), @@ -96,7 +96,7 @@ export const SourcesView: React.FC = ({ children }) => { defaultMessage="Documents will not be searchable from Workplace Search until user and group mappings have been configured. {documentPermissionsLink}." values={{ documentPermissionsLink: ( - + {DOCUMENT_PERMISSIONS_LINK} ), diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/error_state/error_state.test.tsx b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/error_state/error_state.test.tsx index a8fcdfd7cb257..e4e14b19f1894 100644 --- a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/error_state/error_state.test.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/error_state/error_state.test.tsx @@ -15,8 +15,10 @@ import { ErrorState } from './'; describe('ErrorState', () => { it('renders', () => { - const wrapper = shallow(); + const wrapper = shallow(); - expect(wrapper.find(ErrorStatePrompt)).toHaveLength(1); + const prompt = wrapper.find(ErrorStatePrompt); + expect(prompt).toHaveLength(1); + expect(prompt.prop('errorConnectingMessage')).toEqual('I am an error'); }); }); diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/error_state/error_state.tsx b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/error_state/error_state.tsx index 83ac3a26c44e5..493c37189ceb7 100644 --- a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/error_state/error_state.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/error_state/error_state.tsx @@ -15,7 +15,9 @@ import { SetWorkplaceSearchChrome as SetPageChrome } from '../../../shared/kiban import { SendWorkplaceSearchTelemetry as SendTelemetry } from '../../../shared/telemetry'; import { ViewContentHeader } from '../../components/shared/view_content_header'; -export const ErrorState: React.FC = () => { +export const ErrorState: React.FC<{ errorConnectingMessage?: string }> = ({ + errorConnectingMessage, +}) => { return ( <> @@ -23,7 +25,7 @@ export const ErrorState: React.FC = () => { - + ); diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/role_mappings/role_mappings.tsx b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/role_mappings/role_mappings.tsx index f7e578b1b4d23..c0362b44b618b 100644 --- a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/role_mappings/role_mappings.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/role_mappings/role_mappings.tsx @@ -12,6 +12,7 @@ import { useActions, useValues } from 'kea'; import { EuiSpacer } from '@elastic/eui'; import { WORKPLACE_SEARCH_PLUGIN } from '../../../../../common/constants'; +import { docLinks } from '../../../shared/doc_links'; import { RoleMappingsTable, RoleMappingsHeading, @@ -22,7 +23,6 @@ import { } from '../../../shared/role_mapping'; import { ROLE_MAPPINGS_TITLE } from '../../../shared/role_mapping/constants'; import { WorkplaceSearchPageTemplate } from '../../components/layout'; -import { SECURITY_DOCS_URL } from '../../routes'; import { ROLE_MAPPINGS_TABLE_HEADER } from './constants'; @@ -56,7 +56,7 @@ export const RoleMappings: React.FC = () => { const rolesEmptyState = ( ); @@ -65,7 +65,7 @@ export const RoleMappings: React.FC = () => {
initializeRoleMapping()} /> { @@ -100,7 +100,7 @@ export const OauthApplication: React.FC = () => { <> {NON_PLATINUM_OAUTH_DESCRIPTION} - + {EXPLORE_PLATINUM_FEATURES_LINK} diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/setup_guide/setup_guide.tsx b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/setup_guide/setup_guide.tsx index 009dbffafebd8..3c3a7085d7116 100644 --- a/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/setup_guide/setup_guide.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/views/setup_guide/setup_guide.tsx @@ -12,14 +12,14 @@ import { i18n } from '@kbn/i18n'; import { FormattedMessage } from '@kbn/i18n-react'; import { WORKPLACE_SEARCH_PLUGIN } from '../../../../../common/constants'; +import { docLinks } from '../../../shared/doc_links'; import { SetWorkplaceSearchChrome as SetPageChrome } from '../../../shared/kibana_chrome'; import { SetupGuideLayout, SETUP_GUIDE_TITLE } from '../../../shared/setup_guide'; import { SendWorkplaceSearchTelemetry as SendTelemetry } from '../../../shared/telemetry'; -import { GETTING_STARTED_DOCS_URL } from '../../routes'; import GettingStarted from './assets/getting_started.png'; -const GETTING_STARTED_LINK_URL = GETTING_STARTED_DOCS_URL; +const GETTING_STARTED_LINK_URL = docLinks.workplaceSearchGettingStarted; export const SetupGuide: React.FC = () => { return ( diff --git a/x-pack/plugins/enterprise_search/public/plugin.ts b/x-pack/plugins/enterprise_search/public/plugin.ts index 19f2aa212d7fd..9a8ff64649f0e 100644 --- a/x-pack/plugins/enterprise_search/public/plugin.ts +++ b/x-pack/plugins/enterprise_search/public/plugin.ts @@ -39,6 +39,7 @@ export interface ClientConfigType { export interface ClientData extends InitialAppData { publicUrl?: string; errorConnecting?: boolean; + errorConnectingMessage?: string; } interface PluginsSetup { @@ -193,8 +194,9 @@ export class EnterpriseSearchPlugin implements Plugin { try { this.data = await http.get('/internal/enterprise_search/config_data'); this.hasInitialized = true; - } catch { + } catch (e) { this.data.errorConnecting = true; + this.data.errorConnectingMessage = `${e.res.status} ${e.message}`; } } } diff --git a/x-pack/plugins/enterprise_search/server/lib/enterprise_search_config_api.test.ts b/x-pack/plugins/enterprise_search/server/lib/enterprise_search_config_api.test.ts index f6e3280a8abb2..0a0a097da10aa 100644 --- a/x-pack/plugins/enterprise_search/server/lib/enterprise_search_config_api.test.ts +++ b/x-pack/plugins/enterprise_search/server/lib/enterprise_search_config_api.test.ts @@ -120,6 +120,7 @@ describe('callEnterpriseSearchConfigAPI', () => { expect(await callEnterpriseSearchConfigAPI(mockDependencies)).toEqual({ ...DEFAULT_INITIAL_APP_DATA, + errorConnectingMessage: undefined, kibanaVersion: '1.0.0', access: { hasAppSearchAccess: true, diff --git a/x-pack/plugins/enterprise_search/server/routes/app_search/crawler.test.ts b/x-pack/plugins/enterprise_search/server/routes/app_search/crawler.test.ts index 3070be1e56b5b..c9212bca322d7 100644 --- a/x-pack/plugins/enterprise_search/server/routes/app_search/crawler.test.ts +++ b/x-pack/plugins/enterprise_search/server/routes/app_search/crawler.test.ts @@ -28,7 +28,7 @@ describe('crawler routes', () => { it('creates a request to enterprise search', () => { expect(mockRequestHandler.createRequest).toHaveBeenCalledWith({ - path: '/api/as/v0/engines/:name/crawler', + path: '/api/as/v1/engines/:name/crawler', }); }); @@ -61,7 +61,7 @@ describe('crawler routes', () => { it('creates a request to enterprise search', () => { expect(mockRequestHandler.createRequest).toHaveBeenCalledWith({ - path: '/api/as/v0/engines/:name/crawler/crawl_requests', + path: '/api/as/v1/engines/:name/crawler/crawl_requests', }); }); @@ -94,7 +94,7 @@ describe('crawler routes', () => { it('creates a request to enterprise search', () => { expect(mockRequestHandler.createRequest).toHaveBeenCalledWith({ - path: '/api/as/v0/engines/:name/crawler/crawl_requests/:id', + path: '/api/as/v1/engines/:name/crawler/crawl_requests/:id', }); }); @@ -132,7 +132,7 @@ describe('crawler routes', () => { it('creates a request to enterprise search', () => { expect(mockRequestHandler.createRequest).toHaveBeenCalledWith({ - path: '/api/as/v0/engines/:name/crawler/crawl_requests', + path: '/api/as/v1/engines/:name/crawler/crawl_requests', }); }); @@ -165,7 +165,7 @@ describe('crawler routes', () => { it('creates a request to enterprise search', () => { expect(mockRequestHandler.createRequest).toHaveBeenCalledWith({ - path: '/api/as/v0/engines/:name/crawler/domains', + path: '/api/as/v1/engines/:name/crawler/domains', }); }); @@ -204,7 +204,7 @@ describe('crawler routes', () => { it('creates a request to enterprise search', () => { expect(mockRequestHandler.createRequest).toHaveBeenCalledWith({ - path: '/api/as/v0/engines/:name/crawler/crawl_requests/active/cancel', + path: '/api/as/v1/engines/:name/crawler/crawl_requests/active/cancel', }); }); @@ -237,7 +237,7 @@ describe('crawler routes', () => { it('creates a request to enterprise search', () => { expect(mockRequestHandler.createRequest).toHaveBeenCalledWith({ - path: '/api/as/v0/engines/:name/crawler/domains', + path: '/api/as/v1/engines/:name/crawler/domains', }); }); @@ -293,7 +293,7 @@ describe('crawler routes', () => { it('creates a request to enterprise search', () => { expect(mockRequestHandler.createRequest).toHaveBeenCalledWith({ - path: '/api/as/v0/engines/:name/crawler/domains/:id', + path: '/api/as/v1/engines/:name/crawler/domains/:id', }); }); @@ -339,7 +339,7 @@ describe('crawler routes', () => { it('creates a request to enterprise search', () => { expect(mockRequestHandler.createRequest).toHaveBeenCalledWith({ - path: '/api/as/v0/engines/:name/crawler/domains/:id', + path: '/api/as/v1/engines/:name/crawler/domains/:id', }); }); @@ -397,7 +397,7 @@ describe('crawler routes', () => { it('creates a request to enterprise search', () => { expect(mockRequestHandler.createRequest).toHaveBeenCalledWith({ - path: '/api/as/v0/engines/:name/crawler/domains/:id', + path: '/api/as/v1/engines/:name/crawler/domains/:id', }); }); @@ -435,7 +435,7 @@ describe('crawler routes', () => { it('creates a request to enterprise search', () => { expect(mockRequestHandler.createRequest).toHaveBeenCalledWith({ - path: '/api/as/v0/crawler/validate_url', + path: '/api/as/v1/crawler/validate_url', }); }); @@ -472,7 +472,7 @@ describe('crawler routes', () => { it('creates a request to enterprise search', () => { expect(mockRequestHandler.createRequest).toHaveBeenCalledWith({ - path: '/api/as/v0/engines/:name/crawler/process_crawls', + path: '/api/as/v1/engines/:name/crawler/process_crawls', }); }); @@ -519,7 +519,7 @@ describe('crawler routes', () => { it('creates a request to enterprise search', () => { expect(mockRequestHandler.createRequest).toHaveBeenCalledWith({ - path: '/api/as/v0/engines/:name/crawler/crawl_schedule', + path: '/api/as/v1/engines/:name/crawler/crawl_schedule', }); }); @@ -556,7 +556,7 @@ describe('crawler routes', () => { it('creates a request to enterprise search', () => { expect(mockRequestHandler.createRequest).toHaveBeenCalledWith({ - path: '/api/as/v0/engines/:name/crawler/crawl_schedule', + path: '/api/as/v1/engines/:name/crawler/crawl_schedule', }); }); @@ -611,7 +611,7 @@ describe('crawler routes', () => { it('creates a request to enterprise search', () => { expect(mockRequestHandler.createRequest).toHaveBeenCalledWith({ - path: '/api/as/v0/engines/:name/crawler/crawl_schedule', + path: '/api/as/v1/engines/:name/crawler/crawl_schedule', }); }); diff --git a/x-pack/plugins/enterprise_search/server/routes/app_search/crawler.ts b/x-pack/plugins/enterprise_search/server/routes/app_search/crawler.ts index f53b15dadd061..f0fdc5c16098b 100644 --- a/x-pack/plugins/enterprise_search/server/routes/app_search/crawler.ts +++ b/x-pack/plugins/enterprise_search/server/routes/app_search/crawler.ts @@ -23,7 +23,7 @@ export function registerCrawlerRoutes({ }, }, enterpriseSearchRequestHandler.createRequest({ - path: '/api/as/v0/engines/:name/crawler', + path: '/api/as/v1/engines/:name/crawler', }) ); @@ -37,7 +37,7 @@ export function registerCrawlerRoutes({ }, }, enterpriseSearchRequestHandler.createRequest({ - path: '/api/as/v0/engines/:name/crawler/crawl_requests', + path: '/api/as/v1/engines/:name/crawler/crawl_requests', }) ); @@ -52,7 +52,7 @@ export function registerCrawlerRoutes({ }, }, enterpriseSearchRequestHandler.createRequest({ - path: '/api/as/v0/engines/:name/crawler/crawl_requests/:id', + path: '/api/as/v1/engines/:name/crawler/crawl_requests/:id', }) ); @@ -66,7 +66,7 @@ export function registerCrawlerRoutes({ }, }, enterpriseSearchRequestHandler.createRequest({ - path: '/api/as/v0/engines/:name/crawler/crawl_requests', + path: '/api/as/v1/engines/:name/crawler/crawl_requests', }) ); @@ -80,7 +80,7 @@ export function registerCrawlerRoutes({ }, }, enterpriseSearchRequestHandler.createRequest({ - path: '/api/as/v0/engines/:name/crawler/crawl_requests/active/cancel', + path: '/api/as/v1/engines/:name/crawler/crawl_requests/active/cancel', }) ); @@ -98,7 +98,7 @@ export function registerCrawlerRoutes({ }, }, enterpriseSearchRequestHandler.createRequest({ - path: '/api/as/v0/engines/:name/crawler/domains', + path: '/api/as/v1/engines/:name/crawler/domains', }) ); @@ -123,7 +123,7 @@ export function registerCrawlerRoutes({ }, }, enterpriseSearchRequestHandler.createRequest({ - path: '/api/as/v0/engines/:name/crawler/domains', + path: '/api/as/v1/engines/:name/crawler/domains', }) ); @@ -138,7 +138,7 @@ export function registerCrawlerRoutes({ }, }, enterpriseSearchRequestHandler.createRequest({ - path: '/api/as/v0/engines/:name/crawler/domains/:id', + path: '/api/as/v1/engines/:name/crawler/domains/:id', }) ); @@ -156,7 +156,7 @@ export function registerCrawlerRoutes({ }, }, enterpriseSearchRequestHandler.createRequest({ - path: '/api/as/v0/engines/:name/crawler/domains/:id', + path: '/api/as/v1/engines/:name/crawler/domains/:id', }) ); @@ -183,7 +183,7 @@ export function registerCrawlerRoutes({ }, }, enterpriseSearchRequestHandler.createRequest({ - path: '/api/as/v0/engines/:name/crawler/domains/:id', + path: '/api/as/v1/engines/:name/crawler/domains/:id', }) ); @@ -198,7 +198,7 @@ export function registerCrawlerRoutes({ }, }, enterpriseSearchRequestHandler.createRequest({ - path: '/api/as/v0/crawler/validate_url', + path: '/api/as/v1/crawler/validate_url', }) ); @@ -215,7 +215,7 @@ export function registerCrawlerRoutes({ }, }, enterpriseSearchRequestHandler.createRequest({ - path: '/api/as/v0/engines/:name/crawler/process_crawls', + path: '/api/as/v1/engines/:name/crawler/process_crawls', }) ); @@ -229,7 +229,7 @@ export function registerCrawlerRoutes({ }, }, enterpriseSearchRequestHandler.createRequest({ - path: '/api/as/v0/engines/:name/crawler/crawl_schedule', + path: '/api/as/v1/engines/:name/crawler/crawl_schedule', }) ); @@ -247,7 +247,7 @@ export function registerCrawlerRoutes({ }, }, enterpriseSearchRequestHandler.createRequest({ - path: '/api/as/v0/engines/:name/crawler/crawl_schedule', + path: '/api/as/v1/engines/:name/crawler/crawl_schedule', }) ); @@ -261,7 +261,7 @@ export function registerCrawlerRoutes({ }, }, enterpriseSearchRequestHandler.createRequest({ - path: '/api/as/v0/engines/:name/crawler/crawl_schedule', + path: '/api/as/v1/engines/:name/crawler/crawl_schedule', }) ); } diff --git a/x-pack/plugins/enterprise_search/server/routes/app_search/crawler_crawl_rules.test.ts b/x-pack/plugins/enterprise_search/server/routes/app_search/crawler_crawl_rules.test.ts index 018ab433536b2..c3d1468687ec4 100644 --- a/x-pack/plugins/enterprise_search/server/routes/app_search/crawler_crawl_rules.test.ts +++ b/x-pack/plugins/enterprise_search/server/routes/app_search/crawler_crawl_rules.test.ts @@ -28,7 +28,7 @@ describe('crawler crawl rules routes', () => { it('creates a request to enterprise search', () => { expect(mockRequestHandler.createRequest).toHaveBeenCalledWith({ - path: '/api/as/v0/engines/:engineName/crawler/domains/:domainId/crawl_rules', + path: '/api/as/v1/engines/:engineName/crawler/domains/:domainId/crawl_rules', params: { respond_with: 'index', }, @@ -71,7 +71,7 @@ describe('crawler crawl rules routes', () => { it('creates a request to enterprise search', () => { expect(mockRequestHandler.createRequest).toHaveBeenCalledWith({ - path: '/api/as/v0/engines/:engineName/crawler/domains/:domainId/crawl_rules/:crawlRuleId', + path: '/api/as/v1/engines/:engineName/crawler/domains/:domainId/crawl_rules/:crawlRuleId', params: { respond_with: 'index', }, @@ -115,7 +115,7 @@ describe('crawler crawl rules routes', () => { it('creates a request to enterprise search', () => { expect(mockRequestHandler.createRequest).toHaveBeenCalledWith({ - path: '/api/as/v0/engines/:engineName/crawler/domains/:domainId/crawl_rules/:crawlRuleId', + path: '/api/as/v1/engines/:engineName/crawler/domains/:domainId/crawl_rules/:crawlRuleId', params: { respond_with: 'index', }, diff --git a/x-pack/plugins/enterprise_search/server/routes/app_search/crawler_crawl_rules.ts b/x-pack/plugins/enterprise_search/server/routes/app_search/crawler_crawl_rules.ts index 7c82c73db7263..26637623f0885 100644 --- a/x-pack/plugins/enterprise_search/server/routes/app_search/crawler_crawl_rules.ts +++ b/x-pack/plugins/enterprise_search/server/routes/app_search/crawler_crawl_rules.ts @@ -29,7 +29,7 @@ export function registerCrawlerCrawlRulesRoutes({ }, }, enterpriseSearchRequestHandler.createRequest({ - path: '/api/as/v0/engines/:engineName/crawler/domains/:domainId/crawl_rules', + path: '/api/as/v1/engines/:engineName/crawler/domains/:domainId/crawl_rules', params: { respond_with: 'index', }, @@ -54,7 +54,7 @@ export function registerCrawlerCrawlRulesRoutes({ }, }, enterpriseSearchRequestHandler.createRequest({ - path: '/api/as/v0/engines/:engineName/crawler/domains/:domainId/crawl_rules/:crawlRuleId', + path: '/api/as/v1/engines/:engineName/crawler/domains/:domainId/crawl_rules/:crawlRuleId', params: { respond_with: 'index', }, @@ -73,7 +73,7 @@ export function registerCrawlerCrawlRulesRoutes({ }, }, enterpriseSearchRequestHandler.createRequest({ - path: '/api/as/v0/engines/:engineName/crawler/domains/:domainId/crawl_rules/:crawlRuleId', + path: '/api/as/v1/engines/:engineName/crawler/domains/:domainId/crawl_rules/:crawlRuleId', params: { respond_with: 'index', }, diff --git a/x-pack/plugins/enterprise_search/server/routes/app_search/crawler_entry_points.test.ts b/x-pack/plugins/enterprise_search/server/routes/app_search/crawler_entry_points.test.ts index 6fb7e99400877..dc7ad493a5149 100644 --- a/x-pack/plugins/enterprise_search/server/routes/app_search/crawler_entry_points.test.ts +++ b/x-pack/plugins/enterprise_search/server/routes/app_search/crawler_entry_points.test.ts @@ -28,7 +28,7 @@ describe('crawler entry point routes', () => { it('creates a request to enterprise search', () => { expect(mockRequestHandler.createRequest).toHaveBeenCalledWith({ - path: '/api/as/v0/engines/:engineName/crawler/domains/:domainId/entry_points', + path: '/api/as/v1/engines/:engineName/crawler/domains/:domainId/entry_points', params: { respond_with: 'index', }, @@ -69,7 +69,7 @@ describe('crawler entry point routes', () => { it('creates a request to enterprise search', () => { expect(mockRequestHandler.createRequest).toHaveBeenCalledWith({ - path: '/api/as/v0/engines/:engineName/crawler/domains/:domainId/entry_points/:entryPointId', + path: '/api/as/v1/engines/:engineName/crawler/domains/:domainId/entry_points/:entryPointId', params: { respond_with: 'index', }, @@ -110,7 +110,7 @@ describe('crawler entry point routes', () => { it('creates a request to enterprise search', () => { expect(mockRequestHandler.createRequest).toHaveBeenCalledWith({ - path: '/api/as/v0/engines/:engineName/crawler/domains/:domainId/entry_points/:entryPointId', + path: '/api/as/v1/engines/:engineName/crawler/domains/:domainId/entry_points/:entryPointId', params: { respond_with: 'index', }, diff --git a/x-pack/plugins/enterprise_search/server/routes/app_search/crawler_entry_points.ts b/x-pack/plugins/enterprise_search/server/routes/app_search/crawler_entry_points.ts index a6d6fdb24b41f..fd81475c860ad 100644 --- a/x-pack/plugins/enterprise_search/server/routes/app_search/crawler_entry_points.ts +++ b/x-pack/plugins/enterprise_search/server/routes/app_search/crawler_entry_points.ts @@ -27,7 +27,7 @@ export function registerCrawlerEntryPointRoutes({ }, }, enterpriseSearchRequestHandler.createRequest({ - path: '/api/as/v0/engines/:engineName/crawler/domains/:domainId/entry_points', + path: '/api/as/v1/engines/:engineName/crawler/domains/:domainId/entry_points', params: { respond_with: 'index', }, @@ -49,7 +49,7 @@ export function registerCrawlerEntryPointRoutes({ }, }, enterpriseSearchRequestHandler.createRequest({ - path: '/api/as/v0/engines/:engineName/crawler/domains/:domainId/entry_points/:entryPointId', + path: '/api/as/v1/engines/:engineName/crawler/domains/:domainId/entry_points/:entryPointId', params: { respond_with: 'index', }, @@ -68,7 +68,7 @@ export function registerCrawlerEntryPointRoutes({ }, }, enterpriseSearchRequestHandler.createRequest({ - path: '/api/as/v0/engines/:engineName/crawler/domains/:domainId/entry_points/:entryPointId', + path: '/api/as/v1/engines/:engineName/crawler/domains/:domainId/entry_points/:entryPointId', params: { respond_with: 'index', }, diff --git a/x-pack/plugins/enterprise_search/server/routes/app_search/crawler_sitemaps.test.ts b/x-pack/plugins/enterprise_search/server/routes/app_search/crawler_sitemaps.test.ts index a37a8311093c7..3d6eb86bcba26 100644 --- a/x-pack/plugins/enterprise_search/server/routes/app_search/crawler_sitemaps.test.ts +++ b/x-pack/plugins/enterprise_search/server/routes/app_search/crawler_sitemaps.test.ts @@ -28,7 +28,7 @@ describe('crawler sitemap routes', () => { it('creates a request to enterprise search', () => { expect(mockRequestHandler.createRequest).toHaveBeenCalledWith({ - path: '/api/as/v0/engines/:engineName/crawler/domains/:domainId/sitemaps', + path: '/api/as/v1/engines/:engineName/crawler/domains/:domainId/sitemaps', params: { respond_with: 'index', }, @@ -69,7 +69,7 @@ describe('crawler sitemap routes', () => { it('creates a request to enterprise search', () => { expect(mockRequestHandler.createRequest).toHaveBeenCalledWith({ - path: '/api/as/v0/engines/:engineName/crawler/domains/:domainId/sitemaps/:sitemapId', + path: '/api/as/v1/engines/:engineName/crawler/domains/:domainId/sitemaps/:sitemapId', params: { respond_with: 'index', }, @@ -110,7 +110,7 @@ describe('crawler sitemap routes', () => { it('creates a request to enterprise search', () => { expect(mockRequestHandler.createRequest).toHaveBeenCalledWith({ - path: '/api/as/v0/engines/:engineName/crawler/domains/:domainId/sitemaps/:sitemapId', + path: '/api/as/v1/engines/:engineName/crawler/domains/:domainId/sitemaps/:sitemapId', params: { respond_with: 'index', }, diff --git a/x-pack/plugins/enterprise_search/server/routes/app_search/crawler_sitemaps.ts b/x-pack/plugins/enterprise_search/server/routes/app_search/crawler_sitemaps.ts index b63473888eecc..0965acd967306 100644 --- a/x-pack/plugins/enterprise_search/server/routes/app_search/crawler_sitemaps.ts +++ b/x-pack/plugins/enterprise_search/server/routes/app_search/crawler_sitemaps.ts @@ -27,7 +27,7 @@ export function registerCrawlerSitemapRoutes({ }, }, enterpriseSearchRequestHandler.createRequest({ - path: '/api/as/v0/engines/:engineName/crawler/domains/:domainId/sitemaps', + path: '/api/as/v1/engines/:engineName/crawler/domains/:domainId/sitemaps', params: { respond_with: 'index', }, @@ -49,7 +49,7 @@ export function registerCrawlerSitemapRoutes({ }, }, enterpriseSearchRequestHandler.createRequest({ - path: '/api/as/v0/engines/:engineName/crawler/domains/:domainId/sitemaps/:sitemapId', + path: '/api/as/v1/engines/:engineName/crawler/domains/:domainId/sitemaps/:sitemapId', params: { respond_with: 'index', }, @@ -68,7 +68,7 @@ export function registerCrawlerSitemapRoutes({ }, }, enterpriseSearchRequestHandler.createRequest({ - path: '/api/as/v0/engines/:engineName/crawler/domains/:domainId/sitemaps/:sitemapId', + path: '/api/as/v1/engines/:engineName/crawler/domains/:domainId/sitemaps/:sitemapId', params: { respond_with: 'index', }, diff --git a/x-pack/plugins/fleet/common/constants/preconfiguration.ts b/x-pack/plugins/fleet/common/constants/preconfiguration.ts index 3e7377477c93e..7d22716bc0f1e 100644 --- a/x-pack/plugins/fleet/common/constants/preconfiguration.ts +++ b/x-pack/plugins/fleet/common/constants/preconfiguration.ts @@ -6,6 +6,7 @@ */ import { uniqBy } from 'lodash'; +import uuidv5 from 'uuid/v5'; import type { PreconfiguredAgentPolicy } from '../types'; @@ -18,6 +19,9 @@ import { autoUpgradePoliciesPackages, } from './epm'; +// UUID v5 values require a namespace. We use UUID v5 for some of our preconfigured ID values. +export const UUID_V5_NAMESPACE = 'dde7c2de-1370-4c19-9975-b473d0e03508'; + export const PRECONFIGURATION_DELETION_RECORD_SAVED_OBJECT_TYPE = 'fleet-preconfiguration-deletion-record'; @@ -25,17 +29,22 @@ export const PRECONFIGURATION_LATEST_KEYWORD = 'latest'; type PreconfiguredAgentPolicyWithDefaultInputs = Omit< PreconfiguredAgentPolicy, - 'package_policies' | 'id' + 'package_policies' > & { package_policies: Array>; }; +export const DEFAULT_AGENT_POLICY_ID_SEED = 'default-agent-policy'; +export const DEFAULT_SYSTEM_PACKAGE_POLICY_ID = 'default-system-policy'; + export const DEFAULT_AGENT_POLICY: PreconfiguredAgentPolicyWithDefaultInputs = { + id: uuidv5(DEFAULT_AGENT_POLICY_ID_SEED, UUID_V5_NAMESPACE), name: 'Default policy', namespace: 'default', description: 'Default agent policy created by Kibana', package_policies: [ { + id: DEFAULT_SYSTEM_PACKAGE_POLICY_ID, name: `${FLEET_SYSTEM_PACKAGE}-1`, package: { name: FLEET_SYSTEM_PACKAGE, @@ -47,12 +56,17 @@ export const DEFAULT_AGENT_POLICY: PreconfiguredAgentPolicyWithDefaultInputs = { monitoring_enabled: monitoringTypes, }; +export const DEFAULT_FLEET_SERVER_POLICY_ID = 'default-fleet-server-agent-policy'; +export const DEFAULT_FLEET_SERVER_AGENT_POLICY_ID_SEED = 'default-fleet-server'; + export const DEFAULT_FLEET_SERVER_AGENT_POLICY: PreconfiguredAgentPolicyWithDefaultInputs = { + id: uuidv5(DEFAULT_FLEET_SERVER_AGENT_POLICY_ID_SEED, UUID_V5_NAMESPACE), name: 'Default Fleet Server policy', namespace: 'default', description: 'Default Fleet Server agent policy created by Kibana', package_policies: [ { + id: DEFAULT_FLEET_SERVER_POLICY_ID, name: `${FLEET_SERVER_PACKAGE}-1`, package: { name: FLEET_SERVER_PACKAGE, diff --git a/x-pack/plugins/fleet/common/constants/routes.ts b/x-pack/plugins/fleet/common/constants/routes.ts index 9fc20bbf38eb7..69363f37d33e0 100644 --- a/x-pack/plugins/fleet/common/constants/routes.ts +++ b/x-pack/plugins/fleet/common/constants/routes.ts @@ -18,8 +18,8 @@ export const LIMITED_CONCURRENCY_ROUTE_TAG = 'ingest:limited-concurrency'; // EPM API routes const EPM_PACKAGES_MANY = `${EPM_API_ROOT}/packages`; const EPM_PACKAGES_BULK = `${EPM_PACKAGES_MANY}/_bulk`; -const EPM_PACKAGES_ONE = `${EPM_PACKAGES_MANY}/{pkgkey}`; -const EPM_PACKAGES_FILE = `${EPM_PACKAGES_MANY}/{pkgName}/{pkgVersion}`; +const EPM_PACKAGES_ONE_DEPRECATED = `${EPM_PACKAGES_MANY}/{pkgkey}`; +const EPM_PACKAGES_ONE = `${EPM_PACKAGES_MANY}/{pkgName}/{pkgVersion}`; export const EPM_API_ROUTES = { BULK_INSTALL_PATTERN: EPM_PACKAGES_BULK, LIST_PATTERN: EPM_PACKAGES_MANY, @@ -28,9 +28,13 @@ export const EPM_API_ROUTES = { INSTALL_FROM_REGISTRY_PATTERN: EPM_PACKAGES_ONE, INSTALL_BY_UPLOAD_PATTERN: EPM_PACKAGES_MANY, DELETE_PATTERN: EPM_PACKAGES_ONE, - FILEPATH_PATTERN: `${EPM_PACKAGES_FILE}/{filePath*}`, + FILEPATH_PATTERN: `${EPM_PACKAGES_ONE}/{filePath*}`, CATEGORIES_PATTERN: `${EPM_API_ROOT}/categories`, STATS_PATTERN: `${EPM_PACKAGES_MANY}/{pkgName}/stats`, + + INFO_PATTERN_DEPRECATED: EPM_PACKAGES_ONE_DEPRECATED, + INSTALL_FROM_REGISTRY_PATTERN_DEPRECATED: EPM_PACKAGES_ONE_DEPRECATED, + DELETE_PATTERN_DEPRECATED: EPM_PACKAGES_ONE_DEPRECATED, }; // Data stream API routes @@ -79,7 +83,9 @@ export const SETTINGS_API_ROUTES = { // App API routes export const APP_API_ROUTES = { CHECK_PERMISSIONS_PATTERN: `${API_ROOT}/check-permissions`, - GENERATE_SERVICE_TOKEN_PATTERN: `${API_ROOT}/service-tokens`, + GENERATE_SERVICE_TOKEN_PATTERN: `${API_ROOT}/service_tokens`, + // deprecated since 8.0 + GENERATE_SERVICE_TOKEN_PATTERN_DEPRECATED: `${API_ROOT}/service-tokens`, }; // Agent API routes @@ -95,16 +101,23 @@ export const AGENT_API_ROUTES = { BULK_UNENROLL_PATTERN: `${API_ROOT}/agents/bulk_unenroll`, REASSIGN_PATTERN: `${API_ROOT}/agents/{agentId}/reassign`, BULK_REASSIGN_PATTERN: `${API_ROOT}/agents/bulk_reassign`, - STATUS_PATTERN: `${API_ROOT}/agent-status`, + STATUS_PATTERN: `${API_ROOT}/agent_status`, + // deprecated since 8.0 + STATUS_PATTERN_DEPRECATED: `${API_ROOT}/agent-status`, UPGRADE_PATTERN: `${API_ROOT}/agents/{agentId}/upgrade`, BULK_UPGRADE_PATTERN: `${API_ROOT}/agents/bulk_upgrade`, }; export const ENROLLMENT_API_KEY_ROUTES = { - CREATE_PATTERN: `${API_ROOT}/enrollment-api-keys`, - LIST_PATTERN: `${API_ROOT}/enrollment-api-keys`, - INFO_PATTERN: `${API_ROOT}/enrollment-api-keys/{keyId}`, - DELETE_PATTERN: `${API_ROOT}/enrollment-api-keys/{keyId}`, + CREATE_PATTERN: `${API_ROOT}/enrollment_api_keys`, + LIST_PATTERN: `${API_ROOT}/enrollment_api_keys`, + INFO_PATTERN: `${API_ROOT}/enrollment_api_keys/{keyId}`, + DELETE_PATTERN: `${API_ROOT}/enrollment_api_keys/{keyId}`, + // deprecated since 8.0 + CREATE_PATTERN_DEPRECATED: `${API_ROOT}/enrollment-api-keys`, + LIST_PATTERN_DEPRECATED: `${API_ROOT}/enrollment-api-keys`, + INFO_PATTERN_DEPRECATED: `${API_ROOT}/enrollment-api-keys/{keyId}`, + DELETE_PATTERN_DEPRECATED: `${API_ROOT}/enrollment-api-keys/{keyId}`, }; // Agents setup API routes diff --git a/x-pack/plugins/fleet/common/constants/settings.ts b/x-pack/plugins/fleet/common/constants/settings.ts index 772d938086938..423e71edf10e6 100644 --- a/x-pack/plugins/fleet/common/constants/settings.ts +++ b/x-pack/plugins/fleet/common/constants/settings.ts @@ -6,3 +6,5 @@ */ export const GLOBAL_SETTINGS_SAVED_OBJECT_TYPE = 'ingest_manager_settings'; + +export const GLOBAL_SETTINGS_ID = 'fleet-default-settings'; diff --git a/x-pack/plugins/fleet/common/openapi/bundled.json b/x-pack/plugins/fleet/common/openapi/bundled.json index f30369b5792b8..7423a4dc54bbe 100644 --- a/x-pack/plugins/fleet/common/openapi/bundled.json +++ b/x-pack/plugins/fleet/common/openapi/bundled.json @@ -157,6 +157,7 @@ "parameters": [] }, "/epm/packages/{pkgkey}": { + "deprecated": true, "get": { "summary": "Packages - Info", "tags": [], @@ -352,6 +353,210 @@ } } }, + "/epm/packages/{pkgName}/{pkgVersion}": { + "get": { + "summary": "Packages - Info", + "tags": [], + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "type": "object", + "allOf": [ + { + "properties": { + "item": { + "$ref": "#/components/schemas/package_info" + } + } + }, + { + "properties": { + "status": { + "type": "string", + "enum": [ + "installed", + "installing", + "install_failed", + "not_installed" + ] + }, + "savedObject": { + "type": "string" + } + }, + "required": [ + "status", + "savedObject" + ] + } + ] + } + } + } + } + }, + "operationId": "get-package", + "security": [ + { + "basicAuth": [] + } + ] + }, + "parameters": [ + { + "schema": { + "type": "string" + }, + "name": "pkgName", + "in": "path", + "required": true + }, + { + "schema": { + "type": "string" + }, + "name": "pkgVersion", + "in": "path", + "required": true + } + ], + "post": { + "summary": "Packages - Install", + "tags": [], + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "items": { + "type": "array", + "items": { + "type": "object", + "properties": { + "id": { + "type": "string" + }, + "type": { + "oneOf": [ + { + "$ref": "#/components/schemas/kibana_saved_object_type" + }, + { + "$ref": "#/components/schemas/elasticsearch_asset_type" + } + ] + } + }, + "required": [ + "id", + "type" + ] + } + } + }, + "required": [ + "items" + ] + } + } + } + } + }, + "operationId": "install-package", + "description": "", + "parameters": [ + { + "$ref": "#/components/parameters/kbn_xsrf" + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "force": { + "type": "boolean" + } + } + } + } + } + } + }, + "delete": { + "summary": "Packages - Delete", + "tags": [], + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "items": { + "type": "array", + "items": { + "type": "object", + "properties": { + "id": { + "type": "string" + }, + "type": { + "oneOf": [ + { + "$ref": "#/components/schemas/kibana_saved_object_type" + }, + { + "$ref": "#/components/schemas/elasticsearch_asset_type" + } + ] + } + }, + "required": [ + "id", + "type" + ] + } + } + }, + "required": [ + "items" + ] + } + } + } + } + }, + "operationId": "delete-package", + "parameters": [ + { + "$ref": "#/components/parameters/kbn_xsrf" + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "force": { + "type": "boolean" + } + } + } + } + } + } + } + }, "/agents/setup": { "get": { "summary": "Agents setup - Info", @@ -419,6 +624,72 @@ } }, "/agent-status": { + "deprecated": true, + "get": { + "summary": "Agents - Summary stats", + "tags": [], + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "error": { + "type": "integer" + }, + "events": { + "type": "integer" + }, + "inactive": { + "type": "integer" + }, + "offline": { + "type": "integer" + }, + "online": { + "type": "integer" + }, + "other": { + "type": "integer" + }, + "total": { + "type": "integer" + }, + "updating": { + "type": "integer" + } + }, + "required": [ + "error", + "events", + "inactive", + "offline", + "online", + "other", + "total", + "updating" + ] + } + } + } + } + }, + "operationId": "get-agent-status", + "parameters": [ + { + "schema": { + "type": "string" + }, + "name": "policyId", + "in": "query", + "required": false + } + ] + } + }, + "/agent_status": { "get": { "summary": "Agents - Summary stats", "tags": [], @@ -496,6 +767,13 @@ "type": "object", "properties": { "list": { + "type": "array", + "items": { + "$ref": "#/components/schemas/agent" + }, + "deprecated": true + }, + "items": { "type": "array", "items": { "$ref": "#/components/schemas/agent" @@ -512,7 +790,7 @@ } }, "required": [ - "list", + "items", "total", "page", "perPage" @@ -1294,6 +1572,7 @@ "parameters": [] }, "/enrollment-api-keys": { + "deprecated": true, "get": { "summary": "Enrollment API Keys - List", "tags": [], @@ -1306,6 +1585,13 @@ "type": "object", "properties": { "list": { + "type": "array", + "items": { + "$ref": "#/components/schemas/enrollment_api_key" + }, + "deprecated": true + }, + "items": { "type": "array", "items": { "$ref": "#/components/schemas/enrollment_api_key" @@ -1322,7 +1608,7 @@ } }, "required": [ - "list", + "items", "page", "perPage", "total" @@ -1370,6 +1656,160 @@ } }, "/enrollment-api-keys/{keyId}": { + "deprecated": true, + "parameters": [ + { + "schema": { + "type": "string" + }, + "name": "keyId", + "in": "path", + "required": true + } + ], + "get": { + "summary": "Enrollment API Key - Info", + "tags": [], + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "item": { + "$ref": "#/components/schemas/enrollment_api_key" + } + }, + "required": [ + "item" + ] + } + } + } + } + }, + "operationId": "get-enrollment-api-key" + }, + "delete": { + "summary": "Enrollment API Key - Delete", + "tags": [], + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "action": { + "type": "string", + "enum": [ + "deleted" + ] + } + }, + "required": [ + "action" + ] + } + } + } + } + }, + "operationId": "delete-enrollment-api-key", + "parameters": [ + { + "$ref": "#/components/parameters/kbn_xsrf" + } + ] + } + }, + "/enrollment_api_keys": { + "get": { + "summary": "Enrollment API Keys - List", + "tags": [], + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "list": { + "type": "array", + "items": { + "$ref": "#/components/schemas/enrollment_api_key" + }, + "deprecated": true + }, + "items": { + "type": "array", + "items": { + "$ref": "#/components/schemas/enrollment_api_key" + } + }, + "page": { + "type": "number" + }, + "perPage": { + "type": "number" + }, + "total": { + "type": "number" + } + }, + "required": [ + "items", + "page", + "perPage", + "total" + ] + } + } + } + } + }, + "operationId": "get-enrollment-api-keys", + "parameters": [] + }, + "post": { + "summary": "Enrollment API Key - Create", + "tags": [], + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "item": { + "$ref": "#/components/schemas/enrollment_api_key" + }, + "action": { + "type": "string", + "enum": [ + "created" + ] + } + } + } + } + } + } + }, + "operationId": "create-enrollment-api-keys", + "parameters": [ + { + "$ref": "#/components/parameters/kbn_xsrf" + } + ] + } + }, + "/enrollment_api_keys/{keyId}": { "parameters": [ { "schema": { @@ -2520,10 +2960,6 @@ "unenrollment_started_at": { "type": "string" }, - "shared_id": { - "type": "string", - "deprecated": true - }, "access_api_key_id": { "type": "string" }, diff --git a/x-pack/plugins/fleet/common/openapi/bundled.yaml b/x-pack/plugins/fleet/common/openapi/bundled.yaml index 44242423aa420..13ffa77279c21 100644 --- a/x-pack/plugins/fleet/common/openapi/bundled.yaml +++ b/x-pack/plugins/fleet/common/openapi/bundled.yaml @@ -100,6 +100,7 @@ paths: operationId: list-all-packages parameters: [] /epm/packages/{pkgkey}: + deprecated: true get: summary: Packages - Info tags: [] @@ -213,6 +214,125 @@ paths: properties: force: type: boolean + /epm/packages/{pkgName}/{pkgVersion}: + get: + summary: Packages - Info + tags: [] + responses: + '200': + description: OK + content: + application/json: + schema: + type: object + allOf: + - properties: + item: + $ref: '#/components/schemas/package_info' + - properties: + status: + type: string + enum: + - installed + - installing + - install_failed + - not_installed + savedObject: + type: string + required: + - status + - savedObject + operationId: get-package + security: + - basicAuth: [] + parameters: + - schema: + type: string + name: pkgName + in: path + required: true + - schema: + type: string + name: pkgVersion + in: path + required: true + post: + summary: Packages - Install + tags: [] + responses: + '200': + description: OK + content: + application/json: + schema: + type: object + properties: + items: + type: array + items: + type: object + properties: + id: + type: string + type: + oneOf: + - $ref: '#/components/schemas/kibana_saved_object_type' + - $ref: '#/components/schemas/elasticsearch_asset_type' + required: + - id + - type + required: + - items + operationId: install-package + description: '' + parameters: + - $ref: '#/components/parameters/kbn_xsrf' + requestBody: + content: + application/json: + schema: + type: object + properties: + force: + type: boolean + delete: + summary: Packages - Delete + tags: [] + responses: + '200': + description: OK + content: + application/json: + schema: + type: object + properties: + items: + type: array + items: + type: object + properties: + id: + type: string + type: + oneOf: + - $ref: '#/components/schemas/kibana_saved_object_type' + - $ref: '#/components/schemas/elasticsearch_asset_type' + required: + - id + - type + required: + - items + operationId: delete-package + parameters: + - $ref: '#/components/parameters/kbn_xsrf' + requestBody: + content: + application/json: + schema: + type: object + properties: + force: + type: boolean /agents/setup: get: summary: Agents setup - Info @@ -253,6 +373,51 @@ paths: parameters: - $ref: '#/components/parameters/kbn_xsrf' /agent-status: + deprecated: true + get: + summary: Agents - Summary stats + tags: [] + responses: + '200': + description: OK + content: + application/json: + schema: + type: object + properties: + error: + type: integer + events: + type: integer + inactive: + type: integer + offline: + type: integer + online: + type: integer + other: + type: integer + total: + type: integer + updating: + type: integer + required: + - error + - events + - inactive + - offline + - online + - other + - total + - updating + operationId: get-agent-status + parameters: + - schema: + type: string + name: policyId + in: query + required: false + /agent_status: get: summary: Agents - Summary stats tags: [] @@ -312,6 +477,11 @@ paths: type: array items: $ref: '#/components/schemas/agent' + deprecated: true + items: + type: array + items: + $ref: '#/components/schemas/agent' total: type: number page: @@ -319,7 +489,7 @@ paths: perPage: type: number required: - - list + - items - total - page - perPage @@ -784,6 +954,7 @@ paths: - $ref: '#/components/parameters/kbn_xsrf' parameters: [] /enrollment-api-keys: + deprecated: true get: summary: Enrollment API Keys - List tags: [] @@ -799,6 +970,11 @@ paths: type: array items: $ref: '#/components/schemas/enrollment_api_key' + deprecated: true + items: + type: array + items: + $ref: '#/components/schemas/enrollment_api_key' page: type: number perPage: @@ -806,7 +982,7 @@ paths: total: type: number required: - - list + - items - page - perPage - total @@ -833,6 +1009,104 @@ paths: parameters: - $ref: '#/components/parameters/kbn_xsrf' /enrollment-api-keys/{keyId}: + deprecated: true + parameters: + - schema: + type: string + name: keyId + in: path + required: true + get: + summary: Enrollment API Key - Info + tags: [] + responses: + '200': + description: OK + content: + application/json: + schema: + type: object + properties: + item: + $ref: '#/components/schemas/enrollment_api_key' + required: + - item + operationId: get-enrollment-api-key + delete: + summary: Enrollment API Key - Delete + tags: [] + responses: + '200': + description: OK + content: + application/json: + schema: + type: object + properties: + action: + type: string + enum: + - deleted + required: + - action + operationId: delete-enrollment-api-key + parameters: + - $ref: '#/components/parameters/kbn_xsrf' + /enrollment_api_keys: + get: + summary: Enrollment API Keys - List + tags: [] + responses: + '200': + description: OK + content: + application/json: + schema: + type: object + properties: + list: + type: array + items: + $ref: '#/components/schemas/enrollment_api_key' + deprecated: true + items: + type: array + items: + $ref: '#/components/schemas/enrollment_api_key' + page: + type: number + perPage: + type: number + total: + type: number + required: + - items + - page + - perPage + - total + operationId: get-enrollment-api-keys + parameters: [] + post: + summary: Enrollment API Key - Create + tags: [] + responses: + '200': + description: OK + content: + application/json: + schema: + type: object + properties: + item: + $ref: '#/components/schemas/enrollment_api_key' + action: + type: string + enum: + - created + operationId: create-enrollment-api-keys + parameters: + - $ref: '#/components/parameters/kbn_xsrf' + /enrollment_api_keys/{keyId}: parameters: - schema: type: string @@ -1582,9 +1856,6 @@ components: type: string unenrollment_started_at: type: string - shared_id: - type: string - deprecated: true access_api_key_id: type: string default_api_key_id: diff --git a/x-pack/plugins/fleet/common/openapi/components/schemas/agent.yaml b/x-pack/plugins/fleet/common/openapi/components/schemas/agent.yaml index c21651ca7f8be..72679dd1dab64 100644 --- a/x-pack/plugins/fleet/common/openapi/components/schemas/agent.yaml +++ b/x-pack/plugins/fleet/common/openapi/components/schemas/agent.yaml @@ -11,9 +11,6 @@ properties: type: string unenrollment_started_at: type: string - shared_id: - type: string - deprecated: true access_api_key_id: type: string default_api_key_id: diff --git a/x-pack/plugins/fleet/common/openapi/components/schemas/output.yaml b/x-pack/plugins/fleet/common/openapi/components/schemas/output.yaml index e695f0048e6ad..a91ec2cb14e94 100644 --- a/x-pack/plugins/fleet/common/openapi/components/schemas/output.yaml +++ b/x-pack/plugins/fleet/common/openapi/components/schemas/output.yaml @@ -18,6 +18,8 @@ properties: type: string ca_sha256: type: string + ca_trusted_fingerprint: + type: string api_key: type: string config: diff --git a/x-pack/plugins/fleet/common/openapi/entrypoint.yaml b/x-pack/plugins/fleet/common/openapi/entrypoint.yaml index 5495f2b3ccacf..8dbf54582299a 100644 --- a/x-pack/plugins/fleet/common/openapi/entrypoint.yaml +++ b/x-pack/plugins/fleet/common/openapi/entrypoint.yaml @@ -25,11 +25,17 @@ paths: $ref: paths/epm@packages.yaml '/epm/packages/{pkgkey}': $ref: 'paths/epm@packages@{pkgkey}.yaml' + deprecated: true + '/epm/packages/{pkgName}/{pkgVersion}': + $ref: 'paths/epm@packages@{pkg_name}@{pkg_version}.yaml' # Agent-related endpoints /agents/setup: $ref: paths/agents@setup.yaml /agent-status: $ref: paths/agent_status.yaml + deprecated: true + /agent_status: + $ref: paths/agent_status.yaml /agents: $ref: paths/agents.yaml /agents/bulk_upgrade: @@ -56,7 +62,13 @@ paths: $ref: paths/agent_policies@delete.yaml /enrollment-api-keys: $ref: paths/enrollment_api_keys.yaml + deprecated: true '/enrollment-api-keys/{keyId}': + $ref: 'paths/enrollment_api_keys@{key_id}.yaml' + deprecated: true + /enrollment_api_keys: + $ref: paths/enrollment_api_keys.yaml + '/enrollment_api_keys/{keyId}': $ref: 'paths/enrollment_api_keys@{key_id}.yaml' /package_policies: $ref: paths/package_policies.yaml diff --git a/x-pack/plugins/fleet/common/openapi/paths/agents.yaml b/x-pack/plugins/fleet/common/openapi/paths/agents.yaml index 4a217eda5c5ed..19ea27956dac3 100644 --- a/x-pack/plugins/fleet/common/openapi/paths/agents.yaml +++ b/x-pack/plugins/fleet/common/openapi/paths/agents.yaml @@ -13,6 +13,11 @@ get: type: array items: $ref: ../components/schemas/agent.yaml + deprecated: true + items: + type: array + items: + $ref: ../components/schemas/agent.yaml total: type: number page: @@ -20,7 +25,7 @@ get: perPage: type: number required: - - list + - items - total - page - perPage diff --git a/x-pack/plugins/fleet/common/openapi/paths/enrollment_api_keys.yaml b/x-pack/plugins/fleet/common/openapi/paths/enrollment_api_keys.yaml index 6cfbede4a7ead..9f6ac6de0ebd6 100644 --- a/x-pack/plugins/fleet/common/openapi/paths/enrollment_api_keys.yaml +++ b/x-pack/plugins/fleet/common/openapi/paths/enrollment_api_keys.yaml @@ -13,6 +13,11 @@ get: type: array items: $ref: ../components/schemas/enrollment_api_key.yaml + deprecated: true + items: + type: array + items: + $ref: ../components/schemas/enrollment_api_key.yaml page: type: number perPage: @@ -20,7 +25,7 @@ get: total: type: number required: - - list + - items - page - perPage - total diff --git a/x-pack/plugins/fleet/common/openapi/paths/epm@packages@{pkg_name}@{pkg_version}.yaml b/x-pack/plugins/fleet/common/openapi/paths/epm@packages@{pkg_name}@{pkg_version}.yaml new file mode 100644 index 0000000000000..1c3c92d99ab38 --- /dev/null +++ b/x-pack/plugins/fleet/common/openapi/paths/epm@packages@{pkg_name}@{pkg_version}.yaml @@ -0,0 +1,118 @@ +get: + summary: Packages - Info + tags: [] + responses: + '200': + description: OK + content: + application/json: + schema: + type: object + allOf: + - properties: + item: + $ref: ../components/schemas/package_info.yaml + - properties: + status: + type: string + enum: + - installed + - installing + - install_failed + - not_installed + savedObject: + type: string + required: + - status + - savedObject + operationId: get-package + security: + - basicAuth: [] +parameters: + - schema: + type: string + name: pkgName + in: path + required: true + - schema: + type: string + name: pkgVersion + in: path + required: true +post: + summary: Packages - Install + tags: [] + responses: + '200': + description: OK + content: + application/json: + schema: + type: object + properties: + items: + type: array + items: + type: object + properties: + id: + type: string + type: + oneOf: + - $ref: ../components/schemas/kibana_saved_object_type.yaml + - $ref: ../components/schemas/elasticsearch_asset_type.yaml + required: + - id + - type + required: + - items + operationId: install-package + description: '' + parameters: + - $ref: ../components/headers/kbn_xsrf.yaml + requestBody: + content: + application/json: + schema: + type: object + properties: + force: + type: boolean +delete: + summary: Packages - Delete + tags: [] + responses: + '200': + description: OK + content: + application/json: + schema: + type: object + properties: + items: + type: array + items: + type: object + properties: + id: + type: string + type: + oneOf: + - $ref: ../components/schemas/kibana_saved_object_type.yaml + - $ref: ../components/schemas/elasticsearch_asset_type.yaml + required: + - id + - type + required: + - items + operationId: delete-package + parameters: + - $ref: ../components/headers/kbn_xsrf.yaml + requestBody: + content: + application/json: + schema: + type: object + properties: + force: + type: boolean diff --git a/x-pack/plugins/fleet/common/openapi/paths/outputs@{output_id}.yaml b/x-pack/plugins/fleet/common/openapi/paths/outputs@{output_id}.yaml index 326a65692a03b..d70c78dd7de56 100644 --- a/x-pack/plugins/fleet/common/openapi/paths/outputs@{output_id}.yaml +++ b/x-pack/plugins/fleet/common/openapi/paths/outputs@{output_id}.yaml @@ -61,6 +61,8 @@ put: type: string ca_sha256: type: string + ca_trusted_fingerprint: + type: string config_yaml: type: string required: diff --git a/x-pack/plugins/fleet/common/services/index.ts b/x-pack/plugins/fleet/common/services/index.ts index ba3fb44753643..7698308270fff 100644 --- a/x-pack/plugins/fleet/common/services/index.ts +++ b/x-pack/plugins/fleet/common/services/index.ts @@ -34,3 +34,4 @@ export { } from './validate_package_policy'; export { normalizeHostsForAgents } from './hosts_utils'; +export { splitPkgKey } from './split_pkg_key'; diff --git a/x-pack/plugins/fleet/common/services/routes.ts b/x-pack/plugins/fleet/common/services/routes.ts index 8ab02c462cfa4..d7954aff70dd2 100644 --- a/x-pack/plugins/fleet/common/services/routes.ts +++ b/x-pack/plugins/fleet/common/services/routes.ts @@ -33,8 +33,11 @@ export const epmRouteService = { return EPM_API_ROUTES.LIMITED_LIST_PATTERN; }, - getInfoPath: (pkgkey: string) => { - return EPM_API_ROUTES.INFO_PATTERN.replace('{pkgkey}', pkgkey); + getInfoPath: (pkgName: string, pkgVersion: string) => { + return EPM_API_ROUTES.INFO_PATTERN.replace('{pkgName}', pkgName).replace( + '{pkgVersion}', + pkgVersion + ); }, getStatsPath: (pkgName: string) => { @@ -45,23 +48,27 @@ export const epmRouteService = { return `${EPM_API_ROOT}${filePath.replace('/package', '/packages')}`; }, - getInstallPath: (pkgkey: string) => { - return EPM_API_ROUTES.INSTALL_FROM_REGISTRY_PATTERN.replace('{pkgkey}', pkgkey).replace( - /\/$/, - '' - ); // trim trailing slash + getInstallPath: (pkgName: string, pkgVersion: string) => { + return EPM_API_ROUTES.INSTALL_FROM_REGISTRY_PATTERN.replace('{pkgName}', pkgName) + .replace('{pkgVersion}', pkgVersion) + .replace(/\/$/, ''); // trim trailing slash }, getBulkInstallPath: () => { return EPM_API_ROUTES.BULK_INSTALL_PATTERN; }, - getRemovePath: (pkgkey: string) => { - return EPM_API_ROUTES.DELETE_PATTERN.replace('{pkgkey}', pkgkey).replace(/\/$/, ''); // trim trailing slash + getRemovePath: (pkgName: string, pkgVersion: string) => { + return EPM_API_ROUTES.DELETE_PATTERN.replace('{pkgName}', pkgName) + .replace('{pkgVersion}', pkgVersion) + .replace(/\/$/, ''); // trim trailing slash }, - getUpdatePath: (pkgkey: string) => { - return EPM_API_ROUTES.INFO_PATTERN.replace('{pkgkey}', pkgkey); + getUpdatePath: (pkgName: string, pkgVersion: string) => { + return EPM_API_ROUTES.INFO_PATTERN.replace('{pkgName}', pkgName).replace( + '{pkgVersion}', + pkgVersion + ); }, }; diff --git a/x-pack/plugins/fleet/common/services/split_pkg_key.ts b/x-pack/plugins/fleet/common/services/split_pkg_key.ts new file mode 100644 index 0000000000000..8bbc5b37a2e41 --- /dev/null +++ b/x-pack/plugins/fleet/common/services/split_pkg_key.ts @@ -0,0 +1,34 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import semverValid from 'semver/functions/valid'; + +/** + * Extract the package name and package version from a string. + * + * @param pkgkey a string containing the package name delimited by the package version + */ +export function splitPkgKey(pkgkey: string): { pkgName: string; pkgVersion: string } { + // If no version is provided, use the provided package key as the + // package name and return an empty version value + if (!pkgkey.includes('-')) { + return { pkgName: pkgkey, pkgVersion: '' }; + } + + const pkgName = pkgkey.includes('-') ? pkgkey.substr(0, pkgkey.indexOf('-')) : pkgkey; + + if (pkgName === '') { + throw new Error('Package key parsing failed: package name was empty'); + } + + // this will return the entire string if `indexOf` return -1 + const pkgVersion = pkgkey.substr(pkgkey.indexOf('-') + 1); + if (!semverValid(pkgVersion)) { + throw new Error('Package key parsing failed: package version was not a valid semver'); + } + return { pkgName, pkgVersion }; +} diff --git a/x-pack/plugins/fleet/common/types/models/output.ts b/x-pack/plugins/fleet/common/types/models/output.ts index fada8171b91fc..2ff50c0fc7bdb 100644 --- a/x-pack/plugins/fleet/common/types/models/output.ts +++ b/x-pack/plugins/fleet/common/types/models/output.ts @@ -17,6 +17,7 @@ export interface NewOutput { type: ValueOf; hosts?: string[]; ca_sha256?: string; + ca_trusted_fingerprint?: string; api_key?: string; config_yaml?: string; is_preconfigured?: boolean; diff --git a/x-pack/plugins/fleet/common/types/models/package_policy.ts b/x-pack/plugins/fleet/common/types/models/package_policy.ts index df484646ef66b..75932fd4a790a 100644 --- a/x-pack/plugins/fleet/common/types/models/package_policy.ts +++ b/x-pack/plugins/fleet/common/types/models/package_policy.ts @@ -56,6 +56,7 @@ export interface PackagePolicyInput extends Omit> & { + id?: string | number; name: string; package: Partial & { name: string }; inputs?: InputsOverride[]; diff --git a/x-pack/plugins/fleet/common/types/rest_spec/agent.ts b/x-pack/plugins/fleet/common/types/rest_spec/agent.ts index e6da9d4498ce2..5e091b9c543f2 100644 --- a/x-pack/plugins/fleet/common/types/rest_spec/agent.ts +++ b/x-pack/plugins/fleet/common/types/rest_spec/agent.ts @@ -7,22 +7,19 @@ import type { Agent, AgentAction, NewAgentAction } from '../models'; +import type { ListResult, ListWithKuery } from './common'; + export interface GetAgentsRequest { - query: { - page: number; - perPage: number; - kuery?: string; + query: ListWithKuery & { showInactive: boolean; showUpgradeable?: boolean; }; } -export interface GetAgentsResponse { - list: Agent[]; - total: number; +export interface GetAgentsResponse extends ListResult { totalInactive: number; - page: number; - perPage: number; + // deprecated in 8.x + list?: Agent[]; } export interface GetOneAgentRequest { diff --git a/x-pack/plugins/fleet/common/types/rest_spec/agent_policy.ts b/x-pack/plugins/fleet/common/types/rest_spec/agent_policy.ts index 0975b1e28fb8b..cbf3c9806d388 100644 --- a/x-pack/plugins/fleet/common/types/rest_spec/agent_policy.ts +++ b/x-pack/plugins/fleet/common/types/rest_spec/agent_policy.ts @@ -7,7 +7,7 @@ import type { AgentPolicy, NewAgentPolicy, FullAgentPolicy } from '../models'; -import type { ListWithKuery } from './common'; +import type { ListResult, ListWithKuery } from './common'; export interface GetAgentPoliciesRequest { query: ListWithKuery & { @@ -17,12 +17,7 @@ export interface GetAgentPoliciesRequest { export type GetAgentPoliciesResponseItem = AgentPolicy & { agents?: number }; -export interface GetAgentPoliciesResponse { - items: GetAgentPoliciesResponseItem[]; - total: number; - page: number; - perPage: number; -} +export type GetAgentPoliciesResponse = ListResult; export interface GetOneAgentPolicyRequest { params: { diff --git a/x-pack/plugins/fleet/common/types/rest_spec/enrollment_api_key.ts b/x-pack/plugins/fleet/common/types/rest_spec/enrollment_api_key.ts index da870deb31d9c..7fa724e5079c8 100644 --- a/x-pack/plugins/fleet/common/types/rest_spec/enrollment_api_key.ts +++ b/x-pack/plugins/fleet/common/types/rest_spec/enrollment_api_key.ts @@ -7,20 +7,16 @@ import type { EnrollmentAPIKey } from '../models'; +import type { ListResult, ListWithKuery } from './common'; + export interface GetEnrollmentAPIKeysRequest { - query: { - page: number; - perPage: number; - kuery?: string; - }; + query: ListWithKuery; } -export interface GetEnrollmentAPIKeysResponse { - list: EnrollmentAPIKey[]; - total: number; - page: number; - perPage: number; -} +export type GetEnrollmentAPIKeysResponse = ListResult & { + // deprecated in 8.x + list?: EnrollmentAPIKey[]; +}; export interface GetOneEnrollmentAPIKeyRequest { params: { diff --git a/x-pack/plugins/fleet/common/types/rest_spec/epm.ts b/x-pack/plugins/fleet/common/types/rest_spec/epm.ts index cfe0b4abdcd3c..6a72792e780ef 100644 --- a/x-pack/plugins/fleet/common/types/rest_spec/epm.ts +++ b/x-pack/plugins/fleet/common/types/rest_spec/epm.ts @@ -22,7 +22,9 @@ export interface GetCategoriesRequest { } export interface GetCategoriesResponse { - response: CategorySummaryList; + items: CategorySummaryList; + // deprecated in 8.0 + response?: CategorySummaryList; } export interface GetPackagesRequest { @@ -33,33 +35,46 @@ export interface GetPackagesRequest { } export interface GetPackagesResponse { - response: PackageList; + items: PackageList; + // deprecated in 8.0 + response?: PackageList; } export interface GetLimitedPackagesResponse { - response: string[]; + items: string[]; + // deprecated in 8.0 + response?: string[]; } export interface GetFileRequest { params: { - pkgkey: string; + pkgName: string; + pkgVersion: string; filePath: string; }; } export interface GetInfoRequest { params: { - pkgkey: string; + // deprecated in 8.0 + pkgkey?: string; + pkgName: string; + pkgVersion: string; }; } export interface GetInfoResponse { - response: PackageInfo; + item: PackageInfo; + // deprecated in 8.0 + response?: PackageInfo; } export interface UpdatePackageRequest { params: { - pkgkey: string; + // deprecated in 8.0 + pkgkey?: string; + pkgName: string; + pkgVersion: string; }; body: { keepPoliciesUpToDate?: boolean; @@ -67,7 +82,9 @@ export interface UpdatePackageRequest { } export interface UpdatePackageResponse { - response: PackageInfo; + item: PackageInfo; + // deprecated in 8.0 + response?: PackageInfo; } export interface GetStatsRequest { @@ -82,12 +99,17 @@ export interface GetStatsResponse { export interface InstallPackageRequest { params: { - pkgkey: string; + // deprecated in 8.0 + pkgkey?: string; + pkgName: string; + pkgVersion: string; }; } export interface InstallPackageResponse { - response: AssetReference[]; + items: AssetReference[]; + // deprecated in 8.0 + response?: AssetReference[]; } export interface IBulkInstallPackageHTTPError { @@ -110,7 +132,9 @@ export interface BulkInstallPackageInfo { } export interface BulkInstallPackagesResponse { - response: Array; + items: Array; + // deprecated in 8.0 + response?: Array; } export interface BulkInstallPackagesRequest { @@ -125,10 +149,15 @@ export interface MessageResponse { export interface DeletePackageRequest { params: { - pkgkey: string; + // deprecated in 8.0 + pkgkey?: string; + pkgName: string; + pkgVersion: string; }; } export interface DeletePackageResponse { - response: AssetReference[]; + // deprecated in 8.0 + response?: AssetReference[]; + items: AssetReference[]; } diff --git a/x-pack/plugins/fleet/common/types/rest_spec/output.ts b/x-pack/plugins/fleet/common/types/rest_spec/output.ts index 4e380feeb83a8..9a5001a3af10b 100644 --- a/x-pack/plugins/fleet/common/types/rest_spec/output.ts +++ b/x-pack/plugins/fleet/common/types/rest_spec/output.ts @@ -7,6 +7,8 @@ import type { Output } from '../models'; +import type { ListResult } from './common'; + export interface GetOneOutputResponse { item: Output; } @@ -30,6 +32,7 @@ export interface PutOutputRequest { name?: string; hosts?: string[]; ca_sha256?: string; + ca_trusted_fingerprint?: string; config_yaml?: string; is_default?: boolean; is_default_monitoring?: boolean; @@ -43,6 +46,7 @@ export interface PostOutputRequest { name: string; hosts?: string[]; ca_sha256?: string; + ca_trusted_fingerprint?: string; is_default?: boolean; is_default_monitoring?: boolean; config_yaml?: string; @@ -53,9 +57,4 @@ export interface PutOutputResponse { item: Output; } -export interface GetOutputsResponse { - items: Output[]; - total: number; - page: number; - perPage: number; -} +export type GetOutputsResponse = ListResult; diff --git a/x-pack/plugins/fleet/common/types/rest_spec/package_policy.ts b/x-pack/plugins/fleet/common/types/rest_spec/package_policy.ts index b050a7c798a0b..9eb20383d57bd 100644 --- a/x-pack/plugins/fleet/common/types/rest_spec/package_policy.ts +++ b/x-pack/plugins/fleet/common/types/rest_spec/package_policy.ts @@ -13,20 +13,13 @@ import type { PackagePolicyPackage, } from '../models'; +import type { ListResult, ListWithKuery } from './common'; + export interface GetPackagePoliciesRequest { - query: { - page: number; - perPage: number; - kuery?: string; - }; + query: ListWithKuery; } -export interface GetPackagePoliciesResponse { - items: PackagePolicy[]; - total: number; - page: number; - perPage: number; -} +export type GetPackagePoliciesResponse = ListResult; export interface GetOnePackagePolicyRequest { params: { diff --git a/x-pack/plugins/fleet/cypress/fixtures/integrations/apache.json b/x-pack/plugins/fleet/cypress/fixtures/integrations/apache.json index 3b78048fdd83f..397bc6d653409 100644 --- a/x-pack/plugins/fleet/cypress/fixtures/integrations/apache.json +++ b/x-pack/plugins/fleet/cypress/fixtures/integrations/apache.json @@ -1,5 +1,5 @@ { - "response": { + "item": { "name": "apache", "title": "Apache", "version": "1.1.0", diff --git a/x-pack/plugins/fleet/cypress/integration/integrations.spec.ts b/x-pack/plugins/fleet/cypress/integration/integrations.spec.ts index 88769ece39f2f..8b1a5e97279e8 100644 --- a/x-pack/plugins/fleet/cypress/integration/integrations.spec.ts +++ b/x-pack/plugins/fleet/cypress/integration/integrations.spec.ts @@ -88,7 +88,7 @@ describe('Add Integration', () => { fixture: 'integrations/agent_policy.json', }); // TODO fixture includes 1 package policy, should be empty initially - cy.intercept('GET', '/api/fleet/epm/packages/apache-1.1.0', { + cy.intercept('GET', '/api/fleet/epm/packages/apache/1.1.0', { fixture: 'integrations/apache.json', }); addAndVerifyIntegration(); diff --git a/x-pack/plugins/fleet/cypress/tasks/integrations.ts b/x-pack/plugins/fleet/cypress/tasks/integrations.ts index f1c891fa1186c..e9e3f2613c3e8 100644 --- a/x-pack/plugins/fleet/cypress/tasks/integrations.ts +++ b/x-pack/plugins/fleet/cypress/tasks/integrations.ts @@ -50,7 +50,7 @@ export const deleteIntegrations = async (integration: string) => { export const installPackageWithVersion = (integration: string, version: string) => { cy.request({ - url: `/api/fleet/epm/packages/${integration}-${version}`, + url: `/api/fleet/epm/packages/${integration}/${version}`, headers: { 'kbn-xsrf': 'cypress' }, body: '{ "force": true }', method: 'POST', diff --git a/x-pack/plugins/fleet/dev_docs/api/epm.md b/x-pack/plugins/fleet/dev_docs/api/epm.md index 90b636a5a92a1..1588e228c438b 100644 --- a/x-pack/plugins/fleet/dev_docs/api/epm.md +++ b/x-pack/plugins/fleet/dev_docs/api/epm.md @@ -14,11 +14,11 @@ curl localhost:5601/api/fleet/epm/packages Install a package: ``` -curl -X POST localhost:5601/api/fleet/epm/packages/iptables-1.0.4 +curl -X POST localhost:5601/api/fleet/epm/packages/iptables/1.0.4 ``` Delete a package: ``` -curl -X DELETE localhost:5601/api/fleet/epm/packages/iptables-1.0.4 +curl -X DELETE localhost:5601/api/fleet/epm/packages/iptables/1.0.4 ``` diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/index.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/index.tsx index 4f1211a83ebba..c731936c775e5 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/index.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/index.tsx @@ -24,6 +24,7 @@ import { import type { EuiStepProps } from '@elastic/eui/src/components/steps/step'; import { safeLoad } from 'js-yaml'; +import { splitPkgKey } from '../../../../../../common'; import type { AgentPolicy, NewPackagePolicy, @@ -152,15 +153,16 @@ export const CreatePackagePolicyPage: React.FunctionComponent = () => { // Form state const [formState, setFormState] = useState('VALID'); + const { pkgName, pkgVersion } = splitPkgKey(params.pkgkey); // Fetch package info const { data: packageInfoData, error: packageInfoError, isLoading: isPackageInfoLoading, - } = useGetPackageInfoByKey(params.pkgkey); + } = useGetPackageInfoByKey(pkgName, pkgVersion); const packageInfo = useMemo(() => { - if (packageInfoData && packageInfoData.response) { - return packageInfoData.response; + if (packageInfoData && packageInfoData.item) { + return packageInfoData.item; } }, [packageInfoData]); diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/edit_package_policy_page/index.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/edit_package_policy_page/index.tsx index c0914e41872b1..8d7ac07867605 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/edit_package_policy_page/index.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/edit_package_policy_page/index.tsx @@ -213,15 +213,16 @@ export const EditPackagePolicyForm = memo<{ } const { data: packageData } = await sendGetPackageInfoByKey( - pkgKeyFromPackageInfo(_packageInfo!) + _packageInfo!.name, + _packageInfo!.version ); - if (packageData?.response) { - setPackageInfo(packageData.response); + if (packageData?.item) { + setPackageInfo(packageData.item); const newValidationResults = validatePackagePolicy( newPackagePolicy, - packageData.response, + packageData.item, safeLoad ); setValidationResults(newValidationResults); @@ -348,7 +349,8 @@ export const EditPackagePolicyForm = memo<{ const [formState, setFormState] = useState('INVALID'); const savePackagePolicy = async () => { setFormState('LOADING'); - const result = await sendUpdatePackagePolicy(packagePolicyId, packagePolicy); + const { elasticsearch, ...restPackagePolicy } = packagePolicy; // ignore 'elasticsearch' property since it fails route validation + const result = await sendUpdatePackagePolicy(packagePolicyId, restPackagePolicy); setFormState('SUBMITTED'); return result; }; diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_list_page/index.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_list_page/index.tsx index 0dbe947369ad3..c64d065c1e058 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_list_page/index.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_list_page/index.tsx @@ -241,7 +241,7 @@ export const AgentListPage: React.FunctionComponent<{}> = () => { .join(' or '); if (kueryBuilder) { - kueryBuilder = `(${kueryBuilder}) and ${kueryStatus}`; + kueryBuilder = `(${kueryBuilder}) and (${kueryStatus})`; } else { kueryBuilder = kueryStatus; } @@ -308,7 +308,7 @@ export const AgentListPage: React.FunctionComponent<{}> = () => { inactive: agentsRequest.data.totalInactive, }); - setAgents(agentsRequest.data.list); + setAgents(agentsRequest.data.items); setTotalAgents(agentsRequest.data.total); setTotalInactiveAgents(agentsRequest.data.totalInactive); } catch (error) { diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_requirements_page/components/fleet_server_on_prem_instructions.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_requirements_page/components/fleet_server_on_prem_instructions.tsx index 4efff98fe39b2..b2eaf904ee1bb 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_requirements_page/components/fleet_server_on_prem_instructions.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_requirements_page/components/fleet_server_on_prem_instructions.tsx @@ -245,6 +245,7 @@ export const useFleetServerInstructions = (policyId?: string) => { const { data: settings, resendRequest: refreshSettings } = useGetSettings(); const fleetServerHost = settings?.item.fleet_server_hosts?.[0]; const esHost = output?.hosts?.[0]; + const sslCATrustedFingerprint: string | undefined = output?.ca_trusted_fingerprint; const installCommand = useMemo((): string => { if (!serviceToken || !esHost) { @@ -257,9 +258,18 @@ export const useFleetServerInstructions = (policyId?: string) => { serviceToken, policyId, fleetServerHost, - deploymentMode === 'production' + deploymentMode === 'production', + sslCATrustedFingerprint ); - }, [serviceToken, esHost, platform, policyId, fleetServerHost, deploymentMode]); + }, [ + serviceToken, + esHost, + platform, + policyId, + fleetServerHost, + deploymentMode, + sslCATrustedFingerprint, + ]); const getServiceToken = useCallback(async () => { setIsLoadingServiceToken(true); diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_requirements_page/components/install_command_utils.test.ts b/x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_requirements_page/components/install_command_utils.test.ts index 62580a1445f06..d05107e5058d4 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_requirements_page/components/install_command_utils.test.ts +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_requirements_page/components/install_command_utils.test.ts @@ -17,7 +17,7 @@ describe('getInstallCommandForPlatform', () => { ); expect(res).toMatchInlineSnapshot(` - "sudo ./elastic-agent install \\\\ + "sudo ./elastic-agent install \\\\ --fleet-server-es=http://elasticsearch:9200 \\\\ --fleet-server-service-token=service-token-1" `); @@ -31,7 +31,7 @@ describe('getInstallCommandForPlatform', () => { ); expect(res).toMatchInlineSnapshot(` - ".\\\\elastic-agent.exe install \` + ".\\\\elastic-agent.exe install \` --fleet-server-es=http://elasticsearch:9200 \` --fleet-server-service-token=service-token-1" `); @@ -45,11 +45,30 @@ describe('getInstallCommandForPlatform', () => { ); expect(res).toMatchInlineSnapshot(` - "sudo elastic-agent enroll \\\\ + "sudo elastic-agent enroll \\\\ --fleet-server-es=http://elasticsearch:9200 \\\\ --fleet-server-service-token=service-token-1" `); }); + + it('should return the correct command sslCATrustedFingerprint option is passed', () => { + const res = getInstallCommandForPlatform( + 'linux-mac', + 'http://elasticsearch:9200', + 'service-token-1', + undefined, + undefined, + false, + 'fingerprint123456' + ); + + expect(res).toMatchInlineSnapshot(` + "sudo ./elastic-agent install \\\\ + --fleet-server-es=http://elasticsearch:9200 \\\\ + --fleet-server-service-token=service-token-1 \\\\ + --fleet-server-es-ca-trusted-fingerprint=fingerprint123456" + `); + }); }); describe('with policy id', () => { @@ -62,7 +81,7 @@ describe('getInstallCommandForPlatform', () => { ); expect(res).toMatchInlineSnapshot(` - "sudo ./elastic-agent install \\\\ + "sudo ./elastic-agent install \\\\ --fleet-server-es=http://elasticsearch:9200 \\\\ --fleet-server-service-token=service-token-1 \\\\ --fleet-server-policy=policy-1" @@ -78,7 +97,7 @@ describe('getInstallCommandForPlatform', () => { ); expect(res).toMatchInlineSnapshot(` - ".\\\\elastic-agent.exe install \` + ".\\\\elastic-agent.exe install \` --fleet-server-es=http://elasticsearch:9200 \` --fleet-server-service-token=service-token-1 \` --fleet-server-policy=policy-1" @@ -94,7 +113,7 @@ describe('getInstallCommandForPlatform', () => { ); expect(res).toMatchInlineSnapshot(` - "sudo elastic-agent enroll \\\\ + "sudo elastic-agent enroll \\\\ --fleet-server-es=http://elasticsearch:9200 \\\\ --fleet-server-service-token=service-token-1 \\\\ --fleet-server-policy=policy-1" @@ -178,7 +197,7 @@ describe('getInstallCommandForPlatform', () => { ); expect(res).toMatchInlineSnapshot(` - "sudo elastic-agent enroll \\\\ + "sudo elastic-agent enroll \\\\ --fleet-server-es=http://elasticsearch:9200 \\\\ --fleet-server-service-token=service-token-1" `); diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_requirements_page/components/install_command_utils.ts b/x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_requirements_page/components/install_command_utils.ts index f5c40e8071691..64ae4903af53f 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_requirements_page/components/install_command_utils.ts +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_requirements_page/components/install_command_utils.ts @@ -13,37 +13,49 @@ export function getInstallCommandForPlatform( serviceToken: string, policyId?: string, fleetServerHost?: string, - isProductionDeployment?: boolean + isProductionDeployment?: boolean, + sslCATrustedFingerprint?: string ) { - let commandArguments = ''; - const newLineSeparator = platform === 'windows' ? '`' : '\\'; + const commandArguments = []; + const newLineSeparator = platform === 'windows' ? '`\n' : '\\\n'; if (isProductionDeployment && fleetServerHost) { - commandArguments += `--url=${fleetServerHost} ${newLineSeparator}\n`; - } else { - commandArguments += ` ${newLineSeparator}\n`; + commandArguments.push(['url', fleetServerHost]); } - commandArguments += ` --fleet-server-es=${esHost}`; - commandArguments += ` ${newLineSeparator}\n --fleet-server-service-token=${serviceToken}`; + commandArguments.push(['fleet-server-es', esHost]); + commandArguments.push(['fleet-server-service-token', serviceToken]); if (policyId) { - commandArguments += ` ${newLineSeparator}\n --fleet-server-policy=${policyId}`; + commandArguments.push(['fleet-server-policy', policyId]); + } + + if (sslCATrustedFingerprint) { + commandArguments.push(['fleet-server-es-ca-trusted-fingerprint', sslCATrustedFingerprint]); } if (isProductionDeployment) { - commandArguments += ` ${newLineSeparator}\n --certificate-authorities=`; - commandArguments += ` ${newLineSeparator}\n --fleet-server-es-ca=`; - commandArguments += ` ${newLineSeparator}\n --fleet-server-cert=`; - commandArguments += ` ${newLineSeparator}\n --fleet-server-cert-key=`; + commandArguments.push(['certificate-authorities', '']); + if (!sslCATrustedFingerprint) { + commandArguments.push(['fleet-server-es-ca', '']); + } + commandArguments.push(['fleet-server-cert', '']); + commandArguments.push(['fleet-server-cert-key', '']); } + const commandArgumentsStr = commandArguments.reduce((acc, [key, val]) => { + if (acc === '' && key === 'url') { + return `--${key}=${val}`; + } + return (acc += ` ${newLineSeparator} --${key}=${val}`); + }, ''); + switch (platform) { case 'linux-mac': - return `sudo ./elastic-agent install ${commandArguments}`; + return `sudo ./elastic-agent install ${commandArgumentsStr}`; case 'windows': - return `.\\elastic-agent.exe install ${commandArguments}`; + return `.\\elastic-agent.exe install ${commandArgumentsStr}`; case 'rpm-deb': - return `sudo elastic-agent enroll ${commandArguments}`; + return `sudo elastic-agent enroll ${commandArgumentsStr}`; default: return ''; } diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agents/components/fleet_server_upgrade_modal.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/agents/components/fleet_server_upgrade_modal.tsx index 2d963ea0ddf30..5902f73cae3bc 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/agents/components/fleet_server_upgrade_modal.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agents/components/fleet_server_upgrade_modal.tsx @@ -63,7 +63,7 @@ export const FleetServerUpgradeModal: React.FunctionComponent = ({ onClos throw res.error; } - for (const agent of res.data?.list ?? []) { + for (const agent of res.data?.items ?? []) { if (!agent.policy_id || agentPoliciesAlreadyChecked[agent.policy_id]) { continue; } diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agents/enrollment_token_list_page/index.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/agents/enrollment_token_list_page/index.tsx index b8b66b42b533d..72160fb4ae897 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/agents/enrollment_token_list_page/index.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agents/enrollment_token_list_page/index.tsx @@ -182,7 +182,7 @@ export const EnrollmentTokenListPage: React.FunctionComponent<{}> = () => { const total = enrollmentAPIKeysRequest?.data?.total ?? 0; const rowItems = - enrollmentAPIKeysRequest?.data?.list.filter((enrollmentKey) => { + enrollmentAPIKeysRequest?.data?.items.filter((enrollmentKey) => { if (!agentPolicies.length || !enrollmentKey.policy_id) return false; const agentPolicy = agentPoliciesById[enrollmentKey.policy_id]; return !agentPolicy?.is_managed; diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agents/hooks/use_fleet_server_unhealthy.test.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/agents/hooks/use_fleet_server_unhealthy.test.tsx index 87a269672ed9c..a36b4fb25793f 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/agents/hooks/use_fleet_server_unhealthy.test.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agents/hooks/use_fleet_server_unhealthy.test.tsx @@ -37,7 +37,7 @@ const mockApiCallsWithHealthyFleetServer = (http: MockedFleetStartServices['http }; } - if (path === '/api/fleet/agent-status') { + if (path === '/api/fleet/agent_status') { return { data: { results: { online: 1, updating: 0, offline: 0 }, @@ -65,7 +65,7 @@ const mockApiCallsWithoutHealthyFleetServer = (http: MockedFleetStartServices['h }; } - if (path === '/api/fleet/agent-status') { + if (path === '/api/fleet/agent_status') { return { data: { results: { online: 0, updating: 0, offline: 1 }, diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/edit_output_flyout/index.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/edit_output_flyout/index.tsx index 824eec081e28b..62b22d0bdffc6 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/edit_output_flyout/index.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/edit_output_flyout/index.tsx @@ -135,6 +135,27 @@ export const EditOutputFlyout: React.FunctionComponent = })} {...inputs.elasticsearchUrlInput.props} /> + + } + {...inputs.caTrustedFingerprintInput.formRowProps} + > + + ( void, output?: Output) { isPreconfigured ); + const caTrustedFingerprintInput = useInput( + output?.ca_trusted_fingerprint ?? '', + validateCATrustedFingerPrint, + isPreconfigured + ); + const defaultOutputInput = useSwitchInput( output?.is_default ?? false, isPreconfigured || output?.is_default @@ -127,6 +138,7 @@ export function useOutputForm(onSucess: () => void, output?: Output) { additionalYamlConfigInput, defaultOutputInput, defaultMonitoringOutputInput, + caTrustedFingerprintInput, }; const hasChanged = Object.values(inputs).some((input) => input.hasChanged); @@ -135,13 +147,19 @@ export function useOutputForm(onSucess: () => void, output?: Output) { const nameInputValid = nameInput.validate(); const elasticsearchUrlsValid = elasticsearchUrlInput.validate(); const additionalYamlConfigValid = additionalYamlConfigInput.validate(); - - if (!elasticsearchUrlsValid || !additionalYamlConfigValid || !nameInputValid) { + const caTrustedFingerprintValid = caTrustedFingerprintInput.validate(); + + if ( + !elasticsearchUrlsValid || + !additionalYamlConfigValid || + !nameInputValid || + !caTrustedFingerprintValid + ) { return false; } return true; - }, [nameInput, elasticsearchUrlInput, additionalYamlConfigInput]); + }, [nameInput, elasticsearchUrlInput, additionalYamlConfigInput, caTrustedFingerprintInput]); const submit = useCallback(async () => { try { @@ -157,6 +175,7 @@ export function useOutputForm(onSucess: () => void, output?: Output) { is_default: defaultOutputInput.value, is_default_monitoring: defaultMonitoringOutputInput.value, config_yaml: additionalYamlConfigInput.value, + ca_trusted_fingerprint: caTrustedFingerprintInput.value, }; if (output) { @@ -195,6 +214,7 @@ export function useOutputForm(onSucess: () => void, output?: Output) { defaultMonitoringOutputInput.value, defaultOutputInput.value, elasticsearchUrlInput.value, + caTrustedFingerprintInput.value, nameInput.value, notifications.toasts, onSucess, diff --git a/x-pack/plugins/fleet/public/applications/integrations/hooks/use_links.tsx b/x-pack/plugins/fleet/public/applications/integrations/hooks/use_links.tsx index 032554a4ec439..c39e4e0d097c5 100644 --- a/x-pack/plugins/fleet/public/applications/integrations/hooks/use_links.tsx +++ b/x-pack/plugins/fleet/public/applications/integrations/hooks/use_links.tsx @@ -39,8 +39,7 @@ export function useLinks() { version: string; }) => { const imagePath = removeRelativePath(path); - const pkgkey = `${packageName}-${version}`; - const filePath = `${epmRouteService.getInfoPath(pkgkey)}/${imagePath}`; + const filePath = `${epmRouteService.getInfoPath(packageName, version)}/${imagePath}`; return http.basePath.prepend(filePath); }, }; diff --git a/x-pack/plugins/fleet/public/applications/integrations/hooks/use_package_install.tsx b/x-pack/plugins/fleet/public/applications/integrations/hooks/use_package_install.tsx index ad6f492bc5fce..90a2231da40c6 100644 --- a/x-pack/plugins/fleet/public/applications/integrations/hooks/use_package_install.tsx +++ b/x-pack/plugins/fleet/public/applications/integrations/hooks/use_package_install.tsx @@ -61,9 +61,8 @@ function usePackageInstall({ notifications }: { notifications: NotificationsStar const currStatus = getPackageInstallStatus(name); const newStatus = { ...currStatus, name, status: InstallStatus.installing }; setPackageInstallStatus(newStatus); - const pkgkey = `${name}-${version}`; - const res = await sendInstallPackage(pkgkey); + const res = await sendInstallPackage(name, version); if (res.error) { if (fromUpdate) { // if there is an error during update, set it back to the previous version @@ -126,9 +125,8 @@ function usePackageInstall({ notifications }: { notifications: NotificationsStar redirectToVersion, }: Pick & { redirectToVersion: string }) => { setPackageInstallStatus({ name, status: InstallStatus.uninstalling, version }); - const pkgkey = `${name}-${version}`; - const res = await sendRemovePackage(pkgkey); + const res = await sendRemovePackage(name, version); if (res.error) { setPackageInstallStatus({ name, status: InstallStatus.installed, version }); notifications.toasts.addWarning({ diff --git a/x-pack/plugins/fleet/public/applications/integrations/sections/epm/screens/detail/index.test.tsx b/x-pack/plugins/fleet/public/applications/integrations/sections/epm/screens/detail/index.test.tsx index d442f8a13e27e..02874f12c6592 100644 --- a/x-pack/plugins/fleet/public/applications/integrations/sections/epm/screens/detail/index.test.tsx +++ b/x-pack/plugins/fleet/public/applications/integrations/sections/epm/screens/detail/index.test.tsx @@ -75,7 +75,7 @@ describe('when on integration detail', () => { describe('and the package is not installed', () => { beforeEach(() => { const unInstalledPackage = mockedApi.responseProvider.epmGetInfo(); - unInstalledPackage.response.status = 'not_installed'; + unInstalledPackage.item.status = 'not_installed'; mockedApi.responseProvider.epmGetInfo.mockReturnValue(unInstalledPackage); render(); }); @@ -283,7 +283,7 @@ const mockApiCalls = ( // @ts-ignore const epmPackageResponse: GetInfoResponse = { - response: { + item: { name: 'nginx', title: 'Nginx', version: '0.3.7', @@ -770,7 +770,7 @@ On Windows, the module was tested with Nginx installed from the Chocolatey repos http.get.mockImplementation(async (path: any) => { if (typeof path === 'string') { - if (path === epmRouteService.getInfoPath(`nginx-0.3.7`)) { + if (path === epmRouteService.getInfoPath(`nginx`, `0.3.7`)) { markApiCallAsHandled(); return mockedApiInterface.responseProvider.epmGetInfo(); } diff --git a/x-pack/plugins/fleet/public/applications/integrations/sections/epm/screens/detail/index.tsx b/x-pack/plugins/fleet/public/applications/integrations/sections/epm/screens/detail/index.tsx index 1a3a5c7eadd35..cdebc5f8b3ce1 100644 --- a/x-pack/plugins/fleet/public/applications/integrations/sections/epm/screens/detail/index.tsx +++ b/x-pack/plugins/fleet/public/applications/integrations/sections/epm/screens/detail/index.tsx @@ -27,6 +27,7 @@ import { i18n } from '@kbn/i18n'; import { FormattedMessage } from '@kbn/i18n-react'; import semverLt from 'semver/functions/lt'; +import { splitPkgKey } from '../../../../../../../common'; import { useGetPackageInstallStatus, useSetPackageInstallStatus, @@ -132,26 +133,27 @@ export function Detail() { packageInfo.savedObject && semverLt(packageInfo.savedObject.attributes.version, packageInfo.latestVersion); + const { pkgName, pkgVersion } = splitPkgKey(pkgkey); // Fetch package info const { data: packageInfoData, error: packageInfoError, isLoading: packageInfoLoading, - } = useGetPackageInfoByKey(pkgkey); + } = useGetPackageInfoByKey(pkgName, pkgVersion); const isLoading = packageInfoLoading || permissionCheck.isLoading; const showCustomTab = - useUIExtension(packageInfoData?.response.name ?? '', 'package-detail-custom') !== undefined; + useUIExtension(packageInfoData?.item.name ?? '', 'package-detail-custom') !== undefined; // Track install status state useEffect(() => { - if (packageInfoData?.response) { - const packageInfoResponse = packageInfoData.response; + if (packageInfoData?.item) { + const packageInfoResponse = packageInfoData.item; setPackageInfo(packageInfoResponse); let installedVersion; - const { name } = packageInfoData.response; + const { name } = packageInfoData.item; if ('savedObject' in packageInfoResponse) { installedVersion = packageInfoResponse.savedObject.attributes.version; } diff --git a/x-pack/plugins/fleet/public/applications/integrations/sections/epm/screens/detail/settings/settings.tsx b/x-pack/plugins/fleet/public/applications/integrations/sections/epm/screens/detail/settings/settings.tsx index 73c762d34a2cf..a28f63c3f9163 100644 --- a/x-pack/plugins/fleet/public/applications/integrations/sections/epm/screens/detail/settings/settings.tsx +++ b/x-pack/plugins/fleet/public/applications/integrations/sections/epm/screens/detail/settings/settings.tsx @@ -122,7 +122,7 @@ export const SettingsPage: React.FC = memo(({ packageInfo }: Props) => { try { setKeepPoliciesUpToDateSwitchValue((prev) => !prev); - await sendUpdatePackage(`${packageInfo.name}-${packageInfo.version}`, { + await sendUpdatePackage(packageInfo.name, packageInfo.version, { keepPoliciesUpToDate: !keepPoliciesUpToDateSwitchValue, }); diff --git a/x-pack/plugins/fleet/public/applications/integrations/sections/epm/screens/home/available_packages.tsx b/x-pack/plugins/fleet/public/applications/integrations/sections/epm/screens/home/available_packages.tsx index 81d1701c4a986..7547e06201171 100644 --- a/x-pack/plugins/fleet/public/applications/integrations/sections/epm/screens/home/available_packages.tsx +++ b/x-pack/plugins/fleet/public/applications/integrations/sections/epm/screens/home/available_packages.tsx @@ -215,7 +215,7 @@ export const AvailablePackages: React.FC = memo(() => { category: '', }); const eprIntegrationList = useMemo( - () => packageListToIntegrationsList(eprPackages?.response || []), + () => packageListToIntegrationsList(eprPackages?.items || []), [eprPackages] ); @@ -256,7 +256,7 @@ export const AvailablePackages: React.FC = memo(() => { ? [] : mergeCategoriesAndCount( eprCategories - ? (eprCategories.response as Array<{ id: string; title: string; count: number }>) + ? (eprCategories.items as Array<{ id: string; title: string; count: number }>) : [], cards ); diff --git a/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/advanced_agent_authentication_settings.tsx b/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/advanced_agent_authentication_settings.tsx index 3d069c1d0336b..52c4d09a58c56 100644 --- a/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/advanced_agent_authentication_settings.tsx +++ b/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/advanced_agent_authentication_settings.tsx @@ -103,7 +103,7 @@ export const AdvancedAgentAuthenticationSettings: FunctionComponent = ({ onKeyChange, }) => { const { notifications } = useStartServices(); - const [enrollmentAPIKeys, setEnrollmentAPIKeys] = useState( + const [enrollmentAPIKeys, setEnrollmentAPIKeys] = useState( [] ); @@ -143,7 +143,7 @@ export const AdvancedAgentAuthenticationSettings: FunctionComponent = ({ throw new Error('No data while fetching enrollment API keys'); } - const enrollmentAPIKeysResponse = res.data.list.filter( + const enrollmentAPIKeysResponse = res.data.items.filter( (key) => key.policy_id === agentPolicyId && key.active === true ); diff --git a/x-pack/plugins/fleet/public/hooks/use_package_icon_type.ts b/x-pack/plugins/fleet/public/hooks/use_package_icon_type.ts index 1b21b7bfd78eb..733aaef8b9267 100644 --- a/x-pack/plugins/fleet/public/hooks/use_package_icon_type.ts +++ b/x-pack/plugins/fleet/public/hooks/use_package_icon_type.ts @@ -66,11 +66,11 @@ export const usePackageIconType = ({ } if (tryApi && !paramIcons && !iconList) { - sendGetPackageInfoByKey(cacheKey) + sendGetPackageInfoByKey(packageName, version) .catch((error) => undefined) // Ignore API errors .then((res) => { CACHED_ICONS.delete(cacheKey); - setIconList(res?.data?.response?.icons); + setIconList(res?.data?.item?.icons); }); } diff --git a/x-pack/plugins/fleet/public/hooks/use_package_installations.tsx b/x-pack/plugins/fleet/public/hooks/use_package_installations.tsx index f4735e6f85546..4789770b7046f 100644 --- a/x-pack/plugins/fleet/public/hooks/use_package_installations.tsx +++ b/x-pack/plugins/fleet/public/hooks/use_package_installations.tsx @@ -36,9 +36,8 @@ export const usePackageInstallations = () => { }); const allInstalledPackages = useMemo( - () => - (allPackages?.response || []).filter((pkg) => pkg.status === installationStatuses.Installed), - [allPackages?.response] + () => (allPackages?.items || []).filter((pkg) => pkg.status === installationStatuses.Installed), + [allPackages?.items] ); const updatablePackages = useMemo( diff --git a/x-pack/plugins/fleet/public/hooks/use_request/epm.ts b/x-pack/plugins/fleet/public/hooks/use_request/epm.ts index a7078dd3a3f91..c5e82316e5eb3 100644 --- a/x-pack/plugins/fleet/public/hooks/use_request/epm.ts +++ b/x-pack/plugins/fleet/public/hooks/use_request/epm.ts @@ -67,9 +67,9 @@ export const useGetLimitedPackages = () => { }); }; -export const useGetPackageInfoByKey = (pkgkey: string) => { +export const useGetPackageInfoByKey = (pkgName: string, pkgVersion: string) => { return useRequest({ - path: epmRouteService.getInfoPath(pkgkey), + path: epmRouteService.getInfoPath(pkgName, pkgVersion), method: 'get', }); }; @@ -81,9 +81,9 @@ export const useGetPackageStats = (pkgName: string) => { }); }; -export const sendGetPackageInfoByKey = (pkgkey: string) => { +export const sendGetPackageInfoByKey = (pkgName: string, pkgVersion: string) => { return sendRequest({ - path: epmRouteService.getInfoPath(pkgkey), + path: epmRouteService.getInfoPath(pkgName, pkgVersion), method: 'get', }); }; @@ -102,23 +102,27 @@ export const sendGetFileByPath = (filePath: string) => { }); }; -export const sendInstallPackage = (pkgkey: string) => { +export const sendInstallPackage = (pkgName: string, pkgVersion: string) => { return sendRequest({ - path: epmRouteService.getInstallPath(pkgkey), + path: epmRouteService.getInstallPath(pkgName, pkgVersion), method: 'post', }); }; -export const sendRemovePackage = (pkgkey: string) => { +export const sendRemovePackage = (pkgName: string, pkgVersion: string) => { return sendRequest({ - path: epmRouteService.getRemovePath(pkgkey), + path: epmRouteService.getRemovePath(pkgName, pkgVersion), method: 'delete', }); }; -export const sendUpdatePackage = (pkgkey: string, body: UpdatePackageRequest['body']) => { +export const sendUpdatePackage = ( + pkgName: string, + pkgVersion: string, + body: UpdatePackageRequest['body'] +) => { return sendRequest({ - path: epmRouteService.getUpdatePath(pkgkey), + path: epmRouteService.getUpdatePath(pkgName, pkgVersion), method: 'put', body, }); diff --git a/x-pack/plugins/fleet/public/search_provider.test.ts b/x-pack/plugins/fleet/public/search_provider.test.ts index 97ed199c44502..43e6d93c8031c 100644 --- a/x-pack/plugins/fleet/public/search_provider.test.ts +++ b/x-pack/plugins/fleet/public/search_provider.test.ts @@ -24,7 +24,7 @@ import { sendGetPackages } from './hooks'; const mockSendGetPackages = sendGetPackages as jest.Mock; -const testResponse: GetPackagesResponse['response'] = [ +const testResponse: GetPackagesResponse['items'] = [ { description: 'test', download: 'test', diff --git a/x-pack/plugins/fleet/public/search_provider.ts b/x-pack/plugins/fleet/public/search_provider.ts index d919462f38c28..fe7cca92cf48d 100644 --- a/x-pack/plugins/fleet/public/search_provider.ts +++ b/x-pack/plugins/fleet/public/search_provider.ts @@ -105,7 +105,7 @@ export const createPackageSearchProvider = (core: CoreSetup): GlobalSearchResult const toSearchResults = ( coreStart: CoreStart, - packagesResponse: GetPackagesResponse['response'] + packagesResponse: GetPackagesResponse['items'] ): GlobalSearchProviderResult[] => { return packagesResponse .flatMap( diff --git a/x-pack/plugins/fleet/server/routes/agent/handlers.ts b/x-pack/plugins/fleet/server/routes/agent/handlers.ts index dd77c216413f3..578d4281cba3b 100644 --- a/x-pack/plugins/fleet/server/routes/agent/handlers.ts +++ b/x-pack/plugins/fleet/server/routes/agent/handlers.ts @@ -122,7 +122,8 @@ export const getAgentsHandler: RequestHandler< : 0; const body: GetAgentsResponse = { - list: agents, + list: agents, // deprecated + items: agents, total, totalInactive, page, diff --git a/x-pack/plugins/fleet/server/routes/agent/index.ts b/x-pack/plugins/fleet/server/routes/agent/index.ts index db5b01b319e00..7297252ff3230 100644 --- a/x-pack/plugins/fleet/server/routes/agent/index.ts +++ b/x-pack/plugins/fleet/server/routes/agent/index.ts @@ -116,6 +116,15 @@ export const registerAPIRoutes = (router: IRouter, config: FleetConfigType) => { }, getAgentStatusForAgentPolicyHandler ); + router.get( + { + path: AGENT_API_ROUTES.STATUS_PATTERN_DEPRECATED, + validate: GetAgentStatusRequestSchema, + options: { tags: [`access:${PLUGIN_ID}-read`] }, + }, + getAgentStatusForAgentPolicyHandler + ); + // upgrade agent router.post( { diff --git a/x-pack/plugins/fleet/server/routes/app/index.ts b/x-pack/plugins/fleet/server/routes/app/index.ts index aa2d61732eed5..cb2a01deecb4f 100644 --- a/x-pack/plugins/fleet/server/routes/app/index.ts +++ b/x-pack/plugins/fleet/server/routes/app/index.ts @@ -90,4 +90,13 @@ export const registerRoutes = (router: IRouter) => { }, generateServiceTokenHandler ); + + router.post( + { + path: APP_API_ROUTES.GENERATE_SERVICE_TOKEN_PATTERN_DEPRECATED, + validate: {}, + options: { tags: [`access:${PLUGIN_ID}-all`] }, + }, + generateServiceTokenHandler + ); }; diff --git a/x-pack/plugins/fleet/server/routes/enrollment_api_key/handler.ts b/x-pack/plugins/fleet/server/routes/enrollment_api_key/handler.ts index 0465614c49432..7fef583af50cd 100644 --- a/x-pack/plugins/fleet/server/routes/enrollment_api_key/handler.ts +++ b/x-pack/plugins/fleet/server/routes/enrollment_api_key/handler.ts @@ -36,7 +36,13 @@ export const getEnrollmentApiKeysHandler: RequestHandler< perPage: request.query.perPage, kuery: request.query.kuery, }); - const body: GetEnrollmentAPIKeysResponse = { list: items, total, page, perPage }; + const body: GetEnrollmentAPIKeysResponse = { + list: items, // deprecated + items, + total, + page, + perPage, + }; return response.ok({ body }); } catch (error) { diff --git a/x-pack/plugins/fleet/server/routes/enrollment_api_key/index.ts b/x-pack/plugins/fleet/server/routes/enrollment_api_key/index.ts index 6429d4d29d5c9..39665f14484ba 100644 --- a/x-pack/plugins/fleet/server/routes/enrollment_api_key/index.ts +++ b/x-pack/plugins/fleet/server/routes/enrollment_api_key/index.ts @@ -61,4 +61,44 @@ export const registerRoutes = (routers: { superuser: FleetRouter; fleetSetup: Fl }, postEnrollmentApiKeyHandler ); + + routers.fleetSetup.get( + { + path: ENROLLMENT_API_KEY_ROUTES.INFO_PATTERN_DEPRECATED, + validate: GetOneEnrollmentAPIKeyRequestSchema, + // Disable this tag and the automatic RBAC support until elastic/fleet-server access is removed in 8.0 + // Required to allow elastic/fleet-server to access this API. + // options: { tags: [`access:${PLUGIN_ID}-read`] }, + }, + getOneEnrollmentApiKeyHandler + ); + + routers.superuser.delete( + { + path: ENROLLMENT_API_KEY_ROUTES.DELETE_PATTERN_DEPRECATED, + validate: DeleteEnrollmentAPIKeyRequestSchema, + options: { tags: [`access:${PLUGIN_ID}-all`] }, + }, + deleteEnrollmentApiKeyHandler + ); + + routers.fleetSetup.get( + { + path: ENROLLMENT_API_KEY_ROUTES.LIST_PATTERN_DEPRECATED, + validate: GetEnrollmentAPIKeysRequestSchema, + // Disable this tag and the automatic RBAC support until elastic/fleet-server access is removed in 8.0 + // Required to allow elastic/fleet-server to access this API. + // options: { tags: [`access:${PLUGIN_ID}-read`] }, + }, + getEnrollmentApiKeysHandler + ); + + routers.superuser.post( + { + path: ENROLLMENT_API_KEY_ROUTES.CREATE_PATTERN_DEPRECATED, + validate: PostEnrollmentAPIKeyRequestSchema, + options: { tags: [`access:${PLUGIN_ID}-all`] }, + }, + postEnrollmentApiKeyHandler + ); }; diff --git a/x-pack/plugins/fleet/server/routes/epm/handlers.ts b/x-pack/plugins/fleet/server/routes/epm/handlers.ts index c98038427cafc..4f3f969defd0c 100644 --- a/x-pack/plugins/fleet/server/routes/epm/handlers.ts +++ b/x-pack/plugins/fleet/server/routes/epm/handlers.ts @@ -9,6 +9,7 @@ import path from 'path'; import type { TypeOf } from '@kbn/config-schema'; import mime from 'mime-types'; +import semverValid from 'semver/functions/valid'; import type { ResponseHeaders, KnownHeaders } from 'src/core/server'; import type { @@ -50,8 +51,11 @@ import { getInstallation, } from '../../services/epm/packages'; import type { BulkInstallResponse } from '../../services/epm/packages'; -import { defaultIngestErrorHandler, ingestErrorToResponseOptions } from '../../errors'; -import { splitPkgKey } from '../../services/epm/registry'; +import { + defaultIngestErrorHandler, + ingestErrorToResponseOptions, + IngestManagerError, +} from '../../errors'; import { licenseService } from '../../services'; import { getArchiveEntry } from '../../services/epm/archive/cache'; import { getAsset } from '../../services/epm/archive/storage'; @@ -65,6 +69,7 @@ export const getCategoriesHandler: FleetRequestHandler< try { const res = await getCategories(request.query); const body: GetCategoriesResponse = { + items: res, response: res, }; return response.ok({ body }); @@ -84,6 +89,7 @@ export const getListHandler: FleetRequestHandler< ...request.query, }); const body: GetPackagesResponse = { + items: res, response: res, }; return response.ok({ @@ -99,6 +105,7 @@ export const getLimitedListHandler: FleetRequestHandler = async (context, reques const savedObjectsClient = context.fleet.epm.internalSoClient; const res = await getLimitedPackages({ savedObjectsClient }); const body: GetLimitedPackagesResponse = { + items: res, response: res, }; return response.ok({ @@ -186,13 +193,18 @@ export const getFileHandler: FleetRequestHandler> = async (context, request, response) => { try { - const { pkgkey } = request.params; const savedObjectsClient = context.fleet.epm.internalSoClient; - // TODO: change epm API to /packageName/version so we don't need to do this - const { pkgName, pkgVersion } = splitPkgKey(pkgkey); - const res = await getPackageInfo({ savedObjectsClient, pkgName, pkgVersion }); + const { pkgName, pkgVersion } = request.params; + if (pkgVersion && !semverValid(pkgVersion)) { + throw new IngestManagerError('Package version is not a valid semver'); + } + const res = await getPackageInfo({ + savedObjectsClient, + pkgName, + pkgVersion: pkgVersion || '', + }); const body: GetInfoResponse = { - response: res, + item: res, }; return response.ok({ body }); } catch (error) { @@ -206,14 +218,12 @@ export const updatePackageHandler: FleetRequestHandler< TypeOf > = async (context, request, response) => { try { - const { pkgkey } = request.params; const savedObjectsClient = context.fleet.epm.internalSoClient; - - const { pkgName } = splitPkgKey(pkgkey); + const { pkgName } = request.params; const res = await updatePackage({ savedObjectsClient, pkgName, ...request.body }); const body: UpdatePackageResponse = { - response: res, + item: res, }; return response.ok({ body }); @@ -243,18 +253,18 @@ export const installPackageFromRegistryHandler: FleetRequestHandler< > = async (context, request, response) => { const savedObjectsClient = context.fleet.epm.internalSoClient; const esClient = context.core.elasticsearch.client.asInternalUser; - const { pkgkey } = request.params; + const { pkgName, pkgVersion } = request.params; const res = await installPackage({ installSource: 'registry', savedObjectsClient, - pkgkey, + pkgkey: pkgVersion ? `${pkgName}-${pkgVersion}` : pkgName, esClient, force: request.body?.force, }); if (!res.error) { const body: InstallPackageResponse = { - response: res.assets || [], + items: res.assets || [], }; return response.ok({ body }); } else { @@ -291,6 +301,7 @@ export const bulkInstallPackagesFromRegistryHandler: FleetRequestHandler< }); const payload = bulkInstalledResponses.map(bulkInstallServiceResponseToHttpEntry); const body: BulkInstallPackagesResponse = { + items: payload, response: payload, }; return response.ok({ body }); @@ -321,6 +332,7 @@ export const installPackageByUploadHandler: FleetRequestHandler< }); if (!res.error) { const body: InstallPackageResponse = { + items: res.assets || [], response: res.assets || [], }; return response.ok({ body }); @@ -335,17 +347,18 @@ export const deletePackageHandler: FleetRequestHandler< TypeOf > = async (context, request, response) => { try { - const { pkgkey } = request.params; + const { pkgName, pkgVersion } = request.params; const savedObjectsClient = context.fleet.epm.internalSoClient; const esClient = context.core.elasticsearch.client.asInternalUser; const res = await removeInstallation({ savedObjectsClient, - pkgkey, + pkgName, + pkgVersion, esClient, force: request.body?.force, }); const body: DeletePackageResponse = { - response: res, + items: res, }; return response.ok({ body }); } catch (error) { diff --git a/x-pack/plugins/fleet/server/routes/epm/index.ts b/x-pack/plugins/fleet/server/routes/epm/index.ts index a2f2df4a00c55..b07bb2b1ab77b 100644 --- a/x-pack/plugins/fleet/server/routes/epm/index.ts +++ b/x-pack/plugins/fleet/server/routes/epm/index.ts @@ -5,18 +5,32 @@ * 2.0. */ +import type { IKibanaResponse } from 'src/core/server'; + +import type { + DeletePackageResponse, + GetInfoResponse, + InstallPackageResponse, + UpdatePackageResponse, +} from '../../../common'; + import { PLUGIN_ID, EPM_API_ROUTES } from '../../constants'; +import { splitPkgKey } from '../../services/epm/registry'; import { GetCategoriesRequestSchema, GetPackagesRequestSchema, GetFileRequestSchema, GetInfoRequestSchema, + GetInfoRequestSchemaDeprecated, InstallPackageFromRegistryRequestSchema, + InstallPackageFromRegistryRequestSchemaDeprecated, InstallPackageByUploadRequestSchema, DeletePackageRequestSchema, + DeletePackageRequestSchemaDeprecated, BulkUpgradePackagesFromRegistryRequestSchema, GetStatsRequestSchema, UpdatePackageRequestSchema, + UpdatePackageRequestSchemaDeprecated, } from '../../types'; import type { FleetRouter } from '../../types/request_context'; @@ -142,4 +156,86 @@ export const registerRoutes = (routers: { rbac: FleetRouter; superuser: FleetRou }, deletePackageHandler ); + + // deprecated since 8.0 + routers.rbac.get( + { + path: EPM_API_ROUTES.INFO_PATTERN_DEPRECATED, + validate: GetInfoRequestSchemaDeprecated, + options: { tags: [`access:${PLUGIN_ID}-read`] }, + }, + async (context, request, response) => { + const newRequest = { ...request, params: splitPkgKey(request.params.pkgkey) } as any; + const resp: IKibanaResponse = await getInfoHandler( + context, + newRequest, + response + ); + if (resp.payload?.item) { + // returning item as well here, because pkgVersion is optional in new GET endpoint, and if not specified, the router selects the deprecated route + return response.ok({ body: { item: resp.payload.item, response: resp.payload.item } }); + } + return resp; + } + ); + + routers.superuser.put( + { + path: EPM_API_ROUTES.INFO_PATTERN_DEPRECATED, + validate: UpdatePackageRequestSchemaDeprecated, + options: { tags: [`access:${PLUGIN_ID}-all`] }, + }, + async (context, request, response) => { + const newRequest = { ...request, params: splitPkgKey(request.params.pkgkey) } as any; + const resp: IKibanaResponse = await updatePackageHandler( + context, + newRequest, + response + ); + if (resp.payload?.item) { + return response.ok({ body: { response: resp.payload.item } }); + } + return resp; + } + ); + + routers.superuser.post( + { + path: EPM_API_ROUTES.INSTALL_FROM_REGISTRY_PATTERN_DEPRECATED, + validate: InstallPackageFromRegistryRequestSchemaDeprecated, + options: { tags: [`access:${PLUGIN_ID}-all`] }, + }, + async (context, request, response) => { + const newRequest = { ...request, params: splitPkgKey(request.params.pkgkey) } as any; + const resp: IKibanaResponse = await installPackageFromRegistryHandler( + context, + newRequest, + response + ); + if (resp.payload?.items) { + return response.ok({ body: { response: resp.payload.items } }); + } + return resp; + } + ); + + routers.superuser.delete( + { + path: EPM_API_ROUTES.DELETE_PATTERN_DEPRECATED, + validate: DeletePackageRequestSchemaDeprecated, + options: { tags: [`access:${PLUGIN_ID}-all`] }, + }, + async (context, request, response) => { + const newRequest = { ...request, params: splitPkgKey(request.params.pkgkey) } as any; + const resp: IKibanaResponse = await deletePackageHandler( + context, + newRequest, + response + ); + if (resp.payload?.items) { + return response.ok({ body: { response: resp.payload.items } }); + } + return resp; + } + ); }; diff --git a/x-pack/plugins/fleet/server/saved_objects/index.ts b/x-pack/plugins/fleet/server/saved_objects/index.ts index 26adf7b9fcbc7..6058cfba12cad 100644 --- a/x-pack/plugins/fleet/server/saved_objects/index.ts +++ b/x-pack/plugins/fleet/server/saved_objects/index.ts @@ -113,6 +113,7 @@ const getSavedObjectTypes = ( is_default_monitoring: { type: 'boolean' }, hosts: { type: 'keyword' }, ca_sha256: { type: 'keyword', index: false }, + ca_trusted_fingerprint: { type: 'keyword', index: false }, config: { type: 'flattened' }, config_yaml: { type: 'text' }, is_preconfigured: { type: 'boolean', index: false }, diff --git a/x-pack/plugins/fleet/server/services/agent_policies/full_agent_policy.test.ts b/x-pack/plugins/fleet/server/services/agent_policies/full_agent_policy.test.ts index d720aa72e18f8..1bc1919226248 100644 --- a/x-pack/plugins/fleet/server/services/agent_policies/full_agent_policy.test.ts +++ b/x-pack/plugins/fleet/server/services/agent_policies/full_agent_policy.test.ts @@ -12,7 +12,7 @@ import type { AgentPolicy, Output } from '../../types'; import { agentPolicyService } from '../agent_policy'; import { agentPolicyUpdateEventHandler } from '../agent_policy_update'; -import { getFullAgentPolicy } from './full_agent_policy'; +import { getFullAgentPolicy, transformOutputToFullPolicyOutput } from './full_agent_policy'; import { getMonitoringPermissions } from './monitoring_permissions'; const mockedGetElasticAgentMonitoringPermissions = getMonitoringPermissions as jest.Mock< @@ -305,3 +305,58 @@ describe('getFullAgentPolicy', () => { expect(agentPolicy?.outputs.default).toBeDefined(); }); }); + +describe('transformOutputToFullPolicyOutput', () => { + it('should works with only required field on a output', () => { + const policyOutput = transformOutputToFullPolicyOutput({ + id: 'id123', + hosts: ['http://host.fr'], + is_default: false, + is_default_monitoring: false, + name: 'test output', + type: 'elasticsearch', + api_key: 'apikey123', + }); + + expect(policyOutput).toMatchInlineSnapshot(` + Object { + "api_key": "apikey123", + "ca_sha256": undefined, + "hosts": Array [ + "http://host.fr", + ], + "type": "elasticsearch", + } + `); + }); + it('should support ca_trusted_fingerprint field on a output', () => { + const policyOutput = transformOutputToFullPolicyOutput({ + id: 'id123', + hosts: ['http://host.fr'], + is_default: false, + is_default_monitoring: false, + name: 'test output', + type: 'elasticsearch', + api_key: 'apikey123', + ca_trusted_fingerprint: 'fingerprint123', + config_yaml: ` +test: 1234 +ssl.test: 123 + `, + }); + + expect(policyOutput).toMatchInlineSnapshot(` + Object { + "api_key": "apikey123", + "ca_sha256": undefined, + "hosts": Array [ + "http://host.fr", + ], + "ssl.ca_trusted_fingerprint": "fingerprint123", + "ssl.test": 123, + "test": 1234, + "type": "elasticsearch", + } + `); + }); +}); diff --git a/x-pack/plugins/fleet/server/services/agent_policies/full_agent_policy.ts b/x-pack/plugins/fleet/server/services/agent_policies/full_agent_policy.ts index f89a186c1a5f9..166b2f77dc27b 100644 --- a/x-pack/plugins/fleet/server/services/agent_policies/full_agent_policy.ts +++ b/x-pack/plugins/fleet/server/services/agent_policies/full_agent_policy.ts @@ -168,19 +168,20 @@ export async function getFullAgentPolicy( return fullAgentPolicy; } -function transformOutputToFullPolicyOutput( +export function transformOutputToFullPolicyOutput( output: Output, standalone = false ): FullAgentPolicyOutput { // eslint-disable-next-line @typescript-eslint/naming-convention - const { config_yaml, type, hosts, ca_sha256, api_key } = output; + const { config_yaml, type, hosts, ca_sha256, ca_trusted_fingerprint, api_key } = output; const configJs = config_yaml ? safeLoad(config_yaml) : {}; const newOutput: FullAgentPolicyOutput = { + ...configJs, type, hosts, ca_sha256, api_key, - ...configJs, + ...(ca_trusted_fingerprint ? { 'ssl.ca_trusted_fingerprint': ca_trusted_fingerprint } : {}), }; if (standalone) { diff --git a/x-pack/plugins/fleet/server/services/agent_policy.ts b/x-pack/plugins/fleet/server/services/agent_policy.ts index 69859855d74f0..b63f86e0bf81f 100644 --- a/x-pack/plugins/fleet/server/services/agent_policy.ts +++ b/x-pack/plugins/fleet/server/services/agent_policy.ts @@ -7,6 +7,7 @@ import { uniq, omit } from 'lodash'; import uuid from 'uuid/v4'; +import uuidv5 from 'uuid/v5'; import type { ElasticsearchClient, SavedObjectsClientContract, @@ -33,7 +34,12 @@ import type { ListWithKuery, NewPackagePolicy, } from '../types'; -import { agentPolicyStatuses, packageToPackagePolicy, AGENT_POLICY_INDEX } from '../../common'; +import { + agentPolicyStatuses, + packageToPackagePolicy, + AGENT_POLICY_INDEX, + UUID_V5_NAMESPACE, +} from '../../common'; import type { DeleteAgentPolicyResponse, FleetServerPolicy, @@ -57,6 +63,7 @@ import { agentPolicyUpdateEventHandler } from './agent_policy_update'; import { normalizeKuery, escapeSearchQueryPhrase } from './saved_object'; import { appContextService } from './app_context'; import { getFullAgentPolicy } from './agent_policies'; + const SAVED_OBJECT_TYPE = AGENT_POLICY_SAVED_OBJECT_TYPE; class AgentPolicyService { @@ -127,14 +134,11 @@ class AgentPolicyService { }; let searchParams; - if (id) { - searchParams = { - id: String(id), - }; - } else if ( - preconfiguredAgentPolicy.is_default || - preconfiguredAgentPolicy.is_default_fleet_server - ) { + + const isDefaultPolicy = + preconfiguredAgentPolicy.is_default || preconfiguredAgentPolicy.is_default_fleet_server; + + if (isDefaultPolicy) { searchParams = { searchFields: [ preconfiguredAgentPolicy.is_default_fleet_server @@ -143,10 +147,15 @@ class AgentPolicyService { ], search: 'true', }; + } else if (id) { + searchParams = { + id: String(id), + }; } + if (!searchParams) throw new Error('Missing ID'); - return await this.ensureAgentPolicy(soClient, esClient, newAgentPolicy, searchParams); + return await this.ensureAgentPolicy(soClient, esClient, newAgentPolicy, searchParams, id); } private async ensureAgentPolicy( @@ -158,7 +167,8 @@ class AgentPolicyService { | { searchFields: string[]; search: string; - } + }, + id?: string | number ): Promise<{ created: boolean; policy: AgentPolicy; @@ -196,7 +206,7 @@ class AgentPolicyService { if (agentPolicies.total === 0) { return { created: true, - policy: await this.create(soClient, esClient, newAgentPolicy), + policy: await this.create(soClient, esClient, newAgentPolicy, { id: String(id) }), }; } @@ -780,6 +790,7 @@ export async function addPackageToAgentPolicy( agentPolicy: AgentPolicy, defaultOutput: Output, packagePolicyName?: string, + packagePolicyId?: string | number, packagePolicyDescription?: string, transformPackagePolicy?: (p: NewPackagePolicy) => NewPackagePolicy, bumpAgentPolicyRevison = false @@ -803,7 +814,14 @@ export async function addPackageToAgentPolicy( ? transformPackagePolicy(basePackagePolicy) : basePackagePolicy; + // If an ID is provided via preconfiguration, use that value. Otherwise fall back to + // a UUID v5 value seeded from the agent policy's ID and the provided package policy name. + const id = packagePolicyId + ? String(packagePolicyId) + : uuidv5(`${agentPolicy.id}-${packagePolicyName}`, UUID_V5_NAMESPACE); + await packagePolicyService.create(soClient, esClient, newPackagePolicy, { + id, bumpRevision: bumpAgentPolicyRevison, skipEnsureInstalled: true, skipUniqueNameVerification: true, diff --git a/x-pack/plugins/fleet/server/services/epm/elasticsearch/template/install.ts b/x-pack/plugins/fleet/server/services/epm/elasticsearch/template/install.ts index eb5b43650dad7..4224ff6b01a19 100644 --- a/x-pack/plugins/fleet/server/services/epm/elasticsearch/template/install.ts +++ b/x-pack/plugins/fleet/server/services/epm/elasticsearch/template/install.ts @@ -291,7 +291,6 @@ async function installDataStreamComponentTemplates(params: { }); const templateNames = Object.keys(templates); const templateEntries = Object.entries(templates); - // TODO: Check return values for errors await Promise.all( templateEntries.map(async ([name, body]) => { @@ -307,7 +306,6 @@ async function installDataStreamComponentTemplates(params: { const { clusterPromise } = putComponentTemplate(esClient, logger, { body, name, - create: true, }); return clusterPromise; } @@ -343,8 +341,7 @@ export async function ensureDefaultComponentTemplate( await putComponentTemplate(esClient, logger, { name: FLEET_GLOBAL_COMPONENT_TEMPLATE_NAME, body: FLEET_GLOBAL_COMPONENT_TEMPLATE_CONTENT, - create: true, - }); + }).clusterPromise; } return { isCreated: !existingTemplate }; diff --git a/x-pack/plugins/fleet/server/services/epm/packages/install.ts b/x-pack/plugins/fleet/server/services/epm/packages/install.ts index a580248b43731..77fcc429b2084 100644 --- a/x-pack/plugins/fleet/server/services/epm/packages/install.ts +++ b/x-pack/plugins/fleet/server/services/epm/packages/install.ts @@ -164,7 +164,7 @@ export async function handleInstallPackageFailure({ const installType = getInstallType({ pkgVersion, installedPkg }); if (installType === 'install' || installType === 'reinstall') { logger.error(`uninstalling ${pkgkey} after error installing: [${error.toString()}]`); - await removeInstallation({ savedObjectsClient, pkgkey, esClient }); + await removeInstallation({ savedObjectsClient, pkgName, pkgVersion, esClient }); } await updateInstallStatus({ savedObjectsClient, pkgName, status: 'install_failed' }); diff --git a/x-pack/plugins/fleet/server/services/epm/packages/remove.ts b/x-pack/plugins/fleet/server/services/epm/packages/remove.ts index 957dac8c1aacb..848d17f78c929 100644 --- a/x-pack/plugins/fleet/server/services/epm/packages/remove.ts +++ b/x-pack/plugins/fleet/server/services/epm/packages/remove.ts @@ -22,7 +22,6 @@ import { removeUnusedIndexPatterns } from '../kibana/index_pattern/install'; import { deleteTransforms } from '../elasticsearch/transform/remove'; import { deleteMlModel } from '../elasticsearch/ml_model'; import { packagePolicyService, appContextService } from '../..'; -import { splitPkgKey } from '../registry'; import { deletePackageCache } from '../archive'; import { deleteIlms } from '../elasticsearch/datastream_ilm/remove'; import { removeArchiveEntries } from '../archive/storage'; @@ -31,13 +30,12 @@ import { getInstallation, kibanaSavedObjectTypes } from './index'; export async function removeInstallation(options: { savedObjectsClient: SavedObjectsClientContract; - pkgkey: string; + pkgName: string; + pkgVersion: string; esClient: ElasticsearchClient; force?: boolean; }): Promise { - const { savedObjectsClient, pkgkey, esClient, force } = options; - // TODO: the epm api should change to /name/version so we don't need to do this - const { pkgName, pkgVersion } = splitPkgKey(pkgkey); + const { savedObjectsClient, pkgName, pkgVersion, esClient, force } = options; const installation = await getInstallation({ savedObjectsClient, pkgName }); if (!installation) throw Boom.badRequest(`${pkgName} is not installed`); if (installation.removable === false && !force) diff --git a/x-pack/plugins/fleet/server/services/epm/registry/index.ts b/x-pack/plugins/fleet/server/services/epm/registry/index.ts index 1b6e28a07f8e0..8cfb2844159bc 100644 --- a/x-pack/plugins/fleet/server/services/epm/registry/index.ts +++ b/x-pack/plugins/fleet/server/services/epm/registry/index.ts @@ -8,9 +8,11 @@ import { URL } from 'url'; import mime from 'mime-types'; -import semverValid from 'semver/functions/valid'; + import type { Response } from 'node-fetch'; +import { splitPkgKey as split } from '../../../../common'; + import { KibanaAssetType } from '../../../types'; import type { AssetsGroupedByServiceByType, @@ -31,12 +33,7 @@ import { } from '../archive'; import { streamToBuffer } from '../streams'; import { appContextService } from '../..'; -import { - PackageKeyInvalidError, - PackageNotFoundError, - PackageCacheError, - RegistryResponseError, -} from '../../../errors'; +import { PackageNotFoundError, PackageCacheError, RegistryResponseError } from '../../../errors'; import { fetchUrl, getResponse, getResponseStream } from './requests'; import { getRegistryUrl } from './registry_url'; @@ -46,33 +43,7 @@ export interface SearchParams { experimental?: boolean; } -/** - * Extract the package name and package version from a string. - * - * @param pkgkey a string containing the package name delimited by the package version - */ -export function splitPkgKey(pkgkey: string): { pkgName: string; pkgVersion: string } { - // If no version is provided, use the provided package key as the - // package name and return an empty version value - if (!pkgkey.includes('-')) { - return { pkgName: pkgkey, pkgVersion: '' }; - } - - const pkgName = pkgkey.includes('-') ? pkgkey.substr(0, pkgkey.indexOf('-')) : pkgkey; - - if (pkgName === '') { - throw new PackageKeyInvalidError('Package key parsing failed: package name was empty'); - } - - // this will return the entire string if `indexOf` return -1 - const pkgVersion = pkgkey.substr(pkgkey.indexOf('-') + 1); - if (!semverValid(pkgVersion)) { - throw new PackageKeyInvalidError( - 'Package key parsing failed: package version was not a valid semver' - ); - } - return { pkgName, pkgVersion }; -} +export const splitPkgKey = split; export const pkgToPkgKey = ({ name, version }: { name: string; version: string }) => `${name}-${version}`; diff --git a/x-pack/plugins/fleet/server/services/preconfiguration.test.ts b/x-pack/plugins/fleet/server/services/preconfiguration.test.ts index 4b87c0957c961..8324079e10da8 100644 --- a/x-pack/plugins/fleet/server/services/preconfiguration.test.ts +++ b/x-pack/plugins/fleet/server/services/preconfiguration.test.ts @@ -446,6 +446,7 @@ describe('policy preconfiguration', () => { id: 'test-id', package_policies: [ { + id: 'test-package', package: { name: 'test_package' }, name: 'Test package', }, diff --git a/x-pack/plugins/fleet/server/services/preconfiguration.ts b/x-pack/plugins/fleet/server/services/preconfiguration.ts index 76fa7778eafa2..a41c7606287ee 100644 --- a/x-pack/plugins/fleet/server/services/preconfiguration.ts +++ b/x-pack/plugins/fleet/server/services/preconfiguration.ts @@ -61,6 +61,7 @@ function isPreconfiguredOutputDifferentFromCurrent( preconfiguredOutput.hosts.map(normalizeHostsForAgents) )) || existingOutput.ca_sha256 !== preconfiguredOutput.ca_sha256 || + existingOutput.ca_trusted_fingerprint !== preconfiguredOutput.ca_trusted_fingerprint || existingOutput.config_yaml !== preconfiguredOutput.config_yaml ); } @@ -404,6 +405,7 @@ async function addPreconfiguredPolicyPackages( agentPolicy: AgentPolicy, installedPackagePolicies: Array< Partial> & { + id?: string | number; name: string; installedPackage: Installation; inputs?: InputsOverride[]; @@ -413,7 +415,7 @@ async function addPreconfiguredPolicyPackages( bumpAgentPolicyRevison = false ) { // Add packages synchronously to avoid overwriting - for (const { installedPackage, name, description, inputs } of installedPackagePolicies) { + for (const { installedPackage, id, name, description, inputs } of installedPackagePolicies) { const packageInfo = await getPackageInfo({ savedObjectsClient: soClient, pkgName: installedPackage.name, @@ -427,6 +429,7 @@ async function addPreconfiguredPolicyPackages( agentPolicy, defaultOutput, name, + id, description, (policy) => preconfigurePackageInputs(policy, packageInfo, inputs), bumpAgentPolicyRevison diff --git a/x-pack/plugins/fleet/server/services/settings.ts b/x-pack/plugins/fleet/server/services/settings.ts index 26d581f32d9a2..0e7b7c5e7a093 100644 --- a/x-pack/plugins/fleet/server/services/settings.ts +++ b/x-pack/plugins/fleet/server/services/settings.ts @@ -11,6 +11,7 @@ import type { SavedObjectsClientContract } from 'kibana/server'; import { decodeCloudId, GLOBAL_SETTINGS_SAVED_OBJECT_TYPE, + GLOBAL_SETTINGS_ID, normalizeHostsForAgents, } from '../../common'; import type { SettingsSOAttributes, Settings, BaseSettings } from '../../common'; @@ -80,10 +81,17 @@ export async function saveSettings( } catch (e) { if (e.isBoom && e.output.statusCode === 404) { const defaultSettings = createDefaultSettings(); - const res = await soClient.create(GLOBAL_SETTINGS_SAVED_OBJECT_TYPE, { - ...defaultSettings, - ...data, - }); + const res = await soClient.create( + GLOBAL_SETTINGS_SAVED_OBJECT_TYPE, + { + ...defaultSettings, + ...data, + }, + { + id: GLOBAL_SETTINGS_ID, + overwrite: true, + } + ); return { id: res.id, diff --git a/x-pack/plugins/fleet/server/types/models/preconfiguration.ts b/x-pack/plugins/fleet/server/types/models/preconfiguration.ts index 3ba89f1e526b3..4d030e1e87ed4 100644 --- a/x-pack/plugins/fleet/server/types/models/preconfiguration.ts +++ b/x-pack/plugins/fleet/server/types/models/preconfiguration.ts @@ -87,6 +87,7 @@ export const PreconfiguredOutputsSchema = schema.arrayOf( type: schema.oneOf([schema.literal(outputType.Elasticsearch)]), hosts: schema.maybe(schema.arrayOf(schema.uri({ scheme: ['http', 'https'] }))), ca_sha256: schema.maybe(schema.string()), + ca_trusted_fingerprint: schema.maybe(schema.string()), config: schema.maybe(schema.object({}, { unknowns: 'allow' })), }), { @@ -106,6 +107,7 @@ export const PreconfiguredAgentPoliciesSchema = schema.arrayOf( monitoring_output_id: schema.maybe(schema.string()), package_policies: schema.arrayOf( schema.object({ + id: schema.maybe(schema.oneOf([schema.string(), schema.number()])), name: schema.string(), package: schema.object({ name: schema.string(), diff --git a/x-pack/plugins/fleet/server/types/rest_spec/epm.ts b/x-pack/plugins/fleet/server/types/rest_spec/epm.ts index 918def62a9d0e..390d5dea792cb 100644 --- a/x-pack/plugins/fleet/server/types/rest_spec/epm.ts +++ b/x-pack/plugins/fleet/server/types/rest_spec/epm.ts @@ -30,12 +30,29 @@ export const GetFileRequestSchema = { }; export const GetInfoRequestSchema = { + params: schema.object({ + pkgName: schema.string(), + pkgVersion: schema.maybe(schema.string()), + }), +}; + +export const GetInfoRequestSchemaDeprecated = { params: schema.object({ pkgkey: schema.string(), }), }; export const UpdatePackageRequestSchema = { + params: schema.object({ + pkgName: schema.string(), + pkgVersion: schema.maybe(schema.string()), + }), + body: schema.object({ + keepPoliciesUpToDate: schema.boolean(), + }), +}; + +export const UpdatePackageRequestSchemaDeprecated = { params: schema.object({ pkgkey: schema.string(), }), @@ -51,6 +68,18 @@ export const GetStatsRequestSchema = { }; export const InstallPackageFromRegistryRequestSchema = { + params: schema.object({ + pkgName: schema.string(), + pkgVersion: schema.maybe(schema.string()), + }), + body: schema.nullable( + schema.object({ + force: schema.boolean(), + }) + ), +}; + +export const InstallPackageFromRegistryRequestSchemaDeprecated = { params: schema.object({ pkgkey: schema.string(), }), @@ -72,6 +101,18 @@ export const InstallPackageByUploadRequestSchema = { }; export const DeletePackageRequestSchema = { + params: schema.object({ + pkgName: schema.string(), + pkgVersion: schema.string(), + }), + body: schema.nullable( + schema.object({ + force: schema.boolean(), + }) + ), +}; + +export const DeletePackageRequestSchemaDeprecated = { params: schema.object({ pkgkey: schema.string(), }), diff --git a/x-pack/plugins/fleet/server/types/rest_spec/output.ts b/x-pack/plugins/fleet/server/types/rest_spec/output.ts index dc60b26087219..de2ddeb3a1bfd 100644 --- a/x-pack/plugins/fleet/server/types/rest_spec/output.ts +++ b/x-pack/plugins/fleet/server/types/rest_spec/output.ts @@ -30,6 +30,7 @@ export const PostOutputRequestSchema = { is_default_monitoring: schema.boolean({ defaultValue: false }), hosts: schema.maybe(schema.arrayOf(schema.uri({ scheme: ['http', 'https'] }))), ca_sha256: schema.maybe(schema.string()), + ca_trusted_fingerprint: schema.maybe(schema.string()), config_yaml: schema.maybe(schema.string()), }), }; @@ -45,6 +46,7 @@ export const PutOutputRequestSchema = { is_default_monitoring: schema.maybe(schema.boolean()), hosts: schema.maybe(schema.arrayOf(schema.uri({ scheme: ['http', 'https'] }))), ca_sha256: schema.maybe(schema.string()), + ca_trusted_fingerprint: schema.maybe(schema.string()), config_yaml: schema.maybe(schema.string()), }), }; diff --git a/x-pack/plugins/fleet/storybook/context/fixtures/categories.ts b/x-pack/plugins/fleet/storybook/context/fixtures/categories.ts index 002748bd3d967..1bb8d3300624e 100644 --- a/x-pack/plugins/fleet/storybook/context/fixtures/categories.ts +++ b/x-pack/plugins/fleet/storybook/context/fixtures/categories.ts @@ -6,7 +6,7 @@ */ import type { GetCategoriesResponse } from '../../../public/types'; -export const response: GetCategoriesResponse['response'] = [ +export const items: GetCategoriesResponse['items'] = [ { id: 'aws', title: 'AWS', diff --git a/x-pack/plugins/fleet/storybook/context/fixtures/integration.nginx.ts b/x-pack/plugins/fleet/storybook/context/fixtures/integration.nginx.ts index de4fd228b5342..6f48b15158f8d 100644 --- a/x-pack/plugins/fleet/storybook/context/fixtures/integration.nginx.ts +++ b/x-pack/plugins/fleet/storybook/context/fixtures/integration.nginx.ts @@ -7,7 +7,7 @@ import type { GetInfoResponse } from '../../../public/types'; import { KibanaAssetType, ElasticsearchAssetType } from '../../../common/types'; -export const response: GetInfoResponse['response'] = { +export const item: GetInfoResponse['item'] = { name: 'nginx', title: 'Nginx', version: '0.7.0', diff --git a/x-pack/plugins/fleet/storybook/context/fixtures/integration.okta.ts b/x-pack/plugins/fleet/storybook/context/fixtures/integration.okta.ts index 360c340c9645f..6b766c2d126df 100644 --- a/x-pack/plugins/fleet/storybook/context/fixtures/integration.okta.ts +++ b/x-pack/plugins/fleet/storybook/context/fixtures/integration.okta.ts @@ -7,7 +7,7 @@ import type { GetInfoResponse } from '../../../public/types'; import { KibanaAssetType, ElasticsearchAssetType } from '../../../common/types'; -export const response: GetInfoResponse['response'] = { +export const item: GetInfoResponse['item'] = { name: 'okta', title: 'Okta', version: '1.2.0', diff --git a/x-pack/plugins/fleet/storybook/context/fixtures/packages.ts b/x-pack/plugins/fleet/storybook/context/fixtures/packages.ts index dfe8e905be089..4c13b6b6bf8cb 100644 --- a/x-pack/plugins/fleet/storybook/context/fixtures/packages.ts +++ b/x-pack/plugins/fleet/storybook/context/fixtures/packages.ts @@ -7,7 +7,7 @@ import type { GetPackagesResponse } from '../../../public/types'; -export const response: GetPackagesResponse['response'] = [ +export const items: GetPackagesResponse['items'] = [ { name: 'ga_not_installed', title: 'a. GA, Not Installed', diff --git a/x-pack/plugins/fleet/storybook/context/http.ts b/x-pack/plugins/fleet/storybook/context/http.ts index 3e515c075a595..491b62201e532 100644 --- a/x-pack/plugins/fleet/storybook/context/http.ts +++ b/x-pack/plugins/fleet/storybook/context/http.ts @@ -55,7 +55,7 @@ export const getHttp = (basepath = BASE_PATH) => { // Ideally, this would be a markdown file instead of a ts file, but we don't have // markdown-loader in our package.json, so we'll make do with what we have. - if (path.startsWith('/api/fleet/epm/packages/nginx/')) { + if (path.match('/api/fleet/epm/packages/nginx/.*/.*/')) { const { readme } = await import('./fixtures/readme.nginx'); return readme; } @@ -66,7 +66,7 @@ export const getHttp = (basepath = BASE_PATH) => { // Ideally, this would be a markdown file instead of a ts file, but we don't have // markdown-loader in our package.json, so we'll make do with what we have. - if (path.startsWith('/api/fleet/epm/packages/okta/')) { + if (path.match('/api/fleet/epm/packages/okta/.*/.*/')) { const { readme } = await import('./fixtures/readme.okta'); return readme; } diff --git a/x-pack/plugins/grokdebugger/public/plugin.js b/x-pack/plugins/grokdebugger/public/plugin.js index 63ebbb761b88d..b92cfce6ec0ef 100644 --- a/x-pack/plugins/grokdebugger/public/plugin.js +++ b/x-pack/plugins/grokdebugger/public/plugin.js @@ -22,11 +22,11 @@ export class GrokDebuggerUIPlugin { }), id: PLUGIN.ID, enableRouting: false, - async mount({ element }) { + async mount({ element, theme$ }) { const [coreStart] = await coreSetup.getStartServices(); const license = await plugins.licensing.license$.pipe(first()).toPromise(); const { renderApp } = await import('./render_app'); - return renderApp(license, element, coreStart); + return renderApp(license, element, coreStart, theme$); }, }); diff --git a/x-pack/plugins/grokdebugger/public/render_app.js b/x-pack/plugins/grokdebugger/public/render_app.js index 9666d69d978f0..bcb2560a3c0b7 100644 --- a/x-pack/plugins/grokdebugger/public/render_app.js +++ b/x-pack/plugins/grokdebugger/public/render_app.js @@ -7,23 +7,27 @@ import React from 'react'; import { render, unmountComponentAtNode } from 'react-dom'; +import { I18nProvider } from '@kbn/i18n-react'; +import { KibanaContextProvider, KibanaThemeProvider } from './shared_imports'; import { GrokDebugger } from './components/grok_debugger'; import { GrokdebuggerService } from './services/grokdebugger/grokdebugger_service'; -import { I18nProvider } from '@kbn/i18n-react'; -import { KibanaContextProvider } from '../../../../src/plugins/kibana_react/public'; import { InactiveLicenseSlate } from './components/inactive_license'; -export function renderApp(license, element, coreStart) { +export function renderApp(license, element, coreStart, theme$) { const content = license.isActive ? ( - + + + ) : ( - + + + ); diff --git a/x-pack/plugins/grokdebugger/public/shared_imports.ts b/x-pack/plugins/grokdebugger/public/shared_imports.ts index cab31cb683786..2779673d665b9 100644 --- a/x-pack/plugins/grokdebugger/public/shared_imports.ts +++ b/x-pack/plugins/grokdebugger/public/shared_imports.ts @@ -6,3 +6,8 @@ */ export { EuiCodeEditor } from '../../../../src/plugins/es_ui_shared/public'; + +export { + KibanaContextProvider, + KibanaThemeProvider, +} from '../../../../src/plugins/kibana_react/public'; diff --git a/x-pack/plugins/index_lifecycle_management/__jest__/__snapshots__/policy_table.test.tsx.snap b/x-pack/plugins/index_lifecycle_management/__jest__/__snapshots__/policy_table.test.tsx.snap index 8f375305d359e..f4d7fc149a694 100644 --- a/x-pack/plugins/index_lifecycle_management/__jest__/__snapshots__/policy_table.test.tsx.snap +++ b/x-pack/plugins/index_lifecycle_management/__jest__/__snapshots__/policy_table.test.tsx.snap @@ -42,57 +42,70 @@ exports[`policy table shows empty state when there are no policies 1`] = ` role="main" >
-
-

- Create your first index lifecycle policy -

- -
-

- An index lifecycle policy helps you manage your indices as they age. -

-
- -
- +

+ Create your first index lifecycle policy +

+ +
+
+

+ An index lifecycle policy helps you manage your indices as they age. +

+
+ +
+ +
+
+
`; diff --git a/x-pack/plugins/index_lifecycle_management/public/application/index.tsx b/x-pack/plugins/index_lifecycle_management/public/application/index.tsx index 5a6d8bb878c37..933a2fd28e07f 100644 --- a/x-pack/plugins/index_lifecycle_management/public/application/index.tsx +++ b/x-pack/plugins/index_lifecycle_management/public/application/index.tsx @@ -7,17 +7,25 @@ import React from 'react'; import { render, unmountComponentAtNode } from 'react-dom'; -import { I18nStart, ScopedHistory, ApplicationStart } from 'kibana/public'; -import { UnmountCallback } from 'src/core/public'; -import { CloudSetup } from '../../../cloud/public'; -import { ILicense } from '../../../licensing/public'; - -import { KibanaContextProvider, APP_WRAPPER_CLASS } from '../shared_imports'; +import { Observable } from 'rxjs'; +import { + I18nStart, + ScopedHistory, + ApplicationStart, + UnmountCallback, + CoreTheme, +} from 'src/core/public'; +import { + CloudSetup, + ILicense, + KibanaContextProvider, + APP_WRAPPER_CLASS, + RedirectAppLinks, + KibanaThemeProvider, +} from '../shared_imports'; import { App } from './app'; - import { BreadcrumbService } from './services/breadcrumbs'; -import { RedirectAppLinks } from '../../../../../src/plugins/kibana_react/public'; export const renderApp = ( element: Element, @@ -26,15 +34,18 @@ export const renderApp = ( application: ApplicationStart, breadcrumbService: BreadcrumbService, license: ILicense, + theme$: Observable, cloud?: CloudSetup ): UnmountCallback => { const { getUrlForApp } = application; render( - - - + + + + + , element diff --git a/x-pack/plugins/index_lifecycle_management/public/plugin.tsx b/x-pack/plugins/index_lifecycle_management/public/plugin.tsx index bc0981529c34f..d59fd4f20e63f 100644 --- a/x-pack/plugins/index_lifecycle_management/public/plugin.tsx +++ b/x-pack/plugins/index_lifecycle_management/public/plugin.tsx @@ -50,7 +50,7 @@ export class IndexLifecycleManagementPlugin id: PLUGIN.ID, title: PLUGIN.TITLE, order: 2, - mount: async ({ element, history, setBreadcrumbs }) => { + mount: async ({ element, history, setBreadcrumbs, theme$ }) => { const [coreStart, { licensing }] = await getStartServices(); const { chrome: { docTitle }, @@ -78,6 +78,7 @@ export class IndexLifecycleManagementPlugin application, this.breadcrumbService, license, + theme$, cloud ); diff --git a/x-pack/plugins/index_lifecycle_management/public/shared_imports.ts b/x-pack/plugins/index_lifecycle_management/public/shared_imports.ts index dab299c476eea..dcf435fd72831 100644 --- a/x-pack/plugins/index_lifecycle_management/public/shared_imports.ts +++ b/x-pack/plugins/index_lifecycle_management/public/shared_imports.ts @@ -20,6 +20,7 @@ export type { ValidationConfig, ValidationError, } from '../../../../src/plugins/es_ui_shared/static/forms/hook_form_lib'; + export { useForm, useFormData, @@ -43,8 +44,16 @@ export { export { attemptToURIDecode } from '../../../../src/plugins/es_ui_shared/public'; -export { KibanaContextProvider } from '../../../../src/plugins/kibana_react/public'; +export { + KibanaContextProvider, + KibanaThemeProvider, + RedirectAppLinks, +} from '../../../../src/plugins/kibana_react/public'; export { APP_WRAPPER_CLASS } from '../../../../src/core/public'; export const useKibana = () => _useKibana(); + +export type { CloudSetup } from '../../cloud/public'; + +export type { ILicense } from '../../licensing/public'; diff --git a/x-pack/plugins/index_management/__jest__/client_integration/helpers/http_requests.ts b/x-pack/plugins/index_management/__jest__/client_integration/helpers/http_requests.ts index e26eeadd4edcd..64b8b79d4b2a1 100644 --- a/x-pack/plugins/index_management/__jest__/client_integration/helpers/http_requests.ts +++ b/x-pack/plugins/index_management/__jest__/client_integration/helpers/http_requests.ts @@ -140,6 +140,17 @@ const registerHttpRequestMockHelpers = (server: SinonFakeServer) => { ]); }; + const setLoadNodesPluginsResponse = (response?: HttpResponse, error?: any) => { + const status = error ? error.status || 400 : 200; + const body = error ? error.body : response; + + server.respondWith('GET', `${API_BASE_PATH}/nodes/plugins`, [ + status, + { 'Content-Type': 'application/json' }, + JSON.stringify(body), + ]); + }; + return { setLoadTemplatesResponse, setLoadIndicesResponse, @@ -154,6 +165,7 @@ const registerHttpRequestMockHelpers = (server: SinonFakeServer) => { setUpdateIndexSettingsResponse, setSimulateTemplateResponse, setLoadComponentTemplatesResponse, + setLoadNodesPluginsResponse, }; }; diff --git a/x-pack/plugins/index_management/__jest__/client_integration/helpers/test_subjects.ts b/x-pack/plugins/index_management/__jest__/client_integration/helpers/test_subjects.ts index ac4b4c46ad4d1..5da1fc61742e6 100644 --- a/x-pack/plugins/index_management/__jest__/client_integration/helpers/test_subjects.ts +++ b/x-pack/plugins/index_management/__jest__/client_integration/helpers/test_subjects.ts @@ -25,7 +25,6 @@ export type TestSubjects = | 'ilmPolicyLink' | 'includeStatsSwitch' | 'includeManagedSwitch' - | 'indexActionsContextMenuButton' | 'indexContextMenu' | 'indexManagementHeaderContent' | 'indexTable' diff --git a/x-pack/plugins/index_management/__jest__/client_integration/home/indices_tab.helpers.ts b/x-pack/plugins/index_management/__jest__/client_integration/home/indices_tab.helpers.ts index 0e4564163c553..c1b8dfcc0034f 100644 --- a/x-pack/plugins/index_management/__jest__/client_integration/home/indices_tab.helpers.ts +++ b/x-pack/plugins/index_management/__jest__/client_integration/home/indices_tab.helpers.ts @@ -45,7 +45,6 @@ export const setup = async (overridingDependencies: any = {}): Promise { const { find, component } = testBed; diff --git a/x-pack/plugins/index_management/__jest__/client_integration/home/indices_tab.test.ts b/x-pack/plugins/index_management/__jest__/client_integration/home/indices_tab.test.ts index ec80bf5d712c0..689c48b24a9c3 100644 --- a/x-pack/plugins/index_management/__jest__/client_integration/home/indices_tab.test.ts +++ b/x-pack/plugins/index_management/__jest__/client_integration/home/indices_tab.test.ts @@ -196,11 +196,22 @@ describe('', () => { httpRequestsMockHelpers.setReloadIndicesResponse({ indexNames: [indexName] }); testBed = await setup(); - const { find, component } = testBed; + const { component, find } = testBed; + component.update(); find('indexTableIndexNameLink').at(0).simulate('click'); }); + test('should be able to close an open index', async () => { + const { actions } = testBed; + + await actions.clickManageContextMenuButton(); + await actions.clickContextMenuOption('closeIndexMenuButton'); + + // A refresh call was added after closing an index so we need to check the second to last request. + const latestRequest = server.requests[server.requests.length - 2]; + expect(latestRequest.url).toBe(`${API_BASE_PATH}/indices/close`); + }); test('should be able to flush index', async () => { const { actions } = testBed; diff --git a/x-pack/plugins/index_management/__jest__/client_integration/index_template_wizard/template_create.test.tsx b/x-pack/plugins/index_management/__jest__/client_integration/index_template_wizard/template_create.test.tsx index 67c9ed067227d..65d3678735689 100644 --- a/x-pack/plugins/index_management/__jest__/client_integration/index_template_wizard/template_create.test.tsx +++ b/x-pack/plugins/index_management/__jest__/client_integration/index_template_wizard/template_create.test.tsx @@ -82,6 +82,7 @@ describe('', () => { jest.useFakeTimers(); httpRequestsMockHelpers.setLoadComponentTemplatesResponse(componentTemplates); + httpRequestsMockHelpers.setLoadNodesPluginsResponse([]); // disable all react-beautiful-dnd development warnings (window as any)['__react-beautiful-dnd-disable-dev-warnings'] = true; @@ -296,7 +297,7 @@ describe('', () => { }); describe('mappings (step 4)', () => { - beforeEach(async () => { + const navigateToMappingsStep = async () => { const { actions } = testBed; // Logistics await actions.completeStepOne({ name: TEMPLATE_NAME, indexPatterns: ['index1'] }); @@ -304,6 +305,10 @@ describe('', () => { await actions.completeStepTwo(); // Index settings await actions.completeStepThree('{}'); + }; + + beforeEach(async () => { + await navigateToMappingsStep(); }); it('should set the correct page title', () => { @@ -337,6 +342,43 @@ describe('', () => { expect(find('fieldsListItem').length).toBe(1); }); + + describe('plugin parameters', () => { + const selectMappingsEditorTab = async ( + tab: 'fields' | 'runtimeFields' | 'templates' | 'advanced' + ) => { + const tabIndex = ['fields', 'runtimeFields', 'templates', 'advanced'].indexOf(tab); + const tabElement = testBed.find('mappingsEditor.formTab').at(tabIndex); + await act(async () => { + tabElement.simulate('click'); + }); + testBed.component.update(); + }; + + test('should not render the _size parameter if the mapper size plugin is not installed', async () => { + const { exists } = testBed; + // Navigate to the advanced configuration + await selectMappingsEditorTab('advanced'); + + expect(exists('mappingsEditor.advancedConfiguration.sizeEnabledToggle')).toBe(false); + }); + + test('should render the _size parameter if the mapper size plugin is installed', async () => { + httpRequestsMockHelpers.setLoadNodesPluginsResponse(['mapper-size']); + + await act(async () => { + testBed = await setup(); + }); + testBed.component.update(); + await navigateToMappingsStep(); + + await selectMappingsEditorTab('advanced'); + + expect(testBed.exists('mappingsEditor.advancedConfiguration.sizeEnabledToggle')).toBe( + true + ); + }); + }); }); describe('aliases (step 5)', () => { diff --git a/x-pack/plugins/index_management/__jest__/client_integration/index_template_wizard/template_form.helpers.ts b/x-pack/plugins/index_management/__jest__/client_integration/index_template_wizard/template_form.helpers.ts index 3a8d34c341834..f2fcf7bbab50c 100644 --- a/x-pack/plugins/index_management/__jest__/client_integration/index_template_wizard/template_form.helpers.ts +++ b/x-pack/plugins/index_management/__jest__/client_integration/index_template_wizard/template_form.helpers.ts @@ -339,4 +339,6 @@ export type TestSubjects = | 'versionField' | 'aliasesEditor' | 'settingsEditor' - | 'versionField.input'; + | 'versionField.input' + | 'mappingsEditor.formTab' + | 'mappingsEditor.advancedConfiguration.sizeEnabledToggle'; diff --git a/x-pack/plugins/index_management/public/application/app_context.tsx b/x-pack/plugins/index_management/public/application/app_context.tsx index 5cd0864a4df21..f44ff13b205db 100644 --- a/x-pack/plugins/index_management/public/application/app_context.tsx +++ b/x-pack/plugins/index_management/public/application/app_context.tsx @@ -6,15 +6,15 @@ */ import React, { createContext, useContext } from 'react'; -import { ScopedHistory } from 'kibana/public'; +import { Observable } from 'rxjs'; import SemVer from 'semver/classes/semver'; import { ManagementAppMountParams } from 'src/plugins/management/public'; import { UsageCollectionSetup } from 'src/plugins/usage_collection/public'; +import { CoreSetup, CoreStart, CoreTheme, ScopedHistory } from 'src/core/public'; +import { SharePluginStart } from 'src/plugins/share/public'; -import { CoreSetup, CoreStart } from '../../../../../src/core/public'; -import { UiMetricService, NotificationService, HttpService } from './services'; import { ExtensionsService } from '../services'; -import { SharePluginStart } from '../../../../../src/plugins/share/public'; +import { UiMetricService, NotificationService, HttpService } from './services'; const AppContext = createContext(undefined); @@ -39,6 +39,7 @@ export interface AppDependencies { url: SharePluginStart['url']; docLinks: CoreStart['docLinks']; kibanaVersion: SemVer; + theme$: Observable; } export const AppContextProvider = ({ diff --git a/x-pack/plugins/index_management/public/application/components/component_templates/__jest__/client_integration/component_template_edit.test.tsx b/x-pack/plugins/index_management/public/application/components/component_templates/__jest__/client_integration/component_template_edit.test.tsx index a39baf59d1f05..1f4abac806276 100644 --- a/x-pack/plugins/index_management/public/application/components/component_templates/__jest__/client_integration/component_template_edit.test.tsx +++ b/x-pack/plugins/index_management/public/application/components/component_templates/__jest__/client_integration/component_template_edit.test.tsx @@ -74,13 +74,15 @@ describe('', () => { expect(nameInput.props().disabled).toEqual(true); }); - // FLAKY: https://github.com/elastic/kibana/issues/84906 - describe.skip('form payload', () => { + describe('form payload', () => { it('should send the correct payload with changed values', async () => { const { actions, component, form } = testBed; await act(async () => { form.setInputValue('versionField.input', '1'); + }); + + await act(async () => { actions.clickNextButton(); }); diff --git a/x-pack/plugins/index_management/public/application/components/mappings_editor/__jest__/client_integration/helpers/setup_environment.tsx b/x-pack/plugins/index_management/public/application/components/mappings_editor/__jest__/client_integration/helpers/setup_environment.tsx index d80712dfa0fea..49922b45f2fde 100644 --- a/x-pack/plugins/index_management/public/application/components/mappings_editor/__jest__/client_integration/helpers/setup_environment.tsx +++ b/x-pack/plugins/index_management/public/application/components/mappings_editor/__jest__/client_integration/helpers/setup_environment.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import React from 'react'; +import React, { ComponentType, MemoExoticComponent } from 'react'; import SemVer from 'semver/classes/semver'; /* eslint-disable-next-line @kbn/eslint/no-restricted-paths */ @@ -18,6 +18,7 @@ import { import { MAJOR_VERSION } from '../../../../../../../common'; import { MappingsEditorProvider } from '../../../mappings_editor_context'; import { createKibanaReactContext } from '../../../shared_imports'; +import { Props as MappingsEditorProps } from '../../../mappings_editor'; export const kibanaVersion = new SemVer(MAJOR_VERSION); @@ -82,17 +83,21 @@ const { Provider: KibanaReactContextProvider } = createKibanaReactContext({ }, }); -const defaultProps = { +const defaultProps: MappingsEditorProps = { docLinks: docLinksServiceMock.createStartContract(), + onChange: () => undefined, + esNodesPlugins: [], }; -export const WithAppDependencies = (Comp: any) => (props: any) => - ( - - - - - - - - ); +export const WithAppDependencies = + (Comp: MemoExoticComponent>) => + (props: Partial) => + ( + + + + + + + + ); diff --git a/x-pack/plugins/index_management/public/application/components/mappings_editor/components/configuration_form/configuration_form.tsx b/x-pack/plugins/index_management/public/application/components/mappings_editor/components/configuration_form/configuration_form.tsx index 5a7c6d439d101..4e4c146c85957 100644 --- a/x-pack/plugins/index_management/public/application/components/mappings_editor/components/configuration_form/configuration_form.tsx +++ b/x-pack/plugins/index_management/public/application/components/mappings_editor/components/configuration_form/configuration_form.tsx @@ -10,15 +10,19 @@ import { EuiSpacer } from '@elastic/eui'; import { useForm, Form } from '../../shared_imports'; import { GenericObject, MappingsConfiguration } from '../../types'; +import { MapperSizePluginId } from '../../constants'; import { useDispatch } from '../../mappings_state_context'; import { DynamicMappingSection } from './dynamic_mapping_section'; import { SourceFieldSection } from './source_field_section'; import { MetaFieldSection } from './meta_field_section'; import { RoutingSection } from './routing_section'; +import { MapperSizePluginSection } from './mapper_size_plugin_section'; import { configurationFormSchema } from './configuration_form_schema'; interface Props { value?: MappingsConfiguration; + /** List of plugins installed in the cluster nodes */ + esNodesPlugins: string[]; } const formSerializer = (formData: GenericObject) => { @@ -35,6 +39,7 @@ const formSerializer = (formData: GenericObject) => { sourceField, metaField, _routing, + _size, } = formData; const dynamic = dynamicMappingsEnabled ? true : throwErrorsForUnmappedFields ? 'strict' : false; @@ -47,6 +52,7 @@ const formSerializer = (formData: GenericObject) => { _source: sourceField, _meta: metaField, _routing, + _size, }; return serialized; @@ -67,6 +73,8 @@ const formDeserializer = (formData: GenericObject) => { }, _meta, _routing, + // For the Mapper Size plugin + _size, } = formData; return { @@ -84,10 +92,11 @@ const formDeserializer = (formData: GenericObject) => { }, metaField: _meta ?? {}, _routing, + _size, }; }; -export const ConfigurationForm = React.memo(({ value }: Props) => { +export const ConfigurationForm = React.memo(({ value, esNodesPlugins }: Props) => { const isMounted = useRef(false); const { form } = useForm({ @@ -100,6 +109,9 @@ export const ConfigurationForm = React.memo(({ value }: Props) => { const dispatch = useDispatch(); const { subscribe, submit, reset, getFormData } = form; + const isMapperSizeSectionVisible = + value?._size !== undefined || esNodesPlugins.includes(MapperSizePluginId); + useEffect(() => { const subscription = subscribe(({ data, isValid, validate }) => { dispatch({ @@ -150,6 +162,7 @@ export const ConfigurationForm = React.memo(({ value }: Props) => { + {isMapperSizeSectionVisible && } ); }); diff --git a/x-pack/plugins/index_management/public/application/components/mappings_editor/components/configuration_form/configuration_form_schema.tsx b/x-pack/plugins/index_management/public/application/components/mappings_editor/components/configuration_form/configuration_form_schema.tsx index b5fa5f25b865b..d8e3e8d5ae7c2 100644 --- a/x-pack/plugins/index_management/public/application/components/mappings_editor/components/configuration_form/configuration_form_schema.tsx +++ b/x-pack/plugins/index_management/public/application/components/mappings_editor/components/configuration_form/configuration_form_schema.tsx @@ -192,4 +192,12 @@ export const configurationFormSchema: FormSchema = { defaultValue: false, }, }, + _size: { + enabled: { + label: i18n.translate('xpack.idxMgmt.mappingsEditor.configuration.sizeLabel', { + defaultMessage: 'Index the _source field size in bytes', + }), + defaultValue: false, + }, + }, }; diff --git a/x-pack/plugins/index_management/public/application/components/mappings_editor/components/configuration_form/mapper_size_plugin_section.tsx b/x-pack/plugins/index_management/public/application/components/mappings_editor/components/configuration_form/mapper_size_plugin_section.tsx new file mode 100644 index 0000000000000..db2ded2e09990 --- /dev/null +++ b/x-pack/plugins/index_management/public/application/components/mappings_editor/components/configuration_form/mapper_size_plugin_section.tsx @@ -0,0 +1,47 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import React from 'react'; + +import { i18n } from '@kbn/i18n'; +import { FormattedMessage } from '@kbn/i18n-react'; +import { EuiLink, EuiCode } from '@elastic/eui'; + +import { documentationService } from '../../../../services/documentation'; +import { UseField, FormRow, ToggleField } from '../../shared_imports'; + +export const MapperSizePluginSection = () => { + return ( + + {i18n.translate('xpack.idxMgmt.mappingsEditor.sizeDocumentionLink', { + defaultMessage: 'Learn more.', + })} + + ), + _source: _source, + }} + /> + } + > + + + ); +}; diff --git a/x-pack/plugins/index_management/public/application/components/mappings_editor/components/load_mappings/load_from_json_button.tsx b/x-pack/plugins/index_management/public/application/components/mappings_editor/components/load_mappings/load_from_json_button.tsx index 701cd86510b5d..a875b9985a8f4 100644 --- a/x-pack/plugins/index_management/public/application/components/mappings_editor/components/load_mappings/load_from_json_button.tsx +++ b/x-pack/plugins/index_management/public/application/components/mappings_editor/components/load_mappings/load_from_json_button.tsx @@ -13,10 +13,12 @@ import { LoadMappingsProvider } from './load_mappings_provider'; interface Props { onJson(json: { [key: string]: any }): void; + /** List of plugins installed in the cluster nodes */ + esNodesPlugins: string[]; } -export const LoadMappingsFromJsonButton = ({ onJson }: Props) => ( - +export const LoadMappingsFromJsonButton = ({ onJson, esNodesPlugins }: Props) => ( + {(openModal) => ( {i18n.translate('xpack.idxMgmt.mappingsEditor.loadFromJsonButtonLabel', { diff --git a/x-pack/plugins/index_management/public/application/components/mappings_editor/components/load_mappings/load_mappings_provider.test.tsx b/x-pack/plugins/index_management/public/application/components/mappings_editor/components/load_mappings/load_mappings_provider.test.tsx index 2413df5e5d64d..8259c78b8e140 100644 --- a/x-pack/plugins/index_management/public/application/components/mappings_editor/components/load_mappings/load_mappings_provider.test.tsx +++ b/x-pack/plugins/index_management/public/application/components/mappings_editor/components/load_mappings/load_mappings_provider.test.tsx @@ -22,7 +22,7 @@ import { registerTestBed, TestBed } from '@kbn/test/jest'; import { LoadMappingsProvider } from './load_mappings_provider'; const ComponentToTest = ({ onJson }: { onJson: () => void }) => ( - + {(openModal) => (
"`; +exports[`ResetSessionPage renders as expected 1`] = `"ElasticMockedFonts

You do not have permission to access the requested page

Either go back to the previous page or log in as a different user.

"`; diff --git a/x-pack/plugins/security/server/config_deprecations.test.ts b/x-pack/plugins/security/server/config_deprecations.test.ts index 7a85e614e4b62..150e878f4297f 100644 --- a/x-pack/plugins/security/server/config_deprecations.test.ts +++ b/x-pack/plugins/security/server/config_deprecations.test.ts @@ -182,7 +182,7 @@ describe('Config Deprecations', () => { }, }; const { messages, migrated } = applyConfigDeprecations(cloneDeep(config)); - expect(migrated.security.showInsecureClusterWarning).not.toBeDefined(); + expect(migrated.security?.showInsecureClusterWarning).not.toBeDefined(); expect(migrated.xpack.security.showInsecureClusterWarning).toEqual(false); expect(messages).toMatchInlineSnapshot(` Array [ diff --git a/x-pack/plugins/security_solution/common/constants.ts b/x-pack/plugins/security_solution/common/constants.ts index 14e3c8cc95fe6..7fa387207e3ff 100644 --- a/x-pack/plugins/security_solution/common/constants.ts +++ b/x-pack/plugins/security_solution/common/constants.ts @@ -39,7 +39,7 @@ export const DEFAULT_APP_TIME_RANGE = 'securitySolution:timeDefaults' as const; export const DEFAULT_APP_REFRESH_INTERVAL = 'securitySolution:refreshIntervalDefaults' as const; export const DEFAULT_ALERTS_INDEX = '.alerts-security.alerts' as const; export const DEFAULT_SIGNALS_INDEX = '.siem-signals' as const; -export const DEFAULT_PREVIEW_INDEX = '.siem-preview-signals' as const; +export const DEFAULT_PREVIEW_INDEX = '.preview.alerts-security.alerts' as const; export const DEFAULT_LISTS_INDEX = '.lists' as const; export const DEFAULT_ITEMS_INDEX = '.items' as const; // The DEFAULT_MAX_SIGNALS value exists also in `x-pack/plugins/cases/common/constants.ts` @@ -256,8 +256,6 @@ export const DETECTION_ENGINE_PREPACKAGED_RULES_STATUS_URL = export const DETECTION_ENGINE_RULES_BULK_ACTION = `${DETECTION_ENGINE_RULES_URL}/_bulk_action` as const; export const DETECTION_ENGINE_RULES_PREVIEW = `${DETECTION_ENGINE_RULES_URL}/preview` as const; -export const DETECTION_ENGINE_RULES_PREVIEW_INDEX_URL = - `${DETECTION_ENGINE_RULES_PREVIEW}/index` as const; /** * Internal detection engine routes diff --git a/x-pack/plugins/security_solution/common/detection_engine/constants.ts b/x-pack/plugins/security_solution/common/detection_engine/constants.ts new file mode 100644 index 0000000000000..7f3c822800673 --- /dev/null +++ b/x-pack/plugins/security_solution/common/detection_engine/constants.ts @@ -0,0 +1,13 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +export enum RULE_PREVIEW_INVOCATION_COUNT { + HOUR = 20, + DAY = 24, + WEEK = 168, + MONTH = 30, +} diff --git a/x-pack/plugins/security_solution/common/detection_engine/schemas/request/rule_schemas.ts b/x-pack/plugins/security_solution/common/detection_engine/schemas/request/rule_schemas.ts index c5f4e5631e5c8..97079253606f1 100644 --- a/x-pack/plugins/security_solution/common/detection_engine/schemas/request/rule_schemas.ts +++ b/x-pack/plugins/security_solution/common/detection_engine/schemas/request/rule_schemas.ts @@ -370,6 +370,7 @@ export const previewRulesSchema = t.intersection([ createTypeSpecific, t.type({ invocationCount: t.number }), ]); +export type PreviewRulesSchema = t.TypeOf; type UpdateSchema = SharedUpdateSchema & T; export type EqlUpdateSchema = UpdateSchema>; diff --git a/x-pack/plugins/security_solution/common/detection_engine/schemas/response/error_schema.ts b/x-pack/plugins/security_solution/common/detection_engine/schemas/response/error_schema.ts index 9a4bd3c65c367..d6e1faa7a5180 100644 --- a/x-pack/plugins/security_solution/common/detection_engine/schemas/response/error_schema.ts +++ b/x-pack/plugins/security_solution/common/detection_engine/schemas/response/error_schema.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { NonEmptyString } from '@kbn/securitysolution-io-ts-types'; import * as t from 'io-ts'; import { rule_id, status_code, message } from '../common/schemas'; @@ -12,7 +13,9 @@ import { rule_id, status_code, message } from '../common/schemas'; // We use id: t.string intentionally and _never_ the id from global schemas as // sometimes echo back out the id that the user gave us and it is not guaranteed // to be a UUID but rather just a string -const partial = t.exact(t.partial({ id: t.string, rule_id })); +const partial = t.exact( + t.partial({ id: t.string, rule_id, list_id: NonEmptyString, item_id: NonEmptyString }) +); const required = t.exact( t.type({ error: t.type({ diff --git a/x-pack/plugins/security_solution/common/endpoint/data_loaders/index_endpoint_hosts.ts b/x-pack/plugins/security_solution/common/endpoint/data_loaders/index_endpoint_hosts.ts index ed75823cd30d3..be26f8496c5e9 100644 --- a/x-pack/plugins/security_solution/common/endpoint/data_loaders/index_endpoint_hosts.ts +++ b/x-pack/plugins/security_solution/common/endpoint/data_loaders/index_endpoint_hosts.ts @@ -97,7 +97,7 @@ export async function indexEndpointHostDocs({ client: Client; kbnClient: KbnClient; realPolicies: Record; - epmEndpointPackage: GetPackagesResponse['response'][0]; + epmEndpointPackage: GetPackagesResponse['items'][0]; metadataIndex: string; policyResponseIndex: string; enrollFleet: boolean; diff --git a/x-pack/plugins/security_solution/common/endpoint/data_loaders/setup_fleet_for_endpoint.ts b/x-pack/plugins/security_solution/common/endpoint/data_loaders/setup_fleet_for_endpoint.ts index 61f7123c36840..a236b56737e03 100644 --- a/x-pack/plugins/security_solution/common/endpoint/data_loaders/setup_fleet_for_endpoint.ts +++ b/x-pack/plugins/security_solution/common/endpoint/data_loaders/setup_fleet_for_endpoint.ts @@ -101,7 +101,7 @@ export const installOrUpgradeEndpointFleetPackage = async ( }) .catch(wrapErrorAndRejectPromise)) as AxiosResponse; - const bulkResp = installEndpointPackageResp.data.response; + const bulkResp = installEndpointPackageResp.data.items; if (bulkResp.length <= 0) { throw new EndpointDataLoadingError( diff --git a/x-pack/plugins/security_solution/common/endpoint/generate_data.ts b/x-pack/plugins/security_solution/common/endpoint/generate_data.ts index 9cbe1e19530ca..f9ecb2e018dff 100644 --- a/x-pack/plugins/security_solution/common/endpoint/generate_data.ts +++ b/x-pack/plugins/security_solution/common/endpoint/generate_data.ts @@ -1600,7 +1600,7 @@ export class EndpointDocGenerator extends BaseDataGenerator { /** * Generate an EPM Package for Endpoint */ - public generateEpmPackage(): GetPackagesResponse['response'][0] { + public generateEpmPackage(): GetPackagesResponse['items'][0] { return { id: this.seededUUIDv4(), name: 'endpoint', diff --git a/x-pack/plugins/security_solution/common/endpoint/index_data.ts b/x-pack/plugins/security_solution/common/endpoint/index_data.ts index 5bb3bd3dbae52..5c81196a3709c 100644 --- a/x-pack/plugins/security_solution/common/endpoint/index_data.ts +++ b/x-pack/plugins/security_solution/common/endpoint/index_data.ts @@ -124,13 +124,13 @@ export async function indexHostsAndAlerts( const getEndpointPackageInfo = async ( kbnClient: KbnClient -): Promise => { +): Promise => { const endpointPackage = ( (await kbnClient.request({ path: `${EPM_API_ROUTES.LIST_PATTERN}?category=security`, method: 'GET', })) as AxiosResponse - ).data.response.find((epmPackage) => epmPackage.name === 'endpoint'); + ).data.items.find((epmPackage) => epmPackage.name === 'endpoint'); if (!endpointPackage) { throw new Error('EPM Endpoint package was not found!'); diff --git a/x-pack/plugins/security_solution/common/search_strategy/security_solution/hosts/common/index.ts b/x-pack/plugins/security_solution/common/search_strategy/security_solution/hosts/common/index.ts index f6f5ad4cd23f1..8a9a047aab3fd 100644 --- a/x-pack/plugins/security_solution/common/search_strategy/security_solution/hosts/common/index.ts +++ b/x-pack/plugins/security_solution/common/search_strategy/security_solution/hosts/common/index.ts @@ -45,6 +45,7 @@ export interface HostItem { endpoint?: Maybe; host?: Maybe; lastSeen?: Maybe; + risk?: string; } export interface HostValue { diff --git a/x-pack/plugins/security_solution/common/search_strategy/security_solution/hosts/risk_score/index.test.ts b/x-pack/plugins/security_solution/common/search_strategy/security_solution/hosts/risk_score/index.test.ts new file mode 100644 index 0000000000000..8c58ccaabe8df --- /dev/null +++ b/x-pack/plugins/security_solution/common/search_strategy/security_solution/hosts/risk_score/index.test.ts @@ -0,0 +1,14 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { getHostRiskIndex } from '.'; + +describe('hosts risk search_strategy getHostRiskIndex', () => { + it('should properly return index if space is specified', () => { + expect(getHostRiskIndex('testName')).toEqual('ml_host_risk_score_latest_testName'); + }); +}); diff --git a/x-pack/plugins/security_solution/common/search_strategy/security_solution/hosts/risk_score/index.ts b/x-pack/plugins/security_solution/common/search_strategy/security_solution/hosts/risk_score/index.ts index 23cda0b68f038..4273c08c638f3 100644 --- a/x-pack/plugins/security_solution/common/search_strategy/security_solution/hosts/risk_score/index.ts +++ b/x-pack/plugins/security_solution/common/search_strategy/security_solution/hosts/risk_score/index.ts @@ -10,12 +10,13 @@ import type { IEsSearchRequest, IEsSearchResponse, } from '../../../../../../../../src/plugins/data/common'; +import { RISKY_HOSTS_INDEX_PREFIX } from '../../../../constants'; import { Inspect, Maybe, TimerangeInput } from '../../../common'; export interface HostsRiskScoreRequestOptions extends IEsSearchRequest { defaultIndex: string[]; factoryQueryType?: FactoryQueryTypes; - hostName?: string; + hostNames?: string[]; timerange?: TimerangeInput; } @@ -38,3 +39,7 @@ export interface RuleRisk { rule_name: string; rule_risk: string; } + +export const getHostRiskIndex = (spaceId: string): string => { + return `${RISKY_HOSTS_INDEX_PREFIX}${spaceId}`; +}; diff --git a/x-pack/plugins/security_solution/common/types/timeline/index.ts b/x-pack/plugins/security_solution/common/types/timeline/index.ts index 60fd126e6fd85..442986870ac94 100644 --- a/x-pack/plugins/security_solution/common/types/timeline/index.ts +++ b/x-pack/plugins/security_solution/common/types/timeline/index.ts @@ -633,7 +633,7 @@ export interface ColumnHeaderResult { category?: Maybe; columnHeaderType?: Maybe; description?: Maybe; - example?: Maybe; + example?: Maybe; indexes?: Maybe; id?: Maybe; name?: Maybe; diff --git a/x-pack/plugins/security_solution/common/types/timeline/store.ts b/x-pack/plugins/security_solution/common/types/timeline/store.ts index 1b32f12dafd5b..250ff061e81dd 100644 --- a/x-pack/plugins/security_solution/common/types/timeline/store.ts +++ b/x-pack/plugins/security_solution/common/types/timeline/store.ts @@ -39,7 +39,7 @@ export interface SortColumnTimeline { export interface TimelinePersistInput { columns: ColumnHeaderOptions[]; dataProviders?: DataProvider[]; - dataViewId: string; + dataViewId: string | null; // null if legacy pre-8.0 timeline dateRange?: { start: string; end: string; diff --git a/x-pack/plugins/security_solution/cypress/integration/detection_alerts/cti_enrichments.spec.ts b/x-pack/plugins/security_solution/cypress/integration/detection_alerts/cti_enrichments.spec.ts index 0e87378f4ef96..0e4dbc9a95f9c 100644 --- a/x-pack/plugins/security_solution/cypress/integration/detection_alerts/cti_enrichments.spec.ts +++ b/x-pack/plugins/security_solution/cypress/integration/detection_alerts/cti_enrichments.spec.ts @@ -75,7 +75,9 @@ describe('CTI Enrichment', () => { it('Displays persisted enrichments on the JSON view', () => { const expectedEnrichment = [ { - feed: {}, + feed: { + name: 'AbuseCH malware', + }, indicator: { first_seen: '2021-03-10T08:02:14.000Z', file: { @@ -113,6 +115,7 @@ describe('CTI Enrichment', () => { it('Displays threat indicator details on the threat intel tab', () => { const expectedThreatIndicatorData = [ + { field: 'feed.name', value: 'AbuseCH malware' }, { field: 'indicator.file.hash.md5', value: '9b6c3518a91d23ed77504b5416bfb5b3' }, { field: 'indicator.file.hash.sha256', @@ -173,6 +176,7 @@ describe('CTI Enrichment', () => { const indicatorMatchRuleEnrichment = { field: 'myhash.mysha256', value: 'a04ac6d98ad989312783d4fe3456c53730b212c79a426fb215708b6c6daa3de3', + feedName: 'AbuseCH malware', }; const investigationTimeEnrichment = { field: 'source.ip', @@ -188,7 +192,7 @@ describe('CTI Enrichment', () => { .should('exist') .should( 'have.text', - `${indicatorMatchRuleEnrichment.field} ${indicatorMatchRuleEnrichment.value}` + `${indicatorMatchRuleEnrichment.field} ${indicatorMatchRuleEnrichment.value} from ${indicatorMatchRuleEnrichment.feedName}` ); cy.get(`${INVESTIGATION_TIME_ENRICHMENT_SECTION} ${THREAT_DETAILS_ACCORDION}`) diff --git a/x-pack/plugins/security_solution/cypress/integration/detection_rules/threshold_rule.spec.ts b/x-pack/plugins/security_solution/cypress/integration/detection_rules/threshold_rule.spec.ts index 02d8837261f2f..81022a43ff683 100644 --- a/x-pack/plugins/security_solution/cypress/integration/detection_rules/threshold_rule.spec.ts +++ b/x-pack/plugins/security_solution/cypress/integration/detection_rules/threshold_rule.spec.ts @@ -174,7 +174,7 @@ describe('Detection rules, threshold', () => { cy.get(ALERT_GRID_CELL).contains(rule.name); }); - it('Preview results of keyword using "host.name"', () => { + it.skip('Preview results of keyword using "host.name"', () => { rule.index = [...rule.index, '.siem-signals*']; createCustomRuleActivated(getNewRule()); @@ -188,7 +188,7 @@ describe('Detection rules, threshold', () => { cy.get(PREVIEW_HEADER_SUBTITLE).should('have.text', '3 unique hits'); }); - it('Preview results of "ip" using "source.ip"', () => { + it.skip('Preview results of "ip" using "source.ip"', () => { const previewRule: ThresholdRule = { ...rule, thresholdField: 'source.ip', diff --git a/x-pack/plugins/security_solution/cypress/integration/hosts/hosts_risk_column.ts b/x-pack/plugins/security_solution/cypress/integration/hosts/hosts_risk_column.ts new file mode 100644 index 0000000000000..bb57a8973c8e6 --- /dev/null +++ b/x-pack/plugins/security_solution/cypress/integration/hosts/hosts_risk_column.ts @@ -0,0 +1,33 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { loginAndWaitForPage } from '../../tasks/login'; + +import { HOSTS_URL } from '../../urls/navigation'; +import { cleanKibana } from '../../tasks/common'; +import { esArchiverLoad, esArchiverUnload } from '../../tasks/es_archiver'; +import { TABLE_CELL } from '../../screens/alerts_details'; +import { kqlSearch } from '../../tasks/security_header'; + +describe('All hosts table', () => { + before(() => { + cleanKibana(); + esArchiverLoad('risky_hosts'); + }); + + after(() => { + esArchiverUnload('risky_hosts'); + }); + + it('it renders risk column', () => { + loginAndWaitForPage(HOSTS_URL); + kqlSearch('host.name: "siem-kibana" {enter}'); + + cy.get('[data-test-subj="tableHeaderCell_node.risk_4"]').should('exist'); + cy.get(`${TABLE_CELL} .euiTableCellContent`).eq(4).should('have.text', 'Low'); + }); +}); diff --git a/x-pack/plugins/security_solution/cypress/integration/hosts/risky_hosts_kpi.spec.ts b/x-pack/plugins/security_solution/cypress/integration/hosts/risky_hosts_kpi.spec.ts index 4f282e1e69d5c..602a9118128b5 100644 --- a/x-pack/plugins/security_solution/cypress/integration/hosts/risky_hosts_kpi.spec.ts +++ b/x-pack/plugins/security_solution/cypress/integration/hosts/risky_hosts_kpi.spec.ts @@ -8,13 +8,8 @@ import { loginAndWaitForPage } from '../../tasks/login'; import { HOSTS_URL } from '../../urls/navigation'; -import { cleanKibana } from '../../tasks/common'; describe('RiskyHosts KPI', () => { - before(() => { - cleanKibana(); - }); - it('it renders', () => { loginAndWaitForPage(HOSTS_URL); diff --git a/x-pack/plugins/security_solution/cypress/screens/create_new_rule.ts b/x-pack/plugins/security_solution/cypress/screens/create_new_rule.ts index aadaa5dfa0d88..a3e5e8af3f598 100644 --- a/x-pack/plugins/security_solution/cypress/screens/create_new_rule.ts +++ b/x-pack/plugins/security_solution/cypress/screens/create_new_rule.ts @@ -104,9 +104,9 @@ export const DEFINE_INDEX_INPUT = export const EQL_TYPE = '[data-test-subj="eqlRuleType"]'; -export const EQL_QUERY_INPUT = '[data-test-subj="eqlQueryBarTextInput"]'; +export const PREVIEW_HISTOGRAM = '[data-test-subj="preview-histogram-panel"]'; -export const EQL_QUERY_PREVIEW_HISTOGRAM = '[data-test-subj="queryPreviewEqlHistogram"]'; +export const EQL_QUERY_INPUT = '[data-test-subj="eqlQueryBarTextInput"]'; export const EQL_QUERY_VALIDATION_SPINNER = '[data-test-subj="eql-validation-loading"]'; @@ -170,7 +170,7 @@ export const RISK_OVERRIDE = export const RULES_CREATION_FORM = '[data-test-subj="stepDefineRule"]'; -export const RULES_CREATION_PREVIEW = '[data-test-subj="ruleCreationQueryPreview"]'; +export const RULES_CREATION_PREVIEW = '[data-test-subj="rule-preview"]'; export const RULE_DESCRIPTION_INPUT = '[data-test-subj="detectionEngineStepAboutRuleDescription"] [data-test-subj="input"]'; diff --git a/x-pack/plugins/security_solution/cypress/tasks/create_new_rule.ts b/x-pack/plugins/security_solution/cypress/tasks/create_new_rule.ts index 68449363b8643..538f95c3c0a80 100644 --- a/x-pack/plugins/security_solution/cypress/tasks/create_new_rule.ts +++ b/x-pack/plugins/security_solution/cypress/tasks/create_new_rule.ts @@ -33,7 +33,6 @@ import { DEFAULT_RISK_SCORE_INPUT, DEFINE_CONTINUE_BUTTON, EQL_QUERY_INPUT, - EQL_QUERY_PREVIEW_HISTOGRAM, EQL_QUERY_VALIDATION_SPINNER, EQL_TYPE, FALSE_POSITIVES_INPUT, @@ -92,6 +91,7 @@ import { EMAIL_CONNECTOR_USER_INPUT, EMAIL_CONNECTOR_PASSWORD_INPUT, EMAIL_CONNECTOR_SERVICE_SELECTOR, + PREVIEW_HISTOGRAM, } from '../screens/create_new_rule'; import { TOAST_ERROR } from '../screens/shared'; import { SERVER_SIDE_EVENT_COUNT } from '../screens/timeline'; @@ -324,12 +324,12 @@ export const fillDefineEqlRuleAndContinue = (rule: CustomRule) => { .find(QUERY_PREVIEW_BUTTON) .should('not.be.disabled') .click({ force: true }); - cy.get(EQL_QUERY_PREVIEW_HISTOGRAM) + cy.get(PREVIEW_HISTOGRAM) .invoke('text') .then((text) => { if (text !== 'Hits') { cy.get(RULES_CREATION_PREVIEW).find(QUERY_PREVIEW_BUTTON).click({ force: true }); - cy.get(EQL_QUERY_PREVIEW_HISTOGRAM).should('contain.text', 'Hits'); + cy.get(PREVIEW_HISTOGRAM).should('contain.text', 'Hits'); } }); cy.get(TOAST_ERROR).should('not.exist'); diff --git a/x-pack/plugins/security_solution/cypress/upgrade_integration/custom_query_rule.spec.ts b/x-pack/plugins/security_solution/cypress/upgrade_integration/detections/detection_rules/custom_query_rule.spec.ts similarity index 80% rename from x-pack/plugins/security_solution/cypress/upgrade_integration/custom_query_rule.spec.ts rename to x-pack/plugins/security_solution/cypress/upgrade_integration/detections/detection_rules/custom_query_rule.spec.ts index e4464ae43dd62..efc0d290ac728 100644 --- a/x-pack/plugins/security_solution/cypress/upgrade_integration/custom_query_rule.spec.ts +++ b/x-pack/plugins/security_solution/cypress/upgrade_integration/detections/detection_rules/custom_query_rule.spec.ts @@ -4,6 +4,7 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ +import semver from 'semver'; import { DESTINATION_IP, HOST_NAME, @@ -14,8 +15,8 @@ import { SEVERITY, SOURCE_IP, USER_NAME, -} from '../screens/alerts'; -import { SERVER_SIDE_EVENT_COUNT } from '../screens/alerts_detection_rules'; +} from '../../../screens/alerts'; +import { SERVER_SIDE_EVENT_COUNT } from '../../../screens/alerts_detection_rules'; import { ADDITIONAL_LOOK_BACK_DETAILS, ABOUT_DETAILS, @@ -31,13 +32,16 @@ import { SCHEDULE_DETAILS, SEVERITY_DETAILS, TIMELINE_TEMPLATE_DETAILS, -} from '../screens/rule_details'; +} from '../../../screens/rule_details'; -import { waitForPageToBeLoaded } from '../tasks/common'; -import { waitForRulesTableToBeLoaded, goToTheRuleDetailsOf } from '../tasks/alerts_detection_rules'; -import { loginAndWaitForPage } from '../tasks/login'; +import { waitForPageToBeLoaded } from '../../../tasks/common'; +import { + waitForRulesTableToBeLoaded, + goToTheRuleDetailsOf, +} from '../../../tasks/alerts_detection_rules'; +import { loginAndWaitForPage } from '../../../tasks/login'; -import { DETECTIONS_RULE_MANAGEMENT_URL } from '../urls/navigation'; +import { DETECTIONS_RULE_MANAGEMENT_URL } from '../../../urls/navigation'; const EXPECTED_NUMBER_OF_ALERTS = '1'; @@ -63,8 +67,8 @@ const rule = { severity: 'Low', riskScore: '7', timelineTemplate: 'none', - runsEvery: '10s', - lookBack: '179999990s', + runsEvery: '24h', + lookBack: '49976h', timeline: 'None', }; @@ -100,10 +104,16 @@ describe('After an upgrade, the custom query rule', () => { }); it('Displays the alert details at the tgrid', () => { + let expectedReason; + if (semver.gt(Cypress.env('ORIGINAL_VERSION'), '7.15.0')) { + expectedReason = alert.reason; + } else { + expectedReason = '-'; + } cy.get(RULE_NAME).should('have.text', alert.rule); cy.get(SEVERITY).should('have.text', alert.severity); cy.get(RISK_SCORE).should('have.text', alert.riskScore); - cy.get(REASON).should('have.text', alert.reason).type('{rightarrow}'); + cy.get(REASON).should('have.text', expectedReason).type('{rightarrow}'); cy.get(HOST_NAME).should('have.text', alert.hostName); cy.get(USER_NAME).should('have.text', alert.username); cy.get(PROCESS_NAME).should('have.text', alert.processName); diff --git a/x-pack/plugins/security_solution/cypress/upgrade_integration/threshold_rule.spec.ts b/x-pack/plugins/security_solution/cypress/upgrade_integration/detections/detection_rules/threshold_rule.spec.ts similarity index 80% rename from x-pack/plugins/security_solution/cypress/upgrade_integration/threshold_rule.spec.ts rename to x-pack/plugins/security_solution/cypress/upgrade_integration/detections/detection_rules/threshold_rule.spec.ts index b6dbcd0e3232c..16949c9b34c63 100644 --- a/x-pack/plugins/security_solution/cypress/upgrade_integration/threshold_rule.spec.ts +++ b/x-pack/plugins/security_solution/cypress/upgrade_integration/detections/detection_rules/threshold_rule.spec.ts @@ -4,9 +4,9 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - -import { HOST_NAME, REASON, RISK_SCORE, RULE_NAME, SEVERITY } from '../screens/alerts'; -import { SERVER_SIDE_EVENT_COUNT } from '../screens/alerts_detection_rules'; +import semver from 'semver'; +import { HOST_NAME, REASON, RISK_SCORE, RULE_NAME, SEVERITY } from '../../../screens/alerts'; +import { SERVER_SIDE_EVENT_COUNT } from '../../../screens/alerts_detection_rules'; import { ADDITIONAL_LOOK_BACK_DETAILS, ABOUT_DETAILS, @@ -23,14 +23,17 @@ import { SEVERITY_DETAILS, THRESHOLD_DETAILS, TIMELINE_TEMPLATE_DETAILS, -} from '../screens/rule_details'; +} from '../../../screens/rule_details'; -import { expandFirstAlert } from '../tasks/alerts'; -import { waitForPageToBeLoaded } from '../tasks/common'; -import { waitForRulesTableToBeLoaded, goToRuleDetails } from '../tasks/alerts_detection_rules'; -import { loginAndWaitForPage } from '../tasks/login'; +import { expandFirstAlert } from '../../../tasks/alerts'; +import { waitForPageToBeLoaded } from '../../../tasks/common'; +import { + goToTheRuleDetailsOf, + waitForRulesTableToBeLoaded, +} from '../../../tasks/alerts_detection_rules'; +import { loginAndWaitForPage } from '../../../tasks/login'; -import { DETECTIONS_RULE_MANAGEMENT_URL } from '../urls/navigation'; +import { DETECTIONS_RULE_MANAGEMENT_URL } from '../../../urls/navigation'; import { OVERVIEW_HOST_NAME, OVERVIEW_RISK_SCORE, @@ -40,7 +43,7 @@ import { OVERVIEW_THRESHOLD_COUNT, OVERVIEW_THRESHOLD_VALUE, SUMMARY_VIEW, -} from '../screens/alerts_details'; +} from '../../../screens/alerts_details'; const EXPECTED_NUMBER_OF_ALERTS = '1'; @@ -61,8 +64,8 @@ const rule = { severity: 'Medium', riskScore: '17', timelineTemplate: 'none', - runsEvery: '60s', - lookBack: '2999999m', + runsEvery: '24h', + lookBack: '49976h', timeline: 'None', thresholdField: 'host.name', threholdValue: '1', @@ -72,7 +75,7 @@ describe('After an upgrade, the threshold rule', () => { before(() => { loginAndWaitForPage(DETECTIONS_RULE_MANAGEMENT_URL); waitForRulesTableToBeLoaded(); - goToRuleDetails(); + goToTheRuleDetailsOf(rule.name); waitForPageToBeLoaded(); }); @@ -104,10 +107,16 @@ describe('After an upgrade, the threshold rule', () => { }); it('Displays the alert details in the TGrid', () => { + let expectedReason; + if (semver.gt(Cypress.env('ORIGINAL_VERSION'), '7.15.0')) { + expectedReason = alert.reason; + } else { + expectedReason = '-'; + } cy.get(RULE_NAME).should('have.text', alert.rule); cy.get(SEVERITY).should('have.text', alert.severity); cy.get(RISK_SCORE).should('have.text', alert.riskScore); - cy.get(REASON).should('have.text', alert.reason); + cy.get(REASON).should('have.text', expectedReason); cy.get(HOST_NAME).should('have.text', alert.hostName); }); diff --git a/x-pack/plugins/security_solution/cypress/upgrade_integration/import_case.spec.ts b/x-pack/plugins/security_solution/cypress/upgrade_integration/threat_hunting/cases/import_case.spec.ts similarity index 90% rename from x-pack/plugins/security_solution/cypress/upgrade_integration/import_case.spec.ts rename to x-pack/plugins/security_solution/cypress/upgrade_integration/threat_hunting/cases/import_case.spec.ts index eb72dea9be7e8..e97cebeff00b5 100644 --- a/x-pack/plugins/security_solution/cypress/upgrade_integration/import_case.spec.ts +++ b/x-pack/plugins/security_solution/cypress/upgrade_integration/threat_hunting/cases/import_case.spec.ts @@ -15,7 +15,7 @@ import { ALL_CASES_OPEN_CASES_STATS, ALL_CASES_REPORTER, ALL_CASES_IN_PROGRESS_STATUS, -} from '../screens/all_cases'; +} from '../../../screens/all_cases'; import { CASES_TAGS, CASE_CONNECTOR, @@ -25,16 +25,19 @@ import { CASE_IN_PROGRESS_STATUS, CASE_SWITCH, CASE_USER_ACTION, -} from '../screens/case_details'; -import { CASES_PAGE } from '../screens/kibana_navigation'; +} from '../../../screens/case_details'; +import { CASES_PAGE } from '../../../screens/kibana_navigation'; -import { goToCaseDetails } from '../tasks/all_cases'; -import { deleteCase } from '../tasks/case_details'; -import { navigateFromKibanaCollapsibleTo, openKibanaNavigation } from '../tasks/kibana_navigation'; -import { loginAndWaitForPageWithoutDateRange } from '../tasks/login'; -import { importCase } from '../tasks/saved_objects'; +import { goToCaseDetails } from '../../../tasks/all_cases'; +import { deleteCase } from '../../../tasks/case_details'; +import { + navigateFromKibanaCollapsibleTo, + openKibanaNavigation, +} from '../../../tasks/kibana_navigation'; +import { loginAndWaitForPageWithoutDateRange } from '../../../tasks/login'; +import { importCase } from '../../../tasks/saved_objects'; -import { KIBANA_SAVED_OBJECTS } from '../urls/navigation'; +import { KIBANA_SAVED_OBJECTS } from '../../../urls/navigation'; const CASE_NDJSON = '7_16_case.ndjson'; const importedCase = { diff --git a/x-pack/plugins/security_solution/cypress/upgrade_integration/import_timeline.spec.ts b/x-pack/plugins/security_solution/cypress/upgrade_integration/threat_hunting/timeline/import_timeline.spec.ts similarity index 93% rename from x-pack/plugins/security_solution/cypress/upgrade_integration/import_timeline.spec.ts rename to x-pack/plugins/security_solution/cypress/upgrade_integration/threat_hunting/timeline/import_timeline.spec.ts index f3b3f14e9c260..253a1c9c59b0b 100644 --- a/x-pack/plugins/security_solution/cypress/upgrade_integration/import_timeline.spec.ts +++ b/x-pack/plugins/security_solution/cypress/upgrade_integration/threat_hunting/timeline/import_timeline.spec.ts @@ -28,7 +28,7 @@ import { TIMELINE_QUERY, TIMELINE_TITLE, USER_KPI, -} from '../screens/timeline'; +} from '../../../screens/timeline'; import { NOTE, TIMELINES_USERNAME, @@ -36,19 +36,19 @@ import { TIMELINES_DESCRIPTION, TIMELINES_NOTES_COUNT, TIMELINES_PINNED_EVENT_COUNT, -} from '../screens/timelines'; +} from '../../../screens/timelines'; -import { loginAndWaitForPageWithoutDateRange } from '../tasks/login'; +import { loginAndWaitForPageWithoutDateRange } from '../../../tasks/login'; import { closeTimeline, deleteTimeline, goToCorrelationTab, goToNotesTab, goToPinnedTab, -} from '../tasks/timeline'; -import { expandNotes, importTimeline, openTimeline } from '../tasks/timelines'; +} from '../../../tasks/timeline'; +import { expandNotes, importTimeline, openTimeline } from '../../../tasks/timelines'; -import { TIMELINES_URL } from '../urls/navigation'; +import { TIMELINES_URL } from '../../../urls/navigation'; const timeline = '7_15_timeline.ndjson'; const username = 'elastic'; @@ -64,7 +64,6 @@ const timelineDetails = { }; const detectionAlert = { - timestamp: 'Nov 17, 2021 @ 09:36:25.499', message: '—', eventCategory: 'file', eventAction: 'initial_scan', @@ -149,7 +148,6 @@ describe('Import timeline after upgrade', () => { cy.get(NOTES_TAB_BUTTON).should('have.text', timelineDetails.notesTab); cy.get(PINNED_TAB_BUTTON).should('have.text', timelineDetails.pinnedTab); - cy.get(QUERY_EVENT_TABLE_CELL).eq(0).should('contain', detectionAlert.timestamp); cy.get(QUERY_EVENT_TABLE_CELL).eq(1).should('contain', detectionAlert.message); cy.get(QUERY_EVENT_TABLE_CELL).eq(2).should('contain', detectionAlert.eventCategory); cy.get(QUERY_EVENT_TABLE_CELL).eq(3).should('contain', detectionAlert.eventAction); @@ -196,7 +194,6 @@ describe('Import timeline after upgrade', () => { it('Displays the correct timeline details inside the pinned tab', () => { goToPinnedTab(); - cy.get(PINNED_EVENT_TABLE_CELL).eq(0).should('contain', detectionAlert.timestamp); cy.get(PINNED_EVENT_TABLE_CELL).eq(1).should('contain', detectionAlert.message); cy.get(PINNED_EVENT_TABLE_CELL).eq(2).should('contain', detectionAlert.eventCategory); cy.get(PINNED_EVENT_TABLE_CELL).eq(3).should('contain', detectionAlert.eventAction); diff --git a/x-pack/plugins/security_solution/package.json b/x-pack/plugins/security_solution/package.json index 371ac66004f48..821550f21919a 100644 --- a/x-pack/plugins/security_solution/package.json +++ b/x-pack/plugins/security_solution/package.json @@ -19,6 +19,7 @@ "cypress:run-as-ci": "node --max-old-space-size=2048 ../../../scripts/functional_tests --config ../../test/security_solution_cypress/cli_config.ts", "cypress:run-as-ci:firefox": "node --max-old-space-size=2048 ../../../scripts/functional_tests --config ../../test/security_solution_cypress/config.firefox.ts", "cypress:run:upgrade": "yarn cypress:run:reporter --browser chrome --config integrationFolder=./cypress/upgrade_integration", + "cypress:run:upgrade:old": "yarn cypress:run:reporter --browser chrome --config integrationFolder=./cypress/upgrade_integration --spec ./cypress/upgrade_integration/threat_hunting/**/*.spec.ts,./cypress/upgrade_integration/detections/**/custom_query_rule.spec.ts; status=$?; yarn junit:merge && exit $status", "junit:merge": "../../../node_modules/.bin/mochawesome-merge ../../../target/kibana-security-solution/cypress/results/mochawesome*.json > ../../../target/kibana-security-solution/cypress/results/output.json && ../../../node_modules/.bin/marge ../../../target/kibana-security-solution/cypress/results/output.json --reportDir ../../../target/kibana-security-solution/cypress/results && mkdir -p ../../../target/junit && cp ../../../target/kibana-security-solution/cypress/results/*.xml ../../../target/junit/", "test:generate": "node scripts/endpoint/resolver_generator" } diff --git a/x-pack/plugins/security_solution/public/app/home/global_header/index.test.tsx b/x-pack/plugins/security_solution/public/app/home/global_header/index.test.tsx index c16e77e9182f2..cfcf5307de8d4 100644 --- a/x-pack/plugins/security_solution/public/app/home/global_header/index.test.tsx +++ b/x-pack/plugins/security_solution/public/app/home/global_header/index.test.tsx @@ -12,6 +12,7 @@ import { SecurityPageName } from '../../../../common/constants'; import { createSecuritySolutionStorageMock, mockGlobalState, + mockIndexPattern, SUB_PLUGINS_REDUCER, TestProviders, } from '../../../common/mock'; @@ -36,6 +37,10 @@ jest.mock('../../../common/lib/kibana', () => { }; }); +jest.mock('../../../common/containers/source', () => ({ + useFetchIndex: () => [false, { indicesExist: true, indexPatterns: mockIndexPattern }], +})); + jest.mock('react-reverse-portal', () => ({ InPortal: ({ children }: { children: React.ReactNode }) => <>{children}, OutPortal: ({ children }: { children: React.ReactNode }) => <>{children}, diff --git a/x-pack/plugins/security_solution/public/app/home/template_wrapper/bottom_bar/index.tsx b/x-pack/plugins/security_solution/public/app/home/template_wrapper/bottom_bar/index.tsx index c283bb10c7928..6d09f369be044 100644 --- a/x-pack/plugins/security_solution/public/app/home/template_wrapper/bottom_bar/index.tsx +++ b/x-pack/plugins/security_solution/public/app/home/template_wrapper/bottom_bar/index.tsx @@ -24,10 +24,10 @@ export const SecuritySolutionBottomBar = React.memo( ({ onAppLeave }: { onAppLeave: (handler: AppLeaveHandler) => void }) => { const [showTimeline] = useShowTimeline(); - const { indicesExist } = useSourcererDataView(SourcererScopeName.timeline); - useResolveRedirect(); + const { indicesExist, dataViewId } = useSourcererDataView(SourcererScopeName.timeline); - return indicesExist && showTimeline ? ( + useResolveRedirect(); + return (indicesExist || dataViewId === null) && showTimeline ? ( <> diff --git a/x-pack/plugins/security_solution/public/cases/pages/index.tsx b/x-pack/plugins/security_solution/public/cases/pages/index.tsx index 751e8fde530ba..e6c8e1f6f8c4f 100644 --- a/x-pack/plugins/security_solution/public/cases/pages/index.tsx +++ b/x-pack/plugins/security_solution/public/cases/pages/index.tsx @@ -98,7 +98,7 @@ const CaseContainerComponent: React.FC = () => { timelineActions.createTimeline({ id: TimelineId.casePage, columns: [], - dataViewId: '', + dataViewId: null, indexNames: [], expandedDetail: {}, show: false, diff --git a/x-pack/plugins/security_solution/public/common/components/event_details/alert_summary_view.test.tsx b/x-pack/plugins/security_solution/public/common/components/event_details/alert_summary_view.test.tsx index 7e1e71a01642f..c397ac313c48c 100644 --- a/x-pack/plugins/security_solution/public/common/components/event_details/alert_summary_view.test.tsx +++ b/x-pack/plugins/security_solution/public/common/components/event_details/alert_summary_view.test.tsx @@ -30,6 +30,8 @@ const props = { browserFields: mockBrowserFields, eventId: '5d1d53da502f56aacc14c3cb5c669363d102b31f99822e5d369d4804ed370a31', timelineId: 'detections-page', + title: '', + goToTable: jest.fn(), }; describe('AlertSummaryView', () => { diff --git a/x-pack/plugins/security_solution/public/common/components/event_details/alert_summary_view.tsx b/x-pack/plugins/security_solution/public/common/components/event_details/alert_summary_view.tsx index b42a0425355cc..c30837dc6eca8 100644 --- a/x-pack/plugins/security_solution/public/common/components/event_details/alert_summary_view.tsx +++ b/x-pack/plugins/security_solution/public/common/components/event_details/alert_summary_view.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import { EuiBasicTableColumn, EuiSpacer } from '@elastic/eui'; +import { EuiBasicTableColumn } from '@elastic/eui'; import React, { useMemo } from 'react'; import { BrowserFields } from '../../../../common/search_strategy/index_fields'; @@ -60,18 +60,21 @@ const AlertSummaryViewComponent: React.FC<{ eventId: string; isDraggable?: boolean; timelineId: string; - title?: string; -}> = ({ browserFields, data, eventId, isDraggable, timelineId, title }) => { + title: string; + goToTable: () => void; +}> = ({ browserFields, data, eventId, isDraggable, timelineId, title, goToTable }) => { const summaryRows = useMemo( () => getSummaryRows({ browserFields, data, eventId, isDraggable, timelineId }), [browserFields, data, eventId, isDraggable, timelineId] ); return ( - <> - - - + ); }; diff --git a/x-pack/plugins/security_solution/public/common/components/event_details/event_details.test.tsx b/x-pack/plugins/security_solution/public/common/components/event_details/event_details.test.tsx index 37ca3b0b897a6..14910c77d198c 100644 --- a/x-pack/plugins/security_solution/public/common/components/event_details/event_details.test.tsx +++ b/x-pack/plugins/security_solution/public/common/components/event_details/event_details.test.tsx @@ -48,6 +48,8 @@ describe('EventDetails', () => { timelineId: 'test', eventView: EventsViewType.summaryView, hostRisk: { fields: [], loading: true }, + indexName: 'test', + handleOnEventClosed: jest.fn(), rawEventData, }; diff --git a/x-pack/plugins/security_solution/public/common/components/event_details/event_details.tsx b/x-pack/plugins/security_solution/public/common/components/event_details/event_details.tsx index 0fe48d5a998ea..08f97ab7d1bc7 100644 --- a/x-pack/plugins/security_solution/public/common/components/event_details/event_details.tsx +++ b/x-pack/plugins/security_solution/public/common/components/event_details/event_details.tsx @@ -6,6 +6,7 @@ */ import { + EuiHorizontalRule, EuiTabbedContent, EuiTabbedContentTab, EuiSpacer, @@ -39,7 +40,9 @@ import { EnrichmentRangePicker } from './cti_details/enrichment_range_picker'; import { Reason } from './reason'; import { InvestigationGuideView } from './investigation_guide_view'; + import { HostRisk } from '../../containers/hosts_risk/use_hosts_risk_score'; +import { Overview } from './overview'; type EventViewTab = EuiTabbedContentTab; @@ -59,12 +62,14 @@ interface Props { browserFields: BrowserFields; data: TimelineEventsDetailsItem[]; id: string; + indexName: string; isAlert: boolean; isDraggable?: boolean; rawEventData: object | undefined; timelineTabType: TimelineTabs | 'flyout'; timelineId: string; hostRisk: HostRisk | null; + handleOnEventClosed: () => void; } export const Indent = styled.div` @@ -105,18 +110,21 @@ const EventDetailsComponent: React.FC = ({ browserFields, data, id, + indexName, isAlert, isDraggable, rawEventData, timelineId, timelineTabType, hostRisk, + handleOnEventClosed, }) => { const [selectedTabId, setSelectedTabId] = useState(EventsViewType.summaryView); const handleTabClick = useCallback( (tab: EuiTabbedContentTab) => setSelectedTabId(tab.id as EventViewId), - [setSelectedTabId] + [] ); + const goToTableTab = useCallback(() => setSelectedTabId(EventsViewType.tableView), []); const eventFields = useMemo(() => getEnrichmentFields(data), [data]); const existingEnrichments = useMemo( @@ -152,7 +160,19 @@ const EventDetailsComponent: React.FC = ({ name: i18n.OVERVIEW, content: ( <> + + + + = ({ timelineId, title: i18n.DUCOMENT_SUMMARY, }} + goToTable={goToTableTab} /> {(enrichmentCount > 0 || hostRisk) && ( @@ -188,8 +209,9 @@ const EventDetailsComponent: React.FC = ({ } : undefined, [ - isAlert, id, + indexName, + isAlert, data, browserFields, isDraggable, @@ -198,6 +220,8 @@ const EventDetailsComponent: React.FC = ({ allEnrichments, isEnrichmentsLoading, hostRisk, + goToTableTab, + handleOnEventClosed, ] ); diff --git a/x-pack/plugins/security_solution/public/common/components/event_details/get_alert_summary_rows.tsx b/x-pack/plugins/security_solution/public/common/components/event_details/get_alert_summary_rows.tsx index 4af444c2ab8ad..0bf404fe51e39 100644 --- a/x-pack/plugins/security_solution/public/common/components/event_details/get_alert_summary_rows.tsx +++ b/x-pack/plugins/security_solution/public/common/components/event_details/get_alert_summary_rows.tsx @@ -5,53 +5,31 @@ * 2.0. */ -import { get, getOr, find, isEmpty } from 'lodash/fp'; +import { getOr, find, isEmpty } from 'lodash/fp'; import * as i18n from './translations'; import { BrowserFields } from '../../../../common/search_strategy/index_fields'; import { - ALERTS_HEADERS_RISK_SCORE, - ALERTS_HEADERS_RULE, - ALERTS_HEADERS_SEVERITY, ALERTS_HEADERS_THRESHOLD_CARDINALITY, ALERTS_HEADERS_THRESHOLD_COUNT, ALERTS_HEADERS_THRESHOLD_TERMS, ALERTS_HEADERS_RULE_NAME, - SIGNAL_STATUS, ALERTS_HEADERS_TARGET_IMPORT_HASH, - TIMESTAMP, ALERTS_HEADERS_RULE_DESCRIPTION, } from '../../../detections/components/alerts_table/translations'; import { AGENT_STATUS_FIELD_NAME, IP_FIELD_TYPE, - SIGNAL_RULE_NAME_FIELD_NAME, } from '../../../timelines/components/timeline/body/renderers/constants'; import { DESTINATION_IP_FIELD_NAME, SOURCE_IP_FIELD_NAME } from '../../../network/components/ip'; -import { SummaryRow } from './helpers'; +import { getEnrichedFieldInfo, SummaryRow } from './helpers'; +import { EventSummaryField } from './types'; import { TimelineEventsDetailsItem } from '../../../../common/search_strategy/timeline'; import { isAlertFromEndpointEvent } from '../../utils/endpoint_alert_check'; import { EventCode } from '../../../../common/ecs/event'; -interface EventSummaryField { - id: string; - label?: string; - linkField?: string; - fieldType?: string; - overrideField?: string; -} - const defaultDisplayFields: EventSummaryField[] = [ - { id: 'kibana.alert.workflow_status', label: SIGNAL_STATUS }, - { id: '@timestamp', label: TIMESTAMP }, - { - id: SIGNAL_RULE_NAME_FIELD_NAME, - linkField: 'kibana.alert.rule.uuid', - label: ALERTS_HEADERS_RULE, - }, - { id: 'kibana.alert.rule.severity', label: ALERTS_HEADERS_SEVERITY }, - { id: 'kibana.alert.rule.risk_score', label: ALERTS_HEADERS_RISK_SCORE }, { id: 'host.name' }, { id: 'agent.id', overrideField: AGENT_STATUS_FIELD_NAME, label: i18n.AGENT_STATUS }, { id: 'user.name' }, @@ -151,50 +129,34 @@ export const getSummaryRows = ({ const tableFields = getEventFieldsToDisplay({ eventCategory, eventCode }); return data != null - ? tableFields.reduce((acc, item) => { - const initialDescription = { - contextId: timelineId, - eventId, - isDraggable, - value: null, - fieldType: 'string', - linkValue: undefined, - timelineId, - }; - const field = data.find((d) => d.field === item.id); - if (!field || isEmpty(field?.values)) { + ? tableFields.reduce((acc, field) => { + const item = data.find((d) => d.field === field.id); + if (!item || isEmpty(item?.values)) { return acc; } const linkValueField = - item.linkField != null && data.find((d) => d.field === item.linkField); - const linkValue = getOr(null, 'originalValue.0', linkValueField); - const value = getOr(null, 'originalValue.0', field); - const category = field.category ?? ''; - const fieldName = field.field ?? ''; - - const browserField = get([category, 'fields', fieldName], browserFields); + field.linkField != null && data.find((d) => d.field === field.linkField); const description = { - ...initialDescription, - data: { - field: field.field, - format: browserField?.format ?? '', - type: browserField?.type ?? '', - isObjectArray: field.isObjectArray, - ...(item.overrideField ? { field: item.overrideField } : {}), - }, - values: field.values, - linkValue: linkValue ?? undefined, - fieldFromBrowserField: browserField, + ...getEnrichedFieldInfo({ + item, + linkValueField: linkValueField || undefined, + contextId: timelineId, + timelineId, + browserFields, + eventId, + field, + }), + isDraggable, }; - if (item.id === 'agent.id' && !isAlertFromEndpointEvent({ data })) { + if (field.id === 'agent.id' && !isAlertFromEndpointEvent({ data })) { return acc; } - if (item.id === 'kibana.alert.threshold_result.terms') { + if (field.id === 'kibana.alert.threshold_result.terms') { try { - const terms = getOr(null, 'originalValue', field); + const terms = getOr(null, 'originalValue', item); const parsedValue = terms.map((term: string) => JSON.parse(term)); const thresholdTerms = (parsedValue ?? []).map( (entry: { field: string; value: string }) => { @@ -213,8 +175,9 @@ export const getSummaryRows = ({ } } - if (item.id === 'kibana.alert.threshold_result.cardinality') { + if (field.id === 'kibana.alert.threshold_result.cardinality') { try { + const value = getOr(null, 'originalValue.0', field); const parsedValue = JSON.parse(value); return [ ...acc, @@ -234,7 +197,7 @@ export const getSummaryRows = ({ return [ ...acc, { - title: item.label ?? item.id, + title: field.label ?? field.id, description, }, ]; diff --git a/x-pack/plugins/security_solution/public/common/components/event_details/helpers.tsx b/x-pack/plugins/security_solution/public/common/components/event_details/helpers.tsx index 648bc96b5c9e7..dcca42f2a1df7 100644 --- a/x-pack/plugins/security_solution/public/common/components/event_details/helpers.tsx +++ b/x-pack/plugins/security_solution/public/common/components/event_details/helpers.tsx @@ -22,7 +22,8 @@ import { DEFAULT_DATE_COLUMN_MIN_WIDTH, DEFAULT_COLUMN_MIN_WIDTH, } from '../../../timelines/components/timeline/body/constants'; -import { FieldsData } from './types'; +import type { TimelineEventsDetailsItem } from '../../../../common/search_strategy/timeline'; +import type { EnrichedFieldInfo, EventSummaryField } from './types'; import * as i18n from './translations'; import { ColumnHeaderOptions } from '../../../../common/types'; @@ -56,14 +57,8 @@ export interface Item { export interface AlertSummaryRow { title: string; - description: { - data: FieldsData; - eventId: string; + description: EnrichedFieldInfo & { isDraggable?: boolean; - fieldFromBrowserField?: BrowserField; - linkValue: string | undefined; - timelineId: string; - values: string[] | null | undefined; }; } @@ -232,3 +227,47 @@ export const getSummaryColumns = ( }, ]; }; + +export function getEnrichedFieldInfo({ + browserFields, + contextId, + eventId, + field, + item, + linkValueField, + timelineId, +}: { + browserFields: BrowserFields; + contextId: string; + item: TimelineEventsDetailsItem; + eventId: string; + field?: EventSummaryField; + timelineId: string; + linkValueField?: TimelineEventsDetailsItem; +}): EnrichedFieldInfo { + const fieldInfo = { + contextId, + eventId, + fieldType: 'string', + linkValue: undefined, + timelineId, + }; + const linkValue = getOr(null, 'originalValue.0', linkValueField); + const category = item.category ?? ''; + const fieldName = item.field ?? ''; + + const browserField = get([category, 'fields', fieldName], browserFields); + const overrideField = field?.overrideField; + return { + ...fieldInfo, + data: { + field: overrideField ?? fieldName, + format: browserField?.format ?? '', + type: browserField?.type ?? '', + isObjectArray: item.isObjectArray, + }, + values: item.values, + linkValue: linkValue ?? undefined, + fieldFromBrowserField: browserField, + }; +} diff --git a/x-pack/plugins/security_solution/public/common/components/event_details/overview/__snapshots__/index.test.tsx.snap b/x-pack/plugins/security_solution/public/common/components/event_details/overview/__snapshots__/index.test.tsx.snap new file mode 100644 index 0000000000000..4e62766fc1477 --- /dev/null +++ b/x-pack/plugins/security_solution/public/common/components/event_details/overview/__snapshots__/index.test.tsx.snap @@ -0,0 +1,311 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Event Details Overview Cards renders rows and spacers correctly 1`] = ` + + .c6 { + display: -webkit-box; + display: -webkit-flex; + display: -ms-flexbox; + display: flex; +} + +.c6:focus-within .timelines__hoverActionButton, +.c6:focus-within .securitySolution__hoverActionButton { + opacity: 1; +} + +.c6:hover .timelines__hoverActionButton, +.c6:hover .securitySolution__hoverActionButton { + opacity: 1; +} + +.c6 .timelines__hoverActionButton, +.c6 .securitySolution__hoverActionButton { + opacity: 0; +} + +.c6 .timelines__hoverActionButton:focus, +.c6 .securitySolution__hoverActionButton:focus { + opacity: 1; +} + +.c3 { + text-transform: capitalize; +} + +.c5 { + width: 0; + -webkit-transform: translate(6px); + -ms-transform: translate(6px); + transform: translate(6px); + -webkit-transition: -webkit-transform 50ms ease-in-out; + -webkit-transition: transform 50ms ease-in-out; + transition: transform 50ms ease-in-out; + margin-left: 8px; +} + +.c1.c1.c1 { + background-color: #25262e; + padding: 8px; + height: 78px; +} + +.c1 .hoverActions-active .timelines__hoverActionButton, +.c1 .hoverActions-active .securitySolution__hoverActionButton { + opacity: 1; +} + +.c1:hover .timelines__hoverActionButton, +.c1:hover .securitySolution__hoverActionButton { + opacity: 1; +} + +.c1:hover .c4 { + width: auto; + -webkit-transform: translate(0); + -ms-transform: translate(0); + transform: translate(0); +} + +.c2 { + display: -webkit-box; + -webkit-line-clamp: 2; + -webkit-box-orient: vertical; + overflow: hidden; +} + +.c0 { + -webkit-box-flex: 0; + -webkit-flex-grow: 0; + -ms-flex-positive: 0; + flex-grow: 0; +} + +
+
+
+
+
+ Status +
+
+
+
+
+
+ +
+
+
+
+
+
+

+ You are in a dialog, containing options for field kibana.alert.workflow_status. Press tab to navigate options. Press escape to exit. +

+
+ Filter button +
+
+ Filter out button +
+
+ Overflow button +
+
+
+
+
+
+
+
+
+
+ Risk Score +
+
+
+
+ 47 +
+
+
+
+

+ You are in a dialog, containing options for field kibana.alert.rule.risk_score. Press tab to navigate options. Press escape to exit. +

+
+ Filter button +
+
+ Filter out button +
+
+ Overflow button +
+
+
+
+
+
+
+
+
+
+
+
+
+ Rule +
+
+
+
+ +
+
+
+
+

+ You are in a dialog, containing options for field kibana.alert.rule.name. Press tab to navigate options. Press escape to exit. +

+
+ Filter button +
+
+ Filter out button +
+
+ Overflow button +
+
+
+
+
+
+
+
+
+ +`; diff --git a/x-pack/plugins/security_solution/public/common/components/event_details/overview/index.test.tsx b/x-pack/plugins/security_solution/public/common/components/event_details/overview/index.test.tsx new file mode 100644 index 0000000000000..50da80f7b1304 --- /dev/null +++ b/x-pack/plugins/security_solution/public/common/components/event_details/overview/index.test.tsx @@ -0,0 +1,198 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import React from 'react'; +import { render } from '@testing-library/react'; +import { Overview } from './'; +import { TestProviders } from '../../../../common/mock'; + +jest.mock('../../../lib/kibana'); +jest.mock('../../utils', () => ({ + useThrottledResizeObserver: () => ({ width: 400 }), // force row-chunking +})); + +describe('Event Details Overview Cards', () => { + it('renders all cards', () => { + const { getByText } = render( + + + + ); + + getByText('Status'); + getByText('Severity'); + getByText('Risk Score'); + getByText('Rule'); + }); + + it('renders all cards it has data for', () => { + const { getByText, queryByText } = render( + + + + ); + + getByText('Status'); + getByText('Risk Score'); + getByText('Rule'); + + expect(queryByText('Severity')).not.toBeInTheDocument(); + }); + + it('renders rows and spacers correctly', () => { + const { asFragment } = render( + + + + ); + + expect(asFragment()).toMatchSnapshot(); + }); +}); + +const props = { + handleOnEventClosed: jest.fn(), + contextId: 'detections-page', + eventId: 'testId', + indexName: 'testIndex', + timelineId: 'page', + data: [ + { + category: 'kibana', + field: 'kibana.alert.rule.risk_score', + values: ['47'], + originalValue: ['47'], + isObjectArray: false, + }, + { + category: 'kibana', + field: 'kibana.alert.rule.uuid', + values: ['d9f537c0-47b2-11ec-9517-c1c68c44dec0'], + originalValue: ['d9f537c0-47b2-11ec-9517-c1c68c44dec0'], + isObjectArray: false, + }, + { + category: 'kibana', + field: 'kibana.alert.workflow_status', + values: ['open'], + originalValue: ['open'], + isObjectArray: false, + }, + { + category: 'kibana', + field: 'kibana.alert.rule.name', + values: ['More than one event with user name'], + originalValue: ['More than one event with user name'], + isObjectArray: false, + }, + { + category: 'kibana', + field: 'kibana.alert.rule.severity', + values: ['medium'], + originalValue: ['medium'], + isObjectArray: false, + }, + ], + browserFields: { + kibana: { + fields: { + 'kibana.alert.rule.severity': { + category: 'kibana', + count: 0, + name: 'kibana.alert.rule.severity', + type: 'string', + esTypes: ['keyword'], + scripted: false, + searchable: true, + aggregatable: true, + readFromDocValues: true, + format: 'string', + shortDotsEnable: false, + isMapped: true, + indexes: ['apm-*-transaction*'], + }, + 'kibana.alert.rule.risk_score': { + category: 'kibana', + count: 0, + name: 'kibana.alert.rule.risk_score', + type: 'number', + esTypes: ['float'], + scripted: false, + searchable: true, + aggregatable: true, + readFromDocValues: true, + format: 'number', + shortDotsEnable: false, + isMapped: true, + indexes: ['apm-*-transaction*'], + }, + 'kibana.alert.rule.uuid': { + category: 'kibana', + count: 0, + name: 'kibana.alert.rule.uuid', + type: 'string', + esTypes: ['keyword'], + scripted: false, + searchable: true, + aggregatable: true, + readFromDocValues: true, + format: 'string', + shortDotsEnable: false, + isMapped: true, + indexes: ['apm-*-transaction*'], + }, + 'kibana.alert.workflow_status': { + category: 'kibana', + count: 0, + name: 'kibana.alert.workflow_status', + type: 'string', + esTypes: ['keyword'], + scripted: false, + searchable: true, + aggregatable: true, + readFromDocValues: true, + format: 'string', + shortDotsEnable: false, + isMapped: true, + indexes: ['apm-*-transaction*'], + }, + 'kibana.alert.rule.name': { + category: 'kibana', + count: 0, + name: 'kibana.alert.rule.name', + type: 'string', + esTypes: ['keyword'], + scripted: false, + searchable: true, + aggregatable: true, + readFromDocValues: true, + format: 'string', + shortDotsEnable: false, + isMapped: true, + indexes: ['apm-*-transaction*'], + }, + }, + }, + }, +}; + +const dataWithoutSeverity = props.data.filter( + (data) => data.field !== 'kibana.alert.rule.severity' +); + +const fieldsWithoutSeverity = { + 'kibana.alert.rule.risk_score': props.browserFields.kibana.fields['kibana.alert.rule.risk_score'], + 'kibana.alert.rule.uuid': props.browserFields.kibana.fields['kibana.alert.rule.uuid'], + 'kibana.alert.workflow_status': props.browserFields.kibana.fields['kibana.alert.workflow_status'], + 'kibana.alert.rule.name': props.browserFields.kibana.fields['kibana.alert.rule.name'], +}; + +const propsWithoutSeverity = { + ...props, + browserFields: { kibana: { fields: fieldsWithoutSeverity } }, + data: dataWithoutSeverity, +}; diff --git a/x-pack/plugins/security_solution/public/common/components/event_details/overview/index.tsx b/x-pack/plugins/security_solution/public/common/components/event_details/overview/index.tsx new file mode 100644 index 0000000000000..70a8ec7ad0d22 --- /dev/null +++ b/x-pack/plugins/security_solution/public/common/components/event_details/overview/index.tsx @@ -0,0 +1,210 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { EuiFlexGroup, EuiFlexItem, EuiSpacer } from '@elastic/eui'; +import React, { useMemo } from 'react'; +import { chunk, find } from 'lodash/fp'; +import type { Severity } from '@kbn/securitysolution-io-ts-alerting-types'; + +import type { BrowserFields } from '../../../containers/source'; +import { euiStyled } from '../../../../../../../../src/plugins/kibana_react/common'; +import type { TimelineEventsDetailsItem } from '../../../../../common/search_strategy/timeline'; +import type { EnrichedFieldInfo, EnrichedFieldInfoWithValues } from '../types'; +import { getEnrichedFieldInfo } from '../helpers'; +import { + ALERTS_HEADERS_RISK_SCORE, + ALERTS_HEADERS_RULE, + ALERTS_HEADERS_SEVERITY, + SIGNAL_STATUS, +} from '../../../../detections/components/alerts_table/translations'; +import { + SIGNAL_RULE_NAME_FIELD_NAME, + SIGNAL_STATUS_FIELD_NAME, +} from '../../../../timelines/components/timeline/body/renderers/constants'; +import { FormattedFieldValue } from '../../../../timelines/components/timeline/body/renderers/formatted_field'; +import { OverviewCardWithActions } from '../overview/overview_card'; +import { StatusPopoverButton } from '../overview/status_popover_button'; +import { SeverityBadge } from '../../../../../public/detections/components/rules/severity_badge'; +import { useThrottledResizeObserver } from '../../utils'; +import { isNotNull } from '../../../../../public/timelines/store/timeline/helpers'; + +export const NotGrowingFlexGroup = euiStyled(EuiFlexGroup)` + flex-grow: 0; +`; + +interface Props { + browserFields: BrowserFields; + contextId: string; + data: TimelineEventsDetailsItem[]; + eventId: string; + handleOnEventClosed: () => void; + indexName: string; + timelineId: string; +} + +export const Overview = React.memo( + ({ browserFields, contextId, data, eventId, handleOnEventClosed, indexName, timelineId }) => { + const statusData = useMemo(() => { + const item = find({ field: SIGNAL_STATUS_FIELD_NAME, category: 'kibana' }, data); + return ( + item && + getEnrichedFieldInfo({ + eventId, + contextId, + timelineId, + browserFields, + item, + }) + ); + }, [browserFields, contextId, data, eventId, timelineId]); + + const severityData = useMemo(() => { + const item = find({ field: 'kibana.alert.rule.severity', category: 'kibana' }, data); + return ( + item && + getEnrichedFieldInfo({ + eventId, + contextId, + timelineId, + browserFields, + item, + }) + ); + }, [browserFields, contextId, data, eventId, timelineId]); + + const riskScoreData = useMemo(() => { + const item = find({ field: 'kibana.alert.rule.risk_score', category: 'kibana' }, data); + return ( + item && + getEnrichedFieldInfo({ + eventId, + contextId, + timelineId, + browserFields, + item, + }) + ); + }, [browserFields, contextId, data, eventId, timelineId]); + + const ruleNameData = useMemo(() => { + const item = find({ field: SIGNAL_RULE_NAME_FIELD_NAME, category: 'kibana' }, data); + const linkValueField = find({ field: 'kibana.alert.rule.uuid', category: 'kibana' }, data); + return ( + item && + getEnrichedFieldInfo({ + eventId, + contextId, + timelineId, + browserFields, + item, + linkValueField, + }) + ); + }, [browserFields, contextId, data, eventId, timelineId]); + + const signalCard = hasData(statusData) ? ( + + + + + + ) : null; + + const severityCard = hasData(severityData) ? ( + + + + + + ) : null; + + const riskScoreCard = hasData(riskScoreData) ? ( + + + {riskScoreData.values[0]} + + + ) : null; + + const ruleNameCard = hasData(ruleNameData) ? ( + + + + + + ) : null; + + const { width, ref } = useThrottledResizeObserver(); + + // 675px is the container width at which none of the cards, when hovered, + // creates a visual overflow in a single row setup + const showAsSingleRow = width === 0 || width >= 675; + + // Only render cards with content + const cards = [signalCard, severityCard, riskScoreCard, ruleNameCard].filter(isNotNull); + + // If there is enough space, render a single row. + // Otherwise, render two rows with each two cards. + const content = showAsSingleRow ? ( + {cards} + ) : ( + <> + {chunk(2, cards).map((elements, index, { length }) => { + // Add a spacer between rows but not after the last row + const addSpacer = index < length - 1; + return ( + <> + {elements} + {addSpacer && } + + ); + })} + + ); + + return
{content}
; + } +); + +function hasData(fieldInfo?: EnrichedFieldInfo): fieldInfo is EnrichedFieldInfoWithValues { + return !!fieldInfo && Array.isArray(fieldInfo.values); +} + +Overview.displayName = 'Overview'; diff --git a/x-pack/plugins/security_solution/public/common/components/event_details/overview/overview_card.test.tsx b/x-pack/plugins/security_solution/public/common/components/event_details/overview/overview_card.test.tsx new file mode 100644 index 0000000000000..8ed3dc7e36165 --- /dev/null +++ b/x-pack/plugins/security_solution/public/common/components/event_details/overview/overview_card.test.tsx @@ -0,0 +1,71 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import React from 'react'; +import { render } from '@testing-library/react'; +import { OverviewCardWithActions } from './overview_card'; +import { TestProviders } from '../../../../common/mock'; +import { SeverityBadge } from '../../../../../public/detections/components/rules/severity_badge'; + +const props = { + title: 'Severity', + contextId: 'timeline-case', + enrichedFieldInfo: { + contextId: 'timeline-case', + eventId: 'testid', + fieldType: 'string', + timelineId: 'timeline-case', + data: { + field: 'kibana.alert.rule.severity', + format: 'string', + type: 'string', + isObjectArray: false, + }, + values: ['medium'], + fieldFromBrowserField: { + category: 'kibana', + count: 0, + name: 'kibana.alert.rule.severity', + type: 'string', + esTypes: ['keyword'], + scripted: false, + searchable: true, + aggregatable: true, + readFromDocValues: true, + format: 'string', + shortDotsEnable: false, + isMapped: true, + indexes: ['apm-*-transaction*'], + description: '', + example: '', + fields: {}, + }, + }, +}; + +jest.mock('../../../lib/kibana'); + +describe('OverviewCardWithActions', () => { + test('it renders correctly', () => { + const { getByText } = render( + + + + + + ); + + // Headline + getByText('Severity'); + + // Content + getByText('Medium'); + + // Hover actions + getByText('Add To Timeline'); + }); +}); diff --git a/x-pack/plugins/security_solution/public/common/components/event_details/overview/overview_card.tsx b/x-pack/plugins/security_solution/public/common/components/event_details/overview/overview_card.tsx new file mode 100644 index 0000000000000..4d3dae271f5c9 --- /dev/null +++ b/x-pack/plugins/security_solution/public/common/components/event_details/overview/overview_card.tsx @@ -0,0 +1,99 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { EuiFlexGroup, EuiPanel, EuiSpacer, EuiText } from '@elastic/eui'; +import React from 'react'; + +import { ActionCell } from '../table/action_cell'; +import { euiStyled } from '../../../../../../../../src/plugins/kibana_react/common'; +import { EnrichedFieldInfo } from '../types'; + +const ActionWrapper = euiStyled.div` + width: 0; + transform: translate(6px); + transition: transform 50ms ease-in-out; + margin-left: ${({ theme }) => theme.eui.paddingSizes.s}; +`; + +const OverviewPanel = euiStyled(EuiPanel)` + &&& { + background-color: ${({ theme }) => theme.eui.euiColorLightestShade}; + padding: ${({ theme }) => theme.eui.paddingSizes.s}; + height: 78px; + } + + & { + .hoverActions-active { + .timelines__hoverActionButton, + .securitySolution__hoverActionButton { + opacity: 1; + } + } + + &:hover { + .timelines__hoverActionButton, + .securitySolution__hoverActionButton { + opacity: 1; + } + + ${ActionWrapper} { + width: auto; + transform: translate(0); + } + } + } +`; + +interface OverviewCardProps { + title: string; +} + +export const OverviewCard: React.FC = ({ title, children }) => ( + + {title} + + {children} + +); + +OverviewCard.displayName = 'OverviewCard'; + +const ClampedContent = euiStyled.div` + /* Clamp text content to 2 lines */ + display: -webkit-box; + -webkit-line-clamp: 2; + -webkit-box-orient: vertical; + overflow: hidden; +`; + +ClampedContent.displayName = 'ClampedContent'; + +type OverviewCardWithActionsProps = OverviewCardProps & { + contextId: string; + enrichedFieldInfo: EnrichedFieldInfo; +}; + +export const OverviewCardWithActions: React.FC = ({ + title, + children, + contextId, + enrichedFieldInfo, +}) => { + return ( + + + {children} + + + + + + + ); +}; + +OverviewCardWithActions.displayName = 'OverviewCardWithActions'; diff --git a/x-pack/plugins/security_solution/public/common/components/event_details/overview/status_popover_button.test.tsx b/x-pack/plugins/security_solution/public/common/components/event_details/overview/status_popover_button.test.tsx new file mode 100644 index 0000000000000..3c3316618a72c --- /dev/null +++ b/x-pack/plugins/security_solution/public/common/components/event_details/overview/status_popover_button.test.tsx @@ -0,0 +1,82 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import React from 'react'; +import { render } from '@testing-library/react'; +import { StatusPopoverButton } from './status_popover_button'; +import { TestProviders } from '../../../../common/mock'; + +const props = { + eventId: 'testid', + contextId: 'detections-page', + enrichedFieldInfo: { + contextId: 'detections-page', + eventId: 'testid', + fieldType: 'string', + timelineId: 'detections-page', + data: { + field: 'kibana.alert.workflow_status', + format: 'string', + type: 'string', + isObjectArray: false, + }, + values: ['open'], + fieldFromBrowserField: { + category: 'kibana', + count: 0, + name: 'kibana.alert.workflow_status', + type: 'string', + esTypes: ['keyword'], + scripted: false, + searchable: true, + aggregatable: true, + readFromDocValues: true, + format: 'string', + shortDotsEnable: false, + isMapped: true, + indexes: ['apm-*-transaction*'], + description: '', + example: '', + fields: {}, + }, + }, + indexName: '.internal.alerts-security.alerts-default-000001', + timelineId: 'detections-page', + handleOnEventClosed: jest.fn(), +}; + +jest.mock( + '../../../../detections/containers/detection_engine/alerts/use_alerts_privileges', + () => ({ + useAlertsPrivileges: jest.fn().mockReturnValue({ hasIndexWrite: true, hasKibanaCRUD: true }), + }) +); + +describe('StatusPopoverButton', () => { + test('it renders the correct status', () => { + const { getByText } = render( + + + + ); + + getByText('open'); + }); + + test('it shows the correct options when clicked', () => { + const { getByText } = render( + + + + ); + + getByText('open').click(); + + getByText('Mark as acknowledged'); + getByText('Mark as closed'); + }); +}); diff --git a/x-pack/plugins/security_solution/public/common/components/event_details/overview/status_popover_button.tsx b/x-pack/plugins/security_solution/public/common/components/event_details/overview/status_popover_button.tsx new file mode 100644 index 0000000000000..0ffa1570e7c29 --- /dev/null +++ b/x-pack/plugins/security_solution/public/common/components/event_details/overview/status_popover_button.tsx @@ -0,0 +1,81 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { EuiContextMenuPanel, EuiPopover, EuiPopoverTitle } from '@elastic/eui'; +import React, { useCallback, useMemo, useState } from 'react'; + +import { useAlertsActions } from '../../../../detections/components/alerts_table/timeline_actions/use_alerts_actions'; +import { Status } from '../../../../../common/detection_engine/schemas/common/schemas'; +import { + CHANGE_ALERT_STATUS, + CLICK_TO_CHANGE_ALERT_STATUS, +} from '../../../../detections/components/alerts_table/translations'; +import { FormattedFieldValue } from '../../../../timelines/components/timeline/body/renderers/formatted_field'; +import type { EnrichedFieldInfoWithValues } from '../types'; + +interface StatusPopoverButtonProps { + eventId: string; + contextId: string; + enrichedFieldInfo: EnrichedFieldInfoWithValues; + indexName: string; + timelineId: string; + handleOnEventClosed: () => void; +} + +export const StatusPopoverButton = React.memo( + ({ eventId, contextId, enrichedFieldInfo, indexName, timelineId, handleOnEventClosed }) => { + const [isPopoverOpen, setIsPopoverOpen] = useState(false); + const togglePopover = useCallback(() => setIsPopoverOpen(!isPopoverOpen), [isPopoverOpen]); + const closePopover = useCallback(() => setIsPopoverOpen(false), []); + const closeAfterAction = useCallback(() => { + closePopover(); + handleOnEventClosed(); + }, [closePopover, handleOnEventClosed]); + + const { actionItems } = useAlertsActions({ + closePopover: closeAfterAction, + eventId, + timelineId, + indexName, + alertStatus: enrichedFieldInfo.values[0] as Status, + }); + + const button = useMemo( + () => ( + + ), + [contextId, eventId, enrichedFieldInfo, togglePopover] + ); + + return ( + + {CHANGE_ALERT_STATUS} + + + ); + } +); + +StatusPopoverButton.displayName = 'StatusPopoverButton'; diff --git a/x-pack/plugins/security_solution/public/common/components/event_details/reason.tsx b/x-pack/plugins/security_solution/public/common/components/event_details/reason.tsx index d06f4d3ea105b..88208dd1b9780 100644 --- a/x-pack/plugins/security_solution/public/common/components/event_details/reason.tsx +++ b/x-pack/plugins/security_solution/public/common/components/event_details/reason.tsx @@ -5,18 +5,12 @@ * 2.0. */ -import { EuiTextColor, EuiFlexItem, EuiSpacer, EuiHorizontalRule, EuiTitle } from '@elastic/eui'; -import { ALERT_REASON, ALERT_RULE_UUID } from '@kbn/rule-data-utils'; +import { EuiTextColor, EuiFlexItem } from '@elastic/eui'; +import { ALERT_REASON } from '@kbn/rule-data-utils'; import React, { useMemo } from 'react'; -import styled from 'styled-components'; -import { getRuleDetailsUrl, useFormatUrl } from '../link_to'; -import * as i18n from './translations'; import { TimelineEventsDetailsItem } from '../../../../common/search_strategy'; -import { LinkAnchor } from '../links'; -import { useKibana } from '../../lib/kibana'; -import { APP_UI_ID, SecurityPageName } from '../../../../common/constants'; import { EVENT_DETAILS_PLACEHOLDER } from '../../../timelines/components/side_panel/event_details/translations'; import { getFieldValue } from '../../../detections/components/host_isolation/helpers'; @@ -25,16 +19,7 @@ interface Props { eventId: string; } -export const Indent = styled.div` - padding: 0 8px; - word-break: break-word; - line-height: 1.7em; -`; - export const ReasonComponent: React.FC = ({ eventId, data }) => { - const { navigateToApp } = useKibana().services.application; - const { formatUrl } = useFormatUrl(SecurityPageName.rules); - const reason = useMemo(() => { const siemSignalsReason = getFieldValue( { category: 'signal', field: 'signal.alert.reason' }, @@ -44,47 +29,11 @@ export const ReasonComponent: React.FC = ({ eventId, data }) => { return aadReason.length > 0 ? aadReason : siemSignalsReason; }, [data]); - const ruleId = useMemo(() => { - const siemSignalsRuleId = getFieldValue({ category: 'signal', field: 'signal.rule.id' }, data); - const aadRuleId = getFieldValue({ category: 'kibana', field: ALERT_RULE_UUID }, data); - return aadRuleId.length > 0 ? aadRuleId : siemSignalsRuleId; - }, [data]); - if (!eventId) { return {EVENT_DETAILS_PLACEHOLDER}; } - return reason ? ( - - - -
{i18n.REASON}
-
- - - {reason} - - - - - void }) => { - ev.preventDefault(); - navigateToApp(APP_UI_ID, { - deepLinkId: SecurityPageName.rules, - path: getRuleDetailsUrl(ruleId), - }); - }} - href={formatUrl(getRuleDetailsUrl(ruleId))} - > - {i18n.VIEW_RULE_DETAIL_PAGE} - - - - -
- ) : null; + return reason ? {reason} : null; }; ReasonComponent.displayName = 'ReasonComponent'; diff --git a/x-pack/plugins/security_solution/public/common/components/event_details/summary_view.tsx b/x-pack/plugins/security_solution/public/common/components/event_details/summary_view.tsx index cf8bf3ddb7474..a84d831524983 100644 --- a/x-pack/plugins/security_solution/public/common/components/event_details/summary_view.tsx +++ b/x-pack/plugins/security_solution/public/common/components/event_details/summary_view.tsx @@ -5,14 +5,24 @@ * 2.0. */ -import { EuiInMemoryTable, EuiBasicTableColumn, EuiTitle } from '@elastic/eui'; +import { + EuiInMemoryTable, + EuiBasicTableColumn, + EuiLink, + EuiTitle, + EuiFlexGroup, + EuiFlexItem, + EuiSpacer, + EuiText, +} from '@elastic/eui'; import React from 'react'; import styled from 'styled-components'; import { SummaryRow } from './helpers'; +import { VIEW_ALL_DOCUMENT_FIELDS } from './translations'; export const Indent = styled.div` - padding: 0 4px; + padding: 0 12px; `; // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -43,18 +53,27 @@ export const StyledEuiInMemoryTable = styled(EuiInMemoryTable as any)` `; export const SummaryViewComponent: React.FC<{ - title?: string; + goToTable: () => void; + title: string; summaryColumns: Array>; summaryRows: SummaryRow[]; dataTestSubj?: string; -}> = ({ summaryColumns, summaryRows, dataTestSubj = 'summary-view', title }) => { +}> = ({ goToTable, summaryColumns, summaryRows, dataTestSubj = 'summary-view', title }) => { return ( - <> - {title && ( - -
{title}
-
- )} +
+ + + +
{title}
+
+
+ + + {VIEW_ALL_DOCUMENT_FIELDS} + + +
+ - +
); }; diff --git a/x-pack/plugins/security_solution/public/common/components/event_details/table/action_cell.tsx b/x-pack/plugins/security_solution/public/common/components/event_details/table/action_cell.tsx index 74d46cf3431dc..b49aafea92245 100644 --- a/x-pack/plugins/security_solution/public/common/components/event_details/table/action_cell.tsx +++ b/x-pack/plugins/security_solution/public/common/components/event_details/table/action_cell.tsx @@ -8,27 +8,22 @@ import React, { useCallback, useState, useContext } from 'react'; import { HoverActions } from '../../hover_actions'; import { useActionCellDataProvider } from './use_action_cell_data_provider'; -import { EventFieldsData, FieldsData } from '../types'; +import { EnrichedFieldInfo } from '../types'; import { ColumnHeaderOptions } from '../../../../../common/types/timeline'; -import { BrowserField } from '../../../containers/source'; import { TimelineContext } from '../../../../../../timelines/public'; -interface Props { +interface Props extends EnrichedFieldInfo { contextId: string; - data: FieldsData | EventFieldsData; + applyWidthAndPadding?: boolean; disabled?: boolean; - eventId: string; - fieldFromBrowserField?: BrowserField; getLinkValue?: (field: string) => string | null; - linkValue?: string | null | undefined; onFilterAdded?: () => void; - timelineId?: string; toggleColumn?: (column: ColumnHeaderOptions) => void; - values: string[] | null | undefined; } export const ActionCell: React.FC = React.memo( ({ + applyWidthAndPadding = true, contextId, data, eventId, @@ -68,6 +63,7 @@ export const ActionCell: React.FC = React.memo( return ( { let updateExceptionListItem: jest.SpyInstance>; let getQueryFilter: jest.SpyInstance>; let buildAlertStatusesFilter: jest.SpyInstance< - ReturnType - >; - let buildAlertsRuleIdFilter: jest.SpyInstance< - ReturnType + ReturnType >; + let buildAlertsFilter: jest.SpyInstance>; let addOrUpdateItemsArgs: Parameters; let render: () => RenderHookResult; const onError = jest.fn(); const onSuccess = jest.fn(); - const ruleId = 'rule-id'; + const ruleStaticId = 'rule-id'; const alertIdToClose = 'idToClose'; const bulkCloseIndex = ['.custom']; const itemsToAdd: CreateExceptionListItemSchema[] = [ @@ -128,14 +126,11 @@ describe('useAddOrUpdateException', () => { getQueryFilter = jest.spyOn(getQueryFilterHelper, 'getQueryFilter'); - buildAlertStatusesFilter = jest.spyOn( - buildFilterHelpers, - 'buildAlertStatusesFilterRuleRegistry' - ); + buildAlertStatusesFilter = jest.spyOn(buildFilterHelpers, 'buildAlertStatusesFilter'); - buildAlertsRuleIdFilter = jest.spyOn(buildFilterHelpers, 'buildAlertsRuleIdFilter'); + buildAlertsFilter = jest.spyOn(buildFilterHelpers, 'buildAlertsFilter'); - addOrUpdateItemsArgs = [ruleId, itemsToAddOrUpdate]; + addOrUpdateItemsArgs = [ruleStaticId, itemsToAddOrUpdate]; render = () => renderHook( () => @@ -262,7 +257,7 @@ describe('useAddOrUpdateException', () => { describe('when alertIdToClose is passed in', () => { beforeEach(() => { - addOrUpdateItemsArgs = [ruleId, itemsToAddOrUpdate, alertIdToClose]; + addOrUpdateItemsArgs = [ruleStaticId, itemsToAddOrUpdate, alertIdToClose]; }); it('should update the alert status', async () => { await act(async () => { @@ -317,7 +312,7 @@ describe('useAddOrUpdateException', () => { describe('when bulkCloseIndex is passed in', () => { beforeEach(() => { - addOrUpdateItemsArgs = [ruleId, itemsToAddOrUpdate, undefined, bulkCloseIndex]; + addOrUpdateItemsArgs = [ruleStaticId, itemsToAddOrUpdate, undefined, bulkCloseIndex]; }); it('should update the status of only alerts that are open', async () => { await act(async () => { @@ -351,8 +346,8 @@ describe('useAddOrUpdateException', () => { addOrUpdateItems(...addOrUpdateItemsArgs); } await waitForNextUpdate(); - expect(buildAlertsRuleIdFilter).toHaveBeenCalledTimes(1); - expect(buildAlertsRuleIdFilter.mock.calls[0][0]).toEqual(ruleId); + expect(buildAlertsFilter).toHaveBeenCalledTimes(1); + expect(buildAlertsFilter.mock.calls[0][0]).toEqual(ruleStaticId); }); }); it('should generate the query filter using exceptions', async () => { diff --git a/x-pack/plugins/security_solution/public/common/components/exceptions/use_add_exception.tsx b/x-pack/plugins/security_solution/public/common/components/exceptions/use_add_exception.tsx index 7cb8b643aa0e8..71c49f7c2daad 100644 --- a/x-pack/plugins/security_solution/public/common/components/exceptions/use_add_exception.tsx +++ b/x-pack/plugins/security_solution/public/common/components/exceptions/use_add_exception.tsx @@ -17,27 +17,25 @@ import { HttpStart } from '../../../../../../../src/core/public'; import { updateAlertStatus } from '../../../detections/containers/detection_engine/alerts/api'; import { getUpdateAlertsQuery } from '../../../detections/components/alerts_table/actions'; import { - buildAlertsRuleIdFilter, + buildAlertsFilter, buildAlertStatusesFilter, - buildAlertStatusesFilterRuleRegistry, } from '../../../detections/components/alerts_table/default_config'; import { getQueryFilter } from '../../../../common/detection_engine/get_query_filter'; import { Index } from '../../../../common/detection_engine/schemas/common/schemas'; -import { useIsExperimentalFeatureEnabled } from '../../hooks/use_experimental_features'; import { formatExceptionItemForUpdate, prepareExceptionItemsForBulkClose } from './helpers'; import { useKibana } from '../../lib/kibana'; /** * Adds exception items to the list. Also optionally closes alerts. * - * @param ruleId id of the rule where the exception updates will be applied + * @param ruleStaticId static id of the rule (rule.ruleId, not rule.id) where the exception updates will be applied * @param exceptionItemsToAddOrUpdate array of ExceptionListItemSchema to add or update * @param alertIdToClose - optional string representing alert to close * @param bulkCloseIndex - optional index used to create bulk close query * */ export type AddOrUpdateExceptionItemsFunc = ( - ruleId: string, + ruleStaticId: string, exceptionItemsToAddOrUpdate: Array, alertIdToClose?: string, bulkCloseIndex?: Index @@ -72,10 +70,10 @@ export const useAddOrUpdateException = ({ const addOrUpdateExceptionRef = useRef(null); const { addExceptionListItem, updateExceptionListItem } = useApi(services.http); const addOrUpdateException = useCallback( - async (ruleId, exceptionItemsToAddOrUpdate, alertIdToClose, bulkCloseIndex) => { + async (ruleStaticId, exceptionItemsToAddOrUpdate, alertIdToClose, bulkCloseIndex) => { if (addOrUpdateExceptionRef.current != null) { addOrUpdateExceptionRef.current( - ruleId, + ruleStaticId, exceptionItemsToAddOrUpdate, alertIdToClose, bulkCloseIndex @@ -84,15 +82,13 @@ export const useAddOrUpdateException = ({ }, [] ); - // TODO: Once we are past experimental phase this code should be removed - const ruleRegistryEnabled = useIsExperimentalFeatureEnabled('ruleRegistryEnabled'); useEffect(() => { let isSubscribed = true; const abortCtrl = new AbortController(); const onUpdateExceptionItemsAndAlertStatus: AddOrUpdateExceptionItemsFunc = async ( - ruleId, + ruleStaticId, exceptionItemsToAddOrUpdate, alertIdToClose, bulkCloseIndex @@ -131,15 +127,16 @@ export const useAddOrUpdateException = ({ } if (bulkCloseIndex != null) { - // TODO: Once we are past experimental phase this code should be removed - const alertStatusFilter = ruleRegistryEnabled - ? buildAlertStatusesFilterRuleRegistry(['open', 'acknowledged', 'in-progress']) - : buildAlertStatusesFilter(['open', 'acknowledged', 'in-progress']); + const alertStatusFilter = buildAlertStatusesFilter([ + 'open', + 'acknowledged', + 'in-progress', + ]); const filter = getQueryFilter( '', 'kuery', - [...buildAlertsRuleIdFilter(ruleId), ...alertStatusFilter], + [...buildAlertsFilter(ruleStaticId), ...alertStatusFilter], bulkCloseIndex, prepareExceptionItemsForBulkClose(exceptionItemsToAddOrUpdate), false @@ -185,14 +182,7 @@ export const useAddOrUpdateException = ({ isSubscribed = false; abortCtrl.abort(); }; - }, [ - addExceptionListItem, - http, - onSuccess, - onError, - ruleRegistryEnabled, - updateExceptionListItem, - ]); + }, [addExceptionListItem, http, onSuccess, onError, updateExceptionListItem]); return [{ isLoading }, addOrUpdateException]; }; diff --git a/x-pack/plugins/security_solution/public/common/components/hover_actions/index.tsx b/x-pack/plugins/security_solution/public/common/components/hover_actions/index.tsx index 81ecec7bdc535..311284565ba14 100644 --- a/x-pack/plugins/security_solution/public/common/components/hover_actions/index.tsx +++ b/x-pack/plugins/security_solution/public/common/components/hover_actions/index.tsx @@ -37,8 +37,6 @@ const StyledHoverActionsContainer = styled.div<{ $hideTopN: boolean; $isActive: boolean; }>` - min-width: ${({ $hideTopN }) => `${$hideTopN ? '112px' : '138px'}`}; - padding: ${(props) => `0 ${props.theme.eui.paddingSizes.s}`}; display: flex; ${(props) => @@ -82,8 +80,14 @@ const StyledHoverActionsContainer = styled.div<{ : ''} `; +const StyledHoverActionsContainerWithPaddingsAndMinWidth = styled(StyledHoverActionsContainer)` + min-width: ${({ $hideTopN }) => `${$hideTopN ? '112px' : '138px'}`}; + padding: ${(props) => `0 ${props.theme.eui.paddingSizes.s}`}; +`; + interface Props { additionalContent?: React.ReactNode; + applyWidthAndPadding?: boolean; closeTopN?: () => void; closePopOver?: () => void; dataProvider?: DataProvider | DataProvider[]; @@ -128,6 +132,7 @@ export const HoverActions: React.FC = React.memo( dataType, draggableId, enableOverflowButton = false, + applyWidthAndPadding = true, field, goGetTimelineId, isObjectArray, @@ -227,6 +232,10 @@ export const HoverActions: React.FC = React.memo( values, }); + const Container = applyWidthAndPadding + ? StyledHoverActionsContainerWithPaddingsAndMinWidth + : StyledHoverActionsContainer; + return ( = React.memo( showTopN, })} > - = React.memo( {additionalContent != null && {additionalContent}} {enableOverflowButton && !isCaseView ? overflowActionItems : allActionItems} - + ); } diff --git a/x-pack/plugins/security_solution/public/common/components/matrix_histogram/types.ts b/x-pack/plugins/security_solution/public/common/components/matrix_histogram/types.ts index b67505a66be44..e5da55f740033 100644 --- a/x-pack/plugins/security_solution/public/common/components/matrix_histogram/types.ts +++ b/x-pack/plugins/security_solution/public/common/components/matrix_histogram/types.ts @@ -15,7 +15,7 @@ import { MatrixHistogramType } from '../../../../common/search_strategy/security import { UpdateDateRange } from '../charts/common'; import { GlobalTimeArgs } from '../../containers/use_global_time'; import { DocValueFields } from '../../../../common/search_strategy'; -import { Threshold } from '../../../detections/components/rules/query_preview'; +import { FieldValueThreshold } from '../../../detections/components/rules/threshold_input'; export type MatrixHistogramMappingTypes = Record< string, @@ -77,7 +77,7 @@ export interface MatrixHistogramQueryProps { stackByField: string; startDate: string; histogramType: MatrixHistogramType; - threshold?: Threshold; + threshold?: FieldValueThreshold; skip?: boolean; isPtrIncluded?: boolean; includeMissingData?: boolean; diff --git a/x-pack/plugins/security_solution/public/common/components/page/index.tsx b/x-pack/plugins/security_solution/public/common/components/page/index.tsx index 9666a0837b046..d17f5ceb4f9b1 100644 --- a/x-pack/plugins/security_solution/public/common/components/page/index.tsx +++ b/x-pack/plugins/security_solution/public/common/components/page/index.tsx @@ -27,6 +27,10 @@ export const AppGlobalStyle = createGlobalStyle<{ theme: { eui: { euiColorPrimar z-index: 9900 !important; min-width: 24px; } + .euiPopover__panel.euiPopover__panel-isOpen.sourcererPopoverPanel { + // needs to appear under modal + z-index: 5900 !important; + } .euiToolTip { z-index: 9950 !important; } diff --git a/x-pack/plugins/security_solution/public/common/components/sourcerer/helpers.tsx b/x-pack/plugins/security_solution/public/common/components/sourcerer/helpers.tsx index af21a018ee47a..3d378e72edbf4 100644 --- a/x-pack/plugins/security_solution/public/common/components/sourcerer/helpers.tsx +++ b/x-pack/plugins/security_solution/public/common/components/sourcerer/helpers.tsx @@ -14,7 +14,7 @@ import { EuiFormRow, EuiFormRowProps, } from '@elastic/eui'; -import styled from 'styled-components'; +import styled, { css } from 'styled-components'; import { sourcererModel } from '../../store/sourcerer'; @@ -50,11 +50,25 @@ export const PopoverContent = styled.div` export const StyledBadge = styled(EuiBadge)` margin-left: 8px; + &, + .euiBadge__text { + cursor: pointer; + } +`; + +export const Blockquote = styled.span` + ${({ theme }) => css` + display: block; + border-color: ${theme.eui.euiColorDarkShade}; + border-left: ${theme.eui.euiBorderThick}; + margin: ${theme.eui.euiSizeS} 0 ${theme.eui.euiSizeS} ${theme.eui.euiSizeS}; + padding: ${theme.eui.euiSizeS}; + `} `; interface GetDataViewSelectOptionsProps { dataViewId: string; - defaultDataView: sourcererModel.KibanaDataView; + defaultDataViewId: sourcererModel.KibanaDataView['id']; isModified: boolean; isOnlyDetectionAlerts: boolean; kibanaDataViews: sourcererModel.KibanaDataView[]; @@ -62,7 +76,7 @@ interface GetDataViewSelectOptionsProps { export const getDataViewSelectOptions = ({ dataViewId, - defaultDataView, + defaultDataViewId, isModified, isOnlyDetectionAlerts, kibanaDataViews, @@ -78,12 +92,12 @@ export const getDataViewSelectOptions = ({ ), - value: defaultDataView.id, + value: defaultDataViewId, }, ] : kibanaDataViews.map(({ title, id }) => ({ inputDisplay: - id === defaultDataView.id ? ( + id === defaultDataViewId ? ( {i18n.SECURITY_DEFAULT_DATA_VIEW_LABEL} {isModified && id === dataViewId && ( diff --git a/x-pack/plugins/security_solution/public/common/components/sourcerer/index.test.tsx b/x-pack/plugins/security_solution/public/common/components/sourcerer/index.test.tsx index 763898378e6f4..7d3dc9641929a 100644 --- a/x-pack/plugins/security_solution/public/common/components/sourcerer/index.test.tsx +++ b/x-pack/plugins/security_solution/public/common/components/sourcerer/index.test.tsx @@ -19,8 +19,16 @@ import { } from '../../mock'; import { createStore } from '../../store'; import { EuiSuperSelectOption } from '@elastic/eui/src/components/form/super_select/super_select_control'; +import { waitFor } from '@testing-library/dom'; +import { useSourcererDataView } from '../../containers/sourcerer'; const mockDispatch = jest.fn(); + +jest.mock('../../containers/sourcerer'); +const mockUseUpdateDataView = jest.fn().mockReturnValue(() => true); +jest.mock('./use_update_data_view', () => ({ + useUpdateDataView: () => mockUseUpdateDataView, +})); jest.mock('react-redux', () => { const original = jest.requireActual('react-redux'); @@ -30,6 +38,15 @@ jest.mock('react-redux', () => { }; }); +jest.mock('../../../../../../../src/plugins/kibana_react/public', () => { + const original = jest.requireActual('../../../../../../../src/plugins/kibana_react/public'); + + return { + ...original, + toMountPoint: jest.fn(), + }; +}); + const mockOptions = [ { label: 'apm-*-transaction*', value: 'apm-*-transaction*' }, { label: 'auditbeat-*', value: 'auditbeat-*' }, @@ -57,12 +74,21 @@ const patternListNoSignals = patternList .filter((p) => p !== mockGlobalState.sourcerer.signalIndexName) .sort(); let store: ReturnType; +const sourcererDataView = { + indicesExist: true, + loading: false, +}; + describe('Sourcerer component', () => { const { storage } = createSecuritySolutionStorageMock(); beforeEach(() => { store = createStore(mockGlobalState, SUB_PLUGINS_REDUCER, kibanaObservable, storage); + (useSourcererDataView as jest.Mock).mockReturnValue(sourcererDataView); jest.clearAllMocks(); + }); + + afterAll(() => { jest.restoreAllMocks(); }); @@ -215,7 +241,6 @@ describe('Sourcerer component', () => { ...mockGlobalState.sourcerer.sourcererScopes, [SourcererScopeName.default]: { ...mockGlobalState.sourcerer.sourcererScopes[SourcererScopeName.default], - loading: false, selectedDataViewId: '1234', selectedPatterns: ['filebeat-*'], }, @@ -267,7 +292,6 @@ describe('Sourcerer component', () => { ...mockGlobalState.sourcerer.sourcererScopes, [SourcererScopeName.default]: { ...mockGlobalState.sourcerer.sourcererScopes[SourcererScopeName.default], - loading: false, selectedDataViewId: id, selectedPatterns: patternListNoSignals.slice(0, 2), }, @@ -313,8 +337,6 @@ describe('Sourcerer component', () => { ...mockGlobalState.sourcerer.sourcererScopes, [SourcererScopeName.timeline]: { ...mockGlobalState.sourcerer.sourcererScopes[SourcererScopeName.timeline], - loading: false, - patternList, selectedDataViewId: id, selectedPatterns: patternList.slice(0, 2), }, @@ -355,7 +377,6 @@ describe('Sourcerer component', () => { ...mockGlobalState.sourcerer.sourcererScopes, [SourcererScopeName.default]: { ...mockGlobalState.sourcerer.sourcererScopes[SourcererScopeName.default], - loading: false, selectedDataViewId: id, selectedPatterns: patternListNoSignals.slice(0, 2), }, @@ -629,6 +650,7 @@ describe('timeline sourcerer', () => { }; beforeAll(() => { + (useSourcererDataView as jest.Mock).mockReturnValue(sourcererDataView); wrapper = mount( @@ -713,6 +735,7 @@ describe('timeline sourcerer', () => { }; store = createStore(state2, SUB_PLUGINS_REDUCER, kibanaObservable, storage); + wrapper = mount( @@ -754,6 +777,7 @@ describe('Sourcerer integration tests', () => { const { storage } = createSecuritySolutionStorageMock(); beforeEach(() => { + (useSourcererDataView as jest.Mock).mockReturnValue(sourcererDataView); store = createStore(state, SUB_PLUGINS_REDUCER, kibanaObservable, storage); jest.clearAllMocks(); jest.restoreAllMocks(); @@ -795,11 +819,15 @@ describe('No data', () => { const { storage } = createSecuritySolutionStorageMock(); beforeEach(() => { + (useSourcererDataView as jest.Mock).mockReturnValue({ + ...sourcererDataView, + indicesExist: false, + }); store = createStore(mockNoIndicesState, SUB_PLUGINS_REDUCER, kibanaObservable, storage); jest.clearAllMocks(); jest.restoreAllMocks(); }); - test('Hide sourcerer', () => { + test('Hide sourcerer - default ', () => { const wrapper = mount( @@ -808,4 +836,123 @@ describe('No data', () => { expect(wrapper.find(`[data-test-subj="sourcerer-trigger"]`).exists()).toEqual(false); }); + test('Hide sourcerer - detections ', () => { + const wrapper = mount( + + + + ); + + expect(wrapper.find(`[data-test-subj="sourcerer-trigger"]`).exists()).toEqual(false); + }); + test('Hide sourcerer - timeline ', () => { + const wrapper = mount( + + + + ); + + expect(wrapper.find(`[data-test-subj="timeline-sourcerer-trigger"]`).exists()).toEqual(true); + }); +}); + +describe('Update available', () => { + const { storage } = createSecuritySolutionStorageMock(); + const state2 = { + ...mockGlobalState, + sourcerer: { + ...mockGlobalState.sourcerer, + kibanaDataViews: [ + mockGlobalState.sourcerer.defaultDataView, + { + ...mockGlobalState.sourcerer.defaultDataView, + id: '1234', + title: 'auditbeat-*', + patternList: ['auditbeat-*'], + }, + { + ...mockGlobalState.sourcerer.defaultDataView, + id: '12347', + title: 'packetbeat-*', + patternList: ['packetbeat-*'], + }, + ], + sourcererScopes: { + ...mockGlobalState.sourcerer.sourcererScopes, + [SourcererScopeName.timeline]: { + ...mockGlobalState.sourcerer.sourcererScopes[SourcererScopeName.timeline], + loading: false, + patternList, + selectedDataViewId: null, + selectedPatterns: ['myFakebeat-*'], + missingPatterns: ['myFakebeat-*'], + }, + }, + }, + }; + + let wrapper: ReactWrapper; + + beforeEach(() => { + (useSourcererDataView as jest.Mock).mockReturnValue({ + ...sourcererDataView, + activePatterns: ['myFakebeat-*'], + }); + store = createStore(state2, SUB_PLUGINS_REDUCER, kibanaObservable, storage); + + wrapper = mount( + + + + ); + }); + afterEach(() => { + jest.clearAllMocks(); + }); + + test('Show Update available label', () => { + expect(wrapper.find(`[data-test-subj="sourcerer-deprecated-badge"]`).exists()).toBeTruthy(); + }); + + test('Show correct tooltip', () => { + expect(wrapper.find(`[data-test-subj="sourcerer-tooltip"]`).prop('content')).toEqual( + 'myFakebeat-*' + ); + }); + + test('Show UpdateDefaultDataViewModal', () => { + wrapper.find(`[data-test-subj="timeline-sourcerer-trigger"]`).first().simulate('click'); + + wrapper.find(`button[data-test-subj="sourcerer-deprecated-update"]`).first().simulate('click'); + + expect(wrapper.find(`UpdateDefaultDataViewModal`).prop('isShowing')).toEqual(true); + }); + + test('Show Add index pattern in UpdateDefaultDataViewModal', () => { + wrapper.find(`[data-test-subj="timeline-sourcerer-trigger"]`).first().simulate('click'); + + wrapper.find(`button[data-test-subj="sourcerer-deprecated-update"]`).first().simulate('click'); + + expect(wrapper.find(`button[data-test-subj="sourcerer-update-data-view"]`).text()).toEqual( + 'Add index pattern' + ); + }); + + test('Set all the index patterns from legacy timeline to sourcerer, after clicking on "Add index pattern"', async () => { + wrapper.find(`[data-test-subj="timeline-sourcerer-trigger"]`).first().simulate('click'); + + wrapper.find(`button[data-test-subj="sourcerer-deprecated-update"]`).first().simulate('click'); + + wrapper.find(`button[data-test-subj="sourcerer-update-data-view"]`).simulate('click'); + + await waitFor(() => wrapper.update()); + expect(mockDispatch).toHaveBeenCalledWith( + sourcererActions.setSelectedDataView({ + id: SourcererScopeName.timeline, + selectedDataViewId: 'security-solution', + selectedPatterns: ['myFakebeat-*'], + shouldValidateSelectedPatterns: false, + }) + ); + }); }); diff --git a/x-pack/plugins/security_solution/public/common/components/sourcerer/index.tsx b/x-pack/plugins/security_solution/public/common/components/sourcerer/index.tsx index 89bbeef72a21c..2ffb0670c4edc 100644 --- a/x-pack/plugins/security_solution/public/common/components/sourcerer/index.tsx +++ b/x-pack/plugins/security_solution/public/common/components/sourcerer/index.tsx @@ -13,13 +13,12 @@ import { EuiFlexGroup, EuiFlexItem, EuiForm, + EuiOutsideClickDetector, EuiPopover, EuiPopoverTitle, EuiSpacer, EuiSuperSelect, - EuiToolTip, } from '@elastic/eui'; -import deepEqual from 'fast-deep-equal'; import React, { useCallback, useEffect, useMemo, useState } from 'react'; import { useDispatch } from 'react-redux'; @@ -27,18 +26,13 @@ import * as i18n from './translations'; import { sourcererActions, sourcererModel, sourcererSelectors } from '../../store/sourcerer'; import { useDeepEqualSelector } from '../../hooks/use_selector'; import { SourcererScopeName } from '../../store/sourcerer/model'; -import { checkIfIndicesExist } from '../../store/sourcerer/helpers'; import { usePickIndexPatterns } from './use_pick_index_patterns'; -import { - FormRow, - getDataViewSelectOptions, - getTooltipContent, - PopoverContent, - ResetButton, - StyledBadge, - StyledButton, - StyledFormRow, -} from './helpers'; +import { FormRow, PopoverContent, ResetButton, StyledButton, StyledFormRow } from './helpers'; +import { TemporarySourcerer } from './temporary'; +import { UpdateDefaultDataViewModal } from './update_default_data_view_modal'; +import { useSourcererDataView } from '../../containers/sourcerer'; +import { useUpdateDataView } from './use_update_data_view'; +import { Trigger } from './trigger'; interface SourcererComponentProps { scope: sourcererModel.SourcererScopeName; @@ -54,13 +48,24 @@ export const Sourcerer = React.memo(({ scope: scopeId } defaultDataView, kibanaDataViews, signalIndexName, - sourcererScope: { selectedDataViewId, selectedPatterns, loading }, - sourcererDataView, + sourcererScope: { + selectedDataViewId, + selectedPatterns, + missingPatterns: sourcererMissingPatterns, + }, } = useDeepEqualSelector((state) => sourcererScopeSelector(state, scopeId)); - const indicesExist = useMemo( - () => checkIfIndicesExist({ scopeId, signalIndexName, sourcererDataView }), - [scopeId, signalIndexName, sourcererDataView] + + const { activePatterns, indicesExist, loading } = useSourcererDataView(scopeId); + const [missingPatterns, setMissingPatterns] = useState( + activePatterns && activePatterns.length > 0 + ? sourcererMissingPatterns.filter((p) => activePatterns.includes(p)) + : [] ); + useEffect(() => { + if (activePatterns && activePatterns.length > 0) { + setMissingPatterns(sourcererMissingPatterns.filter((p) => activePatterns.includes(p))); + } + }, [activePatterns, sourcererMissingPatterns]); const [isOnlyDetectionAlertsChecked, setIsOnlyDetectionAlertsChecked] = useState( isTimelineSourcerer && selectedPatterns.join() === signalIndexName @@ -68,15 +73,15 @@ export const Sourcerer = React.memo(({ scope: scopeId } const isOnlyDetectionAlerts: boolean = isDetectionsSourcerer || (isTimelineSourcerer && isOnlyDetectionAlertsChecked); - const [isPopoverOpen, setPopoverIsOpen] = useState(false); - const [dataViewId, setDataViewId] = useState(selectedDataViewId ?? defaultDataView.id); + const [dataViewId, setDataViewId] = useState(selectedDataViewId); const { + allOptions, + dataViewSelectOptions, isModified, onChangeCombo, renderOption, - selectableOptions, selectedOptions, setIndexPatternsByDataView, } = usePickIndexPatterns({ @@ -84,10 +89,12 @@ export const Sourcerer = React.memo(({ scope: scopeId } defaultDataViewId: defaultDataView.id, isOnlyDetectionAlerts, kibanaDataViews, + missingPatterns, scopeId, selectedPatterns, signalIndexName, }); + const onCheckboxChanged = useCallback( (e) => { setIsOnlyDetectionAlertsChecked(e.target.checked); @@ -96,20 +103,26 @@ export const Sourcerer = React.memo(({ scope: scopeId } }, [defaultDataView.id, setIndexPatternsByDataView] ); - const isSavingDisabled = useMemo(() => selectedOptions.length === 0, [selectedOptions]); + const [expandAdvancedOptions, setExpandAdvancedOptions] = useState(false); + const [isShowingUpdateModal, setIsShowingUpdateModal] = useState(false); const setPopoverIsOpenCb = useCallback(() => { setPopoverIsOpen((prevState) => !prevState); setExpandAdvancedOptions(false); // we always want setExpandAdvancedOptions collapsed by default when popover opened }, []); const onChangeDataView = useCallback( - (newSelectedDataView: string, newSelectedPatterns: string[]) => { + ( + newSelectedDataView: string, + newSelectedPatterns: string[], + shouldValidateSelectedPatterns?: boolean + ) => { dispatch( sourcererActions.setSelectedDataView({ id: scopeId, selectedDataViewId: newSelectedDataView, selectedPatterns: newSelectedPatterns, + shouldValidateSelectedPatterns, }) ); }, @@ -128,11 +141,14 @@ export const Sourcerer = React.memo(({ scope: scopeId } setDataViewId(defaultDataView.id); setIndexPatternsByDataView(defaultDataView.id); setIsOnlyDetectionAlertsChecked(false); + setMissingPatterns([]); }, [defaultDataView.id, setIndexPatternsByDataView]); const handleSaveIndices = useCallback(() => { const patterns = selectedOptions.map((so) => so.label); - onChangeDataView(dataViewId, patterns); + if (dataViewId != null) { + onChangeDataView(dataViewId, patterns); + } setPopoverIsOpen(false); }, [onChangeDataView, dataViewId, selectedOptions]); @@ -140,183 +156,220 @@ export const Sourcerer = React.memo(({ scope: scopeId } setPopoverIsOpen(false); setExpandAdvancedOptions(false); }, []); - const trigger = useMemo( - () => ( - - {i18n.DATA_VIEW} - {isModified === 'modified' && {i18n.MODIFIED_BADGE_TITLE}} - {isModified === 'alerts' && ( - - {i18n.ALERTS_BADGE_TITLE} - - )} - - ), - [isTimelineSourcerer, loading, setPopoverIsOpenCb, isModified] - ); - const dataViewSelectOptions = useMemo( - () => - getDataViewSelectOptions({ - dataViewId, - defaultDataView, - isModified: isModified === 'modified', - isOnlyDetectionAlerts, - kibanaDataViews, - }), - [dataViewId, defaultDataView, isModified, isOnlyDetectionAlerts, kibanaDataViews] - ); + // deprecated timeline index pattern handlers + const onContinueUpdateDeprecated = useCallback(() => { + setIsShowingUpdateModal(false); + const patterns = selectedPatterns.filter((pattern) => + defaultDataView.patternList.includes(pattern) + ); + onChangeDataView(defaultDataView.id, patterns); + setPopoverIsOpen(false); + }, [defaultDataView.id, defaultDataView.patternList, onChangeDataView, selectedPatterns]); + + const onUpdateDeprecated = useCallback(() => { + // are all the patterns in the default? + if (missingPatterns.length === 0) { + onContinueUpdateDeprecated(); + } else { + // open modal + setIsShowingUpdateModal(true); + } + }, [missingPatterns, onContinueUpdateDeprecated]); + + const [isTriggerDisabled, setIsTriggerDisabled] = useState(false); + + const onOpenAndReset = useCallback(() => { + setPopoverIsOpen(true); + resetDataSources(); + }, [resetDataSources]); + + const updateDataView = useUpdateDataView(onOpenAndReset); + const onUpdateDataView = useCallback(async () => { + const isUiSettingsSuccess = await updateDataView(missingPatterns); + setIsShowingUpdateModal(false); + setPopoverIsOpen(false); + + if (isUiSettingsSuccess) { + onChangeDataView( + defaultDataView.id, + // to be at this stage, activePatterns is defined, the ?? selectedPatterns is to make TS happy + activePatterns ?? selectedPatterns, + false + ); + setIsTriggerDisabled(true); + } + }, [ + activePatterns, + defaultDataView.id, + missingPatterns, + onChangeDataView, + selectedPatterns, + updateDataView, + ]); useEffect(() => { - setDataViewId((prevSelectedOption) => - selectedDataViewId != null && !deepEqual(selectedDataViewId, prevSelectedOption) - ? selectedDataViewId - : prevSelectedOption - ); + setDataViewId(selectedDataViewId); }, [selectedDataViewId]); - const tooltipContent = useMemo( - () => - getTooltipContent({ - isOnlyDetectionAlerts, - isPopoverOpen, - selectedPatterns, - signalIndexName, - }), - [isPopoverOpen, isOnlyDetectionAlerts, signalIndexName, selectedPatterns] - ); - - const buttonWithTooptip = useMemo(() => { - return tooltipContent ? ( - - {trigger} - - ) : ( - trigger - ); - }, [trigger, tooltipContent]); + const onOutsideClick = useCallback(() => { + setDataViewId(selectedDataViewId); + setMissingPatterns(sourcererMissingPatterns); + }, [selectedDataViewId, sourcererMissingPatterns]); const onExpandAdvancedOptionsClicked = useCallback(() => { setExpandAdvancedOptions((prevState) => !prevState); }, []); - return indicesExist ? ( + // always show sourcerer in timeline + return indicesExist || scopeId === SourcererScopeName.timeline ? ( + } closePopover={handleClosePopOver} + data-test-subj={isTimelineSourcerer ? 'timeline-sourcerer-popover' : 'sourcerer-popover'} display="block" - repositionOnScroll + isOpen={isPopoverOpen} ownFocus + repositionOnScroll > - - - <>{i18n.SELECT_DATA_VIEW} - - {isOnlyDetectionAlerts && ( - - )} - - - {isTimelineSourcerer && ( - - - - )} - - - + + + <>{i18n.SELECT_DATA_VIEW} + + {isOnlyDetectionAlerts && ( + - - - - - - {i18n.INDEX_PATTERNS_ADVANCED_OPTIONS_TITLE} - - {expandAdvancedOptions && } - - - + )} + + {isModified === 'deprecated' || isModified === 'missingPatterns' ? ( + <> + + setIsShowingUpdateModal(false)} + onContinue={onContinueUpdateDeprecated} + onUpdate={onUpdateDataView} + /> + + ) : ( + + <> + {isTimelineSourcerer && ( + + + + )} + {dataViewId && ( + + + + )} - {!isDetectionsSourcerer && ( - - - - - {i18n.INDEX_PATTERNS_RESET} - - - - + + {i18n.INDEX_PATTERNS_ADVANCED_OPTIONS_TITLE} + + {expandAdvancedOptions && } + + - {i18n.SAVE_INDEX_PATTERNS} - - - - + isDisabled={isOnlyDetectionAlerts} + onChange={onChangeCombo} + options={allOptions} + placeholder={i18n.PICK_INDEX_PATTERNS} + renderOption={renderOption} + selectedOptions={selectedOptions} + /> + + + {!isDetectionsSourcerer && ( + + + + + {i18n.INDEX_PATTERNS_RESET} + + + + + {i18n.SAVE_INDEX_PATTERNS} + + + + + )} + + + )} - - - + + ) : null; }); diff --git a/x-pack/plugins/security_solution/public/common/components/sourcerer/refresh_button.tsx b/x-pack/plugins/security_solution/public/common/components/sourcerer/refresh_button.tsx new file mode 100644 index 0000000000000..c30c6aa2dea9c --- /dev/null +++ b/x-pack/plugins/security_solution/public/common/components/sourcerer/refresh_button.tsx @@ -0,0 +1,28 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ +import { EuiButton } from '@elastic/eui'; +import React, { useCallback } from 'react'; +import styled from 'styled-components'; + +import { RELOAD_PAGE_TITLE } from './translations'; + +const StyledRefreshButton = styled(EuiButton)` + float: right; +`; + +export const RefreshButton = React.memo(() => { + const onPageRefresh = useCallback(() => { + document.location.reload(); + }, []); + return ( + + {RELOAD_PAGE_TITLE} + + ); +}); + +RefreshButton.displayName = 'RefreshButton'; diff --git a/x-pack/plugins/security_solution/public/common/components/sourcerer/temporary.tsx b/x-pack/plugins/security_solution/public/common/components/sourcerer/temporary.tsx new file mode 100644 index 0000000000000..36fae76c7739b --- /dev/null +++ b/x-pack/plugins/security_solution/public/common/components/sourcerer/temporary.tsx @@ -0,0 +1,188 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { FormattedMessage } from '@kbn/i18n-react'; +import { + EuiCallOut, + EuiLink, + EuiText, + EuiTextColor, + EuiSpacer, + EuiFlexGroup, + EuiFlexItem, + EuiButton, + EuiToolTip, + EuiIcon, +} from '@elastic/eui'; +import React, { useMemo } from 'react'; +import * as i18n from './translations'; +import { Blockquote, ResetButton } from './helpers'; + +interface Props { + activePatterns?: string[]; + indicesExist: boolean; + isModified: 'deprecated' | 'missingPatterns'; + missingPatterns: string[]; + onClick: () => void; + onClose: () => void; + onUpdate: () => void; + selectedPatterns: string[]; +} + +const translations = { + deprecated: { + title: i18n.CALL_OUT_DEPRECATED_TITLE, + update: i18n.UPDATE_INDEX_PATTERNS, + }, + missingPatterns: { + title: i18n.CALL_OUT_MISSING_PATTERNS_TITLE, + update: i18n.ADD_INDEX_PATTERN, + }, +}; + +export const TemporarySourcerer = React.memo( + ({ + activePatterns, + indicesExist, + isModified, + onClose, + onClick, + onUpdate, + selectedPatterns, + missingPatterns, + }) => { + const trigger = useMemo( + () => ( + + {translations[isModified].update} + + ), + [indicesExist, isModified, onUpdate] + ); + const buttonWithTooltip = useMemo( + () => + !indicesExist ? ( + + {trigger} + + ) : ( + trigger + ), + [indicesExist, trigger] + ); + + const deadPatterns = + activePatterns && activePatterns.length > 0 + ? selectedPatterns.filter((p) => !activePatterns.includes(p)) + : []; + + return ( + <> + + + + +

+ {activePatterns && activePatterns.length > 0 ? ( + 0 ? ( + !activePatterns.includes(p)) + .join(', '), + }} + /> + } + > + + + ) : null, + callout:

{activePatterns.join(', ')}
, + }} + /> + ) : ( + {selectedPatterns.join(', ')}, + }} + /> + )} + + {isModified === 'deprecated' && ( + {i18n.TOGGLE_TO_NEW_SOURCERER}, + }} + /> + )} + {isModified === 'missingPatterns' && ( + <> + {missingPatterns.join(', ')}, + }} + /> + {i18n.TOGGLE_TO_NEW_SOURCERER}, + }} + /> + + )} +

+
+
+ + + + {i18n.INDEX_PATTERNS_CLOSE} + + + {buttonWithTooltip} + + + ); + } +); + +TemporarySourcerer.displayName = 'TemporarySourcerer'; diff --git a/x-pack/plugins/security_solution/public/common/components/sourcerer/translations.ts b/x-pack/plugins/security_solution/public/common/components/sourcerer/translations.ts index fcf465ebfc9ef..2d8e506f39437 100644 --- a/x-pack/plugins/security_solution/public/common/components/sourcerer/translations.ts +++ b/x-pack/plugins/security_solution/public/common/components/sourcerer/translations.ts @@ -11,6 +11,20 @@ export const CALL_OUT_TITLE = i18n.translate('xpack.securitySolution.indexPatter defaultMessage: 'Data view cannot be modified on this page', }); +export const CALL_OUT_DEPRECATED_TITLE = i18n.translate( + 'xpack.securitySolution.indexPatterns.callOutDeprecxatedTitle', + { + defaultMessage: 'This timeline uses a legacy data view selector', + } +); + +export const CALL_OUT_MISSING_PATTERNS_TITLE = i18n.translate( + 'xpack.securitySolution.indexPatterns.callOutMissingPatternsTitle', + { + defaultMessage: 'This timeline is out of date with the Security Data View', + } +); + export const CALL_OUT_TIMELINE_TITLE = i18n.translate( 'xpack.securitySolution.indexPatterns.callOutTimelineTitle', { @@ -18,9 +32,42 @@ export const CALL_OUT_TIMELINE_TITLE = i18n.translate( } ); +export const TOGGLE_TO_NEW_SOURCERER = i18n.translate( + 'xpack.securitySolution.indexPatterns.toggleToNewSourcerer.link', + { + defaultMessage: 'here', + } +); + export const DATA_VIEW = i18n.translate('xpack.securitySolution.indexPatterns.dataViewLabel', { defaultMessage: 'Data view', }); + +export const UPDATE_DATA_VIEW = i18n.translate( + 'xpack.securitySolution.indexPatterns.updateDataView', + { + defaultMessage: + 'Would you like to add this index pattern to Security Data View? Otherwise, we can recreate the data view without the missing index patterns.', + } +); + +export const UPDATE_SECURITY_DATA_VIEW = i18n.translate( + 'xpack.securitySolution.indexPatterns.updateSecurityDataView', + { + defaultMessage: 'Update Security Data View', + } +); + +export const CONTINUE_WITHOUT_ADDING = i18n.translate( + 'xpack.securitySolution.indexPatterns.continue', + { + defaultMessage: 'Continue without adding', + } +); +export const ADD_INDEX_PATTERN = i18n.translate('xpack.securitySolution.indexPatterns.add', { + defaultMessage: 'Add index pattern', +}); + export const MODIFIED_BADGE_TITLE = i18n.translate( 'xpack.securitySolution.indexPatterns.modifiedBadgeTitle', { @@ -35,6 +82,13 @@ export const ALERTS_BADGE_TITLE = i18n.translate( } ); +export const DEPRECATED_BADGE_TITLE = i18n.translate( + 'xpack.securitySolution.indexPatterns.updateAvailableBadgeTitle', + { + defaultMessage: 'Update available', + } +); + export const SECURITY_DEFAULT_DATA_VIEW_LABEL = i18n.translate( 'xpack.securitySolution.indexPatterns.securityDefaultDataViewLabel', { @@ -97,6 +151,14 @@ export const DISABLED_INDEX_PATTERNS = i18n.translate( } ); +export const DISABLED_SOURCERER = i18n.translate('xpack.securitySolution.sourcerer.disabled', { + defaultMessage: 'The updates to the Data view require a page reload to take effect.', +}); + +export const UPDATE_INDEX_PATTERNS = i18n.translate('xpack.securitySolution.indexPatterns.update', { + defaultMessage: 'Update and recreate data view', +}); + export const INDEX_PATTERNS_RESET = i18n.translate( 'xpack.securitySolution.indexPatterns.resetButton', { @@ -104,6 +166,22 @@ export const INDEX_PATTERNS_RESET = i18n.translate( } ); +export const INDEX_PATTERNS_CLOSE = i18n.translate( + 'xpack.securitySolution.indexPatterns.closeButton', + { + defaultMessage: 'Close', + } +); + +export const INACTIVE_PATTERNS = i18n.translate('xpack.securitySolution.indexPatterns.inactive', { + defaultMessage: 'Inactive index patterns', +}); + +export const NO_DATA = i18n.translate('xpack.securitySolution.indexPatterns.noData', { + defaultMessage: + "The index pattern on this timeline doesn't match any data streams, indices, or index aliases.", +}); + export const PICK_INDEX_PATTERNS = i18n.translate( 'xpack.securitySolution.indexPatterns.pickIndexPatternsCombo', { @@ -117,3 +195,24 @@ export const ALERTS_CHECKBOX_LABEL = i18n.translate( defaultMessage: 'Show only detection alerts', } ); + +export const SUCCESS_TOAST_TITLE = i18n.translate( + 'xpack.securitySolution.indexPatterns.successToastTitle', + { + defaultMessage: 'One or more settings require you to reload the page to take effect', + } +); + +export const RELOAD_PAGE_TITLE = i18n.translate( + 'xpack.securitySolution.indexPatterns.reloadPageTitle', + { + defaultMessage: 'Reload page', + } +); + +export const FAILURE_TOAST_TITLE = i18n.translate( + 'xpack.securitySolution.indexPatterns.failureToastTitle', + { + defaultMessage: 'Unable to update data view', + } +); diff --git a/x-pack/plugins/security_solution/public/common/components/sourcerer/trigger.tsx b/x-pack/plugins/security_solution/public/common/components/sourcerer/trigger.tsx new file mode 100644 index 0000000000000..a464036f3b138 --- /dev/null +++ b/x-pack/plugins/security_solution/public/common/components/sourcerer/trigger.tsx @@ -0,0 +1,116 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import React, { FC, memo, useMemo } from 'react'; +import { EuiToolTip } from '@elastic/eui'; +import * as i18n from './translations'; +import { getTooltipContent, StyledBadge, StyledButton } from './helpers'; +import { ModifiedTypes } from './use_pick_index_patterns'; + +interface Props { + activePatterns?: string[]; + disabled: boolean; + isModified: ModifiedTypes; + isOnlyDetectionAlerts: boolean; + isPopoverOpen: boolean; + isTimelineSourcerer: boolean; + loading: boolean; + onClick: () => void; + selectedPatterns: string[]; + signalIndexName: string | null; +} +export const TriggerComponent: FC = ({ + activePatterns, + disabled, + isModified, + isOnlyDetectionAlerts, + isPopoverOpen, + isTimelineSourcerer, + loading, + onClick, + selectedPatterns, + signalIndexName, +}) => { + const badge = useMemo(() => { + switch (isModified) { + case 'modified': + return {i18n.MODIFIED_BADGE_TITLE}; + case 'alerts': + return ( + + {i18n.ALERTS_BADGE_TITLE} + + ); + case 'deprecated': + return ( + + {i18n.DEPRECATED_BADGE_TITLE} + + ); + case 'missingPatterns': + return ( + + {i18n.DEPRECATED_BADGE_TITLE} + + ); + case '': + default: + return null; + } + }, [isModified]); + + const trigger = useMemo( + () => ( + + {i18n.DATA_VIEW} + {!disabled && badge} + + ), + [disabled, badge, isTimelineSourcerer, loading, onClick] + ); + + const tooltipContent = useMemo( + () => + disabled + ? i18n.DISABLED_SOURCERER + : getTooltipContent({ + isOnlyDetectionAlerts, + isPopoverOpen, + // if activePatterns, use because we are in the temporary sourcerer state + selectedPatterns: activePatterns ?? selectedPatterns, + signalIndexName, + }), + [ + activePatterns, + disabled, + isOnlyDetectionAlerts, + isPopoverOpen, + selectedPatterns, + signalIndexName, + ] + ); + + return tooltipContent ? ( + + {trigger} + + ) : ( + trigger + ); +}; + +export const Trigger = memo(TriggerComponent); diff --git a/x-pack/plugins/security_solution/public/common/components/sourcerer/update_default_data_view_modal.tsx b/x-pack/plugins/security_solution/public/common/components/sourcerer/update_default_data_view_modal.tsx new file mode 100644 index 0000000000000..78fc6f82fa748 --- /dev/null +++ b/x-pack/plugins/security_solution/public/common/components/sourcerer/update_default_data_view_modal.tsx @@ -0,0 +1,96 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { FormattedMessage } from '@kbn/i18n-react'; +import { + EuiButton, + EuiFlexGroup, + EuiFlexItem, + EuiModal, + EuiModalBody, + EuiModalHeader, + EuiModalHeaderTitle, + EuiText, + EuiTextColor, +} from '@elastic/eui'; +import React from 'react'; +import styled from 'styled-components'; +import * as i18n from './translations'; +import { Blockquote, ResetButton } from './helpers'; + +interface Props { + isShowing: boolean; + missingPatterns: string[]; + onClose: () => void; + onContinue: () => void; + onUpdate: () => void; +} +const MyEuiModal = styled(EuiModal)` + .euiModal__flex { + width: 60vw; + } + .euiCodeBlock { + height: auto !important; + max-width: 718px; + } + z-index: 99999999; +`; + +export const UpdateDefaultDataViewModal = React.memo( + ({ isShowing, onClose, onContinue, onUpdate, missingPatterns }) => + isShowing ? ( + + + +

{i18n.UPDATE_SECURITY_DATA_VIEW}

+
+
+ + + +

+ {missingPatterns.join(', ')}, + }} + /> + {i18n.UPDATE_DATA_VIEW} +

+
+
+ + + + {i18n.CONTINUE_WITHOUT_ADDING} + + + + + {i18n.ADD_INDEX_PATTERN} + + + +
+
+ ) : null +); + +UpdateDefaultDataViewModal.displayName = 'UpdateDefaultDataViewModal'; diff --git a/x-pack/plugins/security_solution/public/common/components/sourcerer/use_pick_index_patterns.tsx b/x-pack/plugins/security_solution/public/common/components/sourcerer/use_pick_index_patterns.tsx index 2ed2319499398..d7b094ab27b14 100644 --- a/x-pack/plugins/security_solution/public/common/components/sourcerer/use_pick_index_patterns.tsx +++ b/x-pack/plugins/security_solution/public/common/components/sourcerer/use_pick_index_patterns.tsx @@ -6,29 +6,31 @@ */ import React, { useCallback, useEffect, useMemo, useState } from 'react'; -import { EuiComboBoxOptionOption } from '@elastic/eui'; +import { EuiComboBoxOptionOption, EuiSuperSelectOption } from '@elastic/eui'; import { getScopePatternListSelection } from '../../store/sourcerer/helpers'; import { sourcererModel } from '../../store/sourcerer'; -import { getPatternListWithoutSignals } from './helpers'; +import { getDataViewSelectOptions, getPatternListWithoutSignals } from './helpers'; import { SourcererScopeName } from '../../store/sourcerer/model'; interface UsePickIndexPatternsProps { - dataViewId: string; + dataViewId: string | null; defaultDataViewId: string; isOnlyDetectionAlerts: boolean; kibanaDataViews: sourcererModel.SourcererModel['kibanaDataViews']; + missingPatterns: string[]; scopeId: sourcererModel.SourcererScopeName; selectedPatterns: string[]; signalIndexName: string | null; } -export type ModifiedTypes = 'modified' | 'alerts' | ''; +export type ModifiedTypes = 'modified' | 'alerts' | 'deprecated' | 'missingPatterns' | ''; interface UsePickIndexPatterns { + allOptions: Array>; + dataViewSelectOptions: Array>; isModified: ModifiedTypes; onChangeCombo: (newSelectedDataViewId: Array>) => void; renderOption: ({ value }: EuiComboBoxOptionOption) => React.ReactElement; - selectableOptions: Array>; selectedOptions: Array>; setIndexPatternsByDataView: (newSelectedDataViewId: string, isAlerts?: boolean) => void; } @@ -45,6 +47,7 @@ export const usePickIndexPatterns = ({ defaultDataViewId, isOnlyDetectionAlerts, kibanaDataViews, + missingPatterns, scopeId, selectedPatterns, signalIndexName, @@ -54,42 +57,44 @@ export const usePickIndexPatterns = ({ [signalIndexName] ); - const { patternList, selectablePatterns } = useMemo(() => { + const { allPatterns, selectablePatterns } = useMemo<{ + allPatterns: string[]; + selectablePatterns: string[]; + }>(() => { if (isOnlyDetectionAlerts && signalIndexName) { return { - patternList: [signalIndexName], + allPatterns: [signalIndexName], selectablePatterns: [signalIndexName], }; } const theDataView = kibanaDataViews.find((dataView) => dataView.id === dataViewId); - return theDataView != null - ? scopeId === sourcererModel.SourcererScopeName.default - ? { - patternList: getPatternListWithoutSignals( - theDataView.title - .split(',') - // remove duplicates patterns from selector - .filter((pattern, i, self) => self.indexOf(pattern) === i), - signalIndexName - ), - selectablePatterns: getPatternListWithoutSignals( - theDataView.patternList, - signalIndexName - ), - } - : { - patternList: theDataView.title - .split(',') - // remove duplicates patterns from selector - .filter((pattern, i, self) => self.indexOf(pattern) === i), - selectablePatterns: theDataView.patternList, - } - : { patternList: [], selectablePatterns: [] }; + + if (theDataView == null) { + return { + allPatterns: [], + selectablePatterns: [], + }; + } + + const titleAsList = [...new Set(theDataView.title.split(','))]; + + return scopeId === sourcererModel.SourcererScopeName.default + ? { + allPatterns: getPatternListWithoutSignals(titleAsList, signalIndexName), + selectablePatterns: getPatternListWithoutSignals( + theDataView.patternList, + signalIndexName + ), + } + : { + allPatterns: titleAsList, + selectablePatterns: theDataView.patternList, + }; }, [dataViewId, isOnlyDetectionAlerts, kibanaDataViews, scopeId, signalIndexName]); - const selectableOptions = useMemo( - () => patternListToOptions(patternList, selectablePatterns), - [patternList, selectablePatterns] + const allOptions = useMemo( + () => patternListToOptions(allPatterns, selectablePatterns), + [allPatterns, selectablePatterns] ); const [selectedOptions, setSelectedOptions] = useState>>( isOnlyDetectionAlerts ? alertsOptions : patternListToOptions(selectedPatterns) @@ -111,37 +116,50 @@ export const usePickIndexPatterns = ({ ); const defaultSelectedPatternsAsOptions = useMemo( - () => getDefaultSelectedOptionsByDataView(dataViewId), + () => (dataViewId != null ? getDefaultSelectedOptionsByDataView(dataViewId) : []), [dataViewId, getDefaultSelectedOptionsByDataView] ); - const [isModified, setIsModified] = useState<'modified' | 'alerts' | ''>(''); + const [isModified, setIsModified] = useState( + dataViewId == null ? 'deprecated' : missingPatterns.length > 0 ? 'missingPatterns' : '' + ); const onSetIsModified = useCallback( - (patterns?: string[]) => { + (patterns: string[], id: string | null) => { + if (id == null) { + return setIsModified('deprecated'); + } + if (missingPatterns.length > 0) { + return setIsModified('missingPatterns'); + } if (isOnlyDetectionAlerts) { return setIsModified('alerts'); } - const modifiedPatterns = patterns != null ? patterns : selectedPatterns; const isPatternsModified = - defaultSelectedPatternsAsOptions.length !== modifiedPatterns.length || + defaultSelectedPatternsAsOptions.length !== patterns.length || !defaultSelectedPatternsAsOptions.every((option) => - modifiedPatterns.find((pattern) => option.value === pattern) + patterns.find((pattern) => option.value === pattern) ); return setIsModified(isPatternsModified ? 'modified' : ''); }, - [defaultSelectedPatternsAsOptions, isOnlyDetectionAlerts, selectedPatterns] + [defaultSelectedPatternsAsOptions, isOnlyDetectionAlerts, missingPatterns.length] ); - // when scope updates, check modified to set/remove alerts label useEffect(() => { setSelectedOptions( scopeId === SourcererScopeName.detections ? alertsOptions : patternListToOptions(selectedPatterns) ); - onSetIsModified(selectedPatterns); // eslint-disable-next-line react-hooks/exhaustive-deps - }, [scopeId, selectedPatterns]); + }, [selectedPatterns, scopeId]); + // when scope updates, check modified to set/remove alerts label + useEffect(() => { + onSetIsModified( + selectedOptions.map(({ label }) => label), + dataViewId + ); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [dataViewId, missingPatterns, scopeId, selectedOptions]); const onChangeCombo = useCallback((newSelectedOptions) => { setSelectedOptions(newSelectedOptions); @@ -156,11 +174,26 @@ export const usePickIndexPatterns = ({ setSelectedOptions(getDefaultSelectedOptionsByDataView(newSelectedDataViewId, isAlerts)); }; + const dataViewSelectOptions = useMemo( + () => + dataViewId != null + ? getDataViewSelectOptions({ + dataViewId, + defaultDataViewId, + isModified: isModified === 'modified', + isOnlyDetectionAlerts, + kibanaDataViews, + }) + : [], + [dataViewId, defaultDataViewId, isModified, isOnlyDetectionAlerts, kibanaDataViews] + ); + return { + allOptions, + dataViewSelectOptions, isModified, onChangeCombo, renderOption, - selectableOptions, selectedOptions, setIndexPatternsByDataView, }; diff --git a/x-pack/plugins/security_solution/public/common/components/sourcerer/use_update_data_view.test.tsx b/x-pack/plugins/security_solution/public/common/components/sourcerer/use_update_data_view.test.tsx new file mode 100644 index 0000000000000..4ec39a60a97b9 --- /dev/null +++ b/x-pack/plugins/security_solution/public/common/components/sourcerer/use_update_data_view.test.tsx @@ -0,0 +1,95 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { renderHook } from '@testing-library/react-hooks'; +import { useUpdateDataView } from './use_update_data_view'; +import { useKibana } from '../../lib/kibana'; +import * as i18n from './translations'; +const mockAddSuccess = jest.fn(); +const mockAddError = jest.fn(); +const mockSet = jest.fn(); +const mockPatterns = ['packetbeat-*', 'winlogbeat-*']; +jest.mock('../../hooks/use_app_toasts', () => { + const original = jest.requireActual('../../hooks/use_app_toasts'); + + return { + ...original, + useAppToasts: () => ({ + addSuccess: mockAddSuccess, + addError: mockAddError, + }), + }; +}); +jest.mock('../../lib/kibana'); +jest.mock('../../../../../../../src/plugins/kibana_react/public', () => { + const original = jest.requireActual('../../../../../../../src/plugins/kibana_react/public'); + + return { + ...original, + toMountPoint: jest.fn(), + }; +}); +describe('use_update_data_view', () => { + const mockError = jest.fn(); + beforeEach(() => { + (useKibana as jest.Mock).mockImplementation(() => ({ + services: { + uiSettings: { + get: () => mockPatterns, + set: mockSet.mockResolvedValue(true), + }, + }, + })); + jest.clearAllMocks(); + }); + + test('Successful uiSettings updates with correct index pattern, and shows success toast', async () => { + const { result } = renderHook(() => useUpdateDataView(mockError)); + const updateDataView = result.current; + const isUiSettingsSuccess = await updateDataView(['missing-*']); + expect(mockSet.mock.calls[0][1]).toEqual([...mockPatterns, 'missing-*'].sort()); + expect(isUiSettingsSuccess).toEqual(true); + expect(mockAddSuccess).toHaveBeenCalled(); + }); + + test('Failed uiSettings update returns false and shows error toast', async () => { + (useKibana as jest.Mock).mockImplementation(() => ({ + services: { + uiSettings: { + get: () => mockPatterns, + set: mockSet.mockResolvedValue(false), + }, + }, + })); + const { result } = renderHook(() => useUpdateDataView(mockError)); + const updateDataView = result.current; + const isUiSettingsSuccess = await updateDataView(['missing-*']); + expect(mockSet.mock.calls[0][1]).toEqual([...mockPatterns, 'missing-*'].sort()); + expect(isUiSettingsSuccess).toEqual(false); + expect(mockAddError).toHaveBeenCalled(); + expect(mockAddError.mock.calls[0][0]).toEqual(new Error(i18n.FAILURE_TOAST_TITLE)); + }); + + test('Failed uiSettings throws error and shows error toast', async () => { + (useKibana as jest.Mock).mockImplementation(() => ({ + services: { + uiSettings: { + get: jest.fn().mockImplementation(() => { + throw new Error('Uh oh bad times over here'); + }), + set: mockSet.mockResolvedValue(true), + }, + }, + })); + const { result } = renderHook(() => useUpdateDataView(mockError)); + const updateDataView = result.current; + const isUiSettingsSuccess = await updateDataView(['missing-*']); + expect(isUiSettingsSuccess).toEqual(false); + expect(mockAddError).toHaveBeenCalled(); + expect(mockAddError.mock.calls[0][0]).toEqual(new Error('Uh oh bad times over here')); + }); +}); diff --git a/x-pack/plugins/security_solution/public/common/components/sourcerer/use_update_data_view.tsx b/x-pack/plugins/security_solution/public/common/components/sourcerer/use_update_data_view.tsx new file mode 100644 index 0000000000000..68193942ea257 --- /dev/null +++ b/x-pack/plugins/security_solution/public/common/components/sourcerer/use_update_data_view.tsx @@ -0,0 +1,72 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import React, { useCallback } from 'react'; +import { EuiLink } from '@elastic/eui'; +import { FormattedMessage } from '@kbn/i18n-react'; +import { useKibana } from '../../lib/kibana'; +import { DEFAULT_INDEX_KEY } from '../../../../common/constants'; +import { ensurePatternFormat } from '../../store/sourcerer/helpers'; +import { toMountPoint } from '../../../../../../../src/plugins/kibana_react/public'; +import * as i18n from './translations'; +import { RefreshButton } from './refresh_button'; +import { useAppToasts } from '../../hooks/use_app_toasts'; + +export const useUpdateDataView = ( + onOpenAndReset: () => void +): ((missingPatterns: string[]) => Promise) => { + const { uiSettings } = useKibana().services; + const { addSuccess, addError } = useAppToasts(); + return useCallback( + async (missingPatterns: string[]): Promise => { + const asyncSearch = async (): Promise<[boolean, Error | null]> => { + try { + const defaultPatterns = uiSettings.get(DEFAULT_INDEX_KEY); + const uiSettingsIndexPattern = [...defaultPatterns, ...missingPatterns]; + const isSuccess = await uiSettings.set( + DEFAULT_INDEX_KEY, + ensurePatternFormat(uiSettingsIndexPattern) + ); + return [isSuccess, null]; + } catch (e) { + return [false, e]; + } + }; + const [isUiSettingsSuccess, possibleError] = await asyncSearch(); + if (isUiSettingsSuccess) { + addSuccess({ + color: 'success', + title: toMountPoint(i18n.SUCCESS_TOAST_TITLE), + text: toMountPoint(), + iconType: undefined, + toastLifeTimeMs: 600000, + }); + return true; + } + addError(possibleError !== null ? possibleError : new Error(i18n.FAILURE_TOAST_TITLE), { + title: i18n.FAILURE_TOAST_TITLE, + toastMessage: ( + <> + + {i18n.TOGGLE_TO_NEW_SOURCERER} + + ), + }} + /> + + ) as unknown as string, + }); + return false; + }, + [addError, addSuccess, onOpenAndReset, uiSettings] + ); +}; diff --git a/x-pack/plugins/security_solution/public/common/components/url_state/initialize_redux_by_url.tsx b/x-pack/plugins/security_solution/public/common/components/url_state/initialize_redux_by_url.tsx index 7c87aa19484bc..0f7e93f1befca 100644 --- a/x-pack/plugins/security_solution/public/common/components/url_state/initialize_redux_by_url.tsx +++ b/x-pack/plugins/security_solution/public/common/components/url_state/initialize_redux_by_url.tsx @@ -64,7 +64,7 @@ export const useSetInitialStateFromUrl = () => { dispatch( sourcererActions.setSelectedDataView({ id: scope, - selectedDataViewId: sourcererState[scope]?.id ?? '', + selectedDataViewId: sourcererState[scope]?.id ?? null, selectedPatterns: sourcererState[scope]?.selectedPatterns ?? [], }) ) diff --git a/x-pack/plugins/security_solution/public/common/containers/hosts_risk/use_hosts_risk_score.ts b/x-pack/plugins/security_solution/public/common/containers/hosts_risk/use_hosts_risk_score.ts index 41fcd29191da2..debdacb570ad0 100644 --- a/x-pack/plugins/security_solution/public/common/containers/hosts_risk/use_hosts_risk_score.ts +++ b/x-pack/plugins/security_solution/public/common/containers/hosts_risk/use_hosts_risk_score.ts @@ -13,10 +13,10 @@ import { useAppToasts } from '../../hooks/use_app_toasts'; import { useKibana } from '../../lib/kibana'; import { inputsActions } from '../../store/actions'; import { isIndexNotFoundError } from '../../utils/exceptions'; -import { HostsRiskScore } from '../../../../common/search_strategy'; +import { getHostRiskIndex, HostsRiskScore } from '../../../../common/search_strategy'; + import { useHostsRiskScoreComplete } from './use_hosts_risk_score_complete'; import { useIsExperimentalFeatureEnabled } from '../../hooks/use_experimental_features'; -import { getHostRiskIndex } from '../../../helpers'; export const QUERY_ID = 'host_risk_score'; const noop = () => {}; @@ -104,7 +104,7 @@ export const useHostsRiskScore = ({ timerange: timerange ? { to: timerange.to, from: timerange.from, interval: '' } : undefined, - hostName, + hostNames: hostName ? [hostName] : undefined, defaultIndex: [getHostRiskIndex(space.id)], }); }); diff --git a/x-pack/plugins/security_solution/public/common/containers/hosts_risk/use_hosts_risk_score_complete.ts b/x-pack/plugins/security_solution/public/common/containers/hosts_risk/use_hosts_risk_score_complete.ts index 934cb88ee0d86..6faaa3c8f08db 100644 --- a/x-pack/plugins/security_solution/public/common/containers/hosts_risk/use_hosts_risk_score_complete.ts +++ b/x-pack/plugins/security_solution/public/common/containers/hosts_risk/use_hosts_risk_score_complete.ts @@ -28,7 +28,7 @@ export const getHostsRiskScore = ({ data, defaultIndex, timerange, - hostName, + hostNames, signal, }: GetHostsRiskScoreProps): Observable => data.search.search( @@ -36,7 +36,7 @@ export const getHostsRiskScore = ({ defaultIndex, factoryQueryType: HostsQueries.hostsRiskScore, timerange, - hostName, + hostNames, }, { strategy: 'securitySolutionSearchStrategy', diff --git a/x-pack/plugins/security_solution/public/common/containers/sourcerer/index.tsx b/x-pack/plugins/security_solution/public/common/containers/sourcerer/index.tsx index 3311207eb1420..c493cb528d09a 100644 --- a/x-pack/plugins/security_solution/public/common/containers/sourcerer/index.tsx +++ b/x-pack/plugins/security_solution/public/common/containers/sourcerer/index.tsx @@ -5,12 +5,16 @@ * 2.0. */ -import { useCallback, useEffect, useMemo, useRef } from 'react'; +import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import { useDispatch } from 'react-redux'; import { i18n } from '@kbn/i18n'; import { matchPath } from 'react-router-dom'; import { sourcererActions, sourcererSelectors } from '../../store/sourcerer'; -import { SelectedDataView, SourcererScopeName } from '../../store/sourcerer/model'; +import { + SelectedDataView, + SourcererDataView, + SourcererScopeName, +} from '../../store/sourcerer/model'; import { useUserInfo } from '../../../detections/components/user_info'; import { timelineSelectors } from '../../../timelines/store/timeline'; import { @@ -28,6 +32,7 @@ import { checkIfIndicesExist, getScopePatternListSelection } from '../../store/s import { useAppToasts } from '../../hooks/use_app_toasts'; import { postSourcererDataView } from './api'; import { useDataView } from '../source/use_data_view'; +import { useFetchIndex } from '../source'; export const useInitSourcerer = ( scopeId: SourcererScopeName.default | SourcererScopeName.detections = SourcererScopeName.default @@ -37,11 +42,14 @@ export const useInitSourcerer = ( const initialTimelineSourcerer = useRef(true); const initialDetectionSourcerer = useRef(true); const { loading: loadingSignalIndex, isSignalIndexExists, signalIndexName } = useUserInfo(); - const getDefaultDataViewSelector = useMemo( - () => sourcererSelectors.defaultDataViewSelector(), + + const getDataViewsSelector = useMemo( + () => sourcererSelectors.getSourcererDataViewsSelector(), [] ); - const defaultDataView = useDeepEqualSelector(getDefaultDataViewSelector); + const { defaultDataView, signalIndexName: signalIndexNameSourcerer } = useDeepEqualSelector( + (state) => getDataViewsSelector(state) + ); const { addError } = useAppToasts(); @@ -59,12 +67,6 @@ export const useInitSourcerer = ( } }, [addError, defaultDataView.error]); - const getSignalIndexNameSelector = useMemo( - () => sourcererSelectors.signalIndexNameSelector(), - [] - ); - const signalIndexNameSourcerer = useDeepEqualSelector(getSignalIndexNameSelector); - const getTimelineSelector = useMemo(() => timelineSelectors.getTimelineByIdSelector(), []); const activeTimeline = useDeepEqualSelector((state) => getTimelineSelector(state, TimelineId.active) @@ -256,14 +258,26 @@ export const EXCLUDE_ELASTIC_CLOUD_INDEX = '-*elastic-cloud-logs-*'; export const useSourcererDataView = ( scopeId: SourcererScopeName = SourcererScopeName.default ): SelectedDataView => { - const sourcererScopeSelector = useMemo(() => sourcererSelectors.getSourcererScopeSelector(), []); + const { getDataViewsSelector, getSourcererDataViewSelector, getScopeSelector } = useMemo( + () => ({ + getDataViewsSelector: sourcererSelectors.getSourcererDataViewsSelector(), + getSourcererDataViewSelector: sourcererSelectors.sourcererDataViewSelector(), + getScopeSelector: sourcererSelectors.scopeIdSelector(), + }), + [] + ); const { signalIndexName, - sourcererDataView: selectedDataView, - sourcererScope: { selectedPatterns: scopeSelectedPatterns, loading }, - }: sourcererSelectors.SourcererScopeSelector = useDeepEqualSelector((state) => - sourcererScopeSelector(state, scopeId) - ); + selectedDataView, + sourcererScope: { missingPatterns, selectedPatterns: scopeSelectedPatterns, loading }, + }: sourcererSelectors.SourcererScopeSelector = useDeepEqualSelector((state) => { + const sourcererScope = getScopeSelector(state, scopeId); + return { + ...getDataViewsSelector(state), + selectedDataView: getSourcererDataViewSelector(state, sourcererScope.selectedDataViewId), + sourcererScope, + }; + }); const selectedPatterns = useMemo( () => @@ -273,40 +287,69 @@ export const useSourcererDataView = ( [scopeSelectedPatterns] ); + const [legacyPatterns, setLegacyPatterns] = useState([]); + + const [indexPatternsLoading, fetchIndexReturn] = useFetchIndex(legacyPatterns); + + const legacyDataView: Omit & { id: string | null } = useMemo( + () => ({ + ...fetchIndexReturn, + runtimeMappings: {}, + title: '', + id: selectedDataView?.id ?? null, + loading: indexPatternsLoading, + patternList: fetchIndexReturn.indexes, + indexFields: fetchIndexReturn.indexPatterns + .fields as SelectedDataView['indexPattern']['fields'], + }), + [fetchIndexReturn, indexPatternsLoading, selectedDataView] + ); + + useEffect(() => { + if (selectedDataView == null || missingPatterns.length > 0) { + // old way of fetching indices, legacy timeline + setLegacyPatterns(selectedPatterns); + } else { + setLegacyPatterns([]); + } + }, [missingPatterns, selectedDataView, selectedPatterns]); + + const sourcererDataView = useMemo( + () => + selectedDataView == null || missingPatterns.length > 0 ? legacyDataView : selectedDataView, + [legacyDataView, missingPatterns.length, selectedDataView] + ); + const indicesExist = useMemo( - () => checkIfIndicesExist({ scopeId, signalIndexName, sourcererDataView: selectedDataView }), - [scopeId, signalIndexName, selectedDataView] + () => + checkIfIndicesExist({ + scopeId, + signalIndexName, + patternList: sourcererDataView.patternList, + }), + [scopeId, signalIndexName, sourcererDataView] ); return useMemo( () => ({ - browserFields: selectedDataView.browserFields, - dataViewId: selectedDataView.id, - docValueFields: selectedDataView.docValueFields, + browserFields: sourcererDataView.browserFields, + dataViewId: sourcererDataView.id, + docValueFields: sourcererDataView.docValueFields, indexPattern: { - fields: selectedDataView.indexFields, + fields: sourcererDataView.indexFields, title: selectedPatterns.join(','), }, indicesExist, - loading: loading || selectedDataView.loading, - runtimeMappings: selectedDataView.runtimeMappings, + loading: loading || sourcererDataView.loading, + runtimeMappings: sourcererDataView.runtimeMappings, // all active & inactive patterns in DATA_VIEW - patternList: selectedDataView.title.split(','), - // selected patterns in DATA_VIEW + patternList: sourcererDataView.title.split(','), + // selected patterns in DATA_VIEW including filter selectedPatterns: selectedPatterns.sort(), + // if we have to do an update to data view, tell us which patterns are active + ...(legacyPatterns.length > 0 ? { activePatterns: sourcererDataView.patternList } : {}), }), - [ - selectedDataView.browserFields, - selectedDataView.id, - selectedDataView.docValueFields, - selectedDataView.indexFields, - selectedDataView.loading, - selectedDataView.runtimeMappings, - selectedDataView.title, - selectedPatterns, - indicesExist, - loading, - ] + [sourcererDataView, selectedPatterns, indicesExist, loading, legacyPatterns.length] ); }; diff --git a/x-pack/plugins/security_solution/public/common/mock/timeline_results.ts b/x-pack/plugins/security_solution/public/common/mock/timeline_results.ts index 0f814d758e7f5..2de29a8c3acf8 100644 --- a/x-pack/plugins/security_solution/public/common/mock/timeline_results.ts +++ b/x-pack/plugins/security_solution/public/common/mock/timeline_results.ts @@ -1955,7 +1955,7 @@ export const mockTimelineModel: TimelineModel = { columns: mockTimelineModelColumns, defaultColumns: mockTimelineModelColumns, dataProviders: [], - dataViewId: '', + dataViewId: null, dateRange: { end: '2020-03-18T13:52:38.929Z', start: '2020-03-18T13:46:38.929Z', @@ -2092,7 +2092,7 @@ export const defaultTimelineProps: CreateTimelineProps = { queryMatch: { field: '_id', operator: ':', value: '1' }, }, ], - dataViewId: '', + dataViewId: null, dateRange: { end: '2018-11-05T19:03:25.937Z', start: '2018-11-05T18:58:25.937Z' }, deletedEventIds: [], description: '', diff --git a/x-pack/plugins/security_solution/public/common/store/reducer.test.ts b/x-pack/plugins/security_solution/public/common/store/reducer.test.ts index 1cbf08c354b33..e46a4a532d701 100644 --- a/x-pack/plugins/security_solution/public/common/store/reducer.test.ts +++ b/x-pack/plugins/security_solution/public/common/store/reducer.test.ts @@ -8,7 +8,7 @@ import { parseExperimentalConfigValue } from '../../../common/experimental_features'; import { SecuritySubPlugins } from '../../app/types'; import { createInitialState } from './reducer'; -import { mockSourcererState } from '../mock'; +import { mockIndexPattern, mockSourcererState } from '../mock'; import { useSourcererDataView } from '../containers/sourcerer'; import { useDeepEqualSelector } from '../hooks/use_selector'; import { renderHook } from '@testing-library/react-hooks'; @@ -19,6 +19,12 @@ jest.mock('../lib/kibana', () => ({ get: jest.fn(() => ({ uiSettings: { get: () => ({ from: 'now-24h', to: 'now' }) } })), }, })); +jest.mock('../containers/source', () => ({ + useFetchIndex: () => [ + false, + { indexes: [], indicesExist: true, indexPatterns: mockIndexPattern }, + ], +})); describe('createInitialState', () => { describe('sourcerer -> default -> indicesExist', () => { @@ -40,20 +46,24 @@ describe('createInitialState', () => { (useDeepEqualSelector as jest.Mock).mockClear(); }); - test('indicesExist should be TRUE if configIndexPatterns is NOT empty', async () => { + test('indicesExist should be TRUE if patternList is NOT empty', async () => { const { result } = renderHook(() => useSourcererDataView()); expect(result.current.indicesExist).toEqual(true); }); - test('indicesExist should be FALSE if configIndexPatterns is empty', () => { + test('indicesExist should be FALSE if patternList is empty', () => { const state = createInitialState(mockPluginState, { ...defaultState, defaultDataView: { ...defaultState.defaultDataView, - id: '', - title: '', patternList: [], }, + kibanaDataViews: [ + { + ...defaultState.defaultDataView, + patternList: [], + }, + ], }); (useDeepEqualSelector as jest.Mock).mockImplementation((cb) => cb(state)); const { result } = renderHook(() => useSourcererDataView()); diff --git a/x-pack/plugins/security_solution/public/common/store/sourcerer/actions.ts b/x-pack/plugins/security_solution/public/common/store/sourcerer/actions.ts index aa0689de9cca3..6a3d3e71f3750 100644 --- a/x-pack/plugins/security_solution/public/common/store/sourcerer/actions.ts +++ b/x-pack/plugins/security_solution/public/common/store/sourcerer/actions.ts @@ -6,9 +6,8 @@ */ import actionCreatorFactory from 'typescript-fsa'; -import { TimelineEventsType } from '../../../../common/types/timeline'; -import { SourcererDataView, SourcererScopeName } from './model'; +import { SelectedDataView, SourcererDataView, SourcererScopeName } from './model'; import { SecurityDataView } from '../../containers/sourcerer/api'; const actionCreator = actionCreatorFactory('x-pack/security_solution/local/sourcerer'); @@ -39,8 +38,8 @@ export const setSourcererScopeLoading = actionCreator<{ export interface SelectedDataViewPayload { id: SourcererScopeName; - selectedDataViewId: string; - selectedPatterns: string[]; - eventType?: TimelineEventsType; + selectedDataViewId: SelectedDataView['dataViewId']; + selectedPatterns: SelectedDataView['selectedPatterns']; + shouldValidateSelectedPatterns?: boolean; } export const setSelectedDataView = actionCreator('SET_SELECTED_DATA_VIEW'); diff --git a/x-pack/plugins/security_solution/public/common/store/sourcerer/helpers.test.ts b/x-pack/plugins/security_solution/public/common/store/sourcerer/helpers.test.ts index 5945b453673c3..672ecb575ce79 100644 --- a/x-pack/plugins/security_solution/public/common/store/sourcerer/helpers.test.ts +++ b/x-pack/plugins/security_solution/public/common/store/sourcerer/helpers.test.ts @@ -69,7 +69,7 @@ describe('sourcerer store helpers', () => { selectedPatterns: ['auditbeat-*'], }; it('sets selectedPattern', () => { - const result = validateSelectedPatterns(mockGlobalState.sourcerer, payload); + const result = validateSelectedPatterns(mockGlobalState.sourcerer, payload, true); expect(result).toEqual({ [SourcererScopeName.default]: { ...mockGlobalState.sourcerer.sourcererScopes[SourcererScopeName.default], @@ -78,10 +78,14 @@ describe('sourcerer store helpers', () => { }); }); it('sets to default when empty array is passed and scope is default', () => { - const result = validateSelectedPatterns(mockGlobalState.sourcerer, { - ...payload, - selectedPatterns: [], - }); + const result = validateSelectedPatterns( + mockGlobalState.sourcerer, + { + ...payload, + selectedPatterns: [], + }, + true + ); expect(result).toEqual({ [SourcererScopeName.default]: { ...mockGlobalState.sourcerer.sourcererScopes[SourcererScopeName.default], @@ -90,11 +94,15 @@ describe('sourcerer store helpers', () => { }); }); it('sets to default when empty array is passed and scope is detections', () => { - const result = validateSelectedPatterns(mockGlobalState.sourcerer, { - ...payload, - id: SourcererScopeName.detections, - selectedPatterns: [], - }); + const result = validateSelectedPatterns( + mockGlobalState.sourcerer, + { + ...payload, + id: SourcererScopeName.detections, + selectedPatterns: [], + }, + true + ); expect(result).toEqual({ [SourcererScopeName.detections]: { ...mockGlobalState.sourcerer.sourcererScopes[SourcererScopeName.detections], @@ -103,22 +111,21 @@ describe('sourcerer store helpers', () => { }, }); }); - it('sets to default when empty array is passed and scope is timeline', () => { - const result = validateSelectedPatterns(mockGlobalState.sourcerer, { - ...payload, - id: SourcererScopeName.timeline, - selectedPatterns: [], - }); + it('sets to empty when empty array is passed and scope is timeline', () => { + const result = validateSelectedPatterns( + mockGlobalState.sourcerer, + { + ...payload, + id: SourcererScopeName.timeline, + selectedPatterns: [], + }, + true + ); expect(result).toEqual({ [SourcererScopeName.timeline]: { ...mockGlobalState.sourcerer.sourcererScopes[SourcererScopeName.timeline], selectedDataViewId: dataView.id, - selectedPatterns: [ - signalIndexName, - ...mockGlobalState.sourcerer.defaultDataView.patternList.filter( - (p) => p !== signalIndexName - ), - ].sort(), + selectedPatterns: [], }, }); }); @@ -132,11 +139,15 @@ describe('sourcerer store helpers', () => { defaultDataView: dataViewNoSignals, kibanaDataViews: [dataViewNoSignals], }; - const result = validateSelectedPatterns(stateNoSignals, { - ...payload, - id: SourcererScopeName.timeline, - selectedPatterns: [`${mockGlobalState.sourcerer.signalIndexName}`], - }); + const result = validateSelectedPatterns( + stateNoSignals, + { + ...payload, + id: SourcererScopeName.timeline, + selectedPatterns: [`${mockGlobalState.sourcerer.signalIndexName}`], + }, + true + ); expect(result).toEqual({ [SourcererScopeName.timeline]: { ...mockGlobalState.sourcerer.sourcererScopes[SourcererScopeName.timeline], @@ -147,19 +158,23 @@ describe('sourcerer store helpers', () => { }); describe('handles missing dataViewId, 7.16 -> 8.0', () => { it('selectedPatterns.length > 0 & all selectedPatterns exist in defaultDataView, set dataViewId to defaultDataView.id', () => { - const result = validateSelectedPatterns(mockGlobalState.sourcerer, { - ...payload, - id: SourcererScopeName.timeline, - selectedDataViewId: '', - selectedPatterns: [ - mockGlobalState.sourcerer.defaultDataView.patternList[3], - mockGlobalState.sourcerer.defaultDataView.patternList[4], - ], - }); + const result = validateSelectedPatterns( + mockGlobalState.sourcerer, + { + ...payload, + id: SourcererScopeName.timeline, + selectedDataViewId: null, + selectedPatterns: [ + mockGlobalState.sourcerer.defaultDataView.patternList[3], + mockGlobalState.sourcerer.defaultDataView.patternList[4], + ], + }, + true + ); expect(result).toEqual({ [SourcererScopeName.timeline]: { ...mockGlobalState.sourcerer.sourcererScopes[SourcererScopeName.timeline], - selectedDataViewId: dataView.id, + selectedDataViewId: null, selectedPatterns: [ mockGlobalState.sourcerer.defaultDataView.patternList[3], mockGlobalState.sourcerer.defaultDataView.patternList[4], @@ -167,16 +182,20 @@ describe('sourcerer store helpers', () => { }, }); }); - it('selectedPatterns.length > 0 & a pattern in selectedPatterns does not exist in defaultDataView, set dataViewId to null', () => { - const result = validateSelectedPatterns(mockGlobalState.sourcerer, { - ...payload, - id: SourcererScopeName.timeline, - selectedDataViewId: '', - selectedPatterns: [ - mockGlobalState.sourcerer.defaultDataView.patternList[3], - 'journalbeat-*', - ], - }); + it('selectedPatterns.length > 0 & some selectedPatterns do not exist in defaultDataView, set dataViewId to null', () => { + const result = validateSelectedPatterns( + mockGlobalState.sourcerer, + { + ...payload, + id: SourcererScopeName.timeline, + selectedDataViewId: null, + selectedPatterns: [ + mockGlobalState.sourcerer.defaultDataView.patternList[3], + 'journalbeat-*', + ], + }, + true + ); expect(result).toEqual({ [SourcererScopeName.timeline]: { ...mockGlobalState.sourcerer.sourcererScopes[SourcererScopeName.timeline], @@ -185,6 +204,7 @@ describe('sourcerer store helpers', () => { mockGlobalState.sourcerer.defaultDataView.patternList[3], 'journalbeat-*', ], + missingPatterns: ['journalbeat-*'], }, }); }); diff --git a/x-pack/plugins/security_solution/public/common/store/sourcerer/helpers.ts b/x-pack/plugins/security_solution/public/common/store/sourcerer/helpers.ts index 689bf1c4502d8..7f176b0efaca4 100644 --- a/x-pack/plugins/security_solution/public/common/store/sourcerer/helpers.ts +++ b/x-pack/plugins/security_solution/public/common/store/sourcerer/helpers.ts @@ -34,54 +34,58 @@ export const getScopePatternListSelection = ( } }; +export const ensurePatternFormat = (patternList: string[]): string[] => + [ + ...new Set( + patternList.reduce((acc: string[], pattern: string) => [...pattern.split(','), ...acc], []) + ), + ].sort(); + export const validateSelectedPatterns = ( state: SourcererModel, - payload: SelectedDataViewPayload + payload: SelectedDataViewPayload, + shouldValidateSelectedPatterns: boolean ): Partial => { const { id, ...rest } = payload; - let dataView = state.kibanaDataViews.find((p) => p.id === rest.selectedDataViewId); + const dataView = state.kibanaDataViews.find((p) => p.id === rest.selectedDataViewId); // dedupe because these could come from a silly url or pre 8.0 timeline - const dedupePatterns = [...new Set(rest.selectedPatterns)]; - let selectedPatterns = - dataView != null + const dedupePatterns = ensurePatternFormat(rest.selectedPatterns); + let missingPatterns: string[] = []; + // check for missing patterns against default data view only + if (dataView == null || dataView.id === state.defaultDataView.id) { + const dedupeAllDefaultPatterns = ensurePatternFormat( + (dataView ?? state.defaultDataView).title.split(',') + ); + missingPatterns = dedupePatterns.filter( + (pattern) => !dedupeAllDefaultPatterns.includes(pattern) + ); + } + const selectedPatterns = + // shouldValidateSelectedPatterns is false when upgrading from + // legacy pre-8.0 timeline index patterns to data view. + shouldValidateSelectedPatterns && dataView != null && missingPatterns.length === 0 ? dedupePatterns.filter( (pattern) => - // Typescript is being mean and telling me dataView could be undefined here - // so redoing the dataView != null check (dataView != null && dataView.patternList.includes(pattern)) || // this is a hack, but sometimes signal index is deleted and is getting regenerated. it gets set before it is put in the dataView state.signalIndexName == null || state.signalIndexName === pattern ) - : // 7.16 -> 8.0 this will get hit because dataView == null + : // don't remove non-existing patterns, they were saved in the first place in timeline + // but removed from the security data view + // or its a legacy pre-8.0 timeline dedupePatterns; - if (selectedPatterns.length > 0 && dataView == null) { - // we have index patterns, but not a data view id - // find out if we have these index patterns in the defaultDataView - const areAllPatternsInDefault = selectedPatterns.every( - (pattern) => state.defaultDataView.title.indexOf(pattern) > -1 - ); - if (areAllPatternsInDefault) { - dataView = state.defaultDataView; - selectedPatterns = selectedPatterns.filter( - (pattern) => dataView != null && dataView.patternList.includes(pattern) - ); - } - } - // TO DO: Steph/sourcerer If dataView is still undefined here, create temporary dataView - // and prompt user to go create this dataView - // currently UI will take the undefined dataView and default to defaultDataView anyways - // this is a "strategically merged" bug ;) - // https://github.com/elastic/security-team/issues/1921 - return { [id]: { ...state.sourcererScopes[id], ...rest, selectedDataViewId: dataView?.id ?? null, selectedPatterns, - ...(isEmpty(selectedPatterns) + missingPatterns, + // if in timeline, allow for empty in case pattern was deleted + // need flow for this + ...(isEmpty(selectedPatterns) && id !== SourcererScopeName.timeline ? { selectedPatterns: getScopePatternListSelection( dataView ?? state.defaultDataView, @@ -97,17 +101,17 @@ export const validateSelectedPatterns = ( }; interface CheckIfIndicesExistParams { + patternList: sourcererModel.SourcererDataView['patternList']; scopeId: sourcererModel.SourcererScopeName; signalIndexName: string | null; - sourcererDataView: sourcererModel.SourcererDataView; } export const checkIfIndicesExist = ({ + patternList, scopeId, signalIndexName, - sourcererDataView, }: CheckIfIndicesExistParams) => scopeId === SourcererScopeName.detections - ? sourcererDataView.patternList.includes(`${signalIndexName}`) + ? patternList.includes(`${signalIndexName}`) : scopeId === SourcererScopeName.default - ? sourcererDataView.patternList.filter((i) => i !== signalIndexName).length > 0 - : sourcererDataView.patternList.length > 0; + ? patternList.filter((i) => i !== signalIndexName).length > 0 + : patternList.length > 0; diff --git a/x-pack/plugins/security_solution/public/common/store/sourcerer/model.ts b/x-pack/plugins/security_solution/public/common/store/sourcerer/model.ts index a22a04d025d19..61377662fa812 100644 --- a/x-pack/plugins/security_solution/public/common/store/sourcerer/model.ts +++ b/x-pack/plugins/security_solution/public/common/store/sourcerer/model.ts @@ -29,10 +29,15 @@ export interface SourcererScope { id: SourcererScopeName; /** is an update being made to the sourcerer data view */ loading: boolean; - /** selected data view id */ - selectedDataViewId: string; + /** selected data view id, null if it is legacy index patterns*/ + selectedDataViewId: string | null; /** selected patterns within the data view */ selectedPatterns: string[]; + /** if has length, + * id === SourcererScopeName.timeline + * selectedDataViewId === null OR defaultDataView.id + * saved timeline has pattern that is not in the default */ + missingPatterns: string[]; } export type SourcererScopeById = Record; @@ -54,6 +59,7 @@ export interface KibanaDataView { * DataView from Kibana + timelines/index_fields enhanced field data */ export interface SourcererDataView extends KibanaDataView { + id: string; /** we need this for @timestamp data */ browserFields: BrowserFields; /** we need this for @timestamp data */ @@ -75,7 +81,7 @@ export interface SourcererDataView extends KibanaDataView { */ export interface SelectedDataView { browserFields: SourcererDataView['browserFields']; - dataViewId: SourcererDataView['id']; + dataViewId: string | null; // null if legacy pre-8.0 timeline docValueFields: SourcererDataView['docValueFields']; /** * DataViewBase with enhanced index fields used in timelines @@ -88,8 +94,10 @@ export interface SelectedDataView { /** all active & inactive patterns from SourcererDataView['title'] */ patternList: string[]; runtimeMappings: SourcererDataView['runtimeMappings']; - /** all selected patterns from SourcererScope['selectedPatterns'] */ - selectedPatterns: string[]; + /** all selected patterns from SourcererScope['selectedPatterns'] */ + selectedPatterns: SourcererScope['selectedPatterns']; + // active patterns when dataViewId == null + activePatterns?: string[]; } /** @@ -97,7 +105,7 @@ export interface SelectedDataView { */ export interface SourcererModel { /** default security-solution data view */ - defaultDataView: SourcererDataView & { error?: unknown }; + defaultDataView: SourcererDataView & { id: string; error?: unknown }; /** all Kibana data views, including security-solution */ kibanaDataViews: SourcererDataView[]; /** security solution signals index name */ @@ -115,8 +123,9 @@ export type SourcererUrlState = Partial<{ export const initSourcererScope: Omit = { loading: false, - selectedDataViewId: '', + selectedDataViewId: null, selectedPatterns: [], + missingPatterns: [], }; export const initDataView = { browserFields: EMPTY_BROWSER_FIELDS, diff --git a/x-pack/plugins/security_solution/public/common/store/sourcerer/reducer.ts b/x-pack/plugins/security_solution/public/common/store/sourcerer/reducer.ts index e1747a6786cdb..648ba354f29d9 100644 --- a/x-pack/plugins/security_solution/public/common/store/sourcerer/reducer.ts +++ b/x-pack/plugins/security_solution/public/common/store/sourcerer/reducer.ts @@ -72,13 +72,17 @@ export const sourcererReducer = reducerWithInitialState(initialSourcererState) }), }, })) - .case(setSelectedDataView, (state, payload) => ({ - ...state, - sourcererScopes: { - ...state.sourcererScopes, - ...validateSelectedPatterns(state, payload), - }, - })) + .case(setSelectedDataView, (state, payload) => { + const { shouldValidateSelectedPatterns = true, ...patternsInfo } = payload; + + return { + ...state, + sourcererScopes: { + ...state.sourcererScopes, + ...validateSelectedPatterns(state, patternsInfo, shouldValidateSelectedPatterns), + }, + }; + }) .case(setDataView, (state, dataView) => ({ ...state, ...(dataView.id === state.defaultDataView.id diff --git a/x-pack/plugins/security_solution/public/common/store/sourcerer/selectors.ts b/x-pack/plugins/security_solution/public/common/store/sourcerer/selectors.ts index b72d7bfde2dcc..8c0b1ecf6f627 100644 --- a/x-pack/plugins/security_solution/public/common/store/sourcerer/selectors.ts +++ b/x-pack/plugins/security_solution/public/common/store/sourcerer/selectors.ts @@ -26,8 +26,11 @@ export const sourcererDefaultDataViewSelector = ({ sourcerer, }: State): SourcererModel['defaultDataView'] => sourcerer.defaultDataView; -export const dataViewSelector = ({ sourcerer }: State, id: string): SourcererDataView => - sourcerer.kibanaDataViews.find((dataView) => dataView.id === id) ?? sourcerer.defaultDataView; +export const dataViewSelector = ( + { sourcerer }: State, + id: string | null +): SourcererDataView | undefined => + sourcerer.kibanaDataViews.find((dataView) => dataView.id === id); export const sourcererScopeIdSelector = ( { sourcerer }: State, @@ -54,29 +57,48 @@ export const sourcererDataViewSelector = () => createSelector(dataViewSelector, (dataView) => dataView); export interface SourcererScopeSelector extends Omit { - sourcererDataView: SourcererDataView; + selectedDataView: SourcererDataView | undefined; sourcererScope: SourcererScope; } -export const getSourcererScopeSelector = () => { +export const getSourcererDataViewsSelector = () => { const getKibanaDataViewsSelector = kibanaDataViewsSelector(); const getDefaultDataViewSelector = defaultDataViewSelector(); const getSignalIndexNameSelector = signalIndexNameSelector(); - const getSourcererDataViewSelector = sourcererDataViewSelector(); - const getScopeSelector = scopeIdSelector(); - - return (state: State, scopeId: SourcererScopeName): SourcererScopeSelector => { + return (state: State): Omit => { const kibanaDataViews = getKibanaDataViewsSelector(state); const defaultDataView = getDefaultDataViewSelector(state); const signalIndexName = getSignalIndexNameSelector(state); - const scope = getScopeSelector(state, scopeId); - const sourcererDataView = getSourcererDataViewSelector(state, scope.selectedDataViewId); return { defaultDataView, kibanaDataViews, signalIndexName, - sourcererDataView, + }; + }; +}; + +/** + * Attn Future Developer + * Access sourcererScope.selectedPatterns from + * hook useSourcererDataView in `common/containers/sourcerer/index` + * in order to get exclude patterns for searches + * Access sourcererScope.selectedPatterns + * from this function for display purposes only + * */ +export const getSourcererScopeSelector = () => { + const getDataViewsSelector = getSourcererDataViewsSelector(); + const getSourcererDataViewSelector = sourcererDataViewSelector(); + const getScopeSelector = scopeIdSelector(); + + return (state: State, scopeId: SourcererScopeName): SourcererScopeSelector => { + const dataViews = getDataViewsSelector(state); + const scope = getScopeSelector(state, scopeId); + const selectedDataView = getSourcererDataViewSelector(state, scope.selectedDataViewId); + + return { + ...dataViews, + selectedDataView, sourcererScope: scope, }; }; diff --git a/x-pack/plugins/security_solution/public/detections/components/alerts_table/actions.test.tsx b/x-pack/plugins/security_solution/public/detections/components/alerts_table/actions.test.tsx index 73af793275122..a7d443acc3daf 100644 --- a/x-pack/plugins/security_solution/public/detections/components/alerts_table/actions.test.tsx +++ b/x-pack/plugins/security_solution/public/detections/components/alerts_table/actions.test.tsx @@ -140,7 +140,7 @@ describe('alert actions', () => { ], defaultColumns: defaultHeaders, dataProviders: [], - dataViewId: '', + dataViewId: null, dateRange: { end: '2018-11-05T19:03:25.937Z', start: '2018-11-05T18:58:25.937Z', diff --git a/x-pack/plugins/security_solution/public/detections/components/alerts_table/default_config.test.tsx b/x-pack/plugins/security_solution/public/detections/components/alerts_table/default_config.test.tsx index 13e93604863b4..aab6cabdb3a93 100644 --- a/x-pack/plugins/security_solution/public/detections/components/alerts_table/default_config.test.tsx +++ b/x-pack/plugins/security_solution/public/detections/components/alerts_table/default_config.test.tsx @@ -7,7 +7,7 @@ import { ExistsFilter, Filter } from '@kbn/es-query'; import { - buildAlertsRuleIdFilter, + buildAlertsFilter, buildAlertStatusesFilter, buildAlertStatusFilter, buildThreatMatchFilter, @@ -18,21 +18,21 @@ jest.mock('./actions'); describe('alerts default_config', () => { describe('buildAlertsRuleIdFilter', () => { test('given a rule id this will return an array with a single filter', () => { - const filters: Filter[] = buildAlertsRuleIdFilter('rule-id-1'); + const filters: Filter[] = buildAlertsFilter('rule-id-1'); const expectedFilter: Filter = { meta: { alias: null, negate: false, disabled: false, type: 'phrase', - key: 'kibana.alert.rule.uuid', + key: 'kibana.alert.rule.rule_id', params: { query: 'rule-id-1', }, }, query: { match_phrase: { - 'kibana.alert.rule.uuid': 'rule-id-1', + 'kibana.alert.rule.rule_id': 'rule-id-1', }, }, }; diff --git a/x-pack/plugins/security_solution/public/detections/components/alerts_table/default_config.tsx b/x-pack/plugins/security_solution/public/detections/components/alerts_table/default_config.tsx index a5947e45ed0f0..663d133f04b1c 100644 --- a/x-pack/plugins/security_solution/public/detections/components/alerts_table/default_config.tsx +++ b/x-pack/plugins/security_solution/public/detections/components/alerts_table/default_config.tsx @@ -6,21 +6,13 @@ */ import { - ALERT_DURATION, - ALERT_RULE_PRODUCER, - ALERT_START, + ALERT_BUILDING_BLOCK_TYPE, ALERT_WORKFLOW_STATUS, - ALERT_UUID, - ALERT_RULE_UUID, - ALERT_RULE_NAME, - ALERT_RULE_CATEGORY, - ALERT_RULE_SEVERITY, - ALERT_RULE_RISK_SCORE, + ALERT_RULE_RULE_ID, } from '@kbn/rule-data-utils/technical_field_names'; import type { Filter } from '@kbn/es-query'; -import { defaultColumnHeaderType } from '../../../timelines/components/timeline/body/column_headers/default_headers'; -import { ColumnHeaderOptions, RowRendererId } from '../../../../common/types/timeline'; +import { RowRendererId } from '../../../../common/types/timeline'; import { Status } from '../../../../common/detection_engine/schemas/common/schemas'; import { SubsetTimelineModel } from '../../../timelines/store/timeline/model'; import { timelineDefaults } from '../../../timelines/store/timeline/defaults'; @@ -34,12 +26,12 @@ export const buildAlertStatusFilter = (status: Status): Filter[] => { should: [ { term: { - 'kibana.alert.workflow_status': status, + [ALERT_WORKFLOW_STATUS]: status, }, }, { term: { - 'kibana.alert.workflow_status': 'in-progress', + [ALERT_WORKFLOW_STATUS]: 'in-progress', }, }, ], @@ -47,7 +39,7 @@ export const buildAlertStatusFilter = (status: Status): Filter[] => { } : { term: { - 'kibana.alert.workflow_status': status, + [ALERT_WORKFLOW_STATUS]: status, }, }; @@ -58,7 +50,7 @@ export const buildAlertStatusFilter = (status: Status): Filter[] => { negate: false, disabled: false, type: 'phrase', - key: 'kibana.alert.workflow_status', + key: ALERT_WORKFLOW_STATUS, params: { query: status, }, @@ -76,7 +68,7 @@ export const buildAlertStatusesFilter = (statuses: Status[]): Filter[] => { bool: { should: statuses.map((status) => ({ term: { - 'kibana.alert.workflow_status': status, + [ALERT_WORKFLOW_STATUS]: status, }, })), }, @@ -94,8 +86,15 @@ export const buildAlertStatusesFilter = (statuses: Status[]): Filter[] => { ]; }; -export const buildAlertsRuleIdFilter = (ruleId: string | null): Filter[] => - ruleId +/** + * Builds Kuery filter for fetching alerts for a specific rule. Takes the rule's + * static id, i.e. `rule.ruleId` (not rule.id), so that alerts for _all + * historical instances_ of the rule are returned. + * + * @param ruleStaticId Rule's static id: `rule.ruleId` + */ +export const buildAlertsFilter = (ruleStaticId: string | null): Filter[] => + ruleStaticId ? [ { meta: { @@ -103,14 +102,14 @@ export const buildAlertsRuleIdFilter = (ruleId: string | null): Filter[] => negate: false, disabled: false, type: 'phrase', - key: 'kibana.alert.rule.uuid', + key: ALERT_RULE_RULE_ID, params: { - query: ruleId, + query: ruleStaticId, }, }, query: { match_phrase: { - 'kibana.alert.rule.uuid': ruleId, + [ALERT_RULE_RULE_ID]: ruleStaticId, }, }, }, @@ -127,10 +126,10 @@ export const buildShowBuildingBlockFilter = (showBuildingBlockAlerts: boolean): negate: true, disabled: false, type: 'exists', - key: 'kibana.alert.building_block_type', + key: ALERT_BUILDING_BLOCK_TYPE, value: 'exists', }, - query: { exists: { field: 'kibana.alert.building_block_type' } }, + query: { exists: { field: ALERT_BUILDING_BLOCK_TYPE } }, }, ]; @@ -183,121 +182,3 @@ export const requiredFieldsForActions = [ 'host.os.family', 'event.code', ]; - -// TODO: Once we are past experimental phase this code should be removed -export const buildAlertStatusFilterRuleRegistry = (status: Status): Filter[] => { - const combinedQuery = - status === 'acknowledged' - ? { - bool: { - should: [ - { - term: { - [ALERT_WORKFLOW_STATUS]: status, - }, - }, - { - term: { - [ALERT_WORKFLOW_STATUS]: 'in-progress', - }, - }, - ], - }, - } - : { - term: { - [ALERT_WORKFLOW_STATUS]: status, - }, - }; - - return [ - { - meta: { - alias: null, - negate: false, - disabled: false, - type: 'phrase', - key: ALERT_WORKFLOW_STATUS, - params: { - query: status, - }, - }, - query: combinedQuery, - }, - ]; -}; - -// TODO: Once we are past experimental phase this code should be removed -export const buildAlertStatusesFilterRuleRegistry = (statuses: Status[]): Filter[] => { - const combinedQuery = { - bool: { - should: statuses.map((status) => ({ - term: { - [ALERT_WORKFLOW_STATUS]: status, - }, - })), - }, - }; - - return [ - { - meta: { - alias: null, - negate: false, - disabled: false, - }, - query: combinedQuery, - }, - ]; -}; - -export const buildShowBuildingBlockFilterRuleRegistry = ( - showBuildingBlockAlerts: boolean -): Filter[] => - showBuildingBlockAlerts - ? [] - : [ - { - meta: { - alias: null, - negate: true, - disabled: false, - type: 'exists', - key: 'kibana.alert.building_block_type', - value: 'exists', - }, - query: { exists: { field: 'kibana.alert.building_block_type' } }, - }, - ]; - -export const requiredFieldMappingsForActionsRuleRegistry = { - '@timestamp': '@timestamp', - 'event.kind': 'event.kind', - 'rule.severity': ALERT_RULE_SEVERITY, - 'rule.risk_score': ALERT_RULE_RISK_SCORE, - 'alert.uuid': ALERT_UUID, - 'alert.start': ALERT_START, - 'event.action': 'event.action', - 'alert.workflow_status': ALERT_WORKFLOW_STATUS, - 'alert.duration.us': ALERT_DURATION, - 'rule.uuid': ALERT_RULE_UUID, - 'rule.name': ALERT_RULE_NAME, - 'rule.category': ALERT_RULE_CATEGORY, - producer: ALERT_RULE_PRODUCER, - tags: 'tags', -}; - -export const alertsHeadersRuleRegistry: ColumnHeaderOptions[] = Object.entries( - requiredFieldMappingsForActionsRuleRegistry -).map(([alias, field]) => ({ - columnHeaderType: defaultColumnHeaderType, - displayAsText: alias, - id: field, -})); - -export const alertsDefaultModelRuleRegistry: SubsetTimelineModel = { - ...timelineDefaults, - columns: alertsHeadersRuleRegistry, - showCheckboxes: true, - excludedRowRendererIds: Object.values(RowRendererId), -}; diff --git a/x-pack/plugins/security_solution/public/detections/components/alerts_table/index.tsx b/x-pack/plugins/security_solution/public/detections/components/alerts_table/index.tsx index bbab423738ca0..256a063c44158 100644 --- a/x-pack/plugins/security_solution/public/detections/components/alerts_table/index.tsx +++ b/x-pack/plugins/security_solution/public/detections/components/alerts_table/index.tsx @@ -40,9 +40,7 @@ import { updateAlertStatusAction } from './actions'; import { AditionalFiltersAction, AlertsUtilityBar } from './alerts_utility_bar'; import { alertsDefaultModel, - alertsDefaultModelRuleRegistry, buildAlertStatusFilter, - buildAlertStatusFilterRuleRegistry, requiredFieldsForActions, } from './default_config'; import { buildTimeRangeFilter } from './helpers'; @@ -106,8 +104,6 @@ export const AlertsTableComponent: React.FC = ({ const kibana = useKibana(); const [, dispatchToaster] = useStateToaster(); const { addWarning } = useAppToasts(); - // TODO: Once we are past experimental phase this code should be removed - const ruleRegistryEnabled = useIsExperimentalFeatureEnabled('ruleRegistryEnabled'); const ACTION_BUTTON_COUNT = 4; const getGlobalQuery = useCallback( @@ -247,14 +243,9 @@ export const AlertsTableComponent: React.FC = ({ refetchQuery: inputsModel.Refetch, { status, selectedStatus }: UpdateAlertsStatusProps ) => { - // TODO: Once we are past experimental phase this code should be removed - const currentStatusFilter = ruleRegistryEnabled - ? buildAlertStatusFilterRuleRegistry(status) - : buildAlertStatusFilter(status); - await updateAlertStatusAction({ query: showClearSelectionAction - ? getGlobalQuery(currentStatusFilter)?.filterQuery + ? getGlobalQuery(buildAlertStatusFilter(status))?.filterQuery : undefined, alertIds: Object.keys(selectedEventIds), selectedStatus, @@ -273,7 +264,6 @@ export const AlertsTableComponent: React.FC = ({ showClearSelectionAction, onAlertStatusUpdateSuccess, onAlertStatusUpdateFailure, - ruleRegistryEnabled, ] ); @@ -327,24 +317,16 @@ export const AlertsTableComponent: React.FC = ({ ); const defaultFiltersMemo = useMemo(() => { - // TODO: Once we are past experimental phase this code should be removed - const alertStatusFilter = ruleRegistryEnabled - ? buildAlertStatusFilterRuleRegistry(filterGroup) - : buildAlertStatusFilter(filterGroup); + const alertStatusFilter = buildAlertStatusFilter(filterGroup); if (isEmpty(defaultFilters)) { return alertStatusFilter; } else if (defaultFilters != null && !isEmpty(defaultFilters)) { return [...defaultFilters, ...alertStatusFilter]; } - }, [defaultFilters, filterGroup, ruleRegistryEnabled]); + }, [defaultFilters, filterGroup]); const { filterManager } = useKibana().services.data.query; - // TODO: Once we are past experimental phase this code should be removed - const defaultTimelineModel = ruleRegistryEnabled - ? alertsDefaultModelRuleRegistry - : alertsDefaultModel; - const tGridEnabled = useIsExperimentalFeatureEnabled('tGridEnabled'); useEffect(() => { @@ -359,7 +341,7 @@ export const AlertsTableComponent: React.FC = ({ : c ), documentType: i18n.ALERTS_DOCUMENT_TYPE, - excludedRowRendererIds: defaultTimelineModel.excludedRowRendererIds as RowRendererId[], + excludedRowRendererIds: alertsDefaultModel.excludedRowRendererIds as RowRendererId[], filterManager, footerText: i18n.TOTAL_COUNT_OF_ALERTS, id: timelineId, @@ -370,7 +352,7 @@ export const AlertsTableComponent: React.FC = ({ showCheckboxes: true, }) ); - }, [dispatch, defaultTimelineModel, filterManager, tGridEnabled, timelineId]); + }, [dispatch, filterManager, tGridEnabled, timelineId]); const leadingControlColumns = useMemo(() => getDefaultControlColumn(ACTION_BUTTON_COUNT), []); @@ -383,7 +365,7 @@ export const AlertsTableComponent: React.FC = ({ additionalFilters={additionalFiltersComponent} currentFilter={filterGroup} defaultCellActions={defaultCellActions} - defaultModel={defaultTimelineModel} + defaultModel={alertsDefaultModel} end={to} entityType="events" hasAlertsCrud={hasIndexWrite && hasIndexMaintenance} diff --git a/x-pack/plugins/security_solution/public/detections/components/alerts_table/translations.ts b/x-pack/plugins/security_solution/public/detections/components/alerts_table/translations.ts index 309c6c7f9761c..1897ad45fe7ff 100644 --- a/x-pack/plugins/security_solution/public/detections/components/alerts_table/translations.ts +++ b/x-pack/plugins/security_solution/public/detections/components/alerts_table/translations.ts @@ -265,6 +265,20 @@ export const STATUS = i18n.translate( } ); +export const CHANGE_ALERT_STATUS = i18n.translate( + 'xpack.securitySolution.eventsViewer.alerts.overview.changeAlertStatus', + { + defaultMessage: 'Change alert status', + } +); + +export const CLICK_TO_CHANGE_ALERT_STATUS = i18n.translate( + 'xpack.securitySolution.eventsViewer.alerts.overview.clickToChangeAlertStatus', + { + defaultMessage: 'Click to change alert status', + } +); + export const SIGNAL_STATUS = i18n.translate( 'xpack.securitySolution.eventsViewer.alerts.overviewTable.signalStatusTitle', { @@ -278,10 +292,3 @@ export const TRIGGERED = i18n.translate( defaultMessage: 'Triggered', } ); - -export const TIMESTAMP = i18n.translate( - 'xpack.securitySolution.eventsViewer.alerts.overviewTable.timestampTitle', - { - defaultMessage: 'Timestamp', - } -); diff --git a/x-pack/plugins/security_solution/public/detections/components/rules/query_preview/custom_histogram.test.tsx b/x-pack/plugins/security_solution/public/detections/components/rules/query_preview/custom_histogram.test.tsx deleted file mode 100644 index 2e6991f87ec5a..0000000000000 --- a/x-pack/plugins/security_solution/public/detections/components/rules/query_preview/custom_histogram.test.tsx +++ /dev/null @@ -1,128 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -import React from 'react'; -import { mount } from 'enzyme'; - -import * as i18n from '../rule_preview/translations'; -import { useGlobalTime } from '../../../../common/containers/use_global_time'; -import { TestProviders } from '../../../../common/mock'; -import { PreviewCustomQueryHistogram } from './custom_histogram'; - -jest.mock('../../../../common/containers/use_global_time'); - -describe('PreviewCustomQueryHistogram', () => { - const mockSetQuery = jest.fn(); - - beforeEach(() => { - (useGlobalTime as jest.Mock).mockReturnValue({ - from: '2020-07-07T08:20:18.966Z', - isInitializing: false, - to: '2020-07-08T08:20:18.966Z', - setQuery: mockSetQuery, - }); - }); - - afterEach(() => { - jest.clearAllMocks(); - }); - - test('it renders loader when isLoading is true', () => { - const wrapper = mount( - - - - ); - - expect(wrapper.find('[data-test-subj="queryPreviewLoading"]').exists()).toBeTruthy(); - expect( - wrapper.find('[dataTestSubj="queryPreviewCustomHistogram"]').at(0).prop('subtitle') - ).toEqual(i18n.QUERY_PREVIEW_SUBTITLE_LOADING); - }); - - test('it configures data and subtitle', () => { - const wrapper = mount( - - - - ); - - expect(wrapper.find('[data-test-subj="queryPreviewLoading"]').exists()).toBeFalsy(); - expect( - wrapper.find('[dataTestSubj="queryPreviewCustomHistogram"]').at(0).prop('subtitle') - ).toEqual(i18n.QUERY_PREVIEW_TITLE(9154)); - expect(wrapper.find('[dataTestSubj="queryPreviewCustomHistogram"]').at(0).props().data).toEqual( - [ - { - key: 'hits', - value: [ - { - g: 'All others', - x: 1602247050000, - y: 2314, - }, - { - g: 'All others', - x: 1602247162500, - y: 3471, - }, - { - g: 'All others', - x: 1602247275000, - y: 3369, - }, - ], - }, - ] - ); - }); - - test('it invokes setQuery with id, inspect, isLoading and refetch', async () => { - const mockRefetch = jest.fn(); - - mount( - - - - ); - - expect(mockSetQuery).toHaveBeenCalledWith({ - id: 'queryPreviewCustomHistogramQuery', - inspect: { dsl: ['some dsl'], response: ['query response'] }, - loading: false, - refetch: mockRefetch, - }); - }); -}); diff --git a/x-pack/plugins/security_solution/public/detections/components/rules/query_preview/custom_histogram.tsx b/x-pack/plugins/security_solution/public/detections/components/rules/query_preview/custom_histogram.tsx deleted file mode 100644 index 5392b08889128..0000000000000 --- a/x-pack/plugins/security_solution/public/detections/components/rules/query_preview/custom_histogram.tsx +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -import React, { useEffect, useMemo } from 'react'; - -import * as i18n from '../rule_preview/translations'; -import { useGlobalTime } from '../../../../common/containers/use_global_time'; -import { getHistogramConfig } from '../rule_preview/helpers'; -import { - ChartSeriesConfigs, - ChartSeriesData, - ChartData, -} from '../../../../common/components/charts/common'; -import { InspectResponse } from '../../../../../public/types'; -import { inputsModel } from '../../../../common/store'; -import { PreviewHistogram } from './histogram'; - -export const ID = 'queryPreviewCustomHistogramQuery'; - -interface PreviewCustomQueryHistogramProps { - to: string; - from: string; - isLoading: boolean; - data: ChartData[]; - totalCount: number; - inspect: InspectResponse; - refetch: inputsModel.Refetch; -} - -export const PreviewCustomQueryHistogram = ({ - to, - from, - data, - totalCount, - inspect, - refetch, - isLoading, -}: PreviewCustomQueryHistogramProps) => { - const { setQuery, isInitializing } = useGlobalTime(); - - useEffect((): void => { - if (!isLoading && !isInitializing) { - setQuery({ id: ID, inspect, loading: isLoading, refetch }); - } - }, [setQuery, inspect, isLoading, isInitializing, refetch]); - - const barConfig = useMemo( - (): ChartSeriesConfigs => getHistogramConfig(to, from, true), - [from, to] - ); - - const subtitle = useMemo( - (): string => - isLoading ? i18n.QUERY_PREVIEW_SUBTITLE_LOADING : i18n.QUERY_PREVIEW_TITLE(totalCount), - [isLoading, totalCount] - ); - - const chartData = useMemo((): ChartSeriesData[] => [{ key: 'hits', value: data }], [data]); - - return ( - - ); -}; diff --git a/x-pack/plugins/security_solution/public/detections/components/rules/query_preview/eql_histogram.test.tsx b/x-pack/plugins/security_solution/public/detections/components/rules/query_preview/eql_histogram.test.tsx deleted file mode 100644 index df32223fc7ec3..0000000000000 --- a/x-pack/plugins/security_solution/public/detections/components/rules/query_preview/eql_histogram.test.tsx +++ /dev/null @@ -1,152 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -import React from 'react'; -import { mount } from 'enzyme'; - -import * as i18n from '../rule_preview/translations'; -import { useGlobalTime } from '../../../../common/containers/use_global_time'; -import { TestProviders } from '../../../../common/mock'; -import { PreviewEqlQueryHistogram } from './eql_histogram'; - -jest.mock('../../../../common/containers/use_global_time'); - -describe('PreviewEqlQueryHistogram', () => { - const mockSetQuery = jest.fn(); - - beforeEach(() => { - (useGlobalTime as jest.Mock).mockReturnValue({ - from: '2020-07-07T08:20:18.966Z', - isInitializing: false, - to: '2020-07-08T08:20:18.966Z', - setQuery: mockSetQuery, - }); - }); - - afterEach(() => { - jest.clearAllMocks(); - }); - - test('it renders loader when isLoading is true', () => { - const wrapper = mount( - - - - ); - - expect(wrapper.find('[data-test-subj="queryPreviewLoading"]').exists()).toBeTruthy(); - expect( - wrapper.find('[dataTestSubj="queryPreviewEqlHistogram"]').at(0).prop('subtitle') - ).toEqual(i18n.QUERY_PREVIEW_SUBTITLE_LOADING); - }); - - test('it configures data and subtitle', () => { - const wrapper = mount( - - - - ); - - expect(wrapper.find('[data-test-subj="queryPreviewLoading"]').exists()).toBeFalsy(); - expect( - wrapper.find('[dataTestSubj="queryPreviewEqlHistogram"]').at(0).prop('subtitle') - ).toEqual(i18n.QUERY_PREVIEW_TITLE(9154)); - expect(wrapper.find('[dataTestSubj="queryPreviewEqlHistogram"]').at(0).props().data).toEqual([ - { - key: 'hits', - value: [ - { - g: 'All others', - x: 1602247050000, - y: 2314, - }, - { - g: 'All others', - x: 1602247162500, - y: 3471, - }, - { - g: 'All others', - x: 1602247275000, - y: 3369, - }, - ], - }, - ]); - }); - - test('it invokes setQuery with id, inspect, isLoading and refetch', async () => { - const mockRefetch = jest.fn(); - - mount( - - - - ); - - expect(mockSetQuery).toHaveBeenCalledWith({ - id: 'queryEqlPreviewHistogramQuery', - inspect: { dsl: ['some dsl'], response: ['query response'] }, - loading: false, - refetch: mockRefetch, - }); - }); - - test('it displays histogram', () => { - const wrapper = mount( - - - - ); - - expect(wrapper.find('[data-test-subj="queryPreviewLoading"]').exists()).toBeFalsy(); - expect( - wrapper.find('[data-test-subj="sharedPreviewQueryNoHistogramAvailable"]').exists() - ).toBeFalsy(); - expect(wrapper.find('[data-test-subj="sharedPreviewQueryHistogram"]').exists()).toBeTruthy(); - }); -}); diff --git a/x-pack/plugins/security_solution/public/detections/components/rules/query_preview/eql_histogram.tsx b/x-pack/plugins/security_solution/public/detections/components/rules/query_preview/eql_histogram.tsx deleted file mode 100644 index eae2a593d5f25..0000000000000 --- a/x-pack/plugins/security_solution/public/detections/components/rules/query_preview/eql_histogram.tsx +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -import React, { useEffect, useMemo } from 'react'; - -import * as i18n from '../rule_preview/translations'; -import { getHistogramConfig } from '../rule_preview/helpers'; -import { - ChartSeriesData, - ChartSeriesConfigs, - ChartData, -} from '../../../../common/components/charts/common'; -import { InspectQuery } from '../../../../common/store/inputs/model'; -import { useGlobalTime } from '../../../../common/containers/use_global_time'; -import { inputsModel } from '../../../../common/store'; -import { PreviewHistogram } from './histogram'; - -export const ID = 'queryEqlPreviewHistogramQuery'; - -interface PreviewEqlQueryHistogramProps { - to: string; - from: string; - totalCount: number; - isLoading: boolean; - data: ChartData[]; - inspect: InspectQuery; - refetch: inputsModel.Refetch; -} - -export const PreviewEqlQueryHistogram = ({ - from, - to, - totalCount, - data, - inspect, - refetch, - isLoading, -}: PreviewEqlQueryHistogramProps) => { - const { setQuery, isInitializing } = useGlobalTime(); - - useEffect((): void => { - if (!isInitializing) { - setQuery({ id: ID, inspect, loading: false, refetch }); - } - }, [setQuery, inspect, isInitializing, refetch]); - - const barConfig = useMemo((): ChartSeriesConfigs => getHistogramConfig(to, from), [from, to]); - - const subtitle = useMemo( - (): string => - isLoading ? i18n.QUERY_PREVIEW_SUBTITLE_LOADING : i18n.QUERY_PREVIEW_TITLE(totalCount), - [isLoading, totalCount] - ); - - const chartData = useMemo((): ChartSeriesData[] => [{ key: 'hits', value: data }], [data]); - - return ( - - ); -}; diff --git a/x-pack/plugins/security_solution/public/detections/components/rules/query_preview/histogram.test.tsx b/x-pack/plugins/security_solution/public/detections/components/rules/query_preview/histogram.test.tsx deleted file mode 100644 index 500a7f3d0e3db..0000000000000 --- a/x-pack/plugins/security_solution/public/detections/components/rules/query_preview/histogram.test.tsx +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -import React from 'react'; -import { mount } from 'enzyme'; - -import { TestProviders } from '../../../../common/mock'; -import { PreviewHistogram } from './histogram'; -import { getHistogramConfig } from '../rule_preview/helpers'; - -describe('PreviewHistogram', () => { - test('it renders loading icon if "isLoading" is true', () => { - const wrapper = mount( - - - - ); - - expect(wrapper.find('[data-test-subj="queryPreviewLoading"]').exists()).toBeTruthy(); - expect(wrapper.find('[data-test-subj="sharedPreviewQueryHistogram"]').exists()).toBeFalsy(); - }); - - test('it renders chart if "isLoading" is true', () => { - const wrapper = mount( - - - - ); - - expect(wrapper.find('[data-test-subj="queryPreviewLoading"]').exists()).toBeFalsy(); - expect(wrapper.find('[data-test-subj="sharedPreviewQueryHistogram"]').exists()).toBeTruthy(); - }); -}); diff --git a/x-pack/plugins/security_solution/public/detections/components/rules/query_preview/histogram.tsx b/x-pack/plugins/security_solution/public/detections/components/rules/query_preview/histogram.tsx deleted file mode 100644 index 3391ed1c5560a..0000000000000 --- a/x-pack/plugins/security_solution/public/detections/components/rules/query_preview/histogram.tsx +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -import React from 'react'; -import { EuiFlexGroup, EuiFlexItem, EuiText, EuiSpacer, EuiLoadingChart } from '@elastic/eui'; -import styled from 'styled-components'; - -import { BarChart } from '../../../../common/components/charts/barchart'; -import { Panel } from '../../../../common/components/panel'; -import { HeaderSection } from '../../../../common/components/header_section'; -import { ChartSeriesData, ChartSeriesConfigs } from '../../../../common/components/charts/common'; - -const LoadingChart = styled(EuiLoadingChart)` - display: block; - margin: 0 auto; -`; - -interface PreviewHistogramProps { - id: string; - data: ChartSeriesData[]; - dataTestSubj?: string; - barConfig: ChartSeriesConfigs; - title: string; - subtitle: string; - disclaimer: string; - isLoading: boolean; -} - -export const PreviewHistogram = ({ - id, - data, - dataTestSubj, - barConfig, - title, - subtitle, - disclaimer, - isLoading, -}: PreviewHistogramProps) => { - return ( - <> - - - - - - - {isLoading ? ( - - ) : ( - - )} - - - <> - - -

{disclaimer}

-
- -
-
-
- - ); -}; diff --git a/x-pack/plugins/security_solution/public/detections/components/rules/query_preview/index.test.tsx b/x-pack/plugins/security_solution/public/detections/components/rules/query_preview/index.test.tsx deleted file mode 100644 index f14bd5f7354d9..0000000000000 --- a/x-pack/plugins/security_solution/public/detections/components/rules/query_preview/index.test.tsx +++ /dev/null @@ -1,502 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -import React from 'react'; -import { of } from 'rxjs'; -import { ThemeProvider } from 'styled-components'; -import { mount } from 'enzyme'; - -import { TestProviders } from '../../../../common/mock'; -import { useKibana } from '../../../../common/lib/kibana'; -import { PreviewQuery } from './'; -import { getMockEqlResponse } from '../../../../common/hooks/eql/eql_search_response.mock'; -import { useMatrixHistogram } from '../../../../common/containers/matrix_histogram'; -import { useEqlPreview } from '../../../../common/hooks/eql/'; -import { getMockTheme } from '../../../../common/lib/kibana/kibana_react.mock'; -import type { FilterMeta } from '@kbn/es-query'; - -const mockTheme = getMockTheme({ - eui: { - euiSuperDatePickerWidth: '180px', - }, -}); - -jest.mock('../../../../common/lib/kibana'); -jest.mock('../../../../common/containers/matrix_histogram'); -jest.mock('../../../../common/hooks/eql/'); - -describe('PreviewQuery', () => { - beforeEach(() => { - useKibana().services.notifications.toasts.addError = jest.fn(); - - useKibana().services.notifications.toasts.addWarning = jest.fn(); - - (useMatrixHistogram as jest.Mock).mockReturnValue([ - false, - { - inspect: { dsl: [], response: [] }, - totalCount: 1, - refetch: jest.fn(), - data: [], - buckets: [], - }, - (useKibana().services.data.search.search as jest.Mock).mockReturnValue( - of(getMockEqlResponse()) - ), - ]); - - (useEqlPreview as jest.Mock).mockReturnValue([ - false, - (useKibana().services.data.search.search as jest.Mock).mockReturnValue( - of(getMockEqlResponse()) - ), - { - inspect: { dsl: [], response: [] }, - totalCount: 1, - refetch: jest.fn(), - data: [], - buckets: [], - }, - ]); - }); - - afterEach(() => { - jest.clearAllMocks(); - }); - - test('it renders timeframe select and preview button on render', () => { - const wrapper = mount( - - - - ); - - expect(wrapper.find('[data-test-subj="queryPreviewSelect"]').exists()).toBeTruthy(); - expect( - wrapper.find('[data-test-subj="queryPreviewButton"] button').props().disabled - ).toBeFalsy(); - expect(wrapper.find('[data-test-subj="queryPreviewCustomHistogram"]').exists()).toBeFalsy(); - expect(wrapper.find('[data-test-subj="thresholdQueryPreviewHistogram"]').exists()).toBeFalsy(); - expect(wrapper.find('[data-test-subj="queryPreviewEqlHistogram"]').exists()).toBeFalsy(); - }); - - test('it renders preview button disabled if "isDisabled" is true', () => { - const wrapper = mount( - - - - ); - - expect( - wrapper.find('[data-test-subj="queryPreviewButton"] button').props().disabled - ).toBeTruthy(); - }); - - test('it renders preview button disabled if "query" is undefined', () => { - const wrapper = mount( - - - - ); - - expect( - wrapper.find('[data-test-subj="queryPreviewButton"] button').props().disabled - ).toBeTruthy(); - }); - - test('it renders preview button enabled if query exists', () => { - const wrapper = mount( - - - - ); - - expect( - wrapper.find('[data-test-subj="queryPreviewButton"] button').props().disabled - ).toBeFalsy(); - }); - - test('it renders preview button enabled if no query exists but filters do exist', () => { - const wrapper = mount( - - - - ); - - expect( - wrapper.find('[data-test-subj="queryPreviewButton"] button').props().disabled - ).toBeFalsy(); - }); - - test('it renders query histogram when rule type is query and preview button clicked', () => { - const wrapper = mount( - - - - ); - - wrapper.find('[data-test-subj="queryPreviewButton"] button').at(0).simulate('click'); - - const mockCalls = (useKibana().services.data.search.search as jest.Mock).mock.calls; - - expect(mockCalls.length).toEqual(1); - expect(wrapper.find('[data-test-subj="queryPreviewCustomHistogram"]').exists()).toBeTruthy(); - expect(wrapper.find('[data-test-subj="thresholdQueryPreviewHistogram"]').exists()).toBeFalsy(); - expect(wrapper.find('[data-test-subj="queryPreviewEqlHistogram"]').exists()).toBeFalsy(); - }); - - test('it renders noise warning when rule type is query, timeframe is last hour and hit average is greater than 1/hour', async () => { - const wrapper = mount( - - - - ); - - (useMatrixHistogram as jest.Mock).mockReturnValue([ - false, - { - inspect: { dsl: [], response: [] }, - totalCount: 2, - refetch: jest.fn(), - data: [], - buckets: [], - }, - (useKibana().services.data.search.search as jest.Mock).mockReturnValue( - of(getMockEqlResponse()) - ), - ]); - - wrapper.find('[data-test-subj="queryPreviewButton"] button').at(0).simulate('click'); - - expect(wrapper.find('[data-test-subj="previewQueryWarning"]').exists()).toBeTruthy(); - }); - - test('it renders query histogram when rule type is saved_query and preview button clicked', () => { - const wrapper = mount( - - - - ); - - wrapper.find('[data-test-subj="queryPreviewButton"] button').at(0).simulate('click'); - - const mockCalls = (useKibana().services.data.search.search as jest.Mock).mock.calls; - - expect(mockCalls.length).toEqual(1); - expect(wrapper.find('[data-test-subj="queryPreviewCustomHistogram"]').exists()).toBeTruthy(); - expect(wrapper.find('[data-test-subj="thresholdQueryPreviewHistogram"]').exists()).toBeFalsy(); - expect(wrapper.find('[data-test-subj="queryPreviewEqlHistogram"]').exists()).toBeFalsy(); - }); - - test('it renders eql histogram when preview button clicked and rule type is eql', () => { - const wrapper = mount( - - - - ); - - wrapper.find('[data-test-subj="queryPreviewButton"] button').at(0).simulate('click'); - - const mockCalls = (useKibana().services.data.search.search as jest.Mock).mock.calls; - - expect(mockCalls.length).toEqual(1); - expect(wrapper.find('[data-test-subj="queryPreviewCustomHistogram"]').exists()).toBeFalsy(); - expect(wrapper.find('[data-test-subj="thresholdQueryPreviewHistogram"]').exists()).toBeFalsy(); - expect(wrapper.find('[data-test-subj="queryPreviewEqlHistogram"]').exists()).toBeTruthy(); - }); - - test('it renders noise warning when rule type is eql, timeframe is last hour and hit average is greater than 1/hour', async () => { - const wrapper = mount( - - - - ); - - (useEqlPreview as jest.Mock).mockReturnValue([ - false, - (useKibana().services.data.search.search as jest.Mock).mockReturnValue( - of(getMockEqlResponse()) - ), - { - inspect: { dsl: [], response: [] }, - totalCount: 2, - refetch: jest.fn(), - data: [], - buckets: [], - }, - ]); - - wrapper.find('[data-test-subj="queryPreviewButton"] button').at(0).simulate('click'); - - expect(wrapper.find('[data-test-subj="previewQueryWarning"]').exists()).toBeTruthy(); - }); - - test('it renders threshold histogram when preview button clicked, rule type is threshold, and threshold field is defined', () => { - const wrapper = mount( - - - - ); - - (useMatrixHistogram as jest.Mock).mockReturnValue([ - false, - { - inspect: { dsl: [], response: [] }, - totalCount: 500, - refetch: jest.fn(), - data: [], - buckets: [{ key: 'siem-kibana', doc_count: 500 }], - }, - (useKibana().services.data.search.search as jest.Mock).mockReturnValue( - of(getMockEqlResponse()) - ), - ]); - - wrapper.find('[data-test-subj="queryPreviewButton"] button').at(0).simulate('click'); - - const mockCalls = (useKibana().services.data.search.search as jest.Mock).mock.calls; - - expect(mockCalls.length).toEqual(1); - expect(wrapper.find('[data-test-subj="previewQueryWarning"]').exists()).toBeFalsy(); - expect(wrapper.find('[data-test-subj="queryPreviewCustomHistogram"]').exists()).toBeFalsy(); - expect(wrapper.find('[data-test-subj="thresholdQueryPreviewHistogram"]').exists()).toBeTruthy(); - expect(wrapper.find('[data-test-subj="queryPreviewEqlHistogram"]').exists()).toBeFalsy(); - }); - - test('it renders noise warning when rule type is threshold, and threshold field is defined, timeframe is last hour and hit average is greater than 1/hour', async () => { - const wrapper = mount( - - - - ); - - (useMatrixHistogram as jest.Mock).mockReturnValue([ - false, - { - inspect: { dsl: [], response: [] }, - totalCount: 500, - refetch: jest.fn(), - data: [], - buckets: [ - { key: 'siem-kibana', doc_count: 200 }, - { key: 'siem-windows', doc_count: 300 }, - ], - }, - (useKibana().services.data.search.search as jest.Mock).mockReturnValue( - of(getMockEqlResponse()) - ), - ]); - - wrapper.find('[data-test-subj="queryPreviewButton"] button').at(0).simulate('click'); - - expect(wrapper.find('[data-test-subj="previewQueryWarning"]').exists()).toBeTruthy(); - }); - - test('it renders query histogram when preview button clicked, rule type is threshold, and threshold field is empty array', () => { - const wrapper = mount( - - - - ); - - wrapper.find('[data-test-subj="queryPreviewButton"] button').at(0).simulate('click'); - - const mockCalls = (useKibana().services.data.search.search as jest.Mock).mock.calls; - - expect(mockCalls.length).toEqual(1); - expect(wrapper.find('[data-test-subj="queryPreviewCustomHistogram"]').exists()).toBeTruthy(); - expect(wrapper.find('[data-test-subj="thresholdQueryPreviewHistogram"]').exists()).toBeFalsy(); - expect(wrapper.find('[data-test-subj="queryPreviewEqlHistogram"]').exists()).toBeFalsy(); - }); - - test('it renders query histogram when preview button clicked, rule type is threshold, and threshold field is empty string', () => { - const wrapper = mount( - - - - ); - - wrapper.find('[data-test-subj="queryPreviewButton"] button').at(0).simulate('click'); - - const mockCalls = (useKibana().services.data.search.search as jest.Mock).mock.calls; - - expect(mockCalls.length).toEqual(1); - expect(wrapper.find('[data-test-subj="queryPreviewCustomHistogram"]').exists()).toBeTruthy(); - expect(wrapper.find('[data-test-subj="thresholdQueryPreviewHistogram"]').exists()).toBeFalsy(); - expect(wrapper.find('[data-test-subj="queryPreviewEqlHistogram"]').exists()).toBeFalsy(); - }); - - test('it hides histogram when timeframe changes', () => { - const wrapper = mount( - - - - ); - - wrapper.find('[data-test-subj="queryPreviewButton"] button').at(0).simulate('click'); - - expect(wrapper.find('[data-test-subj="queryPreviewCustomHistogram"]').exists()).toBeTruthy(); - - wrapper - .find('[data-test-subj="queryPreviewTimeframeSelect"] select') - .at(0) - .simulate('change', { target: { value: 'd' } }); - - expect(wrapper.find('[data-test-subj="queryPreviewCustomHistogram"]').exists()).toBeFalsy(); - }); -}); diff --git a/x-pack/plugins/security_solution/public/detections/components/rules/query_preview/index.tsx b/x-pack/plugins/security_solution/public/detections/components/rules/query_preview/index.tsx deleted file mode 100644 index e7cc34ef49bef..0000000000000 --- a/x-pack/plugins/security_solution/public/detections/components/rules/query_preview/index.tsx +++ /dev/null @@ -1,362 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -import React, { Fragment, useCallback, useEffect, useMemo, useReducer, useRef } from 'react'; -import { Unit } from '@elastic/datemath'; -import styled from 'styled-components'; -import { - EuiFlexGroup, - EuiFlexItem, - EuiSelect, - EuiFormRow, - EuiButton, - EuiCallOut, - EuiText, - EuiSpacer, -} from '@elastic/eui'; -import { debounce } from 'lodash/fp'; - -import { Type } from '@kbn/securitysolution-io-ts-alerting-types'; -import * as i18n from '../rule_preview/translations'; -import { useMatrixHistogram } from '../../../../common/containers/matrix_histogram'; -import { MatrixHistogramType } from '../../../../../common/search_strategy'; -import { FieldValueQueryBar } from '../query_bar'; -import { PreviewEqlQueryHistogram } from './eql_histogram'; -import { useEqlPreview } from '../../../../common/hooks/eql/'; -import { PreviewThresholdQueryHistogram } from './threshold_histogram'; -import { formatDate } from '../../../../common/components/super_date_picker'; -import { State, queryPreviewReducer } from './reducer'; -import { isNoisy } from '../rule_preview/helpers'; -import { PreviewCustomQueryHistogram } from './custom_histogram'; -import { FieldValueThreshold } from '../threshold_input'; - -const Select = styled(EuiSelect)` - width: ${({ theme }) => theme.eui.euiSuperDatePickerWidth}; -`; - -const PreviewButton = styled(EuiButton)` - margin-left: 0; -`; - -export const initialState: State = { - timeframeOptions: [], - showHistogram: false, - timeframe: 'h', - warnings: [], - queryFilter: undefined, - toTime: '', - fromTime: '', - queryString: '', - language: 'kuery', - filters: [], - thresholdFieldExists: false, - showNonEqlHistogram: false, -}; - -export type Threshold = FieldValueThreshold | undefined; - -interface PreviewQueryProps { - dataTestSubj: string; - idAria: string; - query: FieldValueQueryBar | undefined; - index: string[]; - ruleType: Type; - threshold: Threshold; - isDisabled: boolean; -} - -export const PreviewQuery = ({ - ruleType, - dataTestSubj, - idAria, - query, - index, - threshold, - isDisabled, -}: PreviewQueryProps) => { - const [ - eqlQueryLoading, - startEql, - { - totalCount: eqlQueryTotal, - data: eqlQueryData, - refetch: eqlQueryRefetch, - inspect: eqlQueryInspect, - }, - ] = useEqlPreview(); - - const [ - { - thresholdFieldExists, - showNonEqlHistogram, - timeframeOptions, - showHistogram, - timeframe, - warnings, - queryFilter, - toTime, - fromTime, - queryString, - }, - dispatch, - ] = useReducer(queryPreviewReducer(), { - ...initialState, - toTime: formatDate('now-1h'), - fromTime: formatDate('now'), - }); - const [ - isMatrixHistogramLoading, - { inspect, totalCount: matrixHistTotal, refetch, data: matrixHistoData, buckets }, - startNonEql, - ] = useMatrixHistogram({ - errorMessage: i18n.QUERY_PREVIEW_ERROR, - endDate: fromTime, - startDate: toTime, - filterQuery: queryFilter, - indexNames: index, - includeMissingData: false, - histogramType: MatrixHistogramType.events, - stackByField: 'event.category', - threshold: ruleType === 'threshold' ? threshold : undefined, - skip: true, - }); - - const setQueryInfo = useCallback( - (queryBar: FieldValueQueryBar | undefined, indices: string[], type: Type): void => { - dispatch({ - type: 'setQueryInfo', - queryBar, - index: indices, - ruleType: type, - }); - }, - [dispatch] - ); - - const debouncedSetQueryInfo = useRef(debounce(500, setQueryInfo)); - - const setTimeframeSelect = useCallback( - (selection: Unit): void => { - dispatch({ - type: 'setTimeframeSelect', - timeframe: selection, - }); - }, - [dispatch] - ); - - const setRuleTypeChange = useCallback( - (type: Type): void => { - dispatch({ - type: 'setResetRuleTypeChange', - ruleType: type, - }); - }, - [dispatch] - ); - - const setWarnings = useCallback( - (yikes: string[]): void => { - dispatch({ - type: 'setWarnings', - warnings: yikes, - }); - }, - [dispatch] - ); - - const setNoiseWarning = useCallback((): void => { - dispatch({ - type: 'setNoiseWarning', - }); - }, [dispatch]); - - const setShowHistogram = useCallback( - (show: boolean): void => { - dispatch({ - type: 'setShowHistogram', - show, - }); - }, - [dispatch] - ); - - const setThresholdValues = useCallback( - (thresh: Threshold, type: Type): void => { - dispatch({ - type: 'setThresholdQueryVals', - threshold: thresh, - ruleType: type, - }); - }, - [dispatch] - ); - - useEffect(() => { - debouncedSetQueryInfo.current(query, index, ruleType); - }, [index, query, ruleType]); - - useEffect((): void => { - setThresholdValues(threshold, ruleType); - }, [setThresholdValues, threshold, ruleType]); - - useEffect((): void => { - setRuleTypeChange(ruleType); - }, [ruleType, setRuleTypeChange]); - - useEffect((): void => { - switch (ruleType) { - case 'eql': - if (isNoisy(eqlQueryTotal, timeframe)) { - setNoiseWarning(); - } - break; - case 'threshold': - const totalHits = thresholdFieldExists ? buckets.length : matrixHistTotal; - if (isNoisy(totalHits, timeframe)) { - setNoiseWarning(); - } - break; - default: - if (isNoisy(matrixHistTotal, timeframe)) { - setNoiseWarning(); - } - } - }, [ - timeframe, - matrixHistTotal, - eqlQueryTotal, - ruleType, - setNoiseWarning, - thresholdFieldExists, - buckets.length, - ]); - - const handlePreviewEqlQuery = useCallback( - (to: string, from: string): void => { - startEql({ - index, - query: queryString, - from, - to, - interval: timeframe, - }); - }, - [startEql, index, queryString, timeframe] - ); - - const handleSelectPreviewTimeframe = useCallback( - ({ target: { value } }: React.ChangeEvent): void => { - setTimeframeSelect(value as Unit); - }, - [setTimeframeSelect] - ); - - const handlePreviewClicked = useCallback((): void => { - const to = formatDate('now'); - const from = formatDate(`now-1${timeframe}`); - - setWarnings([]); - setShowHistogram(true); - - if (ruleType === 'eql') { - handlePreviewEqlQuery(to, from); - } else { - startNonEql(to, from); - } - }, [setWarnings, setShowHistogram, ruleType, handlePreviewEqlQuery, startNonEql, timeframe]); - - const previewButtonDisabled = useMemo(() => { - return ( - isMatrixHistogramLoading || - eqlQueryLoading || - isDisabled || - query == null || - (query != null && query.query.query === '' && query.filters.length === 0) - ); - }, [eqlQueryLoading, isDisabled, isMatrixHistogramLoading, query]); - - return ( - <> - - - -